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 bhv_vnode_t *vp = XFS_ITOV(ip);
215 xfs_mount_t *mp = ip->i_mount;
224 xfs_prid_t projid=0, iprojid=0;
225 int mandlock_before, mandlock_after;
226 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
230 xfs_itrace_entry(ip);
232 if (mp->m_flags & XFS_MOUNT_RDONLY)
233 return XFS_ERROR(EROFS);
236 * Cannot set certain attributes.
239 if (mask & XFS_AT_NOSET) {
240 return XFS_ERROR(EINVAL);
243 if (XFS_FORCED_SHUTDOWN(mp))
244 return XFS_ERROR(EIO);
247 * Timestamps do not need to be logged and hence do not
248 * need to be done within a transaction.
250 if (mask & XFS_AT_UPDTIMES) {
251 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
252 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
253 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
254 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
255 xfs_ichgtime(ip, timeflags);
259 olddquot1 = olddquot2 = NULL;
263 * If disk quotas is on, we make sure that the dquots do exist on disk,
264 * before we start any other transactions. Trying to do this later
265 * is messy. We don't care to take a readlock to look at the ids
266 * in inode here, because we can't hold it across the trans_reserve.
267 * If the IDs do change before we take the ilock, we're covered
268 * because the i_*dquot fields will get updated anyway.
270 if (XFS_IS_QUOTA_ON(mp) &&
271 (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
274 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
276 qflags |= XFS_QMOPT_UQUOTA;
278 uid = ip->i_d.di_uid;
280 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
282 qflags |= XFS_QMOPT_GQUOTA;
284 gid = ip->i_d.di_gid;
286 if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
287 projid = vap->va_projid;
288 qflags |= XFS_QMOPT_PQUOTA;
290 projid = ip->i_d.di_projid;
293 * We take a reference when we initialize udqp and gdqp,
294 * so it is important that we never blindly double trip on
295 * the same variable. See xfs_create() for an example.
297 ASSERT(udqp == NULL);
298 ASSERT(gdqp == NULL);
299 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
306 * For the other attributes, we acquire the inode lock and
307 * first do an error checking pass.
310 lock_flags = XFS_ILOCK_EXCL;
311 if (flags & ATTR_NOLOCK)
313 if (!(mask & XFS_AT_SIZE)) {
314 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
315 (mp->m_flags & XFS_MOUNT_WSYNC)) {
316 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
318 if ((code = xfs_trans_reserve(tp, 0,
319 XFS_ICHANGE_LOG_RES(mp), 0,
326 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
327 !(flags & ATTR_DMI)) {
328 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
329 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, ip,
330 vap->va_size, 0, dmflags, NULL);
337 lock_flags |= XFS_IOLOCK_EXCL;
340 xfs_ilock(ip, lock_flags);
342 /* boolean: are we the file owner? */
343 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
346 * Change various properties of a file.
347 * Only the owner or users with CAP_FOWNER
348 * capability may do these things.
351 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
352 XFS_AT_GID|XFS_AT_PROJID)) {
354 * CAP_FOWNER overrides the following restrictions:
356 * The user ID of the calling process must be equal
357 * to the file owner ID, except in cases where the
358 * CAP_FSETID capability is applicable.
360 if (!file_owner && !capable(CAP_FOWNER)) {
361 code = XFS_ERROR(EPERM);
366 * CAP_FSETID overrides the following restrictions:
368 * The effective user ID of the calling process shall match
369 * the file owner when setting the set-user-ID and
370 * set-group-ID bits on that file.
372 * The effective group ID or one of the supplementary group
373 * IDs of the calling process shall match the group owner of
374 * the file when setting the set-group-ID bit on that file
376 if (mask & XFS_AT_MODE) {
379 if ((vap->va_mode & S_ISUID) && !file_owner)
381 if ((vap->va_mode & S_ISGID) &&
382 !in_group_p((gid_t)ip->i_d.di_gid))
385 /* Linux allows this, Irix doesn't. */
386 if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
389 if (m && !capable(CAP_FSETID))
395 * Change file ownership. Must be the owner or privileged.
396 * If the system was configured with the "restricted_chown"
397 * option, the owner is not permitted to give away the file,
398 * and can change the group id only to a group of which he
399 * or she is a member.
401 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
403 * These IDs could have changed since we last looked at them.
404 * But, we're assured that if the ownership did change
405 * while we didn't have the inode locked, inode's dquot(s)
406 * would have changed also.
408 iuid = ip->i_d.di_uid;
409 iprojid = ip->i_d.di_projid;
410 igid = ip->i_d.di_gid;
411 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
412 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
413 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
417 * CAP_CHOWN overrides the following restrictions:
419 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
420 * shall override the restriction that a process cannot
421 * change the user ID of a file it owns and the restriction
422 * that the group ID supplied to the chown() function
423 * shall be equal to either the group ID or one of the
424 * supplementary group IDs of the calling process.
426 if (restricted_chown &&
427 (iuid != uid || (igid != gid &&
428 !in_group_p((gid_t)gid))) &&
429 !capable(CAP_CHOWN)) {
430 code = XFS_ERROR(EPERM);
434 * Do a quota reservation only if uid/projid/gid is actually
437 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
438 (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
439 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
441 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
442 capable(CAP_FOWNER) ?
443 XFS_QMOPT_FORCE_RES : 0);
444 if (code) /* out of quota */
450 * Truncate file. Must have write permission and not be a directory.
452 if (mask & XFS_AT_SIZE) {
453 /* Short circuit the truncate case for zero length files */
454 if ((vap->va_size == 0) &&
455 (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) {
456 xfs_iunlock(ip, XFS_ILOCK_EXCL);
457 lock_flags &= ~XFS_ILOCK_EXCL;
458 if (mask & XFS_AT_CTIME)
459 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
465 code = XFS_ERROR(EISDIR);
467 } else if (!VN_ISREG(vp)) {
468 code = XFS_ERROR(EINVAL);
472 * Make sure that the dquots are attached to the inode.
474 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
479 * Change file access or modified times.
481 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
483 if ((flags & ATTR_UTIME) &&
484 !capable(CAP_FOWNER)) {
485 code = XFS_ERROR(EPERM);
492 * Change extent size or realtime flag.
494 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
496 * Can't change extent size if any extents are allocated.
498 if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
499 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
501 code = XFS_ERROR(EINVAL); /* EFBIG? */
506 * Can't change realtime flag if any extents are allocated.
508 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
509 (mask & XFS_AT_XFLAGS) &&
510 (XFS_IS_REALTIME_INODE(ip)) !=
511 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
512 code = XFS_ERROR(EINVAL); /* EFBIG? */
516 * Extent size must be a multiple of the appropriate block
517 * size, if set at all.
519 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
522 if (XFS_IS_REALTIME_INODE(ip) ||
523 ((mask & XFS_AT_XFLAGS) &&
524 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
525 size = mp->m_sb.sb_rextsize <<
526 mp->m_sb.sb_blocklog;
528 size = mp->m_sb.sb_blocksize;
530 if (vap->va_extsize % size) {
531 code = XFS_ERROR(EINVAL);
536 * If realtime flag is set then must have realtime data.
538 if ((mask & XFS_AT_XFLAGS) &&
539 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
540 if ((mp->m_sb.sb_rblocks == 0) ||
541 (mp->m_sb.sb_rextsize == 0) ||
542 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
543 code = XFS_ERROR(EINVAL);
549 * Can't modify an immutable/append-only file unless
550 * we have appropriate permission.
552 if ((mask & XFS_AT_XFLAGS) &&
554 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
556 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
557 !capable(CAP_LINUX_IMMUTABLE)) {
558 code = XFS_ERROR(EPERM);
564 * Now we can make the changes. Before we join the inode
565 * to the transaction, if XFS_AT_SIZE is set then take care of
566 * the part of the truncation that must be done without the
567 * inode lock. This needs to be done before joining the inode
568 * to the transaction, because the inode cannot be unlocked
569 * once it is a part of the transaction.
571 if (mask & XFS_AT_SIZE) {
573 if ((vap->va_size > ip->i_size) &&
574 (flags & ATTR_NOSIZETOK) == 0) {
575 code = xfs_igrow_start(ip, vap->va_size, credp);
577 xfs_iunlock(ip, XFS_ILOCK_EXCL);
580 * We are going to log the inode size change in this
581 * transaction so any previous writes that are beyond the on
582 * disk EOF and the new EOF that have not been written out need
583 * to be written here. If we do not write the data out, we
584 * expose ourselves to the null files problem.
586 * Only flush from the on disk size to the smaller of the in
587 * memory file size or the new size as that's the range we
588 * really care about here and prevents waiting for other data
589 * not within the range we care about here.
592 (ip->i_size != ip->i_d.di_size) &&
593 (vap->va_size > ip->i_d.di_size)) {
594 code = xfs_flush_pages(ip,
595 ip->i_d.di_size, vap->va_size,
596 XFS_B_ASYNC, FI_NONE);
599 /* wait for all I/O to complete */
603 code = xfs_itruncate_data(ip, vap->va_size);
606 lock_flags &= ~XFS_ILOCK_EXCL;
607 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
610 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
611 if ((code = xfs_trans_reserve(tp, 0,
612 XFS_ITRUNCATE_LOG_RES(mp), 0,
613 XFS_TRANS_PERM_LOG_RES,
614 XFS_ITRUNCATE_LOG_COUNT))) {
615 xfs_trans_cancel(tp, 0);
617 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
620 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
621 xfs_ilock(ip, XFS_ILOCK_EXCL);
625 xfs_trans_ijoin(tp, ip, lock_flags);
626 xfs_trans_ihold(tp, ip);
629 /* determine whether mandatory locking mode changes */
630 mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
633 * Truncate file. Must have write permission and not be a directory.
635 if (mask & XFS_AT_SIZE) {
637 * Only change the c/mtime if we are changing the size
638 * or we are explicitly asked to change it. This handles
639 * the semantic difference between truncate() and ftruncate()
640 * as implemented in the VFS.
642 if (vap->va_size != ip->i_size || (mask & XFS_AT_CTIME))
643 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
645 if (vap->va_size > ip->i_size) {
646 xfs_igrow_finish(tp, ip, vap->va_size,
647 !(flags & ATTR_DMI));
648 } else if ((vap->va_size <= ip->i_size) ||
649 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
651 * signal a sync transaction unless
652 * we're truncating an already unlinked
653 * file on a wsync filesystem
655 code = xfs_itruncate_finish(&tp, ip,
656 (xfs_fsize_t)vap->va_size,
658 ((ip->i_d.di_nlink != 0 ||
659 !(mp->m_flags & XFS_MOUNT_WSYNC))
664 * Truncated "down", so we're removing references
665 * to old data here - if we now delay flushing for
666 * a long time, we expose ourselves unduly to the
667 * notorious NULL files problem. So, we mark this
668 * vnode and flush it when the file is closed, and
669 * do not wait the usual (long) time for writeout.
671 xfs_iflags_set(ip, XFS_ITRUNCATED);
676 * Change file access modes.
678 if (mask & XFS_AT_MODE) {
679 ip->i_d.di_mode &= S_IFMT;
680 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
682 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
683 timeflags |= XFS_ICHGTIME_CHG;
687 * Change file ownership. Must be the owner or privileged.
688 * If the system was configured with the "restricted_chown"
689 * option, the owner is not permitted to give away the file,
690 * and can change the group id only to a group of which he
691 * or she is a member.
693 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
695 * CAP_FSETID overrides the following restrictions:
697 * The set-user-ID and set-group-ID bits of a file will be
698 * cleared upon successful return from chown()
700 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
701 !capable(CAP_FSETID)) {
702 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
706 * Change the ownerships and register quota modifications
707 * in the transaction.
710 if (XFS_IS_UQUOTA_ON(mp)) {
711 ASSERT(mask & XFS_AT_UID);
713 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
714 &ip->i_udquot, udqp);
716 ip->i_d.di_uid = uid;
719 if (XFS_IS_GQUOTA_ON(mp)) {
720 ASSERT(!XFS_IS_PQUOTA_ON(mp));
721 ASSERT(mask & XFS_AT_GID);
723 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
724 &ip->i_gdquot, gdqp);
726 ip->i_d.di_gid = gid;
728 if (iprojid != projid) {
729 if (XFS_IS_PQUOTA_ON(mp)) {
730 ASSERT(!XFS_IS_GQUOTA_ON(mp));
731 ASSERT(mask & XFS_AT_PROJID);
733 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
734 &ip->i_gdquot, gdqp);
736 ip->i_d.di_projid = projid;
738 * We may have to rev the inode as well as
739 * the superblock version number since projids didn't
740 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
742 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
743 xfs_bump_ino_vers2(tp, ip);
746 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
747 timeflags |= XFS_ICHGTIME_CHG;
752 * Change file access or modified times.
754 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
755 if (mask & XFS_AT_ATIME) {
756 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
757 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
758 ip->i_update_core = 1;
759 timeflags &= ~XFS_ICHGTIME_ACC;
761 if (mask & XFS_AT_MTIME) {
762 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
763 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
764 timeflags &= ~XFS_ICHGTIME_MOD;
765 timeflags |= XFS_ICHGTIME_CHG;
767 if (tp && (flags & ATTR_UTIME))
768 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
772 * Change XFS-added attributes.
774 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
775 if (mask & XFS_AT_EXTSIZE) {
777 * Converting bytes to fs blocks.
779 ip->i_d.di_extsize = vap->va_extsize >>
780 mp->m_sb.sb_blocklog;
782 if (mask & XFS_AT_XFLAGS) {
785 /* can't set PREALLOC this way, just preserve it */
786 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
787 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
788 di_flags |= XFS_DIFLAG_IMMUTABLE;
789 if (vap->va_xflags & XFS_XFLAG_APPEND)
790 di_flags |= XFS_DIFLAG_APPEND;
791 if (vap->va_xflags & XFS_XFLAG_SYNC)
792 di_flags |= XFS_DIFLAG_SYNC;
793 if (vap->va_xflags & XFS_XFLAG_NOATIME)
794 di_flags |= XFS_DIFLAG_NOATIME;
795 if (vap->va_xflags & XFS_XFLAG_NODUMP)
796 di_flags |= XFS_DIFLAG_NODUMP;
797 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
798 di_flags |= XFS_DIFLAG_PROJINHERIT;
799 if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
800 di_flags |= XFS_DIFLAG_NODEFRAG;
801 if (vap->va_xflags & XFS_XFLAG_FILESTREAM)
802 di_flags |= XFS_DIFLAG_FILESTREAM;
803 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
804 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
805 di_flags |= XFS_DIFLAG_RTINHERIT;
806 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
807 di_flags |= XFS_DIFLAG_NOSYMLINKS;
808 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
809 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
810 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
811 if (vap->va_xflags & XFS_XFLAG_REALTIME)
812 di_flags |= XFS_DIFLAG_REALTIME;
813 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
814 di_flags |= XFS_DIFLAG_EXTSIZE;
816 ip->i_d.di_flags = di_flags;
818 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
819 timeflags |= XFS_ICHGTIME_CHG;
823 * Change file inode change time only if XFS_AT_CTIME set
824 * AND we have been called by a DMI function.
827 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
828 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
829 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
830 ip->i_update_core = 1;
831 timeflags &= ~XFS_ICHGTIME_CHG;
835 * Send out timestamp changes that need to be set to the
836 * current time. Not done when called by a DMI function.
838 if (timeflags && !(flags & ATTR_DMI))
839 xfs_ichgtime(ip, timeflags);
841 XFS_STATS_INC(xs_ig_attrchg);
844 * If this is a synchronous mount, make sure that the
845 * transaction goes to disk before returning to the user.
846 * This is slightly sub-optimal in that truncates require
847 * two sync transactions instead of one for wsync filesystems.
848 * One for the truncate and one for the timestamps since we
849 * don't want to change the timestamps unless we're sure the
850 * truncate worked. Truncates are less than 1% of the laddis
851 * mix so this probably isn't worth the trouble to optimize.
855 if (mp->m_flags & XFS_MOUNT_WSYNC)
856 xfs_trans_set_sync(tp);
858 code = xfs_trans_commit(tp, commit_flags);
862 * If the (regular) file's mandatory locking mode changed, then
863 * notify the vnode. We do this under the inode lock to prevent
864 * racing calls to vop_vnode_change.
866 mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
868 xfs_iunlock(ip, lock_flags);
871 * Release any dquot(s) the inode had kept before chown.
873 XFS_QM_DQRELE(mp, olddquot1);
874 XFS_QM_DQRELE(mp, olddquot2);
875 XFS_QM_DQRELE(mp, udqp);
876 XFS_QM_DQRELE(mp, gdqp);
882 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
883 !(flags & ATTR_DMI)) {
884 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
885 NULL, DM_RIGHT_NULL, NULL, NULL,
886 0, 0, AT_DELAY_FLAG(flags));
891 commit_flags |= XFS_TRANS_ABORT;
894 XFS_QM_DQRELE(mp, udqp);
895 XFS_QM_DQRELE(mp, gdqp);
897 xfs_trans_cancel(tp, commit_flags);
899 if (lock_flags != 0) {
900 xfs_iunlock(ip, lock_flags);
906 * The maximum pathlen is 1024 bytes. Since the minimum file system
907 * blocksize is 512 bytes, we can get a max of 2 extents back from
910 #define SYMLINK_MAPS 2
917 xfs_mount_t *mp = ip->i_mount;
918 int pathlen = ip->i_d.di_size;
919 int nmaps = SYMLINK_MAPS;
920 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
927 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen), 0, NULL, 0,
928 mval, &nmaps, NULL, NULL);
932 for (n = 0; n < nmaps; n++) {
933 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
934 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
936 bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0);
937 error = XFS_BUF_GETERROR(bp);
939 xfs_ioerror_alert("xfs_readlink",
940 ip->i_mount, bp, XFS_BUF_ADDR(bp));
944 if (pathlen < byte_cnt)
948 memcpy(link, XFS_BUF_PTR(bp), byte_cnt);
952 link[ip->i_d.di_size] = '\0';
964 xfs_mount_t *mp = ip->i_mount;
968 xfs_itrace_entry(ip);
970 if (XFS_FORCED_SHUTDOWN(mp))
971 return XFS_ERROR(EIO);
973 xfs_ilock(ip, XFS_ILOCK_SHARED);
975 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
976 ASSERT(ip->i_d.di_size <= MAXPATHLEN);
978 pathlen = ip->i_d.di_size;
982 if (ip->i_df.if_flags & XFS_IFINLINE) {
983 memcpy(link, ip->i_df.if_u1.if_data, pathlen);
984 link[pathlen] = '\0';
986 error = xfs_readlink_bmap(ip, link);
990 xfs_iunlock(ip, XFS_ILOCK_SHARED);
997 * This is called to sync the inode and its data out to disk.
998 * We need to hold the I/O lock while flushing the data, and
999 * the inode lock while flushing the inode. The inode lock CANNOT
1000 * be held while flushing the data, so acquire after we're done
1012 int log_flushed = 0, changed = 1;
1014 xfs_itrace_entry(ip);
1016 ASSERT(start >= 0 && stop >= -1);
1018 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1019 return XFS_ERROR(EIO);
1021 if (flag & FSYNC_DATA)
1022 filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
1025 * We always need to make sure that the required inode state
1026 * is safe on disk. The vnode might be clean but because
1027 * of committed transactions that haven't hit the disk yet.
1028 * Likewise, there could be unflushed non-transactional
1029 * changes to the inode core that have to go to disk.
1031 * The following code depends on one assumption: that
1032 * any transaction that changes an inode logs the core
1033 * because it has to change some field in the inode core
1034 * (typically nextents or nblocks). That assumption
1035 * implies that any transactions against an inode will
1036 * catch any non-transactional updates. If inode-altering
1037 * transactions exist that violate this assumption, the
1038 * code breaks. Right now, it figures that if the involved
1039 * update_* field is clear and the inode is unpinned, the
1040 * inode is clean. Either it's been flushed or it's been
1041 * committed and the commit has hit the disk unpinning the inode.
1042 * (Note that xfs_inode_item_format() called at commit clears
1043 * the update_* fields.)
1045 xfs_ilock(ip, XFS_ILOCK_SHARED);
1047 /* If we are flushing data then we care about update_size
1048 * being set, otherwise we care about update_core
1050 if ((flag & FSYNC_DATA) ?
1051 (ip->i_update_size == 0) :
1052 (ip->i_update_core == 0)) {
1054 * Timestamps/size haven't changed since last inode
1055 * flush or inode transaction commit. That means
1056 * either nothing got written or a transaction
1057 * committed which caught the updates. If the
1058 * latter happened and the transaction hasn't
1059 * hit the disk yet, the inode will be still
1060 * be pinned. If it is, force the log.
1063 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1065 if (xfs_ipincount(ip)) {
1066 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
1068 ((flag & FSYNC_WAIT)
1069 ? XFS_LOG_SYNC : 0),
1073 * If the inode is not pinned and nothing
1074 * has changed we don't need to flush the
1082 * Kick off a transaction to log the inode
1083 * core to get the updates. Make it
1084 * sync if FSYNC_WAIT is passed in (which
1085 * is done by everybody but specfs). The
1086 * sync transaction will also force the log.
1088 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1089 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1090 if ((error = xfs_trans_reserve(tp, 0,
1091 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1093 xfs_trans_cancel(tp, 0);
1096 xfs_ilock(ip, XFS_ILOCK_EXCL);
1099 * Note - it's possible that we might have pushed
1100 * ourselves out of the way during trans_reserve
1101 * which would flush the inode. But there's no
1102 * guarantee that the inode buffer has actually
1103 * gone out yet (it's delwri). Plus the buffer
1104 * could be pinned anyway if it's part of an
1105 * inode in another recent transaction. So we
1106 * play it safe and fire off the transaction anyway.
1108 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1109 xfs_trans_ihold(tp, ip);
1110 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1111 if (flag & FSYNC_WAIT)
1112 xfs_trans_set_sync(tp);
1113 error = _xfs_trans_commit(tp, 0, &log_flushed);
1115 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1118 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1120 * If the log write didn't issue an ordered tag we need
1121 * to flush the disk cache for the data device now.
1124 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1127 * If this inode is on the RT dev we need to flush that
1130 if (XFS_IS_REALTIME_INODE(ip))
1131 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1138 * This is called by xfs_inactive to free any blocks beyond eof
1139 * when the link count isn't zero and by xfs_dm_punch_hole() when
1140 * punching a hole to EOF.
1150 xfs_fileoff_t end_fsb;
1151 xfs_fileoff_t last_fsb;
1152 xfs_filblks_t map_len;
1154 xfs_bmbt_irec_t imap;
1155 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
1158 * Figure out if there are any blocks beyond the end
1159 * of the file. If not, then there is nothing to do.
1161 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
1162 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1163 map_len = last_fsb - end_fsb;
1168 xfs_ilock(ip, XFS_ILOCK_SHARED);
1169 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
1170 NULL, 0, &imap, &nimaps, NULL, NULL);
1171 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1173 if (!error && (nimaps != 0) &&
1174 (imap.br_startblock != HOLESTARTBLOCK ||
1175 ip->i_delayed_blks)) {
1177 * Attach the dquots to the inode up front.
1179 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1183 * There are blocks after the end of file.
1184 * Free them up now by truncating the file to
1187 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1190 * Do the xfs_itruncate_start() call before
1191 * reserving any log space because
1192 * itruncate_start will call into the buffer
1193 * cache and we can't
1194 * do that within a transaction.
1197 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1198 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
1201 xfs_trans_cancel(tp, 0);
1203 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1207 error = xfs_trans_reserve(tp, 0,
1208 XFS_ITRUNCATE_LOG_RES(mp),
1209 0, XFS_TRANS_PERM_LOG_RES,
1210 XFS_ITRUNCATE_LOG_COUNT);
1212 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1213 xfs_trans_cancel(tp, 0);
1214 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1218 xfs_ilock(ip, XFS_ILOCK_EXCL);
1219 xfs_trans_ijoin(tp, ip,
1222 xfs_trans_ihold(tp, ip);
1224 error = xfs_itruncate_finish(&tp, ip,
1229 * If we get an error at this point we
1230 * simply don't bother truncating the file.
1233 xfs_trans_cancel(tp,
1234 (XFS_TRANS_RELEASE_LOG_RES |
1237 error = xfs_trans_commit(tp,
1238 XFS_TRANS_RELEASE_LOG_RES);
1240 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1247 * Free a symlink that has blocks associated with it.
1250 xfs_inactive_symlink_rmt(
1258 xfs_fsblock_t first_block;
1259 xfs_bmap_free_t free_list;
1262 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1270 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1272 * We're freeing a symlink that has some
1273 * blocks allocated to it. Free the
1274 * blocks here. We know that we've got
1275 * either 1 or 2 extents and that we can
1276 * free them all in one bunmapi call.
1278 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1279 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1280 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1281 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1282 xfs_trans_cancel(tp, 0);
1287 * Lock the inode, fix the size, and join it to the transaction.
1288 * Hold it so in the normal path, we still have it locked for
1289 * the second transaction. In the error paths we need it
1290 * held so the cancel won't rele it, see below.
1292 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1293 size = (int)ip->i_d.di_size;
1294 ip->i_d.di_size = 0;
1295 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1296 xfs_trans_ihold(tp, ip);
1297 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1299 * Find the block(s) so we can inval and unmap them.
1302 XFS_BMAP_INIT(&free_list, &first_block);
1303 nmaps = ARRAY_SIZE(mval);
1304 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1305 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
1309 * Invalidate the block(s).
1311 for (i = 0; i < nmaps; i++) {
1312 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1313 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1314 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1315 xfs_trans_binval(tp, bp);
1318 * Unmap the dead block(s) to the free_list.
1320 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
1321 &first_block, &free_list, NULL, &done)))
1325 * Commit the first transaction. This logs the EFI and the inode.
1327 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
1330 * The transaction must have been committed, since there were
1331 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1332 * The new tp has the extent freeing and EFDs.
1336 * The first xact was committed, so add the inode to the new one.
1337 * Mark it dirty so it will be logged and moved forward in the log as
1338 * part of every commit.
1340 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1341 xfs_trans_ihold(tp, ip);
1342 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1344 * Get a new, empty transaction to return to our caller.
1346 ntp = xfs_trans_dup(tp);
1348 * Commit the transaction containing extent freeing and EFDs.
1349 * If we get an error on the commit here or on the reserve below,
1350 * we need to unlock the inode since the new transaction doesn't
1351 * have the inode attached.
1353 error = xfs_trans_commit(tp, 0);
1356 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1360 * Remove the memory for extent descriptions (just bookkeeping).
1362 if (ip->i_df.if_bytes)
1363 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1364 ASSERT(ip->i_df.if_bytes == 0);
1366 * Put an itruncate log reservation in the new transaction
1369 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1370 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1371 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1375 * Return with the inode locked but not joined to the transaction.
1381 xfs_bmap_cancel(&free_list);
1384 * Have to come here with the inode locked and either
1385 * (held and in the transaction) or (not in the transaction).
1386 * If the inode isn't held then cancel would iput it, but
1387 * that's wrong since this is inactive and the vnode ref
1388 * count is 0 already.
1389 * Cancel won't do anything to the inode if held, but it still
1390 * needs to be locked until the cancel is done, if it was
1391 * joined to the transaction.
1393 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1394 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1401 xfs_inactive_symlink_local(
1407 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1409 * We're freeing a symlink which fit into
1410 * the inode. Just free the memory used
1411 * to hold the old symlink.
1413 error = xfs_trans_reserve(*tpp, 0,
1414 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1415 0, XFS_TRANS_PERM_LOG_RES,
1416 XFS_ITRUNCATE_LOG_COUNT);
1419 xfs_trans_cancel(*tpp, 0);
1423 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1426 * Zero length symlinks _can_ exist.
1428 if (ip->i_df.if_bytes > 0) {
1429 xfs_idata_realloc(ip,
1430 -(ip->i_df.if_bytes),
1432 ASSERT(ip->i_df.if_bytes == 0);
1446 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1449 ASSERT(ip->i_d.di_forkoff != 0);
1450 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1451 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1455 error = xfs_attr_inactive(ip);
1459 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1460 error = xfs_trans_reserve(tp, 0,
1461 XFS_IFREE_LOG_RES(mp),
1462 0, XFS_TRANS_PERM_LOG_RES,
1463 XFS_INACTIVE_LOG_COUNT);
1467 xfs_ilock(ip, XFS_ILOCK_EXCL);
1468 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1469 xfs_trans_ihold(tp, ip);
1470 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1472 ASSERT(ip->i_d.di_anextents == 0);
1478 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1479 xfs_trans_cancel(tp, 0);
1482 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1490 bhv_vnode_t *vp = XFS_ITOV(ip);
1491 xfs_mount_t *mp = ip->i_mount;
1494 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
1497 /* If this is a read-only mount, don't do this (would generate I/O) */
1498 if (mp->m_flags & XFS_MOUNT_RDONLY)
1501 if (!XFS_FORCED_SHUTDOWN(mp)) {
1505 * If we are using filestreams, and we have an unlinked
1506 * file that we are processing the last close on, then nothing
1507 * will be able to reopen and write to this file. Purge this
1508 * inode from the filestreams cache so that it doesn't delay
1509 * teardown of the inode.
1511 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1512 xfs_filestream_deassociate(ip);
1515 * If we previously truncated this file and removed old data
1516 * in the process, we want to initiate "early" writeout on
1517 * the last close. This is an attempt to combat the notorious
1518 * NULL files problem which is particularly noticable from a
1519 * truncate down, buffered (re-)write (delalloc), followed by
1520 * a crash. What we are effectively doing here is
1521 * significantly reducing the time window where we'd otherwise
1522 * be exposed to that problem.
1524 truncated = xfs_iflags_test_and_clear(ip, XFS_ITRUNCATED);
1525 if (truncated && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
1526 xfs_flush_pages(ip, 0, -1, XFS_B_ASYNC, FI_NONE);
1529 if (ip->i_d.di_nlink != 0) {
1530 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1531 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1532 ip->i_delayed_blks > 0)) &&
1533 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1534 (!(ip->i_d.di_flags &
1535 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1536 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1548 * This is called when the vnode reference count for the vnode
1549 * goes to zero. If the file has been unlinked, then it must
1550 * now be truncated. Also, we clear all of the read-ahead state
1551 * kept for the inode here since the file is now closed.
1557 bhv_vnode_t *vp = XFS_ITOV(ip);
1558 xfs_bmap_free_t free_list;
1559 xfs_fsblock_t first_block;
1566 xfs_itrace_entry(ip);
1569 * If the inode is already free, then there can be nothing
1572 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1573 ASSERT(ip->i_df.if_real_bytes == 0);
1574 ASSERT(ip->i_df.if_broot_bytes == 0);
1575 return VN_INACTIVE_CACHE;
1579 * Only do a truncate if it's a regular file with
1580 * some actual space in it. It's OK to look at the
1581 * inode's fields without the lock because we're the
1582 * only one with a reference to the inode.
1584 truncate = ((ip->i_d.di_nlink == 0) &&
1585 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1586 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1587 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1591 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY))
1592 XFS_SEND_DESTROY(mp, ip, DM_RIGHT_NULL);
1596 /* If this is a read-only mount, don't do this (would generate I/O) */
1597 if (mp->m_flags & XFS_MOUNT_RDONLY)
1600 if (ip->i_d.di_nlink != 0) {
1601 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1602 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1603 ip->i_delayed_blks > 0)) &&
1604 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1605 (!(ip->i_d.di_flags &
1606 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1607 (ip->i_delayed_blks != 0)))) {
1608 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1610 return VN_INACTIVE_CACHE;
1615 ASSERT(ip->i_d.di_nlink == 0);
1617 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1618 return VN_INACTIVE_CACHE;
1620 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1623 * Do the xfs_itruncate_start() call before
1624 * reserving any log space because itruncate_start
1625 * will call into the buffer cache and we can't
1626 * do that within a transaction.
1628 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1630 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1632 xfs_trans_cancel(tp, 0);
1633 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1634 return VN_INACTIVE_CACHE;
1637 error = xfs_trans_reserve(tp, 0,
1638 XFS_ITRUNCATE_LOG_RES(mp),
1639 0, XFS_TRANS_PERM_LOG_RES,
1640 XFS_ITRUNCATE_LOG_COUNT);
1642 /* Don't call itruncate_cleanup */
1643 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1644 xfs_trans_cancel(tp, 0);
1645 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1646 return VN_INACTIVE_CACHE;
1649 xfs_ilock(ip, XFS_ILOCK_EXCL);
1650 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1651 xfs_trans_ihold(tp, ip);
1654 * normally, we have to run xfs_itruncate_finish sync.
1655 * But if filesystem is wsync and we're in the inactive
1656 * path, then we know that nlink == 0, and that the
1657 * xaction that made nlink == 0 is permanently committed
1658 * since xfs_remove runs as a synchronous transaction.
1660 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1661 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1664 xfs_trans_cancel(tp,
1665 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1666 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1667 return VN_INACTIVE_CACHE;
1669 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1672 * If we get an error while cleaning up a
1673 * symlink we bail out.
1675 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1676 xfs_inactive_symlink_rmt(ip, &tp) :
1677 xfs_inactive_symlink_local(ip, &tp);
1681 return VN_INACTIVE_CACHE;
1684 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1685 xfs_trans_ihold(tp, ip);
1687 error = xfs_trans_reserve(tp, 0,
1688 XFS_IFREE_LOG_RES(mp),
1689 0, XFS_TRANS_PERM_LOG_RES,
1690 XFS_INACTIVE_LOG_COUNT);
1692 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1693 xfs_trans_cancel(tp, 0);
1694 return VN_INACTIVE_CACHE;
1697 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1698 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1699 xfs_trans_ihold(tp, ip);
1703 * If there are attributes associated with the file
1704 * then blow them away now. The code calls a routine
1705 * that recursively deconstructs the attribute fork.
1706 * We need to just commit the current transaction
1707 * because we can't use it for xfs_attr_inactive().
1709 if (ip->i_d.di_anextents > 0) {
1710 error = xfs_inactive_attrs(ip, &tp);
1712 * If we got an error, the transaction is already
1713 * cancelled, and the inode is unlocked. Just get out.
1716 return VN_INACTIVE_CACHE;
1717 } else if (ip->i_afp) {
1718 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1724 XFS_BMAP_INIT(&free_list, &first_block);
1725 error = xfs_ifree(tp, ip, &free_list);
1728 * If we fail to free the inode, shut down. The cancel
1729 * might do that, we need to make sure. Otherwise the
1730 * inode might be lost for a long time or forever.
1732 if (!XFS_FORCED_SHUTDOWN(mp)) {
1734 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1735 error, mp->m_fsname);
1736 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1738 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1741 * Credit the quota account(s). The inode is gone.
1743 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1746 * Just ignore errors at this point. There is
1747 * nothing we can do except to try to keep going.
1749 (void) xfs_bmap_finish(&tp, &free_list, &committed);
1750 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1753 * Release the dquots held by inode, if any.
1755 XFS_QM_DQDETACH(mp, ip);
1757 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1760 return VN_INACTIVE_CACHE;
1767 struct xfs_name *name,
1775 xfs_itrace_entry(dp);
1777 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1778 return XFS_ERROR(EIO);
1780 lock_mode = xfs_ilock_map_shared(dp);
1781 error = xfs_dir_lookup_int(dp, lock_mode, name, &e_inum, &ip);
1786 xfs_iunlock_map_shared(dp, lock_mode);
1793 struct xfs_name *name,
1799 xfs_mount_t *mp = dp->i_mount;
1803 xfs_bmap_free_t free_list;
1804 xfs_fsblock_t first_block;
1805 boolean_t unlock_dp_on_error = B_FALSE;
1806 int dm_event_sent = 0;
1810 struct xfs_dquot *udqp, *gdqp;
1814 xfs_itrace_entry(dp);
1816 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1817 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1818 dp, DM_RIGHT_NULL, NULL,
1819 DM_RIGHT_NULL, name->name, NULL,
1827 if (XFS_FORCED_SHUTDOWN(mp))
1828 return XFS_ERROR(EIO);
1830 /* Return through std_return after this point. */
1833 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1834 prid = dp->i_d.di_projid;
1836 prid = (xfs_prid_t)dfltprid;
1839 * Make sure that we have allocated dquot(s) on disk.
1841 error = XFS_QM_DQVOPALLOC(mp, dp,
1842 current_fsuid(credp), current_fsgid(credp), prid,
1843 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1849 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1850 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1851 resblks = XFS_CREATE_SPACE_RES(mp, name->len);
1853 * Initially assume that the file does not exist and
1854 * reserve the resources for that case. If that is not
1855 * the case we'll drop the one we have and get a more
1856 * appropriate transaction later.
1858 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1859 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1860 if (error == ENOSPC) {
1862 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1863 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1870 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1871 unlock_dp_on_error = B_TRUE;
1873 XFS_BMAP_INIT(&free_list, &first_block);
1878 * Reserve disk quota and the inode.
1880 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1884 error = xfs_dir_canenter(tp, dp, name, resblks);
1887 error = xfs_dir_ialloc(&tp, dp, mode, 1,
1888 rdev, credp, prid, resblks > 0,
1891 if (error == ENOSPC)
1898 * At this point, we've gotten a newly allocated inode.
1899 * It is locked (and joined to the transaction).
1902 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1905 * Now we join the directory inode to the transaction. We do not do it
1906 * earlier because xfs_dir_ialloc might commit the previous transaction
1907 * (and release all the locks). An error from here on will result in
1908 * the transaction cancel unlocking dp so don't do it explicitly in the
1912 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1913 unlock_dp_on_error = B_FALSE;
1915 error = xfs_dir_createname(tp, dp, name, ip->i_ino,
1916 &first_block, &free_list, resblks ?
1917 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1919 ASSERT(error != ENOSPC);
1922 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1923 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1926 * If this is a synchronous mount, make sure that the
1927 * create transaction goes to disk before returning to
1930 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
1931 xfs_trans_set_sync(tp);
1937 * Attach the dquot(s) to the inodes and modify them incore.
1938 * These ids of the inode couldn't have changed since the new
1939 * inode has been locked ever since it was created.
1941 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
1944 * xfs_trans_commit normally decrements the vnode ref count
1945 * when it unlocks the inode. Since we want to return the
1946 * vnode to the caller, we bump the vnode ref count now.
1950 error = xfs_bmap_finish(&tp, &free_list, &committed);
1952 xfs_bmap_cancel(&free_list);
1956 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1963 XFS_QM_DQRELE(mp, udqp);
1964 XFS_QM_DQRELE(mp, gdqp);
1968 /* Fallthrough to std_return with error = 0 */
1971 if ((*ipp || (error != 0 && dm_event_sent != 0)) &&
1972 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
1973 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
1976 DM_RIGHT_NULL, name->name, NULL,
1982 cancel_flags |= XFS_TRANS_ABORT;
1987 xfs_trans_cancel(tp, cancel_flags);
1989 XFS_QM_DQRELE(mp, udqp);
1990 XFS_QM_DQRELE(mp, gdqp);
1992 if (unlock_dp_on_error)
1993 xfs_iunlock(dp, XFS_ILOCK_EXCL);
1999 * Wait until after the current transaction is aborted to
2000 * release the inode. This prevents recursive transactions
2001 * and deadlocks from xfs_inactive.
2003 cancel_flags |= XFS_TRANS_ABORT;
2004 xfs_trans_cancel(tp, cancel_flags);
2007 XFS_QM_DQRELE(mp, udqp);
2008 XFS_QM_DQRELE(mp, gdqp);
2015 * Some counters to see if (and how often) we are hitting some deadlock
2016 * prevention code paths.
2020 int xfs_rm_lock_delays;
2021 int xfs_rm_attempts;
2025 * The following routine will lock the inodes associated with the
2026 * directory and the named entry in the directory. The locks are
2027 * acquired in increasing inode number.
2029 * If the entry is "..", then only the directory is locked. The
2030 * vnode ref count will still include that from the .. entry in
2033 * There is a deadlock we need to worry about. If the locked directory is
2034 * in the AIL, it might be blocking up the log. The next inode we lock
2035 * could be already locked by another thread waiting for log space (e.g
2036 * a permanent log reservation with a long running transaction (see
2037 * xfs_itruncate_finish)). To solve this, we must check if the directory
2038 * is in the ail and use lock_nowait. If we can't lock, we need to
2039 * drop the inode lock on the directory and try again. xfs_iunlock will
2040 * potentially push the tail if we were holding up the log.
2043 xfs_lock_dir_and_entry(
2045 xfs_inode_t *ip) /* inode of entry 'name' */
2049 xfs_inode_t *ips[2];
2058 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2065 * We want to lock in increasing inum. Since we've already
2066 * acquired the lock on the directory, we may need to release
2067 * if if the inum of the entry turns out to be less.
2069 if (e_inum > dp->i_ino) {
2071 * We are already in the right order, so just
2072 * lock on the inode of the entry.
2073 * We need to use nowait if dp is in the AIL.
2076 lp = (xfs_log_item_t *)dp->i_itemp;
2077 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2078 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2085 * Unlock dp and try again.
2086 * xfs_iunlock will try to push the tail
2087 * if the inode is in the AIL.
2090 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2092 if ((attempts % 5) == 0) {
2093 delay(1); /* Don't just spin the CPU */
2095 xfs_rm_lock_delays++;
2101 xfs_ilock(ip, XFS_ILOCK_EXCL);
2103 } else if (e_inum < dp->i_ino) {
2104 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2108 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2110 /* else e_inum == dp->i_ino */
2111 /* This can happen if we're asked to lock /x/..
2112 * the entry is "..", which is also the parent directory.
2120 int xfs_small_retries;
2121 int xfs_middle_retries;
2122 int xfs_lots_retries;
2123 int xfs_lock_delays;
2127 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2131 xfs_lock_inumorder(int lock_mode, int subclass)
2133 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
2134 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
2135 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
2136 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
2142 * The following routine will lock n inodes in exclusive mode.
2143 * We assume the caller calls us with the inodes in i_ino order.
2145 * We need to detect deadlock where an inode that we lock
2146 * is in the AIL and we start waiting for another inode that is locked
2147 * by a thread in a long running transaction (such as truncate). This can
2148 * result in deadlock since the long running trans might need to wait
2149 * for the inode we just locked in order to push the tail and free space
2159 int attempts = 0, i, j, try_lock;
2162 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2173 for (; i < inodes; i++) {
2176 if (i && (ips[i] == ips[i-1])) /* Already locked */
2180 * If try_lock is not set yet, make sure all locked inodes
2181 * are not in the AIL.
2182 * If any are, set try_lock to be used later.
2186 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2187 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2188 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2195 * If any of the previous locks we have locked is in the AIL,
2196 * we must TRY to get the second and subsequent locks. If
2197 * we can't get any, we must release all we have
2202 /* try_lock must be 0 if i is 0. */
2204 * try_lock means we have an inode locked
2205 * that is in the AIL.
2208 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
2212 * Unlock all previous guys and try again.
2213 * xfs_iunlock will try to push the tail
2214 * if the inode is in the AIL.
2217 for(j = i - 1; j >= 0; j--) {
2220 * Check to see if we've already
2221 * unlocked this one.
2222 * Not the first one going back,
2223 * and the inode ptr is the same.
2225 if ((j != (i - 1)) && ips[j] ==
2229 xfs_iunlock(ips[j], lock_mode);
2232 if ((attempts % 5) == 0) {
2233 delay(1); /* Don't just spin the CPU */
2243 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
2249 if (attempts < 5) xfs_small_retries++;
2250 else if (attempts < 100) xfs_middle_retries++;
2251 else xfs_lots_retries++;
2259 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2260 int remove_which_error_return = 0;
2262 #define REMOVE_DEBUG_TRACE(x)
2263 #endif /* ! DEBUG */
2268 struct xfs_name *name,
2271 xfs_mount_t *mp = dp->i_mount;
2272 xfs_trans_t *tp = NULL;
2274 xfs_bmap_free_t free_list;
2275 xfs_fsblock_t first_block;
2281 xfs_itrace_entry(dp);
2283 if (XFS_FORCED_SHUTDOWN(mp))
2284 return XFS_ERROR(EIO);
2286 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
2287 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dp, DM_RIGHT_NULL,
2288 NULL, DM_RIGHT_NULL, name->name, NULL,
2289 ip->i_d.di_mode, 0, 0);
2295 * We need to get a reference to ip before we get our log
2296 * reservation. The reason for this is that we cannot call
2297 * xfs_iget for an inode for which we do not have a reference
2298 * once we've acquired a log reservation. This is because the
2299 * inode we are trying to get might be in xfs_inactive going
2300 * for a log reservation. Since we'll have to wait for the
2301 * inactive code to complete before returning from xfs_iget,
2302 * we need to make sure that we don't have log space reserved
2303 * when we call xfs_iget. Instead we get an unlocked reference
2304 * to the inode before getting our log reservation.
2308 xfs_itrace_entry(ip);
2311 error = XFS_QM_DQATTACH(mp, dp, 0);
2312 if (!error && dp != ip)
2313 error = XFS_QM_DQATTACH(mp, ip, 0);
2315 REMOVE_DEBUG_TRACE(__LINE__);
2320 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2321 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2323 * We try to get the real space reservation first,
2324 * allowing for directory btree deletion(s) implying
2325 * possible bmap insert(s). If we can't get the space
2326 * reservation then we use 0 instead, and avoid the bmap
2327 * btree insert(s) in the directory code by, if the bmap
2328 * insert tries to happen, instead trimming the LAST
2329 * block from the directory.
2331 resblks = XFS_REMOVE_SPACE_RES(mp);
2332 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2333 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2334 if (error == ENOSPC) {
2336 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2337 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2340 ASSERT(error != ENOSPC);
2341 REMOVE_DEBUG_TRACE(__LINE__);
2342 xfs_trans_cancel(tp, 0);
2347 error = xfs_lock_dir_and_entry(dp, ip);
2349 REMOVE_DEBUG_TRACE(__LINE__);
2350 xfs_trans_cancel(tp, cancel_flags);
2356 * At this point, we've gotten both the directory and the entry
2359 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2362 * Increment vnode ref count only in this case since
2363 * there's an extra vnode reference in the case where
2367 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2371 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2373 XFS_BMAP_INIT(&free_list, &first_block);
2374 error = xfs_dir_removename(tp, dp, name, ip->i_ino,
2375 &first_block, &free_list, 0);
2377 ASSERT(error != ENOENT);
2378 REMOVE_DEBUG_TRACE(__LINE__);
2381 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2384 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2386 error = xfs_droplink(tp, ip);
2388 REMOVE_DEBUG_TRACE(__LINE__);
2392 /* Determine if this is the last link while
2393 * we are in the transaction.
2395 link_zero = (ip)->i_d.di_nlink==0;
2398 * Take an extra ref on the inode so that it doesn't
2399 * go to xfs_inactive() from within the commit.
2404 * If this is a synchronous mount, make sure that the
2405 * remove transaction goes to disk before returning to
2408 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2409 xfs_trans_set_sync(tp);
2412 error = xfs_bmap_finish(&tp, &free_list, &committed);
2414 REMOVE_DEBUG_TRACE(__LINE__);
2418 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2425 * If we are using filestreams, kill the stream association.
2426 * If the file is still open it may get a new one but that
2427 * will get killed on last close in xfs_close() so we don't
2428 * have to worry about that.
2430 if (link_zero && xfs_inode_is_filestream(ip))
2431 xfs_filestream_deassociate(ip);
2433 xfs_itrace_exit(ip);
2436 /* Fall through to std_return with error = 0 */
2438 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
2439 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2441 NULL, DM_RIGHT_NULL,
2442 name->name, NULL, ip->i_d.di_mode, error, 0);
2447 xfs_bmap_cancel(&free_list);
2448 cancel_flags |= XFS_TRANS_ABORT;
2449 xfs_trans_cancel(tp, cancel_flags);
2454 * In this case make sure to not release the inode until after
2455 * the current transaction is aborted. Releasing it beforehand
2456 * can cause us to go to xfs_inactive and start a recursive
2457 * transaction which can easily deadlock with the current one.
2459 xfs_bmap_cancel(&free_list);
2460 cancel_flags |= XFS_TRANS_ABORT;
2461 xfs_trans_cancel(tp, cancel_flags);
2472 struct xfs_name *target_name)
2474 xfs_mount_t *mp = tdp->i_mount;
2476 xfs_inode_t *ips[2];
2478 xfs_bmap_free_t free_list;
2479 xfs_fsblock_t first_block;
2484 xfs_itrace_entry(tdp);
2485 xfs_itrace_entry(sip);
2487 ASSERT(!S_ISDIR(sip->i_d.di_mode));
2489 if (XFS_FORCED_SHUTDOWN(mp))
2490 return XFS_ERROR(EIO);
2492 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
2493 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2496 target_name->name, NULL, 0, 0, 0);
2501 /* Return through std_return after this point. */
2503 error = XFS_QM_DQATTACH(mp, sip, 0);
2504 if (!error && sip != tdp)
2505 error = XFS_QM_DQATTACH(mp, tdp, 0);
2509 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2510 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2511 resblks = XFS_LINK_SPACE_RES(mp, target_name->len);
2512 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2513 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2514 if (error == ENOSPC) {
2516 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2517 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2524 if (sip->i_ino < tdp->i_ino) {
2532 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2535 * Increment vnode ref counts since xfs_trans_commit &
2536 * xfs_trans_cancel will both unlock the inodes and
2537 * decrement the associated ref counts.
2541 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2542 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2545 * If the source has too many links, we can't make any more to it.
2547 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2548 error = XFS_ERROR(EMLINK);
2553 * If we are using project inheritance, we only allow hard link
2554 * creation in our tree when the project IDs are the same; else
2555 * the tree quota mechanism could be circumvented.
2557 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2558 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2559 error = XFS_ERROR(EXDEV);
2563 error = xfs_dir_canenter(tp, tdp, target_name, resblks);
2567 XFS_BMAP_INIT(&free_list, &first_block);
2569 error = xfs_dir_createname(tp, tdp, target_name, sip->i_ino,
2570 &first_block, &free_list, resblks);
2573 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2575 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2577 error = xfs_bumplink(tp, sip);
2582 * If this is a synchronous mount, make sure that the
2583 * link transaction goes to disk before returning to
2586 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2587 xfs_trans_set_sync(tp);
2590 error = xfs_bmap_finish (&tp, &free_list, &committed);
2592 xfs_bmap_cancel(&free_list);
2596 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2600 /* Fall through to std_return with error = 0. */
2602 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
2603 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2606 target_name->name, NULL, 0, error, 0);
2611 cancel_flags |= XFS_TRANS_ABORT;
2615 xfs_trans_cancel(tp, cancel_flags);
2623 struct xfs_name *dir_name,
2628 xfs_mount_t *mp = dp->i_mount;
2629 xfs_inode_t *cdp; /* inode of created dir */
2634 xfs_bmap_free_t free_list;
2635 xfs_fsblock_t first_block;
2636 boolean_t unlock_dp_on_error = B_FALSE;
2637 boolean_t created = B_FALSE;
2638 int dm_event_sent = 0;
2640 struct xfs_dquot *udqp, *gdqp;
2643 if (XFS_FORCED_SHUTDOWN(mp))
2644 return XFS_ERROR(EIO);
2648 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
2649 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2650 dp, DM_RIGHT_NULL, NULL,
2651 DM_RIGHT_NULL, dir_name->name, NULL,
2658 /* Return through std_return after this point. */
2660 xfs_itrace_entry(dp);
2664 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2665 prid = dp->i_d.di_projid;
2667 prid = (xfs_prid_t)dfltprid;
2670 * Make sure that we have allocated dquot(s) on disk.
2672 error = XFS_QM_DQVOPALLOC(mp, dp,
2673 current_fsuid(credp), current_fsgid(credp), prid,
2674 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2678 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2679 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2680 resblks = XFS_MKDIR_SPACE_RES(mp, dir_name->len);
2681 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2682 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2683 if (error == ENOSPC) {
2685 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2686 XFS_TRANS_PERM_LOG_RES,
2687 XFS_MKDIR_LOG_COUNT);
2694 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2695 unlock_dp_on_error = B_TRUE;
2698 * Check for directory link count overflow.
2700 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2701 error = XFS_ERROR(EMLINK);
2706 * Reserve disk quota and the inode.
2708 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2712 error = xfs_dir_canenter(tp, dp, dir_name, resblks);
2716 * create the directory inode.
2718 error = xfs_dir_ialloc(&tp, dp, mode, 2,
2719 0, credp, prid, resblks > 0,
2722 if (error == ENOSPC)
2726 xfs_itrace_ref(cdp);
2729 * Now we add the directory inode to the transaction.
2730 * We waited until now since xfs_dir_ialloc might start
2731 * a new transaction. Had we joined the transaction
2732 * earlier, the locks might have gotten released. An error
2733 * from here on will result in the transaction cancel
2734 * unlocking dp so don't do it explicitly in the error path.
2737 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2738 unlock_dp_on_error = B_FALSE;
2740 XFS_BMAP_INIT(&free_list, &first_block);
2742 error = xfs_dir_createname(tp, dp, dir_name, cdp->i_ino,
2743 &first_block, &free_list, resblks ?
2744 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2746 ASSERT(error != ENOSPC);
2749 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2752 * Bump the in memory version number of the parent directory
2753 * so that other processes accessing it will recognize that
2754 * the directory has changed.
2758 error = xfs_dir_init(tp, cdp, dp);
2763 error = xfs_bumplink(tp, dp);
2773 * Attach the dquots to the new inode and modify the icount incore.
2775 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2778 * If this is a synchronous mount, make sure that the
2779 * mkdir transaction goes to disk before returning to
2782 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2783 xfs_trans_set_sync(tp);
2786 error = xfs_bmap_finish(&tp, &free_list, &committed);
2792 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2793 XFS_QM_DQRELE(mp, udqp);
2794 XFS_QM_DQRELE(mp, gdqp);
2799 /* Fall through to std_return with error = 0 or errno from
2800 * xfs_trans_commit. */
2803 if ((created || (error != 0 && dm_event_sent != 0)) &&
2804 DM_EVENT_ENABLED(dp, DM_EVENT_POSTCREATE)) {
2805 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2807 created ? cdp : NULL,
2809 dir_name->name, NULL,
2816 xfs_bmap_cancel(&free_list);
2818 cancel_flags |= XFS_TRANS_ABORT;
2820 xfs_trans_cancel(tp, cancel_flags);
2821 XFS_QM_DQRELE(mp, udqp);
2822 XFS_QM_DQRELE(mp, gdqp);
2824 if (unlock_dp_on_error)
2825 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2833 struct xfs_name *name,
2836 bhv_vnode_t *dir_vp = XFS_ITOV(dp);
2837 xfs_mount_t *mp = dp->i_mount;
2840 xfs_bmap_free_t free_list;
2841 xfs_fsblock_t first_block;
2847 xfs_itrace_entry(dp);
2849 if (XFS_FORCED_SHUTDOWN(mp))
2850 return XFS_ERROR(EIO);
2852 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
2853 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
2855 NULL, DM_RIGHT_NULL, name->name,
2856 NULL, cdp->i_d.di_mode, 0, 0);
2858 return XFS_ERROR(error);
2862 * We need to get a reference to cdp before we get our log
2863 * reservation. The reason for this is that we cannot call
2864 * xfs_iget for an inode for which we do not have a reference
2865 * once we've acquired a log reservation. This is because the
2866 * inode we are trying to get might be in xfs_inactive going
2867 * for a log reservation. Since we'll have to wait for the
2868 * inactive code to complete before returning from xfs_iget,
2869 * we need to make sure that we don't have log space reserved
2870 * when we call xfs_iget. Instead we get an unlocked reference
2871 * to the inode before getting our log reservation.
2876 * Get the dquots for the inodes.
2878 error = XFS_QM_DQATTACH(mp, dp, 0);
2879 if (!error && dp != cdp)
2880 error = XFS_QM_DQATTACH(mp, cdp, 0);
2883 REMOVE_DEBUG_TRACE(__LINE__);
2887 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
2888 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2890 * We try to get the real space reservation first,
2891 * allowing for directory btree deletion(s) implying
2892 * possible bmap insert(s). If we can't get the space
2893 * reservation then we use 0 instead, and avoid the bmap
2894 * btree insert(s) in the directory code by, if the bmap
2895 * insert tries to happen, instead trimming the LAST
2896 * block from the directory.
2898 resblks = XFS_REMOVE_SPACE_RES(mp);
2899 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2900 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2901 if (error == ENOSPC) {
2903 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2904 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
2907 ASSERT(error != ENOSPC);
2912 XFS_BMAP_INIT(&free_list, &first_block);
2915 * Now lock the child directory inode and the parent directory
2916 * inode in the proper order. This will take care of validating
2917 * that the directory entry for the child directory inode has
2918 * not changed while we were obtaining a log reservation.
2920 error = xfs_lock_dir_and_entry(dp, cdp);
2922 xfs_trans_cancel(tp, cancel_flags);
2927 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2930 * Only increment the parent directory vnode count if
2931 * we didn't bump it in looking up cdp. The only time
2932 * we don't bump it is when we're looking up ".".
2937 xfs_itrace_ref(cdp);
2938 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
2940 ASSERT(cdp->i_d.di_nlink >= 2);
2941 if (cdp->i_d.di_nlink != 2) {
2942 error = XFS_ERROR(ENOTEMPTY);
2945 if (!xfs_dir_isempty(cdp)) {
2946 error = XFS_ERROR(ENOTEMPTY);
2950 error = xfs_dir_removename(tp, dp, name, cdp->i_ino,
2951 &first_block, &free_list, resblks);
2955 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2958 * Bump the in memory generation count on the parent
2959 * directory so that other can know that it has changed.
2964 * Drop the link from cdp's "..".
2966 error = xfs_droplink(tp, dp);
2972 * Drop the link from dp to cdp.
2974 error = xfs_droplink(tp, cdp);
2980 * Drop the "." link from cdp to self.
2982 error = xfs_droplink(tp, cdp);
2987 /* Determine these before committing transaction */
2988 last_cdp_link = (cdp)->i_d.di_nlink==0;
2991 * Take an extra ref on the child vnode so that it
2992 * does not go to xfs_inactive() from within the commit.
2997 * If this is a synchronous mount, make sure that the
2998 * rmdir transaction goes to disk before returning to
3001 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3002 xfs_trans_set_sync(tp);
3005 error = xfs_bmap_finish (&tp, &free_list, &committed);
3007 xfs_bmap_cancel(&free_list);
3008 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3014 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3023 /* Fall through to std_return with error = 0 or the errno
3024 * from xfs_trans_commit. */
3026 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
3027 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3029 NULL, DM_RIGHT_NULL,
3030 name->name, NULL, cdp->i_d.di_mode,
3036 xfs_bmap_cancel(&free_list);
3037 cancel_flags |= XFS_TRANS_ABORT;
3041 xfs_trans_cancel(tp, cancel_flags);
3048 struct xfs_name *link_name,
3049 const char *target_path,
3054 xfs_mount_t *mp = dp->i_mount;
3059 xfs_bmap_free_t free_list;
3060 xfs_fsblock_t first_block;
3061 boolean_t unlock_dp_on_error = B_FALSE;
3064 xfs_fileoff_t first_fsb;
3065 xfs_filblks_t fs_blocks;
3067 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3069 const char *cur_chunk;
3074 struct xfs_dquot *udqp, *gdqp;
3082 xfs_itrace_entry(dp);
3084 if (XFS_FORCED_SHUTDOWN(mp))
3085 return XFS_ERROR(EIO);
3088 * Check component lengths of the target path name.
3090 pathlen = strlen(target_path);
3091 if (pathlen >= MAXPATHLEN) /* total string too long */
3092 return XFS_ERROR(ENAMETOOLONG);
3094 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
3095 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dp,
3096 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3097 link_name->name, target_path, 0, 0, 0);
3102 /* Return through std_return after this point. */
3105 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3106 prid = dp->i_d.di_projid;
3108 prid = (xfs_prid_t)dfltprid;
3111 * Make sure that we have allocated dquot(s) on disk.
3113 error = XFS_QM_DQVOPALLOC(mp, dp,
3114 current_fsuid(credp), current_fsgid(credp), prid,
3115 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3119 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3120 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3122 * The symlink will fit into the inode data fork?
3123 * There can't be any attributes so we get the whole variable part.
3125 if (pathlen <= XFS_LITINO(mp))
3128 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3129 resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
3130 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3131 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3132 if (error == ENOSPC && fs_blocks == 0) {
3134 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3135 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3142 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
3143 unlock_dp_on_error = B_TRUE;
3146 * Check whether the directory allows new symlinks or not.
3148 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3149 error = XFS_ERROR(EPERM);
3154 * Reserve disk quota : blocks and inode.
3156 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3161 * Check for ability to enter directory entry, if no space reserved.
3163 error = xfs_dir_canenter(tp, dp, link_name, resblks);
3167 * Initialize the bmap freelist prior to calling either
3168 * bmapi or the directory create code.
3170 XFS_BMAP_INIT(&free_list, &first_block);
3173 * Allocate an inode for the symlink.
3175 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT),
3176 1, 0, credp, prid, resblks > 0, &ip, NULL);
3178 if (error == ENOSPC)
3185 * An error after we've joined dp to the transaction will result in the
3186 * transaction cancel unlocking dp so don't do it explicitly in the
3190 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3191 unlock_dp_on_error = B_FALSE;
3194 * Also attach the dquot(s) to it, if applicable.
3196 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3199 resblks -= XFS_IALLOC_SPACE_RES(mp);
3201 * If the symlink will fit into the inode, write it inline.
3203 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3204 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3205 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3206 ip->i_d.di_size = pathlen;
3209 * The inode was initially created in extent format.
3211 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3212 ip->i_df.if_flags |= XFS_IFINLINE;
3214 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3215 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3219 nmaps = SYMLINK_MAPS;
3221 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3222 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3223 &first_block, resblks, mval, &nmaps,
3230 resblks -= fs_blocks;
3231 ip->i_d.di_size = pathlen;
3232 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3234 cur_chunk = target_path;
3235 for (n = 0; n < nmaps; n++) {
3236 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3237 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3238 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3239 BTOBB(byte_cnt), 0);
3240 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3241 if (pathlen < byte_cnt) {
3244 pathlen -= byte_cnt;
3246 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3247 cur_chunk += byte_cnt;
3249 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3254 * Create the directory entry for the symlink.
3256 error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
3257 &first_block, &free_list, resblks);
3260 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3261 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3264 * Bump the in memory version number of the parent directory
3265 * so that other processes accessing it will recognize that
3266 * the directory has changed.
3271 * If this is a synchronous mount, make sure that the
3272 * symlink transaction goes to disk before returning to
3275 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3276 xfs_trans_set_sync(tp);
3280 * xfs_trans_commit normally decrements the vnode ref count
3281 * when it unlocks the inode. Since we want to return the
3282 * vnode to the caller, we bump the vnode ref count now.
3286 error = xfs_bmap_finish(&tp, &free_list, &committed);
3290 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3291 XFS_QM_DQRELE(mp, udqp);
3292 XFS_QM_DQRELE(mp, gdqp);
3294 /* Fall through to std_return with error = 0 or errno from
3295 * xfs_trans_commit */
3297 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTSYMLINK)) {
3298 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3301 DM_RIGHT_NULL, link_name->name,
3302 target_path, 0, error, 0);
3312 xfs_bmap_cancel(&free_list);
3313 cancel_flags |= XFS_TRANS_ABORT;
3315 xfs_trans_cancel(tp, cancel_flags);
3316 XFS_QM_DQRELE(mp, udqp);
3317 XFS_QM_DQRELE(mp, gdqp);
3319 if (unlock_dp_on_error)
3320 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3330 xfs_mount_t *mp = ip->i_mount;
3333 if (XFS_FORCED_SHUTDOWN(mp))
3334 return XFS_ERROR(EIO);
3337 * Bypass inodes which have already been cleaned by
3338 * the inode flush clustering code inside xfs_iflush
3340 if (xfs_inode_clean(ip))
3344 * We make this non-blocking if the inode is contended,
3345 * return EAGAIN to indicate to the caller that they
3346 * did not succeed. This prevents the flush path from
3347 * blocking on inodes inside another operation right
3348 * now, they get caught later by xfs_sync.
3350 if (flags & FLUSH_SYNC) {
3351 xfs_ilock(ip, XFS_ILOCK_SHARED);
3353 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3354 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3355 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3362 error = xfs_iflush(ip, (flags & FLUSH_SYNC) ? XFS_IFLUSH_SYNC
3363 : XFS_IFLUSH_ASYNC_NOBLOCK);
3364 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3376 xfs_mount_t *mp = ip->i_mount;
3380 if (!capable(CAP_SYS_ADMIN))
3381 return XFS_ERROR(EPERM);
3383 if (XFS_FORCED_SHUTDOWN(mp))
3384 return XFS_ERROR(EIO);
3386 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3387 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3389 xfs_trans_cancel(tp, 0);
3392 xfs_ilock(ip, XFS_ILOCK_EXCL);
3393 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3395 ip->i_d.di_dmevmask = evmask;
3396 ip->i_d.di_dmstate = state;
3398 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3400 error = xfs_trans_commit(tp, 0);
3409 bhv_vnode_t *vp = XFS_ITOV(ip);
3411 xfs_itrace_entry(ip);
3413 ASSERT(!VN_MAPPED(vp));
3415 /* bad inode, get out here ASAP */
3423 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
3426 * Make sure the atime in the XFS inode is correct before freeing the
3429 xfs_synchronize_atime(ip);
3432 * If we have nothing to flush with this inode then complete the
3433 * teardown now, otherwise break the link between the xfs inode and the
3434 * linux inode and clean up the xfs inode later. This avoids flushing
3435 * the inode to disk during the delete operation itself.
3437 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3438 * first to ensure that xfs_iunpin() will never see an xfs inode
3439 * that has a linux inode being reclaimed. Synchronisation is provided
3440 * by the i_flags_lock.
3442 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3443 xfs_ilock(ip, XFS_ILOCK_EXCL);
3445 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3447 xfs_mount_t *mp = ip->i_mount;
3449 /* Protect sync and unpin from us */
3450 XFS_MOUNT_ILOCK(mp);
3451 spin_lock(&ip->i_flags_lock);
3452 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
3453 vn_to_inode(vp)->i_private = NULL;
3455 spin_unlock(&ip->i_flags_lock);
3456 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
3457 XFS_MOUNT_IUNLOCK(mp);
3468 xfs_perag_t *pag = xfs_get_perag(ip->i_mount, ip->i_ino);
3469 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
3472 if (vp && VN_BAD(vp))
3475 /* The hash lock here protects a thread in xfs_iget_core from
3476 * racing with us on linking the inode back with a vnode.
3477 * Once we have the XFS_IRECLAIM flag set it will not touch
3480 write_lock(&pag->pag_ici_lock);
3481 spin_lock(&ip->i_flags_lock);
3482 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3483 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
3484 spin_unlock(&ip->i_flags_lock);
3485 write_unlock(&pag->pag_ici_lock);
3488 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3492 __xfs_iflags_set(ip, XFS_IRECLAIM);
3493 spin_unlock(&ip->i_flags_lock);
3494 write_unlock(&pag->pag_ici_lock);
3495 xfs_put_perag(ip->i_mount, pag);
3498 * If the inode is still dirty, then flush it out. If the inode
3499 * is not in the AIL, then it will be OK to flush it delwri as
3500 * long as xfs_iflush() does not keep any references to the inode.
3501 * We leave that decision up to xfs_iflush() since it has the
3502 * knowledge of whether it's OK to simply do a delwri flush of
3503 * the inode or whether we need to wait until the inode is
3504 * pulled from the AIL.
3505 * We get the flush lock regardless, though, just to make sure
3506 * we don't free it while it is being flushed.
3509 xfs_ilock(ip, XFS_ILOCK_EXCL);
3513 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3514 if (ip->i_update_core ||
3515 ((ip->i_itemp != NULL) &&
3516 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3517 error = xfs_iflush(ip, sync_mode);
3519 * If we hit an error, typically because of filesystem
3520 * shutdown, we don't need to let vn_reclaim to know
3521 * because we're gonna reclaim the inode anyway.
3524 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3527 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3530 ASSERT(ip->i_update_core == 0);
3531 ASSERT(ip->i_itemp == NULL ||
3532 ip->i_itemp->ili_format.ilf_fields == 0);
3536 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3544 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3547 xfs_inode_t *ip, *n;
3552 XFS_MOUNT_ILOCK(mp);
3553 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3555 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3557 if (xfs_ipincount(ip) ||
3558 !xfs_iflock_nowait(ip)) {
3559 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3563 XFS_MOUNT_IUNLOCK(mp);
3564 if (xfs_finish_reclaim(ip, noblock,
3565 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3574 XFS_MOUNT_IUNLOCK(mp);
3579 * xfs_alloc_file_space()
3580 * This routine allocates disk space for the given file.
3582 * If alloc_type == 0, this request is for an ALLOCSP type
3583 * request which will change the file size. In this case, no
3584 * DMAPI event will be generated by the call. A TRUNCATE event
3585 * will be generated later by xfs_setattr.
3587 * If alloc_type != 0, this request is for a RESVSP type
3588 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3589 * lower block boundary byte address is less than the file's
3598 xfs_alloc_file_space(
3605 xfs_mount_t *mp = ip->i_mount;
3607 xfs_filblks_t allocated_fsb;
3608 xfs_filblks_t allocatesize_fsb;
3609 xfs_extlen_t extsz, temp;
3610 xfs_fileoff_t startoffset_fsb;
3611 xfs_fsblock_t firstfsb;
3617 xfs_bmbt_irec_t imaps[1], *imapp;
3618 xfs_bmap_free_t free_list;
3619 uint qblocks, resblks, resrtextents;
3623 xfs_itrace_entry(ip);
3625 if (XFS_FORCED_SHUTDOWN(mp))
3626 return XFS_ERROR(EIO);
3628 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3632 return XFS_ERROR(EINVAL);
3634 rt = XFS_IS_REALTIME_INODE(ip);
3635 extsz = xfs_get_extsz_hint(ip);
3640 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
3641 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
3642 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
3644 /* Generate a DMAPI event if needed. */
3645 if (alloc_type != 0 && offset < ip->i_size &&
3646 (attr_flags&ATTR_DMI) == 0 &&
3647 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
3648 xfs_off_t end_dmi_offset;
3650 end_dmi_offset = offset+len;
3651 if (end_dmi_offset > ip->i_size)
3652 end_dmi_offset = ip->i_size;
3653 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip, offset,
3654 end_dmi_offset - offset, 0, NULL);
3660 * Allocate file space until done or until there is an error
3663 while (allocatesize_fsb && !error) {
3667 * Determine space reservations for data/realtime.
3669 if (unlikely(extsz)) {
3670 s = startoffset_fsb;
3673 e = startoffset_fsb + allocatesize_fsb;
3674 if ((temp = do_mod(startoffset_fsb, extsz)))
3676 if ((temp = do_mod(e, extsz)))
3680 e = allocatesize_fsb;
3684 resrtextents = qblocks = (uint)(e - s);
3685 resrtextents /= mp->m_sb.sb_rextsize;
3686 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3687 quota_flag = XFS_QMOPT_RES_RTBLKS;
3690 resblks = qblocks = \
3691 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
3692 quota_flag = XFS_QMOPT_RES_REGBLKS;
3696 * Allocate and setup the transaction.
3698 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
3699 error = xfs_trans_reserve(tp, resblks,
3700 XFS_WRITE_LOG_RES(mp), resrtextents,
3701 XFS_TRANS_PERM_LOG_RES,
3702 XFS_WRITE_LOG_COUNT);
3704 * Check for running out of space
3708 * Free the transaction structure.
3710 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
3711 xfs_trans_cancel(tp, 0);
3714 xfs_ilock(ip, XFS_ILOCK_EXCL);
3715 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
3716 qblocks, 0, quota_flag);
3720 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3721 xfs_trans_ihold(tp, ip);
3724 * Issue the xfs_bmapi() call to allocate the blocks
3726 XFS_BMAP_INIT(&free_list, &firstfsb);
3727 error = xfs_bmapi(tp, ip, startoffset_fsb,
3728 allocatesize_fsb, bmapi_flag,
3729 &firstfsb, 0, imapp, &nimaps,
3736 * Complete the transaction
3738 error = xfs_bmap_finish(&tp, &free_list, &committed);
3743 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3744 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3749 allocated_fsb = imapp->br_blockcount;
3752 error = XFS_ERROR(ENOSPC);
3756 startoffset_fsb += allocated_fsb;
3757 allocatesize_fsb -= allocated_fsb;
3760 if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
3761 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
3762 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
3765 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
3767 goto retry; /* Maybe DMAPI app. has made space */
3768 /* else fall through with error from XFS_SEND_DATA */
3773 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
3774 xfs_bmap_cancel(&free_list);
3775 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
3777 error1: /* Just cancel transaction */
3778 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
3779 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3780 goto dmapi_enospc_check;
3784 * Zero file bytes between startoff and endoff inclusive.
3785 * The iolock is held exclusive and no blocks are buffered.
3788 xfs_zero_remaining_bytes(
3793 xfs_bmbt_irec_t imap;
3794 xfs_fileoff_t offset_fsb;
3795 xfs_off_t lastoffset;
3798 xfs_mount_t *mp = ip->i_mount;
3802 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
3803 XFS_IS_REALTIME_INODE(ip) ?
3804 mp->m_rtdev_targp : mp->m_ddev_targp);
3806 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
3807 offset_fsb = XFS_B_TO_FSBT(mp, offset);
3809 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0,
3810 NULL, 0, &imap, &nimap, NULL, NULL);
3811 if (error || nimap < 1)
3813 ASSERT(imap.br_blockcount >= 1);
3814 ASSERT(imap.br_startoff == offset_fsb);
3815 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
3816 if (lastoffset > endoff)
3817 lastoffset = endoff;
3818 if (imap.br_startblock == HOLESTARTBLOCK)
3820 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3821 if (imap.br_state == XFS_EXT_UNWRITTEN)
3824 XFS_BUF_UNWRITE(bp);
3826 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
3828 error = xfs_iowait(bp);
3830 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3831 mp, bp, XFS_BUF_ADDR(bp));
3834 memset(XFS_BUF_PTR(bp) +
3835 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
3836 0, lastoffset - offset + 1);
3841 error = xfs_iowait(bp);
3843 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3844 mp, bp, XFS_BUF_ADDR(bp));
3853 * xfs_free_file_space()
3854 * This routine frees disk space for the given file.
3856 * This routine is only called by xfs_change_file_space
3857 * for an UNRESVSP type call.
3865 xfs_free_file_space(
3874 xfs_off_t end_dmi_offset;
3875 xfs_fileoff_t endoffset_fsb;
3877 xfs_fsblock_t firstfsb;
3878 xfs_bmap_free_t free_list;
3879 xfs_bmbt_irec_t imap;
3887 xfs_fileoff_t startoffset_fsb;
3889 int need_iolock = 1;
3894 xfs_itrace_entry(ip);
3896 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
3900 if (len <= 0) /* if nothing being freed */
3902 rt = XFS_IS_REALTIME_INODE(ip);
3903 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
3904 end_dmi_offset = offset + len;
3905 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
3907 if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
3908 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
3909 if (end_dmi_offset > ip->i_size)
3910 end_dmi_offset = ip->i_size;
3911 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, ip,
3912 offset, end_dmi_offset - offset,
3913 AT_DELAY_FLAG(attr_flags), NULL);
3918 if (attr_flags & ATTR_NOLOCK)
3921 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3922 vn_iowait(ip); /* wait for the completion of any pending DIOs */
3925 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
3926 ioffset = offset & ~(rounding - 1);
3928 if (VN_CACHED(vp) != 0) {
3929 xfs_inval_cached_trace(ip, ioffset, -1, ioffset, -1);
3930 error = xfs_flushinval_pages(ip, ioffset, -1, FI_REMAPF_LOCKED);
3932 goto out_unlock_iolock;
3936 * Need to zero the stuff we're not freeing, on disk.
3937 * If its a realtime file & can't use unwritten extents then we
3938 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3939 * will take care of it for us.
3941 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
3943 error = xfs_bmapi(NULL, ip, startoffset_fsb,
3944 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
3946 goto out_unlock_iolock;
3947 ASSERT(nimap == 0 || nimap == 1);
3948 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3951 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3952 block = imap.br_startblock;
3953 mod = do_div(block, mp->m_sb.sb_rextsize);
3955 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
3958 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1,
3959 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
3961 goto out_unlock_iolock;
3962 ASSERT(nimap == 0 || nimap == 1);
3963 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
3964 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
3966 if (mod && (mod != mp->m_sb.sb_rextsize))
3967 endoffset_fsb -= mod;
3970 if ((done = (endoffset_fsb <= startoffset_fsb)))
3972 * One contiguous piece to clear
3974 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
3977 * Some full blocks, possibly two pieces to clear
3979 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
3980 error = xfs_zero_remaining_bytes(ip, offset,
3981 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
3983 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
3984 error = xfs_zero_remaining_bytes(ip,
3985 XFS_FSB_TO_B(mp, endoffset_fsb),
3990 * free file space until done or until there is an error
3992 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
3993 while (!error && !done) {
3996 * allocate and setup the transaction. Allow this
3997 * transaction to dip into the reserve blocks to ensure
3998 * the freeing of the space succeeds at ENOSPC.
4000 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4001 tp->t_flags |= XFS_TRANS_RESERVE;
4002 error = xfs_trans_reserve(tp,
4004 XFS_WRITE_LOG_RES(mp),
4006 XFS_TRANS_PERM_LOG_RES,
4007 XFS_WRITE_LOG_COUNT);
4010 * check for running out of space
4014 * Free the transaction structure.
4016 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4017 xfs_trans_cancel(tp, 0);
4020 xfs_ilock(ip, XFS_ILOCK_EXCL);
4021 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4022 ip->i_udquot, ip->i_gdquot, resblks, 0,
4023 XFS_QMOPT_RES_REGBLKS);
4027 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4028 xfs_trans_ihold(tp, ip);
4031 * issue the bunmapi() call to free the blocks
4033 XFS_BMAP_INIT(&free_list, &firstfsb);
4034 error = xfs_bunmapi(tp, ip, startoffset_fsb,
4035 endoffset_fsb - startoffset_fsb,
4036 0, 2, &firstfsb, &free_list, NULL, &done);
4042 * complete the transaction
4044 error = xfs_bmap_finish(&tp, &free_list, &committed);
4049 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
4050 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4055 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4059 xfs_bmap_cancel(&free_list);
4061 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4062 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4068 * xfs_change_file_space()
4069 * This routine allocates or frees disk space for the given file.
4070 * The user specified parameters are checked for alignment and size
4079 xfs_change_file_space(
4087 xfs_mount_t *mp = ip->i_mount;
4092 xfs_off_t startoffset;
4097 xfs_itrace_entry(ip);
4099 if (!S_ISREG(ip->i_d.di_mode))
4100 return XFS_ERROR(EINVAL);
4102 switch (bf->l_whence) {
4103 case 0: /*SEEK_SET*/
4105 case 1: /*SEEK_CUR*/
4106 bf->l_start += offset;
4108 case 2: /*SEEK_END*/
4109 bf->l_start += ip->i_size;
4112 return XFS_ERROR(EINVAL);
4115 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4117 if ( (bf->l_start < 0)
4118 || (bf->l_start > XFS_MAXIOFFSET(mp))
4119 || (bf->l_start + llen < 0)
4120 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4121 return XFS_ERROR(EINVAL);
4125 startoffset = bf->l_start;
4129 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4131 * These calls do NOT zero the data space allocated to the file,
4132 * nor do they change the file size.
4134 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4136 * These calls cause the new file data to be zeroed and the file
4137 * size to be changed.
4139 setprealloc = clrprealloc = 0;
4142 case XFS_IOC_RESVSP:
4143 case XFS_IOC_RESVSP64:
4144 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4151 case XFS_IOC_UNRESVSP:
4152 case XFS_IOC_UNRESVSP64:
4153 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4158 case XFS_IOC_ALLOCSP:
4159 case XFS_IOC_ALLOCSP64:
4160 case XFS_IOC_FREESP:
4161 case XFS_IOC_FREESP64:
4162 if (startoffset > fsize) {
4163 error = xfs_alloc_file_space(ip, fsize,
4164 startoffset - fsize, 0, attr_flags);
4169 va.va_mask = XFS_AT_SIZE;
4170 va.va_size = startoffset;
4172 error = xfs_setattr(ip, &va, attr_flags, credp);
4182 return XFS_ERROR(EINVAL);
4186 * update the inode timestamp, mode, and prealloc flag bits
4188 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4190 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4193 xfs_trans_cancel(tp, 0);
4197 xfs_ilock(ip, XFS_ILOCK_EXCL);
4199 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4200 xfs_trans_ihold(tp, ip);
4202 if ((attr_flags & ATTR_DMI) == 0) {
4203 ip->i_d.di_mode &= ~S_ISUID;
4206 * Note that we don't have to worry about mandatory
4207 * file locking being disabled here because we only
4208 * clear the S_ISGID bit if the Group execute bit is
4209 * on, but if it was on then mandatory locking wouldn't
4210 * have been enabled.
4212 if (ip->i_d.di_mode & S_IXGRP)
4213 ip->i_d.di_mode &= ~S_ISGID;
4215 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4218 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4219 else if (clrprealloc)
4220 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4222 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4223 xfs_trans_set_sync(tp);
4225 error = xfs_trans_commit(tp, 0);
4227 xfs_iunlock(ip, XFS_ILOCK_EXCL);