xfs_vnodeops.c 113 KB

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