001-squashfs.patch 121 KB

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