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"
30 #include "xfs_dmapi.h"
31 #include "xfs_mount.h"
32 #include "xfs_da_btree.h"
33 #include "xfs_bmap_btree.h"
34 #include "xfs_alloc_btree.h"
35 #include "xfs_ialloc_btree.h"
36 #include "xfs_dir_sf.h"
37 #include "xfs_dir2_sf.h"
38 #include "xfs_attr_sf.h"
39 #include "xfs_dinode.h"
40 #include "xfs_inode.h"
41 #include "xfs_inode_item.h"
42 #include "xfs_dir_leaf.h"
43 #include "xfs_itable.h"
44 #include "xfs_btree.h"
45 #include "xfs_ialloc.h"
46 #include "xfs_alloc.h"
50 #include "xfs_error.h"
51 #include "xfs_quota.h"
52 #include "xfs_utils.h"
53 #include "xfs_rtalloc.h"
54 #include "xfs_refcache.h"
55 #include "xfs_trans_space.h"
56 #include "xfs_log_priv.h"
65 vnode_t
*vp
= BHV_TO_VNODE(bdp
);
66 xfs_inode_t
*ip
= XFS_BHVTOI(bdp
);
68 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
69 return XFS_ERROR(EIO
);
72 * If it's a directory with any blocks, read-ahead block 0
73 * as we're almost certain to have the next operation be a read there.
75 if (VN_ISDIR(vp
) && ip
->i_d
.di_nextents
> 0) {
76 mode
= xfs_ilock_map_shared(ip
);
77 if (ip
->i_d
.di_nextents
> 0)
78 (void)xfs_da_reada_buf(NULL
, ip
, 0, XFS_DATA_FORK
);
79 xfs_iunlock(ip
, mode
);
88 lastclose_t lastclose
,
91 vnode_t
*vp
= BHV_TO_VNODE(bdp
);
92 xfs_inode_t
*ip
= XFS_BHVTOI(bdp
);
95 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
96 return XFS_ERROR(EIO
);
98 if (lastclose
!= L_TRUE
|| !VN_ISREG(vp
))
102 * If we previously truncated this file and removed old data in
103 * the process, we want to initiate "early" writeout on the last
104 * close. This is an attempt to combat the notorious NULL files
105 * problem which is particularly noticable from a truncate down,
106 * buffered (re-)write (delalloc), followed by a crash. What we
107 * are effectively doing here is significantly reducing the time
108 * window where we'd otherwise be exposed to that problem.
110 if (VUNTRUNCATE(vp
) && VN_DIRTY(vp
) && ip
->i_delayed_blks
> 0)
111 VOP_FLUSH_PAGES(vp
, 0, -1, XFS_B_ASYNC
, FI_NONE
, error
);
129 vp
= BHV_TO_VNODE(bdp
);
130 vn_trace_entry(vp
, __FUNCTION__
, (inst_t
*)__return_address
);
132 ip
= XFS_BHVTOI(bdp
);
135 if (XFS_FORCED_SHUTDOWN(mp
))
136 return XFS_ERROR(EIO
);
138 if (!(flags
& ATTR_LAZY
))
139 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
141 vap
->va_size
= ip
->i_d
.di_size
;
142 if (vap
->va_mask
== XFS_AT_SIZE
)
146 XFS_FSB_TO_BB(mp
, ip
->i_d
.di_nblocks
+ ip
->i_delayed_blks
);
147 vap
->va_nodeid
= ip
->i_ino
;
149 vap
->va_nodeid
+= mp
->m_inoadd
;
151 vap
->va_nlink
= ip
->i_d
.di_nlink
;
154 * Quick exit for non-stat callers
157 ~(XFS_AT_SIZE
|XFS_AT_FSID
|XFS_AT_NODEID
|
158 XFS_AT_NLINK
|XFS_AT_BLKSIZE
)) == 0)
162 * Copy from in-core inode.
164 vap
->va_mode
= ip
->i_d
.di_mode
;
165 vap
->va_uid
= ip
->i_d
.di_uid
;
166 vap
->va_gid
= ip
->i_d
.di_gid
;
167 vap
->va_projid
= ip
->i_d
.di_projid
;
170 * Check vnode type block/char vs. everything else.
172 switch (ip
->i_d
.di_mode
& S_IFMT
) {
175 vap
->va_rdev
= ip
->i_df
.if_u2
.if_rdev
;
176 vap
->va_blocksize
= BLKDEV_IOSIZE
;
181 if (!(ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
)) {
182 vap
->va_blocksize
= xfs_preferred_iosize(mp
);
186 * If the file blocks are being allocated from a
187 * realtime partition, then return the inode's
188 * realtime extent size or the realtime volume's
191 vap
->va_blocksize
= ip
->i_d
.di_extsize
?
192 (ip
->i_d
.di_extsize
<< mp
->m_sb
.sb_blocklog
) :
193 (mp
->m_sb
.sb_rextsize
<< mp
->m_sb
.sb_blocklog
);
198 vn_atime_to_timespec(vp
, &vap
->va_atime
);
199 vap
->va_mtime
.tv_sec
= ip
->i_d
.di_mtime
.t_sec
;
200 vap
->va_mtime
.tv_nsec
= ip
->i_d
.di_mtime
.t_nsec
;
201 vap
->va_ctime
.tv_sec
= ip
->i_d
.di_ctime
.t_sec
;
202 vap
->va_ctime
.tv_nsec
= ip
->i_d
.di_ctime
.t_nsec
;
205 * Exit for stat callers. See if any of the rest of the fields
206 * to be filled in are needed.
209 (XFS_AT_XFLAGS
|XFS_AT_EXTSIZE
|XFS_AT_NEXTENTS
|XFS_AT_ANEXTENTS
|
210 XFS_AT_GENCOUNT
|XFS_AT_VCODE
)) == 0)
214 * Convert di_flags to xflags.
216 vap
->va_xflags
= xfs_ip2xflags(ip
);
219 * Exit for inode revalidate. See if any of the rest of
220 * the fields to be filled in are needed.
223 (XFS_AT_EXTSIZE
|XFS_AT_NEXTENTS
|XFS_AT_ANEXTENTS
|
224 XFS_AT_GENCOUNT
|XFS_AT_VCODE
)) == 0)
227 vap
->va_extsize
= ip
->i_d
.di_extsize
<< mp
->m_sb
.sb_blocklog
;
229 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
) ?
230 ip
->i_df
.if_bytes
/ sizeof(xfs_bmbt_rec_t
) :
234 (ip
->i_afp
->if_flags
& XFS_IFEXTENTS
) ?
235 ip
->i_afp
->if_bytes
/ sizeof(xfs_bmbt_rec_t
) :
236 ip
->i_d
.di_anextents
;
238 vap
->va_anextents
= 0;
239 vap
->va_gen
= ip
->i_d
.di_gen
;
242 if (!(flags
& ATTR_LAZY
))
243 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
269 xfs_prid_t projid
=0, iprojid
=0;
270 int mandlock_before
, mandlock_after
;
271 struct xfs_dquot
*udqp
, *gdqp
, *olddquot1
, *olddquot2
;
275 vp
= BHV_TO_VNODE(bdp
);
276 vn_trace_entry(vp
, __FUNCTION__
, (inst_t
*)__return_address
);
278 if (vp
->v_vfsp
->vfs_flag
& VFS_RDONLY
)
279 return XFS_ERROR(EROFS
);
282 * Cannot set certain attributes.
285 if (mask
& XFS_AT_NOSET
) {
286 return XFS_ERROR(EINVAL
);
289 ip
= XFS_BHVTOI(bdp
);
292 if (XFS_FORCED_SHUTDOWN(mp
))
293 return XFS_ERROR(EIO
);
296 * Timestamps do not need to be logged and hence do not
297 * need to be done within a transaction.
299 if (mask
& XFS_AT_UPDTIMES
) {
300 ASSERT((mask
& ~XFS_AT_UPDTIMES
) == 0);
301 timeflags
= ((mask
& XFS_AT_UPDATIME
) ? XFS_ICHGTIME_ACC
: 0) |
302 ((mask
& XFS_AT_UPDCTIME
) ? XFS_ICHGTIME_CHG
: 0) |
303 ((mask
& XFS_AT_UPDMTIME
) ? XFS_ICHGTIME_MOD
: 0);
304 xfs_ichgtime(ip
, timeflags
);
308 olddquot1
= olddquot2
= NULL
;
312 * If disk quotas is on, we make sure that the dquots do exist on disk,
313 * before we start any other transactions. Trying to do this later
314 * is messy. We don't care to take a readlock to look at the ids
315 * in inode here, because we can't hold it across the trans_reserve.
316 * If the IDs do change before we take the ilock, we're covered
317 * because the i_*dquot fields will get updated anyway.
319 if (XFS_IS_QUOTA_ON(mp
) &&
320 (mask
& (XFS_AT_UID
|XFS_AT_GID
|XFS_AT_PROJID
))) {
323 if ((mask
& XFS_AT_UID
) && XFS_IS_UQUOTA_ON(mp
)) {
325 qflags
|= XFS_QMOPT_UQUOTA
;
327 uid
= ip
->i_d
.di_uid
;
329 if ((mask
& XFS_AT_GID
) && XFS_IS_GQUOTA_ON(mp
)) {
331 qflags
|= XFS_QMOPT_GQUOTA
;
333 gid
= ip
->i_d
.di_gid
;
335 if ((mask
& XFS_AT_PROJID
) && XFS_IS_PQUOTA_ON(mp
)) {
336 projid
= vap
->va_projid
;
337 qflags
|= XFS_QMOPT_PQUOTA
;
339 projid
= ip
->i_d
.di_projid
;
342 * We take a reference when we initialize udqp and gdqp,
343 * so it is important that we never blindly double trip on
344 * the same variable. See xfs_create() for an example.
346 ASSERT(udqp
== NULL
);
347 ASSERT(gdqp
== NULL
);
348 code
= XFS_QM_DQVOPALLOC(mp
, ip
, uid
, gid
, projid
, qflags
,
355 * For the other attributes, we acquire the inode lock and
356 * first do an error checking pass.
359 lock_flags
= XFS_ILOCK_EXCL
;
360 if (flags
& ATTR_NOLOCK
)
362 if (!(mask
& XFS_AT_SIZE
)) {
363 if ((mask
!= (XFS_AT_CTIME
|XFS_AT_ATIME
|XFS_AT_MTIME
)) ||
364 (mp
->m_flags
& XFS_MOUNT_WSYNC
)) {
365 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SETATTR_NOT_SIZE
);
367 if ((code
= xfs_trans_reserve(tp
, 0,
368 XFS_ICHANGE_LOG_RES(mp
), 0,
375 if (DM_EVENT_ENABLED (vp
->v_vfsp
, ip
, DM_EVENT_TRUNCATE
) &&
376 !(flags
& ATTR_DMI
)) {
377 int dmflags
= AT_DELAY_FLAG(flags
) | DM_SEM_FLAG_WR
;
378 code
= XFS_SEND_DATA(mp
, DM_EVENT_TRUNCATE
, vp
,
379 vap
->va_size
, 0, dmflags
, NULL
);
386 lock_flags
|= XFS_IOLOCK_EXCL
;
389 xfs_ilock(ip
, lock_flags
);
391 /* boolean: are we the file owner? */
392 file_owner
= (current_fsuid(credp
) == ip
->i_d
.di_uid
);
395 * Change various properties of a file.
396 * Only the owner or users with CAP_FOWNER
397 * capability may do these things.
400 (XFS_AT_MODE
|XFS_AT_XFLAGS
|XFS_AT_EXTSIZE
|XFS_AT_UID
|
401 XFS_AT_GID
|XFS_AT_PROJID
)) {
403 * CAP_FOWNER overrides the following restrictions:
405 * The user ID of the calling process must be equal
406 * to the file owner ID, except in cases where the
407 * CAP_FSETID capability is applicable.
409 if (!file_owner
&& !capable(CAP_FOWNER
)) {
410 code
= XFS_ERROR(EPERM
);
415 * CAP_FSETID overrides the following restrictions:
417 * The effective user ID of the calling process shall match
418 * the file owner when setting the set-user-ID and
419 * set-group-ID bits on that file.
421 * The effective group ID or one of the supplementary group
422 * IDs of the calling process shall match the group owner of
423 * the file when setting the set-group-ID bit on that file
425 if (mask
& XFS_AT_MODE
) {
428 if ((vap
->va_mode
& S_ISUID
) && !file_owner
)
430 if ((vap
->va_mode
& S_ISGID
) &&
431 !in_group_p((gid_t
)ip
->i_d
.di_gid
))
434 /* Linux allows this, Irix doesn't. */
435 if ((vap
->va_mode
& S_ISVTX
) && !VN_ISDIR(vp
))
438 if (m
&& !capable(CAP_FSETID
))
444 * Change file ownership. Must be the owner or privileged.
445 * If the system was configured with the "restricted_chown"
446 * option, the owner is not permitted to give away the file,
447 * and can change the group id only to a group of which he
448 * or she is a member.
450 if (mask
& (XFS_AT_UID
|XFS_AT_GID
|XFS_AT_PROJID
)) {
452 * These IDs could have changed since we last looked at them.
453 * But, we're assured that if the ownership did change
454 * while we didn't have the inode locked, inode's dquot(s)
455 * would have changed also.
457 iuid
= ip
->i_d
.di_uid
;
458 iprojid
= ip
->i_d
.di_projid
;
459 igid
= ip
->i_d
.di_gid
;
460 gid
= (mask
& XFS_AT_GID
) ? vap
->va_gid
: igid
;
461 uid
= (mask
& XFS_AT_UID
) ? vap
->va_uid
: iuid
;
462 projid
= (mask
& XFS_AT_PROJID
) ? (xfs_prid_t
)vap
->va_projid
:
466 * CAP_CHOWN overrides the following restrictions:
468 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
469 * shall override the restriction that a process cannot
470 * change the user ID of a file it owns and the restriction
471 * that the group ID supplied to the chown() function
472 * shall be equal to either the group ID or one of the
473 * supplementary group IDs of the calling process.
475 if (restricted_chown
&&
476 (iuid
!= uid
|| (igid
!= gid
&&
477 !in_group_p((gid_t
)gid
))) &&
478 !capable(CAP_CHOWN
)) {
479 code
= XFS_ERROR(EPERM
);
483 * Do a quota reservation only if uid/projid/gid is actually
486 if ((XFS_IS_UQUOTA_ON(mp
) && iuid
!= uid
) ||
487 (XFS_IS_PQUOTA_ON(mp
) && iprojid
!= projid
) ||
488 (XFS_IS_GQUOTA_ON(mp
) && igid
!= gid
)) {
490 code
= XFS_QM_DQVOPCHOWNRESV(mp
, tp
, ip
, udqp
, gdqp
,
491 capable(CAP_FOWNER
) ?
492 XFS_QMOPT_FORCE_RES
: 0);
493 if (code
) /* out of quota */
499 * Truncate file. Must have write permission and not be a directory.
501 if (mask
& XFS_AT_SIZE
) {
502 /* Short circuit the truncate case for zero length files */
503 if ((vap
->va_size
== 0) &&
504 (ip
->i_d
.di_size
== 0) && (ip
->i_d
.di_nextents
== 0)) {
505 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
506 lock_flags
&= ~XFS_ILOCK_EXCL
;
507 if (mask
& XFS_AT_CTIME
)
508 xfs_ichgtime(ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
514 code
= XFS_ERROR(EISDIR
);
516 } else if (!VN_ISREG(vp
)) {
517 code
= XFS_ERROR(EINVAL
);
521 * Make sure that the dquots are attached to the inode.
523 if ((code
= XFS_QM_DQATTACH(mp
, ip
, XFS_QMOPT_ILOCKED
)))
528 * Change file access or modified times.
530 if (mask
& (XFS_AT_ATIME
|XFS_AT_MTIME
)) {
532 if ((flags
& ATTR_UTIME
) &&
533 !capable(CAP_FOWNER
)) {
534 code
= XFS_ERROR(EPERM
);
541 * Change extent size or realtime flag.
543 if (mask
& (XFS_AT_EXTSIZE
|XFS_AT_XFLAGS
)) {
545 * Can't change extent size if any extents are allocated.
547 if (ip
->i_d
.di_nextents
&& (mask
& XFS_AT_EXTSIZE
) &&
548 ((ip
->i_d
.di_extsize
<< mp
->m_sb
.sb_blocklog
) !=
550 code
= XFS_ERROR(EINVAL
); /* EFBIG? */
555 * Can't change realtime flag if any extents are allocated.
557 if ((ip
->i_d
.di_nextents
|| ip
->i_delayed_blks
) &&
558 (mask
& XFS_AT_XFLAGS
) &&
559 (ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) !=
560 (vap
->va_xflags
& XFS_XFLAG_REALTIME
)) {
561 code
= XFS_ERROR(EINVAL
); /* EFBIG? */
565 * Extent size must be a multiple of the appropriate block
566 * size, if set at all.
568 if ((mask
& XFS_AT_EXTSIZE
) && vap
->va_extsize
!= 0) {
571 if ((ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
) ||
572 ((mask
& XFS_AT_XFLAGS
) &&
573 (vap
->va_xflags
& XFS_XFLAG_REALTIME
))) {
574 size
= mp
->m_sb
.sb_rextsize
<<
575 mp
->m_sb
.sb_blocklog
;
577 size
= mp
->m_sb
.sb_blocksize
;
579 if (vap
->va_extsize
% size
) {
580 code
= XFS_ERROR(EINVAL
);
585 * If realtime flag is set then must have realtime data.
587 if ((mask
& XFS_AT_XFLAGS
) &&
588 (vap
->va_xflags
& XFS_XFLAG_REALTIME
)) {
589 if ((mp
->m_sb
.sb_rblocks
== 0) ||
590 (mp
->m_sb
.sb_rextsize
== 0) ||
591 (ip
->i_d
.di_extsize
% mp
->m_sb
.sb_rextsize
)) {
592 code
= XFS_ERROR(EINVAL
);
598 * Can't modify an immutable/append-only file unless
599 * we have appropriate permission.
601 if ((mask
& XFS_AT_XFLAGS
) &&
603 (XFS_DIFLAG_IMMUTABLE
|XFS_DIFLAG_APPEND
) ||
605 (XFS_XFLAG_IMMUTABLE
| XFS_XFLAG_APPEND
))) &&
606 !capable(CAP_LINUX_IMMUTABLE
)) {
607 code
= XFS_ERROR(EPERM
);
613 * Now we can make the changes. Before we join the inode
614 * to the transaction, if XFS_AT_SIZE is set then take care of
615 * the part of the truncation that must be done without the
616 * inode lock. This needs to be done before joining the inode
617 * to the transaction, because the inode cannot be unlocked
618 * once it is a part of the transaction.
620 if (mask
& XFS_AT_SIZE
) {
622 if ((vap
->va_size
> ip
->i_d
.di_size
) &&
623 (flags
& ATTR_NOSIZETOK
) == 0) {
624 code
= xfs_igrow_start(ip
, vap
->va_size
, credp
);
626 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
627 vn_iowait(vp
); /* wait for the completion of any pending DIOs */
629 code
= xfs_itruncate_data(ip
, vap
->va_size
);
632 lock_flags
&= ~XFS_ILOCK_EXCL
;
633 ASSERT(lock_flags
== XFS_IOLOCK_EXCL
);
636 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SETATTR_SIZE
);
637 if ((code
= xfs_trans_reserve(tp
, 0,
638 XFS_ITRUNCATE_LOG_RES(mp
), 0,
639 XFS_TRANS_PERM_LOG_RES
,
640 XFS_ITRUNCATE_LOG_COUNT
))) {
641 xfs_trans_cancel(tp
, 0);
643 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
646 commit_flags
= XFS_TRANS_RELEASE_LOG_RES
;
647 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
651 xfs_trans_ijoin(tp
, ip
, lock_flags
);
652 xfs_trans_ihold(tp
, ip
);
655 /* determine whether mandatory locking mode changes */
656 mandlock_before
= MANDLOCK(vp
, ip
->i_d
.di_mode
);
659 * Truncate file. Must have write permission and not be a directory.
661 if (mask
& XFS_AT_SIZE
) {
662 if (vap
->va_size
> ip
->i_d
.di_size
) {
663 xfs_igrow_finish(tp
, ip
, vap
->va_size
,
664 !(flags
& ATTR_DMI
));
665 } else if ((vap
->va_size
<= ip
->i_d
.di_size
) ||
666 ((vap
->va_size
== 0) && ip
->i_d
.di_nextents
)) {
668 * signal a sync transaction unless
669 * we're truncating an already unlinked
670 * file on a wsync filesystem
672 code
= xfs_itruncate_finish(&tp
, ip
,
673 (xfs_fsize_t
)vap
->va_size
,
675 ((ip
->i_d
.di_nlink
!= 0 ||
676 !(mp
->m_flags
& XFS_MOUNT_WSYNC
))
681 * Truncated "down", so we're removing references
682 * to old data here - if we now delay flushing for
683 * a long time, we expose ourselves unduly to the
684 * notorious NULL files problem. So, we mark this
685 * vnode and flush it when the file is closed, and
686 * do not wait the usual (long) time for writeout.
691 * Have to do this even if the file's size doesn't change.
693 timeflags
|= XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
;
697 * Change file access modes.
699 if (mask
& XFS_AT_MODE
) {
700 ip
->i_d
.di_mode
&= S_IFMT
;
701 ip
->i_d
.di_mode
|= vap
->va_mode
& ~S_IFMT
;
703 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
704 timeflags
|= XFS_ICHGTIME_CHG
;
708 * Change file ownership. Must be the owner or privileged.
709 * If the system was configured with the "restricted_chown"
710 * option, the owner is not permitted to give away the file,
711 * and can change the group id only to a group of which he
712 * or she is a member.
714 if (mask
& (XFS_AT_UID
|XFS_AT_GID
|XFS_AT_PROJID
)) {
716 * CAP_FSETID overrides the following restrictions:
718 * The set-user-ID and set-group-ID bits of a file will be
719 * cleared upon successful return from chown()
721 if ((ip
->i_d
.di_mode
& (S_ISUID
|S_ISGID
)) &&
722 !capable(CAP_FSETID
)) {
723 ip
->i_d
.di_mode
&= ~(S_ISUID
|S_ISGID
);
727 * Change the ownerships and register quota modifications
728 * in the transaction.
731 if (XFS_IS_UQUOTA_ON(mp
)) {
732 ASSERT(mask
& XFS_AT_UID
);
734 olddquot1
= XFS_QM_DQVOPCHOWN(mp
, tp
, ip
,
735 &ip
->i_udquot
, udqp
);
737 ip
->i_d
.di_uid
= uid
;
740 if (XFS_IS_GQUOTA_ON(mp
)) {
741 ASSERT(!XFS_IS_PQUOTA_ON(mp
));
742 ASSERT(mask
& XFS_AT_GID
);
744 olddquot2
= XFS_QM_DQVOPCHOWN(mp
, tp
, ip
,
745 &ip
->i_gdquot
, gdqp
);
747 ip
->i_d
.di_gid
= gid
;
749 if (iprojid
!= projid
) {
750 if (XFS_IS_PQUOTA_ON(mp
)) {
751 ASSERT(!XFS_IS_GQUOTA_ON(mp
));
752 ASSERT(mask
& XFS_AT_PROJID
);
754 olddquot2
= XFS_QM_DQVOPCHOWN(mp
, tp
, ip
,
755 &ip
->i_gdquot
, gdqp
);
757 ip
->i_d
.di_projid
= projid
;
759 * We may have to rev the inode as well as
760 * the superblock version number since projids didn't
761 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
763 if (ip
->i_d
.di_version
== XFS_DINODE_VERSION_1
)
764 xfs_bump_ino_vers2(tp
, ip
);
767 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
768 timeflags
|= XFS_ICHGTIME_CHG
;
773 * Change file access or modified times.
775 if (mask
& (XFS_AT_ATIME
|XFS_AT_MTIME
)) {
776 if (mask
& XFS_AT_ATIME
) {
777 ip
->i_d
.di_atime
.t_sec
= vap
->va_atime
.tv_sec
;
778 ip
->i_d
.di_atime
.t_nsec
= vap
->va_atime
.tv_nsec
;
779 ip
->i_update_core
= 1;
780 timeflags
&= ~XFS_ICHGTIME_ACC
;
782 if (mask
& XFS_AT_MTIME
) {
783 ip
->i_d
.di_mtime
.t_sec
= vap
->va_mtime
.tv_sec
;
784 ip
->i_d
.di_mtime
.t_nsec
= vap
->va_mtime
.tv_nsec
;
785 timeflags
&= ~XFS_ICHGTIME_MOD
;
786 timeflags
|= XFS_ICHGTIME_CHG
;
788 if (tp
&& (flags
& ATTR_UTIME
))
789 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
793 * Change XFS-added attributes.
795 if (mask
& (XFS_AT_EXTSIZE
|XFS_AT_XFLAGS
)) {
796 if (mask
& XFS_AT_EXTSIZE
) {
798 * Converting bytes to fs blocks.
800 ip
->i_d
.di_extsize
= vap
->va_extsize
>>
801 mp
->m_sb
.sb_blocklog
;
803 if (mask
& XFS_AT_XFLAGS
) {
806 /* can't set PREALLOC this way, just preserve it */
807 di_flags
= (ip
->i_d
.di_flags
& XFS_DIFLAG_PREALLOC
);
808 if (vap
->va_xflags
& XFS_XFLAG_IMMUTABLE
)
809 di_flags
|= XFS_DIFLAG_IMMUTABLE
;
810 if (vap
->va_xflags
& XFS_XFLAG_APPEND
)
811 di_flags
|= XFS_DIFLAG_APPEND
;
812 if (vap
->va_xflags
& XFS_XFLAG_SYNC
)
813 di_flags
|= XFS_DIFLAG_SYNC
;
814 if (vap
->va_xflags
& XFS_XFLAG_NOATIME
)
815 di_flags
|= XFS_DIFLAG_NOATIME
;
816 if (vap
->va_xflags
& XFS_XFLAG_NODUMP
)
817 di_flags
|= XFS_DIFLAG_NODUMP
;
818 if (vap
->va_xflags
& XFS_XFLAG_PROJINHERIT
)
819 di_flags
|= XFS_DIFLAG_PROJINHERIT
;
820 if (vap
->va_xflags
& XFS_XFLAG_NODEFRAG
)
821 di_flags
|= XFS_DIFLAG_NODEFRAG
;
822 if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
) {
823 if (vap
->va_xflags
& XFS_XFLAG_RTINHERIT
)
824 di_flags
|= XFS_DIFLAG_RTINHERIT
;
825 if (vap
->va_xflags
& XFS_XFLAG_NOSYMLINKS
)
826 di_flags
|= XFS_DIFLAG_NOSYMLINKS
;
827 if (vap
->va_xflags
& XFS_XFLAG_EXTSZINHERIT
)
828 di_flags
|= XFS_DIFLAG_EXTSZINHERIT
;
829 } else if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) {
830 if (vap
->va_xflags
& XFS_XFLAG_REALTIME
) {
831 di_flags
|= XFS_DIFLAG_REALTIME
;
832 ip
->i_iocore
.io_flags
|= XFS_IOCORE_RT
;
834 ip
->i_iocore
.io_flags
&= ~XFS_IOCORE_RT
;
836 if (vap
->va_xflags
& XFS_XFLAG_EXTSIZE
)
837 di_flags
|= XFS_DIFLAG_EXTSIZE
;
839 ip
->i_d
.di_flags
= di_flags
;
841 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
842 timeflags
|= XFS_ICHGTIME_CHG
;
846 * Change file inode change time only if XFS_AT_CTIME set
847 * AND we have been called by a DMI function.
850 if ( (flags
& ATTR_DMI
) && (mask
& XFS_AT_CTIME
) ) {
851 ip
->i_d
.di_ctime
.t_sec
= vap
->va_ctime
.tv_sec
;
852 ip
->i_d
.di_ctime
.t_nsec
= vap
->va_ctime
.tv_nsec
;
853 ip
->i_update_core
= 1;
854 timeflags
&= ~XFS_ICHGTIME_CHG
;
858 * Send out timestamp changes that need to be set to the
859 * current time. Not done when called by a DMI function.
861 if (timeflags
&& !(flags
& ATTR_DMI
))
862 xfs_ichgtime(ip
, timeflags
);
864 XFS_STATS_INC(xs_ig_attrchg
);
867 * If this is a synchronous mount, make sure that the
868 * transaction goes to disk before returning to the user.
869 * This is slightly sub-optimal in that truncates require
870 * two sync transactions instead of one for wsync filesystems.
871 * One for the truncate and one for the timestamps since we
872 * don't want to change the timestamps unless we're sure the
873 * truncate worked. Truncates are less than 1% of the laddis
874 * mix so this probably isn't worth the trouble to optimize.
878 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
879 xfs_trans_set_sync(tp
);
881 code
= xfs_trans_commit(tp
, commit_flags
, NULL
);
885 * If the (regular) file's mandatory locking mode changed, then
886 * notify the vnode. We do this under the inode lock to prevent
887 * racing calls to vop_vnode_change.
889 mandlock_after
= MANDLOCK(vp
, ip
->i_d
.di_mode
);
890 if (mandlock_before
!= mandlock_after
) {
891 VOP_VNODE_CHANGE(vp
, VCHANGE_FLAGS_ENF_LOCKING
,
895 xfs_iunlock(ip
, lock_flags
);
898 * Release any dquot(s) the inode had kept before chown.
900 XFS_QM_DQRELE(mp
, olddquot1
);
901 XFS_QM_DQRELE(mp
, olddquot2
);
902 XFS_QM_DQRELE(mp
, udqp
);
903 XFS_QM_DQRELE(mp
, gdqp
);
909 if (DM_EVENT_ENABLED(vp
->v_vfsp
, ip
, DM_EVENT_ATTRIBUTE
) &&
910 !(flags
& ATTR_DMI
)) {
911 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_ATTRIBUTE
, vp
, DM_RIGHT_NULL
,
912 NULL
, DM_RIGHT_NULL
, NULL
, NULL
,
913 0, 0, AT_DELAY_FLAG(flags
));
918 commit_flags
|= XFS_TRANS_ABORT
;
921 XFS_QM_DQRELE(mp
, udqp
);
922 XFS_QM_DQRELE(mp
, gdqp
);
924 xfs_trans_cancel(tp
, commit_flags
);
926 if (lock_flags
!= 0) {
927 xfs_iunlock(ip
, lock_flags
);
935 * Null conversion from vnode mode bits to inode mode bits, as in efs.
946 vn_trace_entry(BHV_TO_VNODE(bdp
), __FUNCTION__
,
947 (inst_t
*)__return_address
);
949 ip
= XFS_BHVTOI(bdp
);
950 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
951 error
= xfs_iaccess(ip
, mode
, credp
);
952 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
958 * The maximum pathlen is 1024 bytes. Since the minimum file system
959 * blocksize is 512 bytes, we can get a max of 2 extents back from
962 #define SYMLINK_MAPS 2
983 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
989 vp
= BHV_TO_VNODE(bdp
);
990 vn_trace_entry(vp
, __FUNCTION__
, (inst_t
*)__return_address
);
992 ip
= XFS_BHVTOI(bdp
);
995 if (XFS_FORCED_SHUTDOWN(mp
))
996 return XFS_ERROR(EIO
);
998 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
1000 ASSERT((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
);
1002 offset
= uiop
->uio_offset
;
1003 count
= uiop
->uio_resid
;
1006 error
= XFS_ERROR(EINVAL
);
1015 * See if the symlink is stored inline.
1017 pathlen
= (int)ip
->i_d
.di_size
;
1019 if (ip
->i_df
.if_flags
& XFS_IFINLINE
) {
1020 error
= uio_read(ip
->i_df
.if_u1
.if_data
, pathlen
, uiop
);
1024 * Symlink not inline. Call bmap to get it in.
1026 nmaps
= SYMLINK_MAPS
;
1028 error
= xfs_bmapi(NULL
, ip
, 0, XFS_B_TO_FSB(mp
, pathlen
),
1029 0, NULL
, 0, mval
, &nmaps
, NULL
, NULL
);
1035 for (n
= 0; n
< nmaps
; n
++) {
1036 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
1037 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
1038 bp
= xfs_buf_read(mp
->m_ddev_targp
, d
,
1039 BTOBB(byte_cnt
), 0);
1040 error
= XFS_BUF_GETERROR(bp
);
1042 xfs_ioerror_alert("xfs_readlink",
1043 ip
->i_mount
, bp
, XFS_BUF_ADDR(bp
));
1047 if (pathlen
< byte_cnt
)
1049 pathlen
-= byte_cnt
;
1051 error
= uio_read(XFS_BUF_PTR(bp
), byte_cnt
, uiop
);
1058 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
1066 * This is called to sync the inode and its data out to disk.
1067 * We need to hold the I/O lock while flushing the data, and
1068 * the inode lock while flushing the inode. The inode lock CANNOT
1069 * be held while flushing the data, so acquire after we're done
1083 int log_flushed
= 0, changed
= 1;
1085 vn_trace_entry(BHV_TO_VNODE(bdp
),
1086 __FUNCTION__
, (inst_t
*)__return_address
);
1088 ip
= XFS_BHVTOI(bdp
);
1090 ASSERT(start
>= 0 && stop
>= -1);
1092 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
1093 return XFS_ERROR(EIO
);
1096 * We always need to make sure that the required inode state
1097 * is safe on disk. The vnode might be clean but because
1098 * of committed transactions that haven't hit the disk yet.
1099 * Likewise, there could be unflushed non-transactional
1100 * changes to the inode core that have to go to disk.
1102 * The following code depends on one assumption: that
1103 * any transaction that changes an inode logs the core
1104 * because it has to change some field in the inode core
1105 * (typically nextents or nblocks). That assumption
1106 * implies that any transactions against an inode will
1107 * catch any non-transactional updates. If inode-altering
1108 * transactions exist that violate this assumption, the
1109 * code breaks. Right now, it figures that if the involved
1110 * update_* field is clear and the inode is unpinned, the
1111 * inode is clean. Either it's been flushed or it's been
1112 * committed and the commit has hit the disk unpinning the inode.
1113 * (Note that xfs_inode_item_format() called at commit clears
1114 * the update_* fields.)
1116 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
1118 /* If we are flushing data then we care about update_size
1119 * being set, otherwise we care about update_core
1121 if ((flag
& FSYNC_DATA
) ?
1122 (ip
->i_update_size
== 0) :
1123 (ip
->i_update_core
== 0)) {
1125 * Timestamps/size haven't changed since last inode
1126 * flush or inode transaction commit. That means
1127 * either nothing got written or a transaction
1128 * committed which caught the updates. If the
1129 * latter happened and the transaction hasn't
1130 * hit the disk yet, the inode will be still
1131 * be pinned. If it is, force the log.
1134 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
1136 if (xfs_ipincount(ip
)) {
1137 _xfs_log_force(ip
->i_mount
, (xfs_lsn_t
)0,
1139 ((flag
& FSYNC_WAIT
)
1140 ? XFS_LOG_SYNC
: 0),
1144 * If the inode is not pinned and nothing
1145 * has changed we don't need to flush the
1153 * Kick off a transaction to log the inode
1154 * core to get the updates. Make it
1155 * sync if FSYNC_WAIT is passed in (which
1156 * is done by everybody but specfs). The
1157 * sync transaction will also force the log.
1159 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
1160 tp
= xfs_trans_alloc(ip
->i_mount
, XFS_TRANS_FSYNC_TS
);
1161 if ((error
= xfs_trans_reserve(tp
, 0,
1162 XFS_FSYNC_TS_LOG_RES(ip
->i_mount
),
1164 xfs_trans_cancel(tp
, 0);
1167 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1170 * Note - it's possible that we might have pushed
1171 * ourselves out of the way during trans_reserve
1172 * which would flush the inode. But there's no
1173 * guarantee that the inode buffer has actually
1174 * gone out yet (it's delwri). Plus the buffer
1175 * could be pinned anyway if it's part of an
1176 * inode in another recent transaction. So we
1177 * play it safe and fire off the transaction anyway.
1179 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1180 xfs_trans_ihold(tp
, ip
);
1181 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1182 if (flag
& FSYNC_WAIT
)
1183 xfs_trans_set_sync(tp
);
1184 error
= _xfs_trans_commit(tp
, 0, NULL
, &log_flushed
);
1186 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1189 if ((ip
->i_mount
->m_flags
& XFS_MOUNT_BARRIER
) && changed
) {
1191 * If the log write didn't issue an ordered tag we need
1192 * to flush the disk cache for the data device now.
1195 xfs_blkdev_issue_flush(ip
->i_mount
->m_ddev_targp
);
1198 * If this inode is on the RT dev we need to flush that
1201 if (ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
)
1202 xfs_blkdev_issue_flush(ip
->i_mount
->m_rtdev_targp
);
1209 * This is called by xfs_inactive to free any blocks beyond eof,
1210 * when the link count isn't zero.
1213 xfs_inactive_free_eofblocks(
1219 xfs_fileoff_t end_fsb
;
1220 xfs_fileoff_t last_fsb
;
1221 xfs_filblks_t map_len
;
1223 xfs_bmbt_irec_t imap
;
1226 * Figure out if there are any blocks beyond the end
1227 * of the file. If not, then there is nothing to do.
1229 end_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)ip
->i_d
.di_size
));
1230 last_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_MAXIOFFSET(mp
));
1231 map_len
= last_fsb
- end_fsb
;
1236 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
1237 error
= XFS_BMAPI(mp
, NULL
, &ip
->i_iocore
, end_fsb
, map_len
, 0,
1238 NULL
, 0, &imap
, &nimaps
, NULL
, NULL
);
1239 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
1241 if (!error
&& (nimaps
!= 0) &&
1242 (imap
.br_startblock
!= HOLESTARTBLOCK
||
1243 ip
->i_delayed_blks
)) {
1245 * Attach the dquots to the inode up front.
1247 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
1251 * There are blocks after the end of file.
1252 * Free them up now by truncating the file to
1255 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1258 * Do the xfs_itruncate_start() call before
1259 * reserving any log space because
1260 * itruncate_start will call into the buffer
1261 * cache and we can't
1262 * do that within a transaction.
1264 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
1265 xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
,
1268 error
= xfs_trans_reserve(tp
, 0,
1269 XFS_ITRUNCATE_LOG_RES(mp
),
1270 0, XFS_TRANS_PERM_LOG_RES
,
1271 XFS_ITRUNCATE_LOG_COUNT
);
1273 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1274 xfs_trans_cancel(tp
, 0);
1275 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1279 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1280 xfs_trans_ijoin(tp
, ip
,
1283 xfs_trans_ihold(tp
, ip
);
1285 error
= xfs_itruncate_finish(&tp
, ip
,
1290 * If we get an error at this point we
1291 * simply don't bother truncating the file.
1294 xfs_trans_cancel(tp
,
1295 (XFS_TRANS_RELEASE_LOG_RES
|
1298 error
= xfs_trans_commit(tp
,
1299 XFS_TRANS_RELEASE_LOG_RES
,
1302 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1308 * Free a symlink that has blocks associated with it.
1311 xfs_inactive_symlink_rmt(
1319 xfs_fsblock_t first_block
;
1320 xfs_bmap_free_t free_list
;
1323 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
1331 ASSERT(ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
));
1333 * We're freeing a symlink that has some
1334 * blocks allocated to it. Free the
1335 * blocks here. We know that we've got
1336 * either 1 or 2 extents and that we can
1337 * free them all in one bunmapi call.
1339 ASSERT(ip
->i_d
.di_nextents
> 0 && ip
->i_d
.di_nextents
<= 2);
1340 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
1341 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
1342 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1343 xfs_trans_cancel(tp
, 0);
1348 * Lock the inode, fix the size, and join it to the transaction.
1349 * Hold it so in the normal path, we still have it locked for
1350 * the second transaction. In the error paths we need it
1351 * held so the cancel won't rele it, see below.
1353 xfs_ilock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1354 size
= (int)ip
->i_d
.di_size
;
1355 ip
->i_d
.di_size
= 0;
1356 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1357 xfs_trans_ihold(tp
, ip
);
1358 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1360 * Find the block(s) so we can inval and unmap them.
1363 XFS_BMAP_INIT(&free_list
, &first_block
);
1364 nmaps
= ARRAY_SIZE(mval
);
1365 if ((error
= xfs_bmapi(tp
, ip
, 0, XFS_B_TO_FSB(mp
, size
),
1366 XFS_BMAPI_METADATA
, &first_block
, 0, mval
, &nmaps
,
1370 * Invalidate the block(s).
1372 for (i
= 0; i
< nmaps
; i
++) {
1373 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
1374 XFS_FSB_TO_DADDR(mp
, mval
[i
].br_startblock
),
1375 XFS_FSB_TO_BB(mp
, mval
[i
].br_blockcount
), 0);
1376 xfs_trans_binval(tp
, bp
);
1379 * Unmap the dead block(s) to the free_list.
1381 if ((error
= xfs_bunmapi(tp
, ip
, 0, size
, XFS_BMAPI_METADATA
, nmaps
,
1382 &first_block
, &free_list
, NULL
, &done
)))
1386 * Commit the first transaction. This logs the EFI and the inode.
1388 if ((error
= xfs_bmap_finish(&tp
, &free_list
, first_block
, &committed
)))
1391 * The transaction must have been committed, since there were
1392 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1393 * The new tp has the extent freeing and EFDs.
1397 * The first xact was committed, so add the inode to the new one.
1398 * Mark it dirty so it will be logged and moved forward in the log as
1399 * part of every commit.
1401 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1402 xfs_trans_ihold(tp
, ip
);
1403 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1405 * Get a new, empty transaction to return to our caller.
1407 ntp
= xfs_trans_dup(tp
);
1409 * Commit the transaction containing extent freeing and EFDs.
1410 * If we get an error on the commit here or on the reserve below,
1411 * we need to unlock the inode since the new transaction doesn't
1412 * have the inode attached.
1414 error
= xfs_trans_commit(tp
, 0, NULL
);
1417 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1421 * Remove the memory for extent descriptions (just bookkeeping).
1423 if (ip
->i_df
.if_bytes
)
1424 xfs_idata_realloc(ip
, -ip
->i_df
.if_bytes
, XFS_DATA_FORK
);
1425 ASSERT(ip
->i_df
.if_bytes
== 0);
1427 * Put an itruncate log reservation in the new transaction
1430 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
1431 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
1432 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1436 * Return with the inode locked but not joined to the transaction.
1442 xfs_bmap_cancel(&free_list
);
1445 * Have to come here with the inode locked and either
1446 * (held and in the transaction) or (not in the transaction).
1447 * If the inode isn't held then cancel would iput it, but
1448 * that's wrong since this is inactive and the vnode ref
1449 * count is 0 already.
1450 * Cancel won't do anything to the inode if held, but it still
1451 * needs to be locked until the cancel is done, if it was
1452 * joined to the transaction.
1454 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1455 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1462 xfs_inactive_symlink_local(
1468 ASSERT(ip
->i_d
.di_size
<= XFS_IFORK_DSIZE(ip
));
1470 * We're freeing a symlink which fit into
1471 * the inode. Just free the memory used
1472 * to hold the old symlink.
1474 error
= xfs_trans_reserve(*tpp
, 0,
1475 XFS_ITRUNCATE_LOG_RES(ip
->i_mount
),
1476 0, XFS_TRANS_PERM_LOG_RES
,
1477 XFS_ITRUNCATE_LOG_COUNT
);
1480 xfs_trans_cancel(*tpp
, 0);
1484 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1487 * Zero length symlinks _can_ exist.
1489 if (ip
->i_df
.if_bytes
> 0) {
1490 xfs_idata_realloc(ip
,
1491 -(ip
->i_df
.if_bytes
),
1493 ASSERT(ip
->i_df
.if_bytes
== 0);
1507 ASSERT(ismrlocked(&ip
->i_iolock
, MR_UPDATE
));
1510 ASSERT(ip
->i_d
.di_forkoff
!= 0);
1511 xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
1512 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1514 error
= xfs_attr_inactive(ip
);
1517 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1518 return error
; /* goto out */
1521 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1522 error
= xfs_trans_reserve(tp
, 0,
1523 XFS_IFREE_LOG_RES(mp
),
1524 0, XFS_TRANS_PERM_LOG_RES
,
1525 XFS_INACTIVE_LOG_COUNT
);
1527 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1528 xfs_trans_cancel(tp
, 0);
1530 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1534 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1535 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1536 xfs_trans_ihold(tp
, ip
);
1537 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
1539 ASSERT(ip
->i_d
.di_anextents
== 0);
1554 vp
= BHV_TO_VNODE(bdp
);
1555 ip
= XFS_BHVTOI(bdp
);
1558 if (!VN_ISREG(vp
) || (ip
->i_d
.di_mode
== 0))
1561 /* If this is a read-only mount, don't do this (would generate I/O) */
1562 if (vp
->v_vfsp
->vfs_flag
& VFS_RDONLY
)
1565 #ifdef HAVE_REFCACHE
1566 /* If we are in the NFS reference cache then don't do this now */
1571 if (ip
->i_d
.di_nlink
!= 0) {
1572 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
1573 ((ip
->i_d
.di_size
> 0) || (VN_CACHED(vp
) > 0 ||
1574 ip
->i_delayed_blks
> 0)) &&
1575 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
)) &&
1576 (!(ip
->i_d
.di_flags
&
1577 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)))) {
1578 if ((error
= xfs_inactive_free_eofblocks(mp
, ip
)))
1580 /* Update linux inode block count after free above */
1581 vn_to_inode(vp
)->i_blocks
= XFS_FSB_TO_BB(mp
,
1582 ip
->i_d
.di_nblocks
+ ip
->i_delayed_blks
);
1592 * This is called when the vnode reference count for the vnode
1593 * goes to zero. If the file has been unlinked, then it must
1594 * now be truncated. Also, we clear all of the read-ahead state
1595 * kept for the inode here since the file is now closed.
1604 xfs_bmap_free_t free_list
;
1605 xfs_fsblock_t first_block
;
1612 vp
= BHV_TO_VNODE(bdp
);
1613 vn_trace_entry(vp
, __FUNCTION__
, (inst_t
*)__return_address
);
1615 ip
= XFS_BHVTOI(bdp
);
1618 * If the inode is already free, then there can be nothing
1621 if (ip
->i_d
.di_mode
== 0 || VN_BAD(vp
)) {
1622 ASSERT(ip
->i_df
.if_real_bytes
== 0);
1623 ASSERT(ip
->i_df
.if_broot_bytes
== 0);
1624 return VN_INACTIVE_CACHE
;
1628 * Only do a truncate if it's a regular file with
1629 * some actual space in it. It's OK to look at the
1630 * inode's fields without the lock because we're the
1631 * only one with a reference to the inode.
1633 truncate
= ((ip
->i_d
.di_nlink
== 0) &&
1634 ((ip
->i_d
.di_size
!= 0) || (ip
->i_d
.di_nextents
> 0) ||
1635 (ip
->i_delayed_blks
> 0)) &&
1636 ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
));
1640 if (ip
->i_d
.di_nlink
== 0 &&
1641 DM_EVENT_ENABLED(vp
->v_vfsp
, ip
, DM_EVENT_DESTROY
)) {
1642 (void) XFS_SEND_DESTROY(mp
, vp
, DM_RIGHT_NULL
);
1647 /* If this is a read-only mount, don't do this (would generate I/O) */
1648 if (vp
->v_vfsp
->vfs_flag
& VFS_RDONLY
)
1651 if (ip
->i_d
.di_nlink
!= 0) {
1652 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
1653 ((ip
->i_d
.di_size
> 0) || (VN_CACHED(vp
) > 0 ||
1654 ip
->i_delayed_blks
> 0)) &&
1655 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
) &&
1656 (!(ip
->i_d
.di_flags
&
1657 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)) ||
1658 (ip
->i_delayed_blks
!= 0)))) {
1659 if ((error
= xfs_inactive_free_eofblocks(mp
, ip
)))
1660 return VN_INACTIVE_CACHE
;
1661 /* Update linux inode block count after free above */
1662 vn_to_inode(vp
)->i_blocks
= XFS_FSB_TO_BB(mp
,
1663 ip
->i_d
.di_nblocks
+ ip
->i_delayed_blks
);
1668 ASSERT(ip
->i_d
.di_nlink
== 0);
1670 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
1671 return VN_INACTIVE_CACHE
;
1673 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1676 * Do the xfs_itruncate_start() call before
1677 * reserving any log space because itruncate_start
1678 * will call into the buffer cache and we can't
1679 * do that within a transaction.
1681 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
1683 xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
, 0);
1685 error
= xfs_trans_reserve(tp
, 0,
1686 XFS_ITRUNCATE_LOG_RES(mp
),
1687 0, XFS_TRANS_PERM_LOG_RES
,
1688 XFS_ITRUNCATE_LOG_COUNT
);
1690 /* Don't call itruncate_cleanup */
1691 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1692 xfs_trans_cancel(tp
, 0);
1693 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1694 return VN_INACTIVE_CACHE
;
1697 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1698 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1699 xfs_trans_ihold(tp
, ip
);
1702 * normally, we have to run xfs_itruncate_finish sync.
1703 * But if filesystem is wsync and we're in the inactive
1704 * path, then we know that nlink == 0, and that the
1705 * xaction that made nlink == 0 is permanently committed
1706 * since xfs_remove runs as a synchronous transaction.
1708 error
= xfs_itruncate_finish(&tp
, ip
, 0, XFS_DATA_FORK
,
1709 (!(mp
->m_flags
& XFS_MOUNT_WSYNC
) ? 1 : 0));
1712 xfs_trans_cancel(tp
,
1713 XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1714 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1715 return VN_INACTIVE_CACHE
;
1717 } else if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
) {
1720 * If we get an error while cleaning up a
1721 * symlink we bail out.
1723 error
= (ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
)) ?
1724 xfs_inactive_symlink_rmt(ip
, &tp
) :
1725 xfs_inactive_symlink_local(ip
, &tp
);
1729 return VN_INACTIVE_CACHE
;
1732 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1733 xfs_trans_ihold(tp
, ip
);
1735 error
= xfs_trans_reserve(tp
, 0,
1736 XFS_IFREE_LOG_RES(mp
),
1737 0, XFS_TRANS_PERM_LOG_RES
,
1738 XFS_INACTIVE_LOG_COUNT
);
1740 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1741 xfs_trans_cancel(tp
, 0);
1742 return VN_INACTIVE_CACHE
;
1745 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1746 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1747 xfs_trans_ihold(tp
, ip
);
1751 * If there are attributes associated with the file
1752 * then blow them away now. The code calls a routine
1753 * that recursively deconstructs the attribute fork.
1754 * We need to just commit the current transaction
1755 * because we can't use it for xfs_attr_inactive().
1757 if (ip
->i_d
.di_anextents
> 0) {
1758 error
= xfs_inactive_attrs(ip
, &tp
);
1760 * If we got an error, the transaction is already
1761 * cancelled, and the inode is unlocked. Just get out.
1764 return VN_INACTIVE_CACHE
;
1765 } else if (ip
->i_afp
) {
1766 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
1772 XFS_BMAP_INIT(&free_list
, &first_block
);
1773 error
= xfs_ifree(tp
, ip
, &free_list
);
1776 * If we fail to free the inode, shut down. The cancel
1777 * might do that, we need to make sure. Otherwise the
1778 * inode might be lost for a long time or forever.
1780 if (!XFS_FORCED_SHUTDOWN(mp
)) {
1782 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1783 error
, mp
->m_fsname
);
1784 xfs_force_shutdown(mp
, SHUTDOWN_META_IO_ERROR
);
1786 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
1789 * Credit the quota account(s). The inode is gone.
1791 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, XFS_TRANS_DQ_ICOUNT
, -1);
1794 * Just ignore errors at this point. There is
1795 * nothing we can do except to try to keep going.
1797 (void) xfs_bmap_finish(&tp
, &free_list
, first_block
,
1799 (void) xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
1802 * Release the dquots held by inode, if any.
1804 XFS_QM_DQDETACH(mp
, ip
);
1806 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1809 return VN_INACTIVE_CACHE
;
1818 bhv_desc_t
*dir_bdp
,
1825 xfs_inode_t
*dp
, *ip
;
1831 dir_vp
= BHV_TO_VNODE(dir_bdp
);
1832 vn_trace_entry(dir_vp
, __FUNCTION__
, (inst_t
*)__return_address
);
1834 dp
= XFS_BHVTOI(dir_bdp
);
1836 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
1837 return XFS_ERROR(EIO
);
1839 lock_mode
= xfs_ilock_map_shared(dp
);
1840 error
= xfs_dir_lookup_int(dir_bdp
, lock_mode
, dentry
, &e_inum
, &ip
);
1842 *vpp
= XFS_ITOV(ip
);
1845 xfs_iunlock_map_shared(dp
, lock_mode
);
1851 * xfs_create (create a new file).
1855 bhv_desc_t
*dir_bdp
,
1861 char *name
= VNAME(dentry
);
1863 xfs_inode_t
*dp
, *ip
;
1869 xfs_bmap_free_t free_list
;
1870 xfs_fsblock_t first_block
;
1871 boolean_t dp_joined_to_trans
;
1872 int dm_event_sent
= 0;
1876 struct xfs_dquot
*udqp
, *gdqp
;
1882 dir_vp
= BHV_TO_VNODE(dir_bdp
);
1883 vn_trace_entry(dir_vp
, __FUNCTION__
, (inst_t
*)__return_address
);
1885 dp
= XFS_BHVTOI(dir_bdp
);
1888 dm_di_mode
= vap
->va_mode
;
1889 namelen
= VNAMELEN(dentry
);
1891 if (DM_EVENT_ENABLED(dir_vp
->v_vfsp
, dp
, DM_EVENT_CREATE
)) {
1892 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_CREATE
,
1893 dir_vp
, DM_RIGHT_NULL
, NULL
,
1894 DM_RIGHT_NULL
, name
, NULL
,
1902 if (XFS_FORCED_SHUTDOWN(mp
))
1903 return XFS_ERROR(EIO
);
1905 /* Return through std_return after this point. */
1908 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
1909 prid
= dp
->i_d
.di_projid
;
1910 else if (vap
->va_mask
& XFS_AT_PROJID
)
1911 prid
= (xfs_prid_t
)vap
->va_projid
;
1913 prid
= (xfs_prid_t
)dfltprid
;
1916 * Make sure that we have allocated dquot(s) on disk.
1918 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
1919 current_fsuid(credp
), current_fsgid(credp
), prid
,
1920 XFS_QMOPT_QUOTALL
|XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
1925 dp_joined_to_trans
= B_FALSE
;
1927 tp
= xfs_trans_alloc(mp
, XFS_TRANS_CREATE
);
1928 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1929 resblks
= XFS_CREATE_SPACE_RES(mp
, namelen
);
1931 * Initially assume that the file does not exist and
1932 * reserve the resources for that case. If that is not
1933 * the case we'll drop the one we have and get a more
1934 * appropriate transaction later.
1936 error
= xfs_trans_reserve(tp
, resblks
, XFS_CREATE_LOG_RES(mp
), 0,
1937 XFS_TRANS_PERM_LOG_RES
, XFS_CREATE_LOG_COUNT
);
1938 if (error
== ENOSPC
) {
1940 error
= xfs_trans_reserve(tp
, 0, XFS_CREATE_LOG_RES(mp
), 0,
1941 XFS_TRANS_PERM_LOG_RES
, XFS_CREATE_LOG_COUNT
);
1949 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
1951 XFS_BMAP_INIT(&free_list
, &first_block
);
1956 * Reserve disk quota and the inode.
1958 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
1963 (error
= XFS_DIR_CANENTER(mp
, tp
, dp
, name
, namelen
)))
1965 rdev
= (vap
->va_mask
& XFS_AT_RDEV
) ? vap
->va_rdev
: 0;
1966 error
= xfs_dir_ialloc(&tp
, dp
, vap
->va_mode
, 1,
1967 rdev
, credp
, prid
, resblks
> 0,
1970 if (error
== ENOSPC
)
1977 * At this point, we've gotten a newly allocated inode.
1978 * It is locked (and joined to the transaction).
1981 ASSERT(ismrlocked (&ip
->i_lock
, MR_UPDATE
));
1984 * Now we join the directory inode to the transaction.
1985 * We do not do it earlier because xfs_dir_ialloc
1986 * might commit the previous transaction (and release
1991 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
1992 dp_joined_to_trans
= B_TRUE
;
1994 error
= XFS_DIR_CREATENAME(mp
, tp
, dp
, name
, namelen
, ip
->i_ino
,
1995 &first_block
, &free_list
,
1996 resblks
? resblks
- XFS_IALLOC_SPACE_RES(mp
) : 0);
1998 ASSERT(error
!= ENOSPC
);
2001 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2002 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2005 * If this is a synchronous mount, make sure that the
2006 * create transaction goes to disk before returning to
2009 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2010 xfs_trans_set_sync(tp
);
2016 * Attach the dquot(s) to the inodes and modify them incore.
2017 * These ids of the inode couldn't have changed since the new
2018 * inode has been locked ever since it was created.
2020 XFS_QM_DQVOPCREATE(mp
, tp
, ip
, udqp
, gdqp
);
2023 * xfs_trans_commit normally decrements the vnode ref count
2024 * when it unlocks the inode. Since we want to return the
2025 * vnode to the caller, we bump the vnode ref count now.
2030 error
= xfs_bmap_finish(&tp
, &free_list
, first_block
, &committed
);
2032 xfs_bmap_cancel(&free_list
);
2036 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
2043 XFS_QM_DQRELE(mp
, udqp
);
2044 XFS_QM_DQRELE(mp
, gdqp
);
2047 * Propagate the fact that the vnode changed after the
2048 * xfs_inode locks have been released.
2050 VOP_VNODE_CHANGE(vp
, VCHANGE_FLAGS_TRUNCATED
, 3);
2054 /* Fallthrough to std_return with error = 0 */
2057 if ( (*vpp
|| (error
!= 0 && dm_event_sent
!= 0)) &&
2058 DM_EVENT_ENABLED(dir_vp
->v_vfsp
, XFS_BHVTOI(dir_bdp
),
2059 DM_EVENT_POSTCREATE
)) {
2060 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTCREATE
,
2061 dir_vp
, DM_RIGHT_NULL
,
2063 DM_RIGHT_NULL
, name
, NULL
,
2064 dm_di_mode
, error
, 0);
2069 cancel_flags
|= XFS_TRANS_ABORT
;
2074 xfs_trans_cancel(tp
, cancel_flags
);
2076 if (!dp_joined_to_trans
&& (dp
!= NULL
))
2077 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
2078 XFS_QM_DQRELE(mp
, udqp
);
2079 XFS_QM_DQRELE(mp
, gdqp
);
2085 * Wait until after the current transaction is aborted to
2086 * release the inode. This prevents recursive transactions
2087 * and deadlocks from xfs_inactive.
2089 cancel_flags
|= XFS_TRANS_ABORT
;
2090 xfs_trans_cancel(tp
, cancel_flags
);
2093 XFS_QM_DQRELE(mp
, udqp
);
2094 XFS_QM_DQRELE(mp
, gdqp
);
2101 * Some counters to see if (and how often) we are hitting some deadlock
2102 * prevention code paths.
2106 int xfs_rm_lock_delays
;
2107 int xfs_rm_attempts
;
2111 * The following routine will lock the inodes associated with the
2112 * directory and the named entry in the directory. The locks are
2113 * acquired in increasing inode number.
2115 * If the entry is "..", then only the directory is locked. The
2116 * vnode ref count will still include that from the .. entry in
2119 * There is a deadlock we need to worry about. If the locked directory is
2120 * in the AIL, it might be blocking up the log. The next inode we lock
2121 * could be already locked by another thread waiting for log space (e.g
2122 * a permanent log reservation with a long running transaction (see
2123 * xfs_itruncate_finish)). To solve this, we must check if the directory
2124 * is in the ail and use lock_nowait. If we can't lock, we need to
2125 * drop the inode lock on the directory and try again. xfs_iunlock will
2126 * potentially push the tail if we were holding up the log.
2129 xfs_lock_dir_and_entry(
2132 xfs_inode_t
*ip
) /* inode of entry 'name' */
2136 xfs_inode_t
*ips
[2];
2145 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
2152 * We want to lock in increasing inum. Since we've already
2153 * acquired the lock on the directory, we may need to release
2154 * if if the inum of the entry turns out to be less.
2156 if (e_inum
> dp
->i_ino
) {
2158 * We are already in the right order, so just
2159 * lock on the inode of the entry.
2160 * We need to use nowait if dp is in the AIL.
2163 lp
= (xfs_log_item_t
*)dp
->i_itemp
;
2164 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
2165 if (!xfs_ilock_nowait(ip
, XFS_ILOCK_EXCL
)) {
2172 * Unlock dp and try again.
2173 * xfs_iunlock will try to push the tail
2174 * if the inode is in the AIL.
2177 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
2179 if ((attempts
% 5) == 0) {
2180 delay(1); /* Don't just spin the CPU */
2182 xfs_rm_lock_delays
++;
2188 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2190 } else if (e_inum
< dp
->i_ino
) {
2191 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
2195 xfs_lock_inodes(ips
, 2, 0, XFS_ILOCK_EXCL
);
2197 /* else e_inum == dp->i_ino */
2198 /* This can happen if we're asked to lock /x/..
2199 * the entry is "..", which is also the parent directory.
2207 int xfs_small_retries
;
2208 int xfs_middle_retries
;
2209 int xfs_lots_retries
;
2210 int xfs_lock_delays
;
2214 * The following routine will lock n inodes in exclusive mode.
2215 * We assume the caller calls us with the inodes in i_ino order.
2217 * We need to detect deadlock where an inode that we lock
2218 * is in the AIL and we start waiting for another inode that is locked
2219 * by a thread in a long running transaction (such as truncate). This can
2220 * result in deadlock since the long running trans might need to wait
2221 * for the inode we just locked in order to push the tail and free space
2231 int attempts
= 0, i
, j
, try_lock
;
2234 ASSERT(ips
&& (inodes
>= 2)); /* we need at least two */
2245 for (; i
< inodes
; i
++) {
2248 if (i
&& (ips
[i
] == ips
[i
-1])) /* Already locked */
2252 * If try_lock is not set yet, make sure all locked inodes
2253 * are not in the AIL.
2254 * If any are, set try_lock to be used later.
2258 for (j
= (i
- 1); j
>= 0 && !try_lock
; j
--) {
2259 lp
= (xfs_log_item_t
*)ips
[j
]->i_itemp
;
2260 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
2267 * If any of the previous locks we have locked is in the AIL,
2268 * we must TRY to get the second and subsequent locks. If
2269 * we can't get any, we must release all we have
2274 /* try_lock must be 0 if i is 0. */
2276 * try_lock means we have an inode locked
2277 * that is in the AIL.
2280 if (!xfs_ilock_nowait(ips
[i
], lock_mode
)) {
2284 * Unlock all previous guys and try again.
2285 * xfs_iunlock will try to push the tail
2286 * if the inode is in the AIL.
2289 for(j
= i
- 1; j
>= 0; j
--) {
2292 * Check to see if we've already
2293 * unlocked this one.
2294 * Not the first one going back,
2295 * and the inode ptr is the same.
2297 if ((j
!= (i
- 1)) && ips
[j
] ==
2301 xfs_iunlock(ips
[j
], lock_mode
);
2304 if ((attempts
% 5) == 0) {
2305 delay(1); /* Don't just spin the CPU */
2315 xfs_ilock(ips
[i
], lock_mode
);
2321 if (attempts
< 5) xfs_small_retries
++;
2322 else if (attempts
< 100) xfs_middle_retries
++;
2323 else xfs_lots_retries
++;
2331 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2332 int remove_which_error_return
= 0;
2334 #define REMOVE_DEBUG_TRACE(x)
2335 #endif /* ! DEBUG */
2344 bhv_desc_t
*dir_bdp
,
2349 char *name
= VNAME(dentry
);
2350 xfs_inode_t
*dp
, *ip
;
2351 xfs_trans_t
*tp
= NULL
;
2354 xfs_bmap_free_t free_list
;
2355 xfs_fsblock_t first_block
;
2363 dir_vp
= BHV_TO_VNODE(dir_bdp
);
2364 vn_trace_entry(dir_vp
, __FUNCTION__
, (inst_t
*)__return_address
);
2366 dp
= XFS_BHVTOI(dir_bdp
);
2369 if (XFS_FORCED_SHUTDOWN(mp
))
2370 return XFS_ERROR(EIO
);
2372 namelen
= VNAMELEN(dentry
);
2374 if (DM_EVENT_ENABLED(dir_vp
->v_vfsp
, dp
, DM_EVENT_REMOVE
)) {
2375 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_REMOVE
, dir_vp
,
2376 DM_RIGHT_NULL
, NULL
, DM_RIGHT_NULL
,
2377 name
, NULL
, 0, 0, 0);
2382 /* From this point on, return through std_return */
2386 * We need to get a reference to ip before we get our log
2387 * reservation. The reason for this is that we cannot call
2388 * xfs_iget for an inode for which we do not have a reference
2389 * once we've acquired a log reservation. This is because the
2390 * inode we are trying to get might be in xfs_inactive going
2391 * for a log reservation. Since we'll have to wait for the
2392 * inactive code to complete before returning from xfs_iget,
2393 * we need to make sure that we don't have log space reserved
2394 * when we call xfs_iget. Instead we get an unlocked reference
2395 * to the inode before getting our log reservation.
2397 error
= xfs_get_dir_entry(dentry
, &ip
);
2399 REMOVE_DEBUG_TRACE(__LINE__
);
2403 dm_di_mode
= ip
->i_d
.di_mode
;
2405 vn_trace_entry(XFS_ITOV(ip
), __FUNCTION__
, (inst_t
*)__return_address
);
2409 error
= XFS_QM_DQATTACH(mp
, dp
, 0);
2410 if (!error
&& dp
!= ip
)
2411 error
= XFS_QM_DQATTACH(mp
, ip
, 0);
2413 REMOVE_DEBUG_TRACE(__LINE__
);
2418 tp
= xfs_trans_alloc(mp
, XFS_TRANS_REMOVE
);
2419 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2421 * We try to get the real space reservation first,
2422 * allowing for directory btree deletion(s) implying
2423 * possible bmap insert(s). If we can't get the space
2424 * reservation then we use 0 instead, and avoid the bmap
2425 * btree insert(s) in the directory code by, if the bmap
2426 * insert tries to happen, instead trimming the LAST
2427 * block from the directory.
2429 resblks
= XFS_REMOVE_SPACE_RES(mp
);
2430 error
= xfs_trans_reserve(tp
, resblks
, XFS_REMOVE_LOG_RES(mp
), 0,
2431 XFS_TRANS_PERM_LOG_RES
, XFS_REMOVE_LOG_COUNT
);
2432 if (error
== ENOSPC
) {
2434 error
= xfs_trans_reserve(tp
, 0, XFS_REMOVE_LOG_RES(mp
), 0,
2435 XFS_TRANS_PERM_LOG_RES
, XFS_REMOVE_LOG_COUNT
);
2438 ASSERT(error
!= ENOSPC
);
2439 REMOVE_DEBUG_TRACE(__LINE__
);
2440 xfs_trans_cancel(tp
, 0);
2445 error
= xfs_lock_dir_and_entry(dp
, dentry
, ip
);
2447 REMOVE_DEBUG_TRACE(__LINE__
);
2448 xfs_trans_cancel(tp
, cancel_flags
);
2454 * At this point, we've gotten both the directory and the entry
2457 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
2460 * Increment vnode ref count only in this case since
2461 * there's an extra vnode reference in the case where
2465 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
2469 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2471 XFS_BMAP_INIT(&free_list
, &first_block
);
2472 error
= XFS_DIR_REMOVENAME(mp
, tp
, dp
, name
, namelen
, ip
->i_ino
,
2473 &first_block
, &free_list
, 0);
2475 ASSERT(error
!= ENOENT
);
2476 REMOVE_DEBUG_TRACE(__LINE__
);
2479 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2482 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2484 error
= xfs_droplink(tp
, ip
);
2486 REMOVE_DEBUG_TRACE(__LINE__
);
2490 /* Determine if this is the last link while
2491 * we are in the transaction.
2493 link_zero
= (ip
)->i_d
.di_nlink
==0;
2496 * Take an extra ref on the inode so that it doesn't
2497 * go to xfs_inactive() from within the commit.
2502 * If this is a synchronous mount, make sure that the
2503 * remove transaction goes to disk before returning to
2506 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2507 xfs_trans_set_sync(tp
);
2510 error
= xfs_bmap_finish(&tp
, &free_list
, first_block
, &committed
);
2512 REMOVE_DEBUG_TRACE(__LINE__
);
2516 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
2523 * Before we drop our extra reference to the inode, purge it
2524 * from the refcache if it is there. By waiting until afterwards
2525 * to do the IRELE, we ensure that we won't go inactive in the
2526 * xfs_refcache_purge_ip routine (although that would be OK).
2528 xfs_refcache_purge_ip(ip
);
2530 vn_trace_exit(XFS_ITOV(ip
), __FUNCTION__
, (inst_t
*)__return_address
);
2533 * Let interposed file systems know about removed links.
2535 VOP_LINK_REMOVED(XFS_ITOV(ip
), dir_vp
, link_zero
);
2539 /* Fall through to std_return with error = 0 */
2541 if (DM_EVENT_ENABLED(dir_vp
->v_vfsp
, dp
,
2542 DM_EVENT_POSTREMOVE
)) {
2543 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTREMOVE
,
2544 dir_vp
, DM_RIGHT_NULL
,
2545 NULL
, DM_RIGHT_NULL
,
2546 name
, NULL
, dm_di_mode
, error
, 0);
2551 xfs_bmap_cancel(&free_list
);
2552 cancel_flags
|= XFS_TRANS_ABORT
;
2553 xfs_trans_cancel(tp
, cancel_flags
);
2558 * In this case make sure to not release the inode until after
2559 * the current transaction is aborted. Releasing it beforehand
2560 * can cause us to go to xfs_inactive and start a recursive
2561 * transaction which can easily deadlock with the current one.
2563 xfs_bmap_cancel(&free_list
);
2564 cancel_flags
|= XFS_TRANS_ABORT
;
2565 xfs_trans_cancel(tp
, cancel_flags
);
2568 * Before we drop our extra reference to the inode, purge it
2569 * from the refcache if it is there. By waiting until afterwards
2570 * to do the IRELE, we ensure that we won't go inactive in the
2571 * xfs_refcache_purge_ip routine (although that would be OK).
2573 xfs_refcache_purge_ip(ip
);
2587 bhv_desc_t
*target_dir_bdp
,
2592 xfs_inode_t
*tdp
, *sip
;
2595 xfs_inode_t
*ips
[2];
2597 xfs_bmap_free_t free_list
;
2598 xfs_fsblock_t first_block
;
2601 vnode_t
*target_dir_vp
;
2603 char *target_name
= VNAME(dentry
);
2606 target_dir_vp
= BHV_TO_VNODE(target_dir_bdp
);
2607 vn_trace_entry(target_dir_vp
, __FUNCTION__
, (inst_t
*)__return_address
);
2608 vn_trace_entry(src_vp
, __FUNCTION__
, (inst_t
*)__return_address
);
2610 target_namelen
= VNAMELEN(dentry
);
2611 if (VN_ISDIR(src_vp
))
2612 return XFS_ERROR(EPERM
);
2614 sip
= xfs_vtoi(src_vp
);
2615 tdp
= XFS_BHVTOI(target_dir_bdp
);
2617 if (XFS_FORCED_SHUTDOWN(mp
))
2618 return XFS_ERROR(EIO
);
2620 if (DM_EVENT_ENABLED(src_vp
->v_vfsp
, tdp
, DM_EVENT_LINK
)) {
2621 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_LINK
,
2622 target_dir_vp
, DM_RIGHT_NULL
,
2623 src_vp
, DM_RIGHT_NULL
,
2624 target_name
, NULL
, 0, 0, 0);
2629 /* Return through std_return after this point. */
2631 error
= XFS_QM_DQATTACH(mp
, sip
, 0);
2632 if (!error
&& sip
!= tdp
)
2633 error
= XFS_QM_DQATTACH(mp
, tdp
, 0);
2637 tp
= xfs_trans_alloc(mp
, XFS_TRANS_LINK
);
2638 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2639 resblks
= XFS_LINK_SPACE_RES(mp
, target_namelen
);
2640 error
= xfs_trans_reserve(tp
, resblks
, XFS_LINK_LOG_RES(mp
), 0,
2641 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
2642 if (error
== ENOSPC
) {
2644 error
= xfs_trans_reserve(tp
, 0, XFS_LINK_LOG_RES(mp
), 0,
2645 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
2652 if (sip
->i_ino
< tdp
->i_ino
) {
2660 xfs_lock_inodes(ips
, 2, 0, XFS_ILOCK_EXCL
);
2663 * Increment vnode ref counts since xfs_trans_commit &
2664 * xfs_trans_cancel will both unlock the inodes and
2665 * decrement the associated ref counts.
2668 VN_HOLD(target_dir_vp
);
2669 xfs_trans_ijoin(tp
, sip
, XFS_ILOCK_EXCL
);
2670 xfs_trans_ijoin(tp
, tdp
, XFS_ILOCK_EXCL
);
2673 * If the source has too many links, we can't make any more to it.
2675 if (sip
->i_d
.di_nlink
>= XFS_MAXLINK
) {
2676 error
= XFS_ERROR(EMLINK
);
2681 * If we are using project inheritance, we only allow hard link
2682 * creation in our tree when the project IDs are the same; else
2683 * the tree quota mechanism could be circumvented.
2685 if (unlikely((tdp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
) &&
2686 (tdp
->i_d
.di_projid
!= sip
->i_d
.di_projid
))) {
2687 error
= XFS_ERROR(EXDEV
);
2692 (error
= XFS_DIR_CANENTER(mp
, tp
, tdp
, target_name
,
2696 XFS_BMAP_INIT(&free_list
, &first_block
);
2698 error
= XFS_DIR_CREATENAME(mp
, tp
, tdp
, target_name
, target_namelen
,
2699 sip
->i_ino
, &first_block
, &free_list
,
2703 xfs_ichgtime(tdp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2705 xfs_trans_log_inode(tp
, tdp
, XFS_ILOG_CORE
);
2707 error
= xfs_bumplink(tp
, sip
);
2713 * If this is a synchronous mount, make sure that the
2714 * link transaction goes to disk before returning to
2717 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2718 xfs_trans_set_sync(tp
);
2721 error
= xfs_bmap_finish (&tp
, &free_list
, first_block
, &committed
);
2723 xfs_bmap_cancel(&free_list
);
2727 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
2732 /* Fall through to std_return with error = 0. */
2734 if (DM_EVENT_ENABLED(src_vp
->v_vfsp
, sip
,
2735 DM_EVENT_POSTLINK
)) {
2736 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTLINK
,
2737 target_dir_vp
, DM_RIGHT_NULL
,
2738 src_vp
, DM_RIGHT_NULL
,
2739 target_name
, NULL
, 0, error
, 0);
2744 cancel_flags
|= XFS_TRANS_ABORT
;
2748 xfs_trans_cancel(tp
, cancel_flags
);
2757 bhv_desc_t
*dir_bdp
,
2763 char *dir_name
= VNAME(dentry
);
2765 xfs_inode_t
*cdp
; /* inode of created dir */
2766 vnode_t
*cvp
; /* vnode of created dir */
2772 xfs_bmap_free_t free_list
;
2773 xfs_fsblock_t first_block
;
2775 boolean_t dp_joined_to_trans
;
2776 boolean_t created
= B_FALSE
;
2777 int dm_event_sent
= 0;
2779 struct xfs_dquot
*udqp
, *gdqp
;
2784 dir_vp
= BHV_TO_VNODE(dir_bdp
);
2785 dp
= XFS_BHVTOI(dir_bdp
);
2788 if (XFS_FORCED_SHUTDOWN(mp
))
2789 return XFS_ERROR(EIO
);
2791 dir_namelen
= VNAMELEN(dentry
);
2794 dp_joined_to_trans
= B_FALSE
;
2795 dm_di_mode
= vap
->va_mode
;
2797 if (DM_EVENT_ENABLED(dir_vp
->v_vfsp
, dp
, DM_EVENT_CREATE
)) {
2798 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_CREATE
,
2799 dir_vp
, DM_RIGHT_NULL
, NULL
,
2800 DM_RIGHT_NULL
, dir_name
, NULL
,
2807 /* Return through std_return after this point. */
2809 vn_trace_entry(dir_vp
, __FUNCTION__
, (inst_t
*)__return_address
);
2813 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
2814 prid
= dp
->i_d
.di_projid
;
2815 else if (vap
->va_mask
& XFS_AT_PROJID
)
2816 prid
= (xfs_prid_t
)vap
->va_projid
;
2818 prid
= (xfs_prid_t
)dfltprid
;
2821 * Make sure that we have allocated dquot(s) on disk.
2823 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
2824 current_fsuid(credp
), current_fsgid(credp
), prid
,
2825 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
2829 tp
= xfs_trans_alloc(mp
, XFS_TRANS_MKDIR
);
2830 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2831 resblks
= XFS_MKDIR_SPACE_RES(mp
, dir_namelen
);
2832 error
= xfs_trans_reserve(tp
, resblks
, XFS_MKDIR_LOG_RES(mp
), 0,
2833 XFS_TRANS_PERM_LOG_RES
, XFS_MKDIR_LOG_COUNT
);
2834 if (error
== ENOSPC
) {
2836 error
= xfs_trans_reserve(tp
, 0, XFS_MKDIR_LOG_RES(mp
), 0,
2837 XFS_TRANS_PERM_LOG_RES
,
2838 XFS_MKDIR_LOG_COUNT
);
2846 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
2849 * Check for directory link count overflow.
2851 if (dp
->i_d
.di_nlink
>= XFS_MAXLINK
) {
2852 error
= XFS_ERROR(EMLINK
);
2857 * Reserve disk quota and the inode.
2859 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
2864 (error
= XFS_DIR_CANENTER(mp
, tp
, dp
, dir_name
, dir_namelen
)))
2867 * create the directory inode.
2869 error
= xfs_dir_ialloc(&tp
, dp
, vap
->va_mode
, 2,
2870 0, credp
, prid
, resblks
> 0,
2873 if (error
== ENOSPC
)
2880 * Now we add the directory inode to the transaction.
2881 * We waited until now since xfs_dir_ialloc might start
2882 * a new transaction. Had we joined the transaction
2883 * earlier, the locks might have gotten released.
2886 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
2887 dp_joined_to_trans
= B_TRUE
;
2889 XFS_BMAP_INIT(&free_list
, &first_block
);
2891 error
= XFS_DIR_CREATENAME(mp
, tp
, dp
, dir_name
, dir_namelen
,
2892 cdp
->i_ino
, &first_block
, &free_list
,
2893 resblks
? resblks
- XFS_IALLOC_SPACE_RES(mp
) : 0);
2895 ASSERT(error
!= ENOSPC
);
2898 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2901 * Bump the in memory version number of the parent directory
2902 * so that other processes accessing it will recognize that
2903 * the directory has changed.
2907 error
= XFS_DIR_INIT(mp
, tp
, cdp
, dp
);
2913 error
= xfs_bumplink(tp
, dp
);
2918 cvp
= XFS_ITOV(cdp
);
2926 * Attach the dquots to the new inode and modify the icount incore.
2928 XFS_QM_DQVOPCREATE(mp
, tp
, cdp
, udqp
, gdqp
);
2931 * If this is a synchronous mount, make sure that the
2932 * mkdir transaction goes to disk before returning to
2935 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2936 xfs_trans_set_sync(tp
);
2939 error
= xfs_bmap_finish(&tp
, &free_list
, first_block
, &committed
);
2945 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
2946 XFS_QM_DQRELE(mp
, udqp
);
2947 XFS_QM_DQRELE(mp
, gdqp
);
2952 /* Fall through to std_return with error = 0 or errno from
2953 * xfs_trans_commit. */
2956 if ( (created
|| (error
!= 0 && dm_event_sent
!= 0)) &&
2957 DM_EVENT_ENABLED(dir_vp
->v_vfsp
, XFS_BHVTOI(dir_bdp
),
2958 DM_EVENT_POSTCREATE
)) {
2959 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTCREATE
,
2960 dir_vp
, DM_RIGHT_NULL
,
2961 created
? XFS_ITOV(cdp
):NULL
,
2964 dm_di_mode
, error
, 0);
2970 xfs_bmap_cancel(&free_list
);
2972 cancel_flags
|= XFS_TRANS_ABORT
;
2974 xfs_trans_cancel(tp
, cancel_flags
);
2975 XFS_QM_DQRELE(mp
, udqp
);
2976 XFS_QM_DQRELE(mp
, gdqp
);
2978 if (!dp_joined_to_trans
&& (dp
!= NULL
)) {
2979 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
2992 bhv_desc_t
*dir_bdp
,
2996 char *name
= VNAME(dentry
);
2998 xfs_inode_t
*cdp
; /* child directory */
3002 xfs_bmap_free_t free_list
;
3003 xfs_fsblock_t first_block
;
3012 dir_vp
= BHV_TO_VNODE(dir_bdp
);
3013 dp
= XFS_BHVTOI(dir_bdp
);
3016 vn_trace_entry(dir_vp
, __FUNCTION__
, (inst_t
*)__return_address
);
3018 if (XFS_FORCED_SHUTDOWN(XFS_BHVTOI(dir_bdp
)->i_mount
))
3019 return XFS_ERROR(EIO
);
3020 namelen
= VNAMELEN(dentry
);
3022 if (DM_EVENT_ENABLED(dir_vp
->v_vfsp
, dp
, DM_EVENT_REMOVE
)) {
3023 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_REMOVE
,
3024 dir_vp
, DM_RIGHT_NULL
,
3025 NULL
, DM_RIGHT_NULL
,
3026 name
, NULL
, 0, 0, 0);
3028 return XFS_ERROR(error
);
3031 /* Return through std_return after this point. */
3036 * We need to get a reference to cdp before we get our log
3037 * reservation. The reason for this is that we cannot call
3038 * xfs_iget for an inode for which we do not have a reference
3039 * once we've acquired a log reservation. This is because the
3040 * inode we are trying to get might be in xfs_inactive going
3041 * for a log reservation. Since we'll have to wait for the
3042 * inactive code to complete before returning from xfs_iget,
3043 * we need to make sure that we don't have log space reserved
3044 * when we call xfs_iget. Instead we get an unlocked reference
3045 * to the inode before getting our log reservation.
3047 error
= xfs_get_dir_entry(dentry
, &cdp
);
3049 REMOVE_DEBUG_TRACE(__LINE__
);
3053 dm_di_mode
= cdp
->i_d
.di_mode
;
3056 * Get the dquots for the inodes.
3058 error
= XFS_QM_DQATTACH(mp
, dp
, 0);
3059 if (!error
&& dp
!= cdp
)
3060 error
= XFS_QM_DQATTACH(mp
, cdp
, 0);
3063 REMOVE_DEBUG_TRACE(__LINE__
);
3067 tp
= xfs_trans_alloc(mp
, XFS_TRANS_RMDIR
);
3068 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
3070 * We try to get the real space reservation first,
3071 * allowing for directory btree deletion(s) implying
3072 * possible bmap insert(s). If we can't get the space
3073 * reservation then we use 0 instead, and avoid the bmap
3074 * btree insert(s) in the directory code by, if the bmap
3075 * insert tries to happen, instead trimming the LAST
3076 * block from the directory.
3078 resblks
= XFS_REMOVE_SPACE_RES(mp
);
3079 error
= xfs_trans_reserve(tp
, resblks
, XFS_REMOVE_LOG_RES(mp
), 0,
3080 XFS_TRANS_PERM_LOG_RES
, XFS_DEFAULT_LOG_COUNT
);
3081 if (error
== ENOSPC
) {
3083 error
= xfs_trans_reserve(tp
, 0, XFS_REMOVE_LOG_RES(mp
), 0,
3084 XFS_TRANS_PERM_LOG_RES
, XFS_DEFAULT_LOG_COUNT
);
3087 ASSERT(error
!= ENOSPC
);
3092 XFS_BMAP_INIT(&free_list
, &first_block
);
3095 * Now lock the child directory inode and the parent directory
3096 * inode in the proper order. This will take care of validating
3097 * that the directory entry for the child directory inode has
3098 * not changed while we were obtaining a log reservation.
3100 error
= xfs_lock_dir_and_entry(dp
, dentry
, cdp
);
3102 xfs_trans_cancel(tp
, cancel_flags
);
3107 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
3110 * Only increment the parent directory vnode count if
3111 * we didn't bump it in looking up cdp. The only time
3112 * we don't bump it is when we're looking up ".".
3118 xfs_trans_ijoin(tp
, cdp
, XFS_ILOCK_EXCL
);
3120 ASSERT(cdp
->i_d
.di_nlink
>= 2);
3121 if (cdp
->i_d
.di_nlink
!= 2) {
3122 error
= XFS_ERROR(ENOTEMPTY
);
3125 if (!XFS_DIR_ISEMPTY(mp
, cdp
)) {
3126 error
= XFS_ERROR(ENOTEMPTY
);
3130 error
= XFS_DIR_REMOVENAME(mp
, tp
, dp
, name
, namelen
, cdp
->i_ino
,
3131 &first_block
, &free_list
, resblks
);
3136 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
3139 * Bump the in memory generation count on the parent
3140 * directory so that other can know that it has changed.
3145 * Drop the link from cdp's "..".
3147 error
= xfs_droplink(tp
, dp
);
3153 * Drop the link from dp to cdp.
3155 error
= xfs_droplink(tp
, cdp
);
3161 * Drop the "." link from cdp to self.
3163 error
= xfs_droplink(tp
, cdp
);
3168 /* Determine these before committing transaction */
3169 last_cdp_link
= (cdp
)->i_d
.di_nlink
==0;
3172 * Take an extra ref on the child vnode so that it
3173 * does not go to xfs_inactive() from within the commit.
3178 * If this is a synchronous mount, make sure that the
3179 * rmdir transaction goes to disk before returning to
3182 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
3183 xfs_trans_set_sync(tp
);
3186 error
= xfs_bmap_finish (&tp
, &free_list
, first_block
, &committed
);
3188 xfs_bmap_cancel(&free_list
);
3189 xfs_trans_cancel(tp
, (XFS_TRANS_RELEASE_LOG_RES
|
3195 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
3203 * Let interposed file systems know about removed links.
3205 VOP_LINK_REMOVED(XFS_ITOV(cdp
), dir_vp
, last_cdp_link
);
3209 /* Fall through to std_return with error = 0 or the errno
3210 * from xfs_trans_commit. */
3212 if (DM_EVENT_ENABLED(dir_vp
->v_vfsp
, dp
, DM_EVENT_POSTREMOVE
)) {
3213 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTREMOVE
,
3214 dir_vp
, DM_RIGHT_NULL
,
3215 NULL
, DM_RIGHT_NULL
,
3216 name
, NULL
, dm_di_mode
,
3222 xfs_bmap_cancel(&free_list
);
3223 cancel_flags
|= XFS_TRANS_ABORT
;
3227 xfs_trans_cancel(tp
, cancel_flags
);
3235 * Read dp's entries starting at uiop->uio_offset and translate them into
3236 * bufsize bytes worth of struct dirents starting at bufbase.
3240 bhv_desc_t
*dir_bdp
,
3246 xfs_trans_t
*tp
= NULL
;
3250 vn_trace_entry(BHV_TO_VNODE(dir_bdp
), __FUNCTION__
,
3251 (inst_t
*)__return_address
);
3252 dp
= XFS_BHVTOI(dir_bdp
);
3254 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
)) {
3255 return XFS_ERROR(EIO
);
3258 lock_mode
= xfs_ilock_map_shared(dp
);
3259 error
= XFS_DIR_GETDENTS(dp
->i_mount
, tp
, dp
, uiop
, eofp
);
3260 xfs_iunlock_map_shared(dp
, lock_mode
);
3271 bhv_desc_t
*dir_bdp
,
3284 xfs_bmap_free_t free_list
;
3285 xfs_fsblock_t first_block
;
3286 boolean_t dp_joined_to_trans
;
3290 xfs_fileoff_t first_fsb
;
3291 xfs_filblks_t fs_blocks
;
3293 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
3300 struct xfs_dquot
*udqp
, *gdqp
;
3302 char *link_name
= VNAME(dentry
);
3306 dir_vp
= BHV_TO_VNODE(dir_bdp
);
3307 dp
= XFS_BHVTOI(dir_bdp
);
3308 dp_joined_to_trans
= B_FALSE
;
3313 vn_trace_entry(dir_vp
, __FUNCTION__
, (inst_t
*)__return_address
);
3317 if (XFS_FORCED_SHUTDOWN(mp
))
3318 return XFS_ERROR(EIO
);
3320 link_namelen
= VNAMELEN(dentry
);
3323 * Check component lengths of the target path name.
3325 pathlen
= strlen(target_path
);
3326 if (pathlen
>= MAXPATHLEN
) /* total string too long */
3327 return XFS_ERROR(ENAMETOOLONG
);
3328 if (pathlen
>= MAXNAMELEN
) { /* is any component too long? */
3332 for(total
= 0, path
= target_path
; total
< pathlen
;) {
3336 while(*path
== '/') {
3342 * Count up to the next slash or end of path.
3343 * Error out if the component is bigger than MAXNAMELEN.
3345 for(len
= 0; *path
!= '/' && total
< pathlen
;total
++, path
++) {
3346 if (++len
>= MAXNAMELEN
) {
3347 error
= ENAMETOOLONG
;
3354 if (DM_EVENT_ENABLED(dir_vp
->v_vfsp
, dp
, DM_EVENT_SYMLINK
)) {
3355 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_SYMLINK
, dir_vp
,
3356 DM_RIGHT_NULL
, NULL
, DM_RIGHT_NULL
,
3357 link_name
, target_path
, 0, 0, 0);
3362 /* Return through std_return after this point. */
3365 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
3366 prid
= dp
->i_d
.di_projid
;
3367 else if (vap
->va_mask
& XFS_AT_PROJID
)
3368 prid
= (xfs_prid_t
)vap
->va_projid
;
3370 prid
= (xfs_prid_t
)dfltprid
;
3373 * Make sure that we have allocated dquot(s) on disk.
3375 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
3376 current_fsuid(credp
), current_fsgid(credp
), prid
,
3377 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
3381 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SYMLINK
);
3382 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
3384 * The symlink will fit into the inode data fork?
3385 * There can't be any attributes so we get the whole variable part.
3387 if (pathlen
<= XFS_LITINO(mp
))
3390 fs_blocks
= XFS_B_TO_FSB(mp
, pathlen
);
3391 resblks
= XFS_SYMLINK_SPACE_RES(mp
, link_namelen
, fs_blocks
);
3392 error
= xfs_trans_reserve(tp
, resblks
, XFS_SYMLINK_LOG_RES(mp
), 0,
3393 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
3394 if (error
== ENOSPC
&& fs_blocks
== 0) {
3396 error
= xfs_trans_reserve(tp
, 0, XFS_SYMLINK_LOG_RES(mp
), 0,
3397 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
3405 xfs_ilock(dp
, XFS_ILOCK_EXCL
);
3408 * Check whether the directory allows new symlinks or not.
3410 if (dp
->i_d
.di_flags
& XFS_DIFLAG_NOSYMLINKS
) {
3411 error
= XFS_ERROR(EPERM
);
3416 * Reserve disk quota : blocks and inode.
3418 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
3423 * Check for ability to enter directory entry, if no space reserved.
3426 (error
= XFS_DIR_CANENTER(mp
, tp
, dp
, link_name
, link_namelen
)))
3429 * Initialize the bmap freelist prior to calling either
3430 * bmapi or the directory create code.
3432 XFS_BMAP_INIT(&free_list
, &first_block
);
3435 * Allocate an inode for the symlink.
3437 error
= xfs_dir_ialloc(&tp
, dp
, S_IFLNK
| (vap
->va_mode
&~S_IFMT
),
3438 1, 0, credp
, prid
, resblks
> 0, &ip
, NULL
);
3440 if (error
== ENOSPC
)
3447 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
3448 dp_joined_to_trans
= B_TRUE
;
3451 * Also attach the dquot(s) to it, if applicable.
3453 XFS_QM_DQVOPCREATE(mp
, tp
, ip
, udqp
, gdqp
);
3456 resblks
-= XFS_IALLOC_SPACE_RES(mp
);
3458 * If the symlink will fit into the inode, write it inline.
3460 if (pathlen
<= XFS_IFORK_DSIZE(ip
)) {
3461 xfs_idata_realloc(ip
, pathlen
, XFS_DATA_FORK
);
3462 memcpy(ip
->i_df
.if_u1
.if_data
, target_path
, pathlen
);
3463 ip
->i_d
.di_size
= pathlen
;
3466 * The inode was initially created in extent format.
3468 ip
->i_df
.if_flags
&= ~(XFS_IFEXTENTS
| XFS_IFBROOT
);
3469 ip
->i_df
.if_flags
|= XFS_IFINLINE
;
3471 ip
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
3472 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_DDATA
| XFS_ILOG_CORE
);
3476 nmaps
= SYMLINK_MAPS
;
3478 error
= xfs_bmapi(tp
, ip
, first_fsb
, fs_blocks
,
3479 XFS_BMAPI_WRITE
| XFS_BMAPI_METADATA
,
3480 &first_block
, resblks
, mval
, &nmaps
,
3487 resblks
-= fs_blocks
;
3488 ip
->i_d
.di_size
= pathlen
;
3489 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
3491 cur_chunk
= target_path
;
3492 for (n
= 0; n
< nmaps
; n
++) {
3493 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
3494 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
3495 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
3496 BTOBB(byte_cnt
), 0);
3497 ASSERT(bp
&& !XFS_BUF_GETERROR(bp
));
3498 if (pathlen
< byte_cnt
) {
3501 pathlen
-= byte_cnt
;
3503 memcpy(XFS_BUF_PTR(bp
), cur_chunk
, byte_cnt
);
3504 cur_chunk
+= byte_cnt
;
3506 xfs_trans_log_buf(tp
, bp
, 0, byte_cnt
- 1);
3511 * Create the directory entry for the symlink.
3513 error
= XFS_DIR_CREATENAME(mp
, tp
, dp
, link_name
, link_namelen
,
3514 ip
->i_ino
, &first_block
, &free_list
, resblks
);
3518 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
3519 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
3522 * Bump the in memory version number of the parent directory
3523 * so that other processes accessing it will recognize that
3524 * the directory has changed.
3529 * If this is a synchronous mount, make sure that the
3530 * symlink transaction goes to disk before returning to
3533 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
3534 xfs_trans_set_sync(tp
);
3538 * xfs_trans_commit normally decrements the vnode ref count
3539 * when it unlocks the inode. Since we want to return the
3540 * vnode to the caller, we bump the vnode ref count now.
3544 error
= xfs_bmap_finish(&tp
, &free_list
, first_block
, &committed
);
3548 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
3549 XFS_QM_DQRELE(mp
, udqp
);
3550 XFS_QM_DQRELE(mp
, gdqp
);
3552 /* Fall through to std_return with error = 0 or errno from
3553 * xfs_trans_commit */
3555 if (DM_EVENT_ENABLED(dir_vp
->v_vfsp
, XFS_BHVTOI(dir_bdp
),
3556 DM_EVENT_POSTSYMLINK
)) {
3557 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTSYMLINK
,
3558 dir_vp
, DM_RIGHT_NULL
,
3559 error
? NULL
: XFS_ITOV(ip
),
3560 DM_RIGHT_NULL
, link_name
, target_path
,
3576 xfs_bmap_cancel(&free_list
);
3577 cancel_flags
|= XFS_TRANS_ABORT
;
3579 xfs_trans_cancel(tp
, cancel_flags
);
3580 XFS_QM_DQRELE(mp
, udqp
);
3581 XFS_QM_DQRELE(mp
, gdqp
);
3583 if (!dp_joined_to_trans
&& (dp
!= NULL
)) {
3584 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
3594 * A fid routine that takes a pointer to a previously allocated
3595 * fid structure (like xfs_fast_fid) but uses a 64 bit inode number.
3605 vn_trace_entry(BHV_TO_VNODE(bdp
), __FUNCTION__
,
3606 (inst_t
*)__return_address
);
3607 ASSERT(sizeof(fid_t
) >= sizeof(xfs_fid2_t
));
3609 xfid
= (xfs_fid2_t
*)fidp
;
3610 ip
= XFS_BHVTOI(bdp
);
3611 xfid
->fid_len
= sizeof(xfs_fid2_t
) - sizeof(xfid
->fid_len
);
3614 * use memcpy because the inode is a long long and there's no
3615 * assurance that xfid->fid_ino is properly aligned.
3617 memcpy(&xfid
->fid_ino
, &ip
->i_ino
, sizeof(xfid
->fid_ino
));
3618 xfid
->fid_gen
= ip
->i_d
.di_gen
;
3635 vp
= BHV_TO_VNODE(bdp
);
3638 ip
= XFS_BHVTOI(bdp
);
3639 if (locktype
== VRWLOCK_WRITE
) {
3640 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
3641 } else if (locktype
== VRWLOCK_TRY_READ
) {
3642 return xfs_ilock_nowait(ip
, XFS_IOLOCK_SHARED
);
3643 } else if (locktype
== VRWLOCK_TRY_WRITE
) {
3644 return xfs_ilock_nowait(ip
, XFS_IOLOCK_EXCL
);
3646 ASSERT((locktype
== VRWLOCK_READ
) ||
3647 (locktype
== VRWLOCK_WRITE_DIRECT
));
3648 xfs_ilock(ip
, XFS_IOLOCK_SHARED
);
3666 vp
= BHV_TO_VNODE(bdp
);
3669 ip
= XFS_BHVTOI(bdp
);
3670 if (locktype
== VRWLOCK_WRITE
) {
3672 * In the write case, we may have added a new entry to
3673 * the reference cache. This might store a pointer to
3674 * an inode to be released in this inode. If it is there,
3675 * clear the pointer and release the inode after unlocking
3678 xfs_refcache_iunlock(ip
, XFS_IOLOCK_EXCL
);
3680 ASSERT((locktype
== VRWLOCK_READ
) ||
3681 (locktype
== VRWLOCK_WRITE_DIRECT
));
3682 xfs_iunlock(ip
, XFS_IOLOCK_SHARED
);
3694 xfs_inode_log_item_t
*iip
;
3697 ip
= XFS_BHVTOI(bdp
);
3701 if (XFS_FORCED_SHUTDOWN(mp
))
3702 return XFS_ERROR(EIO
);
3705 * Bypass inodes which have already been cleaned by
3706 * the inode flush clustering code inside xfs_iflush
3708 if ((ip
->i_update_core
== 0) &&
3709 ((iip
== NULL
) || !(iip
->ili_format
.ilf_fields
& XFS_ILOG_ALL
)))
3712 if (flags
& FLUSH_LOG
) {
3713 if (iip
&& iip
->ili_last_lsn
) {
3714 xlog_t
*log
= mp
->m_log
;
3716 int s
, log_flags
= XFS_LOG_FORCE
;
3718 s
= GRANT_LOCK(log
);
3719 sync_lsn
= log
->l_last_sync_lsn
;
3720 GRANT_UNLOCK(log
, s
);
3722 if ((XFS_LSN_CMP(iip
->ili_last_lsn
, sync_lsn
) <= 0))
3725 if (flags
& FLUSH_SYNC
)
3726 log_flags
|= XFS_LOG_SYNC
;
3727 return xfs_log_force(mp
, iip
->ili_last_lsn
, log_flags
);
3732 * We make this non-blocking if the inode is contended,
3733 * return EAGAIN to indicate to the caller that they
3734 * did not succeed. This prevents the flush path from
3735 * blocking on inodes inside another operation right
3736 * now, they get caught later by xfs_sync.
3738 if (flags
& FLUSH_INODE
) {
3741 if (xfs_ipincount(ip
))
3744 if (flags
& FLUSH_SYNC
) {
3745 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
3747 } else if (xfs_ilock_nowait(ip
, XFS_ILOCK_SHARED
)) {
3748 if (xfs_ipincount(ip
) || !xfs_iflock_nowait(ip
)) {
3749 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
3756 if (flags
& FLUSH_SYNC
)
3757 flush_flags
= XFS_IFLUSH_SYNC
;
3759 flush_flags
= XFS_IFLUSH_ASYNC
;
3761 error
= xfs_iflush(ip
, flush_flags
);
3762 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
3780 if (!capable(CAP_SYS_ADMIN
))
3781 return XFS_ERROR(EPERM
);
3783 ip
= XFS_BHVTOI(bdp
);
3786 if (XFS_FORCED_SHUTDOWN(mp
))
3787 return XFS_ERROR(EIO
);
3789 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SET_DMATTRS
);
3790 error
= xfs_trans_reserve(tp
, 0, XFS_ICHANGE_LOG_RES (mp
), 0, 0, 0);
3792 xfs_trans_cancel(tp
, 0);
3795 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3796 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3798 ip
->i_iocore
.io_dmevmask
= ip
->i_d
.di_dmevmask
= evmask
;
3799 ip
->i_iocore
.io_dmstate
= ip
->i_d
.di_dmstate
= state
;
3801 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
3803 error
= xfs_trans_commit(tp
, 0, NULL
);
3815 vp
= BHV_TO_VNODE(bdp
);
3816 ip
= XFS_BHVTOI(bdp
);
3818 vn_trace_entry(vp
, __FUNCTION__
, (inst_t
*)__return_address
);
3820 ASSERT(!VN_MAPPED(vp
));
3822 /* bad inode, get out here ASAP */
3830 ASSERT(XFS_FORCED_SHUTDOWN(ip
->i_mount
) || ip
->i_delayed_blks
== 0);
3833 * Make sure the atime in the XFS inode is correct before freeing the
3836 xfs_synchronize_atime(ip
);
3838 /* If we have nothing to flush with this inode then complete the
3839 * teardown now, otherwise break the link between the xfs inode
3840 * and the linux inode and clean up the xfs inode later. This
3841 * avoids flushing the inode to disk during the delete operation
3844 if (!ip
->i_update_core
&& (ip
->i_itemp
== NULL
)) {
3845 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3847 return xfs_finish_reclaim(ip
, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC
);
3849 xfs_mount_t
*mp
= ip
->i_mount
;
3851 /* Protect sync from us */
3852 XFS_MOUNT_ILOCK(mp
);
3853 vn_bhv_remove(VN_BHV_HEAD(vp
), XFS_ITOBHV(ip
));
3854 list_add_tail(&ip
->i_reclaim
, &mp
->m_del_inodes
);
3855 ip
->i_flags
|= XFS_IRECLAIMABLE
;
3856 XFS_MOUNT_IUNLOCK(mp
);
3867 xfs_ihash_t
*ih
= ip
->i_hash
;
3868 vnode_t
*vp
= XFS_ITOV_NULL(ip
);
3871 if (vp
&& VN_BAD(vp
))
3874 /* The hash lock here protects a thread in xfs_iget_core from
3875 * racing with us on linking the inode back with a vnode.
3876 * Once we have the XFS_IRECLAIM flag set it will not touch
3879 write_lock(&ih
->ih_lock
);
3880 if ((ip
->i_flags
& XFS_IRECLAIM
) ||
3881 (!(ip
->i_flags
& XFS_IRECLAIMABLE
) && vp
== NULL
)) {
3882 write_unlock(&ih
->ih_lock
);
3885 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3889 ip
->i_flags
|= XFS_IRECLAIM
;
3890 write_unlock(&ih
->ih_lock
);
3893 * If the inode is still dirty, then flush it out. If the inode
3894 * is not in the AIL, then it will be OK to flush it delwri as
3895 * long as xfs_iflush() does not keep any references to the inode.
3896 * We leave that decision up to xfs_iflush() since it has the
3897 * knowledge of whether it's OK to simply do a delwri flush of
3898 * the inode or whether we need to wait until the inode is
3899 * pulled from the AIL.
3900 * We get the flush lock regardless, though, just to make sure
3901 * we don't free it while it is being flushed.
3903 if (!XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
3905 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3909 if (ip
->i_update_core
||
3910 ((ip
->i_itemp
!= NULL
) &&
3911 (ip
->i_itemp
->ili_format
.ilf_fields
!= 0))) {
3912 error
= xfs_iflush(ip
, sync_mode
);
3914 * If we hit an error, typically because of filesystem
3915 * shutdown, we don't need to let vn_reclaim to know
3916 * because we're gonna reclaim the inode anyway.
3919 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3922 xfs_iflock(ip
); /* synchronize with xfs_iflush_done */
3925 ASSERT(ip
->i_update_core
== 0);
3926 ASSERT(ip
->i_itemp
== NULL
||
3927 ip
->i_itemp
->ili_format
.ilf_fields
== 0);
3928 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3929 } else if (locked
) {
3931 * We are not interested in doing an iflush if we're
3932 * in the process of shutting down the filesystem forcibly.
3933 * So, just reclaim the inode.
3936 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3945 xfs_finish_reclaim_all(xfs_mount_t
*mp
, int noblock
)
3948 xfs_inode_t
*ip
, *n
;
3953 XFS_MOUNT_ILOCK(mp
);
3954 list_for_each_entry_safe(ip
, n
, &mp
->m_del_inodes
, i_reclaim
) {
3956 if (xfs_ilock_nowait(ip
, XFS_ILOCK_EXCL
) == 0)
3958 if (xfs_ipincount(ip
) ||
3959 !xfs_iflock_nowait(ip
)) {
3960 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3964 XFS_MOUNT_IUNLOCK(mp
);
3965 if (xfs_finish_reclaim(ip
, noblock
,
3966 XFS_IFLUSH_DELWRI_ELSE_ASYNC
))
3975 XFS_MOUNT_IUNLOCK(mp
);
3980 * xfs_alloc_file_space()
3981 * This routine allocates disk space for the given file.
3983 * If alloc_type == 0, this request is for an ALLOCSP type
3984 * request which will change the file size. In this case, no
3985 * DMAPI event will be generated by the call. A TRUNCATE event
3986 * will be generated later by xfs_setattr.
3988 * If alloc_type != 0, this request is for a RESVSP type
3989 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3990 * lower block boundary byte address is less than the file's
3999 xfs_alloc_file_space(
4006 xfs_mount_t
*mp
= ip
->i_mount
;
4008 xfs_filblks_t allocated_fsb
;
4009 xfs_filblks_t allocatesize_fsb
;
4010 xfs_extlen_t extsz
, temp
;
4011 xfs_fileoff_t startoffset_fsb
;
4012 xfs_fsblock_t firstfsb
;
4018 xfs_bmbt_irec_t imaps
[1], *imapp
;
4019 xfs_bmap_free_t free_list
;
4020 uint qblocks
, resblks
, resrtextents
;
4024 vn_trace_entry(XFS_ITOV(ip
), __FUNCTION__
, (inst_t
*)__return_address
);
4026 if (XFS_FORCED_SHUTDOWN(mp
))
4027 return XFS_ERROR(EIO
);
4029 rt
= XFS_IS_REALTIME_INODE(ip
);
4031 if (!(extsz
= ip
->i_d
.di_extsize
))
4032 extsz
= mp
->m_sb
.sb_rextsize
;
4034 extsz
= ip
->i_d
.di_extsize
;
4037 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
4041 return XFS_ERROR(EINVAL
);
4047 bmapi_flag
= XFS_BMAPI_WRITE
| (alloc_type
? XFS_BMAPI_PREALLOC
: 0);
4048 startoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
4049 allocatesize_fsb
= XFS_B_TO_FSB(mp
, count
);
4051 /* Generate a DMAPI event if needed. */
4052 if (alloc_type
!= 0 && offset
< ip
->i_d
.di_size
&&
4053 (attr_flags
&ATTR_DMI
) == 0 &&
4054 DM_EVENT_ENABLED(XFS_MTOVFS(mp
), ip
, DM_EVENT_WRITE
)) {
4055 xfs_off_t end_dmi_offset
;
4057 end_dmi_offset
= offset
+len
;
4058 if (end_dmi_offset
> ip
->i_d
.di_size
)
4059 end_dmi_offset
= ip
->i_d
.di_size
;
4060 error
= XFS_SEND_DATA(mp
, DM_EVENT_WRITE
, XFS_ITOV(ip
),
4061 offset
, end_dmi_offset
- offset
,
4068 * Allocate file space until done or until there is an error
4071 while (allocatesize_fsb
&& !error
) {
4075 * Determine space reservations for data/realtime.
4077 if (unlikely(extsz
)) {
4078 s
= startoffset_fsb
;
4081 e
= startoffset_fsb
+ allocatesize_fsb
;
4082 if ((temp
= do_mod(startoffset_fsb
, extsz
)))
4084 if ((temp
= do_mod(e
, extsz
)))
4088 e
= allocatesize_fsb
;
4092 resrtextents
= qblocks
= (uint
)(e
- s
);
4093 resrtextents
/= mp
->m_sb
.sb_rextsize
;
4094 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
4095 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
4098 resblks
= qblocks
= \
4099 XFS_DIOSTRAT_SPACE_RES(mp
, (uint
)(e
- s
));
4100 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
4104 * Allocate and setup the transaction.
4106 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
4107 error
= xfs_trans_reserve(tp
, resblks
,
4108 XFS_WRITE_LOG_RES(mp
), resrtextents
,
4109 XFS_TRANS_PERM_LOG_RES
,
4110 XFS_WRITE_LOG_COUNT
);
4112 * Check for running out of space
4116 * Free the transaction structure.
4118 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
4119 xfs_trans_cancel(tp
, 0);
4122 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
4123 error
= XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, tp
, ip
,
4124 qblocks
, 0, quota_flag
);
4128 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
4129 xfs_trans_ihold(tp
, ip
);
4132 * Issue the xfs_bmapi() call to allocate the blocks
4134 XFS_BMAP_INIT(&free_list
, &firstfsb
);
4135 error
= XFS_BMAPI(mp
, tp
, &ip
->i_iocore
, startoffset_fsb
,
4136 allocatesize_fsb
, bmapi_flag
,
4137 &firstfsb
, 0, imapp
, &nimaps
,
4144 * Complete the transaction
4146 error
= xfs_bmap_finish(&tp
, &free_list
, firstfsb
, &committed
);
4151 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
4152 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4157 allocated_fsb
= imapp
->br_blockcount
;
4160 error
= XFS_ERROR(ENOSPC
);
4164 startoffset_fsb
+= allocated_fsb
;
4165 allocatesize_fsb
-= allocated_fsb
;
4168 if (error
== ENOSPC
&& (attr_flags
&ATTR_DMI
) == 0 &&
4169 DM_EVENT_ENABLED(XFS_MTOVFS(mp
), ip
, DM_EVENT_NOSPACE
)) {
4171 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_NOSPACE
,
4172 XFS_ITOV(ip
), DM_RIGHT_NULL
,
4173 XFS_ITOV(ip
), DM_RIGHT_NULL
,
4174 NULL
, NULL
, 0, 0, 0); /* Delay flag intentionally unused */
4176 goto retry
; /* Maybe DMAPI app. has made space */
4177 /* else fall through with error from XFS_SEND_DATA */
4182 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
4183 xfs_bmap_cancel(&free_list
);
4184 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp
, tp
, ip
, qblocks
, 0, quota_flag
);
4186 error1
: /* Just cancel transaction */
4187 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
4188 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4189 goto dmapi_enospc_check
;
4193 * Zero file bytes between startoff and endoff inclusive.
4194 * The iolock is held exclusive and no blocks are buffered.
4197 xfs_zero_remaining_bytes(
4202 xfs_bmbt_irec_t imap
;
4203 xfs_fileoff_t offset_fsb
;
4204 xfs_off_t lastoffset
;
4207 xfs_mount_t
*mp
= ip
->i_mount
;
4211 bp
= xfs_buf_get_noaddr(mp
->m_sb
.sb_blocksize
,
4212 ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
?
4213 mp
->m_rtdev_targp
: mp
->m_ddev_targp
);
4215 for (offset
= startoff
; offset
<= endoff
; offset
= lastoffset
+ 1) {
4216 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
4218 error
= XFS_BMAPI(mp
, NULL
, &ip
->i_iocore
, offset_fsb
, 1, 0,
4219 NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
4220 if (error
|| nimap
< 1)
4222 ASSERT(imap
.br_blockcount
>= 1);
4223 ASSERT(imap
.br_startoff
== offset_fsb
);
4224 lastoffset
= XFS_FSB_TO_B(mp
, imap
.br_startoff
+ 1) - 1;
4225 if (lastoffset
> endoff
)
4226 lastoffset
= endoff
;
4227 if (imap
.br_startblock
== HOLESTARTBLOCK
)
4229 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
4230 if (imap
.br_state
== XFS_EXT_UNWRITTEN
)
4233 XFS_BUF_UNWRITE(bp
);
4235 XFS_BUF_SET_ADDR(bp
, XFS_FSB_TO_DB(ip
, imap
.br_startblock
));
4237 if ((error
= xfs_iowait(bp
))) {
4238 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
4239 mp
, bp
, XFS_BUF_ADDR(bp
));
4242 memset(XFS_BUF_PTR(bp
) +
4243 (offset
- XFS_FSB_TO_B(mp
, imap
.br_startoff
)),
4244 0, lastoffset
- offset
+ 1);
4249 if ((error
= xfs_iowait(bp
))) {
4250 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
4251 mp
, bp
, XFS_BUF_ADDR(bp
));
4260 * xfs_free_file_space()
4261 * This routine frees disk space for the given file.
4263 * This routine is only called by xfs_change_file_space
4264 * for an UNRESVSP type call.
4272 xfs_free_file_space(
4281 xfs_off_t end_dmi_offset
;
4282 xfs_fileoff_t endoffset_fsb
;
4284 xfs_fsblock_t firstfsb
;
4285 xfs_bmap_free_t free_list
;
4287 xfs_bmbt_irec_t imap
;
4295 xfs_fileoff_t startoffset_fsb
;
4297 int need_iolock
= 1;
4302 vn_trace_entry(vp
, __FUNCTION__
, (inst_t
*)__return_address
);
4304 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
4308 if (len
<= 0) /* if nothing being freed */
4310 rt
= (ip
->i_d
.di_flags
& XFS_DIFLAG_REALTIME
);
4311 startoffset_fsb
= XFS_B_TO_FSB(mp
, offset
);
4312 end_dmi_offset
= offset
+ len
;
4313 endoffset_fsb
= XFS_B_TO_FSBT(mp
, end_dmi_offset
);
4315 if (offset
< ip
->i_d
.di_size
&&
4316 (attr_flags
& ATTR_DMI
) == 0 &&
4317 DM_EVENT_ENABLED(XFS_MTOVFS(mp
), ip
, DM_EVENT_WRITE
)) {
4318 if (end_dmi_offset
> ip
->i_d
.di_size
)
4319 end_dmi_offset
= ip
->i_d
.di_size
;
4320 error
= XFS_SEND_DATA(mp
, DM_EVENT_WRITE
, vp
,
4321 offset
, end_dmi_offset
- offset
,
4322 AT_DELAY_FLAG(attr_flags
), NULL
);
4327 if (attr_flags
& ATTR_NOLOCK
)
4330 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
4331 vn_iowait(vp
); /* wait for the completion of any pending DIOs */
4334 rounding
= MAX((__uint8_t
)(1 << mp
->m_sb
.sb_blocklog
),
4336 ilen
= len
+ (offset
& (rounding
- 1));
4337 ioffset
= offset
& ~(rounding
- 1);
4338 if (ilen
& (rounding
- 1))
4339 ilen
= (ilen
+ rounding
) & ~(rounding
- 1);
4341 if (VN_CACHED(vp
) != 0) {
4342 xfs_inval_cached_trace(&ip
->i_iocore
, ioffset
, -1,
4343 ctooff(offtoct(ioffset
)), -1);
4344 VOP_FLUSHINVAL_PAGES(vp
, ctooff(offtoct(ioffset
)),
4345 -1, FI_REMAPF_LOCKED
);
4349 * Need to zero the stuff we're not freeing, on disk.
4350 * If its a realtime file & can't use unwritten extents then we
4351 * actually need to zero the extent edges. Otherwise xfs_bunmapi
4352 * will take care of it for us.
4354 if (rt
&& !XFS_SB_VERSION_HASEXTFLGBIT(&mp
->m_sb
)) {
4356 error
= XFS_BMAPI(mp
, NULL
, &ip
->i_iocore
, startoffset_fsb
,
4357 1, 0, NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
4359 goto out_unlock_iolock
;
4360 ASSERT(nimap
== 0 || nimap
== 1);
4361 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
4364 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
4365 block
= imap
.br_startblock
;
4366 mod
= do_div(block
, mp
->m_sb
.sb_rextsize
);
4368 startoffset_fsb
+= mp
->m_sb
.sb_rextsize
- mod
;
4371 error
= XFS_BMAPI(mp
, NULL
, &ip
->i_iocore
, endoffset_fsb
- 1,
4372 1, 0, NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
4374 goto out_unlock_iolock
;
4375 ASSERT(nimap
== 0 || nimap
== 1);
4376 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
4377 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
4379 if (mod
&& (mod
!= mp
->m_sb
.sb_rextsize
))
4380 endoffset_fsb
-= mod
;
4383 if ((done
= (endoffset_fsb
<= startoffset_fsb
)))
4385 * One contiguous piece to clear
4387 error
= xfs_zero_remaining_bytes(ip
, offset
, offset
+ len
- 1);
4390 * Some full blocks, possibly two pieces to clear
4392 if (offset
< XFS_FSB_TO_B(mp
, startoffset_fsb
))
4393 error
= xfs_zero_remaining_bytes(ip
, offset
,
4394 XFS_FSB_TO_B(mp
, startoffset_fsb
) - 1);
4396 XFS_FSB_TO_B(mp
, endoffset_fsb
) < offset
+ len
)
4397 error
= xfs_zero_remaining_bytes(ip
,
4398 XFS_FSB_TO_B(mp
, endoffset_fsb
),
4403 * free file space until done or until there is an error
4405 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
4406 while (!error
&& !done
) {
4409 * allocate and setup the transaction
4411 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
4412 error
= xfs_trans_reserve(tp
,
4414 XFS_WRITE_LOG_RES(mp
),
4416 XFS_TRANS_PERM_LOG_RES
,
4417 XFS_WRITE_LOG_COUNT
);
4420 * check for running out of space
4424 * Free the transaction structure.
4426 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
4427 xfs_trans_cancel(tp
, 0);
4430 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
4431 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
,
4432 ip
->i_udquot
, ip
->i_gdquot
, resblks
, 0,
4433 XFS_QMOPT_RES_REGBLKS
);
4437 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
4438 xfs_trans_ihold(tp
, ip
);
4441 * issue the bunmapi() call to free the blocks
4443 XFS_BMAP_INIT(&free_list
, &firstfsb
);
4444 error
= XFS_BUNMAPI(mp
, tp
, &ip
->i_iocore
, startoffset_fsb
,
4445 endoffset_fsb
- startoffset_fsb
,
4446 0, 2, &firstfsb
, &free_list
, NULL
, &done
);
4452 * complete the transaction
4454 error
= xfs_bmap_finish(&tp
, &free_list
, firstfsb
, &committed
);
4459 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
, NULL
);
4460 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4465 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
4469 xfs_bmap_cancel(&free_list
);
4471 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
4472 xfs_iunlock(ip
, need_iolock
? (XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
) :
4478 * xfs_change_file_space()
4479 * This routine allocates or frees disk space for the given file.
4480 * The user specified parameters are checked for alignment and size
4489 xfs_change_file_space(
4503 xfs_off_t startoffset
;
4509 vp
= BHV_TO_VNODE(bdp
);
4510 vn_trace_entry(vp
, __FUNCTION__
, (inst_t
*)__return_address
);
4512 ip
= XFS_BHVTOI(bdp
);
4516 * must be a regular file and have write permission
4519 return XFS_ERROR(EINVAL
);
4521 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
4523 if ((error
= xfs_iaccess(ip
, S_IWUSR
, credp
))) {
4524 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
4528 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
4530 switch (bf
->l_whence
) {
4531 case 0: /*SEEK_SET*/
4533 case 1: /*SEEK_CUR*/
4534 bf
->l_start
+= offset
;
4536 case 2: /*SEEK_END*/
4537 bf
->l_start
+= ip
->i_d
.di_size
;
4540 return XFS_ERROR(EINVAL
);
4543 llen
= bf
->l_len
> 0 ? bf
->l_len
- 1 : bf
->l_len
;
4545 if ( (bf
->l_start
< 0)
4546 || (bf
->l_start
> XFS_MAXIOFFSET(mp
))
4547 || (bf
->l_start
+ llen
< 0)
4548 || (bf
->l_start
+ llen
> XFS_MAXIOFFSET(mp
)))
4549 return XFS_ERROR(EINVAL
);
4553 startoffset
= bf
->l_start
;
4554 fsize
= ip
->i_d
.di_size
;
4557 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
4559 * These calls do NOT zero the data space allocated to the file,
4560 * nor do they change the file size.
4562 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
4564 * These calls cause the new file data to be zeroed and the file
4565 * size to be changed.
4567 setprealloc
= clrprealloc
= 0;
4570 case XFS_IOC_RESVSP
:
4571 case XFS_IOC_RESVSP64
:
4572 error
= xfs_alloc_file_space(ip
, startoffset
, bf
->l_len
,
4579 case XFS_IOC_UNRESVSP
:
4580 case XFS_IOC_UNRESVSP64
:
4581 if ((error
= xfs_free_file_space(ip
, startoffset
, bf
->l_len
,
4586 case XFS_IOC_ALLOCSP
:
4587 case XFS_IOC_ALLOCSP64
:
4588 case XFS_IOC_FREESP
:
4589 case XFS_IOC_FREESP64
:
4590 if (startoffset
> fsize
) {
4591 error
= xfs_alloc_file_space(ip
, fsize
,
4592 startoffset
- fsize
, 0, attr_flags
);
4597 va
.va_mask
= XFS_AT_SIZE
;
4598 va
.va_size
= startoffset
;
4600 error
= xfs_setattr(bdp
, &va
, attr_flags
, credp
);
4610 return XFS_ERROR(EINVAL
);
4614 * update the inode timestamp, mode, and prealloc flag bits
4616 tp
= xfs_trans_alloc(mp
, XFS_TRANS_WRITEID
);
4618 if ((error
= xfs_trans_reserve(tp
, 0, XFS_WRITEID_LOG_RES(mp
),
4621 xfs_trans_cancel(tp
, 0);
4625 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
4627 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
4628 xfs_trans_ihold(tp
, ip
);
4630 if ((attr_flags
& ATTR_DMI
) == 0) {
4631 ip
->i_d
.di_mode
&= ~S_ISUID
;
4634 * Note that we don't have to worry about mandatory
4635 * file locking being disabled here because we only
4636 * clear the S_ISGID bit if the Group execute bit is
4637 * on, but if it was on then mandatory locking wouldn't
4638 * have been enabled.
4640 if (ip
->i_d
.di_mode
& S_IXGRP
)
4641 ip
->i_d
.di_mode
&= ~S_ISGID
;
4643 xfs_ichgtime(ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
4646 ip
->i_d
.di_flags
|= XFS_DIFLAG_PREALLOC
;
4647 else if (clrprealloc
)
4648 ip
->i_d
.di_flags
&= ~XFS_DIFLAG_PREALLOC
;
4650 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4651 xfs_trans_set_sync(tp
);
4653 error
= xfs_trans_commit(tp
, 0, NULL
);
4655 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
4660 vnodeops_t xfs_vnodeops
= {
4661 BHV_IDENTITY_INIT(VN_BHV_XFS
,VNODE_POSITION_XFS
),
4662 .vop_open
= xfs_open
,
4663 .vop_close
= xfs_close
,
4664 .vop_read
= xfs_read
,
4665 #ifdef HAVE_SENDFILE
4666 .vop_sendfile
= xfs_sendfile
,
4669 .vop_splice_read
= xfs_splice_read
,
4670 .vop_splice_write
= xfs_splice_write
,
4672 .vop_write
= xfs_write
,
4673 .vop_ioctl
= xfs_ioctl
,
4674 .vop_getattr
= xfs_getattr
,
4675 .vop_setattr
= xfs_setattr
,
4676 .vop_access
= xfs_access
,
4677 .vop_lookup
= xfs_lookup
,
4678 .vop_create
= xfs_create
,
4679 .vop_remove
= xfs_remove
,
4680 .vop_link
= xfs_link
,
4681 .vop_rename
= xfs_rename
,
4682 .vop_mkdir
= xfs_mkdir
,
4683 .vop_rmdir
= xfs_rmdir
,
4684 .vop_readdir
= xfs_readdir
,
4685 .vop_symlink
= xfs_symlink
,
4686 .vop_readlink
= xfs_readlink
,
4687 .vop_fsync
= xfs_fsync
,
4688 .vop_inactive
= xfs_inactive
,
4689 .vop_fid2
= xfs_fid2
,
4690 .vop_rwlock
= xfs_rwlock
,
4691 .vop_rwunlock
= xfs_rwunlock
,
4692 .vop_bmap
= xfs_bmap
,
4693 .vop_reclaim
= xfs_reclaim
,
4694 .vop_attr_get
= xfs_attr_get
,
4695 .vop_attr_set
= xfs_attr_set
,
4696 .vop_attr_remove
= xfs_attr_remove
,
4697 .vop_attr_list
= xfs_attr_list
,
4698 .vop_link_removed
= (vop_link_removed_t
)fs_noval
,
4699 .vop_vnode_change
= (vop_vnode_change_t
)fs_noval
,
4700 .vop_tosspages
= fs_tosspages
,
4701 .vop_flushinval_pages
= fs_flushinval_pages
,
4702 .vop_flush_pages
= fs_flush_pages
,
4703 .vop_release
= xfs_release
,
4704 .vop_iflush
= xfs_inode_flush
,