2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_types.h"
25 #include "xfs_trans.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_da_btree.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_inode_item.h"
40 #include "xfs_itable.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_alloc.h"
47 #include "xfs_error.h"
48 #include "xfs_quota.h"
49 #include "xfs_utils.h"
50 #include "xfs_rtalloc.h"
51 #include "xfs_refcache.h"
52 #include "xfs_trans_space.h"
53 #include "xfs_log_priv.h"
62 bhv_vnode_t *vp = BHV_TO_VNODE(bdp);
63 xfs_inode_t *ip = XFS_BHVTOI(bdp);
65 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
66 return XFS_ERROR(EIO);
69 * If it's a directory with any blocks, read-ahead block 0
70 * as we're almost certain to have the next operation be a read there.
72 if (VN_ISDIR(vp) && ip->i_d.di_nextents > 0) {
73 mode = xfs_ilock_map_shared(ip);
74 if (ip->i_d.di_nextents > 0)
75 (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
76 xfs_iunlock(ip, mode);
85 lastclose_t lastclose,
88 bhv_vnode_t *vp = BHV_TO_VNODE(bdp);
89 xfs_inode_t *ip = XFS_BHVTOI(bdp);
91 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
92 return XFS_ERROR(EIO);
94 if (lastclose != L_TRUE || !VN_ISREG(vp))
98 * If we previously truncated this file and removed old data in
99 * the process, we want to initiate "early" writeout on the last
100 * close. This is an attempt to combat the notorious NULL files
101 * problem which is particularly noticable from a truncate down,
102 * buffered (re-)write (delalloc), followed by a crash. What we
103 * are effectively doing here is significantly reducing the time
104 * window where we'd otherwise be exposed to that problem.
106 if (VUNTRUNCATE(vp) && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
107 return bhv_vop_flush_pages(vp, 0, -1, XFS_B_ASYNC, FI_NONE);
125 vp = BHV_TO_VNODE(bdp);
126 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
128 ip = XFS_BHVTOI(bdp);
131 if (XFS_FORCED_SHUTDOWN(mp))
132 return XFS_ERROR(EIO);
134 if (!(flags & ATTR_LAZY))
135 xfs_ilock(ip, XFS_ILOCK_SHARED);
137 vap->va_size = ip->i_d.di_size;
138 if (vap->va_mask == XFS_AT_SIZE)
142 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
143 vap->va_nodeid = ip->i_ino;
145 vap->va_nodeid += mp->m_inoadd;
147 vap->va_nlink = ip->i_d.di_nlink;
150 * Quick exit for non-stat callers
153 ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
154 XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
158 * Copy from in-core inode.
160 vap->va_mode = ip->i_d.di_mode;
161 vap->va_uid = ip->i_d.di_uid;
162 vap->va_gid = ip->i_d.di_gid;
163 vap->va_xid = ip->i_d.di_xid;
164 vap->va_projid = ip->i_d.di_projid;
167 * Check vnode type block/char vs. everything else.
169 switch (ip->i_d.di_mode & S_IFMT) {
172 vap->va_rdev = ip->i_df.if_u2.if_rdev;
173 vap->va_blocksize = BLKDEV_IOSIZE;
178 if (!(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
179 vap->va_blocksize = xfs_preferred_iosize(mp);
183 * If the file blocks are being allocated from a
184 * realtime partition, then return the inode's
185 * realtime extent size or the realtime volume's
188 vap->va_blocksize = ip->i_d.di_extsize ?
189 (ip->i_d.di_extsize << mp->m_sb.sb_blocklog) :
190 (mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog);
195 vn_atime_to_timespec(vp, &vap->va_atime);
196 vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
197 vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
198 vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
199 vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
202 * Exit for stat callers. See if any of the rest of the fields
203 * to be filled in are needed.
206 (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
207 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
211 * Convert di_flags to xflags.
213 vap->va_xflags = xfs_ip2xflags(ip);
216 * Exit for inode revalidate. See if any of the rest of
217 * the fields to be filled in are needed.
220 (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
221 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
224 vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
226 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
227 ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
231 (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
232 ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
233 ip->i_d.di_anextents;
235 vap->va_anextents = 0;
236 vap->va_gen = ip->i_d.di_gen;
239 if (!(flags & ATTR_LAZY))
240 xfs_iunlock(ip, XFS_ILOCK_SHARED);
267 xfs_prid_t projid=0, iprojid=0;
268 int mandlock_before, mandlock_after;
269 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
273 vp = BHV_TO_VNODE(bdp);
274 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
276 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
277 return XFS_ERROR(EROFS);
280 * Cannot set certain attributes.
283 if (mask & XFS_AT_NOSET) {
284 return XFS_ERROR(EINVAL);
287 ip = XFS_BHVTOI(bdp);
290 if (XFS_FORCED_SHUTDOWN(mp))
291 return XFS_ERROR(EIO);
294 * Timestamps do not need to be logged and hence do not
295 * need to be done within a transaction.
297 if (mask & XFS_AT_UPDTIMES) {
298 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
299 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
300 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
301 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
302 xfs_ichgtime(ip, timeflags);
306 olddquot1 = olddquot2 = NULL;
310 * If disk quotas is on, we make sure that the dquots do exist on disk,
311 * before we start any other transactions. Trying to do this later
312 * is messy. We don't care to take a readlock to look at the ids
313 * in inode here, because we can't hold it across the trans_reserve.
314 * If the IDs do change before we take the ilock, we're covered
315 * because the i_*dquot fields will get updated anyway.
317 if (XFS_IS_QUOTA_ON(mp) &&
318 (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
321 /* FIXME: handle xid? */
322 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
324 qflags |= XFS_QMOPT_UQUOTA;
326 uid = ip->i_d.di_uid;
328 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
330 qflags |= XFS_QMOPT_GQUOTA;
332 gid = ip->i_d.di_gid;
334 if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
335 projid = vap->va_projid;
336 qflags |= XFS_QMOPT_PQUOTA;
338 projid = ip->i_d.di_projid;
341 * We take a reference when we initialize udqp and gdqp,
342 * so it is important that we never blindly double trip on
343 * the same variable. See xfs_create() for an example.
345 ASSERT(udqp == NULL);
346 ASSERT(gdqp == NULL);
347 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
354 * For the other attributes, we acquire the inode lock and
355 * first do an error checking pass.
358 lock_flags = XFS_ILOCK_EXCL;
359 if (flags & ATTR_NOLOCK)
361 if (!(mask & XFS_AT_SIZE)) {
362 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
363 (mp->m_flags & XFS_MOUNT_WSYNC)) {
364 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
366 if ((code = xfs_trans_reserve(tp, 0,
367 XFS_ICHANGE_LOG_RES(mp), 0,
374 if (DM_EVENT_ENABLED (vp->v_vfsp, ip, DM_EVENT_TRUNCATE) &&
375 !(flags & ATTR_DMI)) {
376 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
377 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
378 vap->va_size, 0, dmflags, NULL);
385 lock_flags |= XFS_IOLOCK_EXCL;
388 xfs_ilock(ip, lock_flags);
390 /* boolean: are we the file owner? */
391 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
394 * Change various properties of a file.
395 * Only the owner or users with CAP_FOWNER
396 * capability may do these things.
399 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
400 XFS_AT_GID|XFS_AT_PROJID)) {
401 /* FIXME: handle xid? */
404 * CAP_FOWNER overrides the following restrictions:
406 * The user ID of the calling process must be equal
407 * to the file owner ID, except in cases where the
408 * CAP_FSETID capability is applicable.
410 if (!file_owner && !capable(CAP_FOWNER)) {
411 code = XFS_ERROR(EPERM);
416 * CAP_FSETID overrides the following restrictions:
418 * The effective user ID of the calling process shall match
419 * the file owner when setting the set-user-ID and
420 * set-group-ID bits on that file.
422 * The effective group ID or one of the supplementary group
423 * IDs of the calling process shall match the group owner of
424 * the file when setting the set-group-ID bit on that file
426 if (mask & XFS_AT_MODE) {
429 if ((vap->va_mode & S_ISUID) && !file_owner)
431 if ((vap->va_mode & S_ISGID) &&
432 !in_group_p((gid_t)ip->i_d.di_gid))
435 /* Linux allows this, Irix doesn't. */
436 if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
439 if (m && !capable(CAP_FSETID))
445 * Change file ownership. Must be the owner or privileged.
446 * If the system was configured with the "restricted_chown"
447 * option, the owner is not permitted to give away the file,
448 * and can change the group id only to a group of which he
449 * or she is a member.
451 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_XID|XFS_AT_PROJID)) {
453 * These IDs could have changed since we last looked at them.
454 * But, we're assured that if the ownership did change
455 * while we didn't have the inode locked, inode's dquot(s)
456 * would have changed also.
458 iuid = ip->i_d.di_uid;
459 igid = ip->i_d.di_gid;
460 ixid = ip->i_d.di_xid;
461 iprojid = ip->i_d.di_projid;
462 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
463 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
464 xid = (mask & XFS_AT_XID) ? vap->va_xid : ixid;
465 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
469 * CAP_CHOWN overrides the following restrictions:
471 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
472 * shall override the restriction that a process cannot
473 * change the user ID of a file it owns and the restriction
474 * that the group ID supplied to the chown() function
475 * shall be equal to either the group ID or one of the
476 * supplementary group IDs of the calling process.
478 if (restricted_chown &&
479 (iuid != uid || (igid != gid &&
480 !in_group_p((gid_t)gid))) &&
481 !capable(CAP_CHOWN)) {
482 code = XFS_ERROR(EPERM);
486 * Do a quota reservation only if uid/projid/gid is actually
489 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
490 (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
491 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
492 /* FIXME: handle xid? */
494 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
495 capable(CAP_FOWNER) ?
496 XFS_QMOPT_FORCE_RES : 0);
497 if (code) /* out of quota */
503 * Truncate file. Must have write permission and not be a directory.
505 if (mask & XFS_AT_SIZE) {
506 /* Short circuit the truncate case for zero length files */
507 if ((vap->va_size == 0) &&
508 (ip->i_d.di_size == 0) && (ip->i_d.di_nextents == 0)) {
509 xfs_iunlock(ip, XFS_ILOCK_EXCL);
510 lock_flags &= ~XFS_ILOCK_EXCL;
511 if (mask & XFS_AT_CTIME)
512 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
518 code = XFS_ERROR(EISDIR);
520 } else if (!VN_ISREG(vp)) {
521 code = XFS_ERROR(EINVAL);
525 * Make sure that the dquots are attached to the inode.
527 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
532 * Change file access or modified times.
534 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
536 if ((flags & ATTR_UTIME) &&
537 !capable(CAP_FOWNER)) {
538 code = XFS_ERROR(EPERM);
545 * Change extent size or realtime flag.
547 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
549 * Can't change extent size if any extents are allocated.
551 if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
552 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
554 code = XFS_ERROR(EINVAL); /* EFBIG? */
559 * Can't change realtime flag if any extents are allocated.
561 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
562 (mask & XFS_AT_XFLAGS) &&
563 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) !=
564 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
565 code = XFS_ERROR(EINVAL); /* EFBIG? */
569 * Extent size must be a multiple of the appropriate block
570 * size, if set at all.
572 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
575 if ((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
576 ((mask & XFS_AT_XFLAGS) &&
577 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
578 size = mp->m_sb.sb_rextsize <<
579 mp->m_sb.sb_blocklog;
581 size = mp->m_sb.sb_blocksize;
583 if (vap->va_extsize % size) {
584 code = XFS_ERROR(EINVAL);
589 * If realtime flag is set then must have realtime data.
591 if ((mask & XFS_AT_XFLAGS) &&
592 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
593 if ((mp->m_sb.sb_rblocks == 0) ||
594 (mp->m_sb.sb_rextsize == 0) ||
595 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
596 code = XFS_ERROR(EINVAL);
602 * Can't modify an immutable/append-only file unless
603 * we have appropriate permission.
605 if ((mask & XFS_AT_XFLAGS) &&
607 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
609 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
610 !capable(CAP_LINUX_IMMUTABLE)) {
611 code = XFS_ERROR(EPERM);
617 * Now we can make the changes. Before we join the inode
618 * to the transaction, if XFS_AT_SIZE is set then take care of
619 * the part of the truncation that must be done without the
620 * inode lock. This needs to be done before joining the inode
621 * to the transaction, because the inode cannot be unlocked
622 * once it is a part of the transaction.
624 if (mask & XFS_AT_SIZE) {
626 if ((vap->va_size > ip->i_d.di_size) &&
627 (flags & ATTR_NOSIZETOK) == 0) {
628 code = xfs_igrow_start(ip, vap->va_size, credp);
630 xfs_iunlock(ip, XFS_ILOCK_EXCL);
631 vn_iowait(vp); /* wait for the completion of any pending DIOs */
633 code = xfs_itruncate_data(ip, vap->va_size);
636 lock_flags &= ~XFS_ILOCK_EXCL;
637 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
640 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
641 if ((code = xfs_trans_reserve(tp, 0,
642 XFS_ITRUNCATE_LOG_RES(mp), 0,
643 XFS_TRANS_PERM_LOG_RES,
644 XFS_ITRUNCATE_LOG_COUNT))) {
645 xfs_trans_cancel(tp, 0);
647 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
650 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
651 xfs_ilock(ip, XFS_ILOCK_EXCL);
655 xfs_trans_ijoin(tp, ip, lock_flags);
656 xfs_trans_ihold(tp, ip);
659 /* determine whether mandatory locking mode changes */
660 mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
663 * Truncate file. Must have write permission and not be a directory.
665 if (mask & XFS_AT_SIZE) {
666 if (vap->va_size > ip->i_d.di_size) {
667 xfs_igrow_finish(tp, ip, vap->va_size,
668 !(flags & ATTR_DMI));
669 } else if ((vap->va_size <= ip->i_d.di_size) ||
670 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
672 * signal a sync transaction unless
673 * we're truncating an already unlinked
674 * file on a wsync filesystem
676 code = xfs_itruncate_finish(&tp, ip,
677 (xfs_fsize_t)vap->va_size,
679 ((ip->i_d.di_nlink != 0 ||
680 !(mp->m_flags & XFS_MOUNT_WSYNC))
685 * Truncated "down", so we're removing references
686 * to old data here - if we now delay flushing for
687 * a long time, we expose ourselves unduly to the
688 * notorious NULL files problem. So, we mark this
689 * vnode and flush it when the file is closed, and
690 * do not wait the usual (long) time for writeout.
695 * Have to do this even if the file's size doesn't change.
697 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
701 * Change file access modes.
703 if (mask & XFS_AT_MODE) {
704 ip->i_d.di_mode &= S_IFMT;
705 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
707 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
708 timeflags |= XFS_ICHGTIME_CHG;
712 * Change file ownership. Must be the owner or privileged.
713 * If the system was configured with the "restricted_chown"
714 * option, the owner is not permitted to give away the file,
715 * and can change the group id only to a group of which he
716 * or she is a member.
718 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_XID|XFS_AT_PROJID)) {
720 * CAP_FSETID overrides the following restrictions:
722 * The set-user-ID and set-group-ID bits of a file will be
723 * cleared upon successful return from chown()
725 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
726 !capable(CAP_FSETID)) {
727 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
731 * Change the ownerships and register quota modifications
732 * in the transaction.
735 if (XFS_IS_GQUOTA_ON(mp)) {
736 /* FIXME: handle xid quota? */
738 ip->i_d.di_xid = xid;
741 if (XFS_IS_UQUOTA_ON(mp)) {
742 ASSERT(mask & XFS_AT_UID);
744 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
745 &ip->i_udquot, udqp);
747 ip->i_d.di_uid = uid;
750 if (XFS_IS_GQUOTA_ON(mp)) {
751 ASSERT(!XFS_IS_PQUOTA_ON(mp));
752 ASSERT(mask & XFS_AT_GID);
754 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
755 &ip->i_gdquot, gdqp);
757 ip->i_d.di_gid = gid;
759 if (iprojid != projid) {
760 if (XFS_IS_PQUOTA_ON(mp)) {
761 ASSERT(!XFS_IS_GQUOTA_ON(mp));
762 ASSERT(mask & XFS_AT_PROJID);
764 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
765 &ip->i_gdquot, gdqp);
767 ip->i_d.di_projid = projid;
769 * We may have to rev the inode as well as
770 * the superblock version number since projids didn't
771 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
773 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
774 xfs_bump_ino_vers2(tp, ip);
777 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
778 timeflags |= XFS_ICHGTIME_CHG;
783 * Change file access or modified times.
785 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
786 if (mask & XFS_AT_ATIME) {
787 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
788 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
789 ip->i_update_core = 1;
790 timeflags &= ~XFS_ICHGTIME_ACC;
792 if (mask & XFS_AT_MTIME) {
793 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
794 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
795 timeflags &= ~XFS_ICHGTIME_MOD;
796 timeflags |= XFS_ICHGTIME_CHG;
798 if (tp && (flags & ATTR_UTIME))
799 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
803 * Change XFS-added attributes.
805 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
806 if (mask & XFS_AT_EXTSIZE) {
808 * Converting bytes to fs blocks.
810 ip->i_d.di_extsize = vap->va_extsize >>
811 mp->m_sb.sb_blocklog;
813 if (mask & XFS_AT_XFLAGS) {
816 /* can't set PREALLOC this way, just preserve it */
817 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
818 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
819 di_flags |= XFS_DIFLAG_IMMUTABLE;
820 if (vap->va_xflags & XFS_XFLAG_IUNLINK)
821 di_flags |= XFS_DIFLAG_IUNLINK;
822 if (vap->va_xflags & XFS_XFLAG_BARRIER)
823 di_flags |= XFS_DIFLAG_BARRIER;
824 if (vap->va_xflags & XFS_XFLAG_APPEND)
825 di_flags |= XFS_DIFLAG_APPEND;
826 if (vap->va_xflags & XFS_XFLAG_SYNC)
827 di_flags |= XFS_DIFLAG_SYNC;
828 if (vap->va_xflags & XFS_XFLAG_NOATIME)
829 di_flags |= XFS_DIFLAG_NOATIME;
830 if (vap->va_xflags & XFS_XFLAG_NODUMP)
831 di_flags |= XFS_DIFLAG_NODUMP;
832 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
833 di_flags |= XFS_DIFLAG_PROJINHERIT;
834 if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
835 di_flags |= XFS_DIFLAG_NODEFRAG;
836 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
837 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
838 di_flags |= XFS_DIFLAG_RTINHERIT;
839 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
840 di_flags |= XFS_DIFLAG_NOSYMLINKS;
841 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
842 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
843 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
844 if (vap->va_xflags & XFS_XFLAG_REALTIME) {
845 di_flags |= XFS_DIFLAG_REALTIME;
846 ip->i_iocore.io_flags |= XFS_IOCORE_RT;
848 ip->i_iocore.io_flags &= ~XFS_IOCORE_RT;
850 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
851 di_flags |= XFS_DIFLAG_EXTSIZE;
853 ip->i_d.di_flags = di_flags;
855 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
856 timeflags |= XFS_ICHGTIME_CHG;
860 * Change file inode change time only if XFS_AT_CTIME set
861 * AND we have been called by a DMI function.
864 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
865 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
866 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
867 ip->i_update_core = 1;
868 timeflags &= ~XFS_ICHGTIME_CHG;
872 * Send out timestamp changes that need to be set to the
873 * current time. Not done when called by a DMI function.
875 if (timeflags && !(flags & ATTR_DMI))
876 xfs_ichgtime(ip, timeflags);
878 XFS_STATS_INC(xs_ig_attrchg);
881 * If this is a synchronous mount, make sure that the
882 * transaction goes to disk before returning to the user.
883 * This is slightly sub-optimal in that truncates require
884 * two sync transactions instead of one for wsync filesystems.
885 * One for the truncate and one for the timestamps since we
886 * don't want to change the timestamps unless we're sure the
887 * truncate worked. Truncates are less than 1% of the laddis
888 * mix so this probably isn't worth the trouble to optimize.
892 if (mp->m_flags & XFS_MOUNT_WSYNC)
893 xfs_trans_set_sync(tp);
895 code = xfs_trans_commit(tp, commit_flags, NULL);
899 * If the (regular) file's mandatory locking mode changed, then
900 * notify the vnode. We do this under the inode lock to prevent
901 * racing calls to vop_vnode_change.
903 mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
904 if (mandlock_before != mandlock_after) {
905 bhv_vop_vnode_change(vp, VCHANGE_FLAGS_ENF_LOCKING,
909 xfs_iunlock(ip, lock_flags);
912 * Release any dquot(s) the inode had kept before chown.
914 XFS_QM_DQRELE(mp, olddquot1);
915 XFS_QM_DQRELE(mp, olddquot2);
916 XFS_QM_DQRELE(mp, udqp);
917 XFS_QM_DQRELE(mp, gdqp);
923 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_ATTRIBUTE) &&
924 !(flags & ATTR_DMI)) {
925 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
926 NULL, DM_RIGHT_NULL, NULL, NULL,
927 0, 0, AT_DELAY_FLAG(flags));
932 commit_flags |= XFS_TRANS_ABORT;
935 XFS_QM_DQRELE(mp, udqp);
936 XFS_QM_DQRELE(mp, gdqp);
938 xfs_trans_cancel(tp, commit_flags);
940 if (lock_flags != 0) {
941 xfs_iunlock(ip, lock_flags);
949 * Null conversion from vnode mode bits to inode mode bits, as in efs.
960 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
961 (inst_t *)__return_address);
963 ip = XFS_BHVTOI(bdp);
964 xfs_ilock(ip, XFS_ILOCK_SHARED);
965 error = xfs_iaccess(ip, mode, credp);
966 xfs_iunlock(ip, XFS_ILOCK_SHARED);
972 * The maximum pathlen is 1024 bytes. Since the minimum file system
973 * blocksize is 512 bytes, we can get a max of 2 extents back from
976 #define SYMLINK_MAPS 2
997 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1003 vp = BHV_TO_VNODE(bdp);
1004 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1006 ip = XFS_BHVTOI(bdp);
1009 if (XFS_FORCED_SHUTDOWN(mp))
1010 return XFS_ERROR(EIO);
1012 xfs_ilock(ip, XFS_ILOCK_SHARED);
1014 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
1016 offset = uiop->uio_offset;
1017 count = uiop->uio_resid;
1020 error = XFS_ERROR(EINVAL);
1029 * See if the symlink is stored inline.
1031 pathlen = (int)ip->i_d.di_size;
1033 if (ip->i_df.if_flags & XFS_IFINLINE) {
1034 error = uio_read(ip->i_df.if_u1.if_data, pathlen, uiop);
1038 * Symlink not inline. Call bmap to get it in.
1040 nmaps = SYMLINK_MAPS;
1042 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen),
1043 0, NULL, 0, mval, &nmaps, NULL, NULL);
1049 for (n = 0; n < nmaps; n++) {
1050 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1051 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1052 bp = xfs_buf_read(mp->m_ddev_targp, d,
1053 BTOBB(byte_cnt), 0);
1054 error = XFS_BUF_GETERROR(bp);
1056 xfs_ioerror_alert("xfs_readlink",
1057 ip->i_mount, bp, XFS_BUF_ADDR(bp));
1061 if (pathlen < byte_cnt)
1063 pathlen -= byte_cnt;
1065 error = uio_read(XFS_BUF_PTR(bp), byte_cnt, uiop);
1072 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1080 * This is called to sync the inode and its data out to disk.
1081 * We need to hold the I/O lock while flushing the data, and
1082 * the inode lock while flushing the inode. The inode lock CANNOT
1083 * be held while flushing the data, so acquire after we're done
1097 int log_flushed = 0, changed = 1;
1099 vn_trace_entry(BHV_TO_VNODE(bdp),
1100 __FUNCTION__, (inst_t *)__return_address);
1102 ip = XFS_BHVTOI(bdp);
1104 ASSERT(start >= 0 && stop >= -1);
1106 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1107 return XFS_ERROR(EIO);
1110 * We always need to make sure that the required inode state
1111 * is safe on disk. The vnode might be clean but because
1112 * of committed transactions that haven't hit the disk yet.
1113 * Likewise, there could be unflushed non-transactional
1114 * changes to the inode core that have to go to disk.
1116 * The following code depends on one assumption: that
1117 * any transaction that changes an inode logs the core
1118 * because it has to change some field in the inode core
1119 * (typically nextents or nblocks). That assumption
1120 * implies that any transactions against an inode will
1121 * catch any non-transactional updates. If inode-altering
1122 * transactions exist that violate this assumption, the
1123 * code breaks. Right now, it figures that if the involved
1124 * update_* field is clear and the inode is unpinned, the
1125 * inode is clean. Either it's been flushed or it's been
1126 * committed and the commit has hit the disk unpinning the inode.
1127 * (Note that xfs_inode_item_format() called at commit clears
1128 * the update_* fields.)
1130 xfs_ilock(ip, XFS_ILOCK_SHARED);
1132 /* If we are flushing data then we care about update_size
1133 * being set, otherwise we care about update_core
1135 if ((flag & FSYNC_DATA) ?
1136 (ip->i_update_size == 0) :
1137 (ip->i_update_core == 0)) {
1139 * Timestamps/size haven't changed since last inode
1140 * flush or inode transaction commit. That means
1141 * either nothing got written or a transaction
1142 * committed which caught the updates. If the
1143 * latter happened and the transaction hasn't
1144 * hit the disk yet, the inode will be still
1145 * be pinned. If it is, force the log.
1148 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1150 if (xfs_ipincount(ip)) {
1151 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
1153 ((flag & FSYNC_WAIT)
1154 ? XFS_LOG_SYNC : 0),
1158 * If the inode is not pinned and nothing
1159 * has changed we don't need to flush the
1167 * Kick off a transaction to log the inode
1168 * core to get the updates. Make it
1169 * sync if FSYNC_WAIT is passed in (which
1170 * is done by everybody but specfs). The
1171 * sync transaction will also force the log.
1173 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1174 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1175 if ((error = xfs_trans_reserve(tp, 0,
1176 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1178 xfs_trans_cancel(tp, 0);
1181 xfs_ilock(ip, XFS_ILOCK_EXCL);
1184 * Note - it's possible that we might have pushed
1185 * ourselves out of the way during trans_reserve
1186 * which would flush the inode. But there's no
1187 * guarantee that the inode buffer has actually
1188 * gone out yet (it's delwri). Plus the buffer
1189 * could be pinned anyway if it's part of an
1190 * inode in another recent transaction. So we
1191 * play it safe and fire off the transaction anyway.
1193 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1194 xfs_trans_ihold(tp, ip);
1195 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1196 if (flag & FSYNC_WAIT)
1197 xfs_trans_set_sync(tp);
1198 error = _xfs_trans_commit(tp, 0, NULL, &log_flushed);
1200 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1203 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1205 * If the log write didn't issue an ordered tag we need
1206 * to flush the disk cache for the data device now.
1209 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1212 * If this inode is on the RT dev we need to flush that
1215 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
1216 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1223 * This is called by xfs_inactive to free any blocks beyond eof,
1224 * when the link count isn't zero.
1227 xfs_inactive_free_eofblocks(
1233 xfs_fileoff_t end_fsb;
1234 xfs_fileoff_t last_fsb;
1235 xfs_filblks_t map_len;
1237 xfs_bmbt_irec_t imap;
1240 * Figure out if there are any blocks beyond the end
1241 * of the file. If not, then there is nothing to do.
1243 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_d.di_size));
1244 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1245 map_len = last_fsb - end_fsb;
1250 xfs_ilock(ip, XFS_ILOCK_SHARED);
1251 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, end_fsb, map_len, 0,
1252 NULL, 0, &imap, &nimaps, NULL, NULL);
1253 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1255 if (!error && (nimaps != 0) &&
1256 (imap.br_startblock != HOLESTARTBLOCK ||
1257 ip->i_delayed_blks)) {
1259 * Attach the dquots to the inode up front.
1261 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1265 * There are blocks after the end of file.
1266 * Free them up now by truncating the file to
1269 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1272 * Do the xfs_itruncate_start() call before
1273 * reserving any log space because
1274 * itruncate_start will call into the buffer
1275 * cache and we can't
1276 * do that within a transaction.
1278 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1279 xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
1282 error = xfs_trans_reserve(tp, 0,
1283 XFS_ITRUNCATE_LOG_RES(mp),
1284 0, XFS_TRANS_PERM_LOG_RES,
1285 XFS_ITRUNCATE_LOG_COUNT);
1287 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1288 xfs_trans_cancel(tp, 0);
1289 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1293 xfs_ilock(ip, XFS_ILOCK_EXCL);
1294 xfs_trans_ijoin(tp, ip,
1297 xfs_trans_ihold(tp, ip);
1299 error = xfs_itruncate_finish(&tp, ip,
1304 * If we get an error at this point we
1305 * simply don't bother truncating the file.
1308 xfs_trans_cancel(tp,
1309 (XFS_TRANS_RELEASE_LOG_RES |
1312 error = xfs_trans_commit(tp,
1313 XFS_TRANS_RELEASE_LOG_RES,
1316 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1322 * Free a symlink that has blocks associated with it.
1325 xfs_inactive_symlink_rmt(
1333 xfs_fsblock_t first_block;
1334 xfs_bmap_free_t free_list;
1337 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1345 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1347 * We're freeing a symlink that has some
1348 * blocks allocated to it. Free the
1349 * blocks here. We know that we've got
1350 * either 1 or 2 extents and that we can
1351 * free them all in one bunmapi call.
1353 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1354 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1355 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1356 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1357 xfs_trans_cancel(tp, 0);
1362 * Lock the inode, fix the size, and join it to the transaction.
1363 * Hold it so in the normal path, we still have it locked for
1364 * the second transaction. In the error paths we need it
1365 * held so the cancel won't rele it, see below.
1367 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1368 size = (int)ip->i_d.di_size;
1369 ip->i_d.di_size = 0;
1370 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1371 xfs_trans_ihold(tp, ip);
1372 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1374 * Find the block(s) so we can inval and unmap them.
1377 XFS_BMAP_INIT(&free_list, &first_block);
1378 nmaps = ARRAY_SIZE(mval);
1379 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1380 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
1384 * Invalidate the block(s).
1386 for (i = 0; i < nmaps; i++) {
1387 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1388 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1389 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1390 xfs_trans_binval(tp, bp);
1393 * Unmap the dead block(s) to the free_list.
1395 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
1396 &first_block, &free_list, NULL, &done)))
1400 * Commit the first transaction. This logs the EFI and the inode.
1402 if ((error = xfs_bmap_finish(&tp, &free_list, first_block, &committed)))
1405 * The transaction must have been committed, since there were
1406 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1407 * The new tp has the extent freeing and EFDs.
1411 * The first xact was committed, so add the inode to the new one.
1412 * Mark it dirty so it will be logged and moved forward in the log as
1413 * part of every commit.
1415 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1416 xfs_trans_ihold(tp, ip);
1417 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1419 * Get a new, empty transaction to return to our caller.
1421 ntp = xfs_trans_dup(tp);
1423 * Commit the transaction containing extent freeing and EFDs.
1424 * If we get an error on the commit here or on the reserve below,
1425 * we need to unlock the inode since the new transaction doesn't
1426 * have the inode attached.
1428 error = xfs_trans_commit(tp, 0, NULL);
1431 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1435 * Remove the memory for extent descriptions (just bookkeeping).
1437 if (ip->i_df.if_bytes)
1438 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1439 ASSERT(ip->i_df.if_bytes == 0);
1441 * Put an itruncate log reservation in the new transaction
1444 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1445 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1446 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1450 * Return with the inode locked but not joined to the transaction.
1456 xfs_bmap_cancel(&free_list);
1459 * Have to come here with the inode locked and either
1460 * (held and in the transaction) or (not in the transaction).
1461 * If the inode isn't held then cancel would iput it, but
1462 * that's wrong since this is inactive and the vnode ref
1463 * count is 0 already.
1464 * Cancel won't do anything to the inode if held, but it still
1465 * needs to be locked until the cancel is done, if it was
1466 * joined to the transaction.
1468 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1469 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1476 xfs_inactive_symlink_local(
1482 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1484 * We're freeing a symlink which fit into
1485 * the inode. Just free the memory used
1486 * to hold the old symlink.
1488 error = xfs_trans_reserve(*tpp, 0,
1489 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1490 0, XFS_TRANS_PERM_LOG_RES,
1491 XFS_ITRUNCATE_LOG_COUNT);
1494 xfs_trans_cancel(*tpp, 0);
1498 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1501 * Zero length symlinks _can_ exist.
1503 if (ip->i_df.if_bytes > 0) {
1504 xfs_idata_realloc(ip,
1505 -(ip->i_df.if_bytes),
1507 ASSERT(ip->i_df.if_bytes == 0);
1521 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1524 ASSERT(ip->i_d.di_forkoff != 0);
1525 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
1526 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1528 error = xfs_attr_inactive(ip);
1531 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1532 return error; /* goto out */
1535 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1536 error = xfs_trans_reserve(tp, 0,
1537 XFS_IFREE_LOG_RES(mp),
1538 0, XFS_TRANS_PERM_LOG_RES,
1539 XFS_INACTIVE_LOG_COUNT);
1541 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1542 xfs_trans_cancel(tp, 0);
1544 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1548 xfs_ilock(ip, XFS_ILOCK_EXCL);
1549 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1550 xfs_trans_ihold(tp, ip);
1551 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1553 ASSERT(ip->i_d.di_anextents == 0);
1568 vp = BHV_TO_VNODE(bdp);
1569 ip = XFS_BHVTOI(bdp);
1572 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
1575 /* If this is a read-only mount, don't do this (would generate I/O) */
1576 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1579 #ifdef HAVE_REFCACHE
1580 /* If we are in the NFS reference cache then don't do this now */
1585 if (ip->i_d.di_nlink != 0) {
1586 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1587 ((ip->i_d.di_size > 0) || (VN_CACHED(vp) > 0 ||
1588 ip->i_delayed_blks > 0)) &&
1589 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1590 (!(ip->i_d.di_flags &
1591 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1592 if ((error = xfs_inactive_free_eofblocks(mp, ip)))
1594 /* Update linux inode block count after free above */
1595 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1596 ip->i_d.di_nblocks + ip->i_delayed_blks);
1606 * This is called when the vnode reference count for the vnode
1607 * goes to zero. If the file has been unlinked, then it must
1608 * now be truncated. Also, we clear all of the read-ahead state
1609 * kept for the inode here since the file is now closed.
1618 xfs_bmap_free_t free_list;
1619 xfs_fsblock_t first_block;
1626 vp = BHV_TO_VNODE(bdp);
1627 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1629 ip = XFS_BHVTOI(bdp);
1632 * If the inode is already free, then there can be nothing
1635 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1636 ASSERT(ip->i_df.if_real_bytes == 0);
1637 ASSERT(ip->i_df.if_broot_bytes == 0);
1638 return VN_INACTIVE_CACHE;
1642 * Only do a truncate if it's a regular file with
1643 * some actual space in it. It's OK to look at the
1644 * inode's fields without the lock because we're the
1645 * only one with a reference to the inode.
1647 truncate = ((ip->i_d.di_nlink == 0) &&
1648 ((ip->i_d.di_size != 0) || (ip->i_d.di_nextents > 0) ||
1649 (ip->i_delayed_blks > 0)) &&
1650 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1654 if (ip->i_d.di_nlink == 0 &&
1655 DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_DESTROY)) {
1656 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1661 /* If this is a read-only mount, don't do this (would generate I/O) */
1662 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1665 if (ip->i_d.di_nlink != 0) {
1666 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1667 ((ip->i_d.di_size > 0) || (VN_CACHED(vp) > 0 ||
1668 ip->i_delayed_blks > 0)) &&
1669 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1670 (!(ip->i_d.di_flags &
1671 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1672 (ip->i_delayed_blks != 0)))) {
1673 if ((error = xfs_inactive_free_eofblocks(mp, ip)))
1674 return VN_INACTIVE_CACHE;
1675 /* Update linux inode block count after free above */
1676 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1677 ip->i_d.di_nblocks + ip->i_delayed_blks);
1682 ASSERT(ip->i_d.di_nlink == 0);
1684 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1685 return VN_INACTIVE_CACHE;
1687 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1690 * Do the xfs_itruncate_start() call before
1691 * reserving any log space because itruncate_start
1692 * will call into the buffer cache and we can't
1693 * do that within a transaction.
1695 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1697 xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1699 error = xfs_trans_reserve(tp, 0,
1700 XFS_ITRUNCATE_LOG_RES(mp),
1701 0, XFS_TRANS_PERM_LOG_RES,
1702 XFS_ITRUNCATE_LOG_COUNT);
1704 /* Don't call itruncate_cleanup */
1705 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1706 xfs_trans_cancel(tp, 0);
1707 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1708 return VN_INACTIVE_CACHE;
1711 xfs_ilock(ip, XFS_ILOCK_EXCL);
1712 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1713 xfs_trans_ihold(tp, ip);
1716 * normally, we have to run xfs_itruncate_finish sync.
1717 * But if filesystem is wsync and we're in the inactive
1718 * path, then we know that nlink == 0, and that the
1719 * xaction that made nlink == 0 is permanently committed
1720 * since xfs_remove runs as a synchronous transaction.
1722 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1723 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1726 xfs_trans_cancel(tp,
1727 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1728 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1729 return VN_INACTIVE_CACHE;
1731 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1734 * If we get an error while cleaning up a
1735 * symlink we bail out.
1737 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1738 xfs_inactive_symlink_rmt(ip, &tp) :
1739 xfs_inactive_symlink_local(ip, &tp);
1743 return VN_INACTIVE_CACHE;
1746 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1747 xfs_trans_ihold(tp, ip);
1749 error = xfs_trans_reserve(tp, 0,
1750 XFS_IFREE_LOG_RES(mp),
1751 0, XFS_TRANS_PERM_LOG_RES,
1752 XFS_INACTIVE_LOG_COUNT);
1754 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1755 xfs_trans_cancel(tp, 0);
1756 return VN_INACTIVE_CACHE;
1759 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1760 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1761 xfs_trans_ihold(tp, ip);
1765 * If there are attributes associated with the file
1766 * then blow them away now. The code calls a routine
1767 * that recursively deconstructs the attribute fork.
1768 * We need to just commit the current transaction
1769 * because we can't use it for xfs_attr_inactive().
1771 if (ip->i_d.di_anextents > 0) {
1772 error = xfs_inactive_attrs(ip, &tp);
1774 * If we got an error, the transaction is already
1775 * cancelled, and the inode is unlocked. Just get out.
1778 return VN_INACTIVE_CACHE;
1779 } else if (ip->i_afp) {
1780 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1786 XFS_BMAP_INIT(&free_list, &first_block);
1787 error = xfs_ifree(tp, ip, &free_list);
1790 * If we fail to free the inode, shut down. The cancel
1791 * might do that, we need to make sure. Otherwise the
1792 * inode might be lost for a long time or forever.
1794 if (!XFS_FORCED_SHUTDOWN(mp)) {
1796 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1797 error, mp->m_fsname);
1798 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1800 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1803 * Credit the quota account(s). The inode is gone.
1805 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1808 * Just ignore errors at this point. There is
1809 * nothing we can do except to try to keep going.
1811 (void) xfs_bmap_finish(&tp, &free_list, first_block,
1813 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
1816 * Release the dquots held by inode, if any.
1818 XFS_QM_DQDETACH(mp, ip);
1820 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1823 return VN_INACTIVE_CACHE;
1832 bhv_desc_t *dir_bdp,
1833 bhv_vname_t *dentry,
1839 xfs_inode_t *dp, *ip;
1843 bhv_vnode_t *dir_vp;
1845 dir_vp = BHV_TO_VNODE(dir_bdp);
1846 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1848 dp = XFS_BHVTOI(dir_bdp);
1850 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1851 return XFS_ERROR(EIO);
1853 lock_mode = xfs_ilock_map_shared(dp);
1854 error = xfs_dir_lookup_int(dir_bdp, lock_mode, dentry, &e_inum, &ip);
1856 *vpp = XFS_ITOV(ip);
1859 xfs_iunlock_map_shared(dp, lock_mode);
1865 * xfs_create (create a new file).
1869 bhv_desc_t *dir_bdp,
1870 bhv_vname_t *dentry,
1875 char *name = VNAME(dentry);
1876 bhv_vnode_t *dir_vp;
1877 xfs_inode_t *dp, *ip;
1878 bhv_vnode_t *vp = NULL;
1883 xfs_bmap_free_t free_list;
1884 xfs_fsblock_t first_block;
1885 boolean_t dp_joined_to_trans;
1886 int dm_event_sent = 0;
1890 struct xfs_dquot *udqp, *gdqp;
1896 dir_vp = BHV_TO_VNODE(dir_bdp);
1897 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1899 dp = XFS_BHVTOI(dir_bdp);
1902 dm_di_mode = vap->va_mode;
1903 namelen = VNAMELEN(dentry);
1905 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
1906 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1907 dir_vp, DM_RIGHT_NULL, NULL,
1908 DM_RIGHT_NULL, name, NULL,
1916 if (XFS_FORCED_SHUTDOWN(mp))
1917 return XFS_ERROR(EIO);
1919 /* Return through std_return after this point. */
1922 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1923 prid = dp->i_d.di_projid;
1924 else if (vap->va_mask & XFS_AT_PROJID)
1925 prid = (xfs_prid_t)vap->va_projid;
1927 prid = (xfs_prid_t)dfltprid;
1930 * Make sure that we have allocated dquot(s) on disk.
1932 error = XFS_QM_DQVOPALLOC(mp, dp,
1933 current_fsuid(credp), current_fsgid(credp), prid,
1934 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1939 dp_joined_to_trans = B_FALSE;
1941 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1942 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1943 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1945 * Initially assume that the file does not exist and
1946 * reserve the resources for that case. If that is not
1947 * the case we'll drop the one we have and get a more
1948 * appropriate transaction later.
1950 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1951 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1952 if (error == ENOSPC) {
1954 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1955 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1963 xfs_ilock(dp, XFS_ILOCK_EXCL);
1965 XFS_BMAP_INIT(&free_list, &first_block);
1970 * Reserve disk quota and the inode.
1972 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1976 if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
1978 rdev = (vap->va_mask & XFS_AT_RDEV) ? vap->va_rdev : 0;
1979 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 1,
1980 rdev, credp, prid, resblks > 0,
1983 if (error == ENOSPC)
1990 * At this point, we've gotten a newly allocated inode.
1991 * It is locked (and joined to the transaction).
1994 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1997 * Now we join the directory inode to the transaction.
1998 * We do not do it earlier because xfs_dir_ialloc
1999 * might commit the previous transaction (and release
2004 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2005 dp_joined_to_trans = B_TRUE;
2007 error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
2008 &first_block, &free_list, resblks ?
2009 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2011 ASSERT(error != ENOSPC);
2014 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2015 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2018 * If this is a synchronous mount, make sure that the
2019 * create transaction goes to disk before returning to
2022 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2023 xfs_trans_set_sync(tp);
2029 * Attach the dquot(s) to the inodes and modify them incore.
2030 * These ids of the inode couldn't have changed since the new
2031 * inode has been locked ever since it was created.
2033 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2036 * xfs_trans_commit normally decrements the vnode ref count
2037 * when it unlocks the inode. Since we want to return the
2038 * vnode to the caller, we bump the vnode ref count now.
2043 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2045 xfs_bmap_cancel(&free_list);
2049 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2056 XFS_QM_DQRELE(mp, udqp);
2057 XFS_QM_DQRELE(mp, gdqp);
2060 * Propagate the fact that the vnode changed after the
2061 * xfs_inode locks have been released.
2063 bhv_vop_vnode_change(vp, VCHANGE_FLAGS_TRUNCATED, 3);
2067 /* Fallthrough to std_return with error = 0 */
2070 if ( (*vpp || (error != 0 && dm_event_sent != 0)) &&
2071 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2072 DM_EVENT_POSTCREATE)) {
2073 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2074 dir_vp, DM_RIGHT_NULL,
2076 DM_RIGHT_NULL, name, NULL,
2077 dm_di_mode, error, 0);
2082 cancel_flags |= XFS_TRANS_ABORT;
2087 xfs_trans_cancel(tp, cancel_flags);
2089 if (!dp_joined_to_trans && (dp != NULL))
2090 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2091 XFS_QM_DQRELE(mp, udqp);
2092 XFS_QM_DQRELE(mp, gdqp);
2098 * Wait until after the current transaction is aborted to
2099 * release the inode. This prevents recursive transactions
2100 * and deadlocks from xfs_inactive.
2102 cancel_flags |= XFS_TRANS_ABORT;
2103 xfs_trans_cancel(tp, cancel_flags);
2106 XFS_QM_DQRELE(mp, udqp);
2107 XFS_QM_DQRELE(mp, gdqp);
2114 * Some counters to see if (and how often) we are hitting some deadlock
2115 * prevention code paths.
2119 int xfs_rm_lock_delays;
2120 int xfs_rm_attempts;
2124 * The following routine will lock the inodes associated with the
2125 * directory and the named entry in the directory. The locks are
2126 * acquired in increasing inode number.
2128 * If the entry is "..", then only the directory is locked. The
2129 * vnode ref count will still include that from the .. entry in
2132 * There is a deadlock we need to worry about. If the locked directory is
2133 * in the AIL, it might be blocking up the log. The next inode we lock
2134 * could be already locked by another thread waiting for log space (e.g
2135 * a permanent log reservation with a long running transaction (see
2136 * xfs_itruncate_finish)). To solve this, we must check if the directory
2137 * is in the ail and use lock_nowait. If we can't lock, we need to
2138 * drop the inode lock on the directory and try again. xfs_iunlock will
2139 * potentially push the tail if we were holding up the log.
2142 xfs_lock_dir_and_entry(
2144 bhv_vname_t *dentry,
2145 xfs_inode_t *ip) /* inode of entry 'name' */
2149 xfs_inode_t *ips[2];
2158 xfs_ilock(dp, XFS_ILOCK_EXCL);
2165 * We want to lock in increasing inum. Since we've already
2166 * acquired the lock on the directory, we may need to release
2167 * if if the inum of the entry turns out to be less.
2169 if (e_inum > dp->i_ino) {
2171 * We are already in the right order, so just
2172 * lock on the inode of the entry.
2173 * We need to use nowait if dp is in the AIL.
2176 lp = (xfs_log_item_t *)dp->i_itemp;
2177 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2178 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2185 * Unlock dp and try again.
2186 * xfs_iunlock will try to push the tail
2187 * if the inode is in the AIL.
2190 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2192 if ((attempts % 5) == 0) {
2193 delay(1); /* Don't just spin the CPU */
2195 xfs_rm_lock_delays++;
2201 xfs_ilock(ip, XFS_ILOCK_EXCL);
2203 } else if (e_inum < dp->i_ino) {
2204 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2208 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2210 /* else e_inum == dp->i_ino */
2211 /* This can happen if we're asked to lock /x/..
2212 * the entry is "..", which is also the parent directory.
2220 int xfs_small_retries;
2221 int xfs_middle_retries;
2222 int xfs_lots_retries;
2223 int xfs_lock_delays;
2227 * The following routine will lock n inodes in exclusive mode.
2228 * We assume the caller calls us with the inodes in i_ino order.
2230 * We need to detect deadlock where an inode that we lock
2231 * is in the AIL and we start waiting for another inode that is locked
2232 * by a thread in a long running transaction (such as truncate). This can
2233 * result in deadlock since the long running trans might need to wait
2234 * for the inode we just locked in order to push the tail and free space
2244 int attempts = 0, i, j, try_lock;
2247 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2258 for (; i < inodes; i++) {
2261 if (i && (ips[i] == ips[i-1])) /* Already locked */
2265 * If try_lock is not set yet, make sure all locked inodes
2266 * are not in the AIL.
2267 * If any are, set try_lock to be used later.
2271 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2272 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2273 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2280 * If any of the previous locks we have locked is in the AIL,
2281 * we must TRY to get the second and subsequent locks. If
2282 * we can't get any, we must release all we have
2287 /* try_lock must be 0 if i is 0. */
2289 * try_lock means we have an inode locked
2290 * that is in the AIL.
2293 if (!xfs_ilock_nowait(ips[i], lock_mode)) {
2297 * Unlock all previous guys and try again.
2298 * xfs_iunlock will try to push the tail
2299 * if the inode is in the AIL.
2302 for(j = i - 1; j >= 0; j--) {
2305 * Check to see if we've already
2306 * unlocked this one.
2307 * Not the first one going back,
2308 * and the inode ptr is the same.
2310 if ((j != (i - 1)) && ips[j] ==
2314 xfs_iunlock(ips[j], lock_mode);
2317 if ((attempts % 5) == 0) {
2318 delay(1); /* Don't just spin the CPU */
2328 xfs_ilock(ips[i], lock_mode);
2334 if (attempts < 5) xfs_small_retries++;
2335 else if (attempts < 100) xfs_middle_retries++;
2336 else xfs_lots_retries++;
2344 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2345 int remove_which_error_return = 0;
2347 #define REMOVE_DEBUG_TRACE(x)
2348 #endif /* ! DEBUG */
2357 bhv_desc_t *dir_bdp,
2358 bhv_vname_t *dentry,
2361 bhv_vnode_t *dir_vp;
2362 char *name = VNAME(dentry);
2363 xfs_inode_t *dp, *ip;
2364 xfs_trans_t *tp = NULL;
2367 xfs_bmap_free_t free_list;
2368 xfs_fsblock_t first_block;
2376 dir_vp = BHV_TO_VNODE(dir_bdp);
2377 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2379 dp = XFS_BHVTOI(dir_bdp);
2382 if (XFS_FORCED_SHUTDOWN(mp))
2383 return XFS_ERROR(EIO);
2385 namelen = VNAMELEN(dentry);
2387 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
2388 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2389 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2390 name, NULL, 0, 0, 0);
2395 /* From this point on, return through std_return */
2399 * We need to get a reference to ip before we get our log
2400 * reservation. The reason for this is that we cannot call
2401 * xfs_iget for an inode for which we do not have a reference
2402 * once we've acquired a log reservation. This is because the
2403 * inode we are trying to get might be in xfs_inactive going
2404 * for a log reservation. Since we'll have to wait for the
2405 * inactive code to complete before returning from xfs_iget,
2406 * we need to make sure that we don't have log space reserved
2407 * when we call xfs_iget. Instead we get an unlocked reference
2408 * to the inode before getting our log reservation.
2410 error = xfs_get_dir_entry(dentry, &ip);
2412 REMOVE_DEBUG_TRACE(__LINE__);
2416 dm_di_mode = ip->i_d.di_mode;
2418 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2422 error = XFS_QM_DQATTACH(mp, dp, 0);
2423 if (!error && dp != ip)
2424 error = XFS_QM_DQATTACH(mp, ip, 0);
2426 REMOVE_DEBUG_TRACE(__LINE__);
2431 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2432 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2434 * We try to get the real space reservation first,
2435 * allowing for directory btree deletion(s) implying
2436 * possible bmap insert(s). If we can't get the space
2437 * reservation then we use 0 instead, and avoid the bmap
2438 * btree insert(s) in the directory code by, if the bmap
2439 * insert tries to happen, instead trimming the LAST
2440 * block from the directory.
2442 resblks = XFS_REMOVE_SPACE_RES(mp);
2443 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2444 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2445 if (error == ENOSPC) {
2447 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2448 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2451 ASSERT(error != ENOSPC);
2452 REMOVE_DEBUG_TRACE(__LINE__);
2453 xfs_trans_cancel(tp, 0);
2458 error = xfs_lock_dir_and_entry(dp, dentry, ip);
2460 REMOVE_DEBUG_TRACE(__LINE__);
2461 xfs_trans_cancel(tp, cancel_flags);
2467 * At this point, we've gotten both the directory and the entry
2470 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2473 * Increment vnode ref count only in this case since
2474 * there's an extra vnode reference in the case where
2478 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2482 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2484 XFS_BMAP_INIT(&free_list, &first_block);
2485 error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2486 &first_block, &free_list, 0);
2488 ASSERT(error != ENOENT);
2489 REMOVE_DEBUG_TRACE(__LINE__);
2492 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2495 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2497 error = xfs_droplink(tp, ip);
2499 REMOVE_DEBUG_TRACE(__LINE__);
2503 /* Determine if this is the last link while
2504 * we are in the transaction.
2506 link_zero = (ip)->i_d.di_nlink==0;
2509 * Take an extra ref on the inode so that it doesn't
2510 * go to xfs_inactive() from within the commit.
2515 * If this is a synchronous mount, make sure that the
2516 * remove transaction goes to disk before returning to
2519 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2520 xfs_trans_set_sync(tp);
2523 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2525 REMOVE_DEBUG_TRACE(__LINE__);
2529 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2536 * Before we drop our extra reference to the inode, purge it
2537 * from the refcache if it is there. By waiting until afterwards
2538 * to do the IRELE, we ensure that we won't go inactive in the
2539 * xfs_refcache_purge_ip routine (although that would be OK).
2541 xfs_refcache_purge_ip(ip);
2543 vn_trace_exit(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2546 * Let interposed file systems know about removed links.
2548 bhv_vop_link_removed(XFS_ITOV(ip), dir_vp, link_zero);
2552 /* Fall through to std_return with error = 0 */
2554 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp,
2555 DM_EVENT_POSTREMOVE)) {
2556 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2557 dir_vp, DM_RIGHT_NULL,
2558 NULL, DM_RIGHT_NULL,
2559 name, NULL, dm_di_mode, error, 0);
2564 xfs_bmap_cancel(&free_list);
2565 cancel_flags |= XFS_TRANS_ABORT;
2566 xfs_trans_cancel(tp, cancel_flags);
2571 * In this case make sure to not release the inode until after
2572 * the current transaction is aborted. Releasing it beforehand
2573 * can cause us to go to xfs_inactive and start a recursive
2574 * transaction which can easily deadlock with the current one.
2576 xfs_bmap_cancel(&free_list);
2577 cancel_flags |= XFS_TRANS_ABORT;
2578 xfs_trans_cancel(tp, cancel_flags);
2581 * Before we drop our extra reference to the inode, purge it
2582 * from the refcache if it is there. By waiting until afterwards
2583 * to do the IRELE, we ensure that we won't go inactive in the
2584 * xfs_refcache_purge_ip routine (although that would be OK).
2586 xfs_refcache_purge_ip(ip);
2600 bhv_desc_t *target_dir_bdp,
2601 bhv_vnode_t *src_vp,
2602 bhv_vname_t *dentry,
2605 xfs_inode_t *tdp, *sip;
2608 xfs_inode_t *ips[2];
2610 xfs_bmap_free_t free_list;
2611 xfs_fsblock_t first_block;
2614 bhv_vnode_t *target_dir_vp;
2616 char *target_name = VNAME(dentry);
2619 target_dir_vp = BHV_TO_VNODE(target_dir_bdp);
2620 vn_trace_entry(target_dir_vp, __FUNCTION__, (inst_t *)__return_address);
2621 vn_trace_entry(src_vp, __FUNCTION__, (inst_t *)__return_address);
2623 target_namelen = VNAMELEN(dentry);
2624 ASSERT(!VN_ISDIR(src_vp));
2626 sip = xfs_vtoi(src_vp);
2627 tdp = XFS_BHVTOI(target_dir_bdp);
2629 if (XFS_FORCED_SHUTDOWN(mp))
2630 return XFS_ERROR(EIO);
2632 if (DM_EVENT_ENABLED(src_vp->v_vfsp, tdp, DM_EVENT_LINK)) {
2633 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2634 target_dir_vp, DM_RIGHT_NULL,
2635 src_vp, DM_RIGHT_NULL,
2636 target_name, NULL, 0, 0, 0);
2641 /* Return through std_return after this point. */
2643 error = XFS_QM_DQATTACH(mp, sip, 0);
2644 if (!error && sip != tdp)
2645 error = XFS_QM_DQATTACH(mp, tdp, 0);
2649 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2650 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2651 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2652 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2653 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2654 if (error == ENOSPC) {
2656 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2657 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2664 if (sip->i_ino < tdp->i_ino) {
2672 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2675 * Increment vnode ref counts since xfs_trans_commit &
2676 * xfs_trans_cancel will both unlock the inodes and
2677 * decrement the associated ref counts.
2680 VN_HOLD(target_dir_vp);
2681 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2682 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2685 * If the source has too many links, we can't make any more to it.
2687 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2688 error = XFS_ERROR(EMLINK);
2693 * If we are using project inheritance, we only allow hard link
2694 * creation in our tree when the project IDs are the same; else
2695 * the tree quota mechanism could be circumvented.
2697 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2698 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2699 error = XFS_ERROR(EXDEV);
2704 (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
2707 XFS_BMAP_INIT(&free_list, &first_block);
2709 error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
2710 sip->i_ino, &first_block, &free_list,
2714 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2716 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2718 error = xfs_bumplink(tp, sip);
2723 * If this is a synchronous mount, make sure that the
2724 * link transaction goes to disk before returning to
2727 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2728 xfs_trans_set_sync(tp);
2731 error = xfs_bmap_finish (&tp, &free_list, first_block, &committed);
2733 xfs_bmap_cancel(&free_list);
2737 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2741 /* Fall through to std_return with error = 0. */
2743 if (DM_EVENT_ENABLED(src_vp->v_vfsp, sip,
2744 DM_EVENT_POSTLINK)) {
2745 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2746 target_dir_vp, DM_RIGHT_NULL,
2747 src_vp, DM_RIGHT_NULL,
2748 target_name, NULL, 0, error, 0);
2753 cancel_flags |= XFS_TRANS_ABORT;
2757 xfs_trans_cancel(tp, cancel_flags);
2768 bhv_desc_t *dir_bdp,
2769 bhv_vname_t *dentry,
2774 char *dir_name = VNAME(dentry);
2776 xfs_inode_t *cdp; /* inode of created dir */
2777 bhv_vnode_t *cvp; /* vnode of created dir */
2783 xfs_bmap_free_t free_list;
2784 xfs_fsblock_t first_block;
2785 bhv_vnode_t *dir_vp;
2786 boolean_t dp_joined_to_trans;
2787 boolean_t created = B_FALSE;
2788 int dm_event_sent = 0;
2790 struct xfs_dquot *udqp, *gdqp;
2795 dir_vp = BHV_TO_VNODE(dir_bdp);
2796 dp = XFS_BHVTOI(dir_bdp);
2799 if (XFS_FORCED_SHUTDOWN(mp))
2800 return XFS_ERROR(EIO);
2802 dir_namelen = VNAMELEN(dentry);
2805 dp_joined_to_trans = B_FALSE;
2806 dm_di_mode = vap->va_mode;
2808 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
2809 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2810 dir_vp, DM_RIGHT_NULL, NULL,
2811 DM_RIGHT_NULL, dir_name, NULL,
2818 /* Return through std_return after this point. */
2820 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2824 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2825 prid = dp->i_d.di_projid;
2826 else if (vap->va_mask & XFS_AT_PROJID)
2827 prid = (xfs_prid_t)vap->va_projid;
2829 prid = (xfs_prid_t)dfltprid;
2832 * Make sure that we have allocated dquot(s) on disk.
2834 error = XFS_QM_DQVOPALLOC(mp, dp,
2835 current_fsuid(credp), current_fsgid(credp), prid,
2836 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2840 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2841 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2842 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2843 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2844 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2845 if (error == ENOSPC) {
2847 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2848 XFS_TRANS_PERM_LOG_RES,
2849 XFS_MKDIR_LOG_COUNT);
2857 xfs_ilock(dp, XFS_ILOCK_EXCL);
2860 * Check for directory link count overflow.
2862 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2863 error = XFS_ERROR(EMLINK);
2868 * Reserve disk quota and the inode.
2870 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2875 (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
2878 * create the directory inode.
2880 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 2,
2881 0, credp, prid, resblks > 0,
2884 if (error == ENOSPC)
2891 * Now we add the directory inode to the transaction.
2892 * We waited until now since xfs_dir_ialloc might start
2893 * a new transaction. Had we joined the transaction
2894 * earlier, the locks might have gotten released.
2897 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2898 dp_joined_to_trans = B_TRUE;
2900 XFS_BMAP_INIT(&free_list, &first_block);
2902 error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2903 &first_block, &free_list, resblks ?
2904 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2906 ASSERT(error != ENOSPC);
2909 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2912 * Bump the in memory version number of the parent directory
2913 * so that other processes accessing it will recognize that
2914 * the directory has changed.
2918 error = xfs_dir_init(tp, cdp, dp);
2923 error = xfs_bumplink(tp, dp);
2927 cvp = XFS_ITOV(cdp);
2935 * Attach the dquots to the new inode and modify the icount incore.
2937 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2940 * If this is a synchronous mount, make sure that the
2941 * mkdir transaction goes to disk before returning to
2944 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2945 xfs_trans_set_sync(tp);
2948 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2954 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2955 XFS_QM_DQRELE(mp, udqp);
2956 XFS_QM_DQRELE(mp, gdqp);
2961 /* Fall through to std_return with error = 0 or errno from
2962 * xfs_trans_commit. */
2965 if ( (created || (error != 0 && dm_event_sent != 0)) &&
2966 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2967 DM_EVENT_POSTCREATE)) {
2968 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2969 dir_vp, DM_RIGHT_NULL,
2970 created ? XFS_ITOV(cdp):NULL,
2973 dm_di_mode, error, 0);
2979 xfs_bmap_cancel(&free_list);
2981 cancel_flags |= XFS_TRANS_ABORT;
2983 xfs_trans_cancel(tp, cancel_flags);
2984 XFS_QM_DQRELE(mp, udqp);
2985 XFS_QM_DQRELE(mp, gdqp);
2987 if (!dp_joined_to_trans && (dp != NULL)) {
2988 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3001 bhv_desc_t *dir_bdp,
3002 bhv_vname_t *dentry,
3005 char *name = VNAME(dentry);
3007 xfs_inode_t *cdp; /* child directory */
3011 xfs_bmap_free_t free_list;
3012 xfs_fsblock_t first_block;
3015 bhv_vnode_t *dir_vp;
3021 dir_vp = BHV_TO_VNODE(dir_bdp);
3022 dp = XFS_BHVTOI(dir_bdp);
3025 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3027 if (XFS_FORCED_SHUTDOWN(XFS_BHVTOI(dir_bdp)->i_mount))
3028 return XFS_ERROR(EIO);
3029 namelen = VNAMELEN(dentry);
3031 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
3032 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
3033 dir_vp, DM_RIGHT_NULL,
3034 NULL, DM_RIGHT_NULL,
3035 name, NULL, 0, 0, 0);
3037 return XFS_ERROR(error);
3040 /* Return through std_return after this point. */
3045 * We need to get a reference to cdp before we get our log
3046 * reservation. The reason for this is that we cannot call
3047 * xfs_iget for an inode for which we do not have a reference
3048 * once we've acquired a log reservation. This is because the
3049 * inode we are trying to get might be in xfs_inactive going
3050 * for a log reservation. Since we'll have to wait for the
3051 * inactive code to complete before returning from xfs_iget,
3052 * we need to make sure that we don't have log space reserved
3053 * when we call xfs_iget. Instead we get an unlocked reference
3054 * to the inode before getting our log reservation.
3056 error = xfs_get_dir_entry(dentry, &cdp);
3058 REMOVE_DEBUG_TRACE(__LINE__);
3062 dm_di_mode = cdp->i_d.di_mode;
3065 * Get the dquots for the inodes.
3067 error = XFS_QM_DQATTACH(mp, dp, 0);
3068 if (!error && dp != cdp)
3069 error = XFS_QM_DQATTACH(mp, cdp, 0);
3072 REMOVE_DEBUG_TRACE(__LINE__);
3076 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
3077 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3079 * We try to get the real space reservation first,
3080 * allowing for directory btree deletion(s) implying
3081 * possible bmap insert(s). If we can't get the space
3082 * reservation then we use 0 instead, and avoid the bmap
3083 * btree insert(s) in the directory code by, if the bmap
3084 * insert tries to happen, instead trimming the LAST
3085 * block from the directory.
3087 resblks = XFS_REMOVE_SPACE_RES(mp);
3088 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
3089 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3090 if (error == ENOSPC) {
3092 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
3093 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3096 ASSERT(error != ENOSPC);
3101 XFS_BMAP_INIT(&free_list, &first_block);
3104 * Now lock the child directory inode and the parent directory
3105 * inode in the proper order. This will take care of validating
3106 * that the directory entry for the child directory inode has
3107 * not changed while we were obtaining a log reservation.
3109 error = xfs_lock_dir_and_entry(dp, dentry, cdp);
3111 xfs_trans_cancel(tp, cancel_flags);
3116 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3119 * Only increment the parent directory vnode count if
3120 * we didn't bump it in looking up cdp. The only time
3121 * we don't bump it is when we're looking up ".".
3127 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3129 ASSERT(cdp->i_d.di_nlink >= 2);
3130 if (cdp->i_d.di_nlink != 2) {
3131 error = XFS_ERROR(ENOTEMPTY);
3134 if (!xfs_dir_isempty(cdp)) {
3135 error = XFS_ERROR(ENOTEMPTY);
3139 error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
3140 &first_block, &free_list, resblks);
3144 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3147 * Bump the in memory generation count on the parent
3148 * directory so that other can know that it has changed.
3153 * Drop the link from cdp's "..".
3155 error = xfs_droplink(tp, dp);
3161 * Drop the link from dp to cdp.
3163 error = xfs_droplink(tp, cdp);
3169 * Drop the "." link from cdp to self.
3171 error = xfs_droplink(tp, cdp);
3176 /* Determine these before committing transaction */
3177 last_cdp_link = (cdp)->i_d.di_nlink==0;
3180 * Take an extra ref on the child vnode so that it
3181 * does not go to xfs_inactive() from within the commit.
3186 * If this is a synchronous mount, make sure that the
3187 * rmdir transaction goes to disk before returning to
3190 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3191 xfs_trans_set_sync(tp);
3194 error = xfs_bmap_finish (&tp, &free_list, first_block, &committed);
3196 xfs_bmap_cancel(&free_list);
3197 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3203 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
3211 * Let interposed file systems know about removed links.
3213 bhv_vop_link_removed(XFS_ITOV(cdp), dir_vp, last_cdp_link);
3217 /* Fall through to std_return with error = 0 or the errno
3218 * from xfs_trans_commit. */
3220 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_POSTREMOVE)) {
3221 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3222 dir_vp, DM_RIGHT_NULL,
3223 NULL, DM_RIGHT_NULL,
3224 name, NULL, dm_di_mode,
3230 xfs_bmap_cancel(&free_list);
3231 cancel_flags |= XFS_TRANS_ABORT;
3235 xfs_trans_cancel(tp, cancel_flags);
3241 * Read dp's entries starting at uiop->uio_offset and translate them into
3242 * bufsize bytes worth of struct dirents starting at bufbase.
3246 bhv_desc_t *dir_bdp,
3252 xfs_trans_t *tp = NULL;
3256 vn_trace_entry(BHV_TO_VNODE(dir_bdp), __FUNCTION__,
3257 (inst_t *)__return_address);
3258 dp = XFS_BHVTOI(dir_bdp);
3260 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
3261 return XFS_ERROR(EIO);
3263 lock_mode = xfs_ilock_map_shared(dp);
3264 error = xfs_dir_getdents(tp, dp, uiop, eofp);
3265 xfs_iunlock_map_shared(dp, lock_mode);
3272 bhv_desc_t *dir_bdp,
3273 bhv_vname_t *dentry,
3285 xfs_bmap_free_t free_list;
3286 xfs_fsblock_t first_block;
3287 boolean_t dp_joined_to_trans;
3288 bhv_vnode_t *dir_vp;
3291 xfs_fileoff_t first_fsb;
3292 xfs_filblks_t fs_blocks;
3294 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3301 struct xfs_dquot *udqp, *gdqp;
3303 char *link_name = VNAME(dentry);
3307 dir_vp = BHV_TO_VNODE(dir_bdp);
3308 dp = XFS_BHVTOI(dir_bdp);
3309 dp_joined_to_trans = B_FALSE;
3314 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3318 if (XFS_FORCED_SHUTDOWN(mp))
3319 return XFS_ERROR(EIO);
3321 link_namelen = VNAMELEN(dentry);
3324 * Check component lengths of the target path name.
3326 pathlen = strlen(target_path);
3327 if (pathlen >= MAXPATHLEN) /* total string too long */
3328 return XFS_ERROR(ENAMETOOLONG);
3329 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3333 for (total = 0, path = target_path; total < pathlen;) {
3337 while(*path == '/') {
3343 * Count up to the next slash or end of path.
3344 * Error out if the component is bigger than MAXNAMELEN.
3346 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3347 if (++len >= MAXNAMELEN) {
3348 error = ENAMETOOLONG;
3355 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_SYMLINK)) {
3356 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3357 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3358 link_name, target_path, 0, 0, 0);
3363 /* Return through std_return after this point. */
3366 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3367 prid = dp->i_d.di_projid;
3368 else if (vap->va_mask & XFS_AT_PROJID)
3369 prid = (xfs_prid_t)vap->va_projid;
3371 prid = (xfs_prid_t)dfltprid;
3374 * Make sure that we have allocated dquot(s) on disk.
3376 error = XFS_QM_DQVOPALLOC(mp, dp,
3377 current_fsuid(credp), current_fsgid(credp), prid,
3378 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3382 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3383 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3385 * The symlink will fit into the inode data fork?
3386 * There can't be any attributes so we get the whole variable part.
3388 if (pathlen <= XFS_LITINO(mp))
3391 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3392 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3393 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3394 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3395 if (error == ENOSPC && fs_blocks == 0) {
3397 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3398 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3406 xfs_ilock(dp, XFS_ILOCK_EXCL);
3409 * Check whether the directory allows new symlinks or not.
3411 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3412 error = XFS_ERROR(EPERM);
3417 * Reserve disk quota : blocks and inode.
3419 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3424 * Check for ability to enter directory entry, if no space reserved.
3427 (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
3430 * Initialize the bmap freelist prior to calling either
3431 * bmapi or the directory create code.
3433 XFS_BMAP_INIT(&free_list, &first_block);
3436 * Allocate an inode for the symlink.
3438 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (vap->va_mode&~S_IFMT),
3439 1, 0, credp, prid, resblks > 0, &ip, NULL);
3441 if (error == ENOSPC)
3448 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3449 dp_joined_to_trans = B_TRUE;
3452 * Also attach the dquot(s) to it, if applicable.
3454 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3457 resblks -= XFS_IALLOC_SPACE_RES(mp);
3459 * If the symlink will fit into the inode, write it inline.
3461 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3462 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3463 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3464 ip->i_d.di_size = pathlen;
3467 * The inode was initially created in extent format.
3469 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3470 ip->i_df.if_flags |= XFS_IFINLINE;
3472 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3473 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3477 nmaps = SYMLINK_MAPS;
3479 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3480 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3481 &first_block, resblks, mval, &nmaps,
3488 resblks -= fs_blocks;
3489 ip->i_d.di_size = pathlen;
3490 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3492 cur_chunk = target_path;
3493 for (n = 0; n < nmaps; n++) {
3494 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3495 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3496 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3497 BTOBB(byte_cnt), 0);
3498 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3499 if (pathlen < byte_cnt) {
3502 pathlen -= byte_cnt;
3504 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3505 cur_chunk += byte_cnt;
3507 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3512 * Create the directory entry for the symlink.
3514 error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
3515 &first_block, &free_list, resblks);
3518 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3519 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3522 * Bump the in memory version number of the parent directory
3523 * so that other processes accessing it will recognize that
3524 * the directory has changed.
3529 * If this is a synchronous mount, make sure that the
3530 * symlink transaction goes to disk before returning to
3533 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3534 xfs_trans_set_sync(tp);
3538 * xfs_trans_commit normally decrements the vnode ref count
3539 * when it unlocks the inode. Since we want to return the
3540 * vnode to the caller, we bump the vnode ref count now.
3544 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
3548 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
3549 XFS_QM_DQRELE(mp, udqp);
3550 XFS_QM_DQRELE(mp, gdqp);
3552 /* Fall through to std_return with error = 0 or errno from
3553 * xfs_trans_commit */
3555 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
3556 DM_EVENT_POSTSYMLINK)) {
3557 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3558 dir_vp, DM_RIGHT_NULL,
3559 error ? NULL : XFS_ITOV(ip),
3560 DM_RIGHT_NULL, link_name, target_path,
3576 xfs_bmap_cancel(&free_list);
3577 cancel_flags |= XFS_TRANS_ABORT;
3579 xfs_trans_cancel(tp, cancel_flags);
3580 XFS_QM_DQRELE(mp, udqp);
3581 XFS_QM_DQRELE(mp, gdqp);
3583 if (!dp_joined_to_trans && (dp != NULL)) {
3584 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3594 * A fid routine that takes a pointer to a previously allocated
3595 * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
3605 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
3606 (inst_t *)__return_address);
3607 ASSERT(sizeof(fid_t) >= sizeof(xfs_fid2_t));
3609 xfid = (xfs_fid2_t *)fidp;
3610 ip = XFS_BHVTOI(bdp);
3611 xfid->fid_len = sizeof(xfs_fid2_t) - sizeof(xfid->fid_len);
3614 * use memcpy because the inode is a long long and there's no
3615 * assurance that xfid->fid_ino is properly aligned.
3617 memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
3618 xfid->fid_gen = ip->i_d.di_gen;
3630 bhv_vrwlock_t locktype)
3635 vp = BHV_TO_VNODE(bdp);
3638 ip = XFS_BHVTOI(bdp);
3639 if (locktype == VRWLOCK_WRITE) {
3640 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3641 } else if (locktype == VRWLOCK_TRY_READ) {
3642 return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
3643 } else if (locktype == VRWLOCK_TRY_WRITE) {
3644 return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
3646 ASSERT((locktype == VRWLOCK_READ) ||
3647 (locktype == VRWLOCK_WRITE_DIRECT));
3648 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3661 bhv_vrwlock_t locktype)
3666 vp = BHV_TO_VNODE(bdp);
3669 ip = XFS_BHVTOI(bdp);
3670 if (locktype == VRWLOCK_WRITE) {
3672 * In the write case, we may have added a new entry to
3673 * the reference cache. This might store a pointer to
3674 * an inode to be released in this inode. If it is there,
3675 * clear the pointer and release the inode after unlocking
3678 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3680 ASSERT((locktype == VRWLOCK_READ) ||
3681 (locktype == VRWLOCK_WRITE_DIRECT));
3682 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3694 xfs_inode_log_item_t *iip;
3697 ip = XFS_BHVTOI(bdp);
3701 if (XFS_FORCED_SHUTDOWN(mp))
3702 return XFS_ERROR(EIO);
3705 * Bypass inodes which have already been cleaned by
3706 * the inode flush clustering code inside xfs_iflush
3708 if ((ip->i_update_core == 0) &&
3709 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3712 if (flags & FLUSH_LOG) {
3713 if (iip && iip->ili_last_lsn) {
3714 xlog_t *log = mp->m_log;
3716 int s, log_flags = XFS_LOG_FORCE;
3718 s = GRANT_LOCK(log);
3719 sync_lsn = log->l_last_sync_lsn;
3720 GRANT_UNLOCK(log, s);
3722 if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) <= 0))
3725 if (flags & FLUSH_SYNC)
3726 log_flags |= XFS_LOG_SYNC;
3727 return xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3732 * We make this non-blocking if the inode is contended,
3733 * return EAGAIN to indicate to the caller that they
3734 * did not succeed. This prevents the flush path from
3735 * blocking on inodes inside another operation right
3736 * now, they get caught later by xfs_sync.
3738 if (flags & FLUSH_INODE) {
3741 if (xfs_ipincount(ip))
3744 if (flags & FLUSH_SYNC) {
3745 xfs_ilock(ip, XFS_ILOCK_SHARED);
3747 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3748 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3749 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3756 if (flags & FLUSH_SYNC)
3757 flush_flags = XFS_IFLUSH_SYNC;
3759 flush_flags = XFS_IFLUSH_ASYNC;
3761 error = xfs_iflush(ip, flush_flags);
3762 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3780 if (!capable(CAP_SYS_ADMIN))
3781 return XFS_ERROR(EPERM);
3783 ip = XFS_BHVTOI(bdp);
3786 if (XFS_FORCED_SHUTDOWN(mp))
3787 return XFS_ERROR(EIO);
3789 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3790 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3792 xfs_trans_cancel(tp, 0);
3795 xfs_ilock(ip, XFS_ILOCK_EXCL);
3796 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3798 ip->i_iocore.io_dmevmask = ip->i_d.di_dmevmask = evmask;
3799 ip->i_iocore.io_dmstate = ip->i_d.di_dmstate = state;
3801 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3803 error = xfs_trans_commit(tp, 0, NULL);
3815 vp = BHV_TO_VNODE(bdp);
3816 ip = XFS_BHVTOI(bdp);
3818 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
3820 ASSERT(!VN_MAPPED(vp));
3822 /* bad inode, get out here ASAP */
3830 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
3833 * Make sure the atime in the XFS inode is correct before freeing the
3836 xfs_synchronize_atime(ip);
3838 /* If we have nothing to flush with this inode then complete the
3839 * teardown now, otherwise break the link between the xfs inode
3840 * and the linux inode and clean up the xfs inode later. This
3841 * avoids flushing the inode to disk during the delete operation
3844 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3845 xfs_ilock(ip, XFS_ILOCK_EXCL);
3847 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3849 xfs_mount_t *mp = ip->i_mount;
3851 /* Protect sync from us */
3852 XFS_MOUNT_ILOCK(mp);
3853 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
3854 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
3855 ip->i_flags |= XFS_IRECLAIMABLE;
3856 XFS_MOUNT_IUNLOCK(mp);
3867 xfs_ihash_t *ih = ip->i_hash;
3868 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
3871 if (vp && VN_BAD(vp))
3874 /* The hash lock here protects a thread in xfs_iget_core from
3875 * racing with us on linking the inode back with a vnode.
3876 * Once we have the XFS_IRECLAIM flag set it will not touch
3879 write_lock(&ih->ih_lock);
3880 if ((ip->i_flags & XFS_IRECLAIM) ||
3881 (!(ip->i_flags & XFS_IRECLAIMABLE) && vp == NULL)) {
3882 write_unlock(&ih->ih_lock);
3885 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3889 ip->i_flags |= XFS_IRECLAIM;
3890 write_unlock(&ih->ih_lock);
3893 * If the inode is still dirty, then flush it out. If the inode
3894 * is not in the AIL, then it will be OK to flush it delwri as
3895 * long as xfs_iflush() does not keep any references to the inode.
3896 * We leave that decision up to xfs_iflush() since it has the
3897 * knowledge of whether it's OK to simply do a delwri flush of
3898 * the inode or whether we need to wait until the inode is
3899 * pulled from the AIL.
3900 * We get the flush lock regardless, though, just to make sure
3901 * we don't free it while it is being flushed.
3903 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3905 xfs_ilock(ip, XFS_ILOCK_EXCL);
3909 if (ip->i_update_core ||
3910 ((ip->i_itemp != NULL) &&
3911 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3912 error = xfs_iflush(ip, sync_mode);
3914 * If we hit an error, typically because of filesystem
3915 * shutdown, we don't need to let vn_reclaim to know
3916 * because we're gonna reclaim the inode anyway.
3919 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3922 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3925 ASSERT(ip->i_update_core == 0);
3926 ASSERT(ip->i_itemp == NULL ||
3927 ip->i_itemp->ili_format.ilf_fields == 0);
3928 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3929 } else if (locked) {
3931 * We are not interested in doing an iflush if we're
3932 * in the process of shutting down the filesystem forcibly.
3933 * So, just reclaim the inode.
3936 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3945 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3948 xfs_inode_t *ip, *n;
3953 XFS_MOUNT_ILOCK(mp);
3954 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3956 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3958 if (xfs_ipincount(ip) ||
3959 !xfs_iflock_nowait(ip)) {
3960 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3964 XFS_MOUNT_IUNLOCK(mp);
3965 if (xfs_finish_reclaim(ip, noblock,
3966 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3975 XFS_MOUNT_IUNLOCK(mp);
3980 * xfs_alloc_file_space()
3981 * This routine allocates disk space for the given file.
3983 * If alloc_type == 0, this request is for an ALLOCSP type
3984 * request which will change the file size. In this case, no
3985 * DMAPI event will be generated by the call. A TRUNCATE event
3986 * will be generated later by xfs_setattr.
3988 * If alloc_type != 0, this request is for a RESVSP type
3989 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3990 * lower block boundary byte address is less than the file's
3999 xfs_alloc_file_space(
4006 xfs_mount_t *mp = ip->i_mount;
4008 xfs_filblks_t allocated_fsb;
4009 xfs_filblks_t allocatesize_fsb;
4010 xfs_extlen_t extsz, temp;
4011 xfs_fileoff_t startoffset_fsb;
4012 xfs_fsblock_t firstfsb;
4018 xfs_bmbt_irec_t imaps[1], *imapp;
4019 xfs_bmap_free_t free_list;
4020 uint qblocks, resblks, resrtextents;
4024 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
4026 if (XFS_FORCED_SHUTDOWN(mp))
4027 return XFS_ERROR(EIO);
4029 rt = XFS_IS_REALTIME_INODE(ip);
4031 if (!(extsz = ip->i_d.di_extsize))
4032 extsz = mp->m_sb.sb_rextsize;
4034 extsz = ip->i_d.di_extsize;
4037 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4041 return XFS_ERROR(EINVAL);
4047 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
4048 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
4049 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
4051 /* Generate a DMAPI event if needed. */
4052 if (alloc_type != 0 && offset < ip->i_d.di_size &&
4053 (attr_flags&ATTR_DMI) == 0 &&
4054 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4055 xfs_off_t end_dmi_offset;
4057 end_dmi_offset = offset+len;
4058 if (end_dmi_offset > ip->i_d.di_size)
4059 end_dmi_offset = ip->i_d.di_size;
4060 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
4061 offset, end_dmi_offset - offset,
4068 * Allocate file space until done or until there is an error
4071 while (allocatesize_fsb && !error) {
4075 * Determine space reservations for data/realtime.
4077 if (unlikely(extsz)) {
4078 s = startoffset_fsb;
4081 e = startoffset_fsb + allocatesize_fsb;
4082 if ((temp = do_mod(startoffset_fsb, extsz)))
4084 if ((temp = do_mod(e, extsz)))
4088 e = allocatesize_fsb;
4092 resrtextents = qblocks = (uint)(e - s);
4093 resrtextents /= mp->m_sb.sb_rextsize;
4094 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4095 quota_flag = XFS_QMOPT_RES_RTBLKS;
4098 resblks = qblocks = \
4099 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
4100 quota_flag = XFS_QMOPT_RES_REGBLKS;
4104 * Allocate and setup the transaction.
4106 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4107 error = xfs_trans_reserve(tp, resblks,
4108 XFS_WRITE_LOG_RES(mp), resrtextents,
4109 XFS_TRANS_PERM_LOG_RES,
4110 XFS_WRITE_LOG_COUNT);
4112 * Check for running out of space
4116 * Free the transaction structure.
4118 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4119 xfs_trans_cancel(tp, 0);
4122 xfs_ilock(ip, XFS_ILOCK_EXCL);
4123 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
4124 qblocks, 0, quota_flag);
4128 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4129 xfs_trans_ihold(tp, ip);
4132 * Issue the xfs_bmapi() call to allocate the blocks
4134 XFS_BMAP_INIT(&free_list, &firstfsb);
4135 error = XFS_BMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4136 allocatesize_fsb, bmapi_flag,
4137 &firstfsb, 0, imapp, &nimaps,
4144 * Complete the transaction
4146 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
4151 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
4152 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4157 allocated_fsb = imapp->br_blockcount;
4160 error = XFS_ERROR(ENOSPC);
4164 startoffset_fsb += allocated_fsb;
4165 allocatesize_fsb -= allocated_fsb;
4168 if (error == ENOSPC && (attr_flags&ATTR_DMI) == 0 &&
4169 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_NOSPACE)) {
4171 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
4172 XFS_ITOV(ip), DM_RIGHT_NULL,
4173 XFS_ITOV(ip), DM_RIGHT_NULL,
4174 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
4176 goto retry; /* Maybe DMAPI app. has made space */
4177 /* else fall through with error from XFS_SEND_DATA */
4182 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
4183 xfs_bmap_cancel(&free_list);
4184 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
4186 error1: /* Just cancel transaction */
4187 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4188 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4189 goto dmapi_enospc_check;
4193 * Zero file bytes between startoff and endoff inclusive.
4194 * The iolock is held exclusive and no blocks are buffered.
4197 xfs_zero_remaining_bytes(
4202 xfs_bmbt_irec_t imap;
4203 xfs_fileoff_t offset_fsb;
4204 xfs_off_t lastoffset;
4207 xfs_mount_t *mp = ip->i_mount;
4211 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
4212 ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
4213 mp->m_rtdev_targp : mp->m_ddev_targp);
4215 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4216 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4218 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, offset_fsb, 1, 0,
4219 NULL, 0, &imap, &nimap, NULL, NULL);
4220 if (error || nimap < 1)
4222 ASSERT(imap.br_blockcount >= 1);
4223 ASSERT(imap.br_startoff == offset_fsb);
4224 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4225 if (lastoffset > endoff)
4226 lastoffset = endoff;
4227 if (imap.br_startblock == HOLESTARTBLOCK)
4229 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4230 if (imap.br_state == XFS_EXT_UNWRITTEN)
4233 XFS_BUF_UNWRITE(bp);
4235 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4237 if ((error = xfs_iowait(bp))) {
4238 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4239 mp, bp, XFS_BUF_ADDR(bp));
4242 memset(XFS_BUF_PTR(bp) +
4243 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4244 0, lastoffset - offset + 1);
4249 if ((error = xfs_iowait(bp))) {
4250 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4251 mp, bp, XFS_BUF_ADDR(bp));
4260 * xfs_free_file_space()
4261 * This routine frees disk space for the given file.
4263 * This routine is only called by xfs_change_file_space
4264 * for an UNRESVSP type call.
4272 xfs_free_file_space(
4281 xfs_off_t end_dmi_offset;
4282 xfs_fileoff_t endoffset_fsb;
4284 xfs_fsblock_t firstfsb;
4285 xfs_bmap_free_t free_list;
4287 xfs_bmbt_irec_t imap;
4295 xfs_fileoff_t startoffset_fsb;
4297 int need_iolock = 1;
4302 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4304 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4308 if (len <= 0) /* if nothing being freed */
4310 rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4311 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4312 end_dmi_offset = offset + len;
4313 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4315 if (offset < ip->i_d.di_size &&
4316 (attr_flags & ATTR_DMI) == 0 &&
4317 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4318 if (end_dmi_offset > ip->i_d.di_size)
4319 end_dmi_offset = ip->i_d.di_size;
4320 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
4321 offset, end_dmi_offset - offset,
4322 AT_DELAY_FLAG(attr_flags), NULL);
4327 if (attr_flags & ATTR_NOLOCK)
4330 xfs_ilock(ip, XFS_IOLOCK_EXCL);
4331 vn_iowait(vp); /* wait for the completion of any pending DIOs */
4334 rounding = MAX((__uint8_t)(1 << mp->m_sb.sb_blocklog),
4336 ilen = len + (offset & (rounding - 1));
4337 ioffset = offset & ~(rounding - 1);
4338 if (ilen & (rounding - 1))
4339 ilen = (ilen + rounding) & ~(rounding - 1);
4341 if (VN_CACHED(vp) != 0) {
4342 xfs_inval_cached_trace(&ip->i_iocore, ioffset, -1,
4343 ctooff(offtoct(ioffset)), -1);
4344 bhv_vop_flushinval_pages(vp, ctooff(offtoct(ioffset)),
4345 -1, FI_REMAPF_LOCKED);
4349 * Need to zero the stuff we're not freeing, on disk.
4350 * If its a realtime file & can't use unwritten extents then we
4351 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4352 * will take care of it for us.
4354 if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4356 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, startoffset_fsb,
4357 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4359 goto out_unlock_iolock;
4360 ASSERT(nimap == 0 || nimap == 1);
4361 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4364 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4365 block = imap.br_startblock;
4366 mod = do_div(block, mp->m_sb.sb_rextsize);
4368 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4371 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, endoffset_fsb - 1,
4372 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4374 goto out_unlock_iolock;
4375 ASSERT(nimap == 0 || nimap == 1);
4376 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4377 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4379 if (mod && (mod != mp->m_sb.sb_rextsize))
4380 endoffset_fsb -= mod;
4383 if ((done = (endoffset_fsb <= startoffset_fsb)))
4385 * One contiguous piece to clear
4387 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4390 * Some full blocks, possibly two pieces to clear
4392 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4393 error = xfs_zero_remaining_bytes(ip, offset,
4394 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4396 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4397 error = xfs_zero_remaining_bytes(ip,
4398 XFS_FSB_TO_B(mp, endoffset_fsb),
4403 * free file space until done or until there is an error
4405 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4406 while (!error && !done) {
4409 * allocate and setup the transaction
4411 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4412 error = xfs_trans_reserve(tp,
4414 XFS_WRITE_LOG_RES(mp),
4416 XFS_TRANS_PERM_LOG_RES,
4417 XFS_WRITE_LOG_COUNT);
4420 * check for running out of space
4424 * Free the transaction structure.
4426 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4427 xfs_trans_cancel(tp, 0);
4430 xfs_ilock(ip, XFS_ILOCK_EXCL);
4431 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4432 ip->i_udquot, ip->i_gdquot, resblks, 0,
4433 XFS_QMOPT_RES_REGBLKS);
4437 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4438 xfs_trans_ihold(tp, ip);
4441 * issue the bunmapi() call to free the blocks
4443 XFS_BMAP_INIT(&free_list, &firstfsb);
4444 error = XFS_BUNMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4445 endoffset_fsb - startoffset_fsb,
4446 0, 2, &firstfsb, &free_list, NULL, &done);
4452 * complete the transaction
4454 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
4459 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
4460 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4465 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4469 xfs_bmap_cancel(&free_list);
4471 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4472 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4478 * xfs_change_file_space()
4479 * This routine allocates or frees disk space for the given file.
4480 * The user specified parameters are checked for alignment and size
4489 xfs_change_file_space(
4503 xfs_off_t startoffset;
4509 vp = BHV_TO_VNODE(bdp);
4510 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4512 ip = XFS_BHVTOI(bdp);
4516 * must be a regular file and have write permission
4519 return XFS_ERROR(EINVAL);
4521 xfs_ilock(ip, XFS_ILOCK_SHARED);
4523 if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
4524 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4528 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4530 switch (bf->l_whence) {
4531 case 0: /*SEEK_SET*/
4533 case 1: /*SEEK_CUR*/
4534 bf->l_start += offset;
4536 case 2: /*SEEK_END*/
4537 bf->l_start += ip->i_d.di_size;
4540 return XFS_ERROR(EINVAL);
4543 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4545 if ( (bf->l_start < 0)
4546 || (bf->l_start > XFS_MAXIOFFSET(mp))
4547 || (bf->l_start + llen < 0)
4548 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4549 return XFS_ERROR(EINVAL);
4553 startoffset = bf->l_start;
4554 fsize = ip->i_d.di_size;
4557 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4559 * These calls do NOT zero the data space allocated to the file,
4560 * nor do they change the file size.
4562 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4564 * These calls cause the new file data to be zeroed and the file
4565 * size to be changed.
4567 setprealloc = clrprealloc = 0;
4570 case XFS_IOC_RESVSP:
4571 case XFS_IOC_RESVSP64:
4572 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4579 case XFS_IOC_UNRESVSP:
4580 case XFS_IOC_UNRESVSP64:
4581 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4586 case XFS_IOC_ALLOCSP:
4587 case XFS_IOC_ALLOCSP64:
4588 case XFS_IOC_FREESP:
4589 case XFS_IOC_FREESP64:
4590 if (startoffset > fsize) {
4591 error = xfs_alloc_file_space(ip, fsize,
4592 startoffset - fsize, 0, attr_flags);
4597 va.va_mask = XFS_AT_SIZE;
4598 va.va_size = startoffset;
4600 error = xfs_setattr(bdp, &va, attr_flags, credp);
4610 return XFS_ERROR(EINVAL);
4614 * update the inode timestamp, mode, and prealloc flag bits
4616 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4618 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4621 xfs_trans_cancel(tp, 0);
4625 xfs_ilock(ip, XFS_ILOCK_EXCL);
4627 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4628 xfs_trans_ihold(tp, ip);
4630 if ((attr_flags & ATTR_DMI) == 0) {
4631 ip->i_d.di_mode &= ~S_ISUID;
4634 * Note that we don't have to worry about mandatory
4635 * file locking being disabled here because we only
4636 * clear the S_ISGID bit if the Group execute bit is
4637 * on, but if it was on then mandatory locking wouldn't
4638 * have been enabled.
4640 if (ip->i_d.di_mode & S_IXGRP)
4641 ip->i_d.di_mode &= ~S_ISGID;
4643 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4646 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4647 else if (clrprealloc)
4648 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4650 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4651 xfs_trans_set_sync(tp);
4653 error = xfs_trans_commit(tp, 0, NULL);
4655 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4660 bhv_vnodeops_t xfs_vnodeops = {
4661 BHV_IDENTITY_INIT(VN_BHV_XFS,VNODE_POSITION_XFS),
4662 .vop_open = xfs_open,
4663 .vop_close = xfs_close,
4664 .vop_read = xfs_read,
4665 #ifdef HAVE_SENDFILE
4666 .vop_sendfile = xfs_sendfile,
4669 .vop_splice_read = xfs_splice_read,
4670 .vop_splice_write = xfs_splice_write,
4672 .vop_write = xfs_write,
4673 .vop_ioctl = xfs_ioctl,
4674 .vop_getattr = xfs_getattr,
4675 .vop_setattr = xfs_setattr,
4676 .vop_access = xfs_access,
4677 .vop_lookup = xfs_lookup,
4678 .vop_create = xfs_create,
4679 .vop_remove = xfs_remove,
4680 .vop_link = xfs_link,
4681 .vop_rename = xfs_rename,
4682 .vop_mkdir = xfs_mkdir,
4683 .vop_rmdir = xfs_rmdir,
4684 .vop_readdir = xfs_readdir,
4685 .vop_symlink = xfs_symlink,
4686 .vop_readlink = xfs_readlink,
4687 .vop_fsync = xfs_fsync,
4688 .vop_inactive = xfs_inactive,
4689 .vop_fid2 = xfs_fid2,
4690 .vop_rwlock = xfs_rwlock,
4691 .vop_rwunlock = xfs_rwunlock,
4692 .vop_bmap = xfs_bmap,
4693 .vop_reclaim = xfs_reclaim,
4694 .vop_attr_get = xfs_attr_get,
4695 .vop_attr_set = xfs_attr_set,
4696 .vop_attr_remove = xfs_attr_remove,
4697 .vop_attr_list = xfs_attr_list,
4698 .vop_link_removed = (vop_link_removed_t)fs_noval,
4699 .vop_vnode_change = (vop_vnode_change_t)fs_noval,
4700 .vop_tosspages = fs_tosspages,
4701 .vop_flushinval_pages = fs_flushinval_pages,
4702 .vop_flush_pages = fs_flush_pages,
4703 .vop_release = xfs_release,
4704 .vop_iflush = xfs_inode_flush,