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"
61 bhv_vnode_t *vp = BHV_TO_VNODE(bdp);
62 xfs_inode_t *ip = XFS_BHVTOI(bdp);
64 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
65 return XFS_ERROR(EIO);
68 * If it's a directory with any blocks, read-ahead block 0
69 * as we're almost certain to have the next operation be a read there.
71 if (VN_ISDIR(vp) && ip->i_d.di_nextents > 0) {
72 mode = xfs_ilock_map_shared(ip);
73 if (ip->i_d.di_nextents > 0)
74 (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
75 xfs_iunlock(ip, mode);
94 vp = BHV_TO_VNODE(bdp);
95 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
100 if (XFS_FORCED_SHUTDOWN(mp))
101 return XFS_ERROR(EIO);
103 if (!(flags & ATTR_LAZY))
104 xfs_ilock(ip, XFS_ILOCK_SHARED);
106 vap->va_size = XFS_ISIZE(ip);
107 if (vap->va_mask == XFS_AT_SIZE)
111 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
112 vap->va_nodeid = ip->i_ino;
114 vap->va_nodeid += mp->m_inoadd;
116 vap->va_nlink = ip->i_d.di_nlink;
119 * Quick exit for non-stat callers
122 ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
123 XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
127 * Copy from in-core inode.
129 vap->va_mode = ip->i_d.di_mode;
130 vap->va_uid = ip->i_d.di_uid;
131 vap->va_gid = ip->i_d.di_gid;
132 vap->va_projid = ip->i_d.di_projid;
135 * Check vnode type block/char vs. everything else.
137 switch (ip->i_d.di_mode & S_IFMT) {
140 vap->va_rdev = ip->i_df.if_u2.if_rdev;
141 vap->va_blocksize = BLKDEV_IOSIZE;
146 if (!(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
147 vap->va_blocksize = xfs_preferred_iosize(mp);
151 * If the file blocks are being allocated from a
152 * realtime partition, then return the inode's
153 * realtime extent size or the realtime volume's
157 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
162 vn_atime_to_timespec(vp, &vap->va_atime);
163 vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
164 vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
165 vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
166 vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
169 * Exit for stat callers. See if any of the rest of the fields
170 * to be filled in are needed.
173 (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
174 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
178 * Convert di_flags to xflags.
180 vap->va_xflags = xfs_ip2xflags(ip);
183 * Exit for inode revalidate. See if any of the rest of
184 * the fields to be filled in are needed.
187 (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
188 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
191 vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
193 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
194 ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
198 (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
199 ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
200 ip->i_d.di_anextents;
202 vap->va_anextents = 0;
203 vap->va_gen = ip->i_d.di_gen;
206 if (!(flags & ATTR_LAZY))
207 xfs_iunlock(ip, XFS_ILOCK_SHARED);
233 xfs_prid_t projid=0, iprojid=0;
234 int mandlock_before, mandlock_after;
235 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
239 vp = BHV_TO_VNODE(bdp);
240 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
242 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
243 return XFS_ERROR(EROFS);
246 * Cannot set certain attributes.
249 if (mask & XFS_AT_NOSET) {
250 return XFS_ERROR(EINVAL);
253 ip = XFS_BHVTOI(bdp);
256 if (XFS_FORCED_SHUTDOWN(mp))
257 return XFS_ERROR(EIO);
260 * Timestamps do not need to be logged and hence do not
261 * need to be done within a transaction.
263 if (mask & XFS_AT_UPDTIMES) {
264 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
265 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
266 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
267 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
268 xfs_ichgtime(ip, timeflags);
272 olddquot1 = olddquot2 = NULL;
276 * If disk quotas is on, we make sure that the dquots do exist on disk,
277 * before we start any other transactions. Trying to do this later
278 * is messy. We don't care to take a readlock to look at the ids
279 * in inode here, because we can't hold it across the trans_reserve.
280 * If the IDs do change before we take the ilock, we're covered
281 * because the i_*dquot fields will get updated anyway.
283 if (XFS_IS_QUOTA_ON(mp) &&
284 (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
287 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
289 qflags |= XFS_QMOPT_UQUOTA;
291 uid = ip->i_d.di_uid;
293 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
295 qflags |= XFS_QMOPT_GQUOTA;
297 gid = ip->i_d.di_gid;
299 if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
300 projid = vap->va_projid;
301 qflags |= XFS_QMOPT_PQUOTA;
303 projid = ip->i_d.di_projid;
306 * We take a reference when we initialize udqp and gdqp,
307 * so it is important that we never blindly double trip on
308 * the same variable. See xfs_create() for an example.
310 ASSERT(udqp == NULL);
311 ASSERT(gdqp == NULL);
312 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
319 * For the other attributes, we acquire the inode lock and
320 * first do an error checking pass.
323 lock_flags = XFS_ILOCK_EXCL;
324 if (flags & ATTR_NOLOCK)
326 if (!(mask & XFS_AT_SIZE)) {
327 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
328 (mp->m_flags & XFS_MOUNT_WSYNC)) {
329 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
331 if ((code = xfs_trans_reserve(tp, 0,
332 XFS_ICHANGE_LOG_RES(mp), 0,
339 if (DM_EVENT_ENABLED (vp->v_vfsp, ip, DM_EVENT_TRUNCATE) &&
340 !(flags & ATTR_DMI)) {
341 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
342 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
343 vap->va_size, 0, dmflags, NULL);
350 lock_flags |= XFS_IOLOCK_EXCL;
353 xfs_ilock(ip, lock_flags);
355 /* boolean: are we the file owner? */
356 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
359 * Change various properties of a file.
360 * Only the owner or users with CAP_FOWNER
361 * capability may do these things.
364 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
365 XFS_AT_GID|XFS_AT_PROJID)) {
367 * CAP_FOWNER overrides the following restrictions:
369 * The user ID of the calling process must be equal
370 * to the file owner ID, except in cases where the
371 * CAP_FSETID capability is applicable.
373 if (!file_owner && !capable(CAP_FOWNER)) {
374 code = XFS_ERROR(EPERM);
379 * CAP_FSETID overrides the following restrictions:
381 * The effective user ID of the calling process shall match
382 * the file owner when setting the set-user-ID and
383 * set-group-ID bits on that file.
385 * The effective group ID or one of the supplementary group
386 * IDs of the calling process shall match the group owner of
387 * the file when setting the set-group-ID bit on that file
389 if (mask & XFS_AT_MODE) {
392 if ((vap->va_mode & S_ISUID) && !file_owner)
394 if ((vap->va_mode & S_ISGID) &&
395 !in_group_p((gid_t)ip->i_d.di_gid))
398 /* Linux allows this, Irix doesn't. */
399 if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
402 if (m && !capable(CAP_FSETID))
408 * Change file ownership. Must be the owner or privileged.
409 * If the system was configured with the "restricted_chown"
410 * option, the owner is not permitted to give away the file,
411 * and can change the group id only to a group of which he
412 * or she is a member.
414 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
416 * These IDs could have changed since we last looked at them.
417 * But, we're assured that if the ownership did change
418 * while we didn't have the inode locked, inode's dquot(s)
419 * would have changed also.
421 iuid = ip->i_d.di_uid;
422 iprojid = ip->i_d.di_projid;
423 igid = ip->i_d.di_gid;
424 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
425 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
426 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
430 * CAP_CHOWN overrides the following restrictions:
432 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
433 * shall override the restriction that a process cannot
434 * change the user ID of a file it owns and the restriction
435 * that the group ID supplied to the chown() function
436 * shall be equal to either the group ID or one of the
437 * supplementary group IDs of the calling process.
439 if (restricted_chown &&
440 (iuid != uid || (igid != gid &&
441 !in_group_p((gid_t)gid))) &&
442 !capable(CAP_CHOWN)) {
443 code = XFS_ERROR(EPERM);
447 * Do a quota reservation only if uid/projid/gid is actually
450 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
451 (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
452 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
454 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
455 capable(CAP_FOWNER) ?
456 XFS_QMOPT_FORCE_RES : 0);
457 if (code) /* out of quota */
463 * Truncate file. Must have write permission and not be a directory.
465 if (mask & XFS_AT_SIZE) {
466 /* Short circuit the truncate case for zero length files */
467 if ((vap->va_size == 0) &&
468 (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) {
469 xfs_iunlock(ip, XFS_ILOCK_EXCL);
470 lock_flags &= ~XFS_ILOCK_EXCL;
471 if (mask & XFS_AT_CTIME)
472 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
478 code = XFS_ERROR(EISDIR);
480 } else if (!VN_ISREG(vp)) {
481 code = XFS_ERROR(EINVAL);
485 * Make sure that the dquots are attached to the inode.
487 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
492 * Change file access or modified times.
494 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
496 if ((flags & ATTR_UTIME) &&
497 !capable(CAP_FOWNER)) {
498 code = XFS_ERROR(EPERM);
505 * Change extent size or realtime flag.
507 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
509 * Can't change extent size if any extents are allocated.
511 if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
512 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
514 code = XFS_ERROR(EINVAL); /* EFBIG? */
519 * Can't change realtime flag if any extents are allocated.
521 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
522 (mask & XFS_AT_XFLAGS) &&
523 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) !=
524 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
525 code = XFS_ERROR(EINVAL); /* EFBIG? */
529 * Extent size must be a multiple of the appropriate block
530 * size, if set at all.
532 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
535 if ((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
536 ((mask & XFS_AT_XFLAGS) &&
537 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
538 size = mp->m_sb.sb_rextsize <<
539 mp->m_sb.sb_blocklog;
541 size = mp->m_sb.sb_blocksize;
543 if (vap->va_extsize % size) {
544 code = XFS_ERROR(EINVAL);
549 * If realtime flag is set then must have realtime data.
551 if ((mask & XFS_AT_XFLAGS) &&
552 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
553 if ((mp->m_sb.sb_rblocks == 0) ||
554 (mp->m_sb.sb_rextsize == 0) ||
555 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
556 code = XFS_ERROR(EINVAL);
562 * Can't modify an immutable/append-only file unless
563 * we have appropriate permission.
565 if ((mask & XFS_AT_XFLAGS) &&
567 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
569 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
570 !capable(CAP_LINUX_IMMUTABLE)) {
571 code = XFS_ERROR(EPERM);
577 * Now we can make the changes. Before we join the inode
578 * to the transaction, if XFS_AT_SIZE is set then take care of
579 * the part of the truncation that must be done without the
580 * inode lock. This needs to be done before joining the inode
581 * to the transaction, because the inode cannot be unlocked
582 * once it is a part of the transaction.
584 if (mask & XFS_AT_SIZE) {
586 if ((vap->va_size > ip->i_size) &&
587 (flags & ATTR_NOSIZETOK) == 0) {
588 code = xfs_igrow_start(ip, vap->va_size, credp);
590 xfs_iunlock(ip, XFS_ILOCK_EXCL);
591 vn_iowait(vp); /* wait for the completion of any pending DIOs */
593 code = xfs_itruncate_data(ip, vap->va_size);
596 lock_flags &= ~XFS_ILOCK_EXCL;
597 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
600 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
601 if ((code = xfs_trans_reserve(tp, 0,
602 XFS_ITRUNCATE_LOG_RES(mp), 0,
603 XFS_TRANS_PERM_LOG_RES,
604 XFS_ITRUNCATE_LOG_COUNT))) {
605 xfs_trans_cancel(tp, 0);
607 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
610 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
611 xfs_ilock(ip, XFS_ILOCK_EXCL);
615 xfs_trans_ijoin(tp, ip, lock_flags);
616 xfs_trans_ihold(tp, ip);
619 /* determine whether mandatory locking mode changes */
620 mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
623 * Truncate file. Must have write permission and not be a directory.
625 if (mask & XFS_AT_SIZE) {
626 if (vap->va_size > ip->i_size) {
627 xfs_igrow_finish(tp, ip, vap->va_size,
628 !(flags & ATTR_DMI));
629 } else if ((vap->va_size <= ip->i_size) ||
630 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
632 * signal a sync transaction unless
633 * we're truncating an already unlinked
634 * file on a wsync filesystem
636 code = xfs_itruncate_finish(&tp, ip,
637 (xfs_fsize_t)vap->va_size,
639 ((ip->i_d.di_nlink != 0 ||
640 !(mp->m_flags & XFS_MOUNT_WSYNC))
645 * Truncated "down", so we're removing references
646 * to old data here - if we now delay flushing for
647 * a long time, we expose ourselves unduly to the
648 * notorious NULL files problem. So, we mark this
649 * vnode and flush it when the file is closed, and
650 * do not wait the usual (long) time for writeout.
655 * Have to do this even if the file's size doesn't change.
657 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
661 * Change file access modes.
663 if (mask & XFS_AT_MODE) {
664 ip->i_d.di_mode &= S_IFMT;
665 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
667 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
668 timeflags |= XFS_ICHGTIME_CHG;
672 * Change file ownership. Must be the owner or privileged.
673 * If the system was configured with the "restricted_chown"
674 * option, the owner is not permitted to give away the file,
675 * and can change the group id only to a group of which he
676 * or she is a member.
678 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
680 * CAP_FSETID overrides the following restrictions:
682 * The set-user-ID and set-group-ID bits of a file will be
683 * cleared upon successful return from chown()
685 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
686 !capable(CAP_FSETID)) {
687 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
691 * Change the ownerships and register quota modifications
692 * in the transaction.
695 if (XFS_IS_UQUOTA_ON(mp)) {
696 ASSERT(mask & XFS_AT_UID);
698 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
699 &ip->i_udquot, udqp);
701 ip->i_d.di_uid = uid;
704 if (XFS_IS_GQUOTA_ON(mp)) {
705 ASSERT(!XFS_IS_PQUOTA_ON(mp));
706 ASSERT(mask & XFS_AT_GID);
708 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
709 &ip->i_gdquot, gdqp);
711 ip->i_d.di_gid = gid;
713 if (iprojid != projid) {
714 if (XFS_IS_PQUOTA_ON(mp)) {
715 ASSERT(!XFS_IS_GQUOTA_ON(mp));
716 ASSERT(mask & XFS_AT_PROJID);
718 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
719 &ip->i_gdquot, gdqp);
721 ip->i_d.di_projid = projid;
723 * We may have to rev the inode as well as
724 * the superblock version number since projids didn't
725 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
727 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
728 xfs_bump_ino_vers2(tp, ip);
731 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
732 timeflags |= XFS_ICHGTIME_CHG;
737 * Change file access or modified times.
739 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
740 if (mask & XFS_AT_ATIME) {
741 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
742 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
743 ip->i_update_core = 1;
744 timeflags &= ~XFS_ICHGTIME_ACC;
746 if (mask & XFS_AT_MTIME) {
747 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
748 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
749 timeflags &= ~XFS_ICHGTIME_MOD;
750 timeflags |= XFS_ICHGTIME_CHG;
752 if (tp && (flags & ATTR_UTIME))
753 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
757 * Change XFS-added attributes.
759 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
760 if (mask & XFS_AT_EXTSIZE) {
762 * Converting bytes to fs blocks.
764 ip->i_d.di_extsize = vap->va_extsize >>
765 mp->m_sb.sb_blocklog;
767 if (mask & XFS_AT_XFLAGS) {
770 /* can't set PREALLOC this way, just preserve it */
771 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
772 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
773 di_flags |= XFS_DIFLAG_IMMUTABLE;
774 if (vap->va_xflags & XFS_XFLAG_APPEND)
775 di_flags |= XFS_DIFLAG_APPEND;
776 if (vap->va_xflags & XFS_XFLAG_SYNC)
777 di_flags |= XFS_DIFLAG_SYNC;
778 if (vap->va_xflags & XFS_XFLAG_NOATIME)
779 di_flags |= XFS_DIFLAG_NOATIME;
780 if (vap->va_xflags & XFS_XFLAG_NODUMP)
781 di_flags |= XFS_DIFLAG_NODUMP;
782 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
783 di_flags |= XFS_DIFLAG_PROJINHERIT;
784 if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
785 di_flags |= XFS_DIFLAG_NODEFRAG;
786 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
787 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
788 di_flags |= XFS_DIFLAG_RTINHERIT;
789 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
790 di_flags |= XFS_DIFLAG_NOSYMLINKS;
791 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
792 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
793 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
794 if (vap->va_xflags & XFS_XFLAG_REALTIME) {
795 di_flags |= XFS_DIFLAG_REALTIME;
796 ip->i_iocore.io_flags |= XFS_IOCORE_RT;
798 ip->i_iocore.io_flags &= ~XFS_IOCORE_RT;
800 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
801 di_flags |= XFS_DIFLAG_EXTSIZE;
803 ip->i_d.di_flags = di_flags;
805 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
806 timeflags |= XFS_ICHGTIME_CHG;
810 * Change file inode change time only if XFS_AT_CTIME set
811 * AND we have been called by a DMI function.
814 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
815 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
816 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
817 ip->i_update_core = 1;
818 timeflags &= ~XFS_ICHGTIME_CHG;
822 * Send out timestamp changes that need to be set to the
823 * current time. Not done when called by a DMI function.
825 if (timeflags && !(flags & ATTR_DMI))
826 xfs_ichgtime(ip, timeflags);
828 XFS_STATS_INC(xs_ig_attrchg);
831 * If this is a synchronous mount, make sure that the
832 * transaction goes to disk before returning to the user.
833 * This is slightly sub-optimal in that truncates require
834 * two sync transactions instead of one for wsync filesystems.
835 * One for the truncate and one for the timestamps since we
836 * don't want to change the timestamps unless we're sure the
837 * truncate worked. Truncates are less than 1% of the laddis
838 * mix so this probably isn't worth the trouble to optimize.
842 if (mp->m_flags & XFS_MOUNT_WSYNC)
843 xfs_trans_set_sync(tp);
845 code = xfs_trans_commit(tp, commit_flags);
849 * If the (regular) file's mandatory locking mode changed, then
850 * notify the vnode. We do this under the inode lock to prevent
851 * racing calls to vop_vnode_change.
853 mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
854 if (mandlock_before != mandlock_after) {
855 bhv_vop_vnode_change(vp, VCHANGE_FLAGS_ENF_LOCKING,
859 xfs_iunlock(ip, lock_flags);
862 * Release any dquot(s) the inode had kept before chown.
864 XFS_QM_DQRELE(mp, olddquot1);
865 XFS_QM_DQRELE(mp, olddquot2);
866 XFS_QM_DQRELE(mp, udqp);
867 XFS_QM_DQRELE(mp, gdqp);
873 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_ATTRIBUTE) &&
874 !(flags & ATTR_DMI)) {
875 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
876 NULL, DM_RIGHT_NULL, NULL, NULL,
877 0, 0, AT_DELAY_FLAG(flags));
882 commit_flags |= XFS_TRANS_ABORT;
885 XFS_QM_DQRELE(mp, udqp);
886 XFS_QM_DQRELE(mp, gdqp);
888 xfs_trans_cancel(tp, commit_flags);
890 if (lock_flags != 0) {
891 xfs_iunlock(ip, lock_flags);
899 * Null conversion from vnode mode bits to inode mode bits, as in efs.
910 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
911 (inst_t *)__return_address);
913 ip = XFS_BHVTOI(bdp);
914 xfs_ilock(ip, XFS_ILOCK_SHARED);
915 error = xfs_iaccess(ip, mode, credp);
916 xfs_iunlock(ip, XFS_ILOCK_SHARED);
922 * The maximum pathlen is 1024 bytes. Since the minimum file system
923 * blocksize is 512 bytes, we can get a max of 2 extents back from
926 #define SYMLINK_MAPS 2
947 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
953 vp = BHV_TO_VNODE(bdp);
954 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
956 ip = XFS_BHVTOI(bdp);
959 if (XFS_FORCED_SHUTDOWN(mp))
960 return XFS_ERROR(EIO);
962 xfs_ilock(ip, XFS_ILOCK_SHARED);
964 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
966 offset = uiop->uio_offset;
967 count = uiop->uio_resid;
970 error = XFS_ERROR(EINVAL);
979 * See if the symlink is stored inline.
981 pathlen = (int)ip->i_d.di_size;
983 if (ip->i_df.if_flags & XFS_IFINLINE) {
984 error = xfs_uio_read(ip->i_df.if_u1.if_data, pathlen, uiop);
988 * Symlink not inline. Call bmap to get it in.
990 nmaps = SYMLINK_MAPS;
992 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen),
993 0, NULL, 0, mval, &nmaps, NULL, NULL);
999 for (n = 0; n < nmaps; n++) {
1000 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1001 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1002 bp = xfs_buf_read(mp->m_ddev_targp, d,
1003 BTOBB(byte_cnt), 0);
1004 error = XFS_BUF_GETERROR(bp);
1006 xfs_ioerror_alert("xfs_readlink",
1007 ip->i_mount, bp, XFS_BUF_ADDR(bp));
1011 if (pathlen < byte_cnt)
1013 pathlen -= byte_cnt;
1015 error = xfs_uio_read(XFS_BUF_PTR(bp), byte_cnt, uiop);
1022 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1030 * This is called to sync the inode and its data out to disk.
1031 * We need to hold the I/O lock while flushing the data, and
1032 * the inode lock while flushing the inode. The inode lock CANNOT
1033 * be held while flushing the data, so acquire after we're done
1047 int log_flushed = 0, changed = 1;
1049 vn_trace_entry(BHV_TO_VNODE(bdp),
1050 __FUNCTION__, (inst_t *)__return_address);
1052 ip = XFS_BHVTOI(bdp);
1054 ASSERT(start >= 0 && stop >= -1);
1056 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1057 return XFS_ERROR(EIO);
1060 * We always need to make sure that the required inode state
1061 * is safe on disk. The vnode might be clean but because
1062 * of committed transactions that haven't hit the disk yet.
1063 * Likewise, there could be unflushed non-transactional
1064 * changes to the inode core that have to go to disk.
1066 * The following code depends on one assumption: that
1067 * any transaction that changes an inode logs the core
1068 * because it has to change some field in the inode core
1069 * (typically nextents or nblocks). That assumption
1070 * implies that any transactions against an inode will
1071 * catch any non-transactional updates. If inode-altering
1072 * transactions exist that violate this assumption, the
1073 * code breaks. Right now, it figures that if the involved
1074 * update_* field is clear and the inode is unpinned, the
1075 * inode is clean. Either it's been flushed or it's been
1076 * committed and the commit has hit the disk unpinning the inode.
1077 * (Note that xfs_inode_item_format() called at commit clears
1078 * the update_* fields.)
1080 xfs_ilock(ip, XFS_ILOCK_SHARED);
1082 /* If we are flushing data then we care about update_size
1083 * being set, otherwise we care about update_core
1085 if ((flag & FSYNC_DATA) ?
1086 (ip->i_update_size == 0) :
1087 (ip->i_update_core == 0)) {
1089 * Timestamps/size haven't changed since last inode
1090 * flush or inode transaction commit. That means
1091 * either nothing got written or a transaction
1092 * committed which caught the updates. If the
1093 * latter happened and the transaction hasn't
1094 * hit the disk yet, the inode will be still
1095 * be pinned. If it is, force the log.
1098 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1100 if (xfs_ipincount(ip)) {
1101 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
1103 ((flag & FSYNC_WAIT)
1104 ? XFS_LOG_SYNC : 0),
1108 * If the inode is not pinned and nothing
1109 * has changed we don't need to flush the
1117 * Kick off a transaction to log the inode
1118 * core to get the updates. Make it
1119 * sync if FSYNC_WAIT is passed in (which
1120 * is done by everybody but specfs). The
1121 * sync transaction will also force the log.
1123 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1124 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1125 if ((error = xfs_trans_reserve(tp, 0,
1126 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1128 xfs_trans_cancel(tp, 0);
1131 xfs_ilock(ip, XFS_ILOCK_EXCL);
1134 * Note - it's possible that we might have pushed
1135 * ourselves out of the way during trans_reserve
1136 * which would flush the inode. But there's no
1137 * guarantee that the inode buffer has actually
1138 * gone out yet (it's delwri). Plus the buffer
1139 * could be pinned anyway if it's part of an
1140 * inode in another recent transaction. So we
1141 * play it safe and fire off the transaction anyway.
1143 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1144 xfs_trans_ihold(tp, ip);
1145 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1146 if (flag & FSYNC_WAIT)
1147 xfs_trans_set_sync(tp);
1148 error = _xfs_trans_commit(tp, 0, &log_flushed);
1150 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1153 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1155 * If the log write didn't issue an ordered tag we need
1156 * to flush the disk cache for the data device now.
1159 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1162 * If this inode is on the RT dev we need to flush that
1165 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
1166 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1173 * This is called by xfs_inactive to free any blocks beyond eof
1174 * when the link count isn't zero and by xfs_dm_punch_hole() when
1175 * punching a hole to EOF.
1185 xfs_fileoff_t end_fsb;
1186 xfs_fileoff_t last_fsb;
1187 xfs_filblks_t map_len;
1189 xfs_bmbt_irec_t imap;
1190 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
1193 * Figure out if there are any blocks beyond the end
1194 * of the file. If not, then there is nothing to do.
1196 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
1197 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1198 map_len = last_fsb - end_fsb;
1203 xfs_ilock(ip, XFS_ILOCK_SHARED);
1204 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, end_fsb, map_len, 0,
1205 NULL, 0, &imap, &nimaps, NULL, NULL);
1206 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1208 if (!error && (nimaps != 0) &&
1209 (imap.br_startblock != HOLESTARTBLOCK ||
1210 ip->i_delayed_blks)) {
1212 * Attach the dquots to the inode up front.
1214 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1218 * There are blocks after the end of file.
1219 * Free them up now by truncating the file to
1222 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1225 * Do the xfs_itruncate_start() call before
1226 * reserving any log space because
1227 * itruncate_start will call into the buffer
1228 * cache and we can't
1229 * do that within a transaction.
1232 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1233 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
1236 xfs_trans_cancel(tp, 0);
1238 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1242 error = xfs_trans_reserve(tp, 0,
1243 XFS_ITRUNCATE_LOG_RES(mp),
1244 0, XFS_TRANS_PERM_LOG_RES,
1245 XFS_ITRUNCATE_LOG_COUNT);
1247 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1248 xfs_trans_cancel(tp, 0);
1249 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1253 xfs_ilock(ip, XFS_ILOCK_EXCL);
1254 xfs_trans_ijoin(tp, ip,
1257 xfs_trans_ihold(tp, ip);
1259 error = xfs_itruncate_finish(&tp, ip,
1264 * If we get an error at this point we
1265 * simply don't bother truncating the file.
1268 xfs_trans_cancel(tp,
1269 (XFS_TRANS_RELEASE_LOG_RES |
1272 error = xfs_trans_commit(tp,
1273 XFS_TRANS_RELEASE_LOG_RES);
1275 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1282 * Free a symlink that has blocks associated with it.
1285 xfs_inactive_symlink_rmt(
1293 xfs_fsblock_t first_block;
1294 xfs_bmap_free_t free_list;
1297 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1305 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1307 * We're freeing a symlink that has some
1308 * blocks allocated to it. Free the
1309 * blocks here. We know that we've got
1310 * either 1 or 2 extents and that we can
1311 * free them all in one bunmapi call.
1313 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1314 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1315 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1316 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1317 xfs_trans_cancel(tp, 0);
1322 * Lock the inode, fix the size, and join it to the transaction.
1323 * Hold it so in the normal path, we still have it locked for
1324 * the second transaction. In the error paths we need it
1325 * held so the cancel won't rele it, see below.
1327 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1328 size = (int)ip->i_d.di_size;
1329 ip->i_d.di_size = 0;
1330 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1331 xfs_trans_ihold(tp, ip);
1332 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1334 * Find the block(s) so we can inval and unmap them.
1337 XFS_BMAP_INIT(&free_list, &first_block);
1338 nmaps = ARRAY_SIZE(mval);
1339 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1340 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
1344 * Invalidate the block(s).
1346 for (i = 0; i < nmaps; i++) {
1347 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1348 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1349 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1350 xfs_trans_binval(tp, bp);
1353 * Unmap the dead block(s) to the free_list.
1355 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
1356 &first_block, &free_list, NULL, &done)))
1360 * Commit the first transaction. This logs the EFI and the inode.
1362 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
1365 * The transaction must have been committed, since there were
1366 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1367 * The new tp has the extent freeing and EFDs.
1371 * The first xact was committed, so add the inode to the new one.
1372 * Mark it dirty so it will be logged and moved forward in the log as
1373 * part of every commit.
1375 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1376 xfs_trans_ihold(tp, ip);
1377 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1379 * Get a new, empty transaction to return to our caller.
1381 ntp = xfs_trans_dup(tp);
1383 * Commit the transaction containing extent freeing and EFDs.
1384 * If we get an error on the commit here or on the reserve below,
1385 * we need to unlock the inode since the new transaction doesn't
1386 * have the inode attached.
1388 error = xfs_trans_commit(tp, 0);
1391 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1395 * Remove the memory for extent descriptions (just bookkeeping).
1397 if (ip->i_df.if_bytes)
1398 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1399 ASSERT(ip->i_df.if_bytes == 0);
1401 * Put an itruncate log reservation in the new transaction
1404 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1405 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1406 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1410 * Return with the inode locked but not joined to the transaction.
1416 xfs_bmap_cancel(&free_list);
1419 * Have to come here with the inode locked and either
1420 * (held and in the transaction) or (not in the transaction).
1421 * If the inode isn't held then cancel would iput it, but
1422 * that's wrong since this is inactive and the vnode ref
1423 * count is 0 already.
1424 * Cancel won't do anything to the inode if held, but it still
1425 * needs to be locked until the cancel is done, if it was
1426 * joined to the transaction.
1428 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1429 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1436 xfs_inactive_symlink_local(
1442 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1444 * We're freeing a symlink which fit into
1445 * the inode. Just free the memory used
1446 * to hold the old symlink.
1448 error = xfs_trans_reserve(*tpp, 0,
1449 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1450 0, XFS_TRANS_PERM_LOG_RES,
1451 XFS_ITRUNCATE_LOG_COUNT);
1454 xfs_trans_cancel(*tpp, 0);
1458 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1461 * Zero length symlinks _can_ exist.
1463 if (ip->i_df.if_bytes > 0) {
1464 xfs_idata_realloc(ip,
1465 -(ip->i_df.if_bytes),
1467 ASSERT(ip->i_df.if_bytes == 0);
1481 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1484 ASSERT(ip->i_d.di_forkoff != 0);
1485 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1486 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1488 error = xfs_attr_inactive(ip);
1491 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1492 return error; /* goto out */
1495 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1496 error = xfs_trans_reserve(tp, 0,
1497 XFS_IFREE_LOG_RES(mp),
1498 0, XFS_TRANS_PERM_LOG_RES,
1499 XFS_INACTIVE_LOG_COUNT);
1501 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1502 xfs_trans_cancel(tp, 0);
1504 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1508 xfs_ilock(ip, XFS_ILOCK_EXCL);
1509 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1510 xfs_trans_ihold(tp, ip);
1511 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1513 ASSERT(ip->i_d.di_anextents == 0);
1528 vp = BHV_TO_VNODE(bdp);
1529 ip = XFS_BHVTOI(bdp);
1532 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
1535 /* If this is a read-only mount, don't do this (would generate I/O) */
1536 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1539 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1541 * If we previously truncated this file and removed old data
1542 * in the process, we want to initiate "early" writeout on
1543 * the last close. This is an attempt to combat the notorious
1544 * NULL files problem which is particularly noticable from a
1545 * truncate down, buffered (re-)write (delalloc), followed by
1546 * a crash. What we are effectively doing here is
1547 * significantly reducing the time window where we'd otherwise
1548 * be exposed to that problem.
1550 if (VUNTRUNCATE(vp) && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
1551 bhv_vop_flush_pages(vp, 0, -1, XFS_B_ASYNC, FI_NONE);
1555 #ifdef HAVE_REFCACHE
1556 /* If we are in the NFS reference cache then don't do this now */
1561 if (ip->i_d.di_nlink != 0) {
1562 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1563 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1564 ip->i_delayed_blks > 0)) &&
1565 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1566 (!(ip->i_d.di_flags &
1567 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1568 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1571 /* Update linux inode block count after free above */
1572 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1573 ip->i_d.di_nblocks + ip->i_delayed_blks);
1583 * This is called when the vnode reference count for the vnode
1584 * goes to zero. If the file has been unlinked, then it must
1585 * now be truncated. Also, we clear all of the read-ahead state
1586 * kept for the inode here since the file is now closed.
1595 xfs_bmap_free_t free_list;
1596 xfs_fsblock_t first_block;
1603 vp = BHV_TO_VNODE(bdp);
1604 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1606 ip = XFS_BHVTOI(bdp);
1609 * If the inode is already free, then there can be nothing
1612 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1613 ASSERT(ip->i_df.if_real_bytes == 0);
1614 ASSERT(ip->i_df.if_broot_bytes == 0);
1615 return VN_INACTIVE_CACHE;
1619 * Only do a truncate if it's a regular file with
1620 * some actual space in it. It's OK to look at the
1621 * inode's fields without the lock because we're the
1622 * only one with a reference to the inode.
1624 truncate = ((ip->i_d.di_nlink == 0) &&
1625 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1626 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1627 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1631 if (ip->i_d.di_nlink == 0 &&
1632 DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_DESTROY)) {
1633 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1638 /* If this is a read-only mount, don't do this (would generate I/O) */
1639 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1642 if (ip->i_d.di_nlink != 0) {
1643 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1644 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1645 ip->i_delayed_blks > 0)) &&
1646 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1647 (!(ip->i_d.di_flags &
1648 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1649 (ip->i_delayed_blks != 0)))) {
1650 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1652 return VN_INACTIVE_CACHE;
1653 /* Update linux inode block count after free above */
1654 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1655 ip->i_d.di_nblocks + ip->i_delayed_blks);
1660 ASSERT(ip->i_d.di_nlink == 0);
1662 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1663 return VN_INACTIVE_CACHE;
1665 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1668 * Do the xfs_itruncate_start() call before
1669 * reserving any log space because itruncate_start
1670 * will call into the buffer cache and we can't
1671 * do that within a transaction.
1673 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1675 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1677 xfs_trans_cancel(tp, 0);
1678 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1679 return VN_INACTIVE_CACHE;
1682 error = xfs_trans_reserve(tp, 0,
1683 XFS_ITRUNCATE_LOG_RES(mp),
1684 0, XFS_TRANS_PERM_LOG_RES,
1685 XFS_ITRUNCATE_LOG_COUNT);
1687 /* Don't call itruncate_cleanup */
1688 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1689 xfs_trans_cancel(tp, 0);
1690 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1691 return VN_INACTIVE_CACHE;
1694 xfs_ilock(ip, XFS_ILOCK_EXCL);
1695 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1696 xfs_trans_ihold(tp, ip);
1699 * normally, we have to run xfs_itruncate_finish sync.
1700 * But if filesystem is wsync and we're in the inactive
1701 * path, then we know that nlink == 0, and that the
1702 * xaction that made nlink == 0 is permanently committed
1703 * since xfs_remove runs as a synchronous transaction.
1705 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1706 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1709 xfs_trans_cancel(tp,
1710 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1711 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1712 return VN_INACTIVE_CACHE;
1714 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1717 * If we get an error while cleaning up a
1718 * symlink we bail out.
1720 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1721 xfs_inactive_symlink_rmt(ip, &tp) :
1722 xfs_inactive_symlink_local(ip, &tp);
1726 return VN_INACTIVE_CACHE;
1729 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1730 xfs_trans_ihold(tp, ip);
1732 error = xfs_trans_reserve(tp, 0,
1733 XFS_IFREE_LOG_RES(mp),
1734 0, XFS_TRANS_PERM_LOG_RES,
1735 XFS_INACTIVE_LOG_COUNT);
1737 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1738 xfs_trans_cancel(tp, 0);
1739 return VN_INACTIVE_CACHE;
1742 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1743 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1744 xfs_trans_ihold(tp, ip);
1748 * If there are attributes associated with the file
1749 * then blow them away now. The code calls a routine
1750 * that recursively deconstructs the attribute fork.
1751 * We need to just commit the current transaction
1752 * because we can't use it for xfs_attr_inactive().
1754 if (ip->i_d.di_anextents > 0) {
1755 error = xfs_inactive_attrs(ip, &tp);
1757 * If we got an error, the transaction is already
1758 * cancelled, and the inode is unlocked. Just get out.
1761 return VN_INACTIVE_CACHE;
1762 } else if (ip->i_afp) {
1763 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1769 XFS_BMAP_INIT(&free_list, &first_block);
1770 error = xfs_ifree(tp, ip, &free_list);
1773 * If we fail to free the inode, shut down. The cancel
1774 * might do that, we need to make sure. Otherwise the
1775 * inode might be lost for a long time or forever.
1777 if (!XFS_FORCED_SHUTDOWN(mp)) {
1779 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1780 error, mp->m_fsname);
1781 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1783 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1786 * Credit the quota account(s). The inode is gone.
1788 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1791 * Just ignore errors at this point. There is
1792 * nothing we can do except to try to keep going.
1794 (void) xfs_bmap_finish(&tp, &free_list, &committed);
1795 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1798 * Release the dquots held by inode, if any.
1800 XFS_QM_DQDETACH(mp, ip);
1802 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1805 return VN_INACTIVE_CACHE;
1814 bhv_desc_t *dir_bdp,
1815 bhv_vname_t *dentry,
1821 xfs_inode_t *dp, *ip;
1825 bhv_vnode_t *dir_vp;
1827 dir_vp = BHV_TO_VNODE(dir_bdp);
1828 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1830 dp = XFS_BHVTOI(dir_bdp);
1832 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1833 return XFS_ERROR(EIO);
1835 lock_mode = xfs_ilock_map_shared(dp);
1836 error = xfs_dir_lookup_int(dir_bdp, lock_mode, dentry, &e_inum, &ip);
1838 *vpp = XFS_ITOV(ip);
1841 xfs_iunlock_map_shared(dp, lock_mode);
1847 * xfs_create (create a new file).
1851 bhv_desc_t *dir_bdp,
1852 bhv_vname_t *dentry,
1857 char *name = VNAME(dentry);
1858 bhv_vnode_t *dir_vp;
1859 xfs_inode_t *dp, *ip;
1860 bhv_vnode_t *vp = NULL;
1865 xfs_bmap_free_t free_list;
1866 xfs_fsblock_t first_block;
1867 boolean_t dp_joined_to_trans;
1868 int dm_event_sent = 0;
1872 struct xfs_dquot *udqp, *gdqp;
1878 dir_vp = BHV_TO_VNODE(dir_bdp);
1879 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1881 dp = XFS_BHVTOI(dir_bdp);
1884 dm_di_mode = vap->va_mode;
1885 namelen = VNAMELEN(dentry);
1887 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
1888 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1889 dir_vp, DM_RIGHT_NULL, NULL,
1890 DM_RIGHT_NULL, name, NULL,
1898 if (XFS_FORCED_SHUTDOWN(mp))
1899 return XFS_ERROR(EIO);
1901 /* Return through std_return after this point. */
1904 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1905 prid = dp->i_d.di_projid;
1906 else if (vap->va_mask & XFS_AT_PROJID)
1907 prid = (xfs_prid_t)vap->va_projid;
1909 prid = (xfs_prid_t)dfltprid;
1912 * Make sure that we have allocated dquot(s) on disk.
1914 error = XFS_QM_DQVOPALLOC(mp, dp,
1915 current_fsuid(credp), current_fsgid(credp), prid,
1916 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1921 dp_joined_to_trans = B_FALSE;
1923 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1924 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1925 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1927 * Initially assume that the file does not exist and
1928 * reserve the resources for that case. If that is not
1929 * the case we'll drop the one we have and get a more
1930 * appropriate transaction later.
1932 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1933 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1934 if (error == ENOSPC) {
1936 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1937 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1945 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1947 XFS_BMAP_INIT(&free_list, &first_block);
1952 * Reserve disk quota and the inode.
1954 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1958 if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
1960 rdev = (vap->va_mask & XFS_AT_RDEV) ? vap->va_rdev : 0;
1961 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 1,
1962 rdev, credp, prid, resblks > 0,
1965 if (error == ENOSPC)
1972 * At this point, we've gotten a newly allocated inode.
1973 * It is locked (and joined to the transaction).
1976 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1979 * Now we join the directory inode to the transaction.
1980 * We do not do it earlier because xfs_dir_ialloc
1981 * might commit the previous transaction (and release
1986 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1987 dp_joined_to_trans = B_TRUE;
1989 error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
1990 &first_block, &free_list, resblks ?
1991 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1993 ASSERT(error != ENOSPC);
1996 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1997 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2000 * If this is a synchronous mount, make sure that the
2001 * create transaction goes to disk before returning to
2004 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2005 xfs_trans_set_sync(tp);
2011 * Attach the dquot(s) to the inodes and modify them incore.
2012 * These ids of the inode couldn't have changed since the new
2013 * inode has been locked ever since it was created.
2015 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2018 * xfs_trans_commit normally decrements the vnode ref count
2019 * when it unlocks the inode. Since we want to return the
2020 * vnode to the caller, we bump the vnode ref count now.
2025 error = xfs_bmap_finish(&tp, &free_list, &committed);
2027 xfs_bmap_cancel(&free_list);
2031 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2038 XFS_QM_DQRELE(mp, udqp);
2039 XFS_QM_DQRELE(mp, gdqp);
2042 * Propagate the fact that the vnode changed after the
2043 * xfs_inode locks have been released.
2045 bhv_vop_vnode_change(vp, VCHANGE_FLAGS_TRUNCATED, 3);
2049 /* Fallthrough to std_return with error = 0 */
2052 if ( (*vpp || (error != 0 && dm_event_sent != 0)) &&
2053 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2054 DM_EVENT_POSTCREATE)) {
2055 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2056 dir_vp, DM_RIGHT_NULL,
2058 DM_RIGHT_NULL, name, NULL,
2059 dm_di_mode, error, 0);
2064 cancel_flags |= XFS_TRANS_ABORT;
2069 xfs_trans_cancel(tp, cancel_flags);
2071 if (!dp_joined_to_trans && (dp != NULL))
2072 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2073 XFS_QM_DQRELE(mp, udqp);
2074 XFS_QM_DQRELE(mp, gdqp);
2080 * Wait until after the current transaction is aborted to
2081 * release the inode. This prevents recursive transactions
2082 * and deadlocks from xfs_inactive.
2084 cancel_flags |= XFS_TRANS_ABORT;
2085 xfs_trans_cancel(tp, cancel_flags);
2088 XFS_QM_DQRELE(mp, udqp);
2089 XFS_QM_DQRELE(mp, gdqp);
2096 * Some counters to see if (and how often) we are hitting some deadlock
2097 * prevention code paths.
2101 int xfs_rm_lock_delays;
2102 int xfs_rm_attempts;
2106 * The following routine will lock the inodes associated with the
2107 * directory and the named entry in the directory. The locks are
2108 * acquired in increasing inode number.
2110 * If the entry is "..", then only the directory is locked. The
2111 * vnode ref count will still include that from the .. entry in
2114 * There is a deadlock we need to worry about. If the locked directory is
2115 * in the AIL, it might be blocking up the log. The next inode we lock
2116 * could be already locked by another thread waiting for log space (e.g
2117 * a permanent log reservation with a long running transaction (see
2118 * xfs_itruncate_finish)). To solve this, we must check if the directory
2119 * is in the ail and use lock_nowait. If we can't lock, we need to
2120 * drop the inode lock on the directory and try again. xfs_iunlock will
2121 * potentially push the tail if we were holding up the log.
2124 xfs_lock_dir_and_entry(
2126 xfs_inode_t *ip) /* inode of entry 'name' */
2130 xfs_inode_t *ips[2];
2139 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2146 * We want to lock in increasing inum. Since we've already
2147 * acquired the lock on the directory, we may need to release
2148 * if if the inum of the entry turns out to be less.
2150 if (e_inum > dp->i_ino) {
2152 * We are already in the right order, so just
2153 * lock on the inode of the entry.
2154 * We need to use nowait if dp is in the AIL.
2157 lp = (xfs_log_item_t *)dp->i_itemp;
2158 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2159 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2166 * Unlock dp and try again.
2167 * xfs_iunlock will try to push the tail
2168 * if the inode is in the AIL.
2171 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2173 if ((attempts % 5) == 0) {
2174 delay(1); /* Don't just spin the CPU */
2176 xfs_rm_lock_delays++;
2182 xfs_ilock(ip, XFS_ILOCK_EXCL);
2184 } else if (e_inum < dp->i_ino) {
2185 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2189 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2191 /* else e_inum == dp->i_ino */
2192 /* This can happen if we're asked to lock /x/..
2193 * the entry is "..", which is also the parent directory.
2201 int xfs_small_retries;
2202 int xfs_middle_retries;
2203 int xfs_lots_retries;
2204 int xfs_lock_delays;
2208 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2212 xfs_lock_inumorder(int lock_mode, int subclass)
2214 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
2215 lock_mode |= (subclass + XFS_IOLOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
2216 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
2217 lock_mode |= (subclass + XFS_ILOCK_INUMORDER) << XFS_ILOCK_SHIFT;
2223 * The following routine will lock n inodes in exclusive mode.
2224 * We assume the caller calls us with the inodes in i_ino order.
2226 * We need to detect deadlock where an inode that we lock
2227 * is in the AIL and we start waiting for another inode that is locked
2228 * by a thread in a long running transaction (such as truncate). This can
2229 * result in deadlock since the long running trans might need to wait
2230 * for the inode we just locked in order to push the tail and free space
2240 int attempts = 0, i, j, try_lock;
2243 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2254 for (; i < inodes; i++) {
2257 if (i && (ips[i] == ips[i-1])) /* Already locked */
2261 * If try_lock is not set yet, make sure all locked inodes
2262 * are not in the AIL.
2263 * If any are, set try_lock to be used later.
2267 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2268 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2269 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2276 * If any of the previous locks we have locked is in the AIL,
2277 * we must TRY to get the second and subsequent locks. If
2278 * we can't get any, we must release all we have
2283 /* try_lock must be 0 if i is 0. */
2285 * try_lock means we have an inode locked
2286 * that is in the AIL.
2289 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
2293 * Unlock all previous guys and try again.
2294 * xfs_iunlock will try to push the tail
2295 * if the inode is in the AIL.
2298 for(j = i - 1; j >= 0; j--) {
2301 * Check to see if we've already
2302 * unlocked this one.
2303 * Not the first one going back,
2304 * and the inode ptr is the same.
2306 if ((j != (i - 1)) && ips[j] ==
2310 xfs_iunlock(ips[j], lock_mode);
2313 if ((attempts % 5) == 0) {
2314 delay(1); /* Don't just spin the CPU */
2324 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
2330 if (attempts < 5) xfs_small_retries++;
2331 else if (attempts < 100) xfs_middle_retries++;
2332 else xfs_lots_retries++;
2340 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2341 int remove_which_error_return = 0;
2343 #define REMOVE_DEBUG_TRACE(x)
2344 #endif /* ! DEBUG */
2353 bhv_desc_t *dir_bdp,
2354 bhv_vname_t *dentry,
2357 bhv_vnode_t *dir_vp;
2358 char *name = VNAME(dentry);
2359 xfs_inode_t *dp, *ip;
2360 xfs_trans_t *tp = NULL;
2363 xfs_bmap_free_t free_list;
2364 xfs_fsblock_t first_block;
2372 dir_vp = BHV_TO_VNODE(dir_bdp);
2373 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2375 dp = XFS_BHVTOI(dir_bdp);
2378 if (XFS_FORCED_SHUTDOWN(mp))
2379 return XFS_ERROR(EIO);
2381 namelen = VNAMELEN(dentry);
2383 if (!xfs_get_dir_entry(dentry, &ip)) {
2384 dm_di_mode = ip->i_d.di_mode;
2388 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
2389 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2390 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2391 name, NULL, dm_di_mode, 0, 0);
2396 /* From this point on, return through std_return */
2400 * We need to get a reference to ip before we get our log
2401 * reservation. The reason for this is that we cannot call
2402 * xfs_iget for an inode for which we do not have a reference
2403 * once we've acquired a log reservation. This is because the
2404 * inode we are trying to get might be in xfs_inactive going
2405 * for a log reservation. Since we'll have to wait for the
2406 * inactive code to complete before returning from xfs_iget,
2407 * we need to make sure that we don't have log space reserved
2408 * when we call xfs_iget. Instead we get an unlocked reference
2409 * to the inode before getting our log reservation.
2411 error = xfs_get_dir_entry(dentry, &ip);
2413 REMOVE_DEBUG_TRACE(__LINE__);
2417 dm_di_mode = ip->i_d.di_mode;
2419 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2423 error = XFS_QM_DQATTACH(mp, dp, 0);
2424 if (!error && dp != ip)
2425 error = XFS_QM_DQATTACH(mp, ip, 0);
2427 REMOVE_DEBUG_TRACE(__LINE__);
2432 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2433 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2435 * We try to get the real space reservation first,
2436 * allowing for directory btree deletion(s) implying
2437 * possible bmap insert(s). If we can't get the space
2438 * reservation then we use 0 instead, and avoid the bmap
2439 * btree insert(s) in the directory code by, if the bmap
2440 * insert tries to happen, instead trimming the LAST
2441 * block from the directory.
2443 resblks = XFS_REMOVE_SPACE_RES(mp);
2444 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2445 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2446 if (error == ENOSPC) {
2448 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2449 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2452 ASSERT(error != ENOSPC);
2453 REMOVE_DEBUG_TRACE(__LINE__);
2454 xfs_trans_cancel(tp, 0);
2459 error = xfs_lock_dir_and_entry(dp, ip);
2461 REMOVE_DEBUG_TRACE(__LINE__);
2462 xfs_trans_cancel(tp, cancel_flags);
2468 * At this point, we've gotten both the directory and the entry
2471 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2474 * Increment vnode ref count only in this case since
2475 * there's an extra vnode reference in the case where
2479 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2483 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2485 XFS_BMAP_INIT(&free_list, &first_block);
2486 error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2487 &first_block, &free_list, 0);
2489 ASSERT(error != ENOENT);
2490 REMOVE_DEBUG_TRACE(__LINE__);
2493 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2496 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2498 error = xfs_droplink(tp, ip);
2500 REMOVE_DEBUG_TRACE(__LINE__);
2504 /* Determine if this is the last link while
2505 * we are in the transaction.
2507 link_zero = (ip)->i_d.di_nlink==0;
2510 * Take an extra ref on the inode so that it doesn't
2511 * go to xfs_inactive() from within the commit.
2516 * If this is a synchronous mount, make sure that the
2517 * remove transaction goes to disk before returning to
2520 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2521 xfs_trans_set_sync(tp);
2524 error = xfs_bmap_finish(&tp, &free_list, &committed);
2526 REMOVE_DEBUG_TRACE(__LINE__);
2530 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2537 * Before we drop our extra reference to the inode, purge it
2538 * from the refcache if it is there. By waiting until afterwards
2539 * to do the IRELE, we ensure that we won't go inactive in the
2540 * xfs_refcache_purge_ip routine (although that would be OK).
2542 xfs_refcache_purge_ip(ip);
2544 vn_trace_exit(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2547 * Let interposed file systems know about removed links.
2549 bhv_vop_link_removed(XFS_ITOV(ip), dir_vp, link_zero);
2553 /* Fall through to std_return with error = 0 */
2555 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp,
2556 DM_EVENT_POSTREMOVE)) {
2557 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2558 dir_vp, DM_RIGHT_NULL,
2559 NULL, DM_RIGHT_NULL,
2560 name, NULL, dm_di_mode, error, 0);
2565 xfs_bmap_cancel(&free_list);
2566 cancel_flags |= XFS_TRANS_ABORT;
2567 xfs_trans_cancel(tp, cancel_flags);
2572 * In this case make sure to not release the inode until after
2573 * the current transaction is aborted. Releasing it beforehand
2574 * can cause us to go to xfs_inactive and start a recursive
2575 * transaction which can easily deadlock with the current one.
2577 xfs_bmap_cancel(&free_list);
2578 cancel_flags |= XFS_TRANS_ABORT;
2579 xfs_trans_cancel(tp, cancel_flags);
2582 * Before we drop our extra reference to the inode, purge it
2583 * from the refcache if it is there. By waiting until afterwards
2584 * to do the IRELE, we ensure that we won't go inactive in the
2585 * xfs_refcache_purge_ip routine (although that would be OK).
2587 xfs_refcache_purge_ip(ip);
2601 bhv_desc_t *target_dir_bdp,
2602 bhv_vnode_t *src_vp,
2603 bhv_vname_t *dentry,
2606 xfs_inode_t *tdp, *sip;
2609 xfs_inode_t *ips[2];
2611 xfs_bmap_free_t free_list;
2612 xfs_fsblock_t first_block;
2615 bhv_vnode_t *target_dir_vp;
2617 char *target_name = VNAME(dentry);
2620 target_dir_vp = BHV_TO_VNODE(target_dir_bdp);
2621 vn_trace_entry(target_dir_vp, __FUNCTION__, (inst_t *)__return_address);
2622 vn_trace_entry(src_vp, __FUNCTION__, (inst_t *)__return_address);
2624 target_namelen = VNAMELEN(dentry);
2625 ASSERT(!VN_ISDIR(src_vp));
2627 sip = xfs_vtoi(src_vp);
2628 tdp = XFS_BHVTOI(target_dir_bdp);
2630 if (XFS_FORCED_SHUTDOWN(mp))
2631 return XFS_ERROR(EIO);
2633 if (DM_EVENT_ENABLED(src_vp->v_vfsp, tdp, DM_EVENT_LINK)) {
2634 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2635 target_dir_vp, DM_RIGHT_NULL,
2636 src_vp, DM_RIGHT_NULL,
2637 target_name, NULL, 0, 0, 0);
2642 /* Return through std_return after this point. */
2644 error = XFS_QM_DQATTACH(mp, sip, 0);
2645 if (!error && sip != tdp)
2646 error = XFS_QM_DQATTACH(mp, tdp, 0);
2650 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2651 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2652 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2653 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2654 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2655 if (error == ENOSPC) {
2657 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2658 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2665 if (sip->i_ino < tdp->i_ino) {
2673 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2676 * Increment vnode ref counts since xfs_trans_commit &
2677 * xfs_trans_cancel will both unlock the inodes and
2678 * decrement the associated ref counts.
2681 VN_HOLD(target_dir_vp);
2682 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2683 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2686 * If the source has too many links, we can't make any more to it.
2688 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2689 error = XFS_ERROR(EMLINK);
2694 * If we are using project inheritance, we only allow hard link
2695 * creation in our tree when the project IDs are the same; else
2696 * the tree quota mechanism could be circumvented.
2698 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2699 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2700 error = XFS_ERROR(EXDEV);
2705 (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
2708 XFS_BMAP_INIT(&free_list, &first_block);
2710 error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
2711 sip->i_ino, &first_block, &free_list,
2715 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2717 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2719 error = xfs_bumplink(tp, sip);
2724 * If this is a synchronous mount, make sure that the
2725 * link transaction goes to disk before returning to
2728 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2729 xfs_trans_set_sync(tp);
2732 error = xfs_bmap_finish (&tp, &free_list, &committed);
2734 xfs_bmap_cancel(&free_list);
2738 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2742 /* Fall through to std_return with error = 0. */
2744 if (DM_EVENT_ENABLED(src_vp->v_vfsp, sip,
2745 DM_EVENT_POSTLINK)) {
2746 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2747 target_dir_vp, DM_RIGHT_NULL,
2748 src_vp, DM_RIGHT_NULL,
2749 target_name, NULL, 0, error, 0);
2754 cancel_flags |= XFS_TRANS_ABORT;
2758 xfs_trans_cancel(tp, cancel_flags);
2769 bhv_desc_t *dir_bdp,
2770 bhv_vname_t *dentry,
2775 char *dir_name = VNAME(dentry);
2777 xfs_inode_t *cdp; /* inode of created dir */
2778 bhv_vnode_t *cvp; /* vnode of created dir */
2784 xfs_bmap_free_t free_list;
2785 xfs_fsblock_t first_block;
2786 bhv_vnode_t *dir_vp;
2787 boolean_t dp_joined_to_trans;
2788 boolean_t created = B_FALSE;
2789 int dm_event_sent = 0;
2791 struct xfs_dquot *udqp, *gdqp;
2796 dir_vp = BHV_TO_VNODE(dir_bdp);
2797 dp = XFS_BHVTOI(dir_bdp);
2800 if (XFS_FORCED_SHUTDOWN(mp))
2801 return XFS_ERROR(EIO);
2803 dir_namelen = VNAMELEN(dentry);
2806 dp_joined_to_trans = B_FALSE;
2807 dm_di_mode = vap->va_mode;
2809 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
2810 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2811 dir_vp, DM_RIGHT_NULL, NULL,
2812 DM_RIGHT_NULL, dir_name, NULL,
2819 /* Return through std_return after this point. */
2821 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2825 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2826 prid = dp->i_d.di_projid;
2827 else if (vap->va_mask & XFS_AT_PROJID)
2828 prid = (xfs_prid_t)vap->va_projid;
2830 prid = (xfs_prid_t)dfltprid;
2833 * Make sure that we have allocated dquot(s) on disk.
2835 error = XFS_QM_DQVOPALLOC(mp, dp,
2836 current_fsuid(credp), current_fsgid(credp), prid,
2837 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2841 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2842 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2843 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2844 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2845 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2846 if (error == ENOSPC) {
2848 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2849 XFS_TRANS_PERM_LOG_RES,
2850 XFS_MKDIR_LOG_COUNT);
2858 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2861 * Check for directory link count overflow.
2863 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2864 error = XFS_ERROR(EMLINK);
2869 * Reserve disk quota and the inode.
2871 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2876 (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
2879 * create the directory inode.
2881 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 2,
2882 0, credp, prid, resblks > 0,
2885 if (error == ENOSPC)
2892 * Now we add the directory inode to the transaction.
2893 * We waited until now since xfs_dir_ialloc might start
2894 * a new transaction. Had we joined the transaction
2895 * earlier, the locks might have gotten released.
2898 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2899 dp_joined_to_trans = B_TRUE;
2901 XFS_BMAP_INIT(&free_list, &first_block);
2903 error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2904 &first_block, &free_list, resblks ?
2905 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2907 ASSERT(error != ENOSPC);
2910 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2913 * Bump the in memory version number of the parent directory
2914 * so that other processes accessing it will recognize that
2915 * the directory has changed.
2919 error = xfs_dir_init(tp, cdp, dp);
2924 error = xfs_bumplink(tp, dp);
2928 cvp = XFS_ITOV(cdp);
2936 * Attach the dquots to the new inode and modify the icount incore.
2938 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2941 * If this is a synchronous mount, make sure that the
2942 * mkdir transaction goes to disk before returning to
2945 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2946 xfs_trans_set_sync(tp);
2949 error = xfs_bmap_finish(&tp, &free_list, &committed);
2955 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2956 XFS_QM_DQRELE(mp, udqp);
2957 XFS_QM_DQRELE(mp, gdqp);
2962 /* Fall through to std_return with error = 0 or errno from
2963 * xfs_trans_commit. */
2966 if ( (created || (error != 0 && dm_event_sent != 0)) &&
2967 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2968 DM_EVENT_POSTCREATE)) {
2969 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2970 dir_vp, DM_RIGHT_NULL,
2971 created ? XFS_ITOV(cdp):NULL,
2974 dm_di_mode, error, 0);
2980 xfs_bmap_cancel(&free_list);
2982 cancel_flags |= XFS_TRANS_ABORT;
2984 xfs_trans_cancel(tp, cancel_flags);
2985 XFS_QM_DQRELE(mp, udqp);
2986 XFS_QM_DQRELE(mp, gdqp);
2988 if (!dp_joined_to_trans && (dp != NULL)) {
2989 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3002 bhv_desc_t *dir_bdp,
3003 bhv_vname_t *dentry,
3006 char *name = VNAME(dentry);
3008 xfs_inode_t *cdp; /* child directory */
3012 xfs_bmap_free_t free_list;
3013 xfs_fsblock_t first_block;
3016 bhv_vnode_t *dir_vp;
3017 int dm_di_mode = S_IFDIR;
3022 dir_vp = BHV_TO_VNODE(dir_bdp);
3023 dp = XFS_BHVTOI(dir_bdp);
3026 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3028 if (XFS_FORCED_SHUTDOWN(XFS_BHVTOI(dir_bdp)->i_mount))
3029 return XFS_ERROR(EIO);
3030 namelen = VNAMELEN(dentry);
3032 if (!xfs_get_dir_entry(dentry, &cdp)) {
3033 dm_di_mode = cdp->i_d.di_mode;
3037 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
3038 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
3039 dir_vp, DM_RIGHT_NULL,
3040 NULL, DM_RIGHT_NULL,
3041 name, NULL, dm_di_mode, 0, 0);
3043 return XFS_ERROR(error);
3046 /* Return through std_return after this point. */
3051 * We need to get a reference to cdp before we get our log
3052 * reservation. The reason for this is that we cannot call
3053 * xfs_iget for an inode for which we do not have a reference
3054 * once we've acquired a log reservation. This is because the
3055 * inode we are trying to get might be in xfs_inactive going
3056 * for a log reservation. Since we'll have to wait for the
3057 * inactive code to complete before returning from xfs_iget,
3058 * we need to make sure that we don't have log space reserved
3059 * when we call xfs_iget. Instead we get an unlocked reference
3060 * to the inode before getting our log reservation.
3062 error = xfs_get_dir_entry(dentry, &cdp);
3064 REMOVE_DEBUG_TRACE(__LINE__);
3068 dm_di_mode = cdp->i_d.di_mode;
3071 * Get the dquots for the inodes.
3073 error = XFS_QM_DQATTACH(mp, dp, 0);
3074 if (!error && dp != cdp)
3075 error = XFS_QM_DQATTACH(mp, cdp, 0);
3078 REMOVE_DEBUG_TRACE(__LINE__);
3082 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
3083 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3085 * We try to get the real space reservation first,
3086 * allowing for directory btree deletion(s) implying
3087 * possible bmap insert(s). If we can't get the space
3088 * reservation then we use 0 instead, and avoid the bmap
3089 * btree insert(s) in the directory code by, if the bmap
3090 * insert tries to happen, instead trimming the LAST
3091 * block from the directory.
3093 resblks = XFS_REMOVE_SPACE_RES(mp);
3094 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
3095 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3096 if (error == ENOSPC) {
3098 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
3099 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3102 ASSERT(error != ENOSPC);
3107 XFS_BMAP_INIT(&free_list, &first_block);
3110 * Now lock the child directory inode and the parent directory
3111 * inode in the proper order. This will take care of validating
3112 * that the directory entry for the child directory inode has
3113 * not changed while we were obtaining a log reservation.
3115 error = xfs_lock_dir_and_entry(dp, cdp);
3117 xfs_trans_cancel(tp, cancel_flags);
3122 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3125 * Only increment the parent directory vnode count if
3126 * we didn't bump it in looking up cdp. The only time
3127 * we don't bump it is when we're looking up ".".
3133 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3135 ASSERT(cdp->i_d.di_nlink >= 2);
3136 if (cdp->i_d.di_nlink != 2) {
3137 error = XFS_ERROR(ENOTEMPTY);
3140 if (!xfs_dir_isempty(cdp)) {
3141 error = XFS_ERROR(ENOTEMPTY);
3145 error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
3146 &first_block, &free_list, resblks);
3150 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3153 * Bump the in memory generation count on the parent
3154 * directory so that other can know that it has changed.
3159 * Drop the link from cdp's "..".
3161 error = xfs_droplink(tp, dp);
3167 * Drop the link from dp to cdp.
3169 error = xfs_droplink(tp, cdp);
3175 * Drop the "." link from cdp to self.
3177 error = xfs_droplink(tp, cdp);
3182 /* Determine these before committing transaction */
3183 last_cdp_link = (cdp)->i_d.di_nlink==0;
3186 * Take an extra ref on the child vnode so that it
3187 * does not go to xfs_inactive() from within the commit.
3192 * If this is a synchronous mount, make sure that the
3193 * rmdir transaction goes to disk before returning to
3196 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3197 xfs_trans_set_sync(tp);
3200 error = xfs_bmap_finish (&tp, &free_list, &committed);
3202 xfs_bmap_cancel(&free_list);
3203 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3209 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3217 * Let interposed file systems know about removed links.
3219 bhv_vop_link_removed(XFS_ITOV(cdp), dir_vp, last_cdp_link);
3223 /* Fall through to std_return with error = 0 or the errno
3224 * from xfs_trans_commit. */
3226 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_POSTREMOVE)) {
3227 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3228 dir_vp, DM_RIGHT_NULL,
3229 NULL, DM_RIGHT_NULL,
3230 name, NULL, dm_di_mode,
3236 xfs_bmap_cancel(&free_list);
3237 cancel_flags |= XFS_TRANS_ABORT;
3241 xfs_trans_cancel(tp, cancel_flags);
3247 * Read dp's entries starting at uiop->uio_offset and translate them into
3248 * bufsize bytes worth of struct dirents starting at bufbase.
3252 bhv_desc_t *dir_bdp,
3258 xfs_trans_t *tp = NULL;
3262 vn_trace_entry(BHV_TO_VNODE(dir_bdp), __FUNCTION__,
3263 (inst_t *)__return_address);
3264 dp = XFS_BHVTOI(dir_bdp);
3266 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
3267 return XFS_ERROR(EIO);
3269 lock_mode = xfs_ilock_map_shared(dp);
3270 error = xfs_dir_getdents(tp, dp, uiop, eofp);
3271 xfs_iunlock_map_shared(dp, lock_mode);
3278 bhv_desc_t *dir_bdp,
3279 bhv_vname_t *dentry,
3291 xfs_bmap_free_t free_list;
3292 xfs_fsblock_t first_block;
3293 boolean_t dp_joined_to_trans;
3294 bhv_vnode_t *dir_vp;
3297 xfs_fileoff_t first_fsb;
3298 xfs_filblks_t fs_blocks;
3300 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3307 struct xfs_dquot *udqp, *gdqp;
3309 char *link_name = VNAME(dentry);
3313 dir_vp = BHV_TO_VNODE(dir_bdp);
3314 dp = XFS_BHVTOI(dir_bdp);
3315 dp_joined_to_trans = B_FALSE;
3320 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3324 if (XFS_FORCED_SHUTDOWN(mp))
3325 return XFS_ERROR(EIO);
3327 link_namelen = VNAMELEN(dentry);
3330 * Check component lengths of the target path name.
3332 pathlen = strlen(target_path);
3333 if (pathlen >= MAXPATHLEN) /* total string too long */
3334 return XFS_ERROR(ENAMETOOLONG);
3335 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3339 for (total = 0, path = target_path; total < pathlen;) {
3343 while(*path == '/') {
3349 * Count up to the next slash or end of path.
3350 * Error out if the component is bigger than MAXNAMELEN.
3352 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3353 if (++len >= MAXNAMELEN) {
3354 error = ENAMETOOLONG;
3361 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_SYMLINK)) {
3362 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3363 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3364 link_name, target_path, 0, 0, 0);
3369 /* Return through std_return after this point. */
3372 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3373 prid = dp->i_d.di_projid;
3374 else if (vap->va_mask & XFS_AT_PROJID)
3375 prid = (xfs_prid_t)vap->va_projid;
3377 prid = (xfs_prid_t)dfltprid;
3380 * Make sure that we have allocated dquot(s) on disk.
3382 error = XFS_QM_DQVOPALLOC(mp, dp,
3383 current_fsuid(credp), current_fsgid(credp), prid,
3384 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3388 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3389 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3391 * The symlink will fit into the inode data fork?
3392 * There can't be any attributes so we get the whole variable part.
3394 if (pathlen <= XFS_LITINO(mp))
3397 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3398 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3399 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3400 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3401 if (error == ENOSPC && fs_blocks == 0) {
3403 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3404 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3412 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
3415 * Check whether the directory allows new symlinks or not.
3417 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3418 error = XFS_ERROR(EPERM);
3423 * Reserve disk quota : blocks and inode.
3425 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3430 * Check for ability to enter directory entry, if no space reserved.
3433 (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
3436 * Initialize the bmap freelist prior to calling either
3437 * bmapi or the directory create code.
3439 XFS_BMAP_INIT(&free_list, &first_block);
3442 * Allocate an inode for the symlink.
3444 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (vap->va_mode&~S_IFMT),
3445 1, 0, credp, prid, resblks > 0, &ip, NULL);
3447 if (error == ENOSPC)
3454 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3455 dp_joined_to_trans = B_TRUE;
3458 * Also attach the dquot(s) to it, if applicable.
3460 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3463 resblks -= XFS_IALLOC_SPACE_RES(mp);
3465 * If the symlink will fit into the inode, write it inline.
3467 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3468 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3469 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3470 ip->i_d.di_size = pathlen;
3473 * The inode was initially created in extent format.
3475 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3476 ip->i_df.if_flags |= XFS_IFINLINE;
3478 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3479 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3483 nmaps = SYMLINK_MAPS;
3485 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3486 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3487 &first_block, resblks, mval, &nmaps,
3494 resblks -= fs_blocks;
3495 ip->i_d.di_size = pathlen;
3496 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3498 cur_chunk = target_path;
3499 for (n = 0; n < nmaps; n++) {
3500 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3501 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3502 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3503 BTOBB(byte_cnt), 0);
3504 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3505 if (pathlen < byte_cnt) {
3508 pathlen -= byte_cnt;
3510 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3511 cur_chunk += byte_cnt;
3513 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3518 * Create the directory entry for the symlink.
3520 error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
3521 &first_block, &free_list, resblks);
3524 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3525 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3528 * Bump the in memory version number of the parent directory
3529 * so that other processes accessing it will recognize that
3530 * the directory has changed.
3535 * If this is a synchronous mount, make sure that the
3536 * symlink transaction goes to disk before returning to
3539 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3540 xfs_trans_set_sync(tp);
3544 * xfs_trans_commit normally decrements the vnode ref count
3545 * when it unlocks the inode. Since we want to return the
3546 * vnode to the caller, we bump the vnode ref count now.
3550 error = xfs_bmap_finish(&tp, &free_list, &committed);
3554 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3555 XFS_QM_DQRELE(mp, udqp);
3556 XFS_QM_DQRELE(mp, gdqp);
3558 /* Fall through to std_return with error = 0 or errno from
3559 * xfs_trans_commit */
3561 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
3562 DM_EVENT_POSTSYMLINK)) {
3563 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3564 dir_vp, DM_RIGHT_NULL,
3565 error ? NULL : XFS_ITOV(ip),
3566 DM_RIGHT_NULL, link_name, target_path,
3582 xfs_bmap_cancel(&free_list);
3583 cancel_flags |= XFS_TRANS_ABORT;
3585 xfs_trans_cancel(tp, cancel_flags);
3586 XFS_QM_DQRELE(mp, udqp);
3587 XFS_QM_DQRELE(mp, gdqp);
3589 if (!dp_joined_to_trans && (dp != NULL)) {
3590 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3600 * A fid routine that takes a pointer to a previously allocated
3601 * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
3611 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
3612 (inst_t *)__return_address);
3613 ASSERT(sizeof(fid_t) >= sizeof(xfs_fid2_t));
3615 xfid = (xfs_fid2_t *)fidp;
3616 ip = XFS_BHVTOI(bdp);
3617 xfid->fid_len = sizeof(xfs_fid2_t) - sizeof(xfid->fid_len);
3620 * use memcpy because the inode is a long long and there's no
3621 * assurance that xfid->fid_ino is properly aligned.
3623 memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
3624 xfid->fid_gen = ip->i_d.di_gen;
3636 bhv_vrwlock_t locktype)
3641 vp = BHV_TO_VNODE(bdp);
3644 ip = XFS_BHVTOI(bdp);
3645 if (locktype == VRWLOCK_WRITE) {
3646 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3647 } else if (locktype == VRWLOCK_TRY_READ) {
3648 return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
3649 } else if (locktype == VRWLOCK_TRY_WRITE) {
3650 return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
3652 ASSERT((locktype == VRWLOCK_READ) ||
3653 (locktype == VRWLOCK_WRITE_DIRECT));
3654 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3667 bhv_vrwlock_t locktype)
3672 vp = BHV_TO_VNODE(bdp);
3675 ip = XFS_BHVTOI(bdp);
3676 if (locktype == VRWLOCK_WRITE) {
3678 * In the write case, we may have added a new entry to
3679 * the reference cache. This might store a pointer to
3680 * an inode to be released in this inode. If it is there,
3681 * clear the pointer and release the inode after unlocking
3684 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3686 ASSERT((locktype == VRWLOCK_READ) ||
3687 (locktype == VRWLOCK_WRITE_DIRECT));
3688 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3700 xfs_inode_log_item_t *iip;
3703 ip = XFS_BHVTOI(bdp);
3707 if (XFS_FORCED_SHUTDOWN(mp))
3708 return XFS_ERROR(EIO);
3711 * Bypass inodes which have already been cleaned by
3712 * the inode flush clustering code inside xfs_iflush
3714 if ((ip->i_update_core == 0) &&
3715 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3718 if (flags & FLUSH_LOG) {
3719 if (iip && iip->ili_last_lsn) {
3720 xlog_t *log = mp->m_log;
3722 int s, log_flags = XFS_LOG_FORCE;
3724 s = GRANT_LOCK(log);
3725 sync_lsn = log->l_last_sync_lsn;
3726 GRANT_UNLOCK(log, s);
3728 if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) <= 0))
3731 if (flags & FLUSH_SYNC)
3732 log_flags |= XFS_LOG_SYNC;
3733 return xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3738 * We make this non-blocking if the inode is contended,
3739 * return EAGAIN to indicate to the caller that they
3740 * did not succeed. This prevents the flush path from
3741 * blocking on inodes inside another operation right
3742 * now, they get caught later by xfs_sync.
3744 if (flags & FLUSH_INODE) {
3747 if (xfs_ipincount(ip))
3750 if (flags & FLUSH_SYNC) {
3751 xfs_ilock(ip, XFS_ILOCK_SHARED);
3753 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3754 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3755 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3762 if (flags & FLUSH_SYNC)
3763 flush_flags = XFS_IFLUSH_SYNC;
3765 flush_flags = XFS_IFLUSH_ASYNC;
3767 error = xfs_iflush(ip, flush_flags);
3768 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3786 if (!capable(CAP_SYS_ADMIN))
3787 return XFS_ERROR(EPERM);
3789 ip = XFS_BHVTOI(bdp);
3792 if (XFS_FORCED_SHUTDOWN(mp))
3793 return XFS_ERROR(EIO);
3795 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3796 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3798 xfs_trans_cancel(tp, 0);
3801 xfs_ilock(ip, XFS_ILOCK_EXCL);
3802 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3804 ip->i_iocore.io_dmevmask = ip->i_d.di_dmevmask = evmask;
3805 ip->i_iocore.io_dmstate = ip->i_d.di_dmstate = state;
3807 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3809 error = xfs_trans_commit(tp, 0);
3821 vp = BHV_TO_VNODE(bdp);
3822 ip = XFS_BHVTOI(bdp);
3824 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
3826 ASSERT(!VN_MAPPED(vp));
3828 /* bad inode, get out here ASAP */
3836 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
3839 * Make sure the atime in the XFS inode is correct before freeing the
3842 xfs_synchronize_atime(ip);
3845 * If we have nothing to flush with this inode then complete the
3846 * teardown now, otherwise break the link between the xfs inode and the
3847 * linux inode and clean up the xfs inode later. This avoids flushing
3848 * the inode to disk during the delete operation itself.
3850 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3851 * first to ensure that xfs_iunpin() will never see an xfs inode
3852 * that has a linux inode being reclaimed. Synchronisation is provided
3853 * by the i_flags_lock.
3855 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3856 xfs_ilock(ip, XFS_ILOCK_EXCL);
3858 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3860 xfs_mount_t *mp = ip->i_mount;
3862 /* Protect sync and unpin from us */
3863 XFS_MOUNT_ILOCK(mp);
3864 spin_lock(&ip->i_flags_lock);
3865 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
3866 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
3867 spin_unlock(&ip->i_flags_lock);
3868 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
3869 XFS_MOUNT_IUNLOCK(mp);
3880 xfs_ihash_t *ih = ip->i_hash;
3881 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
3884 if (vp && VN_BAD(vp))
3887 /* The hash lock here protects a thread in xfs_iget_core from
3888 * racing with us on linking the inode back with a vnode.
3889 * Once we have the XFS_IRECLAIM flag set it will not touch
3892 write_lock(&ih->ih_lock);
3893 spin_lock(&ip->i_flags_lock);
3894 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3895 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
3896 spin_unlock(&ip->i_flags_lock);
3897 write_unlock(&ih->ih_lock);
3900 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3904 __xfs_iflags_set(ip, XFS_IRECLAIM);
3905 spin_unlock(&ip->i_flags_lock);
3906 write_unlock(&ih->ih_lock);
3909 * If the inode is still dirty, then flush it out. If the inode
3910 * is not in the AIL, then it will be OK to flush it delwri as
3911 * long as xfs_iflush() does not keep any references to the inode.
3912 * We leave that decision up to xfs_iflush() since it has the
3913 * knowledge of whether it's OK to simply do a delwri flush of
3914 * the inode or whether we need to wait until the inode is
3915 * pulled from the AIL.
3916 * We get the flush lock regardless, though, just to make sure
3917 * we don't free it while it is being flushed.
3919 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3921 xfs_ilock(ip, XFS_ILOCK_EXCL);
3925 if (ip->i_update_core ||
3926 ((ip->i_itemp != NULL) &&
3927 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3928 error = xfs_iflush(ip, sync_mode);
3930 * If we hit an error, typically because of filesystem
3931 * shutdown, we don't need to let vn_reclaim to know
3932 * because we're gonna reclaim the inode anyway.
3935 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3938 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3941 ASSERT(ip->i_update_core == 0);
3942 ASSERT(ip->i_itemp == NULL ||
3943 ip->i_itemp->ili_format.ilf_fields == 0);
3944 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3945 } else if (locked) {
3947 * We are not interested in doing an iflush if we're
3948 * in the process of shutting down the filesystem forcibly.
3949 * So, just reclaim the inode.
3952 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3961 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3964 xfs_inode_t *ip, *n;
3969 XFS_MOUNT_ILOCK(mp);
3970 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3972 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3974 if (xfs_ipincount(ip) ||
3975 !xfs_iflock_nowait(ip)) {
3976 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3980 XFS_MOUNT_IUNLOCK(mp);
3981 if (xfs_finish_reclaim(ip, noblock,
3982 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
3991 XFS_MOUNT_IUNLOCK(mp);
3996 * xfs_alloc_file_space()
3997 * This routine allocates disk space for the given file.
3999 * If alloc_type == 0, this request is for an ALLOCSP type
4000 * request which will change the file size. In this case, no
4001 * DMAPI event will be generated by the call. A TRUNCATE event
4002 * will be generated later by xfs_setattr.
4004 * If alloc_type != 0, this request is for a RESVSP type
4005 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
4006 * lower block boundary byte address is less than the file's
4015 xfs_alloc_file_space(
4022 xfs_mount_t *mp = ip->i_mount;
4024 xfs_filblks_t allocated_fsb;
4025 xfs_filblks_t allocatesize_fsb;
4026 xfs_extlen_t extsz, temp;
4027 xfs_fileoff_t startoffset_fsb;
4028 xfs_fsblock_t firstfsb;
4034 xfs_bmbt_irec_t imaps[1], *imapp;
4035 xfs_bmap_free_t free_list;
4036 uint qblocks, resblks, resrtextents;
4040 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
4042 if (XFS_FORCED_SHUTDOWN(mp))
4043 return XFS_ERROR(EIO);
4045 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4049 return XFS_ERROR(EINVAL);
4051 rt = XFS_IS_REALTIME_INODE(ip);
4052 extsz = xfs_get_extsz_hint(ip);
4057 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
4058 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
4059 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
4061 /* Generate a DMAPI event if needed. */
4062 if (alloc_type != 0 && offset < ip->i_size &&
4063 (attr_flags&ATTR_DMI) == 0 &&
4064 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4065 xfs_off_t end_dmi_offset;
4067 end_dmi_offset = offset+len;
4068 if (end_dmi_offset > ip->i_size)
4069 end_dmi_offset = ip->i_size;
4070 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
4071 offset, end_dmi_offset - offset,
4078 * Allocate file space until done or until there is an error
4081 while (allocatesize_fsb && !error) {
4085 * Determine space reservations for data/realtime.
4087 if (unlikely(extsz)) {
4088 s = startoffset_fsb;
4091 e = startoffset_fsb + allocatesize_fsb;
4092 if ((temp = do_mod(startoffset_fsb, extsz)))
4094 if ((temp = do_mod(e, extsz)))
4098 e = allocatesize_fsb;
4102 resrtextents = qblocks = (uint)(e - s);
4103 resrtextents /= mp->m_sb.sb_rextsize;
4104 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4105 quota_flag = XFS_QMOPT_RES_RTBLKS;
4108 resblks = qblocks = \
4109 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
4110 quota_flag = XFS_QMOPT_RES_REGBLKS;
4114 * Allocate and setup the transaction.
4116 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4117 error = xfs_trans_reserve(tp, resblks,
4118 XFS_WRITE_LOG_RES(mp), resrtextents,
4119 XFS_TRANS_PERM_LOG_RES,
4120 XFS_WRITE_LOG_COUNT);
4122 * Check for running out of space
4126 * Free the transaction structure.
4128 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4129 xfs_trans_cancel(tp, 0);
4132 xfs_ilock(ip, XFS_ILOCK_EXCL);
4133 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
4134 qblocks, 0, quota_flag);
4138 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4139 xfs_trans_ihold(tp, ip);
4142 * Issue the xfs_bmapi() call to allocate the blocks
4144 XFS_BMAP_INIT(&free_list, &firstfsb);
4145 error = XFS_BMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4146 allocatesize_fsb, bmapi_flag,
4147 &firstfsb, 0, imapp, &nimaps,
4154 * Complete the transaction
4156 error = xfs_bmap_finish(&tp, &free_list, &committed);
4161 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
4162 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4167 allocated_fsb = imapp->br_blockcount;
4170 error = XFS_ERROR(ENOSPC);
4174 startoffset_fsb += allocated_fsb;
4175 allocatesize_fsb -= allocated_fsb;
4178 if (error == ENOSPC && (attr_flags&ATTR_DMI) == 0 &&
4179 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_NOSPACE)) {
4181 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
4182 XFS_ITOV(ip), DM_RIGHT_NULL,
4183 XFS_ITOV(ip), DM_RIGHT_NULL,
4184 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
4186 goto retry; /* Maybe DMAPI app. has made space */
4187 /* else fall through with error from XFS_SEND_DATA */
4192 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
4193 xfs_bmap_cancel(&free_list);
4194 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
4196 error1: /* Just cancel transaction */
4197 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4198 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4199 goto dmapi_enospc_check;
4203 * Zero file bytes between startoff and endoff inclusive.
4204 * The iolock is held exclusive and no blocks are buffered.
4207 xfs_zero_remaining_bytes(
4212 xfs_bmbt_irec_t imap;
4213 xfs_fileoff_t offset_fsb;
4214 xfs_off_t lastoffset;
4217 xfs_mount_t *mp = ip->i_mount;
4221 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
4222 ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
4223 mp->m_rtdev_targp : mp->m_ddev_targp);
4225 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4226 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4228 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, offset_fsb, 1, 0,
4229 NULL, 0, &imap, &nimap, NULL, NULL);
4230 if (error || nimap < 1)
4232 ASSERT(imap.br_blockcount >= 1);
4233 ASSERT(imap.br_startoff == offset_fsb);
4234 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4235 if (lastoffset > endoff)
4236 lastoffset = endoff;
4237 if (imap.br_startblock == HOLESTARTBLOCK)
4239 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4240 if (imap.br_state == XFS_EXT_UNWRITTEN)
4243 XFS_BUF_UNWRITE(bp);
4245 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4247 if ((error = xfs_iowait(bp))) {
4248 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4249 mp, bp, XFS_BUF_ADDR(bp));
4252 memset(XFS_BUF_PTR(bp) +
4253 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4254 0, lastoffset - offset + 1);
4259 if ((error = xfs_iowait(bp))) {
4260 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4261 mp, bp, XFS_BUF_ADDR(bp));
4270 * xfs_free_file_space()
4271 * This routine frees disk space for the given file.
4273 * This routine is only called by xfs_change_file_space
4274 * for an UNRESVSP type call.
4282 xfs_free_file_space(
4291 xfs_off_t end_dmi_offset;
4292 xfs_fileoff_t endoffset_fsb;
4294 xfs_fsblock_t firstfsb;
4295 xfs_bmap_free_t free_list;
4296 xfs_bmbt_irec_t imap;
4304 xfs_fileoff_t startoffset_fsb;
4306 int need_iolock = 1;
4311 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4313 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4317 if (len <= 0) /* if nothing being freed */
4319 rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4320 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4321 end_dmi_offset = offset + len;
4322 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4324 if (offset < ip->i_size &&
4325 (attr_flags & ATTR_DMI) == 0 &&
4326 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4327 if (end_dmi_offset > ip->i_size)
4328 end_dmi_offset = ip->i_size;
4329 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
4330 offset, end_dmi_offset - offset,
4331 AT_DELAY_FLAG(attr_flags), NULL);
4336 if (attr_flags & ATTR_NOLOCK)
4339 xfs_ilock(ip, XFS_IOLOCK_EXCL);
4340 vn_iowait(vp); /* wait for the completion of any pending DIOs */
4343 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, NBPP);
4344 ioffset = offset & ~(rounding - 1);
4346 if (VN_CACHED(vp) != 0) {
4347 xfs_inval_cached_trace(&ip->i_iocore, ioffset, -1,
4348 ctooff(offtoct(ioffset)), -1);
4349 error = bhv_vop_flushinval_pages(vp, ctooff(offtoct(ioffset)),
4350 -1, FI_REMAPF_LOCKED);
4352 goto out_unlock_iolock;
4356 * Need to zero the stuff we're not freeing, on disk.
4357 * If its a realtime file & can't use unwritten extents then we
4358 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4359 * will take care of it for us.
4361 if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4363 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, startoffset_fsb,
4364 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4366 goto out_unlock_iolock;
4367 ASSERT(nimap == 0 || nimap == 1);
4368 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4371 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4372 block = imap.br_startblock;
4373 mod = do_div(block, mp->m_sb.sb_rextsize);
4375 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4378 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, endoffset_fsb - 1,
4379 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4381 goto out_unlock_iolock;
4382 ASSERT(nimap == 0 || nimap == 1);
4383 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4384 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4386 if (mod && (mod != mp->m_sb.sb_rextsize))
4387 endoffset_fsb -= mod;
4390 if ((done = (endoffset_fsb <= startoffset_fsb)))
4392 * One contiguous piece to clear
4394 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4397 * Some full blocks, possibly two pieces to clear
4399 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4400 error = xfs_zero_remaining_bytes(ip, offset,
4401 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4403 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4404 error = xfs_zero_remaining_bytes(ip,
4405 XFS_FSB_TO_B(mp, endoffset_fsb),
4410 * free file space until done or until there is an error
4412 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4413 while (!error && !done) {
4416 * allocate and setup the transaction
4418 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4419 error = xfs_trans_reserve(tp,
4421 XFS_WRITE_LOG_RES(mp),
4423 XFS_TRANS_PERM_LOG_RES,
4424 XFS_WRITE_LOG_COUNT);
4427 * check for running out of space
4431 * Free the transaction structure.
4433 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4434 xfs_trans_cancel(tp, 0);
4437 xfs_ilock(ip, XFS_ILOCK_EXCL);
4438 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4439 ip->i_udquot, ip->i_gdquot, resblks, 0,
4440 XFS_QMOPT_RES_REGBLKS);
4444 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4445 xfs_trans_ihold(tp, ip);
4448 * issue the bunmapi() call to free the blocks
4450 XFS_BMAP_INIT(&free_list, &firstfsb);
4451 error = XFS_BUNMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4452 endoffset_fsb - startoffset_fsb,
4453 0, 2, &firstfsb, &free_list, NULL, &done);
4459 * complete the transaction
4461 error = xfs_bmap_finish(&tp, &free_list, &committed);
4466 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
4467 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4472 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4476 xfs_bmap_cancel(&free_list);
4478 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4479 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4485 * xfs_change_file_space()
4486 * This routine allocates or frees disk space for the given file.
4487 * The user specified parameters are checked for alignment and size
4496 xfs_change_file_space(
4510 xfs_off_t startoffset;
4516 vp = BHV_TO_VNODE(bdp);
4517 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4519 ip = XFS_BHVTOI(bdp);
4523 * must be a regular file and have write permission
4526 return XFS_ERROR(EINVAL);
4528 xfs_ilock(ip, XFS_ILOCK_SHARED);
4530 if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
4531 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4535 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4537 switch (bf->l_whence) {
4538 case 0: /*SEEK_SET*/
4540 case 1: /*SEEK_CUR*/
4541 bf->l_start += offset;
4543 case 2: /*SEEK_END*/
4544 bf->l_start += ip->i_size;
4547 return XFS_ERROR(EINVAL);
4550 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4552 if ( (bf->l_start < 0)
4553 || (bf->l_start > XFS_MAXIOFFSET(mp))
4554 || (bf->l_start + llen < 0)
4555 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4556 return XFS_ERROR(EINVAL);
4560 startoffset = bf->l_start;
4564 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4566 * These calls do NOT zero the data space allocated to the file,
4567 * nor do they change the file size.
4569 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4571 * These calls cause the new file data to be zeroed and the file
4572 * size to be changed.
4574 setprealloc = clrprealloc = 0;
4577 case XFS_IOC_RESVSP:
4578 case XFS_IOC_RESVSP64:
4579 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4586 case XFS_IOC_UNRESVSP:
4587 case XFS_IOC_UNRESVSP64:
4588 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4593 case XFS_IOC_ALLOCSP:
4594 case XFS_IOC_ALLOCSP64:
4595 case XFS_IOC_FREESP:
4596 case XFS_IOC_FREESP64:
4597 if (startoffset > fsize) {
4598 error = xfs_alloc_file_space(ip, fsize,
4599 startoffset - fsize, 0, attr_flags);
4604 va.va_mask = XFS_AT_SIZE;
4605 va.va_size = startoffset;
4607 error = xfs_setattr(bdp, &va, attr_flags, credp);
4617 return XFS_ERROR(EINVAL);
4621 * update the inode timestamp, mode, and prealloc flag bits
4623 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4625 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4628 xfs_trans_cancel(tp, 0);
4632 xfs_ilock(ip, XFS_ILOCK_EXCL);
4634 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4635 xfs_trans_ihold(tp, ip);
4637 if ((attr_flags & ATTR_DMI) == 0) {
4638 ip->i_d.di_mode &= ~S_ISUID;
4641 * Note that we don't have to worry about mandatory
4642 * file locking being disabled here because we only
4643 * clear the S_ISGID bit if the Group execute bit is
4644 * on, but if it was on then mandatory locking wouldn't
4645 * have been enabled.
4647 if (ip->i_d.di_mode & S_IXGRP)
4648 ip->i_d.di_mode &= ~S_ISGID;
4650 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4653 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4654 else if (clrprealloc)
4655 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4657 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4658 xfs_trans_set_sync(tp);
4660 error = xfs_trans_commit(tp, 0);
4662 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4667 bhv_vnodeops_t xfs_vnodeops = {
4668 BHV_IDENTITY_INIT(VN_BHV_XFS,VNODE_POSITION_XFS),
4669 .vop_open = xfs_open,
4670 .vop_read = xfs_read,
4672 .vop_splice_read = xfs_splice_read,
4673 .vop_splice_write = xfs_splice_write,
4675 .vop_write = xfs_write,
4676 .vop_ioctl = xfs_ioctl,
4677 .vop_getattr = xfs_getattr,
4678 .vop_setattr = xfs_setattr,
4679 .vop_access = xfs_access,
4680 .vop_lookup = xfs_lookup,
4681 .vop_create = xfs_create,
4682 .vop_remove = xfs_remove,
4683 .vop_link = xfs_link,
4684 .vop_rename = xfs_rename,
4685 .vop_mkdir = xfs_mkdir,
4686 .vop_rmdir = xfs_rmdir,
4687 .vop_readdir = xfs_readdir,
4688 .vop_symlink = xfs_symlink,
4689 .vop_readlink = xfs_readlink,
4690 .vop_fsync = xfs_fsync,
4691 .vop_inactive = xfs_inactive,
4692 .vop_fid2 = xfs_fid2,
4693 .vop_rwlock = xfs_rwlock,
4694 .vop_rwunlock = xfs_rwunlock,
4695 .vop_bmap = xfs_bmap,
4696 .vop_reclaim = xfs_reclaim,
4697 .vop_attr_get = xfs_attr_get,
4698 .vop_attr_set = xfs_attr_set,
4699 .vop_attr_remove = xfs_attr_remove,
4700 .vop_attr_list = xfs_attr_list,
4701 .vop_link_removed = (vop_link_removed_t)fs_noval,
4702 .vop_vnode_change = (vop_vnode_change_t)fs_noval,
4703 .vop_tosspages = fs_tosspages,
4704 .vop_flushinval_pages = fs_flushinval_pages,
4705 .vop_flush_pages = fs_flush_pages,
4706 .vop_release = xfs_release,
4707 .vop_iflush = xfs_inode_flush,