jfs_imap.c 88 KB

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