jfs_imap.c 84 KB

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