2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_types.h"
25 #include "xfs_trans.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_da_btree.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_inode_item.h"
40 #include "xfs_itable.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_alloc.h"
47 #include "xfs_error.h"
48 #include "xfs_quota.h"
49 #include "xfs_utils.h"
50 #include "xfs_rtalloc.h"
51 #include "xfs_refcache.h"
52 #include "xfs_trans_space.h"
53 #include "xfs_log_priv.h"
54 #include "xfs_filestream.h"
62 bhv_vnode_t *vp = BHV_TO_VNODE(bdp);
63 xfs_inode_t *ip = XFS_BHVTOI(bdp);
65 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
66 return XFS_ERROR(EIO);
69 * If it's a directory with any blocks, read-ahead block 0
70 * as we're almost certain to have the next operation be a read there.
72 if (VN_ISDIR(vp) && ip->i_d.di_nextents > 0) {
73 mode = xfs_ilock_map_shared(ip);
74 if (ip->i_d.di_nextents > 0)
75 (void)xfs_da_reada_buf(NULL, ip, 0, XFS_DATA_FORK);
76 xfs_iunlock(ip, mode);
95 vp = BHV_TO_VNODE(bdp);
96 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
101 if (XFS_FORCED_SHUTDOWN(mp))
102 return XFS_ERROR(EIO);
104 if (!(flags & ATTR_LAZY))
105 xfs_ilock(ip, XFS_ILOCK_SHARED);
107 vap->va_size = XFS_ISIZE(ip);
108 if (vap->va_mask == XFS_AT_SIZE)
112 XFS_FSB_TO_BB(mp, ip->i_d.di_nblocks + ip->i_delayed_blks);
113 vap->va_nodeid = ip->i_ino;
115 vap->va_nodeid += mp->m_inoadd;
117 vap->va_nlink = ip->i_d.di_nlink;
120 * Quick exit for non-stat callers
123 ~(XFS_AT_SIZE|XFS_AT_FSID|XFS_AT_NODEID|
124 XFS_AT_NLINK|XFS_AT_BLKSIZE)) == 0)
128 * Copy from in-core inode.
130 vap->va_mode = ip->i_d.di_mode;
131 vap->va_uid = ip->i_d.di_uid;
132 vap->va_gid = ip->i_d.di_gid;
133 vap->va_projid = ip->i_d.di_projid;
136 * Check vnode type block/char vs. everything else.
138 switch (ip->i_d.di_mode & S_IFMT) {
141 vap->va_rdev = ip->i_df.if_u2.if_rdev;
142 vap->va_blocksize = BLKDEV_IOSIZE;
147 if (!(ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
148 vap->va_blocksize = xfs_preferred_iosize(mp);
152 * If the file blocks are being allocated from a
153 * realtime partition, then return the inode's
154 * realtime extent size or the realtime volume's
158 xfs_get_extsz_hint(ip) << mp->m_sb.sb_blocklog;
163 vn_atime_to_timespec(vp, &vap->va_atime);
164 vap->va_mtime.tv_sec = ip->i_d.di_mtime.t_sec;
165 vap->va_mtime.tv_nsec = ip->i_d.di_mtime.t_nsec;
166 vap->va_ctime.tv_sec = ip->i_d.di_ctime.t_sec;
167 vap->va_ctime.tv_nsec = ip->i_d.di_ctime.t_nsec;
170 * Exit for stat callers. See if any of the rest of the fields
171 * to be filled in are needed.
174 (XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
175 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
179 * Convert di_flags to xflags.
181 vap->va_xflags = xfs_ip2xflags(ip);
184 * Exit for inode revalidate. See if any of the rest of
185 * the fields to be filled in are needed.
188 (XFS_AT_EXTSIZE|XFS_AT_NEXTENTS|XFS_AT_ANEXTENTS|
189 XFS_AT_GENCOUNT|XFS_AT_VCODE)) == 0)
192 vap->va_extsize = ip->i_d.di_extsize << mp->m_sb.sb_blocklog;
194 (ip->i_df.if_flags & XFS_IFEXTENTS) ?
195 ip->i_df.if_bytes / sizeof(xfs_bmbt_rec_t) :
199 (ip->i_afp->if_flags & XFS_IFEXTENTS) ?
200 ip->i_afp->if_bytes / sizeof(xfs_bmbt_rec_t) :
201 ip->i_d.di_anextents;
203 vap->va_anextents = 0;
204 vap->va_gen = ip->i_d.di_gen;
207 if (!(flags & ATTR_LAZY))
208 xfs_iunlock(ip, XFS_ILOCK_SHARED);
234 xfs_prid_t projid=0, iprojid=0;
235 int mandlock_before, mandlock_after;
236 struct xfs_dquot *udqp, *gdqp, *olddquot1, *olddquot2;
240 vp = BHV_TO_VNODE(bdp);
241 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
243 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
244 return XFS_ERROR(EROFS);
247 * Cannot set certain attributes.
250 if (mask & XFS_AT_NOSET) {
251 return XFS_ERROR(EINVAL);
254 ip = XFS_BHVTOI(bdp);
257 if (XFS_FORCED_SHUTDOWN(mp))
258 return XFS_ERROR(EIO);
261 * Timestamps do not need to be logged and hence do not
262 * need to be done within a transaction.
264 if (mask & XFS_AT_UPDTIMES) {
265 ASSERT((mask & ~XFS_AT_UPDTIMES) == 0);
266 timeflags = ((mask & XFS_AT_UPDATIME) ? XFS_ICHGTIME_ACC : 0) |
267 ((mask & XFS_AT_UPDCTIME) ? XFS_ICHGTIME_CHG : 0) |
268 ((mask & XFS_AT_UPDMTIME) ? XFS_ICHGTIME_MOD : 0);
269 xfs_ichgtime(ip, timeflags);
273 olddquot1 = olddquot2 = NULL;
277 * If disk quotas is on, we make sure that the dquots do exist on disk,
278 * before we start any other transactions. Trying to do this later
279 * is messy. We don't care to take a readlock to look at the ids
280 * in inode here, because we can't hold it across the trans_reserve.
281 * If the IDs do change before we take the ilock, we're covered
282 * because the i_*dquot fields will get updated anyway.
284 if (XFS_IS_QUOTA_ON(mp) &&
285 (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID))) {
288 if ((mask & XFS_AT_UID) && XFS_IS_UQUOTA_ON(mp)) {
290 qflags |= XFS_QMOPT_UQUOTA;
292 uid = ip->i_d.di_uid;
294 if ((mask & XFS_AT_GID) && XFS_IS_GQUOTA_ON(mp)) {
296 qflags |= XFS_QMOPT_GQUOTA;
298 gid = ip->i_d.di_gid;
300 if ((mask & XFS_AT_PROJID) && XFS_IS_PQUOTA_ON(mp)) {
301 projid = vap->va_projid;
302 qflags |= XFS_QMOPT_PQUOTA;
304 projid = ip->i_d.di_projid;
307 * We take a reference when we initialize udqp and gdqp,
308 * so it is important that we never blindly double trip on
309 * the same variable. See xfs_create() for an example.
311 ASSERT(udqp == NULL);
312 ASSERT(gdqp == NULL);
313 code = XFS_QM_DQVOPALLOC(mp, ip, uid, gid, projid, qflags,
320 * For the other attributes, we acquire the inode lock and
321 * first do an error checking pass.
324 lock_flags = XFS_ILOCK_EXCL;
325 if (flags & ATTR_NOLOCK)
327 if (!(mask & XFS_AT_SIZE)) {
328 if ((mask != (XFS_AT_CTIME|XFS_AT_ATIME|XFS_AT_MTIME)) ||
329 (mp->m_flags & XFS_MOUNT_WSYNC)) {
330 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
332 if ((code = xfs_trans_reserve(tp, 0,
333 XFS_ICHANGE_LOG_RES(mp), 0,
340 if (DM_EVENT_ENABLED(ip, DM_EVENT_TRUNCATE) &&
341 !(flags & ATTR_DMI)) {
342 int dmflags = AT_DELAY_FLAG(flags) | DM_SEM_FLAG_WR;
343 code = XFS_SEND_DATA(mp, DM_EVENT_TRUNCATE, vp,
344 vap->va_size, 0, dmflags, NULL);
351 lock_flags |= XFS_IOLOCK_EXCL;
354 xfs_ilock(ip, lock_flags);
356 /* boolean: are we the file owner? */
357 file_owner = (current_fsuid(credp) == ip->i_d.di_uid);
360 * Change various properties of a file.
361 * Only the owner or users with CAP_FOWNER
362 * capability may do these things.
365 (XFS_AT_MODE|XFS_AT_XFLAGS|XFS_AT_EXTSIZE|XFS_AT_UID|
366 XFS_AT_GID|XFS_AT_PROJID)) {
368 * CAP_FOWNER overrides the following restrictions:
370 * The user ID of the calling process must be equal
371 * to the file owner ID, except in cases where the
372 * CAP_FSETID capability is applicable.
374 if (!file_owner && !capable(CAP_FOWNER)) {
375 code = XFS_ERROR(EPERM);
380 * CAP_FSETID overrides the following restrictions:
382 * The effective user ID of the calling process shall match
383 * the file owner when setting the set-user-ID and
384 * set-group-ID bits on that file.
386 * The effective group ID or one of the supplementary group
387 * IDs of the calling process shall match the group owner of
388 * the file when setting the set-group-ID bit on that file
390 if (mask & XFS_AT_MODE) {
393 if ((vap->va_mode & S_ISUID) && !file_owner)
395 if ((vap->va_mode & S_ISGID) &&
396 !in_group_p((gid_t)ip->i_d.di_gid))
399 /* Linux allows this, Irix doesn't. */
400 if ((vap->va_mode & S_ISVTX) && !VN_ISDIR(vp))
403 if (m && !capable(CAP_FSETID))
409 * Change file ownership. Must be the owner or privileged.
410 * If the system was configured with the "restricted_chown"
411 * option, the owner is not permitted to give away the file,
412 * and can change the group id only to a group of which he
413 * or she is a member.
415 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
417 * These IDs could have changed since we last looked at them.
418 * But, we're assured that if the ownership did change
419 * while we didn't have the inode locked, inode's dquot(s)
420 * would have changed also.
422 iuid = ip->i_d.di_uid;
423 iprojid = ip->i_d.di_projid;
424 igid = ip->i_d.di_gid;
425 gid = (mask & XFS_AT_GID) ? vap->va_gid : igid;
426 uid = (mask & XFS_AT_UID) ? vap->va_uid : iuid;
427 projid = (mask & XFS_AT_PROJID) ? (xfs_prid_t)vap->va_projid :
431 * CAP_CHOWN overrides the following restrictions:
433 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
434 * shall override the restriction that a process cannot
435 * change the user ID of a file it owns and the restriction
436 * that the group ID supplied to the chown() function
437 * shall be equal to either the group ID or one of the
438 * supplementary group IDs of the calling process.
440 if (restricted_chown &&
441 (iuid != uid || (igid != gid &&
442 !in_group_p((gid_t)gid))) &&
443 !capable(CAP_CHOWN)) {
444 code = XFS_ERROR(EPERM);
448 * Do a quota reservation only if uid/projid/gid is actually
451 if ((XFS_IS_UQUOTA_ON(mp) && iuid != uid) ||
452 (XFS_IS_PQUOTA_ON(mp) && iprojid != projid) ||
453 (XFS_IS_GQUOTA_ON(mp) && igid != gid)) {
455 code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
456 capable(CAP_FOWNER) ?
457 XFS_QMOPT_FORCE_RES : 0);
458 if (code) /* out of quota */
464 * Truncate file. Must have write permission and not be a directory.
466 if (mask & XFS_AT_SIZE) {
467 /* Short circuit the truncate case for zero length files */
468 if ((vap->va_size == 0) &&
469 (ip->i_size == 0) && (ip->i_d.di_nextents == 0)) {
470 xfs_iunlock(ip, XFS_ILOCK_EXCL);
471 lock_flags &= ~XFS_ILOCK_EXCL;
472 if (mask & XFS_AT_CTIME)
473 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
479 code = XFS_ERROR(EISDIR);
481 } else if (!VN_ISREG(vp)) {
482 code = XFS_ERROR(EINVAL);
486 * Make sure that the dquots are attached to the inode.
488 if ((code = XFS_QM_DQATTACH(mp, ip, XFS_QMOPT_ILOCKED)))
493 * Change file access or modified times.
495 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
497 if ((flags & ATTR_UTIME) &&
498 !capable(CAP_FOWNER)) {
499 code = XFS_ERROR(EPERM);
506 * Change extent size or realtime flag.
508 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
510 * Can't change extent size if any extents are allocated.
512 if (ip->i_d.di_nextents && (mask & XFS_AT_EXTSIZE) &&
513 ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
515 code = XFS_ERROR(EINVAL); /* EFBIG? */
520 * Can't change realtime flag if any extents are allocated.
522 if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
523 (mask & XFS_AT_XFLAGS) &&
524 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) !=
525 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
526 code = XFS_ERROR(EINVAL); /* EFBIG? */
530 * Extent size must be a multiple of the appropriate block
531 * size, if set at all.
533 if ((mask & XFS_AT_EXTSIZE) && vap->va_extsize != 0) {
536 if ((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ||
537 ((mask & XFS_AT_XFLAGS) &&
538 (vap->va_xflags & XFS_XFLAG_REALTIME))) {
539 size = mp->m_sb.sb_rextsize <<
540 mp->m_sb.sb_blocklog;
542 size = mp->m_sb.sb_blocksize;
544 if (vap->va_extsize % size) {
545 code = XFS_ERROR(EINVAL);
550 * If realtime flag is set then must have realtime data.
552 if ((mask & XFS_AT_XFLAGS) &&
553 (vap->va_xflags & XFS_XFLAG_REALTIME)) {
554 if ((mp->m_sb.sb_rblocks == 0) ||
555 (mp->m_sb.sb_rextsize == 0) ||
556 (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
557 code = XFS_ERROR(EINVAL);
563 * Can't modify an immutable/append-only file unless
564 * we have appropriate permission.
566 if ((mask & XFS_AT_XFLAGS) &&
568 (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
570 (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
571 !capable(CAP_LINUX_IMMUTABLE)) {
572 code = XFS_ERROR(EPERM);
578 * Now we can make the changes. Before we join the inode
579 * to the transaction, if XFS_AT_SIZE is set then take care of
580 * the part of the truncation that must be done without the
581 * inode lock. This needs to be done before joining the inode
582 * to the transaction, because the inode cannot be unlocked
583 * once it is a part of the transaction.
585 if (mask & XFS_AT_SIZE) {
587 if ((vap->va_size > ip->i_size) &&
588 (flags & ATTR_NOSIZETOK) == 0) {
589 code = xfs_igrow_start(ip, vap->va_size, credp);
591 xfs_iunlock(ip, XFS_ILOCK_EXCL);
594 * We are going to log the inode size change in this
595 * transaction so any previous writes that are beyond the on
596 * disk EOF and the new EOF that have not been written out need
597 * to be written here. If we do not write the data out, we
598 * expose ourselves to the null files problem.
600 * Only flush from the on disk size to the smaller of the in
601 * memory file size or the new size as that's the range we
602 * really care about here and prevents waiting for other data
603 * not within the range we care about here.
606 (ip->i_size != ip->i_d.di_size) &&
607 (vap->va_size > ip->i_d.di_size)) {
608 code = bhv_vop_flush_pages(XFS_ITOV(ip),
609 ip->i_d.di_size, vap->va_size,
610 XFS_B_ASYNC, FI_NONE);
613 /* wait for all I/O to complete */
617 code = xfs_itruncate_data(ip, vap->va_size);
620 lock_flags &= ~XFS_ILOCK_EXCL;
621 ASSERT(lock_flags == XFS_IOLOCK_EXCL);
624 tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_SIZE);
625 if ((code = xfs_trans_reserve(tp, 0,
626 XFS_ITRUNCATE_LOG_RES(mp), 0,
627 XFS_TRANS_PERM_LOG_RES,
628 XFS_ITRUNCATE_LOG_COUNT))) {
629 xfs_trans_cancel(tp, 0);
631 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
634 commit_flags = XFS_TRANS_RELEASE_LOG_RES;
635 xfs_ilock(ip, XFS_ILOCK_EXCL);
639 xfs_trans_ijoin(tp, ip, lock_flags);
640 xfs_trans_ihold(tp, ip);
643 /* determine whether mandatory locking mode changes */
644 mandlock_before = MANDLOCK(vp, ip->i_d.di_mode);
647 * Truncate file. Must have write permission and not be a directory.
649 if (mask & XFS_AT_SIZE) {
650 if (vap->va_size > ip->i_size) {
651 xfs_igrow_finish(tp, ip, vap->va_size,
652 !(flags & ATTR_DMI));
653 } else if ((vap->va_size <= ip->i_size) ||
654 ((vap->va_size == 0) && ip->i_d.di_nextents)) {
656 * signal a sync transaction unless
657 * we're truncating an already unlinked
658 * file on a wsync filesystem
660 code = xfs_itruncate_finish(&tp, ip,
661 (xfs_fsize_t)vap->va_size,
663 ((ip->i_d.di_nlink != 0 ||
664 !(mp->m_flags & XFS_MOUNT_WSYNC))
669 * Truncated "down", so we're removing references
670 * to old data here - if we now delay flushing for
671 * a long time, we expose ourselves unduly to the
672 * notorious NULL files problem. So, we mark this
673 * vnode and flush it when the file is closed, and
674 * do not wait the usual (long) time for writeout.
679 * Have to do this even if the file's size doesn't change.
681 timeflags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
685 * Change file access modes.
687 if (mask & XFS_AT_MODE) {
688 ip->i_d.di_mode &= S_IFMT;
689 ip->i_d.di_mode |= vap->va_mode & ~S_IFMT;
691 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
692 timeflags |= XFS_ICHGTIME_CHG;
696 * Change file ownership. Must be the owner or privileged.
697 * If the system was configured with the "restricted_chown"
698 * option, the owner is not permitted to give away the file,
699 * and can change the group id only to a group of which he
700 * or she is a member.
702 if (mask & (XFS_AT_UID|XFS_AT_GID|XFS_AT_PROJID)) {
704 * CAP_FSETID overrides the following restrictions:
706 * The set-user-ID and set-group-ID bits of a file will be
707 * cleared upon successful return from chown()
709 if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
710 !capable(CAP_FSETID)) {
711 ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
715 * Change the ownerships and register quota modifications
716 * in the transaction.
719 if (XFS_IS_UQUOTA_ON(mp)) {
720 ASSERT(mask & XFS_AT_UID);
722 olddquot1 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
723 &ip->i_udquot, udqp);
725 ip->i_d.di_uid = uid;
728 if (XFS_IS_GQUOTA_ON(mp)) {
729 ASSERT(!XFS_IS_PQUOTA_ON(mp));
730 ASSERT(mask & XFS_AT_GID);
732 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
733 &ip->i_gdquot, gdqp);
735 ip->i_d.di_gid = gid;
737 if (iprojid != projid) {
738 if (XFS_IS_PQUOTA_ON(mp)) {
739 ASSERT(!XFS_IS_GQUOTA_ON(mp));
740 ASSERT(mask & XFS_AT_PROJID);
742 olddquot2 = XFS_QM_DQVOPCHOWN(mp, tp, ip,
743 &ip->i_gdquot, gdqp);
745 ip->i_d.di_projid = projid;
747 * We may have to rev the inode as well as
748 * the superblock version number since projids didn't
749 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
751 if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
752 xfs_bump_ino_vers2(tp, ip);
755 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
756 timeflags |= XFS_ICHGTIME_CHG;
761 * Change file access or modified times.
763 if (mask & (XFS_AT_ATIME|XFS_AT_MTIME)) {
764 if (mask & XFS_AT_ATIME) {
765 ip->i_d.di_atime.t_sec = vap->va_atime.tv_sec;
766 ip->i_d.di_atime.t_nsec = vap->va_atime.tv_nsec;
767 ip->i_update_core = 1;
768 timeflags &= ~XFS_ICHGTIME_ACC;
770 if (mask & XFS_AT_MTIME) {
771 ip->i_d.di_mtime.t_sec = vap->va_mtime.tv_sec;
772 ip->i_d.di_mtime.t_nsec = vap->va_mtime.tv_nsec;
773 timeflags &= ~XFS_ICHGTIME_MOD;
774 timeflags |= XFS_ICHGTIME_CHG;
776 if (tp && (flags & ATTR_UTIME))
777 xfs_trans_log_inode (tp, ip, XFS_ILOG_CORE);
781 * Change XFS-added attributes.
783 if (mask & (XFS_AT_EXTSIZE|XFS_AT_XFLAGS)) {
784 if (mask & XFS_AT_EXTSIZE) {
786 * Converting bytes to fs blocks.
788 ip->i_d.di_extsize = vap->va_extsize >>
789 mp->m_sb.sb_blocklog;
791 if (mask & XFS_AT_XFLAGS) {
794 /* can't set PREALLOC this way, just preserve it */
795 di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
796 if (vap->va_xflags & XFS_XFLAG_IMMUTABLE)
797 di_flags |= XFS_DIFLAG_IMMUTABLE;
798 if (vap->va_xflags & XFS_XFLAG_APPEND)
799 di_flags |= XFS_DIFLAG_APPEND;
800 if (vap->va_xflags & XFS_XFLAG_SYNC)
801 di_flags |= XFS_DIFLAG_SYNC;
802 if (vap->va_xflags & XFS_XFLAG_NOATIME)
803 di_flags |= XFS_DIFLAG_NOATIME;
804 if (vap->va_xflags & XFS_XFLAG_NODUMP)
805 di_flags |= XFS_DIFLAG_NODUMP;
806 if (vap->va_xflags & XFS_XFLAG_PROJINHERIT)
807 di_flags |= XFS_DIFLAG_PROJINHERIT;
808 if (vap->va_xflags & XFS_XFLAG_NODEFRAG)
809 di_flags |= XFS_DIFLAG_NODEFRAG;
810 if (vap->va_xflags & XFS_XFLAG_FILESTREAM)
811 di_flags |= XFS_DIFLAG_FILESTREAM;
812 if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
813 if (vap->va_xflags & XFS_XFLAG_RTINHERIT)
814 di_flags |= XFS_DIFLAG_RTINHERIT;
815 if (vap->va_xflags & XFS_XFLAG_NOSYMLINKS)
816 di_flags |= XFS_DIFLAG_NOSYMLINKS;
817 if (vap->va_xflags & XFS_XFLAG_EXTSZINHERIT)
818 di_flags |= XFS_DIFLAG_EXTSZINHERIT;
819 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
820 if (vap->va_xflags & XFS_XFLAG_REALTIME) {
821 di_flags |= XFS_DIFLAG_REALTIME;
822 ip->i_iocore.io_flags |= XFS_IOCORE_RT;
824 ip->i_iocore.io_flags &= ~XFS_IOCORE_RT;
826 if (vap->va_xflags & XFS_XFLAG_EXTSIZE)
827 di_flags |= XFS_DIFLAG_EXTSIZE;
829 ip->i_d.di_flags = di_flags;
831 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
832 timeflags |= XFS_ICHGTIME_CHG;
836 * Change file inode change time only if XFS_AT_CTIME set
837 * AND we have been called by a DMI function.
840 if ( (flags & ATTR_DMI) && (mask & XFS_AT_CTIME) ) {
841 ip->i_d.di_ctime.t_sec = vap->va_ctime.tv_sec;
842 ip->i_d.di_ctime.t_nsec = vap->va_ctime.tv_nsec;
843 ip->i_update_core = 1;
844 timeflags &= ~XFS_ICHGTIME_CHG;
848 * Send out timestamp changes that need to be set to the
849 * current time. Not done when called by a DMI function.
851 if (timeflags && !(flags & ATTR_DMI))
852 xfs_ichgtime(ip, timeflags);
854 XFS_STATS_INC(xs_ig_attrchg);
857 * If this is a synchronous mount, make sure that the
858 * transaction goes to disk before returning to the user.
859 * This is slightly sub-optimal in that truncates require
860 * two sync transactions instead of one for wsync filesystems.
861 * One for the truncate and one for the timestamps since we
862 * don't want to change the timestamps unless we're sure the
863 * truncate worked. Truncates are less than 1% of the laddis
864 * mix so this probably isn't worth the trouble to optimize.
868 if (mp->m_flags & XFS_MOUNT_WSYNC)
869 xfs_trans_set_sync(tp);
871 code = xfs_trans_commit(tp, commit_flags);
875 * If the (regular) file's mandatory locking mode changed, then
876 * notify the vnode. We do this under the inode lock to prevent
877 * racing calls to vop_vnode_change.
879 mandlock_after = MANDLOCK(vp, ip->i_d.di_mode);
880 if (mandlock_before != mandlock_after) {
881 bhv_vop_vnode_change(vp, VCHANGE_FLAGS_ENF_LOCKING,
885 xfs_iunlock(ip, lock_flags);
888 * Release any dquot(s) the inode had kept before chown.
890 XFS_QM_DQRELE(mp, olddquot1);
891 XFS_QM_DQRELE(mp, olddquot2);
892 XFS_QM_DQRELE(mp, udqp);
893 XFS_QM_DQRELE(mp, gdqp);
899 if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE) &&
900 !(flags & ATTR_DMI)) {
901 (void) XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, vp, DM_RIGHT_NULL,
902 NULL, DM_RIGHT_NULL, NULL, NULL,
903 0, 0, AT_DELAY_FLAG(flags));
908 commit_flags |= XFS_TRANS_ABORT;
911 XFS_QM_DQRELE(mp, udqp);
912 XFS_QM_DQRELE(mp, gdqp);
914 xfs_trans_cancel(tp, commit_flags);
916 if (lock_flags != 0) {
917 xfs_iunlock(ip, lock_flags);
925 * Null conversion from vnode mode bits to inode mode bits, as in efs.
936 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
937 (inst_t *)__return_address);
939 ip = XFS_BHVTOI(bdp);
940 xfs_ilock(ip, XFS_ILOCK_SHARED);
941 error = xfs_iaccess(ip, mode, credp);
942 xfs_iunlock(ip, XFS_ILOCK_SHARED);
948 * The maximum pathlen is 1024 bytes. Since the minimum file system
949 * blocksize is 512 bytes, we can get a max of 2 extents back from
952 #define SYMLINK_MAPS 2
973 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
979 vp = BHV_TO_VNODE(bdp);
980 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
982 ip = XFS_BHVTOI(bdp);
985 if (XFS_FORCED_SHUTDOWN(mp))
986 return XFS_ERROR(EIO);
988 xfs_ilock(ip, XFS_ILOCK_SHARED);
990 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFLNK);
992 offset = uiop->uio_offset;
993 count = uiop->uio_resid;
996 error = XFS_ERROR(EINVAL);
1005 * See if the symlink is stored inline.
1007 pathlen = (int)ip->i_d.di_size;
1009 if (ip->i_df.if_flags & XFS_IFINLINE) {
1010 error = xfs_uio_read(ip->i_df.if_u1.if_data, pathlen, uiop);
1014 * Symlink not inline. Call bmap to get it in.
1016 nmaps = SYMLINK_MAPS;
1018 error = xfs_bmapi(NULL, ip, 0, XFS_B_TO_FSB(mp, pathlen),
1019 0, NULL, 0, mval, &nmaps, NULL, NULL);
1025 for (n = 0; n < nmaps; n++) {
1026 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
1027 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
1028 bp = xfs_buf_read(mp->m_ddev_targp, d,
1029 BTOBB(byte_cnt), 0);
1030 error = XFS_BUF_GETERROR(bp);
1032 xfs_ioerror_alert("xfs_readlink",
1033 ip->i_mount, bp, XFS_BUF_ADDR(bp));
1037 if (pathlen < byte_cnt)
1039 pathlen -= byte_cnt;
1041 error = xfs_uio_read(XFS_BUF_PTR(bp), byte_cnt, uiop);
1048 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1056 * This is called to sync the inode and its data out to disk.
1057 * We need to hold the I/O lock while flushing the data, and
1058 * the inode lock while flushing the inode. The inode lock CANNOT
1059 * be held while flushing the data, so acquire after we're done
1073 int log_flushed = 0, changed = 1;
1075 vn_trace_entry(BHV_TO_VNODE(bdp),
1076 __FUNCTION__, (inst_t *)__return_address);
1078 ip = XFS_BHVTOI(bdp);
1080 ASSERT(start >= 0 && stop >= -1);
1082 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
1083 return XFS_ERROR(EIO);
1085 if (flag & FSYNC_DATA)
1086 filemap_fdatawait(vn_to_inode(XFS_ITOV(ip))->i_mapping);
1089 * We always need to make sure that the required inode state
1090 * is safe on disk. The vnode might be clean but because
1091 * of committed transactions that haven't hit the disk yet.
1092 * Likewise, there could be unflushed non-transactional
1093 * changes to the inode core that have to go to disk.
1095 * The following code depends on one assumption: that
1096 * any transaction that changes an inode logs the core
1097 * because it has to change some field in the inode core
1098 * (typically nextents or nblocks). That assumption
1099 * implies that any transactions against an inode will
1100 * catch any non-transactional updates. If inode-altering
1101 * transactions exist that violate this assumption, the
1102 * code breaks. Right now, it figures that if the involved
1103 * update_* field is clear and the inode is unpinned, the
1104 * inode is clean. Either it's been flushed or it's been
1105 * committed and the commit has hit the disk unpinning the inode.
1106 * (Note that xfs_inode_item_format() called at commit clears
1107 * the update_* fields.)
1109 xfs_ilock(ip, XFS_ILOCK_SHARED);
1111 /* If we are flushing data then we care about update_size
1112 * being set, otherwise we care about update_core
1114 if ((flag & FSYNC_DATA) ?
1115 (ip->i_update_size == 0) :
1116 (ip->i_update_core == 0)) {
1118 * Timestamps/size haven't changed since last inode
1119 * flush or inode transaction commit. That means
1120 * either nothing got written or a transaction
1121 * committed which caught the updates. If the
1122 * latter happened and the transaction hasn't
1123 * hit the disk yet, the inode will be still
1124 * be pinned. If it is, force the log.
1127 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1129 if (xfs_ipincount(ip)) {
1130 _xfs_log_force(ip->i_mount, (xfs_lsn_t)0,
1132 ((flag & FSYNC_WAIT)
1133 ? XFS_LOG_SYNC : 0),
1137 * If the inode is not pinned and nothing
1138 * has changed we don't need to flush the
1146 * Kick off a transaction to log the inode
1147 * core to get the updates. Make it
1148 * sync if FSYNC_WAIT is passed in (which
1149 * is done by everybody but specfs). The
1150 * sync transaction will also force the log.
1152 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1153 tp = xfs_trans_alloc(ip->i_mount, XFS_TRANS_FSYNC_TS);
1154 if ((error = xfs_trans_reserve(tp, 0,
1155 XFS_FSYNC_TS_LOG_RES(ip->i_mount),
1157 xfs_trans_cancel(tp, 0);
1160 xfs_ilock(ip, XFS_ILOCK_EXCL);
1163 * Note - it's possible that we might have pushed
1164 * ourselves out of the way during trans_reserve
1165 * which would flush the inode. But there's no
1166 * guarantee that the inode buffer has actually
1167 * gone out yet (it's delwri). Plus the buffer
1168 * could be pinned anyway if it's part of an
1169 * inode in another recent transaction. So we
1170 * play it safe and fire off the transaction anyway.
1172 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
1173 xfs_trans_ihold(tp, ip);
1174 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1175 if (flag & FSYNC_WAIT)
1176 xfs_trans_set_sync(tp);
1177 error = _xfs_trans_commit(tp, 0, &log_flushed);
1179 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1182 if ((ip->i_mount->m_flags & XFS_MOUNT_BARRIER) && changed) {
1184 * If the log write didn't issue an ordered tag we need
1185 * to flush the disk cache for the data device now.
1188 xfs_blkdev_issue_flush(ip->i_mount->m_ddev_targp);
1191 * If this inode is on the RT dev we need to flush that
1194 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)
1195 xfs_blkdev_issue_flush(ip->i_mount->m_rtdev_targp);
1202 * This is called by xfs_inactive to free any blocks beyond eof
1203 * when the link count isn't zero and by xfs_dm_punch_hole() when
1204 * punching a hole to EOF.
1214 xfs_fileoff_t end_fsb;
1215 xfs_fileoff_t last_fsb;
1216 xfs_filblks_t map_len;
1218 xfs_bmbt_irec_t imap;
1219 int use_iolock = (flags & XFS_FREE_EOF_LOCK);
1222 * Figure out if there are any blocks beyond the end
1223 * of the file. If not, then there is nothing to do.
1225 end_fsb = XFS_B_TO_FSB(mp, ((xfs_ufsize_t)ip->i_size));
1226 last_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_MAXIOFFSET(mp));
1227 map_len = last_fsb - end_fsb;
1232 xfs_ilock(ip, XFS_ILOCK_SHARED);
1233 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, end_fsb, map_len, 0,
1234 NULL, 0, &imap, &nimaps, NULL, NULL);
1235 xfs_iunlock(ip, XFS_ILOCK_SHARED);
1237 if (!error && (nimaps != 0) &&
1238 (imap.br_startblock != HOLESTARTBLOCK ||
1239 ip->i_delayed_blks)) {
1241 * Attach the dquots to the inode up front.
1243 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1247 * There are blocks after the end of file.
1248 * Free them up now by truncating the file to
1251 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1254 * Do the xfs_itruncate_start() call before
1255 * reserving any log space because
1256 * itruncate_start will call into the buffer
1257 * cache and we can't
1258 * do that within a transaction.
1261 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1262 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE,
1265 xfs_trans_cancel(tp, 0);
1267 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1271 error = xfs_trans_reserve(tp, 0,
1272 XFS_ITRUNCATE_LOG_RES(mp),
1273 0, XFS_TRANS_PERM_LOG_RES,
1274 XFS_ITRUNCATE_LOG_COUNT);
1276 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1277 xfs_trans_cancel(tp, 0);
1278 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1282 xfs_ilock(ip, XFS_ILOCK_EXCL);
1283 xfs_trans_ijoin(tp, ip,
1286 xfs_trans_ihold(tp, ip);
1288 error = xfs_itruncate_finish(&tp, ip,
1293 * If we get an error at this point we
1294 * simply don't bother truncating the file.
1297 xfs_trans_cancel(tp,
1298 (XFS_TRANS_RELEASE_LOG_RES |
1301 error = xfs_trans_commit(tp,
1302 XFS_TRANS_RELEASE_LOG_RES);
1304 xfs_iunlock(ip, (use_iolock ? (XFS_IOLOCK_EXCL|XFS_ILOCK_EXCL)
1311 * Free a symlink that has blocks associated with it.
1314 xfs_inactive_symlink_rmt(
1322 xfs_fsblock_t first_block;
1323 xfs_bmap_free_t free_list;
1326 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
1334 ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
1336 * We're freeing a symlink that has some
1337 * blocks allocated to it. Free the
1338 * blocks here. We know that we've got
1339 * either 1 or 2 extents and that we can
1340 * free them all in one bunmapi call.
1342 ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
1343 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1344 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1345 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1346 xfs_trans_cancel(tp, 0);
1351 * Lock the inode, fix the size, and join it to the transaction.
1352 * Hold it so in the normal path, we still have it locked for
1353 * the second transaction. In the error paths we need it
1354 * held so the cancel won't rele it, see below.
1356 xfs_ilock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1357 size = (int)ip->i_d.di_size;
1358 ip->i_d.di_size = 0;
1359 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1360 xfs_trans_ihold(tp, ip);
1361 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1363 * Find the block(s) so we can inval and unmap them.
1366 XFS_BMAP_INIT(&free_list, &first_block);
1367 nmaps = ARRAY_SIZE(mval);
1368 if ((error = xfs_bmapi(tp, ip, 0, XFS_B_TO_FSB(mp, size),
1369 XFS_BMAPI_METADATA, &first_block, 0, mval, &nmaps,
1373 * Invalidate the block(s).
1375 for (i = 0; i < nmaps; i++) {
1376 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
1377 XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
1378 XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
1379 xfs_trans_binval(tp, bp);
1382 * Unmap the dead block(s) to the free_list.
1384 if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
1385 &first_block, &free_list, NULL, &done)))
1389 * Commit the first transaction. This logs the EFI and the inode.
1391 if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
1394 * The transaction must have been committed, since there were
1395 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1396 * The new tp has the extent freeing and EFDs.
1400 * The first xact was committed, so add the inode to the new one.
1401 * Mark it dirty so it will be logged and moved forward in the log as
1402 * part of every commit.
1404 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1405 xfs_trans_ihold(tp, ip);
1406 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1408 * Get a new, empty transaction to return to our caller.
1410 ntp = xfs_trans_dup(tp);
1412 * Commit the transaction containing extent freeing and EFDs.
1413 * If we get an error on the commit here or on the reserve below,
1414 * we need to unlock the inode since the new transaction doesn't
1415 * have the inode attached.
1417 error = xfs_trans_commit(tp, 0);
1420 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1424 * Remove the memory for extent descriptions (just bookkeeping).
1426 if (ip->i_df.if_bytes)
1427 xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
1428 ASSERT(ip->i_df.if_bytes == 0);
1430 * Put an itruncate log reservation in the new transaction
1433 if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
1434 XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
1435 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1439 * Return with the inode locked but not joined to the transaction.
1445 xfs_bmap_cancel(&free_list);
1448 * Have to come here with the inode locked and either
1449 * (held and in the transaction) or (not in the transaction).
1450 * If the inode isn't held then cancel would iput it, but
1451 * that's wrong since this is inactive and the vnode ref
1452 * count is 0 already.
1453 * Cancel won't do anything to the inode if held, but it still
1454 * needs to be locked until the cancel is done, if it was
1455 * joined to the transaction.
1457 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1458 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1465 xfs_inactive_symlink_local(
1471 ASSERT(ip->i_d.di_size <= XFS_IFORK_DSIZE(ip));
1473 * We're freeing a symlink which fit into
1474 * the inode. Just free the memory used
1475 * to hold the old symlink.
1477 error = xfs_trans_reserve(*tpp, 0,
1478 XFS_ITRUNCATE_LOG_RES(ip->i_mount),
1479 0, XFS_TRANS_PERM_LOG_RES,
1480 XFS_ITRUNCATE_LOG_COUNT);
1483 xfs_trans_cancel(*tpp, 0);
1487 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1490 * Zero length symlinks _can_ exist.
1492 if (ip->i_df.if_bytes > 0) {
1493 xfs_idata_realloc(ip,
1494 -(ip->i_df.if_bytes),
1496 ASSERT(ip->i_df.if_bytes == 0);
1510 ASSERT(ismrlocked(&ip->i_iolock, MR_UPDATE));
1513 ASSERT(ip->i_d.di_forkoff != 0);
1514 xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1515 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1517 error = xfs_attr_inactive(ip);
1520 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1521 return error; /* goto out */
1524 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1525 error = xfs_trans_reserve(tp, 0,
1526 XFS_IFREE_LOG_RES(mp),
1527 0, XFS_TRANS_PERM_LOG_RES,
1528 XFS_INACTIVE_LOG_COUNT);
1530 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1531 xfs_trans_cancel(tp, 0);
1533 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1537 xfs_ilock(ip, XFS_ILOCK_EXCL);
1538 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1539 xfs_trans_ihold(tp, ip);
1540 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1542 ASSERT(ip->i_d.di_anextents == 0);
1557 vp = BHV_TO_VNODE(bdp);
1558 ip = XFS_BHVTOI(bdp);
1561 if (!VN_ISREG(vp) || (ip->i_d.di_mode == 0))
1564 /* If this is a read-only mount, don't do this (would generate I/O) */
1565 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1568 if (!XFS_FORCED_SHUTDOWN(mp)) {
1570 * If we are using filestreams, and we have an unlinked
1571 * file that we are processing the last close on, then nothing
1572 * will be able to reopen and write to this file. Purge this
1573 * inode from the filestreams cache so that it doesn't delay
1574 * teardown of the inode.
1576 if ((ip->i_d.di_nlink == 0) && xfs_inode_is_filestream(ip))
1577 xfs_filestream_deassociate(ip);
1580 * If we previously truncated this file and removed old data
1581 * in the process, we want to initiate "early" writeout on
1582 * the last close. This is an attempt to combat the notorious
1583 * NULL files problem which is particularly noticable from a
1584 * truncate down, buffered (re-)write (delalloc), followed by
1585 * a crash. What we are effectively doing here is
1586 * significantly reducing the time window where we'd otherwise
1587 * be exposed to that problem.
1589 if (VUNTRUNCATE(vp) && VN_DIRTY(vp) && ip->i_delayed_blks > 0)
1590 bhv_vop_flush_pages(vp, 0, -1, XFS_B_ASYNC, FI_NONE);
1593 #ifdef HAVE_REFCACHE
1594 /* If we are in the NFS reference cache then don't do this now */
1599 if (ip->i_d.di_nlink != 0) {
1600 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1601 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1602 ip->i_delayed_blks > 0)) &&
1603 (ip->i_df.if_flags & XFS_IFEXTENTS)) &&
1604 (!(ip->i_d.di_flags &
1605 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))) {
1606 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1609 /* Update linux inode block count after free above */
1610 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1611 ip->i_d.di_nblocks + ip->i_delayed_blks);
1621 * This is called when the vnode reference count for the vnode
1622 * goes to zero. If the file has been unlinked, then it must
1623 * now be truncated. Also, we clear all of the read-ahead state
1624 * kept for the inode here since the file is now closed.
1633 xfs_bmap_free_t free_list;
1634 xfs_fsblock_t first_block;
1641 vp = BHV_TO_VNODE(bdp);
1642 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
1644 ip = XFS_BHVTOI(bdp);
1647 * If the inode is already free, then there can be nothing
1650 if (ip->i_d.di_mode == 0 || VN_BAD(vp)) {
1651 ASSERT(ip->i_df.if_real_bytes == 0);
1652 ASSERT(ip->i_df.if_broot_bytes == 0);
1653 return VN_INACTIVE_CACHE;
1657 * Only do a truncate if it's a regular file with
1658 * some actual space in it. It's OK to look at the
1659 * inode's fields without the lock because we're the
1660 * only one with a reference to the inode.
1662 truncate = ((ip->i_d.di_nlink == 0) &&
1663 ((ip->i_d.di_size != 0) || (ip->i_size != 0) ||
1664 (ip->i_d.di_nextents > 0) || (ip->i_delayed_blks > 0)) &&
1665 ((ip->i_d.di_mode & S_IFMT) == S_IFREG));
1669 if (ip->i_d.di_nlink == 0 && DM_EVENT_ENABLED(ip, DM_EVENT_DESTROY)) {
1670 (void) XFS_SEND_DESTROY(mp, vp, DM_RIGHT_NULL);
1675 /* If this is a read-only mount, don't do this (would generate I/O) */
1676 if (vp->v_vfsp->vfs_flag & VFS_RDONLY)
1679 if (ip->i_d.di_nlink != 0) {
1680 if ((((ip->i_d.di_mode & S_IFMT) == S_IFREG) &&
1681 ((ip->i_size > 0) || (VN_CACHED(vp) > 0 ||
1682 ip->i_delayed_blks > 0)) &&
1683 (ip->i_df.if_flags & XFS_IFEXTENTS) &&
1684 (!(ip->i_d.di_flags &
1685 (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)) ||
1686 (ip->i_delayed_blks != 0)))) {
1687 error = xfs_free_eofblocks(mp, ip, XFS_FREE_EOF_LOCK);
1689 return VN_INACTIVE_CACHE;
1690 /* Update linux inode block count after free above */
1691 vn_to_inode(vp)->i_blocks = XFS_FSB_TO_BB(mp,
1692 ip->i_d.di_nblocks + ip->i_delayed_blks);
1697 ASSERT(ip->i_d.di_nlink == 0);
1699 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
1700 return VN_INACTIVE_CACHE;
1702 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
1705 * Do the xfs_itruncate_start() call before
1706 * reserving any log space because itruncate_start
1707 * will call into the buffer cache and we can't
1708 * do that within a transaction.
1710 xfs_ilock(ip, XFS_IOLOCK_EXCL);
1712 error = xfs_itruncate_start(ip, XFS_ITRUNC_DEFINITE, 0);
1714 xfs_trans_cancel(tp, 0);
1715 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1716 return VN_INACTIVE_CACHE;
1719 error = xfs_trans_reserve(tp, 0,
1720 XFS_ITRUNCATE_LOG_RES(mp),
1721 0, XFS_TRANS_PERM_LOG_RES,
1722 XFS_ITRUNCATE_LOG_COUNT);
1724 /* Don't call itruncate_cleanup */
1725 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1726 xfs_trans_cancel(tp, 0);
1727 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
1728 return VN_INACTIVE_CACHE;
1731 xfs_ilock(ip, XFS_ILOCK_EXCL);
1732 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1733 xfs_trans_ihold(tp, ip);
1736 * normally, we have to run xfs_itruncate_finish sync.
1737 * But if filesystem is wsync and we're in the inactive
1738 * path, then we know that nlink == 0, and that the
1739 * xaction that made nlink == 0 is permanently committed
1740 * since xfs_remove runs as a synchronous transaction.
1742 error = xfs_itruncate_finish(&tp, ip, 0, XFS_DATA_FORK,
1743 (!(mp->m_flags & XFS_MOUNT_WSYNC) ? 1 : 0));
1746 xfs_trans_cancel(tp,
1747 XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1748 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1749 return VN_INACTIVE_CACHE;
1751 } else if ((ip->i_d.di_mode & S_IFMT) == S_IFLNK) {
1754 * If we get an error while cleaning up a
1755 * symlink we bail out.
1757 error = (ip->i_d.di_size > XFS_IFORK_DSIZE(ip)) ?
1758 xfs_inactive_symlink_rmt(ip, &tp) :
1759 xfs_inactive_symlink_local(ip, &tp);
1763 return VN_INACTIVE_CACHE;
1766 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1767 xfs_trans_ihold(tp, ip);
1769 error = xfs_trans_reserve(tp, 0,
1770 XFS_IFREE_LOG_RES(mp),
1771 0, XFS_TRANS_PERM_LOG_RES,
1772 XFS_INACTIVE_LOG_COUNT);
1774 ASSERT(XFS_FORCED_SHUTDOWN(mp));
1775 xfs_trans_cancel(tp, 0);
1776 return VN_INACTIVE_CACHE;
1779 xfs_ilock(ip, XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL);
1780 xfs_trans_ijoin(tp, ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1781 xfs_trans_ihold(tp, ip);
1785 * If there are attributes associated with the file
1786 * then blow them away now. The code calls a routine
1787 * that recursively deconstructs the attribute fork.
1788 * We need to just commit the current transaction
1789 * because we can't use it for xfs_attr_inactive().
1791 if (ip->i_d.di_anextents > 0) {
1792 error = xfs_inactive_attrs(ip, &tp);
1794 * If we got an error, the transaction is already
1795 * cancelled, and the inode is unlocked. Just get out.
1798 return VN_INACTIVE_CACHE;
1799 } else if (ip->i_afp) {
1800 xfs_idestroy_fork(ip, XFS_ATTR_FORK);
1806 XFS_BMAP_INIT(&free_list, &first_block);
1807 error = xfs_ifree(tp, ip, &free_list);
1810 * If we fail to free the inode, shut down. The cancel
1811 * might do that, we need to make sure. Otherwise the
1812 * inode might be lost for a long time or forever.
1814 if (!XFS_FORCED_SHUTDOWN(mp)) {
1816 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1817 error, mp->m_fsname);
1818 xfs_force_shutdown(mp, SHUTDOWN_META_IO_ERROR);
1820 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
1823 * Credit the quota account(s). The inode is gone.
1825 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_ICOUNT, -1);
1828 * Just ignore errors at this point. There is
1829 * nothing we can do except to try to keep going.
1831 (void) xfs_bmap_finish(&tp, &free_list, &committed);
1832 (void) xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1835 * Release the dquots held by inode, if any.
1837 XFS_QM_DQDETACH(mp, ip);
1839 xfs_iunlock(ip, XFS_IOLOCK_EXCL | XFS_ILOCK_EXCL);
1842 return VN_INACTIVE_CACHE;
1851 bhv_desc_t *dir_bdp,
1852 bhv_vname_t *dentry,
1858 xfs_inode_t *dp, *ip;
1862 bhv_vnode_t *dir_vp;
1864 dir_vp = BHV_TO_VNODE(dir_bdp);
1865 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1867 dp = XFS_BHVTOI(dir_bdp);
1869 if (XFS_FORCED_SHUTDOWN(dp->i_mount))
1870 return XFS_ERROR(EIO);
1872 lock_mode = xfs_ilock_map_shared(dp);
1873 error = xfs_dir_lookup_int(dir_bdp, lock_mode, dentry, &e_inum, &ip);
1875 *vpp = XFS_ITOV(ip);
1878 xfs_iunlock_map_shared(dp, lock_mode);
1884 * xfs_create (create a new file).
1888 bhv_desc_t *dir_bdp,
1889 bhv_vname_t *dentry,
1894 char *name = VNAME(dentry);
1895 bhv_vnode_t *dir_vp;
1896 xfs_inode_t *dp, *ip;
1897 bhv_vnode_t *vp = NULL;
1902 xfs_bmap_free_t free_list;
1903 xfs_fsblock_t first_block;
1904 boolean_t dp_joined_to_trans;
1905 int dm_event_sent = 0;
1909 struct xfs_dquot *udqp, *gdqp;
1915 dir_vp = BHV_TO_VNODE(dir_bdp);
1916 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
1918 dp = XFS_BHVTOI(dir_bdp);
1921 dm_di_mode = vap->va_mode;
1922 namelen = VNAMELEN(dentry);
1924 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
1925 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
1926 dir_vp, DM_RIGHT_NULL, NULL,
1927 DM_RIGHT_NULL, name, NULL,
1935 if (XFS_FORCED_SHUTDOWN(mp))
1936 return XFS_ERROR(EIO);
1938 /* Return through std_return after this point. */
1941 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
1942 prid = dp->i_d.di_projid;
1943 else if (vap->va_mask & XFS_AT_PROJID)
1944 prid = (xfs_prid_t)vap->va_projid;
1946 prid = (xfs_prid_t)dfltprid;
1949 * Make sure that we have allocated dquot(s) on disk.
1951 error = XFS_QM_DQVOPALLOC(mp, dp,
1952 current_fsuid(credp), current_fsgid(credp), prid,
1953 XFS_QMOPT_QUOTALL|XFS_QMOPT_INHERIT, &udqp, &gdqp);
1958 dp_joined_to_trans = B_FALSE;
1960 tp = xfs_trans_alloc(mp, XFS_TRANS_CREATE);
1961 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
1962 resblks = XFS_CREATE_SPACE_RES(mp, namelen);
1964 * Initially assume that the file does not exist and
1965 * reserve the resources for that case. If that is not
1966 * the case we'll drop the one we have and get a more
1967 * appropriate transaction later.
1969 error = xfs_trans_reserve(tp, resblks, XFS_CREATE_LOG_RES(mp), 0,
1970 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1971 if (error == ENOSPC) {
1973 error = xfs_trans_reserve(tp, 0, XFS_CREATE_LOG_RES(mp), 0,
1974 XFS_TRANS_PERM_LOG_RES, XFS_CREATE_LOG_COUNT);
1982 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
1984 XFS_BMAP_INIT(&free_list, &first_block);
1989 * Reserve disk quota and the inode.
1991 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
1995 if (resblks == 0 && (error = xfs_dir_canenter(tp, dp, name, namelen)))
1997 rdev = (vap->va_mask & XFS_AT_RDEV) ? vap->va_rdev : 0;
1998 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 1,
1999 rdev, credp, prid, resblks > 0,
2002 if (error == ENOSPC)
2009 * At this point, we've gotten a newly allocated inode.
2010 * It is locked (and joined to the transaction).
2013 ASSERT(ismrlocked (&ip->i_lock, MR_UPDATE));
2016 * Now we join the directory inode to the transaction.
2017 * We do not do it earlier because xfs_dir_ialloc
2018 * might commit the previous transaction (and release
2023 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2024 dp_joined_to_trans = B_TRUE;
2026 error = xfs_dir_createname(tp, dp, name, namelen, ip->i_ino,
2027 &first_block, &free_list, resblks ?
2028 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2030 ASSERT(error != ENOSPC);
2033 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2034 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2037 * If this is a synchronous mount, make sure that the
2038 * create transaction goes to disk before returning to
2041 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2042 xfs_trans_set_sync(tp);
2048 * Attach the dquot(s) to the inodes and modify them incore.
2049 * These ids of the inode couldn't have changed since the new
2050 * inode has been locked ever since it was created.
2052 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
2055 * xfs_trans_commit normally decrements the vnode ref count
2056 * when it unlocks the inode. Since we want to return the
2057 * vnode to the caller, we bump the vnode ref count now.
2062 error = xfs_bmap_finish(&tp, &free_list, &committed);
2064 xfs_bmap_cancel(&free_list);
2068 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2075 XFS_QM_DQRELE(mp, udqp);
2076 XFS_QM_DQRELE(mp, gdqp);
2079 * Propagate the fact that the vnode changed after the
2080 * xfs_inode locks have been released.
2082 bhv_vop_vnode_change(vp, VCHANGE_FLAGS_TRUNCATED, 3);
2086 /* Fallthrough to std_return with error = 0 */
2089 if ((*vpp || (error != 0 && dm_event_sent != 0)) &&
2090 DM_EVENT_ENABLED(XFS_BHVTOI(dir_bdp), DM_EVENT_POSTCREATE)) {
2091 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
2092 dir_vp, DM_RIGHT_NULL,
2094 DM_RIGHT_NULL, name, NULL,
2095 dm_di_mode, error, 0);
2100 cancel_flags |= XFS_TRANS_ABORT;
2105 xfs_trans_cancel(tp, cancel_flags);
2107 if (!dp_joined_to_trans && (dp != NULL))
2108 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2109 XFS_QM_DQRELE(mp, udqp);
2110 XFS_QM_DQRELE(mp, gdqp);
2116 * Wait until after the current transaction is aborted to
2117 * release the inode. This prevents recursive transactions
2118 * and deadlocks from xfs_inactive.
2120 cancel_flags |= XFS_TRANS_ABORT;
2121 xfs_trans_cancel(tp, cancel_flags);
2124 XFS_QM_DQRELE(mp, udqp);
2125 XFS_QM_DQRELE(mp, gdqp);
2132 * Some counters to see if (and how often) we are hitting some deadlock
2133 * prevention code paths.
2137 int xfs_rm_lock_delays;
2138 int xfs_rm_attempts;
2142 * The following routine will lock the inodes associated with the
2143 * directory and the named entry in the directory. The locks are
2144 * acquired in increasing inode number.
2146 * If the entry is "..", then only the directory is locked. The
2147 * vnode ref count will still include that from the .. entry in
2150 * There is a deadlock we need to worry about. If the locked directory is
2151 * in the AIL, it might be blocking up the log. The next inode we lock
2152 * could be already locked by another thread waiting for log space (e.g
2153 * a permanent log reservation with a long running transaction (see
2154 * xfs_itruncate_finish)). To solve this, we must check if the directory
2155 * is in the ail and use lock_nowait. If we can't lock, we need to
2156 * drop the inode lock on the directory and try again. xfs_iunlock will
2157 * potentially push the tail if we were holding up the log.
2160 xfs_lock_dir_and_entry(
2162 xfs_inode_t *ip) /* inode of entry 'name' */
2166 xfs_inode_t *ips[2];
2175 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2182 * We want to lock in increasing inum. Since we've already
2183 * acquired the lock on the directory, we may need to release
2184 * if if the inum of the entry turns out to be less.
2186 if (e_inum > dp->i_ino) {
2188 * We are already in the right order, so just
2189 * lock on the inode of the entry.
2190 * We need to use nowait if dp is in the AIL.
2193 lp = (xfs_log_item_t *)dp->i_itemp;
2194 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2195 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL)) {
2202 * Unlock dp and try again.
2203 * xfs_iunlock will try to push the tail
2204 * if the inode is in the AIL.
2207 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2209 if ((attempts % 5) == 0) {
2210 delay(1); /* Don't just spin the CPU */
2212 xfs_rm_lock_delays++;
2218 xfs_ilock(ip, XFS_ILOCK_EXCL);
2220 } else if (e_inum < dp->i_ino) {
2221 xfs_iunlock(dp, XFS_ILOCK_EXCL);
2225 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2227 /* else e_inum == dp->i_ino */
2228 /* This can happen if we're asked to lock /x/..
2229 * the entry is "..", which is also the parent directory.
2237 int xfs_small_retries;
2238 int xfs_middle_retries;
2239 int xfs_lots_retries;
2240 int xfs_lock_delays;
2244 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2248 xfs_lock_inumorder(int lock_mode, int subclass)
2250 if (lock_mode & (XFS_IOLOCK_SHARED|XFS_IOLOCK_EXCL))
2251 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_IOLOCK_SHIFT;
2252 if (lock_mode & (XFS_ILOCK_SHARED|XFS_ILOCK_EXCL))
2253 lock_mode |= (subclass + XFS_LOCK_INUMORDER) << XFS_ILOCK_SHIFT;
2259 * The following routine will lock n inodes in exclusive mode.
2260 * We assume the caller calls us with the inodes in i_ino order.
2262 * We need to detect deadlock where an inode that we lock
2263 * is in the AIL and we start waiting for another inode that is locked
2264 * by a thread in a long running transaction (such as truncate). This can
2265 * result in deadlock since the long running trans might need to wait
2266 * for the inode we just locked in order to push the tail and free space
2276 int attempts = 0, i, j, try_lock;
2279 ASSERT(ips && (inodes >= 2)); /* we need at least two */
2290 for (; i < inodes; i++) {
2293 if (i && (ips[i] == ips[i-1])) /* Already locked */
2297 * If try_lock is not set yet, make sure all locked inodes
2298 * are not in the AIL.
2299 * If any are, set try_lock to be used later.
2303 for (j = (i - 1); j >= 0 && !try_lock; j--) {
2304 lp = (xfs_log_item_t *)ips[j]->i_itemp;
2305 if (lp && (lp->li_flags & XFS_LI_IN_AIL)) {
2312 * If any of the previous locks we have locked is in the AIL,
2313 * we must TRY to get the second and subsequent locks. If
2314 * we can't get any, we must release all we have
2319 /* try_lock must be 0 if i is 0. */
2321 * try_lock means we have an inode locked
2322 * that is in the AIL.
2325 if (!xfs_ilock_nowait(ips[i], xfs_lock_inumorder(lock_mode, i))) {
2329 * Unlock all previous guys and try again.
2330 * xfs_iunlock will try to push the tail
2331 * if the inode is in the AIL.
2334 for(j = i - 1; j >= 0; j--) {
2337 * Check to see if we've already
2338 * unlocked this one.
2339 * Not the first one going back,
2340 * and the inode ptr is the same.
2342 if ((j != (i - 1)) && ips[j] ==
2346 xfs_iunlock(ips[j], lock_mode);
2349 if ((attempts % 5) == 0) {
2350 delay(1); /* Don't just spin the CPU */
2360 xfs_ilock(ips[i], xfs_lock_inumorder(lock_mode, i));
2366 if (attempts < 5) xfs_small_retries++;
2367 else if (attempts < 100) xfs_middle_retries++;
2368 else xfs_lots_retries++;
2376 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2377 int remove_which_error_return = 0;
2379 #define REMOVE_DEBUG_TRACE(x)
2380 #endif /* ! DEBUG */
2389 bhv_desc_t *dir_bdp,
2390 bhv_vname_t *dentry,
2393 bhv_vnode_t *dir_vp;
2394 char *name = VNAME(dentry);
2395 xfs_inode_t *dp, *ip;
2396 xfs_trans_t *tp = NULL;
2399 xfs_bmap_free_t free_list;
2400 xfs_fsblock_t first_block;
2408 dir_vp = BHV_TO_VNODE(dir_bdp);
2409 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2411 dp = XFS_BHVTOI(dir_bdp);
2414 if (XFS_FORCED_SHUTDOWN(mp))
2415 return XFS_ERROR(EIO);
2417 namelen = VNAMELEN(dentry);
2419 if (!xfs_get_dir_entry(dentry, &ip)) {
2420 dm_di_mode = ip->i_d.di_mode;
2424 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
2425 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE, dir_vp,
2426 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
2427 name, NULL, dm_di_mode, 0, 0);
2432 /* From this point on, return through std_return */
2436 * We need to get a reference to ip before we get our log
2437 * reservation. The reason for this is that we cannot call
2438 * xfs_iget for an inode for which we do not have a reference
2439 * once we've acquired a log reservation. This is because the
2440 * inode we are trying to get might be in xfs_inactive going
2441 * for a log reservation. Since we'll have to wait for the
2442 * inactive code to complete before returning from xfs_iget,
2443 * we need to make sure that we don't have log space reserved
2444 * when we call xfs_iget. Instead we get an unlocked reference
2445 * to the inode before getting our log reservation.
2447 error = xfs_get_dir_entry(dentry, &ip);
2449 REMOVE_DEBUG_TRACE(__LINE__);
2453 dm_di_mode = ip->i_d.di_mode;
2455 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2459 error = XFS_QM_DQATTACH(mp, dp, 0);
2460 if (!error && dp != ip)
2461 error = XFS_QM_DQATTACH(mp, ip, 0);
2463 REMOVE_DEBUG_TRACE(__LINE__);
2468 tp = xfs_trans_alloc(mp, XFS_TRANS_REMOVE);
2469 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2471 * We try to get the real space reservation first,
2472 * allowing for directory btree deletion(s) implying
2473 * possible bmap insert(s). If we can't get the space
2474 * reservation then we use 0 instead, and avoid the bmap
2475 * btree insert(s) in the directory code by, if the bmap
2476 * insert tries to happen, instead trimming the LAST
2477 * block from the directory.
2479 resblks = XFS_REMOVE_SPACE_RES(mp);
2480 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
2481 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2482 if (error == ENOSPC) {
2484 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
2485 XFS_TRANS_PERM_LOG_RES, XFS_REMOVE_LOG_COUNT);
2488 ASSERT(error != ENOSPC);
2489 REMOVE_DEBUG_TRACE(__LINE__);
2490 xfs_trans_cancel(tp, 0);
2495 error = xfs_lock_dir_and_entry(dp, ip);
2497 REMOVE_DEBUG_TRACE(__LINE__);
2498 xfs_trans_cancel(tp, cancel_flags);
2504 * At this point, we've gotten both the directory and the entry
2507 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2510 * Increment vnode ref count only in this case since
2511 * there's an extra vnode reference in the case where
2515 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
2519 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2521 XFS_BMAP_INIT(&free_list, &first_block);
2522 error = xfs_dir_removename(tp, dp, name, namelen, ip->i_ino,
2523 &first_block, &free_list, 0);
2525 ASSERT(error != ENOENT);
2526 REMOVE_DEBUG_TRACE(__LINE__);
2529 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2532 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
2534 error = xfs_droplink(tp, ip);
2536 REMOVE_DEBUG_TRACE(__LINE__);
2540 /* Determine if this is the last link while
2541 * we are in the transaction.
2543 link_zero = (ip)->i_d.di_nlink==0;
2546 * Take an extra ref on the inode so that it doesn't
2547 * go to xfs_inactive() from within the commit.
2552 * If this is a synchronous mount, make sure that the
2553 * remove transaction goes to disk before returning to
2556 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2557 xfs_trans_set_sync(tp);
2560 error = xfs_bmap_finish(&tp, &free_list, &committed);
2562 REMOVE_DEBUG_TRACE(__LINE__);
2566 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2573 * Before we drop our extra reference to the inode, purge it
2574 * from the refcache if it is there. By waiting until afterwards
2575 * to do the IRELE, we ensure that we won't go inactive in the
2576 * xfs_refcache_purge_ip routine (although that would be OK).
2578 xfs_refcache_purge_ip(ip);
2581 * If we are using filestreams, kill the stream association.
2582 * If the file is still open it may get a new one but that
2583 * will get killed on last close in xfs_close() so we don't
2584 * have to worry about that.
2586 if (link_zero && xfs_inode_is_filestream(ip))
2587 xfs_filestream_deassociate(ip);
2589 vn_trace_exit(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
2592 * Let interposed file systems know about removed links.
2594 bhv_vop_link_removed(XFS_ITOV(ip), dir_vp, link_zero);
2598 /* Fall through to std_return with error = 0 */
2600 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
2601 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
2602 dir_vp, DM_RIGHT_NULL,
2603 NULL, DM_RIGHT_NULL,
2604 name, NULL, dm_di_mode, error, 0);
2609 xfs_bmap_cancel(&free_list);
2610 cancel_flags |= XFS_TRANS_ABORT;
2611 xfs_trans_cancel(tp, cancel_flags);
2616 * In this case make sure to not release the inode until after
2617 * the current transaction is aborted. Releasing it beforehand
2618 * can cause us to go to xfs_inactive and start a recursive
2619 * transaction which can easily deadlock with the current one.
2621 xfs_bmap_cancel(&free_list);
2622 cancel_flags |= XFS_TRANS_ABORT;
2623 xfs_trans_cancel(tp, cancel_flags);
2626 * Before we drop our extra reference to the inode, purge it
2627 * from the refcache if it is there. By waiting until afterwards
2628 * to do the IRELE, we ensure that we won't go inactive in the
2629 * xfs_refcache_purge_ip routine (although that would be OK).
2631 xfs_refcache_purge_ip(ip);
2645 bhv_desc_t *target_dir_bdp,
2646 bhv_vnode_t *src_vp,
2647 bhv_vname_t *dentry,
2650 xfs_inode_t *tdp, *sip;
2653 xfs_inode_t *ips[2];
2655 xfs_bmap_free_t free_list;
2656 xfs_fsblock_t first_block;
2659 bhv_vnode_t *target_dir_vp;
2661 char *target_name = VNAME(dentry);
2664 target_dir_vp = BHV_TO_VNODE(target_dir_bdp);
2665 vn_trace_entry(target_dir_vp, __FUNCTION__, (inst_t *)__return_address);
2666 vn_trace_entry(src_vp, __FUNCTION__, (inst_t *)__return_address);
2668 target_namelen = VNAMELEN(dentry);
2669 ASSERT(!VN_ISDIR(src_vp));
2671 sip = xfs_vtoi(src_vp);
2672 tdp = XFS_BHVTOI(target_dir_bdp);
2674 if (XFS_FORCED_SHUTDOWN(mp))
2675 return XFS_ERROR(EIO);
2677 if (DM_EVENT_ENABLED(tdp, DM_EVENT_LINK)) {
2678 error = XFS_SEND_NAMESP(mp, DM_EVENT_LINK,
2679 target_dir_vp, DM_RIGHT_NULL,
2680 src_vp, DM_RIGHT_NULL,
2681 target_name, NULL, 0, 0, 0);
2686 /* Return through std_return after this point. */
2688 error = XFS_QM_DQATTACH(mp, sip, 0);
2689 if (!error && sip != tdp)
2690 error = XFS_QM_DQATTACH(mp, tdp, 0);
2694 tp = xfs_trans_alloc(mp, XFS_TRANS_LINK);
2695 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2696 resblks = XFS_LINK_SPACE_RES(mp, target_namelen);
2697 error = xfs_trans_reserve(tp, resblks, XFS_LINK_LOG_RES(mp), 0,
2698 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2699 if (error == ENOSPC) {
2701 error = xfs_trans_reserve(tp, 0, XFS_LINK_LOG_RES(mp), 0,
2702 XFS_TRANS_PERM_LOG_RES, XFS_LINK_LOG_COUNT);
2709 if (sip->i_ino < tdp->i_ino) {
2717 xfs_lock_inodes(ips, 2, 0, XFS_ILOCK_EXCL);
2720 * Increment vnode ref counts since xfs_trans_commit &
2721 * xfs_trans_cancel will both unlock the inodes and
2722 * decrement the associated ref counts.
2725 VN_HOLD(target_dir_vp);
2726 xfs_trans_ijoin(tp, sip, XFS_ILOCK_EXCL);
2727 xfs_trans_ijoin(tp, tdp, XFS_ILOCK_EXCL);
2730 * If the source has too many links, we can't make any more to it.
2732 if (sip->i_d.di_nlink >= XFS_MAXLINK) {
2733 error = XFS_ERROR(EMLINK);
2738 * If we are using project inheritance, we only allow hard link
2739 * creation in our tree when the project IDs are the same; else
2740 * the tree quota mechanism could be circumvented.
2742 if (unlikely((tdp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT) &&
2743 (tdp->i_d.di_projid != sip->i_d.di_projid))) {
2744 error = XFS_ERROR(EXDEV);
2749 (error = xfs_dir_canenter(tp, tdp, target_name, target_namelen)))
2752 XFS_BMAP_INIT(&free_list, &first_block);
2754 error = xfs_dir_createname(tp, tdp, target_name, target_namelen,
2755 sip->i_ino, &first_block, &free_list,
2759 xfs_ichgtime(tdp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2761 xfs_trans_log_inode(tp, tdp, XFS_ILOG_CORE);
2763 error = xfs_bumplink(tp, sip);
2768 * If this is a synchronous mount, make sure that the
2769 * link transaction goes to disk before returning to
2772 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2773 xfs_trans_set_sync(tp);
2776 error = xfs_bmap_finish (&tp, &free_list, &committed);
2778 xfs_bmap_cancel(&free_list);
2782 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2786 /* Fall through to std_return with error = 0. */
2788 if (DM_EVENT_ENABLED(sip, DM_EVENT_POSTLINK)) {
2789 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTLINK,
2790 target_dir_vp, DM_RIGHT_NULL,
2791 src_vp, DM_RIGHT_NULL,
2792 target_name, NULL, 0, error, 0);
2797 cancel_flags |= XFS_TRANS_ABORT;
2801 xfs_trans_cancel(tp, cancel_flags);
2812 bhv_desc_t *dir_bdp,
2813 bhv_vname_t *dentry,
2818 char *dir_name = VNAME(dentry);
2820 xfs_inode_t *cdp; /* inode of created dir */
2821 bhv_vnode_t *cvp; /* vnode of created dir */
2827 xfs_bmap_free_t free_list;
2828 xfs_fsblock_t first_block;
2829 bhv_vnode_t *dir_vp;
2830 boolean_t dp_joined_to_trans;
2831 boolean_t created = B_FALSE;
2832 int dm_event_sent = 0;
2834 struct xfs_dquot *udqp, *gdqp;
2839 dir_vp = BHV_TO_VNODE(dir_bdp);
2840 dp = XFS_BHVTOI(dir_bdp);
2843 if (XFS_FORCED_SHUTDOWN(mp))
2844 return XFS_ERROR(EIO);
2846 dir_namelen = VNAMELEN(dentry);
2849 dp_joined_to_trans = B_FALSE;
2850 dm_di_mode = vap->va_mode;
2852 if (DM_EVENT_ENABLED(dp, DM_EVENT_CREATE)) {
2853 error = XFS_SEND_NAMESP(mp, DM_EVENT_CREATE,
2854 dir_vp, DM_RIGHT_NULL, NULL,
2855 DM_RIGHT_NULL, dir_name, NULL,
2862 /* Return through std_return after this point. */
2864 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
2868 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
2869 prid = dp->i_d.di_projid;
2870 else if (vap->va_mask & XFS_AT_PROJID)
2871 prid = (xfs_prid_t)vap->va_projid;
2873 prid = (xfs_prid_t)dfltprid;
2876 * Make sure that we have allocated dquot(s) on disk.
2878 error = XFS_QM_DQVOPALLOC(mp, dp,
2879 current_fsuid(credp), current_fsgid(credp), prid,
2880 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
2884 tp = xfs_trans_alloc(mp, XFS_TRANS_MKDIR);
2885 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
2886 resblks = XFS_MKDIR_SPACE_RES(mp, dir_namelen);
2887 error = xfs_trans_reserve(tp, resblks, XFS_MKDIR_LOG_RES(mp), 0,
2888 XFS_TRANS_PERM_LOG_RES, XFS_MKDIR_LOG_COUNT);
2889 if (error == ENOSPC) {
2891 error = xfs_trans_reserve(tp, 0, XFS_MKDIR_LOG_RES(mp), 0,
2892 XFS_TRANS_PERM_LOG_RES,
2893 XFS_MKDIR_LOG_COUNT);
2901 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
2904 * Check for directory link count overflow.
2906 if (dp->i_d.di_nlink >= XFS_MAXLINK) {
2907 error = XFS_ERROR(EMLINK);
2912 * Reserve disk quota and the inode.
2914 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
2919 (error = xfs_dir_canenter(tp, dp, dir_name, dir_namelen)))
2922 * create the directory inode.
2924 error = xfs_dir_ialloc(&tp, dp, vap->va_mode, 2,
2925 0, credp, prid, resblks > 0,
2928 if (error == ENOSPC)
2935 * Now we add the directory inode to the transaction.
2936 * We waited until now since xfs_dir_ialloc might start
2937 * a new transaction. Had we joined the transaction
2938 * earlier, the locks might have gotten released.
2941 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
2942 dp_joined_to_trans = B_TRUE;
2944 XFS_BMAP_INIT(&free_list, &first_block);
2946 error = xfs_dir_createname(tp, dp, dir_name, dir_namelen, cdp->i_ino,
2947 &first_block, &free_list, resblks ?
2948 resblks - XFS_IALLOC_SPACE_RES(mp) : 0);
2950 ASSERT(error != ENOSPC);
2953 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
2956 * Bump the in memory version number of the parent directory
2957 * so that other processes accessing it will recognize that
2958 * the directory has changed.
2962 error = xfs_dir_init(tp, cdp, dp);
2967 error = xfs_bumplink(tp, dp);
2971 cvp = XFS_ITOV(cdp);
2979 * Attach the dquots to the new inode and modify the icount incore.
2981 XFS_QM_DQVOPCREATE(mp, tp, cdp, udqp, gdqp);
2984 * If this is a synchronous mount, make sure that the
2985 * mkdir transaction goes to disk before returning to
2988 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
2989 xfs_trans_set_sync(tp);
2992 error = xfs_bmap_finish(&tp, &free_list, &committed);
2998 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
2999 XFS_QM_DQRELE(mp, udqp);
3000 XFS_QM_DQRELE(mp, gdqp);
3005 /* Fall through to std_return with error = 0 or errno from
3006 * xfs_trans_commit. */
3009 if ((created || (error != 0 && dm_event_sent != 0)) &&
3010 DM_EVENT_ENABLED(XFS_BHVTOI(dir_bdp), DM_EVENT_POSTCREATE)) {
3011 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTCREATE,
3012 dir_vp, DM_RIGHT_NULL,
3013 created ? XFS_ITOV(cdp):NULL,
3016 dm_di_mode, error, 0);
3022 xfs_bmap_cancel(&free_list);
3024 cancel_flags |= XFS_TRANS_ABORT;
3026 xfs_trans_cancel(tp, cancel_flags);
3027 XFS_QM_DQRELE(mp, udqp);
3028 XFS_QM_DQRELE(mp, gdqp);
3030 if (!dp_joined_to_trans && (dp != NULL)) {
3031 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3044 bhv_desc_t *dir_bdp,
3045 bhv_vname_t *dentry,
3048 char *name = VNAME(dentry);
3050 xfs_inode_t *cdp; /* child directory */
3054 xfs_bmap_free_t free_list;
3055 xfs_fsblock_t first_block;
3058 bhv_vnode_t *dir_vp;
3059 int dm_di_mode = S_IFDIR;
3064 dir_vp = BHV_TO_VNODE(dir_bdp);
3065 dp = XFS_BHVTOI(dir_bdp);
3068 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3070 if (XFS_FORCED_SHUTDOWN(XFS_BHVTOI(dir_bdp)->i_mount))
3071 return XFS_ERROR(EIO);
3072 namelen = VNAMELEN(dentry);
3074 if (!xfs_get_dir_entry(dentry, &cdp)) {
3075 dm_di_mode = cdp->i_d.di_mode;
3079 if (DM_EVENT_ENABLED(dp, DM_EVENT_REMOVE)) {
3080 error = XFS_SEND_NAMESP(mp, DM_EVENT_REMOVE,
3081 dir_vp, DM_RIGHT_NULL,
3082 NULL, DM_RIGHT_NULL,
3083 name, NULL, dm_di_mode, 0, 0);
3085 return XFS_ERROR(error);
3088 /* Return through std_return after this point. */
3093 * We need to get a reference to cdp before we get our log
3094 * reservation. The reason for this is that we cannot call
3095 * xfs_iget for an inode for which we do not have a reference
3096 * once we've acquired a log reservation. This is because the
3097 * inode we are trying to get might be in xfs_inactive going
3098 * for a log reservation. Since we'll have to wait for the
3099 * inactive code to complete before returning from xfs_iget,
3100 * we need to make sure that we don't have log space reserved
3101 * when we call xfs_iget. Instead we get an unlocked reference
3102 * to the inode before getting our log reservation.
3104 error = xfs_get_dir_entry(dentry, &cdp);
3106 REMOVE_DEBUG_TRACE(__LINE__);
3110 dm_di_mode = cdp->i_d.di_mode;
3113 * Get the dquots for the inodes.
3115 error = XFS_QM_DQATTACH(mp, dp, 0);
3116 if (!error && dp != cdp)
3117 error = XFS_QM_DQATTACH(mp, cdp, 0);
3120 REMOVE_DEBUG_TRACE(__LINE__);
3124 tp = xfs_trans_alloc(mp, XFS_TRANS_RMDIR);
3125 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3127 * We try to get the real space reservation first,
3128 * allowing for directory btree deletion(s) implying
3129 * possible bmap insert(s). If we can't get the space
3130 * reservation then we use 0 instead, and avoid the bmap
3131 * btree insert(s) in the directory code by, if the bmap
3132 * insert tries to happen, instead trimming the LAST
3133 * block from the directory.
3135 resblks = XFS_REMOVE_SPACE_RES(mp);
3136 error = xfs_trans_reserve(tp, resblks, XFS_REMOVE_LOG_RES(mp), 0,
3137 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3138 if (error == ENOSPC) {
3140 error = xfs_trans_reserve(tp, 0, XFS_REMOVE_LOG_RES(mp), 0,
3141 XFS_TRANS_PERM_LOG_RES, XFS_DEFAULT_LOG_COUNT);
3144 ASSERT(error != ENOSPC);
3149 XFS_BMAP_INIT(&free_list, &first_block);
3152 * Now lock the child directory inode and the parent directory
3153 * inode in the proper order. This will take care of validating
3154 * that the directory entry for the child directory inode has
3155 * not changed while we were obtaining a log reservation.
3157 error = xfs_lock_dir_and_entry(dp, cdp);
3159 xfs_trans_cancel(tp, cancel_flags);
3164 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3167 * Only increment the parent directory vnode count if
3168 * we didn't bump it in looking up cdp. The only time
3169 * we don't bump it is when we're looking up ".".
3175 xfs_trans_ijoin(tp, cdp, XFS_ILOCK_EXCL);
3177 ASSERT(cdp->i_d.di_nlink >= 2);
3178 if (cdp->i_d.di_nlink != 2) {
3179 error = XFS_ERROR(ENOTEMPTY);
3182 if (!xfs_dir_isempty(cdp)) {
3183 error = XFS_ERROR(ENOTEMPTY);
3187 error = xfs_dir_removename(tp, dp, name, namelen, cdp->i_ino,
3188 &first_block, &free_list, resblks);
3192 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3195 * Bump the in memory generation count on the parent
3196 * directory so that other can know that it has changed.
3201 * Drop the link from cdp's "..".
3203 error = xfs_droplink(tp, dp);
3209 * Drop the link from dp to cdp.
3211 error = xfs_droplink(tp, cdp);
3217 * Drop the "." link from cdp to self.
3219 error = xfs_droplink(tp, cdp);
3224 /* Determine these before committing transaction */
3225 last_cdp_link = (cdp)->i_d.di_nlink==0;
3228 * Take an extra ref on the child vnode so that it
3229 * does not go to xfs_inactive() from within the commit.
3234 * If this is a synchronous mount, make sure that the
3235 * rmdir transaction goes to disk before returning to
3238 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3239 xfs_trans_set_sync(tp);
3242 error = xfs_bmap_finish (&tp, &free_list, &committed);
3244 xfs_bmap_cancel(&free_list);
3245 xfs_trans_cancel(tp, (XFS_TRANS_RELEASE_LOG_RES |
3251 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3259 * Let interposed file systems know about removed links.
3261 bhv_vop_link_removed(XFS_ITOV(cdp), dir_vp, last_cdp_link);
3265 /* Fall through to std_return with error = 0 or the errno
3266 * from xfs_trans_commit. */
3268 if (DM_EVENT_ENABLED(dp, DM_EVENT_POSTREMOVE)) {
3269 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTREMOVE,
3270 dir_vp, DM_RIGHT_NULL,
3271 NULL, DM_RIGHT_NULL,
3272 name, NULL, dm_di_mode,
3278 xfs_bmap_cancel(&free_list);
3279 cancel_flags |= XFS_TRANS_ABORT;
3283 xfs_trans_cancel(tp, cancel_flags);
3289 bhv_desc_t *dir_bdp,
3290 bhv_vname_t *dentry,
3302 xfs_bmap_free_t free_list;
3303 xfs_fsblock_t first_block;
3304 boolean_t dp_joined_to_trans;
3305 bhv_vnode_t *dir_vp;
3308 xfs_fileoff_t first_fsb;
3309 xfs_filblks_t fs_blocks;
3311 xfs_bmbt_irec_t mval[SYMLINK_MAPS];
3318 struct xfs_dquot *udqp, *gdqp;
3320 char *link_name = VNAME(dentry);
3324 dir_vp = BHV_TO_VNODE(dir_bdp);
3325 dp = XFS_BHVTOI(dir_bdp);
3326 dp_joined_to_trans = B_FALSE;
3331 vn_trace_entry(dir_vp, __FUNCTION__, (inst_t *)__return_address);
3335 if (XFS_FORCED_SHUTDOWN(mp))
3336 return XFS_ERROR(EIO);
3338 link_namelen = VNAMELEN(dentry);
3341 * Check component lengths of the target path name.
3343 pathlen = strlen(target_path);
3344 if (pathlen >= MAXPATHLEN) /* total string too long */
3345 return XFS_ERROR(ENAMETOOLONG);
3346 if (pathlen >= MAXNAMELEN) { /* is any component too long? */
3350 for (total = 0, path = target_path; total < pathlen;) {
3354 while(*path == '/') {
3360 * Count up to the next slash or end of path.
3361 * Error out if the component is bigger than MAXNAMELEN.
3363 for(len = 0; *path != '/' && total < pathlen;total++, path++) {
3364 if (++len >= MAXNAMELEN) {
3365 error = ENAMETOOLONG;
3372 if (DM_EVENT_ENABLED(dp, DM_EVENT_SYMLINK)) {
3373 error = XFS_SEND_NAMESP(mp, DM_EVENT_SYMLINK, dir_vp,
3374 DM_RIGHT_NULL, NULL, DM_RIGHT_NULL,
3375 link_name, target_path, 0, 0, 0);
3380 /* Return through std_return after this point. */
3383 if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
3384 prid = dp->i_d.di_projid;
3385 else if (vap->va_mask & XFS_AT_PROJID)
3386 prid = (xfs_prid_t)vap->va_projid;
3388 prid = (xfs_prid_t)dfltprid;
3391 * Make sure that we have allocated dquot(s) on disk.
3393 error = XFS_QM_DQVOPALLOC(mp, dp,
3394 current_fsuid(credp), current_fsgid(credp), prid,
3395 XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
3399 tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
3400 cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
3402 * The symlink will fit into the inode data fork?
3403 * There can't be any attributes so we get the whole variable part.
3405 if (pathlen <= XFS_LITINO(mp))
3408 fs_blocks = XFS_B_TO_FSB(mp, pathlen);
3409 resblks = XFS_SYMLINK_SPACE_RES(mp, link_namelen, fs_blocks);
3410 error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
3411 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3412 if (error == ENOSPC && fs_blocks == 0) {
3414 error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
3415 XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
3423 xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
3426 * Check whether the directory allows new symlinks or not.
3428 if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
3429 error = XFS_ERROR(EPERM);
3434 * Reserve disk quota : blocks and inode.
3436 error = XFS_TRANS_RESERVE_QUOTA(mp, tp, udqp, gdqp, resblks, 1, 0);
3441 * Check for ability to enter directory entry, if no space reserved.
3444 (error = xfs_dir_canenter(tp, dp, link_name, link_namelen)))
3447 * Initialize the bmap freelist prior to calling either
3448 * bmapi or the directory create code.
3450 XFS_BMAP_INIT(&free_list, &first_block);
3453 * Allocate an inode for the symlink.
3455 error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (vap->va_mode&~S_IFMT),
3456 1, 0, credp, prid, resblks > 0, &ip, NULL);
3458 if (error == ENOSPC)
3465 xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
3466 dp_joined_to_trans = B_TRUE;
3469 * Also attach the dquot(s) to it, if applicable.
3471 XFS_QM_DQVOPCREATE(mp, tp, ip, udqp, gdqp);
3474 resblks -= XFS_IALLOC_SPACE_RES(mp);
3476 * If the symlink will fit into the inode, write it inline.
3478 if (pathlen <= XFS_IFORK_DSIZE(ip)) {
3479 xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
3480 memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
3481 ip->i_d.di_size = pathlen;
3484 * The inode was initially created in extent format.
3486 ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
3487 ip->i_df.if_flags |= XFS_IFINLINE;
3489 ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
3490 xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
3494 nmaps = SYMLINK_MAPS;
3496 error = xfs_bmapi(tp, ip, first_fsb, fs_blocks,
3497 XFS_BMAPI_WRITE | XFS_BMAPI_METADATA,
3498 &first_block, resblks, mval, &nmaps,
3505 resblks -= fs_blocks;
3506 ip->i_d.di_size = pathlen;
3507 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3509 cur_chunk = target_path;
3510 for (n = 0; n < nmaps; n++) {
3511 d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
3512 byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
3513 bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
3514 BTOBB(byte_cnt), 0);
3515 ASSERT(bp && !XFS_BUF_GETERROR(bp));
3516 if (pathlen < byte_cnt) {
3519 pathlen -= byte_cnt;
3521 memcpy(XFS_BUF_PTR(bp), cur_chunk, byte_cnt);
3522 cur_chunk += byte_cnt;
3524 xfs_trans_log_buf(tp, bp, 0, byte_cnt - 1);
3529 * Create the directory entry for the symlink.
3531 error = xfs_dir_createname(tp, dp, link_name, link_namelen, ip->i_ino,
3532 &first_block, &free_list, resblks);
3535 xfs_ichgtime(dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
3536 xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
3539 * Bump the in memory version number of the parent directory
3540 * so that other processes accessing it will recognize that
3541 * the directory has changed.
3546 * If this is a synchronous mount, make sure that the
3547 * symlink transaction goes to disk before returning to
3550 if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
3551 xfs_trans_set_sync(tp);
3555 * xfs_trans_commit normally decrements the vnode ref count
3556 * when it unlocks the inode. Since we want to return the
3557 * vnode to the caller, we bump the vnode ref count now.
3561 error = xfs_bmap_finish(&tp, &free_list, &committed);
3565 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3566 XFS_QM_DQRELE(mp, udqp);
3567 XFS_QM_DQRELE(mp, gdqp);
3569 /* Fall through to std_return with error = 0 or errno from
3570 * xfs_trans_commit */
3572 if (DM_EVENT_ENABLED(XFS_BHVTOI(dir_bdp), DM_EVENT_POSTSYMLINK)) {
3573 (void) XFS_SEND_NAMESP(mp, DM_EVENT_POSTSYMLINK,
3574 dir_vp, DM_RIGHT_NULL,
3575 error ? NULL : XFS_ITOV(ip),
3576 DM_RIGHT_NULL, link_name, target_path,
3592 xfs_bmap_cancel(&free_list);
3593 cancel_flags |= XFS_TRANS_ABORT;
3595 xfs_trans_cancel(tp, cancel_flags);
3596 XFS_QM_DQRELE(mp, udqp);
3597 XFS_QM_DQRELE(mp, gdqp);
3599 if (!dp_joined_to_trans && (dp != NULL)) {
3600 xfs_iunlock(dp, XFS_ILOCK_EXCL);
3610 * A fid routine that takes a pointer to a previously allocated
3611 * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
3621 vn_trace_entry(BHV_TO_VNODE(bdp), __FUNCTION__,
3622 (inst_t *)__return_address);
3623 ASSERT(sizeof(fid_t) >= sizeof(xfs_fid2_t));
3625 xfid = (xfs_fid2_t *)fidp;
3626 ip = XFS_BHVTOI(bdp);
3627 xfid->fid_len = sizeof(xfs_fid2_t) - sizeof(xfid->fid_len);
3630 * use memcpy because the inode is a long long and there's no
3631 * assurance that xfid->fid_ino is properly aligned.
3633 memcpy(&xfid->fid_ino, &ip->i_ino, sizeof(xfid->fid_ino));
3634 xfid->fid_gen = ip->i_d.di_gen;
3646 bhv_vrwlock_t locktype)
3651 vp = BHV_TO_VNODE(bdp);
3654 ip = XFS_BHVTOI(bdp);
3655 if (locktype == VRWLOCK_WRITE) {
3656 xfs_ilock(ip, XFS_IOLOCK_EXCL);
3657 } else if (locktype == VRWLOCK_TRY_READ) {
3658 return xfs_ilock_nowait(ip, XFS_IOLOCK_SHARED);
3659 } else if (locktype == VRWLOCK_TRY_WRITE) {
3660 return xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL);
3662 ASSERT((locktype == VRWLOCK_READ) ||
3663 (locktype == VRWLOCK_WRITE_DIRECT));
3664 xfs_ilock(ip, XFS_IOLOCK_SHARED);
3677 bhv_vrwlock_t locktype)
3682 vp = BHV_TO_VNODE(bdp);
3685 ip = XFS_BHVTOI(bdp);
3686 if (locktype == VRWLOCK_WRITE) {
3688 * In the write case, we may have added a new entry to
3689 * the reference cache. This might store a pointer to
3690 * an inode to be released in this inode. If it is there,
3691 * clear the pointer and release the inode after unlocking
3694 xfs_refcache_iunlock(ip, XFS_IOLOCK_EXCL);
3696 ASSERT((locktype == VRWLOCK_READ) ||
3697 (locktype == VRWLOCK_WRITE_DIRECT));
3698 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
3710 xfs_inode_log_item_t *iip;
3713 ip = XFS_BHVTOI(bdp);
3717 if (XFS_FORCED_SHUTDOWN(mp))
3718 return XFS_ERROR(EIO);
3721 * Bypass inodes which have already been cleaned by
3722 * the inode flush clustering code inside xfs_iflush
3724 if ((ip->i_update_core == 0) &&
3725 ((iip == NULL) || !(iip->ili_format.ilf_fields & XFS_ILOG_ALL)))
3728 if (flags & FLUSH_LOG) {
3729 if (iip && iip->ili_last_lsn) {
3730 xlog_t *log = mp->m_log;
3732 int s, log_flags = XFS_LOG_FORCE;
3734 s = GRANT_LOCK(log);
3735 sync_lsn = log->l_last_sync_lsn;
3736 GRANT_UNLOCK(log, s);
3738 if ((XFS_LSN_CMP(iip->ili_last_lsn, sync_lsn) > 0)) {
3739 if (flags & FLUSH_SYNC)
3740 log_flags |= XFS_LOG_SYNC;
3741 error = xfs_log_force(mp, iip->ili_last_lsn, log_flags);
3746 if (ip->i_update_core == 0)
3752 * We make this non-blocking if the inode is contended,
3753 * return EAGAIN to indicate to the caller that they
3754 * did not succeed. This prevents the flush path from
3755 * blocking on inodes inside another operation right
3756 * now, they get caught later by xfs_sync.
3758 if (flags & FLUSH_INODE) {
3761 if (flags & FLUSH_SYNC) {
3762 xfs_ilock(ip, XFS_ILOCK_SHARED);
3764 } else if (xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
3765 if (xfs_ipincount(ip) || !xfs_iflock_nowait(ip)) {
3766 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3773 if (flags & FLUSH_SYNC)
3774 flush_flags = XFS_IFLUSH_SYNC;
3776 flush_flags = XFS_IFLUSH_ASYNC;
3778 error = xfs_iflush(ip, flush_flags);
3779 xfs_iunlock(ip, XFS_ILOCK_SHARED);
3797 if (!capable(CAP_SYS_ADMIN))
3798 return XFS_ERROR(EPERM);
3800 ip = XFS_BHVTOI(bdp);
3803 if (XFS_FORCED_SHUTDOWN(mp))
3804 return XFS_ERROR(EIO);
3806 tp = xfs_trans_alloc(mp, XFS_TRANS_SET_DMATTRS);
3807 error = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES (mp), 0, 0, 0);
3809 xfs_trans_cancel(tp, 0);
3812 xfs_ilock(ip, XFS_ILOCK_EXCL);
3813 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3815 ip->i_iocore.io_dmevmask = ip->i_d.di_dmevmask = evmask;
3816 ip->i_iocore.io_dmstate = ip->i_d.di_dmstate = state;
3818 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3820 error = xfs_trans_commit(tp, 0);
3832 vp = BHV_TO_VNODE(bdp);
3833 ip = XFS_BHVTOI(bdp);
3835 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
3837 ASSERT(!VN_MAPPED(vp));
3839 /* bad inode, get out here ASAP */
3847 ASSERT(XFS_FORCED_SHUTDOWN(ip->i_mount) || ip->i_delayed_blks == 0);
3850 * Make sure the atime in the XFS inode is correct before freeing the
3853 xfs_synchronize_atime(ip);
3856 * If we have nothing to flush with this inode then complete the
3857 * teardown now, otherwise break the link between the xfs inode and the
3858 * linux inode and clean up the xfs inode later. This avoids flushing
3859 * the inode to disk during the delete operation itself.
3861 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3862 * first to ensure that xfs_iunpin() will never see an xfs inode
3863 * that has a linux inode being reclaimed. Synchronisation is provided
3864 * by the i_flags_lock.
3866 if (!ip->i_update_core && (ip->i_itemp == NULL)) {
3867 xfs_ilock(ip, XFS_ILOCK_EXCL);
3869 return xfs_finish_reclaim(ip, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC);
3871 xfs_mount_t *mp = ip->i_mount;
3873 /* Protect sync and unpin from us */
3874 XFS_MOUNT_ILOCK(mp);
3875 spin_lock(&ip->i_flags_lock);
3876 __xfs_iflags_set(ip, XFS_IRECLAIMABLE);
3877 vn_bhv_remove(VN_BHV_HEAD(vp), XFS_ITOBHV(ip));
3878 spin_unlock(&ip->i_flags_lock);
3879 list_add_tail(&ip->i_reclaim, &mp->m_del_inodes);
3880 XFS_MOUNT_IUNLOCK(mp);
3891 xfs_ihash_t *ih = ip->i_hash;
3892 bhv_vnode_t *vp = XFS_ITOV_NULL(ip);
3895 if (vp && VN_BAD(vp))
3898 /* The hash lock here protects a thread in xfs_iget_core from
3899 * racing with us on linking the inode back with a vnode.
3900 * Once we have the XFS_IRECLAIM flag set it will not touch
3903 write_lock(&ih->ih_lock);
3904 spin_lock(&ip->i_flags_lock);
3905 if (__xfs_iflags_test(ip, XFS_IRECLAIM) ||
3906 (!__xfs_iflags_test(ip, XFS_IRECLAIMABLE) && vp == NULL)) {
3907 spin_unlock(&ip->i_flags_lock);
3908 write_unlock(&ih->ih_lock);
3911 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3915 __xfs_iflags_set(ip, XFS_IRECLAIM);
3916 spin_unlock(&ip->i_flags_lock);
3917 write_unlock(&ih->ih_lock);
3920 * If the inode is still dirty, then flush it out. If the inode
3921 * is not in the AIL, then it will be OK to flush it delwri as
3922 * long as xfs_iflush() does not keep any references to the inode.
3923 * We leave that decision up to xfs_iflush() since it has the
3924 * knowledge of whether it's OK to simply do a delwri flush of
3925 * the inode or whether we need to wait until the inode is
3926 * pulled from the AIL.
3927 * We get the flush lock regardless, though, just to make sure
3928 * we don't free it while it is being flushed.
3930 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
3932 xfs_ilock(ip, XFS_ILOCK_EXCL);
3936 if (ip->i_update_core ||
3937 ((ip->i_itemp != NULL) &&
3938 (ip->i_itemp->ili_format.ilf_fields != 0))) {
3939 error = xfs_iflush(ip, sync_mode);
3941 * If we hit an error, typically because of filesystem
3942 * shutdown, we don't need to let vn_reclaim to know
3943 * because we're gonna reclaim the inode anyway.
3946 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3949 xfs_iflock(ip); /* synchronize with xfs_iflush_done */
3952 ASSERT(ip->i_update_core == 0);
3953 ASSERT(ip->i_itemp == NULL ||
3954 ip->i_itemp->ili_format.ilf_fields == 0);
3955 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3956 } else if (locked) {
3958 * We are not interested in doing an iflush if we're
3959 * in the process of shutting down the filesystem forcibly.
3960 * So, just reclaim the inode.
3963 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3972 xfs_finish_reclaim_all(xfs_mount_t *mp, int noblock)
3975 xfs_inode_t *ip, *n;
3980 XFS_MOUNT_ILOCK(mp);
3981 list_for_each_entry_safe(ip, n, &mp->m_del_inodes, i_reclaim) {
3983 if (xfs_ilock_nowait(ip, XFS_ILOCK_EXCL) == 0)
3985 if (xfs_ipincount(ip) ||
3986 !xfs_iflock_nowait(ip)) {
3987 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3991 XFS_MOUNT_IUNLOCK(mp);
3992 if (xfs_finish_reclaim(ip, noblock,
3993 XFS_IFLUSH_DELWRI_ELSE_ASYNC))
4002 XFS_MOUNT_IUNLOCK(mp);
4007 * xfs_alloc_file_space()
4008 * This routine allocates disk space for the given file.
4010 * If alloc_type == 0, this request is for an ALLOCSP type
4011 * request which will change the file size. In this case, no
4012 * DMAPI event will be generated by the call. A TRUNCATE event
4013 * will be generated later by xfs_setattr.
4015 * If alloc_type != 0, this request is for a RESVSP type
4016 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
4017 * lower block boundary byte address is less than the file's
4026 xfs_alloc_file_space(
4033 xfs_mount_t *mp = ip->i_mount;
4035 xfs_filblks_t allocated_fsb;
4036 xfs_filblks_t allocatesize_fsb;
4037 xfs_extlen_t extsz, temp;
4038 xfs_fileoff_t startoffset_fsb;
4039 xfs_fsblock_t firstfsb;
4045 xfs_bmbt_irec_t imaps[1], *imapp;
4046 xfs_bmap_free_t free_list;
4047 uint qblocks, resblks, resrtextents;
4051 vn_trace_entry(XFS_ITOV(ip), __FUNCTION__, (inst_t *)__return_address);
4053 if (XFS_FORCED_SHUTDOWN(mp))
4054 return XFS_ERROR(EIO);
4056 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4060 return XFS_ERROR(EINVAL);
4062 rt = XFS_IS_REALTIME_INODE(ip);
4063 extsz = xfs_get_extsz_hint(ip);
4068 bmapi_flag = XFS_BMAPI_WRITE | (alloc_type ? XFS_BMAPI_PREALLOC : 0);
4069 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
4070 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
4072 /* Generate a DMAPI event if needed. */
4073 if (alloc_type != 0 && offset < ip->i_size &&
4074 (attr_flags&ATTR_DMI) == 0 &&
4075 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
4076 xfs_off_t end_dmi_offset;
4078 end_dmi_offset = offset+len;
4079 if (end_dmi_offset > ip->i_size)
4080 end_dmi_offset = ip->i_size;
4081 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, XFS_ITOV(ip),
4082 offset, end_dmi_offset - offset,
4089 * Allocate file space until done or until there is an error
4092 while (allocatesize_fsb && !error) {
4096 * Determine space reservations for data/realtime.
4098 if (unlikely(extsz)) {
4099 s = startoffset_fsb;
4102 e = startoffset_fsb + allocatesize_fsb;
4103 if ((temp = do_mod(startoffset_fsb, extsz)))
4105 if ((temp = do_mod(e, extsz)))
4109 e = allocatesize_fsb;
4113 resrtextents = qblocks = (uint)(e - s);
4114 resrtextents /= mp->m_sb.sb_rextsize;
4115 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4116 quota_flag = XFS_QMOPT_RES_RTBLKS;
4119 resblks = qblocks = \
4120 XFS_DIOSTRAT_SPACE_RES(mp, (uint)(e - s));
4121 quota_flag = XFS_QMOPT_RES_REGBLKS;
4125 * Allocate and setup the transaction.
4127 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4128 error = xfs_trans_reserve(tp, resblks,
4129 XFS_WRITE_LOG_RES(mp), resrtextents,
4130 XFS_TRANS_PERM_LOG_RES,
4131 XFS_WRITE_LOG_COUNT);
4133 * Check for running out of space
4137 * Free the transaction structure.
4139 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4140 xfs_trans_cancel(tp, 0);
4143 xfs_ilock(ip, XFS_ILOCK_EXCL);
4144 error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, tp, ip,
4145 qblocks, 0, quota_flag);
4149 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4150 xfs_trans_ihold(tp, ip);
4153 * Issue the xfs_bmapi() call to allocate the blocks
4155 XFS_BMAP_INIT(&free_list, &firstfsb);
4156 error = XFS_BMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4157 allocatesize_fsb, bmapi_flag,
4158 &firstfsb, 0, imapp, &nimaps,
4165 * Complete the transaction
4167 error = xfs_bmap_finish(&tp, &free_list, &committed);
4172 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
4173 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4178 allocated_fsb = imapp->br_blockcount;
4181 error = XFS_ERROR(ENOSPC);
4185 startoffset_fsb += allocated_fsb;
4186 allocatesize_fsb -= allocated_fsb;
4189 if (error == ENOSPC && (attr_flags & ATTR_DMI) == 0 &&
4190 DM_EVENT_ENABLED(ip, DM_EVENT_NOSPACE)) {
4191 error = XFS_SEND_NAMESP(mp, DM_EVENT_NOSPACE,
4192 XFS_ITOV(ip), DM_RIGHT_NULL,
4193 XFS_ITOV(ip), DM_RIGHT_NULL,
4194 NULL, NULL, 0, 0, 0); /* Delay flag intentionally unused */
4196 goto retry; /* Maybe DMAPI app. has made space */
4197 /* else fall through with error from XFS_SEND_DATA */
4202 error0: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
4203 xfs_bmap_cancel(&free_list);
4204 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp, tp, ip, qblocks, 0, quota_flag);
4206 error1: /* Just cancel transaction */
4207 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4208 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4209 goto dmapi_enospc_check;
4213 * Zero file bytes between startoff and endoff inclusive.
4214 * The iolock is held exclusive and no blocks are buffered.
4217 xfs_zero_remaining_bytes(
4222 xfs_bmbt_irec_t imap;
4223 xfs_fileoff_t offset_fsb;
4224 xfs_off_t lastoffset;
4227 xfs_mount_t *mp = ip->i_mount;
4231 bp = xfs_buf_get_noaddr(mp->m_sb.sb_blocksize,
4232 ip->i_d.di_flags & XFS_DIFLAG_REALTIME ?
4233 mp->m_rtdev_targp : mp->m_ddev_targp);
4235 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
4236 offset_fsb = XFS_B_TO_FSBT(mp, offset);
4238 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, offset_fsb, 1, 0,
4239 NULL, 0, &imap, &nimap, NULL, NULL);
4240 if (error || nimap < 1)
4242 ASSERT(imap.br_blockcount >= 1);
4243 ASSERT(imap.br_startoff == offset_fsb);
4244 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
4245 if (lastoffset > endoff)
4246 lastoffset = endoff;
4247 if (imap.br_startblock == HOLESTARTBLOCK)
4249 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4250 if (imap.br_state == XFS_EXT_UNWRITTEN)
4253 XFS_BUF_UNWRITE(bp);
4255 XFS_BUF_SET_ADDR(bp, XFS_FSB_TO_DB(ip, imap.br_startblock));
4257 if ((error = xfs_iowait(bp))) {
4258 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4259 mp, bp, XFS_BUF_ADDR(bp));
4262 memset(XFS_BUF_PTR(bp) +
4263 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
4264 0, lastoffset - offset + 1);
4269 if ((error = xfs_iowait(bp))) {
4270 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4271 mp, bp, XFS_BUF_ADDR(bp));
4280 * xfs_free_file_space()
4281 * This routine frees disk space for the given file.
4283 * This routine is only called by xfs_change_file_space
4284 * for an UNRESVSP type call.
4292 xfs_free_file_space(
4301 xfs_off_t end_dmi_offset;
4302 xfs_fileoff_t endoffset_fsb;
4304 xfs_fsblock_t firstfsb;
4305 xfs_bmap_free_t free_list;
4306 xfs_bmbt_irec_t imap;
4314 xfs_fileoff_t startoffset_fsb;
4316 int need_iolock = 1;
4321 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4323 if ((error = XFS_QM_DQATTACH(mp, ip, 0)))
4327 if (len <= 0) /* if nothing being freed */
4329 rt = (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4330 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
4331 end_dmi_offset = offset + len;
4332 endoffset_fsb = XFS_B_TO_FSBT(mp, end_dmi_offset);
4334 if (offset < ip->i_size && (attr_flags & ATTR_DMI) == 0 &&
4335 DM_EVENT_ENABLED(ip, DM_EVENT_WRITE)) {
4336 if (end_dmi_offset > ip->i_size)
4337 end_dmi_offset = ip->i_size;
4338 error = XFS_SEND_DATA(mp, DM_EVENT_WRITE, vp,
4339 offset, end_dmi_offset - offset,
4340 AT_DELAY_FLAG(attr_flags), NULL);
4345 if (attr_flags & ATTR_NOLOCK)
4348 xfs_ilock(ip, XFS_IOLOCK_EXCL);
4349 vn_iowait(vp); /* wait for the completion of any pending DIOs */
4352 rounding = max_t(uint, 1 << mp->m_sb.sb_blocklog, NBPP);
4353 ioffset = offset & ~(rounding - 1);
4355 if (VN_CACHED(vp) != 0) {
4356 xfs_inval_cached_trace(&ip->i_iocore, ioffset, -1,
4357 ctooff(offtoct(ioffset)), -1);
4358 error = bhv_vop_flushinval_pages(vp, ctooff(offtoct(ioffset)),
4359 -1, FI_REMAPF_LOCKED);
4361 goto out_unlock_iolock;
4365 * Need to zero the stuff we're not freeing, on disk.
4366 * If its a realtime file & can't use unwritten extents then we
4367 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4368 * will take care of it for us.
4370 if (rt && !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4372 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, startoffset_fsb,
4373 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4375 goto out_unlock_iolock;
4376 ASSERT(nimap == 0 || nimap == 1);
4377 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4380 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4381 block = imap.br_startblock;
4382 mod = do_div(block, mp->m_sb.sb_rextsize);
4384 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
4387 error = XFS_BMAPI(mp, NULL, &ip->i_iocore, endoffset_fsb - 1,
4388 1, 0, NULL, 0, &imap, &nimap, NULL, NULL);
4390 goto out_unlock_iolock;
4391 ASSERT(nimap == 0 || nimap == 1);
4392 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
4393 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
4395 if (mod && (mod != mp->m_sb.sb_rextsize))
4396 endoffset_fsb -= mod;
4399 if ((done = (endoffset_fsb <= startoffset_fsb)))
4401 * One contiguous piece to clear
4403 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
4406 * Some full blocks, possibly two pieces to clear
4408 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
4409 error = xfs_zero_remaining_bytes(ip, offset,
4410 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
4412 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
4413 error = xfs_zero_remaining_bytes(ip,
4414 XFS_FSB_TO_B(mp, endoffset_fsb),
4419 * free file space until done or until there is an error
4421 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
4422 while (!error && !done) {
4425 * allocate and setup the transaction. Allow this
4426 * transaction to dip into the reserve blocks to ensure
4427 * the freeing of the space succeeds at ENOSPC.
4429 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
4430 tp->t_flags |= XFS_TRANS_RESERVE;
4431 error = xfs_trans_reserve(tp,
4433 XFS_WRITE_LOG_RES(mp),
4435 XFS_TRANS_PERM_LOG_RES,
4436 XFS_WRITE_LOG_COUNT);
4439 * check for running out of space
4443 * Free the transaction structure.
4445 ASSERT(error == ENOSPC || XFS_FORCED_SHUTDOWN(mp));
4446 xfs_trans_cancel(tp, 0);
4449 xfs_ilock(ip, XFS_ILOCK_EXCL);
4450 error = XFS_TRANS_RESERVE_QUOTA(mp, tp,
4451 ip->i_udquot, ip->i_gdquot, resblks, 0,
4452 XFS_QMOPT_RES_REGBLKS);
4456 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4457 xfs_trans_ihold(tp, ip);
4460 * issue the bunmapi() call to free the blocks
4462 XFS_BMAP_INIT(&free_list, &firstfsb);
4463 error = XFS_BUNMAPI(mp, tp, &ip->i_iocore, startoffset_fsb,
4464 endoffset_fsb - startoffset_fsb,
4465 0, 2, &firstfsb, &free_list, NULL, &done);
4471 * complete the transaction
4473 error = xfs_bmap_finish(&tp, &free_list, &committed);
4478 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
4479 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4484 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
4488 xfs_bmap_cancel(&free_list);
4490 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
4491 xfs_iunlock(ip, need_iolock ? (XFS_ILOCK_EXCL | XFS_IOLOCK_EXCL) :
4497 * xfs_change_file_space()
4498 * This routine allocates or frees disk space for the given file.
4499 * The user specified parameters are checked for alignment and size
4508 xfs_change_file_space(
4522 xfs_off_t startoffset;
4528 vp = BHV_TO_VNODE(bdp);
4529 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
4531 ip = XFS_BHVTOI(bdp);
4535 * must be a regular file and have write permission
4538 return XFS_ERROR(EINVAL);
4540 xfs_ilock(ip, XFS_ILOCK_SHARED);
4542 if ((error = xfs_iaccess(ip, S_IWUSR, credp))) {
4543 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4547 xfs_iunlock(ip, XFS_ILOCK_SHARED);
4549 switch (bf->l_whence) {
4550 case 0: /*SEEK_SET*/
4552 case 1: /*SEEK_CUR*/
4553 bf->l_start += offset;
4555 case 2: /*SEEK_END*/
4556 bf->l_start += ip->i_size;
4559 return XFS_ERROR(EINVAL);
4562 llen = bf->l_len > 0 ? bf->l_len - 1 : bf->l_len;
4564 if ( (bf->l_start < 0)
4565 || (bf->l_start > XFS_MAXIOFFSET(mp))
4566 || (bf->l_start + llen < 0)
4567 || (bf->l_start + llen > XFS_MAXIOFFSET(mp)))
4568 return XFS_ERROR(EINVAL);
4572 startoffset = bf->l_start;
4576 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4578 * These calls do NOT zero the data space allocated to the file,
4579 * nor do they change the file size.
4581 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4583 * These calls cause the new file data to be zeroed and the file
4584 * size to be changed.
4586 setprealloc = clrprealloc = 0;
4589 case XFS_IOC_RESVSP:
4590 case XFS_IOC_RESVSP64:
4591 error = xfs_alloc_file_space(ip, startoffset, bf->l_len,
4598 case XFS_IOC_UNRESVSP:
4599 case XFS_IOC_UNRESVSP64:
4600 if ((error = xfs_free_file_space(ip, startoffset, bf->l_len,
4605 case XFS_IOC_ALLOCSP:
4606 case XFS_IOC_ALLOCSP64:
4607 case XFS_IOC_FREESP:
4608 case XFS_IOC_FREESP64:
4609 if (startoffset > fsize) {
4610 error = xfs_alloc_file_space(ip, fsize,
4611 startoffset - fsize, 0, attr_flags);
4616 va.va_mask = XFS_AT_SIZE;
4617 va.va_size = startoffset;
4619 error = xfs_setattr(bdp, &va, attr_flags, credp);
4629 return XFS_ERROR(EINVAL);
4633 * update the inode timestamp, mode, and prealloc flag bits
4635 tp = xfs_trans_alloc(mp, XFS_TRANS_WRITEID);
4637 if ((error = xfs_trans_reserve(tp, 0, XFS_WRITEID_LOG_RES(mp),
4640 xfs_trans_cancel(tp, 0);
4644 xfs_ilock(ip, XFS_ILOCK_EXCL);
4646 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
4647 xfs_trans_ihold(tp, ip);
4649 if ((attr_flags & ATTR_DMI) == 0) {
4650 ip->i_d.di_mode &= ~S_ISUID;
4653 * Note that we don't have to worry about mandatory
4654 * file locking being disabled here because we only
4655 * clear the S_ISGID bit if the Group execute bit is
4656 * on, but if it was on then mandatory locking wouldn't
4657 * have been enabled.
4659 if (ip->i_d.di_mode & S_IXGRP)
4660 ip->i_d.di_mode &= ~S_ISGID;
4662 xfs_ichgtime(ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
4665 ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC;
4666 else if (clrprealloc)
4667 ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC;
4669 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
4670 xfs_trans_set_sync(tp);
4672 error = xfs_trans_commit(tp, 0);
4674 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4679 bhv_vnodeops_t xfs_vnodeops = {
4680 BHV_IDENTITY_INIT(VN_BHV_XFS,VNODE_POSITION_XFS),
4681 .vop_open = xfs_open,
4682 .vop_read = xfs_read,
4684 .vop_splice_read = xfs_splice_read,
4685 .vop_splice_write = xfs_splice_write,
4687 .vop_write = xfs_write,
4688 .vop_ioctl = xfs_ioctl,
4689 .vop_getattr = xfs_getattr,
4690 .vop_setattr = xfs_setattr,
4691 .vop_access = xfs_access,
4692 .vop_lookup = xfs_lookup,
4693 .vop_create = xfs_create,
4694 .vop_remove = xfs_remove,
4695 .vop_link = xfs_link,
4696 .vop_rename = xfs_rename,
4697 .vop_mkdir = xfs_mkdir,
4698 .vop_rmdir = xfs_rmdir,
4699 .vop_readdir = xfs_readdir,
4700 .vop_symlink = xfs_symlink,
4701 .vop_readlink = xfs_readlink,
4702 .vop_fsync = xfs_fsync,
4703 .vop_inactive = xfs_inactive,
4704 .vop_fid2 = xfs_fid2,
4705 .vop_rwlock = xfs_rwlock,
4706 .vop_rwunlock = xfs_rwunlock,
4707 .vop_bmap = xfs_bmap,
4708 .vop_reclaim = xfs_reclaim,
4709 .vop_attr_get = xfs_attr_get,
4710 .vop_attr_set = xfs_attr_set,
4711 .vop_attr_remove = xfs_attr_remove,
4712 .vop_attr_list = xfs_attr_list,
4713 .vop_link_removed = (vop_link_removed_t)fs_noval,
4714 .vop_vnode_change = (vop_vnode_change_t)fs_noval,
4715 .vop_tosspages = fs_tosspages,
4716 .vop_flushinval_pages = fs_flushinval_pages,
4717 .vop_flush_pages = fs_flush_pages,
4718 .vop_release = xfs_release,
4719 .vop_iflush = xfs_inode_flush,