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,
1266 xfs_trans_cancel(tp, 0);
1268 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1272 error = xfs_trans_reserve(tp, 0,
1273 XFS_ITRUNCATE_LOG_RES(mp),
1274 0, XFS_TRANS_PERM_LOG_RES,
1275 XFS_ITRUNCATE_LOG_COUNT);
1277 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1278 xfs_trans_cancel(tp, 0);
1279 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1283 xfs_ilock(ip, XFS_ILOCK_EXCL);
1284 xfs_trans_ijoin(tp, ip,
1287 xfs_trans_ihold(tp, ip);
1289 error = xfs_itruncate_finish(&tp, ip,
1294 * If we get an error at this point we
1295 * simply don't bother truncating the file.
1298 xfs_trans_cancel(tp,
1299 (XFS_TRANS_RELEASE_LOG_RES |
1302 error = xfs_trans_commit(tp,
1303 XFS_TRANS_RELEASE_LOG_RES);
1305 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1312 * Free a symlink that has blocks associated with it.
1315 xfs_inactive_symlink_rmt(
1323 xfs_fsblock_t first_block;
1324 xfs_bmap_free_t free_list;
1327 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1335 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1337 * We're freeing a symlink that has some
1338 * blocks allocated to it. Free the
1339 * blocks here. We know that we've got
1340 * either 1 or 2 extents and that we can
1341 * free them all in one bunmapi call.
1343 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1344 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1345 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1346 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1347 xfs_trans_cancel(tp, 0);
1352 * Lock the inode, fix the size, and join it to the transaction.
1353 * Hold it so in the normal path, we still have it locked for
1354 * the second transaction. In the error paths we need it
1355 * held so the cancel won't rele it, see below.
1357 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1358 size = (int)ip->i_d.di_size;
1359 ip->i_d.di_size = 0;
1360 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1361 xfs_trans_ihold(tp, ip);
1362 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1364 * Find the block(s) so we can inval and unmap them.
1367 XFS_BMAP_INIT(&free_list, &first_block);
1368 nmaps = ARRAY_SIZE(mval);
1369 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1370 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
1374 * Invalidate the block(s).
1376 for (i = 0; i < nmaps; i++) {
1377 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1378 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1379 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1380 xfs_trans_binval(tp, bp);
1383 * Unmap the dead block(s) to the free_list.
1385 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
1386 &first_block, &free_list, NULL, &done)))
1390 * Commit the first transaction. This logs the EFI and the inode.
1392 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
1395 * The transaction must have been committed, since there were
1396 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1397 * The new tp has the extent freeing and EFDs.
1401 * The first xact was committed, so add the inode to the new one.
1402 * Mark it dirty so it will be logged and moved forward in the log as
1403 * part of every commit.
1405 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1406 xfs_trans_ihold(tp, ip);
1407 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1409 * Get a new, empty transaction to return to our caller.
1411 ntp = xfs_trans_dup(tp);
1413 * Commit the transaction containing extent freeing and EFDs.
1414 * If we get an error on the commit here or on the reserve below,
1415 * we need to unlock the inode since the new transaction doesn't
1416 * have the inode attached.
1418 error = xfs_trans_commit(tp, 0);
1421 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1425 * Remove the memory for extent descriptions (just bookkeeping).
1427 if (ip->i_df.if_bytes)
1428 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1429 ASSERT(ip->i_df.if_bytes == 0);
1431 * Put an itruncate log reservation in the new transaction
1434 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1435 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1436 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1440 * Return with the inode locked but not joined to the transaction.
1446 xfs_bmap_cancel(&free_list);
1449 * Have to come here with the inode locked and either
1450 * (held and in the transaction) or (not in the transaction).
1451 * If the inode isn't held then cancel would iput it, but
1452 * that's wrong since this is inactive and the vnode ref
1453 * count is 0 already.
1454 * Cancel won't do anything to the inode if held, but it still
1455 * needs to be locked until the cancel is done, if it was
1456 * joined to the transaction.
1458 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1459 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1466 xfs_inactive_symlink_local(
1472 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1474 * We're freeing a symlink which fit into
1475 * the inode. Just free the memory used
1476 * to hold the old symlink.
1478 error = xfs_trans_reserve(*tpp, 0,
1479 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1480 0, XFS_TRANS_PERM_LOG_RES,
1481 XFS_ITRUNCATE_LOG_COUNT);
1484 xfs_trans_cancel(*tpp, 0);
1488 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1491 * Zero length symlinks _can_ exist.
1493 if (ip->i_df.if_bytes > 0) {
1494 xfs_idata_realloc(ip,
1495 -(ip->i_df.if_bytes),
1497 ASSERT(ip->i_df.if_bytes == 0);
1511 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1514 ASSERT(ip->i_d.di_forkoff != 0);
1515 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1516 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1518 error = xfs_attr_inactive(ip);
1521 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1522 return error; /* goto out */
1525 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1526 error = xfs_trans_reserve(tp, 0,
1527 XFS_IFREE_LOG_RES(mp),
1528 0, XFS_TRANS_PERM_LOG_RES,
1529 XFS_INACTIVE_LOG_COUNT);
1531 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1532 xfs_trans_cancel(tp, 0);
1534 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1538 xfs_ilock(ip, XFS_ILOCK_EXCL);
1539 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1540 xfs_trans_ihold(tp, ip);
1541 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1543 ASSERT(ip->i_d.di_anextents == 0);
1558 vp = BHV_TO_VNODE(bdp);
1559 ip = XFS_BHVTOI(bdp);
1562 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
1565 /* If this is a read-only mount, don't do this (would generate I/O) */
1566 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1569 #ifdef HAVE_REFCACHE
1570 /* If we are in the NFS reference cache then don't do this now */
1575 if (ip->i_d.di_nlink != 0) {
1576 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1577 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1578 ip->i_delayed_blks > 0)) &&
1579 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1580 (!(ip->i_d.di_flags &
1581 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1582 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1585 /* Update linux inode block count after free above */
1586 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1587 ip->i_d.di_nblocks + ip->i_delayed_blks);
1597 * This is called when the vnode reference count for the vnode
1598 * goes to zero. If the file has been unlinked, then it must
1599 * now be truncated. Also, we clear all of the read-ahead state
1600 * kept for the inode here since the file is now closed.
1609 xfs_bmap_free_t free_list;
1610 xfs_fsblock_t first_block;
1617 vp = BHV_TO_VNODE(bdp);
1618 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1620 ip = XFS_BHVTOI(bdp);
1623 * If the inode is already free, then there can be nothing
1626 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1627 ASSERT(ip->i_df.if_real_bytes == 0);
1628 ASSERT(ip->i_df.if_broot_bytes == 0);
1629 return VN_INACTIVE_CACHE;
1633 * Only do a truncate if it's a regular file with
1634 * some actual space in it. It's OK to look at the
1635 * inode's fields without the lock because we're the
1636 * only one with a reference to the inode.
1638 truncate = ((ip->i_d.di_nlink == 0) &&
1639 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1640 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1641 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1645 if (ip->i_d.di_nlink == 0 &&
1646 DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_DESTROY)) {
1647 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1652 /* If this is a read-only mount, don't do this (would generate I/O) */
1653 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1656 if (ip->i_d.di_nlink != 0) {
1657 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1658 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1659 ip->i_delayed_blks > 0)) &&
1660 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1661 (!(ip->i_d.di_flags &
1662 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1663 (ip->i_delayed_blks != 0)))) {
1664 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1666 return VN_INACTIVE_CACHE;
1667 /* Update linux inode block count after free above */
1668 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1669 ip->i_d.di_nblocks + ip->i_delayed_blks);
1674 ASSERT(ip->i_d.di_nlink == 0);
1676 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1677 return VN_INACTIVE_CACHE;
1679 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1682 * Do the xfs_itruncate_start() call before
1683 * reserving any log space because itruncate_start
1684 * will call into the buffer cache and we can't
1685 * do that within a transaction.
1687 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1689 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1691 xfs_trans_cancel(tp, 0);
1692 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1693 return VN_INACTIVE_CACHE;
1696 error = xfs_trans_reserve(tp, 0,
1697 XFS_ITRUNCATE_LOG_RES(mp),
1698 0, XFS_TRANS_PERM_LOG_RES,
1699 XFS_ITRUNCATE_LOG_COUNT);
1701 /* Don't call itruncate_cleanup */
1702 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1703 xfs_trans_cancel(tp, 0);
1704 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1705 return VN_INACTIVE_CACHE;
1708 xfs_ilock(ip, XFS_ILOCK_EXCL);
1709 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1710 xfs_trans_ihold(tp, ip);
1713 * normally, we have to run xfs_itruncate_finish sync.
1714 * But if filesystem is wsync and we're in the inactive
1715 * path, then we know that nlink == 0, and that the
1716 * xaction that made nlink == 0 is permanently committed
1717 * since xfs_remove runs as a synchronous transaction.
1719 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1720 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1723 xfs_trans_cancel(tp,
1724 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1725 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1726 return VN_INACTIVE_CACHE;
1728 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1731 * If we get an error while cleaning up a
1732 * symlink we bail out.
1734 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1735 xfs_inactive_symlink_rmt(ip, &tp) :
1736 xfs_inactive_symlink_local(ip, &tp);
1740 return VN_INACTIVE_CACHE;
1743 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1744 xfs_trans_ihold(tp, ip);
1746 error = xfs_trans_reserve(tp, 0,
1747 XFS_IFREE_LOG_RES(mp),
1748 0, XFS_TRANS_PERM_LOG_RES,
1749 XFS_INACTIVE_LOG_COUNT);
1751 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1752 xfs_trans_cancel(tp, 0);
1753 return VN_INACTIVE_CACHE;
1756 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1757 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1758 xfs_trans_ihold(tp, ip);
1762 * If there are attributes associated with the file
1763 * then blow them away now. The code calls a routine
1764 * that recursively deconstructs the attribute fork.
1765 * We need to just commit the current transaction
1766 * because we can't use it for xfs_attr_inactive().
1768 if (ip->i_d.di_anextents > 0) {
1769 error = xfs_inactive_attrs(ip, &tp);
1771 * If we got an error, the transaction is already
1772 * cancelled, and the inode is unlocked. Just get out.
1775 return VN_INACTIVE_CACHE;
1776 } else if (ip->i_afp) {
1777 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1783 XFS_BMAP_INIT(&free_list, &first_block);
1784 error = xfs_ifree(tp, ip, &free_list);
1787 * If we fail to free the inode, shut down. The cancel
1788 * might do that, we need to make sure. Otherwise the
1789 * inode might be lost for a long time or forever.
1791 if (!XFS_FORCED_SHUTDOWN(mp)) {
1793 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1794 error, mp->m_fsname);
1795 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1797 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1800 * Credit the quota account(s). The inode is gone.
1802 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1805 * Just ignore errors at this point. There is
1806 * nothing we can do except to try to keep going.
1808 (void) xfs_bmap_finish(&tp, &free_list, &committed);
1809 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1812 * Release the dquots held by inode, if any.
1814 XFS_QM_DQDETACH(mp, ip);
1816 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1819 return VN_INACTIVE_CACHE;
1828 bhv_desc_t *dir_bdp,
1829 bhv_vname_t *dentry,
1835 xfs_inode_t *dp, *ip;
1839 bhv_vnode_t *dir_vp;
1841 dir_vp = BHV_TO_VNODE(dir_bdp);
1842 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1844 dp = XFS_BHVTOI(dir_bdp);
1846 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1847 return XFS_ERROR(EIO);
1849 lock_mode = xfs_ilock_map_shared(dp);
1850 error = xfs_dir_lookup_int(dir_bdp, lock_mode, dentry, &e_inum, &ip);
1852 *vpp = XFS_ITOV(ip);
1855 xfs_iunlock_map_shared(dp, lock_mode);
1861 * xfs_create (create a new file).
1865 bhv_desc_t *dir_bdp,
1866 bhv_vname_t *dentry,
1871 char *name = VNAME(dentry);
1872 bhv_vnode_t *dir_vp;
1873 xfs_inode_t *dp, *ip;
1874 bhv_vnode_t *vp = NULL;
1879 xfs_bmap_free_t free_list;
1880 xfs_fsblock_t first_block;
1881 boolean_t dp_joined_to_trans;
1882 int dm_event_sent = 0;
1886 struct xfs_dquot *udqp, *gdqp;
1892 dir_vp = BHV_TO_VNODE(dir_bdp);
1893 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1895 dp = XFS_BHVTOI(dir_bdp);
1898 dm_di_mode = vap->va_mode;
1899 namelen = VNAMELEN(dentry);
1901 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
1902 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1903 dir_vp, DM_RIGHT_NULL, NULL,
1904 DM_RIGHT_NULL, name, NULL,
1912 if (XFS_FORCED_SHUTDOWN(mp))
1913 return XFS_ERROR(EIO);
1915 /* Return through std_return after this point. */
1918 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1919 prid = dp->i_d.di_projid;
1920 else if (vap->va_mask & XFS_AT_PROJID)
1921 prid = (xfs_prid_t)vap->va_projid;
1923 prid = (xfs_prid_t)dfltprid;
1926 * Make sure that we have allocated dquot(s) on disk.
1928 error = XFS_QM_DQVOPALLOC(mp, dp,
1929 current_fsuid(credp), current_fsgid(credp), prid,
1930 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1935 dp_joined_to_trans = B_FALSE;
1937 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1938 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1939 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1941 * Initially assume that the file does not exist and
1942 * reserve the resources for that case. If that is not
1943 * the case we'll drop the one we have and get a more
1944 * appropriate transaction later.
1946 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1947 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1948 if (error == ENOSPC) {
1950 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1951 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1959 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1961 XFS_BMAP_INIT(&free_list, &first_block);
1966 * Reserve disk quota and the inode.
1968 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1972 if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
1974 rdev = (vap->va_mask & XFS_AT_RDEV) ? vap->va_rdev : 0;
1975 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 1,
1976 rdev, credp, prid, resblks > 0,
1979 if (error == ENOSPC)
1986 * At this point, we've gotten a newly allocated inode.
1987 * It is locked (and joined to the transaction).
1990 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1993 * Now we join the directory inode to the transaction.
1994 * We do not do it earlier because xfs_dir_ialloc
1995 * might commit the previous transaction (and release
2000 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2001 dp_joined_to_trans = B_TRUE;
2003 error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
2004 &first_block, &free_list, resblks ?
2005 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2007 ASSERT(error != ENOSPC);
2010 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2011 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2014 * If this is a synchronous mount, make sure that the
2015 * create transaction goes to disk before returning to
2018 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2019 xfs_trans_set_sync(tp);
2025 * Attach the dquot(s) to the inodes and modify them incore.
2026 * These ids of the inode couldn't have changed since the new
2027 * inode has been locked ever since it was created.
2029 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2032 * xfs_trans_commit normally decrements the vnode ref count
2033 * when it unlocks the inode. Since we want to return the
2034 * vnode to the caller, we bump the vnode ref count now.
2039 error = xfs_bmap_finish(&tp, &free_list, &committed);
2041 xfs_bmap_cancel(&free_list);
2045 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2052 XFS_QM_DQRELE(mp, udqp);
2053 XFS_QM_DQRELE(mp, gdqp);
2056 * Propagate the fact that the vnode changed after the
2057 * xfs_inode locks have been released.
2059 bhv_vop_vnode_change(vp, VCHANGE_FLAGS_TRUNCATED, 3);
2063 /* Fallthrough to std_return with error = 0 */
2066 if ( (*vpp || (error != 0 && dm_event_sent != 0)) &&
2067 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2068 DM_EVENT_POSTCREATE)) {
2069 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2070 dir_vp, DM_RIGHT_NULL,
2072 DM_RIGHT_NULL, name, NULL,
2073 dm_di_mode, error, 0);
2078 cancel_flags |= XFS_TRANS_ABORT;
2083 xfs_trans_cancel(tp, cancel_flags);
2085 if (!dp_joined_to_trans && (dp != NULL))
2086 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2087 XFS_QM_DQRELE(mp, udqp);
2088 XFS_QM_DQRELE(mp, gdqp);
2094 * Wait until after the current transaction is aborted to
2095 * release the inode. This prevents recursive transactions
2096 * and deadlocks from xfs_inactive.
2098 cancel_flags |= XFS_TRANS_ABORT;
2099 xfs_trans_cancel(tp, cancel_flags);
2102 XFS_QM_DQRELE(mp, udqp);
2103 XFS_QM_DQRELE(mp, gdqp);
2110 * Some counters to see if (and how often) we are hitting some deadlock
2111 * prevention code paths.
2115 int xfs_rm_lock_delays;
2116 int xfs_rm_attempts;
2120 * The following routine will lock the inodes associated with the
2121 * directory and the named entry in the directory. The locks are
2122 * acquired in increasing inode number.
2124 * If the entry is "..", then only the directory is locked. The
2125 * vnode ref count will still include that from the .. entry in
2128 * There is a deadlock we need to worry about. If the locked directory is
2129 * in the AIL, it might be blocking up the log. The next inode we lock
2130 * could be already locked by another thread waiting for log space (e.g
2131 * a permanent log reservation with a long running transaction (see
2132 * xfs_itruncate_finish)). To solve this, we must check if the directory
2133 * is in the ail and use lock_nowait. If we can't lock, we need to
2134 * drop the inode lock on the directory and try again. xfs_iunlock will
2135 * potentially push the tail if we were holding up the log.
2138 xfs_lock_dir_and_entry(
2140 xfs_inode_t *ip) /* inode of entry 'name' */
2144 xfs_inode_t *ips[2];
2153 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2160 * We want to lock in increasing inum. Since we've already
2161 * acquired the lock on the directory, we may need to release
2162 * if if the inum of the entry turns out to be less.
2164 if (e_inum > dp->i_ino) {
2166 * We are already in the right order, so just
2167 * lock on the inode of the entry.
2168 * We need to use nowait if dp is in the AIL.
2171 lp = (xfs_log_item_t *)dp->i_itemp;
2172 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2173 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2180 * Unlock dp and try again.
2181 * xfs_iunlock will try to push the tail
2182 * if the inode is in the AIL.
2185 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2187 if ((attempts % 5) == 0) {
2188 delay(1); /* Don't just spin the CPU */
2190 xfs_rm_lock_delays++;
2196 xfs_ilock(ip, XFS_ILOCK_EXCL);
2198 } else if (e_inum < dp->i_ino) {
2199 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2203 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2205 /* else e_inum == dp->i_ino */
2206 /* This can happen if we're asked to lock /x/..
2207 * the entry is "..", which is also the parent directory.
2215 int xfs_small_retries;
2216 int xfs_middle_retries;
2217 int xfs_lots_retries;
2218 int xfs_lock_delays;
2222 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2226 xfs_lock_inumorder(int lock_mode, int subclass)
2228 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
2229 lock_mode |= (subclass + XFS_IOLOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
2230 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
2231 lock_mode |= (subclass + XFS_ILOCK_INUMORDER) << XFS_ILOCK_SHIFT;
2237 * The following routine will lock n inodes in exclusive mode.
2238 * We assume the caller calls us with the inodes in i_ino order.
2240 * We need to detect deadlock where an inode that we lock
2241 * is in the AIL and we start waiting for another inode that is locked
2242 * by a thread in a long running transaction (such as truncate). This can
2243 * result in deadlock since the long running trans might need to wait
2244 * for the inode we just locked in order to push the tail and free space
2254 int attempts = 0, i, j, try_lock;
2257 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2268 for (; i < inodes; i++) {
2271 if (i && (ips[i] == ips[i-1])) /* Already locked */
2275 * If try_lock is not set yet, make sure all locked inodes
2276 * are not in the AIL.
2277 * If any are, set try_lock to be used later.
2281 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2282 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2283 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2290 * If any of the previous locks we have locked is in the AIL,
2291 * we must TRY to get the second and subsequent locks. If
2292 * we can't get any, we must release all we have
2297 /* try_lock must be 0 if i is 0. */
2299 * try_lock means we have an inode locked
2300 * that is in the AIL.
2303 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
2307 * Unlock all previous guys and try again.
2308 * xfs_iunlock will try to push the tail
2309 * if the inode is in the AIL.
2312 for(j = i - 1; j >= 0; j--) {
2315 * Check to see if we've already
2316 * unlocked this one.
2317 * Not the first one going back,
2318 * and the inode ptr is the same.
2320 if ((j != (i - 1)) && ips[j] ==
2324 xfs_iunlock(ips[j], lock_mode);
2327 if ((attempts % 5) == 0) {
2328 delay(1); /* Don't just spin the CPU */
2338 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
2344 if (attempts < 5) xfs_small_retries++;
2345 else if (attempts < 100) xfs_middle_retries++;
2346 else xfs_lots_retries++;
2354 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2355 int remove_which_error_return = 0;
2357 #define REMOVE_DEBUG_TRACE(x)
2358 #endif /* ! DEBUG */
2367 bhv_desc_t *dir_bdp,
2368 bhv_vname_t *dentry,
2371 bhv_vnode_t *dir_vp;
2372 char *name = VNAME(dentry);
2373 xfs_inode_t *dp, *ip;
2374 xfs_trans_t *tp = NULL;
2377 xfs_bmap_free_t free_list;
2378 xfs_fsblock_t first_block;
2386 dir_vp = BHV_TO_VNODE(dir_bdp);
2387 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2389 dp = XFS_BHVTOI(dir_bdp);
2392 if (XFS_FORCED_SHUTDOWN(mp))
2393 return XFS_ERROR(EIO);
2395 namelen = VNAMELEN(dentry);
2397 if (!xfs_get_dir_entry(dentry, &ip)) {
2398 dm_di_mode = ip->i_d.di_mode;
2402 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
2403 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2404 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2405 name, NULL, dm_di_mode, 0, 0);
2410 /* From this point on, return through std_return */
2414 * We need to get a reference to ip before we get our log
2415 * reservation. The reason for this is that we cannot call
2416 * xfs_iget for an inode for which we do not have a reference
2417 * once we've acquired a log reservation. This is because the
2418 * inode we are trying to get might be in xfs_inactive going
2419 * for a log reservation. Since we'll have to wait for the
2420 * inactive code to complete before returning from xfs_iget,
2421 * we need to make sure that we don't have log space reserved
2422 * when we call xfs_iget. Instead we get an unlocked reference
2423 * to the inode before getting our log reservation.
2425 error = xfs_get_dir_entry(dentry, &ip);
2427 REMOVE_DEBUG_TRACE(__LINE__);
2431 dm_di_mode = ip->i_d.di_mode;
2433 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2437 error = XFS_QM_DQATTACH(mp, dp, 0);
2438 if (!error && dp != ip)
2439 error = XFS_QM_DQATTACH(mp, ip, 0);
2441 REMOVE_DEBUG_TRACE(__LINE__);
2446 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2447 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2449 * We try to get the real space reservation first,
2450 * allowing for directory btree deletion(s) implying
2451 * possible bmap insert(s). If we can't get the space
2452 * reservation then we use 0 instead, and avoid the bmap
2453 * btree insert(s) in the directory code by, if the bmap
2454 * insert tries to happen, instead trimming the LAST
2455 * block from the directory.
2457 resblks = XFS_REMOVE_SPACE_RES(mp);
2458 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2459 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2460 if (error == ENOSPC) {
2462 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2463 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2466 ASSERT(error != ENOSPC);
2467 REMOVE_DEBUG_TRACE(__LINE__);
2468 xfs_trans_cancel(tp, 0);
2473 error = xfs_lock_dir_and_entry(dp, ip);
2475 REMOVE_DEBUG_TRACE(__LINE__);
2476 xfs_trans_cancel(tp, cancel_flags);
2482 * At this point, we've gotten both the directory and the entry
2485 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2488 * Increment vnode ref count only in this case since
2489 * there's an extra vnode reference in the case where
2493 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2497 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2499 XFS_BMAP_INIT(&free_list, &first_block);
2500 error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2501 &first_block, &free_list, 0);
2503 ASSERT(error != ENOENT);
2504 REMOVE_DEBUG_TRACE(__LINE__);
2507 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2510 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2512 error = xfs_droplink(tp, ip);
2514 REMOVE_DEBUG_TRACE(__LINE__);
2518 /* Determine if this is the last link while
2519 * we are in the transaction.
2521 link_zero = (ip)->i_d.di_nlink==0;
2524 * Take an extra ref on the inode so that it doesn't
2525 * go to xfs_inactive() from within the commit.
2530 * If this is a synchronous mount, make sure that the
2531 * remove transaction goes to disk before returning to
2534 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2535 xfs_trans_set_sync(tp);
2538 error = xfs_bmap_finish(&tp, &free_list, &committed);
2540 REMOVE_DEBUG_TRACE(__LINE__);
2544 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2551 * Before we drop our extra reference to the inode, purge it
2552 * from the refcache if it is there. By waiting until afterwards
2553 * to do the IRELE, we ensure that we won't go inactive in the
2554 * xfs_refcache_purge_ip routine (although that would be OK).
2556 xfs_refcache_purge_ip(ip);
2558 vn_trace_exit(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2561 * Let interposed file systems know about removed links.
2563 bhv_vop_link_removed(XFS_ITOV(ip), dir_vp, link_zero);
2567 /* Fall through to std_return with error = 0 */
2569 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp,
2570 DM_EVENT_POSTREMOVE)) {
2571 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2572 dir_vp, DM_RIGHT_NULL,
2573 NULL, DM_RIGHT_NULL,
2574 name, NULL, dm_di_mode, error, 0);
2579 xfs_bmap_cancel(&free_list);
2580 cancel_flags |= XFS_TRANS_ABORT;
2581 xfs_trans_cancel(tp, cancel_flags);
2586 * In this case make sure to not release the inode until after
2587 * the current transaction is aborted. Releasing it beforehand
2588 * can cause us to go to xfs_inactive and start a recursive
2589 * transaction which can easily deadlock with the current one.
2591 xfs_bmap_cancel(&free_list);
2592 cancel_flags |= XFS_TRANS_ABORT;
2593 xfs_trans_cancel(tp, cancel_flags);
2596 * Before we drop our extra reference to the inode, purge it
2597 * from the refcache if it is there. By waiting until afterwards
2598 * to do the IRELE, we ensure that we won't go inactive in the
2599 * xfs_refcache_purge_ip routine (although that would be OK).
2601 xfs_refcache_purge_ip(ip);
2615 bhv_desc_t *target_dir_bdp,
2616 bhv_vnode_t *src_vp,
2617 bhv_vname_t *dentry,
2620 xfs_inode_t *tdp, *sip;
2623 xfs_inode_t *ips[2];
2625 xfs_bmap_free_t free_list;
2626 xfs_fsblock_t first_block;
2629 bhv_vnode_t *target_dir_vp;
2631 char *target_name = VNAME(dentry);
2634 target_dir_vp = BHV_TO_VNODE(target_dir_bdp);
2635 vn_trace_entry(target_dir_vp, __FUNCTION__, (inst_t *)__return_address);
2636 vn_trace_entry(src_vp, __FUNCTION__, (inst_t *)__return_address);
2638 target_namelen = VNAMELEN(dentry);
2639 ASSERT(!VN_ISDIR(src_vp));
2641 sip = xfs_vtoi(src_vp);
2642 tdp = XFS_BHVTOI(target_dir_bdp);
2644 if (XFS_FORCED_SHUTDOWN(mp))
2645 return XFS_ERROR(EIO);
2647 if (DM_EVENT_ENABLED(src_vp->v_vfsp, tdp, DM_EVENT_LINK)) {
2648 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2649 target_dir_vp, DM_RIGHT_NULL,
2650 src_vp, DM_RIGHT_NULL,
2651 target_name, NULL, 0, 0, 0);
2656 /* Return through std_return after this point. */
2658 error = XFS_QM_DQATTACH(mp, sip, 0);
2659 if (!error && sip != tdp)
2660 error = XFS_QM_DQATTACH(mp, tdp, 0);
2664 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2665 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2666 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2667 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2668 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2669 if (error == ENOSPC) {
2671 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2672 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2679 if (sip->i_ino < tdp->i_ino) {
2687 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2690 * Increment vnode ref counts since xfs_trans_commit &
2691 * xfs_trans_cancel will both unlock the inodes and
2692 * decrement the associated ref counts.
2695 VN_HOLD(target_dir_vp);
2696 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2697 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2700 * If the source has too many links, we can't make any more to it.
2702 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2703 error = XFS_ERROR(EMLINK);
2708 * If we are using project inheritance, we only allow hard link
2709 * creation in our tree when the project IDs are the same; else
2710 * the tree quota mechanism could be circumvented.
2712 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2713 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2714 error = XFS_ERROR(EXDEV);
2719 (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
2722 XFS_BMAP_INIT(&free_list, &first_block);
2724 error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
2725 sip->i_ino, &first_block, &free_list,
2729 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2731 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2733 error = xfs_bumplink(tp, sip);
2738 * If this is a synchronous mount, make sure that the
2739 * link transaction goes to disk before returning to
2742 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2743 xfs_trans_set_sync(tp);
2746 error = xfs_bmap_finish (&tp, &free_list, &committed);
2748 xfs_bmap_cancel(&free_list);
2752 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2756 /* Fall through to std_return with error = 0. */
2758 if (DM_EVENT_ENABLED(src_vp->v_vfsp, sip,
2759 DM_EVENT_POSTLINK)) {
2760 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2761 target_dir_vp, DM_RIGHT_NULL,
2762 src_vp, DM_RIGHT_NULL,
2763 target_name, NULL, 0, error, 0);
2768 cancel_flags |= XFS_TRANS_ABORT;
2772 xfs_trans_cancel(tp, cancel_flags);
2783 bhv_desc_t *dir_bdp,
2784 bhv_vname_t *dentry,
2789 char *dir_name = VNAME(dentry);
2791 xfs_inode_t *cdp; /* inode of created dir */
2792 bhv_vnode_t *cvp; /* vnode of created dir */
2798 xfs_bmap_free_t free_list;
2799 xfs_fsblock_t first_block;
2800 bhv_vnode_t *dir_vp;
2801 boolean_t dp_joined_to_trans;
2802 boolean_t created = B_FALSE;
2803 int dm_event_sent = 0;
2805 struct xfs_dquot *udqp, *gdqp;
2810 dir_vp = BHV_TO_VNODE(dir_bdp);
2811 dp = XFS_BHVTOI(dir_bdp);
2814 if (XFS_FORCED_SHUTDOWN(mp))
2815 return XFS_ERROR(EIO);
2817 dir_namelen = VNAMELEN(dentry);
2820 dp_joined_to_trans = B_FALSE;
2821 dm_di_mode = vap->va_mode;
2823 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
2824 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2825 dir_vp, DM_RIGHT_NULL, NULL,
2826 DM_RIGHT_NULL, dir_name, NULL,
2833 /* Return through std_return after this point. */
2835 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2839 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2840 prid = dp->i_d.di_projid;
2841 else if (vap->va_mask & XFS_AT_PROJID)
2842 prid = (xfs_prid_t)vap->va_projid;
2844 prid = (xfs_prid_t)dfltprid;
2847 * Make sure that we have allocated dquot(s) on disk.
2849 error = XFS_QM_DQVOPALLOC(mp, dp,
2850 current_fsuid(credp), current_fsgid(credp), prid,
2851 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2855 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2856 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2857 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2858 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2859 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2860 if (error == ENOSPC) {
2862 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2863 XFS_TRANS_PERM_LOG_RES,
2864 XFS_MKDIR_LOG_COUNT);
2872 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2875 * Check for directory link count overflow.
2877 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2878 error = XFS_ERROR(EMLINK);
2883 * Reserve disk quota and the inode.
2885 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2890 (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
2893 * create the directory inode.
2895 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 2,
2896 0, credp, prid, resblks > 0,
2899 if (error == ENOSPC)
2906 * Now we add the directory inode to the transaction.
2907 * We waited until now since xfs_dir_ialloc might start
2908 * a new transaction. Had we joined the transaction
2909 * earlier, the locks might have gotten released.
2912 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2913 dp_joined_to_trans = B_TRUE;
2915 XFS_BMAP_INIT(&free_list, &first_block);
2917 error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2918 &first_block, &free_list, resblks ?
2919 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2921 ASSERT(error != ENOSPC);
2924 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2927 * Bump the in memory version number of the parent directory
2928 * so that other processes accessing it will recognize that
2929 * the directory has changed.
2933 error = xfs_dir_init(tp, cdp, dp);
2938 error = xfs_bumplink(tp, dp);
2942 cvp = XFS_ITOV(cdp);
2950 * Attach the dquots to the new inode and modify the icount incore.
2952 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2955 * If this is a synchronous mount, make sure that the
2956 * mkdir transaction goes to disk before returning to
2959 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2960 xfs_trans_set_sync(tp);
2963 error = xfs_bmap_finish(&tp, &free_list, &committed);
2969 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2970 XFS_QM_DQRELE(mp, udqp);
2971 XFS_QM_DQRELE(mp, gdqp);
2976 /* Fall through to std_return with error = 0 or errno from
2977 * xfs_trans_commit. */
2980 if ( (created || (error != 0 && dm_event_sent != 0)) &&
2981 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2982 DM_EVENT_POSTCREATE)) {
2983 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2984 dir_vp, DM_RIGHT_NULL,
2985 created ? XFS_ITOV(cdp):NULL,
2988 dm_di_mode, error, 0);
2994 xfs_bmap_cancel(&free_list);
2996 cancel_flags |= XFS_TRANS_ABORT;
2998 xfs_trans_cancel(tp, cancel_flags);
2999 XFS_QM_DQRELE(mp, udqp);
3000 XFS_QM_DQRELE(mp, gdqp);
3002 if (!dp_joined_to_trans && (dp != NULL)) {
3003 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3016 bhv_desc_t *dir_bdp,
3017 bhv_vname_t *dentry,
3020 char *name = VNAME(dentry);
3022 xfs_inode_t *cdp; /* child directory */
3026 xfs_bmap_free_t free_list;
3027 xfs_fsblock_t first_block;
3030 bhv_vnode_t *dir_vp;
3031 int dm_di_mode = S_IFDIR;
3036 dir_vp = BHV_TO_VNODE(dir_bdp);
3037 dp = XFS_BHVTOI(dir_bdp);
3040 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3042 if (XFS_FORCED_SHUTDOWN(XFS_BHVTOI(dir_bdp)->i_mount))
3043 return XFS_ERROR(EIO);
3044 namelen = VNAMELEN(dentry);
3046 if (!xfs_get_dir_entry(dentry, &cdp)) {
3047 dm_di_mode = cdp->i_d.di_mode;
3051 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
3052 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
3053 dir_vp, DM_RIGHT_NULL,
3054 NULL, DM_RIGHT_NULL,
3055 name, NULL, dm_di_mode, 0, 0);
3057 return XFS_ERROR(error);
3060 /* Return through std_return after this point. */
3065 * We need to get a reference to cdp before we get our log
3066 * reservation. The reason for this is that we cannot call
3067 * xfs_iget for an inode for which we do not have a reference
3068 * once we've acquired a log reservation. This is because the
3069 * inode we are trying to get might be in xfs_inactive going
3070 * for a log reservation. Since we'll have to wait for the
3071 * inactive code to complete before returning from xfs_iget,
3072 * we need to make sure that we don't have log space reserved
3073 * when we call xfs_iget. Instead we get an unlocked reference
3074 * to the inode before getting our log reservation.
3076 error = xfs_get_dir_entry(dentry, &cdp);
3078 REMOVE_DEBUG_TRACE(__LINE__);
3082 dm_di_mode = cdp->i_d.di_mode;
3085 * Get the dquots for the inodes.
3087 error = XFS_QM_DQATTACH(mp, dp, 0);
3088 if (!error && dp != cdp)
3089 error = XFS_QM_DQATTACH(mp, cdp, 0);
3092 REMOVE_DEBUG_TRACE(__LINE__);
3096 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
3097 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3099 * We try to get the real space reservation first,
3100 * allowing for directory btree deletion(s) implying
3101 * possible bmap insert(s). If we can't get the space
3102 * reservation then we use 0 instead, and avoid the bmap
3103 * btree insert(s) in the directory code by, if the bmap
3104 * insert tries to happen, instead trimming the LAST
3105 * block from the directory.
3107 resblks = XFS_REMOVE_SPACE_RES(mp);
3108 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
3109 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3110 if (error == ENOSPC) {
3112 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
3113 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3116 ASSERT(error != ENOSPC);
3121 XFS_BMAP_INIT(&free_list, &first_block);
3124 * Now lock the child directory inode and the parent directory
3125 * inode in the proper order. This will take care of validating
3126 * that the directory entry for the child directory inode has
3127 * not changed while we were obtaining a log reservation.
3129 error = xfs_lock_dir_and_entry(dp, cdp);
3131 xfs_trans_cancel(tp, cancel_flags);
3136 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3139 * Only increment the parent directory vnode count if
3140 * we didn't bump it in looking up cdp. The only time
3141 * we don't bump it is when we're looking up ".".
3147 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3149 ASSERT(cdp->i_d.di_nlink >= 2);
3150 if (cdp->i_d.di_nlink != 2) {
3151 error = XFS_ERROR(ENOTEMPTY);
3154 if (!xfs_dir_isempty(cdp)) {
3155 error = XFS_ERROR(ENOTEMPTY);
3159 error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
3160 &first_block, &free_list, resblks);
3164 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3167 * Bump the in memory generation count on the parent
3168 * directory so that other can know that it has changed.
3173 * Drop the link from cdp's "..".
3175 error = xfs_droplink(tp, dp);
3181 * Drop the link from dp to cdp.
3183 error = xfs_droplink(tp, cdp);
3189 * Drop the "." link from cdp to self.
3191 error = xfs_droplink(tp, cdp);
3196 /* Determine these before committing transaction */
3197 last_cdp_link = (cdp)->i_d.di_nlink==0;
3200 * Take an extra ref on the child vnode so that it
3201 * does not go to xfs_inactive() from within the commit.
3206 * If this is a synchronous mount, make sure that the
3207 * rmdir transaction goes to disk before returning to
3210 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3211 xfs_trans_set_sync(tp);
3214 error = xfs_bmap_finish (&tp, &free_list, &committed);
3216 xfs_bmap_cancel(&free_list);
3217 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3223 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3231 * Let interposed file systems know about removed links.
3233 bhv_vop_link_removed(XFS_ITOV(cdp), dir_vp, last_cdp_link);
3237 /* Fall through to std_return with error = 0 or the errno
3238 * from xfs_trans_commit. */
3240 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_POSTREMOVE)) {
3241 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3242 dir_vp, DM_RIGHT_NULL,
3243 NULL, DM_RIGHT_NULL,
3244 name, NULL, dm_di_mode,
3250 xfs_bmap_cancel(&free_list);
3251 cancel_flags |= XFS_TRANS_ABORT;
3255 xfs_trans_cancel(tp, cancel_flags);
3261 * Read dp's entries starting at uiop->uio_offset and translate them into
3262 * bufsize bytes worth of struct dirents starting at bufbase.
3266 bhv_desc_t *dir_bdp,
3272 xfs_trans_t *tp = NULL;
3276 vn_trace_entry(BHV_TO_VNODE(dir_bdp), __FUNCTION__,
3277 (inst_t *)__return_address);
3278 dp = XFS_BHVTOI(dir_bdp);
3280 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
3281 return XFS_ERROR(EIO);
3283 lock_mode = xfs_ilock_map_shared(dp);
3284 error = xfs_dir_getdents(tp, dp, uiop, eofp);
3285 xfs_iunlock_map_shared(dp, lock_mode);
3292 bhv_desc_t *dir_bdp,
3293 bhv_vname_t *dentry,
3305 xfs_bmap_free_t free_list;
3306 xfs_fsblock_t first_block;
3307 boolean_t dp_joined_to_trans;
3308 bhv_vnode_t *dir_vp;
3311 xfs_fileoff_t first_fsb;
3312 xfs_filblks_t fs_blocks;
3314 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3321 struct xfs_dquot *udqp, *gdqp;
3323 char *link_name = VNAME(dentry);
3327 dir_vp = BHV_TO_VNODE(dir_bdp);
3328 dp = XFS_BHVTOI(dir_bdp);
3329 dp_joined_to_trans = B_FALSE;
3334 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3338 if (XFS_FORCED_SHUTDOWN(mp))
3339 return XFS_ERROR(EIO);
3341 link_namelen = VNAMELEN(dentry);
3344 * Check component lengths of the target path name.
3346 pathlen = strlen(target_path);
3347 if (pathlen >= MAXPATHLEN) /* total string too long */
3348 return XFS_ERROR(ENAMETOOLONG);
3349 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3353 for (total = 0, path = target_path; total < pathlen;) {
3357 while(*path == '/') {
3363 * Count up to the next slash or end of path.
3364 * Error out if the component is bigger than MAXNAMELEN.
3366 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3367 if (++len >= MAXNAMELEN) {
3368 error = ENAMETOOLONG;
3375 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_SYMLINK)) {
3376 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3377 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3378 link_name, target_path, 0, 0, 0);
3383 /* Return through std_return after this point. */
3386 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3387 prid = dp->i_d.di_projid;
3388 else if (vap->va_mask & XFS_AT_PROJID)
3389 prid = (xfs_prid_t)vap->va_projid;
3391 prid = (xfs_prid_t)dfltprid;
3394 * Make sure that we have allocated dquot(s) on disk.
3396 error = XFS_QM_DQVOPALLOC(mp, dp,
3397 current_fsuid(credp), current_fsgid(credp), prid,
3398 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3402 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3403 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3405 * The symlink will fit into the inode data fork?
3406 * There can't be any attributes so we get the whole variable part.
3408 if (pathlen <= XFS_LITINO(mp))
3411 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3412 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3413 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3414 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3415 if (error == ENOSPC && fs_blocks == 0) {
3417 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3418 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3426 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
3429 * Check whether the directory allows new symlinks or not.
3431 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3432 error = XFS_ERROR(EPERM);
3437 * Reserve disk quota : blocks and inode.
3439 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3444 * Check for ability to enter directory entry, if no space reserved.
3447 (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
3450 * Initialize the bmap freelist prior to calling either
3451 * bmapi or the directory create code.
3453 XFS_BMAP_INIT(&free_list, &first_block);
3456 * Allocate an inode for the symlink.
3458 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (vap->va_mode&~S_IFMT),
3459 1, 0, credp, prid, resblks > 0, &ip, NULL);
3461 if (error == ENOSPC)
3468 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3469 dp_joined_to_trans = B_TRUE;
3472 * Also attach the dquot(s) to it, if applicable.
3474 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3477 resblks -= XFS_IALLOC_SPACE_RES(mp);
3479 * If the symlink will fit into the inode, write it inline.
3481 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3482 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3483 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3484 ip->i_d.di_size = pathlen;
3487 * The inode was initially created in extent format.
3489 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3490 ip->i_df.if_flags |= XFS_IFINLINE;
3492 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3493 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3497 nmaps = SYMLINK_MAPS;
3499 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3500 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3501 &first_block, resblks, mval, &nmaps,
3508 resblks -= fs_blocks;
3509 ip->i_d.di_size = pathlen;
3510 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3512 cur_chunk = target_path;
3513 for (n = 0; n < nmaps; n++) {
3514 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3515 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3516 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3517 BTOBB(byte_cnt), 0);
3518 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3519 if (pathlen < byte_cnt) {
3522 pathlen -= byte_cnt;
3524 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3525 cur_chunk += byte_cnt;
3527 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3532 * Create the directory entry for the symlink.
3534 error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
3535 &first_block, &free_list, resblks);
3538 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3539 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3542 * Bump the in memory version number of the parent directory
3543 * so that other processes accessing it will recognize that
3544 * the directory has changed.
3549 * If this is a synchronous mount, make sure that the
3550 * symlink transaction goes to disk before returning to
3553 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3554 xfs_trans_set_sync(tp);
3558 * xfs_trans_commit normally decrements the vnode ref count
3559 * when it unlocks the inode. Since we want to return the
3560 * vnode to the caller, we bump the vnode ref count now.
3564 error = xfs_bmap_finish(&tp, &free_list, &committed);
3568 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3569 XFS_QM_DQRELE(mp, udqp);
3570 XFS_QM_DQRELE(mp, gdqp);
3572 /* Fall through to std_return with error = 0 or errno from
3573 * xfs_trans_commit */
3575 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
3576 DM_EVENT_POSTSYMLINK)) {
3577 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3578 dir_vp, DM_RIGHT_NULL,
3579 error ? NULL : XFS_ITOV(ip),
3580 DM_RIGHT_NULL, link_name, target_path,
3596 xfs_bmap_cancel(&free_list);
3597 cancel_flags |= XFS_TRANS_ABORT;
3599 xfs_trans_cancel(tp, cancel_flags);
3600 XFS_QM_DQRELE(mp, udqp);
3601 XFS_QM_DQRELE(mp, gdqp);
3603 if (!dp_joined_to_trans && (dp != NULL)) {
3604 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3614 * A fid routine that takes a pointer to a previously allocated
3615 * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
3625 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
3626 (inst_t *)__return_address);
3627 ASSERT(sizeof(fid_t) >= sizeof(xfs_fid2_t));
3629 xfid = (xfs_fid2_t *)fidp;
3630 ip = XFS_BHVTOI(bdp);
3631 xfid->fid_len = sizeof(xfs_fid2_t) - sizeof(xfid->fid_len);
3634 * use memcpy because the inode is a long long and there's no
3635 * assurance that xfid->fid_ino is properly aligned.
3637 memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
3638 xfid->fid_gen = ip->i_d.di_gen;
3650 bhv_vrwlock_t locktype)
3655 vp = BHV_TO_VNODE(bdp);
3658 ip = XFS_BHVTOI(bdp);
3659 if (locktype == VRWLOCK_WRITE) {
3660 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3661 } else if (locktype == VRWLOCK_TRY_READ) {
3662 return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
3663 } else if (locktype == VRWLOCK_TRY_WRITE) {
3664 return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
3666 ASSERT((locktype == VRWLOCK_READ) ||
3667 (locktype == VRWLOCK_WRITE_DIRECT));
3668 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3681 bhv_vrwlock_t locktype)
3686 vp = BHV_TO_VNODE(bdp);
3689 ip = XFS_BHVTOI(bdp);
3690 if (locktype == VRWLOCK_WRITE) {
3692 * In the write case, we may have added a new entry to
3693 * the reference cache. This might store a pointer to
3694 * an inode to be released in this inode. If it is there,
3695 * clear the pointer and release the inode after unlocking
3698 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3700 ASSERT((locktype == VRWLOCK_READ) ||
3701 (locktype == VRWLOCK_WRITE_DIRECT));
3702 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3714 xfs_inode_log_item_t *iip;
3717 ip = XFS_BHVTOI(bdp);
3721 if (XFS_FORCED_SHUTDOWN(mp))
3722 return XFS_ERROR(EIO);
3725 * Bypass inodes which have already been cleaned by
3726 * the inode flush clustering code inside xfs_iflush
3728 if ((ip->i_update_core == 0) &&
3729 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3732 if (flags & FLUSH_LOG) {
3733 if (iip && iip->ili_last_lsn) {
3734 xlog_t *log = mp->m_log;
3736 int s, log_flags = XFS_LOG_FORCE;
3738 s = GRANT_LOCK(log);
3739 sync_lsn = log->l_last_sync_lsn;
3740 GRANT_UNLOCK(log, s);
3742 if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) <= 0))
3745 if (flags & FLUSH_SYNC)
3746 log_flags |= XFS_LOG_SYNC;
3747 return xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3752 * We make this non-blocking if the inode is contended,
3753 * return EAGAIN to indicate to the caller that they
3754 * did not succeed. This prevents the flush path from
3755 * blocking on inodes inside another operation right
3756 * now, they get caught later by xfs_sync.
3758 if (flags & FLUSH_INODE) {
3761 if (xfs_ipincount(ip))
3764 if (flags & FLUSH_SYNC) {
3765 xfs_ilock(ip, XFS_ILOCK_SHARED);
3767 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3768 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3769 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3776 if (flags & FLUSH_SYNC)
3777 flush_flags = XFS_IFLUSH_SYNC;
3779 flush_flags = XFS_IFLUSH_ASYNC;
3781 error = xfs_iflush(ip, flush_flags);
3782 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3800 if (!capable(CAP_SYS_ADMIN))
3801 return XFS_ERROR(EPERM);
3803 ip = XFS_BHVTOI(bdp);
3806 if (XFS_FORCED_SHUTDOWN(mp))
3807 return XFS_ERROR(EIO);
3809 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3810 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3812 xfs_trans_cancel(tp, 0);
3815 xfs_ilock(ip, XFS_ILOCK_EXCL);
3816 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3818 ip->i_iocore.io_dmevmask = ip->i_d.di_dmevmask = evmask;
3819 ip->i_iocore.io_dmstate = ip->i_d.di_dmstate = state;
3821 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3823 error = xfs_trans_commit(tp, 0);
3835 vp = BHV_TO_VNODE(bdp);
3836 ip = XFS_BHVTOI(bdp);
3838 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
3840 ASSERT(!VN_MAPPED(vp));
3842 /* bad inode, get out here ASAP */
3850 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
3853 * Make sure the atime in the XFS inode is correct before freeing the
3856 xfs_synchronize_atime(ip);
3859 * If we have nothing to flush with this inode then complete the
3860 * teardown now, otherwise break the link between the xfs inode and the
3861 * linux inode and clean up the xfs inode later. This avoids flushing
3862 * the inode to disk during the delete operation itself.
3864 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3865 * first to ensure that xfs_iunpin() will never see an xfs inode
3866 * that has a linux inode being reclaimed. Synchronisation is provided
3867 * by the i_flags_lock.
3869 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3870 xfs_ilock(ip, XFS_ILOCK_EXCL);
3872 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3874 xfs_mount_t *mp = ip->i_mount;
3876 /* Protect sync and unpin from us */
3877 XFS_MOUNT_ILOCK(mp);
3878 spin_lock(&ip->i_flags_lock);
3879 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
3880 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
3881 spin_unlock(&ip->i_flags_lock);
3882 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
3883 XFS_MOUNT_IUNLOCK(mp);
3894 xfs_ihash_t *ih = ip->i_hash;
3895 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
3898 if (vp && VN_BAD(vp))
3901 /* The hash lock here protects a thread in xfs_iget_core from
3902 * racing with us on linking the inode back with a vnode.
3903 * Once we have the XFS_IRECLAIM flag set it will not touch
3906 write_lock(&ih->ih_lock);
3907 spin_lock(&ip->i_flags_lock);
3908 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3909 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
3910 spin_unlock(&ip->i_flags_lock);
3911 write_unlock(&ih->ih_lock);
3914 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3918 __xfs_iflags_set(ip, XFS_IRECLAIM);
3919 spin_unlock(&ip->i_flags_lock);
3920 write_unlock(&ih->ih_lock);
3923 * If the inode is still dirty, then flush it out. If the inode
3924 * is not in the AIL, then it will be OK to flush it delwri as
3925 * long as xfs_iflush() does not keep any references to the inode.
3926 * We leave that decision up to xfs_iflush() since it has the
3927 * knowledge of whether it's OK to simply do a delwri flush of
3928 * the inode or whether we need to wait until the inode is
3929 * pulled from the AIL.
3930 * We get the flush lock regardless, though, just to make sure
3931 * we don't free it while it is being flushed.
3933 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3935 xfs_ilock(ip, XFS_ILOCK_EXCL);
3939 if (ip->i_update_core ||
3940 ((ip->i_itemp != NULL) &&
3941 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3942 error = xfs_iflush(ip, sync_mode);
3944 * If we hit an error, typically because of filesystem
3945 * shutdown, we don't need to let vn_reclaim to know
3946 * because we're gonna reclaim the inode anyway.
3949 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3952 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3955 ASSERT(ip->i_update_core == 0);
3956 ASSERT(ip->i_itemp == NULL ||
3957 ip->i_itemp->ili_format.ilf_fields == 0);
3958 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3959 } else if (locked) {
3961 * We are not interested in doing an iflush if we're
3962 * in the process of shutting down the filesystem forcibly.
3963 * So, just reclaim the inode.
3966 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3975 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3978 xfs_inode_t *ip, *n;
3983 XFS_MOUNT_ILOCK(mp);
3984 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3986 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3988 if (xfs_ipincount(ip) ||
3989 !xfs_iflock_nowait(ip)) {
3990 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3994 XFS_MOUNT_IUNLOCK(mp);
3995 if (xfs_finish_reclaim(ip, noblock,
3996 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
4005 XFS_MOUNT_IUNLOCK(mp);
4010 * xfs_alloc_file_space()
4011 * This routine allocates disk space for the given file.
4013 * If alloc_type == 0, this request is for an ALLOCSP type
4014 * request which will change the file size. In this case, no
4015 * DMAPI event will be generated by the call. A TRUNCATE event
4016 * will be generated later by xfs_setattr.
4018 * If alloc_type != 0, this request is for a RESVSP type
4019 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
4020 * lower block boundary byte address is less than the file's
4029 xfs_alloc_file_space(
4036 xfs_mount_t *mp = ip->i_mount;
4038 xfs_filblks_t allocated_fsb;
4039 xfs_filblks_t allocatesize_fsb;
4040 xfs_extlen_t extsz, temp;
4041 xfs_fileoff_t startoffset_fsb;
4042 xfs_fsblock_t firstfsb;
4048 xfs_bmbt_irec_t imaps[1], *imapp;
4049 xfs_bmap_free_t free_list;
4050 uint qblocks, resblks, resrtextents;
4054 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
4056 if (XFS_FORCED_SHUTDOWN(mp))
4057 return XFS_ERROR(EIO);
4059 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4063 return XFS_ERROR(EINVAL);
4065 rt = XFS_IS_REALTIME_INODE(ip);
4066 extsz = xfs_get_extsz_hint(ip);
4071 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
4072 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
4073 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
4075 /* Generate a DMAPI event if needed. */
4076 if (alloc_type != 0 && offset < ip->i_size &&
4077 (attr_flags&ATTR_DMI) == 0 &&
4078 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4079 xfs_off_t end_dmi_offset;
4081 end_dmi_offset = offset+len;
4082 if (end_dmi_offset > ip->i_size)
4083 end_dmi_offset = ip->i_size;
4084 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
4085 offset, end_dmi_offset - offset,
4092 * Allocate file space until done or until there is an error
4095 while (allocatesize_fsb && !error) {
4099 * Determine space reservations for data/realtime.
4101 if (unlikely(extsz)) {
4102 s = startoffset_fsb;
4105 e = startoffset_fsb + allocatesize_fsb;
4106 if ((temp = do_mod(startoffset_fsb, extsz)))
4108 if ((temp = do_mod(e, extsz)))
4112 e = allocatesize_fsb;
4116 resrtextents = qblocks = (uint)(e - s);
4117 resrtextents /= mp->m_sb.sb_rextsize;
4118 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4119 quota_flag = XFS_QMOPT_RES_RTBLKS;
4122 resblks = qblocks = \
4123 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
4124 quota_flag = XFS_QMOPT_RES_REGBLKS;
4128 * Allocate and setup the transaction.
4130 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4131 error = xfs_trans_reserve(tp, resblks,
4132 XFS_WRITE_LOG_RES(mp), resrtextents,
4133 XFS_TRANS_PERM_LOG_RES,
4134 XFS_WRITE_LOG_COUNT);
4136 * Check for running out of space
4140 * Free the transaction structure.
4142 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4143 xfs_trans_cancel(tp, 0);
4146 xfs_ilock(ip, XFS_ILOCK_EXCL);
4147 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
4148 qblocks, 0, quota_flag);
4152 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4153 xfs_trans_ihold(tp, ip);
4156 * Issue the xfs_bmapi() call to allocate the blocks
4158 XFS_BMAP_INIT(&free_list, &firstfsb);
4159 error = XFS_BMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4160 allocatesize_fsb, bmapi_flag,
4161 &firstfsb, 0, imapp, &nimaps,
4168 * Complete the transaction
4170 error = xfs_bmap_finish(&tp, &free_list, &committed);
4175 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
4176 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4181 allocated_fsb = imapp->br_blockcount;
4184 error = XFS_ERROR(ENOSPC);
4188 startoffset_fsb += allocated_fsb;
4189 allocatesize_fsb -= allocated_fsb;
4192 if (error == ENOSPC && (attr_flags&ATTR_DMI) == 0 &&
4193 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_NOSPACE)) {
4195 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
4196 XFS_ITOV(ip), DM_RIGHT_NULL,
4197 XFS_ITOV(ip), DM_RIGHT_NULL,
4198 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
4200 goto retry; /* Maybe DMAPI app. has made space */
4201 /* else fall through with error from XFS_SEND_DATA */
4206 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
4207 xfs_bmap_cancel(&free_list);
4208 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
4210 error1: /* Just cancel transaction */
4211 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4212 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4213 goto dmapi_enospc_check;
4217 * Zero file bytes between startoff and endoff inclusive.
4218 * The iolock is held exclusive and no blocks are buffered.
4221 xfs_zero_remaining_bytes(
4226 xfs_bmbt_irec_t imap;
4227 xfs_fileoff_t offset_fsb;
4228 xfs_off_t lastoffset;
4231 xfs_mount_t *mp = ip->i_mount;
4235 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
4236 ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
4237 mp->m_rtdev_targp : mp->m_ddev_targp);
4239 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4240 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4242 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, offset_fsb, 1, 0,
4243 NULL, 0, &imap, &nimap, NULL, NULL);
4244 if (error || nimap < 1)
4246 ASSERT(imap.br_blockcount >= 1);
4247 ASSERT(imap.br_startoff == offset_fsb);
4248 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4249 if (lastoffset > endoff)
4250 lastoffset = endoff;
4251 if (imap.br_startblock == HOLESTARTBLOCK)
4253 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4254 if (imap.br_state == XFS_EXT_UNWRITTEN)
4257 XFS_BUF_UNWRITE(bp);
4259 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4261 if ((error = xfs_iowait(bp))) {
4262 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4263 mp, bp, XFS_BUF_ADDR(bp));
4266 memset(XFS_BUF_PTR(bp) +
4267 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4268 0, lastoffset - offset + 1);
4273 if ((error = xfs_iowait(bp))) {
4274 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4275 mp, bp, XFS_BUF_ADDR(bp));
4284 * xfs_free_file_space()
4285 * This routine frees disk space for the given file.
4287 * This routine is only called by xfs_change_file_space
4288 * for an UNRESVSP type call.
4296 xfs_free_file_space(
4305 xfs_off_t end_dmi_offset;
4306 xfs_fileoff_t endoffset_fsb;
4308 xfs_fsblock_t firstfsb;
4309 xfs_bmap_free_t free_list;
4310 xfs_bmbt_irec_t imap;
4318 xfs_fileoff_t startoffset_fsb;
4320 int need_iolock = 1;
4325 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4327 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4331 if (len <= 0) /* if nothing being freed */
4333 rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4334 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4335 end_dmi_offset = offset + len;
4336 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4338 if (offset < ip->i_size &&
4339 (attr_flags & ATTR_DMI) == 0 &&
4340 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4341 if (end_dmi_offset > ip->i_size)
4342 end_dmi_offset = ip->i_size;
4343 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
4344 offset, end_dmi_offset - offset,
4345 AT_DELAY_FLAG(attr_flags), NULL);
4350 if (attr_flags & ATTR_NOLOCK)
4353 xfs_ilock(ip, XFS_IOLOCK_EXCL);
4354 vn_iowait(vp); /* wait for the completion of any pending DIOs */
4357 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, NBPP);
4358 ioffset = offset & ~(rounding - 1);
4360 if (VN_CACHED(vp) != 0) {
4361 xfs_inval_cached_trace(&ip->i_iocore, ioffset, -1,
4362 ctooff(offtoct(ioffset)), -1);
4363 error = bhv_vop_flushinval_pages(vp, ctooff(offtoct(ioffset)),
4364 -1, FI_REMAPF_LOCKED);
4366 goto out_unlock_iolock;
4370 * Need to zero the stuff we're not freeing, on disk.
4371 * If its a realtime file & can't use unwritten extents then we
4372 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4373 * will take care of it for us.
4375 if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4377 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, startoffset_fsb,
4378 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4380 goto out_unlock_iolock;
4381 ASSERT(nimap == 0 || nimap == 1);
4382 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4385 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4386 block = imap.br_startblock;
4387 mod = do_div(block, mp->m_sb.sb_rextsize);
4389 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4392 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, endoffset_fsb - 1,
4393 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4395 goto out_unlock_iolock;
4396 ASSERT(nimap == 0 || nimap == 1);
4397 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4398 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4400 if (mod && (mod != mp->m_sb.sb_rextsize))
4401 endoffset_fsb -= mod;
4404 if ((done = (endoffset_fsb <= startoffset_fsb)))
4406 * One contiguous piece to clear
4408 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4411 * Some full blocks, possibly two pieces to clear
4413 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4414 error = xfs_zero_remaining_bytes(ip, offset,
4415 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4417 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4418 error = xfs_zero_remaining_bytes(ip,
4419 XFS_FSB_TO_B(mp, endoffset_fsb),
4424 * free file space until done or until there is an error
4426 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4427 while (!error && !done) {
4430 * allocate and setup the transaction
4432 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4433 error = xfs_trans_reserve(tp,
4435 XFS_WRITE_LOG_RES(mp),
4437 XFS_TRANS_PERM_LOG_RES,
4438 XFS_WRITE_LOG_COUNT);
4441 * check for running out of space
4445 * Free the transaction structure.
4447 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4448 xfs_trans_cancel(tp, 0);
4451 xfs_ilock(ip, XFS_ILOCK_EXCL);
4452 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4453 ip->i_udquot, ip->i_gdquot, resblks, 0,
4454 XFS_QMOPT_RES_REGBLKS);
4458 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4459 xfs_trans_ihold(tp, ip);
4462 * issue the bunmapi() call to free the blocks
4464 XFS_BMAP_INIT(&free_list, &firstfsb);
4465 error = XFS_BUNMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4466 endoffset_fsb - startoffset_fsb,
4467 0, 2, &firstfsb, &free_list, NULL, &done);
4473 * complete the transaction
4475 error = xfs_bmap_finish(&tp, &free_list, &committed);
4480 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
4481 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4486 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4490 xfs_bmap_cancel(&free_list);
4492 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4493 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4499 * xfs_change_file_space()
4500 * This routine allocates or frees disk space for the given file.
4501 * The user specified parameters are checked for alignment and size
4510 xfs_change_file_space(
4524 xfs_off_t startoffset;
4530 vp = BHV_TO_VNODE(bdp);
4531 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4533 ip = XFS_BHVTOI(bdp);
4537 * must be a regular file and have write permission
4540 return XFS_ERROR(EINVAL);
4542 xfs_ilock(ip, XFS_ILOCK_SHARED);
4544 if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
4545 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4549 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4551 switch (bf->l_whence) {
4552 case 0: /*SEEK_SET*/
4554 case 1: /*SEEK_CUR*/
4555 bf->l_start += offset;
4557 case 2: /*SEEK_END*/
4558 bf->l_start += ip->i_size;
4561 return XFS_ERROR(EINVAL);
4564 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4566 if ( (bf->l_start < 0)
4567 || (bf->l_start > XFS_MAXIOFFSET(mp))
4568 || (bf->l_start + llen < 0)
4569 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4570 return XFS_ERROR(EINVAL);
4574 startoffset = bf->l_start;
4578 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4580 * These calls do NOT zero the data space allocated to the file,
4581 * nor do they change the file size.
4583 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4585 * These calls cause the new file data to be zeroed and the file
4586 * size to be changed.
4588 setprealloc = clrprealloc = 0;
4591 case XFS_IOC_RESVSP:
4592 case XFS_IOC_RESVSP64:
4593 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4600 case XFS_IOC_UNRESVSP:
4601 case XFS_IOC_UNRESVSP64:
4602 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4607 case XFS_IOC_ALLOCSP:
4608 case XFS_IOC_ALLOCSP64:
4609 case XFS_IOC_FREESP:
4610 case XFS_IOC_FREESP64:
4611 if (startoffset > fsize) {
4612 error = xfs_alloc_file_space(ip, fsize,
4613 startoffset - fsize, 0, attr_flags);
4618 va.va_mask = XFS_AT_SIZE;
4619 va.va_size = startoffset;
4621 error = xfs_setattr(bdp, &va, attr_flags, credp);
4631 return XFS_ERROR(EINVAL);
4635 * update the inode timestamp, mode, and prealloc flag bits
4637 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4639 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4642 xfs_trans_cancel(tp, 0);
4646 xfs_ilock(ip, XFS_ILOCK_EXCL);
4648 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4649 xfs_trans_ihold(tp, ip);
4651 if ((attr_flags & ATTR_DMI) == 0) {
4652 ip->i_d.di_mode &= ~S_ISUID;
4655 * Note that we don't have to worry about mandatory
4656 * file locking being disabled here because we only
4657 * clear the S_ISGID bit if the Group execute bit is
4658 * on, but if it was on then mandatory locking wouldn't
4659 * have been enabled.
4661 if (ip->i_d.di_mode & S_IXGRP)
4662 ip->i_d.di_mode &= ~S_ISGID;
4664 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4667 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4668 else if (clrprealloc)
4669 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4671 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4672 xfs_trans_set_sync(tp);
4674 error = xfs_trans_commit(tp, 0);
4676 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4681 bhv_vnodeops_t xfs_vnodeops = {
4682 BHV_IDENTITY_INIT(VN_BHV_XFS,VNODE_POSITION_XFS),
4683 .vop_open = xfs_open,
4684 .vop_close = xfs_close,
4685 .vop_read = xfs_read,
4687 .vop_splice_read = xfs_splice_read,
4688 .vop_splice_write = xfs_splice_write,
4690 .vop_write = xfs_write,
4691 .vop_ioctl = xfs_ioctl,
4692 .vop_getattr = xfs_getattr,
4693 .vop_setattr = xfs_setattr,
4694 .vop_access = xfs_access,
4695 .vop_lookup = xfs_lookup,
4696 .vop_create = xfs_create,
4697 .vop_remove = xfs_remove,
4698 .vop_link = xfs_link,
4699 .vop_rename = xfs_rename,
4700 .vop_mkdir = xfs_mkdir,
4701 .vop_rmdir = xfs_rmdir,
4702 .vop_readdir = xfs_readdir,
4703 .vop_symlink = xfs_symlink,
4704 .vop_readlink = xfs_readlink,
4705 .vop_fsync = xfs_fsync,
4706 .vop_inactive = xfs_inactive,
4707 .vop_fid2 = xfs_fid2,
4708 .vop_rwlock = xfs_rwlock,
4709 .vop_rwunlock = xfs_rwunlock,
4710 .vop_bmap = xfs_bmap,
4711 .vop_reclaim = xfs_reclaim,
4712 .vop_attr_get = xfs_attr_get,
4713 .vop_attr_set = xfs_attr_set,
4714 .vop_attr_remove = xfs_attr_remove,
4715 .vop_attr_list = xfs_attr_list,
4716 .vop_link_removed = (vop_link_removed_t)fs_noval,
4717 .vop_vnode_change = (vop_vnode_change_t)fs_noval,
4718 .vop_tosspages = fs_tosspages,
4719 .vop_flushinval_pages = fs_flushinval_pages,
4720 .vop_flush_pages = fs_flush_pages,
4721 .vop_release = xfs_release,
4722 .vop_iflush = xfs_inode_flush,