jfs_imap.c 85 KB

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