PageRenderTime 101ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/xfs/xfs_vnodeops.c

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