xfs_vnodeops.c 114 KB

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