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);
84 lastclose_t lastclose,
87 bhv_vnode_t *vp = BHV_TO_VNODE(bdp);
88 xfs_inode_t *ip = XFS_BHVTOI(bdp);
90 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
91 return XFS_ERROR(EIO);
93 if (lastclose != L_TRUE || !VN_ISREG(vp))
97 * If we previously truncated this file and removed old data in
98 * the process, we want to initiate "early" writeout on the last
99 * close. This is an attempt to combat the notorious NULL files
100 * problem which is particularly noticable from a truncate down,
101 * buffered (re-)write (delalloc), followed by a crash. What we
102 * are effectively doing here is significantly reducing the time
103 * window where we'd otherwise be exposed to that problem.
105 if (VUNTRUNCATE(vp) && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
106 return bhv_vop_flush_pages(vp, 0, -1, XFS_B_ASYNC, FI_NONE);
124 vp = BHV_TO_VNODE(bdp);
125 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
127 ip = XFS_BHVTOI(bdp);
130 if (XFS_FORCED_SHUTDOWN(mp))
131 return XFS_ERROR(EIO);
133 if (!(flags & ATTR_LAZY))
134 xfs_ilock(ip, XFS_ILOCK_SHARED);
136 vap->va_size = XFS_ISIZE(ip);
137 if (vap->va_mask == XFS_AT_SIZE)
141 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
142 vap->va_nodeid = ip->i_ino;
144 vap->va_nodeid += mp->m_inoadd;
146 vap->va_nlink = ip->i_d.di_nlink;
149 * Quick exit for non-stat callers
152 ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
153 XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
157 * Copy from in-core inode.
159 vap->va_mode = ip->i_d.di_mode;
160 vap->va_uid = ip->i_d.di_uid;
161 vap->va_gid = ip->i_d.di_gid;
162 vap->va_projid = ip->i_d.di_projid;
165 * Check vnode type block/char vs. everything else.
167 switch (ip->i_d.di_mode & S_IFMT) {
170 vap->va_rdev = ip->i_df.if_u2.if_rdev;
171 vap->va_blocksize = BLKDEV_IOSIZE;
176 if (!(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
177 vap->va_blocksize = xfs_preferred_iosize(mp);
181 * If the file blocks are being allocated from a
182 * realtime partition, then return the inode's
183 * realtime extent size or the realtime volume's
187 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
192 vn_atime_to_timespec(vp, &vap->va_atime);
193 vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
194 vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
195 vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
196 vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
199 * Exit for stat callers. See if any of the rest of the fields
200 * to be filled in are needed.
203 (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
204 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
208 * Convert di_flags to xflags.
210 vap->va_xflags = xfs_ip2xflags(ip);
213 * Exit for inode revalidate. See if any of the rest of
214 * the fields to be filled in are needed.
217 (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
218 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
221 vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
223 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
224 ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
228 (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
229 ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
230 ip->i_d.di_anextents;
232 vap->va_anextents = 0;
233 vap->va_gen = ip->i_d.di_gen;
236 if (!(flags & ATTR_LAZY))
237 xfs_iunlock(ip, XFS_ILOCK_SHARED);
263 xfs_prid_t projid=0, iprojid=0;
264 int mandlock_before, mandlock_after;
265 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
269 vp = BHV_TO_VNODE(bdp);
270 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
272 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
273 return XFS_ERROR(EROFS);
276 * Cannot set certain attributes.
279 if (mask & XFS_AT_NOSET) {
280 return XFS_ERROR(EINVAL);
283 ip = XFS_BHVTOI(bdp);
286 if (XFS_FORCED_SHUTDOWN(mp))
287 return XFS_ERROR(EIO);
290 * Timestamps do not need to be logged and hence do not
291 * need to be done within a transaction.
293 if (mask & XFS_AT_UPDTIMES) {
294 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
295 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
296 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
297 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
298 xfs_ichgtime(ip, timeflags);
302 olddquot1 = olddquot2 = NULL;
306 * If disk quotas is on, we make sure that the dquots do exist on disk,
307 * before we start any other transactions. Trying to do this later
308 * is messy. We don't care to take a readlock to look at the ids
309 * in inode here, because we can't hold it across the trans_reserve.
310 * If the IDs do change before we take the ilock, we're covered
311 * because the i_*dquot fields will get updated anyway.
313 if (XFS_IS_QUOTA_ON(mp) &&
314 (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
317 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
319 qflags |= XFS_QMOPT_UQUOTA;
321 uid = ip->i_d.di_uid;
323 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
325 qflags |= XFS_QMOPT_GQUOTA;
327 gid = ip->i_d.di_gid;
329 if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
330 projid = vap->va_projid;
331 qflags |= XFS_QMOPT_PQUOTA;
333 projid = ip->i_d.di_projid;
336 * We take a reference when we initialize udqp and gdqp,
337 * so it is important that we never blindly double trip on
338 * the same variable. See xfs_create() for an example.
340 ASSERT(udqp == NULL);
341 ASSERT(gdqp == NULL);
342 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
349 * For the other attributes, we acquire the inode lock and
350 * first do an error checking pass.
353 lock_flags = XFS_ILOCK_EXCL;
354 if (flags & ATTR_NOLOCK)
356 if (!(mask & XFS_AT_SIZE)) {
357 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
358 (mp->m_flags & XFS_MOUNT_WSYNC)) {
359 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
361 if ((code = xfs_trans_reserve(tp, 0,
362 XFS_ICHANGE_LOG_RES(mp), 0,
369 if (DM_EVENT_ENABLED (vp->v_vfsp, ip, DM_EVENT_TRUNCATE) &&
370 !(flags & ATTR_DMI)) {
371 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
372 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
373 vap->va_size, 0, dmflags, NULL);
380 lock_flags |= XFS_IOLOCK_EXCL;
383 xfs_ilock(ip, lock_flags);
385 /* boolean: are we the file owner? */
386 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
389 * Change various properties of a file.
390 * Only the owner or users with CAP_FOWNER
391 * capability may do these things.
394 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
395 XFS_AT_GID|XFS_AT_PROJID)) {
397 * CAP_FOWNER overrides the following restrictions:
399 * The user ID of the calling process must be equal
400 * to the file owner ID, except in cases where the
401 * CAP_FSETID capability is applicable.
403 if (!file_owner && !capable(CAP_FOWNER)) {
404 code = XFS_ERROR(EPERM);
409 * CAP_FSETID overrides the following restrictions:
411 * The effective user ID of the calling process shall match
412 * the file owner when setting the set-user-ID and
413 * set-group-ID bits on that file.
415 * The effective group ID or one of the supplementary group
416 * IDs of the calling process shall match the group owner of
417 * the file when setting the set-group-ID bit on that file
419 if (mask & XFS_AT_MODE) {
422 if ((vap->va_mode & S_ISUID) && !file_owner)
424 if ((vap->va_mode & S_ISGID) &&
425 !in_group_p((gid_t)ip->i_d.di_gid))
428 /* Linux allows this, Irix doesn't. */
429 if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
432 if (m && !capable(CAP_FSETID))
438 * Change file ownership. Must be the owner or privileged.
439 * If the system was configured with the "restricted_chown"
440 * option, the owner is not permitted to give away the file,
441 * and can change the group id only to a group of which he
442 * or she is a member.
444 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
446 * These IDs could have changed since we last looked at them.
447 * But, we're assured that if the ownership did change
448 * while we didn't have the inode locked, inode's dquot(s)
449 * would have changed also.
451 iuid = ip->i_d.di_uid;
452 iprojid = ip->i_d.di_projid;
453 igid = ip->i_d.di_gid;
454 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
455 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
456 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
460 * CAP_CHOWN overrides the following restrictions:
462 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
463 * shall override the restriction that a process cannot
464 * change the user ID of a file it owns and the restriction
465 * that the group ID supplied to the chown() function
466 * shall be equal to either the group ID or one of the
467 * supplementary group IDs of the calling process.
469 if (restricted_chown &&
470 (iuid != uid || (igid != gid &&
471 !in_group_p((gid_t)gid))) &&
472 !capable(CAP_CHOWN)) {
473 code = XFS_ERROR(EPERM);
477 * Do a quota reservation only if uid/projid/gid is actually
480 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
481 (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
482 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
484 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
485 capable(CAP_FOWNER) ?
486 XFS_QMOPT_FORCE_RES : 0);
487 if (code) /* out of quota */
493 * Truncate file. Must have write permission and not be a directory.
495 if (mask & XFS_AT_SIZE) {
496 /* Short circuit the truncate case for zero length files */
497 if ((vap->va_size == 0) &&
498 (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) {
499 xfs_iunlock(ip, XFS_ILOCK_EXCL);
500 lock_flags &= ~XFS_ILOCK_EXCL;
501 if (mask & XFS_AT_CTIME)
502 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
508 code = XFS_ERROR(EISDIR);
510 } else if (!VN_ISREG(vp)) {
511 code = XFS_ERROR(EINVAL);
515 * Make sure that the dquots are attached to the inode.
517 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
522 * Change file access or modified times.
524 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
526 if ((flags & ATTR_UTIME) &&
527 !capable(CAP_FOWNER)) {
528 code = XFS_ERROR(EPERM);
535 * Change extent size or realtime flag.
537 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
539 * Can't change extent size if any extents are allocated.
541 if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
542 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
544 code = XFS_ERROR(EINVAL); /* EFBIG? */
549 * Can't change realtime flag if any extents are allocated.
551 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
552 (mask & XFS_AT_XFLAGS) &&
553 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) !=
554 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
555 code = XFS_ERROR(EINVAL); /* EFBIG? */
559 * Extent size must be a multiple of the appropriate block
560 * size, if set at all.
562 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
565 if ((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
566 ((mask & XFS_AT_XFLAGS) &&
567 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
568 size = mp->m_sb.sb_rextsize <<
569 mp->m_sb.sb_blocklog;
571 size = mp->m_sb.sb_blocksize;
573 if (vap->va_extsize % size) {
574 code = XFS_ERROR(EINVAL);
579 * If realtime flag is set then must have realtime data.
581 if ((mask & XFS_AT_XFLAGS) &&
582 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
583 if ((mp->m_sb.sb_rblocks == 0) ||
584 (mp->m_sb.sb_rextsize == 0) ||
585 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
586 code = XFS_ERROR(EINVAL);
592 * Can't modify an immutable/append-only file unless
593 * we have appropriate permission.
595 if ((mask & XFS_AT_XFLAGS) &&
597 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
599 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
600 !capable(CAP_LINUX_IMMUTABLE)) {
601 code = XFS_ERROR(EPERM);
607 * Now we can make the changes. Before we join the inode
608 * to the transaction, if XFS_AT_SIZE is set then take care of
609 * the part of the truncation that must be done without the
610 * inode lock. This needs to be done before joining the inode
611 * to the transaction, because the inode cannot be unlocked
612 * once it is a part of the transaction.
614 if (mask & XFS_AT_SIZE) {
616 if ((vap->va_size > ip->i_size) &&
617 (flags & ATTR_NOSIZETOK) == 0) {
618 code = xfs_igrow_start(ip, vap->va_size, credp);
620 xfs_iunlock(ip, XFS_ILOCK_EXCL);
621 vn_iowait(vp); /* wait for the completion of any pending DIOs */
623 code = xfs_itruncate_data(ip, vap->va_size);
626 lock_flags &= ~XFS_ILOCK_EXCL;
627 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
630 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
631 if ((code = xfs_trans_reserve(tp, 0,
632 XFS_ITRUNCATE_LOG_RES(mp), 0,
633 XFS_TRANS_PERM_LOG_RES,
634 XFS_ITRUNCATE_LOG_COUNT))) {
635 xfs_trans_cancel(tp, 0);
637 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
640 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
641 xfs_ilock(ip, XFS_ILOCK_EXCL);
645 xfs_trans_ijoin(tp, ip, lock_flags);
646 xfs_trans_ihold(tp, ip);
649 /* determine whether mandatory locking mode changes */
650 mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
653 * Truncate file. Must have write permission and not be a directory.
655 if (mask & XFS_AT_SIZE) {
656 if (vap->va_size > ip->i_size) {
657 xfs_igrow_finish(tp, ip, vap->va_size,
658 !(flags & ATTR_DMI));
659 } else if ((vap->va_size <= ip->i_size) ||
660 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
662 * signal a sync transaction unless
663 * we're truncating an already unlinked
664 * file on a wsync filesystem
666 code = xfs_itruncate_finish(&tp, ip,
667 (xfs_fsize_t)vap->va_size,
669 ((ip->i_d.di_nlink != 0 ||
670 !(mp->m_flags & XFS_MOUNT_WSYNC))
675 * Truncated "down", so we're removing references
676 * to old data here - if we now delay flushing for
677 * a long time, we expose ourselves unduly to the
678 * notorious NULL files problem. So, we mark this
679 * vnode and flush it when the file is closed, and
680 * do not wait the usual (long) time for writeout.
685 * Have to do this even if the file's size doesn't change.
687 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
691 * Change file access modes.
693 if (mask & XFS_AT_MODE) {
694 ip->i_d.di_mode &= S_IFMT;
695 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
697 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
698 timeflags |= XFS_ICHGTIME_CHG;
702 * Change file ownership. Must be the owner or privileged.
703 * If the system was configured with the "restricted_chown"
704 * option, the owner is not permitted to give away the file,
705 * and can change the group id only to a group of which he
706 * or she is a member.
708 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
710 * CAP_FSETID overrides the following restrictions:
712 * The set-user-ID and set-group-ID bits of a file will be
713 * cleared upon successful return from chown()
715 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
716 !capable(CAP_FSETID)) {
717 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
721 * Change the ownerships and register quota modifications
722 * in the transaction.
725 if (XFS_IS_UQUOTA_ON(mp)) {
726 ASSERT(mask & XFS_AT_UID);
728 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
729 &ip->i_udquot, udqp);
731 ip->i_d.di_uid = uid;
734 if (XFS_IS_GQUOTA_ON(mp)) {
735 ASSERT(!XFS_IS_PQUOTA_ON(mp));
736 ASSERT(mask & XFS_AT_GID);
738 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
739 &ip->i_gdquot, gdqp);
741 ip->i_d.di_gid = gid;
743 if (iprojid != projid) {
744 if (XFS_IS_PQUOTA_ON(mp)) {
745 ASSERT(!XFS_IS_GQUOTA_ON(mp));
746 ASSERT(mask & XFS_AT_PROJID);
748 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
749 &ip->i_gdquot, gdqp);
751 ip->i_d.di_projid = projid;
753 * We may have to rev the inode as well as
754 * the superblock version number since projids didn't
755 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
757 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
758 xfs_bump_ino_vers2(tp, ip);
761 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
762 timeflags |= XFS_ICHGTIME_CHG;
767 * Change file access or modified times.
769 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
770 if (mask & XFS_AT_ATIME) {
771 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
772 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
773 ip->i_update_core = 1;
774 timeflags &= ~XFS_ICHGTIME_ACC;
776 if (mask & XFS_AT_MTIME) {
777 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
778 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
779 timeflags &= ~XFS_ICHGTIME_MOD;
780 timeflags |= XFS_ICHGTIME_CHG;
782 if (tp && (flags & ATTR_UTIME))
783 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
787 * Change XFS-added attributes.
789 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
790 if (mask & XFS_AT_EXTSIZE) {
792 * Converting bytes to fs blocks.
794 ip->i_d.di_extsize = vap->va_extsize >>
795 mp->m_sb.sb_blocklog;
797 if (mask & XFS_AT_XFLAGS) {
800 /* can't set PREALLOC this way, just preserve it */
801 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
802 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
803 di_flags |= XFS_DIFLAG_IMMUTABLE;
804 if (vap->va_xflags & XFS_XFLAG_APPEND)
805 di_flags |= XFS_DIFLAG_APPEND;
806 if (vap->va_xflags & XFS_XFLAG_SYNC)
807 di_flags |= XFS_DIFLAG_SYNC;
808 if (vap->va_xflags & XFS_XFLAG_NOATIME)
809 di_flags |= XFS_DIFLAG_NOATIME;
810 if (vap->va_xflags & XFS_XFLAG_NODUMP)
811 di_flags |= XFS_DIFLAG_NODUMP;
812 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
813 di_flags |= XFS_DIFLAG_PROJINHERIT;
814 if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
815 di_flags |= XFS_DIFLAG_NODEFRAG;
816 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
817 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
818 di_flags |= XFS_DIFLAG_RTINHERIT;
819 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
820 di_flags |= XFS_DIFLAG_NOSYMLINKS;
821 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
822 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
823 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
824 if (vap->va_xflags & XFS_XFLAG_REALTIME) {
825 di_flags |= XFS_DIFLAG_REALTIME;
826 ip->i_iocore.io_flags |= XFS_IOCORE_RT;
828 ip->i_iocore.io_flags &= ~XFS_IOCORE_RT;
830 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
831 di_flags |= XFS_DIFLAG_EXTSIZE;
833 ip->i_d.di_flags = di_flags;
835 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
836 timeflags |= XFS_ICHGTIME_CHG;
840 * Change file inode change time only if XFS_AT_CTIME set
841 * AND we have been called by a DMI function.
844 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
845 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
846 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
847 ip->i_update_core = 1;
848 timeflags &= ~XFS_ICHGTIME_CHG;
852 * Send out timestamp changes that need to be set to the
853 * current time. Not done when called by a DMI function.
855 if (timeflags && !(flags & ATTR_DMI))
856 xfs_ichgtime(ip, timeflags);
858 XFS_STATS_INC(xs_ig_attrchg);
861 * If this is a synchronous mount, make sure that the
862 * transaction goes to disk before returning to the user.
863 * This is slightly sub-optimal in that truncates require
864 * two sync transactions instead of one for wsync filesystems.
865 * One for the truncate and one for the timestamps since we
866 * don't want to change the timestamps unless we're sure the
867 * truncate worked. Truncates are less than 1% of the laddis
868 * mix so this probably isn't worth the trouble to optimize.
872 if (mp->m_flags & XFS_MOUNT_WSYNC)
873 xfs_trans_set_sync(tp);
875 code = xfs_trans_commit(tp, commit_flags);
879 * If the (regular) file's mandatory locking mode changed, then
880 * notify the vnode. We do this under the inode lock to prevent
881 * racing calls to vop_vnode_change.
883 mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
884 if (mandlock_before != mandlock_after) {
885 bhv_vop_vnode_change(vp, VCHANGE_FLAGS_ENF_LOCKING,
889 xfs_iunlock(ip, lock_flags);
892 * Release any dquot(s) the inode had kept before chown.
894 XFS_QM_DQRELE(mp, olddquot1);
895 XFS_QM_DQRELE(mp, olddquot2);
896 XFS_QM_DQRELE(mp, udqp);
897 XFS_QM_DQRELE(mp, gdqp);
903 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_ATTRIBUTE) &&
904 !(flags & ATTR_DMI)) {
905 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
906 NULL, DM_RIGHT_NULL, NULL, NULL,
907 0, 0, AT_DELAY_FLAG(flags));
912 commit_flags |= XFS_TRANS_ABORT;
915 XFS_QM_DQRELE(mp, udqp);
916 XFS_QM_DQRELE(mp, gdqp);
918 xfs_trans_cancel(tp, commit_flags);
920 if (lock_flags != 0) {
921 xfs_iunlock(ip, lock_flags);
929 * Null conversion from vnode mode bits to inode mode bits, as in efs.
940 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
941 (inst_t *)__return_address);
943 ip = XFS_BHVTOI(bdp);
944 xfs_ilock(ip, XFS_ILOCK_SHARED);
945 error = xfs_iaccess(ip, mode, credp);
946 xfs_iunlock(ip, XFS_ILOCK_SHARED);
952 * The maximum pathlen is 1024 bytes. Since the minimum file system
953 * blocksize is 512 bytes, we can get a max of 2 extents back from
956 #define SYMLINK_MAPS 2
977 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
983 vp = BHV_TO_VNODE(bdp);
984 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
986 ip = XFS_BHVTOI(bdp);
989 if (XFS_FORCED_SHUTDOWN(mp))
990 return XFS_ERROR(EIO);
992 xfs_ilock(ip, XFS_ILOCK_SHARED);
994 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
996 offset = uiop->uio_offset;
997 count = uiop->uio_resid;
1000 error = XFS_ERROR(EINVAL);
1009 * See if the symlink is stored inline.
1011 pathlen = (int)ip->i_d.di_size;
1013 if (ip->i_df.if_flags & XFS_IFINLINE) {
1014 error = xfs_uio_read(ip->i_df.if_u1.if_data, pathlen, uiop);
1018 * Symlink not inline. Call bmap to get it in.
1020 nmaps = SYMLINK_MAPS;
1022 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen),
1023 0, NULL, 0, mval, &nmaps, NULL, NULL);
1029 for (n = 0; n < nmaps; n++) {
1030 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1031 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1032 bp = xfs_buf_read(mp->m_ddev_targp, d,
1033 BTOBB(byte_cnt), 0);
1034 error = XFS_BUF_GETERROR(bp);
1036 xfs_ioerror_alert("xfs_readlink",
1037 ip->i_mount, bp, XFS_BUF_ADDR(bp));
1041 if (pathlen < byte_cnt)
1043 pathlen -= byte_cnt;
1045 error = xfs_uio_read(XFS_BUF_PTR(bp), byte_cnt, uiop);
1052 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1060 * This is called to sync the inode and its data out to disk.
1061 * We need to hold the I/O lock while flushing the data, and
1062 * the inode lock while flushing the inode. The inode lock CANNOT
1063 * be held while flushing the data, so acquire after we're done
1077 int log_flushed = 0, changed = 1;
1079 vn_trace_entry(BHV_TO_VNODE(bdp),
1080 __FUNCTION__, (inst_t *)__return_address);
1082 ip = XFS_BHVTOI(bdp);
1084 ASSERT(start >= 0 && stop >= -1);
1086 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1087 return XFS_ERROR(EIO);
1090 * We always need to make sure that the required inode state
1091 * is safe on disk. The vnode might be clean but because
1092 * of committed transactions that haven't hit the disk yet.
1093 * Likewise, there could be unflushed non-transactional
1094 * changes to the inode core that have to go to disk.
1096 * The following code depends on one assumption: that
1097 * any transaction that changes an inode logs the core
1098 * because it has to change some field in the inode core
1099 * (typically nextents or nblocks). That assumption
1100 * implies that any transactions against an inode will
1101 * catch any non-transactional updates. If inode-altering
1102 * transactions exist that violate this assumption, the
1103 * code breaks. Right now, it figures that if the involved
1104 * update_* field is clear and the inode is unpinned, the
1105 * inode is clean. Either it's been flushed or it's been
1106 * committed and the commit has hit the disk unpinning the inode.
1107 * (Note that xfs_inode_item_format() called at commit clears
1108 * the update_* fields.)
1110 xfs_ilock(ip, XFS_ILOCK_SHARED);
1112 /* If we are flushing data then we care about update_size
1113 * being set, otherwise we care about update_core
1115 if ((flag & FSYNC_DATA) ?
1116 (ip->i_update_size == 0) :
1117 (ip->i_update_core == 0)) {
1119 * Timestamps/size haven't changed since last inode
1120 * flush or inode transaction commit. That means
1121 * either nothing got written or a transaction
1122 * committed which caught the updates. If the
1123 * latter happened and the transaction hasn't
1124 * hit the disk yet, the inode will be still
1125 * be pinned. If it is, force the log.
1128 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1130 if (xfs_ipincount(ip)) {
1131 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
1133 ((flag & FSYNC_WAIT)
1134 ? XFS_LOG_SYNC : 0),
1138 * If the inode is not pinned and nothing
1139 * has changed we don't need to flush the
1147 * Kick off a transaction to log the inode
1148 * core to get the updates. Make it
1149 * sync if FSYNC_WAIT is passed in (which
1150 * is done by everybody but specfs). The
1151 * sync transaction will also force the log.
1153 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1154 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1155 if ((error = xfs_trans_reserve(tp, 0,
1156 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1158 xfs_trans_cancel(tp, 0);
1161 xfs_ilock(ip, XFS_ILOCK_EXCL);
1164 * Note - it's possible that we might have pushed
1165 * ourselves out of the way during trans_reserve
1166 * which would flush the inode. But there's no
1167 * guarantee that the inode buffer has actually
1168 * gone out yet (it's delwri). Plus the buffer
1169 * could be pinned anyway if it's part of an
1170 * inode in another recent transaction. So we
1171 * play it safe and fire off the transaction anyway.
1173 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1174 xfs_trans_ihold(tp, ip);
1175 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1176 if (flag & FSYNC_WAIT)
1177 xfs_trans_set_sync(tp);
1178 error = _xfs_trans_commit(tp, 0, &log_flushed);
1180 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1183 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1185 * If the log write didn't issue an ordered tag we need
1186 * to flush the disk cache for the data device now.
1189 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1192 * If this inode is on the RT dev we need to flush that
1195 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
1196 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1203 * This is called by xfs_inactive to free any blocks beyond eof
1204 * when the link count isn't zero and by xfs_dm_punch_hole() when
1205 * punching a hole to EOF.
1215 xfs_fileoff_t end_fsb;
1216 xfs_fileoff_t last_fsb;
1217 xfs_filblks_t map_len;
1219 xfs_bmbt_irec_t imap;
1220 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
1223 * Figure out if there are any blocks beyond the end
1224 * of the file. If not, then there is nothing to do.
1226 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
1227 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1228 map_len = last_fsb - end_fsb;
1233 xfs_ilock(ip, XFS_ILOCK_SHARED);
1234 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, end_fsb, map_len, 0,
1235 NULL, 0, &imap, &nimaps, NULL, NULL);
1236 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1238 if (!error && (nimaps != 0) &&
1239 (imap.br_startblock != HOLESTARTBLOCK ||
1240 ip->i_delayed_blks)) {
1242 * Attach the dquots to the inode up front.
1244 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1248 * There are blocks after the end of file.
1249 * Free them up now by truncating the file to
1252 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1255 * Do the xfs_itruncate_start() call before
1256 * reserving any log space because
1257 * itruncate_start will call into the buffer
1258 * cache and we can't
1259 * do that within a transaction.
1262 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1263 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
1267 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1271 error = xfs_trans_reserve(tp, 0,
1272 XFS_ITRUNCATE_LOG_RES(mp),
1273 0, XFS_TRANS_PERM_LOG_RES,
1274 XFS_ITRUNCATE_LOG_COUNT);
1276 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1277 xfs_trans_cancel(tp, 0);
1278 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1282 xfs_ilock(ip, XFS_ILOCK_EXCL);
1283 xfs_trans_ijoin(tp, ip,
1286 xfs_trans_ihold(tp, ip);
1288 error = xfs_itruncate_finish(&tp, ip,
1293 * If we get an error at this point we
1294 * simply don't bother truncating the file.
1297 xfs_trans_cancel(tp,
1298 (XFS_TRANS_RELEASE_LOG_RES |
1301 error = xfs_trans_commit(tp,
1302 XFS_TRANS_RELEASE_LOG_RES);
1304 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1311 * Free a symlink that has blocks associated with it.
1314 xfs_inactive_symlink_rmt(
1322 xfs_fsblock_t first_block;
1323 xfs_bmap_free_t free_list;
1326 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1334 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1336 * We're freeing a symlink that has some
1337 * blocks allocated to it. Free the
1338 * blocks here. We know that we've got
1339 * either 1 or 2 extents and that we can
1340 * free them all in one bunmapi call.
1342 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1343 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1344 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1345 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1346 xfs_trans_cancel(tp, 0);
1351 * Lock the inode, fix the size, and join it to the transaction.
1352 * Hold it so in the normal path, we still have it locked for
1353 * the second transaction. In the error paths we need it
1354 * held so the cancel won't rele it, see below.
1356 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1357 size = (int)ip->i_d.di_size;
1358 ip->i_d.di_size = 0;
1359 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1360 xfs_trans_ihold(tp, ip);
1361 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1363 * Find the block(s) so we can inval and unmap them.
1366 XFS_BMAP_INIT(&free_list, &first_block);
1367 nmaps = ARRAY_SIZE(mval);
1368 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1369 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
1373 * Invalidate the block(s).
1375 for (i = 0; i < nmaps; i++) {
1376 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1377 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1378 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1379 xfs_trans_binval(tp, bp);
1382 * Unmap the dead block(s) to the free_list.
1384 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
1385 &first_block, &free_list, NULL, &done)))
1389 * Commit the first transaction. This logs the EFI and the inode.
1391 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
1394 * The transaction must have been committed, since there were
1395 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1396 * The new tp has the extent freeing and EFDs.
1400 * The first xact was committed, so add the inode to the new one.
1401 * Mark it dirty so it will be logged and moved forward in the log as
1402 * part of every commit.
1404 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1405 xfs_trans_ihold(tp, ip);
1406 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1408 * Get a new, empty transaction to return to our caller.
1410 ntp = xfs_trans_dup(tp);
1412 * Commit the transaction containing extent freeing and EFDs.
1413 * If we get an error on the commit here or on the reserve below,
1414 * we need to unlock the inode since the new transaction doesn't
1415 * have the inode attached.
1417 error = xfs_trans_commit(tp, 0);
1420 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1424 * Remove the memory for extent descriptions (just bookkeeping).
1426 if (ip->i_df.if_bytes)
1427 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1428 ASSERT(ip->i_df.if_bytes == 0);
1430 * Put an itruncate log reservation in the new transaction
1433 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1434 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1435 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1439 * Return with the inode locked but not joined to the transaction.
1445 xfs_bmap_cancel(&free_list);
1448 * Have to come here with the inode locked and either
1449 * (held and in the transaction) or (not in the transaction).
1450 * If the inode isn't held then cancel would iput it, but
1451 * that's wrong since this is inactive and the vnode ref
1452 * count is 0 already.
1453 * Cancel won't do anything to the inode if held, but it still
1454 * needs to be locked until the cancel is done, if it was
1455 * joined to the transaction.
1457 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1458 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1465 xfs_inactive_symlink_local(
1471 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1473 * We're freeing a symlink which fit into
1474 * the inode. Just free the memory used
1475 * to hold the old symlink.
1477 error = xfs_trans_reserve(*tpp, 0,
1478 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1479 0, XFS_TRANS_PERM_LOG_RES,
1480 XFS_ITRUNCATE_LOG_COUNT);
1483 xfs_trans_cancel(*tpp, 0);
1487 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1490 * Zero length symlinks _can_ exist.
1492 if (ip->i_df.if_bytes > 0) {
1493 xfs_idata_realloc(ip,
1494 -(ip->i_df.if_bytes),
1496 ASSERT(ip->i_df.if_bytes == 0);
1510 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1513 ASSERT(ip->i_d.di_forkoff != 0);
1514 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1515 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1517 error = xfs_attr_inactive(ip);
1520 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1521 return error; /* goto out */
1524 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1525 error = xfs_trans_reserve(tp, 0,
1526 XFS_IFREE_LOG_RES(mp),
1527 0, XFS_TRANS_PERM_LOG_RES,
1528 XFS_INACTIVE_LOG_COUNT);
1530 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1531 xfs_trans_cancel(tp, 0);
1533 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1537 xfs_ilock(ip, XFS_ILOCK_EXCL);
1538 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1539 xfs_trans_ihold(tp, ip);
1540 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1542 ASSERT(ip->i_d.di_anextents == 0);
1557 vp = BHV_TO_VNODE(bdp);
1558 ip = XFS_BHVTOI(bdp);
1561 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
1564 /* If this is a read-only mount, don't do this (would generate I/O) */
1565 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1568 #ifdef HAVE_REFCACHE
1569 /* If we are in the NFS reference cache then don't do this now */
1574 if (ip->i_d.di_nlink != 0) {
1575 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1576 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1577 ip->i_delayed_blks > 0)) &&
1578 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1579 (!(ip->i_d.di_flags &
1580 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1581 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1584 /* Update linux inode block count after free above */
1585 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1586 ip->i_d.di_nblocks + ip->i_delayed_blks);
1596 * This is called when the vnode reference count for the vnode
1597 * goes to zero. If the file has been unlinked, then it must
1598 * now be truncated. Also, we clear all of the read-ahead state
1599 * kept for the inode here since the file is now closed.
1608 xfs_bmap_free_t free_list;
1609 xfs_fsblock_t first_block;
1616 vp = BHV_TO_VNODE(bdp);
1617 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1619 ip = XFS_BHVTOI(bdp);
1622 * If the inode is already free, then there can be nothing
1625 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1626 ASSERT(ip->i_df.if_real_bytes == 0);
1627 ASSERT(ip->i_df.if_broot_bytes == 0);
1628 return VN_INACTIVE_CACHE;
1632 * Only do a truncate if it's a regular file with
1633 * some actual space in it. It's OK to look at the
1634 * inode's fields without the lock because we're the
1635 * only one with a reference to the inode.
1637 truncate = ((ip->i_d.di_nlink == 0) &&
1638 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1639 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1640 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1644 if (ip->i_d.di_nlink == 0 &&
1645 DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_DESTROY)) {
1646 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1651 /* If this is a read-only mount, don't do this (would generate I/O) */
1652 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1655 if (ip->i_d.di_nlink != 0) {
1656 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1657 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1658 ip->i_delayed_blks > 0)) &&
1659 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1660 (!(ip->i_d.di_flags &
1661 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1662 (ip->i_delayed_blks != 0)))) {
1663 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1665 return VN_INACTIVE_CACHE;
1666 /* Update linux inode block count after free above */
1667 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1668 ip->i_d.di_nblocks + ip->i_delayed_blks);
1673 ASSERT(ip->i_d.di_nlink == 0);
1675 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1676 return VN_INACTIVE_CACHE;
1678 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1681 * Do the xfs_itruncate_start() call before
1682 * reserving any log space because itruncate_start
1683 * will call into the buffer cache and we can't
1684 * do that within a transaction.
1686 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1688 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1690 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1691 return VN_INACTIVE_CACHE;
1694 error = xfs_trans_reserve(tp, 0,
1695 XFS_ITRUNCATE_LOG_RES(mp),
1696 0, XFS_TRANS_PERM_LOG_RES,
1697 XFS_ITRUNCATE_LOG_COUNT);
1699 /* Don't call itruncate_cleanup */
1700 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1701 xfs_trans_cancel(tp, 0);
1702 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1703 return VN_INACTIVE_CACHE;
1706 xfs_ilock(ip, XFS_ILOCK_EXCL);
1707 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1708 xfs_trans_ihold(tp, ip);
1711 * normally, we have to run xfs_itruncate_finish sync.
1712 * But if filesystem is wsync and we're in the inactive
1713 * path, then we know that nlink == 0, and that the
1714 * xaction that made nlink == 0 is permanently committed
1715 * since xfs_remove runs as a synchronous transaction.
1717 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1718 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1721 xfs_trans_cancel(tp,
1722 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1723 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1724 return VN_INACTIVE_CACHE;
1726 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1729 * If we get an error while cleaning up a
1730 * symlink we bail out.
1732 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1733 xfs_inactive_symlink_rmt(ip, &tp) :
1734 xfs_inactive_symlink_local(ip, &tp);
1738 return VN_INACTIVE_CACHE;
1741 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1742 xfs_trans_ihold(tp, ip);
1744 error = xfs_trans_reserve(tp, 0,
1745 XFS_IFREE_LOG_RES(mp),
1746 0, XFS_TRANS_PERM_LOG_RES,
1747 XFS_INACTIVE_LOG_COUNT);
1749 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1750 xfs_trans_cancel(tp, 0);
1751 return VN_INACTIVE_CACHE;
1754 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1755 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1756 xfs_trans_ihold(tp, ip);
1760 * If there are attributes associated with the file
1761 * then blow them away now. The code calls a routine
1762 * that recursively deconstructs the attribute fork.
1763 * We need to just commit the current transaction
1764 * because we can't use it for xfs_attr_inactive().
1766 if (ip->i_d.di_anextents > 0) {
1767 error = xfs_inactive_attrs(ip, &tp);
1769 * If we got an error, the transaction is already
1770 * cancelled, and the inode is unlocked. Just get out.
1773 return VN_INACTIVE_CACHE;
1774 } else if (ip->i_afp) {
1775 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1781 XFS_BMAP_INIT(&free_list, &first_block);
1782 error = xfs_ifree(tp, ip, &free_list);
1785 * If we fail to free the inode, shut down. The cancel
1786 * might do that, we need to make sure. Otherwise the
1787 * inode might be lost for a long time or forever.
1789 if (!XFS_FORCED_SHUTDOWN(mp)) {
1791 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1792 error, mp->m_fsname);
1793 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1795 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1798 * Credit the quota account(s). The inode is gone.
1800 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1803 * Just ignore errors at this point. There is
1804 * nothing we can do except to try to keep going.
1806 (void) xfs_bmap_finish(&tp, &free_list, &committed);
1807 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1810 * Release the dquots held by inode, if any.
1812 XFS_QM_DQDETACH(mp, ip);
1814 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1817 return VN_INACTIVE_CACHE;
1826 bhv_desc_t *dir_bdp,
1827 bhv_vname_t *dentry,
1833 xfs_inode_t *dp, *ip;
1837 bhv_vnode_t *dir_vp;
1839 dir_vp = BHV_TO_VNODE(dir_bdp);
1840 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1842 dp = XFS_BHVTOI(dir_bdp);
1844 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1845 return XFS_ERROR(EIO);
1847 lock_mode = xfs_ilock_map_shared(dp);
1848 error = xfs_dir_lookup_int(dir_bdp, lock_mode, dentry, &e_inum, &ip);
1850 *vpp = XFS_ITOV(ip);
1853 xfs_iunlock_map_shared(dp, lock_mode);
1859 * xfs_create (create a new file).
1863 bhv_desc_t *dir_bdp,
1864 bhv_vname_t *dentry,
1869 char *name = VNAME(dentry);
1870 bhv_vnode_t *dir_vp;
1871 xfs_inode_t *dp, *ip;
1872 bhv_vnode_t *vp = NULL;
1877 xfs_bmap_free_t free_list;
1878 xfs_fsblock_t first_block;
1879 boolean_t dp_joined_to_trans;
1880 int dm_event_sent = 0;
1884 struct xfs_dquot *udqp, *gdqp;
1890 dir_vp = BHV_TO_VNODE(dir_bdp);
1891 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1893 dp = XFS_BHVTOI(dir_bdp);
1896 dm_di_mode = vap->va_mode;
1897 namelen = VNAMELEN(dentry);
1899 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
1900 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1901 dir_vp, DM_RIGHT_NULL, NULL,
1902 DM_RIGHT_NULL, name, NULL,
1910 if (XFS_FORCED_SHUTDOWN(mp))
1911 return XFS_ERROR(EIO);
1913 /* Return through std_return after this point. */
1916 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1917 prid = dp->i_d.di_projid;
1918 else if (vap->va_mask & XFS_AT_PROJID)
1919 prid = (xfs_prid_t)vap->va_projid;
1921 prid = (xfs_prid_t)dfltprid;
1924 * Make sure that we have allocated dquot(s) on disk.
1926 error = XFS_QM_DQVOPALLOC(mp, dp,
1927 current_fsuid(credp), current_fsgid(credp), prid,
1928 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1933 dp_joined_to_trans = B_FALSE;
1935 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1936 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1937 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1939 * Initially assume that the file does not exist and
1940 * reserve the resources for that case. If that is not
1941 * the case we'll drop the one we have and get a more
1942 * appropriate transaction later.
1944 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1945 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1946 if (error == ENOSPC) {
1948 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1949 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1957 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1959 XFS_BMAP_INIT(&free_list, &first_block);
1964 * Reserve disk quota and the inode.
1966 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1970 if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
1972 rdev = (vap->va_mask & XFS_AT_RDEV) ? vap->va_rdev : 0;
1973 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 1,
1974 rdev, credp, prid, resblks > 0,
1977 if (error == ENOSPC)
1984 * At this point, we've gotten a newly allocated inode.
1985 * It is locked (and joined to the transaction).
1988 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1991 * Now we join the directory inode to the transaction.
1992 * We do not do it earlier because xfs_dir_ialloc
1993 * might commit the previous transaction (and release
1998 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1999 dp_joined_to_trans = B_TRUE;
2001 error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
2002 &first_block, &free_list, resblks ?
2003 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2005 ASSERT(error != ENOSPC);
2008 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2009 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2012 * If this is a synchronous mount, make sure that the
2013 * create transaction goes to disk before returning to
2016 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2017 xfs_trans_set_sync(tp);
2023 * Attach the dquot(s) to the inodes and modify them incore.
2024 * These ids of the inode couldn't have changed since the new
2025 * inode has been locked ever since it was created.
2027 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2030 * xfs_trans_commit normally decrements the vnode ref count
2031 * when it unlocks the inode. Since we want to return the
2032 * vnode to the caller, we bump the vnode ref count now.
2037 error = xfs_bmap_finish(&tp, &free_list, &committed);
2039 xfs_bmap_cancel(&free_list);
2043 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2050 XFS_QM_DQRELE(mp, udqp);
2051 XFS_QM_DQRELE(mp, gdqp);
2054 * Propagate the fact that the vnode changed after the
2055 * xfs_inode locks have been released.
2057 bhv_vop_vnode_change(vp, VCHANGE_FLAGS_TRUNCATED, 3);
2061 /* Fallthrough to std_return with error = 0 */
2064 if ( (*vpp || (error != 0 && dm_event_sent != 0)) &&
2065 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2066 DM_EVENT_POSTCREATE)) {
2067 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2068 dir_vp, DM_RIGHT_NULL,
2070 DM_RIGHT_NULL, name, NULL,
2071 dm_di_mode, error, 0);
2076 cancel_flags |= XFS_TRANS_ABORT;
2081 xfs_trans_cancel(tp, cancel_flags);
2083 if (!dp_joined_to_trans && (dp != NULL))
2084 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2085 XFS_QM_DQRELE(mp, udqp);
2086 XFS_QM_DQRELE(mp, gdqp);
2092 * Wait until after the current transaction is aborted to
2093 * release the inode. This prevents recursive transactions
2094 * and deadlocks from xfs_inactive.
2096 cancel_flags |= XFS_TRANS_ABORT;
2097 xfs_trans_cancel(tp, cancel_flags);
2100 XFS_QM_DQRELE(mp, udqp);
2101 XFS_QM_DQRELE(mp, gdqp);
2108 * Some counters to see if (and how often) we are hitting some deadlock
2109 * prevention code paths.
2113 int xfs_rm_lock_delays;
2114 int xfs_rm_attempts;
2118 * The following routine will lock the inodes associated with the
2119 * directory and the named entry in the directory. The locks are
2120 * acquired in increasing inode number.
2122 * If the entry is "..", then only the directory is locked. The
2123 * vnode ref count will still include that from the .. entry in
2126 * There is a deadlock we need to worry about. If the locked directory is
2127 * in the AIL, it might be blocking up the log. The next inode we lock
2128 * could be already locked by another thread waiting for log space (e.g
2129 * a permanent log reservation with a long running transaction (see
2130 * xfs_itruncate_finish)). To solve this, we must check if the directory
2131 * is in the ail and use lock_nowait. If we can't lock, we need to
2132 * drop the inode lock on the directory and try again. xfs_iunlock will
2133 * potentially push the tail if we were holding up the log.
2136 xfs_lock_dir_and_entry(
2138 xfs_inode_t *ip) /* inode of entry 'name' */
2142 xfs_inode_t *ips[2];
2151 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2158 * We want to lock in increasing inum. Since we've already
2159 * acquired the lock on the directory, we may need to release
2160 * if if the inum of the entry turns out to be less.
2162 if (e_inum > dp->i_ino) {
2164 * We are already in the right order, so just
2165 * lock on the inode of the entry.
2166 * We need to use nowait if dp is in the AIL.
2169 lp = (xfs_log_item_t *)dp->i_itemp;
2170 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2171 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2178 * Unlock dp and try again.
2179 * xfs_iunlock will try to push the tail
2180 * if the inode is in the AIL.
2183 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2185 if ((attempts % 5) == 0) {
2186 delay(1); /* Don't just spin the CPU */
2188 xfs_rm_lock_delays++;
2194 xfs_ilock(ip, XFS_ILOCK_EXCL);
2196 } else if (e_inum < dp->i_ino) {
2197 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2201 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2203 /* else e_inum == dp->i_ino */
2204 /* This can happen if we're asked to lock /x/..
2205 * the entry is "..", which is also the parent directory.
2213 int xfs_small_retries;
2214 int xfs_middle_retries;
2215 int xfs_lots_retries;
2216 int xfs_lock_delays;
2220 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2224 xfs_lock_inumorder(int lock_mode, int subclass)
2226 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
2227 lock_mode |= (subclass + XFS_IOLOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
2228 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
2229 lock_mode |= (subclass + XFS_ILOCK_INUMORDER) << XFS_ILOCK_SHIFT;
2235 * The following routine will lock n inodes in exclusive mode.
2236 * We assume the caller calls us with the inodes in i_ino order.
2238 * We need to detect deadlock where an inode that we lock
2239 * is in the AIL and we start waiting for another inode that is locked
2240 * by a thread in a long running transaction (such as truncate). This can
2241 * result in deadlock since the long running trans might need to wait
2242 * for the inode we just locked in order to push the tail and free space
2252 int attempts = 0, i, j, try_lock;
2255 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2266 for (; i < inodes; i++) {
2269 if (i && (ips[i] == ips[i-1])) /* Already locked */
2273 * If try_lock is not set yet, make sure all locked inodes
2274 * are not in the AIL.
2275 * If any are, set try_lock to be used later.
2279 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2280 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2281 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2288 * If any of the previous locks we have locked is in the AIL,
2289 * we must TRY to get the second and subsequent locks. If
2290 * we can't get any, we must release all we have
2295 /* try_lock must be 0 if i is 0. */
2297 * try_lock means we have an inode locked
2298 * that is in the AIL.
2301 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
2305 * Unlock all previous guys and try again.
2306 * xfs_iunlock will try to push the tail
2307 * if the inode is in the AIL.
2310 for(j = i - 1; j >= 0; j--) {
2313 * Check to see if we've already
2314 * unlocked this one.
2315 * Not the first one going back,
2316 * and the inode ptr is the same.
2318 if ((j != (i - 1)) && ips[j] ==
2322 xfs_iunlock(ips[j], lock_mode);
2325 if ((attempts % 5) == 0) {
2326 delay(1); /* Don't just spin the CPU */
2336 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
2342 if (attempts < 5) xfs_small_retries++;
2343 else if (attempts < 100) xfs_middle_retries++;
2344 else xfs_lots_retries++;
2352 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2353 int remove_which_error_return = 0;
2355 #define REMOVE_DEBUG_TRACE(x)
2356 #endif /* ! DEBUG */
2365 bhv_desc_t *dir_bdp,
2366 bhv_vname_t *dentry,
2369 bhv_vnode_t *dir_vp;
2370 char *name = VNAME(dentry);
2371 xfs_inode_t *dp, *ip;
2372 xfs_trans_t *tp = NULL;
2375 xfs_bmap_free_t free_list;
2376 xfs_fsblock_t first_block;
2384 dir_vp = BHV_TO_VNODE(dir_bdp);
2385 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2387 dp = XFS_BHVTOI(dir_bdp);
2390 if (XFS_FORCED_SHUTDOWN(mp))
2391 return XFS_ERROR(EIO);
2393 namelen = VNAMELEN(dentry);
2395 if (!xfs_get_dir_entry(dentry, &ip)) {
2396 dm_di_mode = ip->i_d.di_mode;
2400 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
2401 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2402 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2403 name, NULL, dm_di_mode, 0, 0);
2408 /* From this point on, return through std_return */
2412 * We need to get a reference to ip before we get our log
2413 * reservation. The reason for this is that we cannot call
2414 * xfs_iget for an inode for which we do not have a reference
2415 * once we've acquired a log reservation. This is because the
2416 * inode we are trying to get might be in xfs_inactive going
2417 * for a log reservation. Since we'll have to wait for the
2418 * inactive code to complete before returning from xfs_iget,
2419 * we need to make sure that we don't have log space reserved
2420 * when we call xfs_iget. Instead we get an unlocked reference
2421 * to the inode before getting our log reservation.
2423 error = xfs_get_dir_entry(dentry, &ip);
2425 REMOVE_DEBUG_TRACE(__LINE__);
2429 dm_di_mode = ip->i_d.di_mode;
2431 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2435 error = XFS_QM_DQATTACH(mp, dp, 0);
2436 if (!error && dp != ip)
2437 error = XFS_QM_DQATTACH(mp, ip, 0);
2439 REMOVE_DEBUG_TRACE(__LINE__);
2444 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2445 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2447 * We try to get the real space reservation first,
2448 * allowing for directory btree deletion(s) implying
2449 * possible bmap insert(s). If we can't get the space
2450 * reservation then we use 0 instead, and avoid the bmap
2451 * btree insert(s) in the directory code by, if the bmap
2452 * insert tries to happen, instead trimming the LAST
2453 * block from the directory.
2455 resblks = XFS_REMOVE_SPACE_RES(mp);
2456 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2457 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2458 if (error == ENOSPC) {
2460 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2461 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2464 ASSERT(error != ENOSPC);
2465 REMOVE_DEBUG_TRACE(__LINE__);
2466 xfs_trans_cancel(tp, 0);
2471 error = xfs_lock_dir_and_entry(dp, ip);
2473 REMOVE_DEBUG_TRACE(__LINE__);
2474 xfs_trans_cancel(tp, cancel_flags);
2480 * At this point, we've gotten both the directory and the entry
2483 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2486 * Increment vnode ref count only in this case since
2487 * there's an extra vnode reference in the case where
2491 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2495 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2497 XFS_BMAP_INIT(&free_list, &first_block);
2498 error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2499 &first_block, &free_list, 0);
2501 ASSERT(error != ENOENT);
2502 REMOVE_DEBUG_TRACE(__LINE__);
2505 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2508 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2510 error = xfs_droplink(tp, ip);
2512 REMOVE_DEBUG_TRACE(__LINE__);
2516 /* Determine if this is the last link while
2517 * we are in the transaction.
2519 link_zero = (ip)->i_d.di_nlink==0;
2522 * Take an extra ref on the inode so that it doesn't
2523 * go to xfs_inactive() from within the commit.
2528 * If this is a synchronous mount, make sure that the
2529 * remove transaction goes to disk before returning to
2532 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2533 xfs_trans_set_sync(tp);
2536 error = xfs_bmap_finish(&tp, &free_list, &committed);
2538 REMOVE_DEBUG_TRACE(__LINE__);
2542 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2549 * Before we drop our extra reference to the inode, purge it
2550 * from the refcache if it is there. By waiting until afterwards
2551 * to do the IRELE, we ensure that we won't go inactive in the
2552 * xfs_refcache_purge_ip routine (although that would be OK).
2554 xfs_refcache_purge_ip(ip);
2556 vn_trace_exit(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2559 * Let interposed file systems know about removed links.
2561 bhv_vop_link_removed(XFS_ITOV(ip), dir_vp, link_zero);
2565 /* Fall through to std_return with error = 0 */
2567 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp,
2568 DM_EVENT_POSTREMOVE)) {
2569 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2570 dir_vp, DM_RIGHT_NULL,
2571 NULL, DM_RIGHT_NULL,
2572 name, NULL, dm_di_mode, error, 0);
2577 xfs_bmap_cancel(&free_list);
2578 cancel_flags |= XFS_TRANS_ABORT;
2579 xfs_trans_cancel(tp, cancel_flags);
2584 * In this case make sure to not release the inode until after
2585 * the current transaction is aborted. Releasing it beforehand
2586 * can cause us to go to xfs_inactive and start a recursive
2587 * transaction which can easily deadlock with the current one.
2589 xfs_bmap_cancel(&free_list);
2590 cancel_flags |= XFS_TRANS_ABORT;
2591 xfs_trans_cancel(tp, cancel_flags);
2594 * Before we drop our extra reference to the inode, purge it
2595 * from the refcache if it is there. By waiting until afterwards
2596 * to do the IRELE, we ensure that we won't go inactive in the
2597 * xfs_refcache_purge_ip routine (although that would be OK).
2599 xfs_refcache_purge_ip(ip);
2613 bhv_desc_t *target_dir_bdp,
2614 bhv_vnode_t *src_vp,
2615 bhv_vname_t *dentry,
2618 xfs_inode_t *tdp, *sip;
2621 xfs_inode_t *ips[2];
2623 xfs_bmap_free_t free_list;
2624 xfs_fsblock_t first_block;
2627 bhv_vnode_t *target_dir_vp;
2629 char *target_name = VNAME(dentry);
2632 target_dir_vp = BHV_TO_VNODE(target_dir_bdp);
2633 vn_trace_entry(target_dir_vp, __FUNCTION__, (inst_t *)__return_address);
2634 vn_trace_entry(src_vp, __FUNCTION__, (inst_t *)__return_address);
2636 target_namelen = VNAMELEN(dentry);
2637 ASSERT(!VN_ISDIR(src_vp));
2639 sip = xfs_vtoi(src_vp);
2640 tdp = XFS_BHVTOI(target_dir_bdp);
2642 if (XFS_FORCED_SHUTDOWN(mp))
2643 return XFS_ERROR(EIO);
2645 if (DM_EVENT_ENABLED(src_vp->v_vfsp, tdp, DM_EVENT_LINK)) {
2646 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2647 target_dir_vp, DM_RIGHT_NULL,
2648 src_vp, DM_RIGHT_NULL,
2649 target_name, NULL, 0, 0, 0);
2654 /* Return through std_return after this point. */
2656 error = XFS_QM_DQATTACH(mp, sip, 0);
2657 if (!error && sip != tdp)
2658 error = XFS_QM_DQATTACH(mp, tdp, 0);
2662 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2663 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2664 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2665 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2666 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2667 if (error == ENOSPC) {
2669 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2670 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2677 if (sip->i_ino < tdp->i_ino) {
2685 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2688 * Increment vnode ref counts since xfs_trans_commit &
2689 * xfs_trans_cancel will both unlock the inodes and
2690 * decrement the associated ref counts.
2693 VN_HOLD(target_dir_vp);
2694 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2695 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2698 * If the source has too many links, we can't make any more to it.
2700 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2701 error = XFS_ERROR(EMLINK);
2706 * If we are using project inheritance, we only allow hard link
2707 * creation in our tree when the project IDs are the same; else
2708 * the tree quota mechanism could be circumvented.
2710 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2711 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2712 error = XFS_ERROR(EXDEV);
2717 (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
2720 XFS_BMAP_INIT(&free_list, &first_block);
2722 error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
2723 sip->i_ino, &first_block, &free_list,
2727 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2729 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2731 error = xfs_bumplink(tp, sip);
2736 * If this is a synchronous mount, make sure that the
2737 * link transaction goes to disk before returning to
2740 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2741 xfs_trans_set_sync(tp);
2744 error = xfs_bmap_finish (&tp, &free_list, &committed);
2746 xfs_bmap_cancel(&free_list);
2750 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2754 /* Fall through to std_return with error = 0. */
2756 if (DM_EVENT_ENABLED(src_vp->v_vfsp, sip,
2757 DM_EVENT_POSTLINK)) {
2758 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2759 target_dir_vp, DM_RIGHT_NULL,
2760 src_vp, DM_RIGHT_NULL,
2761 target_name, NULL, 0, error, 0);
2766 cancel_flags |= XFS_TRANS_ABORT;
2770 xfs_trans_cancel(tp, cancel_flags);
2781 bhv_desc_t *dir_bdp,
2782 bhv_vname_t *dentry,
2787 char *dir_name = VNAME(dentry);
2789 xfs_inode_t *cdp; /* inode of created dir */
2790 bhv_vnode_t *cvp; /* vnode of created dir */
2796 xfs_bmap_free_t free_list;
2797 xfs_fsblock_t first_block;
2798 bhv_vnode_t *dir_vp;
2799 boolean_t dp_joined_to_trans;
2800 boolean_t created = B_FALSE;
2801 int dm_event_sent = 0;
2803 struct xfs_dquot *udqp, *gdqp;
2808 dir_vp = BHV_TO_VNODE(dir_bdp);
2809 dp = XFS_BHVTOI(dir_bdp);
2812 if (XFS_FORCED_SHUTDOWN(mp))
2813 return XFS_ERROR(EIO);
2815 dir_namelen = VNAMELEN(dentry);
2818 dp_joined_to_trans = B_FALSE;
2819 dm_di_mode = vap->va_mode;
2821 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
2822 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2823 dir_vp, DM_RIGHT_NULL, NULL,
2824 DM_RIGHT_NULL, dir_name, NULL,
2831 /* Return through std_return after this point. */
2833 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2837 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2838 prid = dp->i_d.di_projid;
2839 else if (vap->va_mask & XFS_AT_PROJID)
2840 prid = (xfs_prid_t)vap->va_projid;
2842 prid = (xfs_prid_t)dfltprid;
2845 * Make sure that we have allocated dquot(s) on disk.
2847 error = XFS_QM_DQVOPALLOC(mp, dp,
2848 current_fsuid(credp), current_fsgid(credp), prid,
2849 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2853 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2854 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2855 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2856 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2857 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2858 if (error == ENOSPC) {
2860 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2861 XFS_TRANS_PERM_LOG_RES,
2862 XFS_MKDIR_LOG_COUNT);
2870 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2873 * Check for directory link count overflow.
2875 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2876 error = XFS_ERROR(EMLINK);
2881 * Reserve disk quota and the inode.
2883 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2888 (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
2891 * create the directory inode.
2893 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 2,
2894 0, credp, prid, resblks > 0,
2897 if (error == ENOSPC)
2904 * Now we add the directory inode to the transaction.
2905 * We waited until now since xfs_dir_ialloc might start
2906 * a new transaction. Had we joined the transaction
2907 * earlier, the locks might have gotten released.
2910 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2911 dp_joined_to_trans = B_TRUE;
2913 XFS_BMAP_INIT(&free_list, &first_block);
2915 error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2916 &first_block, &free_list, resblks ?
2917 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2919 ASSERT(error != ENOSPC);
2922 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2925 * Bump the in memory version number of the parent directory
2926 * so that other processes accessing it will recognize that
2927 * the directory has changed.
2931 error = xfs_dir_init(tp, cdp, dp);
2936 error = xfs_bumplink(tp, dp);
2940 cvp = XFS_ITOV(cdp);
2948 * Attach the dquots to the new inode and modify the icount incore.
2950 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2953 * If this is a synchronous mount, make sure that the
2954 * mkdir transaction goes to disk before returning to
2957 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2958 xfs_trans_set_sync(tp);
2961 error = xfs_bmap_finish(&tp, &free_list, &committed);
2967 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2968 XFS_QM_DQRELE(mp, udqp);
2969 XFS_QM_DQRELE(mp, gdqp);
2974 /* Fall through to std_return with error = 0 or errno from
2975 * xfs_trans_commit. */
2978 if ( (created || (error != 0 && dm_event_sent != 0)) &&
2979 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2980 DM_EVENT_POSTCREATE)) {
2981 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2982 dir_vp, DM_RIGHT_NULL,
2983 created ? XFS_ITOV(cdp):NULL,
2986 dm_di_mode, error, 0);
2992 xfs_bmap_cancel(&free_list);
2994 cancel_flags |= XFS_TRANS_ABORT;
2996 xfs_trans_cancel(tp, cancel_flags);
2997 XFS_QM_DQRELE(mp, udqp);
2998 XFS_QM_DQRELE(mp, gdqp);
3000 if (!dp_joined_to_trans && (dp != NULL)) {
3001 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3014 bhv_desc_t *dir_bdp,
3015 bhv_vname_t *dentry,
3018 char *name = VNAME(dentry);
3020 xfs_inode_t *cdp; /* child directory */
3024 xfs_bmap_free_t free_list;
3025 xfs_fsblock_t first_block;
3028 bhv_vnode_t *dir_vp;
3029 int dm_di_mode = S_IFDIR;
3034 dir_vp = BHV_TO_VNODE(dir_bdp);
3035 dp = XFS_BHVTOI(dir_bdp);
3038 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3040 if (XFS_FORCED_SHUTDOWN(XFS_BHVTOI(dir_bdp)->i_mount))
3041 return XFS_ERROR(EIO);
3042 namelen = VNAMELEN(dentry);
3044 if (!xfs_get_dir_entry(dentry, &cdp)) {
3045 dm_di_mode = cdp->i_d.di_mode;
3049 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
3050 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
3051 dir_vp, DM_RIGHT_NULL,
3052 NULL, DM_RIGHT_NULL,
3053 name, NULL, dm_di_mode, 0, 0);
3055 return XFS_ERROR(error);
3058 /* Return through std_return after this point. */
3063 * We need to get a reference to cdp before we get our log
3064 * reservation. The reason for this is that we cannot call
3065 * xfs_iget for an inode for which we do not have a reference
3066 * once we've acquired a log reservation. This is because the
3067 * inode we are trying to get might be in xfs_inactive going
3068 * for a log reservation. Since we'll have to wait for the
3069 * inactive code to complete before returning from xfs_iget,
3070 * we need to make sure that we don't have log space reserved
3071 * when we call xfs_iget. Instead we get an unlocked reference
3072 * to the inode before getting our log reservation.
3074 error = xfs_get_dir_entry(dentry, &cdp);
3076 REMOVE_DEBUG_TRACE(__LINE__);
3080 dm_di_mode = cdp->i_d.di_mode;
3083 * Get the dquots for the inodes.
3085 error = XFS_QM_DQATTACH(mp, dp, 0);
3086 if (!error && dp != cdp)
3087 error = XFS_QM_DQATTACH(mp, cdp, 0);
3090 REMOVE_DEBUG_TRACE(__LINE__);
3094 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
3095 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3097 * We try to get the real space reservation first,
3098 * allowing for directory btree deletion(s) implying
3099 * possible bmap insert(s). If we can't get the space
3100 * reservation then we use 0 instead, and avoid the bmap
3101 * btree insert(s) in the directory code by, if the bmap
3102 * insert tries to happen, instead trimming the LAST
3103 * block from the directory.
3105 resblks = XFS_REMOVE_SPACE_RES(mp);
3106 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
3107 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3108 if (error == ENOSPC) {
3110 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
3111 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3114 ASSERT(error != ENOSPC);
3119 XFS_BMAP_INIT(&free_list, &first_block);
3122 * Now lock the child directory inode and the parent directory
3123 * inode in the proper order. This will take care of validating
3124 * that the directory entry for the child directory inode has
3125 * not changed while we were obtaining a log reservation.
3127 error = xfs_lock_dir_and_entry(dp, cdp);
3129 xfs_trans_cancel(tp, cancel_flags);
3134 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3137 * Only increment the parent directory vnode count if
3138 * we didn't bump it in looking up cdp. The only time
3139 * we don't bump it is when we're looking up ".".
3145 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3147 ASSERT(cdp->i_d.di_nlink >= 2);
3148 if (cdp->i_d.di_nlink != 2) {
3149 error = XFS_ERROR(ENOTEMPTY);
3152 if (!xfs_dir_isempty(cdp)) {
3153 error = XFS_ERROR(ENOTEMPTY);
3157 error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
3158 &first_block, &free_list, resblks);
3162 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3165 * Bump the in memory generation count on the parent
3166 * directory so that other can know that it has changed.
3171 * Drop the link from cdp's "..".
3173 error = xfs_droplink(tp, dp);
3179 * Drop the link from dp to cdp.
3181 error = xfs_droplink(tp, cdp);
3187 * Drop the "." link from cdp to self.
3189 error = xfs_droplink(tp, cdp);
3194 /* Determine these before committing transaction */
3195 last_cdp_link = (cdp)->i_d.di_nlink==0;
3198 * Take an extra ref on the child vnode so that it
3199 * does not go to xfs_inactive() from within the commit.
3204 * If this is a synchronous mount, make sure that the
3205 * rmdir transaction goes to disk before returning to
3208 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3209 xfs_trans_set_sync(tp);
3212 error = xfs_bmap_finish (&tp, &free_list, &committed);
3214 xfs_bmap_cancel(&free_list);
3215 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3221 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3229 * Let interposed file systems know about removed links.
3231 bhv_vop_link_removed(XFS_ITOV(cdp), dir_vp, last_cdp_link);
3235 /* Fall through to std_return with error = 0 or the errno
3236 * from xfs_trans_commit. */
3238 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_POSTREMOVE)) {
3239 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3240 dir_vp, DM_RIGHT_NULL,
3241 NULL, DM_RIGHT_NULL,
3242 name, NULL, dm_di_mode,
3248 xfs_bmap_cancel(&free_list);
3249 cancel_flags |= XFS_TRANS_ABORT;
3253 xfs_trans_cancel(tp, cancel_flags);
3259 * Read dp's entries starting at uiop->uio_offset and translate them into
3260 * bufsize bytes worth of struct dirents starting at bufbase.
3264 bhv_desc_t *dir_bdp,
3270 xfs_trans_t *tp = NULL;
3274 vn_trace_entry(BHV_TO_VNODE(dir_bdp), __FUNCTION__,
3275 (inst_t *)__return_address);
3276 dp = XFS_BHVTOI(dir_bdp);
3278 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
3279 return XFS_ERROR(EIO);
3281 lock_mode = xfs_ilock_map_shared(dp);
3282 error = xfs_dir_getdents(tp, dp, uiop, eofp);
3283 xfs_iunlock_map_shared(dp, lock_mode);
3290 bhv_desc_t *dir_bdp,
3291 bhv_vname_t *dentry,
3303 xfs_bmap_free_t free_list;
3304 xfs_fsblock_t first_block;
3305 boolean_t dp_joined_to_trans;
3306 bhv_vnode_t *dir_vp;
3309 xfs_fileoff_t first_fsb;
3310 xfs_filblks_t fs_blocks;
3312 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3319 struct xfs_dquot *udqp, *gdqp;
3321 char *link_name = VNAME(dentry);
3325 dir_vp = BHV_TO_VNODE(dir_bdp);
3326 dp = XFS_BHVTOI(dir_bdp);
3327 dp_joined_to_trans = B_FALSE;
3332 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3336 if (XFS_FORCED_SHUTDOWN(mp))
3337 return XFS_ERROR(EIO);
3339 link_namelen = VNAMELEN(dentry);
3342 * Check component lengths of the target path name.
3344 pathlen = strlen(target_path);
3345 if (pathlen >= MAXPATHLEN) /* total string too long */
3346 return XFS_ERROR(ENAMETOOLONG);
3347 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3351 for (total = 0, path = target_path; total < pathlen;) {
3355 while(*path == '/') {
3361 * Count up to the next slash or end of path.
3362 * Error out if the component is bigger than MAXNAMELEN.
3364 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3365 if (++len >= MAXNAMELEN) {
3366 error = ENAMETOOLONG;
3373 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_SYMLINK)) {
3374 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3375 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3376 link_name, target_path, 0, 0, 0);
3381 /* Return through std_return after this point. */
3384 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3385 prid = dp->i_d.di_projid;
3386 else if (vap->va_mask & XFS_AT_PROJID)
3387 prid = (xfs_prid_t)vap->va_projid;
3389 prid = (xfs_prid_t)dfltprid;
3392 * Make sure that we have allocated dquot(s) on disk.
3394 error = XFS_QM_DQVOPALLOC(mp, dp,
3395 current_fsuid(credp), current_fsgid(credp), prid,
3396 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3400 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3401 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3403 * The symlink will fit into the inode data fork?
3404 * There can't be any attributes so we get the whole variable part.
3406 if (pathlen <= XFS_LITINO(mp))
3409 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3410 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3411 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3412 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3413 if (error == ENOSPC && fs_blocks == 0) {
3415 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3416 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3424 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
3427 * Check whether the directory allows new symlinks or not.
3429 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3430 error = XFS_ERROR(EPERM);
3435 * Reserve disk quota : blocks and inode.
3437 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3442 * Check for ability to enter directory entry, if no space reserved.
3445 (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
3448 * Initialize the bmap freelist prior to calling either
3449 * bmapi or the directory create code.
3451 XFS_BMAP_INIT(&free_list, &first_block);
3454 * Allocate an inode for the symlink.
3456 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (vap->va_mode&~S_IFMT),
3457 1, 0, credp, prid, resblks > 0, &ip, NULL);
3459 if (error == ENOSPC)
3466 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3467 dp_joined_to_trans = B_TRUE;
3470 * Also attach the dquot(s) to it, if applicable.
3472 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3475 resblks -= XFS_IALLOC_SPACE_RES(mp);
3477 * If the symlink will fit into the inode, write it inline.
3479 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3480 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3481 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3482 ip->i_d.di_size = pathlen;
3485 * The inode was initially created in extent format.
3487 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3488 ip->i_df.if_flags |= XFS_IFINLINE;
3490 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3491 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3495 nmaps = SYMLINK_MAPS;
3497 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3498 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3499 &first_block, resblks, mval, &nmaps,
3506 resblks -= fs_blocks;
3507 ip->i_d.di_size = pathlen;
3508 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3510 cur_chunk = target_path;
3511 for (n = 0; n < nmaps; n++) {
3512 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3513 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3514 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3515 BTOBB(byte_cnt), 0);
3516 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3517 if (pathlen < byte_cnt) {
3520 pathlen -= byte_cnt;
3522 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3523 cur_chunk += byte_cnt;
3525 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3530 * Create the directory entry for the symlink.
3532 error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
3533 &first_block, &free_list, resblks);
3536 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3537 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3540 * Bump the in memory version number of the parent directory
3541 * so that other processes accessing it will recognize that
3542 * the directory has changed.
3547 * If this is a synchronous mount, make sure that the
3548 * symlink transaction goes to disk before returning to
3551 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3552 xfs_trans_set_sync(tp);
3556 * xfs_trans_commit normally decrements the vnode ref count
3557 * when it unlocks the inode. Since we want to return the
3558 * vnode to the caller, we bump the vnode ref count now.
3562 error = xfs_bmap_finish(&tp, &free_list, &committed);
3566 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3567 XFS_QM_DQRELE(mp, udqp);
3568 XFS_QM_DQRELE(mp, gdqp);
3570 /* Fall through to std_return with error = 0 or errno from
3571 * xfs_trans_commit */
3573 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
3574 DM_EVENT_POSTSYMLINK)) {
3575 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3576 dir_vp, DM_RIGHT_NULL,
3577 error ? NULL : XFS_ITOV(ip),
3578 DM_RIGHT_NULL, link_name, target_path,
3594 xfs_bmap_cancel(&free_list);
3595 cancel_flags |= XFS_TRANS_ABORT;
3597 xfs_trans_cancel(tp, cancel_flags);
3598 XFS_QM_DQRELE(mp, udqp);
3599 XFS_QM_DQRELE(mp, gdqp);
3601 if (!dp_joined_to_trans && (dp != NULL)) {
3602 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3612 * A fid routine that takes a pointer to a previously allocated
3613 * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
3623 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
3624 (inst_t *)__return_address);
3625 ASSERT(sizeof(fid_t) >= sizeof(xfs_fid2_t));
3627 xfid = (xfs_fid2_t *)fidp;
3628 ip = XFS_BHVTOI(bdp);
3629 xfid->fid_len = sizeof(xfs_fid2_t) - sizeof(xfid->fid_len);
3632 * use memcpy because the inode is a long long and there's no
3633 * assurance that xfid->fid_ino is properly aligned.
3635 memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
3636 xfid->fid_gen = ip->i_d.di_gen;
3648 bhv_vrwlock_t locktype)
3653 vp = BHV_TO_VNODE(bdp);
3656 ip = XFS_BHVTOI(bdp);
3657 if (locktype == VRWLOCK_WRITE) {
3658 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3659 } else if (locktype == VRWLOCK_TRY_READ) {
3660 return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
3661 } else if (locktype == VRWLOCK_TRY_WRITE) {
3662 return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
3664 ASSERT((locktype == VRWLOCK_READ) ||
3665 (locktype == VRWLOCK_WRITE_DIRECT));
3666 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3679 bhv_vrwlock_t locktype)
3684 vp = BHV_TO_VNODE(bdp);
3687 ip = XFS_BHVTOI(bdp);
3688 if (locktype == VRWLOCK_WRITE) {
3690 * In the write case, we may have added a new entry to
3691 * the reference cache. This might store a pointer to
3692 * an inode to be released in this inode. If it is there,
3693 * clear the pointer and release the inode after unlocking
3696 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3698 ASSERT((locktype == VRWLOCK_READ) ||
3699 (locktype == VRWLOCK_WRITE_DIRECT));
3700 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3712 xfs_inode_log_item_t *iip;
3715 ip = XFS_BHVTOI(bdp);
3719 if (XFS_FORCED_SHUTDOWN(mp))
3720 return XFS_ERROR(EIO);
3723 * Bypass inodes which have already been cleaned by
3724 * the inode flush clustering code inside xfs_iflush
3726 if ((ip->i_update_core == 0) &&
3727 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3730 if (flags & FLUSH_LOG) {
3731 if (iip && iip->ili_last_lsn) {
3732 xlog_t *log = mp->m_log;
3734 int s, log_flags = XFS_LOG_FORCE;
3736 s = GRANT_LOCK(log);
3737 sync_lsn = log->l_last_sync_lsn;
3738 GRANT_UNLOCK(log, s);
3740 if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) <= 0))
3743 if (flags & FLUSH_SYNC)
3744 log_flags |= XFS_LOG_SYNC;
3745 return xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3750 * We make this non-blocking if the inode is contended,
3751 * return EAGAIN to indicate to the caller that they
3752 * did not succeed. This prevents the flush path from
3753 * blocking on inodes inside another operation right
3754 * now, they get caught later by xfs_sync.
3756 if (flags & FLUSH_INODE) {
3759 if (xfs_ipincount(ip))
3762 if (flags & FLUSH_SYNC) {
3763 xfs_ilock(ip, XFS_ILOCK_SHARED);
3765 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3766 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3767 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3774 if (flags & FLUSH_SYNC)
3775 flush_flags = XFS_IFLUSH_SYNC;
3777 flush_flags = XFS_IFLUSH_ASYNC;
3779 error = xfs_iflush(ip, flush_flags);
3780 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3798 if (!capable(CAP_SYS_ADMIN))
3799 return XFS_ERROR(EPERM);
3801 ip = XFS_BHVTOI(bdp);
3804 if (XFS_FORCED_SHUTDOWN(mp))
3805 return XFS_ERROR(EIO);
3807 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3808 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3810 xfs_trans_cancel(tp, 0);
3813 xfs_ilock(ip, XFS_ILOCK_EXCL);
3814 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3816 ip->i_iocore.io_dmevmask = ip->i_d.di_dmevmask = evmask;
3817 ip->i_iocore.io_dmstate = ip->i_d.di_dmstate = state;
3819 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3821 error = xfs_trans_commit(tp, 0);
3833 vp = BHV_TO_VNODE(bdp);
3834 ip = XFS_BHVTOI(bdp);
3836 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
3838 ASSERT(!VN_MAPPED(vp));
3840 /* bad inode, get out here ASAP */
3848 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
3851 * Make sure the atime in the XFS inode is correct before freeing the
3854 xfs_synchronize_atime(ip);
3857 * If we have nothing to flush with this inode then complete the
3858 * teardown now, otherwise break the link between the xfs inode and the
3859 * linux inode and clean up the xfs inode later. This avoids flushing
3860 * the inode to disk during the delete operation itself.
3862 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3863 * first to ensure that xfs_iunpin() will never see an xfs inode
3864 * that has a linux inode being reclaimed. Synchronisation is provided
3865 * by the i_flags_lock.
3867 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3868 xfs_ilock(ip, XFS_ILOCK_EXCL);
3870 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3872 xfs_mount_t *mp = ip->i_mount;
3874 /* Protect sync and unpin from us */
3875 XFS_MOUNT_ILOCK(mp);
3876 spin_lock(&ip->i_flags_lock);
3877 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
3878 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
3879 spin_unlock(&ip->i_flags_lock);
3880 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
3881 XFS_MOUNT_IUNLOCK(mp);
3892 xfs_ihash_t *ih = ip->i_hash;
3893 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
3896 if (vp && VN_BAD(vp))
3899 /* The hash lock here protects a thread in xfs_iget_core from
3900 * racing with us on linking the inode back with a vnode.
3901 * Once we have the XFS_IRECLAIM flag set it will not touch
3904 write_lock(&ih->ih_lock);
3905 spin_lock(&ip->i_flags_lock);
3906 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3907 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
3908 spin_unlock(&ip->i_flags_lock);
3909 write_unlock(&ih->ih_lock);
3912 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3916 __xfs_iflags_set(ip, XFS_IRECLAIM);
3917 spin_unlock(&ip->i_flags_lock);
3918 write_unlock(&ih->ih_lock);
3921 * If the inode is still dirty, then flush it out. If the inode
3922 * is not in the AIL, then it will be OK to flush it delwri as
3923 * long as xfs_iflush() does not keep any references to the inode.
3924 * We leave that decision up to xfs_iflush() since it has the
3925 * knowledge of whether it's OK to simply do a delwri flush of
3926 * the inode or whether we need to wait until the inode is
3927 * pulled from the AIL.
3928 * We get the flush lock regardless, though, just to make sure
3929 * we don't free it while it is being flushed.
3931 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3933 xfs_ilock(ip, XFS_ILOCK_EXCL);
3937 if (ip->i_update_core ||
3938 ((ip->i_itemp != NULL) &&
3939 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3940 error = xfs_iflush(ip, sync_mode);
3942 * If we hit an error, typically because of filesystem
3943 * shutdown, we don't need to let vn_reclaim to know
3944 * because we're gonna reclaim the inode anyway.
3947 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3950 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3953 ASSERT(ip->i_update_core == 0);
3954 ASSERT(ip->i_itemp == NULL ||
3955 ip->i_itemp->ili_format.ilf_fields == 0);
3956 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3957 } else if (locked) {
3959 * We are not interested in doing an iflush if we're
3960 * in the process of shutting down the filesystem forcibly.
3961 * So, just reclaim the inode.
3964 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3973 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3976 xfs_inode_t *ip, *n;
3981 XFS_MOUNT_ILOCK(mp);
3982 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3984 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3986 if (xfs_ipincount(ip) ||
3987 !xfs_iflock_nowait(ip)) {
3988 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3992 XFS_MOUNT_IUNLOCK(mp);
3993 if (xfs_finish_reclaim(ip, noblock,
3994 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
4003 XFS_MOUNT_IUNLOCK(mp);
4008 * xfs_alloc_file_space()
4009 * This routine allocates disk space for the given file.
4011 * If alloc_type == 0, this request is for an ALLOCSP type
4012 * request which will change the file size. In this case, no
4013 * DMAPI event will be generated by the call. A TRUNCATE event
4014 * will be generated later by xfs_setattr.
4016 * If alloc_type != 0, this request is for a RESVSP type
4017 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
4018 * lower block boundary byte address is less than the file's
4027 xfs_alloc_file_space(
4034 xfs_mount_t *mp = ip->i_mount;
4036 xfs_filblks_t allocated_fsb;
4037 xfs_filblks_t allocatesize_fsb;
4038 xfs_extlen_t extsz, temp;
4039 xfs_fileoff_t startoffset_fsb;
4040 xfs_fsblock_t firstfsb;
4046 xfs_bmbt_irec_t imaps[1], *imapp;
4047 xfs_bmap_free_t free_list;
4048 uint qblocks, resblks, resrtextents;
4052 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
4054 if (XFS_FORCED_SHUTDOWN(mp))
4055 return XFS_ERROR(EIO);
4057 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4061 return XFS_ERROR(EINVAL);
4063 rt = XFS_IS_REALTIME_INODE(ip);
4064 extsz = xfs_get_extsz_hint(ip);
4069 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
4070 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
4071 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
4073 /* Generate a DMAPI event if needed. */
4074 if (alloc_type != 0 && offset < ip->i_size &&
4075 (attr_flags&ATTR_DMI) == 0 &&
4076 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4077 xfs_off_t end_dmi_offset;
4079 end_dmi_offset = offset+len;
4080 if (end_dmi_offset > ip->i_size)
4081 end_dmi_offset = ip->i_size;
4082 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
4083 offset, end_dmi_offset - offset,
4090 * Allocate file space until done or until there is an error
4093 while (allocatesize_fsb && !error) {
4097 * Determine space reservations for data/realtime.
4099 if (unlikely(extsz)) {
4100 s = startoffset_fsb;
4103 e = startoffset_fsb + allocatesize_fsb;
4104 if ((temp = do_mod(startoffset_fsb, extsz)))
4106 if ((temp = do_mod(e, extsz)))
4110 e = allocatesize_fsb;
4114 resrtextents = qblocks = (uint)(e - s);
4115 resrtextents /= mp->m_sb.sb_rextsize;
4116 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4117 quota_flag = XFS_QMOPT_RES_RTBLKS;
4120 resblks = qblocks = \
4121 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
4122 quota_flag = XFS_QMOPT_RES_REGBLKS;
4126 * Allocate and setup the transaction.
4128 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4129 error = xfs_trans_reserve(tp, resblks,
4130 XFS_WRITE_LOG_RES(mp), resrtextents,
4131 XFS_TRANS_PERM_LOG_RES,
4132 XFS_WRITE_LOG_COUNT);
4134 * Check for running out of space
4138 * Free the transaction structure.
4140 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4141 xfs_trans_cancel(tp, 0);
4144 xfs_ilock(ip, XFS_ILOCK_EXCL);
4145 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
4146 qblocks, 0, quota_flag);
4150 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4151 xfs_trans_ihold(tp, ip);
4154 * Issue the xfs_bmapi() call to allocate the blocks
4156 XFS_BMAP_INIT(&free_list, &firstfsb);
4157 error = XFS_BMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4158 allocatesize_fsb, bmapi_flag,
4159 &firstfsb, 0, imapp, &nimaps,
4166 * Complete the transaction
4168 error = xfs_bmap_finish(&tp, &free_list, &committed);
4173 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
4174 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4179 allocated_fsb = imapp->br_blockcount;
4182 error = XFS_ERROR(ENOSPC);
4186 startoffset_fsb += allocated_fsb;
4187 allocatesize_fsb -= allocated_fsb;
4190 if (error == ENOSPC && (attr_flags&ATTR_DMI) == 0 &&
4191 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_NOSPACE)) {
4193 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
4194 XFS_ITOV(ip), DM_RIGHT_NULL,
4195 XFS_ITOV(ip), DM_RIGHT_NULL,
4196 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
4198 goto retry; /* Maybe DMAPI app. has made space */
4199 /* else fall through with error from XFS_SEND_DATA */
4204 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
4205 xfs_bmap_cancel(&free_list);
4206 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
4208 error1: /* Just cancel transaction */
4209 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4210 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4211 goto dmapi_enospc_check;
4215 * Zero file bytes between startoff and endoff inclusive.
4216 * The iolock is held exclusive and no blocks are buffered.
4219 xfs_zero_remaining_bytes(
4224 xfs_bmbt_irec_t imap;
4225 xfs_fileoff_t offset_fsb;
4226 xfs_off_t lastoffset;
4229 xfs_mount_t *mp = ip->i_mount;
4233 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
4234 ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
4235 mp->m_rtdev_targp : mp->m_ddev_targp);
4237 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4238 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4240 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, offset_fsb, 1, 0,
4241 NULL, 0, &imap, &nimap, NULL, NULL);
4242 if (error || nimap < 1)
4244 ASSERT(imap.br_blockcount >= 1);
4245 ASSERT(imap.br_startoff == offset_fsb);
4246 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4247 if (lastoffset > endoff)
4248 lastoffset = endoff;
4249 if (imap.br_startblock == HOLESTARTBLOCK)
4251 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4252 if (imap.br_state == XFS_EXT_UNWRITTEN)
4255 XFS_BUF_UNWRITE(bp);
4257 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4259 if ((error = xfs_iowait(bp))) {
4260 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4261 mp, bp, XFS_BUF_ADDR(bp));
4264 memset(XFS_BUF_PTR(bp) +
4265 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4266 0, lastoffset - offset + 1);
4271 if ((error = xfs_iowait(bp))) {
4272 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4273 mp, bp, XFS_BUF_ADDR(bp));
4282 * xfs_free_file_space()
4283 * This routine frees disk space for the given file.
4285 * This routine is only called by xfs_change_file_space
4286 * for an UNRESVSP type call.
4294 xfs_free_file_space(
4303 xfs_off_t end_dmi_offset;
4304 xfs_fileoff_t endoffset_fsb;
4306 xfs_fsblock_t firstfsb;
4307 xfs_bmap_free_t free_list;
4308 xfs_bmbt_irec_t imap;
4316 xfs_fileoff_t startoffset_fsb;
4318 int need_iolock = 1;
4323 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4325 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4329 if (len <= 0) /* if nothing being freed */
4331 rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4332 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4333 end_dmi_offset = offset + len;
4334 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4336 if (offset < ip->i_size &&
4337 (attr_flags & ATTR_DMI) == 0 &&
4338 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4339 if (end_dmi_offset > ip->i_size)
4340 end_dmi_offset = ip->i_size;
4341 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
4342 offset, end_dmi_offset - offset,
4343 AT_DELAY_FLAG(attr_flags), NULL);
4348 if (attr_flags & ATTR_NOLOCK)
4351 xfs_ilock(ip, XFS_IOLOCK_EXCL);
4352 vn_iowait(vp); /* wait for the completion of any pending DIOs */
4355 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, NBPP);
4356 ioffset = offset & ~(rounding - 1);
4358 if (VN_CACHED(vp) != 0) {
4359 xfs_inval_cached_trace(&ip->i_iocore, ioffset, -1,
4360 ctooff(offtoct(ioffset)), -1);
4361 error = bhv_vop_flushinval_pages(vp, ctooff(offtoct(ioffset)),
4362 -1, FI_REMAPF_LOCKED);
4364 goto out_unlock_iolock;
4368 * Need to zero the stuff we're not freeing, on disk.
4369 * If its a realtime file & can't use unwritten extents then we
4370 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4371 * will take care of it for us.
4373 if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4375 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, startoffset_fsb,
4376 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4378 goto out_unlock_iolock;
4379 ASSERT(nimap == 0 || nimap == 1);
4380 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4383 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4384 block = imap.br_startblock;
4385 mod = do_div(block, mp->m_sb.sb_rextsize);
4387 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4390 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, endoffset_fsb - 1,
4391 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4393 goto out_unlock_iolock;
4394 ASSERT(nimap == 0 || nimap == 1);
4395 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4396 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4398 if (mod && (mod != mp->m_sb.sb_rextsize))
4399 endoffset_fsb -= mod;
4402 if ((done = (endoffset_fsb <= startoffset_fsb)))
4404 * One contiguous piece to clear
4406 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4409 * Some full blocks, possibly two pieces to clear
4411 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4412 error = xfs_zero_remaining_bytes(ip, offset,
4413 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4415 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4416 error = xfs_zero_remaining_bytes(ip,
4417 XFS_FSB_TO_B(mp, endoffset_fsb),
4422 * free file space until done or until there is an error
4424 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4425 while (!error && !done) {
4428 * allocate and setup the transaction
4430 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4431 error = xfs_trans_reserve(tp,
4433 XFS_WRITE_LOG_RES(mp),
4435 XFS_TRANS_PERM_LOG_RES,
4436 XFS_WRITE_LOG_COUNT);
4439 * check for running out of space
4443 * Free the transaction structure.
4445 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4446 xfs_trans_cancel(tp, 0);
4449 xfs_ilock(ip, XFS_ILOCK_EXCL);
4450 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4451 ip->i_udquot, ip->i_gdquot, resblks, 0,
4452 XFS_QMOPT_RES_REGBLKS);
4456 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4457 xfs_trans_ihold(tp, ip);
4460 * issue the bunmapi() call to free the blocks
4462 XFS_BMAP_INIT(&free_list, &firstfsb);
4463 error = XFS_BUNMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4464 endoffset_fsb - startoffset_fsb,
4465 0, 2, &firstfsb, &free_list, NULL, &done);
4471 * complete the transaction
4473 error = xfs_bmap_finish(&tp, &free_list, &committed);
4478 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
4479 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4484 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4488 xfs_bmap_cancel(&free_list);
4490 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4491 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4497 * xfs_change_file_space()
4498 * This routine allocates or frees disk space for the given file.
4499 * The user specified parameters are checked for alignment and size
4508 xfs_change_file_space(
4522 xfs_off_t startoffset;
4528 vp = BHV_TO_VNODE(bdp);
4529 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4531 ip = XFS_BHVTOI(bdp);
4535 * must be a regular file and have write permission
4538 return XFS_ERROR(EINVAL);
4540 xfs_ilock(ip, XFS_ILOCK_SHARED);
4542 if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
4543 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4547 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4549 switch (bf->l_whence) {
4550 case 0: /*SEEK_SET*/
4552 case 1: /*SEEK_CUR*/
4553 bf->l_start += offset;
4555 case 2: /*SEEK_END*/
4556 bf->l_start += ip->i_size;
4559 return XFS_ERROR(EINVAL);
4562 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4564 if ( (bf->l_start < 0)
4565 || (bf->l_start > XFS_MAXIOFFSET(mp))
4566 || (bf->l_start + llen < 0)
4567 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4568 return XFS_ERROR(EINVAL);
4572 startoffset = bf->l_start;
4576 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4578 * These calls do NOT zero the data space allocated to the file,
4579 * nor do they change the file size.
4581 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4583 * These calls cause the new file data to be zeroed and the file
4584 * size to be changed.
4586 setprealloc = clrprealloc = 0;
4589 case XFS_IOC_RESVSP:
4590 case XFS_IOC_RESVSP64:
4591 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4598 case XFS_IOC_UNRESVSP:
4599 case XFS_IOC_UNRESVSP64:
4600 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4605 case XFS_IOC_ALLOCSP:
4606 case XFS_IOC_ALLOCSP64:
4607 case XFS_IOC_FREESP:
4608 case XFS_IOC_FREESP64:
4609 if (startoffset > fsize) {
4610 error = xfs_alloc_file_space(ip, fsize,
4611 startoffset - fsize, 0, attr_flags);
4616 va.va_mask = XFS_AT_SIZE;
4617 va.va_size = startoffset;
4619 error = xfs_setattr(bdp, &va, attr_flags, credp);
4629 return XFS_ERROR(EINVAL);
4633 * update the inode timestamp, mode, and prealloc flag bits
4635 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4637 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4640 xfs_trans_cancel(tp, 0);
4644 xfs_ilock(ip, XFS_ILOCK_EXCL);
4646 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4647 xfs_trans_ihold(tp, ip);
4649 if ((attr_flags & ATTR_DMI) == 0) {
4650 ip->i_d.di_mode &= ~S_ISUID;
4653 * Note that we don't have to worry about mandatory
4654 * file locking being disabled here because we only
4655 * clear the S_ISGID bit if the Group execute bit is
4656 * on, but if it was on then mandatory locking wouldn't
4657 * have been enabled.
4659 if (ip->i_d.di_mode & S_IXGRP)
4660 ip->i_d.di_mode &= ~S_ISGID;
4662 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4665 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4666 else if (clrprealloc)
4667 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4669 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4670 xfs_trans_set_sync(tp);
4672 error = xfs_trans_commit(tp, 0);
4674 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4679 bhv_vnodeops_t xfs_vnodeops = {
4680 BHV_IDENTITY_INIT(VN_BHV_XFS,VNODE_POSITION_XFS),
4681 .vop_open = xfs_open,
4682 .vop_close = xfs_close,
4683 .vop_read = xfs_read,
4685 .vop_splice_read = xfs_splice_read,
4686 .vop_splice_write = xfs_splice_write,
4688 .vop_write = xfs_write,
4689 .vop_ioctl = xfs_ioctl,
4690 .vop_getattr = xfs_getattr,
4691 .vop_setattr = xfs_setattr,
4692 .vop_access = xfs_access,
4693 .vop_lookup = xfs_lookup,
4694 .vop_create = xfs_create,
4695 .vop_remove = xfs_remove,
4696 .vop_link = xfs_link,
4697 .vop_rename = xfs_rename,
4698 .vop_mkdir = xfs_mkdir,
4699 .vop_rmdir = xfs_rmdir,
4700 .vop_readdir = xfs_readdir,
4701 .vop_symlink = xfs_symlink,
4702 .vop_readlink = xfs_readlink,
4703 .vop_fsync = xfs_fsync,
4704 .vop_inactive = xfs_inactive,
4705 .vop_fid2 = xfs_fid2,
4706 .vop_rwlock = xfs_rwlock,
4707 .vop_rwunlock = xfs_rwunlock,
4708 .vop_bmap = xfs_bmap,
4709 .vop_reclaim = xfs_reclaim,
4710 .vop_attr_get = xfs_attr_get,
4711 .vop_attr_set = xfs_attr_set,
4712 .vop_attr_remove = xfs_attr_remove,
4713 .vop_attr_list = xfs_attr_list,
4714 .vop_link_removed = (vop_link_removed_t)fs_noval,
4715 .vop_vnode_change = (vop_vnode_change_t)fs_noval,
4716 .vop_tosspages = fs_tosspages,
4717 .vop_flushinval_pages = fs_flushinval_pages,
4718 .vop_flush_pages = fs_flush_pages,
4719 .vop_release = xfs_release,
4720 .vop_iflush = xfs_inode_flush,