xfs_vnodeops.c 113 KB

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