PageRenderTime 66ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/xfs/xfs_vnodeops.c

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