xfs_vnodeops.c 115 KB

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