xfs_vnodeops.c 113 KB

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