2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
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.
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.
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
21 #include "xfs_types.h"
25 #include "xfs_trans.h"
29 #include "xfs_mount.h"
30 #include "xfs_da_btree.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_inode_item.h"
36 #include "xfs_itable.h"
37 #include "xfs_ialloc.h"
38 #include "xfs_alloc.h"
43 #include "xfs_error.h"
44 #include "xfs_quota.h"
45 #include "xfs_utils.h"
46 #include "xfs_rtalloc.h"
47 #include "xfs_trans_space.h"
48 #include "xfs_log_priv.h"
49 #include "xfs_filestream.h"
50 #include "xfs_vnodeops.h"
51 #include "xfs_trace.h"
54 * The maximum pathlen is 1024 bytes. Since the minimum file system
55 * blocksize is 512 bytes, we can get a max of 2 extents back from
58 #define SYMLINK_MAPS 2
65 xfs_mount_t *mp = ip->i_mount;
66 int pathlen = ip->i_d.di_size;
67 int nmaps = SYMLINK_MAPS;
68 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
75 error = xfs_bmapi_read(ip, 0, XFS_B_TO_FSB(mp, pathlen), mval, &nmaps,
80 for (n = 0; n < nmaps; n++) {
81 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
82 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
84 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt),
85 XBF_LOCK | XBF_MAPPED | XBF_DONT_BLOCK);
87 return XFS_ERROR(ENOMEM);
90 xfs_buf_ioerror_alert(bp, __func__);
94 if (pathlen < byte_cnt)
98 memcpy(link, bp->b_addr, byte_cnt);
102 link[ip->i_d.di_size] = '\0';
114 xfs_mount_t *mp = ip->i_mount;
118 trace_xfs_readlink(ip);
120 if (XFS_FORCED_SHUTDOWN(mp))
121 return XFS_ERROR(EIO);
123 xfs_ilock(ip, XFS_ILOCK_SHARED);
125 pathlen = ip->i_d.di_size;
129 if (pathlen < 0 || pathlen > MAXPATHLEN) {
130 xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
131 __func__, (unsigned long long) ip->i_ino,
132 (long long) pathlen);
134 error = XFS_ERROR(EFSCORRUPTED);
139 if (ip->i_df.if_flags & XFS_IFINLINE) {
140 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
141 link[pathlen] = '\0';
143 error = xfs_readlink_bmap(ip, link);
147 xfs_iunlock(ip, XFS_ILOCK_SHARED);
152 * Flags for xfs_free_eofblocks
154 #define XFS_FREE_EOF_TRYLOCK (1<<0)
157 * This is called by xfs_inactive to free any blocks beyond eof
158 * when the link count isn't zero and by xfs_dm_punch_hole() when
159 * punching a hole to EOF.
169 xfs_fileoff_t end_fsb;
170 xfs_fileoff_t last_fsb;
171 xfs_filblks_t map_len;
173 xfs_bmbt_irec_t imap;
176 * Figure out if there are any blocks beyond the end
177 * of the file. If not, then there is nothing to do.
179 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
180 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
181 if (last_fsb <= end_fsb)
183 map_len = last_fsb - end_fsb;
186 xfs_ilock(ip, XFS_ILOCK_SHARED);
187 error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
188 xfs_iunlock(ip, XFS_ILOCK_SHARED);
190 if (!error && (nimaps != 0) &&
191 (imap.br_startblock != HOLESTARTBLOCK ||
192 ip->i_delayed_blks)) {
194 * Attach the dquots to the inode up front.
196 error = xfs_qm_dqattach(ip, 0);
201 * There are blocks after the end of file.
202 * Free them up now by truncating the file to
205 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
207 if (flags & XFS_FREE_EOF_TRYLOCK) {
208 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
209 xfs_trans_cancel(tp, 0);
213 xfs_ilock(ip, XFS_IOLOCK_EXCL);
216 error = xfs_trans_reserve(tp, 0,
217 XFS_ITRUNCATE_LOG_RES(mp),
218 0, XFS_TRANS_PERM_LOG_RES,
219 XFS_ITRUNCATE_LOG_COUNT);
221 ASSERT(XFS_FORCED_SHUTDOWN(mp));
222 xfs_trans_cancel(tp, 0);
223 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
227 xfs_ilock(ip, XFS_ILOCK_EXCL);
228 xfs_trans_ijoin(tp, ip, 0);
230 error = xfs_itruncate_data(&tp, ip, ip->i_size);
233 * If we get an error at this point we simply don't
234 * bother truncating the file.
237 (XFS_TRANS_RELEASE_LOG_RES |
240 error = xfs_trans_commit(tp,
241 XFS_TRANS_RELEASE_LOG_RES);
243 xfs_iunlock(ip, XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL);
249 * Free a symlink that has blocks associated with it.
252 xfs_inactive_symlink_rmt(
260 xfs_fsblock_t first_block;
261 xfs_bmap_free_t free_list;
264 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
272 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
274 * We're freeing a symlink that has some
275 * blocks allocated to it. Free the
276 * blocks here. We know that we've got
277 * either 1 or 2 extents and that we can
278 * free them all in one bunmapi call.
280 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
281 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
282 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
283 ASSERT(XFS_FORCED_SHUTDOWN(mp));
284 xfs_trans_cancel(tp, 0);
289 * Lock the inode, fix the size, and join it to the transaction.
290 * Hold it so in the normal path, we still have it locked for
291 * the second transaction. In the error paths we need it
292 * held so the cancel won't rele it, see below.
294 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
295 size = (int)ip->i_d.di_size;
297 xfs_trans_ijoin(tp, ip, 0);
298 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
300 * Find the block(s) so we can inval and unmap them.
303 xfs_bmap_init(&free_list, &first_block);
304 nmaps = ARRAY_SIZE(mval);
305 error = xfs_bmapi_read(ip, 0, XFS_B_TO_FSB(mp, size),
310 * Invalidate the block(s).
312 for (i = 0; i < nmaps; i++) {
313 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
314 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
315 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
320 xfs_trans_binval(tp, bp);
323 * Unmap the dead block(s) to the free_list.
325 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
326 &first_block, &free_list, &done)))
330 * Commit the first transaction. This logs the EFI and the inode.
332 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
335 * The transaction must have been committed, since there were
336 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
337 * The new tp has the extent freeing and EFDs.
341 * The first xact was committed, so add the inode to the new one.
342 * Mark it dirty so it will be logged and moved forward in the log as
343 * part of every commit.
345 xfs_trans_ijoin(tp, ip, 0);
346 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
348 * Get a new, empty transaction to return to our caller.
350 ntp = xfs_trans_dup(tp);
352 * Commit the transaction containing extent freeing and EFDs.
353 * If we get an error on the commit here or on the reserve below,
354 * we need to unlock the inode since the new transaction doesn't
355 * have the inode attached.
357 error = xfs_trans_commit(tp, 0);
360 ASSERT(XFS_FORCED_SHUTDOWN(mp));
364 * transaction commit worked ok so we can drop the extra ticket
365 * reference that we gained in xfs_trans_dup()
367 xfs_log_ticket_put(tp->t_ticket);
370 * Remove the memory for extent descriptions (just bookkeeping).
372 if (ip->i_df.if_bytes)
373 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
374 ASSERT(ip->i_df.if_bytes == 0);
376 * Put an itruncate log reservation in the new transaction
379 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
380 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
381 ASSERT(XFS_FORCED_SHUTDOWN(mp));
385 * Return with the inode locked but not joined to the transaction.
391 xfs_bmap_cancel(&free_list);
394 * Have to come here with the inode locked and either
395 * (held and in the transaction) or (not in the transaction).
396 * If the inode isn't held then cancel would iput it, but
397 * that's wrong since this is inactive and the vnode ref
398 * count is 0 already.
399 * Cancel won't do anything to the inode if held, but it still
400 * needs to be locked until the cancel is done, if it was
401 * joined to the transaction.
403 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
404 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
411 xfs_inactive_symlink_local(
417 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
419 * We're freeing a symlink which fit into
420 * the inode. Just free the memory used
421 * to hold the old symlink.
423 error = xfs_trans_reserve(*tpp, 0,
424 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
425 0, XFS_TRANS_PERM_LOG_RES,
426 XFS_ITRUNCATE_LOG_COUNT);
429 xfs_trans_cancel(*tpp, 0);
433 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
436 * Zero length symlinks _can_ exist.
438 if (ip->i_df.if_bytes > 0) {
439 xfs_idata_realloc(ip,
440 -(ip->i_df.if_bytes),
442 ASSERT(ip->i_df.if_bytes == 0);
456 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
459 ASSERT(ip->i_d.di_forkoff != 0);
460 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
461 xfs_iunlock(ip, XFS_ILOCK_EXCL);
465 error = xfs_attr_inactive(ip);
469 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
470 error = xfs_trans_reserve(tp, 0,
471 XFS_IFREE_LOG_RES(mp),
472 0, XFS_TRANS_PERM_LOG_RES,
473 XFS_INACTIVE_LOG_COUNT);
477 xfs_ilock(ip, XFS_ILOCK_EXCL);
478 xfs_trans_ijoin(tp, ip, 0);
479 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
481 ASSERT(ip->i_d.di_anextents == 0);
487 ASSERT(XFS_FORCED_SHUTDOWN(mp));
488 xfs_trans_cancel(tp, 0);
491 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
499 xfs_mount_t *mp = ip->i_mount;
502 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
505 /* If this is a read-only mount, don't do this (would generate I/O) */
506 if (mp->m_flags & XFS_MOUNT_RDONLY)
509 if (!XFS_FORCED_SHUTDOWN(mp)) {
513 * If we are using filestreams, and we have an unlinked
514 * file that we are processing the last close on, then nothing
515 * will be able to reopen and write to this file. Purge this
516 * inode from the filestreams cache so that it doesn't delay
517 * teardown of the inode.
519 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
520 xfs_filestream_deassociate(ip);
523 * If we previously truncated this file and removed old data
524 * in the process, we want to initiate "early" writeout on
525 * the last close. This is an attempt to combat the notorious
526 * NULL files problem which is particularly noticeable from a
527 * truncate down, buffered (re-)write (delalloc), followed by
528 * a crash. What we are effectively doing here is
529 * significantly reducing the time window where we'd otherwise
530 * be exposed to that problem.
532 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
534 xfs_iflags_clear(ip, XFS_IDIRTY_RELEASE);
535 if (VN_DIRTY(VFS_I(ip)) && ip->i_delayed_blks > 0)
536 xfs_flush_pages(ip, 0, -1, XBF_ASYNC, FI_NONE);
540 if (ip->i_d.di_nlink == 0)
543 if ((S_ISREG(ip->i_d.di_mode) &&
544 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
545 ip->i_delayed_blks > 0)) &&
546 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
547 (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
550 * If we can't get the iolock just skip truncating the blocks
551 * past EOF because we could deadlock with the mmap_sem
552 * otherwise. We'll get another chance to drop them once the
553 * last reference to the inode is dropped, so we'll never leak
554 * blocks permanently.
556 * Further, check if the inode is being opened, written and
557 * closed frequently and we have delayed allocation blocks
558 * outstanding (e.g. streaming writes from the NFS server),
559 * truncating the blocks past EOF will cause fragmentation to
562 * In this case don't do the truncation, either, but we have to
563 * be careful how we detect this case. Blocks beyond EOF show
564 * up as i_delayed_blks even when the inode is clean, so we
565 * need to truncate them away first before checking for a dirty
566 * release. Hence on the first dirty close we will still remove
567 * the speculative allocation, but after that we will leave it
570 if (xfs_iflags_test(ip, XFS_IDIRTY_RELEASE))
573 error = xfs_free_eofblocks(mp, ip,
574 XFS_FREE_EOF_TRYLOCK);
578 /* delalloc blocks after truncation means it really is dirty */
579 if (ip->i_delayed_blks)
580 xfs_iflags_set(ip, XFS_IDIRTY_RELEASE);
588 * This is called when the vnode reference count for the vnode
589 * goes to zero. If the file has been unlinked, then it must
590 * now be truncated. Also, we clear all of the read-ahead state
591 * kept for the inode here since the file is now closed.
597 xfs_bmap_free_t free_list;
598 xfs_fsblock_t first_block;
606 * If the inode is already free, then there can be nothing
609 if (ip->i_d.di_mode == 0 || is_bad_inode(VFS_I(ip))) {
610 ASSERT(ip->i_df.if_real_bytes == 0);
611 ASSERT(ip->i_df.if_broot_bytes == 0);
612 return VN_INACTIVE_CACHE;
616 * Only do a truncate if it's a regular file with
617 * some actual space in it. It's OK to look at the
618 * inode's fields without the lock because we're the
619 * only one with a reference to the inode.
621 truncate = ((ip->i_d.di_nlink == 0) &&
622 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
623 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
624 S_ISREG(ip->i_d.di_mode));
630 /* If this is a read-only mount, don't do this (would generate I/O) */
631 if (mp->m_flags & XFS_MOUNT_RDONLY)
634 if (ip->i_d.di_nlink != 0) {
635 if ((S_ISREG(ip->i_d.di_mode) &&
636 ((ip->i_size > 0) || (VN_CACHED(VFS_I(ip)) > 0 ||
637 ip->i_delayed_blks > 0)) &&
638 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
639 (!(ip->i_d.di_flags &
640 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
641 (ip->i_delayed_blks != 0)))) {
642 error = xfs_free_eofblocks(mp, ip, 0);
644 return VN_INACTIVE_CACHE;
649 ASSERT(ip->i_d.di_nlink == 0);
651 error = xfs_qm_dqattach(ip, 0);
653 return VN_INACTIVE_CACHE;
655 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
657 xfs_ilock(ip, XFS_IOLOCK_EXCL);
659 error = xfs_trans_reserve(tp, 0,
660 XFS_ITRUNCATE_LOG_RES(mp),
661 0, XFS_TRANS_PERM_LOG_RES,
662 XFS_ITRUNCATE_LOG_COUNT);
664 /* Don't call itruncate_cleanup */
665 ASSERT(XFS_FORCED_SHUTDOWN(mp));
666 xfs_trans_cancel(tp, 0);
667 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
668 return VN_INACTIVE_CACHE;
671 xfs_ilock(ip, XFS_ILOCK_EXCL);
672 xfs_trans_ijoin(tp, ip, 0);
674 error = xfs_itruncate_data(&tp, ip, 0);
677 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
678 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
679 return VN_INACTIVE_CACHE;
681 } else if (S_ISLNK(ip->i_d.di_mode)) {
684 * If we get an error while cleaning up a
685 * symlink we bail out.
687 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
688 xfs_inactive_symlink_rmt(ip, &tp) :
689 xfs_inactive_symlink_local(ip, &tp);
693 return VN_INACTIVE_CACHE;
696 xfs_trans_ijoin(tp, ip, 0);
698 error = xfs_trans_reserve(tp, 0,
699 XFS_IFREE_LOG_RES(mp),
700 0, XFS_TRANS_PERM_LOG_RES,
701 XFS_INACTIVE_LOG_COUNT);
703 ASSERT(XFS_FORCED_SHUTDOWN(mp));
704 xfs_trans_cancel(tp, 0);
705 return VN_INACTIVE_CACHE;
708 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
709 xfs_trans_ijoin(tp, ip, 0);
713 * If there are attributes associated with the file
714 * then blow them away now. The code calls a routine
715 * that recursively deconstructs the attribute fork.
716 * We need to just commit the current transaction
717 * because we can't use it for xfs_attr_inactive().
719 if (ip->i_d.di_anextents > 0) {
720 error = xfs_inactive_attrs(ip, &tp);
722 * If we got an error, the transaction is already
723 * cancelled, and the inode is unlocked. Just get out.
726 return VN_INACTIVE_CACHE;
727 } else if (ip->i_afp) {
728 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
734 xfs_bmap_init(&free_list, &first_block);
735 error = xfs_ifree(tp, ip, &free_list);
738 * If we fail to free the inode, shut down. The cancel
739 * might do that, we need to make sure. Otherwise the
740 * inode might be lost for a long time or forever.
742 if (!XFS_FORCED_SHUTDOWN(mp)) {
743 xfs_notice(mp, "%s: xfs_ifree returned error %d",
745 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
747 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
750 * Credit the quota account(s). The inode is gone.
752 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
755 * Just ignore errors at this point. There is nothing we can
756 * do except to try to keep going. Make sure it's not a silent
759 error = xfs_bmap_finish(&tp, &free_list, &committed);
761 xfs_notice(mp, "%s: xfs_bmap_finish returned error %d",
763 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
765 xfs_notice(mp, "%s: xfs_trans_commit returned error %d",
770 * Release the dquots held by inode, if any.
773 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
776 return VN_INACTIVE_CACHE;
780 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
781 * is allowed, otherwise it has to be an exact match. If a CI match is found,
782 * ci_name->name will point to a the actual name (caller must free) or
783 * will be set to NULL if an exact match is found.
788 struct xfs_name *name,
790 struct xfs_name *ci_name)
796 trace_xfs_lookup(dp, name);
798 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
799 return XFS_ERROR(EIO);
801 lock_mode = xfs_ilock_map_shared(dp);
802 error = xfs_dir_lookup(NULL, dp, name, &inum, ci_name);
803 xfs_iunlock_map_shared(dp, lock_mode);
808 error = xfs_iget(dp->i_mount, NULL, inum, 0, 0, ipp);
816 kmem_free(ci_name->name);
825 struct xfs_name *name,
830 int is_dir = S_ISDIR(mode);
831 struct xfs_mount *mp = dp->i_mount;
832 struct xfs_inode *ip = NULL;
833 struct xfs_trans *tp = NULL;
835 xfs_bmap_free_t free_list;
836 xfs_fsblock_t first_block;
837 boolean_t unlock_dp_on_error = B_FALSE;
841 struct xfs_dquot *udqp = NULL;
842 struct xfs_dquot *gdqp = NULL;
847 trace_xfs_create(dp, name);
849 if (XFS_FORCED_SHUTDOWN(mp))
850 return XFS_ERROR(EIO);
852 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
853 prid = xfs_get_projid(dp);
855 prid = XFS_PROJID_DEFAULT;
858 * Make sure that we have allocated dquot(s) on disk.
860 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
861 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
867 resblks = XFS_MKDIR_SPACE_RES(mp, name->len);
868 log_res = XFS_MKDIR_LOG_RES(mp);
869 log_count = XFS_MKDIR_LOG_COUNT;
870 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
872 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
873 log_res = XFS_CREATE_LOG_RES(mp);
874 log_count = XFS_CREATE_LOG_COUNT;
875 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
878 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
881 * Initially assume that the file does not exist and
882 * reserve the resources for that case. If that is not
883 * the case we'll drop the one we have and get a more
884 * appropriate transaction later.
886 error = xfs_trans_reserve(tp, resblks, log_res, 0,
887 XFS_TRANS_PERM_LOG_RES, log_count);
888 if (error == ENOSPC) {
889 /* flush outstanding delalloc blocks and retry */
890 xfs_flush_inodes(dp);
891 error = xfs_trans_reserve(tp, resblks, log_res, 0,
892 XFS_TRANS_PERM_LOG_RES, log_count);
894 if (error == ENOSPC) {
895 /* No space at all so try a "no-allocation" reservation */
897 error = xfs_trans_reserve(tp, 0, log_res, 0,
898 XFS_TRANS_PERM_LOG_RES, log_count);
902 goto out_trans_cancel;
905 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
906 unlock_dp_on_error = B_TRUE;
909 * Check for directory link count overflow.
911 if (is_dir && dp->i_d.di_nlink >= XFS_MAXLINK) {
912 error = XFS_ERROR(EMLINK);
913 goto out_trans_cancel;
916 xfs_bmap_init(&free_list, &first_block);
919 * Reserve disk quota and the inode.
921 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
923 goto out_trans_cancel;
925 error = xfs_dir_canenter(tp, dp, name, resblks);
927 goto out_trans_cancel;
930 * A newly created regular or special file just has one directory
931 * entry pointing to them, but a directory also the "." entry
932 * pointing to itself.
934 error = xfs_dir_ialloc(&tp, dp, mode, is_dir ? 2 : 1, rdev,
935 prid, resblks > 0, &ip, &committed);
938 goto out_trans_cancel;
939 goto out_trans_abort;
943 * Now we join the directory inode to the transaction. We do not do it
944 * earlier because xfs_dir_ialloc might commit the previous transaction
945 * (and release all the locks). An error from here on will result in
946 * the transaction cancel unlocking dp so don't do it explicitly in the
949 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
950 unlock_dp_on_error = B_FALSE;
952 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
953 &first_block, &free_list, resblks ?
954 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
956 ASSERT(error != ENOSPC);
957 goto out_trans_abort;
959 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
960 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
963 error = xfs_dir_init(tp, ip, dp);
965 goto out_bmap_cancel;
967 error = xfs_bumplink(tp, dp);
969 goto out_bmap_cancel;
973 * If this is a synchronous mount, make sure that the
974 * create transaction goes to disk before returning to
977 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
978 xfs_trans_set_sync(tp);
981 * Attach the dquot(s) to the inodes and modify them incore.
982 * These ids of the inode couldn't have changed since the new
983 * inode has been locked ever since it was created.
985 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
987 error = xfs_bmap_finish(&tp, &free_list, &committed);
989 goto out_bmap_cancel;
991 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
993 goto out_release_inode;
1002 xfs_bmap_cancel(&free_list);
1004 cancel_flags |= XFS_TRANS_ABORT;
1006 xfs_trans_cancel(tp, cancel_flags);
1009 * Wait until after the current transaction is aborted to
1010 * release the inode. This prevents recursive transactions
1011 * and deadlocks from xfs_inactive.
1016 xfs_qm_dqrele(udqp);
1017 xfs_qm_dqrele(gdqp);
1019 if (unlock_dp_on_error)
1020 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1026 int xfs_small_retries;
1027 int xfs_middle_retries;
1028 int xfs_lots_retries;
1029 int xfs_lock_delays;
1033 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1037 xfs_lock_inumorder(int lock_mode, int subclass)
1039 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1040 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
1041 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
1042 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
1048 * The following routine will lock n inodes in exclusive mode.
1049 * We assume the caller calls us with the inodes in i_ino order.
1051 * We need to detect deadlock where an inode that we lock
1052 * is in the AIL and we start waiting for another inode that is locked
1053 * by a thread in a long running transaction (such as truncate). This can
1054 * result in deadlock since the long running trans might need to wait
1055 * for the inode we just locked in order to push the tail and free space
1064 int attempts = 0, i, j, try_lock;
1067 ASSERT(ips && (inodes >= 2)); /* we need at least two */
1073 for (; i < inodes; i++) {
1076 if (i && (ips[i] == ips[i-1])) /* Already locked */
1080 * If try_lock is not set yet, make sure all locked inodes
1081 * are not in the AIL.
1082 * If any are, set try_lock to be used later.
1086 for (j = (i - 1); j >= 0 && !try_lock; j--) {
1087 lp = (xfs_log_item_t *)ips[j]->i_itemp;
1088 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1095 * If any of the previous locks we have locked is in the AIL,
1096 * we must TRY to get the second and subsequent locks. If
1097 * we can't get any, we must release all we have
1102 /* try_lock must be 0 if i is 0. */
1104 * try_lock means we have an inode locked
1105 * that is in the AIL.
1108 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
1112 * Unlock all previous guys and try again.
1113 * xfs_iunlock will try to push the tail
1114 * if the inode is in the AIL.
1117 for(j = i - 1; j >= 0; j--) {
1120 * Check to see if we've already
1121 * unlocked this one.
1122 * Not the first one going back,
1123 * and the inode ptr is the same.
1125 if ((j != (i - 1)) && ips[j] ==
1129 xfs_iunlock(ips[j], lock_mode);
1132 if ((attempts % 5) == 0) {
1133 delay(1); /* Don't just spin the CPU */
1143 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
1149 if (attempts < 5) xfs_small_retries++;
1150 else if (attempts < 100) xfs_middle_retries++;
1151 else xfs_lots_retries++;
1159 * xfs_lock_two_inodes() can only be used to lock one type of lock
1160 * at a time - the iolock or the ilock, but not both at once. If
1161 * we lock both at once, lockdep will report false positives saying
1162 * we have violated locking orders.
1165 xfs_lock_two_inodes(
1174 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
1175 ASSERT((lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL)) == 0);
1176 ASSERT(ip0->i_ino != ip1->i_ino);
1178 if (ip0->i_ino > ip1->i_ino) {
1185 xfs_ilock(ip0, xfs_lock_inumorder(lock_mode, 0));
1188 * If the first lock we have locked is in the AIL, we must TRY to get
1189 * the second lock. If we can't get it, we must release the first one
1192 lp = (xfs_log_item_t *)ip0->i_itemp;
1193 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
1194 if (!xfs_ilock_nowait(ip1, xfs_lock_inumorder(lock_mode, 1))) {
1195 xfs_iunlock(ip0, lock_mode);
1196 if ((++attempts % 5) == 0)
1197 delay(1); /* Don't just spin the CPU */
1201 xfs_ilock(ip1, xfs_lock_inumorder(lock_mode, 1));
1208 struct xfs_name *name,
1211 xfs_mount_t *mp = dp->i_mount;
1212 xfs_trans_t *tp = NULL;
1213 int is_dir = S_ISDIR(ip->i_d.di_mode);
1215 xfs_bmap_free_t free_list;
1216 xfs_fsblock_t first_block;
1223 trace_xfs_remove(dp, name);
1225 if (XFS_FORCED_SHUTDOWN(mp))
1226 return XFS_ERROR(EIO);
1228 error = xfs_qm_dqattach(dp, 0);
1232 error = xfs_qm_dqattach(ip, 0);
1237 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
1238 log_count = XFS_DEFAULT_LOG_COUNT;
1240 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
1241 log_count = XFS_REMOVE_LOG_COUNT;
1243 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1246 * We try to get the real space reservation first,
1247 * allowing for directory btree deletion(s) implying
1248 * possible bmap insert(s). If we can't get the space
1249 * reservation then we use 0 instead, and avoid the bmap
1250 * btree insert(s) in the directory code by, if the bmap
1251 * insert tries to happen, instead trimming the LAST
1252 * block from the directory.
1254 resblks = XFS_REMOVE_SPACE_RES(mp);
1255 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
1256 XFS_TRANS_PERM_LOG_RES, log_count);
1257 if (error == ENOSPC) {
1259 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
1260 XFS_TRANS_PERM_LOG_RES, log_count);
1263 ASSERT(error != ENOSPC);
1265 goto out_trans_cancel;
1268 xfs_lock_two_inodes(dp, ip, XFS_ILOCK_EXCL);
1270 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1271 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1274 * If we're removing a directory perform some additional validation.
1277 ASSERT(ip->i_d.di_nlink >= 2);
1278 if (ip->i_d.di_nlink != 2) {
1279 error = XFS_ERROR(ENOTEMPTY);
1280 goto out_trans_cancel;
1282 if (!xfs_dir_isempty(ip)) {
1283 error = XFS_ERROR(ENOTEMPTY);
1284 goto out_trans_cancel;
1288 xfs_bmap_init(&free_list, &first_block);
1289 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
1290 &first_block, &free_list, resblks);
1292 ASSERT(error != ENOENT);
1293 goto out_bmap_cancel;
1295 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1299 * Drop the link from ip's "..".
1301 error = xfs_droplink(tp, dp);
1303 goto out_bmap_cancel;
1306 * Drop the "." link from ip to self.
1308 error = xfs_droplink(tp, ip);
1310 goto out_bmap_cancel;
1313 * When removing a non-directory we need to log the parent
1314 * inode here. For a directory this is done implicitly
1315 * by the xfs_droplink call for the ".." entry.
1317 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1321 * Drop the link from dp to ip.
1323 error = xfs_droplink(tp, ip);
1325 goto out_bmap_cancel;
1328 * Determine if this is the last link while
1329 * we are in the transaction.
1331 link_zero = (ip->i_d.di_nlink == 0);
1334 * If this is a synchronous mount, make sure that the
1335 * remove transaction goes to disk before returning to
1338 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC))
1339 xfs_trans_set_sync(tp);
1341 error = xfs_bmap_finish(&tp, &free_list, &committed);
1343 goto out_bmap_cancel;
1345 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1350 * If we are using filestreams, kill the stream association.
1351 * If the file is still open it may get a new one but that
1352 * will get killed on last close in xfs_close() so we don't
1353 * have to worry about that.
1355 if (!is_dir && link_zero && xfs_inode_is_filestream(ip))
1356 xfs_filestream_deassociate(ip);
1361 xfs_bmap_cancel(&free_list);
1362 cancel_flags |= XFS_TRANS_ABORT;
1364 xfs_trans_cancel(tp, cancel_flags);
1373 struct xfs_name *target_name)
1375 xfs_mount_t *mp = tdp->i_mount;
1378 xfs_bmap_free_t free_list;
1379 xfs_fsblock_t first_block;
1384 trace_xfs_link(tdp, target_name);
1386 ASSERT(!S_ISDIR(sip->i_d.di_mode));
1388 if (XFS_FORCED_SHUTDOWN(mp))
1389 return XFS_ERROR(EIO);
1391 error = xfs_qm_dqattach(sip, 0);
1395 error = xfs_qm_dqattach(tdp, 0);
1399 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
1400 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1401 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
1402 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
1403 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1404 if (error == ENOSPC) {
1406 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
1407 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
1414 xfs_lock_two_inodes(sip, tdp, XFS_ILOCK_EXCL);
1416 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
1417 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
1420 * If the source has too many links, we can't make any more to it.
1422 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
1423 error = XFS_ERROR(EMLINK);
1428 * If we are using project inheritance, we only allow hard link
1429 * creation in our tree when the project IDs are the same; else
1430 * the tree quota mechanism could be circumvented.
1432 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
1433 (xfs_get_projid(tdp) != xfs_get_projid(sip)))) {
1434 error = XFS_ERROR(EXDEV);
1438 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
1442 xfs_bmap_init(&free_list, &first_block);
1444 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
1445 &first_block, &free_list, resblks);
1448 xfs_trans_ichgtime(tp, tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1449 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
1451 error = xfs_bumplink(tp, sip);
1456 * If this is a synchronous mount, make sure that the
1457 * link transaction goes to disk before returning to
1460 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1461 xfs_trans_set_sync(tp);
1464 error = xfs_bmap_finish (&tp, &free_list, &committed);
1466 xfs_bmap_cancel(&free_list);
1470 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1473 cancel_flags |= XFS_TRANS_ABORT;
1475 xfs_trans_cancel(tp, cancel_flags);
1483 struct xfs_name *link_name,
1484 const char *target_path,
1488 xfs_mount_t *mp = dp->i_mount;
1493 xfs_bmap_free_t free_list;
1494 xfs_fsblock_t first_block;
1495 boolean_t unlock_dp_on_error = B_FALSE;
1498 xfs_fileoff_t first_fsb;
1499 xfs_filblks_t fs_blocks;
1501 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1503 const char *cur_chunk;
1508 struct xfs_dquot *udqp, *gdqp;
1516 trace_xfs_symlink(dp, link_name);
1518 if (XFS_FORCED_SHUTDOWN(mp))
1519 return XFS_ERROR(EIO);
1522 * Check component lengths of the target path name.
1524 pathlen = strlen(target_path);
1525 if (pathlen >= MAXPATHLEN) /* total string too long */
1526 return XFS_ERROR(ENAMETOOLONG);
1529 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1530 prid = xfs_get_projid(dp);
1532 prid = XFS_PROJID_DEFAULT;
1535 * Make sure that we have allocated dquot(s) on disk.
1537 error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
1538 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
1542 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
1543 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1545 * The symlink will fit into the inode data fork?
1546 * There can't be any attributes so we get the whole variable part.
1548 if (pathlen <= XFS_LITINO(mp))
1551 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
1552 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
1553 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
1554 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
1555 if (error == ENOSPC && fs_blocks == 0) {
1557 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
1558 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
1565 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1566 unlock_dp_on_error = B_TRUE;
1569 * Check whether the directory allows new symlinks or not.
1571 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
1572 error = XFS_ERROR(EPERM);
1577 * Reserve disk quota : blocks and inode.
1579 error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
1584 * Check for ability to enter directory entry, if no space reserved.
1586 error = xfs_dir_canenter(tp, dp, link_name, resblks);
1590 * Initialize the bmap freelist prior to calling either
1591 * bmapi or the directory create code.
1593 xfs_bmap_init(&free_list, &first_block);
1596 * Allocate an inode for the symlink.
1598 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
1599 prid, resblks > 0, &ip, NULL);
1601 if (error == ENOSPC)
1607 * An error after we've joined dp to the transaction will result in the
1608 * transaction cancel unlocking dp so don't do it explicitly in the
1611 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1612 unlock_dp_on_error = B_FALSE;
1615 * Also attach the dquot(s) to it, if applicable.
1617 xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
1620 resblks -= XFS_IALLOC_SPACE_RES(mp);
1622 * If the symlink will fit into the inode, write it inline.
1624 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
1625 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
1626 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
1627 ip->i_d.di_size = pathlen;
1630 * The inode was initially created in extent format.
1632 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
1633 ip->i_df.if_flags |= XFS_IFINLINE;
1635 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
1636 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
1640 nmaps = SYMLINK_MAPS;
1642 error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
1643 XFS_BMAPI_METADATA, &first_block, resblks,
1644 mval, &nmaps, &free_list);
1649 resblks -= fs_blocks;
1650 ip->i_d.di_size = pathlen;
1651 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1653 cur_chunk = target_path;
1654 for (n = 0; n < nmaps; n++) {
1655 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1656 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1657 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
1658 BTOBB(byte_cnt), 0);
1663 if (pathlen < byte_cnt) {
1666 pathlen -= byte_cnt;
1668 memcpy(bp->b_addr, cur_chunk, byte_cnt);
1669 cur_chunk += byte_cnt;
1671 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
1676 * Create the directory entry for the symlink.
1678 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
1679 &first_block, &free_list, resblks);
1682 xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1683 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1686 * If this is a synchronous mount, make sure that the
1687 * symlink transaction goes to disk before returning to
1690 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1691 xfs_trans_set_sync(tp);
1694 error = xfs_bmap_finish(&tp, &free_list, &committed);
1698 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1699 xfs_qm_dqrele(udqp);
1700 xfs_qm_dqrele(gdqp);
1708 xfs_bmap_cancel(&free_list);
1709 cancel_flags |= XFS_TRANS_ABORT;
1711 xfs_trans_cancel(tp, cancel_flags);
1712 xfs_qm_dqrele(udqp);
1713 xfs_qm_dqrele(gdqp);
1715 if (unlock_dp_on_error)
1716 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1727 xfs_mount_t *mp = ip->i_mount;
1731 if (!capable(CAP_SYS_ADMIN))
1732 return XFS_ERROR(EPERM);
1734 if (XFS_FORCED_SHUTDOWN(mp))
1735 return XFS_ERROR(EIO);
1737 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
1738 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
1740 xfs_trans_cancel(tp, 0);
1743 xfs_ilock(ip, XFS_ILOCK_EXCL);
1744 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1746 ip->i_d.di_dmevmask = evmask;
1747 ip->i_d.di_dmstate = state;
1749 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1750 error = xfs_trans_commit(tp, 0);
1756 * xfs_alloc_file_space()
1757 * This routine allocates disk space for the given file.
1759 * If alloc_type == 0, this request is for an ALLOCSP type
1760 * request which will change the file size. In this case, no
1761 * DMAPI event will be generated by the call. A TRUNCATE event
1762 * will be generated later by xfs_setattr.
1764 * If alloc_type != 0, this request is for a RESVSP type
1765 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
1766 * lower block boundary byte address is less than the file's
1775 xfs_alloc_file_space(
1782 xfs_mount_t *mp = ip->i_mount;
1784 xfs_filblks_t allocated_fsb;
1785 xfs_filblks_t allocatesize_fsb;
1786 xfs_extlen_t extsz, temp;
1787 xfs_fileoff_t startoffset_fsb;
1788 xfs_fsblock_t firstfsb;
1793 xfs_bmbt_irec_t imaps[1], *imapp;
1794 xfs_bmap_free_t free_list;
1795 uint qblocks, resblks, resrtextents;
1799 trace_xfs_alloc_file_space(ip);
1801 if (XFS_FORCED_SHUTDOWN(mp))
1802 return XFS_ERROR(EIO);
1804 error = xfs_qm_dqattach(ip, 0);
1809 return XFS_ERROR(EINVAL);
1811 rt = XFS_IS_REALTIME_INODE(ip);
1812 extsz = xfs_get_extsz_hint(ip);
1817 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
1818 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
1821 * Allocate file space until done or until there is an error
1823 while (allocatesize_fsb && !error) {
1827 * Determine space reservations for data/realtime.
1829 if (unlikely(extsz)) {
1830 s = startoffset_fsb;
1833 e = startoffset_fsb + allocatesize_fsb;
1834 if ((temp = do_mod(startoffset_fsb, extsz)))
1836 if ((temp = do_mod(e, extsz)))
1840 e = allocatesize_fsb;
1844 * The transaction reservation is limited to a 32-bit block
1845 * count, hence we need to limit the number of blocks we are
1846 * trying to reserve to avoid an overflow. We can't allocate
1847 * more than @nimaps extents, and an extent is limited on disk
1848 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
1850 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
1852 resrtextents = qblocks = resblks;
1853 resrtextents /= mp->m_sb.sb_rextsize;
1854 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1855 quota_flag = XFS_QMOPT_RES_RTBLKS;
1858 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
1859 quota_flag = XFS_QMOPT_RES_REGBLKS;
1863 * Allocate and setup the transaction.
1865 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
1866 error = xfs_trans_reserve(tp, resblks,
1867 XFS_WRITE_LOG_RES(mp), resrtextents,
1868 XFS_TRANS_PERM_LOG_RES,
1869 XFS_WRITE_LOG_COUNT);
1871 * Check for running out of space
1875 * Free the transaction structure.
1877 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1878 xfs_trans_cancel(tp, 0);
1881 xfs_ilock(ip, XFS_ILOCK_EXCL);
1882 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
1887 xfs_trans_ijoin(tp, ip, 0);
1889 xfs_bmap_init(&free_list, &firstfsb);
1890 error = xfs_bmapi_write(tp, ip, startoffset_fsb,
1891 allocatesize_fsb, alloc_type, &firstfsb,
1892 0, imapp, &nimaps, &free_list);
1898 * Complete the transaction
1900 error = xfs_bmap_finish(&tp, &free_list, &committed);
1905 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1906 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1911 allocated_fsb = imapp->br_blockcount;
1914 error = XFS_ERROR(ENOSPC);
1918 startoffset_fsb += allocated_fsb;
1919 allocatesize_fsb -= allocated_fsb;
1924 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
1925 xfs_bmap_cancel(&free_list);
1926 xfs_trans_unreserve_quota_nblks(tp, ip, qblocks, 0, quota_flag);
1928 error1: /* Just cancel transaction */
1929 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1930 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1935 * Zero file bytes between startoff and endoff inclusive.
1936 * The iolock is held exclusive and no blocks are buffered.
1938 * This function is used by xfs_free_file_space() to zero
1939 * partial blocks when the range to free is not block aligned.
1940 * When unreserving space with boundaries that are not block
1941 * aligned we round up the start and round down the end
1942 * boundaries and then use this function to zero the parts of
1943 * the blocks that got dropped during the rounding.
1946 xfs_zero_remaining_bytes(
1951 xfs_bmbt_irec_t imap;
1952 xfs_fileoff_t offset_fsb;
1953 xfs_off_t lastoffset;
1956 xfs_mount_t *mp = ip->i_mount;
1961 * Avoid doing I/O beyond eof - it's not necessary
1962 * since nothing can read beyond eof. The space will
1963 * be zeroed when the file is extended anyway.
1965 if (startoff >= ip->i_size)
1968 if (endoff > ip->i_size)
1969 endoff = ip->i_size;
1971 bp = xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip) ?
1972 mp->m_rtdev_targp : mp->m_ddev_targp,
1973 mp->m_sb.sb_blocksize, XBF_DONT_BLOCK);
1975 return XFS_ERROR(ENOMEM);
1979 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
1980 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1982 error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0);
1983 if (error || nimap < 1)
1985 ASSERT(imap.br_blockcount >= 1);
1986 ASSERT(imap.br_startoff == offset_fsb);
1987 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
1988 if (lastoffset > endoff)
1989 lastoffset = endoff;
1990 if (imap.br_startblock == HOLESTARTBLOCK)
1992 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1993 if (imap.br_state == XFS_EXT_UNWRITTEN)
1996 XFS_BUF_UNWRITE(bp);
1998 XFS_BUF_SET_ADDR(bp, xfs_fsb_to_db(ip, imap.br_startblock));
2000 error = xfs_buf_iowait(bp);
2002 xfs_buf_ioerror_alert(bp,
2003 "xfs_zero_remaining_bytes(read)");
2007 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
2008 0, lastoffset - offset + 1);
2013 error = xfs_buf_iowait(bp);
2015 xfs_buf_ioerror_alert(bp,
2016 "xfs_zero_remaining_bytes(write)");
2025 * xfs_free_file_space()
2026 * This routine frees disk space for the given file.
2028 * This routine is only called by xfs_change_file_space
2029 * for an UNRESVSP type call.
2037 xfs_free_file_space(
2045 xfs_fileoff_t endoffset_fsb;
2047 xfs_fsblock_t firstfsb;
2048 xfs_bmap_free_t free_list;
2049 xfs_bmbt_irec_t imap;
2057 xfs_fileoff_t startoffset_fsb;
2059 int need_iolock = 1;
2063 trace_xfs_free_file_space(ip);
2065 error = xfs_qm_dqattach(ip, 0);
2070 if (len <= 0) /* if nothing being freed */
2072 rt = XFS_IS_REALTIME_INODE(ip);
2073 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
2074 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
2076 if (attr_flags & XFS_ATTR_NOLOCK)
2079 xfs_ilock(ip, XFS_IOLOCK_EXCL);
2080 /* wait for the completion of any pending DIOs */
2081 inode_dio_wait(VFS_I(ip));
2084 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
2085 ioffset = offset & ~(rounding - 1);
2087 if (VN_CACHED(VFS_I(ip)) != 0) {
2088 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
2090 goto out_unlock_iolock;
2094 * Need to zero the stuff we're not freeing, on disk.
2095 * If it's a realtime file & can't use unwritten extents then we
2096 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2097 * will take care of it for us.
2099 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
2101 error = xfs_bmapi_read(ip, startoffset_fsb, 1,
2104 goto out_unlock_iolock;
2105 ASSERT(nimap == 0 || nimap == 1);
2106 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2109 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2110 block = imap.br_startblock;
2111 mod = do_div(block, mp->m_sb.sb_rextsize);
2113 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
2116 error = xfs_bmapi_read(ip, endoffset_fsb - 1, 1,
2119 goto out_unlock_iolock;
2120 ASSERT(nimap == 0 || nimap == 1);
2121 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
2122 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
2124 if (mod && (mod != mp->m_sb.sb_rextsize))
2125 endoffset_fsb -= mod;
2128 if ((done = (endoffset_fsb <= startoffset_fsb)))
2130 * One contiguous piece to clear
2132 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
2135 * Some full blocks, possibly two pieces to clear
2137 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
2138 error = xfs_zero_remaining_bytes(ip, offset,
2139 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
2141 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
2142 error = xfs_zero_remaining_bytes(ip,
2143 XFS_FSB_TO_B(mp, endoffset_fsb),
2148 * free file space until done or until there is an error
2150 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
2151 while (!error && !done) {
2154 * allocate and setup the transaction. Allow this
2155 * transaction to dip into the reserve blocks to ensure
2156 * the freeing of the space succeeds at ENOSPC.
2158 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
2159 tp->t_flags |= XFS_TRANS_RESERVE;
2160 error = xfs_trans_reserve(tp,
2162 XFS_WRITE_LOG_RES(mp),
2164 XFS_TRANS_PERM_LOG_RES,
2165 XFS_WRITE_LOG_COUNT);
2168 * check for running out of space
2172 * Free the transaction structure.
2174 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
2175 xfs_trans_cancel(tp, 0);
2178 xfs_ilock(ip, XFS_ILOCK_EXCL);
2179 error = xfs_trans_reserve_quota(tp, mp,
2180 ip->i_udquot, ip->i_gdquot,
2181 resblks, 0, XFS_QMOPT_RES_REGBLKS);
2185 xfs_trans_ijoin(tp, ip, 0);
2188 * issue the bunmapi() call to free the blocks
2190 xfs_bmap_init(&free_list, &firstfsb);
2191 error = xfs_bunmapi(tp, ip, startoffset_fsb,
2192 endoffset_fsb - startoffset_fsb,
2193 0, 2, &firstfsb, &free_list, &done);
2199 * complete the transaction
2201 error = xfs_bmap_finish(&tp, &free_list, &committed);
2206 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2207 xfs_iunlock(ip, XFS_ILOCK_EXCL);
2212 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
2216 xfs_bmap_cancel(&free_list);
2218 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
2219 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
2225 * xfs_change_file_space()
2226 * This routine allocates or frees disk space for the given file.
2227 * The user specified parameters are checked for alignment and size
2236 xfs_change_file_space(
2237 struct dentry *dentry,
2243 xfs_inode_t *ip = XFS_I(dentry->d_inode);
2244 xfs_mount_t *mp = ip->i_mount;
2249 xfs_off_t startoffset;
2255 if (!S_ISREG(ip->i_d.di_mode))
2256 return XFS_ERROR(EINVAL);
2258 switch (bf->l_whence) {
2259 case 0: /*SEEK_SET*/
2261 case 1: /*SEEK_CUR*/
2262 bf->l_start += offset;
2264 case 2: /*SEEK_END*/
2265 bf->l_start += ip->i_size;
2268 return XFS_ERROR(EINVAL);
2271 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
2273 if ( (bf->l_start < 0)
2274 || (bf->l_start > XFS_MAXIOFFSET(mp))
2275 || (bf->l_start + llen < 0)
2276 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
2277 return XFS_ERROR(EINVAL);
2281 startoffset = bf->l_start;
2285 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
2287 * These calls do NOT zero the data space allocated to the file,
2288 * nor do they change the file size.
2290 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
2292 * These calls cause the new file data to be zeroed and the file
2293 * size to be changed.
2295 setprealloc = clrprealloc = 0;
2296 prealloc_type = XFS_BMAPI_PREALLOC;
2299 case XFS_IOC_ZERO_RANGE:
2300 prealloc_type |= XFS_BMAPI_CONVERT;
2301 xfs_tosspages(ip, startoffset, startoffset + bf->l_len, 0);
2303 case XFS_IOC_RESVSP:
2304 case XFS_IOC_RESVSP64:
2305 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
2306 prealloc_type, attr_flags);
2312 case XFS_IOC_UNRESVSP:
2313 case XFS_IOC_UNRESVSP64:
2314 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
2319 case XFS_IOC_ALLOCSP:
2320 case XFS_IOC_ALLOCSP64:
2321 case XFS_IOC_FREESP:
2322 case XFS_IOC_FREESP64:
2323 if (startoffset > fsize) {
2324 error = xfs_alloc_file_space(ip, fsize,
2325 startoffset - fsize, 0, attr_flags);
2330 iattr.ia_valid = ATTR_SIZE;
2331 iattr.ia_size = startoffset;
2333 error = xfs_setattr_size(dentry, &iattr, attr_flags);
2343 return XFS_ERROR(EINVAL);
2347 * update the inode timestamp, mode, and prealloc flag bits
2349 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
2351 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
2354 xfs_trans_cancel(tp, 0);
2358 xfs_ilock(ip, XFS_ILOCK_EXCL);
2359 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2361 if ((attr_flags & XFS_ATTR_DMI) == 0) {
2362 ip->i_d.di_mode &= ~S_ISUID;
2365 * Note that we don't have to worry about mandatory
2366 * file locking being disabled here because we only
2367 * clear the S_ISGID bit if the Group execute bit is
2368 * on, but if it was on then mandatory locking wouldn't
2369 * have been enabled.
2371 if (ip->i_d.di_mode & S_IXGRP)
2372 ip->i_d.di_mode &= ~S_ISGID;
2374 xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2377 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
2378 else if (clrprealloc)
2379 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
2381 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
2382 if (attr_flags & XFS_ATTR_SYNC)
2383 xfs_trans_set_sync(tp);
2384 return xfs_trans_commit(tp, 0);