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