xfs_inode.c 134 KB

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