xfs_vnodeops.c 114 KB

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