xfs_vnodeops.c 101 KB

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