jfs_imap.c 88 KB

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