xfs_inode.c 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * 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 the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <linux/log2.h>
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_types.h"
  22. #include "xfs_bit.h"
  23. #include "xfs_log.h"
  24. #include "xfs_inum.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_trans_priv.h"
  27. #include "xfs_sb.h"
  28. #include "xfs_ag.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_attr_sf.h"
  34. #include "xfs_dinode.h"
  35. #include "xfs_inode.h"
  36. #include "xfs_buf_item.h"
  37. #include "xfs_inode_item.h"
  38. #include "xfs_btree.h"
  39. #include "xfs_alloc.h"
  40. #include "xfs_ialloc.h"
  41. #include "xfs_bmap.h"
  42. #include "xfs_error.h"
  43. #include "xfs_utils.h"
  44. #include "xfs_quota.h"
  45. #include "xfs_filestream.h"
  46. #include "xfs_vnodeops.h"
  47. #include "xfs_trace.h"
  48. kmem_zone_t *xfs_ifork_zone;
  49. kmem_zone_t *xfs_inode_zone;
  50. /*
  51. * Used in xfs_itruncate_extents(). This is the maximum number of extents
  52. * freed from a file in a single transaction.
  53. */
  54. #define XFS_ITRUNC_MAX_EXTENTS 2
  55. STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
  56. STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
  57. STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
  58. STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
  59. #ifdef DEBUG
  60. /*
  61. * Make sure that the extents in the given memory buffer
  62. * are valid.
  63. */
  64. STATIC void
  65. xfs_validate_extents(
  66. xfs_ifork_t *ifp,
  67. int nrecs,
  68. xfs_exntfmt_t fmt)
  69. {
  70. xfs_bmbt_irec_t irec;
  71. xfs_bmbt_rec_host_t rec;
  72. int i;
  73. for (i = 0; i < nrecs; i++) {
  74. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  75. rec.l0 = get_unaligned(&ep->l0);
  76. rec.l1 = get_unaligned(&ep->l1);
  77. xfs_bmbt_get_all(&rec, &irec);
  78. if (fmt == XFS_EXTFMT_NOSTATE)
  79. ASSERT(irec.br_state == XFS_EXT_NORM);
  80. }
  81. }
  82. #else /* DEBUG */
  83. #define xfs_validate_extents(ifp, nrecs, fmt)
  84. #endif /* DEBUG */
  85. /*
  86. * Check that none of the inode's in the buffer have a next
  87. * unlinked field of 0.
  88. */
  89. #if defined(DEBUG)
  90. void
  91. xfs_inobp_check(
  92. xfs_mount_t *mp,
  93. xfs_buf_t *bp)
  94. {
  95. int i;
  96. int j;
  97. xfs_dinode_t *dip;
  98. j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
  99. for (i = 0; i < j; i++) {
  100. dip = (xfs_dinode_t *)xfs_buf_offset(bp,
  101. i * mp->m_sb.sb_inodesize);
  102. if (!dip->di_next_unlinked) {
  103. xfs_alert(mp,
  104. "Detected bogus zero next_unlinked field in incore inode buffer 0x%p.",
  105. bp);
  106. ASSERT(dip->di_next_unlinked);
  107. }
  108. }
  109. }
  110. #endif
  111. /*
  112. * Find the buffer associated with the given inode map
  113. * We do basic validation checks on the buffer once it has been
  114. * retrieved from disk.
  115. */
  116. STATIC int
  117. xfs_imap_to_bp(
  118. xfs_mount_t *mp,
  119. xfs_trans_t *tp,
  120. struct xfs_imap *imap,
  121. xfs_buf_t **bpp,
  122. uint buf_flags,
  123. uint iget_flags)
  124. {
  125. int error;
  126. int i;
  127. int ni;
  128. xfs_buf_t *bp;
  129. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
  130. (int)imap->im_len, buf_flags, &bp);
  131. if (error) {
  132. if (error != EAGAIN) {
  133. xfs_warn(mp,
  134. "%s: xfs_trans_read_buf() returned error %d.",
  135. __func__, error);
  136. } else {
  137. ASSERT(buf_flags & XBF_TRYLOCK);
  138. }
  139. return error;
  140. }
  141. /*
  142. * Validate the magic number and version of every inode in the buffer
  143. * (if DEBUG kernel) or the first inode in the buffer, otherwise.
  144. */
  145. #ifdef DEBUG
  146. ni = BBTOB(imap->im_len) >> mp->m_sb.sb_inodelog;
  147. #else /* usual case */
  148. ni = 1;
  149. #endif
  150. for (i = 0; i < ni; i++) {
  151. int di_ok;
  152. xfs_dinode_t *dip;
  153. dip = (xfs_dinode_t *)xfs_buf_offset(bp,
  154. (i << mp->m_sb.sb_inodelog));
  155. di_ok = dip->di_magic == cpu_to_be16(XFS_DINODE_MAGIC) &&
  156. XFS_DINODE_GOOD_VERSION(dip->di_version);
  157. if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
  158. XFS_ERRTAG_ITOBP_INOTOBP,
  159. XFS_RANDOM_ITOBP_INOTOBP))) {
  160. if (iget_flags & XFS_IGET_UNTRUSTED) {
  161. xfs_trans_brelse(tp, bp);
  162. return XFS_ERROR(EINVAL);
  163. }
  164. XFS_CORRUPTION_ERROR("xfs_imap_to_bp",
  165. XFS_ERRLEVEL_HIGH, mp, dip);
  166. #ifdef DEBUG
  167. xfs_emerg(mp,
  168. "bad inode magic/vsn daddr %lld #%d (magic=%x)",
  169. (unsigned long long)imap->im_blkno, i,
  170. be16_to_cpu(dip->di_magic));
  171. ASSERT(0);
  172. #endif
  173. xfs_trans_brelse(tp, bp);
  174. return XFS_ERROR(EFSCORRUPTED);
  175. }
  176. }
  177. xfs_inobp_check(mp, bp);
  178. *bpp = bp;
  179. return 0;
  180. }
  181. /*
  182. * This routine is called to map an inode number within a file
  183. * system to the buffer containing the on-disk version of the
  184. * inode. It returns a pointer to the buffer containing the
  185. * on-disk inode in the bpp parameter, and in the dip parameter
  186. * it returns a pointer to the on-disk inode within that buffer.
  187. *
  188. * If a non-zero error is returned, then the contents of bpp and
  189. * dipp are undefined.
  190. *
  191. * Use xfs_imap() to determine the size and location of the
  192. * buffer to read from disk.
  193. */
  194. int
  195. xfs_inotobp(
  196. xfs_mount_t *mp,
  197. xfs_trans_t *tp,
  198. xfs_ino_t ino,
  199. xfs_dinode_t **dipp,
  200. xfs_buf_t **bpp,
  201. int *offset,
  202. uint imap_flags)
  203. {
  204. struct xfs_imap imap;
  205. xfs_buf_t *bp;
  206. int error;
  207. imap.im_blkno = 0;
  208. error = xfs_imap(mp, tp, ino, &imap, imap_flags);
  209. if (error)
  210. return error;
  211. error = xfs_imap_to_bp(mp, tp, &imap, &bp, 0, imap_flags);
  212. if (error)
  213. return error;
  214. *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
  215. *bpp = bp;
  216. *offset = imap.im_boffset;
  217. return 0;
  218. }
  219. /*
  220. * This routine is called to map an inode to the buffer containing
  221. * the on-disk version of the inode. It returns a pointer to the
  222. * buffer containing the on-disk inode in the bpp parameter, and in
  223. * the dip parameter it returns a pointer to the on-disk inode within
  224. * that buffer.
  225. *
  226. * If a non-zero error is returned, then the contents of bpp and
  227. * dipp are undefined.
  228. *
  229. * The inode is expected to already been mapped to its buffer and read
  230. * in once, thus we can use the mapping information stored in the inode
  231. * rather than calling xfs_imap(). This allows us to avoid the overhead
  232. * of looking at the inode btree for small block file systems
  233. * (see xfs_imap()).
  234. */
  235. int
  236. xfs_itobp(
  237. xfs_mount_t *mp,
  238. xfs_trans_t *tp,
  239. xfs_inode_t *ip,
  240. xfs_dinode_t **dipp,
  241. xfs_buf_t **bpp,
  242. uint buf_flags)
  243. {
  244. xfs_buf_t *bp;
  245. int error;
  246. ASSERT(ip->i_imap.im_blkno != 0);
  247. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp, buf_flags, 0);
  248. if (error)
  249. return error;
  250. if (!bp) {
  251. ASSERT(buf_flags & XBF_TRYLOCK);
  252. ASSERT(tp == NULL);
  253. *bpp = NULL;
  254. return EAGAIN;
  255. }
  256. *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
  257. *bpp = bp;
  258. return 0;
  259. }
  260. /*
  261. * Move inode type and inode format specific information from the
  262. * on-disk inode to the in-core inode. For fifos, devs, and sockets
  263. * this means set if_rdev to the proper value. For files, directories,
  264. * and symlinks this means to bring in the in-line data or extent
  265. * pointers. For a file in B-tree format, only the root is immediately
  266. * brought in-core. The rest will be in-lined in if_extents when it
  267. * is first referenced (see xfs_iread_extents()).
  268. */
  269. STATIC int
  270. xfs_iformat(
  271. xfs_inode_t *ip,
  272. xfs_dinode_t *dip)
  273. {
  274. xfs_attr_shortform_t *atp;
  275. int size;
  276. int error = 0;
  277. xfs_fsize_t di_size;
  278. if (unlikely(be32_to_cpu(dip->di_nextents) +
  279. be16_to_cpu(dip->di_anextents) >
  280. be64_to_cpu(dip->di_nblocks))) {
  281. xfs_warn(ip->i_mount,
  282. "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
  283. (unsigned long long)ip->i_ino,
  284. (int)(be32_to_cpu(dip->di_nextents) +
  285. be16_to_cpu(dip->di_anextents)),
  286. (unsigned long long)
  287. be64_to_cpu(dip->di_nblocks));
  288. XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
  289. ip->i_mount, dip);
  290. return XFS_ERROR(EFSCORRUPTED);
  291. }
  292. if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
  293. xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
  294. (unsigned long long)ip->i_ino,
  295. dip->di_forkoff);
  296. XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
  297. ip->i_mount, dip);
  298. return XFS_ERROR(EFSCORRUPTED);
  299. }
  300. if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
  301. !ip->i_mount->m_rtdev_targp)) {
  302. xfs_warn(ip->i_mount,
  303. "corrupt dinode %Lu, has realtime flag set.",
  304. ip->i_ino);
  305. XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
  306. XFS_ERRLEVEL_LOW, ip->i_mount, dip);
  307. return XFS_ERROR(EFSCORRUPTED);
  308. }
  309. switch (ip->i_d.di_mode & S_IFMT) {
  310. case S_IFIFO:
  311. case S_IFCHR:
  312. case S_IFBLK:
  313. case S_IFSOCK:
  314. if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
  315. XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
  316. ip->i_mount, dip);
  317. return XFS_ERROR(EFSCORRUPTED);
  318. }
  319. ip->i_d.di_size = 0;
  320. ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
  321. break;
  322. case S_IFREG:
  323. case S_IFLNK:
  324. case S_IFDIR:
  325. switch (dip->di_format) {
  326. case XFS_DINODE_FMT_LOCAL:
  327. /*
  328. * no local regular files yet
  329. */
  330. if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
  331. xfs_warn(ip->i_mount,
  332. "corrupt inode %Lu (local format for regular file).",
  333. (unsigned long long) ip->i_ino);
  334. XFS_CORRUPTION_ERROR("xfs_iformat(4)",
  335. XFS_ERRLEVEL_LOW,
  336. ip->i_mount, dip);
  337. return XFS_ERROR(EFSCORRUPTED);
  338. }
  339. di_size = be64_to_cpu(dip->di_size);
  340. if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
  341. xfs_warn(ip->i_mount,
  342. "corrupt inode %Lu (bad size %Ld for local inode).",
  343. (unsigned long long) ip->i_ino,
  344. (long long) di_size);
  345. XFS_CORRUPTION_ERROR("xfs_iformat(5)",
  346. XFS_ERRLEVEL_LOW,
  347. ip->i_mount, dip);
  348. return XFS_ERROR(EFSCORRUPTED);
  349. }
  350. size = (int)di_size;
  351. error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
  352. break;
  353. case XFS_DINODE_FMT_EXTENTS:
  354. error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
  355. break;
  356. case XFS_DINODE_FMT_BTREE:
  357. error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
  358. break;
  359. default:
  360. XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
  361. ip->i_mount);
  362. return XFS_ERROR(EFSCORRUPTED);
  363. }
  364. break;
  365. default:
  366. XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
  367. return XFS_ERROR(EFSCORRUPTED);
  368. }
  369. if (error) {
  370. return error;
  371. }
  372. if (!XFS_DFORK_Q(dip))
  373. return 0;
  374. ASSERT(ip->i_afp == NULL);
  375. ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
  376. switch (dip->di_aformat) {
  377. case XFS_DINODE_FMT_LOCAL:
  378. atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
  379. size = be16_to_cpu(atp->hdr.totsize);
  380. if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
  381. xfs_warn(ip->i_mount,
  382. "corrupt inode %Lu (bad attr fork size %Ld).",
  383. (unsigned long long) ip->i_ino,
  384. (long long) size);
  385. XFS_CORRUPTION_ERROR("xfs_iformat(8)",
  386. XFS_ERRLEVEL_LOW,
  387. ip->i_mount, dip);
  388. return XFS_ERROR(EFSCORRUPTED);
  389. }
  390. error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
  391. break;
  392. case XFS_DINODE_FMT_EXTENTS:
  393. error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
  394. break;
  395. case XFS_DINODE_FMT_BTREE:
  396. error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
  397. break;
  398. default:
  399. error = XFS_ERROR(EFSCORRUPTED);
  400. break;
  401. }
  402. if (error) {
  403. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  404. ip->i_afp = NULL;
  405. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  406. }
  407. return error;
  408. }
  409. /*
  410. * The file is in-lined in the on-disk inode.
  411. * If it fits into if_inline_data, then copy
  412. * it there, otherwise allocate a buffer for it
  413. * and copy the data there. Either way, set
  414. * if_data to point at the data.
  415. * If we allocate a buffer for the data, make
  416. * sure that its size is a multiple of 4 and
  417. * record the real size in i_real_bytes.
  418. */
  419. STATIC int
  420. xfs_iformat_local(
  421. xfs_inode_t *ip,
  422. xfs_dinode_t *dip,
  423. int whichfork,
  424. int size)
  425. {
  426. xfs_ifork_t *ifp;
  427. int real_size;
  428. /*
  429. * If the size is unreasonable, then something
  430. * is wrong and we just bail out rather than crash in
  431. * kmem_alloc() or memcpy() below.
  432. */
  433. if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  434. xfs_warn(ip->i_mount,
  435. "corrupt inode %Lu (bad size %d for local fork, size = %d).",
  436. (unsigned long long) ip->i_ino, size,
  437. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
  438. XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
  439. ip->i_mount, dip);
  440. return XFS_ERROR(EFSCORRUPTED);
  441. }
  442. ifp = XFS_IFORK_PTR(ip, whichfork);
  443. real_size = 0;
  444. if (size == 0)
  445. ifp->if_u1.if_data = NULL;
  446. else if (size <= sizeof(ifp->if_u2.if_inline_data))
  447. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  448. else {
  449. real_size = roundup(size, 4);
  450. ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
  451. }
  452. ifp->if_bytes = size;
  453. ifp->if_real_bytes = real_size;
  454. if (size)
  455. memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
  456. ifp->if_flags &= ~XFS_IFEXTENTS;
  457. ifp->if_flags |= XFS_IFINLINE;
  458. return 0;
  459. }
  460. /*
  461. * The file consists of a set of extents all
  462. * of which fit into the on-disk inode.
  463. * If there are few enough extents to fit into
  464. * the if_inline_ext, then copy them there.
  465. * Otherwise allocate a buffer for them and copy
  466. * them into it. Either way, set if_extents
  467. * to point at the extents.
  468. */
  469. STATIC int
  470. xfs_iformat_extents(
  471. xfs_inode_t *ip,
  472. xfs_dinode_t *dip,
  473. int whichfork)
  474. {
  475. xfs_bmbt_rec_t *dp;
  476. xfs_ifork_t *ifp;
  477. int nex;
  478. int size;
  479. int i;
  480. ifp = XFS_IFORK_PTR(ip, whichfork);
  481. nex = XFS_DFORK_NEXTENTS(dip, whichfork);
  482. size = nex * (uint)sizeof(xfs_bmbt_rec_t);
  483. /*
  484. * If the number of extents is unreasonable, then something
  485. * is wrong and we just bail out rather than crash in
  486. * kmem_alloc() or memcpy() below.
  487. */
  488. if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  489. xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
  490. (unsigned long long) ip->i_ino, nex);
  491. XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
  492. ip->i_mount, dip);
  493. return XFS_ERROR(EFSCORRUPTED);
  494. }
  495. ifp->if_real_bytes = 0;
  496. if (nex == 0)
  497. ifp->if_u1.if_extents = NULL;
  498. else if (nex <= XFS_INLINE_EXTS)
  499. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  500. else
  501. xfs_iext_add(ifp, 0, nex);
  502. ifp->if_bytes = size;
  503. if (size) {
  504. dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
  505. xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
  506. for (i = 0; i < nex; i++, dp++) {
  507. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  508. ep->l0 = get_unaligned_be64(&dp->l0);
  509. ep->l1 = get_unaligned_be64(&dp->l1);
  510. }
  511. XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
  512. if (whichfork != XFS_DATA_FORK ||
  513. XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
  514. if (unlikely(xfs_check_nostate_extents(
  515. ifp, 0, nex))) {
  516. XFS_ERROR_REPORT("xfs_iformat_extents(2)",
  517. XFS_ERRLEVEL_LOW,
  518. ip->i_mount);
  519. return XFS_ERROR(EFSCORRUPTED);
  520. }
  521. }
  522. ifp->if_flags |= XFS_IFEXTENTS;
  523. return 0;
  524. }
  525. /*
  526. * The file has too many extents to fit into
  527. * the inode, so they are in B-tree format.
  528. * Allocate a buffer for the root of the B-tree
  529. * and copy the root into it. The i_extents
  530. * field will remain NULL until all of the
  531. * extents are read in (when they are needed).
  532. */
  533. STATIC int
  534. xfs_iformat_btree(
  535. xfs_inode_t *ip,
  536. xfs_dinode_t *dip,
  537. int whichfork)
  538. {
  539. xfs_bmdr_block_t *dfp;
  540. xfs_ifork_t *ifp;
  541. /* REFERENCED */
  542. int nrecs;
  543. int size;
  544. ifp = XFS_IFORK_PTR(ip, whichfork);
  545. dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
  546. size = XFS_BMAP_BROOT_SPACE(dfp);
  547. nrecs = be16_to_cpu(dfp->bb_numrecs);
  548. /*
  549. * blow out if -- fork has less extents than can fit in
  550. * fork (fork shouldn't be a btree format), root btree
  551. * block has more records than can fit into the fork,
  552. * or the number of extents is greater than the number of
  553. * blocks.
  554. */
  555. if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
  556. XFS_IFORK_MAXEXT(ip, whichfork) ||
  557. XFS_BMDR_SPACE_CALC(nrecs) >
  558. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork) ||
  559. XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
  560. xfs_warn(ip->i_mount, "corrupt inode %Lu (btree).",
  561. (unsigned long long) ip->i_ino);
  562. XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
  563. ip->i_mount, dip);
  564. return XFS_ERROR(EFSCORRUPTED);
  565. }
  566. ifp->if_broot_bytes = size;
  567. ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
  568. ASSERT(ifp->if_broot != NULL);
  569. /*
  570. * Copy and convert from the on-disk structure
  571. * to the in-memory structure.
  572. */
  573. xfs_bmdr_to_bmbt(ip->i_mount, dfp,
  574. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
  575. ifp->if_broot, size);
  576. ifp->if_flags &= ~XFS_IFEXTENTS;
  577. ifp->if_flags |= XFS_IFBROOT;
  578. return 0;
  579. }
  580. STATIC void
  581. xfs_dinode_from_disk(
  582. xfs_icdinode_t *to,
  583. xfs_dinode_t *from)
  584. {
  585. to->di_magic = be16_to_cpu(from->di_magic);
  586. to->di_mode = be16_to_cpu(from->di_mode);
  587. to->di_version = from ->di_version;
  588. to->di_format = from->di_format;
  589. to->di_onlink = be16_to_cpu(from->di_onlink);
  590. to->di_uid = be32_to_cpu(from->di_uid);
  591. to->di_gid = be32_to_cpu(from->di_gid);
  592. to->di_nlink = be32_to_cpu(from->di_nlink);
  593. to->di_projid_lo = be16_to_cpu(from->di_projid_lo);
  594. to->di_projid_hi = be16_to_cpu(from->di_projid_hi);
  595. memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
  596. to->di_flushiter = be16_to_cpu(from->di_flushiter);
  597. to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
  598. to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
  599. to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
  600. to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
  601. to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
  602. to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
  603. to->di_size = be64_to_cpu(from->di_size);
  604. to->di_nblocks = be64_to_cpu(from->di_nblocks);
  605. to->di_extsize = be32_to_cpu(from->di_extsize);
  606. to->di_nextents = be32_to_cpu(from->di_nextents);
  607. to->di_anextents = be16_to_cpu(from->di_anextents);
  608. to->di_forkoff = from->di_forkoff;
  609. to->di_aformat = from->di_aformat;
  610. to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
  611. to->di_dmstate = be16_to_cpu(from->di_dmstate);
  612. to->di_flags = be16_to_cpu(from->di_flags);
  613. to->di_gen = be32_to_cpu(from->di_gen);
  614. }
  615. void
  616. xfs_dinode_to_disk(
  617. xfs_dinode_t *to,
  618. xfs_icdinode_t *from)
  619. {
  620. to->di_magic = cpu_to_be16(from->di_magic);
  621. to->di_mode = cpu_to_be16(from->di_mode);
  622. to->di_version = from ->di_version;
  623. to->di_format = from->di_format;
  624. to->di_onlink = cpu_to_be16(from->di_onlink);
  625. to->di_uid = cpu_to_be32(from->di_uid);
  626. to->di_gid = cpu_to_be32(from->di_gid);
  627. to->di_nlink = cpu_to_be32(from->di_nlink);
  628. to->di_projid_lo = cpu_to_be16(from->di_projid_lo);
  629. to->di_projid_hi = cpu_to_be16(from->di_projid_hi);
  630. memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
  631. to->di_flushiter = cpu_to_be16(from->di_flushiter);
  632. to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
  633. to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
  634. to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
  635. to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
  636. to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
  637. to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
  638. to->di_size = cpu_to_be64(from->di_size);
  639. to->di_nblocks = cpu_to_be64(from->di_nblocks);
  640. to->di_extsize = cpu_to_be32(from->di_extsize);
  641. to->di_nextents = cpu_to_be32(from->di_nextents);
  642. to->di_anextents = cpu_to_be16(from->di_anextents);
  643. to->di_forkoff = from->di_forkoff;
  644. to->di_aformat = from->di_aformat;
  645. to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
  646. to->di_dmstate = cpu_to_be16(from->di_dmstate);
  647. to->di_flags = cpu_to_be16(from->di_flags);
  648. to->di_gen = cpu_to_be32(from->di_gen);
  649. }
  650. STATIC uint
  651. _xfs_dic2xflags(
  652. __uint16_t di_flags)
  653. {
  654. uint flags = 0;
  655. if (di_flags & XFS_DIFLAG_ANY) {
  656. if (di_flags & XFS_DIFLAG_REALTIME)
  657. flags |= XFS_XFLAG_REALTIME;
  658. if (di_flags & XFS_DIFLAG_PREALLOC)
  659. flags |= XFS_XFLAG_PREALLOC;
  660. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  661. flags |= XFS_XFLAG_IMMUTABLE;
  662. if (di_flags & XFS_DIFLAG_APPEND)
  663. flags |= XFS_XFLAG_APPEND;
  664. if (di_flags & XFS_DIFLAG_SYNC)
  665. flags |= XFS_XFLAG_SYNC;
  666. if (di_flags & XFS_DIFLAG_NOATIME)
  667. flags |= XFS_XFLAG_NOATIME;
  668. if (di_flags & XFS_DIFLAG_NODUMP)
  669. flags |= XFS_XFLAG_NODUMP;
  670. if (di_flags & XFS_DIFLAG_RTINHERIT)
  671. flags |= XFS_XFLAG_RTINHERIT;
  672. if (di_flags & XFS_DIFLAG_PROJINHERIT)
  673. flags |= XFS_XFLAG_PROJINHERIT;
  674. if (di_flags & XFS_DIFLAG_NOSYMLINKS)
  675. flags |= XFS_XFLAG_NOSYMLINKS;
  676. if (di_flags & XFS_DIFLAG_EXTSIZE)
  677. flags |= XFS_XFLAG_EXTSIZE;
  678. if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
  679. flags |= XFS_XFLAG_EXTSZINHERIT;
  680. if (di_flags & XFS_DIFLAG_NODEFRAG)
  681. flags |= XFS_XFLAG_NODEFRAG;
  682. if (di_flags & XFS_DIFLAG_FILESTREAM)
  683. flags |= XFS_XFLAG_FILESTREAM;
  684. }
  685. return flags;
  686. }
  687. uint
  688. xfs_ip2xflags(
  689. xfs_inode_t *ip)
  690. {
  691. xfs_icdinode_t *dic = &ip->i_d;
  692. return _xfs_dic2xflags(dic->di_flags) |
  693. (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
  694. }
  695. uint
  696. xfs_dic2xflags(
  697. xfs_dinode_t *dip)
  698. {
  699. return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
  700. (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
  701. }
  702. /*
  703. * Read the disk inode attributes into the in-core inode structure.
  704. */
  705. int
  706. xfs_iread(
  707. xfs_mount_t *mp,
  708. xfs_trans_t *tp,
  709. xfs_inode_t *ip,
  710. uint iget_flags)
  711. {
  712. xfs_buf_t *bp;
  713. xfs_dinode_t *dip;
  714. int error;
  715. /*
  716. * Fill in the location information in the in-core inode.
  717. */
  718. error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, iget_flags);
  719. if (error)
  720. return error;
  721. /*
  722. * Get pointers to the on-disk inode and the buffer containing it.
  723. */
  724. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp, 0, iget_flags);
  725. if (error)
  726. return error;
  727. dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
  728. /*
  729. * If we got something that isn't an inode it means someone
  730. * (nfs or dmi) has a stale handle.
  731. */
  732. if (dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC)) {
  733. #ifdef DEBUG
  734. xfs_alert(mp,
  735. "%s: dip->di_magic (0x%x) != XFS_DINODE_MAGIC (0x%x)",
  736. __func__, be16_to_cpu(dip->di_magic), XFS_DINODE_MAGIC);
  737. #endif /* DEBUG */
  738. error = XFS_ERROR(EINVAL);
  739. goto out_brelse;
  740. }
  741. /*
  742. * If the on-disk inode is already linked to a directory
  743. * entry, copy all of the inode into the in-core inode.
  744. * xfs_iformat() handles copying in the inode format
  745. * specific information.
  746. * Otherwise, just get the truly permanent information.
  747. */
  748. if (dip->di_mode) {
  749. xfs_dinode_from_disk(&ip->i_d, dip);
  750. error = xfs_iformat(ip, dip);
  751. if (error) {
  752. #ifdef DEBUG
  753. xfs_alert(mp, "%s: xfs_iformat() returned error %d",
  754. __func__, error);
  755. #endif /* DEBUG */
  756. goto out_brelse;
  757. }
  758. } else {
  759. ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
  760. ip->i_d.di_version = dip->di_version;
  761. ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
  762. ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
  763. /*
  764. * Make sure to pull in the mode here as well in
  765. * case the inode is released without being used.
  766. * This ensures that xfs_inactive() will see that
  767. * the inode is already free and not try to mess
  768. * with the uninitialized part of it.
  769. */
  770. ip->i_d.di_mode = 0;
  771. }
  772. /*
  773. * The inode format changed when we moved the link count and
  774. * made it 32 bits long. If this is an old format inode,
  775. * convert it in memory to look like a new one. If it gets
  776. * flushed to disk we will convert back before flushing or
  777. * logging it. We zero out the new projid field and the old link
  778. * count field. We'll handle clearing the pad field (the remains
  779. * of the old uuid field) when we actually convert the inode to
  780. * the new format. We don't change the version number so that we
  781. * can distinguish this from a real new format inode.
  782. */
  783. if (ip->i_d.di_version == 1) {
  784. ip->i_d.di_nlink = ip->i_d.di_onlink;
  785. ip->i_d.di_onlink = 0;
  786. xfs_set_projid(ip, 0);
  787. }
  788. ip->i_delayed_blks = 0;
  789. /*
  790. * Mark the buffer containing the inode as something to keep
  791. * around for a while. This helps to keep recently accessed
  792. * meta-data in-core longer.
  793. */
  794. xfs_buf_set_ref(bp, XFS_INO_REF);
  795. /*
  796. * Use xfs_trans_brelse() to release the buffer containing the
  797. * on-disk inode, because it was acquired with xfs_trans_read_buf()
  798. * in xfs_itobp() above. If tp is NULL, this is just a normal
  799. * brelse(). If we're within a transaction, then xfs_trans_brelse()
  800. * will only release the buffer if it is not dirty within the
  801. * transaction. It will be OK to release the buffer in this case,
  802. * because inodes on disk are never destroyed and we will be
  803. * locking the new in-core inode before putting it in the hash
  804. * table where other processes can find it. Thus we don't have
  805. * to worry about the inode being changed just because we released
  806. * the buffer.
  807. */
  808. out_brelse:
  809. xfs_trans_brelse(tp, bp);
  810. return error;
  811. }
  812. /*
  813. * Read in extents from a btree-format inode.
  814. * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
  815. */
  816. int
  817. xfs_iread_extents(
  818. xfs_trans_t *tp,
  819. xfs_inode_t *ip,
  820. int whichfork)
  821. {
  822. int error;
  823. xfs_ifork_t *ifp;
  824. xfs_extnum_t nextents;
  825. if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  826. XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
  827. ip->i_mount);
  828. return XFS_ERROR(EFSCORRUPTED);
  829. }
  830. nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
  831. ifp = XFS_IFORK_PTR(ip, whichfork);
  832. /*
  833. * We know that the size is valid (it's checked in iformat_btree)
  834. */
  835. ifp->if_bytes = ifp->if_real_bytes = 0;
  836. ifp->if_flags |= XFS_IFEXTENTS;
  837. xfs_iext_add(ifp, 0, nextents);
  838. error = xfs_bmap_read_extents(tp, ip, whichfork);
  839. if (error) {
  840. xfs_iext_destroy(ifp);
  841. ifp->if_flags &= ~XFS_IFEXTENTS;
  842. return error;
  843. }
  844. xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
  845. return 0;
  846. }
  847. /*
  848. * Allocate an inode on disk and return a copy of its in-core version.
  849. * The in-core inode is locked exclusively. Set mode, nlink, and rdev
  850. * appropriately within the inode. The uid and gid for the inode are
  851. * set according to the contents of the given cred structure.
  852. *
  853. * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
  854. * has a free inode available, call xfs_iget()
  855. * to obtain the in-core version of the allocated inode. Finally,
  856. * fill in the inode and log its initial contents. In this case,
  857. * ialloc_context would be set to NULL and call_again set to false.
  858. *
  859. * If xfs_dialloc() does not have an available inode,
  860. * it will replenish its supply by doing an allocation. Since we can
  861. * only do one allocation within a transaction without deadlocks, we
  862. * must commit the current transaction before returning the inode itself.
  863. * In this case, therefore, we will set call_again to true and return.
  864. * The caller should then commit the current transaction, start a new
  865. * transaction, and call xfs_ialloc() again to actually get the inode.
  866. *
  867. * To ensure that some other process does not grab the inode that
  868. * was allocated during the first call to xfs_ialloc(), this routine
  869. * also returns the [locked] bp pointing to the head of the freelist
  870. * as ialloc_context. The caller should hold this buffer across
  871. * the commit and pass it back into this routine on the second call.
  872. *
  873. * If we are allocating quota inodes, we do not have a parent inode
  874. * to attach to or associate with (i.e. pip == NULL) because they
  875. * are not linked into the directory structure - they are attached
  876. * directly to the superblock - and so have no parent.
  877. */
  878. int
  879. xfs_ialloc(
  880. xfs_trans_t *tp,
  881. xfs_inode_t *pip,
  882. umode_t mode,
  883. xfs_nlink_t nlink,
  884. xfs_dev_t rdev,
  885. prid_t prid,
  886. int okalloc,
  887. xfs_buf_t **ialloc_context,
  888. boolean_t *call_again,
  889. xfs_inode_t **ipp)
  890. {
  891. xfs_ino_t ino;
  892. xfs_inode_t *ip;
  893. uint flags;
  894. int error;
  895. timespec_t tv;
  896. int filestreams = 0;
  897. /*
  898. * Call the space management code to pick
  899. * the on-disk inode to be allocated.
  900. */
  901. error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
  902. ialloc_context, call_again, &ino);
  903. if (error)
  904. return error;
  905. if (*call_again || ino == NULLFSINO) {
  906. *ipp = NULL;
  907. return 0;
  908. }
  909. ASSERT(*ialloc_context == NULL);
  910. /*
  911. * Get the in-core inode with the lock held exclusively.
  912. * This is because we're setting fields here we need
  913. * to prevent others from looking at until we're done.
  914. */
  915. error = xfs_iget(tp->t_mountp, tp, ino, XFS_IGET_CREATE,
  916. XFS_ILOCK_EXCL, &ip);
  917. if (error)
  918. return error;
  919. ASSERT(ip != NULL);
  920. ip->i_d.di_mode = mode;
  921. ip->i_d.di_onlink = 0;
  922. ip->i_d.di_nlink = nlink;
  923. ASSERT(ip->i_d.di_nlink == nlink);
  924. ip->i_d.di_uid = current_fsuid();
  925. ip->i_d.di_gid = current_fsgid();
  926. xfs_set_projid(ip, prid);
  927. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  928. /*
  929. * If the superblock version is up to where we support new format
  930. * inodes and this is currently an old format inode, then change
  931. * the inode version number now. This way we only do the conversion
  932. * here rather than here and in the flush/logging code.
  933. */
  934. if (xfs_sb_version_hasnlink(&tp->t_mountp->m_sb) &&
  935. ip->i_d.di_version == 1) {
  936. ip->i_d.di_version = 2;
  937. /*
  938. * We've already zeroed the old link count, the projid field,
  939. * and the pad field.
  940. */
  941. }
  942. /*
  943. * Project ids won't be stored on disk if we are using a version 1 inode.
  944. */
  945. if ((prid != 0) && (ip->i_d.di_version == 1))
  946. xfs_bump_ino_vers2(tp, ip);
  947. if (pip && XFS_INHERIT_GID(pip)) {
  948. ip->i_d.di_gid = pip->i_d.di_gid;
  949. if ((pip->i_d.di_mode & S_ISGID) && S_ISDIR(mode)) {
  950. ip->i_d.di_mode |= S_ISGID;
  951. }
  952. }
  953. /*
  954. * If the group ID of the new file does not match the effective group
  955. * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
  956. * (and only if the irix_sgid_inherit compatibility variable is set).
  957. */
  958. if ((irix_sgid_inherit) &&
  959. (ip->i_d.di_mode & S_ISGID) &&
  960. (!in_group_p((gid_t)ip->i_d.di_gid))) {
  961. ip->i_d.di_mode &= ~S_ISGID;
  962. }
  963. ip->i_d.di_size = 0;
  964. ip->i_d.di_nextents = 0;
  965. ASSERT(ip->i_d.di_nblocks == 0);
  966. nanotime(&tv);
  967. ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
  968. ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
  969. ip->i_d.di_atime = ip->i_d.di_mtime;
  970. ip->i_d.di_ctime = ip->i_d.di_mtime;
  971. /*
  972. * di_gen will have been taken care of in xfs_iread.
  973. */
  974. ip->i_d.di_extsize = 0;
  975. ip->i_d.di_dmevmask = 0;
  976. ip->i_d.di_dmstate = 0;
  977. ip->i_d.di_flags = 0;
  978. flags = XFS_ILOG_CORE;
  979. switch (mode & S_IFMT) {
  980. case S_IFIFO:
  981. case S_IFCHR:
  982. case S_IFBLK:
  983. case S_IFSOCK:
  984. ip->i_d.di_format = XFS_DINODE_FMT_DEV;
  985. ip->i_df.if_u2.if_rdev = rdev;
  986. ip->i_df.if_flags = 0;
  987. flags |= XFS_ILOG_DEV;
  988. break;
  989. case S_IFREG:
  990. /*
  991. * we can't set up filestreams until after the VFS inode
  992. * is set up properly.
  993. */
  994. if (pip && xfs_inode_is_filestream(pip))
  995. filestreams = 1;
  996. /* fall through */
  997. case S_IFDIR:
  998. if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
  999. uint di_flags = 0;
  1000. if (S_ISDIR(mode)) {
  1001. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  1002. di_flags |= XFS_DIFLAG_RTINHERIT;
  1003. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  1004. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  1005. ip->i_d.di_extsize = pip->i_d.di_extsize;
  1006. }
  1007. } else if (S_ISREG(mode)) {
  1008. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  1009. di_flags |= XFS_DIFLAG_REALTIME;
  1010. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  1011. di_flags |= XFS_DIFLAG_EXTSIZE;
  1012. ip->i_d.di_extsize = pip->i_d.di_extsize;
  1013. }
  1014. }
  1015. if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
  1016. xfs_inherit_noatime)
  1017. di_flags |= XFS_DIFLAG_NOATIME;
  1018. if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
  1019. xfs_inherit_nodump)
  1020. di_flags |= XFS_DIFLAG_NODUMP;
  1021. if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
  1022. xfs_inherit_sync)
  1023. di_flags |= XFS_DIFLAG_SYNC;
  1024. if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
  1025. xfs_inherit_nosymlinks)
  1026. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  1027. if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1028. di_flags |= XFS_DIFLAG_PROJINHERIT;
  1029. if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
  1030. xfs_inherit_nodefrag)
  1031. di_flags |= XFS_DIFLAG_NODEFRAG;
  1032. if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
  1033. di_flags |= XFS_DIFLAG_FILESTREAM;
  1034. ip->i_d.di_flags |= di_flags;
  1035. }
  1036. /* FALLTHROUGH */
  1037. case S_IFLNK:
  1038. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  1039. ip->i_df.if_flags = XFS_IFEXTENTS;
  1040. ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
  1041. ip->i_df.if_u1.if_extents = NULL;
  1042. break;
  1043. default:
  1044. ASSERT(0);
  1045. }
  1046. /*
  1047. * Attribute fork settings for new inode.
  1048. */
  1049. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  1050. ip->i_d.di_anextents = 0;
  1051. /*
  1052. * Log the new values stuffed into the inode.
  1053. */
  1054. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1055. xfs_trans_log_inode(tp, ip, flags);
  1056. /* now that we have an i_mode we can setup inode ops and unlock */
  1057. xfs_setup_inode(ip);
  1058. /* now we have set up the vfs inode we can associate the filestream */
  1059. if (filestreams) {
  1060. error = xfs_filestream_associate(pip, ip);
  1061. if (error < 0)
  1062. return -error;
  1063. if (!error)
  1064. xfs_iflags_set(ip, XFS_IFILESTREAM);
  1065. }
  1066. *ipp = ip;
  1067. return 0;
  1068. }
  1069. /*
  1070. * Free up the underlying blocks past new_size. The new size must be smaller
  1071. * than the current size. This routine can be used both for the attribute and
  1072. * data fork, and does not modify the inode size, which is left to the caller.
  1073. *
  1074. * The transaction passed to this routine must have made a permanent log
  1075. * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
  1076. * given transaction and start new ones, so make sure everything involved in
  1077. * the transaction is tidy before calling here. Some transaction will be
  1078. * returned to the caller to be committed. The incoming transaction must
  1079. * already include the inode, and both inode locks must be held exclusively.
  1080. * The inode must also be "held" within the transaction. On return the inode
  1081. * will be "held" within the returned transaction. This routine does NOT
  1082. * require any disk space to be reserved for it within the transaction.
  1083. *
  1084. * If we get an error, we must return with the inode locked and linked into the
  1085. * current transaction. This keeps things simple for the higher level code,
  1086. * because it always knows that the inode is locked and held in the transaction
  1087. * that returns to it whether errors occur or not. We don't mark the inode
  1088. * dirty on error so that transactions can be easily aborted if possible.
  1089. */
  1090. int
  1091. xfs_itruncate_extents(
  1092. struct xfs_trans **tpp,
  1093. struct xfs_inode *ip,
  1094. int whichfork,
  1095. xfs_fsize_t new_size)
  1096. {
  1097. struct xfs_mount *mp = ip->i_mount;
  1098. struct xfs_trans *tp = *tpp;
  1099. struct xfs_trans *ntp;
  1100. xfs_bmap_free_t free_list;
  1101. xfs_fsblock_t first_block;
  1102. xfs_fileoff_t first_unmap_block;
  1103. xfs_fileoff_t last_block;
  1104. xfs_filblks_t unmap_len;
  1105. int committed;
  1106. int error = 0;
  1107. int done = 0;
  1108. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  1109. ASSERT(new_size <= XFS_ISIZE(ip));
  1110. ASSERT(tp->t_flags & XFS_TRANS_PERM_LOG_RES);
  1111. ASSERT(ip->i_itemp != NULL);
  1112. ASSERT(ip->i_itemp->ili_lock_flags == 0);
  1113. ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
  1114. trace_xfs_itruncate_extents_start(ip, new_size);
  1115. /*
  1116. * Since it is possible for space to become allocated beyond
  1117. * the end of the file (in a crash where the space is allocated
  1118. * but the inode size is not yet updated), simply remove any
  1119. * blocks which show up between the new EOF and the maximum
  1120. * possible file size. If the first block to be removed is
  1121. * beyond the maximum file size (ie it is the same as last_block),
  1122. * then there is nothing to do.
  1123. */
  1124. first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1125. last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  1126. if (first_unmap_block == last_block)
  1127. return 0;
  1128. ASSERT(first_unmap_block < last_block);
  1129. unmap_len = last_block - first_unmap_block + 1;
  1130. while (!done) {
  1131. xfs_bmap_init(&free_list, &first_block);
  1132. error = xfs_bunmapi(tp, ip,
  1133. first_unmap_block, unmap_len,
  1134. xfs_bmapi_aflag(whichfork),
  1135. XFS_ITRUNC_MAX_EXTENTS,
  1136. &first_block, &free_list,
  1137. &done);
  1138. if (error)
  1139. goto out_bmap_cancel;
  1140. /*
  1141. * Duplicate the transaction that has the permanent
  1142. * reservation and commit the old transaction.
  1143. */
  1144. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1145. if (committed)
  1146. xfs_trans_ijoin(tp, ip, 0);
  1147. if (error)
  1148. goto out_bmap_cancel;
  1149. if (committed) {
  1150. /*
  1151. * Mark the inode dirty so it will be logged and
  1152. * moved forward in the log as part of every commit.
  1153. */
  1154. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1155. }
  1156. ntp = xfs_trans_dup(tp);
  1157. error = xfs_trans_commit(tp, 0);
  1158. tp = ntp;
  1159. xfs_trans_ijoin(tp, ip, 0);
  1160. if (error)
  1161. goto out;
  1162. /*
  1163. * Transaction commit worked ok so we can drop the extra ticket
  1164. * reference that we gained in xfs_trans_dup()
  1165. */
  1166. xfs_log_ticket_put(tp->t_ticket);
  1167. error = xfs_trans_reserve(tp, 0,
  1168. XFS_ITRUNCATE_LOG_RES(mp), 0,
  1169. XFS_TRANS_PERM_LOG_RES,
  1170. XFS_ITRUNCATE_LOG_COUNT);
  1171. if (error)
  1172. goto out;
  1173. }
  1174. /*
  1175. * Always re-log the inode so that our permanent transaction can keep
  1176. * on rolling it forward in the log.
  1177. */
  1178. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1179. trace_xfs_itruncate_extents_end(ip, new_size);
  1180. out:
  1181. *tpp = tp;
  1182. return error;
  1183. out_bmap_cancel:
  1184. /*
  1185. * If the bunmapi call encounters an error, return to the caller where
  1186. * the transaction can be properly aborted. We just need to make sure
  1187. * we're not holding any resources that we were not when we came in.
  1188. */
  1189. xfs_bmap_cancel(&free_list);
  1190. goto out;
  1191. }
  1192. /*
  1193. * This is called when the inode's link count goes to 0.
  1194. * We place the on-disk inode on a list in the AGI. It
  1195. * will be pulled from this list when the inode is freed.
  1196. */
  1197. int
  1198. xfs_iunlink(
  1199. xfs_trans_t *tp,
  1200. xfs_inode_t *ip)
  1201. {
  1202. xfs_mount_t *mp;
  1203. xfs_agi_t *agi;
  1204. xfs_dinode_t *dip;
  1205. xfs_buf_t *agibp;
  1206. xfs_buf_t *ibp;
  1207. xfs_agino_t agino;
  1208. short bucket_index;
  1209. int offset;
  1210. int error;
  1211. ASSERT(ip->i_d.di_nlink == 0);
  1212. ASSERT(ip->i_d.di_mode != 0);
  1213. mp = tp->t_mountp;
  1214. /*
  1215. * Get the agi buffer first. It ensures lock ordering
  1216. * on the list.
  1217. */
  1218. error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
  1219. if (error)
  1220. return error;
  1221. agi = XFS_BUF_TO_AGI(agibp);
  1222. /*
  1223. * Get the index into the agi hash table for the
  1224. * list this inode will go on.
  1225. */
  1226. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1227. ASSERT(agino != 0);
  1228. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1229. ASSERT(agi->agi_unlinked[bucket_index]);
  1230. ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
  1231. if (agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO)) {
  1232. /*
  1233. * There is already another inode in the bucket we need
  1234. * to add ourselves to. Add us at the front of the list.
  1235. * Here we put the head pointer into our next pointer,
  1236. * and then we fall through to point the head at us.
  1237. */
  1238. error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0);
  1239. if (error)
  1240. return error;
  1241. ASSERT(dip->di_next_unlinked == cpu_to_be32(NULLAGINO));
  1242. dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
  1243. offset = ip->i_imap.im_boffset +
  1244. offsetof(xfs_dinode_t, di_next_unlinked);
  1245. xfs_trans_inode_buf(tp, ibp);
  1246. xfs_trans_log_buf(tp, ibp, offset,
  1247. (offset + sizeof(xfs_agino_t) - 1));
  1248. xfs_inobp_check(mp, ibp);
  1249. }
  1250. /*
  1251. * Point the bucket head pointer at the inode being inserted.
  1252. */
  1253. ASSERT(agino != 0);
  1254. agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
  1255. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1256. (sizeof(xfs_agino_t) * bucket_index);
  1257. xfs_trans_log_buf(tp, agibp, offset,
  1258. (offset + sizeof(xfs_agino_t) - 1));
  1259. return 0;
  1260. }
  1261. /*
  1262. * Pull the on-disk inode from the AGI unlinked list.
  1263. */
  1264. STATIC int
  1265. xfs_iunlink_remove(
  1266. xfs_trans_t *tp,
  1267. xfs_inode_t *ip)
  1268. {
  1269. xfs_ino_t next_ino;
  1270. xfs_mount_t *mp;
  1271. xfs_agi_t *agi;
  1272. xfs_dinode_t *dip;
  1273. xfs_buf_t *agibp;
  1274. xfs_buf_t *ibp;
  1275. xfs_agnumber_t agno;
  1276. xfs_agino_t agino;
  1277. xfs_agino_t next_agino;
  1278. xfs_buf_t *last_ibp;
  1279. xfs_dinode_t *last_dip = NULL;
  1280. short bucket_index;
  1281. int offset, last_offset = 0;
  1282. int error;
  1283. mp = tp->t_mountp;
  1284. agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1285. /*
  1286. * Get the agi buffer first. It ensures lock ordering
  1287. * on the list.
  1288. */
  1289. error = xfs_read_agi(mp, tp, agno, &agibp);
  1290. if (error)
  1291. return error;
  1292. agi = XFS_BUF_TO_AGI(agibp);
  1293. /*
  1294. * Get the index into the agi hash table for the
  1295. * list this inode will go on.
  1296. */
  1297. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1298. ASSERT(agino != 0);
  1299. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1300. ASSERT(agi->agi_unlinked[bucket_index] != cpu_to_be32(NULLAGINO));
  1301. ASSERT(agi->agi_unlinked[bucket_index]);
  1302. if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
  1303. /*
  1304. * We're at the head of the list. Get the inode's
  1305. * on-disk buffer to see if there is anyone after us
  1306. * on the list. Only modify our next pointer if it
  1307. * is not already NULLAGINO. This saves us the overhead
  1308. * of dealing with the buffer when there is no need to
  1309. * change it.
  1310. */
  1311. error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0);
  1312. if (error) {
  1313. xfs_warn(mp, "%s: xfs_itobp() returned error %d.",
  1314. __func__, error);
  1315. return error;
  1316. }
  1317. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1318. ASSERT(next_agino != 0);
  1319. if (next_agino != NULLAGINO) {
  1320. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1321. offset = ip->i_imap.im_boffset +
  1322. offsetof(xfs_dinode_t, di_next_unlinked);
  1323. xfs_trans_inode_buf(tp, ibp);
  1324. xfs_trans_log_buf(tp, ibp, offset,
  1325. (offset + sizeof(xfs_agino_t) - 1));
  1326. xfs_inobp_check(mp, ibp);
  1327. } else {
  1328. xfs_trans_brelse(tp, ibp);
  1329. }
  1330. /*
  1331. * Point the bucket head pointer at the next inode.
  1332. */
  1333. ASSERT(next_agino != 0);
  1334. ASSERT(next_agino != agino);
  1335. agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
  1336. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1337. (sizeof(xfs_agino_t) * bucket_index);
  1338. xfs_trans_log_buf(tp, agibp, offset,
  1339. (offset + sizeof(xfs_agino_t) - 1));
  1340. } else {
  1341. /*
  1342. * We need to search the list for the inode being freed.
  1343. */
  1344. next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
  1345. last_ibp = NULL;
  1346. while (next_agino != agino) {
  1347. /*
  1348. * If the last inode wasn't the one pointing to
  1349. * us, then release its buffer since we're not
  1350. * going to do anything with it.
  1351. */
  1352. if (last_ibp != NULL) {
  1353. xfs_trans_brelse(tp, last_ibp);
  1354. }
  1355. next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
  1356. error = xfs_inotobp(mp, tp, next_ino, &last_dip,
  1357. &last_ibp, &last_offset, 0);
  1358. if (error) {
  1359. xfs_warn(mp,
  1360. "%s: xfs_inotobp() returned error %d.",
  1361. __func__, error);
  1362. return error;
  1363. }
  1364. next_agino = be32_to_cpu(last_dip->di_next_unlinked);
  1365. ASSERT(next_agino != NULLAGINO);
  1366. ASSERT(next_agino != 0);
  1367. }
  1368. /*
  1369. * Now last_ibp points to the buffer previous to us on
  1370. * the unlinked list. Pull us from the list.
  1371. */
  1372. error = xfs_itobp(mp, tp, ip, &dip, &ibp, 0);
  1373. if (error) {
  1374. xfs_warn(mp, "%s: xfs_itobp(2) returned error %d.",
  1375. __func__, error);
  1376. return error;
  1377. }
  1378. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1379. ASSERT(next_agino != 0);
  1380. ASSERT(next_agino != agino);
  1381. if (next_agino != NULLAGINO) {
  1382. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1383. offset = ip->i_imap.im_boffset +
  1384. offsetof(xfs_dinode_t, di_next_unlinked);
  1385. xfs_trans_inode_buf(tp, ibp);
  1386. xfs_trans_log_buf(tp, ibp, offset,
  1387. (offset + sizeof(xfs_agino_t) - 1));
  1388. xfs_inobp_check(mp, ibp);
  1389. } else {
  1390. xfs_trans_brelse(tp, ibp);
  1391. }
  1392. /*
  1393. * Point the previous inode on the list to the next inode.
  1394. */
  1395. last_dip->di_next_unlinked = cpu_to_be32(next_agino);
  1396. ASSERT(next_agino != 0);
  1397. offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
  1398. xfs_trans_inode_buf(tp, last_ibp);
  1399. xfs_trans_log_buf(tp, last_ibp, offset,
  1400. (offset + sizeof(xfs_agino_t) - 1));
  1401. xfs_inobp_check(mp, last_ibp);
  1402. }
  1403. return 0;
  1404. }
  1405. /*
  1406. * A big issue when freeing the inode cluster is is that we _cannot_ skip any
  1407. * inodes that are in memory - they all must be marked stale and attached to
  1408. * the cluster buffer.
  1409. */
  1410. STATIC int
  1411. xfs_ifree_cluster(
  1412. xfs_inode_t *free_ip,
  1413. xfs_trans_t *tp,
  1414. xfs_ino_t inum)
  1415. {
  1416. xfs_mount_t *mp = free_ip->i_mount;
  1417. int blks_per_cluster;
  1418. int nbufs;
  1419. int ninodes;
  1420. int i, j;
  1421. xfs_daddr_t blkno;
  1422. xfs_buf_t *bp;
  1423. xfs_inode_t *ip;
  1424. xfs_inode_log_item_t *iip;
  1425. xfs_log_item_t *lip;
  1426. struct xfs_perag *pag;
  1427. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, inum));
  1428. if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
  1429. blks_per_cluster = 1;
  1430. ninodes = mp->m_sb.sb_inopblock;
  1431. nbufs = XFS_IALLOC_BLOCKS(mp);
  1432. } else {
  1433. blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
  1434. mp->m_sb.sb_blocksize;
  1435. ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
  1436. nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
  1437. }
  1438. for (j = 0; j < nbufs; j++, inum += ninodes) {
  1439. blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
  1440. XFS_INO_TO_AGBNO(mp, inum));
  1441. /*
  1442. * We obtain and lock the backing buffer first in the process
  1443. * here, as we have to ensure that any dirty inode that we
  1444. * can't get the flush lock on is attached to the buffer.
  1445. * If we scan the in-memory inodes first, then buffer IO can
  1446. * complete before we get a lock on it, and hence we may fail
  1447. * to mark all the active inodes on the buffer stale.
  1448. */
  1449. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
  1450. mp->m_bsize * blks_per_cluster, 0);
  1451. if (!bp)
  1452. return ENOMEM;
  1453. /*
  1454. * Walk the inodes already attached to the buffer and mark them
  1455. * stale. These will all have the flush locks held, so an
  1456. * in-memory inode walk can't lock them. By marking them all
  1457. * stale first, we will not attempt to lock them in the loop
  1458. * below as the XFS_ISTALE flag will be set.
  1459. */
  1460. lip = bp->b_fspriv;
  1461. while (lip) {
  1462. if (lip->li_type == XFS_LI_INODE) {
  1463. iip = (xfs_inode_log_item_t *)lip;
  1464. ASSERT(iip->ili_logged == 1);
  1465. lip->li_cb = xfs_istale_done;
  1466. xfs_trans_ail_copy_lsn(mp->m_ail,
  1467. &iip->ili_flush_lsn,
  1468. &iip->ili_item.li_lsn);
  1469. xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
  1470. }
  1471. lip = lip->li_bio_list;
  1472. }
  1473. /*
  1474. * For each inode in memory attempt to add it to the inode
  1475. * buffer and set it up for being staled on buffer IO
  1476. * completion. This is safe as we've locked out tail pushing
  1477. * and flushing by locking the buffer.
  1478. *
  1479. * We have already marked every inode that was part of a
  1480. * transaction stale above, which means there is no point in
  1481. * even trying to lock them.
  1482. */
  1483. for (i = 0; i < ninodes; i++) {
  1484. retry:
  1485. rcu_read_lock();
  1486. ip = radix_tree_lookup(&pag->pag_ici_root,
  1487. XFS_INO_TO_AGINO(mp, (inum + i)));
  1488. /* Inode not in memory, nothing to do */
  1489. if (!ip) {
  1490. rcu_read_unlock();
  1491. continue;
  1492. }
  1493. /*
  1494. * because this is an RCU protected lookup, we could
  1495. * find a recently freed or even reallocated inode
  1496. * during the lookup. We need to check under the
  1497. * i_flags_lock for a valid inode here. Skip it if it
  1498. * is not valid, the wrong inode or stale.
  1499. */
  1500. spin_lock(&ip->i_flags_lock);
  1501. if (ip->i_ino != inum + i ||
  1502. __xfs_iflags_test(ip, XFS_ISTALE)) {
  1503. spin_unlock(&ip->i_flags_lock);
  1504. rcu_read_unlock();
  1505. continue;
  1506. }
  1507. spin_unlock(&ip->i_flags_lock);
  1508. /*
  1509. * Don't try to lock/unlock the current inode, but we
  1510. * _cannot_ skip the other inodes that we did not find
  1511. * in the list attached to the buffer and are not
  1512. * already marked stale. If we can't lock it, back off
  1513. * and retry.
  1514. */
  1515. if (ip != free_ip &&
  1516. !xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  1517. rcu_read_unlock();
  1518. delay(1);
  1519. goto retry;
  1520. }
  1521. rcu_read_unlock();
  1522. xfs_iflock(ip);
  1523. xfs_iflags_set(ip, XFS_ISTALE);
  1524. /*
  1525. * we don't need to attach clean inodes or those only
  1526. * with unlogged changes (which we throw away, anyway).
  1527. */
  1528. iip = ip->i_itemp;
  1529. if (!iip || xfs_inode_clean(ip)) {
  1530. ASSERT(ip != free_ip);
  1531. xfs_ifunlock(ip);
  1532. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1533. continue;
  1534. }
  1535. iip->ili_last_fields = iip->ili_fields;
  1536. iip->ili_fields = 0;
  1537. iip->ili_logged = 1;
  1538. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  1539. &iip->ili_item.li_lsn);
  1540. xfs_buf_attach_iodone(bp, xfs_istale_done,
  1541. &iip->ili_item);
  1542. if (ip != free_ip)
  1543. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1544. }
  1545. xfs_trans_stale_inode_buf(tp, bp);
  1546. xfs_trans_binval(tp, bp);
  1547. }
  1548. xfs_perag_put(pag);
  1549. return 0;
  1550. }
  1551. /*
  1552. * This is called to return an inode to the inode free list.
  1553. * The inode should already be truncated to 0 length and have
  1554. * no pages associated with it. This routine also assumes that
  1555. * the inode is already a part of the transaction.
  1556. *
  1557. * The on-disk copy of the inode will have been added to the list
  1558. * of unlinked inodes in the AGI. We need to remove the inode from
  1559. * that list atomically with respect to freeing it here.
  1560. */
  1561. int
  1562. xfs_ifree(
  1563. xfs_trans_t *tp,
  1564. xfs_inode_t *ip,
  1565. xfs_bmap_free_t *flist)
  1566. {
  1567. int error;
  1568. int delete;
  1569. xfs_ino_t first_ino;
  1570. xfs_dinode_t *dip;
  1571. xfs_buf_t *ibp;
  1572. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  1573. ASSERT(ip->i_d.di_nlink == 0);
  1574. ASSERT(ip->i_d.di_nextents == 0);
  1575. ASSERT(ip->i_d.di_anextents == 0);
  1576. ASSERT(ip->i_d.di_size == 0 || !S_ISREG(ip->i_d.di_mode));
  1577. ASSERT(ip->i_d.di_nblocks == 0);
  1578. /*
  1579. * Pull the on-disk inode from the AGI unlinked list.
  1580. */
  1581. error = xfs_iunlink_remove(tp, ip);
  1582. if (error != 0) {
  1583. return error;
  1584. }
  1585. error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
  1586. if (error != 0) {
  1587. return error;
  1588. }
  1589. ip->i_d.di_mode = 0; /* mark incore inode as free */
  1590. ip->i_d.di_flags = 0;
  1591. ip->i_d.di_dmevmask = 0;
  1592. ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
  1593. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  1594. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  1595. /*
  1596. * Bump the generation count so no one will be confused
  1597. * by reincarnations of this inode.
  1598. */
  1599. ip->i_d.di_gen++;
  1600. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1601. error = xfs_itobp(ip->i_mount, tp, ip, &dip, &ibp, 0);
  1602. if (error)
  1603. return error;
  1604. /*
  1605. * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
  1606. * from picking up this inode when it is reclaimed (its incore state
  1607. * initialzed but not flushed to disk yet). The in-core di_mode is
  1608. * already cleared and a corresponding transaction logged.
  1609. * The hack here just synchronizes the in-core to on-disk
  1610. * di_mode value in advance before the actual inode sync to disk.
  1611. * This is OK because the inode is already unlinked and would never
  1612. * change its di_mode again for this inode generation.
  1613. * This is a temporary hack that would require a proper fix
  1614. * in the future.
  1615. */
  1616. dip->di_mode = 0;
  1617. if (delete) {
  1618. error = xfs_ifree_cluster(ip, tp, first_ino);
  1619. }
  1620. return error;
  1621. }
  1622. /*
  1623. * Reallocate the space for if_broot based on the number of records
  1624. * being added or deleted as indicated in rec_diff. Move the records
  1625. * and pointers in if_broot to fit the new size. When shrinking this
  1626. * will eliminate holes between the records and pointers created by
  1627. * the caller. When growing this will create holes to be filled in
  1628. * by the caller.
  1629. *
  1630. * The caller must not request to add more records than would fit in
  1631. * the on-disk inode root. If the if_broot is currently NULL, then
  1632. * if we adding records one will be allocated. The caller must also
  1633. * not request that the number of records go below zero, although
  1634. * it can go to zero.
  1635. *
  1636. * ip -- the inode whose if_broot area is changing
  1637. * ext_diff -- the change in the number of records, positive or negative,
  1638. * requested for the if_broot array.
  1639. */
  1640. void
  1641. xfs_iroot_realloc(
  1642. xfs_inode_t *ip,
  1643. int rec_diff,
  1644. int whichfork)
  1645. {
  1646. struct xfs_mount *mp = ip->i_mount;
  1647. int cur_max;
  1648. xfs_ifork_t *ifp;
  1649. struct xfs_btree_block *new_broot;
  1650. int new_max;
  1651. size_t new_size;
  1652. char *np;
  1653. char *op;
  1654. /*
  1655. * Handle the degenerate case quietly.
  1656. */
  1657. if (rec_diff == 0) {
  1658. return;
  1659. }
  1660. ifp = XFS_IFORK_PTR(ip, whichfork);
  1661. if (rec_diff > 0) {
  1662. /*
  1663. * If there wasn't any memory allocated before, just
  1664. * allocate it now and get out.
  1665. */
  1666. if (ifp->if_broot_bytes == 0) {
  1667. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
  1668. ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  1669. ifp->if_broot_bytes = (int)new_size;
  1670. return;
  1671. }
  1672. /*
  1673. * If there is already an existing if_broot, then we need
  1674. * to realloc() it and shift the pointers to their new
  1675. * location. The records don't change location because
  1676. * they are kept butted up against the btree block header.
  1677. */
  1678. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  1679. new_max = cur_max + rec_diff;
  1680. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
  1681. ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
  1682. (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
  1683. KM_SLEEP | KM_NOFS);
  1684. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  1685. ifp->if_broot_bytes);
  1686. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  1687. (int)new_size);
  1688. ifp->if_broot_bytes = (int)new_size;
  1689. ASSERT(ifp->if_broot_bytes <=
  1690. XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
  1691. memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
  1692. return;
  1693. }
  1694. /*
  1695. * rec_diff is less than 0. In this case, we are shrinking the
  1696. * if_broot buffer. It must already exist. If we go to zero
  1697. * records, just get rid of the root and clear the status bit.
  1698. */
  1699. ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
  1700. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  1701. new_max = cur_max + rec_diff;
  1702. ASSERT(new_max >= 0);
  1703. if (new_max > 0)
  1704. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
  1705. else
  1706. new_size = 0;
  1707. if (new_size > 0) {
  1708. new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
  1709. /*
  1710. * First copy over the btree block header.
  1711. */
  1712. memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN);
  1713. } else {
  1714. new_broot = NULL;
  1715. ifp->if_flags &= ~XFS_IFBROOT;
  1716. }
  1717. /*
  1718. * Only copy the records and pointers if there are any.
  1719. */
  1720. if (new_max > 0) {
  1721. /*
  1722. * First copy the records.
  1723. */
  1724. op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
  1725. np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
  1726. memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
  1727. /*
  1728. * Then copy the pointers.
  1729. */
  1730. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  1731. ifp->if_broot_bytes);
  1732. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
  1733. (int)new_size);
  1734. memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
  1735. }
  1736. kmem_free(ifp->if_broot);
  1737. ifp->if_broot = new_broot;
  1738. ifp->if_broot_bytes = (int)new_size;
  1739. ASSERT(ifp->if_broot_bytes <=
  1740. XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
  1741. return;
  1742. }
  1743. /*
  1744. * This is called when the amount of space needed for if_data
  1745. * is increased or decreased. The change in size is indicated by
  1746. * the number of bytes that need to be added or deleted in the
  1747. * byte_diff parameter.
  1748. *
  1749. * If the amount of space needed has decreased below the size of the
  1750. * inline buffer, then switch to using the inline buffer. Otherwise,
  1751. * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
  1752. * to what is needed.
  1753. *
  1754. * ip -- the inode whose if_data area is changing
  1755. * byte_diff -- the change in the number of bytes, positive or negative,
  1756. * requested for the if_data array.
  1757. */
  1758. void
  1759. xfs_idata_realloc(
  1760. xfs_inode_t *ip,
  1761. int byte_diff,
  1762. int whichfork)
  1763. {
  1764. xfs_ifork_t *ifp;
  1765. int new_size;
  1766. int real_size;
  1767. if (byte_diff == 0) {
  1768. return;
  1769. }
  1770. ifp = XFS_IFORK_PTR(ip, whichfork);
  1771. new_size = (int)ifp->if_bytes + byte_diff;
  1772. ASSERT(new_size >= 0);
  1773. if (new_size == 0) {
  1774. if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  1775. kmem_free(ifp->if_u1.if_data);
  1776. }
  1777. ifp->if_u1.if_data = NULL;
  1778. real_size = 0;
  1779. } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
  1780. /*
  1781. * If the valid extents/data can fit in if_inline_ext/data,
  1782. * copy them from the malloc'd vector and free it.
  1783. */
  1784. if (ifp->if_u1.if_data == NULL) {
  1785. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  1786. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  1787. ASSERT(ifp->if_real_bytes != 0);
  1788. memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
  1789. new_size);
  1790. kmem_free(ifp->if_u1.if_data);
  1791. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  1792. }
  1793. real_size = 0;
  1794. } else {
  1795. /*
  1796. * Stuck with malloc/realloc.
  1797. * For inline data, the underlying buffer must be
  1798. * a multiple of 4 bytes in size so that it can be
  1799. * logged and stay on word boundaries. We enforce
  1800. * that here.
  1801. */
  1802. real_size = roundup(new_size, 4);
  1803. if (ifp->if_u1.if_data == NULL) {
  1804. ASSERT(ifp->if_real_bytes == 0);
  1805. ifp->if_u1.if_data = kmem_alloc(real_size,
  1806. KM_SLEEP | KM_NOFS);
  1807. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  1808. /*
  1809. * Only do the realloc if the underlying size
  1810. * is really changing.
  1811. */
  1812. if (ifp->if_real_bytes != real_size) {
  1813. ifp->if_u1.if_data =
  1814. kmem_realloc(ifp->if_u1.if_data,
  1815. real_size,
  1816. ifp->if_real_bytes,
  1817. KM_SLEEP | KM_NOFS);
  1818. }
  1819. } else {
  1820. ASSERT(ifp->if_real_bytes == 0);
  1821. ifp->if_u1.if_data = kmem_alloc(real_size,
  1822. KM_SLEEP | KM_NOFS);
  1823. memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
  1824. ifp->if_bytes);
  1825. }
  1826. }
  1827. ifp->if_real_bytes = real_size;
  1828. ifp->if_bytes = new_size;
  1829. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  1830. }
  1831. void
  1832. xfs_idestroy_fork(
  1833. xfs_inode_t *ip,
  1834. int whichfork)
  1835. {
  1836. xfs_ifork_t *ifp;
  1837. ifp = XFS_IFORK_PTR(ip, whichfork);
  1838. if (ifp->if_broot != NULL) {
  1839. kmem_free(ifp->if_broot);
  1840. ifp->if_broot = NULL;
  1841. }
  1842. /*
  1843. * If the format is local, then we can't have an extents
  1844. * array so just look for an inline data array. If we're
  1845. * not local then we may or may not have an extents list,
  1846. * so check and free it up if we do.
  1847. */
  1848. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  1849. if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
  1850. (ifp->if_u1.if_data != NULL)) {
  1851. ASSERT(ifp->if_real_bytes != 0);
  1852. kmem_free(ifp->if_u1.if_data);
  1853. ifp->if_u1.if_data = NULL;
  1854. ifp->if_real_bytes = 0;
  1855. }
  1856. } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
  1857. ((ifp->if_flags & XFS_IFEXTIREC) ||
  1858. ((ifp->if_u1.if_extents != NULL) &&
  1859. (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
  1860. ASSERT(ifp->if_real_bytes != 0);
  1861. xfs_iext_destroy(ifp);
  1862. }
  1863. ASSERT(ifp->if_u1.if_extents == NULL ||
  1864. ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
  1865. ASSERT(ifp->if_real_bytes == 0);
  1866. if (whichfork == XFS_ATTR_FORK) {
  1867. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  1868. ip->i_afp = NULL;
  1869. }
  1870. }
  1871. /*
  1872. * This is called to unpin an inode. The caller must have the inode locked
  1873. * in at least shared mode so that the buffer cannot be subsequently pinned
  1874. * once someone is waiting for it to be unpinned.
  1875. */
  1876. static void
  1877. xfs_iunpin(
  1878. struct xfs_inode *ip)
  1879. {
  1880. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  1881. trace_xfs_inode_unpin_nowait(ip, _RET_IP_);
  1882. /* Give the log a push to start the unpinning I/O */
  1883. xfs_log_force_lsn(ip->i_mount, ip->i_itemp->ili_last_lsn, 0);
  1884. }
  1885. static void
  1886. __xfs_iunpin_wait(
  1887. struct xfs_inode *ip)
  1888. {
  1889. wait_queue_head_t *wq = bit_waitqueue(&ip->i_flags, __XFS_IPINNED_BIT);
  1890. DEFINE_WAIT_BIT(wait, &ip->i_flags, __XFS_IPINNED_BIT);
  1891. xfs_iunpin(ip);
  1892. do {
  1893. prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
  1894. if (xfs_ipincount(ip))
  1895. io_schedule();
  1896. } while (xfs_ipincount(ip));
  1897. finish_wait(wq, &wait.wait);
  1898. }
  1899. void
  1900. xfs_iunpin_wait(
  1901. struct xfs_inode *ip)
  1902. {
  1903. if (xfs_ipincount(ip))
  1904. __xfs_iunpin_wait(ip);
  1905. }
  1906. /*
  1907. * xfs_iextents_copy()
  1908. *
  1909. * This is called to copy the REAL extents (as opposed to the delayed
  1910. * allocation extents) from the inode into the given buffer. It
  1911. * returns the number of bytes copied into the buffer.
  1912. *
  1913. * If there are no delayed allocation extents, then we can just
  1914. * memcpy() the extents into the buffer. Otherwise, we need to
  1915. * examine each extent in turn and skip those which are delayed.
  1916. */
  1917. int
  1918. xfs_iextents_copy(
  1919. xfs_inode_t *ip,
  1920. xfs_bmbt_rec_t *dp,
  1921. int whichfork)
  1922. {
  1923. int copied;
  1924. int i;
  1925. xfs_ifork_t *ifp;
  1926. int nrecs;
  1927. xfs_fsblock_t start_block;
  1928. ifp = XFS_IFORK_PTR(ip, whichfork);
  1929. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  1930. ASSERT(ifp->if_bytes > 0);
  1931. nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  1932. XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
  1933. ASSERT(nrecs > 0);
  1934. /*
  1935. * There are some delayed allocation extents in the
  1936. * inode, so copy the extents one at a time and skip
  1937. * the delayed ones. There must be at least one
  1938. * non-delayed extent.
  1939. */
  1940. copied = 0;
  1941. for (i = 0; i < nrecs; i++) {
  1942. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  1943. start_block = xfs_bmbt_get_startblock(ep);
  1944. if (isnullstartblock(start_block)) {
  1945. /*
  1946. * It's a delayed allocation extent, so skip it.
  1947. */
  1948. continue;
  1949. }
  1950. /* Translate to on disk format */
  1951. put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
  1952. put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
  1953. dp++;
  1954. copied++;
  1955. }
  1956. ASSERT(copied != 0);
  1957. xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
  1958. return (copied * (uint)sizeof(xfs_bmbt_rec_t));
  1959. }
  1960. /*
  1961. * Each of the following cases stores data into the same region
  1962. * of the on-disk inode, so only one of them can be valid at
  1963. * any given time. While it is possible to have conflicting formats
  1964. * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
  1965. * in EXTENTS format, this can only happen when the fork has
  1966. * changed formats after being modified but before being flushed.
  1967. * In these cases, the format always takes precedence, because the
  1968. * format indicates the current state of the fork.
  1969. */
  1970. /*ARGSUSED*/
  1971. STATIC void
  1972. xfs_iflush_fork(
  1973. xfs_inode_t *ip,
  1974. xfs_dinode_t *dip,
  1975. xfs_inode_log_item_t *iip,
  1976. int whichfork,
  1977. xfs_buf_t *bp)
  1978. {
  1979. char *cp;
  1980. xfs_ifork_t *ifp;
  1981. xfs_mount_t *mp;
  1982. #ifdef XFS_TRANS_DEBUG
  1983. int first;
  1984. #endif
  1985. static const short brootflag[2] =
  1986. { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
  1987. static const short dataflag[2] =
  1988. { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
  1989. static const short extflag[2] =
  1990. { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
  1991. if (!iip)
  1992. return;
  1993. ifp = XFS_IFORK_PTR(ip, whichfork);
  1994. /*
  1995. * This can happen if we gave up in iformat in an error path,
  1996. * for the attribute fork.
  1997. */
  1998. if (!ifp) {
  1999. ASSERT(whichfork == XFS_ATTR_FORK);
  2000. return;
  2001. }
  2002. cp = XFS_DFORK_PTR(dip, whichfork);
  2003. mp = ip->i_mount;
  2004. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  2005. case XFS_DINODE_FMT_LOCAL:
  2006. if ((iip->ili_fields & dataflag[whichfork]) &&
  2007. (ifp->if_bytes > 0)) {
  2008. ASSERT(ifp->if_u1.if_data != NULL);
  2009. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  2010. memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
  2011. }
  2012. break;
  2013. case XFS_DINODE_FMT_EXTENTS:
  2014. ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
  2015. !(iip->ili_fields & extflag[whichfork]));
  2016. if ((iip->ili_fields & extflag[whichfork]) &&
  2017. (ifp->if_bytes > 0)) {
  2018. ASSERT(xfs_iext_get_ext(ifp, 0));
  2019. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
  2020. (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
  2021. whichfork);
  2022. }
  2023. break;
  2024. case XFS_DINODE_FMT_BTREE:
  2025. if ((iip->ili_fields & brootflag[whichfork]) &&
  2026. (ifp->if_broot_bytes > 0)) {
  2027. ASSERT(ifp->if_broot != NULL);
  2028. ASSERT(ifp->if_broot_bytes <=
  2029. (XFS_IFORK_SIZE(ip, whichfork) +
  2030. XFS_BROOT_SIZE_ADJ));
  2031. xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
  2032. (xfs_bmdr_block_t *)cp,
  2033. XFS_DFORK_SIZE(dip, mp, whichfork));
  2034. }
  2035. break;
  2036. case XFS_DINODE_FMT_DEV:
  2037. if (iip->ili_fields & XFS_ILOG_DEV) {
  2038. ASSERT(whichfork == XFS_DATA_FORK);
  2039. xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
  2040. }
  2041. break;
  2042. case XFS_DINODE_FMT_UUID:
  2043. if (iip->ili_fields & XFS_ILOG_UUID) {
  2044. ASSERT(whichfork == XFS_DATA_FORK);
  2045. memcpy(XFS_DFORK_DPTR(dip),
  2046. &ip->i_df.if_u2.if_uuid,
  2047. sizeof(uuid_t));
  2048. }
  2049. break;
  2050. default:
  2051. ASSERT(0);
  2052. break;
  2053. }
  2054. }
  2055. STATIC int
  2056. xfs_iflush_cluster(
  2057. xfs_inode_t *ip,
  2058. xfs_buf_t *bp)
  2059. {
  2060. xfs_mount_t *mp = ip->i_mount;
  2061. struct xfs_perag *pag;
  2062. unsigned long first_index, mask;
  2063. unsigned long inodes_per_cluster;
  2064. int ilist_size;
  2065. xfs_inode_t **ilist;
  2066. xfs_inode_t *iq;
  2067. int nr_found;
  2068. int clcount = 0;
  2069. int bufwasdelwri;
  2070. int i;
  2071. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  2072. inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
  2073. ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
  2074. ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
  2075. if (!ilist)
  2076. goto out_put;
  2077. mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
  2078. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
  2079. rcu_read_lock();
  2080. /* really need a gang lookup range call here */
  2081. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
  2082. first_index, inodes_per_cluster);
  2083. if (nr_found == 0)
  2084. goto out_free;
  2085. for (i = 0; i < nr_found; i++) {
  2086. iq = ilist[i];
  2087. if (iq == ip)
  2088. continue;
  2089. /*
  2090. * because this is an RCU protected lookup, we could find a
  2091. * recently freed or even reallocated inode during the lookup.
  2092. * We need to check under the i_flags_lock for a valid inode
  2093. * here. Skip it if it is not valid or the wrong inode.
  2094. */
  2095. spin_lock(&ip->i_flags_lock);
  2096. if (!ip->i_ino ||
  2097. (XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index) {
  2098. spin_unlock(&ip->i_flags_lock);
  2099. continue;
  2100. }
  2101. spin_unlock(&ip->i_flags_lock);
  2102. /*
  2103. * Do an un-protected check to see if the inode is dirty and
  2104. * is a candidate for flushing. These checks will be repeated
  2105. * later after the appropriate locks are acquired.
  2106. */
  2107. if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
  2108. continue;
  2109. /*
  2110. * Try to get locks. If any are unavailable or it is pinned,
  2111. * then this inode cannot be flushed and is skipped.
  2112. */
  2113. if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
  2114. continue;
  2115. if (!xfs_iflock_nowait(iq)) {
  2116. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2117. continue;
  2118. }
  2119. if (xfs_ipincount(iq)) {
  2120. xfs_ifunlock(iq);
  2121. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2122. continue;
  2123. }
  2124. /*
  2125. * arriving here means that this inode can be flushed. First
  2126. * re-check that it's dirty before flushing.
  2127. */
  2128. if (!xfs_inode_clean(iq)) {
  2129. int error;
  2130. error = xfs_iflush_int(iq, bp);
  2131. if (error) {
  2132. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2133. goto cluster_corrupt_out;
  2134. }
  2135. clcount++;
  2136. } else {
  2137. xfs_ifunlock(iq);
  2138. }
  2139. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2140. }
  2141. if (clcount) {
  2142. XFS_STATS_INC(xs_icluster_flushcnt);
  2143. XFS_STATS_ADD(xs_icluster_flushinode, clcount);
  2144. }
  2145. out_free:
  2146. rcu_read_unlock();
  2147. kmem_free(ilist);
  2148. out_put:
  2149. xfs_perag_put(pag);
  2150. return 0;
  2151. cluster_corrupt_out:
  2152. /*
  2153. * Corruption detected in the clustering loop. Invalidate the
  2154. * inode buffer and shut down the filesystem.
  2155. */
  2156. rcu_read_unlock();
  2157. /*
  2158. * Clean up the buffer. If it was delwri, just release it --
  2159. * brelse can handle it with no problems. If not, shut down the
  2160. * filesystem before releasing the buffer.
  2161. */
  2162. bufwasdelwri = (bp->b_flags & _XBF_DELWRI_Q);
  2163. if (bufwasdelwri)
  2164. xfs_buf_relse(bp);
  2165. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2166. if (!bufwasdelwri) {
  2167. /*
  2168. * Just like incore_relse: if we have b_iodone functions,
  2169. * mark the buffer as an error and call them. Otherwise
  2170. * mark it as stale and brelse.
  2171. */
  2172. if (bp->b_iodone) {
  2173. XFS_BUF_UNDONE(bp);
  2174. xfs_buf_stale(bp);
  2175. xfs_buf_ioerror(bp, EIO);
  2176. xfs_buf_ioend(bp, 0);
  2177. } else {
  2178. xfs_buf_stale(bp);
  2179. xfs_buf_relse(bp);
  2180. }
  2181. }
  2182. /*
  2183. * Unlocks the flush lock
  2184. */
  2185. xfs_iflush_abort(iq, false);
  2186. kmem_free(ilist);
  2187. xfs_perag_put(pag);
  2188. return XFS_ERROR(EFSCORRUPTED);
  2189. }
  2190. /*
  2191. * Flush dirty inode metadata into the backing buffer.
  2192. *
  2193. * The caller must have the inode lock and the inode flush lock held. The
  2194. * inode lock will still be held upon return to the caller, and the inode
  2195. * flush lock will be released after the inode has reached the disk.
  2196. *
  2197. * The caller must write out the buffer returned in *bpp and release it.
  2198. */
  2199. int
  2200. xfs_iflush(
  2201. struct xfs_inode *ip,
  2202. struct xfs_buf **bpp)
  2203. {
  2204. struct xfs_mount *mp = ip->i_mount;
  2205. struct xfs_buf *bp;
  2206. struct xfs_dinode *dip;
  2207. int error;
  2208. XFS_STATS_INC(xs_iflush_count);
  2209. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2210. ASSERT(xfs_isiflocked(ip));
  2211. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2212. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2213. *bpp = NULL;
  2214. xfs_iunpin_wait(ip);
  2215. /*
  2216. * For stale inodes we cannot rely on the backing buffer remaining
  2217. * stale in cache for the remaining life of the stale inode and so
  2218. * xfs_itobp() below may give us a buffer that no longer contains
  2219. * inodes below. We have to check this after ensuring the inode is
  2220. * unpinned so that it is safe to reclaim the stale inode after the
  2221. * flush call.
  2222. */
  2223. if (xfs_iflags_test(ip, XFS_ISTALE)) {
  2224. xfs_ifunlock(ip);
  2225. return 0;
  2226. }
  2227. /*
  2228. * This may have been unpinned because the filesystem is shutting
  2229. * down forcibly. If that's the case we must not write this inode
  2230. * to disk, because the log record didn't make it to disk.
  2231. *
  2232. * We also have to remove the log item from the AIL in this case,
  2233. * as we wait for an empty AIL as part of the unmount process.
  2234. */
  2235. if (XFS_FORCED_SHUTDOWN(mp)) {
  2236. error = XFS_ERROR(EIO);
  2237. goto abort_out;
  2238. }
  2239. /*
  2240. * Get the buffer containing the on-disk inode.
  2241. */
  2242. error = xfs_itobp(mp, NULL, ip, &dip, &bp, XBF_TRYLOCK);
  2243. if (error || !bp) {
  2244. xfs_ifunlock(ip);
  2245. return error;
  2246. }
  2247. /*
  2248. * First flush out the inode that xfs_iflush was called with.
  2249. */
  2250. error = xfs_iflush_int(ip, bp);
  2251. if (error)
  2252. goto corrupt_out;
  2253. /*
  2254. * If the buffer is pinned then push on the log now so we won't
  2255. * get stuck waiting in the write for too long.
  2256. */
  2257. if (xfs_buf_ispinned(bp))
  2258. xfs_log_force(mp, 0);
  2259. /*
  2260. * inode clustering:
  2261. * see if other inodes can be gathered into this write
  2262. */
  2263. error = xfs_iflush_cluster(ip, bp);
  2264. if (error)
  2265. goto cluster_corrupt_out;
  2266. *bpp = bp;
  2267. return 0;
  2268. corrupt_out:
  2269. xfs_buf_relse(bp);
  2270. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2271. cluster_corrupt_out:
  2272. error = XFS_ERROR(EFSCORRUPTED);
  2273. abort_out:
  2274. /*
  2275. * Unlocks the flush lock
  2276. */
  2277. xfs_iflush_abort(ip, false);
  2278. return error;
  2279. }
  2280. STATIC int
  2281. xfs_iflush_int(
  2282. xfs_inode_t *ip,
  2283. xfs_buf_t *bp)
  2284. {
  2285. xfs_inode_log_item_t *iip;
  2286. xfs_dinode_t *dip;
  2287. xfs_mount_t *mp;
  2288. #ifdef XFS_TRANS_DEBUG
  2289. int first;
  2290. #endif
  2291. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2292. ASSERT(xfs_isiflocked(ip));
  2293. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2294. ip->i_d.di_nextents > XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK));
  2295. iip = ip->i_itemp;
  2296. mp = ip->i_mount;
  2297. /* set *dip = inode's place in the buffer */
  2298. dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
  2299. if (XFS_TEST_ERROR(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC),
  2300. mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
  2301. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2302. "%s: Bad inode %Lu magic number 0x%x, ptr 0x%p",
  2303. __func__, ip->i_ino, be16_to_cpu(dip->di_magic), dip);
  2304. goto corrupt_out;
  2305. }
  2306. if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
  2307. mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
  2308. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2309. "%s: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
  2310. __func__, ip->i_ino, ip, ip->i_d.di_magic);
  2311. goto corrupt_out;
  2312. }
  2313. if (S_ISREG(ip->i_d.di_mode)) {
  2314. if (XFS_TEST_ERROR(
  2315. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2316. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
  2317. mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
  2318. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2319. "%s: Bad regular inode %Lu, ptr 0x%p",
  2320. __func__, ip->i_ino, ip);
  2321. goto corrupt_out;
  2322. }
  2323. } else if (S_ISDIR(ip->i_d.di_mode)) {
  2324. if (XFS_TEST_ERROR(
  2325. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2326. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  2327. (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
  2328. mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
  2329. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2330. "%s: Bad directory inode %Lu, ptr 0x%p",
  2331. __func__, ip->i_ino, ip);
  2332. goto corrupt_out;
  2333. }
  2334. }
  2335. if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
  2336. ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
  2337. XFS_RANDOM_IFLUSH_5)) {
  2338. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2339. "%s: detected corrupt incore inode %Lu, "
  2340. "total extents = %d, nblocks = %Ld, ptr 0x%p",
  2341. __func__, ip->i_ino,
  2342. ip->i_d.di_nextents + ip->i_d.di_anextents,
  2343. ip->i_d.di_nblocks, ip);
  2344. goto corrupt_out;
  2345. }
  2346. if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
  2347. mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
  2348. xfs_alert_tag(mp, XFS_PTAG_IFLUSH,
  2349. "%s: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
  2350. __func__, ip->i_ino, ip->i_d.di_forkoff, ip);
  2351. goto corrupt_out;
  2352. }
  2353. /*
  2354. * bump the flush iteration count, used to detect flushes which
  2355. * postdate a log record during recovery.
  2356. */
  2357. ip->i_d.di_flushiter++;
  2358. /*
  2359. * Copy the dirty parts of the inode into the on-disk
  2360. * inode. We always copy out the core of the inode,
  2361. * because if the inode is dirty at all the core must
  2362. * be.
  2363. */
  2364. xfs_dinode_to_disk(dip, &ip->i_d);
  2365. /* Wrap, we never let the log put out DI_MAX_FLUSH */
  2366. if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
  2367. ip->i_d.di_flushiter = 0;
  2368. /*
  2369. * If this is really an old format inode and the superblock version
  2370. * has not been updated to support only new format inodes, then
  2371. * convert back to the old inode format. If the superblock version
  2372. * has been updated, then make the conversion permanent.
  2373. */
  2374. ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
  2375. if (ip->i_d.di_version == 1) {
  2376. if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
  2377. /*
  2378. * Convert it back.
  2379. */
  2380. ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
  2381. dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
  2382. } else {
  2383. /*
  2384. * The superblock version has already been bumped,
  2385. * so just make the conversion to the new inode
  2386. * format permanent.
  2387. */
  2388. ip->i_d.di_version = 2;
  2389. dip->di_version = 2;
  2390. ip->i_d.di_onlink = 0;
  2391. dip->di_onlink = 0;
  2392. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  2393. memset(&(dip->di_pad[0]), 0,
  2394. sizeof(dip->di_pad));
  2395. ASSERT(xfs_get_projid(ip) == 0);
  2396. }
  2397. }
  2398. xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
  2399. if (XFS_IFORK_Q(ip))
  2400. xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
  2401. xfs_inobp_check(mp, bp);
  2402. /*
  2403. * We've recorded everything logged in the inode, so we'd like to clear
  2404. * the ili_fields bits so we don't log and flush things unnecessarily.
  2405. * However, we can't stop logging all this information until the data
  2406. * we've copied into the disk buffer is written to disk. If we did we
  2407. * might overwrite the copy of the inode in the log with all the data
  2408. * after re-logging only part of it, and in the face of a crash we
  2409. * wouldn't have all the data we need to recover.
  2410. *
  2411. * What we do is move the bits to the ili_last_fields field. When
  2412. * logging the inode, these bits are moved back to the ili_fields field.
  2413. * In the xfs_iflush_done() routine we clear ili_last_fields, since we
  2414. * know that the information those bits represent is permanently on
  2415. * disk. As long as the flush completes before the inode is logged
  2416. * again, then both ili_fields and ili_last_fields will be cleared.
  2417. *
  2418. * We can play with the ili_fields bits here, because the inode lock
  2419. * must be held exclusively in order to set bits there and the flush
  2420. * lock protects the ili_last_fields bits. Set ili_logged so the flush
  2421. * done routine can tell whether or not to look in the AIL. Also, store
  2422. * the current LSN of the inode so that we can tell whether the item has
  2423. * moved in the AIL from xfs_iflush_done(). In order to read the lsn we
  2424. * need the AIL lock, because it is a 64 bit value that cannot be read
  2425. * atomically.
  2426. */
  2427. if (iip != NULL && iip->ili_fields != 0) {
  2428. iip->ili_last_fields = iip->ili_fields;
  2429. iip->ili_fields = 0;
  2430. iip->ili_logged = 1;
  2431. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  2432. &iip->ili_item.li_lsn);
  2433. /*
  2434. * Attach the function xfs_iflush_done to the inode's
  2435. * buffer. This will remove the inode from the AIL
  2436. * and unlock the inode's flush lock when the inode is
  2437. * completely written to disk.
  2438. */
  2439. xfs_buf_attach_iodone(bp, xfs_iflush_done, &iip->ili_item);
  2440. ASSERT(bp->b_fspriv != NULL);
  2441. ASSERT(bp->b_iodone != NULL);
  2442. } else {
  2443. /*
  2444. * We're flushing an inode which is not in the AIL and has
  2445. * not been logged. For this case we can immediately drop
  2446. * the inode flush lock because we can avoid the whole
  2447. * AIL state thing. It's OK to drop the flush lock now,
  2448. * because we've already locked the buffer and to do anything
  2449. * you really need both.
  2450. */
  2451. if (iip != NULL) {
  2452. ASSERT(iip->ili_logged == 0);
  2453. ASSERT(iip->ili_last_fields == 0);
  2454. ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
  2455. }
  2456. xfs_ifunlock(ip);
  2457. }
  2458. return 0;
  2459. corrupt_out:
  2460. return XFS_ERROR(EFSCORRUPTED);
  2461. }
  2462. /*
  2463. * Return a pointer to the extent record at file index idx.
  2464. */
  2465. xfs_bmbt_rec_host_t *
  2466. xfs_iext_get_ext(
  2467. xfs_ifork_t *ifp, /* inode fork pointer */
  2468. xfs_extnum_t idx) /* index of target extent */
  2469. {
  2470. ASSERT(idx >= 0);
  2471. ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
  2472. if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
  2473. return ifp->if_u1.if_ext_irec->er_extbuf;
  2474. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  2475. xfs_ext_irec_t *erp; /* irec pointer */
  2476. int erp_idx = 0; /* irec index */
  2477. xfs_extnum_t page_idx = idx; /* ext index in target list */
  2478. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  2479. return &erp->er_extbuf[page_idx];
  2480. } else if (ifp->if_bytes) {
  2481. return &ifp->if_u1.if_extents[idx];
  2482. } else {
  2483. return NULL;
  2484. }
  2485. }
  2486. /*
  2487. * Insert new item(s) into the extent records for incore inode
  2488. * fork 'ifp'. 'count' new items are inserted at index 'idx'.
  2489. */
  2490. void
  2491. xfs_iext_insert(
  2492. xfs_inode_t *ip, /* incore inode pointer */
  2493. xfs_extnum_t idx, /* starting index of new items */
  2494. xfs_extnum_t count, /* number of inserted items */
  2495. xfs_bmbt_irec_t *new, /* items to insert */
  2496. int state) /* type of extent conversion */
  2497. {
  2498. xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
  2499. xfs_extnum_t i; /* extent record index */
  2500. trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
  2501. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  2502. xfs_iext_add(ifp, idx, count);
  2503. for (i = idx; i < idx + count; i++, new++)
  2504. xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
  2505. }
  2506. /*
  2507. * This is called when the amount of space required for incore file
  2508. * extents needs to be increased. The ext_diff parameter stores the
  2509. * number of new extents being added and the idx parameter contains
  2510. * the extent index where the new extents will be added. If the new
  2511. * extents are being appended, then we just need to (re)allocate and
  2512. * initialize the space. Otherwise, if the new extents are being
  2513. * inserted into the middle of the existing entries, a bit more work
  2514. * is required to make room for the new extents to be inserted. The
  2515. * caller is responsible for filling in the new extent entries upon
  2516. * return.
  2517. */
  2518. void
  2519. xfs_iext_add(
  2520. xfs_ifork_t *ifp, /* inode fork pointer */
  2521. xfs_extnum_t idx, /* index to begin adding exts */
  2522. int ext_diff) /* number of extents to add */
  2523. {
  2524. int byte_diff; /* new bytes being added */
  2525. int new_size; /* size of extents after adding */
  2526. xfs_extnum_t nextents; /* number of extents in file */
  2527. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2528. ASSERT((idx >= 0) && (idx <= nextents));
  2529. byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
  2530. new_size = ifp->if_bytes + byte_diff;
  2531. /*
  2532. * If the new number of extents (nextents + ext_diff)
  2533. * fits inside the inode, then continue to use the inline
  2534. * extent buffer.
  2535. */
  2536. if (nextents + ext_diff <= XFS_INLINE_EXTS) {
  2537. if (idx < nextents) {
  2538. memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
  2539. &ifp->if_u2.if_inline_ext[idx],
  2540. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  2541. memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
  2542. }
  2543. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  2544. ifp->if_real_bytes = 0;
  2545. }
  2546. /*
  2547. * Otherwise use a linear (direct) extent list.
  2548. * If the extents are currently inside the inode,
  2549. * xfs_iext_realloc_direct will switch us from
  2550. * inline to direct extent allocation mode.
  2551. */
  2552. else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
  2553. xfs_iext_realloc_direct(ifp, new_size);
  2554. if (idx < nextents) {
  2555. memmove(&ifp->if_u1.if_extents[idx + ext_diff],
  2556. &ifp->if_u1.if_extents[idx],
  2557. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  2558. memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
  2559. }
  2560. }
  2561. /* Indirection array */
  2562. else {
  2563. xfs_ext_irec_t *erp;
  2564. int erp_idx = 0;
  2565. int page_idx = idx;
  2566. ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
  2567. if (ifp->if_flags & XFS_IFEXTIREC) {
  2568. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
  2569. } else {
  2570. xfs_iext_irec_init(ifp);
  2571. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2572. erp = ifp->if_u1.if_ext_irec;
  2573. }
  2574. /* Extents fit in target extent page */
  2575. if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
  2576. if (page_idx < erp->er_extcount) {
  2577. memmove(&erp->er_extbuf[page_idx + ext_diff],
  2578. &erp->er_extbuf[page_idx],
  2579. (erp->er_extcount - page_idx) *
  2580. sizeof(xfs_bmbt_rec_t));
  2581. memset(&erp->er_extbuf[page_idx], 0, byte_diff);
  2582. }
  2583. erp->er_extcount += ext_diff;
  2584. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  2585. }
  2586. /* Insert a new extent page */
  2587. else if (erp) {
  2588. xfs_iext_add_indirect_multi(ifp,
  2589. erp_idx, page_idx, ext_diff);
  2590. }
  2591. /*
  2592. * If extent(s) are being appended to the last page in
  2593. * the indirection array and the new extent(s) don't fit
  2594. * in the page, then erp is NULL and erp_idx is set to
  2595. * the next index needed in the indirection array.
  2596. */
  2597. else {
  2598. int count = ext_diff;
  2599. while (count) {
  2600. erp = xfs_iext_irec_new(ifp, erp_idx);
  2601. erp->er_extcount = count;
  2602. count -= MIN(count, (int)XFS_LINEAR_EXTS);
  2603. if (count) {
  2604. erp_idx++;
  2605. }
  2606. }
  2607. }
  2608. }
  2609. ifp->if_bytes = new_size;
  2610. }
  2611. /*
  2612. * This is called when incore extents are being added to the indirection
  2613. * array and the new extents do not fit in the target extent list. The
  2614. * erp_idx parameter contains the irec index for the target extent list
  2615. * in the indirection array, and the idx parameter contains the extent
  2616. * index within the list. The number of extents being added is stored
  2617. * in the count parameter.
  2618. *
  2619. * |-------| |-------|
  2620. * | | | | idx - number of extents before idx
  2621. * | idx | | count |
  2622. * | | | | count - number of extents being inserted at idx
  2623. * |-------| |-------|
  2624. * | count | | nex2 | nex2 - number of extents after idx + count
  2625. * |-------| |-------|
  2626. */
  2627. void
  2628. xfs_iext_add_indirect_multi(
  2629. xfs_ifork_t *ifp, /* inode fork pointer */
  2630. int erp_idx, /* target extent irec index */
  2631. xfs_extnum_t idx, /* index within target list */
  2632. int count) /* new extents being added */
  2633. {
  2634. int byte_diff; /* new bytes being added */
  2635. xfs_ext_irec_t *erp; /* pointer to irec entry */
  2636. xfs_extnum_t ext_diff; /* number of extents to add */
  2637. xfs_extnum_t ext_cnt; /* new extents still needed */
  2638. xfs_extnum_t nex2; /* extents after idx + count */
  2639. xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
  2640. int nlists; /* number of irec's (lists) */
  2641. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2642. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  2643. nex2 = erp->er_extcount - idx;
  2644. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  2645. /*
  2646. * Save second part of target extent list
  2647. * (all extents past */
  2648. if (nex2) {
  2649. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  2650. nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
  2651. memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
  2652. erp->er_extcount -= nex2;
  2653. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
  2654. memset(&erp->er_extbuf[idx], 0, byte_diff);
  2655. }
  2656. /*
  2657. * Add the new extents to the end of the target
  2658. * list, then allocate new irec record(s) and
  2659. * extent buffer(s) as needed to store the rest
  2660. * of the new extents.
  2661. */
  2662. ext_cnt = count;
  2663. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
  2664. if (ext_diff) {
  2665. erp->er_extcount += ext_diff;
  2666. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  2667. ext_cnt -= ext_diff;
  2668. }
  2669. while (ext_cnt) {
  2670. erp_idx++;
  2671. erp = xfs_iext_irec_new(ifp, erp_idx);
  2672. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
  2673. erp->er_extcount = ext_diff;
  2674. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  2675. ext_cnt -= ext_diff;
  2676. }
  2677. /* Add nex2 extents back to indirection array */
  2678. if (nex2) {
  2679. xfs_extnum_t ext_avail;
  2680. int i;
  2681. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  2682. ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
  2683. i = 0;
  2684. /*
  2685. * If nex2 extents fit in the current page, append
  2686. * nex2_ep after the new extents.
  2687. */
  2688. if (nex2 <= ext_avail) {
  2689. i = erp->er_extcount;
  2690. }
  2691. /*
  2692. * Otherwise, check if space is available in the
  2693. * next page.
  2694. */
  2695. else if ((erp_idx < nlists - 1) &&
  2696. (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
  2697. ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
  2698. erp_idx++;
  2699. erp++;
  2700. /* Create a hole for nex2 extents */
  2701. memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
  2702. erp->er_extcount * sizeof(xfs_bmbt_rec_t));
  2703. }
  2704. /*
  2705. * Final choice, create a new extent page for
  2706. * nex2 extents.
  2707. */
  2708. else {
  2709. erp_idx++;
  2710. erp = xfs_iext_irec_new(ifp, erp_idx);
  2711. }
  2712. memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
  2713. kmem_free(nex2_ep);
  2714. erp->er_extcount += nex2;
  2715. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
  2716. }
  2717. }
  2718. /*
  2719. * This is called when the amount of space required for incore file
  2720. * extents needs to be decreased. The ext_diff parameter stores the
  2721. * number of extents to be removed and the idx parameter contains
  2722. * the extent index where the extents will be removed from.
  2723. *
  2724. * If the amount of space needed has decreased below the linear
  2725. * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
  2726. * extent array. Otherwise, use kmem_realloc() to adjust the
  2727. * size to what is needed.
  2728. */
  2729. void
  2730. xfs_iext_remove(
  2731. xfs_inode_t *ip, /* incore inode pointer */
  2732. xfs_extnum_t idx, /* index to begin removing exts */
  2733. int ext_diff, /* number of extents to remove */
  2734. int state) /* type of extent conversion */
  2735. {
  2736. xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
  2737. xfs_extnum_t nextents; /* number of extents in file */
  2738. int new_size; /* size of extents after removal */
  2739. trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
  2740. ASSERT(ext_diff > 0);
  2741. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2742. new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
  2743. if (new_size == 0) {
  2744. xfs_iext_destroy(ifp);
  2745. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  2746. xfs_iext_remove_indirect(ifp, idx, ext_diff);
  2747. } else if (ifp->if_real_bytes) {
  2748. xfs_iext_remove_direct(ifp, idx, ext_diff);
  2749. } else {
  2750. xfs_iext_remove_inline(ifp, idx, ext_diff);
  2751. }
  2752. ifp->if_bytes = new_size;
  2753. }
  2754. /*
  2755. * This removes ext_diff extents from the inline buffer, beginning
  2756. * at extent index idx.
  2757. */
  2758. void
  2759. xfs_iext_remove_inline(
  2760. xfs_ifork_t *ifp, /* inode fork pointer */
  2761. xfs_extnum_t idx, /* index to begin removing exts */
  2762. int ext_diff) /* number of extents to remove */
  2763. {
  2764. int nextents; /* number of extents in file */
  2765. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  2766. ASSERT(idx < XFS_INLINE_EXTS);
  2767. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2768. ASSERT(((nextents - ext_diff) > 0) &&
  2769. (nextents - ext_diff) < XFS_INLINE_EXTS);
  2770. if (idx + ext_diff < nextents) {
  2771. memmove(&ifp->if_u2.if_inline_ext[idx],
  2772. &ifp->if_u2.if_inline_ext[idx + ext_diff],
  2773. (nextents - (idx + ext_diff)) *
  2774. sizeof(xfs_bmbt_rec_t));
  2775. memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
  2776. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  2777. } else {
  2778. memset(&ifp->if_u2.if_inline_ext[idx], 0,
  2779. ext_diff * sizeof(xfs_bmbt_rec_t));
  2780. }
  2781. }
  2782. /*
  2783. * This removes ext_diff extents from a linear (direct) extent list,
  2784. * beginning at extent index idx. If the extents are being removed
  2785. * from the end of the list (ie. truncate) then we just need to re-
  2786. * allocate the list to remove the extra space. Otherwise, if the
  2787. * extents are being removed from the middle of the existing extent
  2788. * entries, then we first need to move the extent records beginning
  2789. * at idx + ext_diff up in the list to overwrite the records being
  2790. * removed, then remove the extra space via kmem_realloc.
  2791. */
  2792. void
  2793. xfs_iext_remove_direct(
  2794. xfs_ifork_t *ifp, /* inode fork pointer */
  2795. xfs_extnum_t idx, /* index to begin removing exts */
  2796. int ext_diff) /* number of extents to remove */
  2797. {
  2798. xfs_extnum_t nextents; /* number of extents in file */
  2799. int new_size; /* size of extents after removal */
  2800. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  2801. new_size = ifp->if_bytes -
  2802. (ext_diff * sizeof(xfs_bmbt_rec_t));
  2803. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2804. if (new_size == 0) {
  2805. xfs_iext_destroy(ifp);
  2806. return;
  2807. }
  2808. /* Move extents up in the list (if needed) */
  2809. if (idx + ext_diff < nextents) {
  2810. memmove(&ifp->if_u1.if_extents[idx],
  2811. &ifp->if_u1.if_extents[idx + ext_diff],
  2812. (nextents - (idx + ext_diff)) *
  2813. sizeof(xfs_bmbt_rec_t));
  2814. }
  2815. memset(&ifp->if_u1.if_extents[nextents - ext_diff],
  2816. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  2817. /*
  2818. * Reallocate the direct extent list. If the extents
  2819. * will fit inside the inode then xfs_iext_realloc_direct
  2820. * will switch from direct to inline extent allocation
  2821. * mode for us.
  2822. */
  2823. xfs_iext_realloc_direct(ifp, new_size);
  2824. ifp->if_bytes = new_size;
  2825. }
  2826. /*
  2827. * This is called when incore extents are being removed from the
  2828. * indirection array and the extents being removed span multiple extent
  2829. * buffers. The idx parameter contains the file extent index where we
  2830. * want to begin removing extents, and the count parameter contains
  2831. * how many extents need to be removed.
  2832. *
  2833. * |-------| |-------|
  2834. * | nex1 | | | nex1 - number of extents before idx
  2835. * |-------| | count |
  2836. * | | | | count - number of extents being removed at idx
  2837. * | count | |-------|
  2838. * | | | nex2 | nex2 - number of extents after idx + count
  2839. * |-------| |-------|
  2840. */
  2841. void
  2842. xfs_iext_remove_indirect(
  2843. xfs_ifork_t *ifp, /* inode fork pointer */
  2844. xfs_extnum_t idx, /* index to begin removing extents */
  2845. int count) /* number of extents to remove */
  2846. {
  2847. xfs_ext_irec_t *erp; /* indirection array pointer */
  2848. int erp_idx = 0; /* indirection array index */
  2849. xfs_extnum_t ext_cnt; /* extents left to remove */
  2850. xfs_extnum_t ext_diff; /* extents to remove in current list */
  2851. xfs_extnum_t nex1; /* number of extents before idx */
  2852. xfs_extnum_t nex2; /* extents after idx + count */
  2853. int page_idx = idx; /* index in target extent list */
  2854. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  2855. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  2856. ASSERT(erp != NULL);
  2857. nex1 = page_idx;
  2858. ext_cnt = count;
  2859. while (ext_cnt) {
  2860. nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
  2861. ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
  2862. /*
  2863. * Check for deletion of entire list;
  2864. * xfs_iext_irec_remove() updates extent offsets.
  2865. */
  2866. if (ext_diff == erp->er_extcount) {
  2867. xfs_iext_irec_remove(ifp, erp_idx);
  2868. ext_cnt -= ext_diff;
  2869. nex1 = 0;
  2870. if (ext_cnt) {
  2871. ASSERT(erp_idx < ifp->if_real_bytes /
  2872. XFS_IEXT_BUFSZ);
  2873. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  2874. nex1 = 0;
  2875. continue;
  2876. } else {
  2877. break;
  2878. }
  2879. }
  2880. /* Move extents up (if needed) */
  2881. if (nex2) {
  2882. memmove(&erp->er_extbuf[nex1],
  2883. &erp->er_extbuf[nex1 + ext_diff],
  2884. nex2 * sizeof(xfs_bmbt_rec_t));
  2885. }
  2886. /* Zero out rest of page */
  2887. memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
  2888. ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
  2889. /* Update remaining counters */
  2890. erp->er_extcount -= ext_diff;
  2891. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
  2892. ext_cnt -= ext_diff;
  2893. nex1 = 0;
  2894. erp_idx++;
  2895. erp++;
  2896. }
  2897. ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
  2898. xfs_iext_irec_compact(ifp);
  2899. }
  2900. /*
  2901. * Create, destroy, or resize a linear (direct) block of extents.
  2902. */
  2903. void
  2904. xfs_iext_realloc_direct(
  2905. xfs_ifork_t *ifp, /* inode fork pointer */
  2906. int new_size) /* new size of extents */
  2907. {
  2908. int rnew_size; /* real new size of extents */
  2909. rnew_size = new_size;
  2910. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
  2911. ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
  2912. (new_size != ifp->if_real_bytes)));
  2913. /* Free extent records */
  2914. if (new_size == 0) {
  2915. xfs_iext_destroy(ifp);
  2916. }
  2917. /* Resize direct extent list and zero any new bytes */
  2918. else if (ifp->if_real_bytes) {
  2919. /* Check if extents will fit inside the inode */
  2920. if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
  2921. xfs_iext_direct_to_inline(ifp, new_size /
  2922. (uint)sizeof(xfs_bmbt_rec_t));
  2923. ifp->if_bytes = new_size;
  2924. return;
  2925. }
  2926. if (!is_power_of_2(new_size)){
  2927. rnew_size = roundup_pow_of_two(new_size);
  2928. }
  2929. if (rnew_size != ifp->if_real_bytes) {
  2930. ifp->if_u1.if_extents =
  2931. kmem_realloc(ifp->if_u1.if_extents,
  2932. rnew_size,
  2933. ifp->if_real_bytes, KM_NOFS);
  2934. }
  2935. if (rnew_size > ifp->if_real_bytes) {
  2936. memset(&ifp->if_u1.if_extents[ifp->if_bytes /
  2937. (uint)sizeof(xfs_bmbt_rec_t)], 0,
  2938. rnew_size - ifp->if_real_bytes);
  2939. }
  2940. }
  2941. /*
  2942. * Switch from the inline extent buffer to a direct
  2943. * extent list. Be sure to include the inline extent
  2944. * bytes in new_size.
  2945. */
  2946. else {
  2947. new_size += ifp->if_bytes;
  2948. if (!is_power_of_2(new_size)) {
  2949. rnew_size = roundup_pow_of_two(new_size);
  2950. }
  2951. xfs_iext_inline_to_direct(ifp, rnew_size);
  2952. }
  2953. ifp->if_real_bytes = rnew_size;
  2954. ifp->if_bytes = new_size;
  2955. }
  2956. /*
  2957. * Switch from linear (direct) extent records to inline buffer.
  2958. */
  2959. void
  2960. xfs_iext_direct_to_inline(
  2961. xfs_ifork_t *ifp, /* inode fork pointer */
  2962. xfs_extnum_t nextents) /* number of extents in file */
  2963. {
  2964. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  2965. ASSERT(nextents <= XFS_INLINE_EXTS);
  2966. /*
  2967. * The inline buffer was zeroed when we switched
  2968. * from inline to direct extent allocation mode,
  2969. * so we don't need to clear it here.
  2970. */
  2971. memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
  2972. nextents * sizeof(xfs_bmbt_rec_t));
  2973. kmem_free(ifp->if_u1.if_extents);
  2974. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  2975. ifp->if_real_bytes = 0;
  2976. }
  2977. /*
  2978. * Switch from inline buffer to linear (direct) extent records.
  2979. * new_size should already be rounded up to the next power of 2
  2980. * by the caller (when appropriate), so use new_size as it is.
  2981. * However, since new_size may be rounded up, we can't update
  2982. * if_bytes here. It is the caller's responsibility to update
  2983. * if_bytes upon return.
  2984. */
  2985. void
  2986. xfs_iext_inline_to_direct(
  2987. xfs_ifork_t *ifp, /* inode fork pointer */
  2988. int new_size) /* number of extents in file */
  2989. {
  2990. ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
  2991. memset(ifp->if_u1.if_extents, 0, new_size);
  2992. if (ifp->if_bytes) {
  2993. memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
  2994. ifp->if_bytes);
  2995. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  2996. sizeof(xfs_bmbt_rec_t));
  2997. }
  2998. ifp->if_real_bytes = new_size;
  2999. }
  3000. /*
  3001. * Resize an extent indirection array to new_size bytes.
  3002. */
  3003. STATIC void
  3004. xfs_iext_realloc_indirect(
  3005. xfs_ifork_t *ifp, /* inode fork pointer */
  3006. int new_size) /* new indirection array size */
  3007. {
  3008. int nlists; /* number of irec's (ex lists) */
  3009. int size; /* current indirection array size */
  3010. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3011. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3012. size = nlists * sizeof(xfs_ext_irec_t);
  3013. ASSERT(ifp->if_real_bytes);
  3014. ASSERT((new_size >= 0) && (new_size != size));
  3015. if (new_size == 0) {
  3016. xfs_iext_destroy(ifp);
  3017. } else {
  3018. ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
  3019. kmem_realloc(ifp->if_u1.if_ext_irec,
  3020. new_size, size, KM_NOFS);
  3021. }
  3022. }
  3023. /*
  3024. * Switch from indirection array to linear (direct) extent allocations.
  3025. */
  3026. STATIC void
  3027. xfs_iext_indirect_to_direct(
  3028. xfs_ifork_t *ifp) /* inode fork pointer */
  3029. {
  3030. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3031. xfs_extnum_t nextents; /* number of extents in file */
  3032. int size; /* size of file extents */
  3033. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3034. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3035. ASSERT(nextents <= XFS_LINEAR_EXTS);
  3036. size = nextents * sizeof(xfs_bmbt_rec_t);
  3037. xfs_iext_irec_compact_pages(ifp);
  3038. ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
  3039. ep = ifp->if_u1.if_ext_irec->er_extbuf;
  3040. kmem_free(ifp->if_u1.if_ext_irec);
  3041. ifp->if_flags &= ~XFS_IFEXTIREC;
  3042. ifp->if_u1.if_extents = ep;
  3043. ifp->if_bytes = size;
  3044. if (nextents < XFS_LINEAR_EXTS) {
  3045. xfs_iext_realloc_direct(ifp, size);
  3046. }
  3047. }
  3048. /*
  3049. * Free incore file extents.
  3050. */
  3051. void
  3052. xfs_iext_destroy(
  3053. xfs_ifork_t *ifp) /* inode fork pointer */
  3054. {
  3055. if (ifp->if_flags & XFS_IFEXTIREC) {
  3056. int erp_idx;
  3057. int nlists;
  3058. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3059. for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
  3060. xfs_iext_irec_remove(ifp, erp_idx);
  3061. }
  3062. ifp->if_flags &= ~XFS_IFEXTIREC;
  3063. } else if (ifp->if_real_bytes) {
  3064. kmem_free(ifp->if_u1.if_extents);
  3065. } else if (ifp->if_bytes) {
  3066. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  3067. sizeof(xfs_bmbt_rec_t));
  3068. }
  3069. ifp->if_u1.if_extents = NULL;
  3070. ifp->if_real_bytes = 0;
  3071. ifp->if_bytes = 0;
  3072. }
  3073. /*
  3074. * Return a pointer to the extent record for file system block bno.
  3075. */
  3076. xfs_bmbt_rec_host_t * /* pointer to found extent record */
  3077. xfs_iext_bno_to_ext(
  3078. xfs_ifork_t *ifp, /* inode fork pointer */
  3079. xfs_fileoff_t bno, /* block number to search for */
  3080. xfs_extnum_t *idxp) /* index of target extent */
  3081. {
  3082. xfs_bmbt_rec_host_t *base; /* pointer to first extent */
  3083. xfs_filblks_t blockcount = 0; /* number of blocks in extent */
  3084. xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
  3085. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  3086. int high; /* upper boundary in search */
  3087. xfs_extnum_t idx = 0; /* index of target extent */
  3088. int low; /* lower boundary in search */
  3089. xfs_extnum_t nextents; /* number of file extents */
  3090. xfs_fileoff_t startoff = 0; /* start offset of extent */
  3091. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3092. if (nextents == 0) {
  3093. *idxp = 0;
  3094. return NULL;
  3095. }
  3096. low = 0;
  3097. if (ifp->if_flags & XFS_IFEXTIREC) {
  3098. /* Find target extent list */
  3099. int erp_idx = 0;
  3100. erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
  3101. base = erp->er_extbuf;
  3102. high = erp->er_extcount - 1;
  3103. } else {
  3104. base = ifp->if_u1.if_extents;
  3105. high = nextents - 1;
  3106. }
  3107. /* Binary search extent records */
  3108. while (low <= high) {
  3109. idx = (low + high) >> 1;
  3110. ep = base + idx;
  3111. startoff = xfs_bmbt_get_startoff(ep);
  3112. blockcount = xfs_bmbt_get_blockcount(ep);
  3113. if (bno < startoff) {
  3114. high = idx - 1;
  3115. } else if (bno >= startoff + blockcount) {
  3116. low = idx + 1;
  3117. } else {
  3118. /* Convert back to file-based extent index */
  3119. if (ifp->if_flags & XFS_IFEXTIREC) {
  3120. idx += erp->er_extoff;
  3121. }
  3122. *idxp = idx;
  3123. return ep;
  3124. }
  3125. }
  3126. /* Convert back to file-based extent index */
  3127. if (ifp->if_flags & XFS_IFEXTIREC) {
  3128. idx += erp->er_extoff;
  3129. }
  3130. if (bno >= startoff + blockcount) {
  3131. if (++idx == nextents) {
  3132. ep = NULL;
  3133. } else {
  3134. ep = xfs_iext_get_ext(ifp, idx);
  3135. }
  3136. }
  3137. *idxp = idx;
  3138. return ep;
  3139. }
  3140. /*
  3141. * Return a pointer to the indirection array entry containing the
  3142. * extent record for filesystem block bno. Store the index of the
  3143. * target irec in *erp_idxp.
  3144. */
  3145. xfs_ext_irec_t * /* pointer to found extent record */
  3146. xfs_iext_bno_to_irec(
  3147. xfs_ifork_t *ifp, /* inode fork pointer */
  3148. xfs_fileoff_t bno, /* block number to search for */
  3149. int *erp_idxp) /* irec index of target ext list */
  3150. {
  3151. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  3152. xfs_ext_irec_t *erp_next; /* next indirection array entry */
  3153. int erp_idx; /* indirection array index */
  3154. int nlists; /* number of extent irec's (lists) */
  3155. int high; /* binary search upper limit */
  3156. int low; /* binary search lower limit */
  3157. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3158. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3159. erp_idx = 0;
  3160. low = 0;
  3161. high = nlists - 1;
  3162. while (low <= high) {
  3163. erp_idx = (low + high) >> 1;
  3164. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3165. erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
  3166. if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
  3167. high = erp_idx - 1;
  3168. } else if (erp_next && bno >=
  3169. xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
  3170. low = erp_idx + 1;
  3171. } else {
  3172. break;
  3173. }
  3174. }
  3175. *erp_idxp = erp_idx;
  3176. return erp;
  3177. }
  3178. /*
  3179. * Return a pointer to the indirection array entry containing the
  3180. * extent record at file extent index *idxp. Store the index of the
  3181. * target irec in *erp_idxp and store the page index of the target
  3182. * extent record in *idxp.
  3183. */
  3184. xfs_ext_irec_t *
  3185. xfs_iext_idx_to_irec(
  3186. xfs_ifork_t *ifp, /* inode fork pointer */
  3187. xfs_extnum_t *idxp, /* extent index (file -> page) */
  3188. int *erp_idxp, /* pointer to target irec */
  3189. int realloc) /* new bytes were just added */
  3190. {
  3191. xfs_ext_irec_t *prev; /* pointer to previous irec */
  3192. xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
  3193. int erp_idx; /* indirection array index */
  3194. int nlists; /* number of irec's (ex lists) */
  3195. int high; /* binary search upper limit */
  3196. int low; /* binary search lower limit */
  3197. xfs_extnum_t page_idx = *idxp; /* extent index in target list */
  3198. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3199. ASSERT(page_idx >= 0);
  3200. ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
  3201. ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
  3202. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3203. erp_idx = 0;
  3204. low = 0;
  3205. high = nlists - 1;
  3206. /* Binary search extent irec's */
  3207. while (low <= high) {
  3208. erp_idx = (low + high) >> 1;
  3209. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3210. prev = erp_idx > 0 ? erp - 1 : NULL;
  3211. if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
  3212. realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
  3213. high = erp_idx - 1;
  3214. } else if (page_idx > erp->er_extoff + erp->er_extcount ||
  3215. (page_idx == erp->er_extoff + erp->er_extcount &&
  3216. !realloc)) {
  3217. low = erp_idx + 1;
  3218. } else if (page_idx == erp->er_extoff + erp->er_extcount &&
  3219. erp->er_extcount == XFS_LINEAR_EXTS) {
  3220. ASSERT(realloc);
  3221. page_idx = 0;
  3222. erp_idx++;
  3223. erp = erp_idx < nlists ? erp + 1 : NULL;
  3224. break;
  3225. } else {
  3226. page_idx -= erp->er_extoff;
  3227. break;
  3228. }
  3229. }
  3230. *idxp = page_idx;
  3231. *erp_idxp = erp_idx;
  3232. return(erp);
  3233. }
  3234. /*
  3235. * Allocate and initialize an indirection array once the space needed
  3236. * for incore extents increases above XFS_IEXT_BUFSZ.
  3237. */
  3238. void
  3239. xfs_iext_irec_init(
  3240. xfs_ifork_t *ifp) /* inode fork pointer */
  3241. {
  3242. xfs_ext_irec_t *erp; /* indirection array pointer */
  3243. xfs_extnum_t nextents; /* number of extents in file */
  3244. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  3245. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3246. ASSERT(nextents <= XFS_LINEAR_EXTS);
  3247. erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
  3248. if (nextents == 0) {
  3249. ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  3250. } else if (!ifp->if_real_bytes) {
  3251. xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
  3252. } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
  3253. xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
  3254. }
  3255. erp->er_extbuf = ifp->if_u1.if_extents;
  3256. erp->er_extcount = nextents;
  3257. erp->er_extoff = 0;
  3258. ifp->if_flags |= XFS_IFEXTIREC;
  3259. ifp->if_real_bytes = XFS_IEXT_BUFSZ;
  3260. ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
  3261. ifp->if_u1.if_ext_irec = erp;
  3262. return;
  3263. }
  3264. /*
  3265. * Allocate and initialize a new entry in the indirection array.
  3266. */
  3267. xfs_ext_irec_t *
  3268. xfs_iext_irec_new(
  3269. xfs_ifork_t *ifp, /* inode fork pointer */
  3270. int erp_idx) /* index for new irec */
  3271. {
  3272. xfs_ext_irec_t *erp; /* indirection array pointer */
  3273. int i; /* loop counter */
  3274. int nlists; /* number of irec's (ex lists) */
  3275. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3276. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3277. /* Resize indirection array */
  3278. xfs_iext_realloc_indirect(ifp, ++nlists *
  3279. sizeof(xfs_ext_irec_t));
  3280. /*
  3281. * Move records down in the array so the
  3282. * new page can use erp_idx.
  3283. */
  3284. erp = ifp->if_u1.if_ext_irec;
  3285. for (i = nlists - 1; i > erp_idx; i--) {
  3286. memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
  3287. }
  3288. ASSERT(i == erp_idx);
  3289. /* Initialize new extent record */
  3290. erp = ifp->if_u1.if_ext_irec;
  3291. erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  3292. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  3293. memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
  3294. erp[erp_idx].er_extcount = 0;
  3295. erp[erp_idx].er_extoff = erp_idx > 0 ?
  3296. erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
  3297. return (&erp[erp_idx]);
  3298. }
  3299. /*
  3300. * Remove a record from the indirection array.
  3301. */
  3302. void
  3303. xfs_iext_irec_remove(
  3304. xfs_ifork_t *ifp, /* inode fork pointer */
  3305. int erp_idx) /* irec index to remove */
  3306. {
  3307. xfs_ext_irec_t *erp; /* indirection array pointer */
  3308. int i; /* loop counter */
  3309. int nlists; /* number of irec's (ex lists) */
  3310. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3311. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3312. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3313. if (erp->er_extbuf) {
  3314. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
  3315. -erp->er_extcount);
  3316. kmem_free(erp->er_extbuf);
  3317. }
  3318. /* Compact extent records */
  3319. erp = ifp->if_u1.if_ext_irec;
  3320. for (i = erp_idx; i < nlists - 1; i++) {
  3321. memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
  3322. }
  3323. /*
  3324. * Manually free the last extent record from the indirection
  3325. * array. A call to xfs_iext_realloc_indirect() with a size
  3326. * of zero would result in a call to xfs_iext_destroy() which
  3327. * would in turn call this function again, creating a nasty
  3328. * infinite loop.
  3329. */
  3330. if (--nlists) {
  3331. xfs_iext_realloc_indirect(ifp,
  3332. nlists * sizeof(xfs_ext_irec_t));
  3333. } else {
  3334. kmem_free(ifp->if_u1.if_ext_irec);
  3335. }
  3336. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  3337. }
  3338. /*
  3339. * This is called to clean up large amounts of unused memory allocated
  3340. * by the indirection array. Before compacting anything though, verify
  3341. * that the indirection array is still needed and switch back to the
  3342. * linear extent list (or even the inline buffer) if possible. The
  3343. * compaction policy is as follows:
  3344. *
  3345. * Full Compaction: Extents fit into a single page (or inline buffer)
  3346. * Partial Compaction: Extents occupy less than 50% of allocated space
  3347. * No Compaction: Extents occupy at least 50% of allocated space
  3348. */
  3349. void
  3350. xfs_iext_irec_compact(
  3351. xfs_ifork_t *ifp) /* inode fork pointer */
  3352. {
  3353. xfs_extnum_t nextents; /* number of extents in file */
  3354. int nlists; /* number of irec's (ex lists) */
  3355. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3356. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3357. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3358. if (nextents == 0) {
  3359. xfs_iext_destroy(ifp);
  3360. } else if (nextents <= XFS_INLINE_EXTS) {
  3361. xfs_iext_indirect_to_direct(ifp);
  3362. xfs_iext_direct_to_inline(ifp, nextents);
  3363. } else if (nextents <= XFS_LINEAR_EXTS) {
  3364. xfs_iext_indirect_to_direct(ifp);
  3365. } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
  3366. xfs_iext_irec_compact_pages(ifp);
  3367. }
  3368. }
  3369. /*
  3370. * Combine extents from neighboring extent pages.
  3371. */
  3372. void
  3373. xfs_iext_irec_compact_pages(
  3374. xfs_ifork_t *ifp) /* inode fork pointer */
  3375. {
  3376. xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
  3377. int erp_idx = 0; /* indirection array index */
  3378. int nlists; /* number of irec's (ex lists) */
  3379. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3380. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3381. while (erp_idx < nlists - 1) {
  3382. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3383. erp_next = erp + 1;
  3384. if (erp_next->er_extcount <=
  3385. (XFS_LINEAR_EXTS - erp->er_extcount)) {
  3386. memcpy(&erp->er_extbuf[erp->er_extcount],
  3387. erp_next->er_extbuf, erp_next->er_extcount *
  3388. sizeof(xfs_bmbt_rec_t));
  3389. erp->er_extcount += erp_next->er_extcount;
  3390. /*
  3391. * Free page before removing extent record
  3392. * so er_extoffs don't get modified in
  3393. * xfs_iext_irec_remove.
  3394. */
  3395. kmem_free(erp_next->er_extbuf);
  3396. erp_next->er_extbuf = NULL;
  3397. xfs_iext_irec_remove(ifp, erp_idx + 1);
  3398. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3399. } else {
  3400. erp_idx++;
  3401. }
  3402. }
  3403. }
  3404. /*
  3405. * This is called to update the er_extoff field in the indirection
  3406. * array when extents have been added or removed from one of the
  3407. * extent lists. erp_idx contains the irec index to begin updating
  3408. * at and ext_diff contains the number of extents that were added
  3409. * or removed.
  3410. */
  3411. void
  3412. xfs_iext_irec_update_extoffs(
  3413. xfs_ifork_t *ifp, /* inode fork pointer */
  3414. int erp_idx, /* irec index to update */
  3415. int ext_diff) /* number of new extents */
  3416. {
  3417. int i; /* loop counter */
  3418. int nlists; /* number of irec's (ex lists */
  3419. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3420. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3421. for (i = erp_idx; i < nlists; i++) {
  3422. ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
  3423. }
  3424. }