xfs_inode.c 131 KB

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