xfs_vnodeops.c 115 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_types.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_inum.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_da_btree.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_alloc_btree.h"
  33. #include "xfs_ialloc_btree.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_attr_sf.h"
  36. #include "xfs_dinode.h"
  37. #include "xfs_inode.h"
  38. #include "xfs_inode_item.h"
  39. #include "xfs_itable.h"
  40. #include "xfs_btree.h"
  41. #include "xfs_ialloc.h"
  42. #include "xfs_alloc.h"
  43. #include "xfs_bmap.h"
  44. #include "xfs_attr.h"
  45. #include "xfs_rw.h"
  46. #include "xfs_error.h"
  47. #include "xfs_quota.h"
  48. #include "xfs_utils.h"
  49. #include "xfs_rtalloc.h"
  50. #include "xfs_refcache.h"
  51. #include "xfs_trans_space.h"
  52. #include "xfs_log_priv.h"
  53. STATIC int
  54. xfs_open(
  55. bhv_desc_t *bdp,
  56. cred_t *credp)
  57. {
  58. int mode;
  59. bhv_vnode_t *vp = BHV_TO_VNODE(bdp);
  60. xfs_inode_t *ip = XFS_BHVTOI(bdp);
  61. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  62. return XFS_ERROR(EIO);
  63. /*
  64. * If it's a directory with any blocks, read-ahead block 0
  65. * as we're almost certain to have the next operation be a read there.
  66. */
  67. if (VN_ISDIR(vp) && ip->i_d.di_nextents > 0) {
  68. mode = xfs_ilock_map_shared(ip);
  69. if (ip->i_d.di_nextents > 0)
  70. (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
  71. xfs_iunlock(ip, mode);
  72. }
  73. return 0;
  74. }
  75. STATIC int
  76. xfs_close(
  77. bhv_desc_t *bdp,
  78. int flags,
  79. lastclose_t lastclose,
  80. cred_t *credp)
  81. {
  82. bhv_vnode_t *vp = BHV_TO_VNODE(bdp);
  83. xfs_inode_t *ip = XFS_BHVTOI(bdp);
  84. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  85. return XFS_ERROR(EIO);
  86. if (lastclose != L_TRUE || !VN_ISREG(vp))
  87. return 0;
  88. /*
  89. * If we previously truncated this file and removed old data in
  90. * the process, we want to initiate "early" writeout on the last
  91. * close. This is an attempt to combat the notorious NULL files
  92. * problem which is particularly noticable from a truncate down,
  93. * buffered (re-)write (delalloc), followed by a crash. What we
  94. * are effectively doing here is significantly reducing the time
  95. * window where we'd otherwise be exposed to that problem.
  96. */
  97. if (VUNTRUNCATE(vp) && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
  98. return bhv_vop_flush_pages(vp, 0, -1, XFS_B_ASYNC, FI_NONE);
  99. return 0;
  100. }
  101. /*
  102. * xfs_getattr
  103. */
  104. STATIC int
  105. xfs_getattr(
  106. bhv_desc_t *bdp,
  107. bhv_vattr_t *vap,
  108. int flags,
  109. cred_t *credp)
  110. {
  111. xfs_inode_t *ip;
  112. xfs_mount_t *mp;
  113. bhv_vnode_t *vp;
  114. vp = BHV_TO_VNODE(bdp);
  115. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  116. ip = XFS_BHVTOI(bdp);
  117. mp = ip->i_mount;
  118. if (XFS_FORCED_SHUTDOWN(mp))
  119. return XFS_ERROR(EIO);
  120. if (!(flags & ATTR_LAZY))
  121. xfs_ilock(ip, XFS_ILOCK_SHARED);
  122. vap->va_size = ip->i_d.di_size;
  123. if (vap->va_mask == XFS_AT_SIZE)
  124. goto all_done;
  125. vap->va_nblocks =
  126. XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
  127. vap->va_nodeid = ip->i_ino;
  128. #if XFS_BIG_INUMS
  129. vap->va_nodeid += mp->m_inoadd;
  130. #endif
  131. vap->va_nlink = ip->i_d.di_nlink;
  132. /*
  133. * Quick exit for non-stat callers
  134. */
  135. if ((vap->va_mask &
  136. ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
  137. XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
  138. goto all_done;
  139. /*
  140. * Copy from in-core inode.
  141. */
  142. vap->va_mode = ip->i_d.di_mode;
  143. vap->va_uid = ip->i_d.di_uid;
  144. vap->va_gid = ip->i_d.di_gid;
  145. vap->va_projid = ip->i_d.di_projid;
  146. /*
  147. * Check vnode type block/char vs. everything else.
  148. */
  149. switch (ip->i_d.di_mode & S_IFMT) {
  150. case S_IFBLK:
  151. case S_IFCHR:
  152. vap->va_rdev = ip->i_df.if_u2.if_rdev;
  153. vap->va_blocksize = BLKDEV_IOSIZE;
  154. break;
  155. default:
  156. vap->va_rdev = 0;
  157. if (!(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
  158. vap->va_blocksize = xfs_preferred_iosize(mp);
  159. } else {
  160. /*
  161. * If the file blocks are being allocated from a
  162. * realtime partition, then return the inode's
  163. * realtime extent size or the realtime volume's
  164. * extent size.
  165. */
  166. vap->va_blocksize = ip->i_d.di_extsize ?
  167. (ip->i_d.di_extsize << mp->m_sb.sb_blocklog) :
  168. (mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog);
  169. }
  170. break;
  171. }
  172. vn_atime_to_timespec(vp, &vap->va_atime);
  173. vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  174. vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  175. vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  176. vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  177. /*
  178. * Exit for stat callers. See if any of the rest of the fields
  179. * to be filled in are needed.
  180. */
  181. if ((vap->va_mask &
  182. (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
  183. XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
  184. goto all_done;
  185. /*
  186. * Convert di_flags to xflags.
  187. */
  188. vap->va_xflags = xfs_ip2xflags(ip);
  189. /*
  190. * Exit for inode revalidate. See if any of the rest of
  191. * the fields to be filled in are needed.
  192. */
  193. if ((vap->va_mask &
  194. (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
  195. XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
  196. goto all_done;
  197. vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
  198. vap->va_nextents =
  199. (ip->i_df.if_flags & XFS_IFEXTENTS) ?
  200. ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
  201. ip->i_d.di_nextents;
  202. if (ip->i_afp)
  203. vap->va_anextents =
  204. (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
  205. ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
  206. ip->i_d.di_anextents;
  207. else
  208. vap->va_anextents = 0;
  209. vap->va_gen = ip->i_d.di_gen;
  210. all_done:
  211. if (!(flags & ATTR_LAZY))
  212. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  213. return 0;
  214. }
  215. /*
  216. * xfs_setattr
  217. */
  218. int
  219. xfs_setattr(
  220. bhv_desc_t *bdp,
  221. bhv_vattr_t *vap,
  222. int flags,
  223. cred_t *credp)
  224. {
  225. xfs_inode_t *ip;
  226. xfs_trans_t *tp;
  227. xfs_mount_t *mp;
  228. int mask;
  229. int code;
  230. uint lock_flags;
  231. uint commit_flags=0;
  232. uid_t uid=0, iuid=0;
  233. gid_t gid=0, igid=0;
  234. int timeflags = 0;
  235. bhv_vnode_t *vp;
  236. xfs_prid_t projid=0, iprojid=0;
  237. int mandlock_before, mandlock_after;
  238. struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
  239. int file_owner;
  240. int need_iolock = 1;
  241. vp = BHV_TO_VNODE(bdp);
  242. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  243. if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
  244. return XFS_ERROR(EROFS);
  245. /*
  246. * Cannot set certain attributes.
  247. */
  248. mask = vap->va_mask;
  249. if (mask & XFS_AT_NOSET) {
  250. return XFS_ERROR(EINVAL);
  251. }
  252. ip = XFS_BHVTOI(bdp);
  253. mp = ip->i_mount;
  254. if (XFS_FORCED_SHUTDOWN(mp))
  255. return XFS_ERROR(EIO);
  256. /*
  257. * Timestamps do not need to be logged and hence do not
  258. * need to be done within a transaction.
  259. */
  260. if (mask & XFS_AT_UPDTIMES) {
  261. ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
  262. timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
  263. ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
  264. ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
  265. xfs_ichgtime(ip, timeflags);
  266. return 0;
  267. }
  268. olddquot1 = olddquot2 = NULL;
  269. udqp = gdqp = NULL;
  270. /*
  271. * If disk quotas is on, we make sure that the dquots do exist on disk,
  272. * before we start any other transactions. Trying to do this later
  273. * is messy. We don't care to take a readlock to look at the ids
  274. * in inode here, because we can't hold it across the trans_reserve.
  275. * If the IDs do change before we take the ilock, we're covered
  276. * because the i_*dquot fields will get updated anyway.
  277. */
  278. if (XFS_IS_QUOTA_ON(mp) &&
  279. (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
  280. uint qflags = 0;
  281. if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
  282. uid = vap->va_uid;
  283. qflags |= XFS_QMOPT_UQUOTA;
  284. } else {
  285. uid = ip->i_d.di_uid;
  286. }
  287. if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
  288. gid = vap->va_gid;
  289. qflags |= XFS_QMOPT_GQUOTA;
  290. } else {
  291. gid = ip->i_d.di_gid;
  292. }
  293. if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
  294. projid = vap->va_projid;
  295. qflags |= XFS_QMOPT_PQUOTA;
  296. } else {
  297. projid = ip->i_d.di_projid;
  298. }
  299. /*
  300. * We take a reference when we initialize udqp and gdqp,
  301. * so it is important that we never blindly double trip on
  302. * the same variable. See xfs_create() for an example.
  303. */
  304. ASSERT(udqp == NULL);
  305. ASSERT(gdqp == NULL);
  306. code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
  307. &udqp, &gdqp);
  308. if (code)
  309. return code;
  310. }
  311. /*
  312. * For the other attributes, we acquire the inode lock and
  313. * first do an error checking pass.
  314. */
  315. tp = NULL;
  316. lock_flags = XFS_ILOCK_EXCL;
  317. if (flags & ATTR_NOLOCK)
  318. need_iolock = 0;
  319. if (!(mask & XFS_AT_SIZE)) {
  320. if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
  321. (mp->m_flags & XFS_MOUNT_WSYNC)) {
  322. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  323. commit_flags = 0;
  324. if ((code = xfs_trans_reserve(tp, 0,
  325. XFS_ICHANGE_LOG_RES(mp), 0,
  326. 0, 0))) {
  327. lock_flags = 0;
  328. goto error_return;
  329. }
  330. }
  331. } else {
  332. if (DM_EVENT_ENABLED (vp->v_vfsp, ip, DM_EVENT_TRUNCATE) &&
  333. !(flags & ATTR_DMI)) {
  334. int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
  335. code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
  336. vap->va_size, 0, dmflags, NULL);
  337. if (code) {
  338. lock_flags = 0;
  339. goto error_return;
  340. }
  341. }
  342. if (need_iolock)
  343. lock_flags |= XFS_IOLOCK_EXCL;
  344. }
  345. xfs_ilock(ip, lock_flags);
  346. /* boolean: are we the file owner? */
  347. file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
  348. /*
  349. * Change various properties of a file.
  350. * Only the owner or users with CAP_FOWNER
  351. * capability may do these things.
  352. */
  353. if (mask &
  354. (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
  355. XFS_AT_GID|XFS_AT_PROJID)) {
  356. /*
  357. * CAP_FOWNER overrides the following restrictions:
  358. *
  359. * The user ID of the calling process must be equal
  360. * to the file owner ID, except in cases where the
  361. * CAP_FSETID capability is applicable.
  362. */
  363. if (!file_owner && !capable(CAP_FOWNER)) {
  364. code = XFS_ERROR(EPERM);
  365. goto error_return;
  366. }
  367. /*
  368. * CAP_FSETID overrides the following restrictions:
  369. *
  370. * The effective user ID of the calling process shall match
  371. * the file owner when setting the set-user-ID and
  372. * set-group-ID bits on that file.
  373. *
  374. * The effective group ID or one of the supplementary group
  375. * IDs of the calling process shall match the group owner of
  376. * the file when setting the set-group-ID bit on that file
  377. */
  378. if (mask & XFS_AT_MODE) {
  379. mode_t m = 0;
  380. if ((vap->va_mode & S_ISUID) && !file_owner)
  381. m |= S_ISUID;
  382. if ((vap->va_mode & S_ISGID) &&
  383. !in_group_p((gid_t)ip->i_d.di_gid))
  384. m |= S_ISGID;
  385. #if 0
  386. /* Linux allows this, Irix doesn't. */
  387. if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
  388. m |= S_ISVTX;
  389. #endif
  390. if (m && !capable(CAP_FSETID))
  391. vap->va_mode &= ~m;
  392. }
  393. }
  394. /*
  395. * Change file ownership. Must be the owner or privileged.
  396. * If the system was configured with the "restricted_chown"
  397. * option, the owner is not permitted to give away the file,
  398. * and can change the group id only to a group of which he
  399. * or she is a member.
  400. */
  401. if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
  402. /*
  403. * These IDs could have changed since we last looked at them.
  404. * But, we're assured that if the ownership did change
  405. * while we didn't have the inode locked, inode's dquot(s)
  406. * would have changed also.
  407. */
  408. iuid = ip->i_d.di_uid;
  409. iprojid = ip->i_d.di_projid;
  410. igid = ip->i_d.di_gid;
  411. gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
  412. uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
  413. projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
  414. iprojid;
  415. /*
  416. * CAP_CHOWN overrides the following restrictions:
  417. *
  418. * If _POSIX_CHOWN_RESTRICTED is defined, this capability
  419. * shall override the restriction that a process cannot
  420. * change the user ID of a file it owns and the restriction
  421. * that the group ID supplied to the chown() function
  422. * shall be equal to either the group ID or one of the
  423. * supplementary group IDs of the calling process.
  424. */
  425. if (restricted_chown &&
  426. (iuid != uid || (igid != gid &&
  427. !in_group_p((gid_t)gid))) &&
  428. !capable(CAP_CHOWN)) {
  429. code = XFS_ERROR(EPERM);
  430. goto error_return;
  431. }
  432. /*
  433. * Do a quota reservation only if uid/projid/gid is actually
  434. * going to change.
  435. */
  436. if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
  437. (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
  438. (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
  439. ASSERT(tp);
  440. code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
  441. capable(CAP_FOWNER) ?
  442. XFS_QMOPT_FORCE_RES : 0);
  443. if (code) /* out of quota */
  444. goto error_return;
  445. }
  446. }
  447. /*
  448. * Truncate file. Must have write permission and not be a directory.
  449. */
  450. if (mask & XFS_AT_SIZE) {
  451. /* Short circuit the truncate case for zero length files */
  452. if ((vap->va_size == 0) &&
  453. (ip->i_d.di_size == 0) && (ip->i_d.di_nextents == 0)) {
  454. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  455. lock_flags &= ~XFS_ILOCK_EXCL;
  456. if (mask & XFS_AT_CTIME)
  457. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  458. code = 0;
  459. goto error_return;
  460. }
  461. if (VN_ISDIR(vp)) {
  462. code = XFS_ERROR(EISDIR);
  463. goto error_return;
  464. } else if (!VN_ISREG(vp)) {
  465. code = XFS_ERROR(EINVAL);
  466. goto error_return;
  467. }
  468. /*
  469. * Make sure that the dquots are attached to the inode.
  470. */
  471. if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
  472. goto error_return;
  473. }
  474. /*
  475. * Change file access or modified times.
  476. */
  477. if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
  478. if (!file_owner) {
  479. if ((flags & ATTR_UTIME) &&
  480. !capable(CAP_FOWNER)) {
  481. code = XFS_ERROR(EPERM);
  482. goto error_return;
  483. }
  484. }
  485. }
  486. /*
  487. * Change extent size or realtime flag.
  488. */
  489. if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
  490. /*
  491. * Can't change extent size if any extents are allocated.
  492. */
  493. if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
  494. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
  495. vap->va_extsize) ) {
  496. code = XFS_ERROR(EINVAL); /* EFBIG? */
  497. goto error_return;
  498. }
  499. /*
  500. * Can't change realtime flag if any extents are allocated.
  501. */
  502. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  503. (mask & XFS_AT_XFLAGS) &&
  504. (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) !=
  505. (vap->va_xflags & XFS_XFLAG_REALTIME)) {
  506. code = XFS_ERROR(EINVAL); /* EFBIG? */
  507. goto error_return;
  508. }
  509. /*
  510. * Extent size must be a multiple of the appropriate block
  511. * size, if set at all.
  512. */
  513. if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
  514. xfs_extlen_t size;
  515. if ((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
  516. ((mask & XFS_AT_XFLAGS) &&
  517. (vap->va_xflags & XFS_XFLAG_REALTIME))) {
  518. size = mp->m_sb.sb_rextsize <<
  519. mp->m_sb.sb_blocklog;
  520. } else {
  521. size = mp->m_sb.sb_blocksize;
  522. }
  523. if (vap->va_extsize % size) {
  524. code = XFS_ERROR(EINVAL);
  525. goto error_return;
  526. }
  527. }
  528. /*
  529. * If realtime flag is set then must have realtime data.
  530. */
  531. if ((mask & XFS_AT_XFLAGS) &&
  532. (vap->va_xflags & XFS_XFLAG_REALTIME)) {
  533. if ((mp->m_sb.sb_rblocks == 0) ||
  534. (mp->m_sb.sb_rextsize == 0) ||
  535. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
  536. code = XFS_ERROR(EINVAL);
  537. goto error_return;
  538. }
  539. }
  540. /*
  541. * Can't modify an immutable/append-only file unless
  542. * we have appropriate permission.
  543. */
  544. if ((mask & XFS_AT_XFLAGS) &&
  545. (ip->i_d.di_flags &
  546. (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
  547. (vap->va_xflags &
  548. (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
  549. !capable(CAP_LINUX_IMMUTABLE)) {
  550. code = XFS_ERROR(EPERM);
  551. goto error_return;
  552. }
  553. }
  554. /*
  555. * Now we can make the changes. Before we join the inode
  556. * to the transaction, if XFS_AT_SIZE is set then take care of
  557. * the part of the truncation that must be done without the
  558. * inode lock. This needs to be done before joining the inode
  559. * to the transaction, because the inode cannot be unlocked
  560. * once it is a part of the transaction.
  561. */
  562. if (mask & XFS_AT_SIZE) {
  563. code = 0;
  564. if ((vap->va_size > ip->i_d.di_size) &&
  565. (flags & ATTR_NOSIZETOK) == 0) {
  566. code = xfs_igrow_start(ip, vap->va_size, credp);
  567. }
  568. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  569. vn_iowait(vp); /* wait for the completion of any pending DIOs */
  570. if (!code)
  571. code = xfs_itruncate_data(ip, vap->va_size);
  572. if (code) {
  573. ASSERT(tp == NULL);
  574. lock_flags &= ~XFS_ILOCK_EXCL;
  575. ASSERT(lock_flags == XFS_IOLOCK_EXCL);
  576. goto error_return;
  577. }
  578. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
  579. if ((code = xfs_trans_reserve(tp, 0,
  580. XFS_ITRUNCATE_LOG_RES(mp), 0,
  581. XFS_TRANS_PERM_LOG_RES,
  582. XFS_ITRUNCATE_LOG_COUNT))) {
  583. xfs_trans_cancel(tp, 0);
  584. if (need_iolock)
  585. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  586. return code;
  587. }
  588. commit_flags = XFS_TRANS_RELEASE_LOG_RES;
  589. xfs_ilock(ip, XFS_ILOCK_EXCL);
  590. }
  591. if (tp) {
  592. xfs_trans_ijoin(tp, ip, lock_flags);
  593. xfs_trans_ihold(tp, ip);
  594. }
  595. /* determine whether mandatory locking mode changes */
  596. mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
  597. /*
  598. * Truncate file. Must have write permission and not be a directory.
  599. */
  600. if (mask & XFS_AT_SIZE) {
  601. if (vap->va_size > ip->i_d.di_size) {
  602. xfs_igrow_finish(tp, ip, vap->va_size,
  603. !(flags & ATTR_DMI));
  604. } else if ((vap->va_size <= ip->i_d.di_size) ||
  605. ((vap->va_size == 0) && ip->i_d.di_nextents)) {
  606. /*
  607. * signal a sync transaction unless
  608. * we're truncating an already unlinked
  609. * file on a wsync filesystem
  610. */
  611. code = xfs_itruncate_finish(&tp, ip,
  612. (xfs_fsize_t)vap->va_size,
  613. XFS_DATA_FORK,
  614. ((ip->i_d.di_nlink != 0 ||
  615. !(mp->m_flags & XFS_MOUNT_WSYNC))
  616. ? 1 : 0));
  617. if (code)
  618. goto abort_return;
  619. /*
  620. * Truncated "down", so we're removing references
  621. * to old data here - if we now delay flushing for
  622. * a long time, we expose ourselves unduly to the
  623. * notorious NULL files problem. So, we mark this
  624. * vnode and flush it when the file is closed, and
  625. * do not wait the usual (long) time for writeout.
  626. */
  627. VTRUNCATE(vp);
  628. }
  629. /*
  630. * Have to do this even if the file's size doesn't change.
  631. */
  632. timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  633. }
  634. /*
  635. * Change file access modes.
  636. */
  637. if (mask & XFS_AT_MODE) {
  638. ip->i_d.di_mode &= S_IFMT;
  639. ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
  640. xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
  641. timeflags |= XFS_ICHGTIME_CHG;
  642. }
  643. /*
  644. * Change file ownership. Must be the owner or privileged.
  645. * If the system was configured with the "restricted_chown"
  646. * option, the owner is not permitted to give away the file,
  647. * and can change the group id only to a group of which he
  648. * or she is a member.
  649. */
  650. if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
  651. /*
  652. * CAP_FSETID overrides the following restrictions:
  653. *
  654. * The set-user-ID and set-group-ID bits of a file will be
  655. * cleared upon successful return from chown()
  656. */
  657. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  658. !capable(CAP_FSETID)) {
  659. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  660. }
  661. /*
  662. * Change the ownerships and register quota modifications
  663. * in the transaction.
  664. */
  665. if (iuid != uid) {
  666. if (XFS_IS_UQUOTA_ON(mp)) {
  667. ASSERT(mask & XFS_AT_UID);
  668. ASSERT(udqp);
  669. olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
  670. &ip->i_udquot, udqp);
  671. }
  672. ip->i_d.di_uid = uid;
  673. }
  674. if (igid != gid) {
  675. if (XFS_IS_GQUOTA_ON(mp)) {
  676. ASSERT(!XFS_IS_PQUOTA_ON(mp));
  677. ASSERT(mask & XFS_AT_GID);
  678. ASSERT(gdqp);
  679. olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
  680. &ip->i_gdquot, gdqp);
  681. }
  682. ip->i_d.di_gid = gid;
  683. }
  684. if (iprojid != projid) {
  685. if (XFS_IS_PQUOTA_ON(mp)) {
  686. ASSERT(!XFS_IS_GQUOTA_ON(mp));
  687. ASSERT(mask & XFS_AT_PROJID);
  688. ASSERT(gdqp);
  689. olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
  690. &ip->i_gdquot, gdqp);
  691. }
  692. ip->i_d.di_projid = projid;
  693. /*
  694. * We may have to rev the inode as well as
  695. * the superblock version number since projids didn't
  696. * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
  697. */
  698. if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
  699. xfs_bump_ino_vers2(tp, ip);
  700. }
  701. xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
  702. timeflags |= XFS_ICHGTIME_CHG;
  703. }
  704. /*
  705. * Change file access or modified times.
  706. */
  707. if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
  708. if (mask & XFS_AT_ATIME) {
  709. ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
  710. ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
  711. ip->i_update_core = 1;
  712. timeflags &= ~XFS_ICHGTIME_ACC;
  713. }
  714. if (mask & XFS_AT_MTIME) {
  715. ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
  716. ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
  717. timeflags &= ~XFS_ICHGTIME_MOD;
  718. timeflags |= XFS_ICHGTIME_CHG;
  719. }
  720. if (tp && (flags & ATTR_UTIME))
  721. xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
  722. }
  723. /*
  724. * Change XFS-added attributes.
  725. */
  726. if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
  727. if (mask & XFS_AT_EXTSIZE) {
  728. /*
  729. * Converting bytes to fs blocks.
  730. */
  731. ip->i_d.di_extsize = vap->va_extsize >>
  732. mp->m_sb.sb_blocklog;
  733. }
  734. if (mask & XFS_AT_XFLAGS) {
  735. uint di_flags;
  736. /* can't set PREALLOC this way, just preserve it */
  737. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  738. if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
  739. di_flags |= XFS_DIFLAG_IMMUTABLE;
  740. if (vap->va_xflags & XFS_XFLAG_APPEND)
  741. di_flags |= XFS_DIFLAG_APPEND;
  742. if (vap->va_xflags & XFS_XFLAG_SYNC)
  743. di_flags |= XFS_DIFLAG_SYNC;
  744. if (vap->va_xflags & XFS_XFLAG_NOATIME)
  745. di_flags |= XFS_DIFLAG_NOATIME;
  746. if (vap->va_xflags & XFS_XFLAG_NODUMP)
  747. di_flags |= XFS_DIFLAG_NODUMP;
  748. if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
  749. di_flags |= XFS_DIFLAG_PROJINHERIT;
  750. if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
  751. di_flags |= XFS_DIFLAG_NODEFRAG;
  752. if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
  753. if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
  754. di_flags |= XFS_DIFLAG_RTINHERIT;
  755. if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
  756. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  757. if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
  758. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  759. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
  760. if (vap->va_xflags & XFS_XFLAG_REALTIME) {
  761. di_flags |= XFS_DIFLAG_REALTIME;
  762. ip->i_iocore.io_flags |= XFS_IOCORE_RT;
  763. } else {
  764. ip->i_iocore.io_flags &= ~XFS_IOCORE_RT;
  765. }
  766. if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
  767. di_flags |= XFS_DIFLAG_EXTSIZE;
  768. }
  769. ip->i_d.di_flags = di_flags;
  770. }
  771. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  772. timeflags |= XFS_ICHGTIME_CHG;
  773. }
  774. /*
  775. * Change file inode change time only if XFS_AT_CTIME set
  776. * AND we have been called by a DMI function.
  777. */
  778. if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
  779. ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
  780. ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
  781. ip->i_update_core = 1;
  782. timeflags &= ~XFS_ICHGTIME_CHG;
  783. }
  784. /*
  785. * Send out timestamp changes that need to be set to the
  786. * current time. Not done when called by a DMI function.
  787. */
  788. if (timeflags && !(flags & ATTR_DMI))
  789. xfs_ichgtime(ip, timeflags);
  790. XFS_STATS_INC(xs_ig_attrchg);
  791. /*
  792. * If this is a synchronous mount, make sure that the
  793. * transaction goes to disk before returning to the user.
  794. * This is slightly sub-optimal in that truncates require
  795. * two sync transactions instead of one for wsync filesystems.
  796. * One for the truncate and one for the timestamps since we
  797. * don't want to change the timestamps unless we're sure the
  798. * truncate worked. Truncates are less than 1% of the laddis
  799. * mix so this probably isn't worth the trouble to optimize.
  800. */
  801. code = 0;
  802. if (tp) {
  803. if (mp->m_flags & XFS_MOUNT_WSYNC)
  804. xfs_trans_set_sync(tp);
  805. code = xfs_trans_commit(tp, commit_flags);
  806. }
  807. /*
  808. * If the (regular) file's mandatory locking mode changed, then
  809. * notify the vnode. We do this under the inode lock to prevent
  810. * racing calls to vop_vnode_change.
  811. */
  812. mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
  813. if (mandlock_before != mandlock_after) {
  814. bhv_vop_vnode_change(vp, VCHANGE_FLAGS_ENF_LOCKING,
  815. mandlock_after);
  816. }
  817. xfs_iunlock(ip, lock_flags);
  818. /*
  819. * Release any dquot(s) the inode had kept before chown.
  820. */
  821. XFS_QM_DQRELE(mp, olddquot1);
  822. XFS_QM_DQRELE(mp, olddquot2);
  823. XFS_QM_DQRELE(mp, udqp);
  824. XFS_QM_DQRELE(mp, gdqp);
  825. if (code) {
  826. return code;
  827. }
  828. if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_ATTRIBUTE) &&
  829. !(flags & ATTR_DMI)) {
  830. (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
  831. NULL, DM_RIGHT_NULL, NULL, NULL,
  832. 0, 0, AT_DELAY_FLAG(flags));
  833. }
  834. return 0;
  835. abort_return:
  836. commit_flags |= XFS_TRANS_ABORT;
  837. /* FALLTHROUGH */
  838. error_return:
  839. XFS_QM_DQRELE(mp, udqp);
  840. XFS_QM_DQRELE(mp, gdqp);
  841. if (tp) {
  842. xfs_trans_cancel(tp, commit_flags);
  843. }
  844. if (lock_flags != 0) {
  845. xfs_iunlock(ip, lock_flags);
  846. }
  847. return code;
  848. }
  849. /*
  850. * xfs_access
  851. * Null conversion from vnode mode bits to inode mode bits, as in efs.
  852. */
  853. STATIC int
  854. xfs_access(
  855. bhv_desc_t *bdp,
  856. int mode,
  857. cred_t *credp)
  858. {
  859. xfs_inode_t *ip;
  860. int error;
  861. vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
  862. (inst_t *)__return_address);
  863. ip = XFS_BHVTOI(bdp);
  864. xfs_ilock(ip, XFS_ILOCK_SHARED);
  865. error = xfs_iaccess(ip, mode, credp);
  866. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  867. return error;
  868. }
  869. /*
  870. * The maximum pathlen is 1024 bytes. Since the minimum file system
  871. * blocksize is 512 bytes, we can get a max of 2 extents back from
  872. * bmapi.
  873. */
  874. #define SYMLINK_MAPS 2
  875. /*
  876. * xfs_readlink
  877. *
  878. */
  879. STATIC int
  880. xfs_readlink(
  881. bhv_desc_t *bdp,
  882. uio_t *uiop,
  883. int ioflags,
  884. cred_t *credp)
  885. {
  886. xfs_inode_t *ip;
  887. int count;
  888. xfs_off_t offset;
  889. int pathlen;
  890. bhv_vnode_t *vp;
  891. int error = 0;
  892. xfs_mount_t *mp;
  893. int nmaps;
  894. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  895. xfs_daddr_t d;
  896. int byte_cnt;
  897. int n;
  898. xfs_buf_t *bp;
  899. vp = BHV_TO_VNODE(bdp);
  900. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  901. ip = XFS_BHVTOI(bdp);
  902. mp = ip->i_mount;
  903. if (XFS_FORCED_SHUTDOWN(mp))
  904. return XFS_ERROR(EIO);
  905. xfs_ilock(ip, XFS_ILOCK_SHARED);
  906. ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
  907. offset = uiop->uio_offset;
  908. count = uiop->uio_resid;
  909. if (offset < 0) {
  910. error = XFS_ERROR(EINVAL);
  911. goto error_return;
  912. }
  913. if (count <= 0) {
  914. error = 0;
  915. goto error_return;
  916. }
  917. /*
  918. * See if the symlink is stored inline.
  919. */
  920. pathlen = (int)ip->i_d.di_size;
  921. if (ip->i_df.if_flags & XFS_IFINLINE) {
  922. error = xfs_uio_read(ip->i_df.if_u1.if_data, pathlen, uiop);
  923. }
  924. else {
  925. /*
  926. * Symlink not inline. Call bmap to get it in.
  927. */
  928. nmaps = SYMLINK_MAPS;
  929. error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen),
  930. 0, NULL, 0, mval, &nmaps, NULL, NULL);
  931. if (error) {
  932. goto error_return;
  933. }
  934. for (n = 0; n < nmaps; n++) {
  935. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  936. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  937. bp = xfs_buf_read(mp->m_ddev_targp, d,
  938. BTOBB(byte_cnt), 0);
  939. error = XFS_BUF_GETERROR(bp);
  940. if (error) {
  941. xfs_ioerror_alert("xfs_readlink",
  942. ip->i_mount, bp, XFS_BUF_ADDR(bp));
  943. xfs_buf_relse(bp);
  944. goto error_return;
  945. }
  946. if (pathlen < byte_cnt)
  947. byte_cnt = pathlen;
  948. pathlen -= byte_cnt;
  949. error = xfs_uio_read(XFS_BUF_PTR(bp), byte_cnt, uiop);
  950. xfs_buf_relse (bp);
  951. }
  952. }
  953. error_return:
  954. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  955. return error;
  956. }
  957. /*
  958. * xfs_fsync
  959. *
  960. * This is called to sync the inode and its data out to disk.
  961. * We need to hold the I/O lock while flushing the data, and
  962. * the inode lock while flushing the inode. The inode lock CANNOT
  963. * be held while flushing the data, so acquire after we're done
  964. * with that.
  965. */
  966. STATIC int
  967. xfs_fsync(
  968. bhv_desc_t *bdp,
  969. int flag,
  970. cred_t *credp,
  971. xfs_off_t start,
  972. xfs_off_t stop)
  973. {
  974. xfs_inode_t *ip;
  975. xfs_trans_t *tp;
  976. int error;
  977. int log_flushed = 0, changed = 1;
  978. vn_trace_entry(BHV_TO_VNODE(bdp),
  979. __FUNCTION__, (inst_t *)__return_address);
  980. ip = XFS_BHVTOI(bdp);
  981. ASSERT(start >= 0 && stop >= -1);
  982. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  983. return XFS_ERROR(EIO);
  984. /*
  985. * We always need to make sure that the required inode state
  986. * is safe on disk. The vnode might be clean but because
  987. * of committed transactions that haven't hit the disk yet.
  988. * Likewise, there could be unflushed non-transactional
  989. * changes to the inode core that have to go to disk.
  990. *
  991. * The following code depends on one assumption: that
  992. * any transaction that changes an inode logs the core
  993. * because it has to change some field in the inode core
  994. * (typically nextents or nblocks). That assumption
  995. * implies that any transactions against an inode will
  996. * catch any non-transactional updates. If inode-altering
  997. * transactions exist that violate this assumption, the
  998. * code breaks. Right now, it figures that if the involved
  999. * update_* field is clear and the inode is unpinned, the
  1000. * inode is clean. Either it's been flushed or it's been
  1001. * committed and the commit has hit the disk unpinning the inode.
  1002. * (Note that xfs_inode_item_format() called at commit clears
  1003. * the update_* fields.)
  1004. */
  1005. xfs_ilock(ip, XFS_ILOCK_SHARED);
  1006. /* If we are flushing data then we care about update_size
  1007. * being set, otherwise we care about update_core
  1008. */
  1009. if ((flag & FSYNC_DATA) ?
  1010. (ip->i_update_size == 0) :
  1011. (ip->i_update_core == 0)) {
  1012. /*
  1013. * Timestamps/size haven't changed since last inode
  1014. * flush or inode transaction commit. That means
  1015. * either nothing got written or a transaction
  1016. * committed which caught the updates. If the
  1017. * latter happened and the transaction hasn't
  1018. * hit the disk yet, the inode will be still
  1019. * be pinned. If it is, force the log.
  1020. */
  1021. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  1022. if (xfs_ipincount(ip)) {
  1023. _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
  1024. XFS_LOG_FORCE |
  1025. ((flag & FSYNC_WAIT)
  1026. ? XFS_LOG_SYNC : 0),
  1027. &log_flushed);
  1028. } else {
  1029. /*
  1030. * If the inode is not pinned and nothing
  1031. * has changed we don't need to flush the
  1032. * cache.
  1033. */
  1034. changed = 0;
  1035. }
  1036. error = 0;
  1037. } else {
  1038. /*
  1039. * Kick off a transaction to log the inode
  1040. * core to get the updates. Make it
  1041. * sync if FSYNC_WAIT is passed in (which
  1042. * is done by everybody but specfs). The
  1043. * sync transaction will also force the log.
  1044. */
  1045. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  1046. tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
  1047. if ((error = xfs_trans_reserve(tp, 0,
  1048. XFS_FSYNC_TS_LOG_RES(ip->i_mount),
  1049. 0, 0, 0))) {
  1050. xfs_trans_cancel(tp, 0);
  1051. return error;
  1052. }
  1053. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1054. /*
  1055. * Note - it's possible that we might have pushed
  1056. * ourselves out of the way during trans_reserve
  1057. * which would flush the inode. But there's no
  1058. * guarantee that the inode buffer has actually
  1059. * gone out yet (it's delwri). Plus the buffer
  1060. * could be pinned anyway if it's part of an
  1061. * inode in another recent transaction. So we
  1062. * play it safe and fire off the transaction anyway.
  1063. */
  1064. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1065. xfs_trans_ihold(tp, ip);
  1066. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1067. if (flag & FSYNC_WAIT)
  1068. xfs_trans_set_sync(tp);
  1069. error = _xfs_trans_commit(tp, 0, &log_flushed);
  1070. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1071. }
  1072. if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
  1073. /*
  1074. * If the log write didn't issue an ordered tag we need
  1075. * to flush the disk cache for the data device now.
  1076. */
  1077. if (!log_flushed)
  1078. xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
  1079. /*
  1080. * If this inode is on the RT dev we need to flush that
  1081. * cache as well.
  1082. */
  1083. if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
  1084. xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
  1085. }
  1086. return error;
  1087. }
  1088. /*
  1089. * This is called by xfs_inactive to free any blocks beyond eof,
  1090. * when the link count isn't zero.
  1091. */
  1092. STATIC int
  1093. xfs_inactive_free_eofblocks(
  1094. xfs_mount_t *mp,
  1095. xfs_inode_t *ip)
  1096. {
  1097. xfs_trans_t *tp;
  1098. int error;
  1099. xfs_fileoff_t end_fsb;
  1100. xfs_fileoff_t last_fsb;
  1101. xfs_filblks_t map_len;
  1102. int nimaps;
  1103. xfs_bmbt_irec_t imap;
  1104. /*
  1105. * Figure out if there are any blocks beyond the end
  1106. * of the file. If not, then there is nothing to do.
  1107. */
  1108. end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_d.di_size));
  1109. last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  1110. map_len = last_fsb - end_fsb;
  1111. if (map_len <= 0)
  1112. return 0;
  1113. nimaps = 1;
  1114. xfs_ilock(ip, XFS_ILOCK_SHARED);
  1115. error = XFS_BMAPI(mp, NULL, &ip->i_iocore, end_fsb, map_len, 0,
  1116. NULL, 0, &imap, &nimaps, NULL, NULL);
  1117. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  1118. if (!error && (nimaps != 0) &&
  1119. (imap.br_startblock != HOLESTARTBLOCK ||
  1120. ip->i_delayed_blks)) {
  1121. /*
  1122. * Attach the dquots to the inode up front.
  1123. */
  1124. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  1125. return error;
  1126. /*
  1127. * There are blocks after the end of file.
  1128. * Free them up now by truncating the file to
  1129. * its current size.
  1130. */
  1131. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1132. /*
  1133. * Do the xfs_itruncate_start() call before
  1134. * reserving any log space because
  1135. * itruncate_start will call into the buffer
  1136. * cache and we can't
  1137. * do that within a transaction.
  1138. */
  1139. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1140. error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
  1141. ip->i_d.di_size);
  1142. if (error) {
  1143. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1144. return error;
  1145. }
  1146. error = xfs_trans_reserve(tp, 0,
  1147. XFS_ITRUNCATE_LOG_RES(mp),
  1148. 0, XFS_TRANS_PERM_LOG_RES,
  1149. XFS_ITRUNCATE_LOG_COUNT);
  1150. if (error) {
  1151. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1152. xfs_trans_cancel(tp, 0);
  1153. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1154. return error;
  1155. }
  1156. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1157. xfs_trans_ijoin(tp, ip,
  1158. XFS_IOLOCK_EXCL |
  1159. XFS_ILOCK_EXCL);
  1160. xfs_trans_ihold(tp, ip);
  1161. error = xfs_itruncate_finish(&tp, ip,
  1162. ip->i_d.di_size,
  1163. XFS_DATA_FORK,
  1164. 0);
  1165. /*
  1166. * If we get an error at this point we
  1167. * simply don't bother truncating the file.
  1168. */
  1169. if (error) {
  1170. xfs_trans_cancel(tp,
  1171. (XFS_TRANS_RELEASE_LOG_RES |
  1172. XFS_TRANS_ABORT));
  1173. } else {
  1174. error = xfs_trans_commit(tp,
  1175. XFS_TRANS_RELEASE_LOG_RES);
  1176. }
  1177. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1178. }
  1179. return error;
  1180. }
  1181. /*
  1182. * Free a symlink that has blocks associated with it.
  1183. */
  1184. STATIC int
  1185. xfs_inactive_symlink_rmt(
  1186. xfs_inode_t *ip,
  1187. xfs_trans_t **tpp)
  1188. {
  1189. xfs_buf_t *bp;
  1190. int committed;
  1191. int done;
  1192. int error;
  1193. xfs_fsblock_t first_block;
  1194. xfs_bmap_free_t free_list;
  1195. int i;
  1196. xfs_mount_t *mp;
  1197. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  1198. int nmaps;
  1199. xfs_trans_t *ntp;
  1200. int size;
  1201. xfs_trans_t *tp;
  1202. tp = *tpp;
  1203. mp = ip->i_mount;
  1204. ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
  1205. /*
  1206. * We're freeing a symlink that has some
  1207. * blocks allocated to it. Free the
  1208. * blocks here. We know that we've got
  1209. * either 1 or 2 extents and that we can
  1210. * free them all in one bunmapi call.
  1211. */
  1212. ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
  1213. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  1214. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  1215. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1216. xfs_trans_cancel(tp, 0);
  1217. *tpp = NULL;
  1218. return error;
  1219. }
  1220. /*
  1221. * Lock the inode, fix the size, and join it to the transaction.
  1222. * Hold it so in the normal path, we still have it locked for
  1223. * the second transaction. In the error paths we need it
  1224. * held so the cancel won't rele it, see below.
  1225. */
  1226. xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1227. size = (int)ip->i_d.di_size;
  1228. ip->i_d.di_size = 0;
  1229. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1230. xfs_trans_ihold(tp, ip);
  1231. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1232. /*
  1233. * Find the block(s) so we can inval and unmap them.
  1234. */
  1235. done = 0;
  1236. XFS_BMAP_INIT(&free_list, &first_block);
  1237. nmaps = ARRAY_SIZE(mval);
  1238. if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
  1239. XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
  1240. &free_list, NULL)))
  1241. goto error0;
  1242. /*
  1243. * Invalidate the block(s).
  1244. */
  1245. for (i = 0; i < nmaps; i++) {
  1246. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  1247. XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
  1248. XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
  1249. xfs_trans_binval(tp, bp);
  1250. }
  1251. /*
  1252. * Unmap the dead block(s) to the free_list.
  1253. */
  1254. if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
  1255. &first_block, &free_list, NULL, &done)))
  1256. goto error1;
  1257. ASSERT(done);
  1258. /*
  1259. * Commit the first transaction. This logs the EFI and the inode.
  1260. */
  1261. if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
  1262. goto error1;
  1263. /*
  1264. * The transaction must have been committed, since there were
  1265. * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
  1266. * The new tp has the extent freeing and EFDs.
  1267. */
  1268. ASSERT(committed);
  1269. /*
  1270. * The first xact was committed, so add the inode to the new one.
  1271. * Mark it dirty so it will be logged and moved forward in the log as
  1272. * part of every commit.
  1273. */
  1274. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1275. xfs_trans_ihold(tp, ip);
  1276. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1277. /*
  1278. * Get a new, empty transaction to return to our caller.
  1279. */
  1280. ntp = xfs_trans_dup(tp);
  1281. /*
  1282. * Commit the transaction containing extent freeing and EFDs.
  1283. * If we get an error on the commit here or on the reserve below,
  1284. * we need to unlock the inode since the new transaction doesn't
  1285. * have the inode attached.
  1286. */
  1287. error = xfs_trans_commit(tp, 0);
  1288. tp = ntp;
  1289. if (error) {
  1290. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1291. goto error0;
  1292. }
  1293. /*
  1294. * Remove the memory for extent descriptions (just bookkeeping).
  1295. */
  1296. if (ip->i_df.if_bytes)
  1297. xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
  1298. ASSERT(ip->i_df.if_bytes == 0);
  1299. /*
  1300. * Put an itruncate log reservation in the new transaction
  1301. * for our caller.
  1302. */
  1303. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  1304. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  1305. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1306. goto error0;
  1307. }
  1308. /*
  1309. * Return with the inode locked but not joined to the transaction.
  1310. */
  1311. *tpp = tp;
  1312. return 0;
  1313. error1:
  1314. xfs_bmap_cancel(&free_list);
  1315. error0:
  1316. /*
  1317. * Have to come here with the inode locked and either
  1318. * (held and in the transaction) or (not in the transaction).
  1319. * If the inode isn't held then cancel would iput it, but
  1320. * that's wrong since this is inactive and the vnode ref
  1321. * count is 0 already.
  1322. * Cancel won't do anything to the inode if held, but it still
  1323. * needs to be locked until the cancel is done, if it was
  1324. * joined to the transaction.
  1325. */
  1326. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1327. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1328. *tpp = NULL;
  1329. return error;
  1330. }
  1331. STATIC int
  1332. xfs_inactive_symlink_local(
  1333. xfs_inode_t *ip,
  1334. xfs_trans_t **tpp)
  1335. {
  1336. int error;
  1337. ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
  1338. /*
  1339. * We're freeing a symlink which fit into
  1340. * the inode. Just free the memory used
  1341. * to hold the old symlink.
  1342. */
  1343. error = xfs_trans_reserve(*tpp, 0,
  1344. XFS_ITRUNCATE_LOG_RES(ip->i_mount),
  1345. 0, XFS_TRANS_PERM_LOG_RES,
  1346. XFS_ITRUNCATE_LOG_COUNT);
  1347. if (error) {
  1348. xfs_trans_cancel(*tpp, 0);
  1349. *tpp = NULL;
  1350. return error;
  1351. }
  1352. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1353. /*
  1354. * Zero length symlinks _can_ exist.
  1355. */
  1356. if (ip->i_df.if_bytes > 0) {
  1357. xfs_idata_realloc(ip,
  1358. -(ip->i_df.if_bytes),
  1359. XFS_DATA_FORK);
  1360. ASSERT(ip->i_df.if_bytes == 0);
  1361. }
  1362. return 0;
  1363. }
  1364. STATIC int
  1365. xfs_inactive_attrs(
  1366. xfs_inode_t *ip,
  1367. xfs_trans_t **tpp)
  1368. {
  1369. xfs_trans_t *tp;
  1370. int error;
  1371. xfs_mount_t *mp;
  1372. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
  1373. tp = *tpp;
  1374. mp = ip->i_mount;
  1375. ASSERT(ip->i_d.di_forkoff != 0);
  1376. xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1377. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1378. error = xfs_attr_inactive(ip);
  1379. if (error) {
  1380. *tpp = NULL;
  1381. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1382. return error; /* goto out */
  1383. }
  1384. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1385. error = xfs_trans_reserve(tp, 0,
  1386. XFS_IFREE_LOG_RES(mp),
  1387. 0, XFS_TRANS_PERM_LOG_RES,
  1388. XFS_INACTIVE_LOG_COUNT);
  1389. if (error) {
  1390. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1391. xfs_trans_cancel(tp, 0);
  1392. *tpp = NULL;
  1393. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1394. return error;
  1395. }
  1396. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1397. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1398. xfs_trans_ihold(tp, ip);
  1399. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1400. ASSERT(ip->i_d.di_anextents == 0);
  1401. *tpp = tp;
  1402. return 0;
  1403. }
  1404. STATIC int
  1405. xfs_release(
  1406. bhv_desc_t *bdp)
  1407. {
  1408. xfs_inode_t *ip;
  1409. bhv_vnode_t *vp;
  1410. xfs_mount_t *mp;
  1411. int error;
  1412. vp = BHV_TO_VNODE(bdp);
  1413. ip = XFS_BHVTOI(bdp);
  1414. mp = ip->i_mount;
  1415. if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
  1416. return 0;
  1417. /* If this is a read-only mount, don't do this (would generate I/O) */
  1418. if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
  1419. return 0;
  1420. #ifdef HAVE_REFCACHE
  1421. /* If we are in the NFS reference cache then don't do this now */
  1422. if (ip->i_refcache)
  1423. return 0;
  1424. #endif
  1425. if (ip->i_d.di_nlink != 0) {
  1426. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1427. ((ip->i_d.di_size > 0) || (VN_CACHED(vp) > 0 ||
  1428. ip->i_delayed_blks > 0)) &&
  1429. (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
  1430. (!(ip->i_d.di_flags &
  1431. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
  1432. if ((error = xfs_inactive_free_eofblocks(mp, ip)))
  1433. return error;
  1434. /* Update linux inode block count after free above */
  1435. vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
  1436. ip->i_d.di_nblocks + ip->i_delayed_blks);
  1437. }
  1438. }
  1439. return 0;
  1440. }
  1441. /*
  1442. * xfs_inactive
  1443. *
  1444. * This is called when the vnode reference count for the vnode
  1445. * goes to zero. If the file has been unlinked, then it must
  1446. * now be truncated. Also, we clear all of the read-ahead state
  1447. * kept for the inode here since the file is now closed.
  1448. */
  1449. STATIC int
  1450. xfs_inactive(
  1451. bhv_desc_t *bdp,
  1452. cred_t *credp)
  1453. {
  1454. xfs_inode_t *ip;
  1455. bhv_vnode_t *vp;
  1456. xfs_bmap_free_t free_list;
  1457. xfs_fsblock_t first_block;
  1458. int committed;
  1459. xfs_trans_t *tp;
  1460. xfs_mount_t *mp;
  1461. int error;
  1462. int truncate;
  1463. vp = BHV_TO_VNODE(bdp);
  1464. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  1465. ip = XFS_BHVTOI(bdp);
  1466. /*
  1467. * If the inode is already free, then there can be nothing
  1468. * to clean up here.
  1469. */
  1470. if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
  1471. ASSERT(ip->i_df.if_real_bytes == 0);
  1472. ASSERT(ip->i_df.if_broot_bytes == 0);
  1473. return VN_INACTIVE_CACHE;
  1474. }
  1475. /*
  1476. * Only do a truncate if it's a regular file with
  1477. * some actual space in it. It's OK to look at the
  1478. * inode's fields without the lock because we're the
  1479. * only one with a reference to the inode.
  1480. */
  1481. truncate = ((ip->i_d.di_nlink == 0) &&
  1482. ((ip->i_d.di_size != 0) || (ip->i_d.di_nextents > 0) ||
  1483. (ip->i_delayed_blks > 0)) &&
  1484. ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
  1485. mp = ip->i_mount;
  1486. if (ip->i_d.di_nlink == 0 &&
  1487. DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_DESTROY)) {
  1488. (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
  1489. }
  1490. error = 0;
  1491. /* If this is a read-only mount, don't do this (would generate I/O) */
  1492. if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
  1493. goto out;
  1494. if (ip->i_d.di_nlink != 0) {
  1495. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1496. ((ip->i_d.di_size > 0) || (VN_CACHED(vp) > 0 ||
  1497. ip->i_delayed_blks > 0)) &&
  1498. (ip->i_df.if_flags & XFS_IFEXTENTS) &&
  1499. (!(ip->i_d.di_flags &
  1500. (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
  1501. (ip->i_delayed_blks != 0)))) {
  1502. if ((error = xfs_inactive_free_eofblocks(mp, ip)))
  1503. return VN_INACTIVE_CACHE;
  1504. /* Update linux inode block count after free above */
  1505. vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
  1506. ip->i_d.di_nblocks + ip->i_delayed_blks);
  1507. }
  1508. goto out;
  1509. }
  1510. ASSERT(ip->i_d.di_nlink == 0);
  1511. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  1512. return VN_INACTIVE_CACHE;
  1513. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1514. if (truncate) {
  1515. /*
  1516. * Do the xfs_itruncate_start() call before
  1517. * reserving any log space because itruncate_start
  1518. * will call into the buffer cache and we can't
  1519. * do that within a transaction.
  1520. */
  1521. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1522. error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
  1523. if (error) {
  1524. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1525. return VN_INACTIVE_CACHE;
  1526. }
  1527. error = xfs_trans_reserve(tp, 0,
  1528. XFS_ITRUNCATE_LOG_RES(mp),
  1529. 0, XFS_TRANS_PERM_LOG_RES,
  1530. XFS_ITRUNCATE_LOG_COUNT);
  1531. if (error) {
  1532. /* Don't call itruncate_cleanup */
  1533. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1534. xfs_trans_cancel(tp, 0);
  1535. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1536. return VN_INACTIVE_CACHE;
  1537. }
  1538. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1539. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1540. xfs_trans_ihold(tp, ip);
  1541. /*
  1542. * normally, we have to run xfs_itruncate_finish sync.
  1543. * But if filesystem is wsync and we're in the inactive
  1544. * path, then we know that nlink == 0, and that the
  1545. * xaction that made nlink == 0 is permanently committed
  1546. * since xfs_remove runs as a synchronous transaction.
  1547. */
  1548. error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
  1549. (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
  1550. if (error) {
  1551. xfs_trans_cancel(tp,
  1552. XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1553. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1554. return VN_INACTIVE_CACHE;
  1555. }
  1556. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
  1557. /*
  1558. * If we get an error while cleaning up a
  1559. * symlink we bail out.
  1560. */
  1561. error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
  1562. xfs_inactive_symlink_rmt(ip, &tp) :
  1563. xfs_inactive_symlink_local(ip, &tp);
  1564. if (error) {
  1565. ASSERT(tp == NULL);
  1566. return VN_INACTIVE_CACHE;
  1567. }
  1568. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1569. xfs_trans_ihold(tp, ip);
  1570. } else {
  1571. error = xfs_trans_reserve(tp, 0,
  1572. XFS_IFREE_LOG_RES(mp),
  1573. 0, XFS_TRANS_PERM_LOG_RES,
  1574. XFS_INACTIVE_LOG_COUNT);
  1575. if (error) {
  1576. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1577. xfs_trans_cancel(tp, 0);
  1578. return VN_INACTIVE_CACHE;
  1579. }
  1580. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1581. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1582. xfs_trans_ihold(tp, ip);
  1583. }
  1584. /*
  1585. * If there are attributes associated with the file
  1586. * then blow them away now. The code calls a routine
  1587. * that recursively deconstructs the attribute fork.
  1588. * We need to just commit the current transaction
  1589. * because we can't use it for xfs_attr_inactive().
  1590. */
  1591. if (ip->i_d.di_anextents > 0) {
  1592. error = xfs_inactive_attrs(ip, &tp);
  1593. /*
  1594. * If we got an error, the transaction is already
  1595. * cancelled, and the inode is unlocked. Just get out.
  1596. */
  1597. if (error)
  1598. return VN_INACTIVE_CACHE;
  1599. } else if (ip->i_afp) {
  1600. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1601. }
  1602. /*
  1603. * Free the inode.
  1604. */
  1605. XFS_BMAP_INIT(&free_list, &first_block);
  1606. error = xfs_ifree(tp, ip, &free_list);
  1607. if (error) {
  1608. /*
  1609. * If we fail to free the inode, shut down. The cancel
  1610. * might do that, we need to make sure. Otherwise the
  1611. * inode might be lost for a long time or forever.
  1612. */
  1613. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1614. cmn_err(CE_NOTE,
  1615. "xfs_inactive: xfs_ifree() returned an error = %d on %s",
  1616. error, mp->m_fsname);
  1617. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  1618. }
  1619. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  1620. } else {
  1621. /*
  1622. * Credit the quota account(s). The inode is gone.
  1623. */
  1624. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1625. /*
  1626. * Just ignore errors at this point. There is
  1627. * nothing we can do except to try to keep going.
  1628. */
  1629. (void) xfs_bmap_finish(&tp, &free_list, &committed);
  1630. (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1631. }
  1632. /*
  1633. * Release the dquots held by inode, if any.
  1634. */
  1635. XFS_QM_DQDETACH(mp, ip);
  1636. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1637. out:
  1638. return VN_INACTIVE_CACHE;
  1639. }
  1640. /*
  1641. * xfs_lookup
  1642. */
  1643. STATIC int
  1644. xfs_lookup(
  1645. bhv_desc_t *dir_bdp,
  1646. bhv_vname_t *dentry,
  1647. bhv_vnode_t **vpp,
  1648. int flags,
  1649. bhv_vnode_t *rdir,
  1650. cred_t *credp)
  1651. {
  1652. xfs_inode_t *dp, *ip;
  1653. xfs_ino_t e_inum;
  1654. int error;
  1655. uint lock_mode;
  1656. bhv_vnode_t *dir_vp;
  1657. dir_vp = BHV_TO_VNODE(dir_bdp);
  1658. vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
  1659. dp = XFS_BHVTOI(dir_bdp);
  1660. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  1661. return XFS_ERROR(EIO);
  1662. lock_mode = xfs_ilock_map_shared(dp);
  1663. error = xfs_dir_lookup_int(dir_bdp, lock_mode, dentry, &e_inum, &ip);
  1664. if (!error) {
  1665. *vpp = XFS_ITOV(ip);
  1666. ITRACE(ip);
  1667. }
  1668. xfs_iunlock_map_shared(dp, lock_mode);
  1669. return error;
  1670. }
  1671. /*
  1672. * xfs_create (create a new file).
  1673. */
  1674. STATIC int
  1675. xfs_create(
  1676. bhv_desc_t *dir_bdp,
  1677. bhv_vname_t *dentry,
  1678. bhv_vattr_t *vap,
  1679. bhv_vnode_t **vpp,
  1680. cred_t *credp)
  1681. {
  1682. char *name = VNAME(dentry);
  1683. bhv_vnode_t *dir_vp;
  1684. xfs_inode_t *dp, *ip;
  1685. bhv_vnode_t *vp = NULL;
  1686. xfs_trans_t *tp;
  1687. xfs_mount_t *mp;
  1688. xfs_dev_t rdev;
  1689. int error;
  1690. xfs_bmap_free_t free_list;
  1691. xfs_fsblock_t first_block;
  1692. boolean_t dp_joined_to_trans;
  1693. int dm_event_sent = 0;
  1694. uint cancel_flags;
  1695. int committed;
  1696. xfs_prid_t prid;
  1697. struct xfs_dquot *udqp, *gdqp;
  1698. uint resblks;
  1699. int dm_di_mode;
  1700. int namelen;
  1701. ASSERT(!*vpp);
  1702. dir_vp = BHV_TO_VNODE(dir_bdp);
  1703. vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
  1704. dp = XFS_BHVTOI(dir_bdp);
  1705. mp = dp->i_mount;
  1706. dm_di_mode = vap->va_mode;
  1707. namelen = VNAMELEN(dentry);
  1708. if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
  1709. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  1710. dir_vp, DM_RIGHT_NULL, NULL,
  1711. DM_RIGHT_NULL, name, NULL,
  1712. dm_di_mode, 0, 0);
  1713. if (error)
  1714. return error;
  1715. dm_event_sent = 1;
  1716. }
  1717. if (XFS_FORCED_SHUTDOWN(mp))
  1718. return XFS_ERROR(EIO);
  1719. /* Return through std_return after this point. */
  1720. udqp = gdqp = NULL;
  1721. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1722. prid = dp->i_d.di_projid;
  1723. else if (vap->va_mask & XFS_AT_PROJID)
  1724. prid = (xfs_prid_t)vap->va_projid;
  1725. else
  1726. prid = (xfs_prid_t)dfltprid;
  1727. /*
  1728. * Make sure that we have allocated dquot(s) on disk.
  1729. */
  1730. error = XFS_QM_DQVOPALLOC(mp, dp,
  1731. current_fsuid(credp), current_fsgid(credp), prid,
  1732. XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
  1733. if (error)
  1734. goto std_return;
  1735. ip = NULL;
  1736. dp_joined_to_trans = B_FALSE;
  1737. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  1738. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1739. resblks = XFS_CREATE_SPACE_RES(mp, namelen);
  1740. /*
  1741. * Initially assume that the file does not exist and
  1742. * reserve the resources for that case. If that is not
  1743. * the case we'll drop the one we have and get a more
  1744. * appropriate transaction later.
  1745. */
  1746. error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
  1747. XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
  1748. if (error == ENOSPC) {
  1749. resblks = 0;
  1750. error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
  1751. XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
  1752. }
  1753. if (error) {
  1754. cancel_flags = 0;
  1755. dp = NULL;
  1756. goto error_return;
  1757. }
  1758. xfs_ilock(dp, XFS_ILOCK_EXCL);
  1759. XFS_BMAP_INIT(&free_list, &first_block);
  1760. ASSERT(ip == NULL);
  1761. /*
  1762. * Reserve disk quota and the inode.
  1763. */
  1764. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  1765. if (error)
  1766. goto error_return;
  1767. if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
  1768. goto error_return;
  1769. rdev = (vap->va_mask & XFS_AT_RDEV) ? vap->va_rdev : 0;
  1770. error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 1,
  1771. rdev, credp, prid, resblks > 0,
  1772. &ip, &committed);
  1773. if (error) {
  1774. if (error == ENOSPC)
  1775. goto error_return;
  1776. goto abort_return;
  1777. }
  1778. ITRACE(ip);
  1779. /*
  1780. * At this point, we've gotten a newly allocated inode.
  1781. * It is locked (and joined to the transaction).
  1782. */
  1783. ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
  1784. /*
  1785. * Now we join the directory inode to the transaction.
  1786. * We do not do it earlier because xfs_dir_ialloc
  1787. * might commit the previous transaction (and release
  1788. * all the locks).
  1789. */
  1790. VN_HOLD(dir_vp);
  1791. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  1792. dp_joined_to_trans = B_TRUE;
  1793. error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
  1794. &first_block, &free_list, resblks ?
  1795. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  1796. if (error) {
  1797. ASSERT(error != ENOSPC);
  1798. goto abort_return;
  1799. }
  1800. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1801. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  1802. /*
  1803. * If this is a synchronous mount, make sure that the
  1804. * create transaction goes to disk before returning to
  1805. * the user.
  1806. */
  1807. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  1808. xfs_trans_set_sync(tp);
  1809. }
  1810. dp->i_gen++;
  1811. /*
  1812. * Attach the dquot(s) to the inodes and modify them incore.
  1813. * These ids of the inode couldn't have changed since the new
  1814. * inode has been locked ever since it was created.
  1815. */
  1816. XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
  1817. /*
  1818. * xfs_trans_commit normally decrements the vnode ref count
  1819. * when it unlocks the inode. Since we want to return the
  1820. * vnode to the caller, we bump the vnode ref count now.
  1821. */
  1822. IHOLD(ip);
  1823. vp = XFS_ITOV(ip);
  1824. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1825. if (error) {
  1826. xfs_bmap_cancel(&free_list);
  1827. goto abort_rele;
  1828. }
  1829. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1830. if (error) {
  1831. IRELE(ip);
  1832. tp = NULL;
  1833. goto error_return;
  1834. }
  1835. XFS_QM_DQRELE(mp, udqp);
  1836. XFS_QM_DQRELE(mp, gdqp);
  1837. /*
  1838. * Propagate the fact that the vnode changed after the
  1839. * xfs_inode locks have been released.
  1840. */
  1841. bhv_vop_vnode_change(vp, VCHANGE_FLAGS_TRUNCATED, 3);
  1842. *vpp = vp;
  1843. /* Fallthrough to std_return with error = 0 */
  1844. std_return:
  1845. if ( (*vpp || (error != 0 && dm_event_sent != 0)) &&
  1846. DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
  1847. DM_EVENT_POSTCREATE)) {
  1848. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
  1849. dir_vp, DM_RIGHT_NULL,
  1850. *vpp ? vp:NULL,
  1851. DM_RIGHT_NULL, name, NULL,
  1852. dm_di_mode, error, 0);
  1853. }
  1854. return error;
  1855. abort_return:
  1856. cancel_flags |= XFS_TRANS_ABORT;
  1857. /* FALLTHROUGH */
  1858. error_return:
  1859. if (tp != NULL)
  1860. xfs_trans_cancel(tp, cancel_flags);
  1861. if (!dp_joined_to_trans && (dp != NULL))
  1862. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1863. XFS_QM_DQRELE(mp, udqp);
  1864. XFS_QM_DQRELE(mp, gdqp);
  1865. goto std_return;
  1866. abort_rele:
  1867. /*
  1868. * Wait until after the current transaction is aborted to
  1869. * release the inode. This prevents recursive transactions
  1870. * and deadlocks from xfs_inactive.
  1871. */
  1872. cancel_flags |= XFS_TRANS_ABORT;
  1873. xfs_trans_cancel(tp, cancel_flags);
  1874. IRELE(ip);
  1875. XFS_QM_DQRELE(mp, udqp);
  1876. XFS_QM_DQRELE(mp, gdqp);
  1877. goto std_return;
  1878. }
  1879. #ifdef DEBUG
  1880. /*
  1881. * Some counters to see if (and how often) we are hitting some deadlock
  1882. * prevention code paths.
  1883. */
  1884. int xfs_rm_locks;
  1885. int xfs_rm_lock_delays;
  1886. int xfs_rm_attempts;
  1887. #endif
  1888. /*
  1889. * The following routine will lock the inodes associated with the
  1890. * directory and the named entry in the directory. The locks are
  1891. * acquired in increasing inode number.
  1892. *
  1893. * If the entry is "..", then only the directory is locked. The
  1894. * vnode ref count will still include that from the .. entry in
  1895. * this case.
  1896. *
  1897. * There is a deadlock we need to worry about. If the locked directory is
  1898. * in the AIL, it might be blocking up the log. The next inode we lock
  1899. * could be already locked by another thread waiting for log space (e.g
  1900. * a permanent log reservation with a long running transaction (see
  1901. * xfs_itruncate_finish)). To solve this, we must check if the directory
  1902. * is in the ail and use lock_nowait. If we can't lock, we need to
  1903. * drop the inode lock on the directory and try again. xfs_iunlock will
  1904. * potentially push the tail if we were holding up the log.
  1905. */
  1906. STATIC int
  1907. xfs_lock_dir_and_entry(
  1908. xfs_inode_t *dp,
  1909. xfs_inode_t *ip) /* inode of entry 'name' */
  1910. {
  1911. int attempts;
  1912. xfs_ino_t e_inum;
  1913. xfs_inode_t *ips[2];
  1914. xfs_log_item_t *lp;
  1915. #ifdef DEBUG
  1916. xfs_rm_locks++;
  1917. #endif
  1918. attempts = 0;
  1919. again:
  1920. xfs_ilock(dp, XFS_ILOCK_EXCL);
  1921. e_inum = ip->i_ino;
  1922. ITRACE(ip);
  1923. /*
  1924. * We want to lock in increasing inum. Since we've already
  1925. * acquired the lock on the directory, we may need to release
  1926. * if if the inum of the entry turns out to be less.
  1927. */
  1928. if (e_inum > dp->i_ino) {
  1929. /*
  1930. * We are already in the right order, so just
  1931. * lock on the inode of the entry.
  1932. * We need to use nowait if dp is in the AIL.
  1933. */
  1934. lp = (xfs_log_item_t *)dp->i_itemp;
  1935. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  1936. if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
  1937. attempts++;
  1938. #ifdef DEBUG
  1939. xfs_rm_attempts++;
  1940. #endif
  1941. /*
  1942. * Unlock dp and try again.
  1943. * xfs_iunlock will try to push the tail
  1944. * if the inode is in the AIL.
  1945. */
  1946. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1947. if ((attempts % 5) == 0) {
  1948. delay(1); /* Don't just spin the CPU */
  1949. #ifdef DEBUG
  1950. xfs_rm_lock_delays++;
  1951. #endif
  1952. }
  1953. goto again;
  1954. }
  1955. } else {
  1956. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1957. }
  1958. } else if (e_inum < dp->i_ino) {
  1959. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  1960. ips[0] = ip;
  1961. ips[1] = dp;
  1962. xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
  1963. }
  1964. /* else e_inum == dp->i_ino */
  1965. /* This can happen if we're asked to lock /x/..
  1966. * the entry is "..", which is also the parent directory.
  1967. */
  1968. return 0;
  1969. }
  1970. #ifdef DEBUG
  1971. int xfs_locked_n;
  1972. int xfs_small_retries;
  1973. int xfs_middle_retries;
  1974. int xfs_lots_retries;
  1975. int xfs_lock_delays;
  1976. #endif
  1977. /*
  1978. * The following routine will lock n inodes in exclusive mode.
  1979. * We assume the caller calls us with the inodes in i_ino order.
  1980. *
  1981. * We need to detect deadlock where an inode that we lock
  1982. * is in the AIL and we start waiting for another inode that is locked
  1983. * by a thread in a long running transaction (such as truncate). This can
  1984. * result in deadlock since the long running trans might need to wait
  1985. * for the inode we just locked in order to push the tail and free space
  1986. * in the log.
  1987. */
  1988. void
  1989. xfs_lock_inodes(
  1990. xfs_inode_t **ips,
  1991. int inodes,
  1992. int first_locked,
  1993. uint lock_mode)
  1994. {
  1995. int attempts = 0, i, j, try_lock;
  1996. xfs_log_item_t *lp;
  1997. ASSERT(ips && (inodes >= 2)); /* we need at least two */
  1998. if (first_locked) {
  1999. try_lock = 1;
  2000. i = 1;
  2001. } else {
  2002. try_lock = 0;
  2003. i = 0;
  2004. }
  2005. again:
  2006. for (; i < inodes; i++) {
  2007. ASSERT(ips[i]);
  2008. if (i && (ips[i] == ips[i-1])) /* Already locked */
  2009. continue;
  2010. /*
  2011. * If try_lock is not set yet, make sure all locked inodes
  2012. * are not in the AIL.
  2013. * If any are, set try_lock to be used later.
  2014. */
  2015. if (!try_lock) {
  2016. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  2017. lp = (xfs_log_item_t *)ips[j]->i_itemp;
  2018. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  2019. try_lock++;
  2020. }
  2021. }
  2022. }
  2023. /*
  2024. * If any of the previous locks we have locked is in the AIL,
  2025. * we must TRY to get the second and subsequent locks. If
  2026. * we can't get any, we must release all we have
  2027. * and try again.
  2028. */
  2029. if (try_lock) {
  2030. /* try_lock must be 0 if i is 0. */
  2031. /*
  2032. * try_lock means we have an inode locked
  2033. * that is in the AIL.
  2034. */
  2035. ASSERT(i != 0);
  2036. if (!xfs_ilock_nowait(ips[i], lock_mode)) {
  2037. attempts++;
  2038. /*
  2039. * Unlock all previous guys and try again.
  2040. * xfs_iunlock will try to push the tail
  2041. * if the inode is in the AIL.
  2042. */
  2043. for(j = i - 1; j >= 0; j--) {
  2044. /*
  2045. * Check to see if we've already
  2046. * unlocked this one.
  2047. * Not the first one going back,
  2048. * and the inode ptr is the same.
  2049. */
  2050. if ((j != (i - 1)) && ips[j] ==
  2051. ips[j+1])
  2052. continue;
  2053. xfs_iunlock(ips[j], lock_mode);
  2054. }
  2055. if ((attempts % 5) == 0) {
  2056. delay(1); /* Don't just spin the CPU */
  2057. #ifdef DEBUG
  2058. xfs_lock_delays++;
  2059. #endif
  2060. }
  2061. i = 0;
  2062. try_lock = 0;
  2063. goto again;
  2064. }
  2065. } else {
  2066. xfs_ilock(ips[i], lock_mode);
  2067. }
  2068. }
  2069. #ifdef DEBUG
  2070. if (attempts) {
  2071. if (attempts < 5) xfs_small_retries++;
  2072. else if (attempts < 100) xfs_middle_retries++;
  2073. else xfs_lots_retries++;
  2074. } else {
  2075. xfs_locked_n++;
  2076. }
  2077. #endif
  2078. }
  2079. #ifdef DEBUG
  2080. #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
  2081. int remove_which_error_return = 0;
  2082. #else /* ! DEBUG */
  2083. #define REMOVE_DEBUG_TRACE(x)
  2084. #endif /* ! DEBUG */
  2085. /*
  2086. * xfs_remove
  2087. *
  2088. */
  2089. STATIC int
  2090. xfs_remove(
  2091. bhv_desc_t *dir_bdp,
  2092. bhv_vname_t *dentry,
  2093. cred_t *credp)
  2094. {
  2095. bhv_vnode_t *dir_vp;
  2096. char *name = VNAME(dentry);
  2097. xfs_inode_t *dp, *ip;
  2098. xfs_trans_t *tp = NULL;
  2099. xfs_mount_t *mp;
  2100. int error = 0;
  2101. xfs_bmap_free_t free_list;
  2102. xfs_fsblock_t first_block;
  2103. int cancel_flags;
  2104. int committed;
  2105. int dm_di_mode = 0;
  2106. int link_zero;
  2107. uint resblks;
  2108. int namelen;
  2109. dir_vp = BHV_TO_VNODE(dir_bdp);
  2110. vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
  2111. dp = XFS_BHVTOI(dir_bdp);
  2112. mp = dp->i_mount;
  2113. if (XFS_FORCED_SHUTDOWN(mp))
  2114. return XFS_ERROR(EIO);
  2115. namelen = VNAMELEN(dentry);
  2116. if (!xfs_get_dir_entry(dentry, &ip)) {
  2117. dm_di_mode = ip->i_d.di_mode;
  2118. IRELE(ip);
  2119. }
  2120. if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
  2121. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
  2122. DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
  2123. name, NULL, dm_di_mode, 0, 0);
  2124. if (error)
  2125. return error;
  2126. }
  2127. /* From this point on, return through std_return */
  2128. ip = NULL;
  2129. /*
  2130. * We need to get a reference to ip before we get our log
  2131. * reservation. The reason for this is that we cannot call
  2132. * xfs_iget for an inode for which we do not have a reference
  2133. * once we've acquired a log reservation. This is because the
  2134. * inode we are trying to get might be in xfs_inactive going
  2135. * for a log reservation. Since we'll have to wait for the
  2136. * inactive code to complete before returning from xfs_iget,
  2137. * we need to make sure that we don't have log space reserved
  2138. * when we call xfs_iget. Instead we get an unlocked reference
  2139. * to the inode before getting our log reservation.
  2140. */
  2141. error = xfs_get_dir_entry(dentry, &ip);
  2142. if (error) {
  2143. REMOVE_DEBUG_TRACE(__LINE__);
  2144. goto std_return;
  2145. }
  2146. dm_di_mode = ip->i_d.di_mode;
  2147. vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
  2148. ITRACE(ip);
  2149. error = XFS_QM_DQATTACH(mp, dp, 0);
  2150. if (!error && dp != ip)
  2151. error = XFS_QM_DQATTACH(mp, ip, 0);
  2152. if (error) {
  2153. REMOVE_DEBUG_TRACE(__LINE__);
  2154. IRELE(ip);
  2155. goto std_return;
  2156. }
  2157. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  2158. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2159. /*
  2160. * We try to get the real space reservation first,
  2161. * allowing for directory btree deletion(s) implying
  2162. * possible bmap insert(s). If we can't get the space
  2163. * reservation then we use 0 instead, and avoid the bmap
  2164. * btree insert(s) in the directory code by, if the bmap
  2165. * insert tries to happen, instead trimming the LAST
  2166. * block from the directory.
  2167. */
  2168. resblks = XFS_REMOVE_SPACE_RES(mp);
  2169. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  2170. XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
  2171. if (error == ENOSPC) {
  2172. resblks = 0;
  2173. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  2174. XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
  2175. }
  2176. if (error) {
  2177. ASSERT(error != ENOSPC);
  2178. REMOVE_DEBUG_TRACE(__LINE__);
  2179. xfs_trans_cancel(tp, 0);
  2180. IRELE(ip);
  2181. return error;
  2182. }
  2183. error = xfs_lock_dir_and_entry(dp, ip);
  2184. if (error) {
  2185. REMOVE_DEBUG_TRACE(__LINE__);
  2186. xfs_trans_cancel(tp, cancel_flags);
  2187. IRELE(ip);
  2188. goto std_return;
  2189. }
  2190. /*
  2191. * At this point, we've gotten both the directory and the entry
  2192. * inodes locked.
  2193. */
  2194. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2195. if (dp != ip) {
  2196. /*
  2197. * Increment vnode ref count only in this case since
  2198. * there's an extra vnode reference in the case where
  2199. * dp == ip.
  2200. */
  2201. IHOLD(dp);
  2202. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  2203. }
  2204. /*
  2205. * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
  2206. */
  2207. XFS_BMAP_INIT(&free_list, &first_block);
  2208. error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
  2209. &first_block, &free_list, 0);
  2210. if (error) {
  2211. ASSERT(error != ENOENT);
  2212. REMOVE_DEBUG_TRACE(__LINE__);
  2213. goto error1;
  2214. }
  2215. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2216. dp->i_gen++;
  2217. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  2218. error = xfs_droplink(tp, ip);
  2219. if (error) {
  2220. REMOVE_DEBUG_TRACE(__LINE__);
  2221. goto error1;
  2222. }
  2223. /* Determine if this is the last link while
  2224. * we are in the transaction.
  2225. */
  2226. link_zero = (ip)->i_d.di_nlink==0;
  2227. /*
  2228. * Take an extra ref on the inode so that it doesn't
  2229. * go to xfs_inactive() from within the commit.
  2230. */
  2231. IHOLD(ip);
  2232. /*
  2233. * If this is a synchronous mount, make sure that the
  2234. * remove transaction goes to disk before returning to
  2235. * the user.
  2236. */
  2237. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2238. xfs_trans_set_sync(tp);
  2239. }
  2240. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2241. if (error) {
  2242. REMOVE_DEBUG_TRACE(__LINE__);
  2243. goto error_rele;
  2244. }
  2245. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2246. if (error) {
  2247. IRELE(ip);
  2248. goto std_return;
  2249. }
  2250. /*
  2251. * Before we drop our extra reference to the inode, purge it
  2252. * from the refcache if it is there. By waiting until afterwards
  2253. * to do the IRELE, we ensure that we won't go inactive in the
  2254. * xfs_refcache_purge_ip routine (although that would be OK).
  2255. */
  2256. xfs_refcache_purge_ip(ip);
  2257. vn_trace_exit(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
  2258. /*
  2259. * Let interposed file systems know about removed links.
  2260. */
  2261. bhv_vop_link_removed(XFS_ITOV(ip), dir_vp, link_zero);
  2262. IRELE(ip);
  2263. /* Fall through to std_return with error = 0 */
  2264. std_return:
  2265. if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp,
  2266. DM_EVENT_POSTREMOVE)) {
  2267. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
  2268. dir_vp, DM_RIGHT_NULL,
  2269. NULL, DM_RIGHT_NULL,
  2270. name, NULL, dm_di_mode, error, 0);
  2271. }
  2272. return error;
  2273. error1:
  2274. xfs_bmap_cancel(&free_list);
  2275. cancel_flags |= XFS_TRANS_ABORT;
  2276. xfs_trans_cancel(tp, cancel_flags);
  2277. goto std_return;
  2278. error_rele:
  2279. /*
  2280. * In this case make sure to not release the inode until after
  2281. * the current transaction is aborted. Releasing it beforehand
  2282. * can cause us to go to xfs_inactive and start a recursive
  2283. * transaction which can easily deadlock with the current one.
  2284. */
  2285. xfs_bmap_cancel(&free_list);
  2286. cancel_flags |= XFS_TRANS_ABORT;
  2287. xfs_trans_cancel(tp, cancel_flags);
  2288. /*
  2289. * Before we drop our extra reference to the inode, purge it
  2290. * from the refcache if it is there. By waiting until afterwards
  2291. * to do the IRELE, we ensure that we won't go inactive in the
  2292. * xfs_refcache_purge_ip routine (although that would be OK).
  2293. */
  2294. xfs_refcache_purge_ip(ip);
  2295. IRELE(ip);
  2296. goto std_return;
  2297. }
  2298. /*
  2299. * xfs_link
  2300. *
  2301. */
  2302. STATIC int
  2303. xfs_link(
  2304. bhv_desc_t *target_dir_bdp,
  2305. bhv_vnode_t *src_vp,
  2306. bhv_vname_t *dentry,
  2307. cred_t *credp)
  2308. {
  2309. xfs_inode_t *tdp, *sip;
  2310. xfs_trans_t *tp;
  2311. xfs_mount_t *mp;
  2312. xfs_inode_t *ips[2];
  2313. int error;
  2314. xfs_bmap_free_t free_list;
  2315. xfs_fsblock_t first_block;
  2316. int cancel_flags;
  2317. int committed;
  2318. bhv_vnode_t *target_dir_vp;
  2319. int resblks;
  2320. char *target_name = VNAME(dentry);
  2321. int target_namelen;
  2322. target_dir_vp = BHV_TO_VNODE(target_dir_bdp);
  2323. vn_trace_entry(target_dir_vp, __FUNCTION__, (inst_t *)__return_address);
  2324. vn_trace_entry(src_vp, __FUNCTION__, (inst_t *)__return_address);
  2325. target_namelen = VNAMELEN(dentry);
  2326. ASSERT(!VN_ISDIR(src_vp));
  2327. sip = xfs_vtoi(src_vp);
  2328. tdp = XFS_BHVTOI(target_dir_bdp);
  2329. mp = tdp->i_mount;
  2330. if (XFS_FORCED_SHUTDOWN(mp))
  2331. return XFS_ERROR(EIO);
  2332. if (DM_EVENT_ENABLED(src_vp->v_vfsp, tdp, DM_EVENT_LINK)) {
  2333. error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
  2334. target_dir_vp, DM_RIGHT_NULL,
  2335. src_vp, DM_RIGHT_NULL,
  2336. target_name, NULL, 0, 0, 0);
  2337. if (error)
  2338. return error;
  2339. }
  2340. /* Return through std_return after this point. */
  2341. error = XFS_QM_DQATTACH(mp, sip, 0);
  2342. if (!error && sip != tdp)
  2343. error = XFS_QM_DQATTACH(mp, tdp, 0);
  2344. if (error)
  2345. goto std_return;
  2346. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  2347. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2348. resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
  2349. error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
  2350. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  2351. if (error == ENOSPC) {
  2352. resblks = 0;
  2353. error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
  2354. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  2355. }
  2356. if (error) {
  2357. cancel_flags = 0;
  2358. goto error_return;
  2359. }
  2360. if (sip->i_ino < tdp->i_ino) {
  2361. ips[0] = sip;
  2362. ips[1] = tdp;
  2363. } else {
  2364. ips[0] = tdp;
  2365. ips[1] = sip;
  2366. }
  2367. xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
  2368. /*
  2369. * Increment vnode ref counts since xfs_trans_commit &
  2370. * xfs_trans_cancel will both unlock the inodes and
  2371. * decrement the associated ref counts.
  2372. */
  2373. VN_HOLD(src_vp);
  2374. VN_HOLD(target_dir_vp);
  2375. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  2376. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  2377. /*
  2378. * If the source has too many links, we can't make any more to it.
  2379. */
  2380. if (sip->i_d.di_nlink >= XFS_MAXLINK) {
  2381. error = XFS_ERROR(EMLINK);
  2382. goto error_return;
  2383. }
  2384. /*
  2385. * If we are using project inheritance, we only allow hard link
  2386. * creation in our tree when the project IDs are the same; else
  2387. * the tree quota mechanism could be circumvented.
  2388. */
  2389. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  2390. (tdp->i_d.di_projid != sip->i_d.di_projid))) {
  2391. error = XFS_ERROR(EXDEV);
  2392. goto error_return;
  2393. }
  2394. if (resblks == 0 &&
  2395. (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
  2396. goto error_return;
  2397. XFS_BMAP_INIT(&free_list, &first_block);
  2398. error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
  2399. sip->i_ino, &first_block, &free_list,
  2400. resblks);
  2401. if (error)
  2402. goto abort_return;
  2403. xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2404. tdp->i_gen++;
  2405. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  2406. error = xfs_bumplink(tp, sip);
  2407. if (error)
  2408. goto abort_return;
  2409. /*
  2410. * If this is a synchronous mount, make sure that the
  2411. * link transaction goes to disk before returning to
  2412. * the user.
  2413. */
  2414. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2415. xfs_trans_set_sync(tp);
  2416. }
  2417. error = xfs_bmap_finish (&tp, &free_list, &committed);
  2418. if (error) {
  2419. xfs_bmap_cancel(&free_list);
  2420. goto abort_return;
  2421. }
  2422. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2423. if (error)
  2424. goto std_return;
  2425. /* Fall through to std_return with error = 0. */
  2426. std_return:
  2427. if (DM_EVENT_ENABLED(src_vp->v_vfsp, sip,
  2428. DM_EVENT_POSTLINK)) {
  2429. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
  2430. target_dir_vp, DM_RIGHT_NULL,
  2431. src_vp, DM_RIGHT_NULL,
  2432. target_name, NULL, 0, error, 0);
  2433. }
  2434. return error;
  2435. abort_return:
  2436. cancel_flags |= XFS_TRANS_ABORT;
  2437. /* FALLTHROUGH */
  2438. error_return:
  2439. xfs_trans_cancel(tp, cancel_flags);
  2440. goto std_return;
  2441. }
  2442. /*
  2443. * xfs_mkdir
  2444. *
  2445. */
  2446. STATIC int
  2447. xfs_mkdir(
  2448. bhv_desc_t *dir_bdp,
  2449. bhv_vname_t *dentry,
  2450. bhv_vattr_t *vap,
  2451. bhv_vnode_t **vpp,
  2452. cred_t *credp)
  2453. {
  2454. char *dir_name = VNAME(dentry);
  2455. xfs_inode_t *dp;
  2456. xfs_inode_t *cdp; /* inode of created dir */
  2457. bhv_vnode_t *cvp; /* vnode of created dir */
  2458. xfs_trans_t *tp;
  2459. xfs_mount_t *mp;
  2460. int cancel_flags;
  2461. int error;
  2462. int committed;
  2463. xfs_bmap_free_t free_list;
  2464. xfs_fsblock_t first_block;
  2465. bhv_vnode_t *dir_vp;
  2466. boolean_t dp_joined_to_trans;
  2467. boolean_t created = B_FALSE;
  2468. int dm_event_sent = 0;
  2469. xfs_prid_t prid;
  2470. struct xfs_dquot *udqp, *gdqp;
  2471. uint resblks;
  2472. int dm_di_mode;
  2473. int dir_namelen;
  2474. dir_vp = BHV_TO_VNODE(dir_bdp);
  2475. dp = XFS_BHVTOI(dir_bdp);
  2476. mp = dp->i_mount;
  2477. if (XFS_FORCED_SHUTDOWN(mp))
  2478. return XFS_ERROR(EIO);
  2479. dir_namelen = VNAMELEN(dentry);
  2480. tp = NULL;
  2481. dp_joined_to_trans = B_FALSE;
  2482. dm_di_mode = vap->va_mode;
  2483. if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
  2484. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  2485. dir_vp, DM_RIGHT_NULL, NULL,
  2486. DM_RIGHT_NULL, dir_name, NULL,
  2487. dm_di_mode, 0, 0);
  2488. if (error)
  2489. return error;
  2490. dm_event_sent = 1;
  2491. }
  2492. /* Return through std_return after this point. */
  2493. vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
  2494. mp = dp->i_mount;
  2495. udqp = gdqp = NULL;
  2496. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  2497. prid = dp->i_d.di_projid;
  2498. else if (vap->va_mask & XFS_AT_PROJID)
  2499. prid = (xfs_prid_t)vap->va_projid;
  2500. else
  2501. prid = (xfs_prid_t)dfltprid;
  2502. /*
  2503. * Make sure that we have allocated dquot(s) on disk.
  2504. */
  2505. error = XFS_QM_DQVOPALLOC(mp, dp,
  2506. current_fsuid(credp), current_fsgid(credp), prid,
  2507. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  2508. if (error)
  2509. goto std_return;
  2510. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  2511. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2512. resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
  2513. error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
  2514. XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
  2515. if (error == ENOSPC) {
  2516. resblks = 0;
  2517. error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
  2518. XFS_TRANS_PERM_LOG_RES,
  2519. XFS_MKDIR_LOG_COUNT);
  2520. }
  2521. if (error) {
  2522. cancel_flags = 0;
  2523. dp = NULL;
  2524. goto error_return;
  2525. }
  2526. xfs_ilock(dp, XFS_ILOCK_EXCL);
  2527. /*
  2528. * Check for directory link count overflow.
  2529. */
  2530. if (dp->i_d.di_nlink >= XFS_MAXLINK) {
  2531. error = XFS_ERROR(EMLINK);
  2532. goto error_return;
  2533. }
  2534. /*
  2535. * Reserve disk quota and the inode.
  2536. */
  2537. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  2538. if (error)
  2539. goto error_return;
  2540. if (resblks == 0 &&
  2541. (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
  2542. goto error_return;
  2543. /*
  2544. * create the directory inode.
  2545. */
  2546. error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 2,
  2547. 0, credp, prid, resblks > 0,
  2548. &cdp, NULL);
  2549. if (error) {
  2550. if (error == ENOSPC)
  2551. goto error_return;
  2552. goto abort_return;
  2553. }
  2554. ITRACE(cdp);
  2555. /*
  2556. * Now we add the directory inode to the transaction.
  2557. * We waited until now since xfs_dir_ialloc might start
  2558. * a new transaction. Had we joined the transaction
  2559. * earlier, the locks might have gotten released.
  2560. */
  2561. VN_HOLD(dir_vp);
  2562. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2563. dp_joined_to_trans = B_TRUE;
  2564. XFS_BMAP_INIT(&free_list, &first_block);
  2565. error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
  2566. &first_block, &free_list, resblks ?
  2567. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  2568. if (error) {
  2569. ASSERT(error != ENOSPC);
  2570. goto error1;
  2571. }
  2572. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2573. /*
  2574. * Bump the in memory version number of the parent directory
  2575. * so that other processes accessing it will recognize that
  2576. * the directory has changed.
  2577. */
  2578. dp->i_gen++;
  2579. error = xfs_dir_init(tp, cdp, dp);
  2580. if (error)
  2581. goto error2;
  2582. cdp->i_gen = 1;
  2583. error = xfs_bumplink(tp, dp);
  2584. if (error)
  2585. goto error2;
  2586. cvp = XFS_ITOV(cdp);
  2587. created = B_TRUE;
  2588. *vpp = cvp;
  2589. IHOLD(cdp);
  2590. /*
  2591. * Attach the dquots to the new inode and modify the icount incore.
  2592. */
  2593. XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
  2594. /*
  2595. * If this is a synchronous mount, make sure that the
  2596. * mkdir transaction goes to disk before returning to
  2597. * the user.
  2598. */
  2599. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2600. xfs_trans_set_sync(tp);
  2601. }
  2602. error = xfs_bmap_finish(&tp, &free_list, &committed);
  2603. if (error) {
  2604. IRELE(cdp);
  2605. goto error2;
  2606. }
  2607. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2608. XFS_QM_DQRELE(mp, udqp);
  2609. XFS_QM_DQRELE(mp, gdqp);
  2610. if (error) {
  2611. IRELE(cdp);
  2612. }
  2613. /* Fall through to std_return with error = 0 or errno from
  2614. * xfs_trans_commit. */
  2615. std_return:
  2616. if ( (created || (error != 0 && dm_event_sent != 0)) &&
  2617. DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
  2618. DM_EVENT_POSTCREATE)) {
  2619. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
  2620. dir_vp, DM_RIGHT_NULL,
  2621. created ? XFS_ITOV(cdp):NULL,
  2622. DM_RIGHT_NULL,
  2623. dir_name, NULL,
  2624. dm_di_mode, error, 0);
  2625. }
  2626. return error;
  2627. error2:
  2628. error1:
  2629. xfs_bmap_cancel(&free_list);
  2630. abort_return:
  2631. cancel_flags |= XFS_TRANS_ABORT;
  2632. error_return:
  2633. xfs_trans_cancel(tp, cancel_flags);
  2634. XFS_QM_DQRELE(mp, udqp);
  2635. XFS_QM_DQRELE(mp, gdqp);
  2636. if (!dp_joined_to_trans && (dp != NULL)) {
  2637. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  2638. }
  2639. goto std_return;
  2640. }
  2641. /*
  2642. * xfs_rmdir
  2643. *
  2644. */
  2645. STATIC int
  2646. xfs_rmdir(
  2647. bhv_desc_t *dir_bdp,
  2648. bhv_vname_t *dentry,
  2649. cred_t *credp)
  2650. {
  2651. char *name = VNAME(dentry);
  2652. xfs_inode_t *dp;
  2653. xfs_inode_t *cdp; /* child directory */
  2654. xfs_trans_t *tp;
  2655. xfs_mount_t *mp;
  2656. int error;
  2657. xfs_bmap_free_t free_list;
  2658. xfs_fsblock_t first_block;
  2659. int cancel_flags;
  2660. int committed;
  2661. bhv_vnode_t *dir_vp;
  2662. int dm_di_mode = S_IFDIR;
  2663. int last_cdp_link;
  2664. int namelen;
  2665. uint resblks;
  2666. dir_vp = BHV_TO_VNODE(dir_bdp);
  2667. dp = XFS_BHVTOI(dir_bdp);
  2668. mp = dp->i_mount;
  2669. vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
  2670. if (XFS_FORCED_SHUTDOWN(XFS_BHVTOI(dir_bdp)->i_mount))
  2671. return XFS_ERROR(EIO);
  2672. namelen = VNAMELEN(dentry);
  2673. if (!xfs_get_dir_entry(dentry, &cdp)) {
  2674. dm_di_mode = cdp->i_d.di_mode;
  2675. IRELE(cdp);
  2676. }
  2677. if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
  2678. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
  2679. dir_vp, DM_RIGHT_NULL,
  2680. NULL, DM_RIGHT_NULL,
  2681. name, NULL, dm_di_mode, 0, 0);
  2682. if (error)
  2683. return XFS_ERROR(error);
  2684. }
  2685. /* Return through std_return after this point. */
  2686. cdp = NULL;
  2687. /*
  2688. * We need to get a reference to cdp before we get our log
  2689. * reservation. The reason for this is that we cannot call
  2690. * xfs_iget for an inode for which we do not have a reference
  2691. * once we've acquired a log reservation. This is because the
  2692. * inode we are trying to get might be in xfs_inactive going
  2693. * for a log reservation. Since we'll have to wait for the
  2694. * inactive code to complete before returning from xfs_iget,
  2695. * we need to make sure that we don't have log space reserved
  2696. * when we call xfs_iget. Instead we get an unlocked reference
  2697. * to the inode before getting our log reservation.
  2698. */
  2699. error = xfs_get_dir_entry(dentry, &cdp);
  2700. if (error) {
  2701. REMOVE_DEBUG_TRACE(__LINE__);
  2702. goto std_return;
  2703. }
  2704. mp = dp->i_mount;
  2705. dm_di_mode = cdp->i_d.di_mode;
  2706. /*
  2707. * Get the dquots for the inodes.
  2708. */
  2709. error = XFS_QM_DQATTACH(mp, dp, 0);
  2710. if (!error && dp != cdp)
  2711. error = XFS_QM_DQATTACH(mp, cdp, 0);
  2712. if (error) {
  2713. IRELE(cdp);
  2714. REMOVE_DEBUG_TRACE(__LINE__);
  2715. goto std_return;
  2716. }
  2717. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  2718. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2719. /*
  2720. * We try to get the real space reservation first,
  2721. * allowing for directory btree deletion(s) implying
  2722. * possible bmap insert(s). If we can't get the space
  2723. * reservation then we use 0 instead, and avoid the bmap
  2724. * btree insert(s) in the directory code by, if the bmap
  2725. * insert tries to happen, instead trimming the LAST
  2726. * block from the directory.
  2727. */
  2728. resblks = XFS_REMOVE_SPACE_RES(mp);
  2729. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  2730. XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
  2731. if (error == ENOSPC) {
  2732. resblks = 0;
  2733. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  2734. XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
  2735. }
  2736. if (error) {
  2737. ASSERT(error != ENOSPC);
  2738. cancel_flags = 0;
  2739. IRELE(cdp);
  2740. goto error_return;
  2741. }
  2742. XFS_BMAP_INIT(&free_list, &first_block);
  2743. /*
  2744. * Now lock the child directory inode and the parent directory
  2745. * inode in the proper order. This will take care of validating
  2746. * that the directory entry for the child directory inode has
  2747. * not changed while we were obtaining a log reservation.
  2748. */
  2749. error = xfs_lock_dir_and_entry(dp, cdp);
  2750. if (error) {
  2751. xfs_trans_cancel(tp, cancel_flags);
  2752. IRELE(cdp);
  2753. goto std_return;
  2754. }
  2755. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2756. if (dp != cdp) {
  2757. /*
  2758. * Only increment the parent directory vnode count if
  2759. * we didn't bump it in looking up cdp. The only time
  2760. * we don't bump it is when we're looking up ".".
  2761. */
  2762. VN_HOLD(dir_vp);
  2763. }
  2764. ITRACE(cdp);
  2765. xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
  2766. ASSERT(cdp->i_d.di_nlink >= 2);
  2767. if (cdp->i_d.di_nlink != 2) {
  2768. error = XFS_ERROR(ENOTEMPTY);
  2769. goto error_return;
  2770. }
  2771. if (!xfs_dir_isempty(cdp)) {
  2772. error = XFS_ERROR(ENOTEMPTY);
  2773. goto error_return;
  2774. }
  2775. error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
  2776. &first_block, &free_list, resblks);
  2777. if (error)
  2778. goto error1;
  2779. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2780. /*
  2781. * Bump the in memory generation count on the parent
  2782. * directory so that other can know that it has changed.
  2783. */
  2784. dp->i_gen++;
  2785. /*
  2786. * Drop the link from cdp's "..".
  2787. */
  2788. error = xfs_droplink(tp, dp);
  2789. if (error) {
  2790. goto error1;
  2791. }
  2792. /*
  2793. * Drop the link from dp to cdp.
  2794. */
  2795. error = xfs_droplink(tp, cdp);
  2796. if (error) {
  2797. goto error1;
  2798. }
  2799. /*
  2800. * Drop the "." link from cdp to self.
  2801. */
  2802. error = xfs_droplink(tp, cdp);
  2803. if (error) {
  2804. goto error1;
  2805. }
  2806. /* Determine these before committing transaction */
  2807. last_cdp_link = (cdp)->i_d.di_nlink==0;
  2808. /*
  2809. * Take an extra ref on the child vnode so that it
  2810. * does not go to xfs_inactive() from within the commit.
  2811. */
  2812. IHOLD(cdp);
  2813. /*
  2814. * If this is a synchronous mount, make sure that the
  2815. * rmdir transaction goes to disk before returning to
  2816. * the user.
  2817. */
  2818. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2819. xfs_trans_set_sync(tp);
  2820. }
  2821. error = xfs_bmap_finish (&tp, &free_list, &committed);
  2822. if (error) {
  2823. xfs_bmap_cancel(&free_list);
  2824. xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
  2825. XFS_TRANS_ABORT));
  2826. IRELE(cdp);
  2827. goto std_return;
  2828. }
  2829. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  2830. if (error) {
  2831. IRELE(cdp);
  2832. goto std_return;
  2833. }
  2834. /*
  2835. * Let interposed file systems know about removed links.
  2836. */
  2837. bhv_vop_link_removed(XFS_ITOV(cdp), dir_vp, last_cdp_link);
  2838. IRELE(cdp);
  2839. /* Fall through to std_return with error = 0 or the errno
  2840. * from xfs_trans_commit. */
  2841. std_return:
  2842. if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_POSTREMOVE)) {
  2843. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
  2844. dir_vp, DM_RIGHT_NULL,
  2845. NULL, DM_RIGHT_NULL,
  2846. name, NULL, dm_di_mode,
  2847. error, 0);
  2848. }
  2849. return error;
  2850. error1:
  2851. xfs_bmap_cancel(&free_list);
  2852. cancel_flags |= XFS_TRANS_ABORT;
  2853. /* FALLTHROUGH */
  2854. error_return:
  2855. xfs_trans_cancel(tp, cancel_flags);
  2856. goto std_return;
  2857. }
  2858. /*
  2859. * Read dp's entries starting at uiop->uio_offset and translate them into
  2860. * bufsize bytes worth of struct dirents starting at bufbase.
  2861. */
  2862. STATIC int
  2863. xfs_readdir(
  2864. bhv_desc_t *dir_bdp,
  2865. uio_t *uiop,
  2866. cred_t *credp,
  2867. int *eofp)
  2868. {
  2869. xfs_inode_t *dp;
  2870. xfs_trans_t *tp = NULL;
  2871. int error = 0;
  2872. uint lock_mode;
  2873. vn_trace_entry(BHV_TO_VNODE(dir_bdp), __FUNCTION__,
  2874. (inst_t *)__return_address);
  2875. dp = XFS_BHVTOI(dir_bdp);
  2876. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  2877. return XFS_ERROR(EIO);
  2878. lock_mode = xfs_ilock_map_shared(dp);
  2879. error = xfs_dir_getdents(tp, dp, uiop, eofp);
  2880. xfs_iunlock_map_shared(dp, lock_mode);
  2881. return error;
  2882. }
  2883. STATIC int
  2884. xfs_symlink(
  2885. bhv_desc_t *dir_bdp,
  2886. bhv_vname_t *dentry,
  2887. bhv_vattr_t *vap,
  2888. char *target_path,
  2889. bhv_vnode_t **vpp,
  2890. cred_t *credp)
  2891. {
  2892. xfs_trans_t *tp;
  2893. xfs_mount_t *mp;
  2894. xfs_inode_t *dp;
  2895. xfs_inode_t *ip;
  2896. int error;
  2897. int pathlen;
  2898. xfs_bmap_free_t free_list;
  2899. xfs_fsblock_t first_block;
  2900. boolean_t dp_joined_to_trans;
  2901. bhv_vnode_t *dir_vp;
  2902. uint cancel_flags;
  2903. int committed;
  2904. xfs_fileoff_t first_fsb;
  2905. xfs_filblks_t fs_blocks;
  2906. int nmaps;
  2907. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  2908. xfs_daddr_t d;
  2909. char *cur_chunk;
  2910. int byte_cnt;
  2911. int n;
  2912. xfs_buf_t *bp;
  2913. xfs_prid_t prid;
  2914. struct xfs_dquot *udqp, *gdqp;
  2915. uint resblks;
  2916. char *link_name = VNAME(dentry);
  2917. int link_namelen;
  2918. *vpp = NULL;
  2919. dir_vp = BHV_TO_VNODE(dir_bdp);
  2920. dp = XFS_BHVTOI(dir_bdp);
  2921. dp_joined_to_trans = B_FALSE;
  2922. error = 0;
  2923. ip = NULL;
  2924. tp = NULL;
  2925. vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
  2926. mp = dp->i_mount;
  2927. if (XFS_FORCED_SHUTDOWN(mp))
  2928. return XFS_ERROR(EIO);
  2929. link_namelen = VNAMELEN(dentry);
  2930. /*
  2931. * Check component lengths of the target path name.
  2932. */
  2933. pathlen = strlen(target_path);
  2934. if (pathlen >= MAXPATHLEN) /* total string too long */
  2935. return XFS_ERROR(ENAMETOOLONG);
  2936. if (pathlen >= MAXNAMELEN) { /* is any component too long? */
  2937. int len, total;
  2938. char *path;
  2939. for (total = 0, path = target_path; total < pathlen;) {
  2940. /*
  2941. * Skip any slashes.
  2942. */
  2943. while(*path == '/') {
  2944. total++;
  2945. path++;
  2946. }
  2947. /*
  2948. * Count up to the next slash or end of path.
  2949. * Error out if the component is bigger than MAXNAMELEN.
  2950. */
  2951. for(len = 0; *path != '/' && total < pathlen;total++, path++) {
  2952. if (++len >= MAXNAMELEN) {
  2953. error = ENAMETOOLONG;
  2954. return error;
  2955. }
  2956. }
  2957. }
  2958. }
  2959. if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_SYMLINK)) {
  2960. error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
  2961. DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
  2962. link_name, target_path, 0, 0, 0);
  2963. if (error)
  2964. return error;
  2965. }
  2966. /* Return through std_return after this point. */
  2967. udqp = gdqp = NULL;
  2968. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  2969. prid = dp->i_d.di_projid;
  2970. else if (vap->va_mask & XFS_AT_PROJID)
  2971. prid = (xfs_prid_t)vap->va_projid;
  2972. else
  2973. prid = (xfs_prid_t)dfltprid;
  2974. /*
  2975. * Make sure that we have allocated dquot(s) on disk.
  2976. */
  2977. error = XFS_QM_DQVOPALLOC(mp, dp,
  2978. current_fsuid(credp), current_fsgid(credp), prid,
  2979. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  2980. if (error)
  2981. goto std_return;
  2982. tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
  2983. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2984. /*
  2985. * The symlink will fit into the inode data fork?
  2986. * There can't be any attributes so we get the whole variable part.
  2987. */
  2988. if (pathlen <= XFS_LITINO(mp))
  2989. fs_blocks = 0;
  2990. else
  2991. fs_blocks = XFS_B_TO_FSB(mp, pathlen);
  2992. resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
  2993. error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
  2994. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  2995. if (error == ENOSPC && fs_blocks == 0) {
  2996. resblks = 0;
  2997. error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
  2998. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  2999. }
  3000. if (error) {
  3001. cancel_flags = 0;
  3002. dp = NULL;
  3003. goto error_return;
  3004. }
  3005. xfs_ilock(dp, XFS_ILOCK_EXCL);
  3006. /*
  3007. * Check whether the directory allows new symlinks or not.
  3008. */
  3009. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  3010. error = XFS_ERROR(EPERM);
  3011. goto error_return;
  3012. }
  3013. /*
  3014. * Reserve disk quota : blocks and inode.
  3015. */
  3016. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  3017. if (error)
  3018. goto error_return;
  3019. /*
  3020. * Check for ability to enter directory entry, if no space reserved.
  3021. */
  3022. if (resblks == 0 &&
  3023. (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
  3024. goto error_return;
  3025. /*
  3026. * Initialize the bmap freelist prior to calling either
  3027. * bmapi or the directory create code.
  3028. */
  3029. XFS_BMAP_INIT(&free_list, &first_block);
  3030. /*
  3031. * Allocate an inode for the symlink.
  3032. */
  3033. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (vap->va_mode&~S_IFMT),
  3034. 1, 0, credp, prid, resblks > 0, &ip, NULL);
  3035. if (error) {
  3036. if (error == ENOSPC)
  3037. goto error_return;
  3038. goto error1;
  3039. }
  3040. ITRACE(ip);
  3041. VN_HOLD(dir_vp);
  3042. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  3043. dp_joined_to_trans = B_TRUE;
  3044. /*
  3045. * Also attach the dquot(s) to it, if applicable.
  3046. */
  3047. XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
  3048. if (resblks)
  3049. resblks -= XFS_IALLOC_SPACE_RES(mp);
  3050. /*
  3051. * If the symlink will fit into the inode, write it inline.
  3052. */
  3053. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  3054. xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
  3055. memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
  3056. ip->i_d.di_size = pathlen;
  3057. /*
  3058. * The inode was initially created in extent format.
  3059. */
  3060. ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  3061. ip->i_df.if_flags |= XFS_IFINLINE;
  3062. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  3063. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  3064. } else {
  3065. first_fsb = 0;
  3066. nmaps = SYMLINK_MAPS;
  3067. error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
  3068. XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
  3069. &first_block, resblks, mval, &nmaps,
  3070. &free_list, NULL);
  3071. if (error) {
  3072. goto error1;
  3073. }
  3074. if (resblks)
  3075. resblks -= fs_blocks;
  3076. ip->i_d.di_size = pathlen;
  3077. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3078. cur_chunk = target_path;
  3079. for (n = 0; n < nmaps; n++) {
  3080. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  3081. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  3082. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  3083. BTOBB(byte_cnt), 0);
  3084. ASSERT(bp && !XFS_BUF_GETERROR(bp));
  3085. if (pathlen < byte_cnt) {
  3086. byte_cnt = pathlen;
  3087. }
  3088. pathlen -= byte_cnt;
  3089. memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
  3090. cur_chunk += byte_cnt;
  3091. xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
  3092. }
  3093. }
  3094. /*
  3095. * Create the directory entry for the symlink.
  3096. */
  3097. error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
  3098. &first_block, &free_list, resblks);
  3099. if (error)
  3100. goto error1;
  3101. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  3102. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  3103. /*
  3104. * Bump the in memory version number of the parent directory
  3105. * so that other processes accessing it will recognize that
  3106. * the directory has changed.
  3107. */
  3108. dp->i_gen++;
  3109. /*
  3110. * If this is a synchronous mount, make sure that the
  3111. * symlink transaction goes to disk before returning to
  3112. * the user.
  3113. */
  3114. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  3115. xfs_trans_set_sync(tp);
  3116. }
  3117. /*
  3118. * xfs_trans_commit normally decrements the vnode ref count
  3119. * when it unlocks the inode. Since we want to return the
  3120. * vnode to the caller, we bump the vnode ref count now.
  3121. */
  3122. IHOLD(ip);
  3123. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3124. if (error) {
  3125. goto error2;
  3126. }
  3127. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3128. XFS_QM_DQRELE(mp, udqp);
  3129. XFS_QM_DQRELE(mp, gdqp);
  3130. /* Fall through to std_return with error = 0 or errno from
  3131. * xfs_trans_commit */
  3132. std_return:
  3133. if (DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
  3134. DM_EVENT_POSTSYMLINK)) {
  3135. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
  3136. dir_vp, DM_RIGHT_NULL,
  3137. error ? NULL : XFS_ITOV(ip),
  3138. DM_RIGHT_NULL, link_name, target_path,
  3139. 0, error, 0);
  3140. }
  3141. if (!error) {
  3142. bhv_vnode_t *vp;
  3143. ASSERT(ip);
  3144. vp = XFS_ITOV(ip);
  3145. *vpp = vp;
  3146. }
  3147. return error;
  3148. error2:
  3149. IRELE(ip);
  3150. error1:
  3151. xfs_bmap_cancel(&free_list);
  3152. cancel_flags |= XFS_TRANS_ABORT;
  3153. error_return:
  3154. xfs_trans_cancel(tp, cancel_flags);
  3155. XFS_QM_DQRELE(mp, udqp);
  3156. XFS_QM_DQRELE(mp, gdqp);
  3157. if (!dp_joined_to_trans && (dp != NULL)) {
  3158. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  3159. }
  3160. goto std_return;
  3161. }
  3162. /*
  3163. * xfs_fid2
  3164. *
  3165. * A fid routine that takes a pointer to a previously allocated
  3166. * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
  3167. */
  3168. STATIC int
  3169. xfs_fid2(
  3170. bhv_desc_t *bdp,
  3171. fid_t *fidp)
  3172. {
  3173. xfs_inode_t *ip;
  3174. xfs_fid2_t *xfid;
  3175. vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
  3176. (inst_t *)__return_address);
  3177. ASSERT(sizeof(fid_t) >= sizeof(xfs_fid2_t));
  3178. xfid = (xfs_fid2_t *)fidp;
  3179. ip = XFS_BHVTOI(bdp);
  3180. xfid->fid_len = sizeof(xfs_fid2_t) - sizeof(xfid->fid_len);
  3181. xfid->fid_pad = 0;
  3182. /*
  3183. * use memcpy because the inode is a long long and there's no
  3184. * assurance that xfid->fid_ino is properly aligned.
  3185. */
  3186. memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
  3187. xfid->fid_gen = ip->i_d.di_gen;
  3188. return 0;
  3189. }
  3190. /*
  3191. * xfs_rwlock
  3192. */
  3193. int
  3194. xfs_rwlock(
  3195. bhv_desc_t *bdp,
  3196. bhv_vrwlock_t locktype)
  3197. {
  3198. xfs_inode_t *ip;
  3199. bhv_vnode_t *vp;
  3200. vp = BHV_TO_VNODE(bdp);
  3201. if (VN_ISDIR(vp))
  3202. return 1;
  3203. ip = XFS_BHVTOI(bdp);
  3204. if (locktype == VRWLOCK_WRITE) {
  3205. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  3206. } else if (locktype == VRWLOCK_TRY_READ) {
  3207. return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
  3208. } else if (locktype == VRWLOCK_TRY_WRITE) {
  3209. return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
  3210. } else {
  3211. ASSERT((locktype == VRWLOCK_READ) ||
  3212. (locktype == VRWLOCK_WRITE_DIRECT));
  3213. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  3214. }
  3215. return 1;
  3216. }
  3217. /*
  3218. * xfs_rwunlock
  3219. */
  3220. void
  3221. xfs_rwunlock(
  3222. bhv_desc_t *bdp,
  3223. bhv_vrwlock_t locktype)
  3224. {
  3225. xfs_inode_t *ip;
  3226. bhv_vnode_t *vp;
  3227. vp = BHV_TO_VNODE(bdp);
  3228. if (VN_ISDIR(vp))
  3229. return;
  3230. ip = XFS_BHVTOI(bdp);
  3231. if (locktype == VRWLOCK_WRITE) {
  3232. /*
  3233. * In the write case, we may have added a new entry to
  3234. * the reference cache. This might store a pointer to
  3235. * an inode to be released in this inode. If it is there,
  3236. * clear the pointer and release the inode after unlocking
  3237. * this one.
  3238. */
  3239. xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
  3240. } else {
  3241. ASSERT((locktype == VRWLOCK_READ) ||
  3242. (locktype == VRWLOCK_WRITE_DIRECT));
  3243. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  3244. }
  3245. return;
  3246. }
  3247. STATIC int
  3248. xfs_inode_flush(
  3249. bhv_desc_t *bdp,
  3250. int flags)
  3251. {
  3252. xfs_inode_t *ip;
  3253. xfs_mount_t *mp;
  3254. xfs_inode_log_item_t *iip;
  3255. int error = 0;
  3256. ip = XFS_BHVTOI(bdp);
  3257. mp = ip->i_mount;
  3258. iip = ip->i_itemp;
  3259. if (XFS_FORCED_SHUTDOWN(mp))
  3260. return XFS_ERROR(EIO);
  3261. /*
  3262. * Bypass inodes which have already been cleaned by
  3263. * the inode flush clustering code inside xfs_iflush
  3264. */
  3265. if ((ip->i_update_core == 0) &&
  3266. ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
  3267. return 0;
  3268. if (flags & FLUSH_LOG) {
  3269. if (iip && iip->ili_last_lsn) {
  3270. xlog_t *log = mp->m_log;
  3271. xfs_lsn_t sync_lsn;
  3272. int s, log_flags = XFS_LOG_FORCE;
  3273. s = GRANT_LOCK(log);
  3274. sync_lsn = log->l_last_sync_lsn;
  3275. GRANT_UNLOCK(log, s);
  3276. if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) <= 0))
  3277. return 0;
  3278. if (flags & FLUSH_SYNC)
  3279. log_flags |= XFS_LOG_SYNC;
  3280. return xfs_log_force(mp, iip->ili_last_lsn, log_flags);
  3281. }
  3282. }
  3283. /*
  3284. * We make this non-blocking if the inode is contended,
  3285. * return EAGAIN to indicate to the caller that they
  3286. * did not succeed. This prevents the flush path from
  3287. * blocking on inodes inside another operation right
  3288. * now, they get caught later by xfs_sync.
  3289. */
  3290. if (flags & FLUSH_INODE) {
  3291. int flush_flags;
  3292. if (xfs_ipincount(ip))
  3293. return EAGAIN;
  3294. if (flags & FLUSH_SYNC) {
  3295. xfs_ilock(ip, XFS_ILOCK_SHARED);
  3296. xfs_iflock(ip);
  3297. } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  3298. if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
  3299. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3300. return EAGAIN;
  3301. }
  3302. } else {
  3303. return EAGAIN;
  3304. }
  3305. if (flags & FLUSH_SYNC)
  3306. flush_flags = XFS_IFLUSH_SYNC;
  3307. else
  3308. flush_flags = XFS_IFLUSH_ASYNC;
  3309. error = xfs_iflush(ip, flush_flags);
  3310. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3311. }
  3312. return error;
  3313. }
  3314. int
  3315. xfs_set_dmattrs (
  3316. bhv_desc_t *bdp,
  3317. u_int evmask,
  3318. u_int16_t state,
  3319. cred_t *credp)
  3320. {
  3321. xfs_inode_t *ip;
  3322. xfs_trans_t *tp;
  3323. xfs_mount_t *mp;
  3324. int error;
  3325. if (!capable(CAP_SYS_ADMIN))
  3326. return XFS_ERROR(EPERM);
  3327. ip = XFS_BHVTOI(bdp);
  3328. mp = ip->i_mount;
  3329. if (XFS_FORCED_SHUTDOWN(mp))
  3330. return XFS_ERROR(EIO);
  3331. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  3332. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
  3333. if (error) {
  3334. xfs_trans_cancel(tp, 0);
  3335. return error;
  3336. }
  3337. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3338. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3339. ip->i_iocore.io_dmevmask = ip->i_d.di_dmevmask = evmask;
  3340. ip->i_iocore.io_dmstate = ip->i_d.di_dmstate = state;
  3341. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3342. IHOLD(ip);
  3343. error = xfs_trans_commit(tp, 0);
  3344. return error;
  3345. }
  3346. STATIC int
  3347. xfs_reclaim(
  3348. bhv_desc_t *bdp)
  3349. {
  3350. xfs_inode_t *ip;
  3351. bhv_vnode_t *vp;
  3352. vp = BHV_TO_VNODE(bdp);
  3353. ip = XFS_BHVTOI(bdp);
  3354. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  3355. ASSERT(!VN_MAPPED(vp));
  3356. /* bad inode, get out here ASAP */
  3357. if (VN_BAD(vp)) {
  3358. xfs_ireclaim(ip);
  3359. return 0;
  3360. }
  3361. vn_iowait(vp);
  3362. ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
  3363. /*
  3364. * Make sure the atime in the XFS inode is correct before freeing the
  3365. * Linux inode.
  3366. */
  3367. xfs_synchronize_atime(ip);
  3368. /*
  3369. * If we have nothing to flush with this inode then complete the
  3370. * teardown now, otherwise break the link between the xfs inode and the
  3371. * linux inode and clean up the xfs inode later. This avoids flushing
  3372. * the inode to disk during the delete operation itself.
  3373. *
  3374. * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
  3375. * first to ensure that xfs_iunpin() will never see an xfs inode
  3376. * that has a linux inode being reclaimed. Synchronisation is provided
  3377. * by the i_flags_lock.
  3378. */
  3379. if (!ip->i_update_core && (ip->i_itemp == NULL)) {
  3380. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3381. xfs_iflock(ip);
  3382. return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
  3383. } else {
  3384. xfs_mount_t *mp = ip->i_mount;
  3385. /* Protect sync and unpin from us */
  3386. XFS_MOUNT_ILOCK(mp);
  3387. spin_lock(&ip->i_flags_lock);
  3388. __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
  3389. vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
  3390. spin_unlock(&ip->i_flags_lock);
  3391. list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
  3392. XFS_MOUNT_IUNLOCK(mp);
  3393. }
  3394. return 0;
  3395. }
  3396. int
  3397. xfs_finish_reclaim(
  3398. xfs_inode_t *ip,
  3399. int locked,
  3400. int sync_mode)
  3401. {
  3402. xfs_ihash_t *ih = ip->i_hash;
  3403. bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
  3404. int error;
  3405. if (vp && VN_BAD(vp))
  3406. goto reclaim;
  3407. /* The hash lock here protects a thread in xfs_iget_core from
  3408. * racing with us on linking the inode back with a vnode.
  3409. * Once we have the XFS_IRECLAIM flag set it will not touch
  3410. * us.
  3411. */
  3412. write_lock(&ih->ih_lock);
  3413. spin_lock(&ip->i_flags_lock);
  3414. if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
  3415. (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
  3416. spin_unlock(&ip->i_flags_lock);
  3417. write_unlock(&ih->ih_lock);
  3418. if (locked) {
  3419. xfs_ifunlock(ip);
  3420. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3421. }
  3422. return 1;
  3423. }
  3424. __xfs_iflags_set(ip, XFS_IRECLAIM);
  3425. spin_unlock(&ip->i_flags_lock);
  3426. write_unlock(&ih->ih_lock);
  3427. /*
  3428. * If the inode is still dirty, then flush it out. If the inode
  3429. * is not in the AIL, then it will be OK to flush it delwri as
  3430. * long as xfs_iflush() does not keep any references to the inode.
  3431. * We leave that decision up to xfs_iflush() since it has the
  3432. * knowledge of whether it's OK to simply do a delwri flush of
  3433. * the inode or whether we need to wait until the inode is
  3434. * pulled from the AIL.
  3435. * We get the flush lock regardless, though, just to make sure
  3436. * we don't free it while it is being flushed.
  3437. */
  3438. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  3439. if (!locked) {
  3440. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3441. xfs_iflock(ip);
  3442. }
  3443. if (ip->i_update_core ||
  3444. ((ip->i_itemp != NULL) &&
  3445. (ip->i_itemp->ili_format.ilf_fields != 0))) {
  3446. error = xfs_iflush(ip, sync_mode);
  3447. /*
  3448. * If we hit an error, typically because of filesystem
  3449. * shutdown, we don't need to let vn_reclaim to know
  3450. * because we're gonna reclaim the inode anyway.
  3451. */
  3452. if (error) {
  3453. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3454. goto reclaim;
  3455. }
  3456. xfs_iflock(ip); /* synchronize with xfs_iflush_done */
  3457. }
  3458. ASSERT(ip->i_update_core == 0);
  3459. ASSERT(ip->i_itemp == NULL ||
  3460. ip->i_itemp->ili_format.ilf_fields == 0);
  3461. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3462. } else if (locked) {
  3463. /*
  3464. * We are not interested in doing an iflush if we're
  3465. * in the process of shutting down the filesystem forcibly.
  3466. * So, just reclaim the inode.
  3467. */
  3468. xfs_ifunlock(ip);
  3469. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3470. }
  3471. reclaim:
  3472. xfs_ireclaim(ip);
  3473. return 0;
  3474. }
  3475. int
  3476. xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
  3477. {
  3478. int purged;
  3479. xfs_inode_t *ip, *n;
  3480. int done = 0;
  3481. while (!done) {
  3482. purged = 0;
  3483. XFS_MOUNT_ILOCK(mp);
  3484. list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
  3485. if (noblock) {
  3486. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
  3487. continue;
  3488. if (xfs_ipincount(ip) ||
  3489. !xfs_iflock_nowait(ip)) {
  3490. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3491. continue;
  3492. }
  3493. }
  3494. XFS_MOUNT_IUNLOCK(mp);
  3495. if (xfs_finish_reclaim(ip, noblock,
  3496. XFS_IFLUSH_DELWRI_ELSE_ASYNC))
  3497. delay(1);
  3498. purged = 1;
  3499. break;
  3500. }
  3501. done = !purged;
  3502. }
  3503. XFS_MOUNT_IUNLOCK(mp);
  3504. return 0;
  3505. }
  3506. /*
  3507. * xfs_alloc_file_space()
  3508. * This routine allocates disk space for the given file.
  3509. *
  3510. * If alloc_type == 0, this request is for an ALLOCSP type
  3511. * request which will change the file size. In this case, no
  3512. * DMAPI event will be generated by the call. A TRUNCATE event
  3513. * will be generated later by xfs_setattr.
  3514. *
  3515. * If alloc_type != 0, this request is for a RESVSP type
  3516. * request, and a DMAPI DM_EVENT_WRITE will be generated if the
  3517. * lower block boundary byte address is less than the file's
  3518. * length.
  3519. *
  3520. * RETURNS:
  3521. * 0 on success
  3522. * errno on error
  3523. *
  3524. */
  3525. STATIC int
  3526. xfs_alloc_file_space(
  3527. xfs_inode_t *ip,
  3528. xfs_off_t offset,
  3529. xfs_off_t len,
  3530. int alloc_type,
  3531. int attr_flags)
  3532. {
  3533. xfs_mount_t *mp = ip->i_mount;
  3534. xfs_off_t count;
  3535. xfs_filblks_t allocated_fsb;
  3536. xfs_filblks_t allocatesize_fsb;
  3537. xfs_extlen_t extsz, temp;
  3538. xfs_fileoff_t startoffset_fsb;
  3539. xfs_fsblock_t firstfsb;
  3540. int nimaps;
  3541. int bmapi_flag;
  3542. int quota_flag;
  3543. int rt;
  3544. xfs_trans_t *tp;
  3545. xfs_bmbt_irec_t imaps[1], *imapp;
  3546. xfs_bmap_free_t free_list;
  3547. uint qblocks, resblks, resrtextents;
  3548. int committed;
  3549. int error;
  3550. vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
  3551. if (XFS_FORCED_SHUTDOWN(mp))
  3552. return XFS_ERROR(EIO);
  3553. rt = XFS_IS_REALTIME_INODE(ip);
  3554. if (unlikely(rt)) {
  3555. if (!(extsz = ip->i_d.di_extsize))
  3556. extsz = mp->m_sb.sb_rextsize;
  3557. } else {
  3558. extsz = ip->i_d.di_extsize;
  3559. }
  3560. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  3561. return error;
  3562. if (len <= 0)
  3563. return XFS_ERROR(EINVAL);
  3564. count = len;
  3565. error = 0;
  3566. imapp = &imaps[0];
  3567. nimaps = 1;
  3568. bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
  3569. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  3570. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  3571. /* Generate a DMAPI event if needed. */
  3572. if (alloc_type != 0 && offset < ip->i_d.di_size &&
  3573. (attr_flags&ATTR_DMI) == 0 &&
  3574. DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
  3575. xfs_off_t end_dmi_offset;
  3576. end_dmi_offset = offset+len;
  3577. if (end_dmi_offset > ip->i_d.di_size)
  3578. end_dmi_offset = ip->i_d.di_size;
  3579. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
  3580. offset, end_dmi_offset - offset,
  3581. 0, NULL);
  3582. if (error)
  3583. return error;
  3584. }
  3585. /*
  3586. * Allocate file space until done or until there is an error
  3587. */
  3588. retry:
  3589. while (allocatesize_fsb && !error) {
  3590. xfs_fileoff_t s, e;
  3591. /*
  3592. * Determine space reservations for data/realtime.
  3593. */
  3594. if (unlikely(extsz)) {
  3595. s = startoffset_fsb;
  3596. do_div(s, extsz);
  3597. s *= extsz;
  3598. e = startoffset_fsb + allocatesize_fsb;
  3599. if ((temp = do_mod(startoffset_fsb, extsz)))
  3600. e += temp;
  3601. if ((temp = do_mod(e, extsz)))
  3602. e += extsz - temp;
  3603. } else {
  3604. s = 0;
  3605. e = allocatesize_fsb;
  3606. }
  3607. if (unlikely(rt)) {
  3608. resrtextents = qblocks = (uint)(e - s);
  3609. resrtextents /= mp->m_sb.sb_rextsize;
  3610. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  3611. quota_flag = XFS_QMOPT_RES_RTBLKS;
  3612. } else {
  3613. resrtextents = 0;
  3614. resblks = qblocks = \
  3615. XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
  3616. quota_flag = XFS_QMOPT_RES_REGBLKS;
  3617. }
  3618. /*
  3619. * Allocate and setup the transaction.
  3620. */
  3621. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  3622. error = xfs_trans_reserve(tp, resblks,
  3623. XFS_WRITE_LOG_RES(mp), resrtextents,
  3624. XFS_TRANS_PERM_LOG_RES,
  3625. XFS_WRITE_LOG_COUNT);
  3626. /*
  3627. * Check for running out of space
  3628. */
  3629. if (error) {
  3630. /*
  3631. * Free the transaction structure.
  3632. */
  3633. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  3634. xfs_trans_cancel(tp, 0);
  3635. break;
  3636. }
  3637. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3638. error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
  3639. qblocks, 0, quota_flag);
  3640. if (error)
  3641. goto error1;
  3642. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3643. xfs_trans_ihold(tp, ip);
  3644. /*
  3645. * Issue the xfs_bmapi() call to allocate the blocks
  3646. */
  3647. XFS_BMAP_INIT(&free_list, &firstfsb);
  3648. error = XFS_BMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
  3649. allocatesize_fsb, bmapi_flag,
  3650. &firstfsb, 0, imapp, &nimaps,
  3651. &free_list, NULL);
  3652. if (error) {
  3653. goto error0;
  3654. }
  3655. /*
  3656. * Complete the transaction
  3657. */
  3658. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3659. if (error) {
  3660. goto error0;
  3661. }
  3662. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3663. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3664. if (error) {
  3665. break;
  3666. }
  3667. allocated_fsb = imapp->br_blockcount;
  3668. if (nimaps == 0) {
  3669. error = XFS_ERROR(ENOSPC);
  3670. break;
  3671. }
  3672. startoffset_fsb += allocated_fsb;
  3673. allocatesize_fsb -= allocated_fsb;
  3674. }
  3675. dmapi_enospc_check:
  3676. if (error == ENOSPC && (attr_flags&ATTR_DMI) == 0 &&
  3677. DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_NOSPACE)) {
  3678. error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
  3679. XFS_ITOV(ip), DM_RIGHT_NULL,
  3680. XFS_ITOV(ip), DM_RIGHT_NULL,
  3681. NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
  3682. if (error == 0)
  3683. goto retry; /* Maybe DMAPI app. has made space */
  3684. /* else fall through with error from XFS_SEND_DATA */
  3685. }
  3686. return error;
  3687. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  3688. xfs_bmap_cancel(&free_list);
  3689. XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
  3690. error1: /* Just cancel transaction */
  3691. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  3692. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3693. goto dmapi_enospc_check;
  3694. }
  3695. /*
  3696. * Zero file bytes between startoff and endoff inclusive.
  3697. * The iolock is held exclusive and no blocks are buffered.
  3698. */
  3699. STATIC int
  3700. xfs_zero_remaining_bytes(
  3701. xfs_inode_t *ip,
  3702. xfs_off_t startoff,
  3703. xfs_off_t endoff)
  3704. {
  3705. xfs_bmbt_irec_t imap;
  3706. xfs_fileoff_t offset_fsb;
  3707. xfs_off_t lastoffset;
  3708. xfs_off_t offset;
  3709. xfs_buf_t *bp;
  3710. xfs_mount_t *mp = ip->i_mount;
  3711. int nimap;
  3712. int error = 0;
  3713. bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
  3714. ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
  3715. mp->m_rtdev_targp : mp->m_ddev_targp);
  3716. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  3717. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  3718. nimap = 1;
  3719. error = XFS_BMAPI(mp, NULL, &ip->i_iocore, offset_fsb, 1, 0,
  3720. NULL, 0, &imap, &nimap, NULL, NULL);
  3721. if (error || nimap < 1)
  3722. break;
  3723. ASSERT(imap.br_blockcount >= 1);
  3724. ASSERT(imap.br_startoff == offset_fsb);
  3725. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  3726. if (lastoffset > endoff)
  3727. lastoffset = endoff;
  3728. if (imap.br_startblock == HOLESTARTBLOCK)
  3729. continue;
  3730. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3731. if (imap.br_state == XFS_EXT_UNWRITTEN)
  3732. continue;
  3733. XFS_BUF_UNDONE(bp);
  3734. XFS_BUF_UNWRITE(bp);
  3735. XFS_BUF_READ(bp);
  3736. XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
  3737. xfsbdstrat(mp, bp);
  3738. if ((error = xfs_iowait(bp))) {
  3739. xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
  3740. mp, bp, XFS_BUF_ADDR(bp));
  3741. break;
  3742. }
  3743. memset(XFS_BUF_PTR(bp) +
  3744. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  3745. 0, lastoffset - offset + 1);
  3746. XFS_BUF_UNDONE(bp);
  3747. XFS_BUF_UNREAD(bp);
  3748. XFS_BUF_WRITE(bp);
  3749. xfsbdstrat(mp, bp);
  3750. if ((error = xfs_iowait(bp))) {
  3751. xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
  3752. mp, bp, XFS_BUF_ADDR(bp));
  3753. break;
  3754. }
  3755. }
  3756. xfs_buf_free(bp);
  3757. return error;
  3758. }
  3759. /*
  3760. * xfs_free_file_space()
  3761. * This routine frees disk space for the given file.
  3762. *
  3763. * This routine is only called by xfs_change_file_space
  3764. * for an UNRESVSP type call.
  3765. *
  3766. * RETURNS:
  3767. * 0 on success
  3768. * errno on error
  3769. *
  3770. */
  3771. STATIC int
  3772. xfs_free_file_space(
  3773. xfs_inode_t *ip,
  3774. xfs_off_t offset,
  3775. xfs_off_t len,
  3776. int attr_flags)
  3777. {
  3778. bhv_vnode_t *vp;
  3779. int committed;
  3780. int done;
  3781. xfs_off_t end_dmi_offset;
  3782. xfs_fileoff_t endoffset_fsb;
  3783. int error;
  3784. xfs_fsblock_t firstfsb;
  3785. xfs_bmap_free_t free_list;
  3786. xfs_off_t ilen;
  3787. xfs_bmbt_irec_t imap;
  3788. xfs_off_t ioffset;
  3789. xfs_extlen_t mod=0;
  3790. xfs_mount_t *mp;
  3791. int nimap;
  3792. uint resblks;
  3793. uint rounding;
  3794. int rt;
  3795. xfs_fileoff_t startoffset_fsb;
  3796. xfs_trans_t *tp;
  3797. int need_iolock = 1;
  3798. vp = XFS_ITOV(ip);
  3799. mp = ip->i_mount;
  3800. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  3801. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  3802. return error;
  3803. error = 0;
  3804. if (len <= 0) /* if nothing being freed */
  3805. return error;
  3806. rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
  3807. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  3808. end_dmi_offset = offset + len;
  3809. endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
  3810. if (offset < ip->i_d.di_size &&
  3811. (attr_flags & ATTR_DMI) == 0 &&
  3812. DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
  3813. if (end_dmi_offset > ip->i_d.di_size)
  3814. end_dmi_offset = ip->i_d.di_size;
  3815. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
  3816. offset, end_dmi_offset - offset,
  3817. AT_DELAY_FLAG(attr_flags), NULL);
  3818. if (error)
  3819. return error;
  3820. }
  3821. if (attr_flags & ATTR_NOLOCK)
  3822. need_iolock = 0;
  3823. if (need_iolock) {
  3824. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  3825. vn_iowait(vp); /* wait for the completion of any pending DIOs */
  3826. }
  3827. rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, NBPP);
  3828. ilen = len + (offset & (rounding - 1));
  3829. ioffset = offset & ~(rounding - 1);
  3830. if (ilen & (rounding - 1))
  3831. ilen = (ilen + rounding) & ~(rounding - 1);
  3832. if (VN_CACHED(vp) != 0) {
  3833. xfs_inval_cached_trace(&ip->i_iocore, ioffset, -1,
  3834. ctooff(offtoct(ioffset)), -1);
  3835. error = bhv_vop_flushinval_pages(vp, ctooff(offtoct(ioffset)),
  3836. -1, FI_REMAPF_LOCKED);
  3837. if (error)
  3838. goto out_unlock_iolock;
  3839. }
  3840. /*
  3841. * Need to zero the stuff we're not freeing, on disk.
  3842. * If its a realtime file & can't use unwritten extents then we
  3843. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  3844. * will take care of it for us.
  3845. */
  3846. if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
  3847. nimap = 1;
  3848. error = XFS_BMAPI(mp, NULL, &ip->i_iocore, startoffset_fsb,
  3849. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  3850. if (error)
  3851. goto out_unlock_iolock;
  3852. ASSERT(nimap == 0 || nimap == 1);
  3853. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  3854. xfs_daddr_t block;
  3855. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3856. block = imap.br_startblock;
  3857. mod = do_div(block, mp->m_sb.sb_rextsize);
  3858. if (mod)
  3859. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  3860. }
  3861. nimap = 1;
  3862. error = XFS_BMAPI(mp, NULL, &ip->i_iocore, endoffset_fsb - 1,
  3863. 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
  3864. if (error)
  3865. goto out_unlock_iolock;
  3866. ASSERT(nimap == 0 || nimap == 1);
  3867. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  3868. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3869. mod++;
  3870. if (mod && (mod != mp->m_sb.sb_rextsize))
  3871. endoffset_fsb -= mod;
  3872. }
  3873. }
  3874. if ((done = (endoffset_fsb <= startoffset_fsb)))
  3875. /*
  3876. * One contiguous piece to clear
  3877. */
  3878. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  3879. else {
  3880. /*
  3881. * Some full blocks, possibly two pieces to clear
  3882. */
  3883. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  3884. error = xfs_zero_remaining_bytes(ip, offset,
  3885. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  3886. if (!error &&
  3887. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  3888. error = xfs_zero_remaining_bytes(ip,
  3889. XFS_FSB_TO_B(mp, endoffset_fsb),
  3890. offset + len - 1);
  3891. }
  3892. /*
  3893. * free file space until done or until there is an error
  3894. */
  3895. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  3896. while (!error && !done) {
  3897. /*
  3898. * allocate and setup the transaction
  3899. */
  3900. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  3901. error = xfs_trans_reserve(tp,
  3902. resblks,
  3903. XFS_WRITE_LOG_RES(mp),
  3904. 0,
  3905. XFS_TRANS_PERM_LOG_RES,
  3906. XFS_WRITE_LOG_COUNT);
  3907. /*
  3908. * check for running out of space
  3909. */
  3910. if (error) {
  3911. /*
  3912. * Free the transaction structure.
  3913. */
  3914. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  3915. xfs_trans_cancel(tp, 0);
  3916. break;
  3917. }
  3918. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3919. error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
  3920. ip->i_udquot, ip->i_gdquot, resblks, 0,
  3921. XFS_QMOPT_RES_REGBLKS);
  3922. if (error)
  3923. goto error1;
  3924. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3925. xfs_trans_ihold(tp, ip);
  3926. /*
  3927. * issue the bunmapi() call to free the blocks
  3928. */
  3929. XFS_BMAP_INIT(&free_list, &firstfsb);
  3930. error = XFS_BUNMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
  3931. endoffset_fsb - startoffset_fsb,
  3932. 0, 2, &firstfsb, &free_list, NULL, &done);
  3933. if (error) {
  3934. goto error0;
  3935. }
  3936. /*
  3937. * complete the transaction
  3938. */
  3939. error = xfs_bmap_finish(&tp, &free_list, &committed);
  3940. if (error) {
  3941. goto error0;
  3942. }
  3943. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  3944. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3945. }
  3946. out_unlock_iolock:
  3947. if (need_iolock)
  3948. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  3949. return error;
  3950. error0:
  3951. xfs_bmap_cancel(&free_list);
  3952. error1:
  3953. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  3954. xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
  3955. XFS_ILOCK_EXCL);
  3956. return error;
  3957. }
  3958. /*
  3959. * xfs_change_file_space()
  3960. * This routine allocates or frees disk space for the given file.
  3961. * The user specified parameters are checked for alignment and size
  3962. * limitations.
  3963. *
  3964. * RETURNS:
  3965. * 0 on success
  3966. * errno on error
  3967. *
  3968. */
  3969. int
  3970. xfs_change_file_space(
  3971. bhv_desc_t *bdp,
  3972. int cmd,
  3973. xfs_flock64_t *bf,
  3974. xfs_off_t offset,
  3975. cred_t *credp,
  3976. int attr_flags)
  3977. {
  3978. int clrprealloc;
  3979. int error;
  3980. xfs_fsize_t fsize;
  3981. xfs_inode_t *ip;
  3982. xfs_mount_t *mp;
  3983. int setprealloc;
  3984. xfs_off_t startoffset;
  3985. xfs_off_t llen;
  3986. xfs_trans_t *tp;
  3987. bhv_vattr_t va;
  3988. bhv_vnode_t *vp;
  3989. vp = BHV_TO_VNODE(bdp);
  3990. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  3991. ip = XFS_BHVTOI(bdp);
  3992. mp = ip->i_mount;
  3993. /*
  3994. * must be a regular file and have write permission
  3995. */
  3996. if (!VN_ISREG(vp))
  3997. return XFS_ERROR(EINVAL);
  3998. xfs_ilock(ip, XFS_ILOCK_SHARED);
  3999. if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
  4000. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  4001. return error;
  4002. }
  4003. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  4004. switch (bf->l_whence) {
  4005. case 0: /*SEEK_SET*/
  4006. break;
  4007. case 1: /*SEEK_CUR*/
  4008. bf->l_start += offset;
  4009. break;
  4010. case 2: /*SEEK_END*/
  4011. bf->l_start += ip->i_d.di_size;
  4012. break;
  4013. default:
  4014. return XFS_ERROR(EINVAL);
  4015. }
  4016. llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
  4017. if ( (bf->l_start < 0)
  4018. || (bf->l_start > XFS_MAXIOFFSET(mp))
  4019. || (bf->l_start + llen < 0)
  4020. || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
  4021. return XFS_ERROR(EINVAL);
  4022. bf->l_whence = 0;
  4023. startoffset = bf->l_start;
  4024. fsize = ip->i_d.di_size;
  4025. /*
  4026. * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
  4027. * file space.
  4028. * These calls do NOT zero the data space allocated to the file,
  4029. * nor do they change the file size.
  4030. *
  4031. * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
  4032. * space.
  4033. * These calls cause the new file data to be zeroed and the file
  4034. * size to be changed.
  4035. */
  4036. setprealloc = clrprealloc = 0;
  4037. switch (cmd) {
  4038. case XFS_IOC_RESVSP:
  4039. case XFS_IOC_RESVSP64:
  4040. error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
  4041. 1, attr_flags);
  4042. if (error)
  4043. return error;
  4044. setprealloc = 1;
  4045. break;
  4046. case XFS_IOC_UNRESVSP:
  4047. case XFS_IOC_UNRESVSP64:
  4048. if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
  4049. attr_flags)))
  4050. return error;
  4051. break;
  4052. case XFS_IOC_ALLOCSP:
  4053. case XFS_IOC_ALLOCSP64:
  4054. case XFS_IOC_FREESP:
  4055. case XFS_IOC_FREESP64:
  4056. if (startoffset > fsize) {
  4057. error = xfs_alloc_file_space(ip, fsize,
  4058. startoffset - fsize, 0, attr_flags);
  4059. if (error)
  4060. break;
  4061. }
  4062. va.va_mask = XFS_AT_SIZE;
  4063. va.va_size = startoffset;
  4064. error = xfs_setattr(bdp, &va, attr_flags, credp);
  4065. if (error)
  4066. return error;
  4067. clrprealloc = 1;
  4068. break;
  4069. default:
  4070. ASSERT(0);
  4071. return XFS_ERROR(EINVAL);
  4072. }
  4073. /*
  4074. * update the inode timestamp, mode, and prealloc flag bits
  4075. */
  4076. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  4077. if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
  4078. 0, 0, 0))) {
  4079. /* ASSERT(0); */
  4080. xfs_trans_cancel(tp, 0);
  4081. return error;
  4082. }
  4083. xfs_ilock(ip, XFS_ILOCK_EXCL);
  4084. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  4085. xfs_trans_ihold(tp, ip);
  4086. if ((attr_flags & ATTR_DMI) == 0) {
  4087. ip->i_d.di_mode &= ~S_ISUID;
  4088. /*
  4089. * Note that we don't have to worry about mandatory
  4090. * file locking being disabled here because we only
  4091. * clear the S_ISGID bit if the Group execute bit is
  4092. * on, but if it was on then mandatory locking wouldn't
  4093. * have been enabled.
  4094. */
  4095. if (ip->i_d.di_mode & S_IXGRP)
  4096. ip->i_d.di_mode &= ~S_ISGID;
  4097. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  4098. }
  4099. if (setprealloc)
  4100. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  4101. else if (clrprealloc)
  4102. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  4103. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  4104. xfs_trans_set_sync(tp);
  4105. error = xfs_trans_commit(tp, 0);
  4106. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  4107. return error;
  4108. }
  4109. bhv_vnodeops_t xfs_vnodeops = {
  4110. BHV_IDENTITY_INIT(VN_BHV_XFS,VNODE_POSITION_XFS),
  4111. .vop_open = xfs_open,
  4112. .vop_close = xfs_close,
  4113. .vop_read = xfs_read,
  4114. #ifdef HAVE_SENDFILE
  4115. .vop_sendfile = xfs_sendfile,
  4116. #endif
  4117. #ifdef HAVE_SPLICE
  4118. .vop_splice_read = xfs_splice_read,
  4119. .vop_splice_write = xfs_splice_write,
  4120. #endif
  4121. .vop_write = xfs_write,
  4122. .vop_ioctl = xfs_ioctl,
  4123. .vop_getattr = xfs_getattr,
  4124. .vop_setattr = xfs_setattr,
  4125. .vop_access = xfs_access,
  4126. .vop_lookup = xfs_lookup,
  4127. .vop_create = xfs_create,
  4128. .vop_remove = xfs_remove,
  4129. .vop_link = xfs_link,
  4130. .vop_rename = xfs_rename,
  4131. .vop_mkdir = xfs_mkdir,
  4132. .vop_rmdir = xfs_rmdir,
  4133. .vop_readdir = xfs_readdir,
  4134. .vop_symlink = xfs_symlink,
  4135. .vop_readlink = xfs_readlink,
  4136. .vop_fsync = xfs_fsync,
  4137. .vop_inactive = xfs_inactive,
  4138. .vop_fid2 = xfs_fid2,
  4139. .vop_rwlock = xfs_rwlock,
  4140. .vop_rwunlock = xfs_rwunlock,
  4141. .vop_bmap = xfs_bmap,
  4142. .vop_reclaim = xfs_reclaim,
  4143. .vop_attr_get = xfs_attr_get,
  4144. .vop_attr_set = xfs_attr_set,
  4145. .vop_attr_remove = xfs_attr_remove,
  4146. .vop_attr_list = xfs_attr_list,
  4147. .vop_link_removed = (vop_link_removed_t)fs_noval,
  4148. .vop_vnode_change = (vop_vnode_change_t)fs_noval,
  4149. .vop_tosspages = fs_tosspages,
  4150. .vop_flushinval_pages = fs_flushinval_pages,
  4151. .vop_flush_pages = fs_flush_pages,
  4152. .vop_release = xfs_release,
  4153. .vop_iflush = xfs_inode_flush,
  4154. };