jfs_imap.c 84 KB

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