xfs_vnodeops.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752
  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 (vp->v_type == VDIR && 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_type = vp->v_type;
  150. vap->va_mode = ip->i_d.di_mode & MODEMASK;
  151. vap->va_uid = ip->i_d.di_uid;
  152. vap->va_gid = ip->i_d.di_gid;
  153. vap->va_projid = ip->i_d.di_projid;
  154. /*
  155. * Check vnode type block/char vs. everything else.
  156. * Do it with bitmask because that's faster than looking
  157. * for multiple values individually.
  158. */
  159. if (((1 << vp->v_type) & ((1<<VBLK) | (1<<VCHR))) == 0) {
  160. vap->va_rdev = 0;
  161. if (!(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
  162. #if 0
  163. /* Large block sizes confuse various
  164. * user space programs, so letting the
  165. * stripe size through is not a good
  166. * idea for now.
  167. */
  168. vap->va_blocksize = mp->m_swidth ?
  169. /*
  170. * If the underlying volume is a stripe, then
  171. * return the stripe width in bytes as the
  172. * recommended I/O size.
  173. */
  174. (mp->m_swidth << mp->m_sb.sb_blocklog) :
  175. /*
  176. * Return the largest of the preferred buffer
  177. * sizes since doing small I/Os into larger
  178. * buffers causes buffers to be decommissioned.
  179. * The value returned is in bytes.
  180. */
  181. (1 << (int)MAX(mp->m_readio_log,
  182. mp->m_writeio_log));
  183. #else
  184. vap->va_blocksize =
  185. /*
  186. * Return the largest of the preferred buffer
  187. * sizes since doing small I/Os into larger
  188. * buffers causes buffers to be decommissioned.
  189. * The value returned is in bytes.
  190. */
  191. 1 << (int)MAX(mp->m_readio_log,
  192. mp->m_writeio_log);
  193. #endif
  194. } else {
  195. /*
  196. * If the file blocks are being allocated from a
  197. * realtime partition, then return the inode's
  198. * realtime extent size or the realtime volume's
  199. * extent size.
  200. */
  201. vap->va_blocksize = ip->i_d.di_extsize ?
  202. (ip->i_d.di_extsize << mp->m_sb.sb_blocklog) :
  203. (mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog);
  204. }
  205. } else {
  206. vap->va_rdev = ip->i_df.if_u2.if_rdev;
  207. vap->va_blocksize = BLKDEV_IOSIZE;
  208. }
  209. vap->va_atime.tv_sec = ip->i_d.di_atime.t_sec;
  210. vap->va_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
  211. vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
  212. vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
  213. vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
  214. vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
  215. /*
  216. * Exit for stat callers. See if any of the rest of the fields
  217. * to be filled in are needed.
  218. */
  219. if ((vap->va_mask &
  220. (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
  221. XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
  222. goto all_done;
  223. /*
  224. * Convert di_flags to xflags.
  225. */
  226. vap->va_xflags = xfs_ip2xflags(ip);
  227. /*
  228. * Exit for inode revalidate. See if any of the rest of
  229. * the fields to be filled in are needed.
  230. */
  231. if ((vap->va_mask &
  232. (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
  233. XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
  234. goto all_done;
  235. vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
  236. vap->va_nextents =
  237. (ip->i_df.if_flags & XFS_IFEXTENTS) ?
  238. ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
  239. ip->i_d.di_nextents;
  240. if (ip->i_afp)
  241. vap->va_anextents =
  242. (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
  243. ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
  244. ip->i_d.di_anextents;
  245. else
  246. vap->va_anextents = 0;
  247. vap->va_gen = ip->i_d.di_gen;
  248. all_done:
  249. if (!(flags & ATTR_LAZY))
  250. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  251. return 0;
  252. }
  253. /*
  254. * xfs_setattr
  255. */
  256. int
  257. xfs_setattr(
  258. bhv_desc_t *bdp,
  259. vattr_t *vap,
  260. int flags,
  261. cred_t *credp)
  262. {
  263. xfs_inode_t *ip;
  264. xfs_trans_t *tp;
  265. xfs_mount_t *mp;
  266. int mask;
  267. int code;
  268. uint lock_flags;
  269. uint commit_flags=0;
  270. uid_t uid=0, iuid=0;
  271. gid_t gid=0, igid=0;
  272. int timeflags = 0;
  273. vnode_t *vp;
  274. xfs_prid_t projid=0, iprojid=0;
  275. int mandlock_before, mandlock_after;
  276. struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
  277. int file_owner;
  278. int need_iolock = 1;
  279. vp = BHV_TO_VNODE(bdp);
  280. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  281. if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
  282. return XFS_ERROR(EROFS);
  283. /*
  284. * Cannot set certain attributes.
  285. */
  286. mask = vap->va_mask;
  287. if (mask & XFS_AT_NOSET) {
  288. return XFS_ERROR(EINVAL);
  289. }
  290. ip = XFS_BHVTOI(bdp);
  291. mp = ip->i_mount;
  292. if (XFS_FORCED_SHUTDOWN(mp))
  293. return XFS_ERROR(EIO);
  294. /*
  295. * Timestamps do not need to be logged and hence do not
  296. * need to be done within a transaction.
  297. */
  298. if (mask & XFS_AT_UPDTIMES) {
  299. ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
  300. timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
  301. ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
  302. ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
  303. xfs_ichgtime(ip, timeflags);
  304. return 0;
  305. }
  306. olddquot1 = olddquot2 = NULL;
  307. udqp = gdqp = NULL;
  308. /*
  309. * If disk quotas is on, we make sure that the dquots do exist on disk,
  310. * before we start any other transactions. Trying to do this later
  311. * is messy. We don't care to take a readlock to look at the ids
  312. * in inode here, because we can't hold it across the trans_reserve.
  313. * If the IDs do change before we take the ilock, we're covered
  314. * because the i_*dquot fields will get updated anyway.
  315. */
  316. if (XFS_IS_QUOTA_ON(mp) &&
  317. (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
  318. uint qflags = 0;
  319. if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
  320. uid = vap->va_uid;
  321. qflags |= XFS_QMOPT_UQUOTA;
  322. } else {
  323. uid = ip->i_d.di_uid;
  324. }
  325. if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
  326. gid = vap->va_gid;
  327. qflags |= XFS_QMOPT_GQUOTA;
  328. } else {
  329. gid = ip->i_d.di_gid;
  330. }
  331. if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
  332. projid = vap->va_projid;
  333. qflags |= XFS_QMOPT_PQUOTA;
  334. } else {
  335. projid = ip->i_d.di_projid;
  336. }
  337. /*
  338. * We take a reference when we initialize udqp and gdqp,
  339. * so it is important that we never blindly double trip on
  340. * the same variable. See xfs_create() for an example.
  341. */
  342. ASSERT(udqp == NULL);
  343. ASSERT(gdqp == NULL);
  344. code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
  345. &udqp, &gdqp);
  346. if (code)
  347. return (code);
  348. }
  349. /*
  350. * For the other attributes, we acquire the inode lock and
  351. * first do an error checking pass.
  352. */
  353. tp = NULL;
  354. lock_flags = XFS_ILOCK_EXCL;
  355. ASSERT(flags & ATTR_NOLOCK ? flags & ATTR_DMI : 1);
  356. if (flags & ATTR_NOLOCK)
  357. need_iolock = 0;
  358. if (!(mask & XFS_AT_SIZE)) {
  359. if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
  360. (mp->m_flags & XFS_MOUNT_WSYNC)) {
  361. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  362. commit_flags = 0;
  363. if ((code = xfs_trans_reserve(tp, 0,
  364. XFS_ICHANGE_LOG_RES(mp), 0,
  365. 0, 0))) {
  366. lock_flags = 0;
  367. goto error_return;
  368. }
  369. }
  370. } else {
  371. if (DM_EVENT_ENABLED (vp->v_vfsp, ip, DM_EVENT_TRUNCATE) &&
  372. !(flags & ATTR_DMI)) {
  373. int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
  374. code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
  375. vap->va_size, 0, dmflags, NULL);
  376. if (code) {
  377. lock_flags = 0;
  378. goto error_return;
  379. }
  380. }
  381. if (need_iolock)
  382. lock_flags |= XFS_IOLOCK_EXCL;
  383. }
  384. xfs_ilock(ip, lock_flags);
  385. /* boolean: are we the file owner? */
  386. file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
  387. /*
  388. * Change various properties of a file.
  389. * Only the owner or users with CAP_FOWNER
  390. * capability may do these things.
  391. */
  392. if (mask &
  393. (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
  394. XFS_AT_GID|XFS_AT_PROJID)) {
  395. /*
  396. * CAP_FOWNER overrides the following restrictions:
  397. *
  398. * The user ID of the calling process must be equal
  399. * to the file owner ID, except in cases where the
  400. * CAP_FSETID capability is applicable.
  401. */
  402. if (!file_owner && !capable(CAP_FOWNER)) {
  403. code = XFS_ERROR(EPERM);
  404. goto error_return;
  405. }
  406. /*
  407. * CAP_FSETID overrides the following restrictions:
  408. *
  409. * The effective user ID of the calling process shall match
  410. * the file owner when setting the set-user-ID and
  411. * set-group-ID bits on that file.
  412. *
  413. * The effective group ID or one of the supplementary group
  414. * IDs of the calling process shall match the group owner of
  415. * the file when setting the set-group-ID bit on that file
  416. */
  417. if (mask & XFS_AT_MODE) {
  418. mode_t m = 0;
  419. if ((vap->va_mode & S_ISUID) && !file_owner)
  420. m |= S_ISUID;
  421. if ((vap->va_mode & S_ISGID) &&
  422. !in_group_p((gid_t)ip->i_d.di_gid))
  423. m |= S_ISGID;
  424. #if 0
  425. /* Linux allows this, Irix doesn't. */
  426. if ((vap->va_mode & S_ISVTX) && vp->v_type != VDIR)
  427. m |= S_ISVTX;
  428. #endif
  429. if (m && !capable(CAP_FSETID))
  430. vap->va_mode &= ~m;
  431. }
  432. }
  433. /*
  434. * Change file ownership. Must be the owner or privileged.
  435. * If the system was configured with the "restricted_chown"
  436. * option, the owner is not permitted to give away the file,
  437. * and can change the group id only to a group of which he
  438. * or she is a member.
  439. */
  440. if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
  441. /*
  442. * These IDs could have changed since we last looked at them.
  443. * But, we're assured that if the ownership did change
  444. * while we didn't have the inode locked, inode's dquot(s)
  445. * would have changed also.
  446. */
  447. iuid = ip->i_d.di_uid;
  448. iprojid = ip->i_d.di_projid;
  449. igid = ip->i_d.di_gid;
  450. gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
  451. uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
  452. projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
  453. iprojid;
  454. /*
  455. * CAP_CHOWN overrides the following restrictions:
  456. *
  457. * If _POSIX_CHOWN_RESTRICTED is defined, this capability
  458. * shall override the restriction that a process cannot
  459. * change the user ID of a file it owns and the restriction
  460. * that the group ID supplied to the chown() function
  461. * shall be equal to either the group ID or one of the
  462. * supplementary group IDs of the calling process.
  463. */
  464. if (restricted_chown &&
  465. (iuid != uid || (igid != gid &&
  466. !in_group_p((gid_t)gid))) &&
  467. !capable(CAP_CHOWN)) {
  468. code = XFS_ERROR(EPERM);
  469. goto error_return;
  470. }
  471. /*
  472. * Do a quota reservation only if uid/projid/gid is actually
  473. * going to change.
  474. */
  475. if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
  476. (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
  477. (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
  478. ASSERT(tp);
  479. code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
  480. capable(CAP_FOWNER) ?
  481. XFS_QMOPT_FORCE_RES : 0);
  482. if (code) /* out of quota */
  483. goto error_return;
  484. }
  485. }
  486. /*
  487. * Truncate file. Must have write permission and not be a directory.
  488. */
  489. if (mask & XFS_AT_SIZE) {
  490. /* Short circuit the truncate case for zero length files */
  491. if ((vap->va_size == 0) &&
  492. (ip->i_d.di_size == 0) && (ip->i_d.di_nextents == 0)) {
  493. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  494. lock_flags &= ~XFS_ILOCK_EXCL;
  495. if (mask & XFS_AT_CTIME)
  496. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  497. code = 0;
  498. goto error_return;
  499. }
  500. if (vp->v_type == VDIR) {
  501. code = XFS_ERROR(EISDIR);
  502. goto error_return;
  503. } else if (vp->v_type != VREG) {
  504. code = XFS_ERROR(EINVAL);
  505. goto error_return;
  506. }
  507. /*
  508. * Make sure that the dquots are attached to the inode.
  509. */
  510. if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
  511. goto error_return;
  512. }
  513. /*
  514. * Change file access or modified times.
  515. */
  516. if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
  517. if (!file_owner) {
  518. if ((flags & ATTR_UTIME) &&
  519. !capable(CAP_FOWNER)) {
  520. code = XFS_ERROR(EPERM);
  521. goto error_return;
  522. }
  523. }
  524. }
  525. /*
  526. * Change extent size or realtime flag.
  527. */
  528. if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
  529. /*
  530. * Can't change extent size if any extents are allocated.
  531. */
  532. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  533. (mask & XFS_AT_EXTSIZE) &&
  534. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
  535. vap->va_extsize) ) {
  536. code = XFS_ERROR(EINVAL); /* EFBIG? */
  537. goto error_return;
  538. }
  539. /*
  540. * Can't set extent size unless the file is marked, or
  541. * about to be marked as a realtime file.
  542. *
  543. * This check will be removed when fixed size extents
  544. * with buffered data writes is implemented.
  545. *
  546. */
  547. if ((mask & XFS_AT_EXTSIZE) &&
  548. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
  549. vap->va_extsize) &&
  550. (!((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
  551. ((mask & XFS_AT_XFLAGS) &&
  552. (vap->va_xflags & XFS_XFLAG_REALTIME))))) {
  553. code = XFS_ERROR(EINVAL);
  554. goto error_return;
  555. }
  556. /*
  557. * Can't change realtime flag if any extents are allocated.
  558. */
  559. if (ip->i_d.di_nextents && (mask & XFS_AT_XFLAGS) &&
  560. (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) !=
  561. (vap->va_xflags & XFS_XFLAG_REALTIME)) {
  562. code = XFS_ERROR(EINVAL); /* EFBIG? */
  563. goto error_return;
  564. }
  565. /*
  566. * Extent size must be a multiple of the appropriate block
  567. * size, if set at all.
  568. */
  569. if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
  570. xfs_extlen_t size;
  571. if ((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
  572. ((mask & XFS_AT_XFLAGS) &&
  573. (vap->va_xflags & XFS_XFLAG_REALTIME))) {
  574. size = mp->m_sb.sb_rextsize <<
  575. mp->m_sb.sb_blocklog;
  576. } else {
  577. size = mp->m_sb.sb_blocksize;
  578. }
  579. if (vap->va_extsize % size) {
  580. code = XFS_ERROR(EINVAL);
  581. goto error_return;
  582. }
  583. }
  584. /*
  585. * If realtime flag is set then must have realtime data.
  586. */
  587. if ((mask & XFS_AT_XFLAGS) &&
  588. (vap->va_xflags & XFS_XFLAG_REALTIME)) {
  589. if ((mp->m_sb.sb_rblocks == 0) ||
  590. (mp->m_sb.sb_rextsize == 0) ||
  591. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
  592. code = XFS_ERROR(EINVAL);
  593. goto error_return;
  594. }
  595. }
  596. /*
  597. * Can't modify an immutable/append-only file unless
  598. * we have appropriate permission.
  599. */
  600. if ((mask & XFS_AT_XFLAGS) &&
  601. (ip->i_d.di_flags &
  602. (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
  603. (vap->va_xflags &
  604. (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
  605. !capable(CAP_LINUX_IMMUTABLE)) {
  606. code = XFS_ERROR(EPERM);
  607. goto error_return;
  608. }
  609. }
  610. /*
  611. * Now we can make the changes. Before we join the inode
  612. * to the transaction, if XFS_AT_SIZE is set then take care of
  613. * the part of the truncation that must be done without the
  614. * inode lock. This needs to be done before joining the inode
  615. * to the transaction, because the inode cannot be unlocked
  616. * once it is a part of the transaction.
  617. */
  618. if (mask & XFS_AT_SIZE) {
  619. code = 0;
  620. if (vap->va_size > ip->i_d.di_size)
  621. code = xfs_igrow_start(ip, vap->va_size, credp);
  622. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  623. if (!code)
  624. code = xfs_itruncate_data(ip, vap->va_size);
  625. if (code) {
  626. ASSERT(tp == NULL);
  627. lock_flags &= ~XFS_ILOCK_EXCL;
  628. ASSERT(lock_flags == XFS_IOLOCK_EXCL);
  629. goto error_return;
  630. }
  631. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
  632. if ((code = xfs_trans_reserve(tp, 0,
  633. XFS_ITRUNCATE_LOG_RES(mp), 0,
  634. XFS_TRANS_PERM_LOG_RES,
  635. XFS_ITRUNCATE_LOG_COUNT))) {
  636. xfs_trans_cancel(tp, 0);
  637. if (need_iolock)
  638. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  639. return code;
  640. }
  641. commit_flags = XFS_TRANS_RELEASE_LOG_RES;
  642. xfs_ilock(ip, XFS_ILOCK_EXCL);
  643. }
  644. if (tp) {
  645. xfs_trans_ijoin(tp, ip, lock_flags);
  646. xfs_trans_ihold(tp, ip);
  647. }
  648. /* determine whether mandatory locking mode changes */
  649. mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
  650. /*
  651. * Truncate file. Must have write permission and not be a directory.
  652. */
  653. if (mask & XFS_AT_SIZE) {
  654. if (vap->va_size > ip->i_d.di_size) {
  655. xfs_igrow_finish(tp, ip, vap->va_size,
  656. !(flags & ATTR_DMI));
  657. } else if ((vap->va_size <= ip->i_d.di_size) ||
  658. ((vap->va_size == 0) && ip->i_d.di_nextents)) {
  659. /*
  660. * signal a sync transaction unless
  661. * we're truncating an already unlinked
  662. * file on a wsync filesystem
  663. */
  664. code = xfs_itruncate_finish(&tp, ip,
  665. (xfs_fsize_t)vap->va_size,
  666. XFS_DATA_FORK,
  667. ((ip->i_d.di_nlink != 0 ||
  668. !(mp->m_flags & XFS_MOUNT_WSYNC))
  669. ? 1 : 0));
  670. if (code) {
  671. goto abort_return;
  672. }
  673. }
  674. /*
  675. * Have to do this even if the file's size doesn't change.
  676. */
  677. timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  678. }
  679. /*
  680. * Change file access modes.
  681. */
  682. if (mask & XFS_AT_MODE) {
  683. ip->i_d.di_mode &= S_IFMT;
  684. ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
  685. xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
  686. timeflags |= XFS_ICHGTIME_CHG;
  687. }
  688. /*
  689. * Change file ownership. Must be the owner or privileged.
  690. * If the system was configured with the "restricted_chown"
  691. * option, the owner is not permitted to give away the file,
  692. * and can change the group id only to a group of which he
  693. * or she is a member.
  694. */
  695. if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
  696. /*
  697. * CAP_FSETID overrides the following restrictions:
  698. *
  699. * The set-user-ID and set-group-ID bits of a file will be
  700. * cleared upon successful return from chown()
  701. */
  702. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  703. !capable(CAP_FSETID)) {
  704. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  705. }
  706. /*
  707. * Change the ownerships and register quota modifications
  708. * in the transaction.
  709. */
  710. if (iuid != uid) {
  711. if (XFS_IS_UQUOTA_ON(mp)) {
  712. ASSERT(mask & XFS_AT_UID);
  713. ASSERT(udqp);
  714. olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
  715. &ip->i_udquot, udqp);
  716. }
  717. ip->i_d.di_uid = uid;
  718. }
  719. if (igid != gid) {
  720. if (XFS_IS_GQUOTA_ON(mp)) {
  721. ASSERT(!XFS_IS_PQUOTA_ON(mp));
  722. ASSERT(mask & XFS_AT_GID);
  723. ASSERT(gdqp);
  724. olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
  725. &ip->i_gdquot, gdqp);
  726. }
  727. ip->i_d.di_gid = gid;
  728. }
  729. if (iprojid != projid) {
  730. if (XFS_IS_PQUOTA_ON(mp)) {
  731. ASSERT(!XFS_IS_GQUOTA_ON(mp));
  732. ASSERT(mask & XFS_AT_PROJID);
  733. ASSERT(gdqp);
  734. olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
  735. &ip->i_gdquot, gdqp);
  736. }
  737. ip->i_d.di_projid = projid;
  738. /*
  739. * We may have to rev the inode as well as
  740. * the superblock version number since projids didn't
  741. * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
  742. */
  743. if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
  744. xfs_bump_ino_vers2(tp, ip);
  745. }
  746. xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
  747. timeflags |= XFS_ICHGTIME_CHG;
  748. }
  749. /*
  750. * Change file access or modified times.
  751. */
  752. if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
  753. if (mask & XFS_AT_ATIME) {
  754. ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
  755. ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
  756. ip->i_update_core = 1;
  757. timeflags &= ~XFS_ICHGTIME_ACC;
  758. }
  759. if (mask & XFS_AT_MTIME) {
  760. ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
  761. ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
  762. timeflags &= ~XFS_ICHGTIME_MOD;
  763. timeflags |= XFS_ICHGTIME_CHG;
  764. }
  765. if (tp && (flags & ATTR_UTIME))
  766. xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
  767. }
  768. /*
  769. * Change XFS-added attributes.
  770. */
  771. if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
  772. if (mask & XFS_AT_EXTSIZE) {
  773. /*
  774. * Converting bytes to fs blocks.
  775. */
  776. ip->i_d.di_extsize = vap->va_extsize >>
  777. mp->m_sb.sb_blocklog;
  778. }
  779. if (mask & XFS_AT_XFLAGS) {
  780. uint di_flags;
  781. /* can't set PREALLOC this way, just preserve it */
  782. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  783. if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
  784. di_flags |= XFS_DIFLAG_IMMUTABLE;
  785. if (vap->va_xflags & XFS_XFLAG_APPEND)
  786. di_flags |= XFS_DIFLAG_APPEND;
  787. if (vap->va_xflags & XFS_XFLAG_SYNC)
  788. di_flags |= XFS_DIFLAG_SYNC;
  789. if (vap->va_xflags & XFS_XFLAG_NOATIME)
  790. di_flags |= XFS_DIFLAG_NOATIME;
  791. if (vap->va_xflags & XFS_XFLAG_NODUMP)
  792. di_flags |= XFS_DIFLAG_NODUMP;
  793. if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
  794. di_flags |= XFS_DIFLAG_PROJINHERIT;
  795. if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
  796. if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
  797. di_flags |= XFS_DIFLAG_RTINHERIT;
  798. if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
  799. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  800. } else {
  801. if (vap->va_xflags & XFS_XFLAG_REALTIME) {
  802. di_flags |= XFS_DIFLAG_REALTIME;
  803. ip->i_iocore.io_flags |= XFS_IOCORE_RT;
  804. } else {
  805. ip->i_iocore.io_flags &= ~XFS_IOCORE_RT;
  806. }
  807. }
  808. ip->i_d.di_flags = di_flags;
  809. }
  810. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  811. timeflags |= XFS_ICHGTIME_CHG;
  812. }
  813. /*
  814. * Change file inode change time only if XFS_AT_CTIME set
  815. * AND we have been called by a DMI function.
  816. */
  817. if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
  818. ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
  819. ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
  820. ip->i_update_core = 1;
  821. timeflags &= ~XFS_ICHGTIME_CHG;
  822. }
  823. /*
  824. * Send out timestamp changes that need to be set to the
  825. * current time. Not done when called by a DMI function.
  826. */
  827. if (timeflags && !(flags & ATTR_DMI))
  828. xfs_ichgtime(ip, timeflags);
  829. XFS_STATS_INC(xs_ig_attrchg);
  830. /*
  831. * If this is a synchronous mount, make sure that the
  832. * transaction goes to disk before returning to the user.
  833. * This is slightly sub-optimal in that truncates require
  834. * two sync transactions instead of one for wsync filesytems.
  835. * One for the truncate and one for the timestamps since we
  836. * don't want to change the timestamps unless we're sure the
  837. * truncate worked. Truncates are less than 1% of the laddis
  838. * mix so this probably isn't worth the trouble to optimize.
  839. */
  840. code = 0;
  841. if (tp) {
  842. if (mp->m_flags & XFS_MOUNT_WSYNC)
  843. xfs_trans_set_sync(tp);
  844. code = xfs_trans_commit(tp, commit_flags, NULL);
  845. }
  846. /*
  847. * If the (regular) file's mandatory locking mode changed, then
  848. * notify the vnode. We do this under the inode lock to prevent
  849. * racing calls to vop_vnode_change.
  850. */
  851. mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
  852. if (mandlock_before != mandlock_after) {
  853. VOP_VNODE_CHANGE(vp, VCHANGE_FLAGS_ENF_LOCKING,
  854. mandlock_after);
  855. }
  856. xfs_iunlock(ip, lock_flags);
  857. /*
  858. * Release any dquot(s) the inode had kept before chown.
  859. */
  860. XFS_QM_DQRELE(mp, olddquot1);
  861. XFS_QM_DQRELE(mp, olddquot2);
  862. XFS_QM_DQRELE(mp, udqp);
  863. XFS_QM_DQRELE(mp, gdqp);
  864. if (code) {
  865. return code;
  866. }
  867. if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_ATTRIBUTE) &&
  868. !(flags & ATTR_DMI)) {
  869. (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
  870. NULL, DM_RIGHT_NULL, NULL, NULL,
  871. 0, 0, AT_DELAY_FLAG(flags));
  872. }
  873. return 0;
  874. abort_return:
  875. commit_flags |= XFS_TRANS_ABORT;
  876. /* FALLTHROUGH */
  877. error_return:
  878. XFS_QM_DQRELE(mp, udqp);
  879. XFS_QM_DQRELE(mp, gdqp);
  880. if (tp) {
  881. xfs_trans_cancel(tp, commit_flags);
  882. }
  883. if (lock_flags != 0) {
  884. xfs_iunlock(ip, lock_flags);
  885. }
  886. return code;
  887. }
  888. /*
  889. * xfs_access
  890. * Null conversion from vnode mode bits to inode mode bits, as in efs.
  891. */
  892. STATIC int
  893. xfs_access(
  894. bhv_desc_t *bdp,
  895. int mode,
  896. cred_t *credp)
  897. {
  898. xfs_inode_t *ip;
  899. int error;
  900. vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
  901. (inst_t *)__return_address);
  902. ip = XFS_BHVTOI(bdp);
  903. xfs_ilock(ip, XFS_ILOCK_SHARED);
  904. error = xfs_iaccess(ip, mode, credp);
  905. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  906. return error;
  907. }
  908. /*
  909. * xfs_readlink
  910. *
  911. */
  912. STATIC int
  913. xfs_readlink(
  914. bhv_desc_t *bdp,
  915. uio_t *uiop,
  916. int ioflags,
  917. cred_t *credp)
  918. {
  919. xfs_inode_t *ip;
  920. int count;
  921. xfs_off_t offset;
  922. int pathlen;
  923. vnode_t *vp;
  924. int error = 0;
  925. xfs_mount_t *mp;
  926. int nmaps;
  927. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  928. xfs_daddr_t d;
  929. int byte_cnt;
  930. int n;
  931. xfs_buf_t *bp;
  932. vp = BHV_TO_VNODE(bdp);
  933. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  934. ip = XFS_BHVTOI(bdp);
  935. mp = ip->i_mount;
  936. if (XFS_FORCED_SHUTDOWN(mp))
  937. return XFS_ERROR(EIO);
  938. xfs_ilock(ip, XFS_ILOCK_SHARED);
  939. ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
  940. offset = uiop->uio_offset;
  941. count = uiop->uio_resid;
  942. if (offset < 0) {
  943. error = XFS_ERROR(EINVAL);
  944. goto error_return;
  945. }
  946. if (count <= 0) {
  947. error = 0;
  948. goto error_return;
  949. }
  950. if (!(ioflags & IO_INVIS)) {
  951. xfs_ichgtime(ip, XFS_ICHGTIME_ACC);
  952. }
  953. /*
  954. * See if the symlink is stored inline.
  955. */
  956. pathlen = (int)ip->i_d.di_size;
  957. if (ip->i_df.if_flags & XFS_IFINLINE) {
  958. error = uio_read(ip->i_df.if_u1.if_data, pathlen, uiop);
  959. }
  960. else {
  961. /*
  962. * Symlink not inline. Call bmap to get it in.
  963. */
  964. nmaps = SYMLINK_MAPS;
  965. error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen),
  966. 0, NULL, 0, mval, &nmaps, NULL);
  967. if (error) {
  968. goto error_return;
  969. }
  970. for (n = 0; n < nmaps; n++) {
  971. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  972. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  973. bp = xfs_buf_read(mp->m_ddev_targp, d,
  974. BTOBB(byte_cnt), 0);
  975. error = XFS_BUF_GETERROR(bp);
  976. if (error) {
  977. xfs_ioerror_alert("xfs_readlink",
  978. ip->i_mount, bp, XFS_BUF_ADDR(bp));
  979. xfs_buf_relse(bp);
  980. goto error_return;
  981. }
  982. if (pathlen < byte_cnt)
  983. byte_cnt = pathlen;
  984. pathlen -= byte_cnt;
  985. error = uio_read(XFS_BUF_PTR(bp), byte_cnt, uiop);
  986. xfs_buf_relse (bp);
  987. }
  988. }
  989. error_return:
  990. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  991. return error;
  992. }
  993. /*
  994. * xfs_fsync
  995. *
  996. * This is called to sync the inode and its data out to disk.
  997. * We need to hold the I/O lock while flushing the data, and
  998. * the inode lock while flushing the inode. The inode lock CANNOT
  999. * be held while flushing the data, so acquire after we're done
  1000. * with that.
  1001. */
  1002. STATIC int
  1003. xfs_fsync(
  1004. bhv_desc_t *bdp,
  1005. int flag,
  1006. cred_t *credp,
  1007. xfs_off_t start,
  1008. xfs_off_t stop)
  1009. {
  1010. xfs_inode_t *ip;
  1011. xfs_trans_t *tp;
  1012. int error;
  1013. vn_trace_entry(BHV_TO_VNODE(bdp),
  1014. __FUNCTION__, (inst_t *)__return_address);
  1015. ip = XFS_BHVTOI(bdp);
  1016. ASSERT(start >= 0 && stop >= -1);
  1017. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  1018. return XFS_ERROR(EIO);
  1019. /*
  1020. * We always need to make sure that the required inode state
  1021. * is safe on disk. The vnode might be clean but because
  1022. * of committed transactions that haven't hit the disk yet.
  1023. * Likewise, there could be unflushed non-transactional
  1024. * changes to the inode core that have to go to disk.
  1025. *
  1026. * The following code depends on one assumption: that
  1027. * any transaction that changes an inode logs the core
  1028. * because it has to change some field in the inode core
  1029. * (typically nextents or nblocks). That assumption
  1030. * implies that any transactions against an inode will
  1031. * catch any non-transactional updates. If inode-altering
  1032. * transactions exist that violate this assumption, the
  1033. * code breaks. Right now, it figures that if the involved
  1034. * update_* field is clear and the inode is unpinned, the
  1035. * inode is clean. Either it's been flushed or it's been
  1036. * committed and the commit has hit the disk unpinning the inode.
  1037. * (Note that xfs_inode_item_format() called at commit clears
  1038. * the update_* fields.)
  1039. */
  1040. xfs_ilock(ip, XFS_ILOCK_SHARED);
  1041. /* If we are flushing data then we care about update_size
  1042. * being set, otherwise we care about update_core
  1043. */
  1044. if ((flag & FSYNC_DATA) ?
  1045. (ip->i_update_size == 0) :
  1046. (ip->i_update_core == 0)) {
  1047. /*
  1048. * Timestamps/size haven't changed since last inode
  1049. * flush or inode transaction commit. That means
  1050. * either nothing got written or a transaction
  1051. * committed which caught the updates. If the
  1052. * latter happened and the transaction hasn't
  1053. * hit the disk yet, the inode will be still
  1054. * be pinned. If it is, force the log.
  1055. */
  1056. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  1057. if (xfs_ipincount(ip)) {
  1058. xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
  1059. XFS_LOG_FORCE |
  1060. ((flag & FSYNC_WAIT)
  1061. ? XFS_LOG_SYNC : 0));
  1062. }
  1063. error = 0;
  1064. } else {
  1065. /*
  1066. * Kick off a transaction to log the inode
  1067. * core to get the updates. Make it
  1068. * sync if FSYNC_WAIT is passed in (which
  1069. * is done by everybody but specfs). The
  1070. * sync transaction will also force the log.
  1071. */
  1072. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  1073. tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
  1074. if ((error = xfs_trans_reserve(tp, 0,
  1075. XFS_FSYNC_TS_LOG_RES(ip->i_mount),
  1076. 0, 0, 0))) {
  1077. xfs_trans_cancel(tp, 0);
  1078. return error;
  1079. }
  1080. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1081. /*
  1082. * Note - it's possible that we might have pushed
  1083. * ourselves out of the way during trans_reserve
  1084. * which would flush the inode. But there's no
  1085. * guarantee that the inode buffer has actually
  1086. * gone out yet (it's delwri). Plus the buffer
  1087. * could be pinned anyway if it's part of an
  1088. * inode in another recent transaction. So we
  1089. * play it safe and fire off the transaction anyway.
  1090. */
  1091. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1092. xfs_trans_ihold(tp, ip);
  1093. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1094. if (flag & FSYNC_WAIT)
  1095. xfs_trans_set_sync(tp);
  1096. error = xfs_trans_commit(tp, 0, NULL);
  1097. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1098. }
  1099. return error;
  1100. }
  1101. /*
  1102. * This is called by xfs_inactive to free any blocks beyond eof,
  1103. * when the link count isn't zero.
  1104. */
  1105. STATIC int
  1106. xfs_inactive_free_eofblocks(
  1107. xfs_mount_t *mp,
  1108. xfs_inode_t *ip)
  1109. {
  1110. xfs_trans_t *tp;
  1111. int error;
  1112. xfs_fileoff_t end_fsb;
  1113. xfs_fileoff_t last_fsb;
  1114. xfs_filblks_t map_len;
  1115. int nimaps;
  1116. xfs_bmbt_irec_t imap;
  1117. /*
  1118. * Figure out if there are any blocks beyond the end
  1119. * of the file. If not, then there is nothing to do.
  1120. */
  1121. end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_d.di_size));
  1122. last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
  1123. map_len = last_fsb - end_fsb;
  1124. if (map_len <= 0)
  1125. return (0);
  1126. nimaps = 1;
  1127. xfs_ilock(ip, XFS_ILOCK_SHARED);
  1128. error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
  1129. NULL, 0, &imap, &nimaps, NULL);
  1130. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  1131. if (!error && (nimaps != 0) &&
  1132. (imap.br_startblock != HOLESTARTBLOCK)) {
  1133. /*
  1134. * Attach the dquots to the inode up front.
  1135. */
  1136. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  1137. return (error);
  1138. /*
  1139. * There are blocks after the end of file.
  1140. * Free them up now by truncating the file to
  1141. * its current size.
  1142. */
  1143. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1144. /*
  1145. * Do the xfs_itruncate_start() call before
  1146. * reserving any log space because
  1147. * itruncate_start will call into the buffer
  1148. * cache and we can't
  1149. * do that within a transaction.
  1150. */
  1151. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1152. xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
  1153. ip->i_d.di_size);
  1154. error = xfs_trans_reserve(tp, 0,
  1155. XFS_ITRUNCATE_LOG_RES(mp),
  1156. 0, XFS_TRANS_PERM_LOG_RES,
  1157. XFS_ITRUNCATE_LOG_COUNT);
  1158. if (error) {
  1159. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1160. xfs_trans_cancel(tp, 0);
  1161. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1162. return (error);
  1163. }
  1164. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1165. xfs_trans_ijoin(tp, ip,
  1166. XFS_IOLOCK_EXCL |
  1167. XFS_ILOCK_EXCL);
  1168. xfs_trans_ihold(tp, ip);
  1169. error = xfs_itruncate_finish(&tp, ip,
  1170. ip->i_d.di_size,
  1171. XFS_DATA_FORK,
  1172. 0);
  1173. /*
  1174. * If we get an error at this point we
  1175. * simply don't bother truncating the file.
  1176. */
  1177. if (error) {
  1178. xfs_trans_cancel(tp,
  1179. (XFS_TRANS_RELEASE_LOG_RES |
  1180. XFS_TRANS_ABORT));
  1181. } else {
  1182. error = xfs_trans_commit(tp,
  1183. XFS_TRANS_RELEASE_LOG_RES,
  1184. NULL);
  1185. }
  1186. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1187. }
  1188. return (error);
  1189. }
  1190. /*
  1191. * Free a symlink that has blocks associated with it.
  1192. */
  1193. STATIC int
  1194. xfs_inactive_symlink_rmt(
  1195. xfs_inode_t *ip,
  1196. xfs_trans_t **tpp)
  1197. {
  1198. xfs_buf_t *bp;
  1199. int committed;
  1200. int done;
  1201. int error;
  1202. xfs_fsblock_t first_block;
  1203. xfs_bmap_free_t free_list;
  1204. int i;
  1205. xfs_mount_t *mp;
  1206. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  1207. int nmaps;
  1208. xfs_trans_t *ntp;
  1209. int size;
  1210. xfs_trans_t *tp;
  1211. tp = *tpp;
  1212. mp = ip->i_mount;
  1213. ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
  1214. /*
  1215. * We're freeing a symlink that has some
  1216. * blocks allocated to it. Free the
  1217. * blocks here. We know that we've got
  1218. * either 1 or 2 extents and that we can
  1219. * free them all in one bunmapi call.
  1220. */
  1221. ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
  1222. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  1223. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  1224. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1225. xfs_trans_cancel(tp, 0);
  1226. *tpp = NULL;
  1227. return error;
  1228. }
  1229. /*
  1230. * Lock the inode, fix the size, and join it to the transaction.
  1231. * Hold it so in the normal path, we still have it locked for
  1232. * the second transaction. In the error paths we need it
  1233. * held so the cancel won't rele it, see below.
  1234. */
  1235. xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1236. size = (int)ip->i_d.di_size;
  1237. ip->i_d.di_size = 0;
  1238. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1239. xfs_trans_ihold(tp, ip);
  1240. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1241. /*
  1242. * Find the block(s) so we can inval and unmap them.
  1243. */
  1244. done = 0;
  1245. XFS_BMAP_INIT(&free_list, &first_block);
  1246. nmaps = sizeof(mval) / sizeof(mval[0]);
  1247. if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
  1248. XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
  1249. &free_list)))
  1250. goto error0;
  1251. /*
  1252. * Invalidate the block(s).
  1253. */
  1254. for (i = 0; i < nmaps; i++) {
  1255. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  1256. XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
  1257. XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
  1258. xfs_trans_binval(tp, bp);
  1259. }
  1260. /*
  1261. * Unmap the dead block(s) to the free_list.
  1262. */
  1263. if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
  1264. &first_block, &free_list, &done)))
  1265. goto error1;
  1266. ASSERT(done);
  1267. /*
  1268. * Commit the first transaction. This logs the EFI and the inode.
  1269. */
  1270. if ((error = xfs_bmap_finish(&tp, &free_list, first_block, &committed)))
  1271. goto error1;
  1272. /*
  1273. * The transaction must have been committed, since there were
  1274. * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
  1275. * The new tp has the extent freeing and EFDs.
  1276. */
  1277. ASSERT(committed);
  1278. /*
  1279. * The first xact was committed, so add the inode to the new one.
  1280. * Mark it dirty so it will be logged and moved forward in the log as
  1281. * part of every commit.
  1282. */
  1283. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1284. xfs_trans_ihold(tp, ip);
  1285. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1286. /*
  1287. * Get a new, empty transaction to return to our caller.
  1288. */
  1289. ntp = xfs_trans_dup(tp);
  1290. /*
  1291. * Commit the transaction containing extent freeing and EFD's.
  1292. * If we get an error on the commit here or on the reserve below,
  1293. * we need to unlock the inode since the new transaction doesn't
  1294. * have the inode attached.
  1295. */
  1296. error = xfs_trans_commit(tp, 0, NULL);
  1297. tp = ntp;
  1298. if (error) {
  1299. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1300. goto error0;
  1301. }
  1302. /*
  1303. * Remove the memory for extent descriptions (just bookkeeping).
  1304. */
  1305. if (ip->i_df.if_bytes)
  1306. xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
  1307. ASSERT(ip->i_df.if_bytes == 0);
  1308. /*
  1309. * Put an itruncate log reservation in the new transaction
  1310. * for our caller.
  1311. */
  1312. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  1313. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  1314. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1315. goto error0;
  1316. }
  1317. /*
  1318. * Return with the inode locked but not joined to the transaction.
  1319. */
  1320. *tpp = tp;
  1321. return 0;
  1322. error1:
  1323. xfs_bmap_cancel(&free_list);
  1324. error0:
  1325. /*
  1326. * Have to come here with the inode locked and either
  1327. * (held and in the transaction) or (not in the transaction).
  1328. * If the inode isn't held then cancel would iput it, but
  1329. * that's wrong since this is inactive and the vnode ref
  1330. * count is 0 already.
  1331. * Cancel won't do anything to the inode if held, but it still
  1332. * needs to be locked until the cancel is done, if it was
  1333. * joined to the transaction.
  1334. */
  1335. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1336. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1337. *tpp = NULL;
  1338. return error;
  1339. }
  1340. STATIC int
  1341. xfs_inactive_symlink_local(
  1342. xfs_inode_t *ip,
  1343. xfs_trans_t **tpp)
  1344. {
  1345. int error;
  1346. ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
  1347. /*
  1348. * We're freeing a symlink which fit into
  1349. * the inode. Just free the memory used
  1350. * to hold the old symlink.
  1351. */
  1352. error = xfs_trans_reserve(*tpp, 0,
  1353. XFS_ITRUNCATE_LOG_RES(ip->i_mount),
  1354. 0, XFS_TRANS_PERM_LOG_RES,
  1355. XFS_ITRUNCATE_LOG_COUNT);
  1356. if (error) {
  1357. xfs_trans_cancel(*tpp, 0);
  1358. *tpp = NULL;
  1359. return (error);
  1360. }
  1361. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1362. /*
  1363. * Zero length symlinks _can_ exist.
  1364. */
  1365. if (ip->i_df.if_bytes > 0) {
  1366. xfs_idata_realloc(ip,
  1367. -(ip->i_df.if_bytes),
  1368. XFS_DATA_FORK);
  1369. ASSERT(ip->i_df.if_bytes == 0);
  1370. }
  1371. return (0);
  1372. }
  1373. /*
  1374. *
  1375. */
  1376. STATIC int
  1377. xfs_inactive_attrs(
  1378. xfs_inode_t *ip,
  1379. xfs_trans_t **tpp)
  1380. {
  1381. xfs_trans_t *tp;
  1382. int error;
  1383. xfs_mount_t *mp;
  1384. ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
  1385. tp = *tpp;
  1386. mp = ip->i_mount;
  1387. ASSERT(ip->i_d.di_forkoff != 0);
  1388. xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
  1389. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1390. error = xfs_attr_inactive(ip);
  1391. if (error) {
  1392. *tpp = NULL;
  1393. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1394. return (error); /* goto out*/
  1395. }
  1396. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1397. error = xfs_trans_reserve(tp, 0,
  1398. XFS_IFREE_LOG_RES(mp),
  1399. 0, XFS_TRANS_PERM_LOG_RES,
  1400. XFS_INACTIVE_LOG_COUNT);
  1401. if (error) {
  1402. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1403. xfs_trans_cancel(tp, 0);
  1404. *tpp = NULL;
  1405. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1406. return (error);
  1407. }
  1408. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1409. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1410. xfs_trans_ihold(tp, ip);
  1411. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1412. ASSERT(ip->i_d.di_anextents == 0);
  1413. *tpp = tp;
  1414. return (0);
  1415. }
  1416. STATIC int
  1417. xfs_release(
  1418. bhv_desc_t *bdp)
  1419. {
  1420. xfs_inode_t *ip;
  1421. vnode_t *vp;
  1422. xfs_mount_t *mp;
  1423. int error;
  1424. vp = BHV_TO_VNODE(bdp);
  1425. ip = XFS_BHVTOI(bdp);
  1426. if ((vp->v_type != VREG) || (ip->i_d.di_mode == 0)) {
  1427. return 0;
  1428. }
  1429. /* If this is a read-only mount, don't do this (would generate I/O) */
  1430. if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
  1431. return 0;
  1432. #ifdef HAVE_REFCACHE
  1433. /* If we are in the NFS reference cache then don't do this now */
  1434. if (ip->i_refcache)
  1435. return 0;
  1436. #endif
  1437. mp = ip->i_mount;
  1438. if (ip->i_d.di_nlink != 0) {
  1439. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1440. ((ip->i_d.di_size > 0) || (VN_CACHED(vp) > 0)) &&
  1441. (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
  1442. (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)))) {
  1443. if ((error = xfs_inactive_free_eofblocks(mp, ip)))
  1444. return (error);
  1445. /* Update linux inode block count after free above */
  1446. LINVFS_GET_IP(vp)->i_blocks = XFS_FSB_TO_BB(mp,
  1447. ip->i_d.di_nblocks + ip->i_delayed_blks);
  1448. }
  1449. }
  1450. return 0;
  1451. }
  1452. /*
  1453. * xfs_inactive
  1454. *
  1455. * This is called when the vnode reference count for the vnode
  1456. * goes to zero. If the file has been unlinked, then it must
  1457. * now be truncated. Also, we clear all of the read-ahead state
  1458. * kept for the inode here since the file is now closed.
  1459. */
  1460. STATIC int
  1461. xfs_inactive(
  1462. bhv_desc_t *bdp,
  1463. cred_t *credp)
  1464. {
  1465. xfs_inode_t *ip;
  1466. vnode_t *vp;
  1467. xfs_bmap_free_t free_list;
  1468. xfs_fsblock_t first_block;
  1469. int committed;
  1470. xfs_trans_t *tp;
  1471. xfs_mount_t *mp;
  1472. int error;
  1473. int truncate;
  1474. vp = BHV_TO_VNODE(bdp);
  1475. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  1476. ip = XFS_BHVTOI(bdp);
  1477. /*
  1478. * If the inode is already free, then there can be nothing
  1479. * to clean up here.
  1480. */
  1481. if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
  1482. ASSERT(ip->i_df.if_real_bytes == 0);
  1483. ASSERT(ip->i_df.if_broot_bytes == 0);
  1484. return VN_INACTIVE_CACHE;
  1485. }
  1486. /*
  1487. * Only do a truncate if it's a regular file with
  1488. * some actual space in it. It's OK to look at the
  1489. * inode's fields without the lock because we're the
  1490. * only one with a reference to the inode.
  1491. */
  1492. truncate = ((ip->i_d.di_nlink == 0) &&
  1493. ((ip->i_d.di_size != 0) || (ip->i_d.di_nextents > 0)) &&
  1494. ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
  1495. mp = ip->i_mount;
  1496. if (ip->i_d.di_nlink == 0 &&
  1497. DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_DESTROY)) {
  1498. (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
  1499. }
  1500. error = 0;
  1501. /* If this is a read-only mount, don't do this (would generate I/O) */
  1502. if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
  1503. goto out;
  1504. if (ip->i_d.di_nlink != 0) {
  1505. if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
  1506. ((ip->i_d.di_size > 0) || (VN_CACHED(vp) > 0)) &&
  1507. (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
  1508. (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)) ||
  1509. (ip->i_delayed_blks != 0))) {
  1510. if ((error = xfs_inactive_free_eofblocks(mp, ip)))
  1511. return (VN_INACTIVE_CACHE);
  1512. /* Update linux inode block count after free above */
  1513. LINVFS_GET_IP(vp)->i_blocks = XFS_FSB_TO_BB(mp,
  1514. ip->i_d.di_nblocks + ip->i_delayed_blks);
  1515. }
  1516. goto out;
  1517. }
  1518. ASSERT(ip->i_d.di_nlink == 0);
  1519. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  1520. return (VN_INACTIVE_CACHE);
  1521. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  1522. if (truncate) {
  1523. /*
  1524. * Do the xfs_itruncate_start() call before
  1525. * reserving any log space because itruncate_start
  1526. * will call into the buffer cache and we can't
  1527. * do that within a transaction.
  1528. */
  1529. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1530. xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
  1531. error = xfs_trans_reserve(tp, 0,
  1532. XFS_ITRUNCATE_LOG_RES(mp),
  1533. 0, XFS_TRANS_PERM_LOG_RES,
  1534. XFS_ITRUNCATE_LOG_COUNT);
  1535. if (error) {
  1536. /* Don't call itruncate_cleanup */
  1537. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1538. xfs_trans_cancel(tp, 0);
  1539. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1540. return (VN_INACTIVE_CACHE);
  1541. }
  1542. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1543. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1544. xfs_trans_ihold(tp, ip);
  1545. /*
  1546. * normally, we have to run xfs_itruncate_finish sync.
  1547. * But if filesystem is wsync and we're in the inactive
  1548. * path, then we know that nlink == 0, and that the
  1549. * xaction that made nlink == 0 is permanently committed
  1550. * since xfs_remove runs as a synchronous transaction.
  1551. */
  1552. error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
  1553. (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
  1554. if (error) {
  1555. xfs_trans_cancel(tp,
  1556. XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1557. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1558. return (VN_INACTIVE_CACHE);
  1559. }
  1560. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
  1561. /*
  1562. * If we get an error while cleaning up a
  1563. * symlink we bail out.
  1564. */
  1565. error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
  1566. xfs_inactive_symlink_rmt(ip, &tp) :
  1567. xfs_inactive_symlink_local(ip, &tp);
  1568. if (error) {
  1569. ASSERT(tp == NULL);
  1570. return (VN_INACTIVE_CACHE);
  1571. }
  1572. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1573. xfs_trans_ihold(tp, ip);
  1574. } else {
  1575. error = xfs_trans_reserve(tp, 0,
  1576. XFS_IFREE_LOG_RES(mp),
  1577. 0, XFS_TRANS_PERM_LOG_RES,
  1578. XFS_INACTIVE_LOG_COUNT);
  1579. if (error) {
  1580. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  1581. xfs_trans_cancel(tp, 0);
  1582. return (VN_INACTIVE_CACHE);
  1583. }
  1584. xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
  1585. xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1586. xfs_trans_ihold(tp, ip);
  1587. }
  1588. /*
  1589. * If there are attributes associated with the file
  1590. * then blow them away now. The code calls a routine
  1591. * that recursively deconstructs the attribute fork.
  1592. * We need to just commit the current transaction
  1593. * because we can't use it for xfs_attr_inactive().
  1594. */
  1595. if (ip->i_d.di_anextents > 0) {
  1596. error = xfs_inactive_attrs(ip, &tp);
  1597. /*
  1598. * If we got an error, the transaction is already
  1599. * cancelled, and the inode is unlocked. Just get out.
  1600. */
  1601. if (error)
  1602. return (VN_INACTIVE_CACHE);
  1603. } else if (ip->i_afp) {
  1604. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  1605. }
  1606. /*
  1607. * Free the inode.
  1608. */
  1609. XFS_BMAP_INIT(&free_list, &first_block);
  1610. error = xfs_ifree(tp, ip, &free_list);
  1611. if (error) {
  1612. /*
  1613. * If we fail to free the inode, shut down. The cancel
  1614. * might do that, we need to make sure. Otherwise the
  1615. * inode might be lost for a long time or forever.
  1616. */
  1617. if (!XFS_FORCED_SHUTDOWN(mp)) {
  1618. cmn_err(CE_NOTE,
  1619. "xfs_inactive: xfs_ifree() returned an error = %d on %s",
  1620. error, mp->m_fsname);
  1621. xfs_force_shutdown(mp, XFS_METADATA_IO_ERROR);
  1622. }
  1623. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  1624. } else {
  1625. /*
  1626. * Credit the quota account(s). The inode is gone.
  1627. */
  1628. XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  1629. /*
  1630. * Just ignore errors at this point. There is
  1631. * nothing we can do except to try to keep going.
  1632. */
  1633. (void) xfs_bmap_finish(&tp, &free_list, first_block,
  1634. &committed);
  1635. (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
  1636. }
  1637. /*
  1638. * Release the dquots held by inode, if any.
  1639. */
  1640. XFS_QM_DQDETACH(mp, ip);
  1641. xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
  1642. out:
  1643. return VN_INACTIVE_CACHE;
  1644. }
  1645. /*
  1646. * xfs_lookup
  1647. */
  1648. STATIC int
  1649. xfs_lookup(
  1650. bhv_desc_t *dir_bdp,
  1651. vname_t *dentry,
  1652. vnode_t **vpp,
  1653. int flags,
  1654. vnode_t *rdir,
  1655. cred_t *credp)
  1656. {
  1657. xfs_inode_t *dp, *ip;
  1658. xfs_ino_t e_inum;
  1659. int error;
  1660. uint lock_mode;
  1661. vnode_t *dir_vp;
  1662. dir_vp = BHV_TO_VNODE(dir_bdp);
  1663. vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
  1664. dp = XFS_BHVTOI(dir_bdp);
  1665. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  1666. return XFS_ERROR(EIO);
  1667. lock_mode = xfs_ilock_map_shared(dp);
  1668. error = xfs_dir_lookup_int(dir_bdp, lock_mode, dentry, &e_inum, &ip);
  1669. if (!error) {
  1670. *vpp = XFS_ITOV(ip);
  1671. ITRACE(ip);
  1672. }
  1673. xfs_iunlock_map_shared(dp, lock_mode);
  1674. return error;
  1675. }
  1676. /*
  1677. * xfs_create (create a new file).
  1678. */
  1679. STATIC int
  1680. xfs_create(
  1681. bhv_desc_t *dir_bdp,
  1682. vname_t *dentry,
  1683. vattr_t *vap,
  1684. vnode_t **vpp,
  1685. cred_t *credp)
  1686. {
  1687. char *name = VNAME(dentry);
  1688. vnode_t *dir_vp;
  1689. xfs_inode_t *dp, *ip;
  1690. vnode_t *vp=NULL;
  1691. xfs_trans_t *tp;
  1692. xfs_mount_t *mp;
  1693. xfs_dev_t rdev;
  1694. int error;
  1695. xfs_bmap_free_t free_list;
  1696. xfs_fsblock_t first_block;
  1697. boolean_t dp_joined_to_trans;
  1698. int dm_event_sent = 0;
  1699. uint cancel_flags;
  1700. int committed;
  1701. xfs_prid_t prid;
  1702. struct xfs_dquot *udqp, *gdqp;
  1703. uint resblks;
  1704. int dm_di_mode;
  1705. int namelen;
  1706. ASSERT(!*vpp);
  1707. dir_vp = BHV_TO_VNODE(dir_bdp);
  1708. vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
  1709. dp = XFS_BHVTOI(dir_bdp);
  1710. mp = dp->i_mount;
  1711. dm_di_mode = vap->va_mode|VTTOIF(vap->va_type);
  1712. namelen = VNAMELEN(dentry);
  1713. if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
  1714. error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
  1715. dir_vp, DM_RIGHT_NULL, NULL,
  1716. DM_RIGHT_NULL, name, NULL,
  1717. dm_di_mode, 0, 0);
  1718. if (error)
  1719. return error;
  1720. dm_event_sent = 1;
  1721. }
  1722. if (XFS_FORCED_SHUTDOWN(mp))
  1723. return XFS_ERROR(EIO);
  1724. /* Return through std_return after this point. */
  1725. udqp = gdqp = NULL;
  1726. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  1727. prid = dp->i_d.di_projid;
  1728. else if (vap->va_mask & XFS_AT_PROJID)
  1729. prid = (xfs_prid_t)vap->va_projid;
  1730. else
  1731. prid = (xfs_prid_t)dfltprid;
  1732. /*
  1733. * Make sure that we have allocated dquot(s) on disk.
  1734. */
  1735. error = XFS_QM_DQVOPALLOC(mp, dp,
  1736. current_fsuid(credp), current_fsgid(credp), prid,
  1737. XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
  1738. if (error)
  1739. goto std_return;
  1740. ip = NULL;
  1741. dp_joined_to_trans = B_FALSE;
  1742. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  1743. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  1744. resblks = XFS_CREATE_SPACE_RES(mp, namelen);
  1745. /*
  1746. * Initially assume that the file does not exist and
  1747. * reserve the resources for that case. If that is not
  1748. * the case we'll drop the one we have and get a more
  1749. * appropriate transaction later.
  1750. */
  1751. error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
  1752. XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
  1753. if (error == ENOSPC) {
  1754. resblks = 0;
  1755. error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
  1756. XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
  1757. }
  1758. if (error) {
  1759. cancel_flags = 0;
  1760. dp = NULL;
  1761. goto error_return;
  1762. }
  1763. xfs_ilock(dp, XFS_ILOCK_EXCL);
  1764. XFS_BMAP_INIT(&free_list, &first_block);
  1765. ASSERT(ip == NULL);
  1766. /*
  1767. * Reserve disk quota and the inode.
  1768. */
  1769. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  1770. if (error)
  1771. goto error_return;
  1772. if (resblks == 0 &&
  1773. (error = XFS_DIR_CANENTER(mp, tp, dp, name, namelen)))
  1774. goto error_return;
  1775. rdev = (vap->va_mask & XFS_AT_RDEV) ? vap->va_rdev : 0;
  1776. error = xfs_dir_ialloc(&tp, dp,
  1777. MAKEIMODE(vap->va_type,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 (src_vp->v_type == VDIR)
  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|VTTOIF(vap->va_type);
  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,
  2557. MAKEIMODE(vap->va_type,vap->va_mode), 2,
  2558. 0, credp, prid, resblks > 0,
  2559. &cdp, NULL);
  2560. if (error) {
  2561. if (error == ENOSPC)
  2562. goto error_return;
  2563. goto abort_return;
  2564. }
  2565. ITRACE(cdp);
  2566. /*
  2567. * Now we add the directory inode to the transaction.
  2568. * We waited until now since xfs_dir_ialloc might start
  2569. * a new transaction. Had we joined the transaction
  2570. * earlier, the locks might have gotten released.
  2571. */
  2572. VN_HOLD(dir_vp);
  2573. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2574. dp_joined_to_trans = B_TRUE;
  2575. XFS_BMAP_INIT(&free_list, &first_block);
  2576. error = XFS_DIR_CREATENAME(mp, tp, dp, dir_name, dir_namelen,
  2577. cdp->i_ino, &first_block, &free_list,
  2578. resblks ? resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  2579. if (error) {
  2580. ASSERT(error != ENOSPC);
  2581. goto error1;
  2582. }
  2583. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2584. /*
  2585. * Bump the in memory version number of the parent directory
  2586. * so that other processes accessing it will recognize that
  2587. * the directory has changed.
  2588. */
  2589. dp->i_gen++;
  2590. error = XFS_DIR_INIT(mp, tp, cdp, dp);
  2591. if (error) {
  2592. goto error2;
  2593. }
  2594. cdp->i_gen = 1;
  2595. error = xfs_bumplink(tp, dp);
  2596. if (error) {
  2597. goto error2;
  2598. }
  2599. cvp = XFS_ITOV(cdp);
  2600. created = B_TRUE;
  2601. *vpp = cvp;
  2602. IHOLD(cdp);
  2603. /*
  2604. * Attach the dquots to the new inode and modify the icount incore.
  2605. */
  2606. XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
  2607. /*
  2608. * If this is a synchronous mount, make sure that the
  2609. * mkdir transaction goes to disk before returning to
  2610. * the user.
  2611. */
  2612. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2613. xfs_trans_set_sync(tp);
  2614. }
  2615. error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
  2616. if (error) {
  2617. IRELE(cdp);
  2618. goto error2;
  2619. }
  2620. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
  2621. XFS_QM_DQRELE(mp, udqp);
  2622. XFS_QM_DQRELE(mp, gdqp);
  2623. if (error) {
  2624. IRELE(cdp);
  2625. }
  2626. /* Fall through to std_return with error = 0 or errno from
  2627. * xfs_trans_commit. */
  2628. std_return:
  2629. if ( (created || (error != 0 && dm_event_sent != 0)) &&
  2630. DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
  2631. DM_EVENT_POSTCREATE)) {
  2632. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
  2633. dir_vp, DM_RIGHT_NULL,
  2634. created ? XFS_ITOV(cdp):NULL,
  2635. DM_RIGHT_NULL,
  2636. dir_name, NULL,
  2637. dm_di_mode, error, 0);
  2638. }
  2639. return error;
  2640. error2:
  2641. error1:
  2642. xfs_bmap_cancel(&free_list);
  2643. abort_return:
  2644. cancel_flags |= XFS_TRANS_ABORT;
  2645. error_return:
  2646. xfs_trans_cancel(tp, cancel_flags);
  2647. XFS_QM_DQRELE(mp, udqp);
  2648. XFS_QM_DQRELE(mp, gdqp);
  2649. if (!dp_joined_to_trans && (dp != NULL)) {
  2650. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  2651. }
  2652. goto std_return;
  2653. }
  2654. /*
  2655. * xfs_rmdir
  2656. *
  2657. */
  2658. STATIC int
  2659. xfs_rmdir(
  2660. bhv_desc_t *dir_bdp,
  2661. vname_t *dentry,
  2662. cred_t *credp)
  2663. {
  2664. char *name = VNAME(dentry);
  2665. xfs_inode_t *dp;
  2666. xfs_inode_t *cdp; /* child directory */
  2667. xfs_trans_t *tp;
  2668. xfs_mount_t *mp;
  2669. int error;
  2670. xfs_bmap_free_t free_list;
  2671. xfs_fsblock_t first_block;
  2672. int cancel_flags;
  2673. int committed;
  2674. vnode_t *dir_vp;
  2675. int dm_di_mode = 0;
  2676. int last_cdp_link;
  2677. int namelen;
  2678. uint resblks;
  2679. dir_vp = BHV_TO_VNODE(dir_bdp);
  2680. dp = XFS_BHVTOI(dir_bdp);
  2681. mp = dp->i_mount;
  2682. vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
  2683. if (XFS_FORCED_SHUTDOWN(XFS_BHVTOI(dir_bdp)->i_mount))
  2684. return XFS_ERROR(EIO);
  2685. namelen = VNAMELEN(dentry);
  2686. if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
  2687. error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
  2688. dir_vp, DM_RIGHT_NULL,
  2689. NULL, DM_RIGHT_NULL,
  2690. name, NULL, 0, 0, 0);
  2691. if (error)
  2692. return XFS_ERROR(error);
  2693. }
  2694. /* Return through std_return after this point. */
  2695. cdp = NULL;
  2696. /*
  2697. * We need to get a reference to cdp before we get our log
  2698. * reservation. The reason for this is that we cannot call
  2699. * xfs_iget for an inode for which we do not have a reference
  2700. * once we've acquired a log reservation. This is because the
  2701. * inode we are trying to get might be in xfs_inactive going
  2702. * for a log reservation. Since we'll have to wait for the
  2703. * inactive code to complete before returning from xfs_iget,
  2704. * we need to make sure that we don't have log space reserved
  2705. * when we call xfs_iget. Instead we get an unlocked referece
  2706. * to the inode before getting our log reservation.
  2707. */
  2708. error = xfs_get_dir_entry(dentry, &cdp);
  2709. if (error) {
  2710. REMOVE_DEBUG_TRACE(__LINE__);
  2711. goto std_return;
  2712. }
  2713. mp = dp->i_mount;
  2714. dm_di_mode = cdp->i_d.di_mode;
  2715. /*
  2716. * Get the dquots for the inodes.
  2717. */
  2718. error = XFS_QM_DQATTACH(mp, dp, 0);
  2719. if (!error && dp != cdp)
  2720. error = XFS_QM_DQATTACH(mp, cdp, 0);
  2721. if (error) {
  2722. IRELE(cdp);
  2723. REMOVE_DEBUG_TRACE(__LINE__);
  2724. goto std_return;
  2725. }
  2726. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  2727. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  2728. /*
  2729. * We try to get the real space reservation first,
  2730. * allowing for directory btree deletion(s) implying
  2731. * possible bmap insert(s). If we can't get the space
  2732. * reservation then we use 0 instead, and avoid the bmap
  2733. * btree insert(s) in the directory code by, if the bmap
  2734. * insert tries to happen, instead trimming the LAST
  2735. * block from the directory.
  2736. */
  2737. resblks = XFS_REMOVE_SPACE_RES(mp);
  2738. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  2739. XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
  2740. if (error == ENOSPC) {
  2741. resblks = 0;
  2742. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  2743. XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
  2744. }
  2745. if (error) {
  2746. ASSERT(error != ENOSPC);
  2747. cancel_flags = 0;
  2748. IRELE(cdp);
  2749. goto error_return;
  2750. }
  2751. XFS_BMAP_INIT(&free_list, &first_block);
  2752. /*
  2753. * Now lock the child directory inode and the parent directory
  2754. * inode in the proper order. This will take care of validating
  2755. * that the directory entry for the child directory inode has
  2756. * not changed while we were obtaining a log reservation.
  2757. */
  2758. error = xfs_lock_dir_and_entry(dp, dentry, cdp);
  2759. if (error) {
  2760. xfs_trans_cancel(tp, cancel_flags);
  2761. IRELE(cdp);
  2762. goto std_return;
  2763. }
  2764. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  2765. if (dp != cdp) {
  2766. /*
  2767. * Only increment the parent directory vnode count if
  2768. * we didn't bump it in looking up cdp. The only time
  2769. * we don't bump it is when we're looking up ".".
  2770. */
  2771. VN_HOLD(dir_vp);
  2772. }
  2773. ITRACE(cdp);
  2774. xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
  2775. ASSERT(cdp->i_d.di_nlink >= 2);
  2776. if (cdp->i_d.di_nlink != 2) {
  2777. error = XFS_ERROR(ENOTEMPTY);
  2778. goto error_return;
  2779. }
  2780. if (!XFS_DIR_ISEMPTY(mp, cdp)) {
  2781. error = XFS_ERROR(ENOTEMPTY);
  2782. goto error_return;
  2783. }
  2784. error = XFS_DIR_REMOVENAME(mp, tp, dp, name, namelen, cdp->i_ino,
  2785. &first_block, &free_list, resblks);
  2786. if (error) {
  2787. goto error1;
  2788. }
  2789. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  2790. /*
  2791. * Bump the in memory generation count on the parent
  2792. * directory so that other can know that it has changed.
  2793. */
  2794. dp->i_gen++;
  2795. /*
  2796. * Drop the link from cdp's "..".
  2797. */
  2798. error = xfs_droplink(tp, dp);
  2799. if (error) {
  2800. goto error1;
  2801. }
  2802. /*
  2803. * Drop the link from dp to cdp.
  2804. */
  2805. error = xfs_droplink(tp, cdp);
  2806. if (error) {
  2807. goto error1;
  2808. }
  2809. /*
  2810. * Drop the "." link from cdp to self.
  2811. */
  2812. error = xfs_droplink(tp, cdp);
  2813. if (error) {
  2814. goto error1;
  2815. }
  2816. /* Determine these before committing transaction */
  2817. last_cdp_link = (cdp)->i_d.di_nlink==0;
  2818. /*
  2819. * Take an extra ref on the child vnode so that it
  2820. * does not go to xfs_inactive() from within the commit.
  2821. */
  2822. IHOLD(cdp);
  2823. /*
  2824. * If this is a synchronous mount, make sure that the
  2825. * rmdir transaction goes to disk before returning to
  2826. * the user.
  2827. */
  2828. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  2829. xfs_trans_set_sync(tp);
  2830. }
  2831. error = xfs_bmap_finish (&tp, &free_list, first_block, &committed);
  2832. if (error) {
  2833. xfs_bmap_cancel(&free_list);
  2834. xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
  2835. XFS_TRANS_ABORT));
  2836. IRELE(cdp);
  2837. goto std_return;
  2838. }
  2839. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
  2840. if (error) {
  2841. IRELE(cdp);
  2842. goto std_return;
  2843. }
  2844. /*
  2845. * Let interposed file systems know about removed links.
  2846. */
  2847. VOP_LINK_REMOVED(XFS_ITOV(cdp), dir_vp, last_cdp_link);
  2848. IRELE(cdp);
  2849. /* Fall through to std_return with error = 0 or the errno
  2850. * from xfs_trans_commit. */
  2851. std_return:
  2852. if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_POSTREMOVE)) {
  2853. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
  2854. dir_vp, DM_RIGHT_NULL,
  2855. NULL, DM_RIGHT_NULL,
  2856. name, NULL, dm_di_mode,
  2857. error, 0);
  2858. }
  2859. return error;
  2860. error1:
  2861. xfs_bmap_cancel(&free_list);
  2862. cancel_flags |= XFS_TRANS_ABORT;
  2863. error_return:
  2864. xfs_trans_cancel(tp, cancel_flags);
  2865. goto std_return;
  2866. }
  2867. /*
  2868. * xfs_readdir
  2869. *
  2870. * Read dp's entries starting at uiop->uio_offset and translate them into
  2871. * bufsize bytes worth of struct dirents starting at bufbase.
  2872. */
  2873. STATIC int
  2874. xfs_readdir(
  2875. bhv_desc_t *dir_bdp,
  2876. uio_t *uiop,
  2877. cred_t *credp,
  2878. int *eofp)
  2879. {
  2880. xfs_inode_t *dp;
  2881. xfs_trans_t *tp = NULL;
  2882. int error = 0;
  2883. uint lock_mode;
  2884. xfs_off_t start_offset;
  2885. vn_trace_entry(BHV_TO_VNODE(dir_bdp), __FUNCTION__,
  2886. (inst_t *)__return_address);
  2887. dp = XFS_BHVTOI(dir_bdp);
  2888. if (XFS_FORCED_SHUTDOWN(dp->i_mount)) {
  2889. return XFS_ERROR(EIO);
  2890. }
  2891. lock_mode = xfs_ilock_map_shared(dp);
  2892. start_offset = uiop->uio_offset;
  2893. error = XFS_DIR_GETDENTS(dp->i_mount, tp, dp, uiop, eofp);
  2894. if (start_offset != uiop->uio_offset) {
  2895. xfs_ichgtime(dp, XFS_ICHGTIME_ACC);
  2896. }
  2897. xfs_iunlock_map_shared(dp, lock_mode);
  2898. return error;
  2899. }
  2900. /*
  2901. * xfs_symlink
  2902. *
  2903. */
  2904. STATIC int
  2905. xfs_symlink(
  2906. bhv_desc_t *dir_bdp,
  2907. vname_t *dentry,
  2908. vattr_t *vap,
  2909. char *target_path,
  2910. vnode_t **vpp,
  2911. cred_t *credp)
  2912. {
  2913. xfs_trans_t *tp;
  2914. xfs_mount_t *mp;
  2915. xfs_inode_t *dp;
  2916. xfs_inode_t *ip;
  2917. int error;
  2918. int pathlen;
  2919. xfs_bmap_free_t free_list;
  2920. xfs_fsblock_t first_block;
  2921. boolean_t dp_joined_to_trans;
  2922. vnode_t *dir_vp;
  2923. uint cancel_flags;
  2924. int committed;
  2925. xfs_fileoff_t first_fsb;
  2926. xfs_filblks_t fs_blocks;
  2927. int nmaps;
  2928. xfs_bmbt_irec_t mval[SYMLINK_MAPS];
  2929. xfs_daddr_t d;
  2930. char *cur_chunk;
  2931. int byte_cnt;
  2932. int n;
  2933. xfs_buf_t *bp;
  2934. xfs_prid_t prid;
  2935. struct xfs_dquot *udqp, *gdqp;
  2936. uint resblks;
  2937. char *link_name = VNAME(dentry);
  2938. int link_namelen;
  2939. *vpp = NULL;
  2940. dir_vp = BHV_TO_VNODE(dir_bdp);
  2941. dp = XFS_BHVTOI(dir_bdp);
  2942. dp_joined_to_trans = B_FALSE;
  2943. error = 0;
  2944. ip = NULL;
  2945. tp = NULL;
  2946. vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
  2947. mp = dp->i_mount;
  2948. if (XFS_FORCED_SHUTDOWN(mp))
  2949. return XFS_ERROR(EIO);
  2950. link_namelen = VNAMELEN(dentry);
  2951. /*
  2952. * Check component lengths of the target path name.
  2953. */
  2954. pathlen = strlen(target_path);
  2955. if (pathlen >= MAXPATHLEN) /* total string too long */
  2956. return XFS_ERROR(ENAMETOOLONG);
  2957. if (pathlen >= MAXNAMELEN) { /* is any component too long? */
  2958. int len, total;
  2959. char *path;
  2960. for(total = 0, path = target_path; total < pathlen;) {
  2961. /*
  2962. * Skip any slashes.
  2963. */
  2964. while(*path == '/') {
  2965. total++;
  2966. path++;
  2967. }
  2968. /*
  2969. * Count up to the next slash or end of path.
  2970. * Error out if the component is bigger than MAXNAMELEN.
  2971. */
  2972. for(len = 0; *path != '/' && total < pathlen;total++, path++) {
  2973. if (++len >= MAXNAMELEN) {
  2974. error = ENAMETOOLONG;
  2975. return error;
  2976. }
  2977. }
  2978. }
  2979. }
  2980. if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_SYMLINK)) {
  2981. error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
  2982. DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
  2983. link_name, target_path, 0, 0, 0);
  2984. if (error)
  2985. return error;
  2986. }
  2987. /* Return through std_return after this point. */
  2988. udqp = gdqp = NULL;
  2989. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  2990. prid = dp->i_d.di_projid;
  2991. else if (vap->va_mask & XFS_AT_PROJID)
  2992. prid = (xfs_prid_t)vap->va_projid;
  2993. else
  2994. prid = (xfs_prid_t)dfltprid;
  2995. /*
  2996. * Make sure that we have allocated dquot(s) on disk.
  2997. */
  2998. error = XFS_QM_DQVOPALLOC(mp, dp,
  2999. current_fsuid(credp), current_fsgid(credp), prid,
  3000. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  3001. if (error)
  3002. goto std_return;
  3003. tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
  3004. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  3005. /*
  3006. * The symlink will fit into the inode data fork?
  3007. * There can't be any attributes so we get the whole variable part.
  3008. */
  3009. if (pathlen <= XFS_LITINO(mp))
  3010. fs_blocks = 0;
  3011. else
  3012. fs_blocks = XFS_B_TO_FSB(mp, pathlen);
  3013. resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
  3014. error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
  3015. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  3016. if (error == ENOSPC && fs_blocks == 0) {
  3017. resblks = 0;
  3018. error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
  3019. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  3020. }
  3021. if (error) {
  3022. cancel_flags = 0;
  3023. dp = NULL;
  3024. goto error_return;
  3025. }
  3026. xfs_ilock(dp, XFS_ILOCK_EXCL);
  3027. /*
  3028. * Check whether the directory allows new symlinks or not.
  3029. */
  3030. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  3031. error = XFS_ERROR(EPERM);
  3032. goto error_return;
  3033. }
  3034. /*
  3035. * Reserve disk quota : blocks and inode.
  3036. */
  3037. error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
  3038. if (error)
  3039. goto error_return;
  3040. /*
  3041. * Check for ability to enter directory entry, if no space reserved.
  3042. */
  3043. if (resblks == 0 &&
  3044. (error = XFS_DIR_CANENTER(mp, tp, dp, link_name, link_namelen)))
  3045. goto error_return;
  3046. /*
  3047. * Initialize the bmap freelist prior to calling either
  3048. * bmapi or the directory create code.
  3049. */
  3050. XFS_BMAP_INIT(&free_list, &first_block);
  3051. /*
  3052. * Allocate an inode for the symlink.
  3053. */
  3054. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (vap->va_mode&~S_IFMT),
  3055. 1, 0, credp, prid, resblks > 0, &ip, NULL);
  3056. if (error) {
  3057. if (error == ENOSPC)
  3058. goto error_return;
  3059. goto error1;
  3060. }
  3061. ITRACE(ip);
  3062. VN_HOLD(dir_vp);
  3063. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  3064. dp_joined_to_trans = B_TRUE;
  3065. /*
  3066. * Also attach the dquot(s) to it, if applicable.
  3067. */
  3068. XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
  3069. if (resblks)
  3070. resblks -= XFS_IALLOC_SPACE_RES(mp);
  3071. /*
  3072. * If the symlink will fit into the inode, write it inline.
  3073. */
  3074. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  3075. xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
  3076. memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
  3077. ip->i_d.di_size = pathlen;
  3078. /*
  3079. * The inode was initially created in extent format.
  3080. */
  3081. ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  3082. ip->i_df.if_flags |= XFS_IFINLINE;
  3083. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  3084. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  3085. } else {
  3086. first_fsb = 0;
  3087. nmaps = SYMLINK_MAPS;
  3088. error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
  3089. XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
  3090. &first_block, resblks, mval, &nmaps,
  3091. &free_list);
  3092. if (error) {
  3093. goto error1;
  3094. }
  3095. if (resblks)
  3096. resblks -= fs_blocks;
  3097. ip->i_d.di_size = pathlen;
  3098. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3099. cur_chunk = target_path;
  3100. for (n = 0; n < nmaps; n++) {
  3101. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  3102. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  3103. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  3104. BTOBB(byte_cnt), 0);
  3105. ASSERT(bp && !XFS_BUF_GETERROR(bp));
  3106. if (pathlen < byte_cnt) {
  3107. byte_cnt = pathlen;
  3108. }
  3109. pathlen -= byte_cnt;
  3110. memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
  3111. cur_chunk += byte_cnt;
  3112. xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
  3113. }
  3114. }
  3115. /*
  3116. * Create the directory entry for the symlink.
  3117. */
  3118. error = XFS_DIR_CREATENAME(mp, tp, dp, link_name, link_namelen,
  3119. ip->i_ino, &first_block, &free_list, resblks);
  3120. if (error) {
  3121. goto error1;
  3122. }
  3123. xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  3124. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  3125. /*
  3126. * Bump the in memory version number of the parent directory
  3127. * so that other processes accessing it will recognize that
  3128. * the directory has changed.
  3129. */
  3130. dp->i_gen++;
  3131. /*
  3132. * If this is a synchronous mount, make sure that the
  3133. * symlink transaction goes to disk before returning to
  3134. * the user.
  3135. */
  3136. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  3137. xfs_trans_set_sync(tp);
  3138. }
  3139. /*
  3140. * xfs_trans_commit normally decrements the vnode ref count
  3141. * when it unlocks the inode. Since we want to return the
  3142. * vnode to the caller, we bump the vnode ref count now.
  3143. */
  3144. IHOLD(ip);
  3145. error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
  3146. if (error) {
  3147. goto error2;
  3148. }
  3149. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
  3150. XFS_QM_DQRELE(mp, udqp);
  3151. XFS_QM_DQRELE(mp, gdqp);
  3152. /* Fall through to std_return with error = 0 or errno from
  3153. * xfs_trans_commit */
  3154. std_return:
  3155. if (DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
  3156. DM_EVENT_POSTSYMLINK)) {
  3157. (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
  3158. dir_vp, DM_RIGHT_NULL,
  3159. error ? NULL : XFS_ITOV(ip),
  3160. DM_RIGHT_NULL, link_name, target_path,
  3161. 0, error, 0);
  3162. }
  3163. if (!error) {
  3164. vnode_t *vp;
  3165. ASSERT(ip);
  3166. vp = XFS_ITOV(ip);
  3167. *vpp = vp;
  3168. }
  3169. return error;
  3170. error2:
  3171. IRELE(ip);
  3172. error1:
  3173. xfs_bmap_cancel(&free_list);
  3174. cancel_flags |= XFS_TRANS_ABORT;
  3175. error_return:
  3176. xfs_trans_cancel(tp, cancel_flags);
  3177. XFS_QM_DQRELE(mp, udqp);
  3178. XFS_QM_DQRELE(mp, gdqp);
  3179. if (!dp_joined_to_trans && (dp != NULL)) {
  3180. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  3181. }
  3182. goto std_return;
  3183. }
  3184. /*
  3185. * xfs_fid2
  3186. *
  3187. * A fid routine that takes a pointer to a previously allocated
  3188. * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
  3189. */
  3190. STATIC int
  3191. xfs_fid2(
  3192. bhv_desc_t *bdp,
  3193. fid_t *fidp)
  3194. {
  3195. xfs_inode_t *ip;
  3196. xfs_fid2_t *xfid;
  3197. vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
  3198. (inst_t *)__return_address);
  3199. ASSERT(sizeof(fid_t) >= sizeof(xfs_fid2_t));
  3200. xfid = (xfs_fid2_t *)fidp;
  3201. ip = XFS_BHVTOI(bdp);
  3202. xfid->fid_len = sizeof(xfs_fid2_t) - sizeof(xfid->fid_len);
  3203. xfid->fid_pad = 0;
  3204. /*
  3205. * use memcpy because the inode is a long long and there's no
  3206. * assurance that xfid->fid_ino is properly aligned.
  3207. */
  3208. memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
  3209. xfid->fid_gen = ip->i_d.di_gen;
  3210. return 0;
  3211. }
  3212. /*
  3213. * xfs_rwlock
  3214. */
  3215. int
  3216. xfs_rwlock(
  3217. bhv_desc_t *bdp,
  3218. vrwlock_t locktype)
  3219. {
  3220. xfs_inode_t *ip;
  3221. vnode_t *vp;
  3222. vp = BHV_TO_VNODE(bdp);
  3223. if (vp->v_type == VDIR)
  3224. return 1;
  3225. ip = XFS_BHVTOI(bdp);
  3226. if (locktype == VRWLOCK_WRITE) {
  3227. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  3228. } else if (locktype == VRWLOCK_TRY_READ) {
  3229. return (xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED));
  3230. } else if (locktype == VRWLOCK_TRY_WRITE) {
  3231. return (xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL));
  3232. } else {
  3233. ASSERT((locktype == VRWLOCK_READ) ||
  3234. (locktype == VRWLOCK_WRITE_DIRECT));
  3235. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  3236. }
  3237. return 1;
  3238. }
  3239. /*
  3240. * xfs_rwunlock
  3241. */
  3242. void
  3243. xfs_rwunlock(
  3244. bhv_desc_t *bdp,
  3245. vrwlock_t locktype)
  3246. {
  3247. xfs_inode_t *ip;
  3248. vnode_t *vp;
  3249. vp = BHV_TO_VNODE(bdp);
  3250. if (vp->v_type == VDIR)
  3251. return;
  3252. ip = XFS_BHVTOI(bdp);
  3253. if (locktype == VRWLOCK_WRITE) {
  3254. /*
  3255. * In the write case, we may have added a new entry to
  3256. * the reference cache. This might store a pointer to
  3257. * an inode to be released in this inode. If it is there,
  3258. * clear the pointer and release the inode after unlocking
  3259. * this one.
  3260. */
  3261. xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
  3262. } else {
  3263. ASSERT((locktype == VRWLOCK_READ) ||
  3264. (locktype == VRWLOCK_WRITE_DIRECT));
  3265. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  3266. }
  3267. return;
  3268. }
  3269. STATIC int
  3270. xfs_inode_flush(
  3271. bhv_desc_t *bdp,
  3272. int flags)
  3273. {
  3274. xfs_inode_t *ip;
  3275. xfs_mount_t *mp;
  3276. xfs_inode_log_item_t *iip;
  3277. int error = 0;
  3278. ip = XFS_BHVTOI(bdp);
  3279. mp = ip->i_mount;
  3280. iip = ip->i_itemp;
  3281. if (XFS_FORCED_SHUTDOWN(mp))
  3282. return XFS_ERROR(EIO);
  3283. /*
  3284. * Bypass inodes which have already been cleaned by
  3285. * the inode flush clustering code inside xfs_iflush
  3286. */
  3287. if ((ip->i_update_core == 0) &&
  3288. ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
  3289. return 0;
  3290. if (flags & FLUSH_LOG) {
  3291. if (iip && iip->ili_last_lsn) {
  3292. xlog_t *log = mp->m_log;
  3293. xfs_lsn_t sync_lsn;
  3294. int s, log_flags = XFS_LOG_FORCE;
  3295. s = GRANT_LOCK(log);
  3296. sync_lsn = log->l_last_sync_lsn;
  3297. GRANT_UNLOCK(log, s);
  3298. if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) <= 0))
  3299. return 0;
  3300. if (flags & FLUSH_SYNC)
  3301. log_flags |= XFS_LOG_SYNC;
  3302. return xfs_log_force(mp, iip->ili_last_lsn, log_flags);
  3303. }
  3304. }
  3305. /*
  3306. * We make this non-blocking if the inode is contended,
  3307. * return EAGAIN to indicate to the caller that they
  3308. * did not succeed. This prevents the flush path from
  3309. * blocking on inodes inside another operation right
  3310. * now, they get caught later by xfs_sync.
  3311. */
  3312. if (flags & FLUSH_INODE) {
  3313. int flush_flags;
  3314. if (xfs_ipincount(ip))
  3315. return EAGAIN;
  3316. if (flags & FLUSH_SYNC) {
  3317. xfs_ilock(ip, XFS_ILOCK_SHARED);
  3318. xfs_iflock(ip);
  3319. } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  3320. if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
  3321. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3322. return EAGAIN;
  3323. }
  3324. } else {
  3325. return EAGAIN;
  3326. }
  3327. if (flags & FLUSH_SYNC)
  3328. flush_flags = XFS_IFLUSH_SYNC;
  3329. else
  3330. flush_flags = XFS_IFLUSH_ASYNC;
  3331. error = xfs_iflush(ip, flush_flags);
  3332. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  3333. }
  3334. return error;
  3335. }
  3336. int
  3337. xfs_set_dmattrs (
  3338. bhv_desc_t *bdp,
  3339. u_int evmask,
  3340. u_int16_t state,
  3341. cred_t *credp)
  3342. {
  3343. xfs_inode_t *ip;
  3344. xfs_trans_t *tp;
  3345. xfs_mount_t *mp;
  3346. int error;
  3347. if (!capable(CAP_SYS_ADMIN))
  3348. return XFS_ERROR(EPERM);
  3349. ip = XFS_BHVTOI(bdp);
  3350. mp = ip->i_mount;
  3351. if (XFS_FORCED_SHUTDOWN(mp))
  3352. return XFS_ERROR(EIO);
  3353. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  3354. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
  3355. if (error) {
  3356. xfs_trans_cancel(tp, 0);
  3357. return error;
  3358. }
  3359. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3360. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3361. ip->i_iocore.io_dmevmask = ip->i_d.di_dmevmask = evmask;
  3362. ip->i_iocore.io_dmstate = ip->i_d.di_dmstate = state;
  3363. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  3364. IHOLD(ip);
  3365. error = xfs_trans_commit(tp, 0, NULL);
  3366. return error;
  3367. }
  3368. /*
  3369. * xfs_reclaim
  3370. */
  3371. STATIC int
  3372. xfs_reclaim(
  3373. bhv_desc_t *bdp)
  3374. {
  3375. xfs_inode_t *ip;
  3376. vnode_t *vp;
  3377. vp = BHV_TO_VNODE(bdp);
  3378. ip = XFS_BHVTOI(bdp);
  3379. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  3380. ASSERT(!VN_MAPPED(vp));
  3381. /* bad inode, get out here ASAP */
  3382. if (VN_BAD(vp)) {
  3383. xfs_ireclaim(ip);
  3384. return 0;
  3385. }
  3386. if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
  3387. if (ip->i_d.di_size > 0) {
  3388. /*
  3389. * Flush and invalidate any data left around that is
  3390. * a part of this file.
  3391. *
  3392. * Get the inode's i/o lock so that buffers are pushed
  3393. * out while holding the proper lock. We can't hold
  3394. * the inode lock here since flushing out buffers may
  3395. * cause us to try to get the lock in xfs_strategy().
  3396. *
  3397. * We don't have to call remapf() here, because there
  3398. * cannot be any mapped file references to this vnode
  3399. * since it is being reclaimed.
  3400. */
  3401. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  3402. /*
  3403. * If we hit an IO error, we need to make sure that the
  3404. * buffer and page caches of file data for
  3405. * the file are tossed away. We don't want to use
  3406. * VOP_FLUSHINVAL_PAGES here because we don't want dirty
  3407. * pages to stay attached to the vnode, but be
  3408. * marked P_BAD. pdflush/vnode_pagebad
  3409. * hates that.
  3410. */
  3411. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  3412. VOP_FLUSHINVAL_PAGES(vp, 0, -1, FI_NONE);
  3413. } else {
  3414. VOP_TOSS_PAGES(vp, 0, -1, FI_NONE);
  3415. }
  3416. ASSERT(VN_CACHED(vp) == 0);
  3417. ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) ||
  3418. ip->i_delayed_blks == 0);
  3419. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  3420. } else if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  3421. /*
  3422. * di_size field may not be quite accurate if we're
  3423. * shutting down.
  3424. */
  3425. VOP_TOSS_PAGES(vp, 0, -1, FI_NONE);
  3426. ASSERT(VN_CACHED(vp) == 0);
  3427. }
  3428. }
  3429. /* If we have nothing to flush with this inode then complete the
  3430. * teardown now, otherwise break the link between the xfs inode
  3431. * and the linux inode and clean up the xfs inode later. This
  3432. * avoids flushing the inode to disk during the delete operation
  3433. * itself.
  3434. */
  3435. if (!ip->i_update_core && (ip->i_itemp == NULL)) {
  3436. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3437. xfs_iflock(ip);
  3438. return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
  3439. } else {
  3440. xfs_mount_t *mp = ip->i_mount;
  3441. /* Protect sync from us */
  3442. XFS_MOUNT_ILOCK(mp);
  3443. vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
  3444. list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
  3445. ip->i_flags |= XFS_IRECLAIMABLE;
  3446. XFS_MOUNT_IUNLOCK(mp);
  3447. }
  3448. return 0;
  3449. }
  3450. int
  3451. xfs_finish_reclaim(
  3452. xfs_inode_t *ip,
  3453. int locked,
  3454. int sync_mode)
  3455. {
  3456. xfs_ihash_t *ih = ip->i_hash;
  3457. vnode_t *vp = XFS_ITOV_NULL(ip);
  3458. int error;
  3459. if (vp && VN_BAD(vp))
  3460. goto reclaim;
  3461. /* The hash lock here protects a thread in xfs_iget_core from
  3462. * racing with us on linking the inode back with a vnode.
  3463. * Once we have the XFS_IRECLAIM flag set it will not touch
  3464. * us.
  3465. */
  3466. write_lock(&ih->ih_lock);
  3467. if ((ip->i_flags & XFS_IRECLAIM) ||
  3468. (!(ip->i_flags & XFS_IRECLAIMABLE) && vp == NULL)) {
  3469. write_unlock(&ih->ih_lock);
  3470. if (locked) {
  3471. xfs_ifunlock(ip);
  3472. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3473. }
  3474. return(1);
  3475. }
  3476. ip->i_flags |= XFS_IRECLAIM;
  3477. write_unlock(&ih->ih_lock);
  3478. /*
  3479. * If the inode is still dirty, then flush it out. If the inode
  3480. * is not in the AIL, then it will be OK to flush it delwri as
  3481. * long as xfs_iflush() does not keep any references to the inode.
  3482. * We leave that decision up to xfs_iflush() since it has the
  3483. * knowledge of whether it's OK to simply do a delwri flush of
  3484. * the inode or whether we need to wait until the inode is
  3485. * pulled from the AIL.
  3486. * We get the flush lock regardless, though, just to make sure
  3487. * we don't free it while it is being flushed.
  3488. */
  3489. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  3490. if (!locked) {
  3491. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3492. xfs_iflock(ip);
  3493. }
  3494. if (ip->i_update_core ||
  3495. ((ip->i_itemp != NULL) &&
  3496. (ip->i_itemp->ili_format.ilf_fields != 0))) {
  3497. error = xfs_iflush(ip, sync_mode);
  3498. /*
  3499. * If we hit an error, typically because of filesystem
  3500. * shutdown, we don't need to let vn_reclaim to know
  3501. * because we're gonna reclaim the inode anyway.
  3502. */
  3503. if (error) {
  3504. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3505. goto reclaim;
  3506. }
  3507. xfs_iflock(ip); /* synchronize with xfs_iflush_done */
  3508. }
  3509. ASSERT(ip->i_update_core == 0);
  3510. ASSERT(ip->i_itemp == NULL ||
  3511. ip->i_itemp->ili_format.ilf_fields == 0);
  3512. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3513. } else if (locked) {
  3514. /*
  3515. * We are not interested in doing an iflush if we're
  3516. * in the process of shutting down the filesystem forcibly.
  3517. * So, just reclaim the inode.
  3518. */
  3519. xfs_ifunlock(ip);
  3520. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3521. }
  3522. reclaim:
  3523. xfs_ireclaim(ip);
  3524. return 0;
  3525. }
  3526. int
  3527. xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
  3528. {
  3529. int purged;
  3530. xfs_inode_t *ip, *n;
  3531. int done = 0;
  3532. while (!done) {
  3533. purged = 0;
  3534. XFS_MOUNT_ILOCK(mp);
  3535. list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
  3536. if (noblock) {
  3537. if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
  3538. continue;
  3539. if (xfs_ipincount(ip) ||
  3540. !xfs_iflock_nowait(ip)) {
  3541. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3542. continue;
  3543. }
  3544. }
  3545. XFS_MOUNT_IUNLOCK(mp);
  3546. xfs_finish_reclaim(ip, noblock,
  3547. XFS_IFLUSH_DELWRI_ELSE_ASYNC);
  3548. purged = 1;
  3549. break;
  3550. }
  3551. done = !purged;
  3552. }
  3553. XFS_MOUNT_IUNLOCK(mp);
  3554. return 0;
  3555. }
  3556. /*
  3557. * xfs_alloc_file_space()
  3558. * This routine allocates disk space for the given file.
  3559. *
  3560. * If alloc_type == 0, this request is for an ALLOCSP type
  3561. * request which will change the file size. In this case, no
  3562. * DMAPI event will be generated by the call. A TRUNCATE event
  3563. * will be generated later by xfs_setattr.
  3564. *
  3565. * If alloc_type != 0, this request is for a RESVSP type
  3566. * request, and a DMAPI DM_EVENT_WRITE will be generated if the
  3567. * lower block boundary byte address is less than the file's
  3568. * length.
  3569. *
  3570. * RETURNS:
  3571. * 0 on success
  3572. * errno on error
  3573. *
  3574. */
  3575. STATIC int
  3576. xfs_alloc_file_space(
  3577. xfs_inode_t *ip,
  3578. xfs_off_t offset,
  3579. xfs_off_t len,
  3580. int alloc_type,
  3581. int attr_flags)
  3582. {
  3583. xfs_filblks_t allocated_fsb;
  3584. xfs_filblks_t allocatesize_fsb;
  3585. int committed;
  3586. xfs_off_t count;
  3587. xfs_filblks_t datablocks;
  3588. int error;
  3589. xfs_fsblock_t firstfsb;
  3590. xfs_bmap_free_t free_list;
  3591. xfs_bmbt_irec_t *imapp;
  3592. xfs_bmbt_irec_t imaps[1];
  3593. xfs_mount_t *mp;
  3594. int numrtextents;
  3595. int reccount;
  3596. uint resblks;
  3597. int rt;
  3598. int rtextsize;
  3599. xfs_fileoff_t startoffset_fsb;
  3600. xfs_trans_t *tp;
  3601. int xfs_bmapi_flags;
  3602. vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
  3603. mp = ip->i_mount;
  3604. if (XFS_FORCED_SHUTDOWN(mp))
  3605. return XFS_ERROR(EIO);
  3606. /*
  3607. * determine if this is a realtime file
  3608. */
  3609. if ((rt = XFS_IS_REALTIME_INODE(ip)) != 0) {
  3610. if (ip->i_d.di_extsize)
  3611. rtextsize = ip->i_d.di_extsize;
  3612. else
  3613. rtextsize = mp->m_sb.sb_rextsize;
  3614. } else
  3615. rtextsize = 0;
  3616. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  3617. return error;
  3618. if (len <= 0)
  3619. return XFS_ERROR(EINVAL);
  3620. count = len;
  3621. error = 0;
  3622. imapp = &imaps[0];
  3623. reccount = 1;
  3624. xfs_bmapi_flags = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
  3625. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  3626. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  3627. /* Generate a DMAPI event if needed. */
  3628. if (alloc_type != 0 && offset < ip->i_d.di_size &&
  3629. (attr_flags&ATTR_DMI) == 0 &&
  3630. DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
  3631. xfs_off_t end_dmi_offset;
  3632. end_dmi_offset = offset+len;
  3633. if (end_dmi_offset > ip->i_d.di_size)
  3634. end_dmi_offset = ip->i_d.di_size;
  3635. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
  3636. offset, end_dmi_offset - offset,
  3637. 0, NULL);
  3638. if (error)
  3639. return(error);
  3640. }
  3641. /*
  3642. * allocate file space until done or until there is an error
  3643. */
  3644. retry:
  3645. while (allocatesize_fsb && !error) {
  3646. /*
  3647. * determine if reserving space on
  3648. * the data or realtime partition.
  3649. */
  3650. if (rt) {
  3651. xfs_fileoff_t s, e;
  3652. s = startoffset_fsb;
  3653. do_div(s, rtextsize);
  3654. s *= rtextsize;
  3655. e = roundup_64(startoffset_fsb + allocatesize_fsb,
  3656. rtextsize);
  3657. numrtextents = (int)(e - s) / mp->m_sb.sb_rextsize;
  3658. datablocks = 0;
  3659. } else {
  3660. datablocks = allocatesize_fsb;
  3661. numrtextents = 0;
  3662. }
  3663. /*
  3664. * allocate and setup the transaction
  3665. */
  3666. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  3667. resblks = XFS_DIOSTRAT_SPACE_RES(mp, datablocks);
  3668. error = xfs_trans_reserve(tp,
  3669. resblks,
  3670. XFS_WRITE_LOG_RES(mp),
  3671. numrtextents,
  3672. XFS_TRANS_PERM_LOG_RES,
  3673. XFS_WRITE_LOG_COUNT);
  3674. /*
  3675. * check for running out of space
  3676. */
  3677. if (error) {
  3678. /*
  3679. * Free the transaction structure.
  3680. */
  3681. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  3682. xfs_trans_cancel(tp, 0);
  3683. break;
  3684. }
  3685. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3686. error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
  3687. ip->i_udquot, ip->i_gdquot, resblks, 0, 0);
  3688. if (error)
  3689. goto error1;
  3690. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3691. xfs_trans_ihold(tp, ip);
  3692. /*
  3693. * issue the bmapi() call to allocate the blocks
  3694. */
  3695. XFS_BMAP_INIT(&free_list, &firstfsb);
  3696. error = xfs_bmapi(tp, ip, startoffset_fsb,
  3697. allocatesize_fsb, xfs_bmapi_flags,
  3698. &firstfsb, 0, imapp, &reccount,
  3699. &free_list);
  3700. if (error) {
  3701. goto error0;
  3702. }
  3703. /*
  3704. * complete the transaction
  3705. */
  3706. error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
  3707. if (error) {
  3708. goto error0;
  3709. }
  3710. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
  3711. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3712. if (error) {
  3713. break;
  3714. }
  3715. allocated_fsb = imapp->br_blockcount;
  3716. if (reccount == 0) {
  3717. error = XFS_ERROR(ENOSPC);
  3718. break;
  3719. }
  3720. startoffset_fsb += allocated_fsb;
  3721. allocatesize_fsb -= allocated_fsb;
  3722. }
  3723. dmapi_enospc_check:
  3724. if (error == ENOSPC && (attr_flags&ATTR_DMI) == 0 &&
  3725. DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_NOSPACE)) {
  3726. error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
  3727. XFS_ITOV(ip), DM_RIGHT_NULL,
  3728. XFS_ITOV(ip), DM_RIGHT_NULL,
  3729. NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
  3730. if (error == 0)
  3731. goto retry; /* Maybe DMAPI app. has made space */
  3732. /* else fall through with error from XFS_SEND_DATA */
  3733. }
  3734. return error;
  3735. error0:
  3736. xfs_bmap_cancel(&free_list);
  3737. error1:
  3738. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  3739. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3740. goto dmapi_enospc_check;
  3741. }
  3742. /*
  3743. * Zero file bytes between startoff and endoff inclusive.
  3744. * The iolock is held exclusive and no blocks are buffered.
  3745. */
  3746. STATIC int
  3747. xfs_zero_remaining_bytes(
  3748. xfs_inode_t *ip,
  3749. xfs_off_t startoff,
  3750. xfs_off_t endoff)
  3751. {
  3752. xfs_bmbt_irec_t imap;
  3753. xfs_fileoff_t offset_fsb;
  3754. xfs_off_t lastoffset;
  3755. xfs_off_t offset;
  3756. xfs_buf_t *bp;
  3757. xfs_mount_t *mp = ip->i_mount;
  3758. int nimap;
  3759. int error = 0;
  3760. bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
  3761. ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
  3762. mp->m_rtdev_targp : mp->m_ddev_targp);
  3763. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  3764. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  3765. nimap = 1;
  3766. error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0, NULL, 0, &imap,
  3767. &nimap, NULL);
  3768. if (error || nimap < 1)
  3769. break;
  3770. ASSERT(imap.br_blockcount >= 1);
  3771. ASSERT(imap.br_startoff == offset_fsb);
  3772. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  3773. if (lastoffset > endoff)
  3774. lastoffset = endoff;
  3775. if (imap.br_startblock == HOLESTARTBLOCK)
  3776. continue;
  3777. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3778. if (imap.br_state == XFS_EXT_UNWRITTEN)
  3779. continue;
  3780. XFS_BUF_UNDONE(bp);
  3781. XFS_BUF_UNWRITE(bp);
  3782. XFS_BUF_READ(bp);
  3783. XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
  3784. xfsbdstrat(mp, bp);
  3785. if ((error = xfs_iowait(bp))) {
  3786. xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
  3787. mp, bp, XFS_BUF_ADDR(bp));
  3788. break;
  3789. }
  3790. memset(XFS_BUF_PTR(bp) +
  3791. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  3792. 0, lastoffset - offset + 1);
  3793. XFS_BUF_UNDONE(bp);
  3794. XFS_BUF_UNREAD(bp);
  3795. XFS_BUF_WRITE(bp);
  3796. xfsbdstrat(mp, bp);
  3797. if ((error = xfs_iowait(bp))) {
  3798. xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
  3799. mp, bp, XFS_BUF_ADDR(bp));
  3800. break;
  3801. }
  3802. }
  3803. xfs_buf_free(bp);
  3804. return error;
  3805. }
  3806. /*
  3807. * xfs_free_file_space()
  3808. * This routine frees disk space for the given file.
  3809. *
  3810. * This routine is only called by xfs_change_file_space
  3811. * for an UNRESVSP type call.
  3812. *
  3813. * RETURNS:
  3814. * 0 on success
  3815. * errno on error
  3816. *
  3817. */
  3818. STATIC int
  3819. xfs_free_file_space(
  3820. xfs_inode_t *ip,
  3821. xfs_off_t offset,
  3822. xfs_off_t len,
  3823. int attr_flags)
  3824. {
  3825. vnode_t *vp;
  3826. int committed;
  3827. int done;
  3828. xfs_off_t end_dmi_offset;
  3829. xfs_fileoff_t endoffset_fsb;
  3830. int error;
  3831. xfs_fsblock_t firstfsb;
  3832. xfs_bmap_free_t free_list;
  3833. xfs_off_t ilen;
  3834. xfs_bmbt_irec_t imap;
  3835. xfs_off_t ioffset;
  3836. xfs_extlen_t mod=0;
  3837. xfs_mount_t *mp;
  3838. int nimap;
  3839. uint resblks;
  3840. int rounding;
  3841. int rt;
  3842. xfs_fileoff_t startoffset_fsb;
  3843. xfs_trans_t *tp;
  3844. int need_iolock = 1;
  3845. vp = XFS_ITOV(ip);
  3846. mp = ip->i_mount;
  3847. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  3848. if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
  3849. return error;
  3850. error = 0;
  3851. if (len <= 0) /* if nothing being freed */
  3852. return error;
  3853. rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
  3854. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  3855. end_dmi_offset = offset + len;
  3856. endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
  3857. if (offset < ip->i_d.di_size &&
  3858. (attr_flags & ATTR_DMI) == 0 &&
  3859. DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
  3860. if (end_dmi_offset > ip->i_d.di_size)
  3861. end_dmi_offset = ip->i_d.di_size;
  3862. error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
  3863. offset, end_dmi_offset - offset,
  3864. AT_DELAY_FLAG(attr_flags), NULL);
  3865. if (error)
  3866. return(error);
  3867. }
  3868. ASSERT(attr_flags & ATTR_NOLOCK ? attr_flags & ATTR_DMI : 1);
  3869. if (attr_flags & ATTR_NOLOCK)
  3870. need_iolock = 0;
  3871. if (need_iolock)
  3872. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  3873. rounding = MAX((__uint8_t)(1 << mp->m_sb.sb_blocklog),
  3874. (__uint8_t)NBPP);
  3875. ilen = len + (offset & (rounding - 1));
  3876. ioffset = offset & ~(rounding - 1);
  3877. if (ilen & (rounding - 1))
  3878. ilen = (ilen + rounding) & ~(rounding - 1);
  3879. if (VN_CACHED(vp) != 0) {
  3880. xfs_inval_cached_trace(&ip->i_iocore, ioffset, -1,
  3881. ctooff(offtoct(ioffset)), -1);
  3882. VOP_FLUSHINVAL_PAGES(vp, ctooff(offtoct(ioffset)),
  3883. -1, FI_REMAPF_LOCKED);
  3884. }
  3885. /*
  3886. * Need to zero the stuff we're not freeing, on disk.
  3887. * If its a realtime file & can't use unwritten extents then we
  3888. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  3889. * will take care of it for us.
  3890. */
  3891. if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
  3892. nimap = 1;
  3893. error = xfs_bmapi(NULL, ip, startoffset_fsb, 1, 0, NULL, 0,
  3894. &imap, &nimap, NULL);
  3895. if (error)
  3896. goto out_unlock_iolock;
  3897. ASSERT(nimap == 0 || nimap == 1);
  3898. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  3899. xfs_daddr_t block;
  3900. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3901. block = imap.br_startblock;
  3902. mod = do_div(block, mp->m_sb.sb_rextsize);
  3903. if (mod)
  3904. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  3905. }
  3906. nimap = 1;
  3907. error = xfs_bmapi(NULL, ip, endoffset_fsb - 1, 1, 0, NULL, 0,
  3908. &imap, &nimap, NULL);
  3909. if (error)
  3910. goto out_unlock_iolock;
  3911. ASSERT(nimap == 0 || nimap == 1);
  3912. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  3913. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  3914. mod++;
  3915. if (mod && (mod != mp->m_sb.sb_rextsize))
  3916. endoffset_fsb -= mod;
  3917. }
  3918. }
  3919. if ((done = (endoffset_fsb <= startoffset_fsb)))
  3920. /*
  3921. * One contiguous piece to clear
  3922. */
  3923. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  3924. else {
  3925. /*
  3926. * Some full blocks, possibly two pieces to clear
  3927. */
  3928. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  3929. error = xfs_zero_remaining_bytes(ip, offset,
  3930. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  3931. if (!error &&
  3932. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  3933. error = xfs_zero_remaining_bytes(ip,
  3934. XFS_FSB_TO_B(mp, endoffset_fsb),
  3935. offset + len - 1);
  3936. }
  3937. /*
  3938. * free file space until done or until there is an error
  3939. */
  3940. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  3941. while (!error && !done) {
  3942. /*
  3943. * allocate and setup the transaction
  3944. */
  3945. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  3946. error = xfs_trans_reserve(tp,
  3947. resblks,
  3948. XFS_WRITE_LOG_RES(mp),
  3949. 0,
  3950. XFS_TRANS_PERM_LOG_RES,
  3951. XFS_WRITE_LOG_COUNT);
  3952. /*
  3953. * check for running out of space
  3954. */
  3955. if (error) {
  3956. /*
  3957. * Free the transaction structure.
  3958. */
  3959. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  3960. xfs_trans_cancel(tp, 0);
  3961. break;
  3962. }
  3963. xfs_ilock(ip, XFS_ILOCK_EXCL);
  3964. error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
  3965. ip->i_udquot, ip->i_gdquot, resblks, 0, rt ?
  3966. XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
  3967. if (error)
  3968. goto error1;
  3969. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  3970. xfs_trans_ihold(tp, ip);
  3971. /*
  3972. * issue the bunmapi() call to free the blocks
  3973. */
  3974. XFS_BMAP_INIT(&free_list, &firstfsb);
  3975. error = xfs_bunmapi(tp, ip, startoffset_fsb,
  3976. endoffset_fsb - startoffset_fsb,
  3977. 0, 2, &firstfsb, &free_list, &done);
  3978. if (error) {
  3979. goto error0;
  3980. }
  3981. /*
  3982. * complete the transaction
  3983. */
  3984. error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
  3985. if (error) {
  3986. goto error0;
  3987. }
  3988. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
  3989. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  3990. }
  3991. out_unlock_iolock:
  3992. if (need_iolock)
  3993. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  3994. return error;
  3995. error0:
  3996. xfs_bmap_cancel(&free_list);
  3997. error1:
  3998. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  3999. xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
  4000. XFS_ILOCK_EXCL);
  4001. return error;
  4002. }
  4003. /*
  4004. * xfs_change_file_space()
  4005. * This routine allocates or frees disk space for the given file.
  4006. * The user specified parameters are checked for alignment and size
  4007. * limitations.
  4008. *
  4009. * RETURNS:
  4010. * 0 on success
  4011. * errno on error
  4012. *
  4013. */
  4014. int
  4015. xfs_change_file_space(
  4016. bhv_desc_t *bdp,
  4017. int cmd,
  4018. xfs_flock64_t *bf,
  4019. xfs_off_t offset,
  4020. cred_t *credp,
  4021. int attr_flags)
  4022. {
  4023. int clrprealloc;
  4024. int error;
  4025. xfs_fsize_t fsize;
  4026. xfs_inode_t *ip;
  4027. xfs_mount_t *mp;
  4028. int setprealloc;
  4029. xfs_off_t startoffset;
  4030. xfs_off_t llen;
  4031. xfs_trans_t *tp;
  4032. vattr_t va;
  4033. vnode_t *vp;
  4034. vp = BHV_TO_VNODE(bdp);
  4035. vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
  4036. ip = XFS_BHVTOI(bdp);
  4037. mp = ip->i_mount;
  4038. /*
  4039. * must be a regular file and have write permission
  4040. */
  4041. if (vp->v_type != VREG)
  4042. return XFS_ERROR(EINVAL);
  4043. xfs_ilock(ip, XFS_ILOCK_SHARED);
  4044. if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
  4045. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  4046. return error;
  4047. }
  4048. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  4049. switch (bf->l_whence) {
  4050. case 0: /*SEEK_SET*/
  4051. break;
  4052. case 1: /*SEEK_CUR*/
  4053. bf->l_start += offset;
  4054. break;
  4055. case 2: /*SEEK_END*/
  4056. bf->l_start += ip->i_d.di_size;
  4057. break;
  4058. default:
  4059. return XFS_ERROR(EINVAL);
  4060. }
  4061. llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
  4062. if ( (bf->l_start < 0)
  4063. || (bf->l_start > XFS_MAXIOFFSET(mp))
  4064. || (bf->l_start + llen < 0)
  4065. || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
  4066. return XFS_ERROR(EINVAL);
  4067. bf->l_whence = 0;
  4068. startoffset = bf->l_start;
  4069. fsize = ip->i_d.di_size;
  4070. /*
  4071. * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
  4072. * file space.
  4073. * These calls do NOT zero the data space allocated to the file,
  4074. * nor do they change the file size.
  4075. *
  4076. * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
  4077. * space.
  4078. * These calls cause the new file data to be zeroed and the file
  4079. * size to be changed.
  4080. */
  4081. setprealloc = clrprealloc = 0;
  4082. switch (cmd) {
  4083. case XFS_IOC_RESVSP:
  4084. case XFS_IOC_RESVSP64:
  4085. error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
  4086. 1, attr_flags);
  4087. if (error)
  4088. return error;
  4089. setprealloc = 1;
  4090. break;
  4091. case XFS_IOC_UNRESVSP:
  4092. case XFS_IOC_UNRESVSP64:
  4093. if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
  4094. attr_flags)))
  4095. return error;
  4096. break;
  4097. case XFS_IOC_ALLOCSP:
  4098. case XFS_IOC_ALLOCSP64:
  4099. case XFS_IOC_FREESP:
  4100. case XFS_IOC_FREESP64:
  4101. if (startoffset > fsize) {
  4102. error = xfs_alloc_file_space(ip, fsize,
  4103. startoffset - fsize, 0, attr_flags);
  4104. if (error)
  4105. break;
  4106. }
  4107. va.va_mask = XFS_AT_SIZE;
  4108. va.va_size = startoffset;
  4109. error = xfs_setattr(bdp, &va, attr_flags, credp);
  4110. if (error)
  4111. return error;
  4112. clrprealloc = 1;
  4113. break;
  4114. default:
  4115. ASSERT(0);
  4116. return XFS_ERROR(EINVAL);
  4117. }
  4118. /*
  4119. * update the inode timestamp, mode, and prealloc flag bits
  4120. */
  4121. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  4122. if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
  4123. 0, 0, 0))) {
  4124. /* ASSERT(0); */
  4125. xfs_trans_cancel(tp, 0);
  4126. return error;
  4127. }
  4128. xfs_ilock(ip, XFS_ILOCK_EXCL);
  4129. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  4130. xfs_trans_ihold(tp, ip);
  4131. if ((attr_flags & ATTR_DMI) == 0) {
  4132. ip->i_d.di_mode &= ~S_ISUID;
  4133. /*
  4134. * Note that we don't have to worry about mandatory
  4135. * file locking being disabled here because we only
  4136. * clear the S_ISGID bit if the Group execute bit is
  4137. * on, but if it was on then mandatory locking wouldn't
  4138. * have been enabled.
  4139. */
  4140. if (ip->i_d.di_mode & S_IXGRP)
  4141. ip->i_d.di_mode &= ~S_ISGID;
  4142. xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  4143. }
  4144. if (setprealloc)
  4145. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  4146. else if (clrprealloc)
  4147. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  4148. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  4149. xfs_trans_set_sync(tp);
  4150. error = xfs_trans_commit(tp, 0, NULL);
  4151. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  4152. return error;
  4153. }
  4154. vnodeops_t xfs_vnodeops = {
  4155. BHV_IDENTITY_INIT(VN_BHV_XFS,VNODE_POSITION_XFS),
  4156. .vop_open = xfs_open,
  4157. .vop_read = xfs_read,
  4158. #ifdef HAVE_SENDFILE
  4159. .vop_sendfile = xfs_sendfile,
  4160. #endif
  4161. .vop_write = xfs_write,
  4162. .vop_ioctl = xfs_ioctl,
  4163. .vop_getattr = xfs_getattr,
  4164. .vop_setattr = xfs_setattr,
  4165. .vop_access = xfs_access,
  4166. .vop_lookup = xfs_lookup,
  4167. .vop_create = xfs_create,
  4168. .vop_remove = xfs_remove,
  4169. .vop_link = xfs_link,
  4170. .vop_rename = xfs_rename,
  4171. .vop_mkdir = xfs_mkdir,
  4172. .vop_rmdir = xfs_rmdir,
  4173. .vop_readdir = xfs_readdir,
  4174. .vop_symlink = xfs_symlink,
  4175. .vop_readlink = xfs_readlink,
  4176. .vop_fsync = xfs_fsync,
  4177. .vop_inactive = xfs_inactive,
  4178. .vop_fid2 = xfs_fid2,
  4179. .vop_rwlock = xfs_rwlock,
  4180. .vop_rwunlock = xfs_rwunlock,
  4181. .vop_bmap = xfs_bmap,
  4182. .vop_reclaim = xfs_reclaim,
  4183. .vop_attr_get = xfs_attr_get,
  4184. .vop_attr_set = xfs_attr_set,
  4185. .vop_attr_remove = xfs_attr_remove,
  4186. .vop_attr_list = xfs_attr_list,
  4187. .vop_link_removed = (vop_link_removed_t)fs_noval,
  4188. .vop_vnode_change = (vop_vnode_change_t)fs_noval,
  4189. .vop_tosspages = fs_tosspages,
  4190. .vop_flushinval_pages = fs_flushinval_pages,
  4191. .vop_flush_pages = fs_flush_pages,
  4192. .vop_release = xfs_release,
  4193. .vop_iflush = xfs_inode_flush,
  4194. };