xfs_vnodeops.c 113 KB

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