2 * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
34 #include "xfs_types.h"
38 #include "xfs_trans.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_da_btree.h"
46 #include "xfs_bmap_btree.h"
47 #include "xfs_alloc_btree.h"
48 #include "xfs_ialloc_btree.h"
49 #include "xfs_dir_sf.h"
50 #include "xfs_dir2_sf.h"
51 #include "xfs_attr_sf.h"
52 #include "xfs_dinode.h"
53 #include "xfs_inode.h"
54 #include "xfs_inode_item.h"
55 #include "xfs_dir_leaf.h"
56 #include "xfs_itable.h"
57 #include "xfs_btree.h"
58 #include "xfs_ialloc.h"
59 #include "xfs_alloc.h"
63 #include "xfs_error.h"
64 #include "xfs_quota.h"
65 #include "xfs_utils.h"
66 #include "xfs_rtalloc.h"
67 #include "xfs_refcache.h"
68 #include "xfs_trans_space.h"
69 #include "xfs_log_priv.h"
74 * The maximum pathlen is 1024 bytes. Since the minimum file system
75 * blocksize is 512 bytes, we can get a max of 2 extents back from
78 #define SYMLINK_MAPS 2
81 * For xfs, we check that the file isn't too big to be opened by this kernel.
82 * No other open action is required for regular files. Devices are handled
83 * through the specfs file system, pipes through fifofs. Device and
84 * fifo vnodes are "wrapped" by specfs and fifofs vnodes, respectively,
85 * when a new vnode is first looked up or created.
96 vp = BHV_TO_VNODE(bdp);
99 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
100 return XFS_ERROR(EIO);
103 * If it's a directory with any blocks, read-ahead block 0
104 * as we're almost certain to have the next operation be a read there.
106 if (VN_ISDIR(vp) && ip->i_d.di_nextents > 0) {
107 mode = xfs_ilock_map_shared(ip);
108 if (ip->i_d.di_nextents > 0)
109 (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
110 xfs_iunlock(ip, mode);
130 vp = BHV_TO_VNODE(bdp);
131 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
133 ip = XFS_BHVTOI(bdp);
136 if (XFS_FORCED_SHUTDOWN(mp))
137 return XFS_ERROR(EIO);
139 if (!(flags & ATTR_LAZY))
140 xfs_ilock(ip, XFS_ILOCK_SHARED);
142 vap->va_size = ip->i_d.di_size;
143 if (vap->va_mask == XFS_AT_SIZE)
147 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
148 vap->va_nodeid = ip->i_ino;
150 vap->va_nodeid += mp->m_inoadd;
152 vap->va_nlink = ip->i_d.di_nlink;
155 * Quick exit for non-stat callers
158 ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
159 XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
163 * Copy from in-core inode.
165 vap->va_mode = ip->i_d.di_mode;
166 vap->va_uid = ip->i_d.di_uid;
167 vap->va_gid = ip->i_d.di_gid;
168 vap->va_projid = ip->i_d.di_projid;
171 * Check vnode type block/char vs. everything else.
173 switch (ip->i_d.di_mode & S_IFMT) {
176 vap->va_rdev = ip->i_df.if_u2.if_rdev;
177 vap->va_blocksize = BLKDEV_IOSIZE;
182 if (!(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
183 vap->va_blocksize = xfs_preferred_iosize(mp);
187 * If the file blocks are being allocated from a
188 * realtime partition, then return the inode's
189 * realtime extent size or the realtime volume's
192 vap->va_blocksize = ip->i_d.di_extsize ?
193 (ip->i_d.di_extsize << mp->m_sb.sb_blocklog) :
194 (mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog);
199 vap->va_atime.tv_sec = ip->i_d.di_atime.t_sec;
200 vap->va_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
201 vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
202 vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
203 vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
204 vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
207 * Exit for stat callers. See if any of the rest of the fields
208 * to be filled in are needed.
211 (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
212 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
216 * Convert di_flags to xflags.
218 vap->va_xflags = xfs_ip2xflags(ip);
221 * Exit for inode revalidate. See if any of the rest of
222 * the fields to be filled in are needed.
225 (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
226 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
229 vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
231 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
232 ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
236 (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
237 ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
238 ip->i_d.di_anextents;
240 vap->va_anextents = 0;
241 vap->va_gen = ip->i_d.di_gen;
244 if (!(flags & ATTR_LAZY))
245 xfs_iunlock(ip, XFS_ILOCK_SHARED);
271 xfs_prid_t projid=0, iprojid=0;
272 int mandlock_before, mandlock_after;
273 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
277 vp = BHV_TO_VNODE(bdp);
278 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
280 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
281 return XFS_ERROR(EROFS);
284 * Cannot set certain attributes.
287 if (mask & XFS_AT_NOSET) {
288 return XFS_ERROR(EINVAL);
291 ip = XFS_BHVTOI(bdp);
294 if (XFS_FORCED_SHUTDOWN(mp))
295 return XFS_ERROR(EIO);
298 * Timestamps do not need to be logged and hence do not
299 * need to be done within a transaction.
301 if (mask & XFS_AT_UPDTIMES) {
302 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
303 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
304 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
305 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
306 xfs_ichgtime(ip, timeflags);
310 olddquot1 = olddquot2 = NULL;
314 * If disk quotas is on, we make sure that the dquots do exist on disk,
315 * before we start any other transactions. Trying to do this later
316 * is messy. We don't care to take a readlock to look at the ids
317 * in inode here, because we can't hold it across the trans_reserve.
318 * If the IDs do change before we take the ilock, we're covered
319 * because the i_*dquot fields will get updated anyway.
321 if (XFS_IS_QUOTA_ON(mp) &&
322 (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
325 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
327 qflags |= XFS_QMOPT_UQUOTA;
329 uid = ip->i_d.di_uid;
331 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
333 qflags |= XFS_QMOPT_GQUOTA;
335 gid = ip->i_d.di_gid;
337 if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
338 projid = vap->va_projid;
339 qflags |= XFS_QMOPT_PQUOTA;
341 projid = ip->i_d.di_projid;
344 * We take a reference when we initialize udqp and gdqp,
345 * so it is important that we never blindly double trip on
346 * the same variable. See xfs_create() for an example.
348 ASSERT(udqp == NULL);
349 ASSERT(gdqp == NULL);
350 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
357 * For the other attributes, we acquire the inode lock and
358 * first do an error checking pass.
361 lock_flags = XFS_ILOCK_EXCL;
362 ASSERT(flags & ATTR_NOLOCK ? flags & ATTR_DMI : 1);
363 if (flags & ATTR_NOLOCK)
365 if (!(mask & XFS_AT_SIZE)) {
366 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
367 (mp->m_flags & XFS_MOUNT_WSYNC)) {
368 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
370 if ((code = xfs_trans_reserve(tp, 0,
371 XFS_ICHANGE_LOG_RES(mp), 0,
378 if (DM_EVENT_ENABLED (vp->v_vfsp, ip, DM_EVENT_TRUNCATE) &&
379 !(flags & ATTR_DMI)) {
380 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
381 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
382 vap->va_size, 0, dmflags, NULL);
389 lock_flags |= XFS_IOLOCK_EXCL;
392 xfs_ilock(ip, lock_flags);
394 /* boolean: are we the file owner? */
395 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
398 * Change various properties of a file.
399 * Only the owner or users with CAP_FOWNER
400 * capability may do these things.
403 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
404 XFS_AT_GID|XFS_AT_PROJID)) {
406 * CAP_FOWNER overrides the following restrictions:
408 * The user ID of the calling process must be equal
409 * to the file owner ID, except in cases where the
410 * CAP_FSETID capability is applicable.
412 if (!file_owner && !capable(CAP_FOWNER)) {
413 code = XFS_ERROR(EPERM);
418 * CAP_FSETID overrides the following restrictions:
420 * The effective user ID of the calling process shall match
421 * the file owner when setting the set-user-ID and
422 * set-group-ID bits on that file.
424 * The effective group ID or one of the supplementary group
425 * IDs of the calling process shall match the group owner of
426 * the file when setting the set-group-ID bit on that file
428 if (mask & XFS_AT_MODE) {
431 if ((vap->va_mode & S_ISUID) && !file_owner)
433 if ((vap->va_mode & S_ISGID) &&
434 !in_group_p((gid_t)ip->i_d.di_gid))
437 /* Linux allows this, Irix doesn't. */
438 if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
441 if (m && !capable(CAP_FSETID))
447 * Change file ownership. Must be the owner or privileged.
448 * If the system was configured with the "restricted_chown"
449 * option, the owner is not permitted to give away the file,
450 * and can change the group id only to a group of which he
451 * or she is a member.
453 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
455 * These IDs could have changed since we last looked at them.
456 * But, we're assured that if the ownership did change
457 * while we didn't have the inode locked, inode's dquot(s)
458 * would have changed also.
460 iuid = ip->i_d.di_uid;
461 iprojid = ip->i_d.di_projid;
462 igid = ip->i_d.di_gid;
463 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
464 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
465 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
469 * CAP_CHOWN overrides the following restrictions:
471 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
472 * shall override the restriction that a process cannot
473 * change the user ID of a file it owns and the restriction
474 * that the group ID supplied to the chown() function
475 * shall be equal to either the group ID or one of the
476 * supplementary group IDs of the calling process.
478 if (restricted_chown &&
479 (iuid != uid || (igid != gid &&
480 !in_group_p((gid_t)gid))) &&
481 !capable(CAP_CHOWN)) {
482 code = XFS_ERROR(EPERM);
486 * Do a quota reservation only if uid/projid/gid is actually
489 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
490 (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
491 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
493 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
494 capable(CAP_FOWNER) ?
495 XFS_QMOPT_FORCE_RES : 0);
496 if (code) /* out of quota */
502 * Truncate file. Must have write permission and not be a directory.
504 if (mask & XFS_AT_SIZE) {
505 /* Short circuit the truncate case for zero length files */
506 if ((vap->va_size == 0) &&
507 (ip->i_d.di_size == 0) && (ip->i_d.di_nextents == 0)) {
508 xfs_iunlock(ip, XFS_ILOCK_EXCL);
509 lock_flags &= ~XFS_ILOCK_EXCL;
510 if (mask & XFS_AT_CTIME)
511 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
517 code = XFS_ERROR(EISDIR);
519 } else if (!VN_ISREG(vp)) {
520 code = XFS_ERROR(EINVAL);
524 * Make sure that the dquots are attached to the inode.
526 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
531 * Change file access or modified times.
533 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
535 if ((flags & ATTR_UTIME) &&
536 !capable(CAP_FOWNER)) {
537 code = XFS_ERROR(EPERM);
544 * Change extent size or realtime flag.
546 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
548 * Can't change extent size if any extents are allocated.
550 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
551 (mask & XFS_AT_EXTSIZE) &&
552 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
554 code = XFS_ERROR(EINVAL); /* EFBIG? */
559 * Can't set extent size unless the file is marked, or
560 * about to be marked as a realtime file.
562 * This check will be removed when fixed size extents
563 * with buffered data writes is implemented.
566 if ((mask & XFS_AT_EXTSIZE) &&
567 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
569 (!((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
570 ((mask & XFS_AT_XFLAGS) &&
571 (vap->va_xflags & XFS_XFLAG_REALTIME))))) {
572 code = XFS_ERROR(EINVAL);
577 * Can't change realtime flag if any extents are allocated.
579 if (ip->i_d.di_nextents && (mask & XFS_AT_XFLAGS) &&
580 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) !=
581 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
582 code = XFS_ERROR(EINVAL); /* EFBIG? */
586 * Extent size must be a multiple of the appropriate block
587 * size, if set at all.
589 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
592 if ((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
593 ((mask & XFS_AT_XFLAGS) &&
594 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
595 size = mp->m_sb.sb_rextsize <<
596 mp->m_sb.sb_blocklog;
598 size = mp->m_sb.sb_blocksize;
600 if (vap->va_extsize % size) {
601 code = XFS_ERROR(EINVAL);
606 * If realtime flag is set then must have realtime data.
608 if ((mask & XFS_AT_XFLAGS) &&
609 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
610 if ((mp->m_sb.sb_rblocks == 0) ||
611 (mp->m_sb.sb_rextsize == 0) ||
612 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
613 code = XFS_ERROR(EINVAL);
619 * Can't modify an immutable/append-only file unless
620 * we have appropriate permission.
622 if ((mask & XFS_AT_XFLAGS) &&
624 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
626 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
627 !capable(CAP_LINUX_IMMUTABLE)) {
628 code = XFS_ERROR(EPERM);
634 * Now we can make the changes. Before we join the inode
635 * to the transaction, if XFS_AT_SIZE is set then take care of
636 * the part of the truncation that must be done without the
637 * inode lock. This needs to be done before joining the inode
638 * to the transaction, because the inode cannot be unlocked
639 * once it is a part of the transaction.
641 if (mask & XFS_AT_SIZE) {
643 if (vap->va_size > ip->i_d.di_size)
644 code = xfs_igrow_start(ip, vap->va_size, credp);
645 xfs_iunlock(ip, XFS_ILOCK_EXCL);
647 code = xfs_itruncate_data(ip, vap->va_size);
650 lock_flags &= ~XFS_ILOCK_EXCL;
651 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
654 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
655 if ((code = xfs_trans_reserve(tp, 0,
656 XFS_ITRUNCATE_LOG_RES(mp), 0,
657 XFS_TRANS_PERM_LOG_RES,
658 XFS_ITRUNCATE_LOG_COUNT))) {
659 xfs_trans_cancel(tp, 0);
661 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
664 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
665 xfs_ilock(ip, XFS_ILOCK_EXCL);
669 xfs_trans_ijoin(tp, ip, lock_flags);
670 xfs_trans_ihold(tp, ip);
673 /* determine whether mandatory locking mode changes */
674 mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
677 * Truncate file. Must have write permission and not be a directory.
679 if (mask & XFS_AT_SIZE) {
680 if (vap->va_size > ip->i_d.di_size) {
681 xfs_igrow_finish(tp, ip, vap->va_size,
682 !(flags & ATTR_DMI));
683 } else if ((vap->va_size <= ip->i_d.di_size) ||
684 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
686 * signal a sync transaction unless
687 * we're truncating an already unlinked
688 * file on a wsync filesystem
690 code = xfs_itruncate_finish(&tp, ip,
691 (xfs_fsize_t)vap->va_size,
693 ((ip->i_d.di_nlink != 0 ||
694 !(mp->m_flags & XFS_MOUNT_WSYNC))
701 * Have to do this even if the file's size doesn't change.
703 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
707 * Change file access modes.
709 if (mask & XFS_AT_MODE) {
710 ip->i_d.di_mode &= S_IFMT;
711 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
713 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
714 timeflags |= XFS_ICHGTIME_CHG;
718 * Change file ownership. Must be the owner or privileged.
719 * If the system was configured with the "restricted_chown"
720 * option, the owner is not permitted to give away the file,
721 * and can change the group id only to a group of which he
722 * or she is a member.
724 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
726 * CAP_FSETID overrides the following restrictions:
728 * The set-user-ID and set-group-ID bits of a file will be
729 * cleared upon successful return from chown()
731 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
732 !capable(CAP_FSETID)) {
733 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
737 * Change the ownerships and register quota modifications
738 * in the transaction.
741 if (XFS_IS_UQUOTA_ON(mp)) {
742 ASSERT(mask & XFS_AT_UID);
744 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
745 &ip->i_udquot, udqp);
747 ip->i_d.di_uid = uid;
750 if (XFS_IS_GQUOTA_ON(mp)) {
751 ASSERT(!XFS_IS_PQUOTA_ON(mp));
752 ASSERT(mask & XFS_AT_GID);
754 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
755 &ip->i_gdquot, gdqp);
757 ip->i_d.di_gid = gid;
759 if (iprojid != projid) {
760 if (XFS_IS_PQUOTA_ON(mp)) {
761 ASSERT(!XFS_IS_GQUOTA_ON(mp));
762 ASSERT(mask & XFS_AT_PROJID);
764 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
765 &ip->i_gdquot, gdqp);
767 ip->i_d.di_projid = projid;
769 * We may have to rev the inode as well as
770 * the superblock version number since projids didn't
771 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
773 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
774 xfs_bump_ino_vers2(tp, ip);
777 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
778 timeflags |= XFS_ICHGTIME_CHG;
783 * Change file access or modified times.
785 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
786 if (mask & XFS_AT_ATIME) {
787 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
788 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
789 ip->i_update_core = 1;
790 timeflags &= ~XFS_ICHGTIME_ACC;
792 if (mask & XFS_AT_MTIME) {
793 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
794 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
795 timeflags &= ~XFS_ICHGTIME_MOD;
796 timeflags |= XFS_ICHGTIME_CHG;
798 if (tp && (flags & ATTR_UTIME))
799 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
803 * Change XFS-added attributes.
805 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
806 if (mask & XFS_AT_EXTSIZE) {
808 * Converting bytes to fs blocks.
810 ip->i_d.di_extsize = vap->va_extsize >>
811 mp->m_sb.sb_blocklog;
813 if (mask & XFS_AT_XFLAGS) {
816 /* can't set PREALLOC this way, just preserve it */
817 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
818 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
819 di_flags |= XFS_DIFLAG_IMMUTABLE;
820 if (vap->va_xflags & XFS_XFLAG_APPEND)
821 di_flags |= XFS_DIFLAG_APPEND;
822 if (vap->va_xflags & XFS_XFLAG_SYNC)
823 di_flags |= XFS_DIFLAG_SYNC;
824 if (vap->va_xflags & XFS_XFLAG_NOATIME)
825 di_flags |= XFS_DIFLAG_NOATIME;
826 if (vap->va_xflags & XFS_XFLAG_NODUMP)
827 di_flags |= XFS_DIFLAG_NODUMP;
828 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
829 di_flags |= XFS_DIFLAG_PROJINHERIT;
830 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
831 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
832 di_flags |= XFS_DIFLAG_RTINHERIT;
833 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
834 di_flags |= XFS_DIFLAG_NOSYMLINKS;
836 if (vap->va_xflags & XFS_XFLAG_REALTIME) {
837 di_flags |= XFS_DIFLAG_REALTIME;
838 ip->i_iocore.io_flags |= XFS_IOCORE_RT;
840 ip->i_iocore.io_flags &= ~XFS_IOCORE_RT;
843 ip->i_d.di_flags = di_flags;
845 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
846 timeflags |= XFS_ICHGTIME_CHG;
850 * Change file inode change time only if XFS_AT_CTIME set
851 * AND we have been called by a DMI function.
854 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
855 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
856 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
857 ip->i_update_core = 1;
858 timeflags &= ~XFS_ICHGTIME_CHG;
862 * Send out timestamp changes that need to be set to the
863 * current time. Not done when called by a DMI function.
865 if (timeflags && !(flags & ATTR_DMI))
866 xfs_ichgtime(ip, timeflags);
868 XFS_STATS_INC(xs_ig_attrchg);
871 * If this is a synchronous mount, make sure that the
872 * transaction goes to disk before returning to the user.
873 * This is slightly sub-optimal in that truncates require
874 * two sync transactions instead of one for wsync filesytems.
875 * One for the truncate and one for the timestamps since we
876 * don't want to change the timestamps unless we're sure the
877 * truncate worked. Truncates are less than 1% of the laddis
878 * mix so this probably isn't worth the trouble to optimize.
882 if (mp->m_flags & XFS_MOUNT_WSYNC)
883 xfs_trans_set_sync(tp);
885 code = xfs_trans_commit(tp, commit_flags, NULL);
889 * If the (regular) file's mandatory locking mode changed, then
890 * notify the vnode. We do this under the inode lock to prevent
891 * racing calls to vop_vnode_change.
893 mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
894 if (mandlock_before != mandlock_after) {
895 VOP_VNODE_CHANGE(vp, VCHANGE_FLAGS_ENF_LOCKING,
899 xfs_iunlock(ip, lock_flags);
902 * Release any dquot(s) the inode had kept before chown.
904 XFS_QM_DQRELE(mp, olddquot1);
905 XFS_QM_DQRELE(mp, olddquot2);
906 XFS_QM_DQRELE(mp, udqp);
907 XFS_QM_DQRELE(mp, gdqp);
913 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_ATTRIBUTE) &&
914 !(flags & ATTR_DMI)) {
915 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
916 NULL, DM_RIGHT_NULL, NULL, NULL,
917 0, 0, AT_DELAY_FLAG(flags));
922 commit_flags |= XFS_TRANS_ABORT;
925 XFS_QM_DQRELE(mp, udqp);
926 XFS_QM_DQRELE(mp, gdqp);
928 xfs_trans_cancel(tp, commit_flags);
930 if (lock_flags != 0) {
931 xfs_iunlock(ip, lock_flags);
939 * Null conversion from vnode mode bits to inode mode bits, as in efs.
950 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
951 (inst_t *)__return_address);
953 ip = XFS_BHVTOI(bdp);
954 xfs_ilock(ip, XFS_ILOCK_SHARED);
955 error = xfs_iaccess(ip, mode, credp);
956 xfs_iunlock(ip, XFS_ILOCK_SHARED);
980 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
986 vp = BHV_TO_VNODE(bdp);
987 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
989 ip = XFS_BHVTOI(bdp);
992 if (XFS_FORCED_SHUTDOWN(mp))
993 return XFS_ERROR(EIO);
995 xfs_ilock(ip, XFS_ILOCK_SHARED);
997 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
999 offset = uiop->uio_offset;
1000 count = uiop->uio_resid;
1003 error = XFS_ERROR(EINVAL);
1011 if (!(ioflags & IO_INVIS)) {
1012 xfs_ichgtime(ip, XFS_ICHGTIME_ACC);
1016 * See if the symlink is stored inline.
1018 pathlen = (int)ip->i_d.di_size;
1020 if (ip->i_df.if_flags & XFS_IFINLINE) {
1021 error = uio_read(ip->i_df.if_u1.if_data, pathlen, uiop);
1025 * Symlink not inline. Call bmap to get it in.
1027 nmaps = SYMLINK_MAPS;
1029 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen),
1030 0, NULL, 0, mval, &nmaps, NULL);
1036 for (n = 0; n < nmaps; n++) {
1037 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1038 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1039 bp = xfs_buf_read(mp->m_ddev_targp, d,
1040 BTOBB(byte_cnt), 0);
1041 error = XFS_BUF_GETERROR(bp);
1043 xfs_ioerror_alert("xfs_readlink",
1044 ip->i_mount, bp, XFS_BUF_ADDR(bp));
1048 if (pathlen < byte_cnt)
1050 pathlen -= byte_cnt;
1052 error = uio_read(XFS_BUF_PTR(bp), byte_cnt, uiop);
1061 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1070 * This is called to sync the inode and its data out to disk.
1071 * We need to hold the I/O lock while flushing the data, and
1072 * the inode lock while flushing the inode. The inode lock CANNOT
1073 * be held while flushing the data, so acquire after we're done
1087 int log_flushed = 0, changed = 1;
1089 vn_trace_entry(BHV_TO_VNODE(bdp),
1090 __FUNCTION__, (inst_t *)__return_address);
1092 ip = XFS_BHVTOI(bdp);
1094 ASSERT(start >= 0 && stop >= -1);
1096 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1097 return XFS_ERROR(EIO);
1100 * We always need to make sure that the required inode state
1101 * is safe on disk. The vnode might be clean but because
1102 * of committed transactions that haven't hit the disk yet.
1103 * Likewise, there could be unflushed non-transactional
1104 * changes to the inode core that have to go to disk.
1106 * The following code depends on one assumption: that
1107 * any transaction that changes an inode logs the core
1108 * because it has to change some field in the inode core
1109 * (typically nextents or nblocks). That assumption
1110 * implies that any transactions against an inode will
1111 * catch any non-transactional updates. If inode-altering
1112 * transactions exist that violate this assumption, the
1113 * code breaks. Right now, it figures that if the involved
1114 * update_* field is clear and the inode is unpinned, the
1115 * inode is clean. Either it's been flushed or it's been
1116 * committed and the commit has hit the disk unpinning the inode.
1117 * (Note that xfs_inode_item_format() called at commit clears
1118 * the update_* fields.)
1120 xfs_ilock(ip, XFS_ILOCK_SHARED);
1122 /* If we are flushing data then we care about update_size
1123 * being set, otherwise we care about update_core
1125 if ((flag & FSYNC_DATA) ?
1126 (ip->i_update_size == 0) :
1127 (ip->i_update_core == 0)) {
1129 * Timestamps/size haven't changed since last inode
1130 * flush or inode transaction commit. That means
1131 * either nothing got written or a transaction
1132 * committed which caught the updates. If the
1133 * latter happened and the transaction hasn't
1134 * hit the disk yet, the inode will be still
1135 * be pinned. If it is, force the log.
1138 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1140 if (xfs_ipincount(ip)) {
1141 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
1143 ((flag & FSYNC_WAIT)
1144 ? XFS_LOG_SYNC : 0),
1148 * If the inode is not pinned and nothing
1149 * has changed we don't need to flush the
1157 * Kick off a transaction to log the inode
1158 * core to get the updates. Make it
1159 * sync if FSYNC_WAIT is passed in (which
1160 * is done by everybody but specfs). The
1161 * sync transaction will also force the log.
1163 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1164 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1165 if ((error = xfs_trans_reserve(tp, 0,
1166 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1168 xfs_trans_cancel(tp, 0);
1171 xfs_ilock(ip, XFS_ILOCK_EXCL);
1174 * Note - it's possible that we might have pushed
1175 * ourselves out of the way during trans_reserve
1176 * which would flush the inode. But there's no
1177 * guarantee that the inode buffer has actually
1178 * gone out yet (it's delwri). Plus the buffer
1179 * could be pinned anyway if it's part of an
1180 * inode in another recent transaction. So we
1181 * play it safe and fire off the transaction anyway.
1183 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1184 xfs_trans_ihold(tp, ip);
1185 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1186 if (flag & FSYNC_WAIT)
1187 xfs_trans_set_sync(tp);
1188 error = _xfs_trans_commit(tp, 0, NULL, &log_flushed);
1190 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1193 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1195 * If the log write didn't issue an ordered tag we need
1196 * to flush the disk cache for the data device now.
1199 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1202 * If this inode is on the RT dev we need to flush that
1205 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
1206 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1213 * This is called by xfs_inactive to free any blocks beyond eof,
1214 * when the link count isn't zero.
1217 xfs_inactive_free_eofblocks(
1223 xfs_fileoff_t end_fsb;
1224 xfs_fileoff_t last_fsb;
1225 xfs_filblks_t map_len;
1227 xfs_bmbt_irec_t imap;
1230 * Figure out if there are any blocks beyond the end
1231 * of the file. If not, then there is nothing to do.
1233 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_d.di_size));
1234 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1235 map_len = last_fsb - end_fsb;
1240 xfs_ilock(ip, XFS_ILOCK_SHARED);
1241 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
1242 NULL, 0, &imap, &nimaps, NULL);
1243 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1245 if (!error && (nimaps != 0) &&
1246 (imap.br_startblock != HOLESTARTBLOCK)) {
1248 * Attach the dquots to the inode up front.
1250 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1254 * There are blocks after the end of file.
1255 * Free them up now by truncating the file to
1258 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1261 * Do the xfs_itruncate_start() call before
1262 * reserving any log space because
1263 * itruncate_start will call into the buffer
1264 * cache and we can't
1265 * do that within a transaction.
1267 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1268 xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
1271 error = xfs_trans_reserve(tp, 0,
1272 XFS_ITRUNCATE_LOG_RES(mp),
1273 0, XFS_TRANS_PERM_LOG_RES,
1274 XFS_ITRUNCATE_LOG_COUNT);
1276 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1277 xfs_trans_cancel(tp, 0);
1278 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1282 xfs_ilock(ip, XFS_ILOCK_EXCL);
1283 xfs_trans_ijoin(tp, ip,
1286 xfs_trans_ihold(tp, ip);
1288 error = xfs_itruncate_finish(&tp, ip,
1293 * If we get an error at this point we
1294 * simply don't bother truncating the file.
1297 xfs_trans_cancel(tp,
1298 (XFS_TRANS_RELEASE_LOG_RES |
1301 error = xfs_trans_commit(tp,
1302 XFS_TRANS_RELEASE_LOG_RES,
1305 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1311 * Free a symlink that has blocks associated with it.
1314 xfs_inactive_symlink_rmt(
1322 xfs_fsblock_t first_block;
1323 xfs_bmap_free_t free_list;
1326 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1334 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1336 * We're freeing a symlink that has some
1337 * blocks allocated to it. Free the
1338 * blocks here. We know that we've got
1339 * either 1 or 2 extents and that we can
1340 * free them all in one bunmapi call.
1342 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1343 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1344 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1345 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1346 xfs_trans_cancel(tp, 0);
1351 * Lock the inode, fix the size, and join it to the transaction.
1352 * Hold it so in the normal path, we still have it locked for
1353 * the second transaction. In the error paths we need it
1354 * held so the cancel won't rele it, see below.
1356 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1357 size = (int)ip->i_d.di_size;
1358 ip->i_d.di_size = 0;
1359 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1360 xfs_trans_ihold(tp, ip);
1361 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1363 * Find the block(s) so we can inval and unmap them.
1366 XFS_BMAP_INIT(&free_list, &first_block);
1367 nmaps = sizeof(mval) / sizeof(mval[0]);
1368 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1369 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
1373 * Invalidate the block(s).
1375 for (i = 0; i < nmaps; i++) {
1376 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1377 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1378 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1379 xfs_trans_binval(tp, bp);
1382 * Unmap the dead block(s) to the free_list.
1384 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
1385 &first_block, &free_list, &done)))
1389 * Commit the first transaction. This logs the EFI and the inode.
1391 if ((error = xfs_bmap_finish(&tp, &free_list, first_block, &committed)))
1394 * The transaction must have been committed, since there were
1395 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1396 * The new tp has the extent freeing and EFDs.
1400 * The first xact was committed, so add the inode to the new one.
1401 * Mark it dirty so it will be logged and moved forward in the log as
1402 * part of every commit.
1404 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1405 xfs_trans_ihold(tp, ip);
1406 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1408 * Get a new, empty transaction to return to our caller.
1410 ntp = xfs_trans_dup(tp);
1412 * Commit the transaction containing extent freeing and EFD's.
1413 * If we get an error on the commit here or on the reserve below,
1414 * we need to unlock the inode since the new transaction doesn't
1415 * have the inode attached.
1417 error = xfs_trans_commit(tp, 0, NULL);
1420 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1424 * Remove the memory for extent descriptions (just bookkeeping).
1426 if (ip->i_df.if_bytes)
1427 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1428 ASSERT(ip->i_df.if_bytes == 0);
1430 * Put an itruncate log reservation in the new transaction
1433 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1434 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1435 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1439 * Return with the inode locked but not joined to the transaction.
1445 xfs_bmap_cancel(&free_list);
1448 * Have to come here with the inode locked and either
1449 * (held and in the transaction) or (not in the transaction).
1450 * If the inode isn't held then cancel would iput it, but
1451 * that's wrong since this is inactive and the vnode ref
1452 * count is 0 already.
1453 * Cancel won't do anything to the inode if held, but it still
1454 * needs to be locked until the cancel is done, if it was
1455 * joined to the transaction.
1457 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1458 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1465 xfs_inactive_symlink_local(
1471 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1473 * We're freeing a symlink which fit into
1474 * the inode. Just free the memory used
1475 * to hold the old symlink.
1477 error = xfs_trans_reserve(*tpp, 0,
1478 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1479 0, XFS_TRANS_PERM_LOG_RES,
1480 XFS_ITRUNCATE_LOG_COUNT);
1483 xfs_trans_cancel(*tpp, 0);
1487 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1490 * Zero length symlinks _can_ exist.
1492 if (ip->i_df.if_bytes > 0) {
1493 xfs_idata_realloc(ip,
1494 -(ip->i_df.if_bytes),
1496 ASSERT(ip->i_df.if_bytes == 0);
1513 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1516 ASSERT(ip->i_d.di_forkoff != 0);
1517 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
1518 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1520 error = xfs_attr_inactive(ip);
1523 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1524 return (error); /* goto out*/
1527 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1528 error = xfs_trans_reserve(tp, 0,
1529 XFS_IFREE_LOG_RES(mp),
1530 0, XFS_TRANS_PERM_LOG_RES,
1531 XFS_INACTIVE_LOG_COUNT);
1533 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1534 xfs_trans_cancel(tp, 0);
1536 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1540 xfs_ilock(ip, XFS_ILOCK_EXCL);
1541 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1542 xfs_trans_ihold(tp, ip);
1543 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1545 ASSERT(ip->i_d.di_anextents == 0);
1560 vp = BHV_TO_VNODE(bdp);
1561 ip = XFS_BHVTOI(bdp);
1563 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0)) {
1567 /* If this is a read-only mount, don't do this (would generate I/O) */
1568 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1571 #ifdef HAVE_REFCACHE
1572 /* If we are in the NFS reference cache then don't do this now */
1579 if (ip->i_d.di_nlink != 0) {
1580 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1581 ((ip->i_d.di_size > 0) || (VN_CACHED(vp) > 0)) &&
1582 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1583 (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)))) {
1584 if ((error = xfs_inactive_free_eofblocks(mp, ip)))
1586 /* Update linux inode block count after free above */
1587 LINVFS_GET_IP(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1588 ip->i_d.di_nblocks + ip->i_delayed_blks);
1598 * This is called when the vnode reference count for the vnode
1599 * goes to zero. If the file has been unlinked, then it must
1600 * now be truncated. Also, we clear all of the read-ahead state
1601 * kept for the inode here since the file is now closed.
1610 xfs_bmap_free_t free_list;
1611 xfs_fsblock_t first_block;
1618 vp = BHV_TO_VNODE(bdp);
1619 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1621 ip = XFS_BHVTOI(bdp);
1624 * If the inode is already free, then there can be nothing
1627 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1628 ASSERT(ip->i_df.if_real_bytes == 0);
1629 ASSERT(ip->i_df.if_broot_bytes == 0);
1630 return VN_INACTIVE_CACHE;
1634 * Only do a truncate if it's a regular file with
1635 * some actual space in it. It's OK to look at the
1636 * inode's fields without the lock because we're the
1637 * only one with a reference to the inode.
1639 truncate = ((ip->i_d.di_nlink == 0) &&
1640 ((ip->i_d.di_size != 0) || (ip->i_d.di_nextents > 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_d.di_size > 0) || (VN_CACHED(vp) > 0)) &&
1659 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1660 (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)) ||
1661 (ip->i_delayed_blks != 0))) {
1662 if ((error = xfs_inactive_free_eofblocks(mp, ip)))
1663 return (VN_INACTIVE_CACHE);
1664 /* Update linux inode block count after free above */
1665 LINVFS_GET_IP(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1666 ip->i_d.di_nblocks + ip->i_delayed_blks);
1671 ASSERT(ip->i_d.di_nlink == 0);
1673 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1674 return (VN_INACTIVE_CACHE);
1676 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1679 * Do the xfs_itruncate_start() call before
1680 * reserving any log space because itruncate_start
1681 * will call into the buffer cache and we can't
1682 * do that within a transaction.
1684 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1686 xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1688 error = xfs_trans_reserve(tp, 0,
1689 XFS_ITRUNCATE_LOG_RES(mp),
1690 0, XFS_TRANS_PERM_LOG_RES,
1691 XFS_ITRUNCATE_LOG_COUNT);
1693 /* Don't call itruncate_cleanup */
1694 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1695 xfs_trans_cancel(tp, 0);
1696 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1697 return (VN_INACTIVE_CACHE);
1700 xfs_ilock(ip, XFS_ILOCK_EXCL);
1701 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1702 xfs_trans_ihold(tp, ip);
1705 * normally, we have to run xfs_itruncate_finish sync.
1706 * But if filesystem is wsync and we're in the inactive
1707 * path, then we know that nlink == 0, and that the
1708 * xaction that made nlink == 0 is permanently committed
1709 * since xfs_remove runs as a synchronous transaction.
1711 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1712 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1715 xfs_trans_cancel(tp,
1716 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1717 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1718 return (VN_INACTIVE_CACHE);
1720 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1723 * If we get an error while cleaning up a
1724 * symlink we bail out.
1726 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1727 xfs_inactive_symlink_rmt(ip, &tp) :
1728 xfs_inactive_symlink_local(ip, &tp);
1732 return (VN_INACTIVE_CACHE);
1735 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1736 xfs_trans_ihold(tp, ip);
1738 error = xfs_trans_reserve(tp, 0,
1739 XFS_IFREE_LOG_RES(mp),
1740 0, XFS_TRANS_PERM_LOG_RES,
1741 XFS_INACTIVE_LOG_COUNT);
1743 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1744 xfs_trans_cancel(tp, 0);
1745 return (VN_INACTIVE_CACHE);
1748 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1749 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1750 xfs_trans_ihold(tp, ip);
1754 * If there are attributes associated with the file
1755 * then blow them away now. The code calls a routine
1756 * that recursively deconstructs the attribute fork.
1757 * We need to just commit the current transaction
1758 * because we can't use it for xfs_attr_inactive().
1760 if (ip->i_d.di_anextents > 0) {
1761 error = xfs_inactive_attrs(ip, &tp);
1763 * If we got an error, the transaction is already
1764 * cancelled, and the inode is unlocked. Just get out.
1767 return (VN_INACTIVE_CACHE);
1768 } else if (ip->i_afp) {
1769 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1775 XFS_BMAP_INIT(&free_list, &first_block);
1776 error = xfs_ifree(tp, ip, &free_list);
1779 * If we fail to free the inode, shut down. The cancel
1780 * might do that, we need to make sure. Otherwise the
1781 * inode might be lost for a long time or forever.
1783 if (!XFS_FORCED_SHUTDOWN(mp)) {
1785 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1786 error, mp->m_fsname);
1787 xfs_force_shutdown(mp, XFS_METADATA_IO_ERROR);
1789 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1792 * Credit the quota account(s). The inode is gone.
1794 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1797 * Just ignore errors at this point. There is
1798 * nothing we can do except to try to keep going.
1800 (void) xfs_bmap_finish(&tp, &free_list, first_block,
1802 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
1805 * Release the dquots held by inode, if any.
1807 XFS_QM_DQDETACH(mp, ip);
1809 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1812 return VN_INACTIVE_CACHE;
1821 bhv_desc_t *dir_bdp,
1828 xfs_inode_t *dp, *ip;
1834 dir_vp = BHV_TO_VNODE(dir_bdp);
1835 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1837 dp = XFS_BHVTOI(dir_bdp);
1839 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1840 return XFS_ERROR(EIO);
1842 lock_mode = xfs_ilock_map_shared(dp);
1843 error = xfs_dir_lookup_int(dir_bdp, lock_mode, dentry, &e_inum, &ip);
1845 *vpp = XFS_ITOV(ip);
1848 xfs_iunlock_map_shared(dp, lock_mode);
1854 * xfs_create (create a new file).
1858 bhv_desc_t *dir_bdp,
1864 char *name = VNAME(dentry);
1866 xfs_inode_t *dp, *ip;
1872 xfs_bmap_free_t free_list;
1873 xfs_fsblock_t first_block;
1874 boolean_t dp_joined_to_trans;
1875 int dm_event_sent = 0;
1879 struct xfs_dquot *udqp, *gdqp;
1885 dir_vp = BHV_TO_VNODE(dir_bdp);
1886 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1888 dp = XFS_BHVTOI(dir_bdp);
1891 dm_di_mode = vap->va_mode;
1892 namelen = VNAMELEN(dentry);
1894 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
1895 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1896 dir_vp, DM_RIGHT_NULL, NULL,
1897 DM_RIGHT_NULL, name, NULL,
1905 if (XFS_FORCED_SHUTDOWN(mp))
1906 return XFS_ERROR(EIO);
1908 /* Return through std_return after this point. */
1911 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1912 prid = dp->i_d.di_projid;
1913 else if (vap->va_mask & XFS_AT_PROJID)
1914 prid = (xfs_prid_t)vap->va_projid;
1916 prid = (xfs_prid_t)dfltprid;
1919 * Make sure that we have allocated dquot(s) on disk.
1921 error = XFS_QM_DQVOPALLOC(mp, dp,
1922 current_fsuid(credp), current_fsgid(credp), prid,
1923 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1928 dp_joined_to_trans = B_FALSE;
1930 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1931 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1932 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1934 * Initially assume that the file does not exist and
1935 * reserve the resources for that case. If that is not
1936 * the case we'll drop the one we have and get a more
1937 * appropriate transaction later.
1939 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1940 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1941 if (error == ENOSPC) {
1943 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1944 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1952 xfs_ilock(dp, XFS_ILOCK_EXCL);
1954 XFS_BMAP_INIT(&free_list, &first_block);
1959 * Reserve disk quota and the inode.
1961 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1966 (error = XFS_DIR_CANENTER(mp, tp, dp, name, namelen)))
1968 rdev = (vap->va_mask & XFS_AT_RDEV) ? vap->va_rdev : 0;
1969 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 1,
1970 rdev, credp, prid, resblks > 0,
1973 if (error == ENOSPC)
1980 * At this point, we've gotten a newly allocated inode.
1981 * It is locked (and joined to the transaction).
1984 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1987 * Now we join the directory inode to the transaction.
1988 * We do not do it earlier because xfs_dir_ialloc
1989 * might commit the previous transaction (and release
1994 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1995 dp_joined_to_trans = B_TRUE;
1997 error = XFS_DIR_CREATENAME(mp, tp, dp, name, namelen, ip->i_ino,
1998 &first_block, &free_list,
1999 resblks ? resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2001 ASSERT(error != ENOSPC);
2004 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2005 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2008 * If this is a synchronous mount, make sure that the
2009 * create transaction goes to disk before returning to
2012 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2013 xfs_trans_set_sync(tp);
2019 * Attach the dquot(s) to the inodes and modify them incore.
2020 * These ids of the inode couldn't have changed since the new
2021 * inode has been locked ever since it was created.
2023 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2026 * xfs_trans_commit normally decrements the vnode ref count
2027 * when it unlocks the inode. Since we want to return the
2028 * vnode to the caller, we bump the vnode ref count now.
2033 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2035 xfs_bmap_cancel(&free_list);
2039 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2046 XFS_QM_DQRELE(mp, udqp);
2047 XFS_QM_DQRELE(mp, gdqp);
2050 * Propogate the fact that the vnode changed after the
2051 * xfs_inode locks have been released.
2053 VOP_VNODE_CHANGE(vp, VCHANGE_FLAGS_TRUNCATED, 3);
2057 /* Fallthrough to std_return with error = 0 */
2060 if ( (*vpp || (error != 0 && dm_event_sent != 0)) &&
2061 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2062 DM_EVENT_POSTCREATE)) {
2063 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2064 dir_vp, DM_RIGHT_NULL,
2066 DM_RIGHT_NULL, name, NULL,
2067 dm_di_mode, error, 0);
2072 cancel_flags |= XFS_TRANS_ABORT;
2077 xfs_trans_cancel(tp, cancel_flags);
2079 if (!dp_joined_to_trans && (dp != NULL))
2080 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2081 XFS_QM_DQRELE(mp, udqp);
2082 XFS_QM_DQRELE(mp, gdqp);
2088 * Wait until after the current transaction is aborted to
2089 * release the inode. This prevents recursive transactions
2090 * and deadlocks from xfs_inactive.
2092 cancel_flags |= XFS_TRANS_ABORT;
2093 xfs_trans_cancel(tp, cancel_flags);
2096 XFS_QM_DQRELE(mp, udqp);
2097 XFS_QM_DQRELE(mp, gdqp);
2104 * Some counters to see if (and how often) we are hitting some deadlock
2105 * prevention code paths.
2109 int xfs_rm_lock_delays;
2110 int xfs_rm_attempts;
2114 * The following routine will lock the inodes associated with the
2115 * directory and the named entry in the directory. The locks are
2116 * acquired in increasing inode number.
2118 * If the entry is "..", then only the directory is locked. The
2119 * vnode ref count will still include that from the .. entry in
2122 * There is a deadlock we need to worry about. If the locked directory is
2123 * in the AIL, it might be blocking up the log. The next inode we lock
2124 * could be already locked by another thread waiting for log space (e.g
2125 * a permanent log reservation with a long running transaction (see
2126 * xfs_itruncate_finish)). To solve this, we must check if the directory
2127 * is in the ail and use lock_nowait. If we can't lock, we need to
2128 * drop the inode lock on the directory and try again. xfs_iunlock will
2129 * potentially push the tail if we were holding up the log.
2132 xfs_lock_dir_and_entry(
2135 xfs_inode_t *ip) /* inode of entry 'name' */
2139 xfs_inode_t *ips[2];
2148 xfs_ilock(dp, XFS_ILOCK_EXCL);
2155 * We want to lock in increasing inum. Since we've already
2156 * acquired the lock on the directory, we may need to release
2157 * if if the inum of the entry turns out to be less.
2159 if (e_inum > dp->i_ino) {
2161 * We are already in the right order, so just
2162 * lock on the inode of the entry.
2163 * We need to use nowait if dp is in the AIL.
2166 lp = (xfs_log_item_t *)dp->i_itemp;
2167 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2168 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2175 * Unlock dp and try again.
2176 * xfs_iunlock will try to push the tail
2177 * if the inode is in the AIL.
2180 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2182 if ((attempts % 5) == 0) {
2183 delay(1); /* Don't just spin the CPU */
2185 xfs_rm_lock_delays++;
2191 xfs_ilock(ip, XFS_ILOCK_EXCL);
2193 } else if (e_inum < dp->i_ino) {
2194 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2198 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2200 /* else e_inum == dp->i_ino */
2201 /* This can happen if we're asked to lock /x/..
2202 * the entry is "..", which is also the parent directory.
2210 int xfs_small_retries;
2211 int xfs_middle_retries;
2212 int xfs_lots_retries;
2213 int xfs_lock_delays;
2217 * The following routine will lock n inodes in exclusive mode.
2218 * We assume the caller calls us with the inodes in i_ino order.
2220 * We need to detect deadlock where an inode that we lock
2221 * is in the AIL and we start waiting for another inode that is locked
2222 * by a thread in a long running transaction (such as truncate). This can
2223 * result in deadlock since the long running trans might need to wait
2224 * for the inode we just locked in order to push the tail and free space
2234 int attempts = 0, i, j, try_lock;
2237 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2248 for (; i < inodes; i++) {
2251 if (i && (ips[i] == ips[i-1])) /* Already locked */
2255 * If try_lock is not set yet, make sure all locked inodes
2256 * are not in the AIL.
2257 * If any are, set try_lock to be used later.
2261 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2262 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2263 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2270 * If any of the previous locks we have locked is in the AIL,
2271 * we must TRY to get the second and subsequent locks. If
2272 * we can't get any, we must release all we have
2277 /* try_lock must be 0 if i is 0. */
2279 * try_lock means we have an inode locked
2280 * that is in the AIL.
2283 if (!xfs_ilock_nowait(ips[i], lock_mode)) {
2287 * Unlock all previous guys and try again.
2288 * xfs_iunlock will try to push the tail
2289 * if the inode is in the AIL.
2292 for(j = i - 1; j >= 0; j--) {
2295 * Check to see if we've already
2296 * unlocked this one.
2297 * Not the first one going back,
2298 * and the inode ptr is the same.
2300 if ((j != (i - 1)) && ips[j] ==
2304 xfs_iunlock(ips[j], lock_mode);
2307 if ((attempts % 5) == 0) {
2308 delay(1); /* Don't just spin the CPU */
2318 xfs_ilock(ips[i], lock_mode);
2324 if (attempts < 5) xfs_small_retries++;
2325 else if (attempts < 100) xfs_middle_retries++;
2326 else xfs_lots_retries++;
2334 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2335 int remove_which_error_return = 0;
2337 #define REMOVE_DEBUG_TRACE(x)
2338 #endif /* ! DEBUG */
2347 bhv_desc_t *dir_bdp,
2352 char *name = VNAME(dentry);
2353 xfs_inode_t *dp, *ip;
2354 xfs_trans_t *tp = NULL;
2357 xfs_bmap_free_t free_list;
2358 xfs_fsblock_t first_block;
2366 dir_vp = BHV_TO_VNODE(dir_bdp);
2367 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2369 dp = XFS_BHVTOI(dir_bdp);
2372 if (XFS_FORCED_SHUTDOWN(mp))
2373 return XFS_ERROR(EIO);
2375 namelen = VNAMELEN(dentry);
2377 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
2378 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2379 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2380 name, NULL, 0, 0, 0);
2385 /* From this point on, return through std_return */
2389 * We need to get a reference to ip before we get our log
2390 * reservation. The reason for this is that we cannot call
2391 * xfs_iget for an inode for which we do not have a reference
2392 * once we've acquired a log reservation. This is because the
2393 * inode we are trying to get might be in xfs_inactive going
2394 * for a log reservation. Since we'll have to wait for the
2395 * inactive code to complete before returning from xfs_iget,
2396 * we need to make sure that we don't have log space reserved
2397 * when we call xfs_iget. Instead we get an unlocked referece
2398 * to the inode before getting our log reservation.
2400 error = xfs_get_dir_entry(dentry, &ip);
2402 REMOVE_DEBUG_TRACE(__LINE__);
2406 dm_di_mode = ip->i_d.di_mode;
2408 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2412 error = XFS_QM_DQATTACH(mp, dp, 0);
2413 if (!error && dp != ip)
2414 error = XFS_QM_DQATTACH(mp, ip, 0);
2416 REMOVE_DEBUG_TRACE(__LINE__);
2421 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2422 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2424 * We try to get the real space reservation first,
2425 * allowing for directory btree deletion(s) implying
2426 * possible bmap insert(s). If we can't get the space
2427 * reservation then we use 0 instead, and avoid the bmap
2428 * btree insert(s) in the directory code by, if the bmap
2429 * insert tries to happen, instead trimming the LAST
2430 * block from the directory.
2432 resblks = XFS_REMOVE_SPACE_RES(mp);
2433 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2434 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2435 if (error == ENOSPC) {
2437 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2438 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2441 ASSERT(error != ENOSPC);
2442 REMOVE_DEBUG_TRACE(__LINE__);
2443 xfs_trans_cancel(tp, 0);
2448 error = xfs_lock_dir_and_entry(dp, dentry, ip);
2450 REMOVE_DEBUG_TRACE(__LINE__);
2451 xfs_trans_cancel(tp, cancel_flags);
2457 * At this point, we've gotten both the directory and the entry
2460 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2463 * Increment vnode ref count only in this case since
2464 * there's an extra vnode reference in the case where
2468 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2472 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2474 XFS_BMAP_INIT(&free_list, &first_block);
2475 error = XFS_DIR_REMOVENAME(mp, tp, dp, name, namelen, ip->i_ino,
2476 &first_block, &free_list, 0);
2478 ASSERT(error != ENOENT);
2479 REMOVE_DEBUG_TRACE(__LINE__);
2482 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2485 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2487 error = xfs_droplink(tp, ip);
2489 REMOVE_DEBUG_TRACE(__LINE__);
2493 /* Determine if this is the last link while
2494 * we are in the transaction.
2496 link_zero = (ip)->i_d.di_nlink==0;
2499 * Take an extra ref on the inode so that it doesn't
2500 * go to xfs_inactive() from within the commit.
2505 * If this is a synchronous mount, make sure that the
2506 * remove transaction goes to disk before returning to
2509 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2510 xfs_trans_set_sync(tp);
2513 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2515 REMOVE_DEBUG_TRACE(__LINE__);
2519 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2526 * Before we drop our extra reference to the inode, purge it
2527 * from the refcache if it is there. By waiting until afterwards
2528 * to do the IRELE, we ensure that we won't go inactive in the
2529 * xfs_refcache_purge_ip routine (although that would be OK).
2531 xfs_refcache_purge_ip(ip);
2533 vn_trace_exit(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2536 * Let interposed file systems know about removed links.
2538 VOP_LINK_REMOVED(XFS_ITOV(ip), dir_vp, link_zero);
2542 /* Fall through to std_return with error = 0 */
2544 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp,
2545 DM_EVENT_POSTREMOVE)) {
2546 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2547 dir_vp, DM_RIGHT_NULL,
2548 NULL, DM_RIGHT_NULL,
2549 name, NULL, dm_di_mode, error, 0);
2554 xfs_bmap_cancel(&free_list);
2555 cancel_flags |= XFS_TRANS_ABORT;
2556 xfs_trans_cancel(tp, cancel_flags);
2561 * In this case make sure to not release the inode until after
2562 * the current transaction is aborted. Releasing it beforehand
2563 * can cause us to go to xfs_inactive and start a recursive
2564 * transaction which can easily deadlock with the current one.
2566 xfs_bmap_cancel(&free_list);
2567 cancel_flags |= XFS_TRANS_ABORT;
2568 xfs_trans_cancel(tp, cancel_flags);
2571 * Before we drop our extra reference to the inode, purge it
2572 * from the refcache if it is there. By waiting until afterwards
2573 * to do the IRELE, we ensure that we won't go inactive in the
2574 * xfs_refcache_purge_ip routine (although that would be OK).
2576 xfs_refcache_purge_ip(ip);
2590 bhv_desc_t *target_dir_bdp,
2595 xfs_inode_t *tdp, *sip;
2598 xfs_inode_t *ips[2];
2600 xfs_bmap_free_t free_list;
2601 xfs_fsblock_t first_block;
2604 vnode_t *target_dir_vp;
2605 bhv_desc_t *src_bdp;
2607 char *target_name = VNAME(dentry);
2610 target_dir_vp = BHV_TO_VNODE(target_dir_bdp);
2611 vn_trace_entry(target_dir_vp, __FUNCTION__, (inst_t *)__return_address);
2612 vn_trace_entry(src_vp, __FUNCTION__, (inst_t *)__return_address);
2614 target_namelen = VNAMELEN(dentry);
2615 if (VN_ISDIR(src_vp))
2616 return XFS_ERROR(EPERM);
2618 src_bdp = vn_bhv_lookup_unlocked(VN_BHV_HEAD(src_vp), &xfs_vnodeops);
2619 sip = XFS_BHVTOI(src_bdp);
2620 tdp = XFS_BHVTOI(target_dir_bdp);
2622 if (XFS_FORCED_SHUTDOWN(mp))
2623 return XFS_ERROR(EIO);
2625 if (DM_EVENT_ENABLED(src_vp->v_vfsp, tdp, DM_EVENT_LINK)) {
2626 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2627 target_dir_vp, DM_RIGHT_NULL,
2628 src_vp, DM_RIGHT_NULL,
2629 target_name, NULL, 0, 0, 0);
2634 /* Return through std_return after this point. */
2636 error = XFS_QM_DQATTACH(mp, sip, 0);
2637 if (!error && sip != tdp)
2638 error = XFS_QM_DQATTACH(mp, tdp, 0);
2642 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2643 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2644 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2645 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2646 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2647 if (error == ENOSPC) {
2649 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2650 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2657 if (sip->i_ino < tdp->i_ino) {
2665 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2668 * Increment vnode ref counts since xfs_trans_commit &
2669 * xfs_trans_cancel will both unlock the inodes and
2670 * decrement the associated ref counts.
2673 VN_HOLD(target_dir_vp);
2674 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2675 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2678 * If the source has too many links, we can't make any more to it.
2680 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2681 error = XFS_ERROR(EMLINK);
2686 * If we are using project inheritance, we only allow hard link
2687 * creation in our tree when the project IDs are the same; else
2688 * the tree quota mechanism could be circumvented.
2690 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2691 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2692 error = XFS_ERROR(EPERM);
2697 (error = XFS_DIR_CANENTER(mp, tp, tdp, target_name,
2701 XFS_BMAP_INIT(&free_list, &first_block);
2703 error = XFS_DIR_CREATENAME(mp, tp, tdp, target_name, target_namelen,
2704 sip->i_ino, &first_block, &free_list,
2708 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2710 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2712 error = xfs_bumplink(tp, sip);
2718 * If this is a synchronous mount, make sure that the
2719 * link transaction goes to disk before returning to
2722 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2723 xfs_trans_set_sync(tp);
2726 error = xfs_bmap_finish (&tp, &free_list, first_block, &committed);
2728 xfs_bmap_cancel(&free_list);
2732 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2737 /* Fall through to std_return with error = 0. */
2739 if (DM_EVENT_ENABLED(src_vp->v_vfsp, sip,
2740 DM_EVENT_POSTLINK)) {
2741 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2742 target_dir_vp, DM_RIGHT_NULL,
2743 src_vp, DM_RIGHT_NULL,
2744 target_name, NULL, 0, error, 0);
2749 cancel_flags |= XFS_TRANS_ABORT;
2752 xfs_trans_cancel(tp, cancel_flags);
2762 bhv_desc_t *dir_bdp,
2768 char *dir_name = VNAME(dentry);
2770 xfs_inode_t *cdp; /* inode of created dir */
2771 vnode_t *cvp; /* vnode of created dir */
2777 xfs_bmap_free_t free_list;
2778 xfs_fsblock_t first_block;
2780 boolean_t dp_joined_to_trans;
2781 boolean_t created = B_FALSE;
2782 int dm_event_sent = 0;
2784 struct xfs_dquot *udqp, *gdqp;
2789 dir_vp = BHV_TO_VNODE(dir_bdp);
2790 dp = XFS_BHVTOI(dir_bdp);
2793 if (XFS_FORCED_SHUTDOWN(mp))
2794 return XFS_ERROR(EIO);
2796 dir_namelen = VNAMELEN(dentry);
2799 dp_joined_to_trans = B_FALSE;
2800 dm_di_mode = vap->va_mode;
2802 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
2803 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2804 dir_vp, DM_RIGHT_NULL, NULL,
2805 DM_RIGHT_NULL, dir_name, NULL,
2812 /* Return through std_return after this point. */
2814 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2818 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2819 prid = dp->i_d.di_projid;
2820 else if (vap->va_mask & XFS_AT_PROJID)
2821 prid = (xfs_prid_t)vap->va_projid;
2823 prid = (xfs_prid_t)dfltprid;
2826 * Make sure that we have allocated dquot(s) on disk.
2828 error = XFS_QM_DQVOPALLOC(mp, dp,
2829 current_fsuid(credp), current_fsgid(credp), prid,
2830 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2834 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2835 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2836 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2837 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2838 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2839 if (error == ENOSPC) {
2841 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2842 XFS_TRANS_PERM_LOG_RES,
2843 XFS_MKDIR_LOG_COUNT);
2851 xfs_ilock(dp, XFS_ILOCK_EXCL);
2854 * Check for directory link count overflow.
2856 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2857 error = XFS_ERROR(EMLINK);
2862 * Reserve disk quota and the inode.
2864 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2869 (error = XFS_DIR_CANENTER(mp, tp, dp, dir_name, dir_namelen)))
2872 * create the directory inode.
2874 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 2,
2875 0, credp, prid, resblks > 0,
2878 if (error == ENOSPC)
2885 * Now we add the directory inode to the transaction.
2886 * We waited until now since xfs_dir_ialloc might start
2887 * a new transaction. Had we joined the transaction
2888 * earlier, the locks might have gotten released.
2891 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2892 dp_joined_to_trans = B_TRUE;
2894 XFS_BMAP_INIT(&free_list, &first_block);
2896 error = XFS_DIR_CREATENAME(mp, tp, dp, dir_name, dir_namelen,
2897 cdp->i_ino, &first_block, &free_list,
2898 resblks ? resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2900 ASSERT(error != ENOSPC);
2903 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2906 * Bump the in memory version number of the parent directory
2907 * so that other processes accessing it will recognize that
2908 * the directory has changed.
2912 error = XFS_DIR_INIT(mp, tp, cdp, dp);
2918 error = xfs_bumplink(tp, dp);
2923 cvp = XFS_ITOV(cdp);
2931 * Attach the dquots to the new inode and modify the icount incore.
2933 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2936 * If this is a synchronous mount, make sure that the
2937 * mkdir transaction goes to disk before returning to
2940 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2941 xfs_trans_set_sync(tp);
2944 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2950 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2951 XFS_QM_DQRELE(mp, udqp);
2952 XFS_QM_DQRELE(mp, gdqp);
2957 /* Fall through to std_return with error = 0 or errno from
2958 * xfs_trans_commit. */
2961 if ( (created || (error != 0 && dm_event_sent != 0)) &&
2962 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2963 DM_EVENT_POSTCREATE)) {
2964 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2965 dir_vp, DM_RIGHT_NULL,
2966 created ? XFS_ITOV(cdp):NULL,
2969 dm_di_mode, error, 0);
2975 xfs_bmap_cancel(&free_list);
2977 cancel_flags |= XFS_TRANS_ABORT;
2979 xfs_trans_cancel(tp, cancel_flags);
2980 XFS_QM_DQRELE(mp, udqp);
2981 XFS_QM_DQRELE(mp, gdqp);
2983 if (!dp_joined_to_trans && (dp != NULL)) {
2984 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2997 bhv_desc_t *dir_bdp,
3001 char *name = VNAME(dentry);
3003 xfs_inode_t *cdp; /* child directory */
3007 xfs_bmap_free_t free_list;
3008 xfs_fsblock_t first_block;
3017 dir_vp = BHV_TO_VNODE(dir_bdp);
3018 dp = XFS_BHVTOI(dir_bdp);
3021 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3023 if (XFS_FORCED_SHUTDOWN(XFS_BHVTOI(dir_bdp)->i_mount))
3024 return XFS_ERROR(EIO);
3025 namelen = VNAMELEN(dentry);
3027 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
3028 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
3029 dir_vp, DM_RIGHT_NULL,
3030 NULL, DM_RIGHT_NULL,
3031 name, NULL, 0, 0, 0);
3033 return XFS_ERROR(error);
3036 /* Return through std_return after this point. */
3041 * We need to get a reference to cdp before we get our log
3042 * reservation. The reason for this is that we cannot call
3043 * xfs_iget for an inode for which we do not have a reference
3044 * once we've acquired a log reservation. This is because the
3045 * inode we are trying to get might be in xfs_inactive going
3046 * for a log reservation. Since we'll have to wait for the
3047 * inactive code to complete before returning from xfs_iget,
3048 * we need to make sure that we don't have log space reserved
3049 * when we call xfs_iget. Instead we get an unlocked referece
3050 * to the inode before getting our log reservation.
3052 error = xfs_get_dir_entry(dentry, &cdp);
3054 REMOVE_DEBUG_TRACE(__LINE__);
3058 dm_di_mode = cdp->i_d.di_mode;
3061 * Get the dquots for the inodes.
3063 error = XFS_QM_DQATTACH(mp, dp, 0);
3064 if (!error && dp != cdp)
3065 error = XFS_QM_DQATTACH(mp, cdp, 0);
3068 REMOVE_DEBUG_TRACE(__LINE__);
3072 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
3073 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3075 * We try to get the real space reservation first,
3076 * allowing for directory btree deletion(s) implying
3077 * possible bmap insert(s). If we can't get the space
3078 * reservation then we use 0 instead, and avoid the bmap
3079 * btree insert(s) in the directory code by, if the bmap
3080 * insert tries to happen, instead trimming the LAST
3081 * block from the directory.
3083 resblks = XFS_REMOVE_SPACE_RES(mp);
3084 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
3085 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3086 if (error == ENOSPC) {
3088 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
3089 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3092 ASSERT(error != ENOSPC);
3097 XFS_BMAP_INIT(&free_list, &first_block);
3100 * Now lock the child directory inode and the parent directory
3101 * inode in the proper order. This will take care of validating
3102 * that the directory entry for the child directory inode has
3103 * not changed while we were obtaining a log reservation.
3105 error = xfs_lock_dir_and_entry(dp, dentry, cdp);
3107 xfs_trans_cancel(tp, cancel_flags);
3112 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3115 * Only increment the parent directory vnode count if
3116 * we didn't bump it in looking up cdp. The only time
3117 * we don't bump it is when we're looking up ".".
3123 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3125 ASSERT(cdp->i_d.di_nlink >= 2);
3126 if (cdp->i_d.di_nlink != 2) {
3127 error = XFS_ERROR(ENOTEMPTY);
3130 if (!XFS_DIR_ISEMPTY(mp, cdp)) {
3131 error = XFS_ERROR(ENOTEMPTY);
3135 error = XFS_DIR_REMOVENAME(mp, tp, dp, name, namelen, cdp->i_ino,
3136 &first_block, &free_list, resblks);
3141 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3144 * Bump the in memory generation count on the parent
3145 * directory so that other can know that it has changed.
3150 * Drop the link from cdp's "..".
3152 error = xfs_droplink(tp, dp);
3158 * Drop the link from dp to cdp.
3160 error = xfs_droplink(tp, cdp);
3166 * Drop the "." link from cdp to self.
3168 error = xfs_droplink(tp, cdp);
3173 /* Determine these before committing transaction */
3174 last_cdp_link = (cdp)->i_d.di_nlink==0;
3177 * Take an extra ref on the child vnode so that it
3178 * does not go to xfs_inactive() from within the commit.
3183 * If this is a synchronous mount, make sure that the
3184 * rmdir transaction goes to disk before returning to
3187 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3188 xfs_trans_set_sync(tp);
3191 error = xfs_bmap_finish (&tp, &free_list, first_block, &committed);
3193 xfs_bmap_cancel(&free_list);
3194 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3200 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
3208 * Let interposed file systems know about removed links.
3210 VOP_LINK_REMOVED(XFS_ITOV(cdp), dir_vp, last_cdp_link);
3214 /* Fall through to std_return with error = 0 or the errno
3215 * from xfs_trans_commit. */
3217 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_POSTREMOVE)) {
3218 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3219 dir_vp, DM_RIGHT_NULL,
3220 NULL, DM_RIGHT_NULL,
3221 name, NULL, dm_di_mode,
3227 xfs_bmap_cancel(&free_list);
3228 cancel_flags |= XFS_TRANS_ABORT;
3230 xfs_trans_cancel(tp, cancel_flags);
3238 * Read dp's entries starting at uiop->uio_offset and translate them into
3239 * bufsize bytes worth of struct dirents starting at bufbase.
3243 bhv_desc_t *dir_bdp,
3249 xfs_trans_t *tp = NULL;
3252 xfs_off_t start_offset;
3254 vn_trace_entry(BHV_TO_VNODE(dir_bdp), __FUNCTION__,
3255 (inst_t *)__return_address);
3256 dp = XFS_BHVTOI(dir_bdp);
3258 if (XFS_FORCED_SHUTDOWN(dp->i_mount)) {
3259 return XFS_ERROR(EIO);
3262 lock_mode = xfs_ilock_map_shared(dp);
3263 start_offset = uiop->uio_offset;
3264 error = XFS_DIR_GETDENTS(dp->i_mount, tp, dp, uiop, eofp);
3265 if (start_offset != uiop->uio_offset) {
3266 xfs_ichgtime(dp, XFS_ICHGTIME_ACC);
3268 xfs_iunlock_map_shared(dp, lock_mode);
3279 bhv_desc_t *dir_bdp,
3292 xfs_bmap_free_t free_list;
3293 xfs_fsblock_t first_block;
3294 boolean_t dp_joined_to_trans;
3298 xfs_fileoff_t first_fsb;
3299 xfs_filblks_t fs_blocks;
3301 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3308 struct xfs_dquot *udqp, *gdqp;
3310 char *link_name = VNAME(dentry);
3314 dir_vp = BHV_TO_VNODE(dir_bdp);
3315 dp = XFS_BHVTOI(dir_bdp);
3316 dp_joined_to_trans = B_FALSE;
3321 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3325 if (XFS_FORCED_SHUTDOWN(mp))
3326 return XFS_ERROR(EIO);
3328 link_namelen = VNAMELEN(dentry);
3331 * Check component lengths of the target path name.
3333 pathlen = strlen(target_path);
3334 if (pathlen >= MAXPATHLEN) /* total string too long */
3335 return XFS_ERROR(ENAMETOOLONG);
3336 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3340 for(total = 0, path = target_path; total < pathlen;) {
3344 while(*path == '/') {
3350 * Count up to the next slash or end of path.
3351 * Error out if the component is bigger than MAXNAMELEN.
3353 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3354 if (++len >= MAXNAMELEN) {
3355 error = ENAMETOOLONG;
3362 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_SYMLINK)) {
3363 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3364 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3365 link_name, target_path, 0, 0, 0);
3370 /* Return through std_return after this point. */
3373 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3374 prid = dp->i_d.di_projid;
3375 else if (vap->va_mask & XFS_AT_PROJID)
3376 prid = (xfs_prid_t)vap->va_projid;
3378 prid = (xfs_prid_t)dfltprid;
3381 * Make sure that we have allocated dquot(s) on disk.
3383 error = XFS_QM_DQVOPALLOC(mp, dp,
3384 current_fsuid(credp), current_fsgid(credp), prid,
3385 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3389 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3390 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3392 * The symlink will fit into the inode data fork?
3393 * There can't be any attributes so we get the whole variable part.
3395 if (pathlen <= XFS_LITINO(mp))
3398 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3399 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3400 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3401 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3402 if (error == ENOSPC && fs_blocks == 0) {
3404 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3405 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3413 xfs_ilock(dp, XFS_ILOCK_EXCL);
3416 * Check whether the directory allows new symlinks or not.
3418 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3419 error = XFS_ERROR(EPERM);
3424 * Reserve disk quota : blocks and inode.
3426 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3431 * Check for ability to enter directory entry, if no space reserved.
3434 (error = XFS_DIR_CANENTER(mp, tp, dp, link_name, link_namelen)))
3437 * Initialize the bmap freelist prior to calling either
3438 * bmapi or the directory create code.
3440 XFS_BMAP_INIT(&free_list, &first_block);
3443 * Allocate an inode for the symlink.
3445 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (vap->va_mode&~S_IFMT),
3446 1, 0, credp, prid, resblks > 0, &ip, NULL);
3448 if (error == ENOSPC)
3455 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3456 dp_joined_to_trans = B_TRUE;
3459 * Also attach the dquot(s) to it, if applicable.
3461 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3464 resblks -= XFS_IALLOC_SPACE_RES(mp);
3466 * If the symlink will fit into the inode, write it inline.
3468 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3469 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3470 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3471 ip->i_d.di_size = pathlen;
3474 * The inode was initially created in extent format.
3476 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3477 ip->i_df.if_flags |= XFS_IFINLINE;
3479 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3480 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3484 nmaps = SYMLINK_MAPS;
3486 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3487 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3488 &first_block, resblks, mval, &nmaps,
3495 resblks -= fs_blocks;
3496 ip->i_d.di_size = pathlen;
3497 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3499 cur_chunk = target_path;
3500 for (n = 0; n < nmaps; n++) {
3501 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3502 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3503 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3504 BTOBB(byte_cnt), 0);
3505 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3506 if (pathlen < byte_cnt) {
3509 pathlen -= byte_cnt;
3511 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3512 cur_chunk += byte_cnt;
3514 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3519 * Create the directory entry for the symlink.
3521 error = XFS_DIR_CREATENAME(mp, tp, dp, link_name, link_namelen,
3522 ip->i_ino, &first_block, &free_list, resblks);
3526 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3527 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3530 * Bump the in memory version number of the parent directory
3531 * so that other processes accessing it will recognize that
3532 * the directory has changed.
3537 * If this is a synchronous mount, make sure that the
3538 * symlink transaction goes to disk before returning to
3541 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3542 xfs_trans_set_sync(tp);
3546 * xfs_trans_commit normally decrements the vnode ref count
3547 * when it unlocks the inode. Since we want to return the
3548 * vnode to the caller, we bump the vnode ref count now.
3552 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
3556 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
3557 XFS_QM_DQRELE(mp, udqp);
3558 XFS_QM_DQRELE(mp, gdqp);
3560 /* Fall through to std_return with error = 0 or errno from
3561 * xfs_trans_commit */
3563 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
3564 DM_EVENT_POSTSYMLINK)) {
3565 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3566 dir_vp, DM_RIGHT_NULL,
3567 error ? NULL : XFS_ITOV(ip),
3568 DM_RIGHT_NULL, link_name, target_path,
3584 xfs_bmap_cancel(&free_list);
3585 cancel_flags |= XFS_TRANS_ABORT;
3587 xfs_trans_cancel(tp, cancel_flags);
3588 XFS_QM_DQRELE(mp, udqp);
3589 XFS_QM_DQRELE(mp, gdqp);
3591 if (!dp_joined_to_trans && (dp != NULL)) {
3592 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3602 * A fid routine that takes a pointer to a previously allocated
3603 * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
3613 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
3614 (inst_t *)__return_address);
3615 ASSERT(sizeof(fid_t) >= sizeof(xfs_fid2_t));
3617 xfid = (xfs_fid2_t *)fidp;
3618 ip = XFS_BHVTOI(bdp);
3619 xfid->fid_len = sizeof(xfs_fid2_t) - sizeof(xfid->fid_len);
3622 * use memcpy because the inode is a long long and there's no
3623 * assurance that xfid->fid_ino is properly aligned.
3625 memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
3626 xfid->fid_gen = ip->i_d.di_gen;
3643 vp = BHV_TO_VNODE(bdp);
3646 ip = XFS_BHVTOI(bdp);
3647 if (locktype == VRWLOCK_WRITE) {
3648 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3649 } else if (locktype == VRWLOCK_TRY_READ) {
3650 return (xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED));
3651 } else if (locktype == VRWLOCK_TRY_WRITE) {
3652 return (xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL));
3654 ASSERT((locktype == VRWLOCK_READ) ||
3655 (locktype == VRWLOCK_WRITE_DIRECT));
3656 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3674 vp = BHV_TO_VNODE(bdp);
3677 ip = XFS_BHVTOI(bdp);
3678 if (locktype == VRWLOCK_WRITE) {
3680 * In the write case, we may have added a new entry to
3681 * the reference cache. This might store a pointer to
3682 * an inode to be released in this inode. If it is there,
3683 * clear the pointer and release the inode after unlocking
3686 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3688 ASSERT((locktype == VRWLOCK_READ) ||
3689 (locktype == VRWLOCK_WRITE_DIRECT));
3690 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3702 xfs_inode_log_item_t *iip;
3705 ip = XFS_BHVTOI(bdp);
3709 if (XFS_FORCED_SHUTDOWN(mp))
3710 return XFS_ERROR(EIO);
3713 * Bypass inodes which have already been cleaned by
3714 * the inode flush clustering code inside xfs_iflush
3716 if ((ip->i_update_core == 0) &&
3717 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3720 if (flags & FLUSH_LOG) {
3721 if (iip && iip->ili_last_lsn) {
3722 xlog_t *log = mp->m_log;
3724 int s, log_flags = XFS_LOG_FORCE;
3726 s = GRANT_LOCK(log);
3727 sync_lsn = log->l_last_sync_lsn;
3728 GRANT_UNLOCK(log, s);
3730 if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) <= 0))
3733 if (flags & FLUSH_SYNC)
3734 log_flags |= XFS_LOG_SYNC;
3735 return xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3740 * We make this non-blocking if the inode is contended,
3741 * return EAGAIN to indicate to the caller that they
3742 * did not succeed. This prevents the flush path from
3743 * blocking on inodes inside another operation right
3744 * now, they get caught later by xfs_sync.
3746 if (flags & FLUSH_INODE) {
3749 if (xfs_ipincount(ip))
3752 if (flags & FLUSH_SYNC) {
3753 xfs_ilock(ip, XFS_ILOCK_SHARED);
3755 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3756 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3757 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3764 if (flags & FLUSH_SYNC)
3765 flush_flags = XFS_IFLUSH_SYNC;
3767 flush_flags = XFS_IFLUSH_ASYNC;
3769 error = xfs_iflush(ip, flush_flags);
3770 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3789 if (!capable(CAP_SYS_ADMIN))
3790 return XFS_ERROR(EPERM);
3792 ip = XFS_BHVTOI(bdp);
3795 if (XFS_FORCED_SHUTDOWN(mp))
3796 return XFS_ERROR(EIO);
3798 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3799 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3801 xfs_trans_cancel(tp, 0);
3804 xfs_ilock(ip, XFS_ILOCK_EXCL);
3805 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3807 ip->i_iocore.io_dmevmask = ip->i_d.di_dmevmask = evmask;
3808 ip->i_iocore.io_dmstate = ip->i_d.di_dmstate = state;
3810 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3812 error = xfs_trans_commit(tp, 0, NULL);
3828 vp = BHV_TO_VNODE(bdp);
3829 ip = XFS_BHVTOI(bdp);
3831 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
3833 ASSERT(!VN_MAPPED(vp));
3835 /* bad inode, get out here ASAP */
3843 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
3844 ASSERT(VN_CACHED(vp) == 0);
3846 /* If we have nothing to flush with this inode then complete the
3847 * teardown now, otherwise break the link between the xfs inode
3848 * and the linux inode and clean up the xfs inode later. This
3849 * avoids flushing the inode to disk during the delete operation
3852 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3853 xfs_ilock(ip, XFS_ILOCK_EXCL);
3855 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3857 xfs_mount_t *mp = ip->i_mount;
3859 /* Protect sync from us */
3860 XFS_MOUNT_ILOCK(mp);
3861 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
3862 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
3863 ip->i_flags |= XFS_IRECLAIMABLE;
3864 XFS_MOUNT_IUNLOCK(mp);
3875 xfs_ihash_t *ih = ip->i_hash;
3876 vnode_t *vp = XFS_ITOV_NULL(ip);
3879 if (vp && VN_BAD(vp))
3882 /* The hash lock here protects a thread in xfs_iget_core from
3883 * racing with us on linking the inode back with a vnode.
3884 * Once we have the XFS_IRECLAIM flag set it will not touch
3887 write_lock(&ih->ih_lock);
3888 if ((ip->i_flags & XFS_IRECLAIM) ||
3889 (!(ip->i_flags & XFS_IRECLAIMABLE) && vp == NULL)) {
3890 write_unlock(&ih->ih_lock);
3893 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3897 ip->i_flags |= XFS_IRECLAIM;
3898 write_unlock(&ih->ih_lock);
3901 * If the inode is still dirty, then flush it out. If the inode
3902 * is not in the AIL, then it will be OK to flush it delwri as
3903 * long as xfs_iflush() does not keep any references to the inode.
3904 * We leave that decision up to xfs_iflush() since it has the
3905 * knowledge of whether it's OK to simply do a delwri flush of
3906 * the inode or whether we need to wait until the inode is
3907 * pulled from the AIL.
3908 * We get the flush lock regardless, though, just to make sure
3909 * we don't free it while it is being flushed.
3911 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3913 xfs_ilock(ip, XFS_ILOCK_EXCL);
3917 if (ip->i_update_core ||
3918 ((ip->i_itemp != NULL) &&
3919 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3920 error = xfs_iflush(ip, sync_mode);
3922 * If we hit an error, typically because of filesystem
3923 * shutdown, we don't need to let vn_reclaim to know
3924 * because we're gonna reclaim the inode anyway.
3927 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3930 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3933 ASSERT(ip->i_update_core == 0);
3934 ASSERT(ip->i_itemp == NULL ||
3935 ip->i_itemp->ili_format.ilf_fields == 0);
3936 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3937 } else if (locked) {
3939 * We are not interested in doing an iflush if we're
3940 * in the process of shutting down the filesystem forcibly.
3941 * So, just reclaim the inode.
3944 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3953 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3956 xfs_inode_t *ip, *n;
3961 XFS_MOUNT_ILOCK(mp);
3962 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3964 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3966 if (xfs_ipincount(ip) ||
3967 !xfs_iflock_nowait(ip)) {
3968 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3972 XFS_MOUNT_IUNLOCK(mp);
3973 xfs_finish_reclaim(ip, noblock,
3974 XFS_IFLUSH_DELWRI_ELSE_ASYNC);
3982 XFS_MOUNT_IUNLOCK(mp);
3987 * xfs_alloc_file_space()
3988 * This routine allocates disk space for the given file.
3990 * If alloc_type == 0, this request is for an ALLOCSP type
3991 * request which will change the file size. In this case, no
3992 * DMAPI event will be generated by the call. A TRUNCATE event
3993 * will be generated later by xfs_setattr.
3995 * If alloc_type != 0, this request is for a RESVSP type
3996 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3997 * lower block boundary byte address is less than the file's
4006 xfs_alloc_file_space(
4013 xfs_filblks_t allocated_fsb;
4014 xfs_filblks_t allocatesize_fsb;
4017 xfs_filblks_t datablocks;
4019 xfs_fsblock_t firstfsb;
4020 xfs_bmap_free_t free_list;
4021 xfs_bmbt_irec_t *imapp;
4022 xfs_bmbt_irec_t imaps[1];
4029 xfs_fileoff_t startoffset_fsb;
4031 int xfs_bmapi_flags;
4033 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
4036 if (XFS_FORCED_SHUTDOWN(mp))
4037 return XFS_ERROR(EIO);
4040 * determine if this is a realtime file
4042 if ((rt = XFS_IS_REALTIME_INODE(ip)) != 0) {
4043 if (ip->i_d.di_extsize)
4044 rtextsize = ip->i_d.di_extsize;
4046 rtextsize = mp->m_sb.sb_rextsize;
4050 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4054 return XFS_ERROR(EINVAL);
4060 xfs_bmapi_flags = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
4061 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
4062 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
4064 /* Generate a DMAPI event if needed. */
4065 if (alloc_type != 0 && offset < ip->i_d.di_size &&
4066 (attr_flags&ATTR_DMI) == 0 &&
4067 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4068 xfs_off_t end_dmi_offset;
4070 end_dmi_offset = offset+len;
4071 if (end_dmi_offset > ip->i_d.di_size)
4072 end_dmi_offset = ip->i_d.di_size;
4073 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
4074 offset, end_dmi_offset - offset,
4081 * allocate file space until done or until there is an error
4084 while (allocatesize_fsb && !error) {
4086 * determine if reserving space on
4087 * the data or realtime partition.
4092 s = startoffset_fsb;
4093 do_div(s, rtextsize);
4095 e = roundup_64(startoffset_fsb + allocatesize_fsb,
4097 numrtextents = (int)(e - s) / mp->m_sb.sb_rextsize;
4100 datablocks = allocatesize_fsb;
4105 * allocate and setup the transaction
4107 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4108 resblks = XFS_DIOSTRAT_SPACE_RES(mp, datablocks);
4109 error = xfs_trans_reserve(tp,
4111 XFS_WRITE_LOG_RES(mp),
4113 XFS_TRANS_PERM_LOG_RES,
4114 XFS_WRITE_LOG_COUNT);
4117 * check for running out of space
4121 * Free the transaction structure.
4123 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4124 xfs_trans_cancel(tp, 0);
4127 xfs_ilock(ip, XFS_ILOCK_EXCL);
4128 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4129 ip->i_udquot, ip->i_gdquot, resblks, 0, 0);
4133 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4134 xfs_trans_ihold(tp, ip);
4137 * issue the bmapi() call to allocate the blocks
4139 XFS_BMAP_INIT(&free_list, &firstfsb);
4140 error = xfs_bmapi(tp, ip, startoffset_fsb,
4141 allocatesize_fsb, xfs_bmapi_flags,
4142 &firstfsb, 0, imapp, &reccount,
4149 * complete the transaction
4151 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
4156 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
4157 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4162 allocated_fsb = imapp->br_blockcount;
4164 if (reccount == 0) {
4165 error = XFS_ERROR(ENOSPC);
4169 startoffset_fsb += allocated_fsb;
4170 allocatesize_fsb -= allocated_fsb;
4173 if (error == ENOSPC && (attr_flags&ATTR_DMI) == 0 &&
4174 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_NOSPACE)) {
4176 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
4177 XFS_ITOV(ip), DM_RIGHT_NULL,
4178 XFS_ITOV(ip), DM_RIGHT_NULL,
4179 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
4181 goto retry; /* Maybe DMAPI app. has made space */
4182 /* else fall through with error from XFS_SEND_DATA */
4188 xfs_bmap_cancel(&free_list);
4190 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4191 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4192 goto dmapi_enospc_check;
4196 * Zero file bytes between startoff and endoff inclusive.
4197 * The iolock is held exclusive and no blocks are buffered.
4200 xfs_zero_remaining_bytes(
4205 xfs_bmbt_irec_t imap;
4206 xfs_fileoff_t offset_fsb;
4207 xfs_off_t lastoffset;
4210 xfs_mount_t *mp = ip->i_mount;
4214 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
4215 ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
4216 mp->m_rtdev_targp : mp->m_ddev_targp);
4218 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4219 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4221 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0, NULL, 0, &imap,
4223 if (error || nimap < 1)
4225 ASSERT(imap.br_blockcount >= 1);
4226 ASSERT(imap.br_startoff == offset_fsb);
4227 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4228 if (lastoffset > endoff)
4229 lastoffset = endoff;
4230 if (imap.br_startblock == HOLESTARTBLOCK)
4232 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4233 if (imap.br_state == XFS_EXT_UNWRITTEN)
4236 XFS_BUF_UNWRITE(bp);
4238 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4240 if ((error = xfs_iowait(bp))) {
4241 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4242 mp, bp, XFS_BUF_ADDR(bp));
4245 memset(XFS_BUF_PTR(bp) +
4246 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4247 0, lastoffset - offset + 1);
4252 if ((error = xfs_iowait(bp))) {
4253 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4254 mp, bp, XFS_BUF_ADDR(bp));
4263 * xfs_free_file_space()
4264 * This routine frees disk space for the given file.
4266 * This routine is only called by xfs_change_file_space
4267 * for an UNRESVSP type call.
4275 xfs_free_file_space(
4284 xfs_off_t end_dmi_offset;
4285 xfs_fileoff_t endoffset_fsb;
4287 xfs_fsblock_t firstfsb;
4288 xfs_bmap_free_t free_list;
4290 xfs_bmbt_irec_t imap;
4298 xfs_fileoff_t startoffset_fsb;
4300 int need_iolock = 1;
4305 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4307 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4311 if (len <= 0) /* if nothing being freed */
4313 rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4314 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4315 end_dmi_offset = offset + len;
4316 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4318 if (offset < ip->i_d.di_size &&
4319 (attr_flags & ATTR_DMI) == 0 &&
4320 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4321 if (end_dmi_offset > ip->i_d.di_size)
4322 end_dmi_offset = ip->i_d.di_size;
4323 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
4324 offset, end_dmi_offset - offset,
4325 AT_DELAY_FLAG(attr_flags), NULL);
4330 ASSERT(attr_flags & ATTR_NOLOCK ? attr_flags & ATTR_DMI : 1);
4331 if (attr_flags & ATTR_NOLOCK)
4334 xfs_ilock(ip, XFS_IOLOCK_EXCL);
4336 rounding = MAX((__uint8_t)(1 << mp->m_sb.sb_blocklog),
4338 ilen = len + (offset & (rounding - 1));
4339 ioffset = offset & ~(rounding - 1);
4340 if (ilen & (rounding - 1))
4341 ilen = (ilen + rounding) & ~(rounding - 1);
4343 if (VN_CACHED(vp) != 0) {
4344 xfs_inval_cached_trace(&ip->i_iocore, ioffset, -1,
4345 ctooff(offtoct(ioffset)), -1);
4346 VOP_FLUSHINVAL_PAGES(vp, ctooff(offtoct(ioffset)),
4347 -1, FI_REMAPF_LOCKED);
4351 * Need to zero the stuff we're not freeing, on disk.
4352 * If its a realtime file & can't use unwritten extents then we
4353 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4354 * will take care of it for us.
4356 if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4358 error = xfs_bmapi(NULL, ip, startoffset_fsb, 1, 0, NULL, 0,
4359 &imap, &nimap, NULL);
4361 goto out_unlock_iolock;
4362 ASSERT(nimap == 0 || nimap == 1);
4363 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4366 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4367 block = imap.br_startblock;
4368 mod = do_div(block, mp->m_sb.sb_rextsize);
4370 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4373 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1, 1, 0, NULL, 0,
4374 &imap, &nimap, NULL);
4376 goto out_unlock_iolock;
4377 ASSERT(nimap == 0 || nimap == 1);
4378 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4379 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4381 if (mod && (mod != mp->m_sb.sb_rextsize))
4382 endoffset_fsb -= mod;
4385 if ((done = (endoffset_fsb <= startoffset_fsb)))
4387 * One contiguous piece to clear
4389 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4392 * Some full blocks, possibly two pieces to clear
4394 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4395 error = xfs_zero_remaining_bytes(ip, offset,
4396 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4398 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4399 error = xfs_zero_remaining_bytes(ip,
4400 XFS_FSB_TO_B(mp, endoffset_fsb),
4405 * free file space until done or until there is an error
4407 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4408 while (!error && !done) {
4411 * allocate and setup the transaction
4413 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4414 error = xfs_trans_reserve(tp,
4416 XFS_WRITE_LOG_RES(mp),
4418 XFS_TRANS_PERM_LOG_RES,
4419 XFS_WRITE_LOG_COUNT);
4422 * check for running out of space
4426 * Free the transaction structure.
4428 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4429 xfs_trans_cancel(tp, 0);
4432 xfs_ilock(ip, XFS_ILOCK_EXCL);
4433 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4434 ip->i_udquot, ip->i_gdquot, resblks, 0, rt ?
4435 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4439 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4440 xfs_trans_ihold(tp, ip);
4443 * issue the bunmapi() call to free the blocks
4445 XFS_BMAP_INIT(&free_list, &firstfsb);
4446 error = xfs_bunmapi(tp, ip, startoffset_fsb,
4447 endoffset_fsb - startoffset_fsb,
4448 0, 2, &firstfsb, &free_list, &done);
4454 * complete the transaction
4456 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
4461 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
4462 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4467 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4471 xfs_bmap_cancel(&free_list);
4473 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4474 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4480 * xfs_change_file_space()
4481 * This routine allocates or frees disk space for the given file.
4482 * The user specified parameters are checked for alignment and size
4491 xfs_change_file_space(
4505 xfs_off_t startoffset;
4511 vp = BHV_TO_VNODE(bdp);
4512 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4514 ip = XFS_BHVTOI(bdp);
4518 * must be a regular file and have write permission
4521 return XFS_ERROR(EINVAL);
4523 xfs_ilock(ip, XFS_ILOCK_SHARED);
4525 if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
4526 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4530 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4532 switch (bf->l_whence) {
4533 case 0: /*SEEK_SET*/
4535 case 1: /*SEEK_CUR*/
4536 bf->l_start += offset;
4538 case 2: /*SEEK_END*/
4539 bf->l_start += ip->i_d.di_size;
4542 return XFS_ERROR(EINVAL);
4545 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4547 if ( (bf->l_start < 0)
4548 || (bf->l_start > XFS_MAXIOFFSET(mp))
4549 || (bf->l_start + llen < 0)
4550 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4551 return XFS_ERROR(EINVAL);
4555 startoffset = bf->l_start;
4556 fsize = ip->i_d.di_size;
4559 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4561 * These calls do NOT zero the data space allocated to the file,
4562 * nor do they change the file size.
4564 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4566 * These calls cause the new file data to be zeroed and the file
4567 * size to be changed.
4569 setprealloc = clrprealloc = 0;
4572 case XFS_IOC_RESVSP:
4573 case XFS_IOC_RESVSP64:
4574 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4581 case XFS_IOC_UNRESVSP:
4582 case XFS_IOC_UNRESVSP64:
4583 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4588 case XFS_IOC_ALLOCSP:
4589 case XFS_IOC_ALLOCSP64:
4590 case XFS_IOC_FREESP:
4591 case XFS_IOC_FREESP64:
4592 if (startoffset > fsize) {
4593 error = xfs_alloc_file_space(ip, fsize,
4594 startoffset - fsize, 0, attr_flags);
4599 va.va_mask = XFS_AT_SIZE;
4600 va.va_size = startoffset;
4602 error = xfs_setattr(bdp, &va, attr_flags, credp);
4612 return XFS_ERROR(EINVAL);
4616 * update the inode timestamp, mode, and prealloc flag bits
4618 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4620 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4623 xfs_trans_cancel(tp, 0);
4627 xfs_ilock(ip, XFS_ILOCK_EXCL);
4629 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4630 xfs_trans_ihold(tp, ip);
4632 if ((attr_flags & ATTR_DMI) == 0) {
4633 ip->i_d.di_mode &= ~S_ISUID;
4636 * Note that we don't have to worry about mandatory
4637 * file locking being disabled here because we only
4638 * clear the S_ISGID bit if the Group execute bit is
4639 * on, but if it was on then mandatory locking wouldn't
4640 * have been enabled.
4642 if (ip->i_d.di_mode & S_IXGRP)
4643 ip->i_d.di_mode &= ~S_ISGID;
4645 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4648 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4649 else if (clrprealloc)
4650 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4652 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4653 xfs_trans_set_sync(tp);
4655 error = xfs_trans_commit(tp, 0, NULL);
4657 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4662 vnodeops_t xfs_vnodeops = {
4663 BHV_IDENTITY_INIT(VN_BHV_XFS,VNODE_POSITION_XFS),
4664 .vop_open = xfs_open,
4665 .vop_read = xfs_read,
4666 #ifdef HAVE_SENDFILE
4667 .vop_sendfile = xfs_sendfile,
4669 .vop_write = xfs_write,
4670 .vop_ioctl = xfs_ioctl,
4671 .vop_getattr = xfs_getattr,
4672 .vop_setattr = xfs_setattr,
4673 .vop_access = xfs_access,
4674 .vop_lookup = xfs_lookup,
4675 .vop_create = xfs_create,
4676 .vop_remove = xfs_remove,
4677 .vop_link = xfs_link,
4678 .vop_rename = xfs_rename,
4679 .vop_mkdir = xfs_mkdir,
4680 .vop_rmdir = xfs_rmdir,
4681 .vop_readdir = xfs_readdir,
4682 .vop_symlink = xfs_symlink,
4683 .vop_readlink = xfs_readlink,
4684 .vop_fsync = xfs_fsync,
4685 .vop_inactive = xfs_inactive,
4686 .vop_fid2 = xfs_fid2,
4687 .vop_rwlock = xfs_rwlock,
4688 .vop_rwunlock = xfs_rwunlock,
4689 .vop_bmap = xfs_bmap,
4690 .vop_reclaim = xfs_reclaim,
4691 .vop_attr_get = xfs_attr_get,
4692 .vop_attr_set = xfs_attr_set,
4693 .vop_attr_remove = xfs_attr_remove,
4694 .vop_attr_list = xfs_attr_list,
4695 .vop_link_removed = (vop_link_removed_t)fs_noval,
4696 .vop_vnode_change = (vop_vnode_change_t)fs_noval,
4697 .vop_tosspages = fs_tosspages,
4698 .vop_flushinval_pages = fs_flushinval_pages,
4699 .vop_flush_pages = fs_flush_pages,
4700 .vop_release = xfs_release,
4701 .vop_iflush = xfs_inode_flush,