jfs_imap.c 85 KB

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