xfs_vnodeops.c 115 KB

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