xfs_vnodeops.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * Copyright (c) 2012 Red Hat, Inc.
  4. * All Rights Reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_format.h"
  22. #include "xfs_bit.h"
  23. #include "xfs_log.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_dir2_format.h"
  30. #include "xfs_dir2.h"
  31. #include "xfs_bmap_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_dinode.h"
  34. #include "xfs_inode.h"
  35. #include "xfs_inode_item.h"
  36. #include "xfs_itable.h"
  37. #include "xfs_ialloc.h"
  38. #include "xfs_alloc.h"
  39. #include "xfs_bmap.h"
  40. #include "xfs_bmap_util.h"
  41. #include "xfs_acl.h"
  42. #include "xfs_attr.h"
  43. #include "xfs_error.h"
  44. #include "xfs_quota.h"
  45. #include "xfs_utils.h"
  46. #include "xfs_rtalloc.h"
  47. #include "xfs_trans_space.h"
  48. #include "xfs_log_priv.h"
  49. #include "xfs_filestream.h"
  50. #include "xfs_vnodeops.h"
  51. #include "xfs_trace.h"
  52. #include "xfs_icache.h"
  53. #include "xfs_symlink.h"
  54. /*
  55. * This is called by xfs_inactive to free any blocks beyond eof
  56. * when the link count isn't zero and by xfs_dm_punch_hole() when
  57. * punching a hole to EOF.
  58. */
  59. int
  60. xfs_free_eofblocks(
  61. xfs_mount_t *mp,
  62. xfs_inode_t *ip,
  63. bool need_iolock)
  64. {
  65. xfs_trans_t *tp;
  66. int error;
  67. xfs_fileoff_t end_fsb;
  68. xfs_fileoff_t last_fsb;
  69. xfs_filblks_t map_len;
  70. int nimaps;
  71. xfs_bmbt_irec_t imap;
  72. /*
  73. * Figure out if there are any blocks beyond the end
  74. * of the file. If not, then there is nothing to do.
  75. */
  76. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
  77. last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
  78. if (last_fsb <= end_fsb)
  79. return 0;
  80. map_len = last_fsb - end_fsb;
  81. nimaps = 1;
  82. xfs_ilock(ip, XFS_ILOCK_SHARED);
  83. error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
  84. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  85. if (!error && (nimaps != 0) &&
  86. (imap.br_startblock != HOLESTARTBLOCK ||
  87. ip->i_delayed_blks)) {
  88. /*
  89. * Attach the dquots to the inode up front.
  90. */
  91. error = xfs_qm_dqattach(ip, 0);
  92. if (error)
  93. return error;
  94. /*
  95. * There are blocks after the end of file.
  96. * Free them up now by truncating the file to
  97. * its current size.
  98. */
  99. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  100. if (need_iolock) {
  101. if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
  102. xfs_trans_cancel(tp, 0);
  103. return EAGAIN;
  104. }
  105. }
  106. error = xfs_trans_reserve(tp, 0,
  107. XFS_ITRUNCATE_LOG_RES(mp),
  108. 0, XFS_TRANS_PERM_LOG_RES,
  109. XFS_ITRUNCATE_LOG_COUNT);
  110. if (error) {
  111. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  112. xfs_trans_cancel(tp, 0);
  113. if (need_iolock)
  114. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  115. return error;
  116. }
  117. xfs_ilock(ip, XFS_ILOCK_EXCL);
  118. xfs_trans_ijoin(tp, ip, 0);
  119. /*
  120. * Do not update the on-disk file size. If we update the
  121. * on-disk file size and then the system crashes before the
  122. * contents of the file are flushed to disk then the files
  123. * may be full of holes (ie NULL files bug).
  124. */
  125. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK,
  126. XFS_ISIZE(ip));
  127. if (error) {
  128. /*
  129. * If we get an error at this point we simply don't
  130. * bother truncating the file.
  131. */
  132. xfs_trans_cancel(tp,
  133. (XFS_TRANS_RELEASE_LOG_RES |
  134. XFS_TRANS_ABORT));
  135. } else {
  136. error = xfs_trans_commit(tp,
  137. XFS_TRANS_RELEASE_LOG_RES);
  138. if (!error)
  139. xfs_inode_clear_eofblocks_tag(ip);
  140. }
  141. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  142. if (need_iolock)
  143. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  144. }
  145. return error;
  146. }
  147. int
  148. xfs_release(
  149. xfs_inode_t *ip)
  150. {
  151. xfs_mount_t *mp = ip->i_mount;
  152. int error;
  153. if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
  154. return 0;
  155. /* If this is a read-only mount, don't do this (would generate I/O) */
  156. if (mp->m_flags & XFS_MOUNT_RDONLY)
  157. return 0;
  158. if (!XFS_FORCED_SHUTDOWN(mp)) {
  159. int truncated;
  160. /*
  161. * If we are using filestreams, and we have an unlinked
  162. * file that we are processing the last close on, then nothing
  163. * will be able to reopen and write to this file. Purge this
  164. * inode from the filestreams cache so that it doesn't delay
  165. * teardown of the inode.
  166. */
  167. if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
  168. xfs_filestream_deassociate(ip);
  169. /*
  170. * If we previously truncated this file and removed old data
  171. * in the process, we want to initiate "early" writeout on
  172. * the last close. This is an attempt to combat the notorious
  173. * NULL files problem which is particularly noticeable from a
  174. * truncate down, buffered (re-)write (delalloc), followed by
  175. * a crash. What we are effectively doing here is
  176. * significantly reducing the time window where we'd otherwise
  177. * be exposed to that problem.
  178. */
  179. truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
  180. if (truncated) {
  181. xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
  182. if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0) {
  183. error = -filemap_flush(VFS_I(ip)->i_mapping);
  184. if (error)
  185. return error;
  186. }
  187. }
  188. }
  189. if (ip->i_d.di_nlink == 0)
  190. return 0;
  191. if (xfs_can_free_eofblocks(ip, false)) {
  192. /*
  193. * If we can't get the iolock just skip truncating the blocks
  194. * past EOF because we could deadlock with the mmap_sem
  195. * otherwise. We'll get another chance to drop them once the
  196. * last reference to the inode is dropped, so we'll never leak
  197. * blocks permanently.
  198. *
  199. * Further, check if the inode is being opened, written and
  200. * closed frequently and we have delayed allocation blocks
  201. * outstanding (e.g. streaming writes from the NFS server),
  202. * truncating the blocks past EOF will cause fragmentation to
  203. * occur.
  204. *
  205. * In this case don't do the truncation, either, but we have to
  206. * be careful how we detect this case. Blocks beyond EOF show
  207. * up as i_delayed_blks even when the inode is clean, so we
  208. * need to truncate them away first before checking for a dirty
  209. * release. Hence on the first dirty close we will still remove
  210. * the speculative allocation, but after that we will leave it
  211. * in place.
  212. */
  213. if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
  214. return 0;
  215. error = xfs_free_eofblocks(mp, ip, true);
  216. if (error && error != EAGAIN)
  217. return error;
  218. /* delalloc blocks after truncation means it really is dirty */
  219. if (ip->i_delayed_blks)
  220. xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
  221. }
  222. return 0;
  223. }
  224. /*
  225. * xfs_inactive
  226. *
  227. * This is called when the vnode reference count for the vnode
  228. * goes to zero. If the file has been unlinked, then it must
  229. * now be truncated. Also, we clear all of the read-ahead state
  230. * kept for the inode here since the file is now closed.
  231. */
  232. int
  233. xfs_inactive(
  234. xfs_inode_t *ip)
  235. {
  236. xfs_bmap_free_t free_list;
  237. xfs_fsblock_t first_block;
  238. int committed;
  239. xfs_trans_t *tp;
  240. xfs_mount_t *mp;
  241. int error;
  242. int truncate = 0;
  243. /*
  244. * If the inode is already free, then there can be nothing
  245. * to clean up here.
  246. */
  247. if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
  248. ASSERT(ip->i_df.if_real_bytes == 0);
  249. ASSERT(ip->i_df.if_broot_bytes == 0);
  250. return VN_INACTIVE_CACHE;
  251. }
  252. mp = ip->i_mount;
  253. error = 0;
  254. /* If this is a read-only mount, don't do this (would generate I/O) */
  255. if (mp->m_flags & XFS_MOUNT_RDONLY)
  256. goto out;
  257. if (ip->i_d.di_nlink != 0) {
  258. /*
  259. * force is true because we are evicting an inode from the
  260. * cache. Post-eof blocks must be freed, lest we end up with
  261. * broken free space accounting.
  262. */
  263. if (xfs_can_free_eofblocks(ip, true)) {
  264. error = xfs_free_eofblocks(mp, ip, false);
  265. if (error)
  266. return VN_INACTIVE_CACHE;
  267. }
  268. goto out;
  269. }
  270. if (S_ISREG(ip->i_d.di_mode) &&
  271. (ip->i_d.di_size != 0 || XFS_ISIZE(ip) != 0 ||
  272. ip->i_d.di_nextents > 0 || ip->i_delayed_blks > 0))
  273. truncate = 1;
  274. error = xfs_qm_dqattach(ip, 0);
  275. if (error)
  276. return VN_INACTIVE_CACHE;
  277. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  278. error = xfs_trans_reserve(tp, 0,
  279. (truncate || S_ISLNK(ip->i_d.di_mode)) ?
  280. XFS_ITRUNCATE_LOG_RES(mp) :
  281. XFS_IFREE_LOG_RES(mp),
  282. 0,
  283. XFS_TRANS_PERM_LOG_RES,
  284. XFS_ITRUNCATE_LOG_COUNT);
  285. if (error) {
  286. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  287. xfs_trans_cancel(tp, 0);
  288. return VN_INACTIVE_CACHE;
  289. }
  290. xfs_ilock(ip, XFS_ILOCK_EXCL);
  291. xfs_trans_ijoin(tp, ip, 0);
  292. if (S_ISLNK(ip->i_d.di_mode)) {
  293. error = xfs_inactive_symlink(ip, &tp);
  294. if (error)
  295. goto out_cancel;
  296. } else if (truncate) {
  297. ip->i_d.di_size = 0;
  298. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  299. error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK, 0);
  300. if (error)
  301. goto out_cancel;
  302. ASSERT(ip->i_d.di_nextents == 0);
  303. }
  304. /*
  305. * If there are attributes associated with the file then blow them away
  306. * now. The code calls a routine that recursively deconstructs the
  307. * attribute fork. We need to just commit the current transaction
  308. * because we can't use it for xfs_attr_inactive().
  309. */
  310. if (ip->i_d.di_anextents > 0) {
  311. ASSERT(ip->i_d.di_forkoff != 0);
  312. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  313. if (error)
  314. goto out_unlock;
  315. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  316. error = xfs_attr_inactive(ip);
  317. if (error)
  318. goto out;
  319. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  320. error = xfs_trans_reserve(tp, 0,
  321. XFS_IFREE_LOG_RES(mp),
  322. 0, XFS_TRANS_PERM_LOG_RES,
  323. XFS_INACTIVE_LOG_COUNT);
  324. if (error) {
  325. xfs_trans_cancel(tp, 0);
  326. goto out;
  327. }
  328. xfs_ilock(ip, XFS_ILOCK_EXCL);
  329. xfs_trans_ijoin(tp, ip, 0);
  330. }
  331. if (ip->i_afp)
  332. xfs_idestroy_fork(ip, XFS_ATTR_FORK);
  333. ASSERT(ip->i_d.di_anextents == 0);
  334. /*
  335. * Free the inode.
  336. */
  337. xfs_bmap_init(&free_list, &first_block);
  338. error = xfs_ifree(tp, ip, &free_list);
  339. if (error) {
  340. /*
  341. * If we fail to free the inode, shut down. The cancel
  342. * might do that, we need to make sure. Otherwise the
  343. * inode might be lost for a long time or forever.
  344. */
  345. if (!XFS_FORCED_SHUTDOWN(mp)) {
  346. xfs_notice(mp, "%s: xfs_ifree returned error %d",
  347. __func__, error);
  348. xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
  349. }
  350. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  351. } else {
  352. /*
  353. * Credit the quota account(s). The inode is gone.
  354. */
  355. xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
  356. /*
  357. * Just ignore errors at this point. There is nothing we can
  358. * do except to try to keep going. Make sure it's not a silent
  359. * error.
  360. */
  361. error = xfs_bmap_finish(&tp, &free_list, &committed);
  362. if (error)
  363. xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
  364. __func__, error);
  365. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  366. if (error)
  367. xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
  368. __func__, error);
  369. }
  370. /*
  371. * Release the dquots held by inode, if any.
  372. */
  373. xfs_qm_dqdetach(ip);
  374. out_unlock:
  375. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  376. out:
  377. return VN_INACTIVE_CACHE;
  378. out_cancel:
  379. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  380. goto out_unlock;
  381. }
  382. /*
  383. * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
  384. * is allowed, otherwise it has to be an exact match. If a CI match is found,
  385. * ci_name->name will point to a the actual name (caller must free) or
  386. * will be set to NULL if an exact match is found.
  387. */
  388. int
  389. xfs_lookup(
  390. xfs_inode_t *dp,
  391. struct xfs_name *name,
  392. xfs_inode_t **ipp,
  393. struct xfs_name *ci_name)
  394. {
  395. xfs_ino_t inum;
  396. int error;
  397. uint lock_mode;
  398. trace_xfs_lookup(dp, name);
  399. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  400. return XFS_ERROR(EIO);
  401. lock_mode = xfs_ilock_map_shared(dp);
  402. error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
  403. xfs_iunlock_map_shared(dp, lock_mode);
  404. if (error)
  405. goto out;
  406. error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
  407. if (error)
  408. goto out_free_name;
  409. return 0;
  410. out_free_name:
  411. if (ci_name)
  412. kmem_free(ci_name->name);
  413. out:
  414. *ipp = NULL;
  415. return error;
  416. }
  417. int
  418. xfs_create(
  419. xfs_inode_t *dp,
  420. struct xfs_name *name,
  421. umode_t mode,
  422. xfs_dev_t rdev,
  423. xfs_inode_t **ipp)
  424. {
  425. int is_dir = S_ISDIR(mode);
  426. struct xfs_mount *mp = dp->i_mount;
  427. struct xfs_inode *ip = NULL;
  428. struct xfs_trans *tp = NULL;
  429. int error;
  430. xfs_bmap_free_t free_list;
  431. xfs_fsblock_t first_block;
  432. bool unlock_dp_on_error = false;
  433. uint cancel_flags;
  434. int committed;
  435. prid_t prid;
  436. struct xfs_dquot *udqp = NULL;
  437. struct xfs_dquot *gdqp = NULL;
  438. struct xfs_dquot *pdqp = NULL;
  439. uint resblks;
  440. uint log_res;
  441. uint log_count;
  442. trace_xfs_create(dp, name);
  443. if (XFS_FORCED_SHUTDOWN(mp))
  444. return XFS_ERROR(EIO);
  445. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  446. prid = xfs_get_projid(dp);
  447. else
  448. prid = XFS_PROJID_DEFAULT;
  449. /*
  450. * Make sure that we have allocated dquot(s) on disk.
  451. */
  452. error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
  453. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
  454. &udqp, &gdqp, &pdqp);
  455. if (error)
  456. return error;
  457. if (is_dir) {
  458. rdev = 0;
  459. resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
  460. log_res = XFS_MKDIR_LOG_RES(mp);
  461. log_count = XFS_MKDIR_LOG_COUNT;
  462. tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
  463. } else {
  464. resblks = XFS_CREATE_SPACE_RES(mp, name->len);
  465. log_res = XFS_CREATE_LOG_RES(mp);
  466. log_count = XFS_CREATE_LOG_COUNT;
  467. tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
  468. }
  469. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  470. /*
  471. * Initially assume that the file does not exist and
  472. * reserve the resources for that case. If that is not
  473. * the case we'll drop the one we have and get a more
  474. * appropriate transaction later.
  475. */
  476. error = xfs_trans_reserve(tp, resblks, log_res, 0,
  477. XFS_TRANS_PERM_LOG_RES, log_count);
  478. if (error == ENOSPC) {
  479. /* flush outstanding delalloc blocks and retry */
  480. xfs_flush_inodes(mp);
  481. error = xfs_trans_reserve(tp, resblks, log_res, 0,
  482. XFS_TRANS_PERM_LOG_RES, log_count);
  483. }
  484. if (error == ENOSPC) {
  485. /* No space at all so try a "no-allocation" reservation */
  486. resblks = 0;
  487. error = xfs_trans_reserve(tp, 0, log_res, 0,
  488. XFS_TRANS_PERM_LOG_RES, log_count);
  489. }
  490. if (error) {
  491. cancel_flags = 0;
  492. goto out_trans_cancel;
  493. }
  494. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  495. unlock_dp_on_error = true;
  496. xfs_bmap_init(&free_list, &first_block);
  497. /*
  498. * Reserve disk quota and the inode.
  499. */
  500. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
  501. pdqp, resblks, 1, 0);
  502. if (error)
  503. goto out_trans_cancel;
  504. error = xfs_dir_canenter(tp, dp, name, resblks);
  505. if (error)
  506. goto out_trans_cancel;
  507. /*
  508. * A newly created regular or special file just has one directory
  509. * entry pointing to them, but a directory also the "." entry
  510. * pointing to itself.
  511. */
  512. error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
  513. prid, resblks > 0, &ip, &committed);
  514. if (error) {
  515. if (error == ENOSPC)
  516. goto out_trans_cancel;
  517. goto out_trans_abort;
  518. }
  519. /*
  520. * Now we join the directory inode to the transaction. We do not do it
  521. * earlier because xfs_dir_ialloc might commit the previous transaction
  522. * (and release all the locks). An error from here on will result in
  523. * the transaction cancel unlocking dp so don't do it explicitly in the
  524. * error path.
  525. */
  526. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  527. unlock_dp_on_error = false;
  528. error = xfs_dir_createname(tp, dp, name, ip->i_ino,
  529. &first_block, &free_list, resblks ?
  530. resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
  531. if (error) {
  532. ASSERT(error != ENOSPC);
  533. goto out_trans_abort;
  534. }
  535. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  536. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  537. if (is_dir) {
  538. error = xfs_dir_init(tp, ip, dp);
  539. if (error)
  540. goto out_bmap_cancel;
  541. error = xfs_bumplink(tp, dp);
  542. if (error)
  543. goto out_bmap_cancel;
  544. }
  545. /*
  546. * If this is a synchronous mount, make sure that the
  547. * create transaction goes to disk before returning to
  548. * the user.
  549. */
  550. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  551. xfs_trans_set_sync(tp);
  552. /*
  553. * Attach the dquot(s) to the inodes and modify them incore.
  554. * These ids of the inode couldn't have changed since the new
  555. * inode has been locked ever since it was created.
  556. */
  557. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  558. error = xfs_bmap_finish(&tp, &free_list, &committed);
  559. if (error)
  560. goto out_bmap_cancel;
  561. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  562. if (error)
  563. goto out_release_inode;
  564. xfs_qm_dqrele(udqp);
  565. xfs_qm_dqrele(gdqp);
  566. xfs_qm_dqrele(pdqp);
  567. *ipp = ip;
  568. return 0;
  569. out_bmap_cancel:
  570. xfs_bmap_cancel(&free_list);
  571. out_trans_abort:
  572. cancel_flags |= XFS_TRANS_ABORT;
  573. out_trans_cancel:
  574. xfs_trans_cancel(tp, cancel_flags);
  575. out_release_inode:
  576. /*
  577. * Wait until after the current transaction is aborted to
  578. * release the inode. This prevents recursive transactions
  579. * and deadlocks from xfs_inactive.
  580. */
  581. if (ip)
  582. IRELE(ip);
  583. xfs_qm_dqrele(udqp);
  584. xfs_qm_dqrele(gdqp);
  585. xfs_qm_dqrele(pdqp);
  586. if (unlock_dp_on_error)
  587. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  588. return error;
  589. }
  590. #ifdef DEBUG
  591. int xfs_locked_n;
  592. int xfs_small_retries;
  593. int xfs_middle_retries;
  594. int xfs_lots_retries;
  595. int xfs_lock_delays;
  596. #endif
  597. /*
  598. * Bump the subclass so xfs_lock_inodes() acquires each lock with
  599. * a different value
  600. */
  601. static inline int
  602. xfs_lock_inumorder(int lock_mode, int subclass)
  603. {
  604. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  605. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
  606. if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
  607. lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
  608. return lock_mode;
  609. }
  610. /*
  611. * The following routine will lock n inodes in exclusive mode.
  612. * We assume the caller calls us with the inodes in i_ino order.
  613. *
  614. * We need to detect deadlock where an inode that we lock
  615. * is in the AIL and we start waiting for another inode that is locked
  616. * by a thread in a long running transaction (such as truncate). This can
  617. * result in deadlock since the long running trans might need to wait
  618. * for the inode we just locked in order to push the tail and free space
  619. * in the log.
  620. */
  621. void
  622. xfs_lock_inodes(
  623. xfs_inode_t **ips,
  624. int inodes,
  625. uint lock_mode)
  626. {
  627. int attempts = 0, i, j, try_lock;
  628. xfs_log_item_t *lp;
  629. ASSERT(ips && (inodes >= 2)); /* we need at least two */
  630. try_lock = 0;
  631. i = 0;
  632. again:
  633. for (; i < inodes; i++) {
  634. ASSERT(ips[i]);
  635. if (i && (ips[i] == ips[i-1])) /* Already locked */
  636. continue;
  637. /*
  638. * If try_lock is not set yet, make sure all locked inodes
  639. * are not in the AIL.
  640. * If any are, set try_lock to be used later.
  641. */
  642. if (!try_lock) {
  643. for (j = (i - 1); j >= 0 && !try_lock; j--) {
  644. lp = (xfs_log_item_t *)ips[j]->i_itemp;
  645. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  646. try_lock++;
  647. }
  648. }
  649. }
  650. /*
  651. * If any of the previous locks we have locked is in the AIL,
  652. * we must TRY to get the second and subsequent locks. If
  653. * we can't get any, we must release all we have
  654. * and try again.
  655. */
  656. if (try_lock) {
  657. /* try_lock must be 0 if i is 0. */
  658. /*
  659. * try_lock means we have an inode locked
  660. * that is in the AIL.
  661. */
  662. ASSERT(i != 0);
  663. if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
  664. attempts++;
  665. /*
  666. * Unlock all previous guys and try again.
  667. * xfs_iunlock will try to push the tail
  668. * if the inode is in the AIL.
  669. */
  670. for(j = i - 1; j >= 0; j--) {
  671. /*
  672. * Check to see if we've already
  673. * unlocked this one.
  674. * Not the first one going back,
  675. * and the inode ptr is the same.
  676. */
  677. if ((j != (i - 1)) && ips[j] ==
  678. ips[j+1])
  679. continue;
  680. xfs_iunlock(ips[j], lock_mode);
  681. }
  682. if ((attempts % 5) == 0) {
  683. delay(1); /* Don't just spin the CPU */
  684. #ifdef DEBUG
  685. xfs_lock_delays++;
  686. #endif
  687. }
  688. i = 0;
  689. try_lock = 0;
  690. goto again;
  691. }
  692. } else {
  693. xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
  694. }
  695. }
  696. #ifdef DEBUG
  697. if (attempts) {
  698. if (attempts < 5) xfs_small_retries++;
  699. else if (attempts < 100) xfs_middle_retries++;
  700. else xfs_lots_retries++;
  701. } else {
  702. xfs_locked_n++;
  703. }
  704. #endif
  705. }
  706. /*
  707. * xfs_lock_two_inodes() can only be used to lock one type of lock
  708. * at a time - the iolock or the ilock, but not both at once. If
  709. * we lock both at once, lockdep will report false positives saying
  710. * we have violated locking orders.
  711. */
  712. void
  713. xfs_lock_two_inodes(
  714. xfs_inode_t *ip0,
  715. xfs_inode_t *ip1,
  716. uint lock_mode)
  717. {
  718. xfs_inode_t *temp;
  719. int attempts = 0;
  720. xfs_log_item_t *lp;
  721. if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
  722. ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
  723. ASSERT(ip0->i_ino != ip1->i_ino);
  724. if (ip0->i_ino > ip1->i_ino) {
  725. temp = ip0;
  726. ip0 = ip1;
  727. ip1 = temp;
  728. }
  729. again:
  730. xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
  731. /*
  732. * If the first lock we have locked is in the AIL, we must TRY to get
  733. * the second lock. If we can't get it, we must release the first one
  734. * and try again.
  735. */
  736. lp = (xfs_log_item_t *)ip0->i_itemp;
  737. if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
  738. if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
  739. xfs_iunlock(ip0, lock_mode);
  740. if ((++attempts % 5) == 0)
  741. delay(1); /* Don't just spin the CPU */
  742. goto again;
  743. }
  744. } else {
  745. xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
  746. }
  747. }
  748. int
  749. xfs_remove(
  750. xfs_inode_t *dp,
  751. struct xfs_name *name,
  752. xfs_inode_t *ip)
  753. {
  754. xfs_mount_t *mp = dp->i_mount;
  755. xfs_trans_t *tp = NULL;
  756. int is_dir = S_ISDIR(ip->i_d.di_mode);
  757. int error = 0;
  758. xfs_bmap_free_t free_list;
  759. xfs_fsblock_t first_block;
  760. int cancel_flags;
  761. int committed;
  762. int link_zero;
  763. uint resblks;
  764. uint log_count;
  765. trace_xfs_remove(dp, name);
  766. if (XFS_FORCED_SHUTDOWN(mp))
  767. return XFS_ERROR(EIO);
  768. error = xfs_qm_dqattach(dp, 0);
  769. if (error)
  770. goto std_return;
  771. error = xfs_qm_dqattach(ip, 0);
  772. if (error)
  773. goto std_return;
  774. if (is_dir) {
  775. tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
  776. log_count = XFS_DEFAULT_LOG_COUNT;
  777. } else {
  778. tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
  779. log_count = XFS_REMOVE_LOG_COUNT;
  780. }
  781. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  782. /*
  783. * We try to get the real space reservation first,
  784. * allowing for directory btree deletion(s) implying
  785. * possible bmap insert(s). If we can't get the space
  786. * reservation then we use 0 instead, and avoid the bmap
  787. * btree insert(s) in the directory code by, if the bmap
  788. * insert tries to happen, instead trimming the LAST
  789. * block from the directory.
  790. */
  791. resblks = XFS_REMOVE_SPACE_RES(mp);
  792. error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
  793. XFS_TRANS_PERM_LOG_RES, log_count);
  794. if (error == ENOSPC) {
  795. resblks = 0;
  796. error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
  797. XFS_TRANS_PERM_LOG_RES, log_count);
  798. }
  799. if (error) {
  800. ASSERT(error != ENOSPC);
  801. cancel_flags = 0;
  802. goto out_trans_cancel;
  803. }
  804. xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
  805. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  806. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  807. /*
  808. * If we're removing a directory perform some additional validation.
  809. */
  810. if (is_dir) {
  811. ASSERT(ip->i_d.di_nlink >= 2);
  812. if (ip->i_d.di_nlink != 2) {
  813. error = XFS_ERROR(ENOTEMPTY);
  814. goto out_trans_cancel;
  815. }
  816. if (!xfs_dir_isempty(ip)) {
  817. error = XFS_ERROR(ENOTEMPTY);
  818. goto out_trans_cancel;
  819. }
  820. }
  821. xfs_bmap_init(&free_list, &first_block);
  822. error = xfs_dir_removename(tp, dp, name, ip->i_ino,
  823. &first_block, &free_list, resblks);
  824. if (error) {
  825. ASSERT(error != ENOENT);
  826. goto out_bmap_cancel;
  827. }
  828. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  829. if (is_dir) {
  830. /*
  831. * Drop the link from ip's "..".
  832. */
  833. error = xfs_droplink(tp, dp);
  834. if (error)
  835. goto out_bmap_cancel;
  836. /*
  837. * Drop the "." link from ip to self.
  838. */
  839. error = xfs_droplink(tp, ip);
  840. if (error)
  841. goto out_bmap_cancel;
  842. } else {
  843. /*
  844. * When removing a non-directory we need to log the parent
  845. * inode here. For a directory this is done implicitly
  846. * by the xfs_droplink call for the ".." entry.
  847. */
  848. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  849. }
  850. /*
  851. * Drop the link from dp to ip.
  852. */
  853. error = xfs_droplink(tp, ip);
  854. if (error)
  855. goto out_bmap_cancel;
  856. /*
  857. * Determine if this is the last link while
  858. * we are in the transaction.
  859. */
  860. link_zero = (ip->i_d.di_nlink == 0);
  861. /*
  862. * If this is a synchronous mount, make sure that the
  863. * remove transaction goes to disk before returning to
  864. * the user.
  865. */
  866. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
  867. xfs_trans_set_sync(tp);
  868. error = xfs_bmap_finish(&tp, &free_list, &committed);
  869. if (error)
  870. goto out_bmap_cancel;
  871. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  872. if (error)
  873. goto std_return;
  874. /*
  875. * If we are using filestreams, kill the stream association.
  876. * If the file is still open it may get a new one but that
  877. * will get killed on last close in xfs_close() so we don't
  878. * have to worry about that.
  879. */
  880. if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
  881. xfs_filestream_deassociate(ip);
  882. return 0;
  883. out_bmap_cancel:
  884. xfs_bmap_cancel(&free_list);
  885. cancel_flags |= XFS_TRANS_ABORT;
  886. out_trans_cancel:
  887. xfs_trans_cancel(tp, cancel_flags);
  888. std_return:
  889. return error;
  890. }
  891. int
  892. xfs_link(
  893. xfs_inode_t *tdp,
  894. xfs_inode_t *sip,
  895. struct xfs_name *target_name)
  896. {
  897. xfs_mount_t *mp = tdp->i_mount;
  898. xfs_trans_t *tp;
  899. int error;
  900. xfs_bmap_free_t free_list;
  901. xfs_fsblock_t first_block;
  902. int cancel_flags;
  903. int committed;
  904. int resblks;
  905. trace_xfs_link(tdp, target_name);
  906. ASSERT(!S_ISDIR(sip->i_d.di_mode));
  907. if (XFS_FORCED_SHUTDOWN(mp))
  908. return XFS_ERROR(EIO);
  909. error = xfs_qm_dqattach(sip, 0);
  910. if (error)
  911. goto std_return;
  912. error = xfs_qm_dqattach(tdp, 0);
  913. if (error)
  914. goto std_return;
  915. tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
  916. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  917. resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
  918. error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
  919. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  920. if (error == ENOSPC) {
  921. resblks = 0;
  922. error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
  923. XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
  924. }
  925. if (error) {
  926. cancel_flags = 0;
  927. goto error_return;
  928. }
  929. xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
  930. xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
  931. xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
  932. /*
  933. * If we are using project inheritance, we only allow hard link
  934. * creation in our tree when the project IDs are the same; else
  935. * the tree quota mechanism could be circumvented.
  936. */
  937. if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
  938. (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
  939. error = XFS_ERROR(EXDEV);
  940. goto error_return;
  941. }
  942. error = xfs_dir_canenter(tp, tdp, target_name, resblks);
  943. if (error)
  944. goto error_return;
  945. xfs_bmap_init(&free_list, &first_block);
  946. error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
  947. &first_block, &free_list, resblks);
  948. if (error)
  949. goto abort_return;
  950. xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  951. xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
  952. error = xfs_bumplink(tp, sip);
  953. if (error)
  954. goto abort_return;
  955. /*
  956. * If this is a synchronous mount, make sure that the
  957. * link transaction goes to disk before returning to
  958. * the user.
  959. */
  960. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  961. xfs_trans_set_sync(tp);
  962. }
  963. error = xfs_bmap_finish (&tp, &free_list, &committed);
  964. if (error) {
  965. xfs_bmap_cancel(&free_list);
  966. goto abort_return;
  967. }
  968. return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  969. abort_return:
  970. cancel_flags |= XFS_TRANS_ABORT;
  971. error_return:
  972. xfs_trans_cancel(tp, cancel_flags);
  973. std_return:
  974. return error;
  975. }
  976. int
  977. xfs_set_dmattrs(
  978. xfs_inode_t *ip,
  979. u_int evmask,
  980. u_int16_t state)
  981. {
  982. xfs_mount_t *mp = ip->i_mount;
  983. xfs_trans_t *tp;
  984. int error;
  985. if (!capable(CAP_SYS_ADMIN))
  986. return XFS_ERROR(EPERM);
  987. if (XFS_FORCED_SHUTDOWN(mp))
  988. return XFS_ERROR(EIO);
  989. tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
  990. error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
  991. if (error) {
  992. xfs_trans_cancel(tp, 0);
  993. return error;
  994. }
  995. xfs_ilock(ip, XFS_ILOCK_EXCL);
  996. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  997. ip->i_d.di_dmevmask = evmask;
  998. ip->i_d.di_dmstate = state;
  999. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1000. error = xfs_trans_commit(tp, 0);
  1001. return error;
  1002. }
  1003. /*
  1004. * xfs_alloc_file_space()
  1005. * This routine allocates disk space for the given file.
  1006. *
  1007. * If alloc_type == 0, this request is for an ALLOCSP type
  1008. * request which will change the file size. In this case, no
  1009. * DMAPI event will be generated by the call. A TRUNCATE event
  1010. * will be generated later by xfs_setattr.
  1011. *
  1012. * If alloc_type != 0, this request is for a RESVSP type
  1013. * request, and a DMAPI DM_EVENT_WRITE will be generated if the
  1014. * lower block boundary byte address is less than the file's
  1015. * length.
  1016. *
  1017. * RETURNS:
  1018. * 0 on success
  1019. * errno on error
  1020. *
  1021. */
  1022. STATIC int
  1023. xfs_alloc_file_space(
  1024. xfs_inode_t *ip,
  1025. xfs_off_t offset,
  1026. xfs_off_t len,
  1027. int alloc_type,
  1028. int attr_flags)
  1029. {
  1030. xfs_mount_t *mp = ip->i_mount;
  1031. xfs_off_t count;
  1032. xfs_filblks_t allocated_fsb;
  1033. xfs_filblks_t allocatesize_fsb;
  1034. xfs_extlen_t extsz, temp;
  1035. xfs_fileoff_t startoffset_fsb;
  1036. xfs_fsblock_t firstfsb;
  1037. int nimaps;
  1038. int quota_flag;
  1039. int rt;
  1040. xfs_trans_t *tp;
  1041. xfs_bmbt_irec_t imaps[1], *imapp;
  1042. xfs_bmap_free_t free_list;
  1043. uint qblocks, resblks, resrtextents;
  1044. int committed;
  1045. int error;
  1046. trace_xfs_alloc_file_space(ip);
  1047. if (XFS_FORCED_SHUTDOWN(mp))
  1048. return XFS_ERROR(EIO);
  1049. error = xfs_qm_dqattach(ip, 0);
  1050. if (error)
  1051. return error;
  1052. if (len <= 0)
  1053. return XFS_ERROR(EINVAL);
  1054. rt = XFS_IS_REALTIME_INODE(ip);
  1055. extsz = xfs_get_extsz_hint(ip);
  1056. count = len;
  1057. imapp = &imaps[0];
  1058. nimaps = 1;
  1059. startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
  1060. allocatesize_fsb = XFS_B_TO_FSB(mp, count);
  1061. /*
  1062. * Allocate file space until done or until there is an error
  1063. */
  1064. while (allocatesize_fsb && !error) {
  1065. xfs_fileoff_t s, e;
  1066. /*
  1067. * Determine space reservations for data/realtime.
  1068. */
  1069. if (unlikely(extsz)) {
  1070. s = startoffset_fsb;
  1071. do_div(s, extsz);
  1072. s *= extsz;
  1073. e = startoffset_fsb + allocatesize_fsb;
  1074. if ((temp = do_mod(startoffset_fsb, extsz)))
  1075. e += temp;
  1076. if ((temp = do_mod(e, extsz)))
  1077. e += extsz - temp;
  1078. } else {
  1079. s = 0;
  1080. e = allocatesize_fsb;
  1081. }
  1082. /*
  1083. * The transaction reservation is limited to a 32-bit block
  1084. * count, hence we need to limit the number of blocks we are
  1085. * trying to reserve to avoid an overflow. We can't allocate
  1086. * more than @nimaps extents, and an extent is limited on disk
  1087. * to MAXEXTLEN (21 bits), so use that to enforce the limit.
  1088. */
  1089. resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
  1090. if (unlikely(rt)) {
  1091. resrtextents = qblocks = resblks;
  1092. resrtextents /= mp->m_sb.sb_rextsize;
  1093. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  1094. quota_flag = XFS_QMOPT_RES_RTBLKS;
  1095. } else {
  1096. resrtextents = 0;
  1097. resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
  1098. quota_flag = XFS_QMOPT_RES_REGBLKS;
  1099. }
  1100. /*
  1101. * Allocate and setup the transaction.
  1102. */
  1103. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  1104. error = xfs_trans_reserve(tp, resblks,
  1105. XFS_WRITE_LOG_RES(mp), resrtextents,
  1106. XFS_TRANS_PERM_LOG_RES,
  1107. XFS_WRITE_LOG_COUNT);
  1108. /*
  1109. * Check for running out of space
  1110. */
  1111. if (error) {
  1112. /*
  1113. * Free the transaction structure.
  1114. */
  1115. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  1116. xfs_trans_cancel(tp, 0);
  1117. break;
  1118. }
  1119. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1120. error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
  1121. 0, quota_flag);
  1122. if (error)
  1123. goto error1;
  1124. xfs_trans_ijoin(tp, ip, 0);
  1125. xfs_bmap_init(&free_list, &firstfsb);
  1126. error = xfs_bmapi_write(tp, ip, startoffset_fsb,
  1127. allocatesize_fsb, alloc_type, &firstfsb,
  1128. 0, imapp, &nimaps, &free_list);
  1129. if (error) {
  1130. goto error0;
  1131. }
  1132. /*
  1133. * Complete the transaction
  1134. */
  1135. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1136. if (error) {
  1137. goto error0;
  1138. }
  1139. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1140. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1141. if (error) {
  1142. break;
  1143. }
  1144. allocated_fsb = imapp->br_blockcount;
  1145. if (nimaps == 0) {
  1146. error = XFS_ERROR(ENOSPC);
  1147. break;
  1148. }
  1149. startoffset_fsb += allocated_fsb;
  1150. allocatesize_fsb -= allocated_fsb;
  1151. }
  1152. return error;
  1153. error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
  1154. xfs_bmap_cancel(&free_list);
  1155. xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
  1156. error1: /* Just cancel transaction */
  1157. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1158. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1159. return error;
  1160. }
  1161. /*
  1162. * Zero file bytes between startoff and endoff inclusive.
  1163. * The iolock is held exclusive and no blocks are buffered.
  1164. *
  1165. * This function is used by xfs_free_file_space() to zero
  1166. * partial blocks when the range to free is not block aligned.
  1167. * When unreserving space with boundaries that are not block
  1168. * aligned we round up the start and round down the end
  1169. * boundaries and then use this function to zero the parts of
  1170. * the blocks that got dropped during the rounding.
  1171. */
  1172. STATIC int
  1173. xfs_zero_remaining_bytes(
  1174. xfs_inode_t *ip,
  1175. xfs_off_t startoff,
  1176. xfs_off_t endoff)
  1177. {
  1178. xfs_bmbt_irec_t imap;
  1179. xfs_fileoff_t offset_fsb;
  1180. xfs_off_t lastoffset;
  1181. xfs_off_t offset;
  1182. xfs_buf_t *bp;
  1183. xfs_mount_t *mp = ip->i_mount;
  1184. int nimap;
  1185. int error = 0;
  1186. /*
  1187. * Avoid doing I/O beyond eof - it's not necessary
  1188. * since nothing can read beyond eof. The space will
  1189. * be zeroed when the file is extended anyway.
  1190. */
  1191. if (startoff >= XFS_ISIZE(ip))
  1192. return 0;
  1193. if (endoff > XFS_ISIZE(ip))
  1194. endoff = XFS_ISIZE(ip);
  1195. bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
  1196. mp->m_rtdev_targp : mp->m_ddev_targp,
  1197. BTOBB(mp->m_sb.sb_blocksize), 0);
  1198. if (!bp)
  1199. return XFS_ERROR(ENOMEM);
  1200. xfs_buf_unlock(bp);
  1201. for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
  1202. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1203. nimap = 1;
  1204. error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0);
  1205. if (error || nimap < 1)
  1206. break;
  1207. ASSERT(imap.br_blockcount >= 1);
  1208. ASSERT(imap.br_startoff == offset_fsb);
  1209. lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
  1210. if (lastoffset > endoff)
  1211. lastoffset = endoff;
  1212. if (imap.br_startblock == HOLESTARTBLOCK)
  1213. continue;
  1214. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1215. if (imap.br_state == XFS_EXT_UNWRITTEN)
  1216. continue;
  1217. XFS_BUF_UNDONE(bp);
  1218. XFS_BUF_UNWRITE(bp);
  1219. XFS_BUF_READ(bp);
  1220. XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
  1221. xfsbdstrat(mp, bp);
  1222. error = xfs_buf_iowait(bp);
  1223. if (error) {
  1224. xfs_buf_ioerror_alert(bp,
  1225. "xfs_zero_remaining_bytes(read)");
  1226. break;
  1227. }
  1228. memset(bp->b_addr +
  1229. (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
  1230. 0, lastoffset - offset + 1);
  1231. XFS_BUF_UNDONE(bp);
  1232. XFS_BUF_UNREAD(bp);
  1233. XFS_BUF_WRITE(bp);
  1234. xfsbdstrat(mp, bp);
  1235. error = xfs_buf_iowait(bp);
  1236. if (error) {
  1237. xfs_buf_ioerror_alert(bp,
  1238. "xfs_zero_remaining_bytes(write)");
  1239. break;
  1240. }
  1241. }
  1242. xfs_buf_free(bp);
  1243. return error;
  1244. }
  1245. /*
  1246. * xfs_free_file_space()
  1247. * This routine frees disk space for the given file.
  1248. *
  1249. * This routine is only called by xfs_change_file_space
  1250. * for an UNRESVSP type call.
  1251. *
  1252. * RETURNS:
  1253. * 0 on success
  1254. * errno on error
  1255. *
  1256. */
  1257. STATIC int
  1258. xfs_free_file_space(
  1259. xfs_inode_t *ip,
  1260. xfs_off_t offset,
  1261. xfs_off_t len,
  1262. int attr_flags)
  1263. {
  1264. int committed;
  1265. int done;
  1266. xfs_fileoff_t endoffset_fsb;
  1267. int error;
  1268. xfs_fsblock_t firstfsb;
  1269. xfs_bmap_free_t free_list;
  1270. xfs_bmbt_irec_t imap;
  1271. xfs_off_t ioffset;
  1272. xfs_extlen_t mod=0;
  1273. xfs_mount_t *mp;
  1274. int nimap;
  1275. uint resblks;
  1276. xfs_off_t rounding;
  1277. int rt;
  1278. xfs_fileoff_t startoffset_fsb;
  1279. xfs_trans_t *tp;
  1280. int need_iolock = 1;
  1281. mp = ip->i_mount;
  1282. trace_xfs_free_file_space(ip);
  1283. error = xfs_qm_dqattach(ip, 0);
  1284. if (error)
  1285. return error;
  1286. error = 0;
  1287. if (len <= 0) /* if nothing being freed */
  1288. return error;
  1289. rt = XFS_IS_REALTIME_INODE(ip);
  1290. startoffset_fsb = XFS_B_TO_FSB(mp, offset);
  1291. endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
  1292. if (attr_flags & XFS_ATTR_NOLOCK)
  1293. need_iolock = 0;
  1294. if (need_iolock) {
  1295. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1296. /* wait for the completion of any pending DIOs */
  1297. inode_dio_wait(VFS_I(ip));
  1298. }
  1299. rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  1300. ioffset = offset & ~(rounding - 1);
  1301. error = -filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
  1302. ioffset, -1);
  1303. if (error)
  1304. goto out_unlock_iolock;
  1305. truncate_pagecache_range(VFS_I(ip), ioffset, -1);
  1306. /*
  1307. * Need to zero the stuff we're not freeing, on disk.
  1308. * If it's a realtime file & can't use unwritten extents then we
  1309. * actually need to zero the extent edges. Otherwise xfs_bunmapi
  1310. * will take care of it for us.
  1311. */
  1312. if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
  1313. nimap = 1;
  1314. error = xfs_bmapi_read(ip, startoffset_fsb, 1,
  1315. &imap, &nimap, 0);
  1316. if (error)
  1317. goto out_unlock_iolock;
  1318. ASSERT(nimap == 0 || nimap == 1);
  1319. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  1320. xfs_daddr_t block;
  1321. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1322. block = imap.br_startblock;
  1323. mod = do_div(block, mp->m_sb.sb_rextsize);
  1324. if (mod)
  1325. startoffset_fsb += mp->m_sb.sb_rextsize - mod;
  1326. }
  1327. nimap = 1;
  1328. error = xfs_bmapi_read(ip, endoffset_fsb - 1, 1,
  1329. &imap, &nimap, 0);
  1330. if (error)
  1331. goto out_unlock_iolock;
  1332. ASSERT(nimap == 0 || nimap == 1);
  1333. if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
  1334. ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
  1335. mod++;
  1336. if (mod && (mod != mp->m_sb.sb_rextsize))
  1337. endoffset_fsb -= mod;
  1338. }
  1339. }
  1340. if ((done = (endoffset_fsb <= startoffset_fsb)))
  1341. /*
  1342. * One contiguous piece to clear
  1343. */
  1344. error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
  1345. else {
  1346. /*
  1347. * Some full blocks, possibly two pieces to clear
  1348. */
  1349. if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
  1350. error = xfs_zero_remaining_bytes(ip, offset,
  1351. XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
  1352. if (!error &&
  1353. XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
  1354. error = xfs_zero_remaining_bytes(ip,
  1355. XFS_FSB_TO_B(mp, endoffset_fsb),
  1356. offset + len - 1);
  1357. }
  1358. /*
  1359. * free file space until done or until there is an error
  1360. */
  1361. resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
  1362. while (!error && !done) {
  1363. /*
  1364. * allocate and setup the transaction. Allow this
  1365. * transaction to dip into the reserve blocks to ensure
  1366. * the freeing of the space succeeds at ENOSPC.
  1367. */
  1368. tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
  1369. tp->t_flags |= XFS_TRANS_RESERVE;
  1370. error = xfs_trans_reserve(tp,
  1371. resblks,
  1372. XFS_WRITE_LOG_RES(mp),
  1373. 0,
  1374. XFS_TRANS_PERM_LOG_RES,
  1375. XFS_WRITE_LOG_COUNT);
  1376. /*
  1377. * check for running out of space
  1378. */
  1379. if (error) {
  1380. /*
  1381. * Free the transaction structure.
  1382. */
  1383. ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
  1384. xfs_trans_cancel(tp, 0);
  1385. break;
  1386. }
  1387. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1388. error = xfs_trans_reserve_quota(tp, mp,
  1389. ip->i_udquot, ip->i_gdquot, ip->i_pdquot,
  1390. resblks, 0, XFS_QMOPT_RES_REGBLKS);
  1391. if (error)
  1392. goto error1;
  1393. xfs_trans_ijoin(tp, ip, 0);
  1394. /*
  1395. * issue the bunmapi() call to free the blocks
  1396. */
  1397. xfs_bmap_init(&free_list, &firstfsb);
  1398. error = xfs_bunmapi(tp, ip, startoffset_fsb,
  1399. endoffset_fsb - startoffset_fsb,
  1400. 0, 2, &firstfsb, &free_list, &done);
  1401. if (error) {
  1402. goto error0;
  1403. }
  1404. /*
  1405. * complete the transaction
  1406. */
  1407. error = xfs_bmap_finish(&tp, &free_list, &committed);
  1408. if (error) {
  1409. goto error0;
  1410. }
  1411. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  1412. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1413. }
  1414. out_unlock_iolock:
  1415. if (need_iolock)
  1416. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1417. return error;
  1418. error0:
  1419. xfs_bmap_cancel(&free_list);
  1420. error1:
  1421. xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
  1422. xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
  1423. XFS_ILOCK_EXCL);
  1424. return error;
  1425. }
  1426. STATIC int
  1427. xfs_zero_file_space(
  1428. struct xfs_inode *ip,
  1429. xfs_off_t offset,
  1430. xfs_off_t len,
  1431. int attr_flags)
  1432. {
  1433. struct xfs_mount *mp = ip->i_mount;
  1434. uint granularity;
  1435. xfs_off_t start_boundary;
  1436. xfs_off_t end_boundary;
  1437. int error;
  1438. granularity = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
  1439. /*
  1440. * Round the range of extents we are going to convert inwards. If the
  1441. * offset is aligned, then it doesn't get changed so we zero from the
  1442. * start of the block offset points to.
  1443. */
  1444. start_boundary = round_up(offset, granularity);
  1445. end_boundary = round_down(offset + len, granularity);
  1446. ASSERT(start_boundary >= offset);
  1447. ASSERT(end_boundary <= offset + len);
  1448. if (!(attr_flags & XFS_ATTR_NOLOCK))
  1449. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1450. if (start_boundary < end_boundary - 1) {
  1451. /* punch out the page cache over the conversion range */
  1452. truncate_pagecache_range(VFS_I(ip), start_boundary,
  1453. end_boundary - 1);
  1454. /* convert the blocks */
  1455. error = xfs_alloc_file_space(ip, start_boundary,
  1456. end_boundary - start_boundary - 1,
  1457. XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT,
  1458. attr_flags);
  1459. if (error)
  1460. goto out_unlock;
  1461. /* We've handled the interior of the range, now for the edges */
  1462. if (start_boundary != offset)
  1463. error = xfs_iozero(ip, offset, start_boundary - offset);
  1464. if (error)
  1465. goto out_unlock;
  1466. if (end_boundary != offset + len)
  1467. error = xfs_iozero(ip, end_boundary,
  1468. offset + len - end_boundary);
  1469. } else {
  1470. /*
  1471. * It's either a sub-granularity range or the range spanned lies
  1472. * partially across two adjacent blocks.
  1473. */
  1474. error = xfs_iozero(ip, offset, len);
  1475. }
  1476. out_unlock:
  1477. if (!(attr_flags & XFS_ATTR_NOLOCK))
  1478. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1479. return error;
  1480. }
  1481. /*
  1482. * xfs_change_file_space()
  1483. * This routine allocates or frees disk space for the given file.
  1484. * The user specified parameters are checked for alignment and size
  1485. * limitations.
  1486. *
  1487. * RETURNS:
  1488. * 0 on success
  1489. * errno on error
  1490. *
  1491. */
  1492. int
  1493. xfs_change_file_space(
  1494. xfs_inode_t *ip,
  1495. int cmd,
  1496. xfs_flock64_t *bf,
  1497. xfs_off_t offset,
  1498. int attr_flags)
  1499. {
  1500. xfs_mount_t *mp = ip->i_mount;
  1501. int clrprealloc;
  1502. int error;
  1503. xfs_fsize_t fsize;
  1504. int setprealloc;
  1505. xfs_off_t startoffset;
  1506. xfs_trans_t *tp;
  1507. struct iattr iattr;
  1508. if (!S_ISREG(ip->i_d.di_mode))
  1509. return XFS_ERROR(EINVAL);
  1510. switch (bf->l_whence) {
  1511. case 0: /*SEEK_SET*/
  1512. break;
  1513. case 1: /*SEEK_CUR*/
  1514. bf->l_start += offset;
  1515. break;
  1516. case 2: /*SEEK_END*/
  1517. bf->l_start += XFS_ISIZE(ip);
  1518. break;
  1519. default:
  1520. return XFS_ERROR(EINVAL);
  1521. }
  1522. /*
  1523. * length of <= 0 for resv/unresv/zero is invalid. length for
  1524. * alloc/free is ignored completely and we have no idea what userspace
  1525. * might have set it to, so set it to zero to allow range
  1526. * checks to pass.
  1527. */
  1528. switch (cmd) {
  1529. case XFS_IOC_ZERO_RANGE:
  1530. case XFS_IOC_RESVSP:
  1531. case XFS_IOC_RESVSP64:
  1532. case XFS_IOC_UNRESVSP:
  1533. case XFS_IOC_UNRESVSP64:
  1534. if (bf->l_len <= 0)
  1535. return XFS_ERROR(EINVAL);
  1536. break;
  1537. default:
  1538. bf->l_len = 0;
  1539. break;
  1540. }
  1541. if (bf->l_start < 0 ||
  1542. bf->l_start > mp->m_super->s_maxbytes ||
  1543. bf->l_start + bf->l_len < 0 ||
  1544. bf->l_start + bf->l_len >= mp->m_super->s_maxbytes)
  1545. return XFS_ERROR(EINVAL);
  1546. bf->l_whence = 0;
  1547. startoffset = bf->l_start;
  1548. fsize = XFS_ISIZE(ip);
  1549. setprealloc = clrprealloc = 0;
  1550. switch (cmd) {
  1551. case XFS_IOC_ZERO_RANGE:
  1552. error = xfs_zero_file_space(ip, startoffset, bf->l_len,
  1553. attr_flags);
  1554. if (error)
  1555. return error;
  1556. setprealloc = 1;
  1557. break;
  1558. case XFS_IOC_RESVSP:
  1559. case XFS_IOC_RESVSP64:
  1560. error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
  1561. XFS_BMAPI_PREALLOC, attr_flags);
  1562. if (error)
  1563. return error;
  1564. setprealloc = 1;
  1565. break;
  1566. case XFS_IOC_UNRESVSP:
  1567. case XFS_IOC_UNRESVSP64:
  1568. if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
  1569. attr_flags)))
  1570. return error;
  1571. break;
  1572. case XFS_IOC_ALLOCSP:
  1573. case XFS_IOC_ALLOCSP64:
  1574. case XFS_IOC_FREESP:
  1575. case XFS_IOC_FREESP64:
  1576. /*
  1577. * These operations actually do IO when extending the file, but
  1578. * the allocation is done seperately to the zeroing that is
  1579. * done. This set of operations need to be serialised against
  1580. * other IO operations, such as truncate and buffered IO. We
  1581. * need to take the IOLOCK here to serialise the allocation and
  1582. * zeroing IO to prevent other IOLOCK holders (e.g. getbmap,
  1583. * truncate, direct IO) from racing against the transient
  1584. * allocated but not written state we can have here.
  1585. */
  1586. xfs_ilock(ip, XFS_IOLOCK_EXCL);
  1587. if (startoffset > fsize) {
  1588. error = xfs_alloc_file_space(ip, fsize,
  1589. startoffset - fsize, 0,
  1590. attr_flags | XFS_ATTR_NOLOCK);
  1591. if (error) {
  1592. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1593. break;
  1594. }
  1595. }
  1596. iattr.ia_valid = ATTR_SIZE;
  1597. iattr.ia_size = startoffset;
  1598. error = xfs_setattr_size(ip, &iattr,
  1599. attr_flags | XFS_ATTR_NOLOCK);
  1600. xfs_iunlock(ip, XFS_IOLOCK_EXCL);
  1601. if (error)
  1602. return error;
  1603. clrprealloc = 1;
  1604. break;
  1605. default:
  1606. ASSERT(0);
  1607. return XFS_ERROR(EINVAL);
  1608. }
  1609. /*
  1610. * update the inode timestamp, mode, and prealloc flag bits
  1611. */
  1612. tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
  1613. if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
  1614. 0, 0, 0))) {
  1615. /* ASSERT(0); */
  1616. xfs_trans_cancel(tp, 0);
  1617. return error;
  1618. }
  1619. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1620. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  1621. if ((attr_flags & XFS_ATTR_DMI) == 0) {
  1622. ip->i_d.di_mode &= ~S_ISUID;
  1623. /*
  1624. * Note that we don't have to worry about mandatory
  1625. * file locking being disabled here because we only
  1626. * clear the S_ISGID bit if the Group execute bit is
  1627. * on, but if it was on then mandatory locking wouldn't
  1628. * have been enabled.
  1629. */
  1630. if (ip->i_d.di_mode & S_IXGRP)
  1631. ip->i_d.di_mode &= ~S_ISGID;
  1632. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1633. }
  1634. if (setprealloc)
  1635. ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
  1636. else if (clrprealloc)
  1637. ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
  1638. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  1639. if (attr_flags & XFS_ATTR_SYNC)
  1640. xfs_trans_set_sync(tp);
  1641. return xfs_trans_commit(tp, 0);
  1642. }