xfs_vnodeops.c 117 KB

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