xfs_vnodeops.c 114 KB

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