xfs_vnodeops.c 113 KB

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