jfs_imap.c 87 KB

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