xfs_inode.c 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433
  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_dir2.h"
  30. #include "xfs_dmapi.h"
  31. #include "xfs_mount.h"
  32. #include "xfs_bmap_btree.h"
  33. #include "xfs_alloc_btree.h"
  34. #include "xfs_ialloc_btree.h"
  35. #include "xfs_dir2_sf.h"
  36. #include "xfs_attr_sf.h"
  37. #include "xfs_dinode.h"
  38. #include "xfs_inode.h"
  39. #include "xfs_buf_item.h"
  40. #include "xfs_inode_item.h"
  41. #include "xfs_btree.h"
  42. #include "xfs_btree_trace.h"
  43. #include "xfs_alloc.h"
  44. #include "xfs_ialloc.h"
  45. #include "xfs_bmap.h"
  46. #include "xfs_rw.h"
  47. #include "xfs_error.h"
  48. #include "xfs_utils.h"
  49. #include "xfs_dir2_trace.h"
  50. #include "xfs_quota.h"
  51. #include "xfs_acl.h"
  52. #include "xfs_filestream.h"
  53. #include "xfs_vnodeops.h"
  54. kmem_zone_t *xfs_ifork_zone;
  55. kmem_zone_t *xfs_inode_zone;
  56. /*
  57. * Used in xfs_itruncate(). This is the maximum number of extents
  58. * freed from a file in a single transaction.
  59. */
  60. #define XFS_ITRUNC_MAX_EXTENTS 2
  61. STATIC int xfs_iflush_int(xfs_inode_t *, xfs_buf_t *);
  62. STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
  63. STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
  64. STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
  65. #ifdef DEBUG
  66. /*
  67. * Make sure that the extents in the given memory buffer
  68. * are valid.
  69. */
  70. STATIC void
  71. xfs_validate_extents(
  72. xfs_ifork_t *ifp,
  73. int nrecs,
  74. xfs_exntfmt_t fmt)
  75. {
  76. xfs_bmbt_irec_t irec;
  77. xfs_bmbt_rec_host_t rec;
  78. int i;
  79. for (i = 0; i < nrecs; i++) {
  80. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  81. rec.l0 = get_unaligned(&ep->l0);
  82. rec.l1 = get_unaligned(&ep->l1);
  83. xfs_bmbt_get_all(&rec, &irec);
  84. if (fmt == XFS_EXTFMT_NOSTATE)
  85. ASSERT(irec.br_state == XFS_EXT_NORM);
  86. }
  87. }
  88. #else /* DEBUG */
  89. #define xfs_validate_extents(ifp, nrecs, fmt)
  90. #endif /* DEBUG */
  91. /*
  92. * Check that none of the inode's in the buffer have a next
  93. * unlinked field of 0.
  94. */
  95. #if defined(DEBUG)
  96. void
  97. xfs_inobp_check(
  98. xfs_mount_t *mp,
  99. xfs_buf_t *bp)
  100. {
  101. int i;
  102. int j;
  103. xfs_dinode_t *dip;
  104. j = mp->m_inode_cluster_size >> mp->m_sb.sb_inodelog;
  105. for (i = 0; i < j; i++) {
  106. dip = (xfs_dinode_t *)xfs_buf_offset(bp,
  107. i * mp->m_sb.sb_inodesize);
  108. if (!dip->di_next_unlinked) {
  109. xfs_fs_cmn_err(CE_ALERT, mp,
  110. "Detected a bogus zero next_unlinked field in incore inode buffer 0x%p. About to pop an ASSERT.",
  111. bp);
  112. ASSERT(dip->di_next_unlinked);
  113. }
  114. }
  115. }
  116. #endif
  117. /*
  118. * Find the buffer associated with the given inode map
  119. * We do basic validation checks on the buffer once it has been
  120. * retrieved from disk.
  121. */
  122. STATIC int
  123. xfs_imap_to_bp(
  124. xfs_mount_t *mp,
  125. xfs_trans_t *tp,
  126. struct xfs_imap *imap,
  127. xfs_buf_t **bpp,
  128. uint buf_flags,
  129. uint imap_flags)
  130. {
  131. int error;
  132. int i;
  133. int ni;
  134. xfs_buf_t *bp;
  135. error = xfs_trans_read_buf(mp, tp, mp->m_ddev_targp, imap->im_blkno,
  136. (int)imap->im_len, buf_flags, &bp);
  137. if (error) {
  138. if (error != EAGAIN) {
  139. cmn_err(CE_WARN,
  140. "xfs_imap_to_bp: xfs_trans_read_buf()returned "
  141. "an error %d on %s. Returning error.",
  142. error, mp->m_fsname);
  143. } else {
  144. ASSERT(buf_flags & XFS_BUF_TRYLOCK);
  145. }
  146. return error;
  147. }
  148. /*
  149. * Validate the magic number and version of every inode in the buffer
  150. * (if DEBUG kernel) or the first inode in the buffer, otherwise.
  151. */
  152. #ifdef DEBUG
  153. ni = BBTOB(imap->im_len) >> mp->m_sb.sb_inodelog;
  154. #else /* usual case */
  155. ni = 1;
  156. #endif
  157. for (i = 0; i < ni; i++) {
  158. int di_ok;
  159. xfs_dinode_t *dip;
  160. dip = (xfs_dinode_t *)xfs_buf_offset(bp,
  161. (i << mp->m_sb.sb_inodelog));
  162. di_ok = be16_to_cpu(dip->di_magic) == XFS_DINODE_MAGIC &&
  163. XFS_DINODE_GOOD_VERSION(dip->di_version);
  164. if (unlikely(XFS_TEST_ERROR(!di_ok, mp,
  165. XFS_ERRTAG_ITOBP_INOTOBP,
  166. XFS_RANDOM_ITOBP_INOTOBP))) {
  167. if (imap_flags & XFS_IMAP_BULKSTAT) {
  168. xfs_trans_brelse(tp, bp);
  169. return XFS_ERROR(EINVAL);
  170. }
  171. XFS_CORRUPTION_ERROR("xfs_imap_to_bp",
  172. XFS_ERRLEVEL_HIGH, mp, dip);
  173. #ifdef DEBUG
  174. cmn_err(CE_PANIC,
  175. "Device %s - bad inode magic/vsn "
  176. "daddr %lld #%d (magic=%x)",
  177. XFS_BUFTARG_NAME(mp->m_ddev_targp),
  178. (unsigned long long)imap->im_blkno, i,
  179. be16_to_cpu(dip->di_magic));
  180. #endif
  181. xfs_trans_brelse(tp, bp);
  182. return XFS_ERROR(EFSCORRUPTED);
  183. }
  184. }
  185. xfs_inobp_check(mp, bp);
  186. /*
  187. * Mark the buffer as an inode buffer now that it looks good
  188. */
  189. XFS_BUF_SET_VTYPE(bp, B_FS_INO);
  190. *bpp = bp;
  191. return 0;
  192. }
  193. /*
  194. * This routine is called to map an inode number within a file
  195. * system to the buffer containing the on-disk version of the
  196. * inode. It returns a pointer to the buffer containing the
  197. * on-disk inode in the bpp parameter, and in the dip parameter
  198. * it returns a pointer to the on-disk inode within that buffer.
  199. *
  200. * If a non-zero error is returned, then the contents of bpp and
  201. * dipp are undefined.
  202. *
  203. * Use xfs_imap() to determine the size and location of the
  204. * buffer to read from disk.
  205. */
  206. int
  207. xfs_inotobp(
  208. xfs_mount_t *mp,
  209. xfs_trans_t *tp,
  210. xfs_ino_t ino,
  211. xfs_dinode_t **dipp,
  212. xfs_buf_t **bpp,
  213. int *offset,
  214. uint imap_flags)
  215. {
  216. struct xfs_imap imap;
  217. xfs_buf_t *bp;
  218. int error;
  219. imap.im_blkno = 0;
  220. error = xfs_imap(mp, tp, ino, &imap, imap_flags);
  221. if (error)
  222. return error;
  223. error = xfs_imap_to_bp(mp, tp, &imap, &bp, XFS_BUF_LOCK, imap_flags);
  224. if (error)
  225. return error;
  226. *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, imap.im_boffset);
  227. *bpp = bp;
  228. *offset = imap.im_boffset;
  229. return 0;
  230. }
  231. /*
  232. * This routine is called to map an inode to the buffer containing
  233. * the on-disk version of the inode. It returns a pointer to the
  234. * buffer containing the on-disk inode in the bpp parameter, and in
  235. * the dip parameter it returns a pointer to the on-disk inode within
  236. * that buffer.
  237. *
  238. * If a non-zero error is returned, then the contents of bpp and
  239. * dipp are undefined.
  240. *
  241. * The inode is expected to already been mapped to its buffer and read
  242. * in once, thus we can use the mapping information stored in the inode
  243. * rather than calling xfs_imap(). This allows us to avoid the overhead
  244. * of looking at the inode btree for small block file systems
  245. * (see xfs_imap()).
  246. */
  247. int
  248. xfs_itobp(
  249. xfs_mount_t *mp,
  250. xfs_trans_t *tp,
  251. xfs_inode_t *ip,
  252. xfs_dinode_t **dipp,
  253. xfs_buf_t **bpp,
  254. uint buf_flags)
  255. {
  256. xfs_buf_t *bp;
  257. int error;
  258. ASSERT(ip->i_imap.im_blkno != 0);
  259. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp, buf_flags, 0);
  260. if (error)
  261. return error;
  262. if (!bp) {
  263. ASSERT(buf_flags & XFS_BUF_TRYLOCK);
  264. ASSERT(tp == NULL);
  265. *bpp = NULL;
  266. return EAGAIN;
  267. }
  268. *dipp = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
  269. *bpp = bp;
  270. return 0;
  271. }
  272. /*
  273. * Move inode type and inode format specific information from the
  274. * on-disk inode to the in-core inode. For fifos, devs, and sockets
  275. * this means set if_rdev to the proper value. For files, directories,
  276. * and symlinks this means to bring in the in-line data or extent
  277. * pointers. For a file in B-tree format, only the root is immediately
  278. * brought in-core. The rest will be in-lined in if_extents when it
  279. * is first referenced (see xfs_iread_extents()).
  280. */
  281. STATIC int
  282. xfs_iformat(
  283. xfs_inode_t *ip,
  284. xfs_dinode_t *dip)
  285. {
  286. xfs_attr_shortform_t *atp;
  287. int size;
  288. int error;
  289. xfs_fsize_t di_size;
  290. ip->i_df.if_ext_max =
  291. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  292. error = 0;
  293. if (unlikely(be32_to_cpu(dip->di_nextents) +
  294. be16_to_cpu(dip->di_anextents) >
  295. be64_to_cpu(dip->di_nblocks))) {
  296. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  297. "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
  298. (unsigned long long)ip->i_ino,
  299. (int)(be32_to_cpu(dip->di_nextents) +
  300. be16_to_cpu(dip->di_anextents)),
  301. (unsigned long long)
  302. be64_to_cpu(dip->di_nblocks));
  303. XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
  304. ip->i_mount, dip);
  305. return XFS_ERROR(EFSCORRUPTED);
  306. }
  307. if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
  308. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  309. "corrupt dinode %Lu, forkoff = 0x%x.",
  310. (unsigned long long)ip->i_ino,
  311. dip->di_forkoff);
  312. XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
  313. ip->i_mount, dip);
  314. return XFS_ERROR(EFSCORRUPTED);
  315. }
  316. switch (ip->i_d.di_mode & S_IFMT) {
  317. case S_IFIFO:
  318. case S_IFCHR:
  319. case S_IFBLK:
  320. case S_IFSOCK:
  321. if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
  322. XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
  323. ip->i_mount, dip);
  324. return XFS_ERROR(EFSCORRUPTED);
  325. }
  326. ip->i_d.di_size = 0;
  327. ip->i_size = 0;
  328. ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
  329. break;
  330. case S_IFREG:
  331. case S_IFLNK:
  332. case S_IFDIR:
  333. switch (dip->di_format) {
  334. case XFS_DINODE_FMT_LOCAL:
  335. /*
  336. * no local regular files yet
  337. */
  338. if (unlikely((be16_to_cpu(dip->di_mode) & S_IFMT) == S_IFREG)) {
  339. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  340. "corrupt inode %Lu "
  341. "(local format for regular file).",
  342. (unsigned long long) ip->i_ino);
  343. XFS_CORRUPTION_ERROR("xfs_iformat(4)",
  344. XFS_ERRLEVEL_LOW,
  345. ip->i_mount, dip);
  346. return XFS_ERROR(EFSCORRUPTED);
  347. }
  348. di_size = be64_to_cpu(dip->di_size);
  349. if (unlikely(di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
  350. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  351. "corrupt inode %Lu "
  352. "(bad size %Ld for local inode).",
  353. (unsigned long long) ip->i_ino,
  354. (long long) di_size);
  355. XFS_CORRUPTION_ERROR("xfs_iformat(5)",
  356. XFS_ERRLEVEL_LOW,
  357. ip->i_mount, dip);
  358. return XFS_ERROR(EFSCORRUPTED);
  359. }
  360. size = (int)di_size;
  361. error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
  362. break;
  363. case XFS_DINODE_FMT_EXTENTS:
  364. error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
  365. break;
  366. case XFS_DINODE_FMT_BTREE:
  367. error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
  368. break;
  369. default:
  370. XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
  371. ip->i_mount);
  372. return XFS_ERROR(EFSCORRUPTED);
  373. }
  374. break;
  375. default:
  376. XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
  377. return XFS_ERROR(EFSCORRUPTED);
  378. }
  379. if (error) {
  380. return error;
  381. }
  382. if (!XFS_DFORK_Q(dip))
  383. return 0;
  384. ASSERT(ip->i_afp == NULL);
  385. ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
  386. ip->i_afp->if_ext_max =
  387. XFS_IFORK_ASIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  388. switch (dip->di_aformat) {
  389. case XFS_DINODE_FMT_LOCAL:
  390. atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
  391. size = be16_to_cpu(atp->hdr.totsize);
  392. error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
  393. break;
  394. case XFS_DINODE_FMT_EXTENTS:
  395. error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
  396. break;
  397. case XFS_DINODE_FMT_BTREE:
  398. error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
  399. break;
  400. default:
  401. error = XFS_ERROR(EFSCORRUPTED);
  402. break;
  403. }
  404. if (error) {
  405. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  406. ip->i_afp = NULL;
  407. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  408. }
  409. return error;
  410. }
  411. /*
  412. * The file is in-lined in the on-disk inode.
  413. * If it fits into if_inline_data, then copy
  414. * it there, otherwise allocate a buffer for it
  415. * and copy the data there. Either way, set
  416. * if_data to point at the data.
  417. * If we allocate a buffer for the data, make
  418. * sure that its size is a multiple of 4 and
  419. * record the real size in i_real_bytes.
  420. */
  421. STATIC int
  422. xfs_iformat_local(
  423. xfs_inode_t *ip,
  424. xfs_dinode_t *dip,
  425. int whichfork,
  426. int size)
  427. {
  428. xfs_ifork_t *ifp;
  429. int real_size;
  430. /*
  431. * If the size is unreasonable, then something
  432. * is wrong and we just bail out rather than crash in
  433. * kmem_alloc() or memcpy() below.
  434. */
  435. if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  436. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  437. "corrupt inode %Lu "
  438. "(bad size %d for local fork, size = %d).",
  439. (unsigned long long) ip->i_ino, size,
  440. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
  441. XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
  442. ip->i_mount, dip);
  443. return XFS_ERROR(EFSCORRUPTED);
  444. }
  445. ifp = XFS_IFORK_PTR(ip, whichfork);
  446. real_size = 0;
  447. if (size == 0)
  448. ifp->if_u1.if_data = NULL;
  449. else if (size <= sizeof(ifp->if_u2.if_inline_data))
  450. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  451. else {
  452. real_size = roundup(size, 4);
  453. ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
  454. }
  455. ifp->if_bytes = size;
  456. ifp->if_real_bytes = real_size;
  457. if (size)
  458. memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
  459. ifp->if_flags &= ~XFS_IFEXTENTS;
  460. ifp->if_flags |= XFS_IFINLINE;
  461. return 0;
  462. }
  463. /*
  464. * The file consists of a set of extents all
  465. * of which fit into the on-disk inode.
  466. * If there are few enough extents to fit into
  467. * the if_inline_ext, then copy them there.
  468. * Otherwise allocate a buffer for them and copy
  469. * them into it. Either way, set if_extents
  470. * to point at the extents.
  471. */
  472. STATIC int
  473. xfs_iformat_extents(
  474. xfs_inode_t *ip,
  475. xfs_dinode_t *dip,
  476. int whichfork)
  477. {
  478. xfs_bmbt_rec_t *dp;
  479. xfs_ifork_t *ifp;
  480. int nex;
  481. int size;
  482. int i;
  483. ifp = XFS_IFORK_PTR(ip, whichfork);
  484. nex = XFS_DFORK_NEXTENTS(dip, whichfork);
  485. size = nex * (uint)sizeof(xfs_bmbt_rec_t);
  486. /*
  487. * If the number of extents is unreasonable, then something
  488. * is wrong and we just bail out rather than crash in
  489. * kmem_alloc() or memcpy() below.
  490. */
  491. if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
  492. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  493. "corrupt inode %Lu ((a)extents = %d).",
  494. (unsigned long long) ip->i_ino, nex);
  495. XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
  496. ip->i_mount, dip);
  497. return XFS_ERROR(EFSCORRUPTED);
  498. }
  499. ifp->if_real_bytes = 0;
  500. if (nex == 0)
  501. ifp->if_u1.if_extents = NULL;
  502. else if (nex <= XFS_INLINE_EXTS)
  503. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  504. else
  505. xfs_iext_add(ifp, 0, nex);
  506. ifp->if_bytes = size;
  507. if (size) {
  508. dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
  509. xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
  510. for (i = 0; i < nex; i++, dp++) {
  511. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  512. ep->l0 = get_unaligned_be64(&dp->l0);
  513. ep->l1 = get_unaligned_be64(&dp->l1);
  514. }
  515. XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
  516. if (whichfork != XFS_DATA_FORK ||
  517. XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
  518. if (unlikely(xfs_check_nostate_extents(
  519. ifp, 0, nex))) {
  520. XFS_ERROR_REPORT("xfs_iformat_extents(2)",
  521. XFS_ERRLEVEL_LOW,
  522. ip->i_mount);
  523. return XFS_ERROR(EFSCORRUPTED);
  524. }
  525. }
  526. ifp->if_flags |= XFS_IFEXTENTS;
  527. return 0;
  528. }
  529. /*
  530. * The file has too many extents to fit into
  531. * the inode, so they are in B-tree format.
  532. * Allocate a buffer for the root of the B-tree
  533. * and copy the root into it. The i_extents
  534. * field will remain NULL until all of the
  535. * extents are read in (when they are needed).
  536. */
  537. STATIC int
  538. xfs_iformat_btree(
  539. xfs_inode_t *ip,
  540. xfs_dinode_t *dip,
  541. int whichfork)
  542. {
  543. xfs_bmdr_block_t *dfp;
  544. xfs_ifork_t *ifp;
  545. /* REFERENCED */
  546. int nrecs;
  547. int size;
  548. ifp = XFS_IFORK_PTR(ip, whichfork);
  549. dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
  550. size = XFS_BMAP_BROOT_SPACE(dfp);
  551. nrecs = be16_to_cpu(dfp->bb_numrecs);
  552. /*
  553. * blow out if -- fork has less extents than can fit in
  554. * fork (fork shouldn't be a btree format), root btree
  555. * block has more records than can fit into the fork,
  556. * or the number of extents is greater than the number of
  557. * blocks.
  558. */
  559. if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max
  560. || XFS_BMDR_SPACE_CALC(nrecs) >
  561. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork)
  562. || XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
  563. xfs_fs_repair_cmn_err(CE_WARN, ip->i_mount,
  564. "corrupt inode %Lu (btree).",
  565. (unsigned long long) ip->i_ino);
  566. XFS_ERROR_REPORT("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
  567. ip->i_mount);
  568. return XFS_ERROR(EFSCORRUPTED);
  569. }
  570. ifp->if_broot_bytes = size;
  571. ifp->if_broot = kmem_alloc(size, KM_SLEEP);
  572. ASSERT(ifp->if_broot != NULL);
  573. /*
  574. * Copy and convert from the on-disk structure
  575. * to the in-memory structure.
  576. */
  577. xfs_bmdr_to_bmbt(ip->i_mount, dfp,
  578. XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
  579. ifp->if_broot, size);
  580. ifp->if_flags &= ~XFS_IFEXTENTS;
  581. ifp->if_flags |= XFS_IFBROOT;
  582. return 0;
  583. }
  584. void
  585. xfs_dinode_from_disk(
  586. xfs_icdinode_t *to,
  587. xfs_dinode_t *from)
  588. {
  589. to->di_magic = be16_to_cpu(from->di_magic);
  590. to->di_mode = be16_to_cpu(from->di_mode);
  591. to->di_version = from ->di_version;
  592. to->di_format = from->di_format;
  593. to->di_onlink = be16_to_cpu(from->di_onlink);
  594. to->di_uid = be32_to_cpu(from->di_uid);
  595. to->di_gid = be32_to_cpu(from->di_gid);
  596. to->di_nlink = be32_to_cpu(from->di_nlink);
  597. to->di_projid = be16_to_cpu(from->di_projid);
  598. memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
  599. to->di_flushiter = be16_to_cpu(from->di_flushiter);
  600. to->di_atime.t_sec = be32_to_cpu(from->di_atime.t_sec);
  601. to->di_atime.t_nsec = be32_to_cpu(from->di_atime.t_nsec);
  602. to->di_mtime.t_sec = be32_to_cpu(from->di_mtime.t_sec);
  603. to->di_mtime.t_nsec = be32_to_cpu(from->di_mtime.t_nsec);
  604. to->di_ctime.t_sec = be32_to_cpu(from->di_ctime.t_sec);
  605. to->di_ctime.t_nsec = be32_to_cpu(from->di_ctime.t_nsec);
  606. to->di_size = be64_to_cpu(from->di_size);
  607. to->di_nblocks = be64_to_cpu(from->di_nblocks);
  608. to->di_extsize = be32_to_cpu(from->di_extsize);
  609. to->di_nextents = be32_to_cpu(from->di_nextents);
  610. to->di_anextents = be16_to_cpu(from->di_anextents);
  611. to->di_forkoff = from->di_forkoff;
  612. to->di_aformat = from->di_aformat;
  613. to->di_dmevmask = be32_to_cpu(from->di_dmevmask);
  614. to->di_dmstate = be16_to_cpu(from->di_dmstate);
  615. to->di_flags = be16_to_cpu(from->di_flags);
  616. to->di_gen = be32_to_cpu(from->di_gen);
  617. }
  618. void
  619. xfs_dinode_to_disk(
  620. xfs_dinode_t *to,
  621. xfs_icdinode_t *from)
  622. {
  623. to->di_magic = cpu_to_be16(from->di_magic);
  624. to->di_mode = cpu_to_be16(from->di_mode);
  625. to->di_version = from ->di_version;
  626. to->di_format = from->di_format;
  627. to->di_onlink = cpu_to_be16(from->di_onlink);
  628. to->di_uid = cpu_to_be32(from->di_uid);
  629. to->di_gid = cpu_to_be32(from->di_gid);
  630. to->di_nlink = cpu_to_be32(from->di_nlink);
  631. to->di_projid = cpu_to_be16(from->di_projid);
  632. memcpy(to->di_pad, from->di_pad, sizeof(to->di_pad));
  633. to->di_flushiter = cpu_to_be16(from->di_flushiter);
  634. to->di_atime.t_sec = cpu_to_be32(from->di_atime.t_sec);
  635. to->di_atime.t_nsec = cpu_to_be32(from->di_atime.t_nsec);
  636. to->di_mtime.t_sec = cpu_to_be32(from->di_mtime.t_sec);
  637. to->di_mtime.t_nsec = cpu_to_be32(from->di_mtime.t_nsec);
  638. to->di_ctime.t_sec = cpu_to_be32(from->di_ctime.t_sec);
  639. to->di_ctime.t_nsec = cpu_to_be32(from->di_ctime.t_nsec);
  640. to->di_size = cpu_to_be64(from->di_size);
  641. to->di_nblocks = cpu_to_be64(from->di_nblocks);
  642. to->di_extsize = cpu_to_be32(from->di_extsize);
  643. to->di_nextents = cpu_to_be32(from->di_nextents);
  644. to->di_anextents = cpu_to_be16(from->di_anextents);
  645. to->di_forkoff = from->di_forkoff;
  646. to->di_aformat = from->di_aformat;
  647. to->di_dmevmask = cpu_to_be32(from->di_dmevmask);
  648. to->di_dmstate = cpu_to_be16(from->di_dmstate);
  649. to->di_flags = cpu_to_be16(from->di_flags);
  650. to->di_gen = cpu_to_be32(from->di_gen);
  651. }
  652. STATIC uint
  653. _xfs_dic2xflags(
  654. __uint16_t di_flags)
  655. {
  656. uint flags = 0;
  657. if (di_flags & XFS_DIFLAG_ANY) {
  658. if (di_flags & XFS_DIFLAG_REALTIME)
  659. flags |= XFS_XFLAG_REALTIME;
  660. if (di_flags & XFS_DIFLAG_PREALLOC)
  661. flags |= XFS_XFLAG_PREALLOC;
  662. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  663. flags |= XFS_XFLAG_IMMUTABLE;
  664. if (di_flags & XFS_DIFLAG_APPEND)
  665. flags |= XFS_XFLAG_APPEND;
  666. if (di_flags & XFS_DIFLAG_SYNC)
  667. flags |= XFS_XFLAG_SYNC;
  668. if (di_flags & XFS_DIFLAG_NOATIME)
  669. flags |= XFS_XFLAG_NOATIME;
  670. if (di_flags & XFS_DIFLAG_NODUMP)
  671. flags |= XFS_XFLAG_NODUMP;
  672. if (di_flags & XFS_DIFLAG_RTINHERIT)
  673. flags |= XFS_XFLAG_RTINHERIT;
  674. if (di_flags & XFS_DIFLAG_PROJINHERIT)
  675. flags |= XFS_XFLAG_PROJINHERIT;
  676. if (di_flags & XFS_DIFLAG_NOSYMLINKS)
  677. flags |= XFS_XFLAG_NOSYMLINKS;
  678. if (di_flags & XFS_DIFLAG_EXTSIZE)
  679. flags |= XFS_XFLAG_EXTSIZE;
  680. if (di_flags & XFS_DIFLAG_EXTSZINHERIT)
  681. flags |= XFS_XFLAG_EXTSZINHERIT;
  682. if (di_flags & XFS_DIFLAG_NODEFRAG)
  683. flags |= XFS_XFLAG_NODEFRAG;
  684. if (di_flags & XFS_DIFLAG_FILESTREAM)
  685. flags |= XFS_XFLAG_FILESTREAM;
  686. }
  687. return flags;
  688. }
  689. uint
  690. xfs_ip2xflags(
  691. xfs_inode_t *ip)
  692. {
  693. xfs_icdinode_t *dic = &ip->i_d;
  694. return _xfs_dic2xflags(dic->di_flags) |
  695. (XFS_IFORK_Q(ip) ? XFS_XFLAG_HASATTR : 0);
  696. }
  697. uint
  698. xfs_dic2xflags(
  699. xfs_dinode_t *dip)
  700. {
  701. return _xfs_dic2xflags(be16_to_cpu(dip->di_flags)) |
  702. (XFS_DFORK_Q(dip) ? XFS_XFLAG_HASATTR : 0);
  703. }
  704. /*
  705. * Allocate and initialise an xfs_inode.
  706. */
  707. STATIC struct xfs_inode *
  708. xfs_inode_alloc(
  709. struct xfs_mount *mp,
  710. xfs_ino_t ino)
  711. {
  712. struct xfs_inode *ip;
  713. /*
  714. * if this didn't occur in transactions, we could use
  715. * KM_MAYFAIL and return NULL here on ENOMEM. Set the
  716. * code up to do this anyway.
  717. */
  718. ip = kmem_zone_alloc(xfs_inode_zone, KM_SLEEP);
  719. if (!ip)
  720. return NULL;
  721. ASSERT(atomic_read(&ip->i_iocount) == 0);
  722. ASSERT(atomic_read(&ip->i_pincount) == 0);
  723. ASSERT(!spin_is_locked(&ip->i_flags_lock));
  724. ASSERT(completion_done(&ip->i_flush));
  725. /*
  726. * initialise the VFS inode here to get failures
  727. * out of the way early.
  728. */
  729. if (!inode_init_always(mp->m_super, VFS_I(ip))) {
  730. kmem_zone_free(xfs_inode_zone, ip);
  731. return NULL;
  732. }
  733. /* initialise the xfs inode */
  734. ip->i_ino = ino;
  735. ip->i_mount = mp;
  736. memset(&ip->i_imap, 0, sizeof(struct xfs_imap));
  737. ip->i_afp = NULL;
  738. memset(&ip->i_df, 0, sizeof(xfs_ifork_t));
  739. ip->i_flags = 0;
  740. ip->i_update_core = 0;
  741. ip->i_update_size = 0;
  742. ip->i_delayed_blks = 0;
  743. memset(&ip->i_d, 0, sizeof(xfs_icdinode_t));
  744. ip->i_size = 0;
  745. ip->i_new_size = 0;
  746. /*
  747. * Initialize inode's trace buffers.
  748. */
  749. #ifdef XFS_INODE_TRACE
  750. ip->i_trace = ktrace_alloc(INODE_TRACE_SIZE, KM_NOFS);
  751. #endif
  752. #ifdef XFS_BMAP_TRACE
  753. ip->i_xtrace = ktrace_alloc(XFS_BMAP_KTRACE_SIZE, KM_NOFS);
  754. #endif
  755. #ifdef XFS_BTREE_TRACE
  756. ip->i_btrace = ktrace_alloc(XFS_BMBT_KTRACE_SIZE, KM_NOFS);
  757. #endif
  758. #ifdef XFS_RW_TRACE
  759. ip->i_rwtrace = ktrace_alloc(XFS_RW_KTRACE_SIZE, KM_NOFS);
  760. #endif
  761. #ifdef XFS_ILOCK_TRACE
  762. ip->i_lock_trace = ktrace_alloc(XFS_ILOCK_KTRACE_SIZE, KM_NOFS);
  763. #endif
  764. #ifdef XFS_DIR2_TRACE
  765. ip->i_dir_trace = ktrace_alloc(XFS_DIR2_KTRACE_SIZE, KM_NOFS);
  766. #endif
  767. return ip;
  768. }
  769. /*
  770. * Given a mount structure and an inode number, return a pointer
  771. * to a newly allocated in-core inode corresponding to the given
  772. * inode number.
  773. *
  774. * Initialize the inode's attributes and extent pointers if it
  775. * already has them (it will not if the inode has no links).
  776. */
  777. int
  778. xfs_iread(
  779. xfs_mount_t *mp,
  780. xfs_trans_t *tp,
  781. xfs_ino_t ino,
  782. xfs_inode_t **ipp,
  783. xfs_daddr_t bno,
  784. uint imap_flags)
  785. {
  786. xfs_buf_t *bp;
  787. xfs_dinode_t *dip;
  788. xfs_inode_t *ip;
  789. int error;
  790. ip = xfs_inode_alloc(mp, ino);
  791. if (!ip)
  792. return ENOMEM;
  793. /*
  794. * Fill in the location information in the in-core inode.
  795. */
  796. ip->i_imap.im_blkno = bno;
  797. error = xfs_imap(mp, tp, ip->i_ino, &ip->i_imap, imap_flags);
  798. if (error)
  799. goto out_destroy_inode;
  800. ASSERT(bno == 0 || bno == ip->i_imap.im_blkno);
  801. /*
  802. * Get pointers to the on-disk inode and the buffer containing it.
  803. */
  804. error = xfs_imap_to_bp(mp, tp, &ip->i_imap, &bp,
  805. XFS_BUF_LOCK, imap_flags);
  806. if (error)
  807. goto out_destroy_inode;
  808. dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
  809. /*
  810. * If we got something that isn't an inode it means someone
  811. * (nfs or dmi) has a stale handle.
  812. */
  813. if (be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC) {
  814. #ifdef DEBUG
  815. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_iread: "
  816. "dip->di_magic (0x%x) != "
  817. "XFS_DINODE_MAGIC (0x%x)",
  818. be16_to_cpu(dip->di_magic),
  819. XFS_DINODE_MAGIC);
  820. #endif /* DEBUG */
  821. error = XFS_ERROR(EINVAL);
  822. goto out_brelse;
  823. }
  824. /*
  825. * If the on-disk inode is already linked to a directory
  826. * entry, copy all of the inode into the in-core inode.
  827. * xfs_iformat() handles copying in the inode format
  828. * specific information.
  829. * Otherwise, just get the truly permanent information.
  830. */
  831. if (dip->di_mode) {
  832. xfs_dinode_from_disk(&ip->i_d, dip);
  833. error = xfs_iformat(ip, dip);
  834. if (error) {
  835. #ifdef DEBUG
  836. xfs_fs_cmn_err(CE_ALERT, mp, "xfs_iread: "
  837. "xfs_iformat() returned error %d",
  838. error);
  839. #endif /* DEBUG */
  840. goto out_brelse;
  841. }
  842. } else {
  843. ip->i_d.di_magic = be16_to_cpu(dip->di_magic);
  844. ip->i_d.di_version = dip->di_version;
  845. ip->i_d.di_gen = be32_to_cpu(dip->di_gen);
  846. ip->i_d.di_flushiter = be16_to_cpu(dip->di_flushiter);
  847. /*
  848. * Make sure to pull in the mode here as well in
  849. * case the inode is released without being used.
  850. * This ensures that xfs_inactive() will see that
  851. * the inode is already free and not try to mess
  852. * with the uninitialized part of it.
  853. */
  854. ip->i_d.di_mode = 0;
  855. /*
  856. * Initialize the per-fork minima and maxima for a new
  857. * inode here. xfs_iformat will do it for old inodes.
  858. */
  859. ip->i_df.if_ext_max =
  860. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  861. }
  862. /*
  863. * The inode format changed when we moved the link count and
  864. * made it 32 bits long. If this is an old format inode,
  865. * convert it in memory to look like a new one. If it gets
  866. * flushed to disk we will convert back before flushing or
  867. * logging it. We zero out the new projid field and the old link
  868. * count field. We'll handle clearing the pad field (the remains
  869. * of the old uuid field) when we actually convert the inode to
  870. * the new format. We don't change the version number so that we
  871. * can distinguish this from a real new format inode.
  872. */
  873. if (ip->i_d.di_version == 1) {
  874. ip->i_d.di_nlink = ip->i_d.di_onlink;
  875. ip->i_d.di_onlink = 0;
  876. ip->i_d.di_projid = 0;
  877. }
  878. ip->i_delayed_blks = 0;
  879. ip->i_size = ip->i_d.di_size;
  880. /*
  881. * Mark the buffer containing the inode as something to keep
  882. * around for a while. This helps to keep recently accessed
  883. * meta-data in-core longer.
  884. */
  885. XFS_BUF_SET_REF(bp, XFS_INO_REF);
  886. /*
  887. * Use xfs_trans_brelse() to release the buffer containing the
  888. * on-disk inode, because it was acquired with xfs_trans_read_buf()
  889. * in xfs_itobp() above. If tp is NULL, this is just a normal
  890. * brelse(). If we're within a transaction, then xfs_trans_brelse()
  891. * will only release the buffer if it is not dirty within the
  892. * transaction. It will be OK to release the buffer in this case,
  893. * because inodes on disk are never destroyed and we will be
  894. * locking the new in-core inode before putting it in the hash
  895. * table where other processes can find it. Thus we don't have
  896. * to worry about the inode being changed just because we released
  897. * the buffer.
  898. */
  899. xfs_trans_brelse(tp, bp);
  900. *ipp = ip;
  901. return 0;
  902. out_brelse:
  903. xfs_trans_brelse(tp, bp);
  904. out_destroy_inode:
  905. xfs_destroy_inode(ip);
  906. return error;
  907. }
  908. /*
  909. * Read in extents from a btree-format inode.
  910. * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
  911. */
  912. int
  913. xfs_iread_extents(
  914. xfs_trans_t *tp,
  915. xfs_inode_t *ip,
  916. int whichfork)
  917. {
  918. int error;
  919. xfs_ifork_t *ifp;
  920. xfs_extnum_t nextents;
  921. size_t size;
  922. if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
  923. XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
  924. ip->i_mount);
  925. return XFS_ERROR(EFSCORRUPTED);
  926. }
  927. nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
  928. size = nextents * sizeof(xfs_bmbt_rec_t);
  929. ifp = XFS_IFORK_PTR(ip, whichfork);
  930. /*
  931. * We know that the size is valid (it's checked in iformat_btree)
  932. */
  933. ifp->if_lastex = NULLEXTNUM;
  934. ifp->if_bytes = ifp->if_real_bytes = 0;
  935. ifp->if_flags |= XFS_IFEXTENTS;
  936. xfs_iext_add(ifp, 0, nextents);
  937. error = xfs_bmap_read_extents(tp, ip, whichfork);
  938. if (error) {
  939. xfs_iext_destroy(ifp);
  940. ifp->if_flags &= ~XFS_IFEXTENTS;
  941. return error;
  942. }
  943. xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
  944. return 0;
  945. }
  946. /*
  947. * Allocate an inode on disk and return a copy of its in-core version.
  948. * The in-core inode is locked exclusively. Set mode, nlink, and rdev
  949. * appropriately within the inode. The uid and gid for the inode are
  950. * set according to the contents of the given cred structure.
  951. *
  952. * Use xfs_dialloc() to allocate the on-disk inode. If xfs_dialloc()
  953. * has a free inode available, call xfs_iget()
  954. * to obtain the in-core version of the allocated inode. Finally,
  955. * fill in the inode and log its initial contents. In this case,
  956. * ialloc_context would be set to NULL and call_again set to false.
  957. *
  958. * If xfs_dialloc() does not have an available inode,
  959. * it will replenish its supply by doing an allocation. Since we can
  960. * only do one allocation within a transaction without deadlocks, we
  961. * must commit the current transaction before returning the inode itself.
  962. * In this case, therefore, we will set call_again to true and return.
  963. * The caller should then commit the current transaction, start a new
  964. * transaction, and call xfs_ialloc() again to actually get the inode.
  965. *
  966. * To ensure that some other process does not grab the inode that
  967. * was allocated during the first call to xfs_ialloc(), this routine
  968. * also returns the [locked] bp pointing to the head of the freelist
  969. * as ialloc_context. The caller should hold this buffer across
  970. * the commit and pass it back into this routine on the second call.
  971. *
  972. * If we are allocating quota inodes, we do not have a parent inode
  973. * to attach to or associate with (i.e. pip == NULL) because they
  974. * are not linked into the directory structure - they are attached
  975. * directly to the superblock - and so have no parent.
  976. */
  977. int
  978. xfs_ialloc(
  979. xfs_trans_t *tp,
  980. xfs_inode_t *pip,
  981. mode_t mode,
  982. xfs_nlink_t nlink,
  983. xfs_dev_t rdev,
  984. cred_t *cr,
  985. xfs_prid_t prid,
  986. int okalloc,
  987. xfs_buf_t **ialloc_context,
  988. boolean_t *call_again,
  989. xfs_inode_t **ipp)
  990. {
  991. xfs_ino_t ino;
  992. xfs_inode_t *ip;
  993. uint flags;
  994. int error;
  995. timespec_t tv;
  996. int filestreams = 0;
  997. /*
  998. * Call the space management code to pick
  999. * the on-disk inode to be allocated.
  1000. */
  1001. error = xfs_dialloc(tp, pip ? pip->i_ino : 0, mode, okalloc,
  1002. ialloc_context, call_again, &ino);
  1003. if (error)
  1004. return error;
  1005. if (*call_again || ino == NULLFSINO) {
  1006. *ipp = NULL;
  1007. return 0;
  1008. }
  1009. ASSERT(*ialloc_context == NULL);
  1010. /*
  1011. * Get the in-core inode with the lock held exclusively.
  1012. * This is because we're setting fields here we need
  1013. * to prevent others from looking at until we're done.
  1014. */
  1015. error = xfs_trans_iget(tp->t_mountp, tp, ino,
  1016. XFS_IGET_CREATE, XFS_ILOCK_EXCL, &ip);
  1017. if (error)
  1018. return error;
  1019. ASSERT(ip != NULL);
  1020. ip->i_d.di_mode = (__uint16_t)mode;
  1021. ip->i_d.di_onlink = 0;
  1022. ip->i_d.di_nlink = nlink;
  1023. ASSERT(ip->i_d.di_nlink == nlink);
  1024. ip->i_d.di_uid = current_fsuid();
  1025. ip->i_d.di_gid = current_fsgid();
  1026. ip->i_d.di_projid = prid;
  1027. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  1028. /*
  1029. * If the superblock version is up to where we support new format
  1030. * inodes and this is currently an old format inode, then change
  1031. * the inode version number now. This way we only do the conversion
  1032. * here rather than here and in the flush/logging code.
  1033. */
  1034. if (xfs_sb_version_hasnlink(&tp->t_mountp->m_sb) &&
  1035. ip->i_d.di_version == 1) {
  1036. ip->i_d.di_version = 2;
  1037. /*
  1038. * We've already zeroed the old link count, the projid field,
  1039. * and the pad field.
  1040. */
  1041. }
  1042. /*
  1043. * Project ids won't be stored on disk if we are using a version 1 inode.
  1044. */
  1045. if ((prid != 0) && (ip->i_d.di_version == 1))
  1046. xfs_bump_ino_vers2(tp, ip);
  1047. if (pip && XFS_INHERIT_GID(pip)) {
  1048. ip->i_d.di_gid = pip->i_d.di_gid;
  1049. if ((pip->i_d.di_mode & S_ISGID) && (mode & S_IFMT) == S_IFDIR) {
  1050. ip->i_d.di_mode |= S_ISGID;
  1051. }
  1052. }
  1053. /*
  1054. * If the group ID of the new file does not match the effective group
  1055. * ID or one of the supplementary group IDs, the S_ISGID bit is cleared
  1056. * (and only if the irix_sgid_inherit compatibility variable is set).
  1057. */
  1058. if ((irix_sgid_inherit) &&
  1059. (ip->i_d.di_mode & S_ISGID) &&
  1060. (!in_group_p((gid_t)ip->i_d.di_gid))) {
  1061. ip->i_d.di_mode &= ~S_ISGID;
  1062. }
  1063. ip->i_d.di_size = 0;
  1064. ip->i_size = 0;
  1065. ip->i_d.di_nextents = 0;
  1066. ASSERT(ip->i_d.di_nblocks == 0);
  1067. nanotime(&tv);
  1068. ip->i_d.di_mtime.t_sec = (__int32_t)tv.tv_sec;
  1069. ip->i_d.di_mtime.t_nsec = (__int32_t)tv.tv_nsec;
  1070. ip->i_d.di_atime = ip->i_d.di_mtime;
  1071. ip->i_d.di_ctime = ip->i_d.di_mtime;
  1072. /*
  1073. * di_gen will have been taken care of in xfs_iread.
  1074. */
  1075. ip->i_d.di_extsize = 0;
  1076. ip->i_d.di_dmevmask = 0;
  1077. ip->i_d.di_dmstate = 0;
  1078. ip->i_d.di_flags = 0;
  1079. flags = XFS_ILOG_CORE;
  1080. switch (mode & S_IFMT) {
  1081. case S_IFIFO:
  1082. case S_IFCHR:
  1083. case S_IFBLK:
  1084. case S_IFSOCK:
  1085. ip->i_d.di_format = XFS_DINODE_FMT_DEV;
  1086. ip->i_df.if_u2.if_rdev = rdev;
  1087. ip->i_df.if_flags = 0;
  1088. flags |= XFS_ILOG_DEV;
  1089. break;
  1090. case S_IFREG:
  1091. /*
  1092. * we can't set up filestreams until after the VFS inode
  1093. * is set up properly.
  1094. */
  1095. if (pip && xfs_inode_is_filestream(pip))
  1096. filestreams = 1;
  1097. /* fall through */
  1098. case S_IFDIR:
  1099. if (pip && (pip->i_d.di_flags & XFS_DIFLAG_ANY)) {
  1100. uint di_flags = 0;
  1101. if ((mode & S_IFMT) == S_IFDIR) {
  1102. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  1103. di_flags |= XFS_DIFLAG_RTINHERIT;
  1104. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  1105. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  1106. ip->i_d.di_extsize = pip->i_d.di_extsize;
  1107. }
  1108. } else if ((mode & S_IFMT) == S_IFREG) {
  1109. if (pip->i_d.di_flags & XFS_DIFLAG_RTINHERIT)
  1110. di_flags |= XFS_DIFLAG_REALTIME;
  1111. if (pip->i_d.di_flags & XFS_DIFLAG_EXTSZINHERIT) {
  1112. di_flags |= XFS_DIFLAG_EXTSIZE;
  1113. ip->i_d.di_extsize = pip->i_d.di_extsize;
  1114. }
  1115. }
  1116. if ((pip->i_d.di_flags & XFS_DIFLAG_NOATIME) &&
  1117. xfs_inherit_noatime)
  1118. di_flags |= XFS_DIFLAG_NOATIME;
  1119. if ((pip->i_d.di_flags & XFS_DIFLAG_NODUMP) &&
  1120. xfs_inherit_nodump)
  1121. di_flags |= XFS_DIFLAG_NODUMP;
  1122. if ((pip->i_d.di_flags & XFS_DIFLAG_SYNC) &&
  1123. xfs_inherit_sync)
  1124. di_flags |= XFS_DIFLAG_SYNC;
  1125. if ((pip->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) &&
  1126. xfs_inherit_nosymlinks)
  1127. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  1128. if (pip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1129. di_flags |= XFS_DIFLAG_PROJINHERIT;
  1130. if ((pip->i_d.di_flags & XFS_DIFLAG_NODEFRAG) &&
  1131. xfs_inherit_nodefrag)
  1132. di_flags |= XFS_DIFLAG_NODEFRAG;
  1133. if (pip->i_d.di_flags & XFS_DIFLAG_FILESTREAM)
  1134. di_flags |= XFS_DIFLAG_FILESTREAM;
  1135. ip->i_d.di_flags |= di_flags;
  1136. }
  1137. /* FALLTHROUGH */
  1138. case S_IFLNK:
  1139. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  1140. ip->i_df.if_flags = XFS_IFEXTENTS;
  1141. ip->i_df.if_bytes = ip->i_df.if_real_bytes = 0;
  1142. ip->i_df.if_u1.if_extents = NULL;
  1143. break;
  1144. default:
  1145. ASSERT(0);
  1146. }
  1147. /*
  1148. * Attribute fork settings for new inode.
  1149. */
  1150. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  1151. ip->i_d.di_anextents = 0;
  1152. /*
  1153. * Log the new values stuffed into the inode.
  1154. */
  1155. xfs_trans_log_inode(tp, ip, flags);
  1156. /* now that we have an i_mode we can setup inode ops and unlock */
  1157. xfs_setup_inode(ip);
  1158. /* now we have set up the vfs inode we can associate the filestream */
  1159. if (filestreams) {
  1160. error = xfs_filestream_associate(pip, ip);
  1161. if (error < 0)
  1162. return -error;
  1163. if (!error)
  1164. xfs_iflags_set(ip, XFS_IFILESTREAM);
  1165. }
  1166. *ipp = ip;
  1167. return 0;
  1168. }
  1169. /*
  1170. * Check to make sure that there are no blocks allocated to the
  1171. * file beyond the size of the file. We don't check this for
  1172. * files with fixed size extents or real time extents, but we
  1173. * at least do it for regular files.
  1174. */
  1175. #ifdef DEBUG
  1176. void
  1177. xfs_isize_check(
  1178. xfs_mount_t *mp,
  1179. xfs_inode_t *ip,
  1180. xfs_fsize_t isize)
  1181. {
  1182. xfs_fileoff_t map_first;
  1183. int nimaps;
  1184. xfs_bmbt_irec_t imaps[2];
  1185. if ((ip->i_d.di_mode & S_IFMT) != S_IFREG)
  1186. return;
  1187. if (XFS_IS_REALTIME_INODE(ip))
  1188. return;
  1189. if (ip->i_d.di_flags & XFS_DIFLAG_EXTSIZE)
  1190. return;
  1191. nimaps = 2;
  1192. map_first = XFS_B_TO_FSB(mp, (xfs_ufsize_t)isize);
  1193. /*
  1194. * The filesystem could be shutting down, so bmapi may return
  1195. * an error.
  1196. */
  1197. if (xfs_bmapi(NULL, ip, map_first,
  1198. (XFS_B_TO_FSB(mp,
  1199. (xfs_ufsize_t)XFS_MAXIOFFSET(mp)) -
  1200. map_first),
  1201. XFS_BMAPI_ENTIRE, NULL, 0, imaps, &nimaps,
  1202. NULL, NULL))
  1203. return;
  1204. ASSERT(nimaps == 1);
  1205. ASSERT(imaps[0].br_startblock == HOLESTARTBLOCK);
  1206. }
  1207. #endif /* DEBUG */
  1208. /*
  1209. * Calculate the last possible buffered byte in a file. This must
  1210. * include data that was buffered beyond the EOF by the write code.
  1211. * This also needs to deal with overflowing the xfs_fsize_t type
  1212. * which can happen for sizes near the limit.
  1213. *
  1214. * We also need to take into account any blocks beyond the EOF. It
  1215. * may be the case that they were buffered by a write which failed.
  1216. * In that case the pages will still be in memory, but the inode size
  1217. * will never have been updated.
  1218. */
  1219. xfs_fsize_t
  1220. xfs_file_last_byte(
  1221. xfs_inode_t *ip)
  1222. {
  1223. xfs_mount_t *mp;
  1224. xfs_fsize_t last_byte;
  1225. xfs_fileoff_t last_block;
  1226. xfs_fileoff_t size_last_block;
  1227. int error;
  1228. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL|XFS_IOLOCK_SHARED));
  1229. mp = ip->i_mount;
  1230. /*
  1231. * Only check for blocks beyond the EOF if the extents have
  1232. * been read in. This eliminates the need for the inode lock,
  1233. * and it also saves us from looking when it really isn't
  1234. * necessary.
  1235. */
  1236. if (ip->i_df.if_flags & XFS_IFEXTENTS) {
  1237. error = xfs_bmap_last_offset(NULL, ip, &last_block,
  1238. XFS_DATA_FORK);
  1239. if (error) {
  1240. last_block = 0;
  1241. }
  1242. } else {
  1243. last_block = 0;
  1244. }
  1245. size_last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)ip->i_size);
  1246. last_block = XFS_FILEOFF_MAX(last_block, size_last_block);
  1247. last_byte = XFS_FSB_TO_B(mp, last_block);
  1248. if (last_byte < 0) {
  1249. return XFS_MAXIOFFSET(mp);
  1250. }
  1251. last_byte += (1 << mp->m_writeio_log);
  1252. if (last_byte < 0) {
  1253. return XFS_MAXIOFFSET(mp);
  1254. }
  1255. return last_byte;
  1256. }
  1257. #if defined(XFS_RW_TRACE)
  1258. STATIC void
  1259. xfs_itrunc_trace(
  1260. int tag,
  1261. xfs_inode_t *ip,
  1262. int flag,
  1263. xfs_fsize_t new_size,
  1264. xfs_off_t toss_start,
  1265. xfs_off_t toss_finish)
  1266. {
  1267. if (ip->i_rwtrace == NULL) {
  1268. return;
  1269. }
  1270. ktrace_enter(ip->i_rwtrace,
  1271. (void*)((long)tag),
  1272. (void*)ip,
  1273. (void*)(unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff),
  1274. (void*)(unsigned long)(ip->i_d.di_size & 0xffffffff),
  1275. (void*)((long)flag),
  1276. (void*)(unsigned long)((new_size >> 32) & 0xffffffff),
  1277. (void*)(unsigned long)(new_size & 0xffffffff),
  1278. (void*)(unsigned long)((toss_start >> 32) & 0xffffffff),
  1279. (void*)(unsigned long)(toss_start & 0xffffffff),
  1280. (void*)(unsigned long)((toss_finish >> 32) & 0xffffffff),
  1281. (void*)(unsigned long)(toss_finish & 0xffffffff),
  1282. (void*)(unsigned long)current_cpu(),
  1283. (void*)(unsigned long)current_pid(),
  1284. (void*)NULL,
  1285. (void*)NULL,
  1286. (void*)NULL);
  1287. }
  1288. #else
  1289. #define xfs_itrunc_trace(tag, ip, flag, new_size, toss_start, toss_finish)
  1290. #endif
  1291. /*
  1292. * Start the truncation of the file to new_size. The new size
  1293. * must be smaller than the current size. This routine will
  1294. * clear the buffer and page caches of file data in the removed
  1295. * range, and xfs_itruncate_finish() will remove the underlying
  1296. * disk blocks.
  1297. *
  1298. * The inode must have its I/O lock locked EXCLUSIVELY, and it
  1299. * must NOT have the inode lock held at all. This is because we're
  1300. * calling into the buffer/page cache code and we can't hold the
  1301. * inode lock when we do so.
  1302. *
  1303. * We need to wait for any direct I/Os in flight to complete before we
  1304. * proceed with the truncate. This is needed to prevent the extents
  1305. * being read or written by the direct I/Os from being removed while the
  1306. * I/O is in flight as there is no other method of synchronising
  1307. * direct I/O with the truncate operation. Also, because we hold
  1308. * the IOLOCK in exclusive mode, we prevent new direct I/Os from being
  1309. * started until the truncate completes and drops the lock. Essentially,
  1310. * the vn_iowait() call forms an I/O barrier that provides strict ordering
  1311. * between direct I/Os and the truncate operation.
  1312. *
  1313. * The flags parameter can have either the value XFS_ITRUNC_DEFINITE
  1314. * or XFS_ITRUNC_MAYBE. The XFS_ITRUNC_MAYBE value should be used
  1315. * in the case that the caller is locking things out of order and
  1316. * may not be able to call xfs_itruncate_finish() with the inode lock
  1317. * held without dropping the I/O lock. If the caller must drop the
  1318. * I/O lock before calling xfs_itruncate_finish(), then xfs_itruncate_start()
  1319. * must be called again with all the same restrictions as the initial
  1320. * call.
  1321. */
  1322. int
  1323. xfs_itruncate_start(
  1324. xfs_inode_t *ip,
  1325. uint flags,
  1326. xfs_fsize_t new_size)
  1327. {
  1328. xfs_fsize_t last_byte;
  1329. xfs_off_t toss_start;
  1330. xfs_mount_t *mp;
  1331. int error = 0;
  1332. ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
  1333. ASSERT((new_size == 0) || (new_size <= ip->i_size));
  1334. ASSERT((flags == XFS_ITRUNC_DEFINITE) ||
  1335. (flags == XFS_ITRUNC_MAYBE));
  1336. mp = ip->i_mount;
  1337. /* wait for the completion of any pending DIOs */
  1338. if (new_size == 0 || new_size < ip->i_size)
  1339. vn_iowait(ip);
  1340. /*
  1341. * Call toss_pages or flushinval_pages to get rid of pages
  1342. * overlapping the region being removed. We have to use
  1343. * the less efficient flushinval_pages in the case that the
  1344. * caller may not be able to finish the truncate without
  1345. * dropping the inode's I/O lock. Make sure
  1346. * to catch any pages brought in by buffers overlapping
  1347. * the EOF by searching out beyond the isize by our
  1348. * block size. We round new_size up to a block boundary
  1349. * so that we don't toss things on the same block as
  1350. * new_size but before it.
  1351. *
  1352. * Before calling toss_page or flushinval_pages, make sure to
  1353. * call remapf() over the same region if the file is mapped.
  1354. * This frees up mapped file references to the pages in the
  1355. * given range and for the flushinval_pages case it ensures
  1356. * that we get the latest mapped changes flushed out.
  1357. */
  1358. toss_start = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1359. toss_start = XFS_FSB_TO_B(mp, toss_start);
  1360. if (toss_start < 0) {
  1361. /*
  1362. * The place to start tossing is beyond our maximum
  1363. * file size, so there is no way that the data extended
  1364. * out there.
  1365. */
  1366. return 0;
  1367. }
  1368. last_byte = xfs_file_last_byte(ip);
  1369. xfs_itrunc_trace(XFS_ITRUNC_START, ip, flags, new_size, toss_start,
  1370. last_byte);
  1371. if (last_byte > toss_start) {
  1372. if (flags & XFS_ITRUNC_DEFINITE) {
  1373. xfs_tosspages(ip, toss_start,
  1374. -1, FI_REMAPF_LOCKED);
  1375. } else {
  1376. error = xfs_flushinval_pages(ip, toss_start,
  1377. -1, FI_REMAPF_LOCKED);
  1378. }
  1379. }
  1380. #ifdef DEBUG
  1381. if (new_size == 0) {
  1382. ASSERT(VN_CACHED(VFS_I(ip)) == 0);
  1383. }
  1384. #endif
  1385. return error;
  1386. }
  1387. /*
  1388. * Shrink the file to the given new_size. The new size must be smaller than
  1389. * the current size. This will free up the underlying blocks in the removed
  1390. * range after a call to xfs_itruncate_start() or xfs_atruncate_start().
  1391. *
  1392. * The transaction passed to this routine must have made a permanent log
  1393. * reservation of at least XFS_ITRUNCATE_LOG_RES. This routine may commit the
  1394. * given transaction and start new ones, so make sure everything involved in
  1395. * the transaction is tidy before calling here. Some transaction will be
  1396. * returned to the caller to be committed. The incoming transaction must
  1397. * already include the inode, and both inode locks must be held exclusively.
  1398. * The inode must also be "held" within the transaction. On return the inode
  1399. * will be "held" within the returned transaction. This routine does NOT
  1400. * require any disk space to be reserved for it within the transaction.
  1401. *
  1402. * The fork parameter must be either xfs_attr_fork or xfs_data_fork, and it
  1403. * indicates the fork which is to be truncated. For the attribute fork we only
  1404. * support truncation to size 0.
  1405. *
  1406. * We use the sync parameter to indicate whether or not the first transaction
  1407. * we perform might have to be synchronous. For the attr fork, it needs to be
  1408. * so if the unlink of the inode is not yet known to be permanent in the log.
  1409. * This keeps us from freeing and reusing the blocks of the attribute fork
  1410. * before the unlink of the inode becomes permanent.
  1411. *
  1412. * For the data fork, we normally have to run synchronously if we're being
  1413. * called out of the inactive path or we're being called out of the create path
  1414. * where we're truncating an existing file. Either way, the truncate needs to
  1415. * be sync so blocks don't reappear in the file with altered data in case of a
  1416. * crash. wsync filesystems can run the first case async because anything that
  1417. * shrinks the inode has to run sync so by the time we're called here from
  1418. * inactive, the inode size is permanently set to 0.
  1419. *
  1420. * Calls from the truncate path always need to be sync unless we're in a wsync
  1421. * filesystem and the file has already been unlinked.
  1422. *
  1423. * The caller is responsible for correctly setting the sync parameter. It gets
  1424. * too hard for us to guess here which path we're being called out of just
  1425. * based on inode state.
  1426. *
  1427. * If we get an error, we must return with the inode locked and linked into the
  1428. * current transaction. This keeps things simple for the higher level code,
  1429. * because it always knows that the inode is locked and held in the transaction
  1430. * that returns to it whether errors occur or not. We don't mark the inode
  1431. * dirty on error so that transactions can be easily aborted if possible.
  1432. */
  1433. int
  1434. xfs_itruncate_finish(
  1435. xfs_trans_t **tp,
  1436. xfs_inode_t *ip,
  1437. xfs_fsize_t new_size,
  1438. int fork,
  1439. int sync)
  1440. {
  1441. xfs_fsblock_t first_block;
  1442. xfs_fileoff_t first_unmap_block;
  1443. xfs_fileoff_t last_block;
  1444. xfs_filblks_t unmap_len=0;
  1445. xfs_mount_t *mp;
  1446. xfs_trans_t *ntp;
  1447. int done;
  1448. int committed;
  1449. xfs_bmap_free_t free_list;
  1450. int error;
  1451. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_IOLOCK_EXCL));
  1452. ASSERT((new_size == 0) || (new_size <= ip->i_size));
  1453. ASSERT(*tp != NULL);
  1454. ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
  1455. ASSERT(ip->i_transp == *tp);
  1456. ASSERT(ip->i_itemp != NULL);
  1457. ASSERT(ip->i_itemp->ili_flags & XFS_ILI_HOLD);
  1458. ntp = *tp;
  1459. mp = (ntp)->t_mountp;
  1460. ASSERT(! XFS_NOT_DQATTACHED(mp, ip));
  1461. /*
  1462. * We only support truncating the entire attribute fork.
  1463. */
  1464. if (fork == XFS_ATTR_FORK) {
  1465. new_size = 0LL;
  1466. }
  1467. first_unmap_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)new_size);
  1468. xfs_itrunc_trace(XFS_ITRUNC_FINISH1, ip, 0, new_size, 0, 0);
  1469. /*
  1470. * The first thing we do is set the size to new_size permanently
  1471. * on disk. This way we don't have to worry about anyone ever
  1472. * being able to look at the data being freed even in the face
  1473. * of a crash. What we're getting around here is the case where
  1474. * we free a block, it is allocated to another file, it is written
  1475. * to, and then we crash. If the new data gets written to the
  1476. * file but the log buffers containing the free and reallocation
  1477. * don't, then we'd end up with garbage in the blocks being freed.
  1478. * As long as we make the new_size permanent before actually
  1479. * freeing any blocks it doesn't matter if they get writtten to.
  1480. *
  1481. * The callers must signal into us whether or not the size
  1482. * setting here must be synchronous. There are a few cases
  1483. * where it doesn't have to be synchronous. Those cases
  1484. * occur if the file is unlinked and we know the unlink is
  1485. * permanent or if the blocks being truncated are guaranteed
  1486. * to be beyond the inode eof (regardless of the link count)
  1487. * and the eof value is permanent. Both of these cases occur
  1488. * only on wsync-mounted filesystems. In those cases, we're
  1489. * guaranteed that no user will ever see the data in the blocks
  1490. * that are being truncated so the truncate can run async.
  1491. * In the free beyond eof case, the file may wind up with
  1492. * more blocks allocated to it than it needs if we crash
  1493. * and that won't get fixed until the next time the file
  1494. * is re-opened and closed but that's ok as that shouldn't
  1495. * be too many blocks.
  1496. *
  1497. * However, we can't just make all wsync xactions run async
  1498. * because there's one call out of the create path that needs
  1499. * to run sync where it's truncating an existing file to size
  1500. * 0 whose size is > 0.
  1501. *
  1502. * It's probably possible to come up with a test in this
  1503. * routine that would correctly distinguish all the above
  1504. * cases from the values of the function parameters and the
  1505. * inode state but for sanity's sake, I've decided to let the
  1506. * layers above just tell us. It's simpler to correctly figure
  1507. * out in the layer above exactly under what conditions we
  1508. * can run async and I think it's easier for others read and
  1509. * follow the logic in case something has to be changed.
  1510. * cscope is your friend -- rcc.
  1511. *
  1512. * The attribute fork is much simpler.
  1513. *
  1514. * For the attribute fork we allow the caller to tell us whether
  1515. * the unlink of the inode that led to this call is yet permanent
  1516. * in the on disk log. If it is not and we will be freeing extents
  1517. * in this inode then we make the first transaction synchronous
  1518. * to make sure that the unlink is permanent by the time we free
  1519. * the blocks.
  1520. */
  1521. if (fork == XFS_DATA_FORK) {
  1522. if (ip->i_d.di_nextents > 0) {
  1523. /*
  1524. * If we are not changing the file size then do
  1525. * not update the on-disk file size - we may be
  1526. * called from xfs_inactive_free_eofblocks(). If we
  1527. * update the on-disk file size and then the system
  1528. * crashes before the contents of the file are
  1529. * flushed to disk then the files may be full of
  1530. * holes (ie NULL files bug).
  1531. */
  1532. if (ip->i_size != new_size) {
  1533. ip->i_d.di_size = new_size;
  1534. ip->i_size = new_size;
  1535. xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
  1536. }
  1537. }
  1538. } else if (sync) {
  1539. ASSERT(!(mp->m_flags & XFS_MOUNT_WSYNC));
  1540. if (ip->i_d.di_anextents > 0)
  1541. xfs_trans_set_sync(ntp);
  1542. }
  1543. ASSERT(fork == XFS_DATA_FORK ||
  1544. (fork == XFS_ATTR_FORK &&
  1545. ((sync && !(mp->m_flags & XFS_MOUNT_WSYNC)) ||
  1546. (sync == 0 && (mp->m_flags & XFS_MOUNT_WSYNC)))));
  1547. /*
  1548. * Since it is possible for space to become allocated beyond
  1549. * the end of the file (in a crash where the space is allocated
  1550. * but the inode size is not yet updated), simply remove any
  1551. * blocks which show up between the new EOF and the maximum
  1552. * possible file size. If the first block to be removed is
  1553. * beyond the maximum file size (ie it is the same as last_block),
  1554. * then there is nothing to do.
  1555. */
  1556. last_block = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  1557. ASSERT(first_unmap_block <= last_block);
  1558. done = 0;
  1559. if (last_block == first_unmap_block) {
  1560. done = 1;
  1561. } else {
  1562. unmap_len = last_block - first_unmap_block + 1;
  1563. }
  1564. while (!done) {
  1565. /*
  1566. * Free up up to XFS_ITRUNC_MAX_EXTENTS. xfs_bunmapi()
  1567. * will tell us whether it freed the entire range or
  1568. * not. If this is a synchronous mount (wsync),
  1569. * then we can tell bunmapi to keep all the
  1570. * transactions asynchronous since the unlink
  1571. * transaction that made this inode inactive has
  1572. * already hit the disk. There's no danger of
  1573. * the freed blocks being reused, there being a
  1574. * crash, and the reused blocks suddenly reappearing
  1575. * in this file with garbage in them once recovery
  1576. * runs.
  1577. */
  1578. XFS_BMAP_INIT(&free_list, &first_block);
  1579. error = xfs_bunmapi(ntp, ip,
  1580. first_unmap_block, unmap_len,
  1581. XFS_BMAPI_AFLAG(fork) |
  1582. (sync ? 0 : XFS_BMAPI_ASYNC),
  1583. XFS_ITRUNC_MAX_EXTENTS,
  1584. &first_block, &free_list,
  1585. NULL, &done);
  1586. if (error) {
  1587. /*
  1588. * If the bunmapi call encounters an error,
  1589. * return to the caller where the transaction
  1590. * can be properly aborted. We just need to
  1591. * make sure we're not holding any resources
  1592. * that we were not when we came in.
  1593. */
  1594. xfs_bmap_cancel(&free_list);
  1595. return error;
  1596. }
  1597. /*
  1598. * Duplicate the transaction that has the permanent
  1599. * reservation and commit the old transaction.
  1600. */
  1601. error = xfs_bmap_finish(tp, &free_list, &committed);
  1602. ntp = *tp;
  1603. if (committed) {
  1604. /* link the inode into the next xact in the chain */
  1605. xfs_trans_ijoin(ntp, ip,
  1606. XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1607. xfs_trans_ihold(ntp, ip);
  1608. }
  1609. if (error) {
  1610. /*
  1611. * If the bmap finish call encounters an error, return
  1612. * to the caller where the transaction can be properly
  1613. * aborted. We just need to make sure we're not
  1614. * holding any resources that we were not when we came
  1615. * in.
  1616. *
  1617. * Aborting from this point might lose some blocks in
  1618. * the file system, but oh well.
  1619. */
  1620. xfs_bmap_cancel(&free_list);
  1621. return error;
  1622. }
  1623. if (committed) {
  1624. /*
  1625. * Mark the inode dirty so it will be logged and
  1626. * moved forward in the log as part of every commit.
  1627. */
  1628. xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
  1629. }
  1630. ntp = xfs_trans_dup(ntp);
  1631. error = xfs_trans_commit(*tp, 0);
  1632. *tp = ntp;
  1633. /* link the inode into the next transaction in the chain */
  1634. xfs_trans_ijoin(ntp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1635. xfs_trans_ihold(ntp, ip);
  1636. if (error)
  1637. return error;
  1638. /*
  1639. * transaction commit worked ok so we can drop the extra ticket
  1640. * reference that we gained in xfs_trans_dup()
  1641. */
  1642. xfs_log_ticket_put(ntp->t_ticket);
  1643. error = xfs_trans_reserve(ntp, 0,
  1644. XFS_ITRUNCATE_LOG_RES(mp), 0,
  1645. XFS_TRANS_PERM_LOG_RES,
  1646. XFS_ITRUNCATE_LOG_COUNT);
  1647. if (error)
  1648. return error;
  1649. }
  1650. /*
  1651. * Only update the size in the case of the data fork, but
  1652. * always re-log the inode so that our permanent transaction
  1653. * can keep on rolling it forward in the log.
  1654. */
  1655. if (fork == XFS_DATA_FORK) {
  1656. xfs_isize_check(mp, ip, new_size);
  1657. /*
  1658. * If we are not changing the file size then do
  1659. * not update the on-disk file size - we may be
  1660. * called from xfs_inactive_free_eofblocks(). If we
  1661. * update the on-disk file size and then the system
  1662. * crashes before the contents of the file are
  1663. * flushed to disk then the files may be full of
  1664. * holes (ie NULL files bug).
  1665. */
  1666. if (ip->i_size != new_size) {
  1667. ip->i_d.di_size = new_size;
  1668. ip->i_size = new_size;
  1669. }
  1670. }
  1671. xfs_trans_log_inode(ntp, ip, XFS_ILOG_CORE);
  1672. ASSERT((new_size != 0) ||
  1673. (fork == XFS_ATTR_FORK) ||
  1674. (ip->i_delayed_blks == 0));
  1675. ASSERT((new_size != 0) ||
  1676. (fork == XFS_ATTR_FORK) ||
  1677. (ip->i_d.di_nextents == 0));
  1678. xfs_itrunc_trace(XFS_ITRUNC_FINISH2, ip, 0, new_size, 0, 0);
  1679. return 0;
  1680. }
  1681. /*
  1682. * This is called when the inode's link count goes to 0.
  1683. * We place the on-disk inode on a list in the AGI. It
  1684. * will be pulled from this list when the inode is freed.
  1685. */
  1686. int
  1687. xfs_iunlink(
  1688. xfs_trans_t *tp,
  1689. xfs_inode_t *ip)
  1690. {
  1691. xfs_mount_t *mp;
  1692. xfs_agi_t *agi;
  1693. xfs_dinode_t *dip;
  1694. xfs_buf_t *agibp;
  1695. xfs_buf_t *ibp;
  1696. xfs_agino_t agino;
  1697. short bucket_index;
  1698. int offset;
  1699. int error;
  1700. ASSERT(ip->i_d.di_nlink == 0);
  1701. ASSERT(ip->i_d.di_mode != 0);
  1702. ASSERT(ip->i_transp == tp);
  1703. mp = tp->t_mountp;
  1704. /*
  1705. * Get the agi buffer first. It ensures lock ordering
  1706. * on the list.
  1707. */
  1708. error = xfs_read_agi(mp, tp, XFS_INO_TO_AGNO(mp, ip->i_ino), &agibp);
  1709. if (error)
  1710. return error;
  1711. agi = XFS_BUF_TO_AGI(agibp);
  1712. /*
  1713. * Get the index into the agi hash table for the
  1714. * list this inode will go on.
  1715. */
  1716. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1717. ASSERT(agino != 0);
  1718. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1719. ASSERT(agi->agi_unlinked[bucket_index]);
  1720. ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != agino);
  1721. if (be32_to_cpu(agi->agi_unlinked[bucket_index]) != NULLAGINO) {
  1722. /*
  1723. * There is already another inode in the bucket we need
  1724. * to add ourselves to. Add us at the front of the list.
  1725. * Here we put the head pointer into our next pointer,
  1726. * and then we fall through to point the head at us.
  1727. */
  1728. error = xfs_itobp(mp, tp, ip, &dip, &ibp, XFS_BUF_LOCK);
  1729. if (error)
  1730. return error;
  1731. ASSERT(be32_to_cpu(dip->di_next_unlinked) == NULLAGINO);
  1732. /* both on-disk, don't endian flip twice */
  1733. dip->di_next_unlinked = agi->agi_unlinked[bucket_index];
  1734. offset = ip->i_imap.im_boffset +
  1735. offsetof(xfs_dinode_t, di_next_unlinked);
  1736. xfs_trans_inode_buf(tp, ibp);
  1737. xfs_trans_log_buf(tp, ibp, offset,
  1738. (offset + sizeof(xfs_agino_t) - 1));
  1739. xfs_inobp_check(mp, ibp);
  1740. }
  1741. /*
  1742. * Point the bucket head pointer at the inode being inserted.
  1743. */
  1744. ASSERT(agino != 0);
  1745. agi->agi_unlinked[bucket_index] = cpu_to_be32(agino);
  1746. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1747. (sizeof(xfs_agino_t) * bucket_index);
  1748. xfs_trans_log_buf(tp, agibp, offset,
  1749. (offset + sizeof(xfs_agino_t) - 1));
  1750. return 0;
  1751. }
  1752. /*
  1753. * Pull the on-disk inode from the AGI unlinked list.
  1754. */
  1755. STATIC int
  1756. xfs_iunlink_remove(
  1757. xfs_trans_t *tp,
  1758. xfs_inode_t *ip)
  1759. {
  1760. xfs_ino_t next_ino;
  1761. xfs_mount_t *mp;
  1762. xfs_agi_t *agi;
  1763. xfs_dinode_t *dip;
  1764. xfs_buf_t *agibp;
  1765. xfs_buf_t *ibp;
  1766. xfs_agnumber_t agno;
  1767. xfs_agino_t agino;
  1768. xfs_agino_t next_agino;
  1769. xfs_buf_t *last_ibp;
  1770. xfs_dinode_t *last_dip = NULL;
  1771. short bucket_index;
  1772. int offset, last_offset = 0;
  1773. int error;
  1774. mp = tp->t_mountp;
  1775. agno = XFS_INO_TO_AGNO(mp, ip->i_ino);
  1776. /*
  1777. * Get the agi buffer first. It ensures lock ordering
  1778. * on the list.
  1779. */
  1780. error = xfs_read_agi(mp, tp, agno, &agibp);
  1781. if (error)
  1782. return error;
  1783. agi = XFS_BUF_TO_AGI(agibp);
  1784. /*
  1785. * Get the index into the agi hash table for the
  1786. * list this inode will go on.
  1787. */
  1788. agino = XFS_INO_TO_AGINO(mp, ip->i_ino);
  1789. ASSERT(agino != 0);
  1790. bucket_index = agino % XFS_AGI_UNLINKED_BUCKETS;
  1791. ASSERT(be32_to_cpu(agi->agi_unlinked[bucket_index]) != NULLAGINO);
  1792. ASSERT(agi->agi_unlinked[bucket_index]);
  1793. if (be32_to_cpu(agi->agi_unlinked[bucket_index]) == agino) {
  1794. /*
  1795. * We're at the head of the list. Get the inode's
  1796. * on-disk buffer to see if there is anyone after us
  1797. * on the list. Only modify our next pointer if it
  1798. * is not already NULLAGINO. This saves us the overhead
  1799. * of dealing with the buffer when there is no need to
  1800. * change it.
  1801. */
  1802. error = xfs_itobp(mp, tp, ip, &dip, &ibp, XFS_BUF_LOCK);
  1803. if (error) {
  1804. cmn_err(CE_WARN,
  1805. "xfs_iunlink_remove: xfs_itobp() returned an error %d on %s. Returning error.",
  1806. error, mp->m_fsname);
  1807. return error;
  1808. }
  1809. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1810. ASSERT(next_agino != 0);
  1811. if (next_agino != NULLAGINO) {
  1812. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1813. offset = ip->i_imap.im_boffset +
  1814. offsetof(xfs_dinode_t, di_next_unlinked);
  1815. xfs_trans_inode_buf(tp, ibp);
  1816. xfs_trans_log_buf(tp, ibp, offset,
  1817. (offset + sizeof(xfs_agino_t) - 1));
  1818. xfs_inobp_check(mp, ibp);
  1819. } else {
  1820. xfs_trans_brelse(tp, ibp);
  1821. }
  1822. /*
  1823. * Point the bucket head pointer at the next inode.
  1824. */
  1825. ASSERT(next_agino != 0);
  1826. ASSERT(next_agino != agino);
  1827. agi->agi_unlinked[bucket_index] = cpu_to_be32(next_agino);
  1828. offset = offsetof(xfs_agi_t, agi_unlinked) +
  1829. (sizeof(xfs_agino_t) * bucket_index);
  1830. xfs_trans_log_buf(tp, agibp, offset,
  1831. (offset + sizeof(xfs_agino_t) - 1));
  1832. } else {
  1833. /*
  1834. * We need to search the list for the inode being freed.
  1835. */
  1836. next_agino = be32_to_cpu(agi->agi_unlinked[bucket_index]);
  1837. last_ibp = NULL;
  1838. while (next_agino != agino) {
  1839. /*
  1840. * If the last inode wasn't the one pointing to
  1841. * us, then release its buffer since we're not
  1842. * going to do anything with it.
  1843. */
  1844. if (last_ibp != NULL) {
  1845. xfs_trans_brelse(tp, last_ibp);
  1846. }
  1847. next_ino = XFS_AGINO_TO_INO(mp, agno, next_agino);
  1848. error = xfs_inotobp(mp, tp, next_ino, &last_dip,
  1849. &last_ibp, &last_offset, 0);
  1850. if (error) {
  1851. cmn_err(CE_WARN,
  1852. "xfs_iunlink_remove: xfs_inotobp() returned an error %d on %s. Returning error.",
  1853. error, mp->m_fsname);
  1854. return error;
  1855. }
  1856. next_agino = be32_to_cpu(last_dip->di_next_unlinked);
  1857. ASSERT(next_agino != NULLAGINO);
  1858. ASSERT(next_agino != 0);
  1859. }
  1860. /*
  1861. * Now last_ibp points to the buffer previous to us on
  1862. * the unlinked list. Pull us from the list.
  1863. */
  1864. error = xfs_itobp(mp, tp, ip, &dip, &ibp, XFS_BUF_LOCK);
  1865. if (error) {
  1866. cmn_err(CE_WARN,
  1867. "xfs_iunlink_remove: xfs_itobp() returned an error %d on %s. Returning error.",
  1868. error, mp->m_fsname);
  1869. return error;
  1870. }
  1871. next_agino = be32_to_cpu(dip->di_next_unlinked);
  1872. ASSERT(next_agino != 0);
  1873. ASSERT(next_agino != agino);
  1874. if (next_agino != NULLAGINO) {
  1875. dip->di_next_unlinked = cpu_to_be32(NULLAGINO);
  1876. offset = ip->i_imap.im_boffset +
  1877. offsetof(xfs_dinode_t, di_next_unlinked);
  1878. xfs_trans_inode_buf(tp, ibp);
  1879. xfs_trans_log_buf(tp, ibp, offset,
  1880. (offset + sizeof(xfs_agino_t) - 1));
  1881. xfs_inobp_check(mp, ibp);
  1882. } else {
  1883. xfs_trans_brelse(tp, ibp);
  1884. }
  1885. /*
  1886. * Point the previous inode on the list to the next inode.
  1887. */
  1888. last_dip->di_next_unlinked = cpu_to_be32(next_agino);
  1889. ASSERT(next_agino != 0);
  1890. offset = last_offset + offsetof(xfs_dinode_t, di_next_unlinked);
  1891. xfs_trans_inode_buf(tp, last_ibp);
  1892. xfs_trans_log_buf(tp, last_ibp, offset,
  1893. (offset + sizeof(xfs_agino_t) - 1));
  1894. xfs_inobp_check(mp, last_ibp);
  1895. }
  1896. return 0;
  1897. }
  1898. STATIC void
  1899. xfs_ifree_cluster(
  1900. xfs_inode_t *free_ip,
  1901. xfs_trans_t *tp,
  1902. xfs_ino_t inum)
  1903. {
  1904. xfs_mount_t *mp = free_ip->i_mount;
  1905. int blks_per_cluster;
  1906. int nbufs;
  1907. int ninodes;
  1908. int i, j, found, pre_flushed;
  1909. xfs_daddr_t blkno;
  1910. xfs_buf_t *bp;
  1911. xfs_inode_t *ip, **ip_found;
  1912. xfs_inode_log_item_t *iip;
  1913. xfs_log_item_t *lip;
  1914. xfs_perag_t *pag = xfs_get_perag(mp, inum);
  1915. if (mp->m_sb.sb_blocksize >= XFS_INODE_CLUSTER_SIZE(mp)) {
  1916. blks_per_cluster = 1;
  1917. ninodes = mp->m_sb.sb_inopblock;
  1918. nbufs = XFS_IALLOC_BLOCKS(mp);
  1919. } else {
  1920. blks_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) /
  1921. mp->m_sb.sb_blocksize;
  1922. ninodes = blks_per_cluster * mp->m_sb.sb_inopblock;
  1923. nbufs = XFS_IALLOC_BLOCKS(mp) / blks_per_cluster;
  1924. }
  1925. ip_found = kmem_alloc(ninodes * sizeof(xfs_inode_t *), KM_NOFS);
  1926. for (j = 0; j < nbufs; j++, inum += ninodes) {
  1927. blkno = XFS_AGB_TO_DADDR(mp, XFS_INO_TO_AGNO(mp, inum),
  1928. XFS_INO_TO_AGBNO(mp, inum));
  1929. /*
  1930. * Look for each inode in memory and attempt to lock it,
  1931. * we can be racing with flush and tail pushing here.
  1932. * any inode we get the locks on, add to an array of
  1933. * inode items to process later.
  1934. *
  1935. * The get the buffer lock, we could beat a flush
  1936. * or tail pushing thread to the lock here, in which
  1937. * case they will go looking for the inode buffer
  1938. * and fail, we need some other form of interlock
  1939. * here.
  1940. */
  1941. found = 0;
  1942. for (i = 0; i < ninodes; i++) {
  1943. read_lock(&pag->pag_ici_lock);
  1944. ip = radix_tree_lookup(&pag->pag_ici_root,
  1945. XFS_INO_TO_AGINO(mp, (inum + i)));
  1946. /* Inode not in memory or we found it already,
  1947. * nothing to do
  1948. */
  1949. if (!ip || xfs_iflags_test(ip, XFS_ISTALE)) {
  1950. read_unlock(&pag->pag_ici_lock);
  1951. continue;
  1952. }
  1953. if (xfs_inode_clean(ip)) {
  1954. read_unlock(&pag->pag_ici_lock);
  1955. continue;
  1956. }
  1957. /* If we can get the locks then add it to the
  1958. * list, otherwise by the time we get the bp lock
  1959. * below it will already be attached to the
  1960. * inode buffer.
  1961. */
  1962. /* This inode will already be locked - by us, lets
  1963. * keep it that way.
  1964. */
  1965. if (ip == free_ip) {
  1966. if (xfs_iflock_nowait(ip)) {
  1967. xfs_iflags_set(ip, XFS_ISTALE);
  1968. if (xfs_inode_clean(ip)) {
  1969. xfs_ifunlock(ip);
  1970. } else {
  1971. ip_found[found++] = ip;
  1972. }
  1973. }
  1974. read_unlock(&pag->pag_ici_lock);
  1975. continue;
  1976. }
  1977. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  1978. if (xfs_iflock_nowait(ip)) {
  1979. xfs_iflags_set(ip, XFS_ISTALE);
  1980. if (xfs_inode_clean(ip)) {
  1981. xfs_ifunlock(ip);
  1982. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1983. } else {
  1984. ip_found[found++] = ip;
  1985. }
  1986. } else {
  1987. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1988. }
  1989. }
  1990. read_unlock(&pag->pag_ici_lock);
  1991. }
  1992. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, blkno,
  1993. mp->m_bsize * blks_per_cluster,
  1994. XFS_BUF_LOCK);
  1995. pre_flushed = 0;
  1996. lip = XFS_BUF_FSPRIVATE(bp, xfs_log_item_t *);
  1997. while (lip) {
  1998. if (lip->li_type == XFS_LI_INODE) {
  1999. iip = (xfs_inode_log_item_t *)lip;
  2000. ASSERT(iip->ili_logged == 1);
  2001. lip->li_cb = (void(*)(xfs_buf_t*,xfs_log_item_t*)) xfs_istale_done;
  2002. xfs_trans_ail_copy_lsn(mp->m_ail,
  2003. &iip->ili_flush_lsn,
  2004. &iip->ili_item.li_lsn);
  2005. xfs_iflags_set(iip->ili_inode, XFS_ISTALE);
  2006. pre_flushed++;
  2007. }
  2008. lip = lip->li_bio_list;
  2009. }
  2010. for (i = 0; i < found; i++) {
  2011. ip = ip_found[i];
  2012. iip = ip->i_itemp;
  2013. if (!iip) {
  2014. ip->i_update_core = 0;
  2015. xfs_ifunlock(ip);
  2016. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2017. continue;
  2018. }
  2019. iip->ili_last_fields = iip->ili_format.ilf_fields;
  2020. iip->ili_format.ilf_fields = 0;
  2021. iip->ili_logged = 1;
  2022. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  2023. &iip->ili_item.li_lsn);
  2024. xfs_buf_attach_iodone(bp,
  2025. (void(*)(xfs_buf_t*,xfs_log_item_t*))
  2026. xfs_istale_done, (xfs_log_item_t *)iip);
  2027. if (ip != free_ip) {
  2028. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  2029. }
  2030. }
  2031. if (found || pre_flushed)
  2032. xfs_trans_stale_inode_buf(tp, bp);
  2033. xfs_trans_binval(tp, bp);
  2034. }
  2035. kmem_free(ip_found);
  2036. xfs_put_perag(mp, pag);
  2037. }
  2038. /*
  2039. * This is called to return an inode to the inode free list.
  2040. * The inode should already be truncated to 0 length and have
  2041. * no pages associated with it. This routine also assumes that
  2042. * the inode is already a part of the transaction.
  2043. *
  2044. * The on-disk copy of the inode will have been added to the list
  2045. * of unlinked inodes in the AGI. We need to remove the inode from
  2046. * that list atomically with respect to freeing it here.
  2047. */
  2048. int
  2049. xfs_ifree(
  2050. xfs_trans_t *tp,
  2051. xfs_inode_t *ip,
  2052. xfs_bmap_free_t *flist)
  2053. {
  2054. int error;
  2055. int delete;
  2056. xfs_ino_t first_ino;
  2057. xfs_dinode_t *dip;
  2058. xfs_buf_t *ibp;
  2059. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  2060. ASSERT(ip->i_transp == tp);
  2061. ASSERT(ip->i_d.di_nlink == 0);
  2062. ASSERT(ip->i_d.di_nextents == 0);
  2063. ASSERT(ip->i_d.di_anextents == 0);
  2064. ASSERT((ip->i_d.di_size == 0 && ip->i_size == 0) ||
  2065. ((ip->i_d.di_mode & S_IFMT) != S_IFREG));
  2066. ASSERT(ip->i_d.di_nblocks == 0);
  2067. /*
  2068. * Pull the on-disk inode from the AGI unlinked list.
  2069. */
  2070. error = xfs_iunlink_remove(tp, ip);
  2071. if (error != 0) {
  2072. return error;
  2073. }
  2074. error = xfs_difree(tp, ip->i_ino, flist, &delete, &first_ino);
  2075. if (error != 0) {
  2076. return error;
  2077. }
  2078. ip->i_d.di_mode = 0; /* mark incore inode as free */
  2079. ip->i_d.di_flags = 0;
  2080. ip->i_d.di_dmevmask = 0;
  2081. ip->i_d.di_forkoff = 0; /* mark the attr fork not in use */
  2082. ip->i_df.if_ext_max =
  2083. XFS_IFORK_DSIZE(ip) / (uint)sizeof(xfs_bmbt_rec_t);
  2084. ip->i_d.di_format = XFS_DINODE_FMT_EXTENTS;
  2085. ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
  2086. /*
  2087. * Bump the generation count so no one will be confused
  2088. * by reincarnations of this inode.
  2089. */
  2090. ip->i_d.di_gen++;
  2091. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  2092. error = xfs_itobp(ip->i_mount, tp, ip, &dip, &ibp, XFS_BUF_LOCK);
  2093. if (error)
  2094. return error;
  2095. /*
  2096. * Clear the on-disk di_mode. This is to prevent xfs_bulkstat
  2097. * from picking up this inode when it is reclaimed (its incore state
  2098. * initialzed but not flushed to disk yet). The in-core di_mode is
  2099. * already cleared and a corresponding transaction logged.
  2100. * The hack here just synchronizes the in-core to on-disk
  2101. * di_mode value in advance before the actual inode sync to disk.
  2102. * This is OK because the inode is already unlinked and would never
  2103. * change its di_mode again for this inode generation.
  2104. * This is a temporary hack that would require a proper fix
  2105. * in the future.
  2106. */
  2107. dip->di_mode = 0;
  2108. if (delete) {
  2109. xfs_ifree_cluster(ip, tp, first_ino);
  2110. }
  2111. return 0;
  2112. }
  2113. /*
  2114. * Reallocate the space for if_broot based on the number of records
  2115. * being added or deleted as indicated in rec_diff. Move the records
  2116. * and pointers in if_broot to fit the new size. When shrinking this
  2117. * will eliminate holes between the records and pointers created by
  2118. * the caller. When growing this will create holes to be filled in
  2119. * by the caller.
  2120. *
  2121. * The caller must not request to add more records than would fit in
  2122. * the on-disk inode root. If the if_broot is currently NULL, then
  2123. * if we adding records one will be allocated. The caller must also
  2124. * not request that the number of records go below zero, although
  2125. * it can go to zero.
  2126. *
  2127. * ip -- the inode whose if_broot area is changing
  2128. * ext_diff -- the change in the number of records, positive or negative,
  2129. * requested for the if_broot array.
  2130. */
  2131. void
  2132. xfs_iroot_realloc(
  2133. xfs_inode_t *ip,
  2134. int rec_diff,
  2135. int whichfork)
  2136. {
  2137. struct xfs_mount *mp = ip->i_mount;
  2138. int cur_max;
  2139. xfs_ifork_t *ifp;
  2140. struct xfs_btree_block *new_broot;
  2141. int new_max;
  2142. size_t new_size;
  2143. char *np;
  2144. char *op;
  2145. /*
  2146. * Handle the degenerate case quietly.
  2147. */
  2148. if (rec_diff == 0) {
  2149. return;
  2150. }
  2151. ifp = XFS_IFORK_PTR(ip, whichfork);
  2152. if (rec_diff > 0) {
  2153. /*
  2154. * If there wasn't any memory allocated before, just
  2155. * allocate it now and get out.
  2156. */
  2157. if (ifp->if_broot_bytes == 0) {
  2158. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(rec_diff);
  2159. ifp->if_broot = kmem_alloc(new_size, KM_SLEEP);
  2160. ifp->if_broot_bytes = (int)new_size;
  2161. return;
  2162. }
  2163. /*
  2164. * If there is already an existing if_broot, then we need
  2165. * to realloc() it and shift the pointers to their new
  2166. * location. The records don't change location because
  2167. * they are kept butted up against the btree block header.
  2168. */
  2169. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  2170. new_max = cur_max + rec_diff;
  2171. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
  2172. ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
  2173. (size_t)XFS_BMAP_BROOT_SPACE_CALC(cur_max), /* old size */
  2174. KM_SLEEP);
  2175. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  2176. ifp->if_broot_bytes);
  2177. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  2178. (int)new_size);
  2179. ifp->if_broot_bytes = (int)new_size;
  2180. ASSERT(ifp->if_broot_bytes <=
  2181. XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
  2182. memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
  2183. return;
  2184. }
  2185. /*
  2186. * rec_diff is less than 0. In this case, we are shrinking the
  2187. * if_broot buffer. It must already exist. If we go to zero
  2188. * records, just get rid of the root and clear the status bit.
  2189. */
  2190. ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
  2191. cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
  2192. new_max = cur_max + rec_diff;
  2193. ASSERT(new_max >= 0);
  2194. if (new_max > 0)
  2195. new_size = (size_t)XFS_BMAP_BROOT_SPACE_CALC(new_max);
  2196. else
  2197. new_size = 0;
  2198. if (new_size > 0) {
  2199. new_broot = kmem_alloc(new_size, KM_SLEEP);
  2200. /*
  2201. * First copy over the btree block header.
  2202. */
  2203. memcpy(new_broot, ifp->if_broot, XFS_BTREE_LBLOCK_LEN);
  2204. } else {
  2205. new_broot = NULL;
  2206. ifp->if_flags &= ~XFS_IFBROOT;
  2207. }
  2208. /*
  2209. * Only copy the records and pointers if there are any.
  2210. */
  2211. if (new_max > 0) {
  2212. /*
  2213. * First copy the records.
  2214. */
  2215. op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
  2216. np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
  2217. memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
  2218. /*
  2219. * Then copy the pointers.
  2220. */
  2221. op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
  2222. ifp->if_broot_bytes);
  2223. np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
  2224. (int)new_size);
  2225. memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
  2226. }
  2227. kmem_free(ifp->if_broot);
  2228. ifp->if_broot = new_broot;
  2229. ifp->if_broot_bytes = (int)new_size;
  2230. ASSERT(ifp->if_broot_bytes <=
  2231. XFS_IFORK_SIZE(ip, whichfork) + XFS_BROOT_SIZE_ADJ);
  2232. return;
  2233. }
  2234. /*
  2235. * This is called when the amount of space needed for if_data
  2236. * is increased or decreased. The change in size is indicated by
  2237. * the number of bytes that need to be added or deleted in the
  2238. * byte_diff parameter.
  2239. *
  2240. * If the amount of space needed has decreased below the size of the
  2241. * inline buffer, then switch to using the inline buffer. Otherwise,
  2242. * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
  2243. * to what is needed.
  2244. *
  2245. * ip -- the inode whose if_data area is changing
  2246. * byte_diff -- the change in the number of bytes, positive or negative,
  2247. * requested for the if_data array.
  2248. */
  2249. void
  2250. xfs_idata_realloc(
  2251. xfs_inode_t *ip,
  2252. int byte_diff,
  2253. int whichfork)
  2254. {
  2255. xfs_ifork_t *ifp;
  2256. int new_size;
  2257. int real_size;
  2258. if (byte_diff == 0) {
  2259. return;
  2260. }
  2261. ifp = XFS_IFORK_PTR(ip, whichfork);
  2262. new_size = (int)ifp->if_bytes + byte_diff;
  2263. ASSERT(new_size >= 0);
  2264. if (new_size == 0) {
  2265. if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  2266. kmem_free(ifp->if_u1.if_data);
  2267. }
  2268. ifp->if_u1.if_data = NULL;
  2269. real_size = 0;
  2270. } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
  2271. /*
  2272. * If the valid extents/data can fit in if_inline_ext/data,
  2273. * copy them from the malloc'd vector and free it.
  2274. */
  2275. if (ifp->if_u1.if_data == NULL) {
  2276. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  2277. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  2278. ASSERT(ifp->if_real_bytes != 0);
  2279. memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
  2280. new_size);
  2281. kmem_free(ifp->if_u1.if_data);
  2282. ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
  2283. }
  2284. real_size = 0;
  2285. } else {
  2286. /*
  2287. * Stuck with malloc/realloc.
  2288. * For inline data, the underlying buffer must be
  2289. * a multiple of 4 bytes in size so that it can be
  2290. * logged and stay on word boundaries. We enforce
  2291. * that here.
  2292. */
  2293. real_size = roundup(new_size, 4);
  2294. if (ifp->if_u1.if_data == NULL) {
  2295. ASSERT(ifp->if_real_bytes == 0);
  2296. ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
  2297. } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
  2298. /*
  2299. * Only do the realloc if the underlying size
  2300. * is really changing.
  2301. */
  2302. if (ifp->if_real_bytes != real_size) {
  2303. ifp->if_u1.if_data =
  2304. kmem_realloc(ifp->if_u1.if_data,
  2305. real_size,
  2306. ifp->if_real_bytes,
  2307. KM_SLEEP);
  2308. }
  2309. } else {
  2310. ASSERT(ifp->if_real_bytes == 0);
  2311. ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP);
  2312. memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
  2313. ifp->if_bytes);
  2314. }
  2315. }
  2316. ifp->if_real_bytes = real_size;
  2317. ifp->if_bytes = new_size;
  2318. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  2319. }
  2320. void
  2321. xfs_idestroy_fork(
  2322. xfs_inode_t *ip,
  2323. int whichfork)
  2324. {
  2325. xfs_ifork_t *ifp;
  2326. ifp = XFS_IFORK_PTR(ip, whichfork);
  2327. if (ifp->if_broot != NULL) {
  2328. kmem_free(ifp->if_broot);
  2329. ifp->if_broot = NULL;
  2330. }
  2331. /*
  2332. * If the format is local, then we can't have an extents
  2333. * array so just look for an inline data array. If we're
  2334. * not local then we may or may not have an extents list,
  2335. * so check and free it up if we do.
  2336. */
  2337. if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
  2338. if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
  2339. (ifp->if_u1.if_data != NULL)) {
  2340. ASSERT(ifp->if_real_bytes != 0);
  2341. kmem_free(ifp->if_u1.if_data);
  2342. ifp->if_u1.if_data = NULL;
  2343. ifp->if_real_bytes = 0;
  2344. }
  2345. } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
  2346. ((ifp->if_flags & XFS_IFEXTIREC) ||
  2347. ((ifp->if_u1.if_extents != NULL) &&
  2348. (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
  2349. ASSERT(ifp->if_real_bytes != 0);
  2350. xfs_iext_destroy(ifp);
  2351. }
  2352. ASSERT(ifp->if_u1.if_extents == NULL ||
  2353. ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
  2354. ASSERT(ifp->if_real_bytes == 0);
  2355. if (whichfork == XFS_ATTR_FORK) {
  2356. kmem_zone_free(xfs_ifork_zone, ip->i_afp);
  2357. ip->i_afp = NULL;
  2358. }
  2359. }
  2360. /*
  2361. * This is called free all the memory associated with an inode.
  2362. * It must free the inode itself and any buffers allocated for
  2363. * if_extents/if_data and if_broot. It must also free the lock
  2364. * associated with the inode.
  2365. *
  2366. * Note: because we don't initialise everything on reallocation out
  2367. * of the zone, we must ensure we nullify everything correctly before
  2368. * freeing the structure.
  2369. */
  2370. void
  2371. xfs_idestroy(
  2372. xfs_inode_t *ip)
  2373. {
  2374. switch (ip->i_d.di_mode & S_IFMT) {
  2375. case S_IFREG:
  2376. case S_IFDIR:
  2377. case S_IFLNK:
  2378. xfs_idestroy_fork(ip, XFS_DATA_FORK);
  2379. break;
  2380. }
  2381. if (ip->i_afp)
  2382. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  2383. #ifdef XFS_INODE_TRACE
  2384. ktrace_free(ip->i_trace);
  2385. #endif
  2386. #ifdef XFS_BMAP_TRACE
  2387. ktrace_free(ip->i_xtrace);
  2388. #endif
  2389. #ifdef XFS_BTREE_TRACE
  2390. ktrace_free(ip->i_btrace);
  2391. #endif
  2392. #ifdef XFS_RW_TRACE
  2393. ktrace_free(ip->i_rwtrace);
  2394. #endif
  2395. #ifdef XFS_ILOCK_TRACE
  2396. ktrace_free(ip->i_lock_trace);
  2397. #endif
  2398. #ifdef XFS_DIR2_TRACE
  2399. ktrace_free(ip->i_dir_trace);
  2400. #endif
  2401. if (ip->i_itemp) {
  2402. /*
  2403. * Only if we are shutting down the fs will we see an
  2404. * inode still in the AIL. If it is there, we should remove
  2405. * it to prevent a use-after-free from occurring.
  2406. */
  2407. xfs_log_item_t *lip = &ip->i_itemp->ili_item;
  2408. struct xfs_ail *ailp = lip->li_ailp;
  2409. ASSERT(((lip->li_flags & XFS_LI_IN_AIL) == 0) ||
  2410. XFS_FORCED_SHUTDOWN(ip->i_mount));
  2411. if (lip->li_flags & XFS_LI_IN_AIL) {
  2412. spin_lock(&ailp->xa_lock);
  2413. if (lip->li_flags & XFS_LI_IN_AIL)
  2414. xfs_trans_ail_delete(ailp, lip);
  2415. else
  2416. spin_unlock(&ailp->xa_lock);
  2417. }
  2418. xfs_inode_item_destroy(ip);
  2419. ip->i_itemp = NULL;
  2420. }
  2421. /* asserts to verify all state is correct here */
  2422. ASSERT(atomic_read(&ip->i_iocount) == 0);
  2423. ASSERT(atomic_read(&ip->i_pincount) == 0);
  2424. ASSERT(!spin_is_locked(&ip->i_flags_lock));
  2425. ASSERT(completion_done(&ip->i_flush));
  2426. kmem_zone_free(xfs_inode_zone, ip);
  2427. }
  2428. /*
  2429. * Increment the pin count of the given buffer.
  2430. * This value is protected by ipinlock spinlock in the mount structure.
  2431. */
  2432. void
  2433. xfs_ipin(
  2434. xfs_inode_t *ip)
  2435. {
  2436. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  2437. atomic_inc(&ip->i_pincount);
  2438. }
  2439. /*
  2440. * Decrement the pin count of the given inode, and wake up
  2441. * anyone in xfs_iwait_unpin() if the count goes to 0. The
  2442. * inode must have been previously pinned with a call to xfs_ipin().
  2443. */
  2444. void
  2445. xfs_iunpin(
  2446. xfs_inode_t *ip)
  2447. {
  2448. ASSERT(atomic_read(&ip->i_pincount) > 0);
  2449. if (atomic_dec_and_test(&ip->i_pincount))
  2450. wake_up(&ip->i_ipin_wait);
  2451. }
  2452. /*
  2453. * This is called to unpin an inode. It can be directed to wait or to return
  2454. * immediately without waiting for the inode to be unpinned. The caller must
  2455. * have the inode locked in at least shared mode so that the buffer cannot be
  2456. * subsequently pinned once someone is waiting for it to be unpinned.
  2457. */
  2458. STATIC void
  2459. __xfs_iunpin_wait(
  2460. xfs_inode_t *ip,
  2461. int wait)
  2462. {
  2463. xfs_inode_log_item_t *iip = ip->i_itemp;
  2464. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2465. if (atomic_read(&ip->i_pincount) == 0)
  2466. return;
  2467. /* Give the log a push to start the unpinning I/O */
  2468. xfs_log_force(ip->i_mount, (iip && iip->ili_last_lsn) ?
  2469. iip->ili_last_lsn : 0, XFS_LOG_FORCE);
  2470. if (wait)
  2471. wait_event(ip->i_ipin_wait, (atomic_read(&ip->i_pincount) == 0));
  2472. }
  2473. static inline void
  2474. xfs_iunpin_wait(
  2475. xfs_inode_t *ip)
  2476. {
  2477. __xfs_iunpin_wait(ip, 1);
  2478. }
  2479. static inline void
  2480. xfs_iunpin_nowait(
  2481. xfs_inode_t *ip)
  2482. {
  2483. __xfs_iunpin_wait(ip, 0);
  2484. }
  2485. /*
  2486. * xfs_iextents_copy()
  2487. *
  2488. * This is called to copy the REAL extents (as opposed to the delayed
  2489. * allocation extents) from the inode into the given buffer. It
  2490. * returns the number of bytes copied into the buffer.
  2491. *
  2492. * If there are no delayed allocation extents, then we can just
  2493. * memcpy() the extents into the buffer. Otherwise, we need to
  2494. * examine each extent in turn and skip those which are delayed.
  2495. */
  2496. int
  2497. xfs_iextents_copy(
  2498. xfs_inode_t *ip,
  2499. xfs_bmbt_rec_t *dp,
  2500. int whichfork)
  2501. {
  2502. int copied;
  2503. int i;
  2504. xfs_ifork_t *ifp;
  2505. int nrecs;
  2506. xfs_fsblock_t start_block;
  2507. ifp = XFS_IFORK_PTR(ip, whichfork);
  2508. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2509. ASSERT(ifp->if_bytes > 0);
  2510. nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  2511. XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
  2512. ASSERT(nrecs > 0);
  2513. /*
  2514. * There are some delayed allocation extents in the
  2515. * inode, so copy the extents one at a time and skip
  2516. * the delayed ones. There must be at least one
  2517. * non-delayed extent.
  2518. */
  2519. copied = 0;
  2520. for (i = 0; i < nrecs; i++) {
  2521. xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
  2522. start_block = xfs_bmbt_get_startblock(ep);
  2523. if (ISNULLSTARTBLOCK(start_block)) {
  2524. /*
  2525. * It's a delayed allocation extent, so skip it.
  2526. */
  2527. continue;
  2528. }
  2529. /* Translate to on disk format */
  2530. put_unaligned(cpu_to_be64(ep->l0), &dp->l0);
  2531. put_unaligned(cpu_to_be64(ep->l1), &dp->l1);
  2532. dp++;
  2533. copied++;
  2534. }
  2535. ASSERT(copied != 0);
  2536. xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
  2537. return (copied * (uint)sizeof(xfs_bmbt_rec_t));
  2538. }
  2539. /*
  2540. * Each of the following cases stores data into the same region
  2541. * of the on-disk inode, so only one of them can be valid at
  2542. * any given time. While it is possible to have conflicting formats
  2543. * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
  2544. * in EXTENTS format, this can only happen when the fork has
  2545. * changed formats after being modified but before being flushed.
  2546. * In these cases, the format always takes precedence, because the
  2547. * format indicates the current state of the fork.
  2548. */
  2549. /*ARGSUSED*/
  2550. STATIC void
  2551. xfs_iflush_fork(
  2552. xfs_inode_t *ip,
  2553. xfs_dinode_t *dip,
  2554. xfs_inode_log_item_t *iip,
  2555. int whichfork,
  2556. xfs_buf_t *bp)
  2557. {
  2558. char *cp;
  2559. xfs_ifork_t *ifp;
  2560. xfs_mount_t *mp;
  2561. #ifdef XFS_TRANS_DEBUG
  2562. int first;
  2563. #endif
  2564. static const short brootflag[2] =
  2565. { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
  2566. static const short dataflag[2] =
  2567. { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
  2568. static const short extflag[2] =
  2569. { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
  2570. if (!iip)
  2571. return;
  2572. ifp = XFS_IFORK_PTR(ip, whichfork);
  2573. /*
  2574. * This can happen if we gave up in iformat in an error path,
  2575. * for the attribute fork.
  2576. */
  2577. if (!ifp) {
  2578. ASSERT(whichfork == XFS_ATTR_FORK);
  2579. return;
  2580. }
  2581. cp = XFS_DFORK_PTR(dip, whichfork);
  2582. mp = ip->i_mount;
  2583. switch (XFS_IFORK_FORMAT(ip, whichfork)) {
  2584. case XFS_DINODE_FMT_LOCAL:
  2585. if ((iip->ili_format.ilf_fields & dataflag[whichfork]) &&
  2586. (ifp->if_bytes > 0)) {
  2587. ASSERT(ifp->if_u1.if_data != NULL);
  2588. ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
  2589. memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
  2590. }
  2591. break;
  2592. case XFS_DINODE_FMT_EXTENTS:
  2593. ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
  2594. !(iip->ili_format.ilf_fields & extflag[whichfork]));
  2595. ASSERT((xfs_iext_get_ext(ifp, 0) != NULL) ||
  2596. (ifp->if_bytes == 0));
  2597. ASSERT((xfs_iext_get_ext(ifp, 0) == NULL) ||
  2598. (ifp->if_bytes > 0));
  2599. if ((iip->ili_format.ilf_fields & extflag[whichfork]) &&
  2600. (ifp->if_bytes > 0)) {
  2601. ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
  2602. (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
  2603. whichfork);
  2604. }
  2605. break;
  2606. case XFS_DINODE_FMT_BTREE:
  2607. if ((iip->ili_format.ilf_fields & brootflag[whichfork]) &&
  2608. (ifp->if_broot_bytes > 0)) {
  2609. ASSERT(ifp->if_broot != NULL);
  2610. ASSERT(ifp->if_broot_bytes <=
  2611. (XFS_IFORK_SIZE(ip, whichfork) +
  2612. XFS_BROOT_SIZE_ADJ));
  2613. xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
  2614. (xfs_bmdr_block_t *)cp,
  2615. XFS_DFORK_SIZE(dip, mp, whichfork));
  2616. }
  2617. break;
  2618. case XFS_DINODE_FMT_DEV:
  2619. if (iip->ili_format.ilf_fields & XFS_ILOG_DEV) {
  2620. ASSERT(whichfork == XFS_DATA_FORK);
  2621. xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
  2622. }
  2623. break;
  2624. case XFS_DINODE_FMT_UUID:
  2625. if (iip->ili_format.ilf_fields & XFS_ILOG_UUID) {
  2626. ASSERT(whichfork == XFS_DATA_FORK);
  2627. memcpy(XFS_DFORK_DPTR(dip),
  2628. &ip->i_df.if_u2.if_uuid,
  2629. sizeof(uuid_t));
  2630. }
  2631. break;
  2632. default:
  2633. ASSERT(0);
  2634. break;
  2635. }
  2636. }
  2637. STATIC int
  2638. xfs_iflush_cluster(
  2639. xfs_inode_t *ip,
  2640. xfs_buf_t *bp)
  2641. {
  2642. xfs_mount_t *mp = ip->i_mount;
  2643. xfs_perag_t *pag = xfs_get_perag(mp, ip->i_ino);
  2644. unsigned long first_index, mask;
  2645. unsigned long inodes_per_cluster;
  2646. int ilist_size;
  2647. xfs_inode_t **ilist;
  2648. xfs_inode_t *iq;
  2649. int nr_found;
  2650. int clcount = 0;
  2651. int bufwasdelwri;
  2652. int i;
  2653. ASSERT(pag->pagi_inodeok);
  2654. ASSERT(pag->pag_ici_init);
  2655. inodes_per_cluster = XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog;
  2656. ilist_size = inodes_per_cluster * sizeof(xfs_inode_t *);
  2657. ilist = kmem_alloc(ilist_size, KM_MAYFAIL|KM_NOFS);
  2658. if (!ilist)
  2659. return 0;
  2660. mask = ~(((XFS_INODE_CLUSTER_SIZE(mp) >> mp->m_sb.sb_inodelog)) - 1);
  2661. first_index = XFS_INO_TO_AGINO(mp, ip->i_ino) & mask;
  2662. read_lock(&pag->pag_ici_lock);
  2663. /* really need a gang lookup range call here */
  2664. nr_found = radix_tree_gang_lookup(&pag->pag_ici_root, (void**)ilist,
  2665. first_index, inodes_per_cluster);
  2666. if (nr_found == 0)
  2667. goto out_free;
  2668. for (i = 0; i < nr_found; i++) {
  2669. iq = ilist[i];
  2670. if (iq == ip)
  2671. continue;
  2672. /* if the inode lies outside this cluster, we're done. */
  2673. if ((XFS_INO_TO_AGINO(mp, iq->i_ino) & mask) != first_index)
  2674. break;
  2675. /*
  2676. * Do an un-protected check to see if the inode is dirty and
  2677. * is a candidate for flushing. These checks will be repeated
  2678. * later after the appropriate locks are acquired.
  2679. */
  2680. if (xfs_inode_clean(iq) && xfs_ipincount(iq) == 0)
  2681. continue;
  2682. /*
  2683. * Try to get locks. If any are unavailable or it is pinned,
  2684. * then this inode cannot be flushed and is skipped.
  2685. */
  2686. if (!xfs_ilock_nowait(iq, XFS_ILOCK_SHARED))
  2687. continue;
  2688. if (!xfs_iflock_nowait(iq)) {
  2689. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2690. continue;
  2691. }
  2692. if (xfs_ipincount(iq)) {
  2693. xfs_ifunlock(iq);
  2694. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2695. continue;
  2696. }
  2697. /*
  2698. * arriving here means that this inode can be flushed. First
  2699. * re-check that it's dirty before flushing.
  2700. */
  2701. if (!xfs_inode_clean(iq)) {
  2702. int error;
  2703. error = xfs_iflush_int(iq, bp);
  2704. if (error) {
  2705. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2706. goto cluster_corrupt_out;
  2707. }
  2708. clcount++;
  2709. } else {
  2710. xfs_ifunlock(iq);
  2711. }
  2712. xfs_iunlock(iq, XFS_ILOCK_SHARED);
  2713. }
  2714. if (clcount) {
  2715. XFS_STATS_INC(xs_icluster_flushcnt);
  2716. XFS_STATS_ADD(xs_icluster_flushinode, clcount);
  2717. }
  2718. out_free:
  2719. read_unlock(&pag->pag_ici_lock);
  2720. kmem_free(ilist);
  2721. return 0;
  2722. cluster_corrupt_out:
  2723. /*
  2724. * Corruption detected in the clustering loop. Invalidate the
  2725. * inode buffer and shut down the filesystem.
  2726. */
  2727. read_unlock(&pag->pag_ici_lock);
  2728. /*
  2729. * Clean up the buffer. If it was B_DELWRI, just release it --
  2730. * brelse can handle it with no problems. If not, shut down the
  2731. * filesystem before releasing the buffer.
  2732. */
  2733. bufwasdelwri = XFS_BUF_ISDELAYWRITE(bp);
  2734. if (bufwasdelwri)
  2735. xfs_buf_relse(bp);
  2736. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2737. if (!bufwasdelwri) {
  2738. /*
  2739. * Just like incore_relse: if we have b_iodone functions,
  2740. * mark the buffer as an error and call them. Otherwise
  2741. * mark it as stale and brelse.
  2742. */
  2743. if (XFS_BUF_IODONE_FUNC(bp)) {
  2744. XFS_BUF_CLR_BDSTRAT_FUNC(bp);
  2745. XFS_BUF_UNDONE(bp);
  2746. XFS_BUF_STALE(bp);
  2747. XFS_BUF_SHUT(bp);
  2748. XFS_BUF_ERROR(bp,EIO);
  2749. xfs_biodone(bp);
  2750. } else {
  2751. XFS_BUF_STALE(bp);
  2752. xfs_buf_relse(bp);
  2753. }
  2754. }
  2755. /*
  2756. * Unlocks the flush lock
  2757. */
  2758. xfs_iflush_abort(iq);
  2759. kmem_free(ilist);
  2760. return XFS_ERROR(EFSCORRUPTED);
  2761. }
  2762. /*
  2763. * xfs_iflush() will write a modified inode's changes out to the
  2764. * inode's on disk home. The caller must have the inode lock held
  2765. * in at least shared mode and the inode flush completion must be
  2766. * active as well. The inode lock will still be held upon return from
  2767. * the call and the caller is free to unlock it.
  2768. * The inode flush will be completed when the inode reaches the disk.
  2769. * The flags indicate how the inode's buffer should be written out.
  2770. */
  2771. int
  2772. xfs_iflush(
  2773. xfs_inode_t *ip,
  2774. uint flags)
  2775. {
  2776. xfs_inode_log_item_t *iip;
  2777. xfs_buf_t *bp;
  2778. xfs_dinode_t *dip;
  2779. xfs_mount_t *mp;
  2780. int error;
  2781. int noblock = (flags == XFS_IFLUSH_ASYNC_NOBLOCK);
  2782. enum { INT_DELWRI = (1 << 0), INT_ASYNC = (1 << 1) };
  2783. XFS_STATS_INC(xs_iflush_count);
  2784. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2785. ASSERT(!completion_done(&ip->i_flush));
  2786. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2787. ip->i_d.di_nextents > ip->i_df.if_ext_max);
  2788. iip = ip->i_itemp;
  2789. mp = ip->i_mount;
  2790. /*
  2791. * If the inode isn't dirty, then just release the inode
  2792. * flush lock and do nothing.
  2793. */
  2794. if (xfs_inode_clean(ip)) {
  2795. xfs_ifunlock(ip);
  2796. return 0;
  2797. }
  2798. /*
  2799. * We can't flush the inode until it is unpinned, so wait for it if we
  2800. * are allowed to block. We know noone new can pin it, because we are
  2801. * holding the inode lock shared and you need to hold it exclusively to
  2802. * pin the inode.
  2803. *
  2804. * If we are not allowed to block, force the log out asynchronously so
  2805. * that when we come back the inode will be unpinned. If other inodes
  2806. * in the same cluster are dirty, they will probably write the inode
  2807. * out for us if they occur after the log force completes.
  2808. */
  2809. if (noblock && xfs_ipincount(ip)) {
  2810. xfs_iunpin_nowait(ip);
  2811. xfs_ifunlock(ip);
  2812. return EAGAIN;
  2813. }
  2814. xfs_iunpin_wait(ip);
  2815. /*
  2816. * This may have been unpinned because the filesystem is shutting
  2817. * down forcibly. If that's the case we must not write this inode
  2818. * to disk, because the log record didn't make it to disk!
  2819. */
  2820. if (XFS_FORCED_SHUTDOWN(mp)) {
  2821. ip->i_update_core = 0;
  2822. if (iip)
  2823. iip->ili_format.ilf_fields = 0;
  2824. xfs_ifunlock(ip);
  2825. return XFS_ERROR(EIO);
  2826. }
  2827. /*
  2828. * Decide how buffer will be flushed out. This is done before
  2829. * the call to xfs_iflush_int because this field is zeroed by it.
  2830. */
  2831. if (iip != NULL && iip->ili_format.ilf_fields != 0) {
  2832. /*
  2833. * Flush out the inode buffer according to the directions
  2834. * of the caller. In the cases where the caller has given
  2835. * us a choice choose the non-delwri case. This is because
  2836. * the inode is in the AIL and we need to get it out soon.
  2837. */
  2838. switch (flags) {
  2839. case XFS_IFLUSH_SYNC:
  2840. case XFS_IFLUSH_DELWRI_ELSE_SYNC:
  2841. flags = 0;
  2842. break;
  2843. case XFS_IFLUSH_ASYNC_NOBLOCK:
  2844. case XFS_IFLUSH_ASYNC:
  2845. case XFS_IFLUSH_DELWRI_ELSE_ASYNC:
  2846. flags = INT_ASYNC;
  2847. break;
  2848. case XFS_IFLUSH_DELWRI:
  2849. flags = INT_DELWRI;
  2850. break;
  2851. default:
  2852. ASSERT(0);
  2853. flags = 0;
  2854. break;
  2855. }
  2856. } else {
  2857. switch (flags) {
  2858. case XFS_IFLUSH_DELWRI_ELSE_SYNC:
  2859. case XFS_IFLUSH_DELWRI_ELSE_ASYNC:
  2860. case XFS_IFLUSH_DELWRI:
  2861. flags = INT_DELWRI;
  2862. break;
  2863. case XFS_IFLUSH_ASYNC_NOBLOCK:
  2864. case XFS_IFLUSH_ASYNC:
  2865. flags = INT_ASYNC;
  2866. break;
  2867. case XFS_IFLUSH_SYNC:
  2868. flags = 0;
  2869. break;
  2870. default:
  2871. ASSERT(0);
  2872. flags = 0;
  2873. break;
  2874. }
  2875. }
  2876. /*
  2877. * Get the buffer containing the on-disk inode.
  2878. */
  2879. error = xfs_itobp(mp, NULL, ip, &dip, &bp,
  2880. noblock ? XFS_BUF_TRYLOCK : XFS_BUF_LOCK);
  2881. if (error || !bp) {
  2882. xfs_ifunlock(ip);
  2883. return error;
  2884. }
  2885. /*
  2886. * First flush out the inode that xfs_iflush was called with.
  2887. */
  2888. error = xfs_iflush_int(ip, bp);
  2889. if (error)
  2890. goto corrupt_out;
  2891. /*
  2892. * If the buffer is pinned then push on the log now so we won't
  2893. * get stuck waiting in the write for too long.
  2894. */
  2895. if (XFS_BUF_ISPINNED(bp))
  2896. xfs_log_force(mp, (xfs_lsn_t)0, XFS_LOG_FORCE);
  2897. /*
  2898. * inode clustering:
  2899. * see if other inodes can be gathered into this write
  2900. */
  2901. error = xfs_iflush_cluster(ip, bp);
  2902. if (error)
  2903. goto cluster_corrupt_out;
  2904. if (flags & INT_DELWRI) {
  2905. xfs_bdwrite(mp, bp);
  2906. } else if (flags & INT_ASYNC) {
  2907. error = xfs_bawrite(mp, bp);
  2908. } else {
  2909. error = xfs_bwrite(mp, bp);
  2910. }
  2911. return error;
  2912. corrupt_out:
  2913. xfs_buf_relse(bp);
  2914. xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE);
  2915. cluster_corrupt_out:
  2916. /*
  2917. * Unlocks the flush lock
  2918. */
  2919. xfs_iflush_abort(ip);
  2920. return XFS_ERROR(EFSCORRUPTED);
  2921. }
  2922. STATIC int
  2923. xfs_iflush_int(
  2924. xfs_inode_t *ip,
  2925. xfs_buf_t *bp)
  2926. {
  2927. xfs_inode_log_item_t *iip;
  2928. xfs_dinode_t *dip;
  2929. xfs_mount_t *mp;
  2930. #ifdef XFS_TRANS_DEBUG
  2931. int first;
  2932. #endif
  2933. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
  2934. ASSERT(!completion_done(&ip->i_flush));
  2935. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  2936. ip->i_d.di_nextents > ip->i_df.if_ext_max);
  2937. iip = ip->i_itemp;
  2938. mp = ip->i_mount;
  2939. /*
  2940. * If the inode isn't dirty, then just release the inode
  2941. * flush lock and do nothing.
  2942. */
  2943. if (xfs_inode_clean(ip)) {
  2944. xfs_ifunlock(ip);
  2945. return 0;
  2946. }
  2947. /* set *dip = inode's place in the buffer */
  2948. dip = (xfs_dinode_t *)xfs_buf_offset(bp, ip->i_imap.im_boffset);
  2949. /*
  2950. * Clear i_update_core before copying out the data.
  2951. * This is for coordination with our timestamp updates
  2952. * that don't hold the inode lock. They will always
  2953. * update the timestamps BEFORE setting i_update_core,
  2954. * so if we clear i_update_core after they set it we
  2955. * are guaranteed to see their updates to the timestamps.
  2956. * I believe that this depends on strongly ordered memory
  2957. * semantics, but we have that. We use the SYNCHRONIZE
  2958. * macro to make sure that the compiler does not reorder
  2959. * the i_update_core access below the data copy below.
  2960. */
  2961. ip->i_update_core = 0;
  2962. SYNCHRONIZE();
  2963. /*
  2964. * Make sure to get the latest atime from the Linux inode.
  2965. */
  2966. xfs_synchronize_atime(ip);
  2967. if (XFS_TEST_ERROR(be16_to_cpu(dip->di_magic) != XFS_DINODE_MAGIC,
  2968. mp, XFS_ERRTAG_IFLUSH_1, XFS_RANDOM_IFLUSH_1)) {
  2969. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  2970. "xfs_iflush: Bad inode %Lu magic number 0x%x, ptr 0x%p",
  2971. ip->i_ino, be16_to_cpu(dip->di_magic), dip);
  2972. goto corrupt_out;
  2973. }
  2974. if (XFS_TEST_ERROR(ip->i_d.di_magic != XFS_DINODE_MAGIC,
  2975. mp, XFS_ERRTAG_IFLUSH_2, XFS_RANDOM_IFLUSH_2)) {
  2976. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  2977. "xfs_iflush: Bad inode %Lu, ptr 0x%p, magic number 0x%x",
  2978. ip->i_ino, ip, ip->i_d.di_magic);
  2979. goto corrupt_out;
  2980. }
  2981. if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
  2982. if (XFS_TEST_ERROR(
  2983. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2984. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE),
  2985. mp, XFS_ERRTAG_IFLUSH_3, XFS_RANDOM_IFLUSH_3)) {
  2986. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  2987. "xfs_iflush: Bad regular inode %Lu, ptr 0x%p",
  2988. ip->i_ino, ip);
  2989. goto corrupt_out;
  2990. }
  2991. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
  2992. if (XFS_TEST_ERROR(
  2993. (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS) &&
  2994. (ip->i_d.di_format != XFS_DINODE_FMT_BTREE) &&
  2995. (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL),
  2996. mp, XFS_ERRTAG_IFLUSH_4, XFS_RANDOM_IFLUSH_4)) {
  2997. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  2998. "xfs_iflush: Bad directory inode %Lu, ptr 0x%p",
  2999. ip->i_ino, ip);
  3000. goto corrupt_out;
  3001. }
  3002. }
  3003. if (XFS_TEST_ERROR(ip->i_d.di_nextents + ip->i_d.di_anextents >
  3004. ip->i_d.di_nblocks, mp, XFS_ERRTAG_IFLUSH_5,
  3005. XFS_RANDOM_IFLUSH_5)) {
  3006. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  3007. "xfs_iflush: detected corrupt incore inode %Lu, total extents = %d, nblocks = %Ld, ptr 0x%p",
  3008. ip->i_ino,
  3009. ip->i_d.di_nextents + ip->i_d.di_anextents,
  3010. ip->i_d.di_nblocks,
  3011. ip);
  3012. goto corrupt_out;
  3013. }
  3014. if (XFS_TEST_ERROR(ip->i_d.di_forkoff > mp->m_sb.sb_inodesize,
  3015. mp, XFS_ERRTAG_IFLUSH_6, XFS_RANDOM_IFLUSH_6)) {
  3016. xfs_cmn_err(XFS_PTAG_IFLUSH, CE_ALERT, mp,
  3017. "xfs_iflush: bad inode %Lu, forkoff 0x%x, ptr 0x%p",
  3018. ip->i_ino, ip->i_d.di_forkoff, ip);
  3019. goto corrupt_out;
  3020. }
  3021. /*
  3022. * bump the flush iteration count, used to detect flushes which
  3023. * postdate a log record during recovery.
  3024. */
  3025. ip->i_d.di_flushiter++;
  3026. /*
  3027. * Copy the dirty parts of the inode into the on-disk
  3028. * inode. We always copy out the core of the inode,
  3029. * because if the inode is dirty at all the core must
  3030. * be.
  3031. */
  3032. xfs_dinode_to_disk(dip, &ip->i_d);
  3033. /* Wrap, we never let the log put out DI_MAX_FLUSH */
  3034. if (ip->i_d.di_flushiter == DI_MAX_FLUSH)
  3035. ip->i_d.di_flushiter = 0;
  3036. /*
  3037. * If this is really an old format inode and the superblock version
  3038. * has not been updated to support only new format inodes, then
  3039. * convert back to the old inode format. If the superblock version
  3040. * has been updated, then make the conversion permanent.
  3041. */
  3042. ASSERT(ip->i_d.di_version == 1 || xfs_sb_version_hasnlink(&mp->m_sb));
  3043. if (ip->i_d.di_version == 1) {
  3044. if (!xfs_sb_version_hasnlink(&mp->m_sb)) {
  3045. /*
  3046. * Convert it back.
  3047. */
  3048. ASSERT(ip->i_d.di_nlink <= XFS_MAXLINK_1);
  3049. dip->di_onlink = cpu_to_be16(ip->i_d.di_nlink);
  3050. } else {
  3051. /*
  3052. * The superblock version has already been bumped,
  3053. * so just make the conversion to the new inode
  3054. * format permanent.
  3055. */
  3056. ip->i_d.di_version = 2;
  3057. dip->di_version = 2;
  3058. ip->i_d.di_onlink = 0;
  3059. dip->di_onlink = 0;
  3060. memset(&(ip->i_d.di_pad[0]), 0, sizeof(ip->i_d.di_pad));
  3061. memset(&(dip->di_pad[0]), 0,
  3062. sizeof(dip->di_pad));
  3063. ASSERT(ip->i_d.di_projid == 0);
  3064. }
  3065. }
  3066. xfs_iflush_fork(ip, dip, iip, XFS_DATA_FORK, bp);
  3067. if (XFS_IFORK_Q(ip))
  3068. xfs_iflush_fork(ip, dip, iip, XFS_ATTR_FORK, bp);
  3069. xfs_inobp_check(mp, bp);
  3070. /*
  3071. * We've recorded everything logged in the inode, so we'd
  3072. * like to clear the ilf_fields bits so we don't log and
  3073. * flush things unnecessarily. However, we can't stop
  3074. * logging all this information until the data we've copied
  3075. * into the disk buffer is written to disk. If we did we might
  3076. * overwrite the copy of the inode in the log with all the
  3077. * data after re-logging only part of it, and in the face of
  3078. * a crash we wouldn't have all the data we need to recover.
  3079. *
  3080. * What we do is move the bits to the ili_last_fields field.
  3081. * When logging the inode, these bits are moved back to the
  3082. * ilf_fields field. In the xfs_iflush_done() routine we
  3083. * clear ili_last_fields, since we know that the information
  3084. * those bits represent is permanently on disk. As long as
  3085. * the flush completes before the inode is logged again, then
  3086. * both ilf_fields and ili_last_fields will be cleared.
  3087. *
  3088. * We can play with the ilf_fields bits here, because the inode
  3089. * lock must be held exclusively in order to set bits there
  3090. * and the flush lock protects the ili_last_fields bits.
  3091. * Set ili_logged so the flush done
  3092. * routine can tell whether or not to look in the AIL.
  3093. * Also, store the current LSN of the inode so that we can tell
  3094. * whether the item has moved in the AIL from xfs_iflush_done().
  3095. * In order to read the lsn we need the AIL lock, because
  3096. * it is a 64 bit value that cannot be read atomically.
  3097. */
  3098. if (iip != NULL && iip->ili_format.ilf_fields != 0) {
  3099. iip->ili_last_fields = iip->ili_format.ilf_fields;
  3100. iip->ili_format.ilf_fields = 0;
  3101. iip->ili_logged = 1;
  3102. xfs_trans_ail_copy_lsn(mp->m_ail, &iip->ili_flush_lsn,
  3103. &iip->ili_item.li_lsn);
  3104. /*
  3105. * Attach the function xfs_iflush_done to the inode's
  3106. * buffer. This will remove the inode from the AIL
  3107. * and unlock the inode's flush lock when the inode is
  3108. * completely written to disk.
  3109. */
  3110. xfs_buf_attach_iodone(bp, (void(*)(xfs_buf_t*,xfs_log_item_t*))
  3111. xfs_iflush_done, (xfs_log_item_t *)iip);
  3112. ASSERT(XFS_BUF_FSPRIVATE(bp, void *) != NULL);
  3113. ASSERT(XFS_BUF_IODONE_FUNC(bp) != NULL);
  3114. } else {
  3115. /*
  3116. * We're flushing an inode which is not in the AIL and has
  3117. * not been logged but has i_update_core set. For this
  3118. * case we can use a B_DELWRI flush and immediately drop
  3119. * the inode flush lock because we can avoid the whole
  3120. * AIL state thing. It's OK to drop the flush lock now,
  3121. * because we've already locked the buffer and to do anything
  3122. * you really need both.
  3123. */
  3124. if (iip != NULL) {
  3125. ASSERT(iip->ili_logged == 0);
  3126. ASSERT(iip->ili_last_fields == 0);
  3127. ASSERT((iip->ili_item.li_flags & XFS_LI_IN_AIL) == 0);
  3128. }
  3129. xfs_ifunlock(ip);
  3130. }
  3131. return 0;
  3132. corrupt_out:
  3133. return XFS_ERROR(EFSCORRUPTED);
  3134. }
  3135. #ifdef XFS_ILOCK_TRACE
  3136. ktrace_t *xfs_ilock_trace_buf;
  3137. void
  3138. xfs_ilock_trace(xfs_inode_t *ip, int lock, unsigned int lockflags, inst_t *ra)
  3139. {
  3140. ktrace_enter(ip->i_lock_trace,
  3141. (void *)ip,
  3142. (void *)(unsigned long)lock, /* 1 = LOCK, 3=UNLOCK, etc */
  3143. (void *)(unsigned long)lockflags, /* XFS_ILOCK_EXCL etc */
  3144. (void *)ra, /* caller of ilock */
  3145. (void *)(unsigned long)current_cpu(),
  3146. (void *)(unsigned long)current_pid(),
  3147. NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
  3148. }
  3149. #endif
  3150. /*
  3151. * Return a pointer to the extent record at file index idx.
  3152. */
  3153. xfs_bmbt_rec_host_t *
  3154. xfs_iext_get_ext(
  3155. xfs_ifork_t *ifp, /* inode fork pointer */
  3156. xfs_extnum_t idx) /* index of target extent */
  3157. {
  3158. ASSERT(idx >= 0);
  3159. if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
  3160. return ifp->if_u1.if_ext_irec->er_extbuf;
  3161. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  3162. xfs_ext_irec_t *erp; /* irec pointer */
  3163. int erp_idx = 0; /* irec index */
  3164. xfs_extnum_t page_idx = idx; /* ext index in target list */
  3165. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  3166. return &erp->er_extbuf[page_idx];
  3167. } else if (ifp->if_bytes) {
  3168. return &ifp->if_u1.if_extents[idx];
  3169. } else {
  3170. return NULL;
  3171. }
  3172. }
  3173. /*
  3174. * Insert new item(s) into the extent records for incore inode
  3175. * fork 'ifp'. 'count' new items are inserted at index 'idx'.
  3176. */
  3177. void
  3178. xfs_iext_insert(
  3179. xfs_ifork_t *ifp, /* inode fork pointer */
  3180. xfs_extnum_t idx, /* starting index of new items */
  3181. xfs_extnum_t count, /* number of inserted items */
  3182. xfs_bmbt_irec_t *new) /* items to insert */
  3183. {
  3184. xfs_extnum_t i; /* extent record index */
  3185. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  3186. xfs_iext_add(ifp, idx, count);
  3187. for (i = idx; i < idx + count; i++, new++)
  3188. xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
  3189. }
  3190. /*
  3191. * This is called when the amount of space required for incore file
  3192. * extents needs to be increased. The ext_diff parameter stores the
  3193. * number of new extents being added and the idx parameter contains
  3194. * the extent index where the new extents will be added. If the new
  3195. * extents are being appended, then we just need to (re)allocate and
  3196. * initialize the space. Otherwise, if the new extents are being
  3197. * inserted into the middle of the existing entries, a bit more work
  3198. * is required to make room for the new extents to be inserted. The
  3199. * caller is responsible for filling in the new extent entries upon
  3200. * return.
  3201. */
  3202. void
  3203. xfs_iext_add(
  3204. xfs_ifork_t *ifp, /* inode fork pointer */
  3205. xfs_extnum_t idx, /* index to begin adding exts */
  3206. int ext_diff) /* number of extents to add */
  3207. {
  3208. int byte_diff; /* new bytes being added */
  3209. int new_size; /* size of extents after adding */
  3210. xfs_extnum_t nextents; /* number of extents in file */
  3211. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3212. ASSERT((idx >= 0) && (idx <= nextents));
  3213. byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
  3214. new_size = ifp->if_bytes + byte_diff;
  3215. /*
  3216. * If the new number of extents (nextents + ext_diff)
  3217. * fits inside the inode, then continue to use the inline
  3218. * extent buffer.
  3219. */
  3220. if (nextents + ext_diff <= XFS_INLINE_EXTS) {
  3221. if (idx < nextents) {
  3222. memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
  3223. &ifp->if_u2.if_inline_ext[idx],
  3224. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  3225. memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
  3226. }
  3227. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  3228. ifp->if_real_bytes = 0;
  3229. ifp->if_lastex = nextents + ext_diff;
  3230. }
  3231. /*
  3232. * Otherwise use a linear (direct) extent list.
  3233. * If the extents are currently inside the inode,
  3234. * xfs_iext_realloc_direct will switch us from
  3235. * inline to direct extent allocation mode.
  3236. */
  3237. else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
  3238. xfs_iext_realloc_direct(ifp, new_size);
  3239. if (idx < nextents) {
  3240. memmove(&ifp->if_u1.if_extents[idx + ext_diff],
  3241. &ifp->if_u1.if_extents[idx],
  3242. (nextents - idx) * sizeof(xfs_bmbt_rec_t));
  3243. memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
  3244. }
  3245. }
  3246. /* Indirection array */
  3247. else {
  3248. xfs_ext_irec_t *erp;
  3249. int erp_idx = 0;
  3250. int page_idx = idx;
  3251. ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
  3252. if (ifp->if_flags & XFS_IFEXTIREC) {
  3253. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
  3254. } else {
  3255. xfs_iext_irec_init(ifp);
  3256. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3257. erp = ifp->if_u1.if_ext_irec;
  3258. }
  3259. /* Extents fit in target extent page */
  3260. if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
  3261. if (page_idx < erp->er_extcount) {
  3262. memmove(&erp->er_extbuf[page_idx + ext_diff],
  3263. &erp->er_extbuf[page_idx],
  3264. (erp->er_extcount - page_idx) *
  3265. sizeof(xfs_bmbt_rec_t));
  3266. memset(&erp->er_extbuf[page_idx], 0, byte_diff);
  3267. }
  3268. erp->er_extcount += ext_diff;
  3269. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  3270. }
  3271. /* Insert a new extent page */
  3272. else if (erp) {
  3273. xfs_iext_add_indirect_multi(ifp,
  3274. erp_idx, page_idx, ext_diff);
  3275. }
  3276. /*
  3277. * If extent(s) are being appended to the last page in
  3278. * the indirection array and the new extent(s) don't fit
  3279. * in the page, then erp is NULL and erp_idx is set to
  3280. * the next index needed in the indirection array.
  3281. */
  3282. else {
  3283. int count = ext_diff;
  3284. while (count) {
  3285. erp = xfs_iext_irec_new(ifp, erp_idx);
  3286. erp->er_extcount = count;
  3287. count -= MIN(count, (int)XFS_LINEAR_EXTS);
  3288. if (count) {
  3289. erp_idx++;
  3290. }
  3291. }
  3292. }
  3293. }
  3294. ifp->if_bytes = new_size;
  3295. }
  3296. /*
  3297. * This is called when incore extents are being added to the indirection
  3298. * array and the new extents do not fit in the target extent list. The
  3299. * erp_idx parameter contains the irec index for the target extent list
  3300. * in the indirection array, and the idx parameter contains the extent
  3301. * index within the list. The number of extents being added is stored
  3302. * in the count parameter.
  3303. *
  3304. * |-------| |-------|
  3305. * | | | | idx - number of extents before idx
  3306. * | idx | | count |
  3307. * | | | | count - number of extents being inserted at idx
  3308. * |-------| |-------|
  3309. * | count | | nex2 | nex2 - number of extents after idx + count
  3310. * |-------| |-------|
  3311. */
  3312. void
  3313. xfs_iext_add_indirect_multi(
  3314. xfs_ifork_t *ifp, /* inode fork pointer */
  3315. int erp_idx, /* target extent irec index */
  3316. xfs_extnum_t idx, /* index within target list */
  3317. int count) /* new extents being added */
  3318. {
  3319. int byte_diff; /* new bytes being added */
  3320. xfs_ext_irec_t *erp; /* pointer to irec entry */
  3321. xfs_extnum_t ext_diff; /* number of extents to add */
  3322. xfs_extnum_t ext_cnt; /* new extents still needed */
  3323. xfs_extnum_t nex2; /* extents after idx + count */
  3324. xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
  3325. int nlists; /* number of irec's (lists) */
  3326. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3327. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3328. nex2 = erp->er_extcount - idx;
  3329. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3330. /*
  3331. * Save second part of target extent list
  3332. * (all extents past */
  3333. if (nex2) {
  3334. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  3335. nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
  3336. memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
  3337. erp->er_extcount -= nex2;
  3338. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
  3339. memset(&erp->er_extbuf[idx], 0, byte_diff);
  3340. }
  3341. /*
  3342. * Add the new extents to the end of the target
  3343. * list, then allocate new irec record(s) and
  3344. * extent buffer(s) as needed to store the rest
  3345. * of the new extents.
  3346. */
  3347. ext_cnt = count;
  3348. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
  3349. if (ext_diff) {
  3350. erp->er_extcount += ext_diff;
  3351. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  3352. ext_cnt -= ext_diff;
  3353. }
  3354. while (ext_cnt) {
  3355. erp_idx++;
  3356. erp = xfs_iext_irec_new(ifp, erp_idx);
  3357. ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
  3358. erp->er_extcount = ext_diff;
  3359. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
  3360. ext_cnt -= ext_diff;
  3361. }
  3362. /* Add nex2 extents back to indirection array */
  3363. if (nex2) {
  3364. xfs_extnum_t ext_avail;
  3365. int i;
  3366. byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
  3367. ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
  3368. i = 0;
  3369. /*
  3370. * If nex2 extents fit in the current page, append
  3371. * nex2_ep after the new extents.
  3372. */
  3373. if (nex2 <= ext_avail) {
  3374. i = erp->er_extcount;
  3375. }
  3376. /*
  3377. * Otherwise, check if space is available in the
  3378. * next page.
  3379. */
  3380. else if ((erp_idx < nlists - 1) &&
  3381. (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
  3382. ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
  3383. erp_idx++;
  3384. erp++;
  3385. /* Create a hole for nex2 extents */
  3386. memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
  3387. erp->er_extcount * sizeof(xfs_bmbt_rec_t));
  3388. }
  3389. /*
  3390. * Final choice, create a new extent page for
  3391. * nex2 extents.
  3392. */
  3393. else {
  3394. erp_idx++;
  3395. erp = xfs_iext_irec_new(ifp, erp_idx);
  3396. }
  3397. memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
  3398. kmem_free(nex2_ep);
  3399. erp->er_extcount += nex2;
  3400. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
  3401. }
  3402. }
  3403. /*
  3404. * This is called when the amount of space required for incore file
  3405. * extents needs to be decreased. The ext_diff parameter stores the
  3406. * number of extents to be removed and the idx parameter contains
  3407. * the extent index where the extents will be removed from.
  3408. *
  3409. * If the amount of space needed has decreased below the linear
  3410. * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
  3411. * extent array. Otherwise, use kmem_realloc() to adjust the
  3412. * size to what is needed.
  3413. */
  3414. void
  3415. xfs_iext_remove(
  3416. xfs_ifork_t *ifp, /* inode fork pointer */
  3417. xfs_extnum_t idx, /* index to begin removing exts */
  3418. int ext_diff) /* number of extents to remove */
  3419. {
  3420. xfs_extnum_t nextents; /* number of extents in file */
  3421. int new_size; /* size of extents after removal */
  3422. ASSERT(ext_diff > 0);
  3423. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3424. new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
  3425. if (new_size == 0) {
  3426. xfs_iext_destroy(ifp);
  3427. } else if (ifp->if_flags & XFS_IFEXTIREC) {
  3428. xfs_iext_remove_indirect(ifp, idx, ext_diff);
  3429. } else if (ifp->if_real_bytes) {
  3430. xfs_iext_remove_direct(ifp, idx, ext_diff);
  3431. } else {
  3432. xfs_iext_remove_inline(ifp, idx, ext_diff);
  3433. }
  3434. ifp->if_bytes = new_size;
  3435. }
  3436. /*
  3437. * This removes ext_diff extents from the inline buffer, beginning
  3438. * at extent index idx.
  3439. */
  3440. void
  3441. xfs_iext_remove_inline(
  3442. xfs_ifork_t *ifp, /* inode fork pointer */
  3443. xfs_extnum_t idx, /* index to begin removing exts */
  3444. int ext_diff) /* number of extents to remove */
  3445. {
  3446. int nextents; /* number of extents in file */
  3447. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  3448. ASSERT(idx < XFS_INLINE_EXTS);
  3449. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3450. ASSERT(((nextents - ext_diff) > 0) &&
  3451. (nextents - ext_diff) < XFS_INLINE_EXTS);
  3452. if (idx + ext_diff < nextents) {
  3453. memmove(&ifp->if_u2.if_inline_ext[idx],
  3454. &ifp->if_u2.if_inline_ext[idx + ext_diff],
  3455. (nextents - (idx + ext_diff)) *
  3456. sizeof(xfs_bmbt_rec_t));
  3457. memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
  3458. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  3459. } else {
  3460. memset(&ifp->if_u2.if_inline_ext[idx], 0,
  3461. ext_diff * sizeof(xfs_bmbt_rec_t));
  3462. }
  3463. }
  3464. /*
  3465. * This removes ext_diff extents from a linear (direct) extent list,
  3466. * beginning at extent index idx. If the extents are being removed
  3467. * from the end of the list (ie. truncate) then we just need to re-
  3468. * allocate the list to remove the extra space. Otherwise, if the
  3469. * extents are being removed from the middle of the existing extent
  3470. * entries, then we first need to move the extent records beginning
  3471. * at idx + ext_diff up in the list to overwrite the records being
  3472. * removed, then remove the extra space via kmem_realloc.
  3473. */
  3474. void
  3475. xfs_iext_remove_direct(
  3476. xfs_ifork_t *ifp, /* inode fork pointer */
  3477. xfs_extnum_t idx, /* index to begin removing exts */
  3478. int ext_diff) /* number of extents to remove */
  3479. {
  3480. xfs_extnum_t nextents; /* number of extents in file */
  3481. int new_size; /* size of extents after removal */
  3482. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  3483. new_size = ifp->if_bytes -
  3484. (ext_diff * sizeof(xfs_bmbt_rec_t));
  3485. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3486. if (new_size == 0) {
  3487. xfs_iext_destroy(ifp);
  3488. return;
  3489. }
  3490. /* Move extents up in the list (if needed) */
  3491. if (idx + ext_diff < nextents) {
  3492. memmove(&ifp->if_u1.if_extents[idx],
  3493. &ifp->if_u1.if_extents[idx + ext_diff],
  3494. (nextents - (idx + ext_diff)) *
  3495. sizeof(xfs_bmbt_rec_t));
  3496. }
  3497. memset(&ifp->if_u1.if_extents[nextents - ext_diff],
  3498. 0, ext_diff * sizeof(xfs_bmbt_rec_t));
  3499. /*
  3500. * Reallocate the direct extent list. If the extents
  3501. * will fit inside the inode then xfs_iext_realloc_direct
  3502. * will switch from direct to inline extent allocation
  3503. * mode for us.
  3504. */
  3505. xfs_iext_realloc_direct(ifp, new_size);
  3506. ifp->if_bytes = new_size;
  3507. }
  3508. /*
  3509. * This is called when incore extents are being removed from the
  3510. * indirection array and the extents being removed span multiple extent
  3511. * buffers. The idx parameter contains the file extent index where we
  3512. * want to begin removing extents, and the count parameter contains
  3513. * how many extents need to be removed.
  3514. *
  3515. * |-------| |-------|
  3516. * | nex1 | | | nex1 - number of extents before idx
  3517. * |-------| | count |
  3518. * | | | | count - number of extents being removed at idx
  3519. * | count | |-------|
  3520. * | | | nex2 | nex2 - number of extents after idx + count
  3521. * |-------| |-------|
  3522. */
  3523. void
  3524. xfs_iext_remove_indirect(
  3525. xfs_ifork_t *ifp, /* inode fork pointer */
  3526. xfs_extnum_t idx, /* index to begin removing extents */
  3527. int count) /* number of extents to remove */
  3528. {
  3529. xfs_ext_irec_t *erp; /* indirection array pointer */
  3530. int erp_idx = 0; /* indirection array index */
  3531. xfs_extnum_t ext_cnt; /* extents left to remove */
  3532. xfs_extnum_t ext_diff; /* extents to remove in current list */
  3533. xfs_extnum_t nex1; /* number of extents before idx */
  3534. xfs_extnum_t nex2; /* extents after idx + count */
  3535. int nlists; /* entries in indirection array */
  3536. int page_idx = idx; /* index in target extent list */
  3537. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3538. erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
  3539. ASSERT(erp != NULL);
  3540. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3541. nex1 = page_idx;
  3542. ext_cnt = count;
  3543. while (ext_cnt) {
  3544. nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
  3545. ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
  3546. /*
  3547. * Check for deletion of entire list;
  3548. * xfs_iext_irec_remove() updates extent offsets.
  3549. */
  3550. if (ext_diff == erp->er_extcount) {
  3551. xfs_iext_irec_remove(ifp, erp_idx);
  3552. ext_cnt -= ext_diff;
  3553. nex1 = 0;
  3554. if (ext_cnt) {
  3555. ASSERT(erp_idx < ifp->if_real_bytes /
  3556. XFS_IEXT_BUFSZ);
  3557. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3558. nex1 = 0;
  3559. continue;
  3560. } else {
  3561. break;
  3562. }
  3563. }
  3564. /* Move extents up (if needed) */
  3565. if (nex2) {
  3566. memmove(&erp->er_extbuf[nex1],
  3567. &erp->er_extbuf[nex1 + ext_diff],
  3568. nex2 * sizeof(xfs_bmbt_rec_t));
  3569. }
  3570. /* Zero out rest of page */
  3571. memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
  3572. ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
  3573. /* Update remaining counters */
  3574. erp->er_extcount -= ext_diff;
  3575. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
  3576. ext_cnt -= ext_diff;
  3577. nex1 = 0;
  3578. erp_idx++;
  3579. erp++;
  3580. }
  3581. ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
  3582. xfs_iext_irec_compact(ifp);
  3583. }
  3584. /*
  3585. * Create, destroy, or resize a linear (direct) block of extents.
  3586. */
  3587. void
  3588. xfs_iext_realloc_direct(
  3589. xfs_ifork_t *ifp, /* inode fork pointer */
  3590. int new_size) /* new size of extents */
  3591. {
  3592. int rnew_size; /* real new size of extents */
  3593. rnew_size = new_size;
  3594. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
  3595. ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
  3596. (new_size != ifp->if_real_bytes)));
  3597. /* Free extent records */
  3598. if (new_size == 0) {
  3599. xfs_iext_destroy(ifp);
  3600. }
  3601. /* Resize direct extent list and zero any new bytes */
  3602. else if (ifp->if_real_bytes) {
  3603. /* Check if extents will fit inside the inode */
  3604. if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
  3605. xfs_iext_direct_to_inline(ifp, new_size /
  3606. (uint)sizeof(xfs_bmbt_rec_t));
  3607. ifp->if_bytes = new_size;
  3608. return;
  3609. }
  3610. if (!is_power_of_2(new_size)){
  3611. rnew_size = roundup_pow_of_two(new_size);
  3612. }
  3613. if (rnew_size != ifp->if_real_bytes) {
  3614. ifp->if_u1.if_extents =
  3615. kmem_realloc(ifp->if_u1.if_extents,
  3616. rnew_size,
  3617. ifp->if_real_bytes, KM_NOFS);
  3618. }
  3619. if (rnew_size > ifp->if_real_bytes) {
  3620. memset(&ifp->if_u1.if_extents[ifp->if_bytes /
  3621. (uint)sizeof(xfs_bmbt_rec_t)], 0,
  3622. rnew_size - ifp->if_real_bytes);
  3623. }
  3624. }
  3625. /*
  3626. * Switch from the inline extent buffer to a direct
  3627. * extent list. Be sure to include the inline extent
  3628. * bytes in new_size.
  3629. */
  3630. else {
  3631. new_size += ifp->if_bytes;
  3632. if (!is_power_of_2(new_size)) {
  3633. rnew_size = roundup_pow_of_two(new_size);
  3634. }
  3635. xfs_iext_inline_to_direct(ifp, rnew_size);
  3636. }
  3637. ifp->if_real_bytes = rnew_size;
  3638. ifp->if_bytes = new_size;
  3639. }
  3640. /*
  3641. * Switch from linear (direct) extent records to inline buffer.
  3642. */
  3643. void
  3644. xfs_iext_direct_to_inline(
  3645. xfs_ifork_t *ifp, /* inode fork pointer */
  3646. xfs_extnum_t nextents) /* number of extents in file */
  3647. {
  3648. ASSERT(ifp->if_flags & XFS_IFEXTENTS);
  3649. ASSERT(nextents <= XFS_INLINE_EXTS);
  3650. /*
  3651. * The inline buffer was zeroed when we switched
  3652. * from inline to direct extent allocation mode,
  3653. * so we don't need to clear it here.
  3654. */
  3655. memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
  3656. nextents * sizeof(xfs_bmbt_rec_t));
  3657. kmem_free(ifp->if_u1.if_extents);
  3658. ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
  3659. ifp->if_real_bytes = 0;
  3660. }
  3661. /*
  3662. * Switch from inline buffer to linear (direct) extent records.
  3663. * new_size should already be rounded up to the next power of 2
  3664. * by the caller (when appropriate), so use new_size as it is.
  3665. * However, since new_size may be rounded up, we can't update
  3666. * if_bytes here. It is the caller's responsibility to update
  3667. * if_bytes upon return.
  3668. */
  3669. void
  3670. xfs_iext_inline_to_direct(
  3671. xfs_ifork_t *ifp, /* inode fork pointer */
  3672. int new_size) /* number of extents in file */
  3673. {
  3674. ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
  3675. memset(ifp->if_u1.if_extents, 0, new_size);
  3676. if (ifp->if_bytes) {
  3677. memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
  3678. ifp->if_bytes);
  3679. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  3680. sizeof(xfs_bmbt_rec_t));
  3681. }
  3682. ifp->if_real_bytes = new_size;
  3683. }
  3684. /*
  3685. * Resize an extent indirection array to new_size bytes.
  3686. */
  3687. void
  3688. xfs_iext_realloc_indirect(
  3689. xfs_ifork_t *ifp, /* inode fork pointer */
  3690. int new_size) /* new indirection array size */
  3691. {
  3692. int nlists; /* number of irec's (ex lists) */
  3693. int size; /* current indirection array size */
  3694. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3695. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3696. size = nlists * sizeof(xfs_ext_irec_t);
  3697. ASSERT(ifp->if_real_bytes);
  3698. ASSERT((new_size >= 0) && (new_size != size));
  3699. if (new_size == 0) {
  3700. xfs_iext_destroy(ifp);
  3701. } else {
  3702. ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
  3703. kmem_realloc(ifp->if_u1.if_ext_irec,
  3704. new_size, size, KM_NOFS);
  3705. }
  3706. }
  3707. /*
  3708. * Switch from indirection array to linear (direct) extent allocations.
  3709. */
  3710. void
  3711. xfs_iext_indirect_to_direct(
  3712. xfs_ifork_t *ifp) /* inode fork pointer */
  3713. {
  3714. xfs_bmbt_rec_host_t *ep; /* extent record pointer */
  3715. xfs_extnum_t nextents; /* number of extents in file */
  3716. int size; /* size of file extents */
  3717. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3718. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3719. ASSERT(nextents <= XFS_LINEAR_EXTS);
  3720. size = nextents * sizeof(xfs_bmbt_rec_t);
  3721. xfs_iext_irec_compact_pages(ifp);
  3722. ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
  3723. ep = ifp->if_u1.if_ext_irec->er_extbuf;
  3724. kmem_free(ifp->if_u1.if_ext_irec);
  3725. ifp->if_flags &= ~XFS_IFEXTIREC;
  3726. ifp->if_u1.if_extents = ep;
  3727. ifp->if_bytes = size;
  3728. if (nextents < XFS_LINEAR_EXTS) {
  3729. xfs_iext_realloc_direct(ifp, size);
  3730. }
  3731. }
  3732. /*
  3733. * Free incore file extents.
  3734. */
  3735. void
  3736. xfs_iext_destroy(
  3737. xfs_ifork_t *ifp) /* inode fork pointer */
  3738. {
  3739. if (ifp->if_flags & XFS_IFEXTIREC) {
  3740. int erp_idx;
  3741. int nlists;
  3742. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3743. for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
  3744. xfs_iext_irec_remove(ifp, erp_idx);
  3745. }
  3746. ifp->if_flags &= ~XFS_IFEXTIREC;
  3747. } else if (ifp->if_real_bytes) {
  3748. kmem_free(ifp->if_u1.if_extents);
  3749. } else if (ifp->if_bytes) {
  3750. memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
  3751. sizeof(xfs_bmbt_rec_t));
  3752. }
  3753. ifp->if_u1.if_extents = NULL;
  3754. ifp->if_real_bytes = 0;
  3755. ifp->if_bytes = 0;
  3756. }
  3757. /*
  3758. * Return a pointer to the extent record for file system block bno.
  3759. */
  3760. xfs_bmbt_rec_host_t * /* pointer to found extent record */
  3761. xfs_iext_bno_to_ext(
  3762. xfs_ifork_t *ifp, /* inode fork pointer */
  3763. xfs_fileoff_t bno, /* block number to search for */
  3764. xfs_extnum_t *idxp) /* index of target extent */
  3765. {
  3766. xfs_bmbt_rec_host_t *base; /* pointer to first extent */
  3767. xfs_filblks_t blockcount = 0; /* number of blocks in extent */
  3768. xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
  3769. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  3770. int high; /* upper boundary in search */
  3771. xfs_extnum_t idx = 0; /* index of target extent */
  3772. int low; /* lower boundary in search */
  3773. xfs_extnum_t nextents; /* number of file extents */
  3774. xfs_fileoff_t startoff = 0; /* start offset of extent */
  3775. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3776. if (nextents == 0) {
  3777. *idxp = 0;
  3778. return NULL;
  3779. }
  3780. low = 0;
  3781. if (ifp->if_flags & XFS_IFEXTIREC) {
  3782. /* Find target extent list */
  3783. int erp_idx = 0;
  3784. erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
  3785. base = erp->er_extbuf;
  3786. high = erp->er_extcount - 1;
  3787. } else {
  3788. base = ifp->if_u1.if_extents;
  3789. high = nextents - 1;
  3790. }
  3791. /* Binary search extent records */
  3792. while (low <= high) {
  3793. idx = (low + high) >> 1;
  3794. ep = base + idx;
  3795. startoff = xfs_bmbt_get_startoff(ep);
  3796. blockcount = xfs_bmbt_get_blockcount(ep);
  3797. if (bno < startoff) {
  3798. high = idx - 1;
  3799. } else if (bno >= startoff + blockcount) {
  3800. low = idx + 1;
  3801. } else {
  3802. /* Convert back to file-based extent index */
  3803. if (ifp->if_flags & XFS_IFEXTIREC) {
  3804. idx += erp->er_extoff;
  3805. }
  3806. *idxp = idx;
  3807. return ep;
  3808. }
  3809. }
  3810. /* Convert back to file-based extent index */
  3811. if (ifp->if_flags & XFS_IFEXTIREC) {
  3812. idx += erp->er_extoff;
  3813. }
  3814. if (bno >= startoff + blockcount) {
  3815. if (++idx == nextents) {
  3816. ep = NULL;
  3817. } else {
  3818. ep = xfs_iext_get_ext(ifp, idx);
  3819. }
  3820. }
  3821. *idxp = idx;
  3822. return ep;
  3823. }
  3824. /*
  3825. * Return a pointer to the indirection array entry containing the
  3826. * extent record for filesystem block bno. Store the index of the
  3827. * target irec in *erp_idxp.
  3828. */
  3829. xfs_ext_irec_t * /* pointer to found extent record */
  3830. xfs_iext_bno_to_irec(
  3831. xfs_ifork_t *ifp, /* inode fork pointer */
  3832. xfs_fileoff_t bno, /* block number to search for */
  3833. int *erp_idxp) /* irec index of target ext list */
  3834. {
  3835. xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
  3836. xfs_ext_irec_t *erp_next; /* next indirection array entry */
  3837. int erp_idx; /* indirection array index */
  3838. int nlists; /* number of extent irec's (lists) */
  3839. int high; /* binary search upper limit */
  3840. int low; /* binary search lower limit */
  3841. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3842. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3843. erp_idx = 0;
  3844. low = 0;
  3845. high = nlists - 1;
  3846. while (low <= high) {
  3847. erp_idx = (low + high) >> 1;
  3848. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3849. erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
  3850. if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
  3851. high = erp_idx - 1;
  3852. } else if (erp_next && bno >=
  3853. xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
  3854. low = erp_idx + 1;
  3855. } else {
  3856. break;
  3857. }
  3858. }
  3859. *erp_idxp = erp_idx;
  3860. return erp;
  3861. }
  3862. /*
  3863. * Return a pointer to the indirection array entry containing the
  3864. * extent record at file extent index *idxp. Store the index of the
  3865. * target irec in *erp_idxp and store the page index of the target
  3866. * extent record in *idxp.
  3867. */
  3868. xfs_ext_irec_t *
  3869. xfs_iext_idx_to_irec(
  3870. xfs_ifork_t *ifp, /* inode fork pointer */
  3871. xfs_extnum_t *idxp, /* extent index (file -> page) */
  3872. int *erp_idxp, /* pointer to target irec */
  3873. int realloc) /* new bytes were just added */
  3874. {
  3875. xfs_ext_irec_t *prev; /* pointer to previous irec */
  3876. xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
  3877. int erp_idx; /* indirection array index */
  3878. int nlists; /* number of irec's (ex lists) */
  3879. int high; /* binary search upper limit */
  3880. int low; /* binary search lower limit */
  3881. xfs_extnum_t page_idx = *idxp; /* extent index in target list */
  3882. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3883. ASSERT(page_idx >= 0 && page_idx <=
  3884. ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
  3885. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3886. erp_idx = 0;
  3887. low = 0;
  3888. high = nlists - 1;
  3889. /* Binary search extent irec's */
  3890. while (low <= high) {
  3891. erp_idx = (low + high) >> 1;
  3892. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3893. prev = erp_idx > 0 ? erp - 1 : NULL;
  3894. if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
  3895. realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
  3896. high = erp_idx - 1;
  3897. } else if (page_idx > erp->er_extoff + erp->er_extcount ||
  3898. (page_idx == erp->er_extoff + erp->er_extcount &&
  3899. !realloc)) {
  3900. low = erp_idx + 1;
  3901. } else if (page_idx == erp->er_extoff + erp->er_extcount &&
  3902. erp->er_extcount == XFS_LINEAR_EXTS) {
  3903. ASSERT(realloc);
  3904. page_idx = 0;
  3905. erp_idx++;
  3906. erp = erp_idx < nlists ? erp + 1 : NULL;
  3907. break;
  3908. } else {
  3909. page_idx -= erp->er_extoff;
  3910. break;
  3911. }
  3912. }
  3913. *idxp = page_idx;
  3914. *erp_idxp = erp_idx;
  3915. return(erp);
  3916. }
  3917. /*
  3918. * Allocate and initialize an indirection array once the space needed
  3919. * for incore extents increases above XFS_IEXT_BUFSZ.
  3920. */
  3921. void
  3922. xfs_iext_irec_init(
  3923. xfs_ifork_t *ifp) /* inode fork pointer */
  3924. {
  3925. xfs_ext_irec_t *erp; /* indirection array pointer */
  3926. xfs_extnum_t nextents; /* number of extents in file */
  3927. ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
  3928. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  3929. ASSERT(nextents <= XFS_LINEAR_EXTS);
  3930. erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
  3931. if (nextents == 0) {
  3932. ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  3933. } else if (!ifp->if_real_bytes) {
  3934. xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
  3935. } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
  3936. xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
  3937. }
  3938. erp->er_extbuf = ifp->if_u1.if_extents;
  3939. erp->er_extcount = nextents;
  3940. erp->er_extoff = 0;
  3941. ifp->if_flags |= XFS_IFEXTIREC;
  3942. ifp->if_real_bytes = XFS_IEXT_BUFSZ;
  3943. ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
  3944. ifp->if_u1.if_ext_irec = erp;
  3945. return;
  3946. }
  3947. /*
  3948. * Allocate and initialize a new entry in the indirection array.
  3949. */
  3950. xfs_ext_irec_t *
  3951. xfs_iext_irec_new(
  3952. xfs_ifork_t *ifp, /* inode fork pointer */
  3953. int erp_idx) /* index for new irec */
  3954. {
  3955. xfs_ext_irec_t *erp; /* indirection array pointer */
  3956. int i; /* loop counter */
  3957. int nlists; /* number of irec's (ex lists) */
  3958. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3959. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3960. /* Resize indirection array */
  3961. xfs_iext_realloc_indirect(ifp, ++nlists *
  3962. sizeof(xfs_ext_irec_t));
  3963. /*
  3964. * Move records down in the array so the
  3965. * new page can use erp_idx.
  3966. */
  3967. erp = ifp->if_u1.if_ext_irec;
  3968. for (i = nlists - 1; i > erp_idx; i--) {
  3969. memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
  3970. }
  3971. ASSERT(i == erp_idx);
  3972. /* Initialize new extent record */
  3973. erp = ifp->if_u1.if_ext_irec;
  3974. erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
  3975. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  3976. memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
  3977. erp[erp_idx].er_extcount = 0;
  3978. erp[erp_idx].er_extoff = erp_idx > 0 ?
  3979. erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
  3980. return (&erp[erp_idx]);
  3981. }
  3982. /*
  3983. * Remove a record from the indirection array.
  3984. */
  3985. void
  3986. xfs_iext_irec_remove(
  3987. xfs_ifork_t *ifp, /* inode fork pointer */
  3988. int erp_idx) /* irec index to remove */
  3989. {
  3990. xfs_ext_irec_t *erp; /* indirection array pointer */
  3991. int i; /* loop counter */
  3992. int nlists; /* number of irec's (ex lists) */
  3993. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  3994. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  3995. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  3996. if (erp->er_extbuf) {
  3997. xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
  3998. -erp->er_extcount);
  3999. kmem_free(erp->er_extbuf);
  4000. }
  4001. /* Compact extent records */
  4002. erp = ifp->if_u1.if_ext_irec;
  4003. for (i = erp_idx; i < nlists - 1; i++) {
  4004. memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
  4005. }
  4006. /*
  4007. * Manually free the last extent record from the indirection
  4008. * array. A call to xfs_iext_realloc_indirect() with a size
  4009. * of zero would result in a call to xfs_iext_destroy() which
  4010. * would in turn call this function again, creating a nasty
  4011. * infinite loop.
  4012. */
  4013. if (--nlists) {
  4014. xfs_iext_realloc_indirect(ifp,
  4015. nlists * sizeof(xfs_ext_irec_t));
  4016. } else {
  4017. kmem_free(ifp->if_u1.if_ext_irec);
  4018. }
  4019. ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
  4020. }
  4021. /*
  4022. * This is called to clean up large amounts of unused memory allocated
  4023. * by the indirection array. Before compacting anything though, verify
  4024. * that the indirection array is still needed and switch back to the
  4025. * linear extent list (or even the inline buffer) if possible. The
  4026. * compaction policy is as follows:
  4027. *
  4028. * Full Compaction: Extents fit into a single page (or inline buffer)
  4029. * Partial Compaction: Extents occupy less than 50% of allocated space
  4030. * No Compaction: Extents occupy at least 50% of allocated space
  4031. */
  4032. void
  4033. xfs_iext_irec_compact(
  4034. xfs_ifork_t *ifp) /* inode fork pointer */
  4035. {
  4036. xfs_extnum_t nextents; /* number of extents in file */
  4037. int nlists; /* number of irec's (ex lists) */
  4038. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  4039. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  4040. nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
  4041. if (nextents == 0) {
  4042. xfs_iext_destroy(ifp);
  4043. } else if (nextents <= XFS_INLINE_EXTS) {
  4044. xfs_iext_indirect_to_direct(ifp);
  4045. xfs_iext_direct_to_inline(ifp, nextents);
  4046. } else if (nextents <= XFS_LINEAR_EXTS) {
  4047. xfs_iext_indirect_to_direct(ifp);
  4048. } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
  4049. xfs_iext_irec_compact_pages(ifp);
  4050. }
  4051. }
  4052. /*
  4053. * Combine extents from neighboring extent pages.
  4054. */
  4055. void
  4056. xfs_iext_irec_compact_pages(
  4057. xfs_ifork_t *ifp) /* inode fork pointer */
  4058. {
  4059. xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
  4060. int erp_idx = 0; /* indirection array index */
  4061. int nlists; /* number of irec's (ex lists) */
  4062. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  4063. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  4064. while (erp_idx < nlists - 1) {
  4065. erp = &ifp->if_u1.if_ext_irec[erp_idx];
  4066. erp_next = erp + 1;
  4067. if (erp_next->er_extcount <=
  4068. (XFS_LINEAR_EXTS - erp->er_extcount)) {
  4069. memcpy(&erp->er_extbuf[erp->er_extcount],
  4070. erp_next->er_extbuf, erp_next->er_extcount *
  4071. sizeof(xfs_bmbt_rec_t));
  4072. erp->er_extcount += erp_next->er_extcount;
  4073. /*
  4074. * Free page before removing extent record
  4075. * so er_extoffs don't get modified in
  4076. * xfs_iext_irec_remove.
  4077. */
  4078. kmem_free(erp_next->er_extbuf);
  4079. erp_next->er_extbuf = NULL;
  4080. xfs_iext_irec_remove(ifp, erp_idx + 1);
  4081. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  4082. } else {
  4083. erp_idx++;
  4084. }
  4085. }
  4086. }
  4087. /*
  4088. * This is called to update the er_extoff field in the indirection
  4089. * array when extents have been added or removed from one of the
  4090. * extent lists. erp_idx contains the irec index to begin updating
  4091. * at and ext_diff contains the number of extents that were added
  4092. * or removed.
  4093. */
  4094. void
  4095. xfs_iext_irec_update_extoffs(
  4096. xfs_ifork_t *ifp, /* inode fork pointer */
  4097. int erp_idx, /* irec index to update */
  4098. int ext_diff) /* number of new extents */
  4099. {
  4100. int i; /* loop counter */
  4101. int nlists; /* number of irec's (ex lists */
  4102. ASSERT(ifp->if_flags & XFS_IFEXTIREC);
  4103. nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
  4104. for (i = erp_idx; i < nlists; i++) {
  4105. ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
  4106. }
  4107. }