jfs_imap.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189
  1. /*
  2. * Copyright (C) International Business Machines Corp., 2000-2004
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. /*
  19. * jfs_imap.c: inode allocation map manager
  20. *
  21. * Serialization:
  22. * Each AG has a simple lock which is used to control the serialization of
  23. * the AG level lists. This lock should be taken first whenever an AG
  24. * level list will be modified or accessed.
  25. *
  26. * Each IAG is locked by obtaining the buffer for the IAG page.
  27. *
  28. * There is also a inode lock for the inode map inode. A read lock needs to
  29. * be taken whenever an IAG is read from the map or the global level
  30. * information is read. A write lock needs to be taken whenever the global
  31. * level information is modified or an atomic operation needs to be used.
  32. *
  33. * If more than one IAG is read at one time, the read lock may not
  34. * be given up until all of the IAG's are read. Otherwise, a deadlock
  35. * may occur when trying to obtain the read lock while another thread
  36. * holding the read lock is waiting on the IAG already being held.
  37. *
  38. * The control page of the inode map is read into memory by diMount().
  39. * Thereafter it should only be modified in memory and then it will be
  40. * written out when the filesystem is unmounted by diUnmount().
  41. */
  42. #include <linux/fs.h>
  43. #include <linux/buffer_head.h>
  44. #include <linux/pagemap.h>
  45. #include <linux/quotaops.h>
  46. #include <linux/slab.h>
  47. #include "jfs_incore.h"
  48. #include "jfs_inode.h"
  49. #include "jfs_filsys.h"
  50. #include "jfs_dinode.h"
  51. #include "jfs_dmap.h"
  52. #include "jfs_imap.h"
  53. #include "jfs_metapage.h"
  54. #include "jfs_superblock.h"
  55. #include "jfs_debug.h"
  56. /*
  57. * imap locks
  58. */
  59. /* iag free list lock */
  60. #define IAGFREE_LOCK_INIT(imap) mutex_init(&imap->im_freelock)
  61. #define IAGFREE_LOCK(imap) mutex_lock(&imap->im_freelock)
  62. #define IAGFREE_UNLOCK(imap) mutex_unlock(&imap->im_freelock)
  63. /* per ag iag list locks */
  64. #define AG_LOCK_INIT(imap,index) mutex_init(&(imap->im_aglock[index]))
  65. #define AG_LOCK(imap,agno) mutex_lock(&imap->im_aglock[agno])
  66. #define AG_UNLOCK(imap,agno) mutex_unlock(&imap->im_aglock[agno])
  67. /*
  68. * forward references
  69. */
  70. static int diAllocAG(struct inomap *, int, bool, struct inode *);
  71. static int diAllocAny(struct inomap *, int, bool, struct inode *);
  72. static int diAllocBit(struct inomap *, struct iag *, int);
  73. static int diAllocExt(struct inomap *, int, struct inode *);
  74. static int diAllocIno(struct inomap *, int, struct inode *);
  75. static int diFindFree(u32, int);
  76. static int diNewExt(struct inomap *, struct iag *, int);
  77. static int diNewIAG(struct inomap *, int *, int, struct metapage **);
  78. static void duplicateIXtree(struct super_block *, s64, int, s64 *);
  79. static int diIAGRead(struct inomap * imap, int, struct metapage **);
  80. static int copy_from_dinode(struct dinode *, struct inode *);
  81. static void copy_to_dinode(struct dinode *, struct inode *);
  82. /*
  83. * NAME: diMount()
  84. *
  85. * FUNCTION: initialize the incore inode map control structures for
  86. * a fileset or aggregate init time.
  87. *
  88. * the inode map's control structure (dinomap) is
  89. * brought in from disk and placed in virtual memory.
  90. *
  91. * PARAMETERS:
  92. * ipimap - pointer to inode map inode for the aggregate or fileset.
  93. *
  94. * RETURN VALUES:
  95. * 0 - success
  96. * -ENOMEM - insufficient free virtual memory.
  97. * -EIO - i/o error.
  98. */
  99. int diMount(struct inode *ipimap)
  100. {
  101. struct inomap *imap;
  102. struct metapage *mp;
  103. int index;
  104. struct dinomap_disk *dinom_le;
  105. /*
  106. * allocate/initialize the in-memory inode map control structure
  107. */
  108. /* allocate the in-memory inode map control structure. */
  109. imap = kmalloc(sizeof(struct inomap), GFP_KERNEL);
  110. if (imap == NULL) {
  111. jfs_err("diMount: kmalloc returned NULL!");
  112. return -ENOMEM;
  113. }
  114. /* read the on-disk inode map control structure. */
  115. mp = read_metapage(ipimap,
  116. IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
  117. PSIZE, 0);
  118. if (mp == NULL) {
  119. kfree(imap);
  120. return -EIO;
  121. }
  122. /* copy the on-disk version to the in-memory version. */
  123. dinom_le = (struct dinomap_disk *) mp->data;
  124. imap->im_freeiag = le32_to_cpu(dinom_le->in_freeiag);
  125. imap->im_nextiag = le32_to_cpu(dinom_le->in_nextiag);
  126. atomic_set(&imap->im_numinos, le32_to_cpu(dinom_le->in_numinos));
  127. atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
  128. imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
  129. imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
  130. for (index = 0; index < MAXAG; index++) {
  131. imap->im_agctl[index].inofree =
  132. le32_to_cpu(dinom_le->in_agctl[index].inofree);
  133. imap->im_agctl[index].extfree =
  134. le32_to_cpu(dinom_le->in_agctl[index].extfree);
  135. imap->im_agctl[index].numinos =
  136. le32_to_cpu(dinom_le->in_agctl[index].numinos);
  137. imap->im_agctl[index].numfree =
  138. le32_to_cpu(dinom_le->in_agctl[index].numfree);
  139. }
  140. /* release the buffer. */
  141. release_metapage(mp);
  142. /*
  143. * allocate/initialize inode allocation map locks
  144. */
  145. /* allocate and init iag free list lock */
  146. IAGFREE_LOCK_INIT(imap);
  147. /* allocate and init ag list locks */
  148. for (index = 0; index < MAXAG; index++) {
  149. AG_LOCK_INIT(imap, index);
  150. }
  151. /* bind the inode map inode and inode map control structure
  152. * to each other.
  153. */
  154. imap->im_ipimap = ipimap;
  155. JFS_IP(ipimap)->i_imap = imap;
  156. return (0);
  157. }
  158. /*
  159. * NAME: diUnmount()
  160. *
  161. * FUNCTION: write to disk the incore inode map control structures for
  162. * a fileset or aggregate at unmount time.
  163. *
  164. * PARAMETERS:
  165. * ipimap - pointer to inode map inode for the aggregate or fileset.
  166. *
  167. * RETURN VALUES:
  168. * 0 - success
  169. * -ENOMEM - insufficient free virtual memory.
  170. * -EIO - i/o error.
  171. */
  172. int diUnmount(struct inode *ipimap, int mounterror)
  173. {
  174. struct inomap *imap = JFS_IP(ipimap)->i_imap;
  175. /*
  176. * update the on-disk inode map control structure
  177. */
  178. if (!(mounterror || isReadOnly(ipimap)))
  179. diSync(ipimap);
  180. /*
  181. * Invalidate the page cache buffers
  182. */
  183. truncate_inode_pages(ipimap->i_mapping, 0);
  184. /*
  185. * free in-memory control structure
  186. */
  187. kfree(imap);
  188. return (0);
  189. }
  190. /*
  191. * diSync()
  192. */
  193. int diSync(struct inode *ipimap)
  194. {
  195. struct dinomap_disk *dinom_le;
  196. struct inomap *imp = JFS_IP(ipimap)->i_imap;
  197. struct metapage *mp;
  198. int index;
  199. /*
  200. * write imap global conrol page
  201. */
  202. /* read the on-disk inode map control structure */
  203. mp = get_metapage(ipimap,
  204. IMAPBLKNO << JFS_SBI(ipimap->i_sb)->l2nbperpage,
  205. PSIZE, 0);
  206. if (mp == NULL) {
  207. jfs_err("diSync: get_metapage failed!");
  208. return -EIO;
  209. }
  210. /* copy the in-memory version to the on-disk version */
  211. dinom_le = (struct dinomap_disk *) mp->data;
  212. dinom_le->in_freeiag = cpu_to_le32(imp->im_freeiag);
  213. dinom_le->in_nextiag = cpu_to_le32(imp->im_nextiag);
  214. dinom_le->in_numinos = cpu_to_le32(atomic_read(&imp->im_numinos));
  215. dinom_le->in_numfree = cpu_to_le32(atomic_read(&imp->im_numfree));
  216. dinom_le->in_nbperiext = cpu_to_le32(imp->im_nbperiext);
  217. dinom_le->in_l2nbperiext = cpu_to_le32(imp->im_l2nbperiext);
  218. for (index = 0; index < MAXAG; index++) {
  219. dinom_le->in_agctl[index].inofree =
  220. cpu_to_le32(imp->im_agctl[index].inofree);
  221. dinom_le->in_agctl[index].extfree =
  222. cpu_to_le32(imp->im_agctl[index].extfree);
  223. dinom_le->in_agctl[index].numinos =
  224. cpu_to_le32(imp->im_agctl[index].numinos);
  225. dinom_le->in_agctl[index].numfree =
  226. cpu_to_le32(imp->im_agctl[index].numfree);
  227. }
  228. /* write out the control structure */
  229. write_metapage(mp);
  230. /*
  231. * write out dirty pages of imap
  232. */
  233. filemap_write_and_wait(ipimap->i_mapping);
  234. diWriteSpecial(ipimap, 0);
  235. return (0);
  236. }
  237. /*
  238. * NAME: diRead()
  239. *
  240. * FUNCTION: initialize an incore inode from disk.
  241. *
  242. * on entry, the specifed incore inode should itself
  243. * specify the disk inode number corresponding to the
  244. * incore inode (i.e. i_number should be initialized).
  245. *
  246. * this routine handles incore inode initialization for
  247. * both "special" and "regular" inodes. special inodes
  248. * are those required early in the mount process and
  249. * require special handling since much of the file system
  250. * is not yet initialized. these "special" inodes are
  251. * identified by a NULL inode map inode pointer and are
  252. * actually initialized by a call to diReadSpecial().
  253. *
  254. * for regular inodes, the iag describing the disk inode
  255. * is read from disk to determine the inode extent address
  256. * for the disk inode. with the inode extent address in
  257. * hand, the page of the extent that contains the disk
  258. * inode is read and the disk inode is copied to the
  259. * incore inode.
  260. *
  261. * PARAMETERS:
  262. * ip - pointer to incore inode to be initialized from disk.
  263. *
  264. * RETURN VALUES:
  265. * 0 - success
  266. * -EIO - i/o error.
  267. * -ENOMEM - insufficient memory
  268. *
  269. */
  270. int diRead(struct inode *ip)
  271. {
  272. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  273. int iagno, ino, extno, rc;
  274. struct inode *ipimap;
  275. struct dinode *dp;
  276. struct iag *iagp;
  277. struct metapage *mp;
  278. s64 blkno, agstart;
  279. struct inomap *imap;
  280. int block_offset;
  281. int inodes_left;
  282. unsigned long pageno;
  283. int rel_inode;
  284. jfs_info("diRead: ino = %ld", ip->i_ino);
  285. ipimap = sbi->ipimap;
  286. JFS_IP(ip)->ipimap = ipimap;
  287. /* determine the iag number for this inode (number) */
  288. iagno = INOTOIAG(ip->i_ino);
  289. /* read the iag */
  290. imap = JFS_IP(ipimap)->i_imap;
  291. IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
  292. rc = diIAGRead(imap, iagno, &mp);
  293. IREAD_UNLOCK(ipimap);
  294. if (rc) {
  295. jfs_err("diRead: diIAGRead returned %d", rc);
  296. return (rc);
  297. }
  298. iagp = (struct iag *) mp->data;
  299. /* determine inode extent that holds the disk inode */
  300. ino = ip->i_ino & (INOSPERIAG - 1);
  301. extno = ino >> L2INOSPEREXT;
  302. if ((lengthPXD(&iagp->inoext[extno]) != imap->im_nbperiext) ||
  303. (addressPXD(&iagp->inoext[extno]) == 0)) {
  304. release_metapage(mp);
  305. return -ESTALE;
  306. }
  307. /* get disk block number of the page within the inode extent
  308. * that holds the disk inode.
  309. */
  310. blkno = INOPBLK(&iagp->inoext[extno], ino, sbi->l2nbperpage);
  311. /* get the ag for the iag */
  312. agstart = le64_to_cpu(iagp->agstart);
  313. release_metapage(mp);
  314. rel_inode = (ino & (INOSPERPAGE - 1));
  315. pageno = blkno >> sbi->l2nbperpage;
  316. if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
  317. /*
  318. * OS/2 didn't always align inode extents on page boundaries
  319. */
  320. inodes_left =
  321. (sbi->nbperpage - block_offset) << sbi->l2niperblk;
  322. if (rel_inode < inodes_left)
  323. rel_inode += block_offset << sbi->l2niperblk;
  324. else {
  325. pageno += 1;
  326. rel_inode -= inodes_left;
  327. }
  328. }
  329. /* read the page of disk inode */
  330. mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
  331. if (!mp) {
  332. jfs_err("diRead: read_metapage failed");
  333. return -EIO;
  334. }
  335. /* locate the disk inode requested */
  336. dp = (struct dinode *) mp->data;
  337. dp += rel_inode;
  338. if (ip->i_ino != le32_to_cpu(dp->di_number)) {
  339. jfs_error(ip->i_sb, "diRead: i_ino != di_number");
  340. rc = -EIO;
  341. } else if (le32_to_cpu(dp->di_nlink) == 0)
  342. rc = -ESTALE;
  343. else
  344. /* copy the disk inode to the in-memory inode */
  345. rc = copy_from_dinode(dp, ip);
  346. release_metapage(mp);
  347. /* set the ag for the inode */
  348. JFS_IP(ip)->agno = BLKTOAG(agstart, sbi);
  349. JFS_IP(ip)->active_ag = -1;
  350. return (rc);
  351. }
  352. /*
  353. * NAME: diReadSpecial()
  354. *
  355. * FUNCTION: initialize a 'special' inode from disk.
  356. *
  357. * this routines handles aggregate level inodes. The
  358. * inode cache cannot differentiate between the
  359. * aggregate inodes and the filesystem inodes, so we
  360. * handle these here. We don't actually use the aggregate
  361. * inode map, since these inodes are at a fixed location
  362. * and in some cases the aggregate inode map isn't initialized
  363. * yet.
  364. *
  365. * PARAMETERS:
  366. * sb - filesystem superblock
  367. * inum - aggregate inode number
  368. * secondary - 1 if secondary aggregate inode table
  369. *
  370. * RETURN VALUES:
  371. * new inode - success
  372. * NULL - i/o error.
  373. */
  374. struct inode *diReadSpecial(struct super_block *sb, ino_t inum, int secondary)
  375. {
  376. struct jfs_sb_info *sbi = JFS_SBI(sb);
  377. uint address;
  378. struct dinode *dp;
  379. struct inode *ip;
  380. struct metapage *mp;
  381. ip = new_inode(sb);
  382. if (ip == NULL) {
  383. jfs_err("diReadSpecial: new_inode returned NULL!");
  384. return ip;
  385. }
  386. if (secondary) {
  387. address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
  388. JFS_IP(ip)->ipimap = sbi->ipaimap2;
  389. } else {
  390. address = AITBL_OFF >> L2PSIZE;
  391. JFS_IP(ip)->ipimap = sbi->ipaimap;
  392. }
  393. ASSERT(inum < INOSPEREXT);
  394. ip->i_ino = inum;
  395. address += inum >> 3; /* 8 inodes per 4K page */
  396. /* read the page of fixed disk inode (AIT) in raw mode */
  397. mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
  398. if (mp == NULL) {
  399. ip->i_nlink = 1; /* Don't want iput() deleting it */
  400. iput(ip);
  401. return (NULL);
  402. }
  403. /* get the pointer to the disk inode of interest */
  404. dp = (struct dinode *) (mp->data);
  405. dp += inum % 8; /* 8 inodes per 4K page */
  406. /* copy on-disk inode to in-memory inode */
  407. if ((copy_from_dinode(dp, ip)) != 0) {
  408. /* handle bad return by returning NULL for ip */
  409. ip->i_nlink = 1; /* Don't want iput() deleting it */
  410. iput(ip);
  411. /* release the page */
  412. release_metapage(mp);
  413. return (NULL);
  414. }
  415. ip->i_mapping->a_ops = &jfs_metapage_aops;
  416. mapping_set_gfp_mask(ip->i_mapping, GFP_NOFS);
  417. /* Allocations to metadata inodes should not affect quotas */
  418. ip->i_flags |= S_NOQUOTA;
  419. if ((inum == FILESYSTEM_I) && (JFS_IP(ip)->ipimap == sbi->ipaimap)) {
  420. sbi->gengen = le32_to_cpu(dp->di_gengen);
  421. sbi->inostamp = le32_to_cpu(dp->di_inostamp);
  422. }
  423. /* release the page */
  424. release_metapage(mp);
  425. /*
  426. * __mark_inode_dirty expects inodes to be hashed. Since we don't
  427. * want special inodes in the fileset inode space, we make them
  428. * appear hashed, but do not put on any lists. hlist_del()
  429. * will work fine and require no locking.
  430. */
  431. ip->i_hash.pprev = &ip->i_hash.next;
  432. return (ip);
  433. }
  434. /*
  435. * NAME: diWriteSpecial()
  436. *
  437. * FUNCTION: Write the special inode to disk
  438. *
  439. * PARAMETERS:
  440. * ip - special inode
  441. * secondary - 1 if secondary aggregate inode table
  442. *
  443. * RETURN VALUES: none
  444. */
  445. void diWriteSpecial(struct inode *ip, int secondary)
  446. {
  447. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  448. uint address;
  449. struct dinode *dp;
  450. ino_t inum = ip->i_ino;
  451. struct metapage *mp;
  452. if (secondary)
  453. address = addressPXD(&sbi->ait2) >> sbi->l2nbperpage;
  454. else
  455. address = AITBL_OFF >> L2PSIZE;
  456. ASSERT(inum < INOSPEREXT);
  457. address += inum >> 3; /* 8 inodes per 4K page */
  458. /* read the page of fixed disk inode (AIT) in raw mode */
  459. mp = read_metapage(ip, address << sbi->l2nbperpage, PSIZE, 1);
  460. if (mp == NULL) {
  461. jfs_err("diWriteSpecial: failed to read aggregate inode "
  462. "extent!");
  463. return;
  464. }
  465. /* get the pointer to the disk inode of interest */
  466. dp = (struct dinode *) (mp->data);
  467. dp += inum % 8; /* 8 inodes per 4K page */
  468. /* copy on-disk inode to in-memory inode */
  469. copy_to_dinode(dp, ip);
  470. memcpy(&dp->di_xtroot, &JFS_IP(ip)->i_xtroot, 288);
  471. if (inum == FILESYSTEM_I)
  472. dp->di_gengen = cpu_to_le32(sbi->gengen);
  473. /* write the page */
  474. write_metapage(mp);
  475. }
  476. /*
  477. * NAME: diFreeSpecial()
  478. *
  479. * FUNCTION: Free allocated space for special inode
  480. */
  481. void diFreeSpecial(struct inode *ip)
  482. {
  483. if (ip == NULL) {
  484. jfs_err("diFreeSpecial called with NULL ip!");
  485. return;
  486. }
  487. filemap_write_and_wait(ip->i_mapping);
  488. truncate_inode_pages(ip->i_mapping, 0);
  489. iput(ip);
  490. }
  491. /*
  492. * NAME: diWrite()
  493. *
  494. * FUNCTION: write the on-disk inode portion of the in-memory inode
  495. * to its corresponding on-disk inode.
  496. *
  497. * on entry, the specifed incore inode should itself
  498. * specify the disk inode number corresponding to the
  499. * incore inode (i.e. i_number should be initialized).
  500. *
  501. * the inode contains the inode extent address for the disk
  502. * inode. with the inode extent address in hand, the
  503. * page of the extent that contains the disk inode is
  504. * read and the disk inode portion of the incore inode
  505. * is copied to the disk inode.
  506. *
  507. * PARAMETERS:
  508. * tid - transacation id
  509. * ip - pointer to incore inode to be written to the inode extent.
  510. *
  511. * RETURN VALUES:
  512. * 0 - success
  513. * -EIO - i/o error.
  514. */
  515. int diWrite(tid_t tid, struct inode *ip)
  516. {
  517. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  518. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  519. int rc = 0;
  520. s32 ino;
  521. struct dinode *dp;
  522. s64 blkno;
  523. int block_offset;
  524. int inodes_left;
  525. struct metapage *mp;
  526. unsigned long pageno;
  527. int rel_inode;
  528. int dioffset;
  529. struct inode *ipimap;
  530. uint type;
  531. lid_t lid;
  532. struct tlock *ditlck, *tlck;
  533. struct linelock *dilinelock, *ilinelock;
  534. struct lv *lv;
  535. int n;
  536. ipimap = jfs_ip->ipimap;
  537. ino = ip->i_ino & (INOSPERIAG - 1);
  538. if (!addressPXD(&(jfs_ip->ixpxd)) ||
  539. (lengthPXD(&(jfs_ip->ixpxd)) !=
  540. JFS_IP(ipimap)->i_imap->im_nbperiext)) {
  541. jfs_error(ip->i_sb, "diWrite: ixpxd invalid");
  542. return -EIO;
  543. }
  544. /*
  545. * read the page of disk inode containing the specified inode:
  546. */
  547. /* compute the block address of the page */
  548. blkno = INOPBLK(&(jfs_ip->ixpxd), ino, sbi->l2nbperpage);
  549. rel_inode = (ino & (INOSPERPAGE - 1));
  550. pageno = blkno >> sbi->l2nbperpage;
  551. if ((block_offset = ((u32) blkno & (sbi->nbperpage - 1)))) {
  552. /*
  553. * OS/2 didn't always align inode extents on page boundaries
  554. */
  555. inodes_left =
  556. (sbi->nbperpage - block_offset) << sbi->l2niperblk;
  557. if (rel_inode < inodes_left)
  558. rel_inode += block_offset << sbi->l2niperblk;
  559. else {
  560. pageno += 1;
  561. rel_inode -= inodes_left;
  562. }
  563. }
  564. /* read the page of disk inode */
  565. retry:
  566. mp = read_metapage(ipimap, pageno << sbi->l2nbperpage, PSIZE, 1);
  567. if (!mp)
  568. return -EIO;
  569. /* get the pointer to the disk inode */
  570. dp = (struct dinode *) mp->data;
  571. dp += rel_inode;
  572. dioffset = (ino & (INOSPERPAGE - 1)) << L2DISIZE;
  573. /*
  574. * acquire transaction lock on the on-disk inode;
  575. * N.B. tlock is acquired on ipimap not ip;
  576. */
  577. if ((ditlck =
  578. txLock(tid, ipimap, mp, tlckINODE | tlckENTRY)) == NULL)
  579. goto retry;
  580. dilinelock = (struct linelock *) & ditlck->lock;
  581. /*
  582. * copy btree root from in-memory inode to on-disk inode
  583. *
  584. * (tlock is taken from inline B+-tree root in in-memory
  585. * inode when the B+-tree root is updated, which is pointed
  586. * by jfs_ip->blid as well as being on tx tlock list)
  587. *
  588. * further processing of btree root is based on the copy
  589. * in in-memory inode, where txLog() will log from, and,
  590. * for xtree root, txUpdateMap() will update map and reset
  591. * XAD_NEW bit;
  592. */
  593. if (S_ISDIR(ip->i_mode) && (lid = jfs_ip->xtlid)) {
  594. /*
  595. * This is the special xtree inside the directory for storing
  596. * the directory table
  597. */
  598. xtpage_t *p, *xp;
  599. xad_t *xad;
  600. jfs_ip->xtlid = 0;
  601. tlck = lid_to_tlock(lid);
  602. assert(tlck->type & tlckXTREE);
  603. tlck->type |= tlckBTROOT;
  604. tlck->mp = mp;
  605. ilinelock = (struct linelock *) & tlck->lock;
  606. /*
  607. * copy xtree root from inode to dinode:
  608. */
  609. p = &jfs_ip->i_xtroot;
  610. xp = (xtpage_t *) &dp->di_dirtable;
  611. lv = ilinelock->lv;
  612. for (n = 0; n < ilinelock->index; n++, lv++) {
  613. memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
  614. lv->length << L2XTSLOTSIZE);
  615. }
  616. /* reset on-disk (metadata page) xtree XAD_NEW bit */
  617. xad = &xp->xad[XTENTRYSTART];
  618. for (n = XTENTRYSTART;
  619. n < le16_to_cpu(xp->header.nextindex); n++, xad++)
  620. if (xad->flag & (XAD_NEW | XAD_EXTENDED))
  621. xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
  622. }
  623. if ((lid = jfs_ip->blid) == 0)
  624. goto inlineData;
  625. jfs_ip->blid = 0;
  626. tlck = lid_to_tlock(lid);
  627. type = tlck->type;
  628. tlck->type |= tlckBTROOT;
  629. tlck->mp = mp;
  630. ilinelock = (struct linelock *) & tlck->lock;
  631. /*
  632. * regular file: 16 byte (XAD slot) granularity
  633. */
  634. if (type & tlckXTREE) {
  635. xtpage_t *p, *xp;
  636. xad_t *xad;
  637. /*
  638. * copy xtree root from inode to dinode:
  639. */
  640. p = &jfs_ip->i_xtroot;
  641. xp = &dp->di_xtroot;
  642. lv = ilinelock->lv;
  643. for (n = 0; n < ilinelock->index; n++, lv++) {
  644. memcpy(&xp->xad[lv->offset], &p->xad[lv->offset],
  645. lv->length << L2XTSLOTSIZE);
  646. }
  647. /* reset on-disk (metadata page) xtree XAD_NEW bit */
  648. xad = &xp->xad[XTENTRYSTART];
  649. for (n = XTENTRYSTART;
  650. n < le16_to_cpu(xp->header.nextindex); n++, xad++)
  651. if (xad->flag & (XAD_NEW | XAD_EXTENDED))
  652. xad->flag &= ~(XAD_NEW | XAD_EXTENDED);
  653. }
  654. /*
  655. * directory: 32 byte (directory entry slot) granularity
  656. */
  657. else if (type & tlckDTREE) {
  658. dtpage_t *p, *xp;
  659. /*
  660. * copy dtree root from inode to dinode:
  661. */
  662. p = (dtpage_t *) &jfs_ip->i_dtroot;
  663. xp = (dtpage_t *) & dp->di_dtroot;
  664. lv = ilinelock->lv;
  665. for (n = 0; n < ilinelock->index; n++, lv++) {
  666. memcpy(&xp->slot[lv->offset], &p->slot[lv->offset],
  667. lv->length << L2DTSLOTSIZE);
  668. }
  669. } else {
  670. jfs_err("diWrite: UFO tlock");
  671. }
  672. inlineData:
  673. /*
  674. * copy inline symlink from in-memory inode to on-disk inode
  675. */
  676. if (S_ISLNK(ip->i_mode) && ip->i_size < IDATASIZE) {
  677. lv = & dilinelock->lv[dilinelock->index];
  678. lv->offset = (dioffset + 2 * 128) >> L2INODESLOTSIZE;
  679. lv->length = 2;
  680. memcpy(&dp->di_fastsymlink, jfs_ip->i_inline, IDATASIZE);
  681. dilinelock->index++;
  682. }
  683. /*
  684. * copy inline data from in-memory inode to on-disk inode:
  685. * 128 byte slot granularity
  686. */
  687. if (test_cflag(COMMIT_Inlineea, ip)) {
  688. lv = & dilinelock->lv[dilinelock->index];
  689. lv->offset = (dioffset + 3 * 128) >> L2INODESLOTSIZE;
  690. lv->length = 1;
  691. memcpy(&dp->di_inlineea, jfs_ip->i_inline_ea, INODESLOTSIZE);
  692. dilinelock->index++;
  693. clear_cflag(COMMIT_Inlineea, ip);
  694. }
  695. /*
  696. * lock/copy inode base: 128 byte slot granularity
  697. */
  698. lv = & dilinelock->lv[dilinelock->index];
  699. lv->offset = dioffset >> L2INODESLOTSIZE;
  700. copy_to_dinode(dp, ip);
  701. if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
  702. lv->length = 2;
  703. memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
  704. } else
  705. lv->length = 1;
  706. dilinelock->index++;
  707. /* release the buffer holding the updated on-disk inode.
  708. * the buffer will be later written by commit processing.
  709. */
  710. write_metapage(mp);
  711. return (rc);
  712. }
  713. /*
  714. * NAME: diFree(ip)
  715. *
  716. * FUNCTION: free a specified inode from the inode working map
  717. * for a fileset or aggregate.
  718. *
  719. * if the inode to be freed represents the first (only)
  720. * free inode within the iag, the iag will be placed on
  721. * the ag free inode list.
  722. *
  723. * freeing the inode will cause the inode extent to be
  724. * freed if the inode is the only allocated inode within
  725. * the extent. in this case all the disk resource backing
  726. * up the inode extent will be freed. in addition, the iag
  727. * will be placed on the ag extent free list if the extent
  728. * is the first free extent in the iag. if freeing the
  729. * extent also means that no free inodes will exist for
  730. * the iag, the iag will also be removed from the ag free
  731. * inode list.
  732. *
  733. * the iag describing the inode will be freed if the extent
  734. * is to be freed and it is the only backed extent within
  735. * the iag. in this case, the iag will be removed from the
  736. * ag free extent list and ag free inode list and placed on
  737. * the inode map's free iag list.
  738. *
  739. * a careful update approach is used to provide consistency
  740. * in the face of updates to multiple buffers. under this
  741. * approach, all required buffers are obtained before making
  742. * any updates and are held until all updates are complete.
  743. *
  744. * PARAMETERS:
  745. * ip - inode to be freed.
  746. *
  747. * RETURN VALUES:
  748. * 0 - success
  749. * -EIO - i/o error.
  750. */
  751. int diFree(struct inode *ip)
  752. {
  753. int rc;
  754. ino_t inum = ip->i_ino;
  755. struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
  756. struct metapage *mp, *amp, *bmp, *cmp, *dmp;
  757. int iagno, ino, extno, bitno, sword, agno;
  758. int back, fwd;
  759. u32 bitmap, mask;
  760. struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
  761. struct inomap *imap = JFS_IP(ipimap)->i_imap;
  762. pxd_t freepxd;
  763. tid_t tid;
  764. struct inode *iplist[3];
  765. struct tlock *tlck;
  766. struct pxd_lock *pxdlock;
  767. /*
  768. * This is just to suppress compiler warnings. The same logic that
  769. * references these variables is used to initialize them.
  770. */
  771. aiagp = biagp = ciagp = diagp = NULL;
  772. /* get the iag number containing the inode.
  773. */
  774. iagno = INOTOIAG(inum);
  775. /* make sure that the iag is contained within
  776. * the map.
  777. */
  778. if (iagno >= imap->im_nextiag) {
  779. print_hex_dump(KERN_ERR, "imap: ", DUMP_PREFIX_ADDRESS, 16, 4,
  780. imap, 32, 0);
  781. jfs_error(ip->i_sb,
  782. "diFree: inum = %d, iagno = %d, nextiag = %d",
  783. (uint) inum, iagno, imap->im_nextiag);
  784. return -EIO;
  785. }
  786. /* get the allocation group for this ino.
  787. */
  788. agno = JFS_IP(ip)->agno;
  789. /* Lock the AG specific inode map information
  790. */
  791. AG_LOCK(imap, agno);
  792. /* Obtain read lock in imap inode. Don't release it until we have
  793. * read all of the IAG's that we are going to.
  794. */
  795. IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
  796. /* read the iag.
  797. */
  798. if ((rc = diIAGRead(imap, iagno, &mp))) {
  799. IREAD_UNLOCK(ipimap);
  800. AG_UNLOCK(imap, agno);
  801. return (rc);
  802. }
  803. iagp = (struct iag *) mp->data;
  804. /* get the inode number and extent number of the inode within
  805. * the iag and the inode number within the extent.
  806. */
  807. ino = inum & (INOSPERIAG - 1);
  808. extno = ino >> L2INOSPEREXT;
  809. bitno = ino & (INOSPEREXT - 1);
  810. mask = HIGHORDER >> bitno;
  811. if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
  812. jfs_error(ip->i_sb,
  813. "diFree: wmap shows inode already free");
  814. }
  815. if (!addressPXD(&iagp->inoext[extno])) {
  816. release_metapage(mp);
  817. IREAD_UNLOCK(ipimap);
  818. AG_UNLOCK(imap, agno);
  819. jfs_error(ip->i_sb, "diFree: invalid inoext");
  820. return -EIO;
  821. }
  822. /* compute the bitmap for the extent reflecting the freed inode.
  823. */
  824. bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
  825. if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
  826. release_metapage(mp);
  827. IREAD_UNLOCK(ipimap);
  828. AG_UNLOCK(imap, agno);
  829. jfs_error(ip->i_sb, "diFree: numfree > numinos");
  830. return -EIO;
  831. }
  832. /*
  833. * inode extent still has some inodes or below low water mark:
  834. * keep the inode extent;
  835. */
  836. if (bitmap ||
  837. imap->im_agctl[agno].numfree < 96 ||
  838. (imap->im_agctl[agno].numfree < 288 &&
  839. (((imap->im_agctl[agno].numfree * 100) /
  840. imap->im_agctl[agno].numinos) <= 25))) {
  841. /* if the iag currently has no free inodes (i.e.,
  842. * the inode being freed is the first free inode of iag),
  843. * insert the iag at head of the inode free list for the ag.
  844. */
  845. if (iagp->nfreeinos == 0) {
  846. /* check if there are any iags on the ag inode
  847. * free list. if so, read the first one so that
  848. * we can link the current iag onto the list at
  849. * the head.
  850. */
  851. if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
  852. /* read the iag that currently is the head
  853. * of the list.
  854. */
  855. if ((rc = diIAGRead(imap, fwd, &amp))) {
  856. IREAD_UNLOCK(ipimap);
  857. AG_UNLOCK(imap, agno);
  858. release_metapage(mp);
  859. return (rc);
  860. }
  861. aiagp = (struct iag *) amp->data;
  862. /* make current head point back to the iag.
  863. */
  864. aiagp->inofreeback = cpu_to_le32(iagno);
  865. write_metapage(amp);
  866. }
  867. /* iag points forward to current head and iag
  868. * becomes the new head of the list.
  869. */
  870. iagp->inofreefwd =
  871. cpu_to_le32(imap->im_agctl[agno].inofree);
  872. iagp->inofreeback = cpu_to_le32(-1);
  873. imap->im_agctl[agno].inofree = iagno;
  874. }
  875. IREAD_UNLOCK(ipimap);
  876. /* update the free inode summary map for the extent if
  877. * freeing the inode means the extent will now have free
  878. * inodes (i.e., the inode being freed is the first free
  879. * inode of extent),
  880. */
  881. if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
  882. sword = extno >> L2EXTSPERSUM;
  883. bitno = extno & (EXTSPERSUM - 1);
  884. iagp->inosmap[sword] &=
  885. cpu_to_le32(~(HIGHORDER >> bitno));
  886. }
  887. /* update the bitmap.
  888. */
  889. iagp->wmap[extno] = cpu_to_le32(bitmap);
  890. /* update the free inode counts at the iag, ag and
  891. * map level.
  892. */
  893. le32_add_cpu(&iagp->nfreeinos, 1);
  894. imap->im_agctl[agno].numfree += 1;
  895. atomic_inc(&imap->im_numfree);
  896. /* release the AG inode map lock
  897. */
  898. AG_UNLOCK(imap, agno);
  899. /* write the iag */
  900. write_metapage(mp);
  901. return (0);
  902. }
  903. /*
  904. * inode extent has become free and above low water mark:
  905. * free the inode extent;
  906. */
  907. /*
  908. * prepare to update iag list(s) (careful update step 1)
  909. */
  910. amp = bmp = cmp = dmp = NULL;
  911. fwd = back = -1;
  912. /* check if the iag currently has no free extents. if so,
  913. * it will be placed on the head of the ag extent free list.
  914. */
  915. if (iagp->nfreeexts == 0) {
  916. /* check if the ag extent free list has any iags.
  917. * if so, read the iag at the head of the list now.
  918. * this (head) iag will be updated later to reflect
  919. * the addition of the current iag at the head of
  920. * the list.
  921. */
  922. if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
  923. if ((rc = diIAGRead(imap, fwd, &amp)))
  924. goto error_out;
  925. aiagp = (struct iag *) amp->data;
  926. }
  927. } else {
  928. /* iag has free extents. check if the addition of a free
  929. * extent will cause all extents to be free within this
  930. * iag. if so, the iag will be removed from the ag extent
  931. * free list and placed on the inode map's free iag list.
  932. */
  933. if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
  934. /* in preparation for removing the iag from the
  935. * ag extent free list, read the iags preceeding
  936. * and following the iag on the ag extent free
  937. * list.
  938. */
  939. if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
  940. if ((rc = diIAGRead(imap, fwd, &amp)))
  941. goto error_out;
  942. aiagp = (struct iag *) amp->data;
  943. }
  944. if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
  945. if ((rc = diIAGRead(imap, back, &bmp)))
  946. goto error_out;
  947. biagp = (struct iag *) bmp->data;
  948. }
  949. }
  950. }
  951. /* remove the iag from the ag inode free list if freeing
  952. * this extent cause the iag to have no free inodes.
  953. */
  954. if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
  955. int inofreeback = le32_to_cpu(iagp->inofreeback);
  956. int inofreefwd = le32_to_cpu(iagp->inofreefwd);
  957. /* in preparation for removing the iag from the
  958. * ag inode free list, read the iags preceeding
  959. * and following the iag on the ag inode free
  960. * list. before reading these iags, we must make
  961. * sure that we already don't have them in hand
  962. * from up above, since re-reading an iag (buffer)
  963. * we are currently holding would cause a deadlock.
  964. */
  965. if (inofreefwd >= 0) {
  966. if (inofreefwd == fwd)
  967. ciagp = (struct iag *) amp->data;
  968. else if (inofreefwd == back)
  969. ciagp = (struct iag *) bmp->data;
  970. else {
  971. if ((rc =
  972. diIAGRead(imap, inofreefwd, &cmp)))
  973. goto error_out;
  974. ciagp = (struct iag *) cmp->data;
  975. }
  976. assert(ciagp != NULL);
  977. }
  978. if (inofreeback >= 0) {
  979. if (inofreeback == fwd)
  980. diagp = (struct iag *) amp->data;
  981. else if (inofreeback == back)
  982. diagp = (struct iag *) bmp->data;
  983. else {
  984. if ((rc =
  985. diIAGRead(imap, inofreeback, &dmp)))
  986. goto error_out;
  987. diagp = (struct iag *) dmp->data;
  988. }
  989. assert(diagp != NULL);
  990. }
  991. }
  992. IREAD_UNLOCK(ipimap);
  993. /*
  994. * invalidate any page of the inode extent freed from buffer cache;
  995. */
  996. freepxd = iagp->inoext[extno];
  997. invalidate_pxd_metapages(ip, freepxd);
  998. /*
  999. * update iag list(s) (careful update step 2)
  1000. */
  1001. /* add the iag to the ag extent free list if this is the
  1002. * first free extent for the iag.
  1003. */
  1004. if (iagp->nfreeexts == 0) {
  1005. if (fwd >= 0)
  1006. aiagp->extfreeback = cpu_to_le32(iagno);
  1007. iagp->extfreefwd =
  1008. cpu_to_le32(imap->im_agctl[agno].extfree);
  1009. iagp->extfreeback = cpu_to_le32(-1);
  1010. imap->im_agctl[agno].extfree = iagno;
  1011. } else {
  1012. /* remove the iag from the ag extent list if all extents
  1013. * are now free and place it on the inode map iag free list.
  1014. */
  1015. if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
  1016. if (fwd >= 0)
  1017. aiagp->extfreeback = iagp->extfreeback;
  1018. if (back >= 0)
  1019. biagp->extfreefwd = iagp->extfreefwd;
  1020. else
  1021. imap->im_agctl[agno].extfree =
  1022. le32_to_cpu(iagp->extfreefwd);
  1023. iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
  1024. IAGFREE_LOCK(imap);
  1025. iagp->iagfree = cpu_to_le32(imap->im_freeiag);
  1026. imap->im_freeiag = iagno;
  1027. IAGFREE_UNLOCK(imap);
  1028. }
  1029. }
  1030. /* remove the iag from the ag inode free list if freeing
  1031. * this extent causes the iag to have no free inodes.
  1032. */
  1033. if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
  1034. if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
  1035. ciagp->inofreeback = iagp->inofreeback;
  1036. if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
  1037. diagp->inofreefwd = iagp->inofreefwd;
  1038. else
  1039. imap->im_agctl[agno].inofree =
  1040. le32_to_cpu(iagp->inofreefwd);
  1041. iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
  1042. }
  1043. /* update the inode extent address and working map
  1044. * to reflect the free extent.
  1045. * the permanent map should have been updated already
  1046. * for the inode being freed.
  1047. */
  1048. if (iagp->pmap[extno] != 0) {
  1049. jfs_error(ip->i_sb, "diFree: the pmap does not show inode free");
  1050. }
  1051. iagp->wmap[extno] = 0;
  1052. PXDlength(&iagp->inoext[extno], 0);
  1053. PXDaddress(&iagp->inoext[extno], 0);
  1054. /* update the free extent and free inode summary maps
  1055. * to reflect the freed extent.
  1056. * the inode summary map is marked to indicate no inodes
  1057. * available for the freed extent.
  1058. */
  1059. sword = extno >> L2EXTSPERSUM;
  1060. bitno = extno & (EXTSPERSUM - 1);
  1061. mask = HIGHORDER >> bitno;
  1062. iagp->inosmap[sword] |= cpu_to_le32(mask);
  1063. iagp->extsmap[sword] &= cpu_to_le32(~mask);
  1064. /* update the number of free inodes and number of free extents
  1065. * for the iag.
  1066. */
  1067. le32_add_cpu(&iagp->nfreeinos, -(INOSPEREXT - 1));
  1068. le32_add_cpu(&iagp->nfreeexts, 1);
  1069. /* update the number of free inodes and backed inodes
  1070. * at the ag and inode map level.
  1071. */
  1072. imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
  1073. imap->im_agctl[agno].numinos -= INOSPEREXT;
  1074. atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
  1075. atomic_sub(INOSPEREXT, &imap->im_numinos);
  1076. if (amp)
  1077. write_metapage(amp);
  1078. if (bmp)
  1079. write_metapage(bmp);
  1080. if (cmp)
  1081. write_metapage(cmp);
  1082. if (dmp)
  1083. write_metapage(dmp);
  1084. /*
  1085. * start transaction to update block allocation map
  1086. * for the inode extent freed;
  1087. *
  1088. * N.B. AG_LOCK is released and iag will be released below, and
  1089. * other thread may allocate inode from/reusing the ixad freed
  1090. * BUT with new/different backing inode extent from the extent
  1091. * to be freed by the transaction;
  1092. */
  1093. tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
  1094. mutex_lock(&JFS_IP(ipimap)->commit_mutex);
  1095. /* acquire tlock of the iag page of the freed ixad
  1096. * to force the page NOHOMEOK (even though no data is
  1097. * logged from the iag page) until NOREDOPAGE|FREEXTENT log
  1098. * for the free of the extent is committed;
  1099. * write FREEXTENT|NOREDOPAGE log record
  1100. * N.B. linelock is overlaid as freed extent descriptor;
  1101. */
  1102. tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
  1103. pxdlock = (struct pxd_lock *) & tlck->lock;
  1104. pxdlock->flag = mlckFREEPXD;
  1105. pxdlock->pxd = freepxd;
  1106. pxdlock->index = 1;
  1107. write_metapage(mp);
  1108. iplist[0] = ipimap;
  1109. /*
  1110. * logredo needs the IAG number and IAG extent index in order
  1111. * to ensure that the IMap is consistent. The least disruptive
  1112. * way to pass these values through to the transaction manager
  1113. * is in the iplist array.
  1114. *
  1115. * It's not pretty, but it works.
  1116. */
  1117. iplist[1] = (struct inode *) (size_t)iagno;
  1118. iplist[2] = (struct inode *) (size_t)extno;
  1119. rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
  1120. txEnd(tid);
  1121. mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
  1122. /* unlock the AG inode map information */
  1123. AG_UNLOCK(imap, agno);
  1124. return (0);
  1125. error_out:
  1126. IREAD_UNLOCK(ipimap);
  1127. if (amp)
  1128. release_metapage(amp);
  1129. if (bmp)
  1130. release_metapage(bmp);
  1131. if (cmp)
  1132. release_metapage(cmp);
  1133. if (dmp)
  1134. release_metapage(dmp);
  1135. AG_UNLOCK(imap, agno);
  1136. release_metapage(mp);
  1137. return (rc);
  1138. }
  1139. /*
  1140. * There are several places in the diAlloc* routines where we initialize
  1141. * the inode.
  1142. */
  1143. static inline void
  1144. diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
  1145. {
  1146. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  1147. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  1148. ip->i_ino = (iagno << L2INOSPERIAG) + ino;
  1149. jfs_ip->ixpxd = iagp->inoext[extno];
  1150. jfs_ip->agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
  1151. jfs_ip->active_ag = -1;
  1152. }
  1153. /*
  1154. * NAME: diAlloc(pip,dir,ip)
  1155. *
  1156. * FUNCTION: allocate a disk inode from the inode working map
  1157. * for a fileset or aggregate.
  1158. *
  1159. * PARAMETERS:
  1160. * pip - pointer to incore inode for the parent inode.
  1161. * dir - 'true' if the new disk inode is for a directory.
  1162. * ip - pointer to a new inode
  1163. *
  1164. * RETURN VALUES:
  1165. * 0 - success.
  1166. * -ENOSPC - insufficient disk resources.
  1167. * -EIO - i/o error.
  1168. */
  1169. int diAlloc(struct inode *pip, bool dir, struct inode *ip)
  1170. {
  1171. int rc, ino, iagno, addext, extno, bitno, sword;
  1172. int nwords, rem, i, agno;
  1173. u32 mask, inosmap, extsmap;
  1174. struct inode *ipimap;
  1175. struct metapage *mp;
  1176. ino_t inum;
  1177. struct iag *iagp;
  1178. struct inomap *imap;
  1179. /* get the pointers to the inode map inode and the
  1180. * corresponding imap control structure.
  1181. */
  1182. ipimap = JFS_SBI(pip->i_sb)->ipimap;
  1183. imap = JFS_IP(ipimap)->i_imap;
  1184. JFS_IP(ip)->ipimap = ipimap;
  1185. JFS_IP(ip)->fileset = FILESYSTEM_I;
  1186. /* for a directory, the allocation policy is to start
  1187. * at the ag level using the preferred ag.
  1188. */
  1189. if (dir) {
  1190. agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
  1191. AG_LOCK(imap, agno);
  1192. goto tryag;
  1193. }
  1194. /* for files, the policy starts off by trying to allocate from
  1195. * the same iag containing the parent disk inode:
  1196. * try to allocate the new disk inode close to the parent disk
  1197. * inode, using parent disk inode number + 1 as the allocation
  1198. * hint. (we use a left-to-right policy to attempt to avoid
  1199. * moving backward on the disk.) compute the hint within the
  1200. * file system and the iag.
  1201. */
  1202. /* get the ag number of this iag */
  1203. agno = JFS_IP(pip)->agno;
  1204. if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
  1205. /*
  1206. * There is an open file actively growing. We want to
  1207. * allocate new inodes from a different ag to avoid
  1208. * fragmentation problems.
  1209. */
  1210. agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
  1211. AG_LOCK(imap, agno);
  1212. goto tryag;
  1213. }
  1214. inum = pip->i_ino + 1;
  1215. ino = inum & (INOSPERIAG - 1);
  1216. /* back off the hint if it is outside of the iag */
  1217. if (ino == 0)
  1218. inum = pip->i_ino;
  1219. /* lock the AG inode map information */
  1220. AG_LOCK(imap, agno);
  1221. /* Get read lock on imap inode */
  1222. IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
  1223. /* get the iag number and read the iag */
  1224. iagno = INOTOIAG(inum);
  1225. if ((rc = diIAGRead(imap, iagno, &mp))) {
  1226. IREAD_UNLOCK(ipimap);
  1227. AG_UNLOCK(imap, agno);
  1228. return (rc);
  1229. }
  1230. iagp = (struct iag *) mp->data;
  1231. /* determine if new inode extent is allowed to be added to the iag.
  1232. * new inode extent can be added to the iag if the ag
  1233. * has less than 32 free disk inodes and the iag has free extents.
  1234. */
  1235. addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
  1236. /*
  1237. * try to allocate from the IAG
  1238. */
  1239. /* check if the inode may be allocated from the iag
  1240. * (i.e. the inode has free inodes or new extent can be added).
  1241. */
  1242. if (iagp->nfreeinos || addext) {
  1243. /* determine the extent number of the hint.
  1244. */
  1245. extno = ino >> L2INOSPEREXT;
  1246. /* check if the extent containing the hint has backed
  1247. * inodes. if so, try to allocate within this extent.
  1248. */
  1249. if (addressPXD(&iagp->inoext[extno])) {
  1250. bitno = ino & (INOSPEREXT - 1);
  1251. if ((bitno =
  1252. diFindFree(le32_to_cpu(iagp->wmap[extno]),
  1253. bitno))
  1254. < INOSPEREXT) {
  1255. ino = (extno << L2INOSPEREXT) + bitno;
  1256. /* a free inode (bit) was found within this
  1257. * extent, so allocate it.
  1258. */
  1259. rc = diAllocBit(imap, iagp, ino);
  1260. IREAD_UNLOCK(ipimap);
  1261. if (rc) {
  1262. assert(rc == -EIO);
  1263. } else {
  1264. /* set the results of the allocation
  1265. * and write the iag.
  1266. */
  1267. diInitInode(ip, iagno, ino, extno,
  1268. iagp);
  1269. mark_metapage_dirty(mp);
  1270. }
  1271. release_metapage(mp);
  1272. /* free the AG lock and return.
  1273. */
  1274. AG_UNLOCK(imap, agno);
  1275. return (rc);
  1276. }
  1277. if (!addext)
  1278. extno =
  1279. (extno ==
  1280. EXTSPERIAG - 1) ? 0 : extno + 1;
  1281. }
  1282. /*
  1283. * no free inodes within the extent containing the hint.
  1284. *
  1285. * try to allocate from the backed extents following
  1286. * hint or, if appropriate (i.e. addext is true), allocate
  1287. * an extent of free inodes at or following the extent
  1288. * containing the hint.
  1289. *
  1290. * the free inode and free extent summary maps are used
  1291. * here, so determine the starting summary map position
  1292. * and the number of words we'll have to examine. again,
  1293. * the approach is to allocate following the hint, so we
  1294. * might have to initially ignore prior bits of the summary
  1295. * map that represent extents prior to the extent containing
  1296. * the hint and later revisit these bits.
  1297. */
  1298. bitno = extno & (EXTSPERSUM - 1);
  1299. nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
  1300. sword = extno >> L2EXTSPERSUM;
  1301. /* mask any prior bits for the starting words of the
  1302. * summary map.
  1303. */
  1304. mask = ONES << (EXTSPERSUM - bitno);
  1305. inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
  1306. extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
  1307. /* scan the free inode and free extent summary maps for
  1308. * free resources.
  1309. */
  1310. for (i = 0; i < nwords; i++) {
  1311. /* check if this word of the free inode summary
  1312. * map describes an extent with free inodes.
  1313. */
  1314. if (~inosmap) {
  1315. /* an extent with free inodes has been
  1316. * found. determine the extent number
  1317. * and the inode number within the extent.
  1318. */
  1319. rem = diFindFree(inosmap, 0);
  1320. extno = (sword << L2EXTSPERSUM) + rem;
  1321. rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
  1322. 0);
  1323. if (rem >= INOSPEREXT) {
  1324. IREAD_UNLOCK(ipimap);
  1325. release_metapage(mp);
  1326. AG_UNLOCK(imap, agno);
  1327. jfs_error(ip->i_sb,
  1328. "diAlloc: can't find free bit "
  1329. "in wmap");
  1330. return -EIO;
  1331. }
  1332. /* determine the inode number within the
  1333. * iag and allocate the inode from the
  1334. * map.
  1335. */
  1336. ino = (extno << L2INOSPEREXT) + rem;
  1337. rc = diAllocBit(imap, iagp, ino);
  1338. IREAD_UNLOCK(ipimap);
  1339. if (rc)
  1340. assert(rc == -EIO);
  1341. else {
  1342. /* set the results of the allocation
  1343. * and write the iag.
  1344. */
  1345. diInitInode(ip, iagno, ino, extno,
  1346. iagp);
  1347. mark_metapage_dirty(mp);
  1348. }
  1349. release_metapage(mp);
  1350. /* free the AG lock and return.
  1351. */
  1352. AG_UNLOCK(imap, agno);
  1353. return (rc);
  1354. }
  1355. /* check if we may allocate an extent of free
  1356. * inodes and whether this word of the free
  1357. * extents summary map describes a free extent.
  1358. */
  1359. if (addext && ~extsmap) {
  1360. /* a free extent has been found. determine
  1361. * the extent number.
  1362. */
  1363. rem = diFindFree(extsmap, 0);
  1364. extno = (sword << L2EXTSPERSUM) + rem;
  1365. /* allocate an extent of free inodes.
  1366. */
  1367. if ((rc = diNewExt(imap, iagp, extno))) {
  1368. /* if there is no disk space for a
  1369. * new extent, try to allocate the
  1370. * disk inode from somewhere else.
  1371. */
  1372. if (rc == -ENOSPC)
  1373. break;
  1374. assert(rc == -EIO);
  1375. } else {
  1376. /* set the results of the allocation
  1377. * and write the iag.
  1378. */
  1379. diInitInode(ip, iagno,
  1380. extno << L2INOSPEREXT,
  1381. extno, iagp);
  1382. mark_metapage_dirty(mp);
  1383. }
  1384. release_metapage(mp);
  1385. /* free the imap inode & the AG lock & return.
  1386. */
  1387. IREAD_UNLOCK(ipimap);
  1388. AG_UNLOCK(imap, agno);
  1389. return (rc);
  1390. }
  1391. /* move on to the next set of summary map words.
  1392. */
  1393. sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
  1394. inosmap = le32_to_cpu(iagp->inosmap[sword]);
  1395. extsmap = le32_to_cpu(iagp->extsmap[sword]);
  1396. }
  1397. }
  1398. /* unlock imap inode */
  1399. IREAD_UNLOCK(ipimap);
  1400. /* nothing doing in this iag, so release it. */
  1401. release_metapage(mp);
  1402. tryag:
  1403. /*
  1404. * try to allocate anywhere within the same AG as the parent inode.
  1405. */
  1406. rc = diAllocAG(imap, agno, dir, ip);
  1407. AG_UNLOCK(imap, agno);
  1408. if (rc != -ENOSPC)
  1409. return (rc);
  1410. /*
  1411. * try to allocate in any AG.
  1412. */
  1413. return (diAllocAny(imap, agno, dir, ip));
  1414. }
  1415. /*
  1416. * NAME: diAllocAG(imap,agno,dir,ip)
  1417. *
  1418. * FUNCTION: allocate a disk inode from the allocation group.
  1419. *
  1420. * this routine first determines if a new extent of free
  1421. * inodes should be added for the allocation group, with
  1422. * the current request satisfied from this extent. if this
  1423. * is the case, an attempt will be made to do just that. if
  1424. * this attempt fails or it has been determined that a new
  1425. * extent should not be added, an attempt is made to satisfy
  1426. * the request by allocating an existing (backed) free inode
  1427. * from the allocation group.
  1428. *
  1429. * PRE CONDITION: Already have the AG lock for this AG.
  1430. *
  1431. * PARAMETERS:
  1432. * imap - pointer to inode map control structure.
  1433. * agno - allocation group to allocate from.
  1434. * dir - 'true' if the new disk inode is for a directory.
  1435. * ip - pointer to the new inode to be filled in on successful return
  1436. * with the disk inode number allocated, its extent address
  1437. * and the start of the ag.
  1438. *
  1439. * RETURN VALUES:
  1440. * 0 - success.
  1441. * -ENOSPC - insufficient disk resources.
  1442. * -EIO - i/o error.
  1443. */
  1444. static int
  1445. diAllocAG(struct inomap * imap, int agno, bool dir, struct inode *ip)
  1446. {
  1447. int rc, addext, numfree, numinos;
  1448. /* get the number of free and the number of backed disk
  1449. * inodes currently within the ag.
  1450. */
  1451. numfree = imap->im_agctl[agno].numfree;
  1452. numinos = imap->im_agctl[agno].numinos;
  1453. if (numfree > numinos) {
  1454. jfs_error(ip->i_sb, "diAllocAG: numfree > numinos");
  1455. return -EIO;
  1456. }
  1457. /* determine if we should allocate a new extent of free inodes
  1458. * within the ag: for directory inodes, add a new extent
  1459. * if there are a small number of free inodes or number of free
  1460. * inodes is a small percentage of the number of backed inodes.
  1461. */
  1462. if (dir)
  1463. addext = (numfree < 64 ||
  1464. (numfree < 256
  1465. && ((numfree * 100) / numinos) <= 20));
  1466. else
  1467. addext = (numfree == 0);
  1468. /*
  1469. * try to allocate a new extent of free inodes.
  1470. */
  1471. if (addext) {
  1472. /* if free space is not avaliable for this new extent, try
  1473. * below to allocate a free and existing (already backed)
  1474. * inode from the ag.
  1475. */
  1476. if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
  1477. return (rc);
  1478. }
  1479. /*
  1480. * try to allocate an existing free inode from the ag.
  1481. */
  1482. return (diAllocIno(imap, agno, ip));
  1483. }
  1484. /*
  1485. * NAME: diAllocAny(imap,agno,dir,iap)
  1486. *
  1487. * FUNCTION: allocate a disk inode from any other allocation group.
  1488. *
  1489. * this routine is called when an allocation attempt within
  1490. * the primary allocation group has failed. if attempts to
  1491. * allocate an inode from any allocation group other than the
  1492. * specified primary group.
  1493. *
  1494. * PARAMETERS:
  1495. * imap - pointer to inode map control structure.
  1496. * agno - primary allocation group (to avoid).
  1497. * dir - 'true' if the new disk inode is for a directory.
  1498. * ip - pointer to a new inode to be filled in on successful return
  1499. * with the disk inode number allocated, its extent address
  1500. * and the start of the ag.
  1501. *
  1502. * RETURN VALUES:
  1503. * 0 - success.
  1504. * -ENOSPC - insufficient disk resources.
  1505. * -EIO - i/o error.
  1506. */
  1507. static int
  1508. diAllocAny(struct inomap * imap, int agno, bool dir, struct inode *ip)
  1509. {
  1510. int ag, rc;
  1511. int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
  1512. /* try to allocate from the ags following agno up to
  1513. * the maximum ag number.
  1514. */
  1515. for (ag = agno + 1; ag <= maxag; ag++) {
  1516. AG_LOCK(imap, ag);
  1517. rc = diAllocAG(imap, ag, dir, ip);
  1518. AG_UNLOCK(imap, ag);
  1519. if (rc != -ENOSPC)
  1520. return (rc);
  1521. }
  1522. /* try to allocate from the ags in front of agno.
  1523. */
  1524. for (ag = 0; ag < agno; ag++) {
  1525. AG_LOCK(imap, ag);
  1526. rc = diAllocAG(imap, ag, dir, ip);
  1527. AG_UNLOCK(imap, ag);
  1528. if (rc != -ENOSPC)
  1529. return (rc);
  1530. }
  1531. /* no free disk inodes.
  1532. */
  1533. return -ENOSPC;
  1534. }
  1535. /*
  1536. * NAME: diAllocIno(imap,agno,ip)
  1537. *
  1538. * FUNCTION: allocate a disk inode from the allocation group's free
  1539. * inode list, returning an error if this free list is
  1540. * empty (i.e. no iags on the list).
  1541. *
  1542. * allocation occurs from the first iag on the list using
  1543. * the iag's free inode summary map to find the leftmost
  1544. * free inode in the iag.
  1545. *
  1546. * PRE CONDITION: Already have AG lock for this AG.
  1547. *
  1548. * PARAMETERS:
  1549. * imap - pointer to inode map control structure.
  1550. * agno - allocation group.
  1551. * ip - pointer to new inode to be filled in on successful return
  1552. * with the disk inode number allocated, its extent address
  1553. * and the start of the ag.
  1554. *
  1555. * RETURN VALUES:
  1556. * 0 - success.
  1557. * -ENOSPC - insufficient disk resources.
  1558. * -EIO - i/o error.
  1559. */
  1560. static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
  1561. {
  1562. int iagno, ino, rc, rem, extno, sword;
  1563. struct metapage *mp;
  1564. struct iag *iagp;
  1565. /* check if there are iags on the ag's free inode list.
  1566. */
  1567. if ((iagno = imap->im_agctl[agno].inofree) < 0)
  1568. return -ENOSPC;
  1569. /* obtain read lock on imap inode */
  1570. IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
  1571. /* read the iag at the head of the list.
  1572. */
  1573. if ((rc = diIAGRead(imap, iagno, &mp))) {
  1574. IREAD_UNLOCK(imap->im_ipimap);
  1575. return (rc);
  1576. }
  1577. iagp = (struct iag *) mp->data;
  1578. /* better be free inodes in this iag if it is on the
  1579. * list.
  1580. */
  1581. if (!iagp->nfreeinos) {
  1582. IREAD_UNLOCK(imap->im_ipimap);
  1583. release_metapage(mp);
  1584. jfs_error(ip->i_sb,
  1585. "diAllocIno: nfreeinos = 0, but iag on freelist");
  1586. return -EIO;
  1587. }
  1588. /* scan the free inode summary map to find an extent
  1589. * with free inodes.
  1590. */
  1591. for (sword = 0;; sword++) {
  1592. if (sword >= SMAPSZ) {
  1593. IREAD_UNLOCK(imap->im_ipimap);
  1594. release_metapage(mp);
  1595. jfs_error(ip->i_sb,
  1596. "diAllocIno: free inode not found in summary map");
  1597. return -EIO;
  1598. }
  1599. if (~iagp->inosmap[sword])
  1600. break;
  1601. }
  1602. /* found a extent with free inodes. determine
  1603. * the extent number.
  1604. */
  1605. rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
  1606. if (rem >= EXTSPERSUM) {
  1607. IREAD_UNLOCK(imap->im_ipimap);
  1608. release_metapage(mp);
  1609. jfs_error(ip->i_sb, "diAllocIno: no free extent found");
  1610. return -EIO;
  1611. }
  1612. extno = (sword << L2EXTSPERSUM) + rem;
  1613. /* find the first free inode in the extent.
  1614. */
  1615. rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
  1616. if (rem >= INOSPEREXT) {
  1617. IREAD_UNLOCK(imap->im_ipimap);
  1618. release_metapage(mp);
  1619. jfs_error(ip->i_sb, "diAllocIno: free inode not found");
  1620. return -EIO;
  1621. }
  1622. /* compute the inode number within the iag.
  1623. */
  1624. ino = (extno << L2INOSPEREXT) + rem;
  1625. /* allocate the inode.
  1626. */
  1627. rc = diAllocBit(imap, iagp, ino);
  1628. IREAD_UNLOCK(imap->im_ipimap);
  1629. if (rc) {
  1630. release_metapage(mp);
  1631. return (rc);
  1632. }
  1633. /* set the results of the allocation and write the iag.
  1634. */
  1635. diInitInode(ip, iagno, ino, extno, iagp);
  1636. write_metapage(mp);
  1637. return (0);
  1638. }
  1639. /*
  1640. * NAME: diAllocExt(imap,agno,ip)
  1641. *
  1642. * FUNCTION: add a new extent of free inodes to an iag, allocating
  1643. * an inode from this extent to satisfy the current allocation
  1644. * request.
  1645. *
  1646. * this routine first tries to find an existing iag with free
  1647. * extents through the ag free extent list. if list is not
  1648. * empty, the head of the list will be selected as the home
  1649. * of the new extent of free inodes. otherwise (the list is
  1650. * empty), a new iag will be allocated for the ag to contain
  1651. * the extent.
  1652. *
  1653. * once an iag has been selected, the free extent summary map
  1654. * is used to locate a free extent within the iag and diNewExt()
  1655. * is called to initialize the extent, with initialization
  1656. * including the allocation of the first inode of the extent
  1657. * for the purpose of satisfying this request.
  1658. *
  1659. * PARAMETERS:
  1660. * imap - pointer to inode map control structure.
  1661. * agno - allocation group number.
  1662. * ip - pointer to new inode to be filled in on successful return
  1663. * with the disk inode number allocated, its extent address
  1664. * and the start of the ag.
  1665. *
  1666. * RETURN VALUES:
  1667. * 0 - success.
  1668. * -ENOSPC - insufficient disk resources.
  1669. * -EIO - i/o error.
  1670. */
  1671. static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
  1672. {
  1673. int rem, iagno, sword, extno, rc;
  1674. struct metapage *mp;
  1675. struct iag *iagp;
  1676. /* check if the ag has any iags with free extents. if not,
  1677. * allocate a new iag for the ag.
  1678. */
  1679. if ((iagno = imap->im_agctl[agno].extfree) < 0) {
  1680. /* If successful, diNewIAG will obtain the read lock on the
  1681. * imap inode.
  1682. */
  1683. if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
  1684. return (rc);
  1685. }
  1686. iagp = (struct iag *) mp->data;
  1687. /* set the ag number if this a brand new iag
  1688. */
  1689. iagp->agstart =
  1690. cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
  1691. } else {
  1692. /* read the iag.
  1693. */
  1694. IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
  1695. if ((rc = diIAGRead(imap, iagno, &mp))) {
  1696. IREAD_UNLOCK(imap->im_ipimap);
  1697. jfs_error(ip->i_sb, "diAllocExt: error reading iag");
  1698. return rc;
  1699. }
  1700. iagp = (struct iag *) mp->data;
  1701. }
  1702. /* using the free extent summary map, find a free extent.
  1703. */
  1704. for (sword = 0;; sword++) {
  1705. if (sword >= SMAPSZ) {
  1706. release_metapage(mp);
  1707. IREAD_UNLOCK(imap->im_ipimap);
  1708. jfs_error(ip->i_sb,
  1709. "diAllocExt: free ext summary map not found");
  1710. return -EIO;
  1711. }
  1712. if (~iagp->extsmap[sword])
  1713. break;
  1714. }
  1715. /* determine the extent number of the free extent.
  1716. */
  1717. rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
  1718. if (rem >= EXTSPERSUM) {
  1719. release_metapage(mp);
  1720. IREAD_UNLOCK(imap->im_ipimap);
  1721. jfs_error(ip->i_sb, "diAllocExt: free extent not found");
  1722. return -EIO;
  1723. }
  1724. extno = (sword << L2EXTSPERSUM) + rem;
  1725. /* initialize the new extent.
  1726. */
  1727. rc = diNewExt(imap, iagp, extno);
  1728. IREAD_UNLOCK(imap->im_ipimap);
  1729. if (rc) {
  1730. /* something bad happened. if a new iag was allocated,
  1731. * place it back on the inode map's iag free list, and
  1732. * clear the ag number information.
  1733. */
  1734. if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
  1735. IAGFREE_LOCK(imap);
  1736. iagp->iagfree = cpu_to_le32(imap->im_freeiag);
  1737. imap->im_freeiag = iagno;
  1738. IAGFREE_UNLOCK(imap);
  1739. }
  1740. write_metapage(mp);
  1741. return (rc);
  1742. }
  1743. /* set the results of the allocation and write the iag.
  1744. */
  1745. diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
  1746. write_metapage(mp);
  1747. return (0);
  1748. }
  1749. /*
  1750. * NAME: diAllocBit(imap,iagp,ino)
  1751. *
  1752. * FUNCTION: allocate a backed inode from an iag.
  1753. *
  1754. * this routine performs the mechanics of allocating a
  1755. * specified inode from a backed extent.
  1756. *
  1757. * if the inode to be allocated represents the last free
  1758. * inode within the iag, the iag will be removed from the
  1759. * ag free inode list.
  1760. *
  1761. * a careful update approach is used to provide consistency
  1762. * in the face of updates to multiple buffers. under this
  1763. * approach, all required buffers are obtained before making
  1764. * any updates and are held all are updates are complete.
  1765. *
  1766. * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
  1767. * this AG. Must have read lock on imap inode.
  1768. *
  1769. * PARAMETERS:
  1770. * imap - pointer to inode map control structure.
  1771. * iagp - pointer to iag.
  1772. * ino - inode number to be allocated within the iag.
  1773. *
  1774. * RETURN VALUES:
  1775. * 0 - success.
  1776. * -ENOSPC - insufficient disk resources.
  1777. * -EIO - i/o error.
  1778. */
  1779. static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
  1780. {
  1781. int extno, bitno, agno, sword, rc;
  1782. struct metapage *amp = NULL, *bmp = NULL;
  1783. struct iag *aiagp = NULL, *biagp = NULL;
  1784. u32 mask;
  1785. /* check if this is the last free inode within the iag.
  1786. * if so, it will have to be removed from the ag free
  1787. * inode list, so get the iags preceeding and following
  1788. * it on the list.
  1789. */
  1790. if (iagp->nfreeinos == cpu_to_le32(1)) {
  1791. if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
  1792. if ((rc =
  1793. diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
  1794. &amp)))
  1795. return (rc);
  1796. aiagp = (struct iag *) amp->data;
  1797. }
  1798. if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
  1799. if ((rc =
  1800. diIAGRead(imap,
  1801. le32_to_cpu(iagp->inofreeback),
  1802. &bmp))) {
  1803. if (amp)
  1804. release_metapage(amp);
  1805. return (rc);
  1806. }
  1807. biagp = (struct iag *) bmp->data;
  1808. }
  1809. }
  1810. /* get the ag number, extent number, inode number within
  1811. * the extent.
  1812. */
  1813. agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
  1814. extno = ino >> L2INOSPEREXT;
  1815. bitno = ino & (INOSPEREXT - 1);
  1816. /* compute the mask for setting the map.
  1817. */
  1818. mask = HIGHORDER >> bitno;
  1819. /* the inode should be free and backed.
  1820. */
  1821. if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
  1822. ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
  1823. (addressPXD(&iagp->inoext[extno]) == 0)) {
  1824. if (amp)
  1825. release_metapage(amp);
  1826. if (bmp)
  1827. release_metapage(bmp);
  1828. jfs_error(imap->im_ipimap->i_sb,
  1829. "diAllocBit: iag inconsistent");
  1830. return -EIO;
  1831. }
  1832. /* mark the inode as allocated in the working map.
  1833. */
  1834. iagp->wmap[extno] |= cpu_to_le32(mask);
  1835. /* check if all inodes within the extent are now
  1836. * allocated. if so, update the free inode summary
  1837. * map to reflect this.
  1838. */
  1839. if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
  1840. sword = extno >> L2EXTSPERSUM;
  1841. bitno = extno & (EXTSPERSUM - 1);
  1842. iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
  1843. }
  1844. /* if this was the last free inode in the iag, remove the
  1845. * iag from the ag free inode list.
  1846. */
  1847. if (iagp->nfreeinos == cpu_to_le32(1)) {
  1848. if (amp) {
  1849. aiagp->inofreeback = iagp->inofreeback;
  1850. write_metapage(amp);
  1851. }
  1852. if (bmp) {
  1853. biagp->inofreefwd = iagp->inofreefwd;
  1854. write_metapage(bmp);
  1855. } else {
  1856. imap->im_agctl[agno].inofree =
  1857. le32_to_cpu(iagp->inofreefwd);
  1858. }
  1859. iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
  1860. }
  1861. /* update the free inode count at the iag, ag, inode
  1862. * map levels.
  1863. */
  1864. le32_add_cpu(&iagp->nfreeinos, -1);
  1865. imap->im_agctl[agno].numfree -= 1;
  1866. atomic_dec(&imap->im_numfree);
  1867. return (0);
  1868. }
  1869. /*
  1870. * NAME: diNewExt(imap,iagp,extno)
  1871. *
  1872. * FUNCTION: initialize a new extent of inodes for an iag, allocating
  1873. * the first inode of the extent for use for the current
  1874. * allocation request.
  1875. *
  1876. * disk resources are allocated for the new extent of inodes
  1877. * and the inodes themselves are initialized to reflect their
  1878. * existence within the extent (i.e. their inode numbers and
  1879. * inode extent addresses are set) and their initial state
  1880. * (mode and link count are set to zero).
  1881. *
  1882. * if the iag is new, it is not yet on an ag extent free list
  1883. * but will now be placed on this list.
  1884. *
  1885. * if the allocation of the new extent causes the iag to
  1886. * have no free extent, the iag will be removed from the
  1887. * ag extent free list.
  1888. *
  1889. * if the iag has no free backed inodes, it will be placed
  1890. * on the ag free inode list, since the addition of the new
  1891. * extent will now cause it to have free inodes.
  1892. *
  1893. * a careful update approach is used to provide consistency
  1894. * (i.e. list consistency) in the face of updates to multiple
  1895. * buffers. under this approach, all required buffers are
  1896. * obtained before making any updates and are held until all
  1897. * updates are complete.
  1898. *
  1899. * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
  1900. * this AG. Must have read lock on imap inode.
  1901. *
  1902. * PARAMETERS:
  1903. * imap - pointer to inode map control structure.
  1904. * iagp - pointer to iag.
  1905. * extno - extent number.
  1906. *
  1907. * RETURN VALUES:
  1908. * 0 - success.
  1909. * -ENOSPC - insufficient disk resources.
  1910. * -EIO - i/o error.
  1911. */
  1912. static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
  1913. {
  1914. int agno, iagno, fwd, back, freei = 0, sword, rc;
  1915. struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL;
  1916. struct metapage *amp, *bmp, *cmp, *dmp;
  1917. struct inode *ipimap;
  1918. s64 blkno, hint;
  1919. int i, j;
  1920. u32 mask;
  1921. ino_t ino;
  1922. struct dinode *dp;
  1923. struct jfs_sb_info *sbi;
  1924. /* better have free extents.
  1925. */
  1926. if (!iagp->nfreeexts) {
  1927. jfs_error(imap->im_ipimap->i_sb, "diNewExt: no free extents");
  1928. return -EIO;
  1929. }
  1930. /* get the inode map inode.
  1931. */
  1932. ipimap = imap->im_ipimap;
  1933. sbi = JFS_SBI(ipimap->i_sb);
  1934. amp = bmp = cmp = NULL;
  1935. /* get the ag and iag numbers for this iag.
  1936. */
  1937. agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
  1938. iagno = le32_to_cpu(iagp->iagnum);
  1939. /* check if this is the last free extent within the
  1940. * iag. if so, the iag must be removed from the ag
  1941. * free extent list, so get the iags preceeding and
  1942. * following the iag on this list.
  1943. */
  1944. if (iagp->nfreeexts == cpu_to_le32(1)) {
  1945. if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
  1946. if ((rc = diIAGRead(imap, fwd, &amp)))
  1947. return (rc);
  1948. aiagp = (struct iag *) amp->data;
  1949. }
  1950. if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
  1951. if ((rc = diIAGRead(imap, back, &bmp)))
  1952. goto error_out;
  1953. biagp = (struct iag *) bmp->data;
  1954. }
  1955. } else {
  1956. /* the iag has free extents. if all extents are free
  1957. * (as is the case for a newly allocated iag), the iag
  1958. * must be added to the ag free extent list, so get
  1959. * the iag at the head of the list in preparation for
  1960. * adding this iag to this list.
  1961. */
  1962. fwd = back = -1;
  1963. if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
  1964. if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
  1965. if ((rc = diIAGRead(imap, fwd, &amp)))
  1966. goto error_out;
  1967. aiagp = (struct iag *) amp->data;
  1968. }
  1969. }
  1970. }
  1971. /* check if the iag has no free inodes. if so, the iag
  1972. * will have to be added to the ag free inode list, so get
  1973. * the iag at the head of the list in preparation for
  1974. * adding this iag to this list. in doing this, we must
  1975. * check if we already have the iag at the head of
  1976. * the list in hand.
  1977. */
  1978. if (iagp->nfreeinos == 0) {
  1979. freei = imap->im_agctl[agno].inofree;
  1980. if (freei >= 0) {
  1981. if (freei == fwd) {
  1982. ciagp = aiagp;
  1983. } else if (freei == back) {
  1984. ciagp = biagp;
  1985. } else {
  1986. if ((rc = diIAGRead(imap, freei, &cmp)))
  1987. goto error_out;
  1988. ciagp = (struct iag *) cmp->data;
  1989. }
  1990. if (ciagp == NULL) {
  1991. jfs_error(imap->im_ipimap->i_sb,
  1992. "diNewExt: ciagp == NULL");
  1993. rc = -EIO;
  1994. goto error_out;
  1995. }
  1996. }
  1997. }
  1998. /* allocate disk space for the inode extent.
  1999. */
  2000. if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
  2001. hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
  2002. else
  2003. hint = addressPXD(&iagp->inoext[extno - 1]) +
  2004. lengthPXD(&iagp->inoext[extno - 1]) - 1;
  2005. if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
  2006. goto error_out;
  2007. /* compute the inode number of the first inode within the
  2008. * extent.
  2009. */
  2010. ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
  2011. /* initialize the inodes within the newly allocated extent a
  2012. * page at a time.
  2013. */
  2014. for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
  2015. /* get a buffer for this page of disk inodes.
  2016. */
  2017. dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
  2018. if (dmp == NULL) {
  2019. rc = -EIO;
  2020. goto error_out;
  2021. }
  2022. dp = (struct dinode *) dmp->data;
  2023. /* initialize the inode number, mode, link count and
  2024. * inode extent address.
  2025. */
  2026. for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
  2027. dp->di_inostamp = cpu_to_le32(sbi->inostamp);
  2028. dp->di_number = cpu_to_le32(ino);
  2029. dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
  2030. dp->di_mode = 0;
  2031. dp->di_nlink = 0;
  2032. PXDaddress(&(dp->di_ixpxd), blkno);
  2033. PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
  2034. }
  2035. write_metapage(dmp);
  2036. }
  2037. /* if this is the last free extent within the iag, remove the
  2038. * iag from the ag free extent list.
  2039. */
  2040. if (iagp->nfreeexts == cpu_to_le32(1)) {
  2041. if (fwd >= 0)
  2042. aiagp->extfreeback = iagp->extfreeback;
  2043. if (back >= 0)
  2044. biagp->extfreefwd = iagp->extfreefwd;
  2045. else
  2046. imap->im_agctl[agno].extfree =
  2047. le32_to_cpu(iagp->extfreefwd);
  2048. iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
  2049. } else {
  2050. /* if the iag has all free extents (newly allocated iag),
  2051. * add the iag to the ag free extent list.
  2052. */
  2053. if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
  2054. if (fwd >= 0)
  2055. aiagp->extfreeback = cpu_to_le32(iagno);
  2056. iagp->extfreefwd = cpu_to_le32(fwd);
  2057. iagp->extfreeback = cpu_to_le32(-1);
  2058. imap->im_agctl[agno].extfree = iagno;
  2059. }
  2060. }
  2061. /* if the iag has no free inodes, add the iag to the
  2062. * ag free inode list.
  2063. */
  2064. if (iagp->nfreeinos == 0) {
  2065. if (freei >= 0)
  2066. ciagp->inofreeback = cpu_to_le32(iagno);
  2067. iagp->inofreefwd =
  2068. cpu_to_le32(imap->im_agctl[agno].inofree);
  2069. iagp->inofreeback = cpu_to_le32(-1);
  2070. imap->im_agctl[agno].inofree = iagno;
  2071. }
  2072. /* initialize the extent descriptor of the extent. */
  2073. PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
  2074. PXDaddress(&iagp->inoext[extno], blkno);
  2075. /* initialize the working and persistent map of the extent.
  2076. * the working map will be initialized such that
  2077. * it indicates the first inode of the extent is allocated.
  2078. */
  2079. iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
  2080. iagp->pmap[extno] = 0;
  2081. /* update the free inode and free extent summary maps
  2082. * for the extent to indicate the extent has free inodes
  2083. * and no longer represents a free extent.
  2084. */
  2085. sword = extno >> L2EXTSPERSUM;
  2086. mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
  2087. iagp->extsmap[sword] |= cpu_to_le32(mask);
  2088. iagp->inosmap[sword] &= cpu_to_le32(~mask);
  2089. /* update the free inode and free extent counts for the
  2090. * iag.
  2091. */
  2092. le32_add_cpu(&iagp->nfreeinos, (INOSPEREXT - 1));
  2093. le32_add_cpu(&iagp->nfreeexts, -1);
  2094. /* update the free and backed inode counts for the ag.
  2095. */
  2096. imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
  2097. imap->im_agctl[agno].numinos += INOSPEREXT;
  2098. /* update the free and backed inode counts for the inode map.
  2099. */
  2100. atomic_add(INOSPEREXT - 1, &imap->im_numfree);
  2101. atomic_add(INOSPEREXT, &imap->im_numinos);
  2102. /* write the iags.
  2103. */
  2104. if (amp)
  2105. write_metapage(amp);
  2106. if (bmp)
  2107. write_metapage(bmp);
  2108. if (cmp)
  2109. write_metapage(cmp);
  2110. return (0);
  2111. error_out:
  2112. /* release the iags.
  2113. */
  2114. if (amp)
  2115. release_metapage(amp);
  2116. if (bmp)
  2117. release_metapage(bmp);
  2118. if (cmp)
  2119. release_metapage(cmp);
  2120. return (rc);
  2121. }
  2122. /*
  2123. * NAME: diNewIAG(imap,iagnop,agno)
  2124. *
  2125. * FUNCTION: allocate a new iag for an allocation group.
  2126. *
  2127. * first tries to allocate the iag from the inode map
  2128. * iagfree list:
  2129. * if the list has free iags, the head of the list is removed
  2130. * and returned to satisfy the request.
  2131. * if the inode map's iag free list is empty, the inode map
  2132. * is extended to hold a new iag. this new iag is initialized
  2133. * and returned to satisfy the request.
  2134. *
  2135. * PARAMETERS:
  2136. * imap - pointer to inode map control structure.
  2137. * iagnop - pointer to an iag number set with the number of the
  2138. * newly allocated iag upon successful return.
  2139. * agno - allocation group number.
  2140. * bpp - Buffer pointer to be filled in with new IAG's buffer
  2141. *
  2142. * RETURN VALUES:
  2143. * 0 - success.
  2144. * -ENOSPC - insufficient disk resources.
  2145. * -EIO - i/o error.
  2146. *
  2147. * serialization:
  2148. * AG lock held on entry/exit;
  2149. * write lock on the map is held inside;
  2150. * read lock on the map is held on successful completion;
  2151. *
  2152. * note: new iag transaction:
  2153. * . synchronously write iag;
  2154. * . write log of xtree and inode of imap;
  2155. * . commit;
  2156. * . synchronous write of xtree (right to left, bottom to top);
  2157. * . at start of logredo(): init in-memory imap with one additional iag page;
  2158. * . at end of logredo(): re-read imap inode to determine
  2159. * new imap size;
  2160. */
  2161. static int
  2162. diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
  2163. {
  2164. int rc;
  2165. int iagno, i, xlen;
  2166. struct inode *ipimap;
  2167. struct super_block *sb;
  2168. struct jfs_sb_info *sbi;
  2169. struct metapage *mp;
  2170. struct iag *iagp;
  2171. s64 xaddr = 0;
  2172. s64 blkno;
  2173. tid_t tid;
  2174. struct inode *iplist[1];
  2175. /* pick up pointers to the inode map and mount inodes */
  2176. ipimap = imap->im_ipimap;
  2177. sb = ipimap->i_sb;
  2178. sbi = JFS_SBI(sb);
  2179. /* acquire the free iag lock */
  2180. IAGFREE_LOCK(imap);
  2181. /* if there are any iags on the inode map free iag list,
  2182. * allocate the iag from the head of the list.
  2183. */
  2184. if (imap->im_freeiag >= 0) {
  2185. /* pick up the iag number at the head of the list */
  2186. iagno = imap->im_freeiag;
  2187. /* determine the logical block number of the iag */
  2188. blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
  2189. } else {
  2190. /* no free iags. the inode map will have to be extented
  2191. * to include a new iag.
  2192. */
  2193. /* acquire inode map lock */
  2194. IWRITE_LOCK(ipimap, RDWRLOCK_IMAP);
  2195. if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
  2196. IWRITE_UNLOCK(ipimap);
  2197. IAGFREE_UNLOCK(imap);
  2198. jfs_error(imap->im_ipimap->i_sb,
  2199. "diNewIAG: ipimap->i_size is wrong");
  2200. return -EIO;
  2201. }
  2202. /* get the next avaliable iag number */
  2203. iagno = imap->im_nextiag;
  2204. /* make sure that we have not exceeded the maximum inode
  2205. * number limit.
  2206. */
  2207. if (iagno > (MAXIAGS - 1)) {
  2208. /* release the inode map lock */
  2209. IWRITE_UNLOCK(ipimap);
  2210. rc = -ENOSPC;
  2211. goto out;
  2212. }
  2213. /*
  2214. * synchronously append new iag page.
  2215. */
  2216. /* determine the logical address of iag page to append */
  2217. blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
  2218. /* Allocate extent for new iag page */
  2219. xlen = sbi->nbperpage;
  2220. if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
  2221. /* release the inode map lock */
  2222. IWRITE_UNLOCK(ipimap);
  2223. goto out;
  2224. }
  2225. /*
  2226. * start transaction of update of the inode map
  2227. * addressing structure pointing to the new iag page;
  2228. */
  2229. tid = txBegin(sb, COMMIT_FORCE);
  2230. mutex_lock(&JFS_IP(ipimap)->commit_mutex);
  2231. /* update the inode map addressing structure to point to it */
  2232. if ((rc =
  2233. xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
  2234. txEnd(tid);
  2235. mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
  2236. /* Free the blocks allocated for the iag since it was
  2237. * not successfully added to the inode map
  2238. */
  2239. dbFree(ipimap, xaddr, (s64) xlen);
  2240. /* release the inode map lock */
  2241. IWRITE_UNLOCK(ipimap);
  2242. goto out;
  2243. }
  2244. /* update the inode map's inode to reflect the extension */
  2245. ipimap->i_size += PSIZE;
  2246. inode_add_bytes(ipimap, PSIZE);
  2247. /* assign a buffer for the page */
  2248. mp = get_metapage(ipimap, blkno, PSIZE, 0);
  2249. if (!mp) {
  2250. /*
  2251. * This is very unlikely since we just created the
  2252. * extent, but let's try to handle it correctly
  2253. */
  2254. xtTruncate(tid, ipimap, ipimap->i_size - PSIZE,
  2255. COMMIT_PWMAP);
  2256. txAbort(tid, 0);
  2257. txEnd(tid);
  2258. mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
  2259. /* release the inode map lock */
  2260. IWRITE_UNLOCK(ipimap);
  2261. rc = -EIO;
  2262. goto out;
  2263. }
  2264. iagp = (struct iag *) mp->data;
  2265. /* init the iag */
  2266. memset(iagp, 0, sizeof(struct iag));
  2267. iagp->iagnum = cpu_to_le32(iagno);
  2268. iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
  2269. iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
  2270. iagp->iagfree = cpu_to_le32(-1);
  2271. iagp->nfreeinos = 0;
  2272. iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
  2273. /* initialize the free inode summary map (free extent
  2274. * summary map initialization handled by bzero).
  2275. */
  2276. for (i = 0; i < SMAPSZ; i++)
  2277. iagp->inosmap[i] = cpu_to_le32(ONES);
  2278. /*
  2279. * Write and sync the metapage
  2280. */
  2281. flush_metapage(mp);
  2282. /*
  2283. * txCommit(COMMIT_FORCE) will synchronously write address
  2284. * index pages and inode after commit in careful update order
  2285. * of address index pages (right to left, bottom up);
  2286. */
  2287. iplist[0] = ipimap;
  2288. rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
  2289. txEnd(tid);
  2290. mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
  2291. duplicateIXtree(sb, blkno, xlen, &xaddr);
  2292. /* update the next avaliable iag number */
  2293. imap->im_nextiag += 1;
  2294. /* Add the iag to the iag free list so we don't lose the iag
  2295. * if a failure happens now.
  2296. */
  2297. imap->im_freeiag = iagno;
  2298. /* Until we have logredo working, we want the imap inode &
  2299. * control page to be up to date.
  2300. */
  2301. diSync(ipimap);
  2302. /* release the inode map lock */
  2303. IWRITE_UNLOCK(ipimap);
  2304. }
  2305. /* obtain read lock on map */
  2306. IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
  2307. /* read the iag */
  2308. if ((rc = diIAGRead(imap, iagno, &mp))) {
  2309. IREAD_UNLOCK(ipimap);
  2310. rc = -EIO;
  2311. goto out;
  2312. }
  2313. iagp = (struct iag *) mp->data;
  2314. /* remove the iag from the iag free list */
  2315. imap->im_freeiag = le32_to_cpu(iagp->iagfree);
  2316. iagp->iagfree = cpu_to_le32(-1);
  2317. /* set the return iag number and buffer pointer */
  2318. *iagnop = iagno;
  2319. *mpp = mp;
  2320. out:
  2321. /* release the iag free lock */
  2322. IAGFREE_UNLOCK(imap);
  2323. return (rc);
  2324. }
  2325. /*
  2326. * NAME: diIAGRead()
  2327. *
  2328. * FUNCTION: get the buffer for the specified iag within a fileset
  2329. * or aggregate inode map.
  2330. *
  2331. * PARAMETERS:
  2332. * imap - pointer to inode map control structure.
  2333. * iagno - iag number.
  2334. * bpp - point to buffer pointer to be filled in on successful
  2335. * exit.
  2336. *
  2337. * SERIALIZATION:
  2338. * must have read lock on imap inode
  2339. * (When called by diExtendFS, the filesystem is quiesced, therefore
  2340. * the read lock is unnecessary.)
  2341. *
  2342. * RETURN VALUES:
  2343. * 0 - success.
  2344. * -EIO - i/o error.
  2345. */
  2346. static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
  2347. {
  2348. struct inode *ipimap = imap->im_ipimap;
  2349. s64 blkno;
  2350. /* compute the logical block number of the iag. */
  2351. blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
  2352. /* read the iag. */
  2353. *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
  2354. if (*mpp == NULL) {
  2355. return -EIO;
  2356. }
  2357. return (0);
  2358. }
  2359. /*
  2360. * NAME: diFindFree()
  2361. *
  2362. * FUNCTION: find the first free bit in a word starting at
  2363. * the specified bit position.
  2364. *
  2365. * PARAMETERS:
  2366. * word - word to be examined.
  2367. * start - starting bit position.
  2368. *
  2369. * RETURN VALUES:
  2370. * bit position of first free bit in the word or 32 if
  2371. * no free bits were found.
  2372. */
  2373. static int diFindFree(u32 word, int start)
  2374. {
  2375. int bitno;
  2376. assert(start < 32);
  2377. /* scan the word for the first free bit. */
  2378. for (word <<= start, bitno = start; bitno < 32;
  2379. bitno++, word <<= 1) {
  2380. if ((word & HIGHORDER) == 0)
  2381. break;
  2382. }
  2383. return (bitno);
  2384. }
  2385. /*
  2386. * NAME: diUpdatePMap()
  2387. *
  2388. * FUNCTION: Update the persistent map in an IAG for the allocation or
  2389. * freeing of the specified inode.
  2390. *
  2391. * PRE CONDITIONS: Working map has already been updated for allocate.
  2392. *
  2393. * PARAMETERS:
  2394. * ipimap - Incore inode map inode
  2395. * inum - Number of inode to mark in permanent map
  2396. * is_free - If 'true' indicates inode should be marked freed, otherwise
  2397. * indicates inode should be marked allocated.
  2398. *
  2399. * RETURN VALUES:
  2400. * 0 for success
  2401. */
  2402. int
  2403. diUpdatePMap(struct inode *ipimap,
  2404. unsigned long inum, bool is_free, struct tblock * tblk)
  2405. {
  2406. int rc;
  2407. struct iag *iagp;
  2408. struct metapage *mp;
  2409. int iagno, ino, extno, bitno;
  2410. struct inomap *imap;
  2411. u32 mask;
  2412. struct jfs_log *log;
  2413. int lsn, difft, diffp;
  2414. unsigned long flags;
  2415. imap = JFS_IP(ipimap)->i_imap;
  2416. /* get the iag number containing the inode */
  2417. iagno = INOTOIAG(inum);
  2418. /* make sure that the iag is contained within the map */
  2419. if (iagno >= imap->im_nextiag) {
  2420. jfs_error(ipimap->i_sb,
  2421. "diUpdatePMap: the iag is outside the map");
  2422. return -EIO;
  2423. }
  2424. /* read the iag */
  2425. IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
  2426. rc = diIAGRead(imap, iagno, &mp);
  2427. IREAD_UNLOCK(ipimap);
  2428. if (rc)
  2429. return (rc);
  2430. metapage_wait_for_io(mp);
  2431. iagp = (struct iag *) mp->data;
  2432. /* get the inode number and extent number of the inode within
  2433. * the iag and the inode number within the extent.
  2434. */
  2435. ino = inum & (INOSPERIAG - 1);
  2436. extno = ino >> L2INOSPEREXT;
  2437. bitno = ino & (INOSPEREXT - 1);
  2438. mask = HIGHORDER >> bitno;
  2439. /*
  2440. * mark the inode free in persistent map:
  2441. */
  2442. if (is_free) {
  2443. /* The inode should have been allocated both in working
  2444. * map and in persistent map;
  2445. * the inode will be freed from working map at the release
  2446. * of last reference release;
  2447. */
  2448. if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
  2449. jfs_error(ipimap->i_sb,
  2450. "diUpdatePMap: inode %ld not marked as "
  2451. "allocated in wmap!", inum);
  2452. }
  2453. if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
  2454. jfs_error(ipimap->i_sb,
  2455. "diUpdatePMap: inode %ld not marked as "
  2456. "allocated in pmap!", inum);
  2457. }
  2458. /* update the bitmap for the extent of the freed inode */
  2459. iagp->pmap[extno] &= cpu_to_le32(~mask);
  2460. }
  2461. /*
  2462. * mark the inode allocated in persistent map:
  2463. */
  2464. else {
  2465. /* The inode should be already allocated in the working map
  2466. * and should be free in persistent map;
  2467. */
  2468. if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
  2469. release_metapage(mp);
  2470. jfs_error(ipimap->i_sb,
  2471. "diUpdatePMap: the inode is not allocated in "
  2472. "the working map");
  2473. return -EIO;
  2474. }
  2475. if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
  2476. release_metapage(mp);
  2477. jfs_error(ipimap->i_sb,
  2478. "diUpdatePMap: the inode is not free in the "
  2479. "persistent map");
  2480. return -EIO;
  2481. }
  2482. /* update the bitmap for the extent of the allocated inode */
  2483. iagp->pmap[extno] |= cpu_to_le32(mask);
  2484. }
  2485. /*
  2486. * update iag lsn
  2487. */
  2488. lsn = tblk->lsn;
  2489. log = JFS_SBI(tblk->sb)->log;
  2490. LOGSYNC_LOCK(log, flags);
  2491. if (mp->lsn != 0) {
  2492. /* inherit older/smaller lsn */
  2493. logdiff(difft, lsn, log);
  2494. logdiff(diffp, mp->lsn, log);
  2495. if (difft < diffp) {
  2496. mp->lsn = lsn;
  2497. /* move mp after tblock in logsync list */
  2498. list_move(&mp->synclist, &tblk->synclist);
  2499. }
  2500. /* inherit younger/larger clsn */
  2501. assert(mp->clsn);
  2502. logdiff(difft, tblk->clsn, log);
  2503. logdiff(diffp, mp->clsn, log);
  2504. if (difft > diffp)
  2505. mp->clsn = tblk->clsn;
  2506. } else {
  2507. mp->log = log;
  2508. mp->lsn = lsn;
  2509. /* insert mp after tblock in logsync list */
  2510. log->count++;
  2511. list_add(&mp->synclist, &tblk->synclist);
  2512. mp->clsn = tblk->clsn;
  2513. }
  2514. LOGSYNC_UNLOCK(log, flags);
  2515. write_metapage(mp);
  2516. return (0);
  2517. }
  2518. /*
  2519. * diExtendFS()
  2520. *
  2521. * function: update imap for extendfs();
  2522. *
  2523. * note: AG size has been increased s.t. each k old contiguous AGs are
  2524. * coalesced into a new AG;
  2525. */
  2526. int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
  2527. {
  2528. int rc, rcx = 0;
  2529. struct inomap *imap = JFS_IP(ipimap)->i_imap;
  2530. struct iag *iagp = NULL, *hiagp = NULL;
  2531. struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
  2532. struct metapage *bp, *hbp;
  2533. int i, n, head;
  2534. int numinos, xnuminos = 0, xnumfree = 0;
  2535. s64 agstart;
  2536. jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
  2537. imap->im_nextiag, atomic_read(&imap->im_numinos),
  2538. atomic_read(&imap->im_numfree));
  2539. /*
  2540. * reconstruct imap
  2541. *
  2542. * coalesce contiguous k (newAGSize/oldAGSize) AGs;
  2543. * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
  2544. * note: new AG size = old AG size * (2**x).
  2545. */
  2546. /* init per AG control information im_agctl[] */
  2547. for (i = 0; i < MAXAG; i++) {
  2548. imap->im_agctl[i].inofree = -1;
  2549. imap->im_agctl[i].extfree = -1;
  2550. imap->im_agctl[i].numinos = 0; /* number of backed inodes */
  2551. imap->im_agctl[i].numfree = 0; /* number of free backed inodes */
  2552. }
  2553. /*
  2554. * process each iag page of the map.
  2555. *
  2556. * rebuild AG Free Inode List, AG Free Inode Extent List;
  2557. */
  2558. for (i = 0; i < imap->im_nextiag; i++) {
  2559. if ((rc = diIAGRead(imap, i, &bp))) {
  2560. rcx = rc;
  2561. continue;
  2562. }
  2563. iagp = (struct iag *) bp->data;
  2564. if (le32_to_cpu(iagp->iagnum) != i) {
  2565. release_metapage(bp);
  2566. jfs_error(ipimap->i_sb,
  2567. "diExtendFs: unexpected value of iagnum");
  2568. return -EIO;
  2569. }
  2570. /* leave free iag in the free iag list */
  2571. if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
  2572. release_metapage(bp);
  2573. continue;
  2574. }
  2575. /* agstart that computes to the same ag is treated as same; */
  2576. agstart = le64_to_cpu(iagp->agstart);
  2577. /* iagp->agstart = agstart & ~(mp->db_agsize - 1); */
  2578. n = agstart >> mp->db_agl2size;
  2579. /* compute backed inodes */
  2580. numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
  2581. << L2INOSPEREXT;
  2582. if (numinos > 0) {
  2583. /* merge AG backed inodes */
  2584. imap->im_agctl[n].numinos += numinos;
  2585. xnuminos += numinos;
  2586. }
  2587. /* if any backed free inodes, insert at AG free inode list */
  2588. if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
  2589. if ((head = imap->im_agctl[n].inofree) == -1) {
  2590. iagp->inofreefwd = cpu_to_le32(-1);
  2591. iagp->inofreeback = cpu_to_le32(-1);
  2592. } else {
  2593. if ((rc = diIAGRead(imap, head, &hbp))) {
  2594. rcx = rc;
  2595. goto nextiag;
  2596. }
  2597. hiagp = (struct iag *) hbp->data;
  2598. hiagp->inofreeback = iagp->iagnum;
  2599. iagp->inofreefwd = cpu_to_le32(head);
  2600. iagp->inofreeback = cpu_to_le32(-1);
  2601. write_metapage(hbp);
  2602. }
  2603. imap->im_agctl[n].inofree =
  2604. le32_to_cpu(iagp->iagnum);
  2605. /* merge AG backed free inodes */
  2606. imap->im_agctl[n].numfree +=
  2607. le32_to_cpu(iagp->nfreeinos);
  2608. xnumfree += le32_to_cpu(iagp->nfreeinos);
  2609. }
  2610. /* if any free extents, insert at AG free extent list */
  2611. if (le32_to_cpu(iagp->nfreeexts) > 0) {
  2612. if ((head = imap->im_agctl[n].extfree) == -1) {
  2613. iagp->extfreefwd = cpu_to_le32(-1);
  2614. iagp->extfreeback = cpu_to_le32(-1);
  2615. } else {
  2616. if ((rc = diIAGRead(imap, head, &hbp))) {
  2617. rcx = rc;
  2618. goto nextiag;
  2619. }
  2620. hiagp = (struct iag *) hbp->data;
  2621. hiagp->extfreeback = iagp->iagnum;
  2622. iagp->extfreefwd = cpu_to_le32(head);
  2623. iagp->extfreeback = cpu_to_le32(-1);
  2624. write_metapage(hbp);
  2625. }
  2626. imap->im_agctl[n].extfree =
  2627. le32_to_cpu(iagp->iagnum);
  2628. }
  2629. nextiag:
  2630. write_metapage(bp);
  2631. }
  2632. if (xnuminos != atomic_read(&imap->im_numinos) ||
  2633. xnumfree != atomic_read(&imap->im_numfree)) {
  2634. jfs_error(ipimap->i_sb,
  2635. "diExtendFs: numinos or numfree incorrect");
  2636. return -EIO;
  2637. }
  2638. return rcx;
  2639. }
  2640. /*
  2641. * duplicateIXtree()
  2642. *
  2643. * serialization: IWRITE_LOCK held on entry/exit
  2644. *
  2645. * note: shadow page with regular inode (rel.2);
  2646. */
  2647. static void duplicateIXtree(struct super_block *sb, s64 blkno,
  2648. int xlen, s64 *xaddr)
  2649. {
  2650. struct jfs_superblock *j_sb;
  2651. struct buffer_head *bh;
  2652. struct inode *ip;
  2653. tid_t tid;
  2654. /* if AIT2 ipmap2 is bad, do not try to update it */
  2655. if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */
  2656. return;
  2657. ip = diReadSpecial(sb, FILESYSTEM_I, 1);
  2658. if (ip == NULL) {
  2659. JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
  2660. if (readSuper(sb, &bh))
  2661. return;
  2662. j_sb = (struct jfs_superblock *)bh->b_data;
  2663. j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
  2664. mark_buffer_dirty(bh);
  2665. sync_dirty_buffer(bh);
  2666. brelse(bh);
  2667. return;
  2668. }
  2669. /* start transaction */
  2670. tid = txBegin(sb, COMMIT_FORCE);
  2671. /* update the inode map addressing structure to point to it */
  2672. if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
  2673. JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
  2674. txAbort(tid, 1);
  2675. goto cleanup;
  2676. }
  2677. /* update the inode map's inode to reflect the extension */
  2678. ip->i_size += PSIZE;
  2679. inode_add_bytes(ip, PSIZE);
  2680. txCommit(tid, 1, &ip, COMMIT_FORCE);
  2681. cleanup:
  2682. txEnd(tid);
  2683. diFreeSpecial(ip);
  2684. }
  2685. /*
  2686. * NAME: copy_from_dinode()
  2687. *
  2688. * FUNCTION: Copies inode info from disk inode to in-memory inode
  2689. *
  2690. * RETURN VALUES:
  2691. * 0 - success
  2692. * -ENOMEM - insufficient memory
  2693. */
  2694. static int copy_from_dinode(struct dinode * dip, struct inode *ip)
  2695. {
  2696. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  2697. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  2698. jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
  2699. jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
  2700. jfs_set_inode_flags(ip);
  2701. ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
  2702. if (sbi->umask != -1) {
  2703. ip->i_mode = (ip->i_mode & ~0777) | (0777 & ~sbi->umask);
  2704. /* For directories, add x permission if r is allowed by umask */
  2705. if (S_ISDIR(ip->i_mode)) {
  2706. if (ip->i_mode & 0400)
  2707. ip->i_mode |= 0100;
  2708. if (ip->i_mode & 0040)
  2709. ip->i_mode |= 0010;
  2710. if (ip->i_mode & 0004)
  2711. ip->i_mode |= 0001;
  2712. }
  2713. }
  2714. ip->i_nlink = le32_to_cpu(dip->di_nlink);
  2715. jfs_ip->saved_uid = le32_to_cpu(dip->di_uid);
  2716. if (sbi->uid == -1)
  2717. ip->i_uid = jfs_ip->saved_uid;
  2718. else {
  2719. ip->i_uid = sbi->uid;
  2720. }
  2721. jfs_ip->saved_gid = le32_to_cpu(dip->di_gid);
  2722. if (sbi->gid == -1)
  2723. ip->i_gid = jfs_ip->saved_gid;
  2724. else {
  2725. ip->i_gid = sbi->gid;
  2726. }
  2727. ip->i_size = le64_to_cpu(dip->di_size);
  2728. ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
  2729. ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
  2730. ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
  2731. ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
  2732. ip->i_ctime.tv_sec = le32_to_cpu(dip->di_ctime.tv_sec);
  2733. ip->i_ctime.tv_nsec = le32_to_cpu(dip->di_ctime.tv_nsec);
  2734. ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
  2735. ip->i_generation = le32_to_cpu(dip->di_gen);
  2736. jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */
  2737. jfs_ip->acl = dip->di_acl; /* as are dxd's */
  2738. jfs_ip->ea = dip->di_ea;
  2739. jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
  2740. jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
  2741. jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
  2742. if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
  2743. jfs_ip->dev = le32_to_cpu(dip->di_rdev);
  2744. ip->i_rdev = new_decode_dev(jfs_ip->dev);
  2745. }
  2746. if (S_ISDIR(ip->i_mode)) {
  2747. memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
  2748. } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
  2749. memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
  2750. } else
  2751. memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
  2752. /* Zero the in-memory-only stuff */
  2753. jfs_ip->cflag = 0;
  2754. jfs_ip->btindex = 0;
  2755. jfs_ip->btorder = 0;
  2756. jfs_ip->bxflag = 0;
  2757. jfs_ip->blid = 0;
  2758. jfs_ip->atlhead = 0;
  2759. jfs_ip->atltail = 0;
  2760. jfs_ip->xtlid = 0;
  2761. return (0);
  2762. }
  2763. /*
  2764. * NAME: copy_to_dinode()
  2765. *
  2766. * FUNCTION: Copies inode info from in-memory inode to disk inode
  2767. */
  2768. static void copy_to_dinode(struct dinode * dip, struct inode *ip)
  2769. {
  2770. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  2771. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  2772. dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
  2773. dip->di_inostamp = cpu_to_le32(sbi->inostamp);
  2774. dip->di_number = cpu_to_le32(ip->i_ino);
  2775. dip->di_gen = cpu_to_le32(ip->i_generation);
  2776. dip->di_size = cpu_to_le64(ip->i_size);
  2777. dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
  2778. dip->di_nlink = cpu_to_le32(ip->i_nlink);
  2779. if (sbi->uid == -1)
  2780. dip->di_uid = cpu_to_le32(ip->i_uid);
  2781. else
  2782. dip->di_uid = cpu_to_le32(jfs_ip->saved_uid);
  2783. if (sbi->gid == -1)
  2784. dip->di_gid = cpu_to_le32(ip->i_gid);
  2785. else
  2786. dip->di_gid = cpu_to_le32(jfs_ip->saved_gid);
  2787. jfs_get_inode_flags(jfs_ip);
  2788. /*
  2789. * mode2 is only needed for storing the higher order bits.
  2790. * Trust i_mode for the lower order ones
  2791. */
  2792. if (sbi->umask == -1)
  2793. dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) |
  2794. ip->i_mode);
  2795. else /* Leave the original permissions alone */
  2796. dip->di_mode = cpu_to_le32(jfs_ip->mode2);
  2797. dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
  2798. dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
  2799. dip->di_ctime.tv_sec = cpu_to_le32(ip->i_ctime.tv_sec);
  2800. dip->di_ctime.tv_nsec = cpu_to_le32(ip->i_ctime.tv_nsec);
  2801. dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
  2802. dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
  2803. dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */
  2804. dip->di_acl = jfs_ip->acl; /* as are dxd's */
  2805. dip->di_ea = jfs_ip->ea;
  2806. dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
  2807. dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
  2808. dip->di_otime.tv_nsec = 0;
  2809. dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
  2810. if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
  2811. dip->di_rdev = cpu_to_le32(jfs_ip->dev);
  2812. }