xfs_vnodeops.c 99 KB

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