2 * Copyright (c) 2000-2004 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_macros.h"
35 #include "xfs_types.h"
38 #include "xfs_trans.h"
43 #include "xfs_dmapi.h"
44 #include "xfs_mount.h"
45 #include "xfs_alloc_btree.h"
46 #include "xfs_bmap_btree.h"
47 #include "xfs_ialloc_btree.h"
48 #include "xfs_itable.h"
49 #include "xfs_btree.h"
50 #include "xfs_ialloc.h"
51 #include "xfs_alloc.h"
52 #include "xfs_attr_sf.h"
53 #include "xfs_dir_sf.h"
54 #include "xfs_dir2_sf.h"
55 #include "xfs_dinode.h"
56 #include "xfs_inode_item.h"
57 #include "xfs_inode.h"
59 #include "xfs_da_btree.h"
62 #include "xfs_refcache.h"
63 #include "xfs_error.h"
65 #include "xfs_rtalloc.h"
66 #include "xfs_quota.h"
67 #include "xfs_utils.h"
68 #include "xfs_trans_space.h"
69 #include "xfs_dir_leaf.h"
71 #include "xfs_log_priv.h"
75 * The maximum pathlen is 1024 bytes. Since the minimum file system
76 * blocksize is 512 bytes, we can get a max of 2 extents back from
79 #define SYMLINK_MAPS 2
82 * For xfs, we check that the file isn't too big to be opened by this kernel.
83 * No other open action is required for regular files. Devices are handled
84 * through the specfs file system, pipes through fifofs. Device and
85 * fifo vnodes are "wrapped" by specfs and fifofs vnodes, respectively,
86 * when a new vnode is first looked up or created.
97 vp = BHV_TO_VNODE(bdp);
100 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
101 return XFS_ERROR(EIO);
104 * If it's a directory with any blocks, read-ahead block 0
105 * as we're almost certain to have the next operation be a read there.
107 if (vp->v_type == VDIR && ip->i_d.di_nextents > 0) {
108 mode = xfs_ilock_map_shared(ip);
109 if (ip->i_d.di_nextents > 0)
110 (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
111 xfs_iunlock(ip, mode);
131 vp = BHV_TO_VNODE(bdp);
132 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
134 ip = XFS_BHVTOI(bdp);
137 if (XFS_FORCED_SHUTDOWN(mp))
138 return XFS_ERROR(EIO);
140 if (!(flags & ATTR_LAZY))
141 xfs_ilock(ip, XFS_ILOCK_SHARED);
143 vap->va_size = ip->i_d.di_size;
144 if (vap->va_mask == XFS_AT_SIZE)
148 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
149 vap->va_nodeid = ip->i_ino;
151 vap->va_nodeid += mp->m_inoadd;
153 vap->va_nlink = ip->i_d.di_nlink;
156 * Quick exit for non-stat callers
159 ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
160 XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
164 * Copy from in-core inode.
166 vap->va_type = vp->v_type;
167 vap->va_mode = ip->i_d.di_mode & MODEMASK;
168 vap->va_uid = ip->i_d.di_uid;
169 vap->va_gid = ip->i_d.di_gid;
170 vap->va_projid = ip->i_d.di_projid;
173 * Check vnode type block/char vs. everything else.
174 * Do it with bitmask because that's faster than looking
175 * for multiple values individually.
177 if (((1 << vp->v_type) & ((1<<VBLK) | (1<<VCHR))) == 0) {
180 if (!(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
183 /* Large block sizes confuse various
184 * user space programs, so letting the
185 * stripe size through is not a good
188 vap->va_blocksize = mp->m_swidth ?
190 * If the underlying volume is a stripe, then
191 * return the stripe width in bytes as the
192 * recommended I/O size.
194 (mp->m_swidth << mp->m_sb.sb_blocklog) :
196 * Return the largest of the preferred buffer
197 * sizes since doing small I/Os into larger
198 * buffers causes buffers to be decommissioned.
199 * The value returned is in bytes.
201 (1 << (int)MAX(mp->m_readio_log,
207 * Return the largest of the preferred buffer
208 * sizes since doing small I/Os into larger
209 * buffers causes buffers to be decommissioned.
210 * The value returned is in bytes.
212 1 << (int)MAX(mp->m_readio_log,
218 * If the file blocks are being allocated from a
219 * realtime partition, then return the inode's
220 * realtime extent size or the realtime volume's
223 vap->va_blocksize = ip->i_d.di_extsize ?
224 (ip->i_d.di_extsize << mp->m_sb.sb_blocklog) :
225 (mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog);
228 vap->va_rdev = ip->i_df.if_u2.if_rdev;
229 vap->va_blocksize = BLKDEV_IOSIZE;
232 vap->va_atime.tv_sec = ip->i_d.di_atime.t_sec;
233 vap->va_atime.tv_nsec = ip->i_d.di_atime.t_nsec;
234 vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
235 vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
236 vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
237 vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
240 * Exit for stat callers. See if any of the rest of the fields
241 * to be filled in are needed.
244 (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
245 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
249 * Convert di_flags to xflags.
251 vap->va_xflags = xfs_ip2xflags(ip);
254 * Exit for inode revalidate. See if any of the rest of
255 * the fields to be filled in are needed.
258 (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
259 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
262 vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
264 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
265 ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
269 (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
270 ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
271 ip->i_d.di_anextents;
273 vap->va_anextents = 0;
274 vap->va_gen = ip->i_d.di_gen;
277 if (!(flags & ATTR_LAZY))
278 xfs_iunlock(ip, XFS_ILOCK_SHARED);
304 xfs_prid_t projid=0, iprojid=0;
305 int mandlock_before, mandlock_after;
306 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
310 vp = BHV_TO_VNODE(bdp);
311 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
313 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
314 return XFS_ERROR(EROFS);
317 * Cannot set certain attributes.
320 if (mask & XFS_AT_NOSET) {
321 return XFS_ERROR(EINVAL);
324 ip = XFS_BHVTOI(bdp);
327 if (XFS_FORCED_SHUTDOWN(mp))
328 return XFS_ERROR(EIO);
331 * Timestamps do not need to be logged and hence do not
332 * need to be done within a transaction.
334 if (mask & XFS_AT_UPDTIMES) {
335 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
336 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
337 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
338 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
339 xfs_ichgtime(ip, timeflags);
343 olddquot1 = olddquot2 = NULL;
347 * If disk quotas is on, we make sure that the dquots do exist on disk,
348 * before we start any other transactions. Trying to do this later
349 * is messy. We don't care to take a readlock to look at the ids
350 * in inode here, because we can't hold it across the trans_reserve.
351 * If the IDs do change before we take the ilock, we're covered
352 * because the i_*dquot fields will get updated anyway.
354 if (XFS_IS_QUOTA_ON(mp) && (mask & (XFS_AT_UID|XFS_AT_GID))) {
357 if (mask & XFS_AT_UID) {
359 qflags |= XFS_QMOPT_UQUOTA;
361 uid = ip->i_d.di_uid;
363 if (mask & XFS_AT_GID) {
365 qflags |= XFS_QMOPT_GQUOTA;
367 gid = ip->i_d.di_gid;
370 * We take a reference when we initialize udqp and gdqp,
371 * so it is important that we never blindly double trip on
372 * the same variable. See xfs_create() for an example.
374 ASSERT(udqp == NULL);
375 ASSERT(gdqp == NULL);
376 code = XFS_QM_DQVOPALLOC(mp, ip, uid,gid, qflags, &udqp, &gdqp);
382 * For the other attributes, we acquire the inode lock and
383 * first do an error checking pass.
386 lock_flags = XFS_ILOCK_EXCL;
387 ASSERT(flags & ATTR_NOLOCK ? flags & ATTR_DMI : 1);
388 if (flags & ATTR_NOLOCK)
390 if (!(mask & XFS_AT_SIZE)) {
391 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
392 (mp->m_flags & XFS_MOUNT_WSYNC)) {
393 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
395 if ((code = xfs_trans_reserve(tp, 0,
396 XFS_ICHANGE_LOG_RES(mp), 0,
403 if (DM_EVENT_ENABLED (vp->v_vfsp, ip, DM_EVENT_TRUNCATE) &&
404 !(flags & ATTR_DMI)) {
405 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
406 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
407 vap->va_size, 0, dmflags, NULL);
414 lock_flags |= XFS_IOLOCK_EXCL;
417 xfs_ilock(ip, lock_flags);
419 /* boolean: are we the file owner? */
420 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
423 * Change various properties of a file.
424 * Only the owner or users with CAP_FOWNER
425 * capability may do these things.
428 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
429 XFS_AT_GID|XFS_AT_PROJID)) {
431 * CAP_FOWNER overrides the following restrictions:
433 * The user ID of the calling process must be equal
434 * to the file owner ID, except in cases where the
435 * CAP_FSETID capability is applicable.
437 if (!file_owner && !capable(CAP_FOWNER)) {
438 code = XFS_ERROR(EPERM);
443 * CAP_FSETID overrides the following restrictions:
445 * The effective user ID of the calling process shall match
446 * the file owner when setting the set-user-ID and
447 * set-group-ID bits on that file.
449 * The effective group ID or one of the supplementary group
450 * IDs of the calling process shall match the group owner of
451 * the file when setting the set-group-ID bit on that file
453 if (mask & XFS_AT_MODE) {
456 if ((vap->va_mode & S_ISUID) && !file_owner)
458 if ((vap->va_mode & S_ISGID) &&
459 !in_group_p((gid_t)ip->i_d.di_gid))
462 /* Linux allows this, Irix doesn't. */
463 if ((vap->va_mode & S_ISVTX) && vp->v_type != VDIR)
466 if (m && !capable(CAP_FSETID))
472 * Change file ownership. Must be the owner or privileged.
473 * If the system was configured with the "restricted_chown"
474 * option, the owner is not permitted to give away the file,
475 * and can change the group id only to a group of which he
476 * or she is a member.
478 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
480 * These IDs could have changed since we last looked at them.
481 * But, we're assured that if the ownership did change
482 * while we didn't have the inode locked, inode's dquot(s)
483 * would have changed also.
485 iuid = ip->i_d.di_uid;
486 iprojid = ip->i_d.di_projid;
487 igid = ip->i_d.di_gid;
488 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
489 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
490 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
494 * CAP_CHOWN overrides the following restrictions:
496 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
497 * shall override the restriction that a process cannot
498 * change the user ID of a file it owns and the restriction
499 * that the group ID supplied to the chown() function
500 * shall be equal to either the group ID or one of the
501 * supplementary group IDs of the calling process.
503 * XXX: How does restricted_chown affect projid?
505 if (restricted_chown &&
506 (iuid != uid || (igid != gid &&
507 !in_group_p((gid_t)gid))) &&
508 !capable(CAP_CHOWN)) {
509 code = XFS_ERROR(EPERM);
513 * Do a quota reservation only if uid or gid is actually
516 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
517 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
519 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
520 capable(CAP_FOWNER) ?
521 XFS_QMOPT_FORCE_RES : 0);
522 if (code) /* out of quota */
528 * Truncate file. Must have write permission and not be a directory.
530 if (mask & XFS_AT_SIZE) {
531 /* Short circuit the truncate case for zero length files */
532 if ((vap->va_size == 0) &&
533 (ip->i_d.di_size == 0) && (ip->i_d.di_nextents == 0)) {
534 xfs_iunlock(ip, XFS_ILOCK_EXCL);
535 lock_flags &= ~XFS_ILOCK_EXCL;
536 if (mask & XFS_AT_CTIME)
537 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
542 if (vp->v_type == VDIR) {
543 code = XFS_ERROR(EISDIR);
545 } else if (vp->v_type != VREG) {
546 code = XFS_ERROR(EINVAL);
550 * Make sure that the dquots are attached to the inode.
552 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
557 * Change file access or modified times.
559 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
561 if ((flags & ATTR_UTIME) &&
562 !capable(CAP_FOWNER)) {
563 code = XFS_ERROR(EPERM);
570 * Change extent size or realtime flag.
572 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
574 * Can't change extent size if any extents are allocated.
576 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
577 (mask & XFS_AT_EXTSIZE) &&
578 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
580 code = XFS_ERROR(EINVAL); /* EFBIG? */
585 * Can't set extent size unless the file is marked, or
586 * about to be marked as a realtime file.
588 * This check will be removed when fixed size extents
589 * with buffered data writes is implemented.
592 if ((mask & XFS_AT_EXTSIZE) &&
593 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
595 (!((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
596 ((mask & XFS_AT_XFLAGS) &&
597 (vap->va_xflags & XFS_XFLAG_REALTIME))))) {
598 code = XFS_ERROR(EINVAL);
603 * Can't change realtime flag if any extents are allocated.
605 if (ip->i_d.di_nextents && (mask & XFS_AT_XFLAGS) &&
606 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) !=
607 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
608 code = XFS_ERROR(EINVAL); /* EFBIG? */
612 * Extent size must be a multiple of the appropriate block
613 * size, if set at all.
615 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
618 if ((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
619 ((mask & XFS_AT_XFLAGS) &&
620 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
621 size = mp->m_sb.sb_rextsize <<
622 mp->m_sb.sb_blocklog;
624 size = mp->m_sb.sb_blocksize;
626 if (vap->va_extsize % size) {
627 code = XFS_ERROR(EINVAL);
632 * If realtime flag is set then must have realtime data.
634 if ((mask & XFS_AT_XFLAGS) &&
635 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
636 if ((mp->m_sb.sb_rblocks == 0) ||
637 (mp->m_sb.sb_rextsize == 0) ||
638 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
639 code = XFS_ERROR(EINVAL);
645 * Can't modify an immutable/append-only file unless
646 * we have appropriate permission.
648 if ((mask & XFS_AT_XFLAGS) &&
650 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
652 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
653 !capable(CAP_LINUX_IMMUTABLE)) {
654 code = XFS_ERROR(EPERM);
660 * Now we can make the changes. Before we join the inode
661 * to the transaction, if XFS_AT_SIZE is set then take care of
662 * the part of the truncation that must be done without the
663 * inode lock. This needs to be done before joining the inode
664 * to the transaction, because the inode cannot be unlocked
665 * once it is a part of the transaction.
667 if (mask & XFS_AT_SIZE) {
669 if (vap->va_size > ip->i_d.di_size)
670 code = xfs_igrow_start(ip, vap->va_size, credp);
671 xfs_iunlock(ip, XFS_ILOCK_EXCL);
673 code = xfs_itruncate_data(ip, vap->va_size);
676 lock_flags &= ~XFS_ILOCK_EXCL;
677 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
680 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
681 if ((code = xfs_trans_reserve(tp, 0,
682 XFS_ITRUNCATE_LOG_RES(mp), 0,
683 XFS_TRANS_PERM_LOG_RES,
684 XFS_ITRUNCATE_LOG_COUNT))) {
685 xfs_trans_cancel(tp, 0);
687 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
690 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
691 xfs_ilock(ip, XFS_ILOCK_EXCL);
695 xfs_trans_ijoin(tp, ip, lock_flags);
696 xfs_trans_ihold(tp, ip);
699 /* determine whether mandatory locking mode changes */
700 mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
703 * Truncate file. Must have write permission and not be a directory.
705 if (mask & XFS_AT_SIZE) {
706 if (vap->va_size > ip->i_d.di_size) {
707 xfs_igrow_finish(tp, ip, vap->va_size,
708 !(flags & ATTR_DMI));
709 } else if ((vap->va_size <= ip->i_d.di_size) ||
710 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
712 * signal a sync transaction unless
713 * we're truncating an already unlinked
714 * file on a wsync filesystem
716 code = xfs_itruncate_finish(&tp, ip,
717 (xfs_fsize_t)vap->va_size,
719 ((ip->i_d.di_nlink != 0 ||
720 !(mp->m_flags & XFS_MOUNT_WSYNC))
727 * Have to do this even if the file's size doesn't change.
729 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
733 * Change file access modes.
735 if (mask & XFS_AT_MODE) {
736 ip->i_d.di_mode &= S_IFMT;
737 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
739 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
740 timeflags |= XFS_ICHGTIME_CHG;
744 * Change file ownership. Must be the owner or privileged.
745 * If the system was configured with the "restricted_chown"
746 * option, the owner is not permitted to give away the file,
747 * and can change the group id only to a group of which he
748 * or she is a member.
750 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
752 * CAP_FSETID overrides the following restrictions:
754 * The set-user-ID and set-group-ID bits of a file will be
755 * cleared upon successful return from chown()
757 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
758 !capable(CAP_FSETID)) {
759 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
763 * Change the ownerships and register quota modifications
764 * in the transaction.
767 if (XFS_IS_UQUOTA_ON(mp)) {
768 ASSERT(mask & XFS_AT_UID);
770 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
771 &ip->i_udquot, udqp);
773 ip->i_d.di_uid = uid;
776 if (XFS_IS_GQUOTA_ON(mp)) {
777 ASSERT(mask & XFS_AT_GID);
779 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
780 &ip->i_gdquot, gdqp);
782 ip->i_d.di_gid = gid;
784 if (iprojid != projid) {
785 ip->i_d.di_projid = projid;
787 * We may have to rev the inode as well as
788 * the superblock version number since projids didn't
789 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
791 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
792 xfs_bump_ino_vers2(tp, ip);
795 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
796 timeflags |= XFS_ICHGTIME_CHG;
801 * Change file access or modified times.
803 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
804 if (mask & XFS_AT_ATIME) {
805 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
806 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
807 ip->i_update_core = 1;
808 timeflags &= ~XFS_ICHGTIME_ACC;
810 if (mask & XFS_AT_MTIME) {
811 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
812 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
813 timeflags &= ~XFS_ICHGTIME_MOD;
814 timeflags |= XFS_ICHGTIME_CHG;
816 if (tp && (flags & ATTR_UTIME))
817 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
821 * Change XFS-added attributes.
823 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
824 if (mask & XFS_AT_EXTSIZE) {
826 * Converting bytes to fs blocks.
828 ip->i_d.di_extsize = vap->va_extsize >>
829 mp->m_sb.sb_blocklog;
831 if (mask & XFS_AT_XFLAGS) {
834 /* can't set PREALLOC this way, just preserve it */
835 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
836 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
837 di_flags |= XFS_DIFLAG_IMMUTABLE;
838 if (vap->va_xflags & XFS_XFLAG_APPEND)
839 di_flags |= XFS_DIFLAG_APPEND;
840 if (vap->va_xflags & XFS_XFLAG_SYNC)
841 di_flags |= XFS_DIFLAG_SYNC;
842 if (vap->va_xflags & XFS_XFLAG_NOATIME)
843 di_flags |= XFS_DIFLAG_NOATIME;
844 if (vap->va_xflags & XFS_XFLAG_NODUMP)
845 di_flags |= XFS_DIFLAG_NODUMP;
846 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
847 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
848 di_flags |= XFS_DIFLAG_RTINHERIT;
849 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
850 di_flags |= XFS_DIFLAG_NOSYMLINKS;
852 if (vap->va_xflags & XFS_XFLAG_REALTIME) {
853 di_flags |= XFS_DIFLAG_REALTIME;
854 ip->i_iocore.io_flags |= XFS_IOCORE_RT;
856 ip->i_iocore.io_flags &= ~XFS_IOCORE_RT;
859 ip->i_d.di_flags = di_flags;
861 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
862 timeflags |= XFS_ICHGTIME_CHG;
866 * Change file inode change time only if XFS_AT_CTIME set
867 * AND we have been called by a DMI function.
870 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
871 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
872 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
873 ip->i_update_core = 1;
874 timeflags &= ~XFS_ICHGTIME_CHG;
878 * Send out timestamp changes that need to be set to the
879 * current time. Not done when called by a DMI function.
881 if (timeflags && !(flags & ATTR_DMI))
882 xfs_ichgtime(ip, timeflags);
884 XFS_STATS_INC(xs_ig_attrchg);
887 * If this is a synchronous mount, make sure that the
888 * transaction goes to disk before returning to the user.
889 * This is slightly sub-optimal in that truncates require
890 * two sync transactions instead of one for wsync filesytems.
891 * One for the truncate and one for the timestamps since we
892 * don't want to change the timestamps unless we're sure the
893 * truncate worked. Truncates are less than 1% of the laddis
894 * mix so this probably isn't worth the trouble to optimize.
898 if (mp->m_flags & XFS_MOUNT_WSYNC)
899 xfs_trans_set_sync(tp);
901 code = xfs_trans_commit(tp, commit_flags, NULL);
905 * If the (regular) file's mandatory locking mode changed, then
906 * notify the vnode. We do this under the inode lock to prevent
907 * racing calls to vop_vnode_change.
909 mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
910 if (mandlock_before != mandlock_after) {
911 VOP_VNODE_CHANGE(vp, VCHANGE_FLAGS_ENF_LOCKING,
915 xfs_iunlock(ip, lock_flags);
918 * Release any dquot(s) the inode had kept before chown.
920 XFS_QM_DQRELE(mp, olddquot1);
921 XFS_QM_DQRELE(mp, olddquot2);
922 XFS_QM_DQRELE(mp, udqp);
923 XFS_QM_DQRELE(mp, gdqp);
929 if (DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_ATTRIBUTE) &&
930 !(flags & ATTR_DMI)) {
931 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
932 NULL, DM_RIGHT_NULL, NULL, NULL,
933 0, 0, AT_DELAY_FLAG(flags));
938 commit_flags |= XFS_TRANS_ABORT;
941 XFS_QM_DQRELE(mp, udqp);
942 XFS_QM_DQRELE(mp, gdqp);
944 xfs_trans_cancel(tp, commit_flags);
946 if (lock_flags != 0) {
947 xfs_iunlock(ip, lock_flags);
955 * Null conversion from vnode mode bits to inode mode bits, as in efs.
966 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
967 (inst_t *)__return_address);
969 ip = XFS_BHVTOI(bdp);
970 xfs_ilock(ip, XFS_ILOCK_SHARED);
971 error = xfs_iaccess(ip, mode, credp);
972 xfs_iunlock(ip, XFS_ILOCK_SHARED);
996 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1002 vp = BHV_TO_VNODE(bdp);
1003 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1005 ip = XFS_BHVTOI(bdp);
1008 if (XFS_FORCED_SHUTDOWN(mp))
1009 return XFS_ERROR(EIO);
1011 xfs_ilock(ip, XFS_ILOCK_SHARED);
1013 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
1015 offset = uiop->uio_offset;
1016 count = uiop->uio_resid;
1019 error = XFS_ERROR(EINVAL);
1027 if (!(ioflags & IO_INVIS)) {
1028 xfs_ichgtime(ip, XFS_ICHGTIME_ACC);
1032 * See if the symlink is stored inline.
1034 pathlen = (int)ip->i_d.di_size;
1036 if (ip->i_df.if_flags & XFS_IFINLINE) {
1037 error = uio_read(ip->i_df.if_u1.if_data, pathlen, uiop);
1041 * Symlink not inline. Call bmap to get it in.
1043 nmaps = SYMLINK_MAPS;
1045 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen),
1046 0, NULL, 0, mval, &nmaps, NULL);
1052 for (n = 0; n < nmaps; n++) {
1053 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1054 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1055 bp = xfs_buf_read(mp->m_ddev_targp, d,
1056 BTOBB(byte_cnt), 0);
1057 error = XFS_BUF_GETERROR(bp);
1059 xfs_ioerror_alert("xfs_readlink",
1060 ip->i_mount, bp, XFS_BUF_ADDR(bp));
1064 if (pathlen < byte_cnt)
1066 pathlen -= byte_cnt;
1068 error = uio_read(XFS_BUF_PTR(bp), byte_cnt, uiop);
1077 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1086 * This is called to sync the inode and its data out to disk.
1087 * We need to hold the I/O lock while flushing the data, and
1088 * the inode lock while flushing the inode. The inode lock CANNOT
1089 * be held while flushing the data, so acquire after we're done
1104 vn_trace_entry(BHV_TO_VNODE(bdp),
1105 __FUNCTION__, (inst_t *)__return_address);
1107 ip = XFS_BHVTOI(bdp);
1109 ASSERT(start >= 0 && stop >= -1);
1111 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1112 return XFS_ERROR(EIO);
1115 * We always need to make sure that the required inode state
1116 * is safe on disk. The vnode might be clean but because
1117 * of committed transactions that haven't hit the disk yet.
1118 * Likewise, there could be unflushed non-transactional
1119 * changes to the inode core that have to go to disk.
1121 * The following code depends on one assumption: that
1122 * any transaction that changes an inode logs the core
1123 * because it has to change some field in the inode core
1124 * (typically nextents or nblocks). That assumption
1125 * implies that any transactions against an inode will
1126 * catch any non-transactional updates. If inode-altering
1127 * transactions exist that violate this assumption, the
1128 * code breaks. Right now, it figures that if the involved
1129 * update_* field is clear and the inode is unpinned, the
1130 * inode is clean. Either it's been flushed or it's been
1131 * committed and the commit has hit the disk unpinning the inode.
1132 * (Note that xfs_inode_item_format() called at commit clears
1133 * the update_* fields.)
1135 xfs_ilock(ip, XFS_ILOCK_SHARED);
1137 /* If we are flushing data then we care about update_size
1138 * being set, otherwise we care about update_core
1140 if ((flag & FSYNC_DATA) ?
1141 (ip->i_update_size == 0) :
1142 (ip->i_update_core == 0)) {
1144 * Timestamps/size haven't changed since last inode
1145 * flush or inode transaction commit. That means
1146 * either nothing got written or a transaction
1147 * committed which caught the updates. If the
1148 * latter happened and the transaction hasn't
1149 * hit the disk yet, the inode will be still
1150 * be pinned. If it is, force the log.
1153 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1155 if (xfs_ipincount(ip)) {
1156 xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
1158 ((flag & FSYNC_WAIT)
1159 ? XFS_LOG_SYNC : 0));
1164 * Kick off a transaction to log the inode
1165 * core to get the updates. Make it
1166 * sync if FSYNC_WAIT is passed in (which
1167 * is done by everybody but specfs). The
1168 * sync transaction will also force the log.
1170 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1171 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1172 if ((error = xfs_trans_reserve(tp, 0,
1173 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1175 xfs_trans_cancel(tp, 0);
1178 xfs_ilock(ip, XFS_ILOCK_EXCL);
1181 * Note - it's possible that we might have pushed
1182 * ourselves out of the way during trans_reserve
1183 * which would flush the inode. But there's no
1184 * guarantee that the inode buffer has actually
1185 * gone out yet (it's delwri). Plus the buffer
1186 * could be pinned anyway if it's part of an
1187 * inode in another recent transaction. So we
1188 * play it safe and fire off the transaction anyway.
1190 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1191 xfs_trans_ihold(tp, ip);
1192 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1193 if (flag & FSYNC_WAIT)
1194 xfs_trans_set_sync(tp);
1195 error = xfs_trans_commit(tp, 0, NULL);
1197 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1203 * This is called by xfs_inactive to free any blocks beyond eof,
1204 * when the link count isn't zero.
1207 xfs_inactive_free_eofblocks(
1213 xfs_fileoff_t end_fsb;
1214 xfs_fileoff_t last_fsb;
1215 xfs_filblks_t map_len;
1217 xfs_bmbt_irec_t imap;
1220 * Figure out if there are any blocks beyond the end
1221 * of the file. If not, then there is nothing to do.
1223 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_d.di_size));
1224 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1225 map_len = last_fsb - end_fsb;
1230 xfs_ilock(ip, XFS_ILOCK_SHARED);
1231 error = xfs_bmapi(NULL, ip, end_fsb, map_len, 0,
1232 NULL, 0, &imap, &nimaps, NULL);
1233 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1235 if (!error && (nimaps != 0) &&
1236 (imap.br_startblock != HOLESTARTBLOCK)) {
1238 * Attach the dquots to the inode up front.
1240 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1244 * There are blocks after the end of file.
1245 * Free them up now by truncating the file to
1248 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1251 * Do the xfs_itruncate_start() call before
1252 * reserving any log space because
1253 * itruncate_start will call into the buffer
1254 * cache and we can't
1255 * do that within a transaction.
1257 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1258 xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
1261 error = xfs_trans_reserve(tp, 0,
1262 XFS_ITRUNCATE_LOG_RES(mp),
1263 0, XFS_TRANS_PERM_LOG_RES,
1264 XFS_ITRUNCATE_LOG_COUNT);
1266 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1267 xfs_trans_cancel(tp, 0);
1268 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1272 xfs_ilock(ip, XFS_ILOCK_EXCL);
1273 xfs_trans_ijoin(tp, ip,
1276 xfs_trans_ihold(tp, ip);
1278 error = xfs_itruncate_finish(&tp, ip,
1283 * If we get an error at this point we
1284 * simply don't bother truncating the file.
1287 xfs_trans_cancel(tp,
1288 (XFS_TRANS_RELEASE_LOG_RES |
1291 error = xfs_trans_commit(tp,
1292 XFS_TRANS_RELEASE_LOG_RES,
1295 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1301 * Free a symlink that has blocks associated with it.
1304 xfs_inactive_symlink_rmt(
1312 xfs_fsblock_t first_block;
1313 xfs_bmap_free_t free_list;
1316 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1324 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1326 * We're freeing a symlink that has some
1327 * blocks allocated to it. Free the
1328 * blocks here. We know that we've got
1329 * either 1 or 2 extents and that we can
1330 * free them all in one bunmapi call.
1332 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1333 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1334 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1335 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1336 xfs_trans_cancel(tp, 0);
1341 * Lock the inode, fix the size, and join it to the transaction.
1342 * Hold it so in the normal path, we still have it locked for
1343 * the second transaction. In the error paths we need it
1344 * held so the cancel won't rele it, see below.
1346 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1347 size = (int)ip->i_d.di_size;
1348 ip->i_d.di_size = 0;
1349 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1350 xfs_trans_ihold(tp, ip);
1351 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1353 * Find the block(s) so we can inval and unmap them.
1356 XFS_BMAP_INIT(&free_list, &first_block);
1357 nmaps = sizeof(mval) / sizeof(mval[0]);
1358 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1359 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
1363 * Invalidate the block(s).
1365 for (i = 0; i < nmaps; i++) {
1366 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1367 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1368 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1369 xfs_trans_binval(tp, bp);
1372 * Unmap the dead block(s) to the free_list.
1374 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
1375 &first_block, &free_list, &done)))
1379 * Commit the first transaction. This logs the EFI and the inode.
1381 if ((error = xfs_bmap_finish(&tp, &free_list, first_block, &committed)))
1384 * The transaction must have been committed, since there were
1385 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1386 * The new tp has the extent freeing and EFDs.
1390 * The first xact was committed, so add the inode to the new one.
1391 * Mark it dirty so it will be logged and moved forward in the log as
1392 * part of every commit.
1394 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1395 xfs_trans_ihold(tp, ip);
1396 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1398 * Get a new, empty transaction to return to our caller.
1400 ntp = xfs_trans_dup(tp);
1402 * Commit the transaction containing extent freeing and EFD's.
1403 * If we get an error on the commit here or on the reserve below,
1404 * we need to unlock the inode since the new transaction doesn't
1405 * have the inode attached.
1407 error = xfs_trans_commit(tp, 0, NULL);
1410 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1414 * Remove the memory for extent descriptions (just bookkeeping).
1416 if (ip->i_df.if_bytes)
1417 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1418 ASSERT(ip->i_df.if_bytes == 0);
1420 * Put an itruncate log reservation in the new transaction
1423 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1424 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1425 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1429 * Return with the inode locked but not joined to the transaction.
1435 xfs_bmap_cancel(&free_list);
1438 * Have to come here with the inode locked and either
1439 * (held and in the transaction) or (not in the transaction).
1440 * If the inode isn't held then cancel would iput it, but
1441 * that's wrong since this is inactive and the vnode ref
1442 * count is 0 already.
1443 * Cancel won't do anything to the inode if held, but it still
1444 * needs to be locked until the cancel is done, if it was
1445 * joined to the transaction.
1447 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1448 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1455 xfs_inactive_symlink_local(
1461 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1463 * We're freeing a symlink which fit into
1464 * the inode. Just free the memory used
1465 * to hold the old symlink.
1467 error = xfs_trans_reserve(*tpp, 0,
1468 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1469 0, XFS_TRANS_PERM_LOG_RES,
1470 XFS_ITRUNCATE_LOG_COUNT);
1473 xfs_trans_cancel(*tpp, 0);
1477 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1480 * Zero length symlinks _can_ exist.
1482 if (ip->i_df.if_bytes > 0) {
1483 xfs_idata_realloc(ip,
1484 -(ip->i_df.if_bytes),
1486 ASSERT(ip->i_df.if_bytes == 0);
1503 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1506 ASSERT(ip->i_d.di_forkoff != 0);
1507 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
1508 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1510 error = xfs_attr_inactive(ip);
1513 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1514 return (error); /* goto out*/
1517 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1518 error = xfs_trans_reserve(tp, 0,
1519 XFS_IFREE_LOG_RES(mp),
1520 0, XFS_TRANS_PERM_LOG_RES,
1521 XFS_INACTIVE_LOG_COUNT);
1523 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1524 xfs_trans_cancel(tp, 0);
1526 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1530 xfs_ilock(ip, XFS_ILOCK_EXCL);
1531 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1532 xfs_trans_ihold(tp, ip);
1533 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1535 ASSERT(ip->i_d.di_anextents == 0);
1550 vp = BHV_TO_VNODE(bdp);
1551 ip = XFS_BHVTOI(bdp);
1553 if ((vp->v_type != VREG) || (ip->i_d.di_mode == 0)) {
1557 /* If this is a read-only mount, don't do this (would generate I/O) */
1558 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1561 #ifdef HAVE_REFCACHE
1562 /* If we are in the NFS reference cache then don't do this now */
1569 if (ip->i_d.di_nlink != 0) {
1570 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1571 ((ip->i_d.di_size > 0) || (VN_CACHED(vp) > 0)) &&
1572 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1573 (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)))) {
1574 if ((error = xfs_inactive_free_eofblocks(mp, ip)))
1576 /* Update linux inode block count after free above */
1577 LINVFS_GET_IP(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1578 ip->i_d.di_nblocks + ip->i_delayed_blks);
1588 * This is called when the vnode reference count for the vnode
1589 * goes to zero. If the file has been unlinked, then it must
1590 * now be truncated. Also, we clear all of the read-ahead state
1591 * kept for the inode here since the file is now closed.
1600 xfs_bmap_free_t free_list;
1601 xfs_fsblock_t first_block;
1608 vp = BHV_TO_VNODE(bdp);
1609 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1611 ip = XFS_BHVTOI(bdp);
1614 * If the inode is already free, then there can be nothing
1617 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1618 ASSERT(ip->i_df.if_real_bytes == 0);
1619 ASSERT(ip->i_df.if_broot_bytes == 0);
1620 return VN_INACTIVE_CACHE;
1624 * Only do a truncate if it's a regular file with
1625 * some actual space in it. It's OK to look at the
1626 * inode's fields without the lock because we're the
1627 * only one with a reference to the inode.
1629 truncate = ((ip->i_d.di_nlink == 0) &&
1630 ((ip->i_d.di_size != 0) || (ip->i_d.di_nextents > 0)) &&
1631 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1635 if (ip->i_d.di_nlink == 0 &&
1636 DM_EVENT_ENABLED(vp->v_vfsp, ip, DM_EVENT_DESTROY)) {
1637 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1642 /* If this is a read-only mount, don't do this (would generate I/O) */
1643 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1646 if (ip->i_d.di_nlink != 0) {
1647 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1648 ((ip->i_d.di_size > 0) || (VN_CACHED(vp) > 0)) &&
1649 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1650 (!(ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)) ||
1651 (ip->i_delayed_blks != 0))) {
1652 if ((error = xfs_inactive_free_eofblocks(mp, ip)))
1653 return (VN_INACTIVE_CACHE);
1654 /* Update linux inode block count after free above */
1655 LINVFS_GET_IP(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1656 ip->i_d.di_nblocks + ip->i_delayed_blks);
1661 ASSERT(ip->i_d.di_nlink == 0);
1663 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1664 return (VN_INACTIVE_CACHE);
1666 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1669 * Do the xfs_itruncate_start() call before
1670 * reserving any log space because itruncate_start
1671 * will call into the buffer cache and we can't
1672 * do that within a transaction.
1674 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1676 xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1678 error = xfs_trans_reserve(tp, 0,
1679 XFS_ITRUNCATE_LOG_RES(mp),
1680 0, XFS_TRANS_PERM_LOG_RES,
1681 XFS_ITRUNCATE_LOG_COUNT);
1683 /* Don't call itruncate_cleanup */
1684 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1685 xfs_trans_cancel(tp, 0);
1686 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1687 return (VN_INACTIVE_CACHE);
1690 xfs_ilock(ip, XFS_ILOCK_EXCL);
1691 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1692 xfs_trans_ihold(tp, ip);
1695 * normally, we have to run xfs_itruncate_finish sync.
1696 * But if filesystem is wsync and we're in the inactive
1697 * path, then we know that nlink == 0, and that the
1698 * xaction that made nlink == 0 is permanently committed
1699 * since xfs_remove runs as a synchronous transaction.
1701 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1702 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1705 xfs_trans_cancel(tp,
1706 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1707 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1708 return (VN_INACTIVE_CACHE);
1710 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1713 * If we get an error while cleaning up a
1714 * symlink we bail out.
1716 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1717 xfs_inactive_symlink_rmt(ip, &tp) :
1718 xfs_inactive_symlink_local(ip, &tp);
1722 return (VN_INACTIVE_CACHE);
1725 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1726 xfs_trans_ihold(tp, ip);
1728 error = xfs_trans_reserve(tp, 0,
1729 XFS_IFREE_LOG_RES(mp),
1730 0, XFS_TRANS_PERM_LOG_RES,
1731 XFS_INACTIVE_LOG_COUNT);
1733 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1734 xfs_trans_cancel(tp, 0);
1735 return (VN_INACTIVE_CACHE);
1738 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1739 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1740 xfs_trans_ihold(tp, ip);
1744 * If there are attributes associated with the file
1745 * then blow them away now. The code calls a routine
1746 * that recursively deconstructs the attribute fork.
1747 * We need to just commit the current transaction
1748 * because we can't use it for xfs_attr_inactive().
1750 if (ip->i_d.di_anextents > 0) {
1751 error = xfs_inactive_attrs(ip, &tp);
1753 * If we got an error, the transaction is already
1754 * cancelled, and the inode is unlocked. Just get out.
1757 return (VN_INACTIVE_CACHE);
1758 } else if (ip->i_afp) {
1759 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1765 XFS_BMAP_INIT(&free_list, &first_block);
1766 error = xfs_ifree(tp, ip, &free_list);
1769 * If we fail to free the inode, shut down. The cancel
1770 * might do that, we need to make sure. Otherwise the
1771 * inode might be lost for a long time or forever.
1773 if (!XFS_FORCED_SHUTDOWN(mp)) {
1775 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1776 error, mp->m_fsname);
1777 xfs_force_shutdown(mp, XFS_METADATA_IO_ERROR);
1779 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1782 * Credit the quota account(s). The inode is gone.
1784 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1787 * Just ignore errors at this point. There is
1788 * nothing we can do except to try to keep going.
1790 (void) xfs_bmap_finish(&tp, &free_list, first_block,
1792 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
1795 * Release the dquots held by inode, if any.
1797 XFS_QM_DQDETACH(mp, ip);
1799 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1802 return VN_INACTIVE_CACHE;
1811 bhv_desc_t *dir_bdp,
1818 xfs_inode_t *dp, *ip;
1824 dir_vp = BHV_TO_VNODE(dir_bdp);
1825 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1827 dp = XFS_BHVTOI(dir_bdp);
1829 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1830 return XFS_ERROR(EIO);
1832 lock_mode = xfs_ilock_map_shared(dp);
1833 error = xfs_dir_lookup_int(dir_bdp, lock_mode, dentry, &e_inum, &ip);
1835 *vpp = XFS_ITOV(ip);
1838 xfs_iunlock_map_shared(dp, lock_mode);
1844 * xfs_create (create a new file).
1848 bhv_desc_t *dir_bdp,
1854 char *name = VNAME(dentry);
1856 xfs_inode_t *dp, *ip;
1862 xfs_bmap_free_t free_list;
1863 xfs_fsblock_t first_block;
1864 boolean_t dp_joined_to_trans;
1865 int dm_event_sent = 0;
1869 struct xfs_dquot *udqp, *gdqp;
1875 dir_vp = BHV_TO_VNODE(dir_bdp);
1876 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1878 dp = XFS_BHVTOI(dir_bdp);
1881 dm_di_mode = vap->va_mode|VTTOIF(vap->va_type);
1882 namelen = VNAMELEN(dentry);
1884 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
1885 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1886 dir_vp, DM_RIGHT_NULL, NULL,
1887 DM_RIGHT_NULL, name, NULL,
1895 if (XFS_FORCED_SHUTDOWN(mp))
1896 return XFS_ERROR(EIO);
1898 /* Return through std_return after this point. */
1901 if (vap->va_mask & XFS_AT_PROJID)
1902 prid = (xfs_prid_t)vap->va_projid;
1904 prid = (xfs_prid_t)dfltprid;
1907 * Make sure that we have allocated dquot(s) on disk.
1909 error = XFS_QM_DQVOPALLOC(mp, dp,
1910 current_fsuid(credp), current_fsgid(credp),
1911 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1916 dp_joined_to_trans = B_FALSE;
1918 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1919 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1920 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1922 * Initially assume that the file does not exist and
1923 * reserve the resources for that case. If that is not
1924 * the case we'll drop the one we have and get a more
1925 * appropriate transaction later.
1927 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1928 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1929 if (error == ENOSPC) {
1931 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1932 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1940 xfs_ilock(dp, XFS_ILOCK_EXCL);
1942 XFS_BMAP_INIT(&free_list, &first_block);
1947 * Reserve disk quota and the inode.
1949 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1954 (error = XFS_DIR_CANENTER(mp, tp, dp, name, namelen)))
1956 rdev = (vap->va_mask & XFS_AT_RDEV) ? vap->va_rdev : 0;
1957 error = xfs_dir_ialloc(&tp, dp,
1958 MAKEIMODE(vap->va_type,vap->va_mode), 1,
1959 rdev, credp, prid, resblks > 0,
1962 if (error == ENOSPC)
1969 * At this point, we've gotten a newly allocated inode.
1970 * It is locked (and joined to the transaction).
1973 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
1976 * Now we join the directory inode to the transaction.
1977 * We do not do it earlier because xfs_dir_ialloc
1978 * might commit the previous transaction (and release
1983 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
1984 dp_joined_to_trans = B_TRUE;
1986 error = XFS_DIR_CREATENAME(mp, tp, dp, name, namelen, ip->i_ino,
1987 &first_block, &free_list,
1988 resblks ? resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
1990 ASSERT(error != ENOSPC);
1993 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
1994 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
1997 * If this is a synchronous mount, make sure that the
1998 * create transaction goes to disk before returning to
2001 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2002 xfs_trans_set_sync(tp);
2008 * Attach the dquot(s) to the inodes and modify them incore.
2009 * These ids of the inode couldn't have changed since the new
2010 * inode has been locked ever since it was created.
2012 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2015 * xfs_trans_commit normally decrements the vnode ref count
2016 * when it unlocks the inode. Since we want to return the
2017 * vnode to the caller, we bump the vnode ref count now.
2022 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2024 xfs_bmap_cancel(&free_list);
2028 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2035 XFS_QM_DQRELE(mp, udqp);
2036 XFS_QM_DQRELE(mp, gdqp);
2039 * Propogate the fact that the vnode changed after the
2040 * xfs_inode locks have been released.
2042 VOP_VNODE_CHANGE(vp, VCHANGE_FLAGS_TRUNCATED, 3);
2046 /* Fallthrough to std_return with error = 0 */
2049 if ( (*vpp || (error != 0 && dm_event_sent != 0)) &&
2050 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2051 DM_EVENT_POSTCREATE)) {
2052 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2053 dir_vp, DM_RIGHT_NULL,
2055 DM_RIGHT_NULL, name, NULL,
2056 dm_di_mode, error, 0);
2061 cancel_flags |= XFS_TRANS_ABORT;
2066 xfs_trans_cancel(tp, cancel_flags);
2068 if (!dp_joined_to_trans && (dp != NULL))
2069 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2070 XFS_QM_DQRELE(mp, udqp);
2071 XFS_QM_DQRELE(mp, gdqp);
2077 * Wait until after the current transaction is aborted to
2078 * release the inode. This prevents recursive transactions
2079 * and deadlocks from xfs_inactive.
2081 cancel_flags |= XFS_TRANS_ABORT;
2082 xfs_trans_cancel(tp, cancel_flags);
2085 XFS_QM_DQRELE(mp, udqp);
2086 XFS_QM_DQRELE(mp, gdqp);
2093 * Some counters to see if (and how often) we are hitting some deadlock
2094 * prevention code paths.
2098 int xfs_rm_lock_delays;
2099 int xfs_rm_attempts;
2103 * The following routine will lock the inodes associated with the
2104 * directory and the named entry in the directory. The locks are
2105 * acquired in increasing inode number.
2107 * If the entry is "..", then only the directory is locked. The
2108 * vnode ref count will still include that from the .. entry in
2111 * There is a deadlock we need to worry about. If the locked directory is
2112 * in the AIL, it might be blocking up the log. The next inode we lock
2113 * could be already locked by another thread waiting for log space (e.g
2114 * a permanent log reservation with a long running transaction (see
2115 * xfs_itruncate_finish)). To solve this, we must check if the directory
2116 * is in the ail and use lock_nowait. If we can't lock, we need to
2117 * drop the inode lock on the directory and try again. xfs_iunlock will
2118 * potentially push the tail if we were holding up the log.
2121 xfs_lock_dir_and_entry(
2124 xfs_inode_t *ip) /* inode of entry 'name' */
2128 xfs_inode_t *ips[2];
2137 xfs_ilock(dp, XFS_ILOCK_EXCL);
2144 * We want to lock in increasing inum. Since we've already
2145 * acquired the lock on the directory, we may need to release
2146 * if if the inum of the entry turns out to be less.
2148 if (e_inum > dp->i_ino) {
2150 * We are already in the right order, so just
2151 * lock on the inode of the entry.
2152 * We need to use nowait if dp is in the AIL.
2155 lp = (xfs_log_item_t *)dp->i_itemp;
2156 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2157 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2164 * Unlock dp and try again.
2165 * xfs_iunlock will try to push the tail
2166 * if the inode is in the AIL.
2169 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2171 if ((attempts % 5) == 0) {
2172 delay(1); /* Don't just spin the CPU */
2174 xfs_rm_lock_delays++;
2180 xfs_ilock(ip, XFS_ILOCK_EXCL);
2182 } else if (e_inum < dp->i_ino) {
2183 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2187 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2189 /* else e_inum == dp->i_ino */
2190 /* This can happen if we're asked to lock /x/..
2191 * the entry is "..", which is also the parent directory.
2199 int xfs_small_retries;
2200 int xfs_middle_retries;
2201 int xfs_lots_retries;
2202 int xfs_lock_delays;
2206 * The following routine will lock n inodes in exclusive mode.
2207 * We assume the caller calls us with the inodes in i_ino order.
2209 * We need to detect deadlock where an inode that we lock
2210 * is in the AIL and we start waiting for another inode that is locked
2211 * by a thread in a long running transaction (such as truncate). This can
2212 * result in deadlock since the long running trans might need to wait
2213 * for the inode we just locked in order to push the tail and free space
2223 int attempts = 0, i, j, try_lock;
2226 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2237 for (; i < inodes; i++) {
2240 if (i && (ips[i] == ips[i-1])) /* Already locked */
2244 * If try_lock is not set yet, make sure all locked inodes
2245 * are not in the AIL.
2246 * If any are, set try_lock to be used later.
2250 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2251 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2252 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2259 * If any of the previous locks we have locked is in the AIL,
2260 * we must TRY to get the second and subsequent locks. If
2261 * we can't get any, we must release all we have
2266 /* try_lock must be 0 if i is 0. */
2268 * try_lock means we have an inode locked
2269 * that is in the AIL.
2272 if (!xfs_ilock_nowait(ips[i], lock_mode)) {
2276 * Unlock all previous guys and try again.
2277 * xfs_iunlock will try to push the tail
2278 * if the inode is in the AIL.
2281 for(j = i - 1; j >= 0; j--) {
2284 * Check to see if we've already
2285 * unlocked this one.
2286 * Not the first one going back,
2287 * and the inode ptr is the same.
2289 if ((j != (i - 1)) && ips[j] ==
2293 xfs_iunlock(ips[j], lock_mode);
2296 if ((attempts % 5) == 0) {
2297 delay(1); /* Don't just spin the CPU */
2307 xfs_ilock(ips[i], lock_mode);
2313 if (attempts < 5) xfs_small_retries++;
2314 else if (attempts < 100) xfs_middle_retries++;
2315 else xfs_lots_retries++;
2323 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2324 int remove_which_error_return = 0;
2326 #define REMOVE_DEBUG_TRACE(x)
2327 #endif /* ! DEBUG */
2336 bhv_desc_t *dir_bdp,
2341 char *name = VNAME(dentry);
2342 xfs_inode_t *dp, *ip;
2343 xfs_trans_t *tp = NULL;
2346 xfs_bmap_free_t free_list;
2347 xfs_fsblock_t first_block;
2355 dir_vp = BHV_TO_VNODE(dir_bdp);
2356 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2358 dp = XFS_BHVTOI(dir_bdp);
2361 if (XFS_FORCED_SHUTDOWN(mp))
2362 return XFS_ERROR(EIO);
2364 namelen = VNAMELEN(dentry);
2366 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
2367 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2368 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2369 name, NULL, 0, 0, 0);
2374 /* From this point on, return through std_return */
2378 * We need to get a reference to ip before we get our log
2379 * reservation. The reason for this is that we cannot call
2380 * xfs_iget for an inode for which we do not have a reference
2381 * once we've acquired a log reservation. This is because the
2382 * inode we are trying to get might be in xfs_inactive going
2383 * for a log reservation. Since we'll have to wait for the
2384 * inactive code to complete before returning from xfs_iget,
2385 * we need to make sure that we don't have log space reserved
2386 * when we call xfs_iget. Instead we get an unlocked referece
2387 * to the inode before getting our log reservation.
2389 error = xfs_get_dir_entry(dentry, &ip);
2391 REMOVE_DEBUG_TRACE(__LINE__);
2395 dm_di_mode = ip->i_d.di_mode;
2397 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2401 error = XFS_QM_DQATTACH(mp, dp, 0);
2402 if (!error && dp != ip)
2403 error = XFS_QM_DQATTACH(mp, ip, 0);
2405 REMOVE_DEBUG_TRACE(__LINE__);
2410 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2411 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2413 * We try to get the real space reservation first,
2414 * allowing for directory btree deletion(s) implying
2415 * possible bmap insert(s). If we can't get the space
2416 * reservation then we use 0 instead, and avoid the bmap
2417 * btree insert(s) in the directory code by, if the bmap
2418 * insert tries to happen, instead trimming the LAST
2419 * block from the directory.
2421 resblks = XFS_REMOVE_SPACE_RES(mp);
2422 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2423 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2424 if (error == ENOSPC) {
2426 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2427 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2430 ASSERT(error != ENOSPC);
2431 REMOVE_DEBUG_TRACE(__LINE__);
2432 xfs_trans_cancel(tp, 0);
2437 error = xfs_lock_dir_and_entry(dp, dentry, ip);
2439 REMOVE_DEBUG_TRACE(__LINE__);
2440 xfs_trans_cancel(tp, cancel_flags);
2446 * At this point, we've gotten both the directory and the entry
2449 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2452 * Increment vnode ref count only in this case since
2453 * there's an extra vnode reference in the case where
2457 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2461 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2463 XFS_BMAP_INIT(&free_list, &first_block);
2464 error = XFS_DIR_REMOVENAME(mp, tp, dp, name, namelen, ip->i_ino,
2465 &first_block, &free_list, 0);
2467 ASSERT(error != ENOENT);
2468 REMOVE_DEBUG_TRACE(__LINE__);
2471 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2474 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2476 error = xfs_droplink(tp, ip);
2478 REMOVE_DEBUG_TRACE(__LINE__);
2482 /* Determine if this is the last link while
2483 * we are in the transaction.
2485 link_zero = (ip)->i_d.di_nlink==0;
2488 * Take an extra ref on the inode so that it doesn't
2489 * go to xfs_inactive() from within the commit.
2494 * If this is a synchronous mount, make sure that the
2495 * remove transaction goes to disk before returning to
2498 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2499 xfs_trans_set_sync(tp);
2502 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2504 REMOVE_DEBUG_TRACE(__LINE__);
2508 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2515 * Before we drop our extra reference to the inode, purge it
2516 * from the refcache if it is there. By waiting until afterwards
2517 * to do the IRELE, we ensure that we won't go inactive in the
2518 * xfs_refcache_purge_ip routine (although that would be OK).
2520 xfs_refcache_purge_ip(ip);
2522 vn_trace_exit(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2525 * Let interposed file systems know about removed links.
2527 VOP_LINK_REMOVED(XFS_ITOV(ip), dir_vp, link_zero);
2531 /* Fall through to std_return with error = 0 */
2533 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp,
2534 DM_EVENT_POSTREMOVE)) {
2535 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2536 dir_vp, DM_RIGHT_NULL,
2537 NULL, DM_RIGHT_NULL,
2538 name, NULL, dm_di_mode, error, 0);
2543 xfs_bmap_cancel(&free_list);
2544 cancel_flags |= XFS_TRANS_ABORT;
2545 xfs_trans_cancel(tp, cancel_flags);
2550 * In this case make sure to not release the inode until after
2551 * the current transaction is aborted. Releasing it beforehand
2552 * can cause us to go to xfs_inactive and start a recursive
2553 * transaction which can easily deadlock with the current one.
2555 xfs_bmap_cancel(&free_list);
2556 cancel_flags |= XFS_TRANS_ABORT;
2557 xfs_trans_cancel(tp, cancel_flags);
2560 * Before we drop our extra reference to the inode, purge it
2561 * from the refcache if it is there. By waiting until afterwards
2562 * to do the IRELE, we ensure that we won't go inactive in the
2563 * xfs_refcache_purge_ip routine (although that would be OK).
2565 xfs_refcache_purge_ip(ip);
2579 bhv_desc_t *target_dir_bdp,
2584 xfs_inode_t *tdp, *sip;
2587 xfs_inode_t *ips[2];
2589 xfs_bmap_free_t free_list;
2590 xfs_fsblock_t first_block;
2593 vnode_t *target_dir_vp;
2594 bhv_desc_t *src_bdp;
2596 char *target_name = VNAME(dentry);
2599 target_dir_vp = BHV_TO_VNODE(target_dir_bdp);
2600 vn_trace_entry(target_dir_vp, __FUNCTION__, (inst_t *)__return_address);
2601 vn_trace_entry(src_vp, __FUNCTION__, (inst_t *)__return_address);
2603 target_namelen = VNAMELEN(dentry);
2604 if (src_vp->v_type == VDIR)
2605 return XFS_ERROR(EPERM);
2608 * For now, manually find the XFS behavior descriptor for
2609 * the source vnode. If it doesn't exist then something
2610 * is wrong and we should just return an error.
2611 * Eventually we need to figure out how link is going to
2612 * work in the face of stacked vnodes.
2614 src_bdp = vn_bhv_lookup_unlocked(VN_BHV_HEAD(src_vp), &xfs_vnodeops);
2615 if (src_bdp == NULL) {
2616 return XFS_ERROR(EXDEV);
2618 sip = XFS_BHVTOI(src_bdp);
2619 tdp = XFS_BHVTOI(target_dir_bdp);
2621 if (XFS_FORCED_SHUTDOWN(mp))
2622 return XFS_ERROR(EIO);
2624 if (DM_EVENT_ENABLED(src_vp->v_vfsp, tdp, DM_EVENT_LINK)) {
2625 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2626 target_dir_vp, DM_RIGHT_NULL,
2627 src_vp, DM_RIGHT_NULL,
2628 target_name, NULL, 0, 0, 0);
2633 /* Return through std_return after this point. */
2635 error = XFS_QM_DQATTACH(mp, sip, 0);
2636 if (!error && sip != tdp)
2637 error = XFS_QM_DQATTACH(mp, tdp, 0);
2641 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2642 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2643 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2644 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2645 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2646 if (error == ENOSPC) {
2648 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2649 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2656 if (sip->i_ino < tdp->i_ino) {
2664 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2667 * Increment vnode ref counts since xfs_trans_commit &
2668 * xfs_trans_cancel will both unlock the inodes and
2669 * decrement the associated ref counts.
2672 VN_HOLD(target_dir_vp);
2673 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2674 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2677 * If the source has too many links, we can't make any more to it.
2679 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2680 error = XFS_ERROR(EMLINK);
2685 (error = XFS_DIR_CANENTER(mp, tp, tdp, target_name,
2689 XFS_BMAP_INIT(&free_list, &first_block);
2691 error = XFS_DIR_CREATENAME(mp, tp, tdp, target_name, target_namelen,
2692 sip->i_ino, &first_block, &free_list,
2696 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2698 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2700 error = xfs_bumplink(tp, sip);
2706 * If this is a synchronous mount, make sure that the
2707 * link transaction goes to disk before returning to
2710 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2711 xfs_trans_set_sync(tp);
2714 error = xfs_bmap_finish (&tp, &free_list, first_block, &committed);
2716 xfs_bmap_cancel(&free_list);
2720 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2725 /* Fall through to std_return with error = 0. */
2727 if (DM_EVENT_ENABLED(src_vp->v_vfsp, sip,
2728 DM_EVENT_POSTLINK)) {
2729 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2730 target_dir_vp, DM_RIGHT_NULL,
2731 src_vp, DM_RIGHT_NULL,
2732 target_name, NULL, 0, error, 0);
2737 cancel_flags |= XFS_TRANS_ABORT;
2740 xfs_trans_cancel(tp, cancel_flags);
2750 bhv_desc_t *dir_bdp,
2756 char *dir_name = VNAME(dentry);
2758 xfs_inode_t *cdp; /* inode of created dir */
2759 vnode_t *cvp; /* vnode of created dir */
2765 xfs_bmap_free_t free_list;
2766 xfs_fsblock_t first_block;
2768 boolean_t dp_joined_to_trans;
2769 boolean_t created = B_FALSE;
2770 int dm_event_sent = 0;
2772 struct xfs_dquot *udqp, *gdqp;
2777 dir_vp = BHV_TO_VNODE(dir_bdp);
2778 dp = XFS_BHVTOI(dir_bdp);
2781 if (XFS_FORCED_SHUTDOWN(mp))
2782 return XFS_ERROR(EIO);
2784 dir_namelen = VNAMELEN(dentry);
2787 dp_joined_to_trans = B_FALSE;
2788 dm_di_mode = vap->va_mode|VTTOIF(vap->va_type);
2790 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_CREATE)) {
2791 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2792 dir_vp, DM_RIGHT_NULL, NULL,
2793 DM_RIGHT_NULL, dir_name, NULL,
2800 /* Return through std_return after this point. */
2802 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2806 if (vap->va_mask & XFS_AT_PROJID)
2807 prid = (xfs_prid_t)vap->va_projid;
2809 prid = (xfs_prid_t)dfltprid;
2812 * Make sure that we have allocated dquot(s) on disk.
2814 error = XFS_QM_DQVOPALLOC(mp, dp,
2815 current_fsuid(credp), current_fsgid(credp),
2816 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2820 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2821 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2822 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2823 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2824 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2825 if (error == ENOSPC) {
2827 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2828 XFS_TRANS_PERM_LOG_RES,
2829 XFS_MKDIR_LOG_COUNT);
2837 xfs_ilock(dp, XFS_ILOCK_EXCL);
2840 * Check for directory link count overflow.
2842 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2843 error = XFS_ERROR(EMLINK);
2848 * Reserve disk quota and the inode.
2850 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2855 (error = XFS_DIR_CANENTER(mp, tp, dp, dir_name, dir_namelen)))
2858 * create the directory inode.
2860 error = xfs_dir_ialloc(&tp, dp,
2861 MAKEIMODE(vap->va_type,vap->va_mode), 2,
2862 0, credp, prid, resblks > 0,
2865 if (error == ENOSPC)
2872 * Now we add the directory inode to the transaction.
2873 * We waited until now since xfs_dir_ialloc might start
2874 * a new transaction. Had we joined the transaction
2875 * earlier, the locks might have gotten released.
2878 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2879 dp_joined_to_trans = B_TRUE;
2881 XFS_BMAP_INIT(&free_list, &first_block);
2883 error = XFS_DIR_CREATENAME(mp, tp, dp, dir_name, dir_namelen,
2884 cdp->i_ino, &first_block, &free_list,
2885 resblks ? resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2887 ASSERT(error != ENOSPC);
2890 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2893 * Bump the in memory version number of the parent directory
2894 * so that other processes accessing it will recognize that
2895 * the directory has changed.
2899 error = XFS_DIR_INIT(mp, tp, cdp, dp);
2905 error = xfs_bumplink(tp, dp);
2910 cvp = XFS_ITOV(cdp);
2918 * Attach the dquots to the new inode and modify the icount incore.
2920 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2923 * If this is a synchronous mount, make sure that the
2924 * mkdir transaction goes to disk before returning to
2927 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2928 xfs_trans_set_sync(tp);
2931 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
2937 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
2938 XFS_QM_DQRELE(mp, udqp);
2939 XFS_QM_DQRELE(mp, gdqp);
2944 /* Fall through to std_return with error = 0 or errno from
2945 * xfs_trans_commit. */
2948 if ( (created || (error != 0 && dm_event_sent != 0)) &&
2949 DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
2950 DM_EVENT_POSTCREATE)) {
2951 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2952 dir_vp, DM_RIGHT_NULL,
2953 created ? XFS_ITOV(cdp):NULL,
2956 dm_di_mode, error, 0);
2962 xfs_bmap_cancel(&free_list);
2964 cancel_flags |= XFS_TRANS_ABORT;
2966 xfs_trans_cancel(tp, cancel_flags);
2967 XFS_QM_DQRELE(mp, udqp);
2968 XFS_QM_DQRELE(mp, gdqp);
2970 if (!dp_joined_to_trans && (dp != NULL)) {
2971 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2984 bhv_desc_t *dir_bdp,
2988 char *name = VNAME(dentry);
2990 xfs_inode_t *cdp; /* child directory */
2994 xfs_bmap_free_t free_list;
2995 xfs_fsblock_t first_block;
3004 dir_vp = BHV_TO_VNODE(dir_bdp);
3005 dp = XFS_BHVTOI(dir_bdp);
3008 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3010 if (XFS_FORCED_SHUTDOWN(XFS_BHVTOI(dir_bdp)->i_mount))
3011 return XFS_ERROR(EIO);
3012 namelen = VNAMELEN(dentry);
3014 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_REMOVE)) {
3015 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
3016 dir_vp, DM_RIGHT_NULL,
3017 NULL, DM_RIGHT_NULL,
3018 name, NULL, 0, 0, 0);
3020 return XFS_ERROR(error);
3023 /* Return through std_return after this point. */
3028 * We need to get a reference to cdp before we get our log
3029 * reservation. The reason for this is that we cannot call
3030 * xfs_iget for an inode for which we do not have a reference
3031 * once we've acquired a log reservation. This is because the
3032 * inode we are trying to get might be in xfs_inactive going
3033 * for a log reservation. Since we'll have to wait for the
3034 * inactive code to complete before returning from xfs_iget,
3035 * we need to make sure that we don't have log space reserved
3036 * when we call xfs_iget. Instead we get an unlocked referece
3037 * to the inode before getting our log reservation.
3039 error = xfs_get_dir_entry(dentry, &cdp);
3041 REMOVE_DEBUG_TRACE(__LINE__);
3045 dm_di_mode = cdp->i_d.di_mode;
3048 * Get the dquots for the inodes.
3050 error = XFS_QM_DQATTACH(mp, dp, 0);
3051 if (!error && dp != cdp)
3052 error = XFS_QM_DQATTACH(mp, cdp, 0);
3055 REMOVE_DEBUG_TRACE(__LINE__);
3059 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
3060 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3062 * We try to get the real space reservation first,
3063 * allowing for directory btree deletion(s) implying
3064 * possible bmap insert(s). If we can't get the space
3065 * reservation then we use 0 instead, and avoid the bmap
3066 * btree insert(s) in the directory code by, if the bmap
3067 * insert tries to happen, instead trimming the LAST
3068 * block from the directory.
3070 resblks = XFS_REMOVE_SPACE_RES(mp);
3071 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
3072 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3073 if (error == ENOSPC) {
3075 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
3076 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3079 ASSERT(error != ENOSPC);
3084 XFS_BMAP_INIT(&free_list, &first_block);
3087 * Now lock the child directory inode and the parent directory
3088 * inode in the proper order. This will take care of validating
3089 * that the directory entry for the child directory inode has
3090 * not changed while we were obtaining a log reservation.
3092 error = xfs_lock_dir_and_entry(dp, dentry, cdp);
3094 xfs_trans_cancel(tp, cancel_flags);
3099 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3102 * Only increment the parent directory vnode count if
3103 * we didn't bump it in looking up cdp. The only time
3104 * we don't bump it is when we're looking up ".".
3110 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3112 ASSERT(cdp->i_d.di_nlink >= 2);
3113 if (cdp->i_d.di_nlink != 2) {
3114 error = XFS_ERROR(ENOTEMPTY);
3117 if (!XFS_DIR_ISEMPTY(mp, cdp)) {
3118 error = XFS_ERROR(ENOTEMPTY);
3122 error = XFS_DIR_REMOVENAME(mp, tp, dp, name, namelen, cdp->i_ino,
3123 &first_block, &free_list, resblks);
3128 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3131 * Bump the in memory generation count on the parent
3132 * directory so that other can know that it has changed.
3137 * Drop the link from cdp's "..".
3139 error = xfs_droplink(tp, dp);
3145 * Drop the link from dp to cdp.
3147 error = xfs_droplink(tp, cdp);
3153 * Drop the "." link from cdp to self.
3155 error = xfs_droplink(tp, cdp);
3160 /* Determine these before committing transaction */
3161 last_cdp_link = (cdp)->i_d.di_nlink==0;
3164 * Take an extra ref on the child vnode so that it
3165 * does not go to xfs_inactive() from within the commit.
3170 * If this is a synchronous mount, make sure that the
3171 * rmdir transaction goes to disk before returning to
3174 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3175 xfs_trans_set_sync(tp);
3178 error = xfs_bmap_finish (&tp, &free_list, first_block, &committed);
3180 xfs_bmap_cancel(&free_list);
3181 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3187 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
3195 * Let interposed file systems know about removed links.
3197 VOP_LINK_REMOVED(XFS_ITOV(cdp), dir_vp, last_cdp_link);
3201 /* Fall through to std_return with error = 0 or the errno
3202 * from xfs_trans_commit. */
3204 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_POSTREMOVE)) {
3205 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3206 dir_vp, DM_RIGHT_NULL,
3207 NULL, DM_RIGHT_NULL,
3208 name, NULL, dm_di_mode,
3214 xfs_bmap_cancel(&free_list);
3215 cancel_flags |= XFS_TRANS_ABORT;
3217 xfs_trans_cancel(tp, cancel_flags);
3225 * Read dp's entries starting at uiop->uio_offset and translate them into
3226 * bufsize bytes worth of struct dirents starting at bufbase.
3230 bhv_desc_t *dir_bdp,
3236 xfs_trans_t *tp = NULL;
3239 xfs_off_t start_offset;
3241 vn_trace_entry(BHV_TO_VNODE(dir_bdp), __FUNCTION__,
3242 (inst_t *)__return_address);
3243 dp = XFS_BHVTOI(dir_bdp);
3245 if (XFS_FORCED_SHUTDOWN(dp->i_mount)) {
3246 return XFS_ERROR(EIO);
3249 lock_mode = xfs_ilock_map_shared(dp);
3250 start_offset = uiop->uio_offset;
3251 error = XFS_DIR_GETDENTS(dp->i_mount, tp, dp, uiop, eofp);
3252 if (start_offset != uiop->uio_offset) {
3253 xfs_ichgtime(dp, XFS_ICHGTIME_ACC);
3255 xfs_iunlock_map_shared(dp, lock_mode);
3266 bhv_desc_t *dir_bdp,
3279 xfs_bmap_free_t free_list;
3280 xfs_fsblock_t first_block;
3281 boolean_t dp_joined_to_trans;
3285 xfs_fileoff_t first_fsb;
3286 xfs_filblks_t fs_blocks;
3288 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3295 struct xfs_dquot *udqp, *gdqp;
3297 char *link_name = VNAME(dentry);
3301 dir_vp = BHV_TO_VNODE(dir_bdp);
3302 dp = XFS_BHVTOI(dir_bdp);
3303 dp_joined_to_trans = B_FALSE;
3308 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3312 if (XFS_FORCED_SHUTDOWN(mp))
3313 return XFS_ERROR(EIO);
3315 link_namelen = VNAMELEN(dentry);
3318 * Check component lengths of the target path name.
3320 pathlen = strlen(target_path);
3321 if (pathlen >= MAXPATHLEN) /* total string too long */
3322 return XFS_ERROR(ENAMETOOLONG);
3323 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3327 for(total = 0, path = target_path; total < pathlen;) {
3331 while(*path == '/') {
3337 * Count up to the next slash or end of path.
3338 * Error out if the component is bigger than MAXNAMELEN.
3340 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3341 if (++len >= MAXNAMELEN) {
3342 error = ENAMETOOLONG;
3349 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, dp, DM_EVENT_SYMLINK)) {
3350 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3351 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3352 link_name, target_path, 0, 0, 0);
3357 /* Return through std_return after this point. */
3360 if (vap->va_mask & XFS_AT_PROJID)
3361 prid = (xfs_prid_t)vap->va_projid;
3363 prid = (xfs_prid_t)dfltprid;
3366 * Make sure that we have allocated dquot(s) on disk.
3368 error = XFS_QM_DQVOPALLOC(mp, dp,
3369 current_fsuid(credp), current_fsgid(credp),
3370 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3374 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3375 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3377 * The symlink will fit into the inode data fork?
3378 * There can't be any attributes so we get the whole variable part.
3380 if (pathlen <= XFS_LITINO(mp))
3383 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3384 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3385 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3386 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3387 if (error == ENOSPC && fs_blocks == 0) {
3389 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3390 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3398 xfs_ilock(dp, XFS_ILOCK_EXCL);
3401 * Check whether the directory allows new symlinks or not.
3403 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3404 error = XFS_ERROR(EPERM);
3409 * Reserve disk quota : blocks and inode.
3411 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3416 * Check for ability to enter directory entry, if no space reserved.
3419 (error = XFS_DIR_CANENTER(mp, tp, dp, link_name, link_namelen)))
3422 * Initialize the bmap freelist prior to calling either
3423 * bmapi or the directory create code.
3425 XFS_BMAP_INIT(&free_list, &first_block);
3428 * Allocate an inode for the symlink.
3430 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (vap->va_mode&~S_IFMT),
3431 1, 0, credp, prid, resblks > 0, &ip, NULL);
3433 if (error == ENOSPC)
3440 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3441 dp_joined_to_trans = B_TRUE;
3444 * Also attach the dquot(s) to it, if applicable.
3446 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3449 resblks -= XFS_IALLOC_SPACE_RES(mp);
3451 * If the symlink will fit into the inode, write it inline.
3453 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3454 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3455 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3456 ip->i_d.di_size = pathlen;
3459 * The inode was initially created in extent format.
3461 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3462 ip->i_df.if_flags |= XFS_IFINLINE;
3464 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3465 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3469 nmaps = SYMLINK_MAPS;
3471 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3472 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3473 &first_block, resblks, mval, &nmaps,
3480 resblks -= fs_blocks;
3481 ip->i_d.di_size = pathlen;
3482 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3484 cur_chunk = target_path;
3485 for (n = 0; n < nmaps; n++) {
3486 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3487 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3488 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3489 BTOBB(byte_cnt), 0);
3490 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3491 if (pathlen < byte_cnt) {
3494 pathlen -= byte_cnt;
3496 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3497 cur_chunk += byte_cnt;
3499 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3504 * Create the directory entry for the symlink.
3506 error = XFS_DIR_CREATENAME(mp, tp, dp, link_name, link_namelen,
3507 ip->i_ino, &first_block, &free_list, resblks);
3511 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3512 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3515 * Bump the in memory version number of the parent directory
3516 * so that other processes accessing it will recognize that
3517 * the directory has changed.
3522 * If this is a synchronous mount, make sure that the
3523 * symlink transaction goes to disk before returning to
3526 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3527 xfs_trans_set_sync(tp);
3531 * xfs_trans_commit normally decrements the vnode ref count
3532 * when it unlocks the inode. Since we want to return the
3533 * vnode to the caller, we bump the vnode ref count now.
3537 error = xfs_bmap_finish(&tp, &free_list, first_block, &committed);
3541 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
3542 XFS_QM_DQRELE(mp, udqp);
3543 XFS_QM_DQRELE(mp, gdqp);
3545 /* Fall through to std_return with error = 0 or errno from
3546 * xfs_trans_commit */
3548 if (DM_EVENT_ENABLED(dir_vp->v_vfsp, XFS_BHVTOI(dir_bdp),
3549 DM_EVENT_POSTSYMLINK)) {
3550 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3551 dir_vp, DM_RIGHT_NULL,
3552 error ? NULL : XFS_ITOV(ip),
3553 DM_RIGHT_NULL, link_name, target_path,
3569 xfs_bmap_cancel(&free_list);
3570 cancel_flags |= XFS_TRANS_ABORT;
3572 xfs_trans_cancel(tp, cancel_flags);
3573 XFS_QM_DQRELE(mp, udqp);
3574 XFS_QM_DQRELE(mp, gdqp);
3576 if (!dp_joined_to_trans && (dp != NULL)) {
3577 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3587 * A fid routine that takes a pointer to a previously allocated
3588 * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
3598 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
3599 (inst_t *)__return_address);
3600 ASSERT(sizeof(fid_t) >= sizeof(xfs_fid2_t));
3602 xfid = (xfs_fid2_t *)fidp;
3603 ip = XFS_BHVTOI(bdp);
3604 xfid->fid_len = sizeof(xfs_fid2_t) - sizeof(xfid->fid_len);
3607 * use memcpy because the inode is a long long and there's no
3608 * assurance that xfid->fid_ino is properly aligned.
3610 memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
3611 xfid->fid_gen = ip->i_d.di_gen;
3628 vp = BHV_TO_VNODE(bdp);
3629 if (vp->v_type == VDIR)
3631 ip = XFS_BHVTOI(bdp);
3632 if (locktype == VRWLOCK_WRITE) {
3633 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3634 } else if (locktype == VRWLOCK_TRY_READ) {
3635 return (xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED));
3636 } else if (locktype == VRWLOCK_TRY_WRITE) {
3637 return (xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL));
3639 ASSERT((locktype == VRWLOCK_READ) ||
3640 (locktype == VRWLOCK_WRITE_DIRECT));
3641 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3659 vp = BHV_TO_VNODE(bdp);
3660 if (vp->v_type == VDIR)
3662 ip = XFS_BHVTOI(bdp);
3663 if (locktype == VRWLOCK_WRITE) {
3665 * In the write case, we may have added a new entry to
3666 * the reference cache. This might store a pointer to
3667 * an inode to be released in this inode. If it is there,
3668 * clear the pointer and release the inode after unlocking
3671 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3673 ASSERT((locktype == VRWLOCK_READ) ||
3674 (locktype == VRWLOCK_WRITE_DIRECT));
3675 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3687 xfs_inode_log_item_t *iip;
3690 ip = XFS_BHVTOI(bdp);
3694 if (XFS_FORCED_SHUTDOWN(mp))
3695 return XFS_ERROR(EIO);
3698 * Bypass inodes which have already been cleaned by
3699 * the inode flush clustering code inside xfs_iflush
3701 if ((ip->i_update_core == 0) &&
3702 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3705 if (flags & FLUSH_LOG) {
3706 if (iip && iip->ili_last_lsn) {
3707 xlog_t *log = mp->m_log;
3709 int s, log_flags = XFS_LOG_FORCE;
3711 s = GRANT_LOCK(log);
3712 sync_lsn = log->l_last_sync_lsn;
3713 GRANT_UNLOCK(log, s);
3715 if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) <= 0))
3718 if (flags & FLUSH_SYNC)
3719 log_flags |= XFS_LOG_SYNC;
3720 return xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3725 * We make this non-blocking if the inode is contended,
3726 * return EAGAIN to indicate to the caller that they
3727 * did not succeed. This prevents the flush path from
3728 * blocking on inodes inside another operation right
3729 * now, they get caught later by xfs_sync.
3731 if (flags & FLUSH_INODE) {
3734 if (xfs_ipincount(ip))
3737 if (flags & FLUSH_SYNC) {
3738 xfs_ilock(ip, XFS_ILOCK_SHARED);
3740 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3741 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3742 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3749 if (flags & FLUSH_SYNC)
3750 flush_flags = XFS_IFLUSH_SYNC;
3752 flush_flags = XFS_IFLUSH_ASYNC;
3754 error = xfs_iflush(ip, flush_flags);
3755 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3774 if (!capable(CAP_SYS_ADMIN))
3775 return XFS_ERROR(EPERM);
3777 ip = XFS_BHVTOI(bdp);
3780 if (XFS_FORCED_SHUTDOWN(mp))
3781 return XFS_ERROR(EIO);
3783 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3784 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3786 xfs_trans_cancel(tp, 0);
3789 xfs_ilock(ip, XFS_ILOCK_EXCL);
3790 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3792 ip->i_iocore.io_dmevmask = ip->i_d.di_dmevmask = evmask;
3793 ip->i_iocore.io_dmstate = ip->i_d.di_dmstate = state;
3795 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3797 error = xfs_trans_commit(tp, 0, NULL);
3813 vp = BHV_TO_VNODE(bdp);
3814 ip = XFS_BHVTOI(bdp);
3816 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
3818 ASSERT(!VN_MAPPED(vp));
3820 /* bad inode, get out here ASAP */
3826 if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
3827 if (ip->i_d.di_size > 0) {
3829 * Flush and invalidate any data left around that is
3830 * a part of this file.
3832 * Get the inode's i/o lock so that buffers are pushed
3833 * out while holding the proper lock. We can't hold
3834 * the inode lock here since flushing out buffers may
3835 * cause us to try to get the lock in xfs_strategy().
3837 * We don't have to call remapf() here, because there
3838 * cannot be any mapped file references to this vnode
3839 * since it is being reclaimed.
3841 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3844 * If we hit an IO error, we need to make sure that the
3845 * buffer and page caches of file data for
3846 * the file are tossed away. We don't want to use
3847 * VOP_FLUSHINVAL_PAGES here because we don't want dirty
3848 * pages to stay attached to the vnode, but be
3849 * marked P_BAD. pdflush/vnode_pagebad
3852 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3853 VOP_FLUSHINVAL_PAGES(vp, 0, -1, FI_NONE);
3855 VOP_TOSS_PAGES(vp, 0, -1, FI_NONE);
3858 ASSERT(VN_CACHED(vp) == 0);
3859 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) ||
3860 ip->i_delayed_blks == 0);
3861 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
3862 } else if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3864 * di_size field may not be quite accurate if we're
3867 VOP_TOSS_PAGES(vp, 0, -1, FI_NONE);
3868 ASSERT(VN_CACHED(vp) == 0);
3872 /* If we have nothing to flush with this inode then complete the
3873 * teardown now, otherwise break the link between the xfs inode
3874 * and the linux inode and clean up the xfs inode later. This
3875 * avoids flushing the inode to disk during the delete operation
3878 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3879 xfs_ilock(ip, XFS_ILOCK_EXCL);
3881 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3883 xfs_mount_t *mp = ip->i_mount;
3885 /* Protect sync from us */
3886 XFS_MOUNT_ILOCK(mp);
3887 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
3888 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
3889 ip->i_flags |= XFS_IRECLAIMABLE;
3890 XFS_MOUNT_IUNLOCK(mp);
3901 xfs_ihash_t *ih = ip->i_hash;
3902 vnode_t *vp = XFS_ITOV_NULL(ip);
3905 if (vp && VN_BAD(vp))
3908 /* The hash lock here protects a thread in xfs_iget_core from
3909 * racing with us on linking the inode back with a vnode.
3910 * Once we have the XFS_IRECLAIM flag set it will not touch
3913 write_lock(&ih->ih_lock);
3914 if ((ip->i_flags & XFS_IRECLAIM) ||
3915 (!(ip->i_flags & XFS_IRECLAIMABLE) && vp == NULL)) {
3916 write_unlock(&ih->ih_lock);
3919 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3923 ip->i_flags |= XFS_IRECLAIM;
3924 write_unlock(&ih->ih_lock);
3927 * If the inode is still dirty, then flush it out. If the inode
3928 * is not in the AIL, then it will be OK to flush it delwri as
3929 * long as xfs_iflush() does not keep any references to the inode.
3930 * We leave that decision up to xfs_iflush() since it has the
3931 * knowledge of whether it's OK to simply do a delwri flush of
3932 * the inode or whether we need to wait until the inode is
3933 * pulled from the AIL.
3934 * We get the flush lock regardless, though, just to make sure
3935 * we don't free it while it is being flushed.
3937 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3939 xfs_ilock(ip, XFS_ILOCK_EXCL);
3943 if (ip->i_update_core ||
3944 ((ip->i_itemp != NULL) &&
3945 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3946 error = xfs_iflush(ip, sync_mode);
3948 * If we hit an error, typically because of filesystem
3949 * shutdown, we don't need to let vn_reclaim to know
3950 * because we're gonna reclaim the inode anyway.
3953 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3956 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3959 ASSERT(ip->i_update_core == 0);
3960 ASSERT(ip->i_itemp == NULL ||
3961 ip->i_itemp->ili_format.ilf_fields == 0);
3962 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3963 } else if (locked) {
3965 * We are not interested in doing an iflush if we're
3966 * in the process of shutting down the filesystem forcibly.
3967 * So, just reclaim the inode.
3970 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3979 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3982 xfs_inode_t *ip, *n;
3987 XFS_MOUNT_ILOCK(mp);
3988 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3990 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3992 if (xfs_ipincount(ip) ||
3993 !xfs_iflock_nowait(ip)) {
3994 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3998 XFS_MOUNT_IUNLOCK(mp);
3999 xfs_finish_reclaim(ip, noblock,
4000 XFS_IFLUSH_DELWRI_ELSE_ASYNC);
4008 XFS_MOUNT_IUNLOCK(mp);
4013 * xfs_alloc_file_space()
4014 * This routine allocates disk space for the given file.
4016 * If alloc_type == 0, this request is for an ALLOCSP type
4017 * request which will change the file size. In this case, no
4018 * DMAPI event will be generated by the call. A TRUNCATE event
4019 * will be generated later by xfs_setattr.
4021 * If alloc_type != 0, this request is for a RESVSP type
4022 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
4023 * lower block boundary byte address is less than the file's
4032 xfs_alloc_file_space(
4039 xfs_filblks_t allocated_fsb;
4040 xfs_filblks_t allocatesize_fsb;
4043 xfs_filblks_t datablocks;
4045 xfs_fsblock_t firstfsb;
4046 xfs_bmap_free_t free_list;
4047 xfs_bmbt_irec_t *imapp;
4048 xfs_bmbt_irec_t imaps[1];
4055 xfs_fileoff_t startoffset_fsb;
4057 int xfs_bmapi_flags;
4059 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
4062 if (XFS_FORCED_SHUTDOWN(mp))
4063 return XFS_ERROR(EIO);
4066 * determine if this is a realtime file
4068 if ((rt = XFS_IS_REALTIME_INODE(ip)) != 0) {
4069 if (ip->i_d.di_extsize)
4070 rtextsize = ip->i_d.di_extsize;
4072 rtextsize = mp->m_sb.sb_rextsize;
4076 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4080 return XFS_ERROR(EINVAL);
4086 xfs_bmapi_flags = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
4087 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
4088 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
4090 /* Generate a DMAPI event if needed. */
4091 if (alloc_type != 0 && offset < ip->i_d.di_size &&
4092 (attr_flags&ATTR_DMI) == 0 &&
4093 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4094 xfs_off_t end_dmi_offset;
4096 end_dmi_offset = offset+len;
4097 if (end_dmi_offset > ip->i_d.di_size)
4098 end_dmi_offset = ip->i_d.di_size;
4099 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
4100 offset, end_dmi_offset - offset,
4107 * allocate file space until done or until there is an error
4110 while (allocatesize_fsb && !error) {
4112 * determine if reserving space on
4113 * the data or realtime partition.
4118 s = startoffset_fsb;
4119 do_div(s, rtextsize);
4121 e = roundup_64(startoffset_fsb + allocatesize_fsb,
4123 numrtextents = (int)(e - s) / mp->m_sb.sb_rextsize;
4126 datablocks = allocatesize_fsb;
4131 * allocate and setup the transaction
4133 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4134 resblks = XFS_DIOSTRAT_SPACE_RES(mp, datablocks);
4135 error = xfs_trans_reserve(tp,
4137 XFS_WRITE_LOG_RES(mp),
4139 XFS_TRANS_PERM_LOG_RES,
4140 XFS_WRITE_LOG_COUNT);
4143 * check for running out of space
4147 * Free the transaction structure.
4149 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4150 xfs_trans_cancel(tp, 0);
4153 xfs_ilock(ip, XFS_ILOCK_EXCL);
4154 error = XFS_TRANS_RESERVE_QUOTA_BYDQUOTS(mp, tp,
4155 ip->i_udquot, ip->i_gdquot, resblks, 0, rt ?
4156 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4160 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4161 xfs_trans_ihold(tp, ip);
4164 * issue the bmapi() call to allocate the blocks
4166 XFS_BMAP_INIT(&free_list, &firstfsb);
4167 error = xfs_bmapi(tp, ip, startoffset_fsb,
4168 allocatesize_fsb, xfs_bmapi_flags,
4169 &firstfsb, 0, imapp, &reccount,
4176 * complete the transaction
4178 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
4183 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
4184 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4189 allocated_fsb = imapp->br_blockcount;
4191 if (reccount == 0) {
4192 error = XFS_ERROR(ENOSPC);
4196 startoffset_fsb += allocated_fsb;
4197 allocatesize_fsb -= allocated_fsb;
4200 if (error == ENOSPC && (attr_flags&ATTR_DMI) == 0 &&
4201 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_NOSPACE)) {
4203 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
4204 XFS_ITOV(ip), DM_RIGHT_NULL,
4205 XFS_ITOV(ip), DM_RIGHT_NULL,
4206 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
4208 goto retry; /* Maybe DMAPI app. has made space */
4209 /* else fall through with error from XFS_SEND_DATA */
4215 xfs_bmap_cancel(&free_list);
4217 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4218 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4219 goto dmapi_enospc_check;
4223 * Zero file bytes between startoff and endoff inclusive.
4224 * The iolock is held exclusive and no blocks are buffered.
4227 xfs_zero_remaining_bytes(
4232 xfs_bmbt_irec_t imap;
4233 xfs_fileoff_t offset_fsb;
4234 xfs_off_t lastoffset;
4237 xfs_mount_t *mp = ip->i_mount;
4241 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
4242 ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
4243 mp->m_rtdev_targp : mp->m_ddev_targp);
4245 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4246 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4248 error = xfs_bmapi(NULL, ip, offset_fsb, 1, 0, NULL, 0, &imap,
4250 if (error || nimap < 1)
4252 ASSERT(imap.br_blockcount >= 1);
4253 ASSERT(imap.br_startoff == offset_fsb);
4254 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4255 if (lastoffset > endoff)
4256 lastoffset = endoff;
4257 if (imap.br_startblock == HOLESTARTBLOCK)
4259 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4260 if (imap.br_state == XFS_EXT_UNWRITTEN)
4263 XFS_BUF_UNWRITE(bp);
4265 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4267 if ((error = xfs_iowait(bp))) {
4268 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4269 mp, bp, XFS_BUF_ADDR(bp));
4272 memset(XFS_BUF_PTR(bp) +
4273 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4274 0, lastoffset - offset + 1);
4279 if ((error = xfs_iowait(bp))) {
4280 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4281 mp, bp, XFS_BUF_ADDR(bp));
4290 * xfs_free_file_space()
4291 * This routine frees disk space for the given file.
4293 * This routine is only called by xfs_change_file_space
4294 * for an UNRESVSP type call.
4302 xfs_free_file_space(
4310 xfs_off_t end_dmi_offset;
4311 xfs_fileoff_t endoffset_fsb;
4313 xfs_fsblock_t firstfsb;
4314 xfs_bmap_free_t free_list;
4316 xfs_bmbt_irec_t imap;
4324 xfs_fileoff_t startoffset_fsb;
4326 int need_iolock = 1;
4328 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
4331 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4335 if (len <= 0) /* if nothing being freed */
4337 rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4338 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4339 end_dmi_offset = offset + len;
4340 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4342 if (offset < ip->i_d.di_size &&
4343 (attr_flags & ATTR_DMI) == 0 &&
4344 DM_EVENT_ENABLED(XFS_MTOVFS(mp), ip, DM_EVENT_WRITE)) {
4345 if (end_dmi_offset > ip->i_d.di_size)
4346 end_dmi_offset = ip->i_d.di_size;
4347 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
4348 offset, end_dmi_offset - offset,
4349 AT_DELAY_FLAG(attr_flags), NULL);
4354 ASSERT(attr_flags & ATTR_NOLOCK ? attr_flags & ATTR_DMI : 1);
4355 if (attr_flags & ATTR_NOLOCK)
4358 xfs_ilock(ip, XFS_IOLOCK_EXCL);
4360 rounding = MAX((__uint8_t)(1 << mp->m_sb.sb_blocklog),
4362 ilen = len + (offset & (rounding - 1));
4363 ioffset = offset & ~(rounding - 1);
4364 if (ilen & (rounding - 1))
4365 ilen = (ilen + rounding) & ~(rounding - 1);
4366 xfs_inval_cached_pages(XFS_ITOV(ip), &(ip->i_iocore), ioffset, 0, 0);
4368 * Need to zero the stuff we're not freeing, on disk.
4369 * If its a realtime file & can't use unwritten extents then we
4370 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4371 * will take care of it for us.
4373 if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4375 error = xfs_bmapi(NULL, ip, startoffset_fsb, 1, 0, NULL, 0,
4376 &imap, &nimap, NULL);
4378 goto out_unlock_iolock;
4379 ASSERT(nimap == 0 || nimap == 1);
4380 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4383 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4384 block = imap.br_startblock;
4385 mod = do_div(block, mp->m_sb.sb_rextsize);
4387 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4390 error = xfs_bmapi(NULL, ip, endoffset_fsb - 1, 1, 0, NULL, 0,
4391 &imap, &nimap, NULL);
4393 goto out_unlock_iolock;
4394 ASSERT(nimap == 0 || nimap == 1);
4395 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4396 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4398 if (mod && (mod != mp->m_sb.sb_rextsize))
4399 endoffset_fsb -= mod;
4402 if ((done = (endoffset_fsb <= startoffset_fsb)))
4404 * One contiguous piece to clear
4406 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4409 * Some full blocks, possibly two pieces to clear
4411 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4412 error = xfs_zero_remaining_bytes(ip, offset,
4413 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4415 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4416 error = xfs_zero_remaining_bytes(ip,
4417 XFS_FSB_TO_B(mp, endoffset_fsb),
4422 * free file space until done or until there is an error
4424 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4425 while (!error && !done) {
4428 * allocate and setup the transaction
4430 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4431 error = xfs_trans_reserve(tp,
4433 XFS_WRITE_LOG_RES(mp),
4435 XFS_TRANS_PERM_LOG_RES,
4436 XFS_WRITE_LOG_COUNT);
4439 * check for running out of space
4443 * Free the transaction structure.
4445 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4446 xfs_trans_cancel(tp, 0);
4449 xfs_ilock(ip, XFS_ILOCK_EXCL);
4450 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4451 ip->i_udquot, ip->i_gdquot, resblks, 0, rt ?
4452 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4456 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4457 xfs_trans_ihold(tp, ip);
4460 * issue the bunmapi() call to free the blocks
4462 XFS_BMAP_INIT(&free_list, &firstfsb);
4463 error = xfs_bunmapi(tp, ip, startoffset_fsb,
4464 endoffset_fsb - startoffset_fsb,
4465 0, 2, &firstfsb, &free_list, &done);
4471 * complete the transaction
4473 error = xfs_bmap_finish(&tp, &free_list, firstfsb, &committed);
4478 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES, NULL);
4479 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4484 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4488 xfs_bmap_cancel(&free_list);
4490 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4491 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4497 * xfs_change_file_space()
4498 * This routine allocates or frees disk space for the given file.
4499 * The user specified parameters are checked for alignment and size
4508 xfs_change_file_space(
4522 xfs_off_t startoffset;
4528 vp = BHV_TO_VNODE(bdp);
4529 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4531 ip = XFS_BHVTOI(bdp);
4535 * must be a regular file and have write permission
4537 if (vp->v_type != VREG)
4538 return XFS_ERROR(EINVAL);
4540 xfs_ilock(ip, XFS_ILOCK_SHARED);
4542 if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
4543 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4547 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4549 switch (bf->l_whence) {
4550 case 0: /*SEEK_SET*/
4552 case 1: /*SEEK_CUR*/
4553 bf->l_start += offset;
4555 case 2: /*SEEK_END*/
4556 bf->l_start += ip->i_d.di_size;
4559 return XFS_ERROR(EINVAL);
4562 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4564 if ( (bf->l_start < 0)
4565 || (bf->l_start > XFS_MAXIOFFSET(mp))
4566 || (bf->l_start + llen < 0)
4567 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4568 return XFS_ERROR(EINVAL);
4572 startoffset = bf->l_start;
4573 fsize = ip->i_d.di_size;
4576 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4578 * These calls do NOT zero the data space allocated to the file,
4579 * nor do they change the file size.
4581 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4583 * These calls cause the new file data to be zeroed and the file
4584 * size to be changed.
4586 setprealloc = clrprealloc = 0;
4589 case XFS_IOC_RESVSP:
4590 case XFS_IOC_RESVSP64:
4591 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4598 case XFS_IOC_UNRESVSP:
4599 case XFS_IOC_UNRESVSP64:
4600 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4605 case XFS_IOC_ALLOCSP:
4606 case XFS_IOC_ALLOCSP64:
4607 case XFS_IOC_FREESP:
4608 case XFS_IOC_FREESP64:
4609 if (startoffset > fsize) {
4610 error = xfs_alloc_file_space(ip, fsize,
4611 startoffset - fsize, 0, attr_flags);
4616 va.va_mask = XFS_AT_SIZE;
4617 va.va_size = startoffset;
4619 error = xfs_setattr(bdp, &va, attr_flags, credp);
4629 return XFS_ERROR(EINVAL);
4633 * update the inode timestamp, mode, and prealloc flag bits
4635 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4637 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4640 xfs_trans_cancel(tp, 0);
4644 xfs_ilock(ip, XFS_ILOCK_EXCL);
4646 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4647 xfs_trans_ihold(tp, ip);
4649 if ((attr_flags & ATTR_DMI) == 0) {
4650 ip->i_d.di_mode &= ~S_ISUID;
4653 * Note that we don't have to worry about mandatory
4654 * file locking being disabled here because we only
4655 * clear the S_ISGID bit if the Group execute bit is
4656 * on, but if it was on then mandatory locking wouldn't
4657 * have been enabled.
4659 if (ip->i_d.di_mode & S_IXGRP)
4660 ip->i_d.di_mode &= ~S_ISGID;
4662 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4665 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4666 else if (clrprealloc)
4667 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4669 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4670 xfs_trans_set_sync(tp);
4672 error = xfs_trans_commit(tp, 0, NULL);
4674 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4679 vnodeops_t xfs_vnodeops = {
4680 BHV_IDENTITY_INIT(VN_BHV_XFS,VNODE_POSITION_XFS),
4681 .vop_open = xfs_open,
4682 .vop_read = xfs_read,
4683 #ifdef HAVE_SENDFILE
4684 .vop_sendfile = xfs_sendfile,
4686 .vop_write = xfs_write,
4687 .vop_ioctl = xfs_ioctl,
4688 .vop_getattr = xfs_getattr,
4689 .vop_setattr = xfs_setattr,
4690 .vop_access = xfs_access,
4691 .vop_lookup = xfs_lookup,
4692 .vop_create = xfs_create,
4693 .vop_remove = xfs_remove,
4694 .vop_link = xfs_link,
4695 .vop_rename = xfs_rename,
4696 .vop_mkdir = xfs_mkdir,
4697 .vop_rmdir = xfs_rmdir,
4698 .vop_readdir = xfs_readdir,
4699 .vop_symlink = xfs_symlink,
4700 .vop_readlink = xfs_readlink,
4701 .vop_fsync = xfs_fsync,
4702 .vop_inactive = xfs_inactive,
4703 .vop_fid2 = xfs_fid2,
4704 .vop_rwlock = xfs_rwlock,
4705 .vop_rwunlock = xfs_rwunlock,
4706 .vop_bmap = xfs_bmap,
4707 .vop_reclaim = xfs_reclaim,
4708 .vop_attr_get = xfs_attr_get,
4709 .vop_attr_set = xfs_attr_set,
4710 .vop_attr_remove = xfs_attr_remove,
4711 .vop_attr_list = xfs_attr_list,
4712 .vop_link_removed = (vop_link_removed_t)fs_noval,
4713 .vop_vnode_change = (vop_vnode_change_t)fs_noval,
4714 .vop_tosspages = fs_tosspages,
4715 .vop_flushinval_pages = fs_flushinval_pages,
4716 .vop_flush_pages = fs_flush_pages,
4717 .vop_release = xfs_release,
4718 .vop_iflush = xfs_inode_flush,