PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/fs/xfs/xfs_vnodeops.c

https://bitbucket.org/DutchDanny/bindroid-xtc-onex
C | 2394 lines | 1541 code | 287 blank | 566 comment | 333 complexity | e157a16cdbc83f260967e2297329a154 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

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

Large files files are truncated, but you can click here to view the full file