jfs_imap.c 83 KB

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