xfs_vnodeops.c 116 KB

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