jfs_imap.c 84 KB

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