xfs_vnodeops.c 113 KB

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