001-squashfs.patch 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058
  1. --- a/fs/Config.in
  2. +++ b/fs/Config.in
  3. @@ -51,6 +51,14 @@ if [ "$CONFIG_JFFS2_FS" = "y" -o "$CONFI
  4. int 'JFFS2 debugging verbosity (0 = quiet, 2 = noisy)' CONFIG_JFFS2_FS_DEBUG 0
  5. fi
  6. tristate 'Compressed ROM file system support' CONFIG_CRAMFS
  7. +tristate 'Squashed file system support' CONFIG_SQUASHFS
  8. +if [ "$CONFIG_SQUASHFS" = "y" -o "$CONFIG_SQUASHFS" = "m" ] ; then
  9. +bool 'Additional options for memory constrained systems ' CONFIG_SQUASHFS_EMBEDDED
  10. +fi
  11. +if [ "$CONFIG_SQUASHFS_EMBEDDED" = "y" ] ; then
  12. +int 'Number of fragments cached' CONFIG_SQUASHFS_FRAGMENT_CACHE_SIZE 3
  13. +bool 'Use Vmalloc rather than Kmalloc' CONFIG_SQUASHFS_VMALLOC
  14. +fi
  15. bool 'Virtual memory file system support (former shm fs)' CONFIG_TMPFS
  16. define_bool CONFIG_RAMFS y
  17. --- a/fs/Makefile
  18. +++ b/fs/Makefile
  19. @@ -65,6 +65,7 @@ subdir-$(CONFIG_REISERFS_FS) += reiserfs
  20. subdir-$(CONFIG_DEVPTS_FS) += devpts
  21. subdir-$(CONFIG_SUN_OPENPROMFS) += openpromfs
  22. subdir-$(CONFIG_BEFS_FS) += befs
  23. +subdir-$(CONFIG_SQUASHFS) += squashfs
  24. subdir-$(CONFIG_JFS_FS) += jfs
  25. subdir-$(CONFIG_XFS_FS) += xfs
  26. --- /dev/null
  27. +++ b/fs/squashfs/inode.c
  28. @@ -0,0 +1,2028 @@
  29. +/*
  30. + * Squashfs - a compressed read only filesystem for Linux
  31. + *
  32. + * Copyright (c) 2002, 2003, 2004, 2005, 2006
  33. + * Phillip Lougher <[email protected]>
  34. + *
  35. + * This program is free software; you can redistribute it and/or
  36. + * modify it under the terms of the GNU General Public License
  37. + * as published by the Free Software Foundation; either version 2,
  38. + * or (at your option) any later version.
  39. + *
  40. + * This program is distributed in the hope that it will be useful,
  41. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  42. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  43. + * GNU General Public License for more details.
  44. + *
  45. + * You should have received a copy of the GNU General Public License
  46. + * along with this program; if not, write to the Free Software
  47. + * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  48. + *
  49. + * inode.c
  50. + */
  51. +
  52. +#include <linux/types.h>
  53. +#include <linux/squashfs_fs.h>
  54. +#include <linux/module.h>
  55. +#include <linux/errno.h>
  56. +#include <linux/slab.h>
  57. +#include <linux/fs.h>
  58. +#include <linux/smp_lock.h>
  59. +#include <linux/locks.h>
  60. +#include <linux/init.h>
  61. +#include <linux/dcache.h>
  62. +#include <linux/wait.h>
  63. +#include <linux/zlib.h>
  64. +#include <linux/blkdev.h>
  65. +#include <linux/vmalloc.h>
  66. +#include <asm/uaccess.h>
  67. +#include <asm/semaphore.h>
  68. +
  69. +#include "squashfs.h"
  70. +
  71. +static struct super_block *squashfs_read_super(struct super_block *, void *, int);
  72. +static void squashfs_put_super(struct super_block *);
  73. +static int squashfs_statfs(struct super_block *, struct statfs *);
  74. +static int squashfs_symlink_readpage(struct file *file, struct page *page);
  75. +static int squashfs_readpage(struct file *file, struct page *page);
  76. +static int squashfs_readpage4K(struct file *file, struct page *page);
  77. +static int squashfs_readdir(struct file *, void *, filldir_t);
  78. +static struct dentry *squashfs_lookup(struct inode *, struct dentry *);
  79. +static struct inode *squashfs_iget(struct super_block *s, squashfs_inode_t inode);
  80. +static long long read_blocklist(struct inode *inode, int index,
  81. + int readahead_blks, char *block_list,
  82. + unsigned short **block_p, unsigned int *bsize);
  83. +
  84. +static z_stream stream;
  85. +
  86. +static DECLARE_FSTYPE_DEV(squashfs_fs_type, "squashfs", squashfs_read_super);
  87. +
  88. +static unsigned char squashfs_filetype_table[] = {
  89. + DT_UNKNOWN, DT_DIR, DT_REG, DT_LNK, DT_BLK, DT_CHR, DT_FIFO, DT_SOCK
  90. +};
  91. +
  92. +static struct super_operations squashfs_ops = {
  93. + .statfs = squashfs_statfs,
  94. + .put_super = squashfs_put_super,
  95. +};
  96. +
  97. +SQSH_EXTERN struct address_space_operations squashfs_symlink_aops = {
  98. + .readpage = squashfs_symlink_readpage
  99. +};
  100. +
  101. +SQSH_EXTERN struct address_space_operations squashfs_aops = {
  102. + .readpage = squashfs_readpage
  103. +};
  104. +
  105. +SQSH_EXTERN struct address_space_operations squashfs_aops_4K = {
  106. + .readpage = squashfs_readpage4K
  107. +};
  108. +
  109. +static struct file_operations squashfs_dir_ops = {
  110. + .read = generic_read_dir,
  111. + .readdir = squashfs_readdir
  112. +};
  113. +
  114. +static struct inode_operations squashfs_dir_inode_ops = {
  115. + .lookup = squashfs_lookup
  116. +};
  117. +
  118. +static struct buffer_head *get_block_length(struct super_block *s,
  119. + int *cur_index, int *offset, int *c_byte)
  120. +{
  121. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  122. + unsigned short temp;
  123. + struct buffer_head *bh;
  124. +
  125. + if (!(bh = sb_bread(s, *cur_index)))
  126. + goto out;
  127. +
  128. + if (msblk->devblksize - *offset == 1) {
  129. + if (msblk->swap)
  130. + ((unsigned char *) &temp)[1] = *((unsigned char *)
  131. + (bh->b_data + *offset));
  132. + else
  133. + ((unsigned char *) &temp)[0] = *((unsigned char *)
  134. + (bh->b_data + *offset));
  135. + brelse(bh);
  136. + if (!(bh = sb_bread(s, ++(*cur_index))))
  137. + goto out;
  138. + if (msblk->swap)
  139. + ((unsigned char *) &temp)[0] = *((unsigned char *)
  140. + bh->b_data);
  141. + else
  142. + ((unsigned char *) &temp)[1] = *((unsigned char *)
  143. + bh->b_data);
  144. + *c_byte = temp;
  145. + *offset = 1;
  146. + } else {
  147. + if (msblk->swap) {
  148. + ((unsigned char *) &temp)[1] = *((unsigned char *)
  149. + (bh->b_data + *offset));
  150. + ((unsigned char *) &temp)[0] = *((unsigned char *)
  151. + (bh->b_data + *offset + 1));
  152. + } else {
  153. + ((unsigned char *) &temp)[0] = *((unsigned char *)
  154. + (bh->b_data + *offset));
  155. + ((unsigned char *) &temp)[1] = *((unsigned char *)
  156. + (bh->b_data + *offset + 1));
  157. + }
  158. + *c_byte = temp;
  159. + *offset += 2;
  160. + }
  161. +
  162. + if (SQUASHFS_CHECK_DATA(msblk->sblk.flags)) {
  163. + if (*offset == msblk->devblksize) {
  164. + brelse(bh);
  165. + if (!(bh = sb_bread(s, ++(*cur_index))))
  166. + goto out;
  167. + *offset = 0;
  168. + }
  169. + if (*((unsigned char *) (bh->b_data + *offset)) !=
  170. + SQUASHFS_MARKER_BYTE) {
  171. + ERROR("Metadata block marker corrupt @ %x\n",
  172. + *cur_index);
  173. + brelse(bh);
  174. + goto out;
  175. + }
  176. + (*offset)++;
  177. + }
  178. + return bh;
  179. +
  180. +out:
  181. + return NULL;
  182. +}
  183. +
  184. +
  185. +SQSH_EXTERN unsigned int squashfs_read_data(struct super_block *s, char *buffer,
  186. + long long index, unsigned int length,
  187. + long long *next_index)
  188. +{
  189. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  190. + struct buffer_head *bh[((SQUASHFS_FILE_MAX_SIZE - 1) >>
  191. + msblk->devblksize_log2) + 2];
  192. + unsigned int offset = index & ((1 << msblk->devblksize_log2) - 1);
  193. + unsigned int cur_index = index >> msblk->devblksize_log2;
  194. + int bytes, avail_bytes, b = 0, k;
  195. + char *c_buffer;
  196. + unsigned int compressed;
  197. + unsigned int c_byte = length;
  198. +
  199. + if (c_byte) {
  200. + bytes = msblk->devblksize - offset;
  201. + compressed = SQUASHFS_COMPRESSED_BLOCK(c_byte);
  202. + c_buffer = compressed ? msblk->read_data : buffer;
  203. + c_byte = SQUASHFS_COMPRESSED_SIZE_BLOCK(c_byte);
  204. +
  205. + TRACE("Block @ 0x%llx, %scompressed size %d\n", index, compressed
  206. + ? "" : "un", (unsigned int) c_byte);
  207. +
  208. + if (!(bh[0] = sb_getblk(s, cur_index)))
  209. + goto block_release;
  210. +
  211. + for (b = 1; bytes < c_byte; b++) {
  212. + if (!(bh[b] = sb_getblk(s, ++cur_index)))
  213. + goto block_release;
  214. + bytes += msblk->devblksize;
  215. + }
  216. + ll_rw_block(READ, b, bh);
  217. + } else {
  218. + if (!(bh[0] = get_block_length(s, &cur_index, &offset,
  219. + &c_byte)))
  220. + goto read_failure;
  221. +
  222. + bytes = msblk->devblksize - offset;
  223. + compressed = SQUASHFS_COMPRESSED(c_byte);
  224. + c_buffer = compressed ? msblk->read_data : buffer;
  225. + c_byte = SQUASHFS_COMPRESSED_SIZE(c_byte);
  226. +
  227. + TRACE("Block @ 0x%llx, %scompressed size %d\n", index, compressed
  228. + ? "" : "un", (unsigned int) c_byte);
  229. +
  230. + for (b = 1; bytes < c_byte; b++) {
  231. + if (!(bh[b] = sb_getblk(s, ++cur_index)))
  232. + goto block_release;
  233. + bytes += msblk->devblksize;
  234. + }
  235. + ll_rw_block(READ, b - 1, bh + 1);
  236. + }
  237. +
  238. + if (compressed)
  239. + down(&msblk->read_data_mutex);
  240. +
  241. + for (bytes = 0, k = 0; k < b; k++) {
  242. + avail_bytes = (c_byte - bytes) > (msblk->devblksize - offset) ?
  243. + msblk->devblksize - offset :
  244. + c_byte - bytes;
  245. + wait_on_buffer(bh[k]);
  246. + if (!buffer_uptodate(bh[k]))
  247. + goto block_release;
  248. + memcpy(c_buffer + bytes, bh[k]->b_data + offset, avail_bytes);
  249. + bytes += avail_bytes;
  250. + offset = 0;
  251. + brelse(bh[k]);
  252. + }
  253. +
  254. + /*
  255. + * uncompress block
  256. + */
  257. + if (compressed) {
  258. + int zlib_err;
  259. +
  260. + stream.next_in = c_buffer;
  261. + stream.avail_in = c_byte;
  262. + stream.next_out = buffer;
  263. + stream.avail_out = msblk->read_size;
  264. +
  265. + if (((zlib_err = zlib_inflateInit(&stream)) != Z_OK) ||
  266. + ((zlib_err = zlib_inflate(&stream, Z_FINISH))
  267. + != Z_STREAM_END) || ((zlib_err =
  268. + zlib_inflateEnd(&stream)) != Z_OK)) {
  269. + ERROR("zlib_fs returned unexpected result 0x%x\n",
  270. + zlib_err);
  271. + bytes = 0;
  272. + } else
  273. + bytes = stream.total_out;
  274. +
  275. + up(&msblk->read_data_mutex);
  276. + }
  277. +
  278. + if (next_index)
  279. + *next_index = index + c_byte + (length ? 0 :
  280. + (SQUASHFS_CHECK_DATA(msblk->sblk.flags)
  281. + ? 3 : 2));
  282. + return bytes;
  283. +
  284. +block_release:
  285. + while (--b >= 0)
  286. + brelse(bh[b]);
  287. +
  288. +read_failure:
  289. + ERROR("sb_bread failed reading block 0x%x\n", cur_index);
  290. + return 0;
  291. +}
  292. +
  293. +
  294. +SQSH_EXTERN int squashfs_get_cached_block(struct super_block *s, char *buffer,
  295. + long long block, unsigned int offset,
  296. + int length, long long *next_block,
  297. + unsigned int *next_offset)
  298. +{
  299. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  300. + int n, i, bytes, return_length = length;
  301. + long long next_index;
  302. +
  303. + TRACE("Entered squashfs_get_cached_block [%llx:%x]\n", block, offset);
  304. +
  305. + while ( 1 ) {
  306. + for (i = 0; i < SQUASHFS_CACHED_BLKS; i++)
  307. + if (msblk->block_cache[i].block == block)
  308. + break;
  309. +
  310. + down(&msblk->block_cache_mutex);
  311. +
  312. + if (i == SQUASHFS_CACHED_BLKS) {
  313. + /* read inode header block */
  314. + for (i = msblk->next_cache, n = SQUASHFS_CACHED_BLKS;
  315. + n ; n --, i = (i + 1) %
  316. + SQUASHFS_CACHED_BLKS)
  317. + if (msblk->block_cache[i].block !=
  318. + SQUASHFS_USED_BLK)
  319. + break;
  320. +
  321. + if (n == 0) {
  322. + wait_queue_t wait;
  323. +
  324. + init_waitqueue_entry(&wait, current);
  325. + add_wait_queue(&msblk->waitq, &wait);
  326. + set_current_state(TASK_UNINTERRUPTIBLE);
  327. + up(&msblk->block_cache_mutex);
  328. + schedule();
  329. + set_current_state(TASK_RUNNING);
  330. + remove_wait_queue(&msblk->waitq, &wait);
  331. + continue;
  332. + }
  333. + msblk->next_cache = (i + 1) % SQUASHFS_CACHED_BLKS;
  334. +
  335. + if (msblk->block_cache[i].block ==
  336. + SQUASHFS_INVALID_BLK) {
  337. + if (!(msblk->block_cache[i].data =
  338. + kmalloc(SQUASHFS_METADATA_SIZE,
  339. + GFP_KERNEL))) {
  340. + ERROR("Failed to allocate cache"
  341. + "block\n");
  342. + up(&msblk->block_cache_mutex);
  343. + goto out;
  344. + }
  345. + }
  346. +
  347. + msblk->block_cache[i].block = SQUASHFS_USED_BLK;
  348. + up(&msblk->block_cache_mutex);
  349. +
  350. + if (!(msblk->block_cache[i].length =
  351. + squashfs_read_data(s,
  352. + msblk->block_cache[i].data,
  353. + block, 0, &next_index))) {
  354. + ERROR("Unable to read cache block [%llx:%x]\n",
  355. + block, offset);
  356. + goto out;
  357. + }
  358. +
  359. + down(&msblk->block_cache_mutex);
  360. + wake_up(&msblk->waitq);
  361. + msblk->block_cache[i].block = block;
  362. + msblk->block_cache[i].next_index = next_index;
  363. + TRACE("Read cache block [%llx:%x]\n", block, offset);
  364. + }
  365. +
  366. + if (msblk->block_cache[i].block != block) {
  367. + up(&msblk->block_cache_mutex);
  368. + continue;
  369. + }
  370. +
  371. + if ((bytes = msblk->block_cache[i].length - offset) >= length) {
  372. + if (buffer)
  373. + memcpy(buffer, msblk->block_cache[i].data +
  374. + offset, length);
  375. + if (msblk->block_cache[i].length - offset == length) {
  376. + *next_block = msblk->block_cache[i].next_index;
  377. + *next_offset = 0;
  378. + } else {
  379. + *next_block = block;
  380. + *next_offset = offset + length;
  381. + }
  382. + up(&msblk->block_cache_mutex);
  383. + goto finish;
  384. + } else {
  385. + if (buffer) {
  386. + memcpy(buffer, msblk->block_cache[i].data +
  387. + offset, bytes);
  388. + buffer += bytes;
  389. + }
  390. + block = msblk->block_cache[i].next_index;
  391. + up(&msblk->block_cache_mutex);
  392. + length -= bytes;
  393. + offset = 0;
  394. + }
  395. + }
  396. +
  397. +finish:
  398. + return return_length;
  399. +out:
  400. + return 0;
  401. +}
  402. +
  403. +
  404. +static int get_fragment_location(struct super_block *s, unsigned int fragment,
  405. + long long *fragment_start_block,
  406. + unsigned int *fragment_size)
  407. +{
  408. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  409. + long long start_block =
  410. + msblk->fragment_index[SQUASHFS_FRAGMENT_INDEX(fragment)];
  411. + int offset = SQUASHFS_FRAGMENT_INDEX_OFFSET(fragment);
  412. + struct squashfs_fragment_entry fragment_entry;
  413. +
  414. + if (msblk->swap) {
  415. + struct squashfs_fragment_entry sfragment_entry;
  416. +
  417. + if (!squashfs_get_cached_block(s, (char *) &sfragment_entry,
  418. + start_block, offset,
  419. + sizeof(sfragment_entry), &start_block,
  420. + &offset))
  421. + goto out;
  422. + SQUASHFS_SWAP_FRAGMENT_ENTRY(&fragment_entry, &sfragment_entry);
  423. + } else
  424. + if (!squashfs_get_cached_block(s, (char *) &fragment_entry,
  425. + start_block, offset,
  426. + sizeof(fragment_entry), &start_block,
  427. + &offset))
  428. + goto out;
  429. +
  430. + *fragment_start_block = fragment_entry.start_block;
  431. + *fragment_size = fragment_entry.size;
  432. +
  433. + return 1;
  434. +
  435. +out:
  436. + return 0;
  437. +}
  438. +
  439. +
  440. +SQSH_EXTERN void release_cached_fragment(struct squashfs_sb_info *msblk, struct
  441. + squashfs_fragment_cache *fragment)
  442. +{
  443. + down(&msblk->fragment_mutex);
  444. + fragment->locked --;
  445. + wake_up(&msblk->fragment_wait_queue);
  446. + up(&msblk->fragment_mutex);
  447. +}
  448. +
  449. +
  450. +SQSH_EXTERN struct squashfs_fragment_cache *get_cached_fragment(struct super_block
  451. + *s, long long start_block,
  452. + int length)
  453. +{
  454. + int i, n;
  455. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  456. +
  457. + while ( 1 ) {
  458. + down(&msblk->fragment_mutex);
  459. +
  460. + for (i = 0; i < SQUASHFS_CACHED_FRAGMENTS &&
  461. + msblk->fragment[i].block != start_block; i++);
  462. +
  463. + if (i == SQUASHFS_CACHED_FRAGMENTS) {
  464. + for (i = msblk->next_fragment, n =
  465. + SQUASHFS_CACHED_FRAGMENTS; n &&
  466. + msblk->fragment[i].locked; n--, i = (i + 1) %
  467. + SQUASHFS_CACHED_FRAGMENTS);
  468. +
  469. + if (n == 0) {
  470. + wait_queue_t wait;
  471. +
  472. + init_waitqueue_entry(&wait, current);
  473. + add_wait_queue(&msblk->fragment_wait_queue,
  474. + &wait);
  475. + set_current_state(TASK_UNINTERRUPTIBLE);
  476. + up(&msblk->fragment_mutex);
  477. + schedule();
  478. + set_current_state(TASK_RUNNING);
  479. + remove_wait_queue(&msblk->fragment_wait_queue,
  480. + &wait);
  481. + continue;
  482. + }
  483. + msblk->next_fragment = (msblk->next_fragment + 1) %
  484. + SQUASHFS_CACHED_FRAGMENTS;
  485. +
  486. + if (msblk->fragment[i].data == NULL)
  487. + if (!(msblk->fragment[i].data = SQUASHFS_ALLOC
  488. + (SQUASHFS_FILE_MAX_SIZE))) {
  489. + ERROR("Failed to allocate fragment "
  490. + "cache block\n");
  491. + up(&msblk->fragment_mutex);
  492. + goto out;
  493. + }
  494. +
  495. + msblk->fragment[i].block = SQUASHFS_INVALID_BLK;
  496. + msblk->fragment[i].locked = 1;
  497. + up(&msblk->fragment_mutex);
  498. +
  499. + if (!(msblk->fragment[i].length = squashfs_read_data(s,
  500. + msblk->fragment[i].data,
  501. + start_block, length, NULL))) {
  502. + ERROR("Unable to read fragment cache block "
  503. + "[%llx]\n", start_block);
  504. + msblk->fragment[i].locked = 0;
  505. + goto out;
  506. + }
  507. +
  508. + msblk->fragment[i].block = start_block;
  509. + TRACE("New fragment %d, start block %lld, locked %d\n",
  510. + i, msblk->fragment[i].block,
  511. + msblk->fragment[i].locked);
  512. + break;
  513. + }
  514. +
  515. + msblk->fragment[i].locked++;
  516. + up(&msblk->fragment_mutex);
  517. + TRACE("Got fragment %d, start block %lld, locked %d\n", i,
  518. + msblk->fragment[i].block,
  519. + msblk->fragment[i].locked);
  520. + break;
  521. + }
  522. +
  523. + return &msblk->fragment[i];
  524. +
  525. +out:
  526. + return NULL;
  527. +}
  528. +
  529. +
  530. +static struct inode *squashfs_new_inode(struct super_block *s,
  531. + struct squashfs_base_inode_header *inodeb)
  532. +{
  533. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  534. + struct inode *i = new_inode(s);
  535. +
  536. + if (i) {
  537. + i->i_ino = inodeb->inode_number;
  538. + i->i_mtime = inodeb->mtime;
  539. + i->i_atime = inodeb->mtime;
  540. + i->i_ctime = inodeb->mtime;
  541. + i->i_uid = msblk->uid[inodeb->uid];
  542. + i->i_mode = inodeb->mode;
  543. + i->i_size = 0;
  544. + if (inodeb->guid == SQUASHFS_GUIDS)
  545. + i->i_gid = i->i_uid;
  546. + else
  547. + i->i_gid = msblk->guid[inodeb->guid];
  548. + }
  549. +
  550. + return i;
  551. +}
  552. +
  553. +
  554. +static struct inode *squashfs_iget(struct super_block *s, squashfs_inode_t inode)
  555. +{
  556. + struct inode *i;
  557. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  558. + struct squashfs_super_block *sblk = &msblk->sblk;
  559. + long long block = SQUASHFS_INODE_BLK(inode) +
  560. + sblk->inode_table_start;
  561. + unsigned int offset = SQUASHFS_INODE_OFFSET(inode);
  562. + long long next_block;
  563. + unsigned int next_offset;
  564. + union squashfs_inode_header id, sid;
  565. + struct squashfs_base_inode_header *inodeb = &id.base,
  566. + *sinodeb = &sid.base;
  567. +
  568. + TRACE("Entered squashfs_iget\n");
  569. +
  570. + if (msblk->swap) {
  571. + if (!squashfs_get_cached_block(s, (char *) sinodeb, block,
  572. + offset, sizeof(*sinodeb), &next_block,
  573. + &next_offset))
  574. + goto failed_read;
  575. + SQUASHFS_SWAP_BASE_INODE_HEADER(inodeb, sinodeb,
  576. + sizeof(*sinodeb));
  577. + } else
  578. + if (!squashfs_get_cached_block(s, (char *) inodeb, block,
  579. + offset, sizeof(*inodeb), &next_block,
  580. + &next_offset))
  581. + goto failed_read;
  582. +
  583. + switch(inodeb->inode_type) {
  584. + case SQUASHFS_FILE_TYPE: {
  585. + unsigned int frag_size;
  586. + long long frag_blk;
  587. + struct squashfs_reg_inode_header *inodep = &id.reg;
  588. + struct squashfs_reg_inode_header *sinodep = &sid.reg;
  589. +
  590. + if (msblk->swap) {
  591. + if (!squashfs_get_cached_block(s, (char *)
  592. + sinodep, block, offset,
  593. + sizeof(*sinodep), &next_block,
  594. + &next_offset))
  595. + goto failed_read;
  596. + SQUASHFS_SWAP_REG_INODE_HEADER(inodep, sinodep);
  597. + } else
  598. + if (!squashfs_get_cached_block(s, (char *)
  599. + inodep, block, offset,
  600. + sizeof(*inodep), &next_block,
  601. + &next_offset))
  602. + goto failed_read;
  603. +
  604. + frag_blk = SQUASHFS_INVALID_BLK;
  605. + if (inodep->fragment != SQUASHFS_INVALID_FRAG &&
  606. + !get_fragment_location(s,
  607. + inodep->fragment, &frag_blk, &frag_size))
  608. + goto failed_read;
  609. +
  610. + if((i = squashfs_new_inode(s, inodeb)) == NULL)
  611. + goto failed_read1;
  612. +
  613. + i->i_nlink = 1;
  614. + i->i_size = inodep->file_size;
  615. + i->i_fop = &generic_ro_fops;
  616. + i->i_mode |= S_IFREG;
  617. + i->i_blocks = ((i->i_size - 1) >> 9) + 1;
  618. + i->i_blksize = PAGE_CACHE_SIZE;
  619. + SQUASHFS_I(i)->u.s1.fragment_start_block = frag_blk;
  620. + SQUASHFS_I(i)->u.s1.fragment_size = frag_size;
  621. + SQUASHFS_I(i)->u.s1.fragment_offset = inodep->offset;
  622. + SQUASHFS_I(i)->start_block = inodep->start_block;
  623. + SQUASHFS_I(i)->u.s1.block_list_start = next_block;
  624. + SQUASHFS_I(i)->offset = next_offset;
  625. + if (sblk->block_size > 4096)
  626. + i->i_data.a_ops = &squashfs_aops;
  627. + else
  628. + i->i_data.a_ops = &squashfs_aops_4K;
  629. +
  630. + TRACE("File inode %x:%x, start_block %llx, "
  631. + "block_list_start %llx, offset %x\n",
  632. + SQUASHFS_INODE_BLK(inode), offset,
  633. + inodep->start_block, next_block,
  634. + next_offset);
  635. + break;
  636. + }
  637. + case SQUASHFS_LREG_TYPE: {
  638. + unsigned int frag_size;
  639. + long long frag_blk;
  640. + struct squashfs_lreg_inode_header *inodep = &id.lreg;
  641. + struct squashfs_lreg_inode_header *sinodep = &sid.lreg;
  642. +
  643. + if (msblk->swap) {
  644. + if (!squashfs_get_cached_block(s, (char *)
  645. + sinodep, block, offset,
  646. + sizeof(*sinodep), &next_block,
  647. + &next_offset))
  648. + goto failed_read;
  649. + SQUASHFS_SWAP_LREG_INODE_HEADER(inodep, sinodep);
  650. + } else
  651. + if (!squashfs_get_cached_block(s, (char *)
  652. + inodep, block, offset,
  653. + sizeof(*inodep), &next_block,
  654. + &next_offset))
  655. + goto failed_read;
  656. +
  657. + frag_blk = SQUASHFS_INVALID_BLK;
  658. + if (inodep->fragment != SQUASHFS_INVALID_FRAG &&
  659. + !get_fragment_location(s,
  660. + inodep->fragment, &frag_blk, &frag_size))
  661. + goto failed_read;
  662. +
  663. + if((i = squashfs_new_inode(s, inodeb)) == NULL)
  664. + goto failed_read1;
  665. +
  666. + i->i_nlink = inodep->nlink;
  667. + i->i_size = inodep->file_size;
  668. + i->i_fop = &generic_ro_fops;
  669. + i->i_mode |= S_IFREG;
  670. + i->i_blocks = ((i->i_size - 1) >> 9) + 1;
  671. + i->i_blksize = PAGE_CACHE_SIZE;
  672. + SQUASHFS_I(i)->u.s1.fragment_start_block = frag_blk;
  673. + SQUASHFS_I(i)->u.s1.fragment_size = frag_size;
  674. + SQUASHFS_I(i)->u.s1.fragment_offset = inodep->offset;
  675. + SQUASHFS_I(i)->start_block = inodep->start_block;
  676. + SQUASHFS_I(i)->u.s1.block_list_start = next_block;
  677. + SQUASHFS_I(i)->offset = next_offset;
  678. + if (sblk->block_size > 4096)
  679. + i->i_data.a_ops = &squashfs_aops;
  680. + else
  681. + i->i_data.a_ops = &squashfs_aops_4K;
  682. +
  683. + TRACE("File inode %x:%x, start_block %llx, "
  684. + "block_list_start %llx, offset %x\n",
  685. + SQUASHFS_INODE_BLK(inode), offset,
  686. + inodep->start_block, next_block,
  687. + next_offset);
  688. + break;
  689. + }
  690. + case SQUASHFS_DIR_TYPE: {
  691. + struct squashfs_dir_inode_header *inodep = &id.dir;
  692. + struct squashfs_dir_inode_header *sinodep = &sid.dir;
  693. +
  694. + if (msblk->swap) {
  695. + if (!squashfs_get_cached_block(s, (char *)
  696. + sinodep, block, offset,
  697. + sizeof(*sinodep), &next_block,
  698. + &next_offset))
  699. + goto failed_read;
  700. + SQUASHFS_SWAP_DIR_INODE_HEADER(inodep, sinodep);
  701. + } else
  702. + if (!squashfs_get_cached_block(s, (char *)
  703. + inodep, block, offset,
  704. + sizeof(*inodep), &next_block,
  705. + &next_offset))
  706. + goto failed_read;
  707. +
  708. + if((i = squashfs_new_inode(s, inodeb)) == NULL)
  709. + goto failed_read1;
  710. +
  711. + i->i_nlink = inodep->nlink;
  712. + i->i_size = inodep->file_size;
  713. + i->i_op = &squashfs_dir_inode_ops;
  714. + i->i_fop = &squashfs_dir_ops;
  715. + i->i_mode |= S_IFDIR;
  716. + SQUASHFS_I(i)->start_block = inodep->start_block;
  717. + SQUASHFS_I(i)->offset = inodep->offset;
  718. + SQUASHFS_I(i)->u.s2.directory_index_count = 0;
  719. + SQUASHFS_I(i)->u.s2.parent_inode = inodep->parent_inode;
  720. +
  721. + TRACE("Directory inode %x:%x, start_block %x, offset "
  722. + "%x\n", SQUASHFS_INODE_BLK(inode),
  723. + offset, inodep->start_block,
  724. + inodep->offset);
  725. + break;
  726. + }
  727. + case SQUASHFS_LDIR_TYPE: {
  728. + struct squashfs_ldir_inode_header *inodep = &id.ldir;
  729. + struct squashfs_ldir_inode_header *sinodep = &sid.ldir;
  730. +
  731. + if (msblk->swap) {
  732. + if (!squashfs_get_cached_block(s, (char *)
  733. + sinodep, block, offset,
  734. + sizeof(*sinodep), &next_block,
  735. + &next_offset))
  736. + goto failed_read;
  737. + SQUASHFS_SWAP_LDIR_INODE_HEADER(inodep,
  738. + sinodep);
  739. + } else
  740. + if (!squashfs_get_cached_block(s, (char *)
  741. + inodep, block, offset,
  742. + sizeof(*inodep), &next_block,
  743. + &next_offset))
  744. + goto failed_read;
  745. +
  746. + if((i = squashfs_new_inode(s, inodeb)) == NULL)
  747. + goto failed_read1;
  748. +
  749. + i->i_nlink = inodep->nlink;
  750. + i->i_size = inodep->file_size;
  751. + i->i_op = &squashfs_dir_inode_ops;
  752. + i->i_fop = &squashfs_dir_ops;
  753. + i->i_mode |= S_IFDIR;
  754. + SQUASHFS_I(i)->start_block = inodep->start_block;
  755. + SQUASHFS_I(i)->offset = inodep->offset;
  756. + SQUASHFS_I(i)->u.s2.directory_index_start = next_block;
  757. + SQUASHFS_I(i)->u.s2.directory_index_offset =
  758. + next_offset;
  759. + SQUASHFS_I(i)->u.s2.directory_index_count =
  760. + inodep->i_count;
  761. + SQUASHFS_I(i)->u.s2.parent_inode = inodep->parent_inode;
  762. +
  763. + TRACE("Long directory inode %x:%x, start_block %x, "
  764. + "offset %x\n",
  765. + SQUASHFS_INODE_BLK(inode), offset,
  766. + inodep->start_block, inodep->offset);
  767. + break;
  768. + }
  769. + case SQUASHFS_SYMLINK_TYPE: {
  770. + struct squashfs_symlink_inode_header *inodep =
  771. + &id.symlink;
  772. + struct squashfs_symlink_inode_header *sinodep =
  773. + &sid.symlink;
  774. +
  775. + if (msblk->swap) {
  776. + if (!squashfs_get_cached_block(s, (char *)
  777. + sinodep, block, offset,
  778. + sizeof(*sinodep), &next_block,
  779. + &next_offset))
  780. + goto failed_read;
  781. + SQUASHFS_SWAP_SYMLINK_INODE_HEADER(inodep,
  782. + sinodep);
  783. + } else
  784. + if (!squashfs_get_cached_block(s, (char *)
  785. + inodep, block, offset,
  786. + sizeof(*inodep), &next_block,
  787. + &next_offset))
  788. + goto failed_read;
  789. +
  790. + if((i = squashfs_new_inode(s, inodeb)) == NULL)
  791. + goto failed_read1;
  792. +
  793. + i->i_nlink = inodep->nlink;
  794. + i->i_size = inodep->symlink_size;
  795. + i->i_op = &page_symlink_inode_operations;
  796. + i->i_data.a_ops = &squashfs_symlink_aops;
  797. + i->i_mode |= S_IFLNK;
  798. + SQUASHFS_I(i)->start_block = next_block;
  799. + SQUASHFS_I(i)->offset = next_offset;
  800. +
  801. + TRACE("Symbolic link inode %x:%x, start_block %llx, "
  802. + "offset %x\n",
  803. + SQUASHFS_INODE_BLK(inode), offset,
  804. + next_block, next_offset);
  805. + break;
  806. + }
  807. + case SQUASHFS_BLKDEV_TYPE:
  808. + case SQUASHFS_CHRDEV_TYPE: {
  809. + struct squashfs_dev_inode_header *inodep = &id.dev;
  810. + struct squashfs_dev_inode_header *sinodep = &sid.dev;
  811. +
  812. + if (msblk->swap) {
  813. + if (!squashfs_get_cached_block(s, (char *)
  814. + sinodep, block, offset,
  815. + sizeof(*sinodep), &next_block,
  816. + &next_offset))
  817. + goto failed_read;
  818. + SQUASHFS_SWAP_DEV_INODE_HEADER(inodep, sinodep);
  819. + } else
  820. + if (!squashfs_get_cached_block(s, (char *)
  821. + inodep, block, offset,
  822. + sizeof(*inodep), &next_block,
  823. + &next_offset))
  824. + goto failed_read;
  825. +
  826. + if ((i = squashfs_new_inode(s, inodeb)) == NULL)
  827. + goto failed_read1;
  828. +
  829. + i->i_nlink = inodep->nlink;
  830. + i->i_mode |= (inodeb->inode_type ==
  831. + SQUASHFS_CHRDEV_TYPE) ? S_IFCHR :
  832. + S_IFBLK;
  833. + init_special_inode(i, i->i_mode, inodep->rdev);
  834. +
  835. + TRACE("Device inode %x:%x, rdev %x\n",
  836. + SQUASHFS_INODE_BLK(inode), offset,
  837. + inodep->rdev);
  838. + break;
  839. + }
  840. + case SQUASHFS_FIFO_TYPE:
  841. + case SQUASHFS_SOCKET_TYPE: {
  842. + struct squashfs_ipc_inode_header *inodep = &id.ipc;
  843. + struct squashfs_ipc_inode_header *sinodep = &sid.ipc;
  844. +
  845. + if (msblk->swap) {
  846. + if (!squashfs_get_cached_block(s, (char *)
  847. + sinodep, block, offset,
  848. + sizeof(*sinodep), &next_block,
  849. + &next_offset))
  850. + goto failed_read;
  851. + SQUASHFS_SWAP_IPC_INODE_HEADER(inodep, sinodep);
  852. + } else
  853. + if (!squashfs_get_cached_block(s, (char *)
  854. + inodep, block, offset,
  855. + sizeof(*inodep), &next_block,
  856. + &next_offset))
  857. + goto failed_read;
  858. +
  859. + if ((i = squashfs_new_inode(s, inodeb)) == NULL)
  860. + goto failed_read1;
  861. +
  862. + i->i_nlink = inodep->nlink;
  863. + i->i_mode |= (inodeb->inode_type == SQUASHFS_FIFO_TYPE)
  864. + ? S_IFIFO : S_IFSOCK;
  865. + init_special_inode(i, i->i_mode, 0);
  866. + break;
  867. + }
  868. + default:
  869. + ERROR("Unknown inode type %d in squashfs_iget!\n",
  870. + inodeb->inode_type);
  871. + goto failed_read1;
  872. + }
  873. +
  874. + insert_inode_hash(i);
  875. + return i;
  876. +
  877. +failed_read:
  878. + ERROR("Unable to read inode [%llx:%x]\n", block, offset);
  879. +
  880. +failed_read1:
  881. + return NULL;
  882. +}
  883. +
  884. +
  885. +int read_fragment_index_table(struct super_block *s)
  886. +{
  887. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  888. + struct squashfs_super_block *sblk = &msblk->sblk;
  889. +
  890. + if (!(msblk->fragment_index = kmalloc(SQUASHFS_FRAGMENT_INDEX_BYTES
  891. + (sblk->fragments), GFP_KERNEL))) {
  892. + ERROR("Failed to allocate uid/gid table\n");
  893. + return 0;
  894. + }
  895. +
  896. + if (SQUASHFS_FRAGMENT_INDEX_BYTES(sblk->fragments) &&
  897. + !squashfs_read_data(s, (char *)
  898. + msblk->fragment_index,
  899. + sblk->fragment_table_start,
  900. + SQUASHFS_FRAGMENT_INDEX_BYTES
  901. + (sblk->fragments) |
  902. + SQUASHFS_COMPRESSED_BIT_BLOCK, NULL)) {
  903. + ERROR("unable to read fragment index table\n");
  904. + return 0;
  905. + }
  906. +
  907. + if (msblk->swap) {
  908. + int i;
  909. + long long fragment;
  910. +
  911. + for (i = 0; i < SQUASHFS_FRAGMENT_INDEXES(sblk->fragments);
  912. + i++) {
  913. + SQUASHFS_SWAP_FRAGMENT_INDEXES((&fragment),
  914. + &msblk->fragment_index[i], 1);
  915. + msblk->fragment_index[i] = fragment;
  916. + }
  917. + }
  918. +
  919. + return 1;
  920. +}
  921. +
  922. +
  923. +static int supported_squashfs_filesystem(struct squashfs_sb_info *msblk, int silent)
  924. +{
  925. + struct squashfs_super_block *sblk = &msblk->sblk;
  926. +
  927. + msblk->iget = squashfs_iget;
  928. + msblk->read_blocklist = read_blocklist;
  929. + msblk->read_fragment_index_table = read_fragment_index_table;
  930. +
  931. + if (sblk->s_major == 1) {
  932. + if (!squashfs_1_0_supported(msblk)) {
  933. + SERROR("Major/Minor mismatch, Squashfs 1.0 filesystems "
  934. + "are unsupported\n");
  935. + SERROR("Please recompile with "
  936. + "Squashfs 1.0 support enabled\n");
  937. + return 0;
  938. + }
  939. + } else if (sblk->s_major == 2) {
  940. + if (!squashfs_2_0_supported(msblk)) {
  941. + SERROR("Major/Minor mismatch, Squashfs 2.0 filesystems "
  942. + "are unsupported\n");
  943. + SERROR("Please recompile with "
  944. + "Squashfs 2.0 support enabled\n");
  945. + return 0;
  946. + }
  947. + } else if(sblk->s_major != SQUASHFS_MAJOR || sblk->s_minor >
  948. + SQUASHFS_MINOR) {
  949. + SERROR("Major/Minor mismatch, trying to mount newer %d.%d "
  950. + "filesystem\n", sblk->s_major, sblk->s_minor);
  951. + SERROR("Please update your kernel\n");
  952. + return 0;
  953. + }
  954. +
  955. + return 1;
  956. +}
  957. +
  958. +
  959. +static struct super_block *squashfs_read_super(struct super_block *s,
  960. + void *data, int silent)
  961. +{
  962. + kdev_t dev = s->s_dev;
  963. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  964. + struct squashfs_super_block *sblk = &msblk->sblk;
  965. + int i;
  966. + struct inode *root;
  967. +
  968. + msblk->devblksize = get_hardsect_size(dev);
  969. + if(msblk->devblksize < BLOCK_SIZE)
  970. + msblk->devblksize = BLOCK_SIZE;
  971. + msblk->devblksize_log2 = ffz(~msblk->devblksize);
  972. + set_blocksize(dev, msblk->devblksize);
  973. + s->s_blocksize = msblk->devblksize;
  974. + s->s_blocksize_bits = msblk->devblksize_log2;
  975. +
  976. + init_MUTEX(&msblk->read_data_mutex);
  977. + init_MUTEX(&msblk->read_page_mutex);
  978. + init_MUTEX(&msblk->block_cache_mutex);
  979. + init_MUTEX(&msblk->fragment_mutex);
  980. +
  981. + init_waitqueue_head(&msblk->waitq);
  982. + init_waitqueue_head(&msblk->fragment_wait_queue);
  983. +
  984. + if (!squashfs_read_data(s, (char *) sblk, SQUASHFS_START,
  985. + sizeof(struct squashfs_super_block) |
  986. + SQUASHFS_COMPRESSED_BIT_BLOCK, NULL)) {
  987. + SERROR("unable to read superblock\n");
  988. + goto failed_mount;
  989. + }
  990. +
  991. + /* Check it is a SQUASHFS superblock */
  992. + msblk->swap = 0;
  993. + if ((s->s_magic = sblk->s_magic) != SQUASHFS_MAGIC) {
  994. + if (sblk->s_magic == SQUASHFS_MAGIC_SWAP) {
  995. + struct squashfs_super_block ssblk;
  996. +
  997. + WARNING("Mounting a different endian SQUASHFS "
  998. + "filesystem on %s\n", bdevname(dev));
  999. +
  1000. + SQUASHFS_SWAP_SUPER_BLOCK(&ssblk, sblk);
  1001. + memcpy(sblk, &ssblk, sizeof(struct squashfs_super_block));
  1002. + msblk->swap = 1;
  1003. + } else {
  1004. + SERROR("Can't find a SQUASHFS superblock on %s\n",
  1005. + bdevname(dev));
  1006. + goto failed_mount;
  1007. + }
  1008. + }
  1009. +
  1010. + /* Check the MAJOR & MINOR versions */
  1011. + if(!supported_squashfs_filesystem(msblk, silent))
  1012. + goto failed_mount;
  1013. +
  1014. + TRACE("Found valid superblock on %s\n", bdevname(dev));
  1015. + TRACE("Inodes are %scompressed\n",
  1016. + SQUASHFS_UNCOMPRESSED_INODES
  1017. + (sblk->flags) ? "un" : "");
  1018. + TRACE("Data is %scompressed\n",
  1019. + SQUASHFS_UNCOMPRESSED_DATA(sblk->flags)
  1020. + ? "un" : "");
  1021. + TRACE("Check data is %s present in the filesystem\n",
  1022. + SQUASHFS_CHECK_DATA(sblk->flags) ?
  1023. + "" : "not");
  1024. + TRACE("Filesystem size %lld bytes\n", sblk->bytes_used);
  1025. + TRACE("Block size %d\n", sblk->block_size);
  1026. + TRACE("Number of inodes %d\n", sblk->inodes);
  1027. + if (sblk->s_major > 1)
  1028. + TRACE("Number of fragments %d\n", sblk->fragments);
  1029. + TRACE("Number of uids %d\n", sblk->no_uids);
  1030. + TRACE("Number of gids %d\n", sblk->no_guids);
  1031. + TRACE("sblk->inode_table_start %llx\n", sblk->inode_table_start);
  1032. + TRACE("sblk->directory_table_start %llx\n", sblk->directory_table_start);
  1033. + if (sblk->s_major > 1)
  1034. + TRACE("sblk->fragment_table_start %llx\n",
  1035. + sblk->fragment_table_start);
  1036. + TRACE("sblk->uid_start %llx\n", sblk->uid_start);
  1037. +
  1038. + s->s_flags |= MS_RDONLY;
  1039. + s->s_op = &squashfs_ops;
  1040. +
  1041. + /* Init inode_table block pointer array */
  1042. + if (!(msblk->block_cache = kmalloc(sizeof(struct squashfs_cache) *
  1043. + SQUASHFS_CACHED_BLKS, GFP_KERNEL))) {
  1044. + ERROR("Failed to allocate block cache\n");
  1045. + goto failed_mount;
  1046. + }
  1047. +
  1048. + for (i = 0; i < SQUASHFS_CACHED_BLKS; i++)
  1049. + msblk->block_cache[i].block = SQUASHFS_INVALID_BLK;
  1050. +
  1051. + msblk->next_cache = 0;
  1052. +
  1053. + /* Allocate read_data block */
  1054. + msblk->read_size = (sblk->block_size < SQUASHFS_METADATA_SIZE) ?
  1055. + SQUASHFS_METADATA_SIZE :
  1056. + sblk->block_size;
  1057. +
  1058. + if (!(msblk->read_data = kmalloc(msblk->read_size, GFP_KERNEL))) {
  1059. + ERROR("Failed to allocate read_data block\n");
  1060. + goto failed_mount;
  1061. + }
  1062. +
  1063. + /* Allocate read_page block */
  1064. + if (!(msblk->read_page = kmalloc(sblk->block_size, GFP_KERNEL))) {
  1065. + ERROR("Failed to allocate read_page block\n");
  1066. + goto failed_mount;
  1067. + }
  1068. +
  1069. + /* Allocate uid and gid tables */
  1070. + if (!(msblk->uid = kmalloc((sblk->no_uids + sblk->no_guids) *
  1071. + sizeof(unsigned int), GFP_KERNEL))) {
  1072. + ERROR("Failed to allocate uid/gid table\n");
  1073. + goto failed_mount;
  1074. + }
  1075. + msblk->guid = msblk->uid + sblk->no_uids;
  1076. +
  1077. + if (msblk->swap) {
  1078. + unsigned int suid[sblk->no_uids + sblk->no_guids];
  1079. +
  1080. + if (!squashfs_read_data(s, (char *) &suid, sblk->uid_start,
  1081. + ((sblk->no_uids + sblk->no_guids) *
  1082. + sizeof(unsigned int)) |
  1083. + SQUASHFS_COMPRESSED_BIT_BLOCK, NULL)) {
  1084. + ERROR("unable to read uid/gid table\n");
  1085. + goto failed_mount;
  1086. + }
  1087. +
  1088. + SQUASHFS_SWAP_DATA(msblk->uid, suid, (sblk->no_uids +
  1089. + sblk->no_guids), (sizeof(unsigned int) * 8));
  1090. + } else
  1091. + if (!squashfs_read_data(s, (char *) msblk->uid, sblk->uid_start,
  1092. + ((sblk->no_uids + sblk->no_guids) *
  1093. + sizeof(unsigned int)) |
  1094. + SQUASHFS_COMPRESSED_BIT_BLOCK, NULL)) {
  1095. + ERROR("unable to read uid/gid table\n");
  1096. + goto failed_mount;
  1097. + }
  1098. +
  1099. +
  1100. + if (sblk->s_major == 1 && squashfs_1_0_supported(msblk))
  1101. + goto allocate_root;
  1102. +
  1103. + if (!(msblk->fragment = kmalloc(sizeof(struct squashfs_fragment_cache) *
  1104. + SQUASHFS_CACHED_FRAGMENTS, GFP_KERNEL))) {
  1105. + ERROR("Failed to allocate fragment block cache\n");
  1106. + goto failed_mount;
  1107. + }
  1108. +
  1109. + for (i = 0; i < SQUASHFS_CACHED_FRAGMENTS; i++) {
  1110. + msblk->fragment[i].locked = 0;
  1111. + msblk->fragment[i].block = SQUASHFS_INVALID_BLK;
  1112. + msblk->fragment[i].data = NULL;
  1113. + }
  1114. +
  1115. + msblk->next_fragment = 0;
  1116. +
  1117. + /* Allocate fragment index table */
  1118. + if(msblk->read_fragment_index_table(s) == 0)
  1119. + goto failed_mount;
  1120. +
  1121. +allocate_root:
  1122. + if ((root = (msblk->iget)(s, sblk->root_inode)) == NULL)
  1123. + goto failed_mount;
  1124. +
  1125. + if ((s->s_root = d_alloc_root(root)) == NULL) {
  1126. + ERROR("Root inode create failed\n");
  1127. + iput(root);
  1128. + goto failed_mount;
  1129. + }
  1130. +
  1131. + TRACE("Leaving squashfs_read_super\n");
  1132. + return s;
  1133. +
  1134. +failed_mount:
  1135. + kfree(msblk->fragment_index);
  1136. + kfree(msblk->fragment);
  1137. + kfree(msblk->uid);
  1138. + kfree(msblk->read_page);
  1139. + kfree(msblk->read_data);
  1140. + kfree(msblk->block_cache);
  1141. + kfree(msblk->fragment_index_2);
  1142. + return NULL;
  1143. +}
  1144. +
  1145. +
  1146. +static int squashfs_statfs(struct super_block *s, struct statfs *buf)
  1147. +{
  1148. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  1149. + struct squashfs_super_block *sblk = &msblk->sblk;
  1150. +
  1151. + TRACE("Entered squashfs_statfs\n");
  1152. +
  1153. + buf->f_type = SQUASHFS_MAGIC;
  1154. + buf->f_bsize = sblk->block_size;
  1155. + buf->f_blocks = ((sblk->bytes_used - 1) >> sblk->block_log) + 1;
  1156. + buf->f_bfree = buf->f_bavail = 0;
  1157. + buf->f_files = sblk->inodes;
  1158. + buf->f_ffree = 0;
  1159. + buf->f_namelen = SQUASHFS_NAME_LEN;
  1160. +
  1161. + return 0;
  1162. +}
  1163. +
  1164. +
  1165. +static int squashfs_symlink_readpage(struct file *file, struct page *page)
  1166. +{
  1167. + struct inode *inode = page->mapping->host;
  1168. + int index = page->index << PAGE_CACHE_SHIFT, length, bytes;
  1169. + long long block = SQUASHFS_I(inode)->start_block;
  1170. + int offset = SQUASHFS_I(inode)->offset;
  1171. + void *pageaddr = kmap(page);
  1172. +
  1173. + TRACE("Entered squashfs_symlink_readpage, page index %ld, start block "
  1174. + "%llx, offset %x\n", page->index,
  1175. + SQUASHFS_I(inode)->start_block,
  1176. + SQUASHFS_I(inode)->offset);
  1177. +
  1178. + for (length = 0; length < index; length += bytes) {
  1179. + if (!(bytes = squashfs_get_cached_block(inode->i_sb, NULL,
  1180. + block, offset, PAGE_CACHE_SIZE, &block,
  1181. + &offset))) {
  1182. + ERROR("Unable to read symbolic link [%llx:%x]\n", block,
  1183. + offset);
  1184. + goto skip_read;
  1185. + }
  1186. + }
  1187. +
  1188. + if (length != index) {
  1189. + ERROR("(squashfs_symlink_readpage) length != index\n");
  1190. + bytes = 0;
  1191. + goto skip_read;
  1192. + }
  1193. +
  1194. + bytes = (i_size_read(inode) - length) > PAGE_CACHE_SIZE ? PAGE_CACHE_SIZE :
  1195. + i_size_read(inode) - length;
  1196. +
  1197. + if (!(bytes = squashfs_get_cached_block(inode->i_sb, pageaddr, block,
  1198. + offset, bytes, &block, &offset)))
  1199. + ERROR("Unable to read symbolic link [%llx:%x]\n", block, offset);
  1200. +
  1201. +skip_read:
  1202. + memset(pageaddr + bytes, 0, PAGE_CACHE_SIZE - bytes);
  1203. + kunmap(page);
  1204. + SetPageUptodate(page);
  1205. + UnlockPage(page);
  1206. +
  1207. + return 0;
  1208. +}
  1209. +
  1210. +
  1211. +struct meta_index *locate_meta_index(struct inode *inode, int index, int offset)
  1212. +{
  1213. + struct meta_index *meta = NULL;
  1214. + struct squashfs_sb_info *msblk = &inode->i_sb->u.squashfs_sb;
  1215. + int i;
  1216. +
  1217. + down(&msblk->meta_index_mutex);
  1218. +
  1219. + TRACE("locate_meta_index: index %d, offset %d\n", index, offset);
  1220. +
  1221. + if(msblk->meta_index == NULL)
  1222. + goto not_allocated;
  1223. +
  1224. + for (i = 0; i < SQUASHFS_META_NUMBER; i ++)
  1225. + if (msblk->meta_index[i].inode_number == inode->i_ino &&
  1226. + msblk->meta_index[i].offset >= offset &&
  1227. + msblk->meta_index[i].offset <= index &&
  1228. + msblk->meta_index[i].locked == 0) {
  1229. + TRACE("locate_meta_index: entry %d, offset %d\n", i,
  1230. + msblk->meta_index[i].offset);
  1231. + meta = &msblk->meta_index[i];
  1232. + offset = meta->offset;
  1233. + }
  1234. +
  1235. + if (meta)
  1236. + meta->locked = 1;
  1237. +
  1238. +not_allocated:
  1239. + up(&msblk->meta_index_mutex);
  1240. +
  1241. + return meta;
  1242. +}
  1243. +
  1244. +
  1245. +struct meta_index *empty_meta_index(struct inode *inode, int offset, int skip)
  1246. +{
  1247. + struct squashfs_sb_info *msblk = &inode->i_sb->u.squashfs_sb;
  1248. + struct meta_index *meta = NULL;
  1249. + int i;
  1250. +
  1251. + down(&msblk->meta_index_mutex);
  1252. +
  1253. + TRACE("empty_meta_index: offset %d, skip %d\n", offset, skip);
  1254. +
  1255. + if(msblk->meta_index == NULL) {
  1256. + if (!(msblk->meta_index = kmalloc(sizeof(struct meta_index) *
  1257. + SQUASHFS_META_NUMBER, GFP_KERNEL))) {
  1258. + ERROR("Failed to allocate meta_index\n");
  1259. + goto failed;
  1260. + }
  1261. + for(i = 0; i < SQUASHFS_META_NUMBER; i++) {
  1262. + msblk->meta_index[i].inode_number = 0;
  1263. + msblk->meta_index[i].locked = 0;
  1264. + }
  1265. + msblk->next_meta_index = 0;
  1266. + }
  1267. +
  1268. + for(i = SQUASHFS_META_NUMBER; i &&
  1269. + msblk->meta_index[msblk->next_meta_index].locked; i --)
  1270. + msblk->next_meta_index = (msblk->next_meta_index + 1) %
  1271. + SQUASHFS_META_NUMBER;
  1272. +
  1273. + if(i == 0) {
  1274. + TRACE("empty_meta_index: failed!\n");
  1275. + goto failed;
  1276. + }
  1277. +
  1278. + TRACE("empty_meta_index: returned meta entry %d, %p\n",
  1279. + msblk->next_meta_index,
  1280. + &msblk->meta_index[msblk->next_meta_index]);
  1281. +
  1282. + meta = &msblk->meta_index[msblk->next_meta_index];
  1283. + msblk->next_meta_index = (msblk->next_meta_index + 1) %
  1284. + SQUASHFS_META_NUMBER;
  1285. +
  1286. + meta->inode_number = inode->i_ino;
  1287. + meta->offset = offset;
  1288. + meta->skip = skip;
  1289. + meta->entries = 0;
  1290. + meta->locked = 1;
  1291. +
  1292. +failed:
  1293. + up(&msblk->meta_index_mutex);
  1294. + return meta;
  1295. +}
  1296. +
  1297. +
  1298. +void release_meta_index(struct inode *inode, struct meta_index *meta)
  1299. +{
  1300. + meta->locked = 0;
  1301. +}
  1302. +
  1303. +
  1304. +static int read_block_index(struct super_block *s, int blocks, char *block_list,
  1305. + long long *start_block, int *offset)
  1306. +{
  1307. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  1308. + unsigned int *block_listp;
  1309. + int block = 0;
  1310. +
  1311. + if (msblk->swap) {
  1312. + char sblock_list[blocks << 2];
  1313. +
  1314. + if (!squashfs_get_cached_block(s, sblock_list, *start_block,
  1315. + *offset, blocks << 2, start_block, offset)) {
  1316. + ERROR("Unable to read block list [%llx:%x]\n",
  1317. + *start_block, *offset);
  1318. + goto failure;
  1319. + }
  1320. + SQUASHFS_SWAP_INTS(((unsigned int *)block_list),
  1321. + ((unsigned int *)sblock_list), blocks);
  1322. + } else
  1323. + if (!squashfs_get_cached_block(s, block_list, *start_block,
  1324. + *offset, blocks << 2, start_block, offset)) {
  1325. + ERROR("Unable to read block list [%llx:%x]\n",
  1326. + *start_block, *offset);
  1327. + goto failure;
  1328. + }
  1329. +
  1330. + for (block_listp = (unsigned int *) block_list; blocks;
  1331. + block_listp++, blocks --)
  1332. + block += SQUASHFS_COMPRESSED_SIZE_BLOCK(*block_listp);
  1333. +
  1334. + return block;
  1335. +
  1336. +failure:
  1337. + return -1;
  1338. +}
  1339. +
  1340. +
  1341. +#define SIZE 256
  1342. +
  1343. +static inline int calculate_skip(int blocks) {
  1344. + int skip = (blocks - 1) / ((SQUASHFS_SLOTS * SQUASHFS_META_ENTRIES + 1) * SQUASHFS_META_INDEXES);
  1345. + return skip >= 7 ? 7 : skip + 1;
  1346. +}
  1347. +
  1348. +
  1349. +static int get_meta_index(struct inode *inode, int index,
  1350. + long long *index_block, int *index_offset,
  1351. + long long *data_block, char *block_list)
  1352. +{
  1353. + struct squashfs_sb_info *msblk = &inode->i_sb->u.squashfs_sb;
  1354. + struct squashfs_super_block *sblk = &msblk->sblk;
  1355. + int skip = calculate_skip(i_size_read(inode) >> sblk->block_log);
  1356. + int offset = 0;
  1357. + struct meta_index *meta;
  1358. + struct meta_entry *meta_entry;
  1359. + long long cur_index_block = SQUASHFS_I(inode)->u.s1.block_list_start;
  1360. + int cur_offset = SQUASHFS_I(inode)->offset;
  1361. + long long cur_data_block = SQUASHFS_I(inode)->start_block;
  1362. + int i;
  1363. +
  1364. + index /= SQUASHFS_META_INDEXES * skip;
  1365. +
  1366. + while ( offset < index ) {
  1367. + meta = locate_meta_index(inode, index, offset + 1);
  1368. +
  1369. + if (meta == NULL) {
  1370. + if ((meta = empty_meta_index(inode, offset + 1,
  1371. + skip)) == NULL)
  1372. + goto all_done;
  1373. + } else {
  1374. + offset = index < meta->offset + meta->entries ? index :
  1375. + meta->offset + meta->entries - 1;
  1376. + meta_entry = &meta->meta_entry[offset - meta->offset];
  1377. + cur_index_block = meta_entry->index_block + sblk->inode_table_start;
  1378. + cur_offset = meta_entry->offset;
  1379. + cur_data_block = meta_entry->data_block;
  1380. + TRACE("get_meta_index: offset %d, meta->offset %d, "
  1381. + "meta->entries %d\n", offset, meta->offset,
  1382. + meta->entries);
  1383. + TRACE("get_meta_index: index_block 0x%llx, offset 0x%x"
  1384. + " data_block 0x%llx\n", cur_index_block,
  1385. + cur_offset, cur_data_block);
  1386. + }
  1387. +
  1388. + for (i = meta->offset + meta->entries; i <= index &&
  1389. + i < meta->offset + SQUASHFS_META_ENTRIES; i++) {
  1390. + int blocks = skip * SQUASHFS_META_INDEXES;
  1391. +
  1392. + while (blocks) {
  1393. + int block = blocks > (SIZE >> 2) ? (SIZE >> 2) :
  1394. + blocks;
  1395. + int res = read_block_index(inode->i_sb, block,
  1396. + block_list, &cur_index_block,
  1397. + &cur_offset);
  1398. +
  1399. + if (res == -1)
  1400. + goto failed;
  1401. +
  1402. + cur_data_block += res;
  1403. + blocks -= block;
  1404. + }
  1405. +
  1406. + meta_entry = &meta->meta_entry[i - meta->offset];
  1407. + meta_entry->index_block = cur_index_block - sblk->inode_table_start;
  1408. + meta_entry->offset = cur_offset;
  1409. + meta_entry->data_block = cur_data_block;
  1410. + meta->entries ++;
  1411. + offset ++;
  1412. + }
  1413. +
  1414. + TRACE("get_meta_index: meta->offset %d, meta->entries %d\n",
  1415. + meta->offset, meta->entries);
  1416. +
  1417. + release_meta_index(inode, meta);
  1418. + }
  1419. +
  1420. +all_done:
  1421. + *index_block = cur_index_block;
  1422. + *index_offset = cur_offset;
  1423. + *data_block = cur_data_block;
  1424. +
  1425. + return offset * SQUASHFS_META_INDEXES * skip;
  1426. +
  1427. +failed:
  1428. + release_meta_index(inode, meta);
  1429. + return -1;
  1430. +}
  1431. +
  1432. +
  1433. +static long long read_blocklist(struct inode *inode, int index,
  1434. + int readahead_blks, char *block_list,
  1435. + unsigned short **block_p, unsigned int *bsize)
  1436. +{
  1437. + long long block_ptr;
  1438. + int offset;
  1439. + long long block;
  1440. + int res = get_meta_index(inode, index, &block_ptr, &offset, &block,
  1441. + block_list);
  1442. +
  1443. + TRACE("read_blocklist: res %d, index %d, block_ptr 0x%llx, offset"
  1444. + " 0x%x, block 0x%llx\n", res, index, block_ptr, offset,
  1445. + block);
  1446. +
  1447. + if(res == -1)
  1448. + goto failure;
  1449. +
  1450. + index -= res;
  1451. +
  1452. + while ( index ) {
  1453. + int blocks = index > (SIZE >> 2) ? (SIZE >> 2) : index;
  1454. + int res = read_block_index(inode->i_sb, blocks, block_list,
  1455. + &block_ptr, &offset);
  1456. + if (res == -1)
  1457. + goto failure;
  1458. + block += res;
  1459. + index -= blocks;
  1460. + }
  1461. +
  1462. + if (read_block_index(inode->i_sb, 1, block_list,
  1463. + &block_ptr, &offset) == -1)
  1464. + goto failure;
  1465. + *bsize = *((unsigned int *) block_list);
  1466. +
  1467. + return block;
  1468. +
  1469. +failure:
  1470. + return 0;
  1471. +}
  1472. +
  1473. +
  1474. +static int squashfs_readpage(struct file *file, struct page *page)
  1475. +{
  1476. + struct inode *inode = page->mapping->host;
  1477. + struct squashfs_sb_info *msblk = &inode->i_sb->u.squashfs_sb;
  1478. + struct squashfs_super_block *sblk = &msblk->sblk;
  1479. + unsigned char block_list[SIZE];
  1480. + long long block;
  1481. + unsigned int bsize, i = 0, bytes = 0, byte_offset = 0;
  1482. + int index = page->index >> (sblk->block_log - PAGE_CACHE_SHIFT);
  1483. + void *pageaddr;
  1484. + struct squashfs_fragment_cache *fragment = NULL;
  1485. + char *data_ptr = msblk->read_page;
  1486. +
  1487. + int mask = (1 << (sblk->block_log - PAGE_CACHE_SHIFT)) - 1;
  1488. + int start_index = page->index & ~mask;
  1489. + int end_index = start_index | mask;
  1490. +
  1491. + TRACE("Entered squashfs_readpage, page index %lx, start block %llx\n",
  1492. + page->index,
  1493. + SQUASHFS_I(inode)->start_block);
  1494. +
  1495. + if (page->index >= ((i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
  1496. + PAGE_CACHE_SHIFT))
  1497. + goto skip_read;
  1498. +
  1499. + if (SQUASHFS_I(inode)->u.s1.fragment_start_block == SQUASHFS_INVALID_BLK
  1500. + || index < (i_size_read(inode) >>
  1501. + sblk->block_log)) {
  1502. + if ((block = (msblk->read_blocklist)(inode, index, 1,
  1503. + block_list, NULL, &bsize)) == 0)
  1504. + goto skip_read;
  1505. +
  1506. + down(&msblk->read_page_mutex);
  1507. +
  1508. + if (!(bytes = squashfs_read_data(inode->i_sb, msblk->read_page,
  1509. + block, bsize, NULL))) {
  1510. + ERROR("Unable to read page, block %llx, size %x\n", block,
  1511. + bsize);
  1512. + up(&msblk->read_page_mutex);
  1513. + goto skip_read;
  1514. + }
  1515. + } else {
  1516. + if ((fragment = get_cached_fragment(inode->i_sb,
  1517. + SQUASHFS_I(inode)->
  1518. + u.s1.fragment_start_block,
  1519. + SQUASHFS_I(inode)->u.s1.fragment_size))
  1520. + == NULL) {
  1521. + ERROR("Unable to read page, block %llx, size %x\n",
  1522. + SQUASHFS_I(inode)->
  1523. + u.s1.fragment_start_block,
  1524. + (int) SQUASHFS_I(inode)->
  1525. + u.s1.fragment_size);
  1526. + goto skip_read;
  1527. + }
  1528. + bytes = SQUASHFS_I(inode)->u.s1.fragment_offset +
  1529. + (i_size_read(inode) & (sblk->block_size
  1530. + - 1));
  1531. + byte_offset = SQUASHFS_I(inode)->u.s1.fragment_offset;
  1532. + data_ptr = fragment->data;
  1533. + }
  1534. +
  1535. + for (i = start_index; i <= end_index && byte_offset < bytes;
  1536. + i++, byte_offset += PAGE_CACHE_SIZE) {
  1537. + struct page *push_page;
  1538. + int available_bytes = (bytes - byte_offset) > PAGE_CACHE_SIZE ?
  1539. + PAGE_CACHE_SIZE : bytes - byte_offset;
  1540. +
  1541. + TRACE("bytes %d, i %d, byte_offset %d, available_bytes %d\n",
  1542. + bytes, i, byte_offset, available_bytes);
  1543. +
  1544. + if (i == page->index) {
  1545. + pageaddr = kmap_atomic(page, KM_USER0);
  1546. + memcpy(pageaddr, data_ptr + byte_offset,
  1547. + available_bytes);
  1548. + memset(pageaddr + available_bytes, 0,
  1549. + PAGE_CACHE_SIZE - available_bytes);
  1550. + kunmap_atomic(pageaddr, KM_USER0);
  1551. + flush_dcache_page(page);
  1552. + SetPageUptodate(page);
  1553. + UnlockPage(page);
  1554. + } else if ((push_page =
  1555. + grab_cache_page_nowait(page->mapping, i))) {
  1556. + pageaddr = kmap_atomic(push_page, KM_USER0);
  1557. +
  1558. + memcpy(pageaddr, data_ptr + byte_offset,
  1559. + available_bytes);
  1560. + memset(pageaddr + available_bytes, 0,
  1561. + PAGE_CACHE_SIZE - available_bytes);
  1562. + kunmap_atomic(pageaddr, KM_USER0);
  1563. + flush_dcache_page(push_page);
  1564. + SetPageUptodate(push_page);
  1565. + UnlockPage(push_page);
  1566. + page_cache_release(push_page);
  1567. + }
  1568. + }
  1569. +
  1570. + if (SQUASHFS_I(inode)->u.s1.fragment_start_block == SQUASHFS_INVALID_BLK
  1571. + || index < (i_size_read(inode) >>
  1572. + sblk->block_log))
  1573. + up(&msblk->read_page_mutex);
  1574. + else
  1575. + release_cached_fragment(msblk, fragment);
  1576. +
  1577. + return 0;
  1578. +
  1579. +skip_read:
  1580. + pageaddr = kmap_atomic(page, KM_USER0);
  1581. + memset(pageaddr + bytes, 0, PAGE_CACHE_SIZE - bytes);
  1582. + kunmap_atomic(pageaddr, KM_USER0);
  1583. + flush_dcache_page(page);
  1584. + SetPageUptodate(page);
  1585. + UnlockPage(page);
  1586. +
  1587. + return 0;
  1588. +}
  1589. +
  1590. +
  1591. +static int squashfs_readpage4K(struct file *file, struct page *page)
  1592. +{
  1593. + struct inode *inode = page->mapping->host;
  1594. + struct squashfs_sb_info *msblk = &inode->i_sb->u.squashfs_sb;
  1595. + struct squashfs_super_block *sblk = &msblk->sblk;
  1596. + unsigned char block_list[SIZE];
  1597. + long long block;
  1598. + unsigned int bsize, bytes = 0;
  1599. + void *pageaddr;
  1600. +
  1601. + TRACE("Entered squashfs_readpage4K, page index %lx, start block %llx\n",
  1602. + page->index,
  1603. + SQUASHFS_I(inode)->start_block);
  1604. +
  1605. + if (page->index >= ((i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
  1606. + PAGE_CACHE_SHIFT)) {
  1607. + pageaddr = kmap_atomic(page, KM_USER0);
  1608. + goto skip_read;
  1609. + }
  1610. +
  1611. + if (SQUASHFS_I(inode)->u.s1.fragment_start_block == SQUASHFS_INVALID_BLK
  1612. + || page->index < (i_size_read(inode) >>
  1613. + sblk->block_log)) {
  1614. + block = (msblk->read_blocklist)(inode, page->index, 1,
  1615. + block_list, NULL, &bsize);
  1616. +
  1617. + down(&msblk->read_page_mutex);
  1618. + bytes = squashfs_read_data(inode->i_sb, msblk->read_page, block,
  1619. + bsize, NULL);
  1620. + pageaddr = kmap_atomic(page, KM_USER0);
  1621. + if (bytes)
  1622. + memcpy(pageaddr, msblk->read_page, bytes);
  1623. + else
  1624. + ERROR("Unable to read page, block %llx, size %x\n",
  1625. + block, bsize);
  1626. + up(&msblk->read_page_mutex);
  1627. + } else {
  1628. + struct squashfs_fragment_cache *fragment =
  1629. + get_cached_fragment(inode->i_sb,
  1630. + SQUASHFS_I(inode)->
  1631. + u.s1.fragment_start_block,
  1632. + SQUASHFS_I(inode)-> u.s1.fragment_size);
  1633. + pageaddr = kmap_atomic(page, KM_USER0);
  1634. + if (fragment) {
  1635. + bytes = i_size_read(inode) & (sblk->block_size - 1);
  1636. + memcpy(pageaddr, fragment->data + SQUASHFS_I(inode)->
  1637. + u.s1.fragment_offset, bytes);
  1638. + release_cached_fragment(msblk, fragment);
  1639. + } else
  1640. + ERROR("Unable to read page, block %llx, size %x\n",
  1641. + SQUASHFS_I(inode)->
  1642. + u.s1.fragment_start_block, (int)
  1643. + SQUASHFS_I(inode)-> u.s1.fragment_size);
  1644. + }
  1645. +
  1646. +skip_read:
  1647. + memset(pageaddr + bytes, 0, PAGE_CACHE_SIZE - bytes);
  1648. + kunmap_atomic(pageaddr, KM_USER0);
  1649. + flush_dcache_page(page);
  1650. + SetPageUptodate(page);
  1651. + UnlockPage(page);
  1652. +
  1653. + return 0;
  1654. +}
  1655. +
  1656. +
  1657. +static int get_dir_index_using_offset(struct super_block *s, long long
  1658. + *next_block, unsigned int *next_offset,
  1659. + long long index_start,
  1660. + unsigned int index_offset, int i_count,
  1661. + long long f_pos)
  1662. +{
  1663. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  1664. + struct squashfs_super_block *sblk = &msblk->sblk;
  1665. + int i, length = 0;
  1666. + struct squashfs_dir_index index;
  1667. +
  1668. + TRACE("Entered get_dir_index_using_offset, i_count %d, f_pos %d\n",
  1669. + i_count, (unsigned int) f_pos);
  1670. +
  1671. + f_pos -= 3;
  1672. + if (f_pos == 0)
  1673. + goto finish;
  1674. +
  1675. + for (i = 0; i < i_count; i++) {
  1676. + if (msblk->swap) {
  1677. + struct squashfs_dir_index sindex;
  1678. + squashfs_get_cached_block(s, (char *) &sindex,
  1679. + index_start, index_offset,
  1680. + sizeof(sindex), &index_start,
  1681. + &index_offset);
  1682. + SQUASHFS_SWAP_DIR_INDEX(&index, &sindex);
  1683. + } else
  1684. + squashfs_get_cached_block(s, (char *) &index,
  1685. + index_start, index_offset,
  1686. + sizeof(index), &index_start,
  1687. + &index_offset);
  1688. +
  1689. + if (index.index > f_pos)
  1690. + break;
  1691. +
  1692. + squashfs_get_cached_block(s, NULL, index_start, index_offset,
  1693. + index.size + 1, &index_start,
  1694. + &index_offset);
  1695. +
  1696. + length = index.index;
  1697. + *next_block = index.start_block + sblk->directory_table_start;
  1698. + }
  1699. +
  1700. + *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
  1701. +
  1702. +finish:
  1703. + return length + 3;
  1704. +}
  1705. +
  1706. +
  1707. +static int get_dir_index_using_name(struct super_block *s, long long
  1708. + *next_block, unsigned int *next_offset,
  1709. + long long index_start,
  1710. + unsigned int index_offset, int i_count,
  1711. + const char *name, int size)
  1712. +{
  1713. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  1714. + struct squashfs_super_block *sblk = &msblk->sblk;
  1715. + int i, length = 0;
  1716. + char buffer[sizeof(struct squashfs_dir_index) + SQUASHFS_NAME_LEN + 1];
  1717. + struct squashfs_dir_index *index = (struct squashfs_dir_index *) buffer;
  1718. + char str[SQUASHFS_NAME_LEN + 1];
  1719. +
  1720. + TRACE("Entered get_dir_index_using_name, i_count %d\n", i_count);
  1721. +
  1722. + strncpy(str, name, size);
  1723. + str[size] = '\0';
  1724. +
  1725. + for (i = 0; i < i_count; i++) {
  1726. + if (msblk->swap) {
  1727. + struct squashfs_dir_index sindex;
  1728. + squashfs_get_cached_block(s, (char *) &sindex,
  1729. + index_start, index_offset,
  1730. + sizeof(sindex), &index_start,
  1731. + &index_offset);
  1732. + SQUASHFS_SWAP_DIR_INDEX(index, &sindex);
  1733. + } else
  1734. + squashfs_get_cached_block(s, (char *) index,
  1735. + index_start, index_offset,
  1736. + sizeof(struct squashfs_dir_index),
  1737. + &index_start, &index_offset);
  1738. +
  1739. + squashfs_get_cached_block(s, index->name, index_start,
  1740. + index_offset, index->size + 1,
  1741. + &index_start, &index_offset);
  1742. +
  1743. + index->name[index->size + 1] = '\0';
  1744. +
  1745. + if (strcmp(index->name, str) > 0)
  1746. + break;
  1747. +
  1748. + length = index->index;
  1749. + *next_block = index->start_block + sblk->directory_table_start;
  1750. + }
  1751. +
  1752. + *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
  1753. + return length + 3;
  1754. +}
  1755. +
  1756. +
  1757. +static int squashfs_readdir(struct file *file, void *dirent, filldir_t filldir)
  1758. +{
  1759. + struct inode *i = file->f_dentry->d_inode;
  1760. + struct squashfs_sb_info *msblk = &i->i_sb->u.squashfs_sb;
  1761. + struct squashfs_super_block *sblk = &msblk->sblk;
  1762. + long long next_block = SQUASHFS_I(i)->start_block +
  1763. + sblk->directory_table_start;
  1764. + int next_offset = SQUASHFS_I(i)->offset, length = 0, dirs_read = 0,
  1765. + dir_count;
  1766. + struct squashfs_dir_header dirh;
  1767. + char buffer[sizeof(struct squashfs_dir_entry) + SQUASHFS_NAME_LEN + 1];
  1768. + struct squashfs_dir_entry *dire = (struct squashfs_dir_entry *) buffer;
  1769. +
  1770. + TRACE("Entered squashfs_readdir [%llx:%x]\n", next_block, next_offset);
  1771. +
  1772. + while(file->f_pos < 3) {
  1773. + char *name;
  1774. + int size, i_ino;
  1775. +
  1776. + if(file->f_pos == 0) {
  1777. + name = ".";
  1778. + size = 1;
  1779. + i_ino = i->i_ino;
  1780. + } else {
  1781. + name = "..";
  1782. + size = 2;
  1783. + i_ino = SQUASHFS_I(i)->u.s2.parent_inode;
  1784. + }
  1785. + TRACE("Calling filldir(%x, %s, %d, %d, %d, %d)\n",
  1786. + (unsigned int) dirent, name, size, (int)
  1787. + file->f_pos, i_ino,
  1788. + squashfs_filetype_table[1]);
  1789. +
  1790. + if (filldir(dirent, name, size,
  1791. + file->f_pos, i_ino,
  1792. + squashfs_filetype_table[1]) < 0) {
  1793. + TRACE("Filldir returned less than 0\n");
  1794. + goto finish;
  1795. + }
  1796. + file->f_pos += size;
  1797. + dirs_read++;
  1798. + }
  1799. +
  1800. + length = get_dir_index_using_offset(i->i_sb, &next_block, &next_offset,
  1801. + SQUASHFS_I(i)->u.s2.directory_index_start,
  1802. + SQUASHFS_I(i)->u.s2.directory_index_offset,
  1803. + SQUASHFS_I(i)->u.s2.directory_index_count,
  1804. + file->f_pos);
  1805. +
  1806. + while (length < i_size_read(i)) {
  1807. + /* read directory header */
  1808. + if (msblk->swap) {
  1809. + struct squashfs_dir_header sdirh;
  1810. +
  1811. + if (!squashfs_get_cached_block(i->i_sb, (char *) &sdirh,
  1812. + next_block, next_offset, sizeof(sdirh),
  1813. + &next_block, &next_offset))
  1814. + goto failed_read;
  1815. +
  1816. + length += sizeof(sdirh);
  1817. + SQUASHFS_SWAP_DIR_HEADER(&dirh, &sdirh);
  1818. + } else {
  1819. + if (!squashfs_get_cached_block(i->i_sb, (char *) &dirh,
  1820. + next_block, next_offset, sizeof(dirh),
  1821. + &next_block, &next_offset))
  1822. + goto failed_read;
  1823. +
  1824. + length += sizeof(dirh);
  1825. + }
  1826. +
  1827. + dir_count = dirh.count + 1;
  1828. + while (dir_count--) {
  1829. + if (msblk->swap) {
  1830. + struct squashfs_dir_entry sdire;
  1831. + if (!squashfs_get_cached_block(i->i_sb, (char *)
  1832. + &sdire, next_block, next_offset,
  1833. + sizeof(sdire), &next_block,
  1834. + &next_offset))
  1835. + goto failed_read;
  1836. +
  1837. + length += sizeof(sdire);
  1838. + SQUASHFS_SWAP_DIR_ENTRY(dire, &sdire);
  1839. + } else {
  1840. + if (!squashfs_get_cached_block(i->i_sb, (char *)
  1841. + dire, next_block, next_offset,
  1842. + sizeof(*dire), &next_block,
  1843. + &next_offset))
  1844. + goto failed_read;
  1845. +
  1846. + length += sizeof(*dire);
  1847. + }
  1848. +
  1849. + if (!squashfs_get_cached_block(i->i_sb, dire->name,
  1850. + next_block, next_offset,
  1851. + dire->size + 1, &next_block,
  1852. + &next_offset))
  1853. + goto failed_read;
  1854. +
  1855. + length += dire->size + 1;
  1856. +
  1857. + if (file->f_pos >= length)
  1858. + continue;
  1859. +
  1860. + dire->name[dire->size + 1] = '\0';
  1861. +
  1862. + TRACE("Calling filldir(%x, %s, %d, %d, %x:%x, %d, %d)\n",
  1863. + (unsigned int) dirent, dire->name,
  1864. + dire->size + 1, (int) file->f_pos,
  1865. + dirh.start_block, dire->offset,
  1866. + dirh.inode_number + dire->inode_number,
  1867. + squashfs_filetype_table[dire->type]);
  1868. +
  1869. + if (filldir(dirent, dire->name, dire->size + 1,
  1870. + file->f_pos,
  1871. + dirh.inode_number + dire->inode_number,
  1872. + squashfs_filetype_table[dire->type])
  1873. + < 0) {
  1874. + TRACE("Filldir returned less than 0\n");
  1875. + goto finish;
  1876. + }
  1877. + file->f_pos = length;
  1878. + dirs_read++;
  1879. + }
  1880. + }
  1881. +
  1882. +finish:
  1883. + return dirs_read;
  1884. +
  1885. +failed_read:
  1886. + ERROR("Unable to read directory block [%llx:%x]\n", next_block,
  1887. + next_offset);
  1888. + return 0;
  1889. +}
  1890. +
  1891. +
  1892. +static struct dentry *squashfs_lookup(struct inode *i, struct dentry *dentry)
  1893. +{
  1894. + const unsigned char *name = dentry->d_name.name;
  1895. + int len = dentry->d_name.len;
  1896. + struct inode *inode = NULL;
  1897. + struct squashfs_sb_info *msblk = &i->i_sb->u.squashfs_sb;
  1898. + struct squashfs_super_block *sblk = &msblk->sblk;
  1899. + long long next_block = SQUASHFS_I(i)->start_block +
  1900. + sblk->directory_table_start;
  1901. + int next_offset = SQUASHFS_I(i)->offset, length = 0,
  1902. + dir_count;
  1903. + struct squashfs_dir_header dirh;
  1904. + char buffer[sizeof(struct squashfs_dir_entry) + SQUASHFS_NAME_LEN];
  1905. + struct squashfs_dir_entry *dire = (struct squashfs_dir_entry *) buffer;
  1906. +
  1907. + TRACE("Entered squashfs_lookup [%llx:%x]\n", next_block, next_offset);
  1908. +
  1909. + if (len > SQUASHFS_NAME_LEN)
  1910. + goto exit_loop;
  1911. +
  1912. + length = get_dir_index_using_name(i->i_sb, &next_block, &next_offset,
  1913. + SQUASHFS_I(i)->u.s2.directory_index_start,
  1914. + SQUASHFS_I(i)->u.s2.directory_index_offset,
  1915. + SQUASHFS_I(i)->u.s2.directory_index_count, name,
  1916. + len);
  1917. +
  1918. + while (length < i_size_read(i)) {
  1919. + /* read directory header */
  1920. + if (msblk->swap) {
  1921. + struct squashfs_dir_header sdirh;
  1922. + if (!squashfs_get_cached_block(i->i_sb, (char *) &sdirh,
  1923. + next_block, next_offset, sizeof(sdirh),
  1924. + &next_block, &next_offset))
  1925. + goto failed_read;
  1926. +
  1927. + length += sizeof(sdirh);
  1928. + SQUASHFS_SWAP_DIR_HEADER(&dirh, &sdirh);
  1929. + } else {
  1930. + if (!squashfs_get_cached_block(i->i_sb, (char *) &dirh,
  1931. + next_block, next_offset, sizeof(dirh),
  1932. + &next_block, &next_offset))
  1933. + goto failed_read;
  1934. +
  1935. + length += sizeof(dirh);
  1936. + }
  1937. +
  1938. + dir_count = dirh.count + 1;
  1939. + while (dir_count--) {
  1940. + if (msblk->swap) {
  1941. + struct squashfs_dir_entry sdire;
  1942. + if (!squashfs_get_cached_block(i->i_sb, (char *)
  1943. + &sdire, next_block,next_offset,
  1944. + sizeof(sdire), &next_block,
  1945. + &next_offset))
  1946. + goto failed_read;
  1947. +
  1948. + length += sizeof(sdire);
  1949. + SQUASHFS_SWAP_DIR_ENTRY(dire, &sdire);
  1950. + } else {
  1951. + if (!squashfs_get_cached_block(i->i_sb, (char *)
  1952. + dire, next_block,next_offset,
  1953. + sizeof(*dire), &next_block,
  1954. + &next_offset))
  1955. + goto failed_read;
  1956. +
  1957. + length += sizeof(*dire);
  1958. + }
  1959. +
  1960. + if (!squashfs_get_cached_block(i->i_sb, dire->name,
  1961. + next_block, next_offset, dire->size + 1,
  1962. + &next_block, &next_offset))
  1963. + goto failed_read;
  1964. +
  1965. + length += dire->size + 1;
  1966. +
  1967. + if (name[0] < dire->name[0])
  1968. + goto exit_loop;
  1969. +
  1970. + if ((len == dire->size + 1) && !strncmp(name,
  1971. + dire->name, len)) {
  1972. + squashfs_inode_t ino =
  1973. + SQUASHFS_MKINODE(dirh.start_block,
  1974. + dire->offset);
  1975. +
  1976. + TRACE("calling squashfs_iget for directory "
  1977. + "entry %s, inode %x:%x, %d\n", name,
  1978. + dirh.start_block, dire->offset,
  1979. + dirh.inode_number + dire->inode_number);
  1980. +
  1981. + inode = (msblk->iget)(i->i_sb, ino);
  1982. +
  1983. + goto exit_loop;
  1984. + }
  1985. + }
  1986. + }
  1987. +
  1988. +exit_loop:
  1989. + d_add(dentry, inode);
  1990. + return ERR_PTR(0);
  1991. +
  1992. +failed_read:
  1993. + ERROR("Unable to read directory block [%llx:%x]\n", next_block,
  1994. + next_offset);
  1995. + goto exit_loop;
  1996. +}
  1997. +
  1998. +
  1999. +static void squashfs_put_super(struct super_block *s)
  2000. +{
  2001. + int i;
  2002. +
  2003. + struct squashfs_sb_info *sbi = &s->u.squashfs_sb;
  2004. + if (sbi->block_cache)
  2005. + for (i = 0; i < SQUASHFS_CACHED_BLKS; i++)
  2006. + if (sbi->block_cache[i].block !=
  2007. + SQUASHFS_INVALID_BLK)
  2008. + kfree(sbi->block_cache[i].data);
  2009. + if (sbi->fragment)
  2010. + for (i = 0; i < SQUASHFS_CACHED_FRAGMENTS; i++)
  2011. + SQUASHFS_FREE(sbi->fragment[i].data);
  2012. + kfree(sbi->fragment);
  2013. + kfree(sbi->block_cache);
  2014. + kfree(sbi->read_data);
  2015. + kfree(sbi->read_page);
  2016. + kfree(sbi->uid);
  2017. + kfree(sbi->fragment_index);
  2018. + kfree(sbi->fragment_index_2);
  2019. + sbi->block_cache = NULL;
  2020. + sbi->uid = NULL;
  2021. + sbi->read_data = NULL;
  2022. + sbi->read_page = NULL;
  2023. + sbi->fragment = NULL;
  2024. + sbi->fragment_index = NULL;
  2025. + sbi->fragment_index_2 = NULL;
  2026. +}
  2027. +
  2028. +
  2029. +static int __init init_squashfs_fs(void)
  2030. +{
  2031. +
  2032. + printk(KERN_INFO "squashfs: version 3.0 (2006/03/15) "
  2033. + "Phillip Lougher\n");
  2034. +
  2035. + if (!(stream.workspace = vmalloc(zlib_inflate_workspacesize()))) {
  2036. + ERROR("Failed to allocate zlib workspace\n");
  2037. + return -ENOMEM;
  2038. + }
  2039. + return register_filesystem(&squashfs_fs_type);
  2040. +}
  2041. +
  2042. +
  2043. +static void __exit exit_squashfs_fs(void)
  2044. +{
  2045. + vfree(stream.workspace);
  2046. + unregister_filesystem(&squashfs_fs_type);
  2047. +}
  2048. +
  2049. +
  2050. +EXPORT_NO_SYMBOLS;
  2051. +
  2052. +module_init(init_squashfs_fs);
  2053. +module_exit(exit_squashfs_fs);
  2054. +MODULE_DESCRIPTION("squashfs, a compressed read-only filesystem");
  2055. +MODULE_AUTHOR("Phillip Lougher <[email protected]>");
  2056. +MODULE_LICENSE("GPL");
  2057. --- /dev/null
  2058. +++ b/fs/squashfs/Makefile
  2059. @@ -0,0 +1,11 @@
  2060. +#
  2061. +# Makefile for the linux squashfs routines.
  2062. +#
  2063. +
  2064. +O_TARGET := squashfs.o
  2065. +
  2066. +obj-y := inode.o squashfs2_0.o
  2067. +
  2068. +obj-m := $(O_TARGET)
  2069. +
  2070. +include $(TOPDIR)/Rules.make
  2071. --- /dev/null
  2072. +++ b/fs/squashfs/squashfs2_0.c
  2073. @@ -0,0 +1,751 @@
  2074. +/*
  2075. + * Squashfs - a compressed read only filesystem for Linux
  2076. + *
  2077. + * Copyright (c) 2002, 2003, 2004, 2005, 2006
  2078. + * Phillip Lougher <[email protected]>
  2079. + *
  2080. + * This program is free software; you can redistribute it and/or
  2081. + * modify it under the terms of the GNU General Public License
  2082. + * as published by the Free Software Foundation; either version 2,
  2083. + * or (at your option) any later version.
  2084. + *
  2085. + * This program is distributed in the hope that it will be useful,
  2086. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2087. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2088. + * GNU General Public License for more details.
  2089. + *
  2090. + * You should have received a copy of the GNU General Public License
  2091. + * along with this program; if not, write to the Free Software
  2092. + * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  2093. + *
  2094. + * squashfs2_0.c
  2095. + */
  2096. +
  2097. +#include <linux/types.h>
  2098. +#include <linux/squashfs_fs.h>
  2099. +#include <linux/module.h>
  2100. +#include <linux/errno.h>
  2101. +#include <linux/slab.h>
  2102. +#include <linux/fs.h>
  2103. +#include <linux/smp_lock.h>
  2104. +#include <linux/locks.h>
  2105. +#include <linux/init.h>
  2106. +#include <linux/dcache.h>
  2107. +#include <linux/wait.h>
  2108. +#include <linux/zlib.h>
  2109. +#include <linux/blkdev.h>
  2110. +#include <linux/vmalloc.h>
  2111. +#include <asm/uaccess.h>
  2112. +#include <asm/semaphore.h>
  2113. +#include "squashfs.h"
  2114. +
  2115. +static int squashfs_readdir_2(struct file *file, void *dirent, filldir_t filldir);
  2116. +static struct dentry *squashfs_lookup_2(struct inode *i, struct dentry *dentry);
  2117. +
  2118. +static struct file_operations squashfs_dir_ops_2 = {
  2119. + .read = generic_read_dir,
  2120. + .readdir = squashfs_readdir_2
  2121. +};
  2122. +
  2123. +static struct inode_operations squashfs_dir_inode_ops_2 = {
  2124. + .lookup = squashfs_lookup_2
  2125. +};
  2126. +
  2127. +static unsigned char squashfs_filetype_table[] = {
  2128. + DT_UNKNOWN, DT_DIR, DT_REG, DT_LNK, DT_BLK, DT_CHR, DT_FIFO, DT_SOCK
  2129. +};
  2130. +
  2131. +static int read_fragment_index_table_2(struct super_block *s)
  2132. +{
  2133. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  2134. + struct squashfs_super_block *sblk = &msblk->sblk;
  2135. +
  2136. + if (!(msblk->fragment_index_2 = kmalloc(SQUASHFS_FRAGMENT_INDEX_BYTES_2
  2137. + (sblk->fragments), GFP_KERNEL))) {
  2138. + ERROR("Failed to allocate uid/gid table\n");
  2139. + return 0;
  2140. + }
  2141. +
  2142. + if (SQUASHFS_FRAGMENT_INDEX_BYTES_2(sblk->fragments) &&
  2143. + !squashfs_read_data(s, (char *)
  2144. + msblk->fragment_index_2,
  2145. + sblk->fragment_table_start,
  2146. + SQUASHFS_FRAGMENT_INDEX_BYTES_2
  2147. + (sblk->fragments) |
  2148. + SQUASHFS_COMPRESSED_BIT_BLOCK, NULL)) {
  2149. + ERROR("unable to read fragment index table\n");
  2150. + return 0;
  2151. + }
  2152. +
  2153. + if (msblk->swap) {
  2154. + int i;
  2155. + unsigned int fragment;
  2156. +
  2157. + for (i = 0; i < SQUASHFS_FRAGMENT_INDEXES_2(sblk->fragments);
  2158. + i++) {
  2159. + SQUASHFS_SWAP_FRAGMENT_INDEXES_2((&fragment),
  2160. + &msblk->fragment_index_2[i], 1);
  2161. + msblk->fragment_index_2[i] = fragment;
  2162. + }
  2163. + }
  2164. +
  2165. + return 1;
  2166. +}
  2167. +
  2168. +
  2169. +static int get_fragment_location_2(struct super_block *s, unsigned int fragment,
  2170. + long long *fragment_start_block,
  2171. + unsigned int *fragment_size)
  2172. +{
  2173. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  2174. + long long start_block =
  2175. + msblk->fragment_index_2[SQUASHFS_FRAGMENT_INDEX_2(fragment)];
  2176. + int offset = SQUASHFS_FRAGMENT_INDEX_OFFSET_2(fragment);
  2177. + struct squashfs_fragment_entry_2 fragment_entry;
  2178. +
  2179. + if (msblk->swap) {
  2180. + struct squashfs_fragment_entry_2 sfragment_entry;
  2181. +
  2182. + if (!squashfs_get_cached_block(s, (char *) &sfragment_entry,
  2183. + start_block, offset,
  2184. + sizeof(sfragment_entry), &start_block,
  2185. + &offset))
  2186. + goto out;
  2187. + SQUASHFS_SWAP_FRAGMENT_ENTRY_2(&fragment_entry, &sfragment_entry);
  2188. + } else
  2189. + if (!squashfs_get_cached_block(s, (char *) &fragment_entry,
  2190. + start_block, offset,
  2191. + sizeof(fragment_entry), &start_block,
  2192. + &offset))
  2193. + goto out;
  2194. +
  2195. + *fragment_start_block = fragment_entry.start_block;
  2196. + *fragment_size = fragment_entry.size;
  2197. +
  2198. + return 1;
  2199. +
  2200. +out:
  2201. + return 0;
  2202. +}
  2203. +
  2204. +
  2205. +static struct inode *squashfs_new_inode(struct super_block *s,
  2206. + struct squashfs_base_inode_header_2 *inodeb, unsigned int ino)
  2207. +{
  2208. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  2209. + struct squashfs_super_block *sblk = &msblk->sblk;
  2210. + struct inode *i = new_inode(s);
  2211. +
  2212. + if (i) {
  2213. + i->i_ino = ino;
  2214. + i->i_mtime = sblk->mkfs_time;
  2215. + i->i_atime = sblk->mkfs_time;
  2216. + i->i_ctime = sblk->mkfs_time;
  2217. + i->i_uid = msblk->uid[inodeb->uid];
  2218. + i->i_mode = inodeb->mode;
  2219. + i->i_nlink = 1;
  2220. + i->i_size = 0;
  2221. + if (inodeb->guid == SQUASHFS_GUIDS)
  2222. + i->i_gid = i->i_uid;
  2223. + else
  2224. + i->i_gid = msblk->guid[inodeb->guid];
  2225. + }
  2226. +
  2227. + return i;
  2228. +}
  2229. +
  2230. +
  2231. +static struct inode *squashfs_iget_2(struct super_block *s, squashfs_inode_t inode)
  2232. +{
  2233. + struct inode *i;
  2234. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  2235. + struct squashfs_super_block *sblk = &msblk->sblk;
  2236. + unsigned int block = SQUASHFS_INODE_BLK(inode) +
  2237. + sblk->inode_table_start;
  2238. + unsigned int offset = SQUASHFS_INODE_OFFSET(inode);
  2239. + unsigned int ino = SQUASHFS_MK_VFS_INODE(block
  2240. + - sblk->inode_table_start, offset);
  2241. + long long next_block;
  2242. + unsigned int next_offset;
  2243. + union squashfs_inode_header_2 id, sid;
  2244. + struct squashfs_base_inode_header_2 *inodeb = &id.base,
  2245. + *sinodeb = &sid.base;
  2246. +
  2247. + TRACE("Entered squashfs_iget\n");
  2248. +
  2249. + if (msblk->swap) {
  2250. + if (!squashfs_get_cached_block(s, (char *) sinodeb, block,
  2251. + offset, sizeof(*sinodeb), &next_block,
  2252. + &next_offset))
  2253. + goto failed_read;
  2254. + SQUASHFS_SWAP_BASE_INODE_HEADER_2(inodeb, sinodeb,
  2255. + sizeof(*sinodeb));
  2256. + } else
  2257. + if (!squashfs_get_cached_block(s, (char *) inodeb, block,
  2258. + offset, sizeof(*inodeb), &next_block,
  2259. + &next_offset))
  2260. + goto failed_read;
  2261. +
  2262. + switch(inodeb->inode_type) {
  2263. + case SQUASHFS_FILE_TYPE: {
  2264. + struct squashfs_reg_inode_header_2 *inodep = &id.reg;
  2265. + struct squashfs_reg_inode_header_2 *sinodep = &sid.reg;
  2266. + long long frag_blk;
  2267. + unsigned int frag_size;
  2268. +
  2269. + if (msblk->swap) {
  2270. + if (!squashfs_get_cached_block(s, (char *)
  2271. + sinodep, block, offset,
  2272. + sizeof(*sinodep), &next_block,
  2273. + &next_offset))
  2274. + goto failed_read;
  2275. + SQUASHFS_SWAP_REG_INODE_HEADER_2(inodep, sinodep);
  2276. + } else
  2277. + if (!squashfs_get_cached_block(s, (char *)
  2278. + inodep, block, offset,
  2279. + sizeof(*inodep), &next_block,
  2280. + &next_offset))
  2281. + goto failed_read;
  2282. +
  2283. + frag_blk = SQUASHFS_INVALID_BLK;
  2284. + if (inodep->fragment != SQUASHFS_INVALID_FRAG &&
  2285. + !get_fragment_location_2(s,
  2286. + inodep->fragment, &frag_blk, &frag_size))
  2287. + goto failed_read;
  2288. +
  2289. + if((i = squashfs_new_inode(s, inodeb, ino)) == NULL)
  2290. + goto failed_read1;
  2291. +
  2292. + i->i_size = inodep->file_size;
  2293. + i->i_fop = &generic_ro_fops;
  2294. + i->i_mode |= S_IFREG;
  2295. + i->i_mtime = inodep->mtime;
  2296. + i->i_atime = inodep->mtime;
  2297. + i->i_ctime = inodep->mtime;
  2298. + i->i_blocks = ((i->i_size - 1) >> 9) + 1;
  2299. + i->i_blksize = PAGE_CACHE_SIZE;
  2300. + SQUASHFS_I(i)->u.s1.fragment_start_block = frag_blk;
  2301. + SQUASHFS_I(i)->u.s1.fragment_size = frag_size;
  2302. + SQUASHFS_I(i)->u.s1.fragment_offset = inodep->offset;
  2303. + SQUASHFS_I(i)->start_block = inodep->start_block;
  2304. + SQUASHFS_I(i)->u.s1.block_list_start = next_block;
  2305. + SQUASHFS_I(i)->offset = next_offset;
  2306. + if (sblk->block_size > 4096)
  2307. + i->i_data.a_ops = &squashfs_aops;
  2308. + else
  2309. + i->i_data.a_ops = &squashfs_aops_4K;
  2310. +
  2311. + TRACE("File inode %x:%x, start_block %x, "
  2312. + "block_list_start %llx, offset %x\n",
  2313. + SQUASHFS_INODE_BLK(inode), offset,
  2314. + inodep->start_block, next_block,
  2315. + next_offset);
  2316. + break;
  2317. + }
  2318. + case SQUASHFS_DIR_TYPE: {
  2319. + struct squashfs_dir_inode_header_2 *inodep = &id.dir;
  2320. + struct squashfs_dir_inode_header_2 *sinodep = &sid.dir;
  2321. +
  2322. + if (msblk->swap) {
  2323. + if (!squashfs_get_cached_block(s, (char *)
  2324. + sinodep, block, offset,
  2325. + sizeof(*sinodep), &next_block,
  2326. + &next_offset))
  2327. + goto failed_read;
  2328. + SQUASHFS_SWAP_DIR_INODE_HEADER_2(inodep, sinodep);
  2329. + } else
  2330. + if (!squashfs_get_cached_block(s, (char *)
  2331. + inodep, block, offset,
  2332. + sizeof(*inodep), &next_block,
  2333. + &next_offset))
  2334. + goto failed_read;
  2335. +
  2336. + if((i = squashfs_new_inode(s, inodeb, ino)) == NULL)
  2337. + goto failed_read1;
  2338. +
  2339. + i->i_size = inodep->file_size;
  2340. + i->i_op = &squashfs_dir_inode_ops_2;
  2341. + i->i_fop = &squashfs_dir_ops_2;
  2342. + i->i_mode |= S_IFDIR;
  2343. + i->i_mtime = inodep->mtime;
  2344. + i->i_atime = inodep->mtime;
  2345. + i->i_ctime = inodep->mtime;
  2346. + SQUASHFS_I(i)->start_block = inodep->start_block;
  2347. + SQUASHFS_I(i)->offset = inodep->offset;
  2348. + SQUASHFS_I(i)->u.s2.directory_index_count = 0;
  2349. + SQUASHFS_I(i)->u.s2.parent_inode = 0;
  2350. +
  2351. + TRACE("Directory inode %x:%x, start_block %x, offset "
  2352. + "%x\n", SQUASHFS_INODE_BLK(inode),
  2353. + offset, inodep->start_block,
  2354. + inodep->offset);
  2355. + break;
  2356. + }
  2357. + case SQUASHFS_LDIR_TYPE: {
  2358. + struct squashfs_ldir_inode_header_2 *inodep = &id.ldir;
  2359. + struct squashfs_ldir_inode_header_2 *sinodep = &sid.ldir;
  2360. +
  2361. + if (msblk->swap) {
  2362. + if (!squashfs_get_cached_block(s, (char *)
  2363. + sinodep, block, offset,
  2364. + sizeof(*sinodep), &next_block,
  2365. + &next_offset))
  2366. + goto failed_read;
  2367. + SQUASHFS_SWAP_LDIR_INODE_HEADER_2(inodep,
  2368. + sinodep);
  2369. + } else
  2370. + if (!squashfs_get_cached_block(s, (char *)
  2371. + inodep, block, offset,
  2372. + sizeof(*inodep), &next_block,
  2373. + &next_offset))
  2374. + goto failed_read;
  2375. +
  2376. + if((i = squashfs_new_inode(s, inodeb, ino)) == NULL)
  2377. + goto failed_read1;
  2378. +
  2379. + i->i_size = inodep->file_size;
  2380. + i->i_op = &squashfs_dir_inode_ops_2;
  2381. + i->i_fop = &squashfs_dir_ops_2;
  2382. + i->i_mode |= S_IFDIR;
  2383. + i->i_mtime = inodep->mtime;
  2384. + i->i_atime = inodep->mtime;
  2385. + i->i_ctime = inodep->mtime;
  2386. + SQUASHFS_I(i)->start_block = inodep->start_block;
  2387. + SQUASHFS_I(i)->offset = inodep->offset;
  2388. + SQUASHFS_I(i)->u.s2.directory_index_start = next_block;
  2389. + SQUASHFS_I(i)->u.s2.directory_index_offset =
  2390. + next_offset;
  2391. + SQUASHFS_I(i)->u.s2.directory_index_count =
  2392. + inodep->i_count;
  2393. + SQUASHFS_I(i)->u.s2.parent_inode = 0;
  2394. +
  2395. + TRACE("Long directory inode %x:%x, start_block %x, "
  2396. + "offset %x\n",
  2397. + SQUASHFS_INODE_BLK(inode), offset,
  2398. + inodep->start_block, inodep->offset);
  2399. + break;
  2400. + }
  2401. + case SQUASHFS_SYMLINK_TYPE: {
  2402. + struct squashfs_symlink_inode_header_2 *inodep =
  2403. + &id.symlink;
  2404. + struct squashfs_symlink_inode_header_2 *sinodep =
  2405. + &sid.symlink;
  2406. +
  2407. + if (msblk->swap) {
  2408. + if (!squashfs_get_cached_block(s, (char *)
  2409. + sinodep, block, offset,
  2410. + sizeof(*sinodep), &next_block,
  2411. + &next_offset))
  2412. + goto failed_read;
  2413. + SQUASHFS_SWAP_SYMLINK_INODE_HEADER_2(inodep,
  2414. + sinodep);
  2415. + } else
  2416. + if (!squashfs_get_cached_block(s, (char *)
  2417. + inodep, block, offset,
  2418. + sizeof(*inodep), &next_block,
  2419. + &next_offset))
  2420. + goto failed_read;
  2421. +
  2422. + if((i = squashfs_new_inode(s, inodeb, ino)) == NULL)
  2423. + goto failed_read1;
  2424. +
  2425. + i->i_size = inodep->symlink_size;
  2426. + i->i_op = &page_symlink_inode_operations;
  2427. + i->i_data.a_ops = &squashfs_symlink_aops;
  2428. + i->i_mode |= S_IFLNK;
  2429. + SQUASHFS_I(i)->start_block = next_block;
  2430. + SQUASHFS_I(i)->offset = next_offset;
  2431. +
  2432. + TRACE("Symbolic link inode %x:%x, start_block %llx, "
  2433. + "offset %x\n",
  2434. + SQUASHFS_INODE_BLK(inode), offset,
  2435. + next_block, next_offset);
  2436. + break;
  2437. + }
  2438. + case SQUASHFS_BLKDEV_TYPE:
  2439. + case SQUASHFS_CHRDEV_TYPE: {
  2440. + struct squashfs_dev_inode_header_2 *inodep = &id.dev;
  2441. + struct squashfs_dev_inode_header_2 *sinodep = &sid.dev;
  2442. +
  2443. + if (msblk->swap) {
  2444. + if (!squashfs_get_cached_block(s, (char *)
  2445. + sinodep, block, offset,
  2446. + sizeof(*sinodep), &next_block,
  2447. + &next_offset))
  2448. + goto failed_read;
  2449. + SQUASHFS_SWAP_DEV_INODE_HEADER_2(inodep, sinodep);
  2450. + } else
  2451. + if (!squashfs_get_cached_block(s, (char *)
  2452. + inodep, block, offset,
  2453. + sizeof(*inodep), &next_block,
  2454. + &next_offset))
  2455. + goto failed_read;
  2456. +
  2457. + if ((i = squashfs_new_inode(s, inodeb, ino)) == NULL)
  2458. + goto failed_read1;
  2459. +
  2460. + i->i_mode |= (inodeb->inode_type ==
  2461. + SQUASHFS_CHRDEV_TYPE) ? S_IFCHR :
  2462. + S_IFBLK;
  2463. + init_special_inode(i, i->i_mode, inodep->rdev);
  2464. +
  2465. + TRACE("Device inode %x:%x, rdev %x\n",
  2466. + SQUASHFS_INODE_BLK(inode), offset,
  2467. + inodep->rdev);
  2468. + break;
  2469. + }
  2470. + case SQUASHFS_FIFO_TYPE:
  2471. + case SQUASHFS_SOCKET_TYPE: {
  2472. + if ((i = squashfs_new_inode(s, inodeb, ino)) == NULL)
  2473. + goto failed_read1;
  2474. +
  2475. + i->i_mode |= (inodeb->inode_type == SQUASHFS_FIFO_TYPE)
  2476. + ? S_IFIFO : S_IFSOCK;
  2477. + init_special_inode(i, i->i_mode, 0);
  2478. + break;
  2479. + }
  2480. + default:
  2481. + ERROR("Unknown inode type %d in squashfs_iget!\n",
  2482. + inodeb->inode_type);
  2483. + goto failed_read1;
  2484. + }
  2485. +
  2486. + insert_inode_hash(i);
  2487. + return i;
  2488. +
  2489. +failed_read:
  2490. + ERROR("Unable to read inode [%x:%x]\n", block, offset);
  2491. +
  2492. +failed_read1:
  2493. + return NULL;
  2494. +}
  2495. +
  2496. +
  2497. +static int get_dir_index_using_offset(struct super_block *s, long long
  2498. + *next_block, unsigned int *next_offset,
  2499. + long long index_start,
  2500. + unsigned int index_offset, int i_count,
  2501. + long long f_pos)
  2502. +{
  2503. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  2504. + struct squashfs_super_block *sblk = &msblk->sblk;
  2505. + int i, length = 0;
  2506. + struct squashfs_dir_index_2 index;
  2507. +
  2508. + TRACE("Entered get_dir_index_using_offset, i_count %d, f_pos %d\n",
  2509. + i_count, (unsigned int) f_pos);
  2510. +
  2511. + if (f_pos == 0)
  2512. + goto finish;
  2513. +
  2514. + for (i = 0; i < i_count; i++) {
  2515. + if (msblk->swap) {
  2516. + struct squashfs_dir_index_2 sindex;
  2517. + squashfs_get_cached_block(s, (char *) &sindex,
  2518. + index_start, index_offset,
  2519. + sizeof(sindex), &index_start,
  2520. + &index_offset);
  2521. + SQUASHFS_SWAP_DIR_INDEX_2(&index, &sindex);
  2522. + } else
  2523. + squashfs_get_cached_block(s, (char *) &index,
  2524. + index_start, index_offset,
  2525. + sizeof(index), &index_start,
  2526. + &index_offset);
  2527. +
  2528. + if (index.index > f_pos)
  2529. + break;
  2530. +
  2531. + squashfs_get_cached_block(s, NULL, index_start, index_offset,
  2532. + index.size + 1, &index_start,
  2533. + &index_offset);
  2534. +
  2535. + length = index.index;
  2536. + *next_block = index.start_block + sblk->directory_table_start;
  2537. + }
  2538. +
  2539. + *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
  2540. +
  2541. +finish:
  2542. + return length;
  2543. +}
  2544. +
  2545. +
  2546. +static int get_dir_index_using_name(struct super_block *s, long long
  2547. + *next_block, unsigned int *next_offset,
  2548. + long long index_start,
  2549. + unsigned int index_offset, int i_count,
  2550. + const char *name, int size)
  2551. +{
  2552. + struct squashfs_sb_info *msblk = &s->u.squashfs_sb;
  2553. + struct squashfs_super_block *sblk = &msblk->sblk;
  2554. + int i, length = 0;
  2555. + char buffer[sizeof(struct squashfs_dir_index_2) + SQUASHFS_NAME_LEN + 1];
  2556. + struct squashfs_dir_index_2 *index = (struct squashfs_dir_index_2 *) buffer;
  2557. + char str[SQUASHFS_NAME_LEN + 1];
  2558. +
  2559. + TRACE("Entered get_dir_index_using_name, i_count %d\n", i_count);
  2560. +
  2561. + strncpy(str, name, size);
  2562. + str[size] = '\0';
  2563. +
  2564. + for (i = 0; i < i_count; i++) {
  2565. + if (msblk->swap) {
  2566. + struct squashfs_dir_index_2 sindex;
  2567. + squashfs_get_cached_block(s, (char *) &sindex,
  2568. + index_start, index_offset,
  2569. + sizeof(sindex), &index_start,
  2570. + &index_offset);
  2571. + SQUASHFS_SWAP_DIR_INDEX_2(index, &sindex);
  2572. + } else
  2573. + squashfs_get_cached_block(s, (char *) index,
  2574. + index_start, index_offset,
  2575. + sizeof(struct squashfs_dir_index_2),
  2576. + &index_start, &index_offset);
  2577. +
  2578. + squashfs_get_cached_block(s, index->name, index_start,
  2579. + index_offset, index->size + 1,
  2580. + &index_start, &index_offset);
  2581. +
  2582. + index->name[index->size + 1] = '\0';
  2583. +
  2584. + if (strcmp(index->name, str) > 0)
  2585. + break;
  2586. +
  2587. + length = index->index;
  2588. + *next_block = index->start_block + sblk->directory_table_start;
  2589. + }
  2590. +
  2591. + *next_offset = (length + *next_offset) % SQUASHFS_METADATA_SIZE;
  2592. + return length;
  2593. +}
  2594. +
  2595. +
  2596. +static int squashfs_readdir_2(struct file *file, void *dirent, filldir_t filldir)
  2597. +{
  2598. + struct inode *i = file->f_dentry->d_inode;
  2599. + struct squashfs_sb_info *msblk = &i->i_sb->u.squashfs_sb;
  2600. + struct squashfs_super_block *sblk = &msblk->sblk;
  2601. + long long next_block = SQUASHFS_I(i)->start_block +
  2602. + sblk->directory_table_start;
  2603. + int next_offset = SQUASHFS_I(i)->offset, length = 0, dirs_read = 0,
  2604. + dir_count;
  2605. + struct squashfs_dir_header_2 dirh;
  2606. + char buffer[sizeof(struct squashfs_dir_entry_2) + SQUASHFS_NAME_LEN + 1];
  2607. + struct squashfs_dir_entry_2 *dire = (struct squashfs_dir_entry_2 *) buffer;
  2608. +
  2609. + TRACE("Entered squashfs_readdir_2 [%llx:%x]\n", next_block, next_offset);
  2610. +
  2611. + length = get_dir_index_using_offset(i->i_sb, &next_block, &next_offset,
  2612. + SQUASHFS_I(i)->u.s2.directory_index_start,
  2613. + SQUASHFS_I(i)->u.s2.directory_index_offset,
  2614. + SQUASHFS_I(i)->u.s2.directory_index_count,
  2615. + file->f_pos);
  2616. +
  2617. + while (length < i_size_read(i)) {
  2618. + /* read directory header */
  2619. + if (msblk->swap) {
  2620. + struct squashfs_dir_header_2 sdirh;
  2621. +
  2622. + if (!squashfs_get_cached_block(i->i_sb, (char *) &sdirh,
  2623. + next_block, next_offset, sizeof(sdirh),
  2624. + &next_block, &next_offset))
  2625. + goto failed_read;
  2626. +
  2627. + length += sizeof(sdirh);
  2628. + SQUASHFS_SWAP_DIR_HEADER_2(&dirh, &sdirh);
  2629. + } else {
  2630. + if (!squashfs_get_cached_block(i->i_sb, (char *) &dirh,
  2631. + next_block, next_offset, sizeof(dirh),
  2632. + &next_block, &next_offset))
  2633. + goto failed_read;
  2634. +
  2635. + length += sizeof(dirh);
  2636. + }
  2637. +
  2638. + dir_count = dirh.count + 1;
  2639. + while (dir_count--) {
  2640. + if (msblk->swap) {
  2641. + struct squashfs_dir_entry_2 sdire;
  2642. + if (!squashfs_get_cached_block(i->i_sb, (char *)
  2643. + &sdire, next_block, next_offset,
  2644. + sizeof(sdire), &next_block,
  2645. + &next_offset))
  2646. + goto failed_read;
  2647. +
  2648. + length += sizeof(sdire);
  2649. + SQUASHFS_SWAP_DIR_ENTRY_2(dire, &sdire);
  2650. + } else {
  2651. + if (!squashfs_get_cached_block(i->i_sb, (char *)
  2652. + dire, next_block, next_offset,
  2653. + sizeof(*dire), &next_block,
  2654. + &next_offset))
  2655. + goto failed_read;
  2656. +
  2657. + length += sizeof(*dire);
  2658. + }
  2659. +
  2660. + if (!squashfs_get_cached_block(i->i_sb, dire->name,
  2661. + next_block, next_offset,
  2662. + dire->size + 1, &next_block,
  2663. + &next_offset))
  2664. + goto failed_read;
  2665. +
  2666. + length += dire->size + 1;
  2667. +
  2668. + if (file->f_pos >= length)
  2669. + continue;
  2670. +
  2671. + dire->name[dire->size + 1] = '\0';
  2672. +
  2673. + TRACE("Calling filldir(%x, %s, %d, %d, %x:%x, %d)\n",
  2674. + (unsigned int) dirent, dire->name,
  2675. + dire->size + 1, (int) file->f_pos,
  2676. + dirh.start_block, dire->offset,
  2677. + squashfs_filetype_table[dire->type]);
  2678. +
  2679. + if (filldir(dirent, dire->name, dire->size + 1,
  2680. + file->f_pos, SQUASHFS_MK_VFS_INODE(
  2681. + dirh.start_block, dire->offset),
  2682. + squashfs_filetype_table[dire->type])
  2683. + < 0) {
  2684. + TRACE("Filldir returned less than 0\n");
  2685. + goto finish;
  2686. + }
  2687. + file->f_pos = length;
  2688. + dirs_read++;
  2689. + }
  2690. + }
  2691. +
  2692. +finish:
  2693. + return dirs_read;
  2694. +
  2695. +failed_read:
  2696. + ERROR("Unable to read directory block [%llx:%x]\n", next_block,
  2697. + next_offset);
  2698. + return 0;
  2699. +}
  2700. +
  2701. +
  2702. +static struct dentry *squashfs_lookup_2(struct inode *i, struct dentry *dentry)
  2703. +{
  2704. + const unsigned char *name = dentry->d_name.name;
  2705. + int len = dentry->d_name.len;
  2706. + struct inode *inode = NULL;
  2707. + struct squashfs_sb_info *msblk = &i->i_sb->u.squashfs_sb;
  2708. + struct squashfs_super_block *sblk = &msblk->sblk;
  2709. + long long next_block = SQUASHFS_I(i)->start_block +
  2710. + sblk->directory_table_start;
  2711. + int next_offset = SQUASHFS_I(i)->offset, length = 0,
  2712. + dir_count;
  2713. + struct squashfs_dir_header_2 dirh;
  2714. + char buffer[sizeof(struct squashfs_dir_entry_2) + SQUASHFS_NAME_LEN];
  2715. + struct squashfs_dir_entry_2 *dire = (struct squashfs_dir_entry_2 *) buffer;
  2716. + int sorted = sblk->s_major == 2 && sblk->s_minor >= 1;
  2717. +
  2718. + TRACE("Entered squashfs_lookup [%llx:%x]\n", next_block, next_offset);
  2719. +
  2720. + if (len > SQUASHFS_NAME_LEN)
  2721. + goto exit_loop;
  2722. +
  2723. + length = get_dir_index_using_name(i->i_sb, &next_block, &next_offset,
  2724. + SQUASHFS_I(i)->u.s2.directory_index_start,
  2725. + SQUASHFS_I(i)->u.s2.directory_index_offset,
  2726. + SQUASHFS_I(i)->u.s2.directory_index_count, name,
  2727. + len);
  2728. +
  2729. + while (length < i_size_read(i)) {
  2730. + /* read directory header */
  2731. + if (msblk->swap) {
  2732. + struct squashfs_dir_header_2 sdirh;
  2733. + if (!squashfs_get_cached_block(i->i_sb, (char *) &sdirh,
  2734. + next_block, next_offset, sizeof(sdirh),
  2735. + &next_block, &next_offset))
  2736. + goto failed_read;
  2737. +
  2738. + length += sizeof(sdirh);
  2739. + SQUASHFS_SWAP_DIR_HEADER_2(&dirh, &sdirh);
  2740. + } else {
  2741. + if (!squashfs_get_cached_block(i->i_sb, (char *) &dirh,
  2742. + next_block, next_offset, sizeof(dirh),
  2743. + &next_block, &next_offset))
  2744. + goto failed_read;
  2745. +
  2746. + length += sizeof(dirh);
  2747. + }
  2748. +
  2749. + dir_count = dirh.count + 1;
  2750. + while (dir_count--) {
  2751. + if (msblk->swap) {
  2752. + struct squashfs_dir_entry_2 sdire;
  2753. + if (!squashfs_get_cached_block(i->i_sb, (char *)
  2754. + &sdire, next_block,next_offset,
  2755. + sizeof(sdire), &next_block,
  2756. + &next_offset))
  2757. + goto failed_read;
  2758. +
  2759. + length += sizeof(sdire);
  2760. + SQUASHFS_SWAP_DIR_ENTRY_2(dire, &sdire);
  2761. + } else {
  2762. + if (!squashfs_get_cached_block(i->i_sb, (char *)
  2763. + dire, next_block,next_offset,
  2764. + sizeof(*dire), &next_block,
  2765. + &next_offset))
  2766. + goto failed_read;
  2767. +
  2768. + length += sizeof(*dire);
  2769. + }
  2770. +
  2771. + if (!squashfs_get_cached_block(i->i_sb, dire->name,
  2772. + next_block, next_offset, dire->size + 1,
  2773. + &next_block, &next_offset))
  2774. + goto failed_read;
  2775. +
  2776. + length += dire->size + 1;
  2777. +
  2778. + if (sorted && name[0] < dire->name[0])
  2779. + goto exit_loop;
  2780. +
  2781. + if ((len == dire->size + 1) && !strncmp(name,
  2782. + dire->name, len)) {
  2783. + squashfs_inode_t ino =
  2784. + SQUASHFS_MKINODE(dirh.start_block,
  2785. + dire->offset);
  2786. +
  2787. + TRACE("calling squashfs_iget for directory "
  2788. + "entry %s, inode %x:%x, %d\n", name,
  2789. + dirh.start_block, dire->offset, ino);
  2790. +
  2791. + inode = (msblk->iget)(i->i_sb, ino);
  2792. +
  2793. + goto exit_loop;
  2794. + }
  2795. + }
  2796. + }
  2797. +
  2798. +exit_loop:
  2799. + d_add(dentry, inode);
  2800. + return ERR_PTR(0);
  2801. +
  2802. +failed_read:
  2803. + ERROR("Unable to read directory block [%llx:%x]\n", next_block,
  2804. + next_offset);
  2805. + goto exit_loop;
  2806. +}
  2807. +
  2808. +
  2809. +int squashfs_2_0_supported(struct squashfs_sb_info *msblk)
  2810. +{
  2811. + struct squashfs_super_block *sblk = &msblk->sblk;
  2812. +
  2813. + msblk->iget = squashfs_iget_2;
  2814. + msblk->read_fragment_index_table = read_fragment_index_table_2;
  2815. +
  2816. + sblk->bytes_used = sblk->bytes_used_2;
  2817. + sblk->uid_start = sblk->uid_start_2;
  2818. + sblk->guid_start = sblk->guid_start_2;
  2819. + sblk->inode_table_start = sblk->inode_table_start_2;
  2820. + sblk->directory_table_start = sblk->directory_table_start_2;
  2821. + sblk->fragment_table_start = sblk->fragment_table_start_2;
  2822. +
  2823. + return 1;
  2824. +}
  2825. --- /dev/null
  2826. +++ b/fs/squashfs/squashfs.h
  2827. @@ -0,0 +1,85 @@
  2828. +/*
  2829. + * Squashfs - a compressed read only filesystem for Linux
  2830. + *
  2831. + * Copyright (c) 2002, 2003, 2004, 2005, 2006
  2832. + * Phillip Lougher <[email protected]>
  2833. + *
  2834. + * This program is free software; you can redistribute it and/or
  2835. + * modify it under the terms of the GNU General Public License
  2836. + * as published by the Free Software Foundation; either version 2,
  2837. + * or (at your option) any later version.
  2838. + *
  2839. + * This program is distributed in the hope that it will be useful,
  2840. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2841. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2842. + * GNU General Public License for more details.
  2843. + *
  2844. + * You should have received a copy of the GNU General Public License
  2845. + * along with this program; if not, write to the Free Software
  2846. + * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  2847. + *
  2848. + * squashfs.h
  2849. + */
  2850. +
  2851. +#ifdef CONFIG_SQUASHFS_1_0_COMPATIBILITY
  2852. +#undef CONFIG_SQUASHFS_1_0_COMPATIBILITY
  2853. +#endif
  2854. +#ifdef SQUASHFS_TRACE
  2855. +#define TRACE(s, args...) printk(KERN_NOTICE "SQUASHFS: "s, ## args)
  2856. +#else
  2857. +#define TRACE(s, args...) {}
  2858. +#endif
  2859. +
  2860. +#define ERROR(s, args...) printk(KERN_ERR "SQUASHFS error: "s, ## args)
  2861. +
  2862. +#define SERROR(s, args...) do { \
  2863. + if (!silent) \
  2864. + printk(KERN_ERR "SQUASHFS error: "s, ## args);\
  2865. + } while(0)
  2866. +
  2867. +#define WARNING(s, args...) printk(KERN_WARNING "SQUASHFS: "s, ## args)
  2868. +
  2869. +#define SQUASHFS_I(INO) (&INO->u.squashfs_i)
  2870. +
  2871. +#define i_size_read(INO) (INO->i_size)
  2872. +
  2873. +#if defined(CONFIG_SQUASHFS_1_0_COMPATIBILITY ) || defined(CONFIG_SQUASHFS_2_0_COMPATIBILITY)
  2874. +#define SQSH_EXTERN
  2875. +extern unsigned int squashfs_read_data(struct super_block *s, char *buffer,
  2876. + long long index, unsigned int length,
  2877. + long long *next_index);
  2878. +extern int squashfs_get_cached_block(struct super_block *s, char *buffer,
  2879. + long long block, unsigned int offset,
  2880. + int length, long long *next_block,
  2881. + unsigned int *next_offset);
  2882. +extern void release_cached_fragment(struct squashfs_sb_info *msblk, struct
  2883. + squashfs_fragment_cache *fragment);
  2884. +extern struct squashfs_fragment_cache *get_cached_fragment(struct super_block
  2885. + *s, long long start_block,
  2886. + int length);
  2887. +extern struct address_space_operations squashfs_symlink_aops;
  2888. +extern struct address_space_operations squashfs_aops;
  2889. +extern struct address_space_operations squashfs_aops_4K;
  2890. +extern struct file_operations squashfs_dir_ops;
  2891. +extern struct inode_operations squashfs_dir_inode_ops;
  2892. +#else
  2893. +#define SQSH_EXTERN static
  2894. +#endif
  2895. +
  2896. +#ifdef CONFIG_SQUASHFS_1_0_COMPATIBILITY
  2897. +extern int squashfs_1_0_supported(struct squashfs_sb_info *msblk);
  2898. +#else
  2899. +static inline int squashfs_1_0_supported(struct squashfs_sb_info *msblk)
  2900. +{
  2901. + return 0;
  2902. +}
  2903. +#endif
  2904. +
  2905. +#ifdef CONFIG_SQUASHFS_2_0_COMPATIBILITY
  2906. +extern int squashfs_2_0_supported(struct squashfs_sb_info *msblk);
  2907. +#else
  2908. +static inline int squashfs_2_0_supported(struct squashfs_sb_info *msblk)
  2909. +{
  2910. + return 0;
  2911. +}
  2912. +#endif
  2913. --- a/include/linux/fs.h
  2914. +++ b/include/linux/fs.h
  2915. @@ -324,6 +324,7 @@ extern void set_bh_page(struct buffer_he
  2916. #include <linux/usbdev_fs_i.h>
  2917. #include <linux/jffs2_fs_i.h>
  2918. #include <linux/cramfs_fs_sb.h>
  2919. +#include <linux/squashfs_fs_i.h>
  2920. /*
  2921. * Attribute flags. These should be or-ed together to figure out what
  2922. @@ -519,6 +520,7 @@ struct inode {
  2923. struct socket socket_i;
  2924. struct usbdev_inode_info usbdev_i;
  2925. struct jffs2_inode_info jffs2_i;
  2926. + struct squashfs_inode_info squashfs_i;
  2927. void *generic_ip;
  2928. } u;
  2929. };
  2930. @@ -736,6 +738,7 @@ struct nameidata {
  2931. #include <linux/usbdev_fs_sb.h>
  2932. #include <linux/cramfs_fs_sb.h>
  2933. #include <linux/jffs2_fs_sb.h>
  2934. +#include <linux/squashfs_fs_sb.h>
  2935. extern struct list_head super_blocks;
  2936. extern spinlock_t sb_lock;
  2937. @@ -795,6 +798,7 @@ struct super_block {
  2938. struct usbdev_sb_info usbdevfs_sb;
  2939. struct jffs2_sb_info jffs2_sb;
  2940. struct cramfs_sb_info cramfs_sb;
  2941. + struct squashfs_sb_info squashfs_sb;
  2942. void *generic_sbp;
  2943. } u;
  2944. /*
  2945. --- /dev/null
  2946. +++ b/include/linux/squashfs_fs.h
  2947. @@ -0,0 +1,915 @@
  2948. +#ifndef SQUASHFS_FS
  2949. +#define SQUASHFS_FS
  2950. +
  2951. +/*
  2952. + * Squashfs
  2953. + *
  2954. + * Copyright (c) 2002, 2003, 2004, 2005, 2006
  2955. + * Phillip Lougher <[email protected]>
  2956. + *
  2957. + * This program is free software; you can redistribute it and/or
  2958. + * modify it under the terms of the GNU General Public License
  2959. + * as published by the Free Software Foundation; either version 2,
  2960. + * or (at your option) any later version.
  2961. + *
  2962. + * This program is distributed in the hope that it will be useful,
  2963. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  2964. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  2965. + * GNU General Public License for more details.
  2966. + *
  2967. + * You should have received a copy of the GNU General Public License
  2968. + * along with this program; if not, write to the Free Software
  2969. + * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  2970. + *
  2971. + * squashfs_fs.h
  2972. + */
  2973. +
  2974. +#ifndef CONFIG_SQUASHFS_2_0_COMPATIBILITY
  2975. +#define CONFIG_SQUASHFS_2_0_COMPATIBILITY
  2976. +#endif
  2977. +
  2978. +#ifdef CONFIG_SQUASHFS_VMALLOC
  2979. +#define SQUASHFS_ALLOC(a) vmalloc(a)
  2980. +#define SQUASHFS_FREE(a) vfree(a)
  2981. +#else
  2982. +#define SQUASHFS_ALLOC(a) kmalloc(a, GFP_KERNEL)
  2983. +#define SQUASHFS_FREE(a) kfree(a)
  2984. +#endif
  2985. +#ifdef CONFIG_SQUASHFS_FRAGMENT_CACHE_SIZE
  2986. +#define SQUASHFS_CACHED_FRAGMENTS CONFIG_SQUASHFS_FRAGMENT_CACHE_SIZE
  2987. +#else
  2988. +#define SQUASHFS_CACHED_FRAGMENTS 3
  2989. +#endif
  2990. +#define SQUASHFS_MAJOR 3
  2991. +#define SQUASHFS_MINOR 0
  2992. +#define SQUASHFS_MAGIC 0x73717368
  2993. +#define SQUASHFS_MAGIC_SWAP 0x68737173
  2994. +#define SQUASHFS_START 0
  2995. +
  2996. +/* size of metadata (inode and directory) blocks */
  2997. +#define SQUASHFS_METADATA_SIZE 8192
  2998. +#define SQUASHFS_METADATA_LOG 13
  2999. +
  3000. +/* default size of data blocks */
  3001. +#define SQUASHFS_FILE_SIZE 65536
  3002. +#define SQUASHFS_FILE_LOG 16
  3003. +
  3004. +#define SQUASHFS_FILE_MAX_SIZE 65536
  3005. +
  3006. +/* Max number of uids and gids */
  3007. +#define SQUASHFS_UIDS 256
  3008. +#define SQUASHFS_GUIDS 255
  3009. +
  3010. +/* Max length of filename (not 255) */
  3011. +#define SQUASHFS_NAME_LEN 256
  3012. +
  3013. +#define SQUASHFS_INVALID ((long long) 0xffffffffffff)
  3014. +#define SQUASHFS_INVALID_FRAG ((unsigned int) 0xffffffff)
  3015. +#define SQUASHFS_INVALID_BLK ((long long) -1)
  3016. +#define SQUASHFS_USED_BLK ((long long) -2)
  3017. +
  3018. +/* Filesystem flags */
  3019. +#define SQUASHFS_NOI 0
  3020. +#define SQUASHFS_NOD 1
  3021. +#define SQUASHFS_CHECK 2
  3022. +#define SQUASHFS_NOF 3
  3023. +#define SQUASHFS_NO_FRAG 4
  3024. +#define SQUASHFS_ALWAYS_FRAG 5
  3025. +#define SQUASHFS_DUPLICATE 6
  3026. +
  3027. +#define SQUASHFS_BIT(flag, bit) ((flag >> bit) & 1)
  3028. +
  3029. +#define SQUASHFS_UNCOMPRESSED_INODES(flags) SQUASHFS_BIT(flags, \
  3030. + SQUASHFS_NOI)
  3031. +
  3032. +#define SQUASHFS_UNCOMPRESSED_DATA(flags) SQUASHFS_BIT(flags, \
  3033. + SQUASHFS_NOD)
  3034. +
  3035. +#define SQUASHFS_UNCOMPRESSED_FRAGMENTS(flags) SQUASHFS_BIT(flags, \
  3036. + SQUASHFS_NOF)
  3037. +
  3038. +#define SQUASHFS_NO_FRAGMENTS(flags) SQUASHFS_BIT(flags, \
  3039. + SQUASHFS_NO_FRAG)
  3040. +
  3041. +#define SQUASHFS_ALWAYS_FRAGMENTS(flags) SQUASHFS_BIT(flags, \
  3042. + SQUASHFS_ALWAYS_FRAG)
  3043. +
  3044. +#define SQUASHFS_DUPLICATES(flags) SQUASHFS_BIT(flags, \
  3045. + SQUASHFS_DUPLICATE)
  3046. +
  3047. +#define SQUASHFS_CHECK_DATA(flags) SQUASHFS_BIT(flags, \
  3048. + SQUASHFS_CHECK)
  3049. +
  3050. +#define SQUASHFS_MKFLAGS(noi, nod, check_data, nof, no_frag, always_frag, \
  3051. + duplicate_checking) (noi | (nod << 1) | (check_data << 2) \
  3052. + | (nof << 3) | (no_frag << 4) | (always_frag << 5) | \
  3053. + (duplicate_checking << 6))
  3054. +
  3055. +/* Max number of types and file types */
  3056. +#define SQUASHFS_DIR_TYPE 1
  3057. +#define SQUASHFS_FILE_TYPE 2
  3058. +#define SQUASHFS_SYMLINK_TYPE 3
  3059. +#define SQUASHFS_BLKDEV_TYPE 4
  3060. +#define SQUASHFS_CHRDEV_TYPE 5
  3061. +#define SQUASHFS_FIFO_TYPE 6
  3062. +#define SQUASHFS_SOCKET_TYPE 7
  3063. +#define SQUASHFS_LDIR_TYPE 8
  3064. +#define SQUASHFS_LREG_TYPE 9
  3065. +
  3066. +/* 1.0 filesystem type definitions */
  3067. +#define SQUASHFS_TYPES 5
  3068. +#define SQUASHFS_IPC_TYPE 0
  3069. +
  3070. +/* Flag whether block is compressed or uncompressed, bit is set if block is
  3071. + * uncompressed */
  3072. +#define SQUASHFS_COMPRESSED_BIT (1 << 15)
  3073. +
  3074. +#define SQUASHFS_COMPRESSED_SIZE(B) (((B) & ~SQUASHFS_COMPRESSED_BIT) ? \
  3075. + (B) & ~SQUASHFS_COMPRESSED_BIT : SQUASHFS_COMPRESSED_BIT)
  3076. +
  3077. +#define SQUASHFS_COMPRESSED(B) (!((B) & SQUASHFS_COMPRESSED_BIT))
  3078. +
  3079. +#define SQUASHFS_COMPRESSED_BIT_BLOCK (1 << 24)
  3080. +
  3081. +#define SQUASHFS_COMPRESSED_SIZE_BLOCK(B) (((B) & \
  3082. + ~SQUASHFS_COMPRESSED_BIT_BLOCK) ? (B) & \
  3083. + ~SQUASHFS_COMPRESSED_BIT_BLOCK : SQUASHFS_COMPRESSED_BIT_BLOCK)
  3084. +
  3085. +#define SQUASHFS_COMPRESSED_BLOCK(B) (!((B) & SQUASHFS_COMPRESSED_BIT_BLOCK))
  3086. +
  3087. +/*
  3088. + * Inode number ops. Inodes consist of a compressed block number, and an
  3089. + * uncompressed offset within that block
  3090. + */
  3091. +#define SQUASHFS_INODE_BLK(a) ((unsigned int) ((a) >> 16))
  3092. +
  3093. +#define SQUASHFS_INODE_OFFSET(a) ((unsigned int) ((a) & 0xffff))
  3094. +
  3095. +#define SQUASHFS_MKINODE(A, B) ((squashfs_inode_t)(((squashfs_inode_t) (A)\
  3096. + << 16) + (B)))
  3097. +
  3098. +/* Compute 32 bit VFS inode number from squashfs inode number */
  3099. +#define SQUASHFS_MK_VFS_INODE(a, b) ((unsigned int) (((a) << 8) + \
  3100. + ((b) >> 2) + 1))
  3101. +/* XXX */
  3102. +
  3103. +/* Translate between VFS mode and squashfs mode */
  3104. +#define SQUASHFS_MODE(a) ((a) & 0xfff)
  3105. +
  3106. +/* fragment and fragment table defines */
  3107. +#define SQUASHFS_FRAGMENT_BYTES(A) (A * sizeof(struct squashfs_fragment_entry))
  3108. +
  3109. +#define SQUASHFS_FRAGMENT_INDEX(A) (SQUASHFS_FRAGMENT_BYTES(A) / \
  3110. + SQUASHFS_METADATA_SIZE)
  3111. +
  3112. +#define SQUASHFS_FRAGMENT_INDEX_OFFSET(A) (SQUASHFS_FRAGMENT_BYTES(A) % \
  3113. + SQUASHFS_METADATA_SIZE)
  3114. +
  3115. +#define SQUASHFS_FRAGMENT_INDEXES(A) ((SQUASHFS_FRAGMENT_BYTES(A) + \
  3116. + SQUASHFS_METADATA_SIZE - 1) / \
  3117. + SQUASHFS_METADATA_SIZE)
  3118. +
  3119. +#define SQUASHFS_FRAGMENT_INDEX_BYTES(A) (SQUASHFS_FRAGMENT_INDEXES(A) *\
  3120. + sizeof(long long))
  3121. +
  3122. +/* cached data constants for filesystem */
  3123. +#define SQUASHFS_CACHED_BLKS 8
  3124. +
  3125. +#define SQUASHFS_MAX_FILE_SIZE_LOG 64
  3126. +
  3127. +#define SQUASHFS_MAX_FILE_SIZE ((long long) 1 << \
  3128. + (SQUASHFS_MAX_FILE_SIZE_LOG - 2))
  3129. +
  3130. +#define SQUASHFS_MARKER_BYTE 0xff
  3131. +
  3132. +/* meta index cache */
  3133. +#define SQUASHFS_META_INDEXES (SQUASHFS_METADATA_SIZE / sizeof(unsigned int))
  3134. +#define SQUASHFS_META_ENTRIES 31
  3135. +#define SQUASHFS_META_NUMBER 8
  3136. +#define SQUASHFS_SLOTS 4
  3137. +
  3138. +struct meta_entry {
  3139. + long long data_block;
  3140. + unsigned int index_block;
  3141. + unsigned short offset;
  3142. + unsigned short pad;
  3143. +};
  3144. +
  3145. +struct meta_index {
  3146. + unsigned int inode_number;
  3147. + unsigned int offset;
  3148. + unsigned short entries;
  3149. + unsigned short skip;
  3150. + unsigned short locked;
  3151. + unsigned short pad;
  3152. + struct meta_entry meta_entry[SQUASHFS_META_ENTRIES];
  3153. +};
  3154. +
  3155. +
  3156. +/*
  3157. + * definitions for structures on disk
  3158. + */
  3159. +
  3160. +typedef long long squashfs_block_t;
  3161. +typedef long long squashfs_inode_t;
  3162. +
  3163. +struct squashfs_super_block {
  3164. + unsigned int s_magic;
  3165. + unsigned int inodes;
  3166. + unsigned int bytes_used_2;
  3167. + unsigned int uid_start_2;
  3168. + unsigned int guid_start_2;
  3169. + unsigned int inode_table_start_2;
  3170. + unsigned int directory_table_start_2;
  3171. + unsigned int s_major:16;
  3172. + unsigned int s_minor:16;
  3173. + unsigned int block_size_1:16;
  3174. + unsigned int block_log:16;
  3175. + unsigned int flags:8;
  3176. + unsigned int no_uids:8;
  3177. + unsigned int no_guids:8;
  3178. + unsigned int mkfs_time /* time of filesystem creation */;
  3179. + squashfs_inode_t root_inode;
  3180. + unsigned int block_size;
  3181. + unsigned int fragments;
  3182. + unsigned int fragment_table_start_2;
  3183. + long long bytes_used;
  3184. + long long uid_start;
  3185. + long long guid_start;
  3186. + long long inode_table_start;
  3187. + long long directory_table_start;
  3188. + long long fragment_table_start;
  3189. + long long unused;
  3190. +} __attribute__ ((packed));
  3191. +
  3192. +struct squashfs_dir_index {
  3193. + unsigned int index;
  3194. + unsigned int start_block;
  3195. + unsigned char size;
  3196. + unsigned char name[0];
  3197. +} __attribute__ ((packed));
  3198. +
  3199. +#define SQUASHFS_BASE_INODE_HEADER \
  3200. + unsigned int inode_type:4; \
  3201. + unsigned int mode:12; \
  3202. + unsigned int uid:8; \
  3203. + unsigned int guid:8; \
  3204. + unsigned int mtime; \
  3205. + unsigned int inode_number;
  3206. +
  3207. +struct squashfs_base_inode_header {
  3208. + SQUASHFS_BASE_INODE_HEADER;
  3209. +} __attribute__ ((packed));
  3210. +
  3211. +struct squashfs_ipc_inode_header {
  3212. + SQUASHFS_BASE_INODE_HEADER;
  3213. + unsigned int nlink;
  3214. +} __attribute__ ((packed));
  3215. +
  3216. +struct squashfs_dev_inode_header {
  3217. + SQUASHFS_BASE_INODE_HEADER;
  3218. + unsigned int nlink;
  3219. + unsigned short rdev;
  3220. +} __attribute__ ((packed));
  3221. +
  3222. +struct squashfs_symlink_inode_header {
  3223. + SQUASHFS_BASE_INODE_HEADER;
  3224. + unsigned int nlink;
  3225. + unsigned short symlink_size;
  3226. + char symlink[0];
  3227. +} __attribute__ ((packed));
  3228. +
  3229. +struct squashfs_reg_inode_header {
  3230. + SQUASHFS_BASE_INODE_HEADER;
  3231. + squashfs_block_t start_block;
  3232. + unsigned int fragment;
  3233. + unsigned int offset;
  3234. + unsigned int file_size;
  3235. + unsigned short block_list[0];
  3236. +} __attribute__ ((packed));
  3237. +
  3238. +struct squashfs_lreg_inode_header {
  3239. + SQUASHFS_BASE_INODE_HEADER;
  3240. + unsigned int nlink;
  3241. + squashfs_block_t start_block;
  3242. + unsigned int fragment;
  3243. + unsigned int offset;
  3244. + long long file_size;
  3245. + unsigned short block_list[0];
  3246. +} __attribute__ ((packed));
  3247. +
  3248. +struct squashfs_dir_inode_header {
  3249. + SQUASHFS_BASE_INODE_HEADER;
  3250. + unsigned int nlink;
  3251. + unsigned int file_size:19;
  3252. + unsigned int offset:13;
  3253. + unsigned int start_block;
  3254. + unsigned int parent_inode;
  3255. +} __attribute__ ((packed));
  3256. +
  3257. +struct squashfs_ldir_inode_header {
  3258. + SQUASHFS_BASE_INODE_HEADER;
  3259. + unsigned int nlink;
  3260. + unsigned int file_size:27;
  3261. + unsigned int offset:13;
  3262. + unsigned int start_block;
  3263. + unsigned int i_count:16;
  3264. + unsigned int parent_inode;
  3265. + struct squashfs_dir_index index[0];
  3266. +} __attribute__ ((packed));
  3267. +
  3268. +union squashfs_inode_header {
  3269. + struct squashfs_base_inode_header base;
  3270. + struct squashfs_dev_inode_header dev;
  3271. + struct squashfs_symlink_inode_header symlink;
  3272. + struct squashfs_reg_inode_header reg;
  3273. + struct squashfs_lreg_inode_header lreg;
  3274. + struct squashfs_dir_inode_header dir;
  3275. + struct squashfs_ldir_inode_header ldir;
  3276. + struct squashfs_ipc_inode_header ipc;
  3277. +};
  3278. +
  3279. +struct squashfs_dir_entry {
  3280. + unsigned int offset:13;
  3281. + unsigned int type:3;
  3282. + unsigned int size:8;
  3283. + int inode_number:16;
  3284. + char name[0];
  3285. +} __attribute__ ((packed));
  3286. +
  3287. +struct squashfs_dir_header {
  3288. + unsigned int count:8;
  3289. + unsigned int start_block;
  3290. + unsigned int inode_number;
  3291. +} __attribute__ ((packed));
  3292. +
  3293. +struct squashfs_fragment_entry {
  3294. + long long start_block;
  3295. + unsigned int size;
  3296. + unsigned int unused;
  3297. +} __attribute__ ((packed));
  3298. +
  3299. +extern int squashfs_uncompress_block(void *d, int dstlen, void *s, int srclen);
  3300. +extern int squashfs_uncompress_init(void);
  3301. +extern int squashfs_uncompress_exit(void);
  3302. +
  3303. +/*
  3304. + * macros to convert each packed bitfield structure from little endian to big
  3305. + * endian and vice versa. These are needed when creating or using a filesystem
  3306. + * on a machine with different byte ordering to the target architecture.
  3307. + *
  3308. + */
  3309. +
  3310. +#define SQUASHFS_SWAP_START \
  3311. + int bits;\
  3312. + int b_pos;\
  3313. + unsigned long long val;\
  3314. + unsigned char *s;\
  3315. + unsigned char *d;
  3316. +
  3317. +#define SQUASHFS_SWAP_SUPER_BLOCK(s, d) {\
  3318. + SQUASHFS_SWAP_START\
  3319. + SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_super_block));\
  3320. + SQUASHFS_SWAP((s)->s_magic, d, 0, 32);\
  3321. + SQUASHFS_SWAP((s)->inodes, d, 32, 32);\
  3322. + SQUASHFS_SWAP((s)->bytes_used_2, d, 64, 32);\
  3323. + SQUASHFS_SWAP((s)->uid_start_2, d, 96, 32);\
  3324. + SQUASHFS_SWAP((s)->guid_start_2, d, 128, 32);\
  3325. + SQUASHFS_SWAP((s)->inode_table_start_2, d, 160, 32);\
  3326. + SQUASHFS_SWAP((s)->directory_table_start_2, d, 192, 32);\
  3327. + SQUASHFS_SWAP((s)->s_major, d, 224, 16);\
  3328. + SQUASHFS_SWAP((s)->s_minor, d, 240, 16);\
  3329. + SQUASHFS_SWAP((s)->block_size_1, d, 256, 16);\
  3330. + SQUASHFS_SWAP((s)->block_log, d, 272, 16);\
  3331. + SQUASHFS_SWAP((s)->flags, d, 288, 8);\
  3332. + SQUASHFS_SWAP((s)->no_uids, d, 296, 8);\
  3333. + SQUASHFS_SWAP((s)->no_guids, d, 304, 8);\
  3334. + SQUASHFS_SWAP((s)->mkfs_time, d, 312, 32);\
  3335. + SQUASHFS_SWAP((s)->root_inode, d, 344, 64);\
  3336. + SQUASHFS_SWAP((s)->block_size, d, 408, 32);\
  3337. + SQUASHFS_SWAP((s)->fragments, d, 440, 32);\
  3338. + SQUASHFS_SWAP((s)->fragment_table_start_2, d, 472, 32);\
  3339. + SQUASHFS_SWAP((s)->bytes_used, d, 504, 64);\
  3340. + SQUASHFS_SWAP((s)->uid_start, d, 568, 64);\
  3341. + SQUASHFS_SWAP((s)->guid_start, d, 632, 64);\
  3342. + SQUASHFS_SWAP((s)->inode_table_start, d, 696, 64);\
  3343. + SQUASHFS_SWAP((s)->directory_table_start, d, 760, 64);\
  3344. + SQUASHFS_SWAP((s)->fragment_table_start, d, 824, 64);\
  3345. + SQUASHFS_SWAP((s)->unused, d, 888, 64);\
  3346. +}
  3347. +
  3348. +#define SQUASHFS_SWAP_BASE_INODE_CORE(s, d, n)\
  3349. + SQUASHFS_MEMSET(s, d, n);\
  3350. + SQUASHFS_SWAP((s)->inode_type, d, 0, 4);\
  3351. + SQUASHFS_SWAP((s)->mode, d, 4, 12);\
  3352. + SQUASHFS_SWAP((s)->uid, d, 16, 8);\
  3353. + SQUASHFS_SWAP((s)->guid, d, 24, 8);\
  3354. + SQUASHFS_SWAP((s)->mtime, d, 32, 32);\
  3355. + SQUASHFS_SWAP((s)->inode_number, d, 64, 32);
  3356. +
  3357. +#define SQUASHFS_SWAP_BASE_INODE_HEADER(s, d, n) {\
  3358. + SQUASHFS_SWAP_START\
  3359. + SQUASHFS_SWAP_BASE_INODE_CORE(s, d, n)\
  3360. +}
  3361. +
  3362. +#define SQUASHFS_SWAP_IPC_INODE_HEADER(s, d) {\
  3363. + SQUASHFS_SWAP_START\
  3364. + SQUASHFS_SWAP_BASE_INODE_CORE(s, d, \
  3365. + sizeof(struct squashfs_ipc_inode_header))\
  3366. + SQUASHFS_SWAP((s)->nlink, d, 96, 32);\
  3367. +}
  3368. +
  3369. +#define SQUASHFS_SWAP_DEV_INODE_HEADER(s, d) {\
  3370. + SQUASHFS_SWAP_START\
  3371. + SQUASHFS_SWAP_BASE_INODE_CORE(s, d, \
  3372. + sizeof(struct squashfs_dev_inode_header)); \
  3373. + SQUASHFS_SWAP((s)->nlink, d, 96, 32);\
  3374. + SQUASHFS_SWAP((s)->rdev, d, 128, 16);\
  3375. +}
  3376. +
  3377. +#define SQUASHFS_SWAP_SYMLINK_INODE_HEADER(s, d) {\
  3378. + SQUASHFS_SWAP_START\
  3379. + SQUASHFS_SWAP_BASE_INODE_CORE(s, d, \
  3380. + sizeof(struct squashfs_symlink_inode_header));\
  3381. + SQUASHFS_SWAP((s)->nlink, d, 96, 32);\
  3382. + SQUASHFS_SWAP((s)->symlink_size, d, 128, 16);\
  3383. +}
  3384. +
  3385. +#define SQUASHFS_SWAP_REG_INODE_HEADER(s, d) {\
  3386. + SQUASHFS_SWAP_START\
  3387. + SQUASHFS_SWAP_BASE_INODE_CORE(s, d, \
  3388. + sizeof(struct squashfs_reg_inode_header));\
  3389. + SQUASHFS_SWAP((s)->start_block, d, 96, 64);\
  3390. + SQUASHFS_SWAP((s)->fragment, d, 160, 32);\
  3391. + SQUASHFS_SWAP((s)->offset, d, 192, 32);\
  3392. + SQUASHFS_SWAP((s)->file_size, d, 224, 32);\
  3393. +}
  3394. +
  3395. +#define SQUASHFS_SWAP_LREG_INODE_HEADER(s, d) {\
  3396. + SQUASHFS_SWAP_START\
  3397. + SQUASHFS_SWAP_BASE_INODE_CORE(s, d, \
  3398. + sizeof(struct squashfs_lreg_inode_header));\
  3399. + SQUASHFS_SWAP((s)->nlink, d, 96, 32);\
  3400. + SQUASHFS_SWAP((s)->start_block, d, 128, 64);\
  3401. + SQUASHFS_SWAP((s)->fragment, d, 192, 32);\
  3402. + SQUASHFS_SWAP((s)->offset, d, 224, 32);\
  3403. + SQUASHFS_SWAP((s)->file_size, d, 256, 64);\
  3404. +}
  3405. +
  3406. +#define SQUASHFS_SWAP_DIR_INODE_HEADER(s, d) {\
  3407. + SQUASHFS_SWAP_START\
  3408. + SQUASHFS_SWAP_BASE_INODE_CORE(s, d, \
  3409. + sizeof(struct squashfs_dir_inode_header));\
  3410. + SQUASHFS_SWAP((s)->nlink, d, 96, 32);\
  3411. + SQUASHFS_SWAP((s)->file_size, d, 128, 19);\
  3412. + SQUASHFS_SWAP((s)->offset, d, 147, 13);\
  3413. + SQUASHFS_SWAP((s)->start_block, d, 160, 32);\
  3414. + SQUASHFS_SWAP((s)->parent_inode, d, 192, 32);\
  3415. +}
  3416. +
  3417. +#define SQUASHFS_SWAP_LDIR_INODE_HEADER(s, d) {\
  3418. + SQUASHFS_SWAP_START\
  3419. + SQUASHFS_SWAP_BASE_INODE_CORE(s, d, \
  3420. + sizeof(struct squashfs_ldir_inode_header));\
  3421. + SQUASHFS_SWAP((s)->nlink, d, 96, 32);\
  3422. + SQUASHFS_SWAP((s)->file_size, d, 128, 27);\
  3423. + SQUASHFS_SWAP((s)->offset, d, 155, 13);\
  3424. + SQUASHFS_SWAP((s)->start_block, d, 168, 32);\
  3425. + SQUASHFS_SWAP((s)->i_count, d, 200, 16);\
  3426. + SQUASHFS_SWAP((s)->parent_inode, d, 216, 32);\
  3427. +}
  3428. +
  3429. +#define SQUASHFS_SWAP_DIR_INDEX(s, d) {\
  3430. + SQUASHFS_SWAP_START\
  3431. + SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_dir_index));\
  3432. + SQUASHFS_SWAP((s)->index, d, 0, 32);\
  3433. + SQUASHFS_SWAP((s)->start_block, d, 32, 32);\
  3434. + SQUASHFS_SWAP((s)->size, d, 64, 8);\
  3435. +}
  3436. +
  3437. +#define SQUASHFS_SWAP_DIR_HEADER(s, d) {\
  3438. + SQUASHFS_SWAP_START\
  3439. + SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_dir_header));\
  3440. + SQUASHFS_SWAP((s)->count, d, 0, 8);\
  3441. + SQUASHFS_SWAP((s)->start_block, d, 8, 32);\
  3442. + SQUASHFS_SWAP((s)->inode_number, d, 40, 32);\
  3443. +}
  3444. +
  3445. +#define SQUASHFS_SWAP_DIR_ENTRY(s, d) {\
  3446. + SQUASHFS_SWAP_START\
  3447. + SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_dir_entry));\
  3448. + SQUASHFS_SWAP((s)->offset, d, 0, 13);\
  3449. + SQUASHFS_SWAP((s)->type, d, 13, 3);\
  3450. + SQUASHFS_SWAP((s)->size, d, 16, 8);\
  3451. + SQUASHFS_SWAP((s)->inode_number, d, 24, 16);\
  3452. +}
  3453. +
  3454. +#define SQUASHFS_SWAP_FRAGMENT_ENTRY(s, d) {\
  3455. + SQUASHFS_SWAP_START\
  3456. + SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_fragment_entry));\
  3457. + SQUASHFS_SWAP((s)->start_block, d, 0, 64);\
  3458. + SQUASHFS_SWAP((s)->size, d, 64, 32);\
  3459. +}
  3460. +
  3461. +#define SQUASHFS_SWAP_SHORTS(s, d, n) {\
  3462. + int entry;\
  3463. + int bit_position;\
  3464. + SQUASHFS_SWAP_START\
  3465. + SQUASHFS_MEMSET(s, d, n * 2);\
  3466. + for(entry = 0, bit_position = 0; entry < n; entry++, bit_position += \
  3467. + 16)\
  3468. + SQUASHFS_SWAP(s[entry], d, bit_position, 16);\
  3469. +}
  3470. +
  3471. +#define SQUASHFS_SWAP_INTS(s, d, n) {\
  3472. + int entry;\
  3473. + int bit_position;\
  3474. + SQUASHFS_SWAP_START\
  3475. + SQUASHFS_MEMSET(s, d, n * 4);\
  3476. + for(entry = 0, bit_position = 0; entry < n; entry++, bit_position += \
  3477. + 32)\
  3478. + SQUASHFS_SWAP(s[entry], d, bit_position, 32);\
  3479. +}
  3480. +
  3481. +#define SQUASHFS_SWAP_LONG_LONGS(s, d, n) {\
  3482. + int entry;\
  3483. + int bit_position;\
  3484. + SQUASHFS_SWAP_START\
  3485. + SQUASHFS_MEMSET(s, d, n * 8);\
  3486. + for(entry = 0, bit_position = 0; entry < n; entry++, bit_position += \
  3487. + 64)\
  3488. + SQUASHFS_SWAP(s[entry], d, bit_position, 64);\
  3489. +}
  3490. +
  3491. +#define SQUASHFS_SWAP_DATA(s, d, n, bits) {\
  3492. + int entry;\
  3493. + int bit_position;\
  3494. + SQUASHFS_SWAP_START\
  3495. + SQUASHFS_MEMSET(s, d, n * bits / 8);\
  3496. + for(entry = 0, bit_position = 0; entry < n; entry++, bit_position += \
  3497. + bits)\
  3498. + SQUASHFS_SWAP(s[entry], d, bit_position, bits);\
  3499. +}
  3500. +
  3501. +#define SQUASHFS_SWAP_FRAGMENT_INDEXES(s, d, n) SQUASHFS_SWAP_LONG_LONGS(s, d, n)
  3502. +
  3503. +#ifdef CONFIG_SQUASHFS_1_0_COMPATIBILITY
  3504. +
  3505. +struct squashfs_base_inode_header_1 {
  3506. + unsigned int inode_type:4;
  3507. + unsigned int mode:12; /* protection */
  3508. + unsigned int uid:4; /* index into uid table */
  3509. + unsigned int guid:4; /* index into guid table */
  3510. +} __attribute__ ((packed));
  3511. +
  3512. +struct squashfs_ipc_inode_header_1 {
  3513. + unsigned int inode_type:4;
  3514. + unsigned int mode:12; /* protection */
  3515. + unsigned int uid:4; /* index into uid table */
  3516. + unsigned int guid:4; /* index into guid table */
  3517. + unsigned int type:4;
  3518. + unsigned int offset:4;
  3519. +} __attribute__ ((packed));
  3520. +
  3521. +struct squashfs_dev_inode_header_1 {
  3522. + unsigned int inode_type:4;
  3523. + unsigned int mode:12; /* protection */
  3524. + unsigned int uid:4; /* index into uid table */
  3525. + unsigned int guid:4; /* index into guid table */
  3526. + unsigned short rdev;
  3527. +} __attribute__ ((packed));
  3528. +
  3529. +struct squashfs_symlink_inode_header_1 {
  3530. + unsigned int inode_type:4;
  3531. + unsigned int mode:12; /* protection */
  3532. + unsigned int uid:4; /* index into uid table */
  3533. + unsigned int guid:4; /* index into guid table */
  3534. + unsigned short symlink_size;
  3535. + char symlink[0];
  3536. +} __attribute__ ((packed));
  3537. +
  3538. +struct squashfs_reg_inode_header_1 {
  3539. + unsigned int inode_type:4;
  3540. + unsigned int mode:12; /* protection */
  3541. + unsigned int uid:4; /* index into uid table */
  3542. + unsigned int guid:4; /* index into guid table */
  3543. + unsigned int mtime;
  3544. + unsigned int start_block;
  3545. + unsigned int file_size:32;
  3546. + unsigned short block_list[0];
  3547. +} __attribute__ ((packed));
  3548. +
  3549. +struct squashfs_dir_inode_header_1 {
  3550. + unsigned int inode_type:4;
  3551. + unsigned int mode:12; /* protection */
  3552. + unsigned int uid:4; /* index into uid table */
  3553. + unsigned int guid:4; /* index into guid table */
  3554. + unsigned int file_size:19;
  3555. + unsigned int offset:13;
  3556. + unsigned int mtime;
  3557. + unsigned int start_block:24;
  3558. +} __attribute__ ((packed));
  3559. +
  3560. +#define SQUASHFS_SWAP_BASE_INODE_CORE_1(s, d, n) \
  3561. + SQUASHFS_MEMSET(s, d, n);\
  3562. + SQUASHFS_SWAP((s)->inode_type, d, 0, 4);\
  3563. + SQUASHFS_SWAP((s)->mode, d, 4, 12);\
  3564. + SQUASHFS_SWAP((s)->uid, d, 16, 4);\
  3565. + SQUASHFS_SWAP((s)->guid, d, 20, 4);
  3566. +
  3567. +#define SQUASHFS_SWAP_BASE_INODE_HEADER_1(s, d, n) {\
  3568. + SQUASHFS_SWAP_START\
  3569. + SQUASHFS_SWAP_BASE_INODE_CORE_1(s, d, n)\
  3570. +}
  3571. +
  3572. +#define SQUASHFS_SWAP_IPC_INODE_HEADER_1(s, d) {\
  3573. + SQUASHFS_SWAP_START\
  3574. + SQUASHFS_SWAP_BASE_INODE_CORE_1(s, d, \
  3575. + sizeof(struct squashfs_ipc_inode_header_1));\
  3576. + SQUASHFS_SWAP((s)->type, d, 24, 4);\
  3577. + SQUASHFS_SWAP((s)->offset, d, 28, 4);\
  3578. +}
  3579. +
  3580. +#define SQUASHFS_SWAP_DEV_INODE_HEADER_1(s, d) {\
  3581. + SQUASHFS_SWAP_START\
  3582. + SQUASHFS_SWAP_BASE_INODE_CORE_1(s, d, \
  3583. + sizeof(struct squashfs_dev_inode_header_1));\
  3584. + SQUASHFS_SWAP((s)->rdev, d, 24, 16);\
  3585. +}
  3586. +
  3587. +#define SQUASHFS_SWAP_SYMLINK_INODE_HEADER_1(s, d) {\
  3588. + SQUASHFS_SWAP_START\
  3589. + SQUASHFS_SWAP_BASE_INODE_CORE_1(s, d, \
  3590. + sizeof(struct squashfs_symlink_inode_header_1));\
  3591. + SQUASHFS_SWAP((s)->symlink_size, d, 24, 16);\
  3592. +}
  3593. +
  3594. +#define SQUASHFS_SWAP_REG_INODE_HEADER_1(s, d) {\
  3595. + SQUASHFS_SWAP_START\
  3596. + SQUASHFS_SWAP_BASE_INODE_CORE_1(s, d, \
  3597. + sizeof(struct squashfs_reg_inode_header_1));\
  3598. + SQUASHFS_SWAP((s)->mtime, d, 24, 32);\
  3599. + SQUASHFS_SWAP((s)->start_block, d, 56, 32);\
  3600. + SQUASHFS_SWAP((s)->file_size, d, 88, 32);\
  3601. +}
  3602. +
  3603. +#define SQUASHFS_SWAP_DIR_INODE_HEADER_1(s, d) {\
  3604. + SQUASHFS_SWAP_START\
  3605. + SQUASHFS_SWAP_BASE_INODE_CORE_1(s, d, \
  3606. + sizeof(struct squashfs_dir_inode_header_1));\
  3607. + SQUASHFS_SWAP((s)->file_size, d, 24, 19);\
  3608. + SQUASHFS_SWAP((s)->offset, d, 43, 13);\
  3609. + SQUASHFS_SWAP((s)->mtime, d, 56, 32);\
  3610. + SQUASHFS_SWAP((s)->start_block, d, 88, 24);\
  3611. +}
  3612. +
  3613. +#endif
  3614. +
  3615. +#ifdef CONFIG_SQUASHFS_2_0_COMPATIBILITY
  3616. +
  3617. +struct squashfs_dir_index_2 {
  3618. + unsigned int index:27;
  3619. + unsigned int start_block:29;
  3620. + unsigned char size;
  3621. + unsigned char name[0];
  3622. +} __attribute__ ((packed));
  3623. +
  3624. +struct squashfs_base_inode_header_2 {
  3625. + unsigned int inode_type:4;
  3626. + unsigned int mode:12; /* protection */
  3627. + unsigned int uid:8; /* index into uid table */
  3628. + unsigned int guid:8; /* index into guid table */
  3629. +} __attribute__ ((packed));
  3630. +
  3631. +struct squashfs_ipc_inode_header_2 {
  3632. + unsigned int inode_type:4;
  3633. + unsigned int mode:12; /* protection */
  3634. + unsigned int uid:8; /* index into uid table */
  3635. + unsigned int guid:8; /* index into guid table */
  3636. +} __attribute__ ((packed));
  3637. +
  3638. +struct squashfs_dev_inode_header_2 {
  3639. + unsigned int inode_type:4;
  3640. + unsigned int mode:12; /* protection */
  3641. + unsigned int uid:8; /* index into uid table */
  3642. + unsigned int guid:8; /* index into guid table */
  3643. + unsigned short rdev;
  3644. +} __attribute__ ((packed));
  3645. +
  3646. +struct squashfs_symlink_inode_header_2 {
  3647. + unsigned int inode_type:4;
  3648. + unsigned int mode:12; /* protection */
  3649. + unsigned int uid:8; /* index into uid table */
  3650. + unsigned int guid:8; /* index into guid table */
  3651. + unsigned short symlink_size;
  3652. + char symlink[0];
  3653. +} __attribute__ ((packed));
  3654. +
  3655. +struct squashfs_reg_inode_header_2 {
  3656. + unsigned int inode_type:4;
  3657. + unsigned int mode:12; /* protection */
  3658. + unsigned int uid:8; /* index into uid table */
  3659. + unsigned int guid:8; /* index into guid table */
  3660. + unsigned int mtime;
  3661. + unsigned int start_block;
  3662. + unsigned int fragment;
  3663. + unsigned int offset;
  3664. + unsigned int file_size:32;
  3665. + unsigned short block_list[0];
  3666. +} __attribute__ ((packed));
  3667. +
  3668. +struct squashfs_dir_inode_header_2 {
  3669. + unsigned int inode_type:4;
  3670. + unsigned int mode:12; /* protection */
  3671. + unsigned int uid:8; /* index into uid table */
  3672. + unsigned int guid:8; /* index into guid table */
  3673. + unsigned int file_size:19;
  3674. + unsigned int offset:13;
  3675. + unsigned int mtime;
  3676. + unsigned int start_block:24;
  3677. +} __attribute__ ((packed));
  3678. +
  3679. +struct squashfs_ldir_inode_header_2 {
  3680. + unsigned int inode_type:4;
  3681. + unsigned int mode:12; /* protection */
  3682. + unsigned int uid:8; /* index into uid table */
  3683. + unsigned int guid:8; /* index into guid table */
  3684. + unsigned int file_size:27;
  3685. + unsigned int offset:13;
  3686. + unsigned int mtime;
  3687. + unsigned int start_block:24;
  3688. + unsigned int i_count:16;
  3689. + struct squashfs_dir_index_2 index[0];
  3690. +} __attribute__ ((packed));
  3691. +
  3692. +union squashfs_inode_header_2 {
  3693. + struct squashfs_base_inode_header_2 base;
  3694. + struct squashfs_dev_inode_header_2 dev;
  3695. + struct squashfs_symlink_inode_header_2 symlink;
  3696. + struct squashfs_reg_inode_header_2 reg;
  3697. + struct squashfs_dir_inode_header_2 dir;
  3698. + struct squashfs_ldir_inode_header_2 ldir;
  3699. + struct squashfs_ipc_inode_header_2 ipc;
  3700. +};
  3701. +
  3702. +struct squashfs_dir_header_2 {
  3703. + unsigned int count:8;
  3704. + unsigned int start_block:24;
  3705. +} __attribute__ ((packed));
  3706. +
  3707. +struct squashfs_dir_entry_2 {
  3708. + unsigned int offset:13;
  3709. + unsigned int type:3;
  3710. + unsigned int size:8;
  3711. + char name[0];
  3712. +} __attribute__ ((packed));
  3713. +
  3714. +struct squashfs_fragment_entry_2 {
  3715. + unsigned int start_block;
  3716. + unsigned int size;
  3717. +} __attribute__ ((packed));
  3718. +
  3719. +#define SQUASHFS_SWAP_BASE_INODE_CORE_2(s, d, n)\
  3720. + SQUASHFS_MEMSET(s, d, n);\
  3721. + SQUASHFS_SWAP((s)->inode_type, d, 0, 4);\
  3722. + SQUASHFS_SWAP((s)->mode, d, 4, 12);\
  3723. + SQUASHFS_SWAP((s)->uid, d, 16, 8);\
  3724. + SQUASHFS_SWAP((s)->guid, d, 24, 8);\
  3725. +
  3726. +#define SQUASHFS_SWAP_BASE_INODE_HEADER_2(s, d, n) {\
  3727. + SQUASHFS_SWAP_START\
  3728. + SQUASHFS_SWAP_BASE_INODE_CORE_2(s, d, n)\
  3729. +}
  3730. +
  3731. +#define SQUASHFS_SWAP_IPC_INODE_HEADER_2(s, d) \
  3732. + SQUASHFS_SWAP_BASE_INODE_HEADER_2(s, d, sizeof(struct squashfs_ipc_inode_header_2))
  3733. +
  3734. +#define SQUASHFS_SWAP_DEV_INODE_HEADER_2(s, d) {\
  3735. + SQUASHFS_SWAP_START\
  3736. + SQUASHFS_SWAP_BASE_INODE_CORE_2(s, d, \
  3737. + sizeof(struct squashfs_dev_inode_header_2)); \
  3738. + SQUASHFS_SWAP((s)->rdev, d, 32, 16);\
  3739. +}
  3740. +
  3741. +#define SQUASHFS_SWAP_SYMLINK_INODE_HEADER_2(s, d) {\
  3742. + SQUASHFS_SWAP_START\
  3743. + SQUASHFS_SWAP_BASE_INODE_CORE_2(s, d, \
  3744. + sizeof(struct squashfs_symlink_inode_header_2));\
  3745. + SQUASHFS_SWAP((s)->symlink_size, d, 32, 16);\
  3746. +}
  3747. +
  3748. +#define SQUASHFS_SWAP_REG_INODE_HEADER_2(s, d) {\
  3749. + SQUASHFS_SWAP_START\
  3750. + SQUASHFS_SWAP_BASE_INODE_CORE_2(s, d, \
  3751. + sizeof(struct squashfs_reg_inode_header_2));\
  3752. + SQUASHFS_SWAP((s)->mtime, d, 32, 32);\
  3753. + SQUASHFS_SWAP((s)->start_block, d, 64, 32);\
  3754. + SQUASHFS_SWAP((s)->fragment, d, 96, 32);\
  3755. + SQUASHFS_SWAP((s)->offset, d, 128, 32);\
  3756. + SQUASHFS_SWAP((s)->file_size, d, 160, 32);\
  3757. +}
  3758. +
  3759. +#define SQUASHFS_SWAP_DIR_INODE_HEADER_2(s, d) {\
  3760. + SQUASHFS_SWAP_START\
  3761. + SQUASHFS_SWAP_BASE_INODE_CORE_2(s, d, \
  3762. + sizeof(struct squashfs_dir_inode_header_2));\
  3763. + SQUASHFS_SWAP((s)->file_size, d, 32, 19);\
  3764. + SQUASHFS_SWAP((s)->offset, d, 51, 13);\
  3765. + SQUASHFS_SWAP((s)->mtime, d, 64, 32);\
  3766. + SQUASHFS_SWAP((s)->start_block, d, 96, 24);\
  3767. +}
  3768. +
  3769. +#define SQUASHFS_SWAP_LDIR_INODE_HEADER_2(s, d) {\
  3770. + SQUASHFS_SWAP_START\
  3771. + SQUASHFS_SWAP_BASE_INODE_CORE_2(s, d, \
  3772. + sizeof(struct squashfs_ldir_inode_header_2));\
  3773. + SQUASHFS_SWAP((s)->file_size, d, 32, 27);\
  3774. + SQUASHFS_SWAP((s)->offset, d, 59, 13);\
  3775. + SQUASHFS_SWAP((s)->mtime, d, 72, 32);\
  3776. + SQUASHFS_SWAP((s)->start_block, d, 104, 24);\
  3777. + SQUASHFS_SWAP((s)->i_count, d, 128, 16);\
  3778. +}
  3779. +
  3780. +#define SQUASHFS_SWAP_DIR_INDEX_2(s, d) {\
  3781. + SQUASHFS_SWAP_START\
  3782. + SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_dir_index_2));\
  3783. + SQUASHFS_SWAP((s)->index, d, 0, 27);\
  3784. + SQUASHFS_SWAP((s)->start_block, d, 27, 29);\
  3785. + SQUASHFS_SWAP((s)->size, d, 56, 8);\
  3786. +}
  3787. +#define SQUASHFS_SWAP_DIR_HEADER_2(s, d) {\
  3788. + SQUASHFS_SWAP_START\
  3789. + SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_dir_header_2));\
  3790. + SQUASHFS_SWAP((s)->count, d, 0, 8);\
  3791. + SQUASHFS_SWAP((s)->start_block, d, 8, 24);\
  3792. +}
  3793. +
  3794. +#define SQUASHFS_SWAP_DIR_ENTRY_2(s, d) {\
  3795. + SQUASHFS_SWAP_START\
  3796. + SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_dir_entry_2));\
  3797. + SQUASHFS_SWAP((s)->offset, d, 0, 13);\
  3798. + SQUASHFS_SWAP((s)->type, d, 13, 3);\
  3799. + SQUASHFS_SWAP((s)->size, d, 16, 8);\
  3800. +}
  3801. +
  3802. +#define SQUASHFS_SWAP_FRAGMENT_ENTRY_2(s, d) {\
  3803. + SQUASHFS_SWAP_START\
  3804. + SQUASHFS_MEMSET(s, d, sizeof(struct squashfs_fragment_entry_2));\
  3805. + SQUASHFS_SWAP((s)->start_block, d, 0, 32);\
  3806. + SQUASHFS_SWAP((s)->size, d, 32, 32);\
  3807. +}
  3808. +
  3809. +#define SQUASHFS_SWAP_FRAGMENT_INDEXES_2(s, d, n) SQUASHFS_SWAP_INTS(s, d, n)
  3810. +
  3811. +/* fragment and fragment table defines */
  3812. +#define SQUASHFS_FRAGMENT_BYTES_2(A) (A * sizeof(struct squashfs_fragment_entry_2))
  3813. +
  3814. +#define SQUASHFS_FRAGMENT_INDEX_2(A) (SQUASHFS_FRAGMENT_BYTES_2(A) / \
  3815. + SQUASHFS_METADATA_SIZE)
  3816. +
  3817. +#define SQUASHFS_FRAGMENT_INDEX_OFFSET_2(A) (SQUASHFS_FRAGMENT_BYTES_2(A) % \
  3818. + SQUASHFS_METADATA_SIZE)
  3819. +
  3820. +#define SQUASHFS_FRAGMENT_INDEXES_2(A) ((SQUASHFS_FRAGMENT_BYTES_2(A) + \
  3821. + SQUASHFS_METADATA_SIZE - 1) / \
  3822. + SQUASHFS_METADATA_SIZE)
  3823. +
  3824. +#define SQUASHFS_FRAGMENT_INDEX_BYTES_2(A) (SQUASHFS_FRAGMENT_INDEXES_2(A) *\
  3825. + sizeof(int))
  3826. +
  3827. +#endif
  3828. +
  3829. +#ifdef __KERNEL__
  3830. +
  3831. +/*
  3832. + * macros used to swap each structure entry, taking into account
  3833. + * bitfields and different bitfield placing conventions on differing
  3834. + * architectures
  3835. + */
  3836. +
  3837. +#include <asm/byteorder.h>
  3838. +
  3839. +#ifdef __BIG_ENDIAN
  3840. + /* convert from little endian to big endian */
  3841. +#define SQUASHFS_SWAP(value, p, pos, tbits) _SQUASHFS_SWAP(value, p, pos, \
  3842. + tbits, b_pos)
  3843. +#else
  3844. + /* convert from big endian to little endian */
  3845. +#define SQUASHFS_SWAP(value, p, pos, tbits) _SQUASHFS_SWAP(value, p, pos, \
  3846. + tbits, 64 - tbits - b_pos)
  3847. +#endif
  3848. +
  3849. +#define _SQUASHFS_SWAP(value, p, pos, tbits, SHIFT) {\
  3850. + b_pos = pos % 8;\
  3851. + val = 0;\
  3852. + s = (unsigned char *)p + (pos / 8);\
  3853. + d = ((unsigned char *) &val) + 7;\
  3854. + for(bits = 0; bits < (tbits + b_pos); bits += 8) \
  3855. + *d-- = *s++;\
  3856. + value = (val >> (SHIFT))/* & ((1 << tbits) - 1)*/;\
  3857. +}
  3858. +
  3859. +#define SQUASHFS_MEMSET(s, d, n) memset(s, 0, n);
  3860. +
  3861. +#endif
  3862. +#endif
  3863. --- /dev/null
  3864. +++ b/include/linux/squashfs_fs_i.h
  3865. @@ -0,0 +1,44 @@
  3866. +#ifndef SQUASHFS_FS_I
  3867. +#define SQUASHFS_FS_I
  3868. +/*
  3869. + * Squashfs
  3870. + *
  3871. + * Copyright (c) 2002, 2003, 2004, 2005, 2006
  3872. + * Phillip Lougher <[email protected]>
  3873. + *
  3874. + * This program is free software; you can redistribute it and/or
  3875. + * modify it under the terms of the GNU General Public License
  3876. + * as published by the Free Software Foundation; either version 2,
  3877. + * or (at your option) any later version.
  3878. + *
  3879. + * This program is distributed in the hope that it will be useful,
  3880. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3881. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3882. + * GNU General Public License for more details.
  3883. + *
  3884. + * You should have received a copy of the GNU General Public License
  3885. + * along with this program; if not, write to the Free Software
  3886. + * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  3887. + *
  3888. + * squashfs_fs_i.h
  3889. + */
  3890. +
  3891. +struct squashfs_inode_info {
  3892. + long long start_block;
  3893. + unsigned int offset;
  3894. + union {
  3895. + struct {
  3896. + long long fragment_start_block;
  3897. + unsigned int fragment_size;
  3898. + unsigned int fragment_offset;
  3899. + long long block_list_start;
  3900. + } s1;
  3901. + struct {
  3902. + long long directory_index_start;
  3903. + unsigned int directory_index_offset;
  3904. + unsigned int directory_index_count;
  3905. + unsigned int parent_inode;
  3906. + } s2;
  3907. + } u;
  3908. +};
  3909. +#endif
  3910. --- /dev/null
  3911. +++ b/include/linux/squashfs_fs_sb.h
  3912. @@ -0,0 +1,74 @@
  3913. +#ifndef SQUASHFS_FS_SB
  3914. +#define SQUASHFS_FS_SB
  3915. +/*
  3916. + * Squashfs
  3917. + *
  3918. + * Copyright (c) 2002, 2003, 2004, 2005, 2006
  3919. + * Phillip Lougher <[email protected]>
  3920. + *
  3921. + * This program is free software; you can redistribute it and/or
  3922. + * modify it under the terms of the GNU General Public License
  3923. + * as published by the Free Software Foundation; either version 2,
  3924. + * or (at your option) any later version.
  3925. + *
  3926. + * This program is distributed in the hope that it will be useful,
  3927. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3928. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3929. + * GNU General Public License for more details.
  3930. + *
  3931. + * You should have received a copy of the GNU General Public License
  3932. + * along with this program; if not, write to the Free Software
  3933. + * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  3934. + *
  3935. + * squashfs_fs_sb.h
  3936. + */
  3937. +
  3938. +#include <linux/squashfs_fs.h>
  3939. +
  3940. +struct squashfs_cache {
  3941. + long long block;
  3942. + int length;
  3943. + long long next_index;
  3944. + char *data;
  3945. +};
  3946. +
  3947. +struct squashfs_fragment_cache {
  3948. + long long block;
  3949. + int length;
  3950. + unsigned int locked;
  3951. + char *data;
  3952. +};
  3953. +
  3954. +struct squashfs_sb_info {
  3955. + struct squashfs_super_block sblk;
  3956. + int devblksize;
  3957. + int devblksize_log2;
  3958. + int swap;
  3959. + struct squashfs_cache *block_cache;
  3960. + struct squashfs_fragment_cache *fragment;
  3961. + int next_cache;
  3962. + int next_fragment;
  3963. + int next_meta_index;
  3964. + unsigned int *uid;
  3965. + unsigned int *guid;
  3966. + long long *fragment_index;
  3967. + unsigned int *fragment_index_2;
  3968. + unsigned int read_size;
  3969. + char *read_data;
  3970. + char *read_page;
  3971. + struct semaphore read_data_mutex;
  3972. + struct semaphore read_page_mutex;
  3973. + struct semaphore block_cache_mutex;
  3974. + struct semaphore fragment_mutex;
  3975. + struct semaphore meta_index_mutex;
  3976. + wait_queue_head_t waitq;
  3977. + wait_queue_head_t fragment_wait_queue;
  3978. + struct meta_index *meta_index;
  3979. + struct inode *(*iget)(struct super_block *s, squashfs_inode_t \
  3980. + inode);
  3981. + long long (*read_blocklist)(struct inode *inode, int \
  3982. + index, int readahead_blks, char *block_list, \
  3983. + unsigned short **block_p, unsigned int *bsize);
  3984. + int (*read_fragment_index_table)(struct super_block *s);
  3985. +};
  3986. +#endif
  3987. --- a/init/do_mounts.c
  3988. +++ b/init/do_mounts.c
  3989. @@ -16,6 +16,7 @@
  3990. #include <linux/minix_fs.h>
  3991. #include <linux/ext2_fs.h>
  3992. #include <linux/romfs_fs.h>
  3993. +#include <linux/squashfs_fs.h>
  3994. #include <linux/cramfs_fs.h>
  3995. #define BUILD_CRAMDISK
  3996. @@ -485,6 +486,7 @@ static int __init crd_load(int in_fd, in
  3997. * minix
  3998. * ext2
  3999. * romfs
  4000. + * squashfs
  4001. * cramfs
  4002. * gzip
  4003. */
  4004. @@ -495,6 +497,7 @@ identify_ramdisk_image(int fd, int start
  4005. struct minix_super_block *minixsb;
  4006. struct ext2_super_block *ext2sb;
  4007. struct romfs_super_block *romfsb;
  4008. + struct squashfs_super_block *squashfsb;
  4009. struct cramfs_super *cramfsb;
  4010. int nblocks = -1;
  4011. unsigned char *buf;
  4012. @@ -506,6 +509,7 @@ identify_ramdisk_image(int fd, int start
  4013. minixsb = (struct minix_super_block *) buf;
  4014. ext2sb = (struct ext2_super_block *) buf;
  4015. romfsb = (struct romfs_super_block *) buf;
  4016. + squashfsb = (struct squashfs_super_block *) buf;
  4017. cramfsb = (struct cramfs_super *) buf;
  4018. memset(buf, 0xe5, size);
  4019. @@ -544,6 +548,15 @@ identify_ramdisk_image(int fd, int start
  4020. goto done;
  4021. }
  4022. + /* squashfs is at block zero too */
  4023. + if (squashfsb->s_magic == SQUASHFS_MAGIC) {
  4024. + printk(KERN_NOTICE
  4025. + "RAMDISK: squashfs filesystem found at block %d\n",
  4026. + start_block);
  4027. + nblocks = (squashfsb->bytes_used+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
  4028. + goto done;
  4029. + }
  4030. +
  4031. /*
  4032. * Read block 1 to test for minix and ext2 superblock
  4033. */
  4034. --- a/lib/Config.in
  4035. +++ b/lib/Config.in
  4036. @@ -10,6 +10,7 @@ tristate 'CRC32 functions' CONFIG_CRC32
  4037. # Do we need the compression support?
  4038. #
  4039. if [ "$CONFIG_CRAMFS" = "y" -o \
  4040. + "$CONFIG_SQUASHFS" = "y" -o \
  4041. "$CONFIG_PPP_DEFLATE" = "y" -o \
  4042. "$CONFIG_CRYPTO_DEFLATE" = "y" -o \
  4043. "$CONFIG_JFFS2_FS" = "y" -o \
  4044. @@ -17,6 +18,7 @@ if [ "$CONFIG_CRAMFS" = "y" -o \
  4045. define_tristate CONFIG_ZLIB_INFLATE y
  4046. else
  4047. if [ "$CONFIG_CRAMFS" = "m" -o \
  4048. + "$CONFIG_SQUASHFS" = "m" -o \
  4049. "$CONFIG_PPP_DEFLATE" = "m" -o \
  4050. "$CONFIG_CRYPTO_DEFLATE" = "m" -o \
  4051. "$CONFIG_JFFS2_FS" = "m" -o \