xfs_vnodeops.c 99 KB

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