xfs_vnodeops.c 112 KB

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