jfs_imap.c 84 KB

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