xfs_vnodeops.c 114 KB

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