xfs_vnodeops.c 112 KB

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