xfs_vnodeops.c 105 KB

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