jfs_imap.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202
  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 == 0) {
  336. jfs_err("diRead: read_metapage failed");
  337. return -EIO;
  338. }
  339. /* locate the 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 == 0)
  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. // baseDinode:
  697. lv = & dilinelock->lv[dilinelock->index];
  698. lv->offset = dioffset >> L2INODESLOTSIZE;
  699. copy_to_dinode(dp, ip);
  700. if (test_and_clear_cflag(COMMIT_Dirtable, ip)) {
  701. lv->length = 2;
  702. memcpy(&dp->di_dirtable, &jfs_ip->i_dirtable, 96);
  703. } else
  704. lv->length = 1;
  705. dilinelock->index++;
  706. #ifdef _JFS_FASTDASD
  707. /*
  708. * We aren't logging changes to the DASD used in directory inodes,
  709. * but we need to write them to disk. If we don't unmount cleanly,
  710. * mount will recalculate the DASD used.
  711. */
  712. if (S_ISDIR(ip->i_mode)
  713. && (ip->i_ipmnt->i_mntflag & JFS_DASD_ENABLED))
  714. memcpy(&dp->di_DASD, &ip->i_DASD, sizeof(struct dasd));
  715. #endif /* _JFS_FASTDASD */
  716. /* release the buffer holding the updated on-disk inode.
  717. * the buffer will be later written by commit processing.
  718. */
  719. write_metapage(mp);
  720. return (rc);
  721. }
  722. /*
  723. * NAME: diFree(ip)
  724. *
  725. * FUNCTION: free a specified inode from the inode working map
  726. * for a fileset or aggregate.
  727. *
  728. * if the inode to be freed represents the first (only)
  729. * free inode within the iag, the iag will be placed on
  730. * the ag free inode list.
  731. *
  732. * freeing the inode will cause the inode extent to be
  733. * freed if the inode is the only allocated inode within
  734. * the extent. in this case all the disk resource backing
  735. * up the inode extent will be freed. in addition, the iag
  736. * will be placed on the ag extent free list if the extent
  737. * is the first free extent in the iag. if freeing the
  738. * extent also means that no free inodes will exist for
  739. * the iag, the iag will also be removed from the ag free
  740. * inode list.
  741. *
  742. * the iag describing the inode will be freed if the extent
  743. * is to be freed and it is the only backed extent within
  744. * the iag. in this case, the iag will be removed from the
  745. * ag free extent list and ag free inode list and placed on
  746. * the inode map's free iag list.
  747. *
  748. * a careful update approach is used to provide consistency
  749. * in the face of updates to multiple buffers. under this
  750. * approach, all required buffers are obtained before making
  751. * any updates and are held until all updates are complete.
  752. *
  753. * PARAMETERS:
  754. * ip - inode to be freed.
  755. *
  756. * RETURN VALUES:
  757. * 0 - success
  758. * -EIO - i/o error.
  759. */
  760. int diFree(struct inode *ip)
  761. {
  762. int rc;
  763. ino_t inum = ip->i_ino;
  764. struct iag *iagp, *aiagp, *biagp, *ciagp, *diagp;
  765. struct metapage *mp, *amp, *bmp, *cmp, *dmp;
  766. int iagno, ino, extno, bitno, sword, agno;
  767. int back, fwd;
  768. u32 bitmap, mask;
  769. struct inode *ipimap = JFS_SBI(ip->i_sb)->ipimap;
  770. struct inomap *imap = JFS_IP(ipimap)->i_imap;
  771. pxd_t freepxd;
  772. tid_t tid;
  773. struct inode *iplist[3];
  774. struct tlock *tlck;
  775. struct pxd_lock *pxdlock;
  776. /*
  777. * This is just to suppress compiler warnings. The same logic that
  778. * references these variables is used to initialize them.
  779. */
  780. aiagp = biagp = ciagp = diagp = NULL;
  781. /* get the iag number containing the inode.
  782. */
  783. iagno = INOTOIAG(inum);
  784. /* make sure that the iag is contained within
  785. * the map.
  786. */
  787. if (iagno >= imap->im_nextiag) {
  788. dump_mem("imap", imap, 32);
  789. jfs_error(ip->i_sb,
  790. "diFree: inum = %d, iagno = %d, nextiag = %d",
  791. (uint) inum, iagno, imap->im_nextiag);
  792. return -EIO;
  793. }
  794. /* get the allocation group for this ino.
  795. */
  796. agno = JFS_IP(ip)->agno;
  797. /* Lock the AG specific inode map information
  798. */
  799. AG_LOCK(imap, agno);
  800. /* Obtain read lock in imap inode. Don't release it until we have
  801. * read all of the IAG's that we are going to.
  802. */
  803. IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
  804. /* read the iag.
  805. */
  806. if ((rc = diIAGRead(imap, iagno, &mp))) {
  807. IREAD_UNLOCK(ipimap);
  808. AG_UNLOCK(imap, agno);
  809. return (rc);
  810. }
  811. iagp = (struct iag *) mp->data;
  812. /* get the inode number and extent number of the inode within
  813. * the iag and the inode number within the extent.
  814. */
  815. ino = inum & (INOSPERIAG - 1);
  816. extno = ino >> L2INOSPEREXT;
  817. bitno = ino & (INOSPEREXT - 1);
  818. mask = HIGHORDER >> bitno;
  819. if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
  820. jfs_error(ip->i_sb,
  821. "diFree: wmap shows inode already free");
  822. }
  823. if (!addressPXD(&iagp->inoext[extno])) {
  824. release_metapage(mp);
  825. IREAD_UNLOCK(ipimap);
  826. AG_UNLOCK(imap, agno);
  827. jfs_error(ip->i_sb, "diFree: invalid inoext");
  828. return -EIO;
  829. }
  830. /* compute the bitmap for the extent reflecting the freed inode.
  831. */
  832. bitmap = le32_to_cpu(iagp->wmap[extno]) & ~mask;
  833. if (imap->im_agctl[agno].numfree > imap->im_agctl[agno].numinos) {
  834. release_metapage(mp);
  835. IREAD_UNLOCK(ipimap);
  836. AG_UNLOCK(imap, agno);
  837. jfs_error(ip->i_sb, "diFree: numfree > numinos");
  838. return -EIO;
  839. }
  840. /*
  841. * inode extent still has some inodes or below low water mark:
  842. * keep the inode extent;
  843. */
  844. if (bitmap ||
  845. imap->im_agctl[agno].numfree < 96 ||
  846. (imap->im_agctl[agno].numfree < 288 &&
  847. (((imap->im_agctl[agno].numfree * 100) /
  848. imap->im_agctl[agno].numinos) <= 25))) {
  849. /* if the iag currently has no free inodes (i.e.,
  850. * the inode being freed is the first free inode of iag),
  851. * insert the iag at head of the inode free list for the ag.
  852. */
  853. if (iagp->nfreeinos == 0) {
  854. /* check if there are any iags on the ag inode
  855. * free list. if so, read the first one so that
  856. * we can link the current iag onto the list at
  857. * the head.
  858. */
  859. if ((fwd = imap->im_agctl[agno].inofree) >= 0) {
  860. /* read the iag that currently is the head
  861. * of the list.
  862. */
  863. if ((rc = diIAGRead(imap, fwd, &amp))) {
  864. IREAD_UNLOCK(ipimap);
  865. AG_UNLOCK(imap, agno);
  866. release_metapage(mp);
  867. return (rc);
  868. }
  869. aiagp = (struct iag *) amp->data;
  870. /* make current head point back to the iag.
  871. */
  872. aiagp->inofreeback = cpu_to_le32(iagno);
  873. write_metapage(amp);
  874. }
  875. /* iag points forward to current head and iag
  876. * becomes the new head of the list.
  877. */
  878. iagp->inofreefwd =
  879. cpu_to_le32(imap->im_agctl[agno].inofree);
  880. iagp->inofreeback = cpu_to_le32(-1);
  881. imap->im_agctl[agno].inofree = iagno;
  882. }
  883. IREAD_UNLOCK(ipimap);
  884. /* update the free inode summary map for the extent if
  885. * freeing the inode means the extent will now have free
  886. * inodes (i.e., the inode being freed is the first free
  887. * inode of extent),
  888. */
  889. if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
  890. sword = extno >> L2EXTSPERSUM;
  891. bitno = extno & (EXTSPERSUM - 1);
  892. iagp->inosmap[sword] &=
  893. cpu_to_le32(~(HIGHORDER >> bitno));
  894. }
  895. /* update the bitmap.
  896. */
  897. iagp->wmap[extno] = cpu_to_le32(bitmap);
  898. /* update the free inode counts at the iag, ag and
  899. * map level.
  900. */
  901. iagp->nfreeinos =
  902. cpu_to_le32(le32_to_cpu(iagp->nfreeinos) + 1);
  903. imap->im_agctl[agno].numfree += 1;
  904. atomic_inc(&imap->im_numfree);
  905. /* release the AG inode map lock
  906. */
  907. AG_UNLOCK(imap, agno);
  908. /* write the iag */
  909. write_metapage(mp);
  910. return (0);
  911. }
  912. /*
  913. * inode extent has become free and above low water mark:
  914. * free the inode extent;
  915. */
  916. /*
  917. * prepare to update iag list(s) (careful update step 1)
  918. */
  919. amp = bmp = cmp = dmp = NULL;
  920. fwd = back = -1;
  921. /* check if the iag currently has no free extents. if so,
  922. * it will be placed on the head of the ag extent free list.
  923. */
  924. if (iagp->nfreeexts == 0) {
  925. /* check if the ag extent free list has any iags.
  926. * if so, read the iag at the head of the list now.
  927. * this (head) iag will be updated later to reflect
  928. * the addition of the current iag at the head of
  929. * the list.
  930. */
  931. if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
  932. if ((rc = diIAGRead(imap, fwd, &amp)))
  933. goto error_out;
  934. aiagp = (struct iag *) amp->data;
  935. }
  936. } else {
  937. /* iag has free extents. check if the addition of a free
  938. * extent will cause all extents to be free within this
  939. * iag. if so, the iag will be removed from the ag extent
  940. * free list and placed on the inode map's free iag list.
  941. */
  942. if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
  943. /* in preparation for removing the iag from the
  944. * ag extent free list, read the iags preceeding
  945. * and following the iag on the ag extent free
  946. * list.
  947. */
  948. if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
  949. if ((rc = diIAGRead(imap, fwd, &amp)))
  950. goto error_out;
  951. aiagp = (struct iag *) amp->data;
  952. }
  953. if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
  954. if ((rc = diIAGRead(imap, back, &bmp)))
  955. goto error_out;
  956. biagp = (struct iag *) bmp->data;
  957. }
  958. }
  959. }
  960. /* remove the iag from the ag inode free list if freeing
  961. * this extent cause the iag to have no free inodes.
  962. */
  963. if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
  964. int inofreeback = le32_to_cpu(iagp->inofreeback);
  965. int inofreefwd = le32_to_cpu(iagp->inofreefwd);
  966. /* in preparation for removing the iag from the
  967. * ag inode free list, read the iags preceeding
  968. * and following the iag on the ag inode free
  969. * list. before reading these iags, we must make
  970. * sure that we already don't have them in hand
  971. * from up above, since re-reading an iag (buffer)
  972. * we are currently holding would cause a deadlock.
  973. */
  974. if (inofreefwd >= 0) {
  975. if (inofreefwd == fwd)
  976. ciagp = (struct iag *) amp->data;
  977. else if (inofreefwd == back)
  978. ciagp = (struct iag *) bmp->data;
  979. else {
  980. if ((rc =
  981. diIAGRead(imap, inofreefwd, &cmp)))
  982. goto error_out;
  983. ciagp = (struct iag *) cmp->data;
  984. }
  985. assert(ciagp != NULL);
  986. }
  987. if (inofreeback >= 0) {
  988. if (inofreeback == fwd)
  989. diagp = (struct iag *) amp->data;
  990. else if (inofreeback == back)
  991. diagp = (struct iag *) bmp->data;
  992. else {
  993. if ((rc =
  994. diIAGRead(imap, inofreeback, &dmp)))
  995. goto error_out;
  996. diagp = (struct iag *) dmp->data;
  997. }
  998. assert(diagp != NULL);
  999. }
  1000. }
  1001. IREAD_UNLOCK(ipimap);
  1002. /*
  1003. * invalidate any page of the inode extent freed from buffer cache;
  1004. */
  1005. freepxd = iagp->inoext[extno];
  1006. invalidate_pxd_metapages(ip, freepxd);
  1007. /*
  1008. * update iag list(s) (careful update step 2)
  1009. */
  1010. /* add the iag to the ag extent free list if this is the
  1011. * first free extent for the iag.
  1012. */
  1013. if (iagp->nfreeexts == 0) {
  1014. if (fwd >= 0)
  1015. aiagp->extfreeback = cpu_to_le32(iagno);
  1016. iagp->extfreefwd =
  1017. cpu_to_le32(imap->im_agctl[agno].extfree);
  1018. iagp->extfreeback = cpu_to_le32(-1);
  1019. imap->im_agctl[agno].extfree = iagno;
  1020. } else {
  1021. /* remove the iag from the ag extent list if all extents
  1022. * are now free and place it on the inode map iag free list.
  1023. */
  1024. if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG - 1)) {
  1025. if (fwd >= 0)
  1026. aiagp->extfreeback = iagp->extfreeback;
  1027. if (back >= 0)
  1028. biagp->extfreefwd = iagp->extfreefwd;
  1029. else
  1030. imap->im_agctl[agno].extfree =
  1031. le32_to_cpu(iagp->extfreefwd);
  1032. iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
  1033. IAGFREE_LOCK(imap);
  1034. iagp->iagfree = cpu_to_le32(imap->im_freeiag);
  1035. imap->im_freeiag = iagno;
  1036. IAGFREE_UNLOCK(imap);
  1037. }
  1038. }
  1039. /* remove the iag from the ag inode free list if freeing
  1040. * this extent causes the iag to have no free inodes.
  1041. */
  1042. if (iagp->nfreeinos == cpu_to_le32(INOSPEREXT - 1)) {
  1043. if ((int) le32_to_cpu(iagp->inofreefwd) >= 0)
  1044. ciagp->inofreeback = iagp->inofreeback;
  1045. if ((int) le32_to_cpu(iagp->inofreeback) >= 0)
  1046. diagp->inofreefwd = iagp->inofreefwd;
  1047. else
  1048. imap->im_agctl[agno].inofree =
  1049. le32_to_cpu(iagp->inofreefwd);
  1050. iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
  1051. }
  1052. /* update the inode extent address and working map
  1053. * to reflect the free extent.
  1054. * the permanent map should have been updated already
  1055. * for the inode being freed.
  1056. */
  1057. if (iagp->pmap[extno] != 0) {
  1058. jfs_error(ip->i_sb, "diFree: the pmap does not show inode free");
  1059. }
  1060. iagp->wmap[extno] = 0;
  1061. PXDlength(&iagp->inoext[extno], 0);
  1062. PXDaddress(&iagp->inoext[extno], 0);
  1063. /* update the free extent and free inode summary maps
  1064. * to reflect the freed extent.
  1065. * the inode summary map is marked to indicate no inodes
  1066. * available for the freed extent.
  1067. */
  1068. sword = extno >> L2EXTSPERSUM;
  1069. bitno = extno & (EXTSPERSUM - 1);
  1070. mask = HIGHORDER >> bitno;
  1071. iagp->inosmap[sword] |= cpu_to_le32(mask);
  1072. iagp->extsmap[sword] &= cpu_to_le32(~mask);
  1073. /* update the number of free inodes and number of free extents
  1074. * for the iag.
  1075. */
  1076. iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) -
  1077. (INOSPEREXT - 1));
  1078. iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) + 1);
  1079. /* update the number of free inodes and backed inodes
  1080. * at the ag and inode map level.
  1081. */
  1082. imap->im_agctl[agno].numfree -= (INOSPEREXT - 1);
  1083. imap->im_agctl[agno].numinos -= INOSPEREXT;
  1084. atomic_sub(INOSPEREXT - 1, &imap->im_numfree);
  1085. atomic_sub(INOSPEREXT, &imap->im_numinos);
  1086. if (amp)
  1087. write_metapage(amp);
  1088. if (bmp)
  1089. write_metapage(bmp);
  1090. if (cmp)
  1091. write_metapage(cmp);
  1092. if (dmp)
  1093. write_metapage(dmp);
  1094. /*
  1095. * start transaction to update block allocation map
  1096. * for the inode extent freed;
  1097. *
  1098. * N.B. AG_LOCK is released and iag will be released below, and
  1099. * other thread may allocate inode from/reusing the ixad freed
  1100. * BUT with new/different backing inode extent from the extent
  1101. * to be freed by the transaction;
  1102. */
  1103. tid = txBegin(ipimap->i_sb, COMMIT_FORCE);
  1104. mutex_lock(&JFS_IP(ipimap)->commit_mutex);
  1105. /* acquire tlock of the iag page of the freed ixad
  1106. * to force the page NOHOMEOK (even though no data is
  1107. * logged from the iag page) until NOREDOPAGE|FREEXTENT log
  1108. * for the free of the extent is committed;
  1109. * write FREEXTENT|NOREDOPAGE log record
  1110. * N.B. linelock is overlaid as freed extent descriptor;
  1111. */
  1112. tlck = txLock(tid, ipimap, mp, tlckINODE | tlckFREE);
  1113. pxdlock = (struct pxd_lock *) & tlck->lock;
  1114. pxdlock->flag = mlckFREEPXD;
  1115. pxdlock->pxd = freepxd;
  1116. pxdlock->index = 1;
  1117. write_metapage(mp);
  1118. iplist[0] = ipimap;
  1119. /*
  1120. * logredo needs the IAG number and IAG extent index in order
  1121. * to ensure that the IMap is consistent. The least disruptive
  1122. * way to pass these values through to the transaction manager
  1123. * is in the iplist array.
  1124. *
  1125. * It's not pretty, but it works.
  1126. */
  1127. iplist[1] = (struct inode *) (size_t)iagno;
  1128. iplist[2] = (struct inode *) (size_t)extno;
  1129. rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
  1130. txEnd(tid);
  1131. mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
  1132. /* unlock the AG inode map information */
  1133. AG_UNLOCK(imap, agno);
  1134. return (0);
  1135. error_out:
  1136. IREAD_UNLOCK(ipimap);
  1137. if (amp)
  1138. release_metapage(amp);
  1139. if (bmp)
  1140. release_metapage(bmp);
  1141. if (cmp)
  1142. release_metapage(cmp);
  1143. if (dmp)
  1144. release_metapage(dmp);
  1145. AG_UNLOCK(imap, agno);
  1146. release_metapage(mp);
  1147. return (rc);
  1148. }
  1149. /*
  1150. * There are several places in the diAlloc* routines where we initialize
  1151. * the inode.
  1152. */
  1153. static inline void
  1154. diInitInode(struct inode *ip, int iagno, int ino, int extno, struct iag * iagp)
  1155. {
  1156. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  1157. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  1158. ip->i_ino = (iagno << L2INOSPERIAG) + ino;
  1159. jfs_ip->ixpxd = iagp->inoext[extno];
  1160. jfs_ip->agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
  1161. jfs_ip->active_ag = -1;
  1162. }
  1163. /*
  1164. * NAME: diAlloc(pip,dir,ip)
  1165. *
  1166. * FUNCTION: allocate a disk inode from the inode working map
  1167. * for a fileset or aggregate.
  1168. *
  1169. * PARAMETERS:
  1170. * pip - pointer to incore inode for the parent inode.
  1171. * dir - 'true' if the new disk inode is for a directory.
  1172. * ip - pointer to a new inode
  1173. *
  1174. * RETURN VALUES:
  1175. * 0 - success.
  1176. * -ENOSPC - insufficient disk resources.
  1177. * -EIO - i/o error.
  1178. */
  1179. int diAlloc(struct inode *pip, bool dir, struct inode *ip)
  1180. {
  1181. int rc, ino, iagno, addext, extno, bitno, sword;
  1182. int nwords, rem, i, agno;
  1183. u32 mask, inosmap, extsmap;
  1184. struct inode *ipimap;
  1185. struct metapage *mp;
  1186. ino_t inum;
  1187. struct iag *iagp;
  1188. struct inomap *imap;
  1189. /* get the pointers to the inode map inode and the
  1190. * corresponding imap control structure.
  1191. */
  1192. ipimap = JFS_SBI(pip->i_sb)->ipimap;
  1193. imap = JFS_IP(ipimap)->i_imap;
  1194. JFS_IP(ip)->ipimap = ipimap;
  1195. JFS_IP(ip)->fileset = FILESYSTEM_I;
  1196. /* for a directory, the allocation policy is to start
  1197. * at the ag level using the preferred ag.
  1198. */
  1199. if (dir) {
  1200. agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
  1201. AG_LOCK(imap, agno);
  1202. goto tryag;
  1203. }
  1204. /* for files, the policy starts off by trying to allocate from
  1205. * the same iag containing the parent disk inode:
  1206. * try to allocate the new disk inode close to the parent disk
  1207. * inode, using parent disk inode number + 1 as the allocation
  1208. * hint. (we use a left-to-right policy to attempt to avoid
  1209. * moving backward on the disk.) compute the hint within the
  1210. * file system and the iag.
  1211. */
  1212. /* get the ag number of this iag */
  1213. agno = JFS_IP(pip)->agno;
  1214. if (atomic_read(&JFS_SBI(pip->i_sb)->bmap->db_active[agno])) {
  1215. /*
  1216. * There is an open file actively growing. We want to
  1217. * allocate new inodes from a different ag to avoid
  1218. * fragmentation problems.
  1219. */
  1220. agno = dbNextAG(JFS_SBI(pip->i_sb)->ipbmap);
  1221. AG_LOCK(imap, agno);
  1222. goto tryag;
  1223. }
  1224. inum = pip->i_ino + 1;
  1225. ino = inum & (INOSPERIAG - 1);
  1226. /* back off the the hint if it is outside of the iag */
  1227. if (ino == 0)
  1228. inum = pip->i_ino;
  1229. /* lock the AG inode map information */
  1230. AG_LOCK(imap, agno);
  1231. /* Get read lock on imap inode */
  1232. IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
  1233. /* get the iag number and read the iag */
  1234. iagno = INOTOIAG(inum);
  1235. if ((rc = diIAGRead(imap, iagno, &mp))) {
  1236. IREAD_UNLOCK(ipimap);
  1237. AG_UNLOCK(imap, agno);
  1238. return (rc);
  1239. }
  1240. iagp = (struct iag *) mp->data;
  1241. /* determine if new inode extent is allowed to be added to the iag.
  1242. * new inode extent can be added to the iag if the ag
  1243. * has less than 32 free disk inodes and the iag has free extents.
  1244. */
  1245. addext = (imap->im_agctl[agno].numfree < 32 && iagp->nfreeexts);
  1246. /*
  1247. * try to allocate from the IAG
  1248. */
  1249. /* check if the inode may be allocated from the iag
  1250. * (i.e. the inode has free inodes or new extent can be added).
  1251. */
  1252. if (iagp->nfreeinos || addext) {
  1253. /* determine the extent number of the hint.
  1254. */
  1255. extno = ino >> L2INOSPEREXT;
  1256. /* check if the extent containing the hint has backed
  1257. * inodes. if so, try to allocate within this extent.
  1258. */
  1259. if (addressPXD(&iagp->inoext[extno])) {
  1260. bitno = ino & (INOSPEREXT - 1);
  1261. if ((bitno =
  1262. diFindFree(le32_to_cpu(iagp->wmap[extno]),
  1263. bitno))
  1264. < INOSPEREXT) {
  1265. ino = (extno << L2INOSPEREXT) + bitno;
  1266. /* a free inode (bit) was found within this
  1267. * extent, so allocate it.
  1268. */
  1269. rc = diAllocBit(imap, iagp, ino);
  1270. IREAD_UNLOCK(ipimap);
  1271. if (rc) {
  1272. assert(rc == -EIO);
  1273. } else {
  1274. /* set the results of the allocation
  1275. * and write the iag.
  1276. */
  1277. diInitInode(ip, iagno, ino, extno,
  1278. iagp);
  1279. mark_metapage_dirty(mp);
  1280. }
  1281. release_metapage(mp);
  1282. /* free the AG lock and return.
  1283. */
  1284. AG_UNLOCK(imap, agno);
  1285. return (rc);
  1286. }
  1287. if (!addext)
  1288. extno =
  1289. (extno ==
  1290. EXTSPERIAG - 1) ? 0 : extno + 1;
  1291. }
  1292. /*
  1293. * no free inodes within the extent containing the hint.
  1294. *
  1295. * try to allocate from the backed extents following
  1296. * hint or, if appropriate (i.e. addext is true), allocate
  1297. * an extent of free inodes at or following the extent
  1298. * containing the hint.
  1299. *
  1300. * the free inode and free extent summary maps are used
  1301. * here, so determine the starting summary map position
  1302. * and the number of words we'll have to examine. again,
  1303. * the approach is to allocate following the hint, so we
  1304. * might have to initially ignore prior bits of the summary
  1305. * map that represent extents prior to the extent containing
  1306. * the hint and later revisit these bits.
  1307. */
  1308. bitno = extno & (EXTSPERSUM - 1);
  1309. nwords = (bitno == 0) ? SMAPSZ : SMAPSZ + 1;
  1310. sword = extno >> L2EXTSPERSUM;
  1311. /* mask any prior bits for the starting words of the
  1312. * summary map.
  1313. */
  1314. mask = ONES << (EXTSPERSUM - bitno);
  1315. inosmap = le32_to_cpu(iagp->inosmap[sword]) | mask;
  1316. extsmap = le32_to_cpu(iagp->extsmap[sword]) | mask;
  1317. /* scan the free inode and free extent summary maps for
  1318. * free resources.
  1319. */
  1320. for (i = 0; i < nwords; i++) {
  1321. /* check if this word of the free inode summary
  1322. * map describes an extent with free inodes.
  1323. */
  1324. if (~inosmap) {
  1325. /* an extent with free inodes has been
  1326. * found. determine the extent number
  1327. * and the inode number within the extent.
  1328. */
  1329. rem = diFindFree(inosmap, 0);
  1330. extno = (sword << L2EXTSPERSUM) + rem;
  1331. rem = diFindFree(le32_to_cpu(iagp->wmap[extno]),
  1332. 0);
  1333. if (rem >= INOSPEREXT) {
  1334. IREAD_UNLOCK(ipimap);
  1335. release_metapage(mp);
  1336. AG_UNLOCK(imap, agno);
  1337. jfs_error(ip->i_sb,
  1338. "diAlloc: can't find free bit "
  1339. "in wmap");
  1340. return EIO;
  1341. }
  1342. /* determine the inode number within the
  1343. * iag and allocate the inode from the
  1344. * map.
  1345. */
  1346. ino = (extno << L2INOSPEREXT) + rem;
  1347. rc = diAllocBit(imap, iagp, ino);
  1348. IREAD_UNLOCK(ipimap);
  1349. if (rc)
  1350. assert(rc == -EIO);
  1351. else {
  1352. /* set the results of the allocation
  1353. * and write the iag.
  1354. */
  1355. diInitInode(ip, iagno, ino, extno,
  1356. iagp);
  1357. mark_metapage_dirty(mp);
  1358. }
  1359. release_metapage(mp);
  1360. /* free the AG lock and return.
  1361. */
  1362. AG_UNLOCK(imap, agno);
  1363. return (rc);
  1364. }
  1365. /* check if we may allocate an extent of free
  1366. * inodes and whether this word of the free
  1367. * extents summary map describes a free extent.
  1368. */
  1369. if (addext && ~extsmap) {
  1370. /* a free extent has been found. determine
  1371. * the extent number.
  1372. */
  1373. rem = diFindFree(extsmap, 0);
  1374. extno = (sword << L2EXTSPERSUM) + rem;
  1375. /* allocate an extent of free inodes.
  1376. */
  1377. if ((rc = diNewExt(imap, iagp, extno))) {
  1378. /* if there is no disk space for a
  1379. * new extent, try to allocate the
  1380. * disk inode from somewhere else.
  1381. */
  1382. if (rc == -ENOSPC)
  1383. break;
  1384. assert(rc == -EIO);
  1385. } else {
  1386. /* set the results of the allocation
  1387. * and write the iag.
  1388. */
  1389. diInitInode(ip, iagno,
  1390. extno << L2INOSPEREXT,
  1391. extno, iagp);
  1392. mark_metapage_dirty(mp);
  1393. }
  1394. release_metapage(mp);
  1395. /* free the imap inode & the AG lock & return.
  1396. */
  1397. IREAD_UNLOCK(ipimap);
  1398. AG_UNLOCK(imap, agno);
  1399. return (rc);
  1400. }
  1401. /* move on to the next set of summary map words.
  1402. */
  1403. sword = (sword == SMAPSZ - 1) ? 0 : sword + 1;
  1404. inosmap = le32_to_cpu(iagp->inosmap[sword]);
  1405. extsmap = le32_to_cpu(iagp->extsmap[sword]);
  1406. }
  1407. }
  1408. /* unlock imap inode */
  1409. IREAD_UNLOCK(ipimap);
  1410. /* nothing doing in this iag, so release it. */
  1411. release_metapage(mp);
  1412. tryag:
  1413. /*
  1414. * try to allocate anywhere within the same AG as the parent inode.
  1415. */
  1416. rc = diAllocAG(imap, agno, dir, ip);
  1417. AG_UNLOCK(imap, agno);
  1418. if (rc != -ENOSPC)
  1419. return (rc);
  1420. /*
  1421. * try to allocate in any AG.
  1422. */
  1423. return (diAllocAny(imap, agno, dir, ip));
  1424. }
  1425. /*
  1426. * NAME: diAllocAG(imap,agno,dir,ip)
  1427. *
  1428. * FUNCTION: allocate a disk inode from the allocation group.
  1429. *
  1430. * this routine first determines if a new extent of free
  1431. * inodes should be added for the allocation group, with
  1432. * the current request satisfied from this extent. if this
  1433. * is the case, an attempt will be made to do just that. if
  1434. * this attempt fails or it has been determined that a new
  1435. * extent should not be added, an attempt is made to satisfy
  1436. * the request by allocating an existing (backed) free inode
  1437. * from the allocation group.
  1438. *
  1439. * PRE CONDITION: Already have the AG lock for this AG.
  1440. *
  1441. * PARAMETERS:
  1442. * imap - pointer to inode map control structure.
  1443. * agno - allocation group to allocate from.
  1444. * dir - 'true' if the new disk inode is for a directory.
  1445. * ip - pointer to the new inode to be filled in on successful return
  1446. * with the disk inode number allocated, its extent address
  1447. * and the start of the ag.
  1448. *
  1449. * RETURN VALUES:
  1450. * 0 - success.
  1451. * -ENOSPC - insufficient disk resources.
  1452. * -EIO - i/o error.
  1453. */
  1454. static int
  1455. diAllocAG(struct inomap * imap, int agno, bool dir, struct inode *ip)
  1456. {
  1457. int rc, addext, numfree, numinos;
  1458. /* get the number of free and the number of backed disk
  1459. * inodes currently within the ag.
  1460. */
  1461. numfree = imap->im_agctl[agno].numfree;
  1462. numinos = imap->im_agctl[agno].numinos;
  1463. if (numfree > numinos) {
  1464. jfs_error(ip->i_sb, "diAllocAG: numfree > numinos");
  1465. return -EIO;
  1466. }
  1467. /* determine if we should allocate a new extent of free inodes
  1468. * within the ag: for directory inodes, add a new extent
  1469. * if there are a small number of free inodes or number of free
  1470. * inodes is a small percentage of the number of backed inodes.
  1471. */
  1472. if (dir)
  1473. addext = (numfree < 64 ||
  1474. (numfree < 256
  1475. && ((numfree * 100) / numinos) <= 20));
  1476. else
  1477. addext = (numfree == 0);
  1478. /*
  1479. * try to allocate a new extent of free inodes.
  1480. */
  1481. if (addext) {
  1482. /* if free space is not avaliable for this new extent, try
  1483. * below to allocate a free and existing (already backed)
  1484. * inode from the ag.
  1485. */
  1486. if ((rc = diAllocExt(imap, agno, ip)) != -ENOSPC)
  1487. return (rc);
  1488. }
  1489. /*
  1490. * try to allocate an existing free inode from the ag.
  1491. */
  1492. return (diAllocIno(imap, agno, ip));
  1493. }
  1494. /*
  1495. * NAME: diAllocAny(imap,agno,dir,iap)
  1496. *
  1497. * FUNCTION: allocate a disk inode from any other allocation group.
  1498. *
  1499. * this routine is called when an allocation attempt within
  1500. * the primary allocation group has failed. if attempts to
  1501. * allocate an inode from any allocation group other than the
  1502. * specified primary group.
  1503. *
  1504. * PARAMETERS:
  1505. * imap - pointer to inode map control structure.
  1506. * agno - primary allocation group (to avoid).
  1507. * dir - 'true' if the new disk inode is for a directory.
  1508. * ip - pointer to a new inode to be filled in on successful return
  1509. * with the disk inode number allocated, its extent address
  1510. * and the start of the ag.
  1511. *
  1512. * RETURN VALUES:
  1513. * 0 - success.
  1514. * -ENOSPC - insufficient disk resources.
  1515. * -EIO - i/o error.
  1516. */
  1517. static int
  1518. diAllocAny(struct inomap * imap, int agno, bool dir, struct inode *ip)
  1519. {
  1520. int ag, rc;
  1521. int maxag = JFS_SBI(imap->im_ipimap->i_sb)->bmap->db_maxag;
  1522. /* try to allocate from the ags following agno up to
  1523. * the maximum ag number.
  1524. */
  1525. for (ag = agno + 1; ag <= maxag; ag++) {
  1526. AG_LOCK(imap, ag);
  1527. rc = diAllocAG(imap, ag, dir, ip);
  1528. AG_UNLOCK(imap, ag);
  1529. if (rc != -ENOSPC)
  1530. return (rc);
  1531. }
  1532. /* try to allocate from the ags in front of agno.
  1533. */
  1534. for (ag = 0; ag < agno; ag++) {
  1535. AG_LOCK(imap, ag);
  1536. rc = diAllocAG(imap, ag, dir, ip);
  1537. AG_UNLOCK(imap, ag);
  1538. if (rc != -ENOSPC)
  1539. return (rc);
  1540. }
  1541. /* no free disk inodes.
  1542. */
  1543. return -ENOSPC;
  1544. }
  1545. /*
  1546. * NAME: diAllocIno(imap,agno,ip)
  1547. *
  1548. * FUNCTION: allocate a disk inode from the allocation group's free
  1549. * inode list, returning an error if this free list is
  1550. * empty (i.e. no iags on the list).
  1551. *
  1552. * allocation occurs from the first iag on the list using
  1553. * the iag's free inode summary map to find the leftmost
  1554. * free inode in the iag.
  1555. *
  1556. * PRE CONDITION: Already have AG lock for this AG.
  1557. *
  1558. * PARAMETERS:
  1559. * imap - pointer to inode map control structure.
  1560. * agno - allocation group.
  1561. * ip - pointer to new inode to be filled in on successful return
  1562. * with the disk inode number allocated, its extent address
  1563. * and the start of the ag.
  1564. *
  1565. * RETURN VALUES:
  1566. * 0 - success.
  1567. * -ENOSPC - insufficient disk resources.
  1568. * -EIO - i/o error.
  1569. */
  1570. static int diAllocIno(struct inomap * imap, int agno, struct inode *ip)
  1571. {
  1572. int iagno, ino, rc, rem, extno, sword;
  1573. struct metapage *mp;
  1574. struct iag *iagp;
  1575. /* check if there are iags on the ag's free inode list.
  1576. */
  1577. if ((iagno = imap->im_agctl[agno].inofree) < 0)
  1578. return -ENOSPC;
  1579. /* obtain read lock on imap inode */
  1580. IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
  1581. /* read the iag at the head of the list.
  1582. */
  1583. if ((rc = diIAGRead(imap, iagno, &mp))) {
  1584. IREAD_UNLOCK(imap->im_ipimap);
  1585. return (rc);
  1586. }
  1587. iagp = (struct iag *) mp->data;
  1588. /* better be free inodes in this iag if it is on the
  1589. * list.
  1590. */
  1591. if (!iagp->nfreeinos) {
  1592. IREAD_UNLOCK(imap->im_ipimap);
  1593. release_metapage(mp);
  1594. jfs_error(ip->i_sb,
  1595. "diAllocIno: nfreeinos = 0, but iag on freelist");
  1596. return -EIO;
  1597. }
  1598. /* scan the free inode summary map to find an extent
  1599. * with free inodes.
  1600. */
  1601. for (sword = 0;; sword++) {
  1602. if (sword >= SMAPSZ) {
  1603. IREAD_UNLOCK(imap->im_ipimap);
  1604. release_metapage(mp);
  1605. jfs_error(ip->i_sb,
  1606. "diAllocIno: free inode not found in summary map");
  1607. return -EIO;
  1608. }
  1609. if (~iagp->inosmap[sword])
  1610. break;
  1611. }
  1612. /* found a extent with free inodes. determine
  1613. * the extent number.
  1614. */
  1615. rem = diFindFree(le32_to_cpu(iagp->inosmap[sword]), 0);
  1616. if (rem >= EXTSPERSUM) {
  1617. IREAD_UNLOCK(imap->im_ipimap);
  1618. release_metapage(mp);
  1619. jfs_error(ip->i_sb, "diAllocIno: no free extent found");
  1620. return -EIO;
  1621. }
  1622. extno = (sword << L2EXTSPERSUM) + rem;
  1623. /* find the first free inode in the extent.
  1624. */
  1625. rem = diFindFree(le32_to_cpu(iagp->wmap[extno]), 0);
  1626. if (rem >= INOSPEREXT) {
  1627. IREAD_UNLOCK(imap->im_ipimap);
  1628. release_metapage(mp);
  1629. jfs_error(ip->i_sb, "diAllocIno: free inode not found");
  1630. return -EIO;
  1631. }
  1632. /* compute the inode number within the iag.
  1633. */
  1634. ino = (extno << L2INOSPEREXT) + rem;
  1635. /* allocate the inode.
  1636. */
  1637. rc = diAllocBit(imap, iagp, ino);
  1638. IREAD_UNLOCK(imap->im_ipimap);
  1639. if (rc) {
  1640. release_metapage(mp);
  1641. return (rc);
  1642. }
  1643. /* set the results of the allocation and write the iag.
  1644. */
  1645. diInitInode(ip, iagno, ino, extno, iagp);
  1646. write_metapage(mp);
  1647. return (0);
  1648. }
  1649. /*
  1650. * NAME: diAllocExt(imap,agno,ip)
  1651. *
  1652. * FUNCTION: add a new extent of free inodes to an iag, allocating
  1653. * an inode from this extent to satisfy the current allocation
  1654. * request.
  1655. *
  1656. * this routine first tries to find an existing iag with free
  1657. * extents through the ag free extent list. if list is not
  1658. * empty, the head of the list will be selected as the home
  1659. * of the new extent of free inodes. otherwise (the list is
  1660. * empty), a new iag will be allocated for the ag to contain
  1661. * the extent.
  1662. *
  1663. * once an iag has been selected, the free extent summary map
  1664. * is used to locate a free extent within the iag and diNewExt()
  1665. * is called to initialize the extent, with initialization
  1666. * including the allocation of the first inode of the extent
  1667. * for the purpose of satisfying this request.
  1668. *
  1669. * PARAMETERS:
  1670. * imap - pointer to inode map control structure.
  1671. * agno - allocation group number.
  1672. * ip - pointer to new inode to be filled in on successful return
  1673. * with the disk inode number allocated, its extent address
  1674. * and the start of the ag.
  1675. *
  1676. * RETURN VALUES:
  1677. * 0 - success.
  1678. * -ENOSPC - insufficient disk resources.
  1679. * -EIO - i/o error.
  1680. */
  1681. static int diAllocExt(struct inomap * imap, int agno, struct inode *ip)
  1682. {
  1683. int rem, iagno, sword, extno, rc;
  1684. struct metapage *mp;
  1685. struct iag *iagp;
  1686. /* check if the ag has any iags with free extents. if not,
  1687. * allocate a new iag for the ag.
  1688. */
  1689. if ((iagno = imap->im_agctl[agno].extfree) < 0) {
  1690. /* If successful, diNewIAG will obtain the read lock on the
  1691. * imap inode.
  1692. */
  1693. if ((rc = diNewIAG(imap, &iagno, agno, &mp))) {
  1694. return (rc);
  1695. }
  1696. iagp = (struct iag *) mp->data;
  1697. /* set the ag number if this a brand new iag
  1698. */
  1699. iagp->agstart =
  1700. cpu_to_le64(AGTOBLK(agno, imap->im_ipimap));
  1701. } else {
  1702. /* read the iag.
  1703. */
  1704. IREAD_LOCK(imap->im_ipimap, RDWRLOCK_IMAP);
  1705. if ((rc = diIAGRead(imap, iagno, &mp))) {
  1706. IREAD_UNLOCK(imap->im_ipimap);
  1707. jfs_error(ip->i_sb, "diAllocExt: error reading iag");
  1708. return rc;
  1709. }
  1710. iagp = (struct iag *) mp->data;
  1711. }
  1712. /* using the free extent summary map, find a free extent.
  1713. */
  1714. for (sword = 0;; sword++) {
  1715. if (sword >= SMAPSZ) {
  1716. release_metapage(mp);
  1717. IREAD_UNLOCK(imap->im_ipimap);
  1718. jfs_error(ip->i_sb,
  1719. "diAllocExt: free ext summary map not found");
  1720. return -EIO;
  1721. }
  1722. if (~iagp->extsmap[sword])
  1723. break;
  1724. }
  1725. /* determine the extent number of the free extent.
  1726. */
  1727. rem = diFindFree(le32_to_cpu(iagp->extsmap[sword]), 0);
  1728. if (rem >= EXTSPERSUM) {
  1729. release_metapage(mp);
  1730. IREAD_UNLOCK(imap->im_ipimap);
  1731. jfs_error(ip->i_sb, "diAllocExt: free extent not found");
  1732. return -EIO;
  1733. }
  1734. extno = (sword << L2EXTSPERSUM) + rem;
  1735. /* initialize the new extent.
  1736. */
  1737. rc = diNewExt(imap, iagp, extno);
  1738. IREAD_UNLOCK(imap->im_ipimap);
  1739. if (rc) {
  1740. /* something bad happened. if a new iag was allocated,
  1741. * place it back on the inode map's iag free list, and
  1742. * clear the ag number information.
  1743. */
  1744. if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
  1745. IAGFREE_LOCK(imap);
  1746. iagp->iagfree = cpu_to_le32(imap->im_freeiag);
  1747. imap->im_freeiag = iagno;
  1748. IAGFREE_UNLOCK(imap);
  1749. }
  1750. write_metapage(mp);
  1751. return (rc);
  1752. }
  1753. /* set the results of the allocation and write the iag.
  1754. */
  1755. diInitInode(ip, iagno, extno << L2INOSPEREXT, extno, iagp);
  1756. write_metapage(mp);
  1757. return (0);
  1758. }
  1759. /*
  1760. * NAME: diAllocBit(imap,iagp,ino)
  1761. *
  1762. * FUNCTION: allocate a backed inode from an iag.
  1763. *
  1764. * this routine performs the mechanics of allocating a
  1765. * specified inode from a backed extent.
  1766. *
  1767. * if the inode to be allocated represents the last free
  1768. * inode within the iag, the iag will be removed from the
  1769. * ag free inode list.
  1770. *
  1771. * a careful update approach is used to provide consistency
  1772. * in the face of updates to multiple buffers. under this
  1773. * approach, all required buffers are obtained before making
  1774. * any updates and are held all are updates are complete.
  1775. *
  1776. * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
  1777. * this AG. Must have read lock on imap inode.
  1778. *
  1779. * PARAMETERS:
  1780. * imap - pointer to inode map control structure.
  1781. * iagp - pointer to iag.
  1782. * ino - inode number to be allocated within the iag.
  1783. *
  1784. * RETURN VALUES:
  1785. * 0 - success.
  1786. * -ENOSPC - insufficient disk resources.
  1787. * -EIO - i/o error.
  1788. */
  1789. static int diAllocBit(struct inomap * imap, struct iag * iagp, int ino)
  1790. {
  1791. int extno, bitno, agno, sword, rc;
  1792. struct metapage *amp = NULL, *bmp = NULL;
  1793. struct iag *aiagp = NULL, *biagp = NULL;
  1794. u32 mask;
  1795. /* check if this is the last free inode within the iag.
  1796. * if so, it will have to be removed from the ag free
  1797. * inode list, so get the iags preceeding and following
  1798. * it on the list.
  1799. */
  1800. if (iagp->nfreeinos == cpu_to_le32(1)) {
  1801. if ((int) le32_to_cpu(iagp->inofreefwd) >= 0) {
  1802. if ((rc =
  1803. diIAGRead(imap, le32_to_cpu(iagp->inofreefwd),
  1804. &amp)))
  1805. return (rc);
  1806. aiagp = (struct iag *) amp->data;
  1807. }
  1808. if ((int) le32_to_cpu(iagp->inofreeback) >= 0) {
  1809. if ((rc =
  1810. diIAGRead(imap,
  1811. le32_to_cpu(iagp->inofreeback),
  1812. &bmp))) {
  1813. if (amp)
  1814. release_metapage(amp);
  1815. return (rc);
  1816. }
  1817. biagp = (struct iag *) bmp->data;
  1818. }
  1819. }
  1820. /* get the ag number, extent number, inode number within
  1821. * the extent.
  1822. */
  1823. agno = BLKTOAG(le64_to_cpu(iagp->agstart), JFS_SBI(imap->im_ipimap->i_sb));
  1824. extno = ino >> L2INOSPEREXT;
  1825. bitno = ino & (INOSPEREXT - 1);
  1826. /* compute the mask for setting the map.
  1827. */
  1828. mask = HIGHORDER >> bitno;
  1829. /* the inode should be free and backed.
  1830. */
  1831. if (((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) ||
  1832. ((le32_to_cpu(iagp->wmap[extno]) & mask) != 0) ||
  1833. (addressPXD(&iagp->inoext[extno]) == 0)) {
  1834. if (amp)
  1835. release_metapage(amp);
  1836. if (bmp)
  1837. release_metapage(bmp);
  1838. jfs_error(imap->im_ipimap->i_sb,
  1839. "diAllocBit: iag inconsistent");
  1840. return -EIO;
  1841. }
  1842. /* mark the inode as allocated in the working map.
  1843. */
  1844. iagp->wmap[extno] |= cpu_to_le32(mask);
  1845. /* check if all inodes within the extent are now
  1846. * allocated. if so, update the free inode summary
  1847. * map to reflect this.
  1848. */
  1849. if (iagp->wmap[extno] == cpu_to_le32(ONES)) {
  1850. sword = extno >> L2EXTSPERSUM;
  1851. bitno = extno & (EXTSPERSUM - 1);
  1852. iagp->inosmap[sword] |= cpu_to_le32(HIGHORDER >> bitno);
  1853. }
  1854. /* if this was the last free inode in the iag, remove the
  1855. * iag from the ag free inode list.
  1856. */
  1857. if (iagp->nfreeinos == cpu_to_le32(1)) {
  1858. if (amp) {
  1859. aiagp->inofreeback = iagp->inofreeback;
  1860. write_metapage(amp);
  1861. }
  1862. if (bmp) {
  1863. biagp->inofreefwd = iagp->inofreefwd;
  1864. write_metapage(bmp);
  1865. } else {
  1866. imap->im_agctl[agno].inofree =
  1867. le32_to_cpu(iagp->inofreefwd);
  1868. }
  1869. iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
  1870. }
  1871. /* update the free inode count at the iag, ag, inode
  1872. * map levels.
  1873. */
  1874. iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) - 1);
  1875. imap->im_agctl[agno].numfree -= 1;
  1876. atomic_dec(&imap->im_numfree);
  1877. return (0);
  1878. }
  1879. /*
  1880. * NAME: diNewExt(imap,iagp,extno)
  1881. *
  1882. * FUNCTION: initialize a new extent of inodes for an iag, allocating
  1883. * the first inode of the extent for use for the current
  1884. * allocation request.
  1885. *
  1886. * disk resources are allocated for the new extent of inodes
  1887. * and the inodes themselves are initialized to reflect their
  1888. * existence within the extent (i.e. their inode numbers and
  1889. * inode extent addresses are set) and their initial state
  1890. * (mode and link count are set to zero).
  1891. *
  1892. * if the iag is new, it is not yet on an ag extent free list
  1893. * but will now be placed on this list.
  1894. *
  1895. * if the allocation of the new extent causes the iag to
  1896. * have no free extent, the iag will be removed from the
  1897. * ag extent free list.
  1898. *
  1899. * if the iag has no free backed inodes, it will be placed
  1900. * on the ag free inode list, since the addition of the new
  1901. * extent will now cause it to have free inodes.
  1902. *
  1903. * a careful update approach is used to provide consistency
  1904. * (i.e. list consistency) in the face of updates to multiple
  1905. * buffers. under this approach, all required buffers are
  1906. * obtained before making any updates and are held until all
  1907. * updates are complete.
  1908. *
  1909. * PRE CONDITION: Already have buffer lock on iagp. Already have AG lock on
  1910. * this AG. Must have read lock on imap inode.
  1911. *
  1912. * PARAMETERS:
  1913. * imap - pointer to inode map control structure.
  1914. * iagp - pointer to iag.
  1915. * extno - extent number.
  1916. *
  1917. * RETURN VALUES:
  1918. * 0 - success.
  1919. * -ENOSPC - insufficient disk resources.
  1920. * -EIO - i/o error.
  1921. */
  1922. static int diNewExt(struct inomap * imap, struct iag * iagp, int extno)
  1923. {
  1924. int agno, iagno, fwd, back, freei = 0, sword, rc;
  1925. struct iag *aiagp = NULL, *biagp = NULL, *ciagp = NULL;
  1926. struct metapage *amp, *bmp, *cmp, *dmp;
  1927. struct inode *ipimap;
  1928. s64 blkno, hint;
  1929. int i, j;
  1930. u32 mask;
  1931. ino_t ino;
  1932. struct dinode *dp;
  1933. struct jfs_sb_info *sbi;
  1934. /* better have free extents.
  1935. */
  1936. if (!iagp->nfreeexts) {
  1937. jfs_error(imap->im_ipimap->i_sb, "diNewExt: no free extents");
  1938. return -EIO;
  1939. }
  1940. /* get the inode map inode.
  1941. */
  1942. ipimap = imap->im_ipimap;
  1943. sbi = JFS_SBI(ipimap->i_sb);
  1944. amp = bmp = cmp = NULL;
  1945. /* get the ag and iag numbers for this iag.
  1946. */
  1947. agno = BLKTOAG(le64_to_cpu(iagp->agstart), sbi);
  1948. iagno = le32_to_cpu(iagp->iagnum);
  1949. /* check if this is the last free extent within the
  1950. * iag. if so, the iag must be removed from the ag
  1951. * free extent list, so get the iags preceeding and
  1952. * following the iag on this list.
  1953. */
  1954. if (iagp->nfreeexts == cpu_to_le32(1)) {
  1955. if ((fwd = le32_to_cpu(iagp->extfreefwd)) >= 0) {
  1956. if ((rc = diIAGRead(imap, fwd, &amp)))
  1957. return (rc);
  1958. aiagp = (struct iag *) amp->data;
  1959. }
  1960. if ((back = le32_to_cpu(iagp->extfreeback)) >= 0) {
  1961. if ((rc = diIAGRead(imap, back, &bmp)))
  1962. goto error_out;
  1963. biagp = (struct iag *) bmp->data;
  1964. }
  1965. } else {
  1966. /* the iag has free extents. if all extents are free
  1967. * (as is the case for a newly allocated iag), the iag
  1968. * must be added to the ag free extent list, so get
  1969. * the iag at the head of the list in preparation for
  1970. * adding this iag to this list.
  1971. */
  1972. fwd = back = -1;
  1973. if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
  1974. if ((fwd = imap->im_agctl[agno].extfree) >= 0) {
  1975. if ((rc = diIAGRead(imap, fwd, &amp)))
  1976. goto error_out;
  1977. aiagp = (struct iag *) amp->data;
  1978. }
  1979. }
  1980. }
  1981. /* check if the iag has no free inodes. if so, the iag
  1982. * will have to be added to the ag free inode list, so get
  1983. * the iag at the head of the list in preparation for
  1984. * adding this iag to this list. in doing this, we must
  1985. * check if we already have the iag at the head of
  1986. * the list in hand.
  1987. */
  1988. if (iagp->nfreeinos == 0) {
  1989. freei = imap->im_agctl[agno].inofree;
  1990. if (freei >= 0) {
  1991. if (freei == fwd) {
  1992. ciagp = aiagp;
  1993. } else if (freei == back) {
  1994. ciagp = biagp;
  1995. } else {
  1996. if ((rc = diIAGRead(imap, freei, &cmp)))
  1997. goto error_out;
  1998. ciagp = (struct iag *) cmp->data;
  1999. }
  2000. if (ciagp == NULL) {
  2001. jfs_error(imap->im_ipimap->i_sb,
  2002. "diNewExt: ciagp == NULL");
  2003. rc = -EIO;
  2004. goto error_out;
  2005. }
  2006. }
  2007. }
  2008. /* allocate disk space for the inode extent.
  2009. */
  2010. if ((extno == 0) || (addressPXD(&iagp->inoext[extno - 1]) == 0))
  2011. hint = ((s64) agno << sbi->bmap->db_agl2size) - 1;
  2012. else
  2013. hint = addressPXD(&iagp->inoext[extno - 1]) +
  2014. lengthPXD(&iagp->inoext[extno - 1]) - 1;
  2015. if ((rc = dbAlloc(ipimap, hint, (s64) imap->im_nbperiext, &blkno)))
  2016. goto error_out;
  2017. /* compute the inode number of the first inode within the
  2018. * extent.
  2019. */
  2020. ino = (iagno << L2INOSPERIAG) + (extno << L2INOSPEREXT);
  2021. /* initialize the inodes within the newly allocated extent a
  2022. * page at a time.
  2023. */
  2024. for (i = 0; i < imap->im_nbperiext; i += sbi->nbperpage) {
  2025. /* get a buffer for this page of disk inodes.
  2026. */
  2027. dmp = get_metapage(ipimap, blkno + i, PSIZE, 1);
  2028. if (dmp == NULL) {
  2029. rc = -EIO;
  2030. goto error_out;
  2031. }
  2032. dp = (struct dinode *) dmp->data;
  2033. /* initialize the inode number, mode, link count and
  2034. * inode extent address.
  2035. */
  2036. for (j = 0; j < INOSPERPAGE; j++, dp++, ino++) {
  2037. dp->di_inostamp = cpu_to_le32(sbi->inostamp);
  2038. dp->di_number = cpu_to_le32(ino);
  2039. dp->di_fileset = cpu_to_le32(FILESYSTEM_I);
  2040. dp->di_mode = 0;
  2041. dp->di_nlink = 0;
  2042. PXDaddress(&(dp->di_ixpxd), blkno);
  2043. PXDlength(&(dp->di_ixpxd), imap->im_nbperiext);
  2044. }
  2045. write_metapage(dmp);
  2046. }
  2047. /* if this is the last free extent within the iag, remove the
  2048. * iag from the ag free extent list.
  2049. */
  2050. if (iagp->nfreeexts == cpu_to_le32(1)) {
  2051. if (fwd >= 0)
  2052. aiagp->extfreeback = iagp->extfreeback;
  2053. if (back >= 0)
  2054. biagp->extfreefwd = iagp->extfreefwd;
  2055. else
  2056. imap->im_agctl[agno].extfree =
  2057. le32_to_cpu(iagp->extfreefwd);
  2058. iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
  2059. } else {
  2060. /* if the iag has all free extents (newly allocated iag),
  2061. * add the iag to the ag free extent list.
  2062. */
  2063. if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
  2064. if (fwd >= 0)
  2065. aiagp->extfreeback = cpu_to_le32(iagno);
  2066. iagp->extfreefwd = cpu_to_le32(fwd);
  2067. iagp->extfreeback = cpu_to_le32(-1);
  2068. imap->im_agctl[agno].extfree = iagno;
  2069. }
  2070. }
  2071. /* if the iag has no free inodes, add the iag to the
  2072. * ag free inode list.
  2073. */
  2074. if (iagp->nfreeinos == 0) {
  2075. if (freei >= 0)
  2076. ciagp->inofreeback = cpu_to_le32(iagno);
  2077. iagp->inofreefwd =
  2078. cpu_to_le32(imap->im_agctl[agno].inofree);
  2079. iagp->inofreeback = cpu_to_le32(-1);
  2080. imap->im_agctl[agno].inofree = iagno;
  2081. }
  2082. /* initialize the extent descriptor of the extent. */
  2083. PXDlength(&iagp->inoext[extno], imap->im_nbperiext);
  2084. PXDaddress(&iagp->inoext[extno], blkno);
  2085. /* initialize the working and persistent map of the extent.
  2086. * the working map will be initialized such that
  2087. * it indicates the first inode of the extent is allocated.
  2088. */
  2089. iagp->wmap[extno] = cpu_to_le32(HIGHORDER);
  2090. iagp->pmap[extno] = 0;
  2091. /* update the free inode and free extent summary maps
  2092. * for the extent to indicate the extent has free inodes
  2093. * and no longer represents a free extent.
  2094. */
  2095. sword = extno >> L2EXTSPERSUM;
  2096. mask = HIGHORDER >> (extno & (EXTSPERSUM - 1));
  2097. iagp->extsmap[sword] |= cpu_to_le32(mask);
  2098. iagp->inosmap[sword] &= cpu_to_le32(~mask);
  2099. /* update the free inode and free extent counts for the
  2100. * iag.
  2101. */
  2102. iagp->nfreeinos = cpu_to_le32(le32_to_cpu(iagp->nfreeinos) +
  2103. (INOSPEREXT - 1));
  2104. iagp->nfreeexts = cpu_to_le32(le32_to_cpu(iagp->nfreeexts) - 1);
  2105. /* update the free and backed inode counts for the ag.
  2106. */
  2107. imap->im_agctl[agno].numfree += (INOSPEREXT - 1);
  2108. imap->im_agctl[agno].numinos += INOSPEREXT;
  2109. /* update the free and backed inode counts for the inode map.
  2110. */
  2111. atomic_add(INOSPEREXT - 1, &imap->im_numfree);
  2112. atomic_add(INOSPEREXT, &imap->im_numinos);
  2113. /* write the iags.
  2114. */
  2115. if (amp)
  2116. write_metapage(amp);
  2117. if (bmp)
  2118. write_metapage(bmp);
  2119. if (cmp)
  2120. write_metapage(cmp);
  2121. return (0);
  2122. error_out:
  2123. /* release the iags.
  2124. */
  2125. if (amp)
  2126. release_metapage(amp);
  2127. if (bmp)
  2128. release_metapage(bmp);
  2129. if (cmp)
  2130. release_metapage(cmp);
  2131. return (rc);
  2132. }
  2133. /*
  2134. * NAME: diNewIAG(imap,iagnop,agno)
  2135. *
  2136. * FUNCTION: allocate a new iag for an allocation group.
  2137. *
  2138. * first tries to allocate the iag from the inode map
  2139. * iagfree list:
  2140. * if the list has free iags, the head of the list is removed
  2141. * and returned to satisfy the request.
  2142. * if the inode map's iag free list is empty, the inode map
  2143. * is extended to hold a new iag. this new iag is initialized
  2144. * and returned to satisfy the request.
  2145. *
  2146. * PARAMETERS:
  2147. * imap - pointer to inode map control structure.
  2148. * iagnop - pointer to an iag number set with the number of the
  2149. * newly allocated iag upon successful return.
  2150. * agno - allocation group number.
  2151. * bpp - Buffer pointer to be filled in with new IAG's buffer
  2152. *
  2153. * RETURN VALUES:
  2154. * 0 - success.
  2155. * -ENOSPC - insufficient disk resources.
  2156. * -EIO - i/o error.
  2157. *
  2158. * serialization:
  2159. * AG lock held on entry/exit;
  2160. * write lock on the map is held inside;
  2161. * read lock on the map is held on successful completion;
  2162. *
  2163. * note: new iag transaction:
  2164. * . synchronously write iag;
  2165. * . write log of xtree and inode of imap;
  2166. * . commit;
  2167. * . synchronous write of xtree (right to left, bottom to top);
  2168. * . at start of logredo(): init in-memory imap with one additional iag page;
  2169. * . at end of logredo(): re-read imap inode to determine
  2170. * new imap size;
  2171. */
  2172. static int
  2173. diNewIAG(struct inomap * imap, int *iagnop, int agno, struct metapage ** mpp)
  2174. {
  2175. int rc;
  2176. int iagno, i, xlen;
  2177. struct inode *ipimap;
  2178. struct super_block *sb;
  2179. struct jfs_sb_info *sbi;
  2180. struct metapage *mp;
  2181. struct iag *iagp;
  2182. s64 xaddr = 0;
  2183. s64 blkno;
  2184. tid_t tid;
  2185. #ifdef _STILL_TO_PORT
  2186. xad_t xad;
  2187. #endif /* _STILL_TO_PORT */
  2188. struct inode *iplist[1];
  2189. /* pick up pointers to the inode map and mount inodes */
  2190. ipimap = imap->im_ipimap;
  2191. sb = ipimap->i_sb;
  2192. sbi = JFS_SBI(sb);
  2193. /* acquire the free iag lock */
  2194. IAGFREE_LOCK(imap);
  2195. /* if there are any iags on the inode map free iag list,
  2196. * allocate the iag from the head of the list.
  2197. */
  2198. if (imap->im_freeiag >= 0) {
  2199. /* pick up the iag number at the head of the list */
  2200. iagno = imap->im_freeiag;
  2201. /* determine the logical block number of the iag */
  2202. blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
  2203. } else {
  2204. /* no free iags. the inode map will have to be extented
  2205. * to include a new iag.
  2206. */
  2207. /* acquire inode map lock */
  2208. IWRITE_LOCK(ipimap, RDWRLOCK_IMAP);
  2209. if (ipimap->i_size >> L2PSIZE != imap->im_nextiag + 1) {
  2210. IWRITE_UNLOCK(ipimap);
  2211. IAGFREE_UNLOCK(imap);
  2212. jfs_error(imap->im_ipimap->i_sb,
  2213. "diNewIAG: ipimap->i_size is wrong");
  2214. return -EIO;
  2215. }
  2216. /* get the next avaliable iag number */
  2217. iagno = imap->im_nextiag;
  2218. /* make sure that we have not exceeded the maximum inode
  2219. * number limit.
  2220. */
  2221. if (iagno > (MAXIAGS - 1)) {
  2222. /* release the inode map lock */
  2223. IWRITE_UNLOCK(ipimap);
  2224. rc = -ENOSPC;
  2225. goto out;
  2226. }
  2227. /*
  2228. * synchronously append new iag page.
  2229. */
  2230. /* determine the logical address of iag page to append */
  2231. blkno = IAGTOLBLK(iagno, sbi->l2nbperpage);
  2232. /* Allocate extent for new iag page */
  2233. xlen = sbi->nbperpage;
  2234. if ((rc = dbAlloc(ipimap, 0, (s64) xlen, &xaddr))) {
  2235. /* release the inode map lock */
  2236. IWRITE_UNLOCK(ipimap);
  2237. goto out;
  2238. }
  2239. /*
  2240. * start transaction of update of the inode map
  2241. * addressing structure pointing to the new iag page;
  2242. */
  2243. tid = txBegin(sb, COMMIT_FORCE);
  2244. mutex_lock(&JFS_IP(ipimap)->commit_mutex);
  2245. /* update the inode map addressing structure to point to it */
  2246. if ((rc =
  2247. xtInsert(tid, ipimap, 0, blkno, xlen, &xaddr, 0))) {
  2248. txEnd(tid);
  2249. mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
  2250. /* Free the blocks allocated for the iag since it was
  2251. * not successfully added to the inode map
  2252. */
  2253. dbFree(ipimap, xaddr, (s64) xlen);
  2254. /* release the inode map lock */
  2255. IWRITE_UNLOCK(ipimap);
  2256. goto out;
  2257. }
  2258. /* update the inode map's inode to reflect the extension */
  2259. ipimap->i_size += PSIZE;
  2260. inode_add_bytes(ipimap, PSIZE);
  2261. /* assign a buffer for the page */
  2262. mp = get_metapage(ipimap, blkno, PSIZE, 0);
  2263. if (!mp) {
  2264. /*
  2265. * This is very unlikely since we just created the
  2266. * extent, but let's try to handle it correctly
  2267. */
  2268. xtTruncate(tid, ipimap, ipimap->i_size - PSIZE,
  2269. COMMIT_PWMAP);
  2270. txAbort(tid, 0);
  2271. txEnd(tid);
  2272. /* release the inode map lock */
  2273. IWRITE_UNLOCK(ipimap);
  2274. rc = -EIO;
  2275. goto out;
  2276. }
  2277. iagp = (struct iag *) mp->data;
  2278. /* init the iag */
  2279. memset(iagp, 0, sizeof(struct iag));
  2280. iagp->iagnum = cpu_to_le32(iagno);
  2281. iagp->inofreefwd = iagp->inofreeback = cpu_to_le32(-1);
  2282. iagp->extfreefwd = iagp->extfreeback = cpu_to_le32(-1);
  2283. iagp->iagfree = cpu_to_le32(-1);
  2284. iagp->nfreeinos = 0;
  2285. iagp->nfreeexts = cpu_to_le32(EXTSPERIAG);
  2286. /* initialize the free inode summary map (free extent
  2287. * summary map initialization handled by bzero).
  2288. */
  2289. for (i = 0; i < SMAPSZ; i++)
  2290. iagp->inosmap[i] = cpu_to_le32(ONES);
  2291. /*
  2292. * Write and sync the metapage
  2293. */
  2294. flush_metapage(mp);
  2295. /*
  2296. * txCommit(COMMIT_FORCE) will synchronously write address
  2297. * index pages and inode after commit in careful update order
  2298. * of address index pages (right to left, bottom up);
  2299. */
  2300. iplist[0] = ipimap;
  2301. rc = txCommit(tid, 1, &iplist[0], COMMIT_FORCE);
  2302. txEnd(tid);
  2303. mutex_unlock(&JFS_IP(ipimap)->commit_mutex);
  2304. duplicateIXtree(sb, blkno, xlen, &xaddr);
  2305. /* update the next avaliable iag number */
  2306. imap->im_nextiag += 1;
  2307. /* Add the iag to the iag free list so we don't lose the iag
  2308. * if a failure happens now.
  2309. */
  2310. imap->im_freeiag = iagno;
  2311. /* Until we have logredo working, we want the imap inode &
  2312. * control page to be up to date.
  2313. */
  2314. diSync(ipimap);
  2315. /* release the inode map lock */
  2316. IWRITE_UNLOCK(ipimap);
  2317. }
  2318. /* obtain read lock on map */
  2319. IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
  2320. /* read the iag */
  2321. if ((rc = diIAGRead(imap, iagno, &mp))) {
  2322. IREAD_UNLOCK(ipimap);
  2323. rc = -EIO;
  2324. goto out;
  2325. }
  2326. iagp = (struct iag *) mp->data;
  2327. /* remove the iag from the iag free list */
  2328. imap->im_freeiag = le32_to_cpu(iagp->iagfree);
  2329. iagp->iagfree = cpu_to_le32(-1);
  2330. /* set the return iag number and buffer pointer */
  2331. *iagnop = iagno;
  2332. *mpp = mp;
  2333. out:
  2334. /* release the iag free lock */
  2335. IAGFREE_UNLOCK(imap);
  2336. return (rc);
  2337. }
  2338. /*
  2339. * NAME: diIAGRead()
  2340. *
  2341. * FUNCTION: get the buffer for the specified iag within a fileset
  2342. * or aggregate inode map.
  2343. *
  2344. * PARAMETERS:
  2345. * imap - pointer to inode map control structure.
  2346. * iagno - iag number.
  2347. * bpp - point to buffer pointer to be filled in on successful
  2348. * exit.
  2349. *
  2350. * SERIALIZATION:
  2351. * must have read lock on imap inode
  2352. * (When called by diExtendFS, the filesystem is quiesced, therefore
  2353. * the read lock is unnecessary.)
  2354. *
  2355. * RETURN VALUES:
  2356. * 0 - success.
  2357. * -EIO - i/o error.
  2358. */
  2359. static int diIAGRead(struct inomap * imap, int iagno, struct metapage ** mpp)
  2360. {
  2361. struct inode *ipimap = imap->im_ipimap;
  2362. s64 blkno;
  2363. /* compute the logical block number of the iag. */
  2364. blkno = IAGTOLBLK(iagno, JFS_SBI(ipimap->i_sb)->l2nbperpage);
  2365. /* read the iag. */
  2366. *mpp = read_metapage(ipimap, blkno, PSIZE, 0);
  2367. if (*mpp == NULL) {
  2368. return -EIO;
  2369. }
  2370. return (0);
  2371. }
  2372. /*
  2373. * NAME: diFindFree()
  2374. *
  2375. * FUNCTION: find the first free bit in a word starting at
  2376. * the specified bit position.
  2377. *
  2378. * PARAMETERS:
  2379. * word - word to be examined.
  2380. * start - starting bit position.
  2381. *
  2382. * RETURN VALUES:
  2383. * bit position of first free bit in the word or 32 if
  2384. * no free bits were found.
  2385. */
  2386. static int diFindFree(u32 word, int start)
  2387. {
  2388. int bitno;
  2389. assert(start < 32);
  2390. /* scan the word for the first free bit. */
  2391. for (word <<= start, bitno = start; bitno < 32;
  2392. bitno++, word <<= 1) {
  2393. if ((word & HIGHORDER) == 0)
  2394. break;
  2395. }
  2396. return (bitno);
  2397. }
  2398. /*
  2399. * NAME: diUpdatePMap()
  2400. *
  2401. * FUNCTION: Update the persistent map in an IAG for the allocation or
  2402. * freeing of the specified inode.
  2403. *
  2404. * PRE CONDITIONS: Working map has already been updated for allocate.
  2405. *
  2406. * PARAMETERS:
  2407. * ipimap - Incore inode map inode
  2408. * inum - Number of inode to mark in permanent map
  2409. * is_free - If 'true' indicates inode should be marked freed, otherwise
  2410. * indicates inode should be marked allocated.
  2411. *
  2412. * RETURN VALUES:
  2413. * 0 for success
  2414. */
  2415. int
  2416. diUpdatePMap(struct inode *ipimap,
  2417. unsigned long inum, bool is_free, struct tblock * tblk)
  2418. {
  2419. int rc;
  2420. struct iag *iagp;
  2421. struct metapage *mp;
  2422. int iagno, ino, extno, bitno;
  2423. struct inomap *imap;
  2424. u32 mask;
  2425. struct jfs_log *log;
  2426. int lsn, difft, diffp;
  2427. unsigned long flags;
  2428. imap = JFS_IP(ipimap)->i_imap;
  2429. /* get the iag number containing the inode */
  2430. iagno = INOTOIAG(inum);
  2431. /* make sure that the iag is contained within the map */
  2432. if (iagno >= imap->im_nextiag) {
  2433. jfs_error(ipimap->i_sb,
  2434. "diUpdatePMap: the iag is outside the map");
  2435. return -EIO;
  2436. }
  2437. /* read the iag */
  2438. IREAD_LOCK(ipimap, RDWRLOCK_IMAP);
  2439. rc = diIAGRead(imap, iagno, &mp);
  2440. IREAD_UNLOCK(ipimap);
  2441. if (rc)
  2442. return (rc);
  2443. metapage_wait_for_io(mp);
  2444. iagp = (struct iag *) mp->data;
  2445. /* get the inode number and extent number of the inode within
  2446. * the iag and the inode number within the extent.
  2447. */
  2448. ino = inum & (INOSPERIAG - 1);
  2449. extno = ino >> L2INOSPEREXT;
  2450. bitno = ino & (INOSPEREXT - 1);
  2451. mask = HIGHORDER >> bitno;
  2452. /*
  2453. * mark the inode free in persistent map:
  2454. */
  2455. if (is_free) {
  2456. /* The inode should have been allocated both in working
  2457. * map and in persistent map;
  2458. * the inode will be freed from working map at the release
  2459. * of last reference release;
  2460. */
  2461. if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
  2462. jfs_error(ipimap->i_sb,
  2463. "diUpdatePMap: inode %ld not marked as "
  2464. "allocated in wmap!", inum);
  2465. }
  2466. if (!(le32_to_cpu(iagp->pmap[extno]) & mask)) {
  2467. jfs_error(ipimap->i_sb,
  2468. "diUpdatePMap: inode %ld not marked as "
  2469. "allocated in pmap!", inum);
  2470. }
  2471. /* update the bitmap for the extent of the freed inode */
  2472. iagp->pmap[extno] &= cpu_to_le32(~mask);
  2473. }
  2474. /*
  2475. * mark the inode allocated in persistent map:
  2476. */
  2477. else {
  2478. /* The inode should be already allocated in the working map
  2479. * and should be free in persistent map;
  2480. */
  2481. if (!(le32_to_cpu(iagp->wmap[extno]) & mask)) {
  2482. release_metapage(mp);
  2483. jfs_error(ipimap->i_sb,
  2484. "diUpdatePMap: the inode is not allocated in "
  2485. "the working map");
  2486. return -EIO;
  2487. }
  2488. if ((le32_to_cpu(iagp->pmap[extno]) & mask) != 0) {
  2489. release_metapage(mp);
  2490. jfs_error(ipimap->i_sb,
  2491. "diUpdatePMap: the inode is not free in the "
  2492. "persistent map");
  2493. return -EIO;
  2494. }
  2495. /* update the bitmap for the extent of the allocated inode */
  2496. iagp->pmap[extno] |= cpu_to_le32(mask);
  2497. }
  2498. /*
  2499. * update iag lsn
  2500. */
  2501. lsn = tblk->lsn;
  2502. log = JFS_SBI(tblk->sb)->log;
  2503. LOGSYNC_LOCK(log, flags);
  2504. if (mp->lsn != 0) {
  2505. /* inherit older/smaller lsn */
  2506. logdiff(difft, lsn, log);
  2507. logdiff(diffp, mp->lsn, log);
  2508. if (difft < diffp) {
  2509. mp->lsn = lsn;
  2510. /* move mp after tblock in logsync list */
  2511. list_move(&mp->synclist, &tblk->synclist);
  2512. }
  2513. /* inherit younger/larger clsn */
  2514. assert(mp->clsn);
  2515. logdiff(difft, tblk->clsn, log);
  2516. logdiff(diffp, mp->clsn, log);
  2517. if (difft > diffp)
  2518. mp->clsn = tblk->clsn;
  2519. } else {
  2520. mp->log = log;
  2521. mp->lsn = lsn;
  2522. /* insert mp after tblock in logsync list */
  2523. log->count++;
  2524. list_add(&mp->synclist, &tblk->synclist);
  2525. mp->clsn = tblk->clsn;
  2526. }
  2527. LOGSYNC_UNLOCK(log, flags);
  2528. write_metapage(mp);
  2529. return (0);
  2530. }
  2531. /*
  2532. * diExtendFS()
  2533. *
  2534. * function: update imap for extendfs();
  2535. *
  2536. * note: AG size has been increased s.t. each k old contiguous AGs are
  2537. * coalesced into a new AG;
  2538. */
  2539. int diExtendFS(struct inode *ipimap, struct inode *ipbmap)
  2540. {
  2541. int rc, rcx = 0;
  2542. struct inomap *imap = JFS_IP(ipimap)->i_imap;
  2543. struct iag *iagp = NULL, *hiagp = NULL;
  2544. struct bmap *mp = JFS_SBI(ipbmap->i_sb)->bmap;
  2545. struct metapage *bp, *hbp;
  2546. int i, n, head;
  2547. int numinos, xnuminos = 0, xnumfree = 0;
  2548. s64 agstart;
  2549. jfs_info("diExtendFS: nextiag:%d numinos:%d numfree:%d",
  2550. imap->im_nextiag, atomic_read(&imap->im_numinos),
  2551. atomic_read(&imap->im_numfree));
  2552. /*
  2553. * reconstruct imap
  2554. *
  2555. * coalesce contiguous k (newAGSize/oldAGSize) AGs;
  2556. * i.e., (AGi, ..., AGj) where i = k*n and j = k*(n+1) - 1 to AGn;
  2557. * note: new AG size = old AG size * (2**x).
  2558. */
  2559. /* init per AG control information im_agctl[] */
  2560. for (i = 0; i < MAXAG; i++) {
  2561. imap->im_agctl[i].inofree = -1;
  2562. imap->im_agctl[i].extfree = -1;
  2563. imap->im_agctl[i].numinos = 0; /* number of backed inodes */
  2564. imap->im_agctl[i].numfree = 0; /* number of free backed inodes */
  2565. }
  2566. /*
  2567. * process each iag page of the map.
  2568. *
  2569. * rebuild AG Free Inode List, AG Free Inode Extent List;
  2570. */
  2571. for (i = 0; i < imap->im_nextiag; i++) {
  2572. if ((rc = diIAGRead(imap, i, &bp))) {
  2573. rcx = rc;
  2574. continue;
  2575. }
  2576. iagp = (struct iag *) bp->data;
  2577. if (le32_to_cpu(iagp->iagnum) != i) {
  2578. release_metapage(bp);
  2579. jfs_error(ipimap->i_sb,
  2580. "diExtendFs: unexpected value of iagnum");
  2581. return -EIO;
  2582. }
  2583. /* leave free iag in the free iag list */
  2584. if (iagp->nfreeexts == cpu_to_le32(EXTSPERIAG)) {
  2585. release_metapage(bp);
  2586. continue;
  2587. }
  2588. /* agstart that computes to the same ag is treated as same; */
  2589. agstart = le64_to_cpu(iagp->agstart);
  2590. /* iagp->agstart = agstart & ~(mp->db_agsize - 1); */
  2591. n = agstart >> mp->db_agl2size;
  2592. /* compute backed inodes */
  2593. numinos = (EXTSPERIAG - le32_to_cpu(iagp->nfreeexts))
  2594. << L2INOSPEREXT;
  2595. if (numinos > 0) {
  2596. /* merge AG backed inodes */
  2597. imap->im_agctl[n].numinos += numinos;
  2598. xnuminos += numinos;
  2599. }
  2600. /* if any backed free inodes, insert at AG free inode list */
  2601. if ((int) le32_to_cpu(iagp->nfreeinos) > 0) {
  2602. if ((head = imap->im_agctl[n].inofree) == -1) {
  2603. iagp->inofreefwd = cpu_to_le32(-1);
  2604. iagp->inofreeback = cpu_to_le32(-1);
  2605. } else {
  2606. if ((rc = diIAGRead(imap, head, &hbp))) {
  2607. rcx = rc;
  2608. goto nextiag;
  2609. }
  2610. hiagp = (struct iag *) hbp->data;
  2611. hiagp->inofreeback = iagp->iagnum;
  2612. iagp->inofreefwd = cpu_to_le32(head);
  2613. iagp->inofreeback = cpu_to_le32(-1);
  2614. write_metapage(hbp);
  2615. }
  2616. imap->im_agctl[n].inofree =
  2617. le32_to_cpu(iagp->iagnum);
  2618. /* merge AG backed free inodes */
  2619. imap->im_agctl[n].numfree +=
  2620. le32_to_cpu(iagp->nfreeinos);
  2621. xnumfree += le32_to_cpu(iagp->nfreeinos);
  2622. }
  2623. /* if any free extents, insert at AG free extent list */
  2624. if (le32_to_cpu(iagp->nfreeexts) > 0) {
  2625. if ((head = imap->im_agctl[n].extfree) == -1) {
  2626. iagp->extfreefwd = cpu_to_le32(-1);
  2627. iagp->extfreeback = cpu_to_le32(-1);
  2628. } else {
  2629. if ((rc = diIAGRead(imap, head, &hbp))) {
  2630. rcx = rc;
  2631. goto nextiag;
  2632. }
  2633. hiagp = (struct iag *) hbp->data;
  2634. hiagp->extfreeback = iagp->iagnum;
  2635. iagp->extfreefwd = cpu_to_le32(head);
  2636. iagp->extfreeback = cpu_to_le32(-1);
  2637. write_metapage(hbp);
  2638. }
  2639. imap->im_agctl[n].extfree =
  2640. le32_to_cpu(iagp->iagnum);
  2641. }
  2642. nextiag:
  2643. write_metapage(bp);
  2644. }
  2645. if (xnuminos != atomic_read(&imap->im_numinos) ||
  2646. xnumfree != atomic_read(&imap->im_numfree)) {
  2647. jfs_error(ipimap->i_sb,
  2648. "diExtendFs: numinos or numfree incorrect");
  2649. return -EIO;
  2650. }
  2651. return rcx;
  2652. }
  2653. /*
  2654. * duplicateIXtree()
  2655. *
  2656. * serialization: IWRITE_LOCK held on entry/exit
  2657. *
  2658. * note: shadow page with regular inode (rel.2);
  2659. */
  2660. static void duplicateIXtree(struct super_block *sb, s64 blkno,
  2661. int xlen, s64 *xaddr)
  2662. {
  2663. struct jfs_superblock *j_sb;
  2664. struct buffer_head *bh;
  2665. struct inode *ip;
  2666. tid_t tid;
  2667. /* if AIT2 ipmap2 is bad, do not try to update it */
  2668. if (JFS_SBI(sb)->mntflag & JFS_BAD_SAIT) /* s_flag */
  2669. return;
  2670. ip = diReadSpecial(sb, FILESYSTEM_I, 1);
  2671. if (ip == NULL) {
  2672. JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
  2673. if (readSuper(sb, &bh))
  2674. return;
  2675. j_sb = (struct jfs_superblock *)bh->b_data;
  2676. j_sb->s_flag |= cpu_to_le32(JFS_BAD_SAIT);
  2677. mark_buffer_dirty(bh);
  2678. sync_dirty_buffer(bh);
  2679. brelse(bh);
  2680. return;
  2681. }
  2682. /* start transaction */
  2683. tid = txBegin(sb, COMMIT_FORCE);
  2684. /* update the inode map addressing structure to point to it */
  2685. if (xtInsert(tid, ip, 0, blkno, xlen, xaddr, 0)) {
  2686. JFS_SBI(sb)->mntflag |= JFS_BAD_SAIT;
  2687. txAbort(tid, 1);
  2688. goto cleanup;
  2689. }
  2690. /* update the inode map's inode to reflect the extension */
  2691. ip->i_size += PSIZE;
  2692. inode_add_bytes(ip, PSIZE);
  2693. txCommit(tid, 1, &ip, COMMIT_FORCE);
  2694. cleanup:
  2695. txEnd(tid);
  2696. diFreeSpecial(ip);
  2697. }
  2698. /*
  2699. * NAME: copy_from_dinode()
  2700. *
  2701. * FUNCTION: Copies inode info from disk inode to in-memory inode
  2702. *
  2703. * RETURN VALUES:
  2704. * 0 - success
  2705. * -ENOMEM - insufficient memory
  2706. */
  2707. static int copy_from_dinode(struct dinode * dip, struct inode *ip)
  2708. {
  2709. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  2710. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  2711. jfs_ip->fileset = le32_to_cpu(dip->di_fileset);
  2712. jfs_ip->mode2 = le32_to_cpu(dip->di_mode);
  2713. ip->i_mode = le32_to_cpu(dip->di_mode) & 0xffff;
  2714. if (sbi->umask != -1) {
  2715. ip->i_mode = (ip->i_mode & ~0777) | (0777 & ~sbi->umask);
  2716. /* For directories, add x permission if r is allowed by umask */
  2717. if (S_ISDIR(ip->i_mode)) {
  2718. if (ip->i_mode & 0400)
  2719. ip->i_mode |= 0100;
  2720. if (ip->i_mode & 0040)
  2721. ip->i_mode |= 0010;
  2722. if (ip->i_mode & 0004)
  2723. ip->i_mode |= 0001;
  2724. }
  2725. }
  2726. ip->i_nlink = le32_to_cpu(dip->di_nlink);
  2727. jfs_ip->saved_uid = le32_to_cpu(dip->di_uid);
  2728. if (sbi->uid == -1)
  2729. ip->i_uid = jfs_ip->saved_uid;
  2730. else {
  2731. ip->i_uid = sbi->uid;
  2732. }
  2733. jfs_ip->saved_gid = le32_to_cpu(dip->di_gid);
  2734. if (sbi->gid == -1)
  2735. ip->i_gid = jfs_ip->saved_gid;
  2736. else {
  2737. ip->i_gid = sbi->gid;
  2738. }
  2739. ip->i_size = le64_to_cpu(dip->di_size);
  2740. ip->i_atime.tv_sec = le32_to_cpu(dip->di_atime.tv_sec);
  2741. ip->i_atime.tv_nsec = le32_to_cpu(dip->di_atime.tv_nsec);
  2742. ip->i_mtime.tv_sec = le32_to_cpu(dip->di_mtime.tv_sec);
  2743. ip->i_mtime.tv_nsec = le32_to_cpu(dip->di_mtime.tv_nsec);
  2744. ip->i_ctime.tv_sec = le32_to_cpu(dip->di_ctime.tv_sec);
  2745. ip->i_ctime.tv_nsec = le32_to_cpu(dip->di_ctime.tv_nsec);
  2746. ip->i_blocks = LBLK2PBLK(ip->i_sb, le64_to_cpu(dip->di_nblocks));
  2747. ip->i_generation = le32_to_cpu(dip->di_gen);
  2748. jfs_ip->ixpxd = dip->di_ixpxd; /* in-memory pxd's are little-endian */
  2749. jfs_ip->acl = dip->di_acl; /* as are dxd's */
  2750. jfs_ip->ea = dip->di_ea;
  2751. jfs_ip->next_index = le32_to_cpu(dip->di_next_index);
  2752. jfs_ip->otime = le32_to_cpu(dip->di_otime.tv_sec);
  2753. jfs_ip->acltype = le32_to_cpu(dip->di_acltype);
  2754. if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
  2755. jfs_ip->dev = le32_to_cpu(dip->di_rdev);
  2756. ip->i_rdev = new_decode_dev(jfs_ip->dev);
  2757. }
  2758. if (S_ISDIR(ip->i_mode)) {
  2759. memcpy(&jfs_ip->i_dirtable, &dip->di_dirtable, 384);
  2760. } else if (S_ISREG(ip->i_mode) || S_ISLNK(ip->i_mode)) {
  2761. memcpy(&jfs_ip->i_xtroot, &dip->di_xtroot, 288);
  2762. } else
  2763. memcpy(&jfs_ip->i_inline_ea, &dip->di_inlineea, 128);
  2764. /* Zero the in-memory-only stuff */
  2765. jfs_ip->cflag = 0;
  2766. jfs_ip->btindex = 0;
  2767. jfs_ip->btorder = 0;
  2768. jfs_ip->bxflag = 0;
  2769. jfs_ip->blid = 0;
  2770. jfs_ip->atlhead = 0;
  2771. jfs_ip->atltail = 0;
  2772. jfs_ip->xtlid = 0;
  2773. return (0);
  2774. }
  2775. /*
  2776. * NAME: copy_to_dinode()
  2777. *
  2778. * FUNCTION: Copies inode info from in-memory inode to disk inode
  2779. */
  2780. static void copy_to_dinode(struct dinode * dip, struct inode *ip)
  2781. {
  2782. struct jfs_inode_info *jfs_ip = JFS_IP(ip);
  2783. struct jfs_sb_info *sbi = JFS_SBI(ip->i_sb);
  2784. dip->di_fileset = cpu_to_le32(jfs_ip->fileset);
  2785. dip->di_inostamp = cpu_to_le32(sbi->inostamp);
  2786. dip->di_number = cpu_to_le32(ip->i_ino);
  2787. dip->di_gen = cpu_to_le32(ip->i_generation);
  2788. dip->di_size = cpu_to_le64(ip->i_size);
  2789. dip->di_nblocks = cpu_to_le64(PBLK2LBLK(ip->i_sb, ip->i_blocks));
  2790. dip->di_nlink = cpu_to_le32(ip->i_nlink);
  2791. if (sbi->uid == -1)
  2792. dip->di_uid = cpu_to_le32(ip->i_uid);
  2793. else
  2794. dip->di_uid = cpu_to_le32(jfs_ip->saved_uid);
  2795. if (sbi->gid == -1)
  2796. dip->di_gid = cpu_to_le32(ip->i_gid);
  2797. else
  2798. dip->di_gid = cpu_to_le32(jfs_ip->saved_gid);
  2799. /*
  2800. * mode2 is only needed for storing the higher order bits.
  2801. * Trust i_mode for the lower order ones
  2802. */
  2803. if (sbi->umask == -1)
  2804. dip->di_mode = cpu_to_le32((jfs_ip->mode2 & 0xffff0000) |
  2805. ip->i_mode);
  2806. else /* Leave the original permissions alone */
  2807. dip->di_mode = cpu_to_le32(jfs_ip->mode2);
  2808. dip->di_atime.tv_sec = cpu_to_le32(ip->i_atime.tv_sec);
  2809. dip->di_atime.tv_nsec = cpu_to_le32(ip->i_atime.tv_nsec);
  2810. dip->di_ctime.tv_sec = cpu_to_le32(ip->i_ctime.tv_sec);
  2811. dip->di_ctime.tv_nsec = cpu_to_le32(ip->i_ctime.tv_nsec);
  2812. dip->di_mtime.tv_sec = cpu_to_le32(ip->i_mtime.tv_sec);
  2813. dip->di_mtime.tv_nsec = cpu_to_le32(ip->i_mtime.tv_nsec);
  2814. dip->di_ixpxd = jfs_ip->ixpxd; /* in-memory pxd's are little-endian */
  2815. dip->di_acl = jfs_ip->acl; /* as are dxd's */
  2816. dip->di_ea = jfs_ip->ea;
  2817. dip->di_next_index = cpu_to_le32(jfs_ip->next_index);
  2818. dip->di_otime.tv_sec = cpu_to_le32(jfs_ip->otime);
  2819. dip->di_otime.tv_nsec = 0;
  2820. dip->di_acltype = cpu_to_le32(jfs_ip->acltype);
  2821. if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
  2822. dip->di_rdev = cpu_to_le32(jfs_ip->dev);
  2823. }