xfs_vnodeops.c 114 KB

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