xfs_vnodeops.c 115 KB

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