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_tag = ip->i_d.di_tag;
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 /* TODO: handle tagging? */
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 /* TODO: handle tagging? */
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_TAG|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 itag = ip->i_d.di_tag;
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 tag = (mask & XFS_AT_TAG) ? vap->va_tag : itag;
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 /* TODO: handle tagging? */
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_TAG|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 ip->i_d.di_tag = tag;
738 if (XFS_IS_UQUOTA_ON(mp)) {
739 ASSERT(mask & XFS_AT_UID);
741 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
742 &ip->i_udquot, udqp);
744 ip->i_d.di_uid = uid;
747 if (XFS_IS_GQUOTA_ON(mp)) {
748 ASSERT(!XFS_IS_PQUOTA_ON(mp));
749 ASSERT(mask & XFS_AT_GID);
751 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
752 &ip->i_gdquot, gdqp);
754 ip->i_d.di_gid = gid;
756 if (iprojid != projid) {
757 if (XFS_IS_PQUOTA_ON(mp)) {
758 ASSERT(!XFS_IS_GQUOTA_ON(mp));
759 ASSERT(mask & XFS_AT_PROJID);
761 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
762 &ip->i_gdquot, gdqp);
764 ip->i_d.di_projid = projid;
766 * We may have to rev the inode as well as
767 * the superblock version number since projids didn't
768 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
770 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
771 xfs_bump_ino_vers2(tp, ip);
774 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
775 timeflags |= XFS_ICHGTIME_CHG;
780 * Change file access or modified times.
782 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
783 if (mask & XFS_AT_ATIME) {
784 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
785 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
786 ip->i_update_core = 1;
787 timeflags &= ~XFS_ICHGTIME_ACC;
789 if (mask & XFS_AT_MTIME) {
790 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
791 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
792 timeflags &= ~XFS_ICHGTIME_MOD;
793 timeflags |= XFS_ICHGTIME_CHG;
795 if (tp && (flags & ATTR_UTIME))
796 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
800 * Change XFS-added attributes.
802 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
803 if (mask & XFS_AT_EXTSIZE) {
805 * Converting bytes to fs blocks.
807 ip->i_d.di_extsize = vap->va_extsize >>
808 mp->m_sb.sb_blocklog;
810 if (mask & XFS_AT_XFLAGS) {
813 /* can't set PREALLOC this way, just preserve it */
814 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
815 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
816 di_flags |= XFS_DIFLAG_IMMUTABLE;
817 if (vap->va_xflags & XFS_XFLAG_IUNLINK)
818 di_flags |= XFS_DIFLAG_IUNLINK;
819 if (vap->va_xflags & XFS_XFLAG_BARRIER)
820 di_flags |= XFS_DIFLAG_BARRIER;
821 if (vap->va_xflags & XFS_XFLAG_APPEND)
822 di_flags |= XFS_DIFLAG_APPEND;
823 if (vap->va_xflags & XFS_XFLAG_SYNC)
824 di_flags |= XFS_DIFLAG_SYNC;
825 if (vap->va_xflags & XFS_XFLAG_NOATIME)
826 di_flags |= XFS_DIFLAG_NOATIME;
827 if (vap->va_xflags & XFS_XFLAG_NODUMP)
828 di_flags |= XFS_DIFLAG_NODUMP;
829 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
830 di_flags |= XFS_DIFLAG_PROJINHERIT;
831 if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
832 di_flags |= XFS_DIFLAG_NODEFRAG;
833 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
834 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
835 di_flags |= XFS_DIFLAG_RTINHERIT;
836 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
837 di_flags |= XFS_DIFLAG_NOSYMLINKS;
838 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
839 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
840 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
841 if (vap->va_xflags & XFS_XFLAG_REALTIME) {
842 di_flags |= XFS_DIFLAG_REALTIME;
843 ip->i_iocore.io_flags |= XFS_IOCORE_RT;
845 ip->i_iocore.io_flags &= ~XFS_IOCORE_RT;
847 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
848 di_flags |= XFS_DIFLAG_EXTSIZE;
850 ip->i_d.di_flags = di_flags;
852 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
853 timeflags |= XFS_ICHGTIME_CHG;
857 * Change file inode change time only if XFS_AT_CTIME set
858 * AND we have been called by a DMI function.
861 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
862 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
863 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
864 ip->i_update_core = 1;
865 timeflags &= ~XFS_ICHGTIME_CHG;
869 * Send out timestamp changes that need to be set to the
870 * current time. Not done when called by a DMI function.
872 if (timeflags && !(flags & ATTR_DMI))
873 xfs_ichgtime(ip, timeflags);
875 XFS_STATS_INC(xs_ig_attrchg);
878 * If this is a synchronous mount, make sure that the
879 * transaction goes to disk before returning to the user.
880 * This is slightly sub-optimal in that truncates require
881 * two sync transactions instead of one for wsync filesystems.
882 * One for the truncate and one for the timestamps since we
883 * don't want to change the timestamps unless we're sure the
884 * truncate worked. Truncates are less than 1% of the laddis
885 * mix so this probably isn't worth the trouble to optimize.
889 if (mp->m_flags & XFS_MOUNT_WSYNC)
890 xfs_trans_set_sync(tp);
892 code = xfs_trans_commit(tp, commit_flags, NULL);
896 * If the (regular) file's mandatory locking mode changed, then
897 * notify the vnode. We do this under the inode lock to prevent
898 * racing calls to vop_vnode_change.
900 mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
901 if (mandlock_before != mandlock_after) {
902 bhv_vop_vnode_change(vp, VCHANGE_FLAGS_ENF_LOCKING,
906 xfs_iunlock(ip, lock_flags);
909 * Release any dquot(s) the inode had kept before chown.
911 XFS_QM_DQRELE(mp, olddquot1);
912 XFS_QM_DQRELE(mp, olddquot2);
913 XFS_QM_DQRELE(mp, udqp);
914 XFS_QM_DQRELE(mp, gdqp);
920 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_ATTRIBUTE) &&
921 !(flags & ATTR_DMI)) {
922 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
923 NULL, DM_RIGHT_NULL, NULL, NULL,
924 0, 0, AT_DELAY_FLAG(flags));
929 commit_flags |= XFS_TRANS_ABORT;
932 XFS_QM_DQRELE(mp, udqp);
933 XFS_QM_DQRELE(mp, gdqp);
935 xfs_trans_cancel(tp, commit_flags);
937 if (lock_flags != 0) {
938 xfs_iunlock(ip, lock_flags);
946 * Null conversion from vnode mode bits to inode mode bits, as in efs.
957 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
958 (inst_t *)__return_address);
960 ip = XFS_BHVTOI(bdp);
961 xfs_ilock(ip, XFS_ILOCK_SHARED);
962 error = xfs_iaccess(ip, mode, credp);
963 xfs_iunlock(ip, XFS_ILOCK_SHARED);
969 * The maximum pathlen is 1024 bytes. Since the minimum file system
970 * blocksize is 512 bytes, we can get a max of 2 extents back from
973 #define SYMLINK_MAPS 2
994 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1000 vp = BHV_TO_VNODE(bdp);
1001 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1003 ip = XFS_BHVTOI(bdp);
1006 if (XFS_FORCED_SHUTDOWN(mp))
1007 return XFS_ERROR(EIO);
1009 xfs_ilock(ip, XFS_ILOCK_SHARED);
1011 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
1013 offset = uiop->uio_offset;
1014 count = uiop->uio_resid;
1017 error = XFS_ERROR(EINVAL);
1026 * See if the symlink is stored inline.
1028 pathlen = (int)ip->i_d.di_size;
1030 if (ip->i_df.if_flags & XFS_IFINLINE) {
1031 error = xfs_uio_read(ip->i_df.if_u1.if_data, pathlen, uiop);
1035 * Symlink not inline. Call bmap to get it in.
1037 nmaps = SYMLINK_MAPS;
1039 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen),
1040 0, NULL, 0, mval, &nmaps, NULL, NULL);
1046 for (n = 0; n < nmaps; n++) {
1047 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1048 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1049 bp = xfs_buf_read(mp->m_ddev_targp, d,
1050 BTOBB(byte_cnt), 0);
1051 error = XFS_BUF_GETERROR(bp);
1053 xfs_ioerror_alert("xfs_readlink",
1054 ip->i_mount, bp, XFS_BUF_ADDR(bp));
1058 if (pathlen < byte_cnt)
1060 pathlen -= byte_cnt;
1062 error = xfs_uio_read(XFS_BUF_PTR(bp), byte_cnt, uiop);
1069 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1077 * This is called to sync the inode and its data out to disk.
1078 * We need to hold the I/O lock while flushing the data, and
1079 * the inode lock while flushing the inode. The inode lock CANNOT
1080 * be held while flushing the data, so acquire after we're done
1094 int log_flushed = 0, changed = 1;
1096 vn_trace_entry(BHV_TO_VNODE(bdp),
1097 __FUNCTION__, (inst_t *)__return_address);
1099 ip = XFS_BHVTOI(bdp);
1101 ASSERT(start >= 0 && stop >= -1);
1103 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1104 return XFS_ERROR(EIO);
1107 * We always need to make sure that the required inode state
1108 * is safe on disk. The vnode might be clean but because
1109 * of committed transactions that haven't hit the disk yet.
1110 * Likewise, there could be unflushed non-transactional
1111 * changes to the inode core that have to go to disk.
1113 * The following code depends on one assumption: that
1114 * any transaction that changes an inode logs the core
1115 * because it has to change some field in the inode core
1116 * (typically nextents or nblocks). That assumption
1117 * implies that any transactions against an inode will
1118 * catch any non-transactional updates. If inode-altering
1119 * transactions exist that violate this assumption, the
1120 * code breaks. Right now, it figures that if the involved
1121 * update_* field is clear and the inode is unpinned, the
1122 * inode is clean. Either it's been flushed or it's been
1123 * committed and the commit has hit the disk unpinning the inode.
1124 * (Note that xfs_inode_item_format() called at commit clears
1125 * the update_* fields.)
1127 xfs_ilock(ip, XFS_ILOCK_SHARED);
1129 /* If we are flushing data then we care about update_size
1130 * being set, otherwise we care about update_core
1132 if ((flag & FSYNC_DATA) ?
1133 (ip->i_update_size == 0) :
1134 (ip->i_update_core == 0)) {
1136 * Timestamps/size haven't changed since last inode
1137 * flush or inode transaction commit. That means
1138 * either nothing got written or a transaction
1139 * committed which caught the updates. If the
1140 * latter happened and the transaction hasn't
1141 * hit the disk yet, the inode will be still
1142 * be pinned. If it is, force the log.
1145 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1147 if (xfs_ipincount(ip)) {
1148 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
1150 ((flag & FSYNC_WAIT)
1151 ? XFS_LOG_SYNC : 0),
1155 * If the inode is not pinned and nothing
1156 * has changed we don't need to flush the
1164 * Kick off a transaction to log the inode
1165 * core to get the updates. Make it
1166 * sync if FSYNC_WAIT is passed in (which
1167 * is done by everybody but specfs). The
1168 * sync transaction will also force the log.
1170 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1171 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1172 if ((error = xfs_trans_reserve(tp, 0,
1173 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1175 xfs_trans_cancel(tp, 0);
1178 xfs_ilock(ip, XFS_ILOCK_EXCL);
1181 * Note - it's possible that we might have pushed
1182 * ourselves out of the way during trans_reserve
1183 * which would flush the inode. But there's no
1184 * guarantee that the inode buffer has actually
1185 * gone out yet (it's delwri). Plus the buffer
1186 * could be pinned anyway if it's part of an
1187 * inode in another recent transaction. So we
1188 * play it safe and fire off the transaction anyway.
1190 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1191 xfs_trans_ihold(tp, ip);
1192 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1193 if (flag & FSYNC_WAIT)
1194 xfs_trans_set_sync(tp);
1195 error = _xfs_trans_commit(tp, 0, NULL, &log_flushed);
1197 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1200 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1202 * If the log write didn't issue an ordered tag we need
1203 * to flush the disk cache for the data device now.
1206 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1209 * If this inode is on the RT dev we need to flush that
1212 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
1213 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1220 * This is called by xfs_inactive to free any blocks beyond eof,
1221 * when the link count isn't zero.
1224 xfs_inactive_free_eofblocks(
1230 xfs_fileoff_t end_fsb;
1231 xfs_fileoff_t last_fsb;
1232 xfs_filblks_t map_len;
1234 xfs_bmbt_irec_t imap;
1237 * Figure out if there are any blocks beyond the end
1238 * of the file. If not, then there is nothing to do.
1240 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_d.di_size));
1241 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1242 map_len = last_fsb - end_fsb;
1247 xfs_ilock(ip, XFS_ILOCK_SHARED);
1248 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, end_fsb, map_len, 0,
1249 NULL, 0, &imap, &nimaps, NULL, NULL);
1250 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1252 if (!error && (nimaps != 0) &&
1253 (imap.br_startblock != HOLESTARTBLOCK ||
1254 ip->i_delayed_blks)) {
1256 * Attach the dquots to the inode up front.
1258 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1262 * There are blocks after the end of file.
1263 * Free them up now by truncating the file to
1266 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1269 * Do the xfs_itruncate_start() call before
1270 * reserving any log space because
1271 * itruncate_start will call into the buffer
1272 * cache and we can't
1273 * do that within a transaction.
1275 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1276 xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
1279 error = xfs_trans_reserve(tp, 0,
1280 XFS_ITRUNCATE_LOG_RES(mp),
1281 0, XFS_TRANS_PERM_LOG_RES,
1282 XFS_ITRUNCATE_LOG_COUNT);
1284 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1285 xfs_trans_cancel(tp, 0);
1286 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1290 xfs_ilock(ip, XFS_ILOCK_EXCL);
1291 xfs_trans_ijoin(tp, ip,
1294 xfs_trans_ihold(tp, ip);
1296 error = xfs_itruncate_finish(&tp, ip,
1301 * If we get an error at this point we
1302 * simply don't bother truncating the file.
1305 xfs_trans_cancel(tp,
1306 (XFS_TRANS_RELEASE_LOG_RES |
1309 error = xfs_trans_commit(tp,
1310 XFS_TRANS_RELEASE_LOG_RES,
1313 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1319 * Free a symlink that has blocks associated with it.
1322 xfs_inactive_symlink_rmt(
1330 xfs_fsblock_t first_block;
1331 xfs_bmap_free_t free_list;
1334 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1342 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1344 * We're freeing a symlink that has some
1345 * blocks allocated to it. Free the
1346 * blocks here. We know that we've got
1347 * either 1 or 2 extents and that we can
1348 * free them all in one bunmapi call.
1350 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1351 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1352 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1353 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1354 xfs_trans_cancel(tp, 0);
1359 * Lock the inode, fix the size, and join it to the transaction.
1360 * Hold it so in the normal path, we still have it locked for
1361 * the second transaction. In the error paths we need it
1362 * held so the cancel won't rele it, see below.
1364 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1365 size = (int)ip->i_d.di_size;
1366 ip->i_d.di_size = 0;
1367 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1368 xfs_trans_ihold(tp, ip);
1369 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1371 * Find the block(s) so we can inval and unmap them.
1374 XFS_BMAP_INIT(&free_list, &first_block);
1375 nmaps = ARRAY_SIZE(mval);
1376 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1377 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
1381 * Invalidate the block(s).
1383 for (i = 0; i < nmaps; i++) {
1384 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1385 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1386 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1387 xfs_trans_binval(tp, bp);
1390 * Unmap the dead block(s) to the free_list.
1392 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
1393 &first_block, &free_list, NULL, &done)))
1397 * Commit the first transaction. This logs the EFI and the inode.
1399 if ((error = xfs_bmap_finish(&tp, &free_list, first_block, &committed)))
1402 * The transaction must have been committed, since there were
1403 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1404 * The new tp has the extent freeing and EFDs.
1408 * The first xact was committed, so add the inode to the new one.
1409 * Mark it dirty so it will be logged and moved forward in the log as
1410 * part of every commit.
1412 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1413 xfs_trans_ihold(tp, ip);
1414 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1416 * Get a new, empty transaction to return to our caller.
1418 ntp = xfs_trans_dup(tp);
1420 * Commit the transaction containing extent freeing and EFDs.
1421 * If we get an error on the commit here or on the reserve below,
1422 * we need to unlock the inode since the new transaction doesn't
1423 * have the inode attached.
1425 error = xfs_trans_commit(tp, 0, NULL);
1428 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1432 * Remove the memory for extent descriptions (just bookkeeping).
1434 if (ip->i_df.if_bytes)
1435 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1436 ASSERT(ip->i_df.if_bytes == 0);
1438 * Put an itruncate log reservation in the new transaction
1441 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1442 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1443 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1447 * Return with the inode locked but not joined to the transaction.
1453 xfs_bmap_cancel(&free_list);
1456 * Have to come here with the inode locked and either
1457 * (held and in the transaction) or (not in the transaction).
1458 * If the inode isn't held then cancel would iput it, but
1459 * that's wrong since this is inactive and the vnode ref
1460 * count is 0 already.
1461 * Cancel won't do anything to the inode if held, but it still
1462 * needs to be locked until the cancel is done, if it was
1463 * joined to the transaction.
1465 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1466 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1473 xfs_inactive_symlink_local(
1479 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1481 * We're freeing a symlink which fit into
1482 * the inode. Just free the memory used
1483 * to hold the old symlink.
1485 error = xfs_trans_reserve(*tpp, 0,
1486 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1487 0, XFS_TRANS_PERM_LOG_RES,
1488 XFS_ITRUNCATE_LOG_COUNT);
1491 xfs_trans_cancel(*tpp, 0);
1495 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1498 * Zero length symlinks _can_ exist.
1500 if (ip->i_df.if_bytes > 0) {
1501 xfs_idata_realloc(ip,
1502 -(ip->i_df.if_bytes),
1504 ASSERT(ip->i_df.if_bytes == 0);
1518 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1521 ASSERT(ip->i_d.di_forkoff != 0);
1522 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
1523 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1525 error = xfs_attr_inactive(ip);
1528 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1529 return error; /* goto out */
1532 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1533 error = xfs_trans_reserve(tp, 0,
1534 XFS_IFREE_LOG_RES(mp),
1535 0, XFS_TRANS_PERM_LOG_RES,
1536 XFS_INACTIVE_LOG_COUNT);
1538 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1539 xfs_trans_cancel(tp, 0);
1541 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1545 xfs_ilock(ip, XFS_ILOCK_EXCL);
1546 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1547 xfs_trans_ihold(tp, ip);
1548 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1550 ASSERT(ip->i_d.di_anextents == 0);
1565 vp = BHV_TO_VNODE(bdp);
1566 ip = XFS_BHVTOI(bdp);
1569 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
1572 /* If this is a read-only mount, don't do this (would generate I/O) */
1573 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1576 #ifdef HAVE_REFCACHE
1577 /* If we are in the NFS reference cache then don't do this now */
1582 if (ip->i_d.di_nlink != 0) {
1583 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1584 ((ip->i_d.di_size > 0) || (VN_CACHED(vp) > 0 ||
1585 ip->i_delayed_blks > 0)) &&
1586 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1587 (!(ip->i_d.di_flags &
1588 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1589 if ((error = xfs_inactive_free_eofblocks(mp, ip)))
1591 /* Update linux inode block count after free above */
1592 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1593 ip->i_d.di_nblocks + ip->i_delayed_blks);
1603 * This is called when the vnode reference count for the vnode
1604 * goes to zero. If the file has been unlinked, then it must
1605 * now be truncated. Also, we clear all of the read-ahead state
1606 * kept for the inode here since the file is now closed.
1615 xfs_bmap_free_t free_list;
1616 xfs_fsblock_t first_block;
1623 vp = BHV_TO_VNODE(bdp);
1624 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1626 ip = XFS_BHVTOI(bdp);
1629 * If the inode is already free, then there can be nothing
1632 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1633 ASSERT(ip->i_df.if_real_bytes == 0);
1634 ASSERT(ip->i_df.if_broot_bytes == 0);
1635 return VN_INACTIVE_CACHE;
1639 * Only do a truncate if it's a regular file with
1640 * some actual space in it. It's OK to look at the
1641 * inode's fields without the lock because we're the
1642 * only one with a reference to the inode.
1644 truncate = ((ip->i_d.di_nlink == 0) &&
1645 ((ip->i_d.di_size != 0) || (ip->i_d.di_nextents > 0) ||
1646 (ip->i_delayed_blks > 0)) &&
1647 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1651 if (ip->i_d.di_nlink == 0 &&
1652 DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_DESTROY)) {
1653 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1658 /* If this is a read-only mount, don't do this (would generate I/O) */
1659 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1662 if (ip->i_d.di_nlink != 0) {
1663 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1664 ((ip->i_d.di_size > 0) || (VN_CACHED(vp) > 0 ||
1665 ip->i_delayed_blks > 0)) &&
1666 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1667 (!(ip->i_d.di_flags &
1668 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1669 (ip->i_delayed_blks != 0)))) {
1670 if ((error = xfs_inactive_free_eofblocks(mp, ip)))
1671 return VN_INACTIVE_CACHE;
1672 /* Update linux inode block count after free above */
1673 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1674 ip->i_d.di_nblocks + ip->i_delayed_blks);
1679 ASSERT(ip->i_d.di_nlink == 0);
1681 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1682 return VN_INACTIVE_CACHE;
1684 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1687 * Do the xfs_itruncate_start() call before
1688 * reserving any log space because itruncate_start
1689 * will call into the buffer cache and we can't
1690 * do that within a transaction.
1692 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1694 xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1696 error = xfs_trans_reserve(tp, 0,
1697 XFS_ITRUNCATE_LOG_RES(mp),
1698 0, XFS_TRANS_PERM_LOG_RES,
1699 XFS_ITRUNCATE_LOG_COUNT);
1701 /* Don't call itruncate_cleanup */
1702 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1703 xfs_trans_cancel(tp, 0);
1704 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1705 return VN_INACTIVE_CACHE;
1708 xfs_ilock(ip, XFS_ILOCK_EXCL);
1709 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1710 xfs_trans_ihold(tp, ip);
1713 * normally, we have to run xfs_itruncate_finish sync.
1714 * But if filesystem is wsync and we're in the inactive
1715 * path, then we know that nlink == 0, and that the
1716 * xaction that made nlink == 0 is permanently committed
1717 * since xfs_remove runs as a synchronous transaction.
1719 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1720 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1723 xfs_trans_cancel(tp,
1724 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1725 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1726 return VN_INACTIVE_CACHE;
1728 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1731 * If we get an error while cleaning up a
1732 * symlink we bail out.
1734 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1735 xfs_inactive_symlink_rmt(ip, &tp) :
1736 xfs_inactive_symlink_local(ip, &tp);
1740 return VN_INACTIVE_CACHE;
1743 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1744 xfs_trans_ihold(tp, ip);
1746 error = xfs_trans_reserve(tp, 0,
1747 XFS_IFREE_LOG_RES(mp),
1748 0, XFS_TRANS_PERM_LOG_RES,
1749 XFS_INACTIVE_LOG_COUNT);
1751 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1752 xfs_trans_cancel(tp, 0);
1753 return VN_INACTIVE_CACHE;
1756 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1757 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1758 xfs_trans_ihold(tp, ip);
1762 * If there are attributes associated with the file
1763 * then blow them away now. The code calls a routine
1764 * that recursively deconstructs the attribute fork.
1765 * We need to just commit the current transaction
1766 * because we can't use it for xfs_attr_inactive().
1768 if (ip->i_d.di_anextents > 0) {
1769 error = xfs_inactive_attrs(ip, &tp);
1771 * If we got an error, the transaction is already
1772 * cancelled, and the inode is unlocked. Just get out.
1775 return VN_INACTIVE_CACHE;
1776 } else if (ip->i_afp) {
1777 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1783 XFS_BMAP_INIT(&free_list, &first_block);
1784 error = xfs_ifree(tp, ip, &free_list);
1787 * If we fail to free the inode, shut down. The cancel
1788 * might do that, we need to make sure. Otherwise the
1789 * inode might be lost for a long time or forever.
1791 if (!XFS_FORCED_SHUTDOWN(mp)) {
1793 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1794 error, mp->m_fsname);
1795 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1797 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1800 * Credit the quota account(s). The inode is gone.
1802 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1805 * Just ignore errors at this point. There is
1806 * nothing we can do except to try to keep going.
1808 (void) xfs_bmap_finish(&tp, &free_list, first_block,
1810 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
1813 * Release the dquots held by inode, if any.
1815 XFS_QM_DQDETACH(mp, ip);
1817 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1820 return VN_INACTIVE_CACHE;
1829 bhv_desc_t *dir_bdp,
1830 bhv_vname_t *dentry,
1836 xfs_inode_t *dp, *ip;
1840 bhv_vnode_t *dir_vp;
1842 dir_vp = BHV_TO_VNODE(dir_bdp);
1843 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1845 dp = XFS_BHVTOI(dir_bdp);
1847 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1848 return XFS_ERROR(EIO);
1850 lock_mode = xfs_ilock_map_shared(dp);
1851 error = xfs_dir_lookup_int(dir_bdp, lock_mode, dentry, &e_inum, &ip);
1853 *vpp = XFS_ITOV(ip);
1856 xfs_iunlock_map_shared(dp, lock_mode);
1862 * xfs_create (create a new file).
1866 bhv_desc_t *dir_bdp,
1867 bhv_vname_t *dentry,
1872 char *name = VNAME(dentry);
1873 bhv_vnode_t *dir_vp;
1874 xfs_inode_t *dp, *ip;
1875 bhv_vnode_t *vp = NULL;
1880 xfs_bmap_free_t free_list;
1881 xfs_fsblock_t first_block;
1882 boolean_t dp_joined_to_trans;
1883 int dm_event_sent = 0;
1887 struct xfs_dquot *udqp, *gdqp;
1893 dir_vp = BHV_TO_VNODE(dir_bdp);
1894 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1896 dp = XFS_BHVTOI(dir_bdp);
1899 dm_di_mode = vap->va_mode;
1900 namelen = VNAMELEN(dentry);
1902 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
1903 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1904 dir_vp, DM_RIGHT_NULL, NULL,
1905 DM_RIGHT_NULL, name, NULL,
1913 if (XFS_FORCED_SHUTDOWN(mp))
1914 return XFS_ERROR(EIO);
1916 /* Return through std_return after this point. */
1919 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1920 prid = dp->i_d.di_projid;
1921 else if (vap->va_mask & XFS_AT_PROJID)
1922 prid = (xfs_prid_t)vap->va_projid;
1924 prid = (xfs_prid_t)dfltprid;
1927 * Make sure that we have allocated dquot(s) on disk.
1929 error = XFS_QM_DQVOPALLOC(mp, dp,
1930 current_fsuid(credp), current_fsgid(credp), prid,
1931 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1936 dp_joined_to_trans = B_FALSE;
1938 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1939 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1940 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1942 * Initially assume that the file does not exist and
1943 * reserve the resources for that case. If that is not
1944 * the case we'll drop the one we have and get a more
1945 * appropriate transaction later.
1947 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1948 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1949 if (error == ENOSPC) {
1951 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1952 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1960 xfs_ilock(dp, XFS_ILOCK_EXCL);
1962 XFS_BMAP_INIT(&free_list, &first_block);
1967 * Reserve disk quota and the inode.
1969 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1973 if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
1975 rdev = (vap->va_mask & XFS_AT_RDEV) ? vap->va_rdev : 0;
1976 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 1,
1977 rdev, credp, prid, resblks > 0,
1980 if (error == ENOSPC)
1987 * At this point, we've gotten a newly allocated inode.
1988 * It is locked (and joined to the transaction).
1991 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1994 * Now we join the directory inode to the transaction.
1995 * We do not do it earlier because xfs_dir_ialloc
1996 * might commit the previous transaction (and release
2001 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2002 dp_joined_to_trans = B_TRUE;
2004 error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
2005 &first_block, &free_list, resblks ?
2006 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2008 ASSERT(error != ENOSPC);
2011 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2012 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2015 * If this is a synchronous mount, make sure that the
2016 * create transaction goes to disk before returning to
2019 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2020 xfs_trans_set_sync(tp);
2026 * Attach the dquot(s) to the inodes and modify them incore.
2027 * These ids of the inode couldn't have changed since the new
2028 * inode has been locked ever since it was created.
2030 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2033 * xfs_trans_commit normally decrements the vnode ref count
2034 * when it unlocks the inode. Since we want to return the
2035 * vnode to the caller, we bump the vnode ref count now.
2040 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2042 xfs_bmap_cancel(&free_list);
2046 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2053 XFS_QM_DQRELE(mp, udqp);
2054 XFS_QM_DQRELE(mp, gdqp);
2057 * Propagate the fact that the vnode changed after the
2058 * xfs_inode locks have been released.
2060 bhv_vop_vnode_change(vp, VCHANGE_FLAGS_TRUNCATED, 3);
2064 /* Fallthrough to std_return with error = 0 */
2067 if ( (*vpp || (error != 0 && dm_event_sent != 0)) &&
2068 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2069 DM_EVENT_POSTCREATE)) {
2070 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2071 dir_vp, DM_RIGHT_NULL,
2073 DM_RIGHT_NULL, name, NULL,
2074 dm_di_mode, error, 0);
2079 cancel_flags |= XFS_TRANS_ABORT;
2084 xfs_trans_cancel(tp, cancel_flags);
2086 if (!dp_joined_to_trans && (dp != NULL))
2087 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2088 XFS_QM_DQRELE(mp, udqp);
2089 XFS_QM_DQRELE(mp, gdqp);
2095 * Wait until after the current transaction is aborted to
2096 * release the inode. This prevents recursive transactions
2097 * and deadlocks from xfs_inactive.
2099 cancel_flags |= XFS_TRANS_ABORT;
2100 xfs_trans_cancel(tp, cancel_flags);
2103 XFS_QM_DQRELE(mp, udqp);
2104 XFS_QM_DQRELE(mp, gdqp);
2111 * Some counters to see if (and how often) we are hitting some deadlock
2112 * prevention code paths.
2116 int xfs_rm_lock_delays;
2117 int xfs_rm_attempts;
2121 * The following routine will lock the inodes associated with the
2122 * directory and the named entry in the directory. The locks are
2123 * acquired in increasing inode number.
2125 * If the entry is "..", then only the directory is locked. The
2126 * vnode ref count will still include that from the .. entry in
2129 * There is a deadlock we need to worry about. If the locked directory is
2130 * in the AIL, it might be blocking up the log. The next inode we lock
2131 * could be already locked by another thread waiting for log space (e.g
2132 * a permanent log reservation with a long running transaction (see
2133 * xfs_itruncate_finish)). To solve this, we must check if the directory
2134 * is in the ail and use lock_nowait. If we can't lock, we need to
2135 * drop the inode lock on the directory and try again. xfs_iunlock will
2136 * potentially push the tail if we were holding up the log.
2139 xfs_lock_dir_and_entry(
2141 bhv_vname_t *dentry,
2142 xfs_inode_t *ip) /* inode of entry 'name' */
2146 xfs_inode_t *ips[2];
2155 xfs_ilock(dp, XFS_ILOCK_EXCL);
2162 * We want to lock in increasing inum. Since we've already
2163 * acquired the lock on the directory, we may need to release
2164 * if if the inum of the entry turns out to be less.
2166 if (e_inum > dp->i_ino) {
2168 * We are already in the right order, so just
2169 * lock on the inode of the entry.
2170 * We need to use nowait if dp is in the AIL.
2173 lp = (xfs_log_item_t *)dp->i_itemp;
2174 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2175 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2182 * Unlock dp and try again.
2183 * xfs_iunlock will try to push the tail
2184 * if the inode is in the AIL.
2187 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2189 if ((attempts % 5) == 0) {
2190 delay(1); /* Don't just spin the CPU */
2192 xfs_rm_lock_delays++;
2198 xfs_ilock(ip, XFS_ILOCK_EXCL);
2200 } else if (e_inum < dp->i_ino) {
2201 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2205 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2207 /* else e_inum == dp->i_ino */
2208 /* This can happen if we're asked to lock /x/..
2209 * the entry is "..", which is also the parent directory.
2217 int xfs_small_retries;
2218 int xfs_middle_retries;
2219 int xfs_lots_retries;
2220 int xfs_lock_delays;
2224 * The following routine will lock n inodes in exclusive mode.
2225 * We assume the caller calls us with the inodes in i_ino order.
2227 * We need to detect deadlock where an inode that we lock
2228 * is in the AIL and we start waiting for another inode that is locked
2229 * by a thread in a long running transaction (such as truncate). This can
2230 * result in deadlock since the long running trans might need to wait
2231 * for the inode we just locked in order to push the tail and free space
2241 int attempts = 0, i, j, try_lock;
2244 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2255 for (; i < inodes; i++) {
2258 if (i && (ips[i] == ips[i-1])) /* Already locked */
2262 * If try_lock is not set yet, make sure all locked inodes
2263 * are not in the AIL.
2264 * If any are, set try_lock to be used later.
2268 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2269 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2270 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2277 * If any of the previous locks we have locked is in the AIL,
2278 * we must TRY to get the second and subsequent locks. If
2279 * we can't get any, we must release all we have
2284 /* try_lock must be 0 if i is 0. */
2286 * try_lock means we have an inode locked
2287 * that is in the AIL.
2290 if (!xfs_ilock_nowait(ips[i], lock_mode)) {
2294 * Unlock all previous guys and try again.
2295 * xfs_iunlock will try to push the tail
2296 * if the inode is in the AIL.
2299 for(j = i - 1; j >= 0; j--) {
2302 * Check to see if we've already
2303 * unlocked this one.
2304 * Not the first one going back,
2305 * and the inode ptr is the same.
2307 if ((j != (i - 1)) && ips[j] ==
2311 xfs_iunlock(ips[j], lock_mode);
2314 if ((attempts % 5) == 0) {
2315 delay(1); /* Don't just spin the CPU */
2325 xfs_ilock(ips[i], lock_mode);
2331 if (attempts < 5) xfs_small_retries++;
2332 else if (attempts < 100) xfs_middle_retries++;
2333 else xfs_lots_retries++;
2341 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2342 int remove_which_error_return = 0;
2344 #define REMOVE_DEBUG_TRACE(x)
2345 #endif /* ! DEBUG */
2354 bhv_desc_t *dir_bdp,
2355 bhv_vname_t *dentry,
2358 bhv_vnode_t *dir_vp;
2359 char *name = VNAME(dentry);
2360 xfs_inode_t *dp, *ip;
2361 xfs_trans_t *tp = NULL;
2364 xfs_bmap_free_t free_list;
2365 xfs_fsblock_t first_block;
2373 dir_vp = BHV_TO_VNODE(dir_bdp);
2374 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2376 dp = XFS_BHVTOI(dir_bdp);
2379 if (XFS_FORCED_SHUTDOWN(mp))
2380 return XFS_ERROR(EIO);
2382 namelen = VNAMELEN(dentry);
2384 if (!xfs_get_dir_entry(dentry, &ip)) {
2385 dm_di_mode = ip->i_d.di_mode;
2389 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
2390 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2391 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2392 name, NULL, dm_di_mode, 0, 0);
2397 /* From this point on, return through std_return */
2401 * We need to get a reference to ip before we get our log
2402 * reservation. The reason for this is that we cannot call
2403 * xfs_iget for an inode for which we do not have a reference
2404 * once we've acquired a log reservation. This is because the
2405 * inode we are trying to get might be in xfs_inactive going
2406 * for a log reservation. Since we'll have to wait for the
2407 * inactive code to complete before returning from xfs_iget,
2408 * we need to make sure that we don't have log space reserved
2409 * when we call xfs_iget. Instead we get an unlocked reference
2410 * to the inode before getting our log reservation.
2412 error = xfs_get_dir_entry(dentry, &ip);
2414 REMOVE_DEBUG_TRACE(__LINE__);
2418 dm_di_mode = ip->i_d.di_mode;
2420 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2424 error = XFS_QM_DQATTACH(mp, dp, 0);
2425 if (!error && dp != ip)
2426 error = XFS_QM_DQATTACH(mp, ip, 0);
2428 REMOVE_DEBUG_TRACE(__LINE__);
2433 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2434 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2436 * We try to get the real space reservation first,
2437 * allowing for directory btree deletion(s) implying
2438 * possible bmap insert(s). If we can't get the space
2439 * reservation then we use 0 instead, and avoid the bmap
2440 * btree insert(s) in the directory code by, if the bmap
2441 * insert tries to happen, instead trimming the LAST
2442 * block from the directory.
2444 resblks = XFS_REMOVE_SPACE_RES(mp);
2445 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2446 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2447 if (error == ENOSPC) {
2449 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2450 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2453 ASSERT(error != ENOSPC);
2454 REMOVE_DEBUG_TRACE(__LINE__);
2455 xfs_trans_cancel(tp, 0);
2460 error = xfs_lock_dir_and_entry(dp, dentry, ip);
2462 REMOVE_DEBUG_TRACE(__LINE__);
2463 xfs_trans_cancel(tp, cancel_flags);
2469 * At this point, we've gotten both the directory and the entry
2472 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2475 * Increment vnode ref count only in this case since
2476 * there's an extra vnode reference in the case where
2480 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2484 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2486 XFS_BMAP_INIT(&free_list, &first_block);
2487 error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2488 &first_block, &free_list, 0);
2490 ASSERT(error != ENOENT);
2491 REMOVE_DEBUG_TRACE(__LINE__);
2494 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2497 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2499 error = xfs_droplink(tp, ip);
2501 REMOVE_DEBUG_TRACE(__LINE__);
2505 /* Determine if this is the last link while
2506 * we are in the transaction.
2508 link_zero = (ip)->i_d.di_nlink==0;
2511 * Take an extra ref on the inode so that it doesn't
2512 * go to xfs_inactive() from within the commit.
2517 * If this is a synchronous mount, make sure that the
2518 * remove transaction goes to disk before returning to
2521 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2522 xfs_trans_set_sync(tp);
2525 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2527 REMOVE_DEBUG_TRACE(__LINE__);
2531 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2538 * Before we drop our extra reference to the inode, purge it
2539 * from the refcache if it is there. By waiting until afterwards
2540 * to do the IRELE, we ensure that we won't go inactive in the
2541 * xfs_refcache_purge_ip routine (although that would be OK).
2543 xfs_refcache_purge_ip(ip);
2545 vn_trace_exit(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2548 * Let interposed file systems know about removed links.
2550 bhv_vop_link_removed(XFS_ITOV(ip), dir_vp, link_zero);
2554 /* Fall through to std_return with error = 0 */
2556 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp,
2557 DM_EVENT_POSTREMOVE)) {
2558 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2559 dir_vp, DM_RIGHT_NULL,
2560 NULL, DM_RIGHT_NULL,
2561 name, NULL, dm_di_mode, error, 0);
2566 xfs_bmap_cancel(&free_list);
2567 cancel_flags |= XFS_TRANS_ABORT;
2568 xfs_trans_cancel(tp, cancel_flags);
2573 * In this case make sure to not release the inode until after
2574 * the current transaction is aborted. Releasing it beforehand
2575 * can cause us to go to xfs_inactive and start a recursive
2576 * transaction which can easily deadlock with the current one.
2578 xfs_bmap_cancel(&free_list);
2579 cancel_flags |= XFS_TRANS_ABORT;
2580 xfs_trans_cancel(tp, cancel_flags);
2583 * Before we drop our extra reference to the inode, purge it
2584 * from the refcache if it is there. By waiting until afterwards
2585 * to do the IRELE, we ensure that we won't go inactive in the
2586 * xfs_refcache_purge_ip routine (although that would be OK).
2588 xfs_refcache_purge_ip(ip);
2602 bhv_desc_t *target_dir_bdp,
2603 bhv_vnode_t *src_vp,
2604 bhv_vname_t *dentry,
2607 xfs_inode_t *tdp, *sip;
2610 xfs_inode_t *ips[2];
2612 xfs_bmap_free_t free_list;
2613 xfs_fsblock_t first_block;
2616 bhv_vnode_t *target_dir_vp;
2618 char *target_name = VNAME(dentry);
2621 target_dir_vp = BHV_TO_VNODE(target_dir_bdp);
2622 vn_trace_entry(target_dir_vp, __FUNCTION__, (inst_t *)__return_address);
2623 vn_trace_entry(src_vp, __FUNCTION__, (inst_t *)__return_address);
2625 target_namelen = VNAMELEN(dentry);
2626 ASSERT(!VN_ISDIR(src_vp));
2628 sip = xfs_vtoi(src_vp);
2629 tdp = XFS_BHVTOI(target_dir_bdp);
2631 if (XFS_FORCED_SHUTDOWN(mp))
2632 return XFS_ERROR(EIO);
2634 if (DM_EVENT_ENABLED(src_vp->v_vfsp, tdp, DM_EVENT_LINK)) {
2635 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2636 target_dir_vp, DM_RIGHT_NULL,
2637 src_vp, DM_RIGHT_NULL,
2638 target_name, NULL, 0, 0, 0);
2643 /* Return through std_return after this point. */
2645 error = XFS_QM_DQATTACH(mp, sip, 0);
2646 if (!error && sip != tdp)
2647 error = XFS_QM_DQATTACH(mp, tdp, 0);
2651 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2652 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2653 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2654 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2655 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2656 if (error == ENOSPC) {
2658 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2659 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2666 if (sip->i_ino < tdp->i_ino) {
2674 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2677 * Increment vnode ref counts since xfs_trans_commit &
2678 * xfs_trans_cancel will both unlock the inodes and
2679 * decrement the associated ref counts.
2682 VN_HOLD(target_dir_vp);
2683 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2684 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2687 * If the source has too many links, we can't make any more to it.
2689 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2690 error = XFS_ERROR(EMLINK);
2695 * If we are using project inheritance, we only allow hard link
2696 * creation in our tree when the project IDs are the same; else
2697 * the tree quota mechanism could be circumvented.
2699 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2700 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2701 error = XFS_ERROR(EXDEV);
2706 (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
2709 XFS_BMAP_INIT(&free_list, &first_block);
2711 error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
2712 sip->i_ino, &first_block, &free_list,
2716 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2718 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2720 error = xfs_bumplink(tp, sip);
2725 * If this is a synchronous mount, make sure that the
2726 * link transaction goes to disk before returning to
2729 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2730 xfs_trans_set_sync(tp);
2733 error = xfs_bmap_finish (&tp, &free_list, first_block, &committed);
2735 xfs_bmap_cancel(&free_list);
2739 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2743 /* Fall through to std_return with error = 0. */
2745 if (DM_EVENT_ENABLED(src_vp->v_vfsp, sip,
2746 DM_EVENT_POSTLINK)) {
2747 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2748 target_dir_vp, DM_RIGHT_NULL,
2749 src_vp, DM_RIGHT_NULL,
2750 target_name, NULL, 0, error, 0);
2755 cancel_flags |= XFS_TRANS_ABORT;
2759 xfs_trans_cancel(tp, cancel_flags);
2770 bhv_desc_t *dir_bdp,
2771 bhv_vname_t *dentry,
2776 char *dir_name = VNAME(dentry);
2778 xfs_inode_t *cdp; /* inode of created dir */
2779 bhv_vnode_t *cvp; /* vnode of created dir */
2785 xfs_bmap_free_t free_list;
2786 xfs_fsblock_t first_block;
2787 bhv_vnode_t *dir_vp;
2788 boolean_t dp_joined_to_trans;
2789 boolean_t created = B_FALSE;
2790 int dm_event_sent = 0;
2792 struct xfs_dquot *udqp, *gdqp;
2797 dir_vp = BHV_TO_VNODE(dir_bdp);
2798 dp = XFS_BHVTOI(dir_bdp);
2801 if (XFS_FORCED_SHUTDOWN(mp))
2802 return XFS_ERROR(EIO);
2804 dir_namelen = VNAMELEN(dentry);
2807 dp_joined_to_trans = B_FALSE;
2808 dm_di_mode = vap->va_mode;
2810 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
2811 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2812 dir_vp, DM_RIGHT_NULL, NULL,
2813 DM_RIGHT_NULL, dir_name, NULL,
2820 /* Return through std_return after this point. */
2822 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2826 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2827 prid = dp->i_d.di_projid;
2828 else if (vap->va_mask & XFS_AT_PROJID)
2829 prid = (xfs_prid_t)vap->va_projid;
2831 prid = (xfs_prid_t)dfltprid;
2834 * Make sure that we have allocated dquot(s) on disk.
2836 error = XFS_QM_DQVOPALLOC(mp, dp,
2837 current_fsuid(credp), current_fsgid(credp), prid,
2838 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2842 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2843 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2844 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2845 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2846 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2847 if (error == ENOSPC) {
2849 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2850 XFS_TRANS_PERM_LOG_RES,
2851 XFS_MKDIR_LOG_COUNT);
2859 xfs_ilock(dp, XFS_ILOCK_EXCL);
2862 * Check for directory link count overflow.
2864 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2865 error = XFS_ERROR(EMLINK);
2870 * Reserve disk quota and the inode.
2872 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2877 (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
2880 * create the directory inode.
2882 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 2,
2883 0, credp, prid, resblks > 0,
2886 if (error == ENOSPC)
2893 * Now we add the directory inode to the transaction.
2894 * We waited until now since xfs_dir_ialloc might start
2895 * a new transaction. Had we joined the transaction
2896 * earlier, the locks might have gotten released.
2899 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2900 dp_joined_to_trans = B_TRUE;
2902 XFS_BMAP_INIT(&free_list, &first_block);
2904 error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2905 &first_block, &free_list, resblks ?
2906 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2908 ASSERT(error != ENOSPC);
2911 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2914 * Bump the in memory version number of the parent directory
2915 * so that other processes accessing it will recognize that
2916 * the directory has changed.
2920 error = xfs_dir_init(tp, cdp, dp);
2925 error = xfs_bumplink(tp, dp);
2929 cvp = XFS_ITOV(cdp);
2937 * Attach the dquots to the new inode and modify the icount incore.
2939 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2942 * If this is a synchronous mount, make sure that the
2943 * mkdir transaction goes to disk before returning to
2946 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2947 xfs_trans_set_sync(tp);
2950 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2956 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2957 XFS_QM_DQRELE(mp, udqp);
2958 XFS_QM_DQRELE(mp, gdqp);
2963 /* Fall through to std_return with error = 0 or errno from
2964 * xfs_trans_commit. */
2967 if ( (created || (error != 0 && dm_event_sent != 0)) &&
2968 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2969 DM_EVENT_POSTCREATE)) {
2970 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2971 dir_vp, DM_RIGHT_NULL,
2972 created ? XFS_ITOV(cdp):NULL,
2975 dm_di_mode, error, 0);
2981 xfs_bmap_cancel(&free_list);
2983 cancel_flags |= XFS_TRANS_ABORT;
2985 xfs_trans_cancel(tp, cancel_flags);
2986 XFS_QM_DQRELE(mp, udqp);
2987 XFS_QM_DQRELE(mp, gdqp);
2989 if (!dp_joined_to_trans && (dp != NULL)) {
2990 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3003 bhv_desc_t *dir_bdp,
3004 bhv_vname_t *dentry,
3007 char *name = VNAME(dentry);
3009 xfs_inode_t *cdp; /* child directory */
3013 xfs_bmap_free_t free_list;
3014 xfs_fsblock_t first_block;
3017 bhv_vnode_t *dir_vp;
3018 int dm_di_mode = S_IFDIR;
3023 dir_vp = BHV_TO_VNODE(dir_bdp);
3024 dp = XFS_BHVTOI(dir_bdp);
3027 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3029 if (XFS_FORCED_SHUTDOWN(XFS_BHVTOI(dir_bdp)->i_mount))
3030 return XFS_ERROR(EIO);
3031 namelen = VNAMELEN(dentry);
3033 if (!xfs_get_dir_entry(dentry, &cdp)) {
3034 dm_di_mode = cdp->i_d.di_mode;
3038 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
3039 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
3040 dir_vp, DM_RIGHT_NULL,
3041 NULL, DM_RIGHT_NULL,
3042 name, NULL, dm_di_mode, 0, 0);
3044 return XFS_ERROR(error);
3047 /* Return through std_return after this point. */
3052 * We need to get a reference to cdp before we get our log
3053 * reservation. The reason for this is that we cannot call
3054 * xfs_iget for an inode for which we do not have a reference
3055 * once we've acquired a log reservation. This is because the
3056 * inode we are trying to get might be in xfs_inactive going
3057 * for a log reservation. Since we'll have to wait for the
3058 * inactive code to complete before returning from xfs_iget,
3059 * we need to make sure that we don't have log space reserved
3060 * when we call xfs_iget. Instead we get an unlocked reference
3061 * to the inode before getting our log reservation.
3063 error = xfs_get_dir_entry(dentry, &cdp);
3065 REMOVE_DEBUG_TRACE(__LINE__);
3069 dm_di_mode = cdp->i_d.di_mode;
3072 * Get the dquots for the inodes.
3074 error = XFS_QM_DQATTACH(mp, dp, 0);
3075 if (!error && dp != cdp)
3076 error = XFS_QM_DQATTACH(mp, cdp, 0);
3079 REMOVE_DEBUG_TRACE(__LINE__);
3083 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
3084 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3086 * We try to get the real space reservation first,
3087 * allowing for directory btree deletion(s) implying
3088 * possible bmap insert(s). If we can't get the space
3089 * reservation then we use 0 instead, and avoid the bmap
3090 * btree insert(s) in the directory code by, if the bmap
3091 * insert tries to happen, instead trimming the LAST
3092 * block from the directory.
3094 resblks = XFS_REMOVE_SPACE_RES(mp);
3095 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
3096 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3097 if (error == ENOSPC) {
3099 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
3100 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3103 ASSERT(error != ENOSPC);
3108 XFS_BMAP_INIT(&free_list, &first_block);
3111 * Now lock the child directory inode and the parent directory
3112 * inode in the proper order. This will take care of validating
3113 * that the directory entry for the child directory inode has
3114 * not changed while we were obtaining a log reservation.
3116 error = xfs_lock_dir_and_entry(dp, dentry, cdp);
3118 xfs_trans_cancel(tp, cancel_flags);
3123 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3126 * Only increment the parent directory vnode count if
3127 * we didn't bump it in looking up cdp. The only time
3128 * we don't bump it is when we're looking up ".".
3134 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3136 ASSERT(cdp->i_d.di_nlink >= 2);
3137 if (cdp->i_d.di_nlink != 2) {
3138 error = XFS_ERROR(ENOTEMPTY);
3141 if (!xfs_dir_isempty(cdp)) {
3142 error = XFS_ERROR(ENOTEMPTY);
3146 error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
3147 &first_block, &free_list, resblks);
3151 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3154 * Bump the in memory generation count on the parent
3155 * directory so that other can know that it has changed.
3160 * Drop the link from cdp's "..".
3162 error = xfs_droplink(tp, dp);
3168 * Drop the link from dp to cdp.
3170 error = xfs_droplink(tp, cdp);
3176 * Drop the "." link from cdp to self.
3178 error = xfs_droplink(tp, cdp);
3183 /* Determine these before committing transaction */
3184 last_cdp_link = (cdp)->i_d.di_nlink==0;
3187 * Take an extra ref on the child vnode so that it
3188 * does not go to xfs_inactive() from within the commit.
3193 * If this is a synchronous mount, make sure that the
3194 * rmdir transaction goes to disk before returning to
3197 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3198 xfs_trans_set_sync(tp);
3201 error = xfs_bmap_finish (&tp, &free_list, first_block, &committed);
3203 xfs_bmap_cancel(&free_list);
3204 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3210 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
3218 * Let interposed file systems know about removed links.
3220 bhv_vop_link_removed(XFS_ITOV(cdp), dir_vp, last_cdp_link);
3224 /* Fall through to std_return with error = 0 or the errno
3225 * from xfs_trans_commit. */
3227 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_POSTREMOVE)) {
3228 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3229 dir_vp, DM_RIGHT_NULL,
3230 NULL, DM_RIGHT_NULL,
3231 name, NULL, dm_di_mode,
3237 xfs_bmap_cancel(&free_list);
3238 cancel_flags |= XFS_TRANS_ABORT;
3242 xfs_trans_cancel(tp, cancel_flags);
3248 * Read dp's entries starting at uiop->uio_offset and translate them into
3249 * bufsize bytes worth of struct dirents starting at bufbase.
3253 bhv_desc_t *dir_bdp,
3259 xfs_trans_t *tp = NULL;
3263 vn_trace_entry(BHV_TO_VNODE(dir_bdp), __FUNCTION__,
3264 (inst_t *)__return_address);
3265 dp = XFS_BHVTOI(dir_bdp);
3267 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
3268 return XFS_ERROR(EIO);
3270 lock_mode = xfs_ilock_map_shared(dp);
3271 error = xfs_dir_getdents(tp, dp, uiop, eofp);
3272 xfs_iunlock_map_shared(dp, lock_mode);
3279 bhv_desc_t *dir_bdp,
3280 bhv_vname_t *dentry,
3292 xfs_bmap_free_t free_list;
3293 xfs_fsblock_t first_block;
3294 boolean_t dp_joined_to_trans;
3295 bhv_vnode_t *dir_vp;
3298 xfs_fileoff_t first_fsb;
3299 xfs_filblks_t fs_blocks;
3301 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3308 struct xfs_dquot *udqp, *gdqp;
3310 char *link_name = VNAME(dentry);
3314 dir_vp = BHV_TO_VNODE(dir_bdp);
3315 dp = XFS_BHVTOI(dir_bdp);
3316 dp_joined_to_trans = B_FALSE;
3321 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3325 if (XFS_FORCED_SHUTDOWN(mp))
3326 return XFS_ERROR(EIO);
3328 link_namelen = VNAMELEN(dentry);
3331 * Check component lengths of the target path name.
3333 pathlen = strlen(target_path);
3334 if (pathlen >= MAXPATHLEN) /* total string too long */
3335 return XFS_ERROR(ENAMETOOLONG);
3336 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3340 for (total = 0, path = target_path; total < pathlen;) {
3344 while(*path == '/') {
3350 * Count up to the next slash or end of path.
3351 * Error out if the component is bigger than MAXNAMELEN.
3353 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3354 if (++len >= MAXNAMELEN) {
3355 error = ENAMETOOLONG;
3362 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_SYMLINK)) {
3363 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3364 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3365 link_name, target_path, 0, 0, 0);
3370 /* Return through std_return after this point. */
3373 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3374 prid = dp->i_d.di_projid;
3375 else if (vap->va_mask & XFS_AT_PROJID)
3376 prid = (xfs_prid_t)vap->va_projid;
3378 prid = (xfs_prid_t)dfltprid;
3381 * Make sure that we have allocated dquot(s) on disk.
3383 error = XFS_QM_DQVOPALLOC(mp, dp,
3384 current_fsuid(credp), current_fsgid(credp), prid,
3385 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3389 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3390 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3392 * The symlink will fit into the inode data fork?
3393 * There can't be any attributes so we get the whole variable part.
3395 if (pathlen <= XFS_LITINO(mp))
3398 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3399 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3400 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3401 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3402 if (error == ENOSPC && fs_blocks == 0) {
3404 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3405 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3413 xfs_ilock(dp, XFS_ILOCK_EXCL);
3416 * Check whether the directory allows new symlinks or not.
3418 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3419 error = XFS_ERROR(EPERM);
3424 * Reserve disk quota : blocks and inode.
3426 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3431 * Check for ability to enter directory entry, if no space reserved.
3434 (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
3437 * Initialize the bmap freelist prior to calling either
3438 * bmapi or the directory create code.
3440 XFS_BMAP_INIT(&free_list, &first_block);
3443 * Allocate an inode for the symlink.
3445 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (vap->va_mode&~S_IFMT),
3446 1, 0, credp, prid, resblks > 0, &ip, NULL);
3448 if (error == ENOSPC)
3455 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3456 dp_joined_to_trans = B_TRUE;
3459 * Also attach the dquot(s) to it, if applicable.
3461 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3464 resblks -= XFS_IALLOC_SPACE_RES(mp);
3466 * If the symlink will fit into the inode, write it inline.
3468 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3469 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3470 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3471 ip->i_d.di_size = pathlen;
3474 * The inode was initially created in extent format.
3476 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3477 ip->i_df.if_flags |= XFS_IFINLINE;
3479 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3480 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3484 nmaps = SYMLINK_MAPS;
3486 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3487 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3488 &first_block, resblks, mval, &nmaps,
3495 resblks -= fs_blocks;
3496 ip->i_d.di_size = pathlen;
3497 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3499 cur_chunk = target_path;
3500 for (n = 0; n < nmaps; n++) {
3501 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3502 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3503 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3504 BTOBB(byte_cnt), 0);
3505 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3506 if (pathlen < byte_cnt) {
3509 pathlen -= byte_cnt;
3511 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3512 cur_chunk += byte_cnt;
3514 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3519 * Create the directory entry for the symlink.
3521 error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
3522 &first_block, &free_list, resblks);
3525 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3526 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3529 * Bump the in memory version number of the parent directory
3530 * so that other processes accessing it will recognize that
3531 * the directory has changed.
3536 * If this is a synchronous mount, make sure that the
3537 * symlink transaction goes to disk before returning to
3540 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3541 xfs_trans_set_sync(tp);
3545 * xfs_trans_commit normally decrements the vnode ref count
3546 * when it unlocks the inode. Since we want to return the
3547 * vnode to the caller, we bump the vnode ref count now.
3551 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
3555 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
3556 XFS_QM_DQRELE(mp, udqp);
3557 XFS_QM_DQRELE(mp, gdqp);
3559 /* Fall through to std_return with error = 0 or errno from
3560 * xfs_trans_commit */
3562 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
3563 DM_EVENT_POSTSYMLINK)) {
3564 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3565 dir_vp, DM_RIGHT_NULL,
3566 error ? NULL : XFS_ITOV(ip),
3567 DM_RIGHT_NULL, link_name, target_path,
3583 xfs_bmap_cancel(&free_list);
3584 cancel_flags |= XFS_TRANS_ABORT;
3586 xfs_trans_cancel(tp, cancel_flags);
3587 XFS_QM_DQRELE(mp, udqp);
3588 XFS_QM_DQRELE(mp, gdqp);
3590 if (!dp_joined_to_trans && (dp != NULL)) {
3591 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3601 * A fid routine that takes a pointer to a previously allocated
3602 * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
3612 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
3613 (inst_t *)__return_address);
3614 ASSERT(sizeof(fid_t) >= sizeof(xfs_fid2_t));
3616 xfid = (xfs_fid2_t *)fidp;
3617 ip = XFS_BHVTOI(bdp);
3618 xfid->fid_len = sizeof(xfs_fid2_t) - sizeof(xfid->fid_len);
3621 * use memcpy because the inode is a long long and there's no
3622 * assurance that xfid->fid_ino is properly aligned.
3624 memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
3625 xfid->fid_gen = ip->i_d.di_gen;
3637 bhv_vrwlock_t locktype)
3642 vp = BHV_TO_VNODE(bdp);
3645 ip = XFS_BHVTOI(bdp);
3646 if (locktype == VRWLOCK_WRITE) {
3647 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3648 } else if (locktype == VRWLOCK_TRY_READ) {
3649 return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
3650 } else if (locktype == VRWLOCK_TRY_WRITE) {
3651 return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
3653 ASSERT((locktype == VRWLOCK_READ) ||
3654 (locktype == VRWLOCK_WRITE_DIRECT));
3655 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3668 bhv_vrwlock_t locktype)
3673 vp = BHV_TO_VNODE(bdp);
3676 ip = XFS_BHVTOI(bdp);
3677 if (locktype == VRWLOCK_WRITE) {
3679 * In the write case, we may have added a new entry to
3680 * the reference cache. This might store a pointer to
3681 * an inode to be released in this inode. If it is there,
3682 * clear the pointer and release the inode after unlocking
3685 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3687 ASSERT((locktype == VRWLOCK_READ) ||
3688 (locktype == VRWLOCK_WRITE_DIRECT));
3689 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3701 xfs_inode_log_item_t *iip;
3704 ip = XFS_BHVTOI(bdp);
3708 if (XFS_FORCED_SHUTDOWN(mp))
3709 return XFS_ERROR(EIO);
3712 * Bypass inodes which have already been cleaned by
3713 * the inode flush clustering code inside xfs_iflush
3715 if ((ip->i_update_core == 0) &&
3716 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3719 if (flags & FLUSH_LOG) {
3720 if (iip && iip->ili_last_lsn) {
3721 xlog_t *log = mp->m_log;
3723 int s, log_flags = XFS_LOG_FORCE;
3725 s = GRANT_LOCK(log);
3726 sync_lsn = log->l_last_sync_lsn;
3727 GRANT_UNLOCK(log, s);
3729 if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) <= 0))
3732 if (flags & FLUSH_SYNC)
3733 log_flags |= XFS_LOG_SYNC;
3734 return xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3739 * We make this non-blocking if the inode is contended,
3740 * return EAGAIN to indicate to the caller that they
3741 * did not succeed. This prevents the flush path from
3742 * blocking on inodes inside another operation right
3743 * now, they get caught later by xfs_sync.
3745 if (flags & FLUSH_INODE) {
3748 if (xfs_ipincount(ip))
3751 if (flags & FLUSH_SYNC) {
3752 xfs_ilock(ip, XFS_ILOCK_SHARED);
3754 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3755 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3756 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3763 if (flags & FLUSH_SYNC)
3764 flush_flags = XFS_IFLUSH_SYNC;
3766 flush_flags = XFS_IFLUSH_ASYNC;
3768 error = xfs_iflush(ip, flush_flags);
3769 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3787 if (!capable(CAP_SYS_ADMIN))
3788 return XFS_ERROR(EPERM);
3790 ip = XFS_BHVTOI(bdp);
3793 if (XFS_FORCED_SHUTDOWN(mp))
3794 return XFS_ERROR(EIO);
3796 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3797 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3799 xfs_trans_cancel(tp, 0);
3802 xfs_ilock(ip, XFS_ILOCK_EXCL);
3803 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3805 ip->i_iocore.io_dmevmask = ip->i_d.di_dmevmask = evmask;
3806 ip->i_iocore.io_dmstate = ip->i_d.di_dmstate = state;
3808 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3810 error = xfs_trans_commit(tp, 0, NULL);
3822 vp = BHV_TO_VNODE(bdp);
3823 ip = XFS_BHVTOI(bdp);
3825 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
3827 ASSERT(!VN_MAPPED(vp));
3829 /* bad inode, get out here ASAP */
3837 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
3840 * Make sure the atime in the XFS inode is correct before freeing the
3843 xfs_synchronize_atime(ip);
3846 * If we have nothing to flush with this inode then complete the
3847 * teardown now, otherwise break the link between the xfs inode and the
3848 * linux inode and clean up the xfs inode later. This avoids flushing
3849 * the inode to disk during the delete operation itself.
3851 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3852 * first to ensure that xfs_iunpin() will never see an xfs inode
3853 * that has a linux inode being reclaimed. Synchronisation is provided
3854 * by the i_flags_lock.
3856 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3857 xfs_ilock(ip, XFS_ILOCK_EXCL);
3859 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3861 xfs_mount_t *mp = ip->i_mount;
3863 /* Protect sync and unpin from us */
3864 XFS_MOUNT_ILOCK(mp);
3865 spin_lock(&ip->i_flags_lock);
3866 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
3867 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
3868 spin_unlock(&ip->i_flags_lock);
3869 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
3870 XFS_MOUNT_IUNLOCK(mp);
3881 xfs_ihash_t *ih = ip->i_hash;
3882 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
3885 if (vp && VN_BAD(vp))
3888 /* The hash lock here protects a thread in xfs_iget_core from
3889 * racing with us on linking the inode back with a vnode.
3890 * Once we have the XFS_IRECLAIM flag set it will not touch
3893 write_lock(&ih->ih_lock);
3894 spin_lock(&ip->i_flags_lock);
3895 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3896 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
3897 spin_unlock(&ip->i_flags_lock);
3898 write_unlock(&ih->ih_lock);
3901 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3905 __xfs_iflags_set(ip, XFS_IRECLAIM);
3906 spin_unlock(&ip->i_flags_lock);
3907 write_unlock(&ih->ih_lock);
3910 * If the inode is still dirty, then flush it out. If the inode
3911 * is not in the AIL, then it will be OK to flush it delwri as
3912 * long as xfs_iflush() does not keep any references to the inode.
3913 * We leave that decision up to xfs_iflush() since it has the
3914 * knowledge of whether it's OK to simply do a delwri flush of
3915 * the inode or whether we need to wait until the inode is
3916 * pulled from the AIL.
3917 * We get the flush lock regardless, though, just to make sure
3918 * we don't free it while it is being flushed.
3920 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3922 xfs_ilock(ip, XFS_ILOCK_EXCL);
3926 if (ip->i_update_core ||
3927 ((ip->i_itemp != NULL) &&
3928 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3929 error = xfs_iflush(ip, sync_mode);
3931 * If we hit an error, typically because of filesystem
3932 * shutdown, we don't need to let vn_reclaim to know
3933 * because we're gonna reclaim the inode anyway.
3936 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3939 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3942 ASSERT(ip->i_update_core == 0);
3943 ASSERT(ip->i_itemp == NULL ||
3944 ip->i_itemp->ili_format.ilf_fields == 0);
3945 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3946 } else if (locked) {
3948 * We are not interested in doing an iflush if we're
3949 * in the process of shutting down the filesystem forcibly.
3950 * So, just reclaim the inode.
3953 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3962 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3965 xfs_inode_t *ip, *n;
3970 XFS_MOUNT_ILOCK(mp);
3971 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3973 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3975 if (xfs_ipincount(ip) ||
3976 !xfs_iflock_nowait(ip)) {
3977 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3981 XFS_MOUNT_IUNLOCK(mp);
3982 if (xfs_finish_reclaim(ip, noblock,
3983 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3992 XFS_MOUNT_IUNLOCK(mp);
3997 * xfs_alloc_file_space()
3998 * This routine allocates disk space for the given file.
4000 * If alloc_type == 0, this request is for an ALLOCSP type
4001 * request which will change the file size. In this case, no
4002 * DMAPI event will be generated by the call. A TRUNCATE event
4003 * will be generated later by xfs_setattr.
4005 * If alloc_type != 0, this request is for a RESVSP type
4006 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
4007 * lower block boundary byte address is less than the file's
4016 xfs_alloc_file_space(
4023 xfs_mount_t *mp = ip->i_mount;
4025 xfs_filblks_t allocated_fsb;
4026 xfs_filblks_t allocatesize_fsb;
4027 xfs_extlen_t extsz, temp;
4028 xfs_fileoff_t startoffset_fsb;
4029 xfs_fsblock_t firstfsb;
4035 xfs_bmbt_irec_t imaps[1], *imapp;
4036 xfs_bmap_free_t free_list;
4037 uint qblocks, resblks, resrtextents;
4041 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
4043 if (XFS_FORCED_SHUTDOWN(mp))
4044 return XFS_ERROR(EIO);
4046 rt = XFS_IS_REALTIME_INODE(ip);
4048 if (!(extsz = ip->i_d.di_extsize))
4049 extsz = mp->m_sb.sb_rextsize;
4051 extsz = ip->i_d.di_extsize;
4054 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4058 return XFS_ERROR(EINVAL);
4064 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
4065 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
4066 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
4068 /* Generate a DMAPI event if needed. */
4069 if (alloc_type != 0 && offset < ip->i_d.di_size &&
4070 (attr_flags&ATTR_DMI) == 0 &&
4071 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4072 xfs_off_t end_dmi_offset;
4074 end_dmi_offset = offset+len;
4075 if (end_dmi_offset > ip->i_d.di_size)
4076 end_dmi_offset = ip->i_d.di_size;
4077 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
4078 offset, end_dmi_offset - offset,
4085 * Allocate file space until done or until there is an error
4088 while (allocatesize_fsb && !error) {
4092 * Determine space reservations for data/realtime.
4094 if (unlikely(extsz)) {
4095 s = startoffset_fsb;
4098 e = startoffset_fsb + allocatesize_fsb;
4099 if ((temp = do_mod(startoffset_fsb, extsz)))
4101 if ((temp = do_mod(e, extsz)))
4105 e = allocatesize_fsb;
4109 resrtextents = qblocks = (uint)(e - s);
4110 resrtextents /= mp->m_sb.sb_rextsize;
4111 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4112 quota_flag = XFS_QMOPT_RES_RTBLKS;
4115 resblks = qblocks = \
4116 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
4117 quota_flag = XFS_QMOPT_RES_REGBLKS;
4121 * Allocate and setup the transaction.
4123 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4124 error = xfs_trans_reserve(tp, resblks,
4125 XFS_WRITE_LOG_RES(mp), resrtextents,
4126 XFS_TRANS_PERM_LOG_RES,
4127 XFS_WRITE_LOG_COUNT);
4129 * Check for running out of space
4133 * Free the transaction structure.
4135 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4136 xfs_trans_cancel(tp, 0);
4139 xfs_ilock(ip, XFS_ILOCK_EXCL);
4140 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
4141 qblocks, 0, quota_flag);
4145 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4146 xfs_trans_ihold(tp, ip);
4149 * Issue the xfs_bmapi() call to allocate the blocks
4151 XFS_BMAP_INIT(&free_list, &firstfsb);
4152 error = XFS_BMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4153 allocatesize_fsb, bmapi_flag,
4154 &firstfsb, 0, imapp, &nimaps,
4161 * Complete the transaction
4163 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
4168 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
4169 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4174 allocated_fsb = imapp->br_blockcount;
4177 error = XFS_ERROR(ENOSPC);
4181 startoffset_fsb += allocated_fsb;
4182 allocatesize_fsb -= allocated_fsb;
4185 if (error == ENOSPC && (attr_flags&ATTR_DMI) == 0 &&
4186 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_NOSPACE)) {
4188 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
4189 XFS_ITOV(ip), DM_RIGHT_NULL,
4190 XFS_ITOV(ip), DM_RIGHT_NULL,
4191 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
4193 goto retry; /* Maybe DMAPI app. has made space */
4194 /* else fall through with error from XFS_SEND_DATA */
4199 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
4200 xfs_bmap_cancel(&free_list);
4201 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
4203 error1: /* Just cancel transaction */
4204 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4205 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4206 goto dmapi_enospc_check;
4210 * Zero file bytes between startoff and endoff inclusive.
4211 * The iolock is held exclusive and no blocks are buffered.
4214 xfs_zero_remaining_bytes(
4219 xfs_bmbt_irec_t imap;
4220 xfs_fileoff_t offset_fsb;
4221 xfs_off_t lastoffset;
4224 xfs_mount_t *mp = ip->i_mount;
4228 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
4229 ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
4230 mp->m_rtdev_targp : mp->m_ddev_targp);
4232 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4233 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4235 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, offset_fsb, 1, 0,
4236 NULL, 0, &imap, &nimap, NULL, NULL);
4237 if (error || nimap < 1)
4239 ASSERT(imap.br_blockcount >= 1);
4240 ASSERT(imap.br_startoff == offset_fsb);
4241 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4242 if (lastoffset > endoff)
4243 lastoffset = endoff;
4244 if (imap.br_startblock == HOLESTARTBLOCK)
4246 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4247 if (imap.br_state == XFS_EXT_UNWRITTEN)
4250 XFS_BUF_UNWRITE(bp);
4252 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4254 if ((error = xfs_iowait(bp))) {
4255 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4256 mp, bp, XFS_BUF_ADDR(bp));
4259 memset(XFS_BUF_PTR(bp) +
4260 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4261 0, lastoffset - offset + 1);
4266 if ((error = xfs_iowait(bp))) {
4267 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4268 mp, bp, XFS_BUF_ADDR(bp));
4277 * xfs_free_file_space()
4278 * This routine frees disk space for the given file.
4280 * This routine is only called by xfs_change_file_space
4281 * for an UNRESVSP type call.
4289 xfs_free_file_space(
4298 xfs_off_t end_dmi_offset;
4299 xfs_fileoff_t endoffset_fsb;
4301 xfs_fsblock_t firstfsb;
4302 xfs_bmap_free_t free_list;
4304 xfs_bmbt_irec_t imap;
4312 xfs_fileoff_t startoffset_fsb;
4314 int need_iolock = 1;
4319 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4321 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4325 if (len <= 0) /* if nothing being freed */
4327 rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4328 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4329 end_dmi_offset = offset + len;
4330 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4332 if (offset < ip->i_d.di_size &&
4333 (attr_flags & ATTR_DMI) == 0 &&
4334 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4335 if (end_dmi_offset > ip->i_d.di_size)
4336 end_dmi_offset = ip->i_d.di_size;
4337 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
4338 offset, end_dmi_offset - offset,
4339 AT_DELAY_FLAG(attr_flags), NULL);
4344 if (attr_flags & ATTR_NOLOCK)
4347 xfs_ilock(ip, XFS_IOLOCK_EXCL);
4348 vn_iowait(vp); /* wait for the completion of any pending DIOs */
4351 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, NBPP);
4352 ilen = len + (offset & (rounding - 1));
4353 ioffset = offset & ~(rounding - 1);
4354 if (ilen & (rounding - 1))
4355 ilen = (ilen + rounding) & ~(rounding - 1);
4357 if (VN_CACHED(vp) != 0) {
4358 xfs_inval_cached_trace(&ip->i_iocore, ioffset, -1,
4359 ctooff(offtoct(ioffset)), -1);
4360 bhv_vop_flushinval_pages(vp, ctooff(offtoct(ioffset)),
4361 -1, FI_REMAPF_LOCKED);
4365 * Need to zero the stuff we're not freeing, on disk.
4366 * If its a realtime file & can't use unwritten extents then we
4367 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4368 * will take care of it for us.
4370 if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4372 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, startoffset_fsb,
4373 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4375 goto out_unlock_iolock;
4376 ASSERT(nimap == 0 || nimap == 1);
4377 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4380 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4381 block = imap.br_startblock;
4382 mod = do_div(block, mp->m_sb.sb_rextsize);
4384 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4387 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, endoffset_fsb - 1,
4388 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4390 goto out_unlock_iolock;
4391 ASSERT(nimap == 0 || nimap == 1);
4392 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4393 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4395 if (mod && (mod != mp->m_sb.sb_rextsize))
4396 endoffset_fsb -= mod;
4399 if ((done = (endoffset_fsb <= startoffset_fsb)))
4401 * One contiguous piece to clear
4403 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4406 * Some full blocks, possibly two pieces to clear
4408 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4409 error = xfs_zero_remaining_bytes(ip, offset,
4410 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4412 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4413 error = xfs_zero_remaining_bytes(ip,
4414 XFS_FSB_TO_B(mp, endoffset_fsb),
4419 * free file space until done or until there is an error
4421 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4422 while (!error && !done) {
4425 * allocate and setup the transaction
4427 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4428 error = xfs_trans_reserve(tp,
4430 XFS_WRITE_LOG_RES(mp),
4432 XFS_TRANS_PERM_LOG_RES,
4433 XFS_WRITE_LOG_COUNT);
4436 * check for running out of space
4440 * Free the transaction structure.
4442 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4443 xfs_trans_cancel(tp, 0);
4446 xfs_ilock(ip, XFS_ILOCK_EXCL);
4447 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4448 ip->i_udquot, ip->i_gdquot, resblks, 0,
4449 XFS_QMOPT_RES_REGBLKS);
4453 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4454 xfs_trans_ihold(tp, ip);
4457 * issue the bunmapi() call to free the blocks
4459 XFS_BMAP_INIT(&free_list, &firstfsb);
4460 error = XFS_BUNMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4461 endoffset_fsb - startoffset_fsb,
4462 0, 2, &firstfsb, &free_list, NULL, &done);
4468 * complete the transaction
4470 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
4475 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
4476 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4481 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4485 xfs_bmap_cancel(&free_list);
4487 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4488 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4494 * xfs_change_file_space()
4495 * This routine allocates or frees disk space for the given file.
4496 * The user specified parameters are checked for alignment and size
4505 xfs_change_file_space(
4519 xfs_off_t startoffset;
4525 vp = BHV_TO_VNODE(bdp);
4526 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4528 ip = XFS_BHVTOI(bdp);
4532 * must be a regular file and have write permission
4535 return XFS_ERROR(EINVAL);
4537 xfs_ilock(ip, XFS_ILOCK_SHARED);
4539 if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
4540 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4544 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4546 switch (bf->l_whence) {
4547 case 0: /*SEEK_SET*/
4549 case 1: /*SEEK_CUR*/
4550 bf->l_start += offset;
4552 case 2: /*SEEK_END*/
4553 bf->l_start += ip->i_d.di_size;
4556 return XFS_ERROR(EINVAL);
4559 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4561 if ( (bf->l_start < 0)
4562 || (bf->l_start > XFS_MAXIOFFSET(mp))
4563 || (bf->l_start + llen < 0)
4564 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4565 return XFS_ERROR(EINVAL);
4569 startoffset = bf->l_start;
4570 fsize = ip->i_d.di_size;
4573 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4575 * These calls do NOT zero the data space allocated to the file,
4576 * nor do they change the file size.
4578 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4580 * These calls cause the new file data to be zeroed and the file
4581 * size to be changed.
4583 setprealloc = clrprealloc = 0;
4586 case XFS_IOC_RESVSP:
4587 case XFS_IOC_RESVSP64:
4588 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4595 case XFS_IOC_UNRESVSP:
4596 case XFS_IOC_UNRESVSP64:
4597 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4602 case XFS_IOC_ALLOCSP:
4603 case XFS_IOC_ALLOCSP64:
4604 case XFS_IOC_FREESP:
4605 case XFS_IOC_FREESP64:
4606 if (startoffset > fsize) {
4607 error = xfs_alloc_file_space(ip, fsize,
4608 startoffset - fsize, 0, attr_flags);
4613 va.va_mask = XFS_AT_SIZE;
4614 va.va_size = startoffset;
4616 error = xfs_setattr(bdp, &va, attr_flags, credp);
4626 return XFS_ERROR(EINVAL);
4630 * update the inode timestamp, mode, and prealloc flag bits
4632 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4634 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4637 xfs_trans_cancel(tp, 0);
4641 xfs_ilock(ip, XFS_ILOCK_EXCL);
4643 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4644 xfs_trans_ihold(tp, ip);
4646 if ((attr_flags & ATTR_DMI) == 0) {
4647 ip->i_d.di_mode &= ~S_ISUID;
4650 * Note that we don't have to worry about mandatory
4651 * file locking being disabled here because we only
4652 * clear the S_ISGID bit if the Group execute bit is
4653 * on, but if it was on then mandatory locking wouldn't
4654 * have been enabled.
4656 if (ip->i_d.di_mode & S_IXGRP)
4657 ip->i_d.di_mode &= ~S_ISGID;
4659 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4662 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4663 else if (clrprealloc)
4664 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4666 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4667 xfs_trans_set_sync(tp);
4669 error = xfs_trans_commit(tp, 0, NULL);
4671 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4676 bhv_vnodeops_t xfs_vnodeops = {
4677 BHV_IDENTITY_INIT(VN_BHV_XFS,VNODE_POSITION_XFS),
4678 .vop_open = xfs_open,
4679 .vop_close = xfs_close,
4680 .vop_read = xfs_read,
4681 #ifdef HAVE_SENDFILE
4682 .vop_sendfile = xfs_sendfile,
4685 .vop_splice_read = xfs_splice_read,
4686 .vop_splice_write = xfs_splice_write,
4688 .vop_write = xfs_write,
4689 .vop_ioctl = xfs_ioctl,
4690 .vop_getattr = xfs_getattr,
4691 .vop_setattr = xfs_setattr,
4692 .vop_access = xfs_access,
4693 .vop_lookup = xfs_lookup,
4694 .vop_create = xfs_create,
4695 .vop_remove = xfs_remove,
4696 .vop_link = xfs_link,
4697 .vop_rename = xfs_rename,
4698 .vop_mkdir = xfs_mkdir,
4699 .vop_rmdir = xfs_rmdir,
4700 .vop_readdir = xfs_readdir,
4701 .vop_symlink = xfs_symlink,
4702 .vop_readlink = xfs_readlink,
4703 .vop_fsync = xfs_fsync,
4704 .vop_inactive = xfs_inactive,
4705 .vop_fid2 = xfs_fid2,
4706 .vop_rwlock = xfs_rwlock,
4707 .vop_rwunlock = xfs_rwunlock,
4708 .vop_bmap = xfs_bmap,
4709 .vop_reclaim = xfs_reclaim,
4710 .vop_attr_get = xfs_attr_get,
4711 .vop_attr_set = xfs_attr_set,
4712 .vop_attr_remove = xfs_attr_remove,
4713 .vop_attr_list = xfs_attr_list,
4714 .vop_link_removed = (vop_link_removed_t)fs_noval,
4715 .vop_vnode_change = (vop_vnode_change_t)fs_noval,
4716 .vop_tosspages = fs_tosspages,
4717 .vop_flushinval_pages = fs_flushinval_pages,
4718 .vop_flush_pages = fs_flush_pages,
4719 .vop_release = xfs_release,
4720 .vop_iflush = xfs_inode_flush,