xfs_vnodeops.c 99 KB

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