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_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_da_btree.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_inode_item.h"
40 #include "xfs_itable.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_alloc.h"
47 #include "xfs_error.h"
48 #include "xfs_quota.h"
49 #include "xfs_utils.h"
50 #include "xfs_rtalloc.h"
51 #include "xfs_refcache.h"
52 #include "xfs_trans_space.h"
53 #include "xfs_log_priv.h"
54 #include "xfs_filestream.h"
62 bhv_vnode_t *vp = BHV_TO_VNODE(bdp);
63 xfs_inode_t *ip = XFS_BHVTOI(bdp);
65 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
66 return XFS_ERROR(EIO);
69 * If it's a directory with any blocks, read-ahead block 0
70 * as we're almost certain to have the next operation be a read there.
72 if (VN_ISDIR(vp) && ip->i_d.di_nextents > 0) {
73 mode = xfs_ilock_map_shared(ip);
74 if (ip->i_d.di_nextents > 0)
75 (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
76 xfs_iunlock(ip, mode);
95 vp = BHV_TO_VNODE(bdp);
96 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
101 if (XFS_FORCED_SHUTDOWN(mp))
102 return XFS_ERROR(EIO);
104 if (!(flags & ATTR_LAZY))
105 xfs_ilock(ip, XFS_ILOCK_SHARED);
107 vap->va_size = XFS_ISIZE(ip);
108 if (vap->va_mask == XFS_AT_SIZE)
112 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
113 vap->va_nodeid = ip->i_ino;
115 vap->va_nodeid += mp->m_inoadd;
117 vap->va_nlink = ip->i_d.di_nlink;
120 * Quick exit for non-stat callers
123 ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
124 XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
128 * Copy from in-core inode.
130 vap->va_mode = ip->i_d.di_mode;
131 vap->va_uid = ip->i_d.di_uid;
132 vap->va_gid = ip->i_d.di_gid;
133 vap->va_projid = ip->i_d.di_projid;
136 * Check vnode type block/char vs. everything else.
138 switch (ip->i_d.di_mode & S_IFMT) {
141 vap->va_rdev = ip->i_df.if_u2.if_rdev;
142 vap->va_blocksize = BLKDEV_IOSIZE;
147 if (!(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
148 vap->va_blocksize = xfs_preferred_iosize(mp);
152 * If the file blocks are being allocated from a
153 * realtime partition, then return the inode's
154 * realtime extent size or the realtime volume's
158 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
163 vn_atime_to_timespec(vp, &vap->va_atime);
164 vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
165 vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
166 vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
167 vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
170 * Exit for stat callers. See if any of the rest of the fields
171 * to be filled in are needed.
174 (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
175 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
179 * Convert di_flags to xflags.
181 vap->va_xflags = xfs_ip2xflags(ip);
184 * Exit for inode revalidate. See if any of the rest of
185 * the fields to be filled in are needed.
188 (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
189 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
192 vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
194 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
195 ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
199 (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
200 ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
201 ip->i_d.di_anextents;
203 vap->va_anextents = 0;
204 vap->va_gen = ip->i_d.di_gen;
207 if (!(flags & ATTR_LAZY))
208 xfs_iunlock(ip, XFS_ILOCK_SHARED);
234 xfs_prid_t projid=0, iprojid=0;
235 int mandlock_before, mandlock_after;
236 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
240 vp = BHV_TO_VNODE(bdp);
241 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
243 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
244 return XFS_ERROR(EROFS);
247 * Cannot set certain attributes.
250 if (mask & XFS_AT_NOSET) {
251 return XFS_ERROR(EINVAL);
254 ip = XFS_BHVTOI(bdp);
257 if (XFS_FORCED_SHUTDOWN(mp))
258 return XFS_ERROR(EIO);
261 * Timestamps do not need to be logged and hence do not
262 * need to be done within a transaction.
264 if (mask & XFS_AT_UPDTIMES) {
265 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
266 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
267 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
268 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
269 xfs_ichgtime(ip, timeflags);
273 olddquot1 = olddquot2 = NULL;
277 * If disk quotas is on, we make sure that the dquots do exist on disk,
278 * before we start any other transactions. Trying to do this later
279 * is messy. We don't care to take a readlock to look at the ids
280 * in inode here, because we can't hold it across the trans_reserve.
281 * If the IDs do change before we take the ilock, we're covered
282 * because the i_*dquot fields will get updated anyway.
284 if (XFS_IS_QUOTA_ON(mp) &&
285 (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
288 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
290 qflags |= XFS_QMOPT_UQUOTA;
292 uid = ip->i_d.di_uid;
294 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
296 qflags |= XFS_QMOPT_GQUOTA;
298 gid = ip->i_d.di_gid;
300 if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
301 projid = vap->va_projid;
302 qflags |= XFS_QMOPT_PQUOTA;
304 projid = ip->i_d.di_projid;
307 * We take a reference when we initialize udqp and gdqp,
308 * so it is important that we never blindly double trip on
309 * the same variable. See xfs_create() for an example.
311 ASSERT(udqp == NULL);
312 ASSERT(gdqp == NULL);
313 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
320 * For the other attributes, we acquire the inode lock and
321 * first do an error checking pass.
324 lock_flags = XFS_ILOCK_EXCL;
325 if (flags & ATTR_NOLOCK)
327 if (!(mask & XFS_AT_SIZE)) {
328 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
329 (mp->m_flags & XFS_MOUNT_WSYNC)) {
330 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
332 if ((code = xfs_trans_reserve(tp, 0,
333 XFS_ICHANGE_LOG_RES(mp), 0,
340 if (DM_EVENT_ENABLED (vp->v_vfsp, ip, DM_EVENT_TRUNCATE) &&
341 !(flags & ATTR_DMI)) {
342 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
343 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
344 vap->va_size, 0, dmflags, NULL);
351 lock_flags |= XFS_IOLOCK_EXCL;
354 xfs_ilock(ip, lock_flags);
356 /* boolean: are we the file owner? */
357 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
360 * Change various properties of a file.
361 * Only the owner or users with CAP_FOWNER
362 * capability may do these things.
365 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
366 XFS_AT_GID|XFS_AT_PROJID)) {
368 * CAP_FOWNER overrides the following restrictions:
370 * The user ID of the calling process must be equal
371 * to the file owner ID, except in cases where the
372 * CAP_FSETID capability is applicable.
374 if (!file_owner && !capable(CAP_FOWNER)) {
375 code = XFS_ERROR(EPERM);
380 * CAP_FSETID overrides the following restrictions:
382 * The effective user ID of the calling process shall match
383 * the file owner when setting the set-user-ID and
384 * set-group-ID bits on that file.
386 * The effective group ID or one of the supplementary group
387 * IDs of the calling process shall match the group owner of
388 * the file when setting the set-group-ID bit on that file
390 if (mask & XFS_AT_MODE) {
393 if ((vap->va_mode & S_ISUID) && !file_owner)
395 if ((vap->va_mode & S_ISGID) &&
396 !in_group_p((gid_t)ip->i_d.di_gid))
399 /* Linux allows this, Irix doesn't. */
400 if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
403 if (m && !capable(CAP_FSETID))
409 * Change file ownership. Must be the owner or privileged.
410 * If the system was configured with the "restricted_chown"
411 * option, the owner is not permitted to give away the file,
412 * and can change the group id only to a group of which he
413 * or she is a member.
415 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
417 * These IDs could have changed since we last looked at them.
418 * But, we're assured that if the ownership did change
419 * while we didn't have the inode locked, inode's dquot(s)
420 * would have changed also.
422 iuid = ip->i_d.di_uid;
423 iprojid = ip->i_d.di_projid;
424 igid = ip->i_d.di_gid;
425 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
426 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
427 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
431 * CAP_CHOWN overrides the following restrictions:
433 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
434 * shall override the restriction that a process cannot
435 * change the user ID of a file it owns and the restriction
436 * that the group ID supplied to the chown() function
437 * shall be equal to either the group ID or one of the
438 * supplementary group IDs of the calling process.
440 if (restricted_chown &&
441 (iuid != uid || (igid != gid &&
442 !in_group_p((gid_t)gid))) &&
443 !capable(CAP_CHOWN)) {
444 code = XFS_ERROR(EPERM);
448 * Do a quota reservation only if uid/projid/gid is actually
451 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
452 (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
453 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
455 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
456 capable(CAP_FOWNER) ?
457 XFS_QMOPT_FORCE_RES : 0);
458 if (code) /* out of quota */
464 * Truncate file. Must have write permission and not be a directory.
466 if (mask & XFS_AT_SIZE) {
467 /* Short circuit the truncate case for zero length files */
468 if ((vap->va_size == 0) &&
469 (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) {
470 xfs_iunlock(ip, XFS_ILOCK_EXCL);
471 lock_flags &= ~XFS_ILOCK_EXCL;
472 if (mask & XFS_AT_CTIME)
473 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
479 code = XFS_ERROR(EISDIR);
481 } else if (!VN_ISREG(vp)) {
482 code = XFS_ERROR(EINVAL);
486 * Make sure that the dquots are attached to the inode.
488 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
493 * Change file access or modified times.
495 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
497 if ((flags & ATTR_UTIME) &&
498 !capable(CAP_FOWNER)) {
499 code = XFS_ERROR(EPERM);
506 * Change extent size or realtime flag.
508 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
510 * Can't change extent size if any extents are allocated.
512 if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
513 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
515 code = XFS_ERROR(EINVAL); /* EFBIG? */
520 * Can't change realtime flag if any extents are allocated.
522 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
523 (mask & XFS_AT_XFLAGS) &&
524 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) !=
525 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
526 code = XFS_ERROR(EINVAL); /* EFBIG? */
530 * Extent size must be a multiple of the appropriate block
531 * size, if set at all.
533 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
536 if ((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
537 ((mask & XFS_AT_XFLAGS) &&
538 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
539 size = mp->m_sb.sb_rextsize <<
540 mp->m_sb.sb_blocklog;
542 size = mp->m_sb.sb_blocksize;
544 if (vap->va_extsize % size) {
545 code = XFS_ERROR(EINVAL);
550 * If realtime flag is set then must have realtime data.
552 if ((mask & XFS_AT_XFLAGS) &&
553 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
554 if ((mp->m_sb.sb_rblocks == 0) ||
555 (mp->m_sb.sb_rextsize == 0) ||
556 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
557 code = XFS_ERROR(EINVAL);
563 * Can't modify an immutable/append-only file unless
564 * we have appropriate permission.
566 if ((mask & XFS_AT_XFLAGS) &&
568 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
570 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
571 !capable(CAP_LINUX_IMMUTABLE)) {
572 code = XFS_ERROR(EPERM);
578 * Now we can make the changes. Before we join the inode
579 * to the transaction, if XFS_AT_SIZE is set then take care of
580 * the part of the truncation that must be done without the
581 * inode lock. This needs to be done before joining the inode
582 * to the transaction, because the inode cannot be unlocked
583 * once it is a part of the transaction.
585 if (mask & XFS_AT_SIZE) {
587 if ((vap->va_size > ip->i_size) &&
588 (flags & ATTR_NOSIZETOK) == 0) {
589 code = xfs_igrow_start(ip, vap->va_size, credp);
591 xfs_iunlock(ip, XFS_ILOCK_EXCL);
592 vn_iowait(vp); /* wait for the completion of any pending DIOs */
594 code = xfs_itruncate_data(ip, vap->va_size);
597 lock_flags &= ~XFS_ILOCK_EXCL;
598 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
601 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
602 if ((code = xfs_trans_reserve(tp, 0,
603 XFS_ITRUNCATE_LOG_RES(mp), 0,
604 XFS_TRANS_PERM_LOG_RES,
605 XFS_ITRUNCATE_LOG_COUNT))) {
606 xfs_trans_cancel(tp, 0);
608 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
611 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
612 xfs_ilock(ip, XFS_ILOCK_EXCL);
616 xfs_trans_ijoin(tp, ip, lock_flags);
617 xfs_trans_ihold(tp, ip);
620 /* determine whether mandatory locking mode changes */
621 mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
624 * Truncate file. Must have write permission and not be a directory.
626 if (mask & XFS_AT_SIZE) {
627 if (vap->va_size > ip->i_size) {
628 xfs_igrow_finish(tp, ip, vap->va_size,
629 !(flags & ATTR_DMI));
630 } else if ((vap->va_size <= ip->i_size) ||
631 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
633 * signal a sync transaction unless
634 * we're truncating an already unlinked
635 * file on a wsync filesystem
637 code = xfs_itruncate_finish(&tp, ip,
638 (xfs_fsize_t)vap->va_size,
640 ((ip->i_d.di_nlink != 0 ||
641 !(mp->m_flags & XFS_MOUNT_WSYNC))
646 * Truncated "down", so we're removing references
647 * to old data here - if we now delay flushing for
648 * a long time, we expose ourselves unduly to the
649 * notorious NULL files problem. So, we mark this
650 * vnode and flush it when the file is closed, and
651 * do not wait the usual (long) time for writeout.
656 * Have to do this even if the file's size doesn't change.
658 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
662 * Change file access modes.
664 if (mask & XFS_AT_MODE) {
665 ip->i_d.di_mode &= S_IFMT;
666 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
668 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
669 timeflags |= XFS_ICHGTIME_CHG;
673 * Change file ownership. Must be the owner or privileged.
674 * If the system was configured with the "restricted_chown"
675 * option, the owner is not permitted to give away the file,
676 * and can change the group id only to a group of which he
677 * or she is a member.
679 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
681 * CAP_FSETID overrides the following restrictions:
683 * The set-user-ID and set-group-ID bits of a file will be
684 * cleared upon successful return from chown()
686 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
687 !capable(CAP_FSETID)) {
688 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
692 * Change the ownerships and register quota modifications
693 * in the transaction.
696 if (XFS_IS_UQUOTA_ON(mp)) {
697 ASSERT(mask & XFS_AT_UID);
699 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
700 &ip->i_udquot, udqp);
702 ip->i_d.di_uid = uid;
705 if (XFS_IS_GQUOTA_ON(mp)) {
706 ASSERT(!XFS_IS_PQUOTA_ON(mp));
707 ASSERT(mask & XFS_AT_GID);
709 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
710 &ip->i_gdquot, gdqp);
712 ip->i_d.di_gid = gid;
714 if (iprojid != projid) {
715 if (XFS_IS_PQUOTA_ON(mp)) {
716 ASSERT(!XFS_IS_GQUOTA_ON(mp));
717 ASSERT(mask & XFS_AT_PROJID);
719 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
720 &ip->i_gdquot, gdqp);
722 ip->i_d.di_projid = projid;
724 * We may have to rev the inode as well as
725 * the superblock version number since projids didn't
726 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
728 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
729 xfs_bump_ino_vers2(tp, ip);
732 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
733 timeflags |= XFS_ICHGTIME_CHG;
738 * Change file access or modified times.
740 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
741 if (mask & XFS_AT_ATIME) {
742 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
743 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
744 ip->i_update_core = 1;
745 timeflags &= ~XFS_ICHGTIME_ACC;
747 if (mask & XFS_AT_MTIME) {
748 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
749 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
750 timeflags &= ~XFS_ICHGTIME_MOD;
751 timeflags |= XFS_ICHGTIME_CHG;
753 if (tp && (flags & ATTR_UTIME))
754 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
758 * Change XFS-added attributes.
760 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
761 if (mask & XFS_AT_EXTSIZE) {
763 * Converting bytes to fs blocks.
765 ip->i_d.di_extsize = vap->va_extsize >>
766 mp->m_sb.sb_blocklog;
768 if (mask & XFS_AT_XFLAGS) {
771 /* can't set PREALLOC this way, just preserve it */
772 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
773 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
774 di_flags |= XFS_DIFLAG_IMMUTABLE;
775 if (vap->va_xflags & XFS_XFLAG_APPEND)
776 di_flags |= XFS_DIFLAG_APPEND;
777 if (vap->va_xflags & XFS_XFLAG_SYNC)
778 di_flags |= XFS_DIFLAG_SYNC;
779 if (vap->va_xflags & XFS_XFLAG_NOATIME)
780 di_flags |= XFS_DIFLAG_NOATIME;
781 if (vap->va_xflags & XFS_XFLAG_NODUMP)
782 di_flags |= XFS_DIFLAG_NODUMP;
783 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
784 di_flags |= XFS_DIFLAG_PROJINHERIT;
785 if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
786 di_flags |= XFS_DIFLAG_NODEFRAG;
787 if (vap->va_xflags & XFS_XFLAG_FILESTREAM)
788 di_flags |= XFS_DIFLAG_FILESTREAM;
789 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
790 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
791 di_flags |= XFS_DIFLAG_RTINHERIT;
792 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
793 di_flags |= XFS_DIFLAG_NOSYMLINKS;
794 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
795 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
796 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
797 if (vap->va_xflags & XFS_XFLAG_REALTIME) {
798 di_flags |= XFS_DIFLAG_REALTIME;
799 ip->i_iocore.io_flags |= XFS_IOCORE_RT;
801 ip->i_iocore.io_flags &= ~XFS_IOCORE_RT;
803 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
804 di_flags |= XFS_DIFLAG_EXTSIZE;
806 ip->i_d.di_flags = di_flags;
808 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
809 timeflags |= XFS_ICHGTIME_CHG;
813 * Change file inode change time only if XFS_AT_CTIME set
814 * AND we have been called by a DMI function.
817 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
818 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
819 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
820 ip->i_update_core = 1;
821 timeflags &= ~XFS_ICHGTIME_CHG;
825 * Send out timestamp changes that need to be set to the
826 * current time. Not done when called by a DMI function.
828 if (timeflags && !(flags & ATTR_DMI))
829 xfs_ichgtime(ip, timeflags);
831 XFS_STATS_INC(xs_ig_attrchg);
834 * If this is a synchronous mount, make sure that the
835 * transaction goes to disk before returning to the user.
836 * This is slightly sub-optimal in that truncates require
837 * two sync transactions instead of one for wsync filesystems.
838 * One for the truncate and one for the timestamps since we
839 * don't want to change the timestamps unless we're sure the
840 * truncate worked. Truncates are less than 1% of the laddis
841 * mix so this probably isn't worth the trouble to optimize.
845 if (mp->m_flags & XFS_MOUNT_WSYNC)
846 xfs_trans_set_sync(tp);
848 code = xfs_trans_commit(tp, commit_flags);
852 * If the (regular) file's mandatory locking mode changed, then
853 * notify the vnode. We do this under the inode lock to prevent
854 * racing calls to vop_vnode_change.
856 mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
857 if (mandlock_before != mandlock_after) {
858 bhv_vop_vnode_change(vp, VCHANGE_FLAGS_ENF_LOCKING,
862 xfs_iunlock(ip, lock_flags);
865 * Release any dquot(s) the inode had kept before chown.
867 XFS_QM_DQRELE(mp, olddquot1);
868 XFS_QM_DQRELE(mp, olddquot2);
869 XFS_QM_DQRELE(mp, udqp);
870 XFS_QM_DQRELE(mp, gdqp);
876 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_ATTRIBUTE) &&
877 !(flags & ATTR_DMI)) {
878 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
879 NULL, DM_RIGHT_NULL, NULL, NULL,
880 0, 0, AT_DELAY_FLAG(flags));
885 commit_flags |= XFS_TRANS_ABORT;
888 XFS_QM_DQRELE(mp, udqp);
889 XFS_QM_DQRELE(mp, gdqp);
891 xfs_trans_cancel(tp, commit_flags);
893 if (lock_flags != 0) {
894 xfs_iunlock(ip, lock_flags);
902 * Null conversion from vnode mode bits to inode mode bits, as in efs.
913 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
914 (inst_t *)__return_address);
916 ip = XFS_BHVTOI(bdp);
917 xfs_ilock(ip, XFS_ILOCK_SHARED);
918 error = xfs_iaccess(ip, mode, credp);
919 xfs_iunlock(ip, XFS_ILOCK_SHARED);
925 * The maximum pathlen is 1024 bytes. Since the minimum file system
926 * blocksize is 512 bytes, we can get a max of 2 extents back from
929 #define SYMLINK_MAPS 2
950 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
956 vp = BHV_TO_VNODE(bdp);
957 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
959 ip = XFS_BHVTOI(bdp);
962 if (XFS_FORCED_SHUTDOWN(mp))
963 return XFS_ERROR(EIO);
965 xfs_ilock(ip, XFS_ILOCK_SHARED);
967 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
969 offset = uiop->uio_offset;
970 count = uiop->uio_resid;
973 error = XFS_ERROR(EINVAL);
982 * See if the symlink is stored inline.
984 pathlen = (int)ip->i_d.di_size;
986 if (ip->i_df.if_flags & XFS_IFINLINE) {
987 error = xfs_uio_read(ip->i_df.if_u1.if_data, pathlen, uiop);
991 * Symlink not inline. Call bmap to get it in.
993 nmaps = SYMLINK_MAPS;
995 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen),
996 0, NULL, 0, mval, &nmaps, NULL, NULL);
1002 for (n = 0; n < nmaps; n++) {
1003 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1004 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1005 bp = xfs_buf_read(mp->m_ddev_targp, d,
1006 BTOBB(byte_cnt), 0);
1007 error = XFS_BUF_GETERROR(bp);
1009 xfs_ioerror_alert("xfs_readlink",
1010 ip->i_mount, bp, XFS_BUF_ADDR(bp));
1014 if (pathlen < byte_cnt)
1016 pathlen -= byte_cnt;
1018 error = xfs_uio_read(XFS_BUF_PTR(bp), byte_cnt, uiop);
1025 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1033 * This is called to sync the inode and its data out to disk.
1034 * We need to hold the I/O lock while flushing the data, and
1035 * the inode lock while flushing the inode. The inode lock CANNOT
1036 * be held while flushing the data, so acquire after we're done
1050 int log_flushed = 0, changed = 1;
1052 vn_trace_entry(BHV_TO_VNODE(bdp),
1053 __FUNCTION__, (inst_t *)__return_address);
1055 ip = XFS_BHVTOI(bdp);
1057 ASSERT(start >= 0 && stop >= -1);
1059 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1060 return XFS_ERROR(EIO);
1063 * We always need to make sure that the required inode state
1064 * is safe on disk. The vnode might be clean but because
1065 * of committed transactions that haven't hit the disk yet.
1066 * Likewise, there could be unflushed non-transactional
1067 * changes to the inode core that have to go to disk.
1069 * The following code depends on one assumption: that
1070 * any transaction that changes an inode logs the core
1071 * because it has to change some field in the inode core
1072 * (typically nextents or nblocks). That assumption
1073 * implies that any transactions against an inode will
1074 * catch any non-transactional updates. If inode-altering
1075 * transactions exist that violate this assumption, the
1076 * code breaks. Right now, it figures that if the involved
1077 * update_* field is clear and the inode is unpinned, the
1078 * inode is clean. Either it's been flushed or it's been
1079 * committed and the commit has hit the disk unpinning the inode.
1080 * (Note that xfs_inode_item_format() called at commit clears
1081 * the update_* fields.)
1083 xfs_ilock(ip, XFS_ILOCK_SHARED);
1085 /* If we are flushing data then we care about update_size
1086 * being set, otherwise we care about update_core
1088 if ((flag & FSYNC_DATA) ?
1089 (ip->i_update_size == 0) :
1090 (ip->i_update_core == 0)) {
1092 * Timestamps/size haven't changed since last inode
1093 * flush or inode transaction commit. That means
1094 * either nothing got written or a transaction
1095 * committed which caught the updates. If the
1096 * latter happened and the transaction hasn't
1097 * hit the disk yet, the inode will be still
1098 * be pinned. If it is, force the log.
1101 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1103 if (xfs_ipincount(ip)) {
1104 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
1106 ((flag & FSYNC_WAIT)
1107 ? XFS_LOG_SYNC : 0),
1111 * If the inode is not pinned and nothing
1112 * has changed we don't need to flush the
1120 * Kick off a transaction to log the inode
1121 * core to get the updates. Make it
1122 * sync if FSYNC_WAIT is passed in (which
1123 * is done by everybody but specfs). The
1124 * sync transaction will also force the log.
1126 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1127 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1128 if ((error = xfs_trans_reserve(tp, 0,
1129 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1131 xfs_trans_cancel(tp, 0);
1134 xfs_ilock(ip, XFS_ILOCK_EXCL);
1137 * Note - it's possible that we might have pushed
1138 * ourselves out of the way during trans_reserve
1139 * which would flush the inode. But there's no
1140 * guarantee that the inode buffer has actually
1141 * gone out yet (it's delwri). Plus the buffer
1142 * could be pinned anyway if it's part of an
1143 * inode in another recent transaction. So we
1144 * play it safe and fire off the transaction anyway.
1146 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1147 xfs_trans_ihold(tp, ip);
1148 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1149 if (flag & FSYNC_WAIT)
1150 xfs_trans_set_sync(tp);
1151 error = _xfs_trans_commit(tp, 0, &log_flushed);
1153 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1156 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1158 * If the log write didn't issue an ordered tag we need
1159 * to flush the disk cache for the data device now.
1162 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1165 * If this inode is on the RT dev we need to flush that
1168 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
1169 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1176 * This is called by xfs_inactive to free any blocks beyond eof
1177 * when the link count isn't zero and by xfs_dm_punch_hole() when
1178 * punching a hole to EOF.
1188 xfs_fileoff_t end_fsb;
1189 xfs_fileoff_t last_fsb;
1190 xfs_filblks_t map_len;
1192 xfs_bmbt_irec_t imap;
1193 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
1196 * Figure out if there are any blocks beyond the end
1197 * of the file. If not, then there is nothing to do.
1199 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
1200 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1201 map_len = last_fsb - end_fsb;
1206 xfs_ilock(ip, XFS_ILOCK_SHARED);
1207 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, end_fsb, map_len, 0,
1208 NULL, 0, &imap, &nimaps, NULL, NULL);
1209 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1211 if (!error && (nimaps != 0) &&
1212 (imap.br_startblock != HOLESTARTBLOCK ||
1213 ip->i_delayed_blks)) {
1215 * Attach the dquots to the inode up front.
1217 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1221 * There are blocks after the end of file.
1222 * Free them up now by truncating the file to
1225 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1228 * Do the xfs_itruncate_start() call before
1229 * reserving any log space because
1230 * itruncate_start will call into the buffer
1231 * cache and we can't
1232 * do that within a transaction.
1235 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1236 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
1239 xfs_trans_cancel(tp, 0);
1241 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1245 error = xfs_trans_reserve(tp, 0,
1246 XFS_ITRUNCATE_LOG_RES(mp),
1247 0, XFS_TRANS_PERM_LOG_RES,
1248 XFS_ITRUNCATE_LOG_COUNT);
1250 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1251 xfs_trans_cancel(tp, 0);
1252 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1256 xfs_ilock(ip, XFS_ILOCK_EXCL);
1257 xfs_trans_ijoin(tp, ip,
1260 xfs_trans_ihold(tp, ip);
1262 error = xfs_itruncate_finish(&tp, ip,
1267 * If we get an error at this point we
1268 * simply don't bother truncating the file.
1271 xfs_trans_cancel(tp,
1272 (XFS_TRANS_RELEASE_LOG_RES |
1275 error = xfs_trans_commit(tp,
1276 XFS_TRANS_RELEASE_LOG_RES);
1278 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1285 * Free a symlink that has blocks associated with it.
1288 xfs_inactive_symlink_rmt(
1296 xfs_fsblock_t first_block;
1297 xfs_bmap_free_t free_list;
1300 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1308 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1310 * We're freeing a symlink that has some
1311 * blocks allocated to it. Free the
1312 * blocks here. We know that we've got
1313 * either 1 or 2 extents and that we can
1314 * free them all in one bunmapi call.
1316 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1317 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1318 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1319 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1320 xfs_trans_cancel(tp, 0);
1325 * Lock the inode, fix the size, and join it to the transaction.
1326 * Hold it so in the normal path, we still have it locked for
1327 * the second transaction. In the error paths we need it
1328 * held so the cancel won't rele it, see below.
1330 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1331 size = (int)ip->i_d.di_size;
1332 ip->i_d.di_size = 0;
1333 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1334 xfs_trans_ihold(tp, ip);
1335 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1337 * Find the block(s) so we can inval and unmap them.
1340 XFS_BMAP_INIT(&free_list, &first_block);
1341 nmaps = ARRAY_SIZE(mval);
1342 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1343 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
1347 * Invalidate the block(s).
1349 for (i = 0; i < nmaps; i++) {
1350 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1351 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1352 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1353 xfs_trans_binval(tp, bp);
1356 * Unmap the dead block(s) to the free_list.
1358 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
1359 &first_block, &free_list, NULL, &done)))
1363 * Commit the first transaction. This logs the EFI and the inode.
1365 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
1368 * The transaction must have been committed, since there were
1369 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1370 * The new tp has the extent freeing and EFDs.
1374 * The first xact was committed, so add the inode to the new one.
1375 * Mark it dirty so it will be logged and moved forward in the log as
1376 * part of every commit.
1378 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1379 xfs_trans_ihold(tp, ip);
1380 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1382 * Get a new, empty transaction to return to our caller.
1384 ntp = xfs_trans_dup(tp);
1386 * Commit the transaction containing extent freeing and EFDs.
1387 * If we get an error on the commit here or on the reserve below,
1388 * we need to unlock the inode since the new transaction doesn't
1389 * have the inode attached.
1391 error = xfs_trans_commit(tp, 0);
1394 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1398 * Remove the memory for extent descriptions (just bookkeeping).
1400 if (ip->i_df.if_bytes)
1401 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1402 ASSERT(ip->i_df.if_bytes == 0);
1404 * Put an itruncate log reservation in the new transaction
1407 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1408 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1409 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1413 * Return with the inode locked but not joined to the transaction.
1419 xfs_bmap_cancel(&free_list);
1422 * Have to come here with the inode locked and either
1423 * (held and in the transaction) or (not in the transaction).
1424 * If the inode isn't held then cancel would iput it, but
1425 * that's wrong since this is inactive and the vnode ref
1426 * count is 0 already.
1427 * Cancel won't do anything to the inode if held, but it still
1428 * needs to be locked until the cancel is done, if it was
1429 * joined to the transaction.
1431 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1432 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1439 xfs_inactive_symlink_local(
1445 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1447 * We're freeing a symlink which fit into
1448 * the inode. Just free the memory used
1449 * to hold the old symlink.
1451 error = xfs_trans_reserve(*tpp, 0,
1452 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1453 0, XFS_TRANS_PERM_LOG_RES,
1454 XFS_ITRUNCATE_LOG_COUNT);
1457 xfs_trans_cancel(*tpp, 0);
1461 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1464 * Zero length symlinks _can_ exist.
1466 if (ip->i_df.if_bytes > 0) {
1467 xfs_idata_realloc(ip,
1468 -(ip->i_df.if_bytes),
1470 ASSERT(ip->i_df.if_bytes == 0);
1484 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1487 ASSERT(ip->i_d.di_forkoff != 0);
1488 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1489 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1491 error = xfs_attr_inactive(ip);
1494 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1495 return error; /* goto out */
1498 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1499 error = xfs_trans_reserve(tp, 0,
1500 XFS_IFREE_LOG_RES(mp),
1501 0, XFS_TRANS_PERM_LOG_RES,
1502 XFS_INACTIVE_LOG_COUNT);
1504 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1505 xfs_trans_cancel(tp, 0);
1507 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1511 xfs_ilock(ip, XFS_ILOCK_EXCL);
1512 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1513 xfs_trans_ihold(tp, ip);
1514 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1516 ASSERT(ip->i_d.di_anextents == 0);
1531 vp = BHV_TO_VNODE(bdp);
1532 ip = XFS_BHVTOI(bdp);
1535 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
1538 /* If this is a read-only mount, don't do this (would generate I/O) */
1539 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1542 if (!XFS_FORCED_SHUTDOWN(mp)) {
1544 * If we are using filestreams, and we have an unlinked
1545 * file that we are processing the last close on, then nothing
1546 * will be able to reopen and write to this file. Purge this
1547 * inode from the filestreams cache so that it doesn't delay
1548 * teardown of the inode.
1550 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1551 xfs_filestream_deassociate(ip);
1554 * If we previously truncated this file and removed old data
1555 * in the process, we want to initiate "early" writeout on
1556 * the last close. This is an attempt to combat the notorious
1557 * NULL files problem which is particularly noticable from a
1558 * truncate down, buffered (re-)write (delalloc), followed by
1559 * a crash. What we are effectively doing here is
1560 * significantly reducing the time window where we'd otherwise
1561 * be exposed to that problem.
1563 if (VUNTRUNCATE(vp) && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
1564 bhv_vop_flush_pages(vp, 0, -1, XFS_B_ASYNC, FI_NONE);
1567 #ifdef HAVE_REFCACHE
1568 /* If we are in the NFS reference cache then don't do this now */
1573 if (ip->i_d.di_nlink != 0) {
1574 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1575 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1576 ip->i_delayed_blks > 0)) &&
1577 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1578 (!(ip->i_d.di_flags &
1579 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1580 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1583 /* Update linux inode block count after free above */
1584 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1585 ip->i_d.di_nblocks + ip->i_delayed_blks);
1595 * This is called when the vnode reference count for the vnode
1596 * goes to zero. If the file has been unlinked, then it must
1597 * now be truncated. Also, we clear all of the read-ahead state
1598 * kept for the inode here since the file is now closed.
1607 xfs_bmap_free_t free_list;
1608 xfs_fsblock_t first_block;
1615 vp = BHV_TO_VNODE(bdp);
1616 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1618 ip = XFS_BHVTOI(bdp);
1621 * If the inode is already free, then there can be nothing
1624 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1625 ASSERT(ip->i_df.if_real_bytes == 0);
1626 ASSERT(ip->i_df.if_broot_bytes == 0);
1627 return VN_INACTIVE_CACHE;
1631 * Only do a truncate if it's a regular file with
1632 * some actual space in it. It's OK to look at the
1633 * inode's fields without the lock because we're the
1634 * only one with a reference to the inode.
1636 truncate = ((ip->i_d.di_nlink == 0) &&
1637 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1638 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1639 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1643 if (ip->i_d.di_nlink == 0 &&
1644 DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_DESTROY)) {
1645 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1650 /* If this is a read-only mount, don't do this (would generate I/O) */
1651 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1654 if (ip->i_d.di_nlink != 0) {
1655 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1656 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1657 ip->i_delayed_blks > 0)) &&
1658 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1659 (!(ip->i_d.di_flags &
1660 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1661 (ip->i_delayed_blks != 0)))) {
1662 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1664 return VN_INACTIVE_CACHE;
1665 /* Update linux inode block count after free above */
1666 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1667 ip->i_d.di_nblocks + ip->i_delayed_blks);
1672 ASSERT(ip->i_d.di_nlink == 0);
1674 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1675 return VN_INACTIVE_CACHE;
1677 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1680 * Do the xfs_itruncate_start() call before
1681 * reserving any log space because itruncate_start
1682 * will call into the buffer cache and we can't
1683 * do that within a transaction.
1685 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1687 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1689 xfs_trans_cancel(tp, 0);
1690 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1691 return VN_INACTIVE_CACHE;
1694 error = xfs_trans_reserve(tp, 0,
1695 XFS_ITRUNCATE_LOG_RES(mp),
1696 0, XFS_TRANS_PERM_LOG_RES,
1697 XFS_ITRUNCATE_LOG_COUNT);
1699 /* Don't call itruncate_cleanup */
1700 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1701 xfs_trans_cancel(tp, 0);
1702 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1703 return VN_INACTIVE_CACHE;
1706 xfs_ilock(ip, XFS_ILOCK_EXCL);
1707 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1708 xfs_trans_ihold(tp, ip);
1711 * normally, we have to run xfs_itruncate_finish sync.
1712 * But if filesystem is wsync and we're in the inactive
1713 * path, then we know that nlink == 0, and that the
1714 * xaction that made nlink == 0 is permanently committed
1715 * since xfs_remove runs as a synchronous transaction.
1717 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1718 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1721 xfs_trans_cancel(tp,
1722 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1723 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1724 return VN_INACTIVE_CACHE;
1726 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1729 * If we get an error while cleaning up a
1730 * symlink we bail out.
1732 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1733 xfs_inactive_symlink_rmt(ip, &tp) :
1734 xfs_inactive_symlink_local(ip, &tp);
1738 return VN_INACTIVE_CACHE;
1741 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1742 xfs_trans_ihold(tp, ip);
1744 error = xfs_trans_reserve(tp, 0,
1745 XFS_IFREE_LOG_RES(mp),
1746 0, XFS_TRANS_PERM_LOG_RES,
1747 XFS_INACTIVE_LOG_COUNT);
1749 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1750 xfs_trans_cancel(tp, 0);
1751 return VN_INACTIVE_CACHE;
1754 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1755 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1756 xfs_trans_ihold(tp, ip);
1760 * If there are attributes associated with the file
1761 * then blow them away now. The code calls a routine
1762 * that recursively deconstructs the attribute fork.
1763 * We need to just commit the current transaction
1764 * because we can't use it for xfs_attr_inactive().
1766 if (ip->i_d.di_anextents > 0) {
1767 error = xfs_inactive_attrs(ip, &tp);
1769 * If we got an error, the transaction is already
1770 * cancelled, and the inode is unlocked. Just get out.
1773 return VN_INACTIVE_CACHE;
1774 } else if (ip->i_afp) {
1775 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1781 XFS_BMAP_INIT(&free_list, &first_block);
1782 error = xfs_ifree(tp, ip, &free_list);
1785 * If we fail to free the inode, shut down. The cancel
1786 * might do that, we need to make sure. Otherwise the
1787 * inode might be lost for a long time or forever.
1789 if (!XFS_FORCED_SHUTDOWN(mp)) {
1791 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1792 error, mp->m_fsname);
1793 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1795 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1798 * Credit the quota account(s). The inode is gone.
1800 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1803 * Just ignore errors at this point. There is
1804 * nothing we can do except to try to keep going.
1806 (void) xfs_bmap_finish(&tp, &free_list, &committed);
1807 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1810 * Release the dquots held by inode, if any.
1812 XFS_QM_DQDETACH(mp, ip);
1814 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1817 return VN_INACTIVE_CACHE;
1826 bhv_desc_t *dir_bdp,
1827 bhv_vname_t *dentry,
1833 xfs_inode_t *dp, *ip;
1837 bhv_vnode_t *dir_vp;
1839 dir_vp = BHV_TO_VNODE(dir_bdp);
1840 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1842 dp = XFS_BHVTOI(dir_bdp);
1844 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1845 return XFS_ERROR(EIO);
1847 lock_mode = xfs_ilock_map_shared(dp);
1848 error = xfs_dir_lookup_int(dir_bdp, lock_mode, dentry, &e_inum, &ip);
1850 *vpp = XFS_ITOV(ip);
1853 xfs_iunlock_map_shared(dp, lock_mode);
1859 * xfs_create (create a new file).
1863 bhv_desc_t *dir_bdp,
1864 bhv_vname_t *dentry,
1869 char *name = VNAME(dentry);
1870 bhv_vnode_t *dir_vp;
1871 xfs_inode_t *dp, *ip;
1872 bhv_vnode_t *vp = NULL;
1877 xfs_bmap_free_t free_list;
1878 xfs_fsblock_t first_block;
1879 boolean_t dp_joined_to_trans;
1880 int dm_event_sent = 0;
1884 struct xfs_dquot *udqp, *gdqp;
1890 dir_vp = BHV_TO_VNODE(dir_bdp);
1891 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1893 dp = XFS_BHVTOI(dir_bdp);
1896 dm_di_mode = vap->va_mode;
1897 namelen = VNAMELEN(dentry);
1899 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
1900 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1901 dir_vp, DM_RIGHT_NULL, NULL,
1902 DM_RIGHT_NULL, name, NULL,
1910 if (XFS_FORCED_SHUTDOWN(mp))
1911 return XFS_ERROR(EIO);
1913 /* Return through std_return after this point. */
1916 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1917 prid = dp->i_d.di_projid;
1918 else if (vap->va_mask & XFS_AT_PROJID)
1919 prid = (xfs_prid_t)vap->va_projid;
1921 prid = (xfs_prid_t)dfltprid;
1924 * Make sure that we have allocated dquot(s) on disk.
1926 error = XFS_QM_DQVOPALLOC(mp, dp,
1927 current_fsuid(credp), current_fsgid(credp), prid,
1928 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1933 dp_joined_to_trans = B_FALSE;
1935 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1936 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1937 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1939 * Initially assume that the file does not exist and
1940 * reserve the resources for that case. If that is not
1941 * the case we'll drop the one we have and get a more
1942 * appropriate transaction later.
1944 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1945 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1946 if (error == ENOSPC) {
1948 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1949 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1957 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1959 XFS_BMAP_INIT(&free_list, &first_block);
1964 * Reserve disk quota and the inode.
1966 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1970 if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
1972 rdev = (vap->va_mask & XFS_AT_RDEV) ? vap->va_rdev : 0;
1973 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 1,
1974 rdev, credp, prid, resblks > 0,
1977 if (error == ENOSPC)
1984 * At this point, we've gotten a newly allocated inode.
1985 * It is locked (and joined to the transaction).
1988 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1991 * Now we join the directory inode to the transaction.
1992 * We do not do it earlier because xfs_dir_ialloc
1993 * might commit the previous transaction (and release
1998 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1999 dp_joined_to_trans = B_TRUE;
2001 error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
2002 &first_block, &free_list, resblks ?
2003 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2005 ASSERT(error != ENOSPC);
2008 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2009 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2012 * If this is a synchronous mount, make sure that the
2013 * create transaction goes to disk before returning to
2016 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2017 xfs_trans_set_sync(tp);
2023 * Attach the dquot(s) to the inodes and modify them incore.
2024 * These ids of the inode couldn't have changed since the new
2025 * inode has been locked ever since it was created.
2027 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2030 * xfs_trans_commit normally decrements the vnode ref count
2031 * when it unlocks the inode. Since we want to return the
2032 * vnode to the caller, we bump the vnode ref count now.
2037 error = xfs_bmap_finish(&tp, &free_list, &committed);
2039 xfs_bmap_cancel(&free_list);
2043 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2050 XFS_QM_DQRELE(mp, udqp);
2051 XFS_QM_DQRELE(mp, gdqp);
2054 * Propagate the fact that the vnode changed after the
2055 * xfs_inode locks have been released.
2057 bhv_vop_vnode_change(vp, VCHANGE_FLAGS_TRUNCATED, 3);
2061 /* Fallthrough to std_return with error = 0 */
2064 if ( (*vpp || (error != 0 && dm_event_sent != 0)) &&
2065 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2066 DM_EVENT_POSTCREATE)) {
2067 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2068 dir_vp, DM_RIGHT_NULL,
2070 DM_RIGHT_NULL, name, NULL,
2071 dm_di_mode, error, 0);
2076 cancel_flags |= XFS_TRANS_ABORT;
2081 xfs_trans_cancel(tp, cancel_flags);
2083 if (!dp_joined_to_trans && (dp != NULL))
2084 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2085 XFS_QM_DQRELE(mp, udqp);
2086 XFS_QM_DQRELE(mp, gdqp);
2092 * Wait until after the current transaction is aborted to
2093 * release the inode. This prevents recursive transactions
2094 * and deadlocks from xfs_inactive.
2096 cancel_flags |= XFS_TRANS_ABORT;
2097 xfs_trans_cancel(tp, cancel_flags);
2100 XFS_QM_DQRELE(mp, udqp);
2101 XFS_QM_DQRELE(mp, gdqp);
2108 * Some counters to see if (and how often) we are hitting some deadlock
2109 * prevention code paths.
2113 int xfs_rm_lock_delays;
2114 int xfs_rm_attempts;
2118 * The following routine will lock the inodes associated with the
2119 * directory and the named entry in the directory. The locks are
2120 * acquired in increasing inode number.
2122 * If the entry is "..", then only the directory is locked. The
2123 * vnode ref count will still include that from the .. entry in
2126 * There is a deadlock we need to worry about. If the locked directory is
2127 * in the AIL, it might be blocking up the log. The next inode we lock
2128 * could be already locked by another thread waiting for log space (e.g
2129 * a permanent log reservation with a long running transaction (see
2130 * xfs_itruncate_finish)). To solve this, we must check if the directory
2131 * is in the ail and use lock_nowait. If we can't lock, we need to
2132 * drop the inode lock on the directory and try again. xfs_iunlock will
2133 * potentially push the tail if we were holding up the log.
2136 xfs_lock_dir_and_entry(
2138 xfs_inode_t *ip) /* inode of entry 'name' */
2142 xfs_inode_t *ips[2];
2151 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2158 * We want to lock in increasing inum. Since we've already
2159 * acquired the lock on the directory, we may need to release
2160 * if if the inum of the entry turns out to be less.
2162 if (e_inum > dp->i_ino) {
2164 * We are already in the right order, so just
2165 * lock on the inode of the entry.
2166 * We need to use nowait if dp is in the AIL.
2169 lp = (xfs_log_item_t *)dp->i_itemp;
2170 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2171 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2178 * Unlock dp and try again.
2179 * xfs_iunlock will try to push the tail
2180 * if the inode is in the AIL.
2183 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2185 if ((attempts % 5) == 0) {
2186 delay(1); /* Don't just spin the CPU */
2188 xfs_rm_lock_delays++;
2194 xfs_ilock(ip, XFS_ILOCK_EXCL);
2196 } else if (e_inum < dp->i_ino) {
2197 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2201 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2203 /* else e_inum == dp->i_ino */
2204 /* This can happen if we're asked to lock /x/..
2205 * the entry is "..", which is also the parent directory.
2213 int xfs_small_retries;
2214 int xfs_middle_retries;
2215 int xfs_lots_retries;
2216 int xfs_lock_delays;
2220 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2224 xfs_lock_inumorder(int lock_mode, int subclass)
2226 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
2227 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
2228 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
2229 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
2235 * The following routine will lock n inodes in exclusive mode.
2236 * We assume the caller calls us with the inodes in i_ino order.
2238 * We need to detect deadlock where an inode that we lock
2239 * is in the AIL and we start waiting for another inode that is locked
2240 * by a thread in a long running transaction (such as truncate). This can
2241 * result in deadlock since the long running trans might need to wait
2242 * for the inode we just locked in order to push the tail and free space
2252 int attempts = 0, i, j, try_lock;
2255 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2266 for (; i < inodes; i++) {
2269 if (i && (ips[i] == ips[i-1])) /* Already locked */
2273 * If try_lock is not set yet, make sure all locked inodes
2274 * are not in the AIL.
2275 * If any are, set try_lock to be used later.
2279 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2280 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2281 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2288 * If any of the previous locks we have locked is in the AIL,
2289 * we must TRY to get the second and subsequent locks. If
2290 * we can't get any, we must release all we have
2295 /* try_lock must be 0 if i is 0. */
2297 * try_lock means we have an inode locked
2298 * that is in the AIL.
2301 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
2305 * Unlock all previous guys and try again.
2306 * xfs_iunlock will try to push the tail
2307 * if the inode is in the AIL.
2310 for(j = i - 1; j >= 0; j--) {
2313 * Check to see if we've already
2314 * unlocked this one.
2315 * Not the first one going back,
2316 * and the inode ptr is the same.
2318 if ((j != (i - 1)) && ips[j] ==
2322 xfs_iunlock(ips[j], lock_mode);
2325 if ((attempts % 5) == 0) {
2326 delay(1); /* Don't just spin the CPU */
2336 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
2342 if (attempts < 5) xfs_small_retries++;
2343 else if (attempts < 100) xfs_middle_retries++;
2344 else xfs_lots_retries++;
2352 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2353 int remove_which_error_return = 0;
2355 #define REMOVE_DEBUG_TRACE(x)
2356 #endif /* ! DEBUG */
2365 bhv_desc_t *dir_bdp,
2366 bhv_vname_t *dentry,
2369 bhv_vnode_t *dir_vp;
2370 char *name = VNAME(dentry);
2371 xfs_inode_t *dp, *ip;
2372 xfs_trans_t *tp = NULL;
2375 xfs_bmap_free_t free_list;
2376 xfs_fsblock_t first_block;
2384 dir_vp = BHV_TO_VNODE(dir_bdp);
2385 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2387 dp = XFS_BHVTOI(dir_bdp);
2390 if (XFS_FORCED_SHUTDOWN(mp))
2391 return XFS_ERROR(EIO);
2393 namelen = VNAMELEN(dentry);
2395 if (!xfs_get_dir_entry(dentry, &ip)) {
2396 dm_di_mode = ip->i_d.di_mode;
2400 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
2401 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2402 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2403 name, NULL, dm_di_mode, 0, 0);
2408 /* From this point on, return through std_return */
2412 * We need to get a reference to ip before we get our log
2413 * reservation. The reason for this is that we cannot call
2414 * xfs_iget for an inode for which we do not have a reference
2415 * once we've acquired a log reservation. This is because the
2416 * inode we are trying to get might be in xfs_inactive going
2417 * for a log reservation. Since we'll have to wait for the
2418 * inactive code to complete before returning from xfs_iget,
2419 * we need to make sure that we don't have log space reserved
2420 * when we call xfs_iget. Instead we get an unlocked reference
2421 * to the inode before getting our log reservation.
2423 error = xfs_get_dir_entry(dentry, &ip);
2425 REMOVE_DEBUG_TRACE(__LINE__);
2429 dm_di_mode = ip->i_d.di_mode;
2431 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2435 error = XFS_QM_DQATTACH(mp, dp, 0);
2436 if (!error && dp != ip)
2437 error = XFS_QM_DQATTACH(mp, ip, 0);
2439 REMOVE_DEBUG_TRACE(__LINE__);
2444 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2445 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2447 * We try to get the real space reservation first,
2448 * allowing for directory btree deletion(s) implying
2449 * possible bmap insert(s). If we can't get the space
2450 * reservation then we use 0 instead, and avoid the bmap
2451 * btree insert(s) in the directory code by, if the bmap
2452 * insert tries to happen, instead trimming the LAST
2453 * block from the directory.
2455 resblks = XFS_REMOVE_SPACE_RES(mp);
2456 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2457 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2458 if (error == ENOSPC) {
2460 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2461 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2464 ASSERT(error != ENOSPC);
2465 REMOVE_DEBUG_TRACE(__LINE__);
2466 xfs_trans_cancel(tp, 0);
2471 error = xfs_lock_dir_and_entry(dp, ip);
2473 REMOVE_DEBUG_TRACE(__LINE__);
2474 xfs_trans_cancel(tp, cancel_flags);
2480 * At this point, we've gotten both the directory and the entry
2483 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2486 * Increment vnode ref count only in this case since
2487 * there's an extra vnode reference in the case where
2491 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2495 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2497 XFS_BMAP_INIT(&free_list, &first_block);
2498 error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2499 &first_block, &free_list, 0);
2501 ASSERT(error != ENOENT);
2502 REMOVE_DEBUG_TRACE(__LINE__);
2505 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2508 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2510 error = xfs_droplink(tp, ip);
2512 REMOVE_DEBUG_TRACE(__LINE__);
2516 /* Determine if this is the last link while
2517 * we are in the transaction.
2519 link_zero = (ip)->i_d.di_nlink==0;
2522 * Take an extra ref on the inode so that it doesn't
2523 * go to xfs_inactive() from within the commit.
2528 * If this is a synchronous mount, make sure that the
2529 * remove transaction goes to disk before returning to
2532 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2533 xfs_trans_set_sync(tp);
2536 error = xfs_bmap_finish(&tp, &free_list, &committed);
2538 REMOVE_DEBUG_TRACE(__LINE__);
2542 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2549 * Before we drop our extra reference to the inode, purge it
2550 * from the refcache if it is there. By waiting until afterwards
2551 * to do the IRELE, we ensure that we won't go inactive in the
2552 * xfs_refcache_purge_ip routine (although that would be OK).
2554 xfs_refcache_purge_ip(ip);
2557 * If we are using filestreams, kill the stream association.
2558 * If the file is still open it may get a new one but that
2559 * will get killed on last close in xfs_close() so we don't
2560 * have to worry about that.
2562 if (link_zero && xfs_inode_is_filestream(ip))
2563 xfs_filestream_deassociate(ip);
2565 vn_trace_exit(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2568 * Let interposed file systems know about removed links.
2570 bhv_vop_link_removed(XFS_ITOV(ip), dir_vp, link_zero);
2574 /* Fall through to std_return with error = 0 */
2576 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp,
2577 DM_EVENT_POSTREMOVE)) {
2578 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2579 dir_vp, DM_RIGHT_NULL,
2580 NULL, DM_RIGHT_NULL,
2581 name, NULL, dm_di_mode, error, 0);
2586 xfs_bmap_cancel(&free_list);
2587 cancel_flags |= XFS_TRANS_ABORT;
2588 xfs_trans_cancel(tp, cancel_flags);
2593 * In this case make sure to not release the inode until after
2594 * the current transaction is aborted. Releasing it beforehand
2595 * can cause us to go to xfs_inactive and start a recursive
2596 * transaction which can easily deadlock with the current one.
2598 xfs_bmap_cancel(&free_list);
2599 cancel_flags |= XFS_TRANS_ABORT;
2600 xfs_trans_cancel(tp, cancel_flags);
2603 * Before we drop our extra reference to the inode, purge it
2604 * from the refcache if it is there. By waiting until afterwards
2605 * to do the IRELE, we ensure that we won't go inactive in the
2606 * xfs_refcache_purge_ip routine (although that would be OK).
2608 xfs_refcache_purge_ip(ip);
2622 bhv_desc_t *target_dir_bdp,
2623 bhv_vnode_t *src_vp,
2624 bhv_vname_t *dentry,
2627 xfs_inode_t *tdp, *sip;
2630 xfs_inode_t *ips[2];
2632 xfs_bmap_free_t free_list;
2633 xfs_fsblock_t first_block;
2636 bhv_vnode_t *target_dir_vp;
2638 char *target_name = VNAME(dentry);
2641 target_dir_vp = BHV_TO_VNODE(target_dir_bdp);
2642 vn_trace_entry(target_dir_vp, __FUNCTION__, (inst_t *)__return_address);
2643 vn_trace_entry(src_vp, __FUNCTION__, (inst_t *)__return_address);
2645 target_namelen = VNAMELEN(dentry);
2646 ASSERT(!VN_ISDIR(src_vp));
2648 sip = xfs_vtoi(src_vp);
2649 tdp = XFS_BHVTOI(target_dir_bdp);
2651 if (XFS_FORCED_SHUTDOWN(mp))
2652 return XFS_ERROR(EIO);
2654 if (DM_EVENT_ENABLED(src_vp->v_vfsp, tdp, DM_EVENT_LINK)) {
2655 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2656 target_dir_vp, DM_RIGHT_NULL,
2657 src_vp, DM_RIGHT_NULL,
2658 target_name, NULL, 0, 0, 0);
2663 /* Return through std_return after this point. */
2665 error = XFS_QM_DQATTACH(mp, sip, 0);
2666 if (!error && sip != tdp)
2667 error = XFS_QM_DQATTACH(mp, tdp, 0);
2671 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2672 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2673 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2674 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2675 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2676 if (error == ENOSPC) {
2678 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2679 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2686 if (sip->i_ino < tdp->i_ino) {
2694 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2697 * Increment vnode ref counts since xfs_trans_commit &
2698 * xfs_trans_cancel will both unlock the inodes and
2699 * decrement the associated ref counts.
2702 VN_HOLD(target_dir_vp);
2703 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2704 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2707 * If the source has too many links, we can't make any more to it.
2709 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2710 error = XFS_ERROR(EMLINK);
2715 * If we are using project inheritance, we only allow hard link
2716 * creation in our tree when the project IDs are the same; else
2717 * the tree quota mechanism could be circumvented.
2719 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2720 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2721 error = XFS_ERROR(EXDEV);
2726 (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
2729 XFS_BMAP_INIT(&free_list, &first_block);
2731 error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
2732 sip->i_ino, &first_block, &free_list,
2736 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2738 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2740 error = xfs_bumplink(tp, sip);
2745 * If this is a synchronous mount, make sure that the
2746 * link transaction goes to disk before returning to
2749 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2750 xfs_trans_set_sync(tp);
2753 error = xfs_bmap_finish (&tp, &free_list, &committed);
2755 xfs_bmap_cancel(&free_list);
2759 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2763 /* Fall through to std_return with error = 0. */
2765 if (DM_EVENT_ENABLED(src_vp->v_vfsp, sip,
2766 DM_EVENT_POSTLINK)) {
2767 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2768 target_dir_vp, DM_RIGHT_NULL,
2769 src_vp, DM_RIGHT_NULL,
2770 target_name, NULL, 0, error, 0);
2775 cancel_flags |= XFS_TRANS_ABORT;
2779 xfs_trans_cancel(tp, cancel_flags);
2790 bhv_desc_t *dir_bdp,
2791 bhv_vname_t *dentry,
2796 char *dir_name = VNAME(dentry);
2798 xfs_inode_t *cdp; /* inode of created dir */
2799 bhv_vnode_t *cvp; /* vnode of created dir */
2805 xfs_bmap_free_t free_list;
2806 xfs_fsblock_t first_block;
2807 bhv_vnode_t *dir_vp;
2808 boolean_t dp_joined_to_trans;
2809 boolean_t created = B_FALSE;
2810 int dm_event_sent = 0;
2812 struct xfs_dquot *udqp, *gdqp;
2817 dir_vp = BHV_TO_VNODE(dir_bdp);
2818 dp = XFS_BHVTOI(dir_bdp);
2821 if (XFS_FORCED_SHUTDOWN(mp))
2822 return XFS_ERROR(EIO);
2824 dir_namelen = VNAMELEN(dentry);
2827 dp_joined_to_trans = B_FALSE;
2828 dm_di_mode = vap->va_mode;
2830 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
2831 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2832 dir_vp, DM_RIGHT_NULL, NULL,
2833 DM_RIGHT_NULL, dir_name, NULL,
2840 /* Return through std_return after this point. */
2842 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2846 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2847 prid = dp->i_d.di_projid;
2848 else if (vap->va_mask & XFS_AT_PROJID)
2849 prid = (xfs_prid_t)vap->va_projid;
2851 prid = (xfs_prid_t)dfltprid;
2854 * Make sure that we have allocated dquot(s) on disk.
2856 error = XFS_QM_DQVOPALLOC(mp, dp,
2857 current_fsuid(credp), current_fsgid(credp), prid,
2858 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2862 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2863 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2864 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2865 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2866 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2867 if (error == ENOSPC) {
2869 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2870 XFS_TRANS_PERM_LOG_RES,
2871 XFS_MKDIR_LOG_COUNT);
2879 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2882 * Check for directory link count overflow.
2884 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2885 error = XFS_ERROR(EMLINK);
2890 * Reserve disk quota and the inode.
2892 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2897 (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
2900 * create the directory inode.
2902 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 2,
2903 0, credp, prid, resblks > 0,
2906 if (error == ENOSPC)
2913 * Now we add the directory inode to the transaction.
2914 * We waited until now since xfs_dir_ialloc might start
2915 * a new transaction. Had we joined the transaction
2916 * earlier, the locks might have gotten released.
2919 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2920 dp_joined_to_trans = B_TRUE;
2922 XFS_BMAP_INIT(&free_list, &first_block);
2924 error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2925 &first_block, &free_list, resblks ?
2926 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2928 ASSERT(error != ENOSPC);
2931 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2934 * Bump the in memory version number of the parent directory
2935 * so that other processes accessing it will recognize that
2936 * the directory has changed.
2940 error = xfs_dir_init(tp, cdp, dp);
2945 error = xfs_bumplink(tp, dp);
2949 cvp = XFS_ITOV(cdp);
2957 * Attach the dquots to the new inode and modify the icount incore.
2959 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2962 * If this is a synchronous mount, make sure that the
2963 * mkdir transaction goes to disk before returning to
2966 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2967 xfs_trans_set_sync(tp);
2970 error = xfs_bmap_finish(&tp, &free_list, &committed);
2976 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2977 XFS_QM_DQRELE(mp, udqp);
2978 XFS_QM_DQRELE(mp, gdqp);
2983 /* Fall through to std_return with error = 0 or errno from
2984 * xfs_trans_commit. */
2987 if ( (created || (error != 0 && dm_event_sent != 0)) &&
2988 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2989 DM_EVENT_POSTCREATE)) {
2990 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2991 dir_vp, DM_RIGHT_NULL,
2992 created ? XFS_ITOV(cdp):NULL,
2995 dm_di_mode, error, 0);
3001 xfs_bmap_cancel(&free_list);
3003 cancel_flags |= XFS_TRANS_ABORT;
3005 xfs_trans_cancel(tp, cancel_flags);
3006 XFS_QM_DQRELE(mp, udqp);
3007 XFS_QM_DQRELE(mp, gdqp);
3009 if (!dp_joined_to_trans && (dp != NULL)) {
3010 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3023 bhv_desc_t *dir_bdp,
3024 bhv_vname_t *dentry,
3027 char *name = VNAME(dentry);
3029 xfs_inode_t *cdp; /* child directory */
3033 xfs_bmap_free_t free_list;
3034 xfs_fsblock_t first_block;
3037 bhv_vnode_t *dir_vp;
3038 int dm_di_mode = S_IFDIR;
3043 dir_vp = BHV_TO_VNODE(dir_bdp);
3044 dp = XFS_BHVTOI(dir_bdp);
3047 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3049 if (XFS_FORCED_SHUTDOWN(XFS_BHVTOI(dir_bdp)->i_mount))
3050 return XFS_ERROR(EIO);
3051 namelen = VNAMELEN(dentry);
3053 if (!xfs_get_dir_entry(dentry, &cdp)) {
3054 dm_di_mode = cdp->i_d.di_mode;
3058 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
3059 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
3060 dir_vp, DM_RIGHT_NULL,
3061 NULL, DM_RIGHT_NULL,
3062 name, NULL, dm_di_mode, 0, 0);
3064 return XFS_ERROR(error);
3067 /* Return through std_return after this point. */
3072 * We need to get a reference to cdp before we get our log
3073 * reservation. The reason for this is that we cannot call
3074 * xfs_iget for an inode for which we do not have a reference
3075 * once we've acquired a log reservation. This is because the
3076 * inode we are trying to get might be in xfs_inactive going
3077 * for a log reservation. Since we'll have to wait for the
3078 * inactive code to complete before returning from xfs_iget,
3079 * we need to make sure that we don't have log space reserved
3080 * when we call xfs_iget. Instead we get an unlocked reference