jfs_imap.c 84 KB

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