jfs_imap.c 84 KB

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