jfs_imap.c 83 KB

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