xfs_vnodeops.c 115 KB

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