xfs_inode.c 137 KB

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