xfs_inode.c 129 KB

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