xfs_vnodeops.c 104 KB

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