xfs_inode.c 132 KB

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