xfs_inode.c 130 KB

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