xfs_vnodeops.c 113 KB

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