jfs_imap.c 84 KB

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