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_trans_space.h"
52 #include "xfs_log_priv.h"
53 #include "xfs_filestream.h"
54 #include "xfs_vnodeops.h"
62 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
63 return XFS_ERROR(EIO);
66 * If it's a directory with any blocks, read-ahead block 0
67 * as we're almost certain to have the next operation be a read there.
69 if (S_ISDIR(ip->i_d.di_mode) && ip->i_d.di_nextents > 0) {
70 mode = xfs_ilock_map_shared(ip);
71 if (ip->i_d.di_nextents > 0)
72 (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
73 xfs_iunlock(ip, mode);
87 bhv_vnode_t *vp = XFS_ITOV(ip);
88 xfs_mount_t *mp = ip->i_mount;
92 if (XFS_FORCED_SHUTDOWN(mp))
93 return XFS_ERROR(EIO);
95 if (!(flags & ATTR_LAZY))
96 xfs_ilock(ip, XFS_ILOCK_SHARED);
98 vap->va_size = XFS_ISIZE(ip);
99 if (vap->va_mask == XFS_AT_SIZE)
103 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
104 vap->va_nodeid = ip->i_ino;
106 vap->va_nodeid += mp->m_inoadd;
108 vap->va_nlink = ip->i_d.di_nlink;
111 * Quick exit for non-stat callers
114 ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
115 XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
119 * Copy from in-core inode.
121 vap->va_mode = ip->i_d.di_mode;
122 vap->va_uid = ip->i_d.di_uid;
123 vap->va_gid = ip->i_d.di_gid;
124 vap->va_projid = ip->i_d.di_projid;
127 * Check vnode type block/char vs. everything else.
129 switch (ip->i_d.di_mode & S_IFMT) {
132 vap->va_rdev = ip->i_df.if_u2.if_rdev;
133 vap->va_blocksize = BLKDEV_IOSIZE;
138 if (!(XFS_IS_REALTIME_INODE(ip))) {
139 vap->va_blocksize = xfs_preferred_iosize(mp);
143 * If the file blocks are being allocated from a
144 * realtime partition, then return the inode's
145 * realtime extent size or the realtime volume's
149 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
154 vn_atime_to_timespec(vp, &vap->va_atime);
155 vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
156 vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
157 vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
158 vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
161 * Exit for stat callers. See if any of the rest of the fields
162 * to be filled in are needed.
165 (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
166 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
170 * Convert di_flags to xflags.
172 vap->va_xflags = xfs_ip2xflags(ip);
175 * Exit for inode revalidate. See if any of the rest of
176 * the fields to be filled in are needed.
179 (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
180 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
183 vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
185 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
186 ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
190 (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
191 ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
192 ip->i_d.di_anextents;
194 vap->va_anextents = 0;
195 vap->va_gen = ip->i_d.di_gen;
198 if (!(flags & ATTR_LAZY))
199 xfs_iunlock(ip, XFS_ILOCK_SHARED);
214 xfs_mount_t *mp = ip->i_mount;
223 xfs_prid_t projid=0, iprojid=0;
224 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
228 xfs_itrace_entry(ip);
230 if (mp->m_flags & XFS_MOUNT_RDONLY)
231 return XFS_ERROR(EROFS);
234 * Cannot set certain attributes.
237 if (mask & XFS_AT_NOSET) {
238 return XFS_ERROR(EINVAL);
241 if (XFS_FORCED_SHUTDOWN(mp))
242 return XFS_ERROR(EIO);
245 * Timestamps do not need to be logged and hence do not
246 * need to be done within a transaction.
248 if (mask & XFS_AT_UPDTIMES) {
249 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
250 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
251 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
252 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
253 xfs_ichgtime(ip, timeflags);
257 olddquot1 = olddquot2 = NULL;
261 * If disk quotas is on, we make sure that the dquots do exist on disk,
262 * before we start any other transactions. Trying to do this later
263 * is messy. We don't care to take a readlock to look at the ids
264 * in inode here, because we can't hold it across the trans_reserve.
265 * If the IDs do change before we take the ilock, we're covered
266 * because the i_*dquot fields will get updated anyway.
268 if (XFS_IS_QUOTA_ON(mp) &&
269 (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
272 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
274 qflags |= XFS_QMOPT_UQUOTA;
276 uid = ip->i_d.di_uid;
278 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
280 qflags |= XFS_QMOPT_GQUOTA;
282 gid = ip->i_d.di_gid;
284 if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
285 projid = vap->va_projid;
286 qflags |= XFS_QMOPT_PQUOTA;
288 projid = ip->i_d.di_projid;
291 * We take a reference when we initialize udqp and gdqp,
292 * so it is important that we never blindly double trip on
293 * the same variable. See xfs_create() for an example.
295 ASSERT(udqp == NULL);
296 ASSERT(gdqp == NULL);
297 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
304 * For the other attributes, we acquire the inode lock and
305 * first do an error checking pass.
308 lock_flags = XFS_ILOCK_EXCL;
309 if (flags & ATTR_NOLOCK)
311 if (!(mask & XFS_AT_SIZE)) {
312 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
313 (mp->m_flags & XFS_MOUNT_WSYNC)) {
314 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
316 if ((code = xfs_trans_reserve(tp, 0,
317 XFS_ICHANGE_LOG_RES(mp), 0,
324 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
325 !(flags & ATTR_DMI)) {
326 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
327 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
328 vap->va_size, 0, dmflags, NULL);
335 lock_flags |= XFS_IOLOCK_EXCL;
338 xfs_ilock(ip, lock_flags);
340 /* boolean: are we the file owner? */
341 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
344 * Change various properties of a file.
345 * Only the owner or users with CAP_FOWNER
346 * capability may do these things.
349 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
350 XFS_AT_GID|XFS_AT_PROJID)) {
352 * CAP_FOWNER overrides the following restrictions:
354 * The user ID of the calling process must be equal
355 * to the file owner ID, except in cases where the
356 * CAP_FSETID capability is applicable.
358 if (!file_owner && !capable(CAP_FOWNER)) {
359 code = XFS_ERROR(EPERM);
364 * CAP_FSETID overrides the following restrictions:
366 * The effective user ID of the calling process shall match
367 * the file owner when setting the set-user-ID and
368 * set-group-ID bits on that file.
370 * The effective group ID or one of the supplementary group
371 * IDs of the calling process shall match the group owner of
372 * the file when setting the set-group-ID bit on that file
374 if (mask & XFS_AT_MODE) {
377 if ((vap->va_mode & S_ISUID) && !file_owner)
379 if ((vap->va_mode & S_ISGID) &&
380 !in_group_p((gid_t)ip->i_d.di_gid))
383 /* Linux allows this, Irix doesn't. */
384 if ((vap->va_mode & S_ISVTX) && !S_ISDIR(ip->i_d.di_mode))
387 if (m && !capable(CAP_FSETID))
393 * Change file ownership. Must be the owner or privileged.
394 * If the system was configured with the "restricted_chown"
395 * option, the owner is not permitted to give away the file,
396 * and can change the group id only to a group of which he
397 * or she is a member.
399 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
401 * These IDs could have changed since we last looked at them.
402 * But, we're assured that if the ownership did change
403 * while we didn't have the inode locked, inode's dquot(s)
404 * would have changed also.
406 iuid = ip->i_d.di_uid;
407 iprojid = ip->i_d.di_projid;
408 igid = ip->i_d.di_gid;
409 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
410 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
411 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
415 * CAP_CHOWN overrides the following restrictions:
417 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
418 * shall override the restriction that a process cannot
419 * change the user ID of a file it owns and the restriction
420 * that the group ID supplied to the chown() function
421 * shall be equal to either the group ID or one of the
422 * supplementary group IDs of the calling process.
424 if (restricted_chown &&
425 (iuid != uid || (igid != gid &&
426 !in_group_p((gid_t)gid))) &&
427 !capable(CAP_CHOWN)) {
428 code = XFS_ERROR(EPERM);
432 * Do a quota reservation only if uid/projid/gid is actually
435 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
436 (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
437 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
439 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
440 capable(CAP_FOWNER) ?
441 XFS_QMOPT_FORCE_RES : 0);
442 if (code) /* out of quota */
448 * Truncate file. Must have write permission and not be a directory.
450 if (mask & XFS_AT_SIZE) {
451 /* Short circuit the truncate case for zero length files */
452 if ((vap->va_size == 0) &&
453 (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) {
454 xfs_iunlock(ip, XFS_ILOCK_EXCL);
455 lock_flags &= ~XFS_ILOCK_EXCL;
456 if (mask & XFS_AT_CTIME)
457 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
462 if (S_ISDIR(ip->i_d.di_mode)) {
463 code = XFS_ERROR(EISDIR);
465 } else if (!S_ISREG(ip->i_d.di_mode)) {
466 code = XFS_ERROR(EINVAL);
470 * Make sure that the dquots are attached to the inode.
472 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
477 * Change file access or modified times.
479 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
481 if ((flags & ATTR_UTIME) &&
482 !capable(CAP_FOWNER)) {
483 code = XFS_ERROR(EPERM);
490 * Change extent size or realtime flag.
492 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
494 * Can't change extent size if any extents are allocated.
496 if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
497 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
499 code = XFS_ERROR(EINVAL); /* EFBIG? */
504 * Can't change realtime flag if any extents are allocated.
506 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
507 (mask & XFS_AT_XFLAGS) &&
508 (XFS_IS_REALTIME_INODE(ip)) !=
509 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
510 code = XFS_ERROR(EINVAL); /* EFBIG? */
514 * Extent size must be a multiple of the appropriate block
515 * size, if set at all.
517 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
520 if (XFS_IS_REALTIME_INODE(ip) ||
521 ((mask & XFS_AT_XFLAGS) &&
522 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
523 size = mp->m_sb.sb_rextsize <<
524 mp->m_sb.sb_blocklog;
526 size = mp->m_sb.sb_blocksize;
528 if (vap->va_extsize % size) {
529 code = XFS_ERROR(EINVAL);
534 * If realtime flag is set then must have realtime data.
536 if ((mask & XFS_AT_XFLAGS) &&
537 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
538 if ((mp->m_sb.sb_rblocks == 0) ||
539 (mp->m_sb.sb_rextsize == 0) ||
540 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
541 code = XFS_ERROR(EINVAL);
547 * Can't modify an immutable/append-only file unless
548 * we have appropriate permission.
550 if ((mask & XFS_AT_XFLAGS) &&
552 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
554 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
555 !capable(CAP_LINUX_IMMUTABLE)) {
556 code = XFS_ERROR(EPERM);
562 * Now we can make the changes. Before we join the inode
563 * to the transaction, if XFS_AT_SIZE is set then take care of
564 * the part of the truncation that must be done without the
565 * inode lock. This needs to be done before joining the inode
566 * to the transaction, because the inode cannot be unlocked
567 * once it is a part of the transaction.
569 if (mask & XFS_AT_SIZE) {
571 if ((vap->va_size > ip->i_size) &&
572 (flags & ATTR_NOSIZETOK) == 0) {
573 code = xfs_igrow_start(ip, vap->va_size, credp);
575 xfs_iunlock(ip, XFS_ILOCK_EXCL);
578 * We are going to log the inode size change in this
579 * transaction so any previous writes that are beyond the on
580 * disk EOF and the new EOF that have not been written out need
581 * to be written here. If we do not write the data out, we
582 * expose ourselves to the null files problem.
584 * Only flush from the on disk size to the smaller of the in
585 * memory file size or the new size as that's the range we
586 * really care about here and prevents waiting for other data
587 * not within the range we care about here.
590 (ip->i_size != ip->i_d.di_size) &&
591 (vap->va_size > ip->i_d.di_size)) {
592 code = xfs_flush_pages(ip,
593 ip->i_d.di_size, vap->va_size,
594 XFS_B_ASYNC, FI_NONE);
597 /* wait for all I/O to complete */
601 code = xfs_itruncate_data(ip, vap->va_size);
604 lock_flags &= ~XFS_ILOCK_EXCL;
605 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
608 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
609 if ((code = xfs_trans_reserve(tp, 0,
610 XFS_ITRUNCATE_LOG_RES(mp), 0,
611 XFS_TRANS_PERM_LOG_RES,
612 XFS_ITRUNCATE_LOG_COUNT))) {
613 xfs_trans_cancel(tp, 0);
615 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
618 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
619 xfs_ilock(ip, XFS_ILOCK_EXCL);
623 xfs_trans_ijoin(tp, ip, lock_flags);
624 xfs_trans_ihold(tp, ip);
628 * Truncate file. Must have write permission and not be a directory.
630 if (mask & XFS_AT_SIZE) {
632 * Only change the c/mtime if we are changing the size
633 * or we are explicitly asked to change it. This handles
634 * the semantic difference between truncate() and ftruncate()
635 * as implemented in the VFS.
637 if (vap->va_size != ip->i_size || (mask & XFS_AT_CTIME))
638 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
640 if (vap->va_size > ip->i_size) {
641 xfs_igrow_finish(tp, ip, vap->va_size,
642 !(flags & ATTR_DMI));
643 } else if ((vap->va_size <= ip->i_size) ||
644 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
646 * signal a sync transaction unless
647 * we're truncating an already unlinked
648 * file on a wsync filesystem
650 code = xfs_itruncate_finish(&tp, ip,
651 (xfs_fsize_t)vap->va_size,
653 ((ip->i_d.di_nlink != 0 ||
654 !(mp->m_flags & XFS_MOUNT_WSYNC))
659 * Truncated "down", so we're removing references
660 * to old data here - if we now delay flushing for
661 * a long time, we expose ourselves unduly to the
662 * notorious NULL files problem. So, we mark this
663 * vnode and flush it when the file is closed, and
664 * do not wait the usual (long) time for writeout.
666 xfs_iflags_set(ip, XFS_ITRUNCATED);
671 * Change file access modes.
673 if (mask & XFS_AT_MODE) {
674 ip->i_d.di_mode &= S_IFMT;
675 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
677 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
678 timeflags |= XFS_ICHGTIME_CHG;
682 * Change file ownership. Must be the owner or privileged.
683 * If the system was configured with the "restricted_chown"
684 * option, the owner is not permitted to give away the file,
685 * and can change the group id only to a group of which he
686 * or she is a member.
688 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
690 * CAP_FSETID overrides the following restrictions:
692 * The set-user-ID and set-group-ID bits of a file will be
693 * cleared upon successful return from chown()
695 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
696 !capable(CAP_FSETID)) {
697 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
701 * Change the ownerships and register quota modifications
702 * in the transaction.
705 if (XFS_IS_UQUOTA_ON(mp)) {
706 ASSERT(mask & XFS_AT_UID);
708 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
709 &ip->i_udquot, udqp);
711 ip->i_d.di_uid = uid;
714 if (XFS_IS_GQUOTA_ON(mp)) {
715 ASSERT(!XFS_IS_PQUOTA_ON(mp));
716 ASSERT(mask & XFS_AT_GID);
718 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
719 &ip->i_gdquot, gdqp);
721 ip->i_d.di_gid = gid;
723 if (iprojid != projid) {
724 if (XFS_IS_PQUOTA_ON(mp)) {
725 ASSERT(!XFS_IS_GQUOTA_ON(mp));
726 ASSERT(mask & XFS_AT_PROJID);
728 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
729 &ip->i_gdquot, gdqp);
731 ip->i_d.di_projid = projid;
733 * We may have to rev the inode as well as
734 * the superblock version number since projids didn't
735 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
737 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
738 xfs_bump_ino_vers2(tp, ip);
741 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
742 timeflags |= XFS_ICHGTIME_CHG;
747 * Change file access or modified times.
749 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
750 if (mask & XFS_AT_ATIME) {
751 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
752 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
753 ip->i_update_core = 1;
754 timeflags &= ~XFS_ICHGTIME_ACC;
756 if (mask & XFS_AT_MTIME) {
757 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
758 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
759 timeflags &= ~XFS_ICHGTIME_MOD;
760 timeflags |= XFS_ICHGTIME_CHG;
762 if (tp && (flags & ATTR_UTIME))
763 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
767 * Change XFS-added attributes.
769 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
770 if (mask & XFS_AT_EXTSIZE) {
772 * Converting bytes to fs blocks.
774 ip->i_d.di_extsize = vap->va_extsize >>
775 mp->m_sb.sb_blocklog;
777 if (mask & XFS_AT_XFLAGS) {
780 /* can't set PREALLOC this way, just preserve it */
781 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
782 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
783 di_flags |= XFS_DIFLAG_IMMUTABLE;
784 if (vap->va_xflags & XFS_XFLAG_APPEND)
785 di_flags |= XFS_DIFLAG_APPEND;
786 if (vap->va_xflags & XFS_XFLAG_SYNC)
787 di_flags |= XFS_DIFLAG_SYNC;
788 if (vap->va_xflags & XFS_XFLAG_NOATIME)
789 di_flags |= XFS_DIFLAG_NOATIME;
790 if (vap->va_xflags & XFS_XFLAG_NODUMP)
791 di_flags |= XFS_DIFLAG_NODUMP;
792 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
793 di_flags |= XFS_DIFLAG_PROJINHERIT;
794 if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
795 di_flags |= XFS_DIFLAG_NODEFRAG;
796 if (vap->va_xflags & XFS_XFLAG_FILESTREAM)
797 di_flags |= XFS_DIFLAG_FILESTREAM;
798 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
799 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
800 di_flags |= XFS_DIFLAG_RTINHERIT;
801 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
802 di_flags |= XFS_DIFLAG_NOSYMLINKS;
803 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
804 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
805 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
806 if (vap->va_xflags & XFS_XFLAG_REALTIME)
807 di_flags |= XFS_DIFLAG_REALTIME;
808 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
809 di_flags |= XFS_DIFLAG_EXTSIZE;
811 ip->i_d.di_flags = di_flags;
813 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
814 timeflags |= XFS_ICHGTIME_CHG;
818 * Change file inode change time only if XFS_AT_CTIME set
819 * AND we have been called by a DMI function.
822 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
823 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
824 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
825 ip->i_update_core = 1;
826 timeflags &= ~XFS_ICHGTIME_CHG;
830 * Send out timestamp changes that need to be set to the
831 * current time. Not done when called by a DMI function.
833 if (timeflags && !(flags & ATTR_DMI))
834 xfs_ichgtime(ip, timeflags);
836 XFS_STATS_INC(xs_ig_attrchg);
839 * If this is a synchronous mount, make sure that the
840 * transaction goes to disk before returning to the user.
841 * This is slightly sub-optimal in that truncates require
842 * two sync transactions instead of one for wsync filesystems.
843 * One for the truncate and one for the timestamps since we
844 * don't want to change the timestamps unless we're sure the
845 * truncate worked. Truncates are less than 1% of the laddis
846 * mix so this probably isn't worth the trouble to optimize.
850 if (mp->m_flags & XFS_MOUNT_WSYNC)
851 xfs_trans_set_sync(tp);
853 code = xfs_trans_commit(tp, commit_flags);
856 xfs_iunlock(ip, lock_flags);
859 * Release any dquot(s) the inode had kept before chown.
861 XFS_QM_DQRELE(mp, olddquot1);
862 XFS_QM_DQRELE(mp, olddquot2);
863 XFS_QM_DQRELE(mp, udqp);
864 XFS_QM_DQRELE(mp, gdqp);
870 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
871 !(flags & ATTR_DMI)) {
872 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
873 NULL, DM_RIGHT_NULL, NULL, NULL,
874 0, 0, AT_DELAY_FLAG(flags));
879 commit_flags |= XFS_TRANS_ABORT;
882 XFS_QM_DQRELE(mp, udqp);
883 XFS_QM_DQRELE(mp, gdqp);
885 xfs_trans_cancel(tp, commit_flags);
887 if (lock_flags != 0) {
888 xfs_iunlock(ip, lock_flags);
894 * The maximum pathlen is 1024 bytes. Since the minimum file system
895 * blocksize is 512 bytes, we can get a max of 2 extents back from
898 #define SYMLINK_MAPS 2
905 xfs_mount_t *mp = ip->i_mount;
906 int pathlen = ip->i_d.di_size;
907 int nmaps = SYMLINK_MAPS;
908 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
915 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
916 mval, &nmaps, NULL, NULL);
920 for (n = 0; n < nmaps; n++) {
921 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
922 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
924 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
925 error = XFS_BUF_GETERROR(bp);
927 xfs_ioerror_alert("xfs_readlink",
928 ip->i_mount, bp, XFS_BUF_ADDR(bp));
932 if (pathlen < byte_cnt)
936 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
940 link[ip->i_d.di_size] = '\0';
952 xfs_mount_t *mp = ip->i_mount;
956 xfs_itrace_entry(ip);
958 if (XFS_FORCED_SHUTDOWN(mp))
959 return XFS_ERROR(EIO);
961 xfs_ilock(ip, XFS_ILOCK_SHARED);
963 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
964 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
966 pathlen = ip->i_d.di_size;
970 if (ip->i_df.if_flags & XFS_IFINLINE) {
971 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
972 link[pathlen] = '\0';
974 error = xfs_readlink_bmap(ip, link);
978 xfs_iunlock(ip, XFS_ILOCK_SHARED);
985 * This is called to sync the inode and its data out to disk.
986 * We need to hold the I/O lock while flushing the data, and
987 * the inode lock while flushing the inode. The inode lock CANNOT
988 * be held while flushing the data, so acquire after we're done
1000 int log_flushed = 0, changed = 1;
1002 xfs_itrace_entry(ip);
1004 ASSERT(start >= 0 && stop >= -1);
1006 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1007 return XFS_ERROR(EIO);
1009 if (flag & FSYNC_DATA)
1010 filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
1013 * We always need to make sure that the required inode state
1014 * is safe on disk. The vnode might be clean but because
1015 * of committed transactions that haven't hit the disk yet.
1016 * Likewise, there could be unflushed non-transactional
1017 * changes to the inode core that have to go to disk.
1019 * The following code depends on one assumption: that
1020 * any transaction that changes an inode logs the core
1021 * because it has to change some field in the inode core
1022 * (typically nextents or nblocks). That assumption
1023 * implies that any transactions against an inode will
1024 * catch any non-transactional updates. If inode-altering
1025 * transactions exist that violate this assumption, the
1026 * code breaks. Right now, it figures that if the involved
1027 * update_* field is clear and the inode is unpinned, the
1028 * inode is clean. Either it's been flushed or it's been
1029 * committed and the commit has hit the disk unpinning the inode.
1030 * (Note that xfs_inode_item_format() called at commit clears
1031 * the update_* fields.)
1033 xfs_ilock(ip, XFS_ILOCK_SHARED);
1035 /* If we are flushing data then we care about update_size
1036 * being set, otherwise we care about update_core
1038 if ((flag & FSYNC_DATA) ?
1039 (ip->i_update_size == 0) :
1040 (ip->i_update_core == 0)) {
1042 * Timestamps/size haven't changed since last inode
1043 * flush or inode transaction commit. That means
1044 * either nothing got written or a transaction
1045 * committed which caught the updates. If the
1046 * latter happened and the transaction hasn't
1047 * hit the disk yet, the inode will be still
1048 * be pinned. If it is, force the log.
1051 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1053 if (xfs_ipincount(ip)) {
1054 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
1056 ((flag & FSYNC_WAIT)
1057 ? XFS_LOG_SYNC : 0),
1061 * If the inode is not pinned and nothing
1062 * has changed we don't need to flush the
1070 * Kick off a transaction to log the inode
1071 * core to get the updates. Make it
1072 * sync if FSYNC_WAIT is passed in (which
1073 * is done by everybody but specfs). The
1074 * sync transaction will also force the log.
1076 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1077 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1078 if ((error = xfs_trans_reserve(tp, 0,
1079 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1081 xfs_trans_cancel(tp, 0);
1084 xfs_ilock(ip, XFS_ILOCK_EXCL);
1087 * Note - it's possible that we might have pushed
1088 * ourselves out of the way during trans_reserve
1089 * which would flush the inode. But there's no
1090 * guarantee that the inode buffer has actually
1091 * gone out yet (it's delwri). Plus the buffer
1092 * could be pinned anyway if it's part of an
1093 * inode in another recent transaction. So we
1094 * play it safe and fire off the transaction anyway.
1096 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1097 xfs_trans_ihold(tp, ip);
1098 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1099 if (flag & FSYNC_WAIT)
1100 xfs_trans_set_sync(tp);
1101 error = _xfs_trans_commit(tp, 0, &log_flushed);
1103 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1106 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1108 * If the log write didn't issue an ordered tag we need
1109 * to flush the disk cache for the data device now.
1112 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1115 * If this inode is on the RT dev we need to flush that
1118 if (XFS_IS_REALTIME_INODE(ip))
1119 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1126 * This is called by xfs_inactive to free any blocks beyond eof
1127 * when the link count isn't zero and by xfs_dm_punch_hole() when
1128 * punching a hole to EOF.
1138 xfs_fileoff_t end_fsb;
1139 xfs_fileoff_t last_fsb;
1140 xfs_filblks_t map_len;
1142 xfs_bmbt_irec_t imap;
1143 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
1146 * Figure out if there are any blocks beyond the end
1147 * of the file. If not, then there is nothing to do.
1149 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
1150 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1151 map_len = last_fsb - end_fsb;
1156 xfs_ilock(ip, XFS_ILOCK_SHARED);
1157 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
1158 NULL, 0, &imap, &nimaps, NULL, NULL);
1159 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1161 if (!error && (nimaps != 0) &&
1162 (imap.br_startblock != HOLESTARTBLOCK ||
1163 ip->i_delayed_blks)) {
1165 * Attach the dquots to the inode up front.
1167 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1171 * There are blocks after the end of file.
1172 * Free them up now by truncating the file to
1175 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1178 * Do the xfs_itruncate_start() call before
1179 * reserving any log space because
1180 * itruncate_start will call into the buffer
1181 * cache and we can't
1182 * do that within a transaction.
1185 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1186 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
1189 xfs_trans_cancel(tp, 0);
1191 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1195 error = xfs_trans_reserve(tp, 0,
1196 XFS_ITRUNCATE_LOG_RES(mp),
1197 0, XFS_TRANS_PERM_LOG_RES,
1198 XFS_ITRUNCATE_LOG_COUNT);
1200 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1201 xfs_trans_cancel(tp, 0);
1202 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1206 xfs_ilock(ip, XFS_ILOCK_EXCL);
1207 xfs_trans_ijoin(tp, ip,
1210 xfs_trans_ihold(tp, ip);
1212 error = xfs_itruncate_finish(&tp, ip,
1217 * If we get an error at this point we
1218 * simply don't bother truncating the file.
1221 xfs_trans_cancel(tp,
1222 (XFS_TRANS_RELEASE_LOG_RES |
1225 error = xfs_trans_commit(tp,
1226 XFS_TRANS_RELEASE_LOG_RES);
1228 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1235 * Free a symlink that has blocks associated with it.
1238 xfs_inactive_symlink_rmt(
1246 xfs_fsblock_t first_block;
1247 xfs_bmap_free_t free_list;
1250 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1258 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1260 * We're freeing a symlink that has some
1261 * blocks allocated to it. Free the
1262 * blocks here. We know that we've got
1263 * either 1 or 2 extents and that we can
1264 * free them all in one bunmapi call.
1266 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1267 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1268 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1269 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1270 xfs_trans_cancel(tp, 0);
1275 * Lock the inode, fix the size, and join it to the transaction.
1276 * Hold it so in the normal path, we still have it locked for
1277 * the second transaction. In the error paths we need it
1278 * held so the cancel won't rele it, see below.
1280 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1281 size = (int)ip->i_d.di_size;
1282 ip->i_d.di_size = 0;
1283 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1284 xfs_trans_ihold(tp, ip);
1285 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1287 * Find the block(s) so we can inval and unmap them.
1290 XFS_BMAP_INIT(&free_list, &first_block);
1291 nmaps = ARRAY_SIZE(mval);
1292 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1293 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
1297 * Invalidate the block(s).
1299 for (i = 0; i < nmaps; i++) {
1300 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1301 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1302 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1303 xfs_trans_binval(tp, bp);
1306 * Unmap the dead block(s) to the free_list.
1308 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
1309 &first_block, &free_list, NULL, &done)))
1313 * Commit the first transaction. This logs the EFI and the inode.
1315 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
1318 * The transaction must have been committed, since there were
1319 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1320 * The new tp has the extent freeing and EFDs.
1324 * The first xact was committed, so add the inode to the new one.
1325 * Mark it dirty so it will be logged and moved forward in the log as
1326 * part of every commit.
1328 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1329 xfs_trans_ihold(tp, ip);
1330 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1332 * Get a new, empty transaction to return to our caller.
1334 ntp = xfs_trans_dup(tp);
1336 * Commit the transaction containing extent freeing and EFDs.
1337 * If we get an error on the commit here or on the reserve below,
1338 * we need to unlock the inode since the new transaction doesn't
1339 * have the inode attached.
1341 error = xfs_trans_commit(tp, 0);
1344 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1348 * Remove the memory for extent descriptions (just bookkeeping).
1350 if (ip->i_df.if_bytes)
1351 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1352 ASSERT(ip->i_df.if_bytes == 0);
1354 * Put an itruncate log reservation in the new transaction
1357 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1358 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1359 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1363 * Return with the inode locked but not joined to the transaction.
1369 xfs_bmap_cancel(&free_list);
1372 * Have to come here with the inode locked and either
1373 * (held and in the transaction) or (not in the transaction).
1374 * If the inode isn't held then cancel would iput it, but
1375 * that's wrong since this is inactive and the vnode ref
1376 * count is 0 already.
1377 * Cancel won't do anything to the inode if held, but it still
1378 * needs to be locked until the cancel is done, if it was
1379 * joined to the transaction.
1381 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1382 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1389 xfs_inactive_symlink_local(
1395 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1397 * We're freeing a symlink which fit into
1398 * the inode. Just free the memory used
1399 * to hold the old symlink.
1401 error = xfs_trans_reserve(*tpp, 0,
1402 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1403 0, XFS_TRANS_PERM_LOG_RES,
1404 XFS_ITRUNCATE_LOG_COUNT);
1407 xfs_trans_cancel(*tpp, 0);
1411 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1414 * Zero length symlinks _can_ exist.
1416 if (ip->i_df.if_bytes > 0) {
1417 xfs_idata_realloc(ip,
1418 -(ip->i_df.if_bytes),
1420 ASSERT(ip->i_df.if_bytes == 0);
1434 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1437 ASSERT(ip->i_d.di_forkoff != 0);
1438 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1439 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1443 error = xfs_attr_inactive(ip);
1447 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1448 error = xfs_trans_reserve(tp, 0,
1449 XFS_IFREE_LOG_RES(mp),
1450 0, XFS_TRANS_PERM_LOG_RES,
1451 XFS_INACTIVE_LOG_COUNT);
1455 xfs_ilock(ip, XFS_ILOCK_EXCL);
1456 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1457 xfs_trans_ihold(tp, ip);
1458 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1460 ASSERT(ip->i_d.di_anextents == 0);
1466 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1467 xfs_trans_cancel(tp, 0);
1470 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1478 bhv_vnode_t *vp = XFS_ITOV(ip);
1479 xfs_mount_t *mp = ip->i_mount;
1482 if (!S_ISREG(ip->i_d.di_mode) || (ip->i_d.di_mode == 0))
1485 /* If this is a read-only mount, don't do this (would generate I/O) */
1486 if (mp->m_flags & XFS_MOUNT_RDONLY)
1489 if (!XFS_FORCED_SHUTDOWN(mp)) {
1493 * If we are using filestreams, and we have an unlinked
1494 * file that we are processing the last close on, then nothing
1495 * will be able to reopen and write to this file. Purge this
1496 * inode from the filestreams cache so that it doesn't delay
1497 * teardown of the inode.
1499 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1500 xfs_filestream_deassociate(ip);
1503 * If we previously truncated this file and removed old data
1504 * in the process, we want to initiate "early" writeout on
1505 * the last close. This is an attempt to combat the notorious
1506 * NULL files problem which is particularly noticable from a
1507 * truncate down, buffered (re-)write (delalloc), followed by
1508 * a crash. What we are effectively doing here is
1509 * significantly reducing the time window where we'd otherwise
1510 * be exposed to that problem.
1512 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1513 if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
1514 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
1517 if (ip->i_d.di_nlink != 0) {
1518 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1519 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1520 ip->i_delayed_blks > 0)) &&
1521 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1522 (!(ip->i_d.di_flags &
1523 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1524 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1536 * This is called when the vnode reference count for the vnode
1537 * goes to zero. If the file has been unlinked, then it must
1538 * now be truncated. Also, we clear all of the read-ahead state
1539 * kept for the inode here since the file is now closed.
1545 bhv_vnode_t *vp = XFS_ITOV(ip);
1546 xfs_bmap_free_t free_list;
1547 xfs_fsblock_t first_block;
1554 xfs_itrace_entry(ip);
1557 * If the inode is already free, then there can be nothing
1560 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1561 ASSERT(ip->i_df.if_real_bytes == 0);
1562 ASSERT(ip->i_df.if_broot_bytes == 0);
1563 return VN_INACTIVE_CACHE;
1567 * Only do a truncate if it's a regular file with
1568 * some actual space in it. It's OK to look at the
1569 * inode's fields without the lock because we're the
1570 * only one with a reference to the inode.
1572 truncate = ((ip->i_d.di_nlink == 0) &&
1573 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1574 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1575 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1579 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1580 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
1584 /* If this is a read-only mount, don't do this (would generate I/O) */
1585 if (mp->m_flags & XFS_MOUNT_RDONLY)
1588 if (ip->i_d.di_nlink != 0) {
1589 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1590 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1591 ip->i_delayed_blks > 0)) &&
1592 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1593 (!(ip->i_d.di_flags &
1594 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1595 (ip->i_delayed_blks != 0)))) {
1596 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1598 return VN_INACTIVE_CACHE;
1603 ASSERT(ip->i_d.di_nlink == 0);
1605 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1606 return VN_INACTIVE_CACHE;
1608 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1611 * Do the xfs_itruncate_start() call before
1612 * reserving any log space because itruncate_start
1613 * will call into the buffer cache and we can't
1614 * do that within a transaction.
1616 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1618 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1620 xfs_trans_cancel(tp, 0);
1621 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1622 return VN_INACTIVE_CACHE;
1625 error = xfs_trans_reserve(tp, 0,
1626 XFS_ITRUNCATE_LOG_RES(mp),
1627 0, XFS_TRANS_PERM_LOG_RES,
1628 XFS_ITRUNCATE_LOG_COUNT);
1630 /* Don't call itruncate_cleanup */
1631 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1632 xfs_trans_cancel(tp, 0);
1633 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1634 return VN_INACTIVE_CACHE;
1637 xfs_ilock(ip, XFS_ILOCK_EXCL);
1638 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1639 xfs_trans_ihold(tp, ip);
1642 * normally, we have to run xfs_itruncate_finish sync.
1643 * But if filesystem is wsync and we're in the inactive
1644 * path, then we know that nlink == 0, and that the
1645 * xaction that made nlink == 0 is permanently committed
1646 * since xfs_remove runs as a synchronous transaction.
1648 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1649 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1652 xfs_trans_cancel(tp,
1653 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1654 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1655 return VN_INACTIVE_CACHE;
1657 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1660 * If we get an error while cleaning up a
1661 * symlink we bail out.
1663 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1664 xfs_inactive_symlink_rmt(ip, &tp) :
1665 xfs_inactive_symlink_local(ip, &tp);
1669 return VN_INACTIVE_CACHE;
1672 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1673 xfs_trans_ihold(tp, ip);
1675 error = xfs_trans_reserve(tp, 0,
1676 XFS_IFREE_LOG_RES(mp),
1677 0, XFS_TRANS_PERM_LOG_RES,
1678 XFS_INACTIVE_LOG_COUNT);
1680 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1681 xfs_trans_cancel(tp, 0);
1682 return VN_INACTIVE_CACHE;
1685 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1686 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1687 xfs_trans_ihold(tp, ip);
1691 * If there are attributes associated with the file
1692 * then blow them away now. The code calls a routine
1693 * that recursively deconstructs the attribute fork.
1694 * We need to just commit the current transaction
1695 * because we can't use it for xfs_attr_inactive().
1697 if (ip->i_d.di_anextents > 0) {
1698 error = xfs_inactive_attrs(ip, &tp);
1700 * If we got an error, the transaction is already
1701 * cancelled, and the inode is unlocked. Just get out.
1704 return VN_INACTIVE_CACHE;
1705 } else if (ip->i_afp) {
1706 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1712 XFS_BMAP_INIT(&free_list, &first_block);
1713 error = xfs_ifree(tp, ip, &free_list);
1716 * If we fail to free the inode, shut down. The cancel
1717 * might do that, we need to make sure. Otherwise the
1718 * inode might be lost for a long time or forever.
1720 if (!XFS_FORCED_SHUTDOWN(mp)) {
1722 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1723 error, mp->m_fsname);
1724 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1726 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1729 * Credit the quota account(s). The inode is gone.
1731 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1734 * Just ignore errors at this point. There is nothing we can
1735 * do except to try to keep going. Make sure it's not a silent
1738 error = xfs_bmap_finish(&tp, &free_list, &committed);
1740 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1741 "xfs_bmap_finish() returned error %d", error);
1742 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1744 xfs_fs_cmn_err(CE_NOTE, mp, "xfs_inactive: "
1745 "xfs_trans_commit() returned error %d", error);
1748 * Release the dquots held by inode, if any.
1750 XFS_QM_DQDETACH(mp, ip);
1752 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1755 return VN_INACTIVE_CACHE;
1762 struct xfs_name *name,
1770 xfs_itrace_entry(dp);
1772 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1773 return XFS_ERROR(EIO);
1775 lock_mode = xfs_ilock_map_shared(dp);
1776 error = xfs_dir_lookup_int(dp, lock_mode, name, &e_inum, &ip);
1781 xfs_iunlock_map_shared(dp, lock_mode);
1788 struct xfs_name *name,
1794 xfs_mount_t *mp = dp->i_mount;
1798 xfs_bmap_free_t free_list;
1799 xfs_fsblock_t first_block;
1800 boolean_t unlock_dp_on_error = B_FALSE;
1801 int dm_event_sent = 0;
1805 struct xfs_dquot *udqp, *gdqp;
1809 xfs_itrace_entry(dp);
1811 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1812 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1813 dp, DM_RIGHT_NULL, NULL,
1814 DM_RIGHT_NULL, name->name, NULL,
1822 if (XFS_FORCED_SHUTDOWN(mp))
1823 return XFS_ERROR(EIO);
1825 /* Return through std_return after this point. */
1828 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1829 prid = dp->i_d.di_projid;
1831 prid = (xfs_prid_t)dfltprid;
1834 * Make sure that we have allocated dquot(s) on disk.
1836 error = XFS_QM_DQVOPALLOC(mp, dp,
1837 current_fsuid(credp), current_fsgid(credp), prid,
1838 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1844 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1845 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1846 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1848 * Initially assume that the file does not exist and
1849 * reserve the resources for that case. If that is not
1850 * the case we'll drop the one we have and get a more
1851 * appropriate transaction later.
1853 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1854 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1855 if (error == ENOSPC) {
1857 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1858 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1865 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1866 unlock_dp_on_error = B_TRUE;
1868 XFS_BMAP_INIT(&free_list, &first_block);
1873 * Reserve disk quota and the inode.
1875 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1879 error = xfs_dir_canenter(tp, dp, name, resblks);
1882 error = xfs_dir_ialloc(&tp, dp, mode, 1,
1883 rdev, credp, prid, resblks > 0,
1886 if (error == ENOSPC)
1893 * At this point, we've gotten a newly allocated inode.
1894 * It is locked (and joined to the transaction).
1897 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1900 * Now we join the directory inode to the transaction. We do not do it
1901 * earlier because xfs_dir_ialloc might commit the previous transaction
1902 * (and release all the locks). An error from here on will result in
1903 * the transaction cancel unlocking dp so don't do it explicitly in the
1907 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1908 unlock_dp_on_error = B_FALSE;
1910 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1911 &first_block, &free_list, resblks ?
1912 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1914 ASSERT(error != ENOSPC);
1917 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1918 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1921 * If this is a synchronous mount, make sure that the
1922 * create transaction goes to disk before returning to
1925 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1926 xfs_trans_set_sync(tp);
1932 * Attach the dquot(s) to the inodes and modify them incore.
1933 * These ids of the inode couldn't have changed since the new
1934 * inode has been locked ever since it was created.
1936 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1939 * xfs_trans_commit normally decrements the vnode ref count
1940 * when it unlocks the inode. Since we want to return the
1941 * vnode to the caller, we bump the vnode ref count now.
1945 error = xfs_bmap_finish(&tp, &free_list, &committed);
1947 xfs_bmap_cancel(&free_list);
1951 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1958 XFS_QM_DQRELE(mp, udqp);
1959 XFS_QM_DQRELE(mp, gdqp);
1963 /* Fallthrough to std_return with error = 0 */
1966 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
1967 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1968 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
1971 DM_RIGHT_NULL, name->name, NULL,
1977 cancel_flags |= XFS_TRANS_ABORT;
1982 xfs_trans_cancel(tp, cancel_flags);
1984 XFS_QM_DQRELE(mp, udqp);
1985 XFS_QM_DQRELE(mp, gdqp);
1987 if (unlock_dp_on_error)
1988 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1994 * Wait until after the current transaction is aborted to
1995 * release the inode. This prevents recursive transactions
1996 * and deadlocks from xfs_inactive.
1998 cancel_flags |= XFS_TRANS_ABORT;
1999 xfs_trans_cancel(tp, cancel_flags);
2002 XFS_QM_DQRELE(mp, udqp);
2003 XFS_QM_DQRELE(mp, gdqp);
2010 * Some counters to see if (and how often) we are hitting some deadlock
2011 * prevention code paths.
2015 int xfs_rm_lock_delays;
2016 int xfs_rm_attempts;
2020 * The following routine will lock the inodes associated with the
2021 * directory and the named entry in the directory. The locks are
2022 * acquired in increasing inode number.
2024 * If the entry is "..", then only the directory is locked. The
2025 * vnode ref count will still include that from the .. entry in
2028 * There is a deadlock we need to worry about. If the locked directory is
2029 * in the AIL, it might be blocking up the log. The next inode we lock
2030 * could be already locked by another thread waiting for log space (e.g
2031 * a permanent log reservation with a long running transaction (see
2032 * xfs_itruncate_finish)). To solve this, we must check if the directory
2033 * is in the ail and use lock_nowait. If we can't lock, we need to
2034 * drop the inode lock on the directory and try again. xfs_iunlock will
2035 * potentially push the tail if we were holding up the log.
2038 xfs_lock_dir_and_entry(
2040 xfs_inode_t *ip) /* inode of entry 'name' */
2044 xfs_inode_t *ips[2];
2053 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2060 * We want to lock in increasing inum. Since we've already
2061 * acquired the lock on the directory, we may need to release
2062 * if if the inum of the entry turns out to be less.
2064 if (e_inum > dp->i_ino) {
2066 * We are already in the right order, so just
2067 * lock on the inode of the entry.
2068 * We need to use nowait if dp is in the AIL.
2071 lp = (xfs_log_item_t *)dp->i_itemp;
2072 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2073 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2080 * Unlock dp and try again.
2081 * xfs_iunlock will try to push the tail
2082 * if the inode is in the AIL.
2085 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2087 if ((attempts % 5) == 0) {
2088 delay(1); /* Don't just spin the CPU */
2090 xfs_rm_lock_delays++;
2096 xfs_ilock(ip, XFS_ILOCK_EXCL);
2098 } else if (e_inum < dp->i_ino) {
2099 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2103 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2105 /* else e_inum == dp->i_ino */
2106 /* This can happen if we're asked to lock /x/..
2107 * the entry is "..", which is also the parent directory.
2115 int xfs_small_retries;
2116 int xfs_middle_retries;
2117 int xfs_lots_retries;
2118 int xfs_lock_delays;
2122 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2126 xfs_lock_inumorder(int lock_mode, int subclass)
2128 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
2129 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
2130 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
2131 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
2137 * The following routine will lock n inodes in exclusive mode.
2138 * We assume the caller calls us with the inodes in i_ino order.
2140 * We need to detect deadlock where an inode that we lock
2141 * is in the AIL and we start waiting for another inode that is locked
2142 * by a thread in a long running transaction (such as truncate). This can
2143 * result in deadlock since the long running trans might need to wait
2144 * for the inode we just locked in order to push the tail and free space
2154 int attempts = 0, i, j, try_lock;
2157 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2168 for (; i < inodes; i++) {
2171 if (i && (ips[i] == ips[i-1])) /* Already locked */
2175 * If try_lock is not set yet, make sure all locked inodes
2176 * are not in the AIL.
2177 * If any are, set try_lock to be used later.
2181 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2182 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2183 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2190 * If any of the previous locks we have locked is in the AIL,
2191 * we must TRY to get the second and subsequent locks. If
2192 * we can't get any, we must release all we have
2197 /* try_lock must be 0 if i is 0. */
2199 * try_lock means we have an inode locked
2200 * that is in the AIL.
2203 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
2207 * Unlock all previous guys and try again.
2208 * xfs_iunlock will try to push the tail
2209 * if the inode is in the AIL.
2212 for(j = i - 1; j >= 0; j--) {
2215 * Check to see if we've already
2216 * unlocked this one.
2217 * Not the first one going back,
2218 * and the inode ptr is the same.
2220 if ((j != (i - 1)) && ips[j] ==
2224 xfs_iunlock(ips[j], lock_mode);
2227 if ((attempts % 5) == 0) {
2228 delay(1); /* Don't just spin the CPU */
2238 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
2244 if (attempts < 5) xfs_small_retries++;
2245 else if (attempts < 100) xfs_middle_retries++;
2246 else xfs_lots_retries++;
2254 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2255 int remove_which_error_return = 0;
2257 #define REMOVE_DEBUG_TRACE(x)
2258 #endif /* ! DEBUG */
2263 struct xfs_name *name,
2266 xfs_mount_t *mp = dp->i_mount;
2267 xfs_trans_t *tp = NULL;
2269 xfs_bmap_free_t free_list;
2270 xfs_fsblock_t first_block;
2276 xfs_itrace_entry(dp);
2278 if (XFS_FORCED_SHUTDOWN(mp))
2279 return XFS_ERROR(EIO);
2281 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
2282 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
2283 NULL, DM_RIGHT_NULL, name->name, NULL,
2284 ip->i_d.di_mode, 0, 0);
2290 * We need to get a reference to ip before we get our log
2291 * reservation. The reason for this is that we cannot call
2292 * xfs_iget for an inode for which we do not have a reference
2293 * once we've acquired a log reservation. This is because the
2294 * inode we are trying to get might be in xfs_inactive going
2295 * for a log reservation. Since we'll have to wait for the
2296 * inactive code to complete before returning from xfs_iget,
2297 * we need to make sure that we don't have log space reserved
2298 * when we call xfs_iget. Instead we get an unlocked reference
2299 * to the inode before getting our log reservation.
2303 xfs_itrace_entry(ip);
2306 error = XFS_QM_DQATTACH(mp, dp, 0);
2307 if (!error && dp != ip)
2308 error = XFS_QM_DQATTACH(mp, ip, 0);
2310 REMOVE_DEBUG_TRACE(__LINE__);
2315 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2316 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2318 * We try to get the real space reservation first,
2319 * allowing for directory btree deletion(s) implying
2320 * possible bmap insert(s). If we can't get the space
2321 * reservation then we use 0 instead, and avoid the bmap
2322 * btree insert(s) in the directory code by, if the bmap
2323 * insert tries to happen, instead trimming the LAST
2324 * block from the directory.
2326 resblks = XFS_REMOVE_SPACE_RES(mp);
2327 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2328 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2329 if (error == ENOSPC) {
2331 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2332 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2335 ASSERT(error != ENOSPC);
2336 REMOVE_DEBUG_TRACE(__LINE__);
2337 xfs_trans_cancel(tp, 0);
2342 error = xfs_lock_dir_and_entry(dp, ip);
2344 REMOVE_DEBUG_TRACE(__LINE__);
2345 xfs_trans_cancel(tp, cancel_flags);
2351 * At this point, we've gotten both the directory and the entry
2354 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2357 * Increment vnode ref count only in this case since
2358 * there's an extra vnode reference in the case where
2362 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2366 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2368 XFS_BMAP_INIT(&free_list, &first_block);
2369 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
2370 &first_block, &free_list, 0);
2372 ASSERT(error != ENOENT);
2373 REMOVE_DEBUG_TRACE(__LINE__);
2376 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2379 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2381 error = xfs_droplink(tp, ip);
2383 REMOVE_DEBUG_TRACE(__LINE__);
2387 /* Determine if this is the last link while
2388 * we are in the transaction.
2390 link_zero = (ip)->i_d.di_nlink==0;
2393 * Take an extra ref on the inode so that it doesn't
2394 * go to xfs_inactive() from within the commit.
2399 * If this is a synchronous mount, make sure that the
2400 * remove transaction goes to disk before returning to
2403 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2404 xfs_trans_set_sync(tp);
2407 error = xfs_bmap_finish(&tp, &free_list, &committed);
2409 REMOVE_DEBUG_TRACE(__LINE__);
2413 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2420 * If we are using filestreams, kill the stream association.
2421 * If the file is still open it may get a new one but that
2422 * will get killed on last close in xfs_close() so we don't
2423 * have to worry about that.
2425 if (link_zero && xfs_inode_is_filestream(ip))
2426 xfs_filestream_deassociate(ip);
2428 xfs_itrace_exit(ip);
2431 /* Fall through to std_return with error = 0 */
2433 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
2434 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2436 NULL, DM_RIGHT_NULL,
2437 name->name, NULL, ip->i_d.di_mode, error, 0);
2442 xfs_bmap_cancel(&free_list);
2443 cancel_flags |= XFS_TRANS_ABORT;
2444 xfs_trans_cancel(tp, cancel_flags);
2449 * In this case make sure to not release the inode until after
2450 * the current transaction is aborted. Releasing it beforehand
2451 * can cause us to go to xfs_inactive and start a recursive
2452 * transaction which can easily deadlock with the current one.
2454 xfs_bmap_cancel(&free_list);
2455 cancel_flags |= XFS_TRANS_ABORT;
2456 xfs_trans_cancel(tp, cancel_flags);
2467 struct xfs_name *target_name)
2469 xfs_mount_t *mp = tdp->i_mount;
2471 xfs_inode_t *ips[2];
2473 xfs_bmap_free_t free_list;
2474 xfs_fsblock_t first_block;
2479 xfs_itrace_entry(tdp);
2480 xfs_itrace_entry(sip);
2482 ASSERT(!S_ISDIR(sip->i_d.di_mode));
2484 if (XFS_FORCED_SHUTDOWN(mp))
2485 return XFS_ERROR(EIO);
2487 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
2488 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2491 target_name->name, NULL, 0, 0, 0);
2496 /* Return through std_return after this point. */
2498 error = XFS_QM_DQATTACH(mp, sip, 0);
2499 if (!error && sip != tdp)
2500 error = XFS_QM_DQATTACH(mp, tdp, 0);
2504 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2505 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2506 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
2507 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2508 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2509 if (error == ENOSPC) {
2511 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2512 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2519 if (sip->i_ino < tdp->i_ino) {
2527 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2530 * Increment vnode ref counts since xfs_trans_commit &
2531 * xfs_trans_cancel will both unlock the inodes and
2532 * decrement the associated ref counts.
2536 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2537 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2540 * If the source has too many links, we can't make any more to it.
2542 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2543 error = XFS_ERROR(EMLINK);
2548 * If we are using project inheritance, we only allow hard link
2549 * creation in our tree when the project IDs are the same; else
2550 * the tree quota mechanism could be circumvented.
2552 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2553 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2554 error = XFS_ERROR(EXDEV);
2558 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2562 XFS_BMAP_INIT(&free_list, &first_block);
2564 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2565 &first_block, &free_list, resblks);
2568 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2570 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2572 error = xfs_bumplink(tp, sip);
2577 * If this is a synchronous mount, make sure that the
2578 * link transaction goes to disk before returning to
2581 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2582 xfs_trans_set_sync(tp);
2585 error = xfs_bmap_finish (&tp, &free_list, &committed);
2587 xfs_bmap_cancel(&free_list);
2591 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2595 /* Fall through to std_return with error = 0. */
2597 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
2598 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2601 target_name->name, NULL, 0, error, 0);
2606 cancel_flags |= XFS_TRANS_ABORT;
2610 xfs_trans_cancel(tp, cancel_flags);
2618 struct xfs_name *dir_name,
2623 xfs_mount_t *mp = dp->i_mount;
2624 xfs_inode_t *cdp; /* inode of created dir */
2629 xfs_bmap_free_t free_list;
2630 xfs_fsblock_t first_block;
2631 boolean_t unlock_dp_on_error = B_FALSE;
2632 boolean_t created = B_FALSE;
2633 int dm_event_sent = 0;
2635 struct xfs_dquot *udqp, *gdqp;
2638 if (XFS_FORCED_SHUTDOWN(mp))
2639 return XFS_ERROR(EIO);
2643 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
2644 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2645 dp, DM_RIGHT_NULL, NULL,
2646 DM_RIGHT_NULL, dir_name->name, NULL,
2653 /* Return through std_return after this point. */
2655 xfs_itrace_entry(dp);
2659 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2660 prid = dp->i_d.di_projid;
2662 prid = (xfs_prid_t)dfltprid;
2665 * Make sure that we have allocated dquot(s) on disk.
2667 error = XFS_QM_DQVOPALLOC(mp, dp,
2668 current_fsuid(credp), current_fsgid(credp), prid,
2669 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2673 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2674 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2675 resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
2676 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2677 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2678 if (error == ENOSPC) {
2680 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2681 XFS_TRANS_PERM_LOG_RES,
2682 XFS_MKDIR_LOG_COUNT);
2689 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2690 unlock_dp_on_error = B_TRUE;
2693 * Check for directory link count overflow.
2695 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2696 error = XFS_ERROR(EMLINK);
2701 * Reserve disk quota and the inode.
2703 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2707 error = xfs_dir_canenter(tp, dp, dir_name, resblks);
2711 * create the directory inode.
2713 error = xfs_dir_ialloc(&tp, dp, mode, 2,
2714 0, credp, prid, resblks > 0,
2717 if (error == ENOSPC)
2721 xfs_itrace_ref(cdp);
2724 * Now we add the directory inode to the transaction.
2725 * We waited until now since xfs_dir_ialloc might start
2726 * a new transaction. Had we joined the transaction
2727 * earlier, the locks might have gotten released. An error
2728 * from here on will result in the transaction cancel
2729 * unlocking dp so don't do it explicitly in the error path.
2732 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2733 unlock_dp_on_error = B_FALSE;
2735 XFS_BMAP_INIT(&free_list, &first_block);
2737 error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
2738 &first_block, &free_list, resblks ?
2739 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2741 ASSERT(error != ENOSPC);
2744 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2747 * Bump the in memory version number of the parent directory
2748 * so that other processes accessing it will recognize that
2749 * the directory has changed.
2753 error = xfs_dir_init(tp, cdp, dp);
2758 error = xfs_bumplink(tp, dp);
2768 * Attach the dquots to the new inode and modify the icount incore.
2770 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2773 * If this is a synchronous mount, make sure that the
2774 * mkdir transaction goes to disk before returning to
2777 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2778 xfs_trans_set_sync(tp);
2781 error = xfs_bmap_finish(&tp, &free_list, &committed);
2787 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2788 XFS_QM_DQRELE(mp, udqp);
2789 XFS_QM_DQRELE(mp, gdqp);
2794 /* Fall through to std_return with error = 0 or errno from
2795 * xfs_trans_commit. */
2798 if ((created || (error != 0 && dm_event_sent != 0)) &&
2799 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
2800 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2802 created ? cdp : NULL,
2804 dir_name->name, NULL,
2811 xfs_bmap_cancel(&free_list);
2813 cancel_flags |= XFS_TRANS_ABORT;
2815 xfs_trans_cancel(tp, cancel_flags);
2816 XFS_QM_DQRELE(mp, udqp);
2817 XFS_QM_DQRELE(mp, gdqp);
2819 if (unlock_dp_on_error)
2820 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2828 struct xfs_name *name,
2831 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
2832 xfs_mount_t *mp = dp->i_mount;
2835 xfs_bmap_free_t free_list;
2836 xfs_fsblock_t first_block;
2842 xfs_itrace_entry(dp);
2844 if (XFS_FORCED_SHUTDOWN(mp))
2845 return XFS_ERROR(EIO);
2847 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
2848 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
2850 NULL, DM_RIGHT_NULL, name->name,
2851 NULL, cdp->i_d.di_mode, 0, 0);
2853 return XFS_ERROR(error);
2857 * We need to get a reference to cdp before we get our log
2858 * reservation. The reason for this is that we cannot call
2859 * xfs_iget for an inode for which we do not have a reference
2860 * once we've acquired a log reservation. This is because the
2861 * inode we are trying to get might be in xfs_inactive going
2862 * for a log reservation. Since we'll have to wait for the
2863 * inactive code to complete before returning from xfs_iget,
2864 * we need to make sure that we don't have log space reserved
2865 * when we call xfs_iget. Instead we get an unlocked reference
2866 * to the inode before getting our log reservation.
2871 * Get the dquots for the inodes.
2873 error = XFS_QM_DQATTACH(mp, dp, 0);
2874 if (!error && dp != cdp)
2875 error = XFS_QM_DQATTACH(mp, cdp, 0);
2878 REMOVE_DEBUG_TRACE(__LINE__);
2882 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
2883 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2885 * We try to get the real space reservation first,
2886 * allowing for directory btree deletion(s) implying
2887 * possible bmap insert(s). If we can't get the space
2888 * reservation then we use 0 instead, and avoid the bmap
2889 * btree insert(s) in the directory code by, if the bmap
2890 * insert tries to happen, instead trimming the LAST
2891 * block from the directory.
2893 resblks = XFS_REMOVE_SPACE_RES(mp);
2894 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2895 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2896 if (error == ENOSPC) {
2898 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2899 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2902 ASSERT(error != ENOSPC);
2907 XFS_BMAP_INIT(&free_list, &first_block);
2910 * Now lock the child directory inode and the parent directory
2911 * inode in the proper order. This will take care of validating
2912 * that the directory entry for the child directory inode has
2913 * not changed while we were obtaining a log reservation.
2915 error = xfs_lock_dir_and_entry(dp, cdp);
2917 xfs_trans_cancel(tp, cancel_flags);
2922 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2925 * Only increment the parent directory vnode count if
2926 * we didn't bump it in looking up cdp. The only time
2927 * we don't bump it is when we're looking up ".".
2932 xfs_itrace_ref(cdp);
2933 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
2935 ASSERT(cdp->i_d.di_nlink >= 2);
2936 if (cdp->i_d.di_nlink != 2) {
2937 error = XFS_ERROR(ENOTEMPTY);
2940 if (!xfs_dir_isempty(cdp)) {
2941 error = XFS_ERROR(ENOTEMPTY);
2945 error = xfs_dir_removename(tp, dp, name, cdp->i_ino,
2946 &first_block, &free_list, resblks);
2950 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2953 * Bump the in memory generation count on the parent
2954 * directory so that other can know that it has changed.
2959 * Drop the link from cdp's "..".
2961 error = xfs_droplink(tp, dp);
2967 * Drop the link from dp to cdp.
2969 error = xfs_droplink(tp, cdp);
2975 * Drop the "." link from cdp to self.
2977 error = xfs_droplink(tp, cdp);
2982 /* Determine these before committing transaction */
2983 last_cdp_link = (cdp)->i_d.di_nlink==0;
2986 * Take an extra ref on the child vnode so that it
2987 * does not go to xfs_inactive() from within the commit.
2992 * If this is a synchronous mount, make sure that the
2993 * rmdir transaction goes to disk before returning to
2996 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2997 xfs_trans_set_sync(tp);
3000 error = xfs_bmap_finish (&tp, &free_list, &committed);
3002 xfs_bmap_cancel(&free_list);
3003 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3009 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3018 /* Fall through to std_return with error = 0 or the errno
3019 * from xfs_trans_commit. */
3021 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
3022 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3024 NULL, DM_RIGHT_NULL,
3025 name->name, NULL, cdp->i_d.di_mode,
3031 xfs_bmap_cancel(&free_list);
3032 cancel_flags |= XFS_TRANS_ABORT;
3036 xfs_trans_cancel(tp, cancel_flags);
3043 struct xfs_name *link_name,
3044 const char *target_path,
3049 xfs_mount_t *mp = dp->i_mount;
3054 xfs_bmap_free_t free_list;
3055 xfs_fsblock_t first_block;
3056 boolean_t unlock_dp_on_error = B_FALSE;
3059 xfs_fileoff_t first_fsb;
3060 xfs_filblks_t fs_blocks;
3062 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3064 const char *cur_chunk;
3069 struct xfs_dquot *udqp, *gdqp;
3077 xfs_itrace_entry(dp);
3079 if (XFS_FORCED_SHUTDOWN(mp))
3080 return XFS_ERROR(EIO);
3083 * Check component lengths of the target path name.
3085 pathlen = strlen(target_path);
3086 if (pathlen >= MAXPATHLEN) /* total string too long */
3087 return XFS_ERROR(ENAMETOOLONG);
3089 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
3090 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
3091 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3092 link_name->name, target_path, 0, 0, 0);
3097 /* Return through std_return after this point. */
3100 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3101 prid = dp->i_d.di_projid;
3103 prid = (xfs_prid_t)dfltprid;
3106 * Make sure that we have allocated dquot(s) on disk.
3108 error = XFS_QM_DQVOPALLOC(mp, dp,
3109 current_fsuid(credp), current_fsgid(credp), prid,
3110 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3114 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3115 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3117 * The symlink will fit into the inode data fork?
3118 * There can't be any attributes so we get the whole variable part.
3120 if (pathlen <= XFS_LITINO(mp))
3123 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3124 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
3125 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3126 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3127 if (error == ENOSPC && fs_blocks == 0) {
3129 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3130 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3137 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
3138 unlock_dp_on_error = B_TRUE;
3141 * Check whether the directory allows new symlinks or not.
3143 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3144 error = XFS_ERROR(EPERM);
3149 * Reserve disk quota : blocks and inode.
3151 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3156 * Check for ability to enter directory entry, if no space reserved.
3158 error = xfs_dir_canenter(tp, dp, link_name, resblks);
3162 * Initialize the bmap freelist prior to calling either
3163 * bmapi or the directory create code.
3165 XFS_BMAP_INIT(&free_list, &first_block);
3168 * Allocate an inode for the symlink.
3170 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
3171 1, 0, credp, prid, resblks > 0, &ip, NULL);
3173 if (error == ENOSPC)
3180 * An error after we've joined dp to the transaction will result in the
3181 * transaction cancel unlocking dp so don't do it explicitly in the
3185 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3186 unlock_dp_on_error = B_FALSE;
3189 * Also attach the dquot(s) to it, if applicable.
3191 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3194 resblks -= XFS_IALLOC_SPACE_RES(mp);
3196 * If the symlink will fit into the inode, write it inline.
3198 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3199 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3200 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3201 ip->i_d.di_size = pathlen;
3204 * The inode was initially created in extent format.
3206 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3207 ip->i_df.if_flags |= XFS_IFINLINE;
3209 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3210 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3214 nmaps = SYMLINK_MAPS;
3216 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3217 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3218 &first_block, resblks, mval, &nmaps,
3225 resblks -= fs_blocks;
3226 ip->i_d.di_size = pathlen;
3227 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3229 cur_chunk = target_path;
3230 for (n = 0; n < nmaps; n++) {
3231 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3232 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3233 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3234 BTOBB(byte_cnt), 0);
3235 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3236 if (pathlen < byte_cnt) {
3239 pathlen -= byte_cnt;
3241 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3242 cur_chunk += byte_cnt;
3244 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3249 * Create the directory entry for the symlink.
3251 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
3252 &first_block, &free_list, resblks);
3255 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3256 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3259 * Bump the in memory version number of the parent directory
3260 * so that other processes accessing it will recognize that
3261 * the directory has changed.
3266 * If this is a synchronous mount, make sure that the
3267 * symlink transaction goes to disk before returning to
3270 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3271 xfs_trans_set_sync(tp);
3275 * xfs_trans_commit normally decrements the vnode ref count
3276 * when it unlocks the inode. Since we want to return the
3277 * vnode to the caller, we bump the vnode ref count now.
3281 error = xfs_bmap_finish(&tp, &free_list, &committed);
3285 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3286 XFS_QM_DQRELE(mp, udqp);
3287 XFS_QM_DQRELE(mp, gdqp);
3289 /* Fall through to std_return with error = 0 or errno from
3290 * xfs_trans_commit */
3292 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
3293 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3296 DM_RIGHT_NULL, link_name->name,
3297 target_path, 0, error, 0);
3307 xfs_bmap_cancel(&free_list);
3308 cancel_flags |= XFS_TRANS_ABORT;
3310 xfs_trans_cancel(tp, cancel_flags);
3311 XFS_QM_DQRELE(mp, udqp);
3312 XFS_QM_DQRELE(mp, gdqp);
3314 if (unlock_dp_on_error)
3315 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3325 xfs_mount_t *mp = ip->i_mount;
3328 if (XFS_FORCED_SHUTDOWN(mp))
3329 return XFS_ERROR(EIO);
3332 * Bypass inodes which have already been cleaned by
3333 * the inode flush clustering code inside xfs_iflush
3335 if (xfs_inode_clean(ip))
3339 * We make this non-blocking if the inode is contended,
3340 * return EAGAIN to indicate to the caller that they
3341 * did not succeed. This prevents the flush path from
3342 * blocking on inodes inside another operation right
3343 * now, they get caught later by xfs_sync.
3345 if (flags & FLUSH_SYNC) {
3346 xfs_ilock(ip, XFS_ILOCK_SHARED);
3348 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3349 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3350 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3357 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
3358 : XFS_IFLUSH_ASYNC_NOBLOCK);
3359 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3371 xfs_mount_t *mp = ip->i_mount;
3375 if (!capable(CAP_SYS_ADMIN))
3376 return XFS_ERROR(EPERM);
3378 if (XFS_FORCED_SHUTDOWN(mp))
3379 return XFS_ERROR(EIO);
3381 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3382 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3384 xfs_trans_cancel(tp, 0);
3387 xfs_ilock(ip, XFS_ILOCK_EXCL);
3388 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3390 ip->i_d.di_dmevmask = evmask;
3391 ip->i_d.di_dmstate = state;
3393 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3395 error = xfs_trans_commit(tp, 0);
3404 bhv_vnode_t *vp = XFS_ITOV(ip);
3406 xfs_itrace_entry(ip);
3408 ASSERT(!VN_MAPPED(vp));
3410 /* bad inode, get out here ASAP */
3418 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
3421 * Make sure the atime in the XFS inode is correct before freeing the
3424 xfs_synchronize_atime(ip);
3427 * If we have nothing to flush with this inode then complete the
3428 * teardown now, otherwise break the link between the xfs inode and the
3429 * linux inode and clean up the xfs inode later. This avoids flushing
3430 * the inode to disk during the delete operation itself.
3432 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3433 * first to ensure that xfs_iunpin() will never see an xfs inode
3434 * that has a linux inode being reclaimed. Synchronisation is provided
3435 * by the i_flags_lock.
3437 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3438 xfs_ilock(ip, XFS_ILOCK_EXCL);
3440 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3442 xfs_mount_t *mp = ip->i_mount;
3444 /* Protect sync and unpin from us */
3445 XFS_MOUNT_ILOCK(mp);
3446 spin_lock(&ip->i_flags_lock);
3447 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
3448 vn_to_inode(vp)->i_private = NULL;
3450 spin_unlock(&ip->i_flags_lock);
3451 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
3452 XFS_MOUNT_IUNLOCK(mp);
3463 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
3464 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
3467 if (vp && VN_BAD(vp))
3470 /* The hash lock here protects a thread in xfs_iget_core from
3471 * racing with us on linking the inode back with a vnode.
3472 * Once we have the XFS_IRECLAIM flag set it will not touch
3475 write_lock(&pag->pag_ici_lock);
3476 spin_lock(&ip->i_flags_lock);
3477 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3478 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
3479 spin_unlock(&ip->i_flags_lock);
3480 write_unlock(&pag->pag_ici_lock);
3483 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3487 __xfs_iflags_set(ip, XFS_IRECLAIM);
3488 spin_unlock(&ip->i_flags_lock);
3489 write_unlock(&pag->pag_ici_lock);
3490 xfs_put_perag(ip->i_mount, pag);
3493 * If the inode is still dirty, then flush it out. If the inode
3494 * is not in the AIL, then it will be OK to flush it delwri as
3495 * long as xfs_iflush() does not keep any references to the inode.
3496 * We leave that decision up to xfs_iflush() since it has the
3497 * knowledge of whether it's OK to simply do a delwri flush of
3498 * the inode or whether we need to wait until the inode is
3499 * pulled from the AIL.
3500 * We get the flush lock regardless, though, just to make sure
3501 * we don't free it while it is being flushed.
3504 xfs_ilock(ip, XFS_ILOCK_EXCL);
3508 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3509 if (ip->i_update_core ||
3510 ((ip->i_itemp != NULL) &&
3511 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3512 error = xfs_iflush(ip, sync_mode);
3514 * If we hit an error, typically because of filesystem
3515 * shutdown, we don't need to let vn_reclaim to know
3516 * because we're gonna reclaim the inode anyway.
3519 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3522 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3525 ASSERT(ip->i_update_core == 0);
3526 ASSERT(ip->i_itemp == NULL ||
3527 ip->i_itemp->ili_format.ilf_fields == 0);
3531 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3539 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3542 xfs_inode_t *ip, *n;
3547 XFS_MOUNT_ILOCK(mp);
3548 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3550 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3552 if (xfs_ipincount(ip) ||
3553 !xfs_iflock_nowait(ip)) {
3554 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3558 XFS_MOUNT_IUNLOCK(mp);
3559 if (xfs_finish_reclaim(ip, noblock,
3560 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3569 XFS_MOUNT_IUNLOCK(mp);
3574 * xfs_alloc_file_space()
3575 * This routine allocates disk space for the given file.
3577 * If alloc_type == 0, this request is for an ALLOCSP type
3578 * request which will change the file size. In this case, no
3579 * DMAPI event will be generated by the call. A TRUNCATE event
3580 * will be generated later by xfs_setattr.
3582 * If alloc_type != 0, this request is for a RESVSP type
3583 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3584 * lower block boundary byte address is less than the file's
3593 xfs_alloc_file_space(
3600 xfs_mount_t *mp = ip->i_mount;
3602 xfs_filblks_t allocated_fsb;
3603 xfs_filblks_t allocatesize_fsb;
3604 xfs_extlen_t extsz, temp;
3605 xfs_fileoff_t startoffset_fsb;
3606 xfs_fsblock_t firstfsb;
3612 xfs_bmbt_irec_t imaps[1], *imapp;
3613 xfs_bmap_free_t free_list;
3614 uint qblocks, resblks, resrtextents;
3618 xfs_itrace_entry(ip);
3620 if (XFS_FORCED_SHUTDOWN(mp))
3621 return XFS_ERROR(EIO);
3623 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3627 return XFS_ERROR(EINVAL);
3629 rt = XFS_IS_REALTIME_INODE(ip);
3630 extsz = xfs_get_extsz_hint(ip);
3635 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
3636 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3637 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3639 /* Generate a DMAPI event if needed. */
3640 if (alloc_type != 0 && offset < ip->i_size &&
3641 (attr_flags&ATTR_DMI) == 0 &&
3642 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
3643 xfs_off_t end_dmi_offset;
3645 end_dmi_offset = offset+len;
3646 if (end_dmi_offset > ip->i_size)
3647 end_dmi_offset = ip->i_size;
3648 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
3649 end_dmi_offset - offset, 0, NULL);
3655 * Allocate file space until done or until there is an error
3658 while (allocatesize_fsb && !error) {
3662 * Determine space reservations for data/realtime.
3664 if (unlikely(extsz)) {
3665 s = startoffset_fsb;
3668 e = startoffset_fsb + allocatesize_fsb;
3669 if ((temp = do_mod(startoffset_fsb, extsz)))
3671 if ((temp = do_mod(e, extsz)))
3675 e = allocatesize_fsb;
3679 resrtextents = qblocks = (uint)(e - s);
3680 resrtextents /= mp->m_sb.sb_rextsize;
3681 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3682 quota_flag = XFS_QMOPT_RES_RTBLKS;
3685 resblks = qblocks = \
3686 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3687 quota_flag = XFS_QMOPT_RES_REGBLKS;
3691 * Allocate and setup the transaction.
3693 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
3694 error = xfs_trans_reserve(tp, resblks,
3695 XFS_WRITE_LOG_RES(mp), resrtextents,
3696 XFS_TRANS_PERM_LOG_RES,
3697 XFS_WRITE_LOG_COUNT);
3699 * Check for running out of space
3703 * Free the transaction structure.
3705 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3706 xfs_trans_cancel(tp, 0);
3709 xfs_ilock(ip, XFS_ILOCK_EXCL);
3710 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3711 qblocks, 0, quota_flag);
3715 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3716 xfs_trans_ihold(tp, ip);
3719 * Issue the xfs_bmapi() call to allocate the blocks
3721 XFS_BMAP_INIT(&free_list, &firstfsb);
3722 error = xfs_bmapi(tp, ip, startoffset_fsb,
3723 allocatesize_fsb, bmapi_flag,
3724 &firstfsb, 0, imapp, &nimaps,
3731 * Complete the transaction
3733 error = xfs_bmap_finish(&tp, &free_list, &committed);
3738 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3739 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3744 allocated_fsb = imapp->br_blockcount;
3747 error = XFS_ERROR(ENOSPC);
3751 startoffset_fsb += allocated_fsb;
3752 allocatesize_fsb -= allocated_fsb;
3755 if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
3756 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
3757 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
3760 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3762 goto retry; /* Maybe DMAPI app. has made space */
3763 /* else fall through with error from XFS_SEND_DATA */
3768 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
3769 xfs_bmap_cancel(&free_list);
3770 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3772 error1: /* Just cancel transaction */
3773 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3774 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3775 goto dmapi_enospc_check;
3779 * Zero file bytes between startoff and endoff inclusive.
3780 * The iolock is held exclusive and no blocks are buffered.
3783 xfs_zero_remaining_bytes(
3788 xfs_bmbt_irec_t imap;
3789 xfs_fileoff_t offset_fsb;
3790 xfs_off_t lastoffset;
3793 xfs_mount_t *mp = ip->i_mount;
3797 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
3798 XFS_IS_REALTIME_INODE(ip) ?
3799 mp->m_rtdev_targp : mp->m_ddev_targp);
3801 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3802 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3804 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
3805 NULL, 0, &imap, &nimap, NULL, NULL);
3806 if (error || nimap < 1)
3808 ASSERT(imap.br_blockcount >= 1);
3809 ASSERT(imap.br_startoff == offset_fsb);
3810 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3811 if (lastoffset > endoff)
3812 lastoffset = endoff;
3813 if (imap.br_startblock == HOLESTARTBLOCK)
3815 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3816 if (imap.br_state == XFS_EXT_UNWRITTEN)
3819 XFS_BUF_UNWRITE(bp);
3821 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3823 error = xfs_iowait(bp);
3825 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3826 mp, bp, XFS_BUF_ADDR(bp));
3829 memset(XFS_BUF_PTR(bp) +
3830 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3831 0, lastoffset - offset + 1);
3836 error = xfs_iowait(bp);
3838 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3839 mp, bp, XFS_BUF_ADDR(bp));
3848 * xfs_free_file_space()
3849 * This routine frees disk space for the given file.
3851 * This routine is only called by xfs_change_file_space
3852 * for an UNRESVSP type call.
3860 xfs_free_file_space(
3869 xfs_off_t end_dmi_offset;
3870 xfs_fileoff_t endoffset_fsb;
3872 xfs_fsblock_t firstfsb;
3873 xfs_bmap_free_t free_list;
3874 xfs_bmbt_irec_t imap;
3882 xfs_fileoff_t startoffset_fsb;
3884 int need_iolock = 1;
3889 xfs_itrace_entry(ip);
3891 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3895 if (len <= 0) /* if nothing being freed */
3897 rt = XFS_IS_REALTIME_INODE(ip);
3898 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3899 end_dmi_offset = offset + len;
3900 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3902 if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
3903 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
3904 if (end_dmi_offset > ip->i_size)
3905 end_dmi_offset = ip->i_size;
3906 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
3907 offset, end_dmi_offset - offset,
3908 AT_DELAY_FLAG(attr_flags), NULL);
3913 if (attr_flags & ATTR_NOLOCK)
3916 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3917 vn_iowait(ip); /* wait for the completion of any pending DIOs */
3920 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
3921 ioffset = offset & ~(rounding - 1);
3923 if (VN_CACHED(vp) != 0) {
3924 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3925 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
3927 goto out_unlock_iolock;
3931 * Need to zero the stuff we're not freeing, on disk.
3932 * If its a realtime file & can't use unwritten extents then we
3933 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3934 * will take care of it for us.
3936 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
3938 error = xfs_bmapi(NULL, ip, startoffset_fsb,
3939 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
3941 goto out_unlock_iolock;
3942 ASSERT(nimap == 0 || nimap == 1);
3943 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3946 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3947 block = imap.br_startblock;
3948 mod = do_div(block, mp->m_sb.sb_rextsize);
3950 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
3953 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
3954 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
3956 goto out_unlock_iolock;
3957 ASSERT(nimap == 0 || nimap == 1);
3958 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3959 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3961 if (mod && (mod != mp->m_sb.sb_rextsize))
3962 endoffset_fsb -= mod;
3965 if ((done = (endoffset_fsb <= startoffset_fsb)))
3967 * One contiguous piece to clear
3969 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
3972 * Some full blocks, possibly two pieces to clear
3974 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
3975 error = xfs_zero_remaining_bytes(ip, offset,
3976 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
3978 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
3979 error = xfs_zero_remaining_bytes(ip,
3980 XFS_FSB_TO_B(mp, endoffset_fsb),
3985 * free file space until done or until there is an error
3987 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3988 while (!error && !done) {
3991 * allocate and setup the transaction. Allow this
3992 * transaction to dip into the reserve blocks to ensure
3993 * the freeing of the space succeeds at ENOSPC.
3995 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
3996 tp->t_flags |= XFS_TRANS_RESERVE;
3997 error = xfs_trans_reserve(tp,
3999 XFS_WRITE_LOG_RES(mp),
4001 XFS_TRANS_PERM_LOG_RES,
4002 XFS_WRITE_LOG_COUNT);
4005 * check for running out of space
4009 * Free the transaction structure.
4011 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4012 xfs_trans_cancel(tp, 0);
4015 xfs_ilock(ip, XFS_ILOCK_EXCL);
4016 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4017 ip->i_udquot, ip->i_gdquot, resblks, 0,
4018 XFS_QMOPT_RES_REGBLKS);
4022 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4023 xfs_trans_ihold(tp, ip);
4026 * issue the bunmapi() call to free the blocks
4028 XFS_BMAP_INIT(&free_list, &firstfsb);
4029 error = xfs_bunmapi(tp, ip, startoffset_fsb,
4030 endoffset_fsb - startoffset_fsb,
4031 0, 2, &firstfsb, &free_list, NULL, &done);
4037 * complete the transaction
4039 error = xfs_bmap_finish(&tp, &free_list, &committed);
4044 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
4045 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4050 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4054 xfs_bmap_cancel(&free_list);
4056 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4057 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4063 * xfs_change_file_space()
4064 * This routine allocates or frees disk space for the given file.
4065 * The user specified parameters are checked for alignment and size
4074 xfs_change_file_space(
4082 xfs_mount_t *mp = ip->i_mount;
4087 xfs_off_t startoffset;
4092 xfs_itrace_entry(ip);
4094 if (!S_ISREG(ip->i_d.di_mode))
4095 return XFS_ERROR(EINVAL);
4097 switch (bf->l_whence) {
4098 case 0: /*SEEK_SET*/
4100 case 1: /*SEEK_CUR*/
4101 bf->l_start += offset;
4103 case 2: /*SEEK_END*/
4104 bf->l_start += ip->i_size;
4107 return XFS_ERROR(EINVAL);
4110 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4112 if ( (bf->l_start < 0)
4113 || (bf->l_start > XFS_MAXIOFFSET(mp))
4114 || (bf->l_start + llen < 0)
4115 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4116 return XFS_ERROR(EINVAL);
4120 startoffset = bf->l_start;
4124 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4126 * These calls do NOT zero the data space allocated to the file,
4127 * nor do they change the file size.
4129 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4131 * These calls cause the new file data to be zeroed and the file
4132 * size to be changed.
4134 setprealloc = clrprealloc = 0;
4137 case XFS_IOC_RESVSP:
4138 case XFS_IOC_RESVSP64:
4139 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4146 case XFS_IOC_UNRESVSP:
4147 case XFS_IOC_UNRESVSP64:
4148 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4153 case XFS_IOC_ALLOCSP:
4154 case XFS_IOC_ALLOCSP64:
4155 case XFS_IOC_FREESP:
4156 case XFS_IOC_FREESP64:
4157 if (startoffset > fsize) {
4158 error = xfs_alloc_file_space(ip, fsize,
4159 startoffset - fsize, 0, attr_flags);
4164 va.va_mask = XFS_AT_SIZE;
4165 va.va_size = startoffset;
4167 error = xfs_setattr(ip, &va, attr_flags, credp);
4177 return XFS_ERROR(EINVAL);
4181 * update the inode timestamp, mode, and prealloc flag bits
4183 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4185 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4188 xfs_trans_cancel(tp, 0);
4192 xfs_ilock(ip, XFS_ILOCK_EXCL);
4194 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4195 xfs_trans_ihold(tp, ip);
4197 if ((attr_flags & ATTR_DMI) == 0) {
4198 ip->i_d.di_mode &= ~S_ISUID;
4201 * Note that we don't have to worry about mandatory
4202 * file locking being disabled here because we only
4203 * clear the S_ISGID bit if the Group execute bit is
4204 * on, but if it was on then mandatory locking wouldn't
4205 * have been enabled.
4207 if (ip->i_d.di_mode & S_IXGRP)
4208 ip->i_d.di_mode &= ~S_ISGID;
4210 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4213 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4214 else if (clrprealloc)
4215 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4217 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4218 xfs_trans_set_sync(tp);
4220 error = xfs_trans_commit(tp, 0);
4222 xfs_iunlock(ip, XFS_ILOCK_EXCL);