xfs_vnodeops.c 115 KB

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