xfs_vnodeops.c 113 KB

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