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