xfs_vnodeops.c 85 KB

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