2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_types.h"
25 #include "xfs_trans.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_da_btree.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_inode_item.h"
40 #include "xfs_itable.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_alloc.h"
47 #include "xfs_error.h"
48 #include "xfs_quota.h"
49 #include "xfs_utils.h"
50 #include "xfs_rtalloc.h"
51 #include "xfs_trans_space.h"
52 #include "xfs_log_priv.h"
53 #include "xfs_filestream.h"
54 #include "xfs_vnodeops.h"
62 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
63 return XFS_ERROR(EIO
);
66 * If it's a directory with any blocks, read-ahead block 0
67 * as we're almost certain to have the next operation be a read there.
69 if (S_ISDIR(ip
->i_d
.di_mode
) && ip
->i_d
.di_nextents
> 0) {
70 mode
= xfs_ilock_map_shared(ip
);
71 if (ip
->i_d
.di_nextents
> 0)
72 (void)xfs_da_reada_buf(NULL
, ip
, 0, XFS_DATA_FORK
);
73 xfs_iunlock(ip
, mode
);
88 xfs_mount_t
*mp
= ip
->i_mount
;
97 xfs_prid_t projid
=0, iprojid
=0;
98 struct xfs_dquot
*udqp
, *gdqp
, *olddquot1
, *olddquot2
;
102 xfs_itrace_entry(ip
);
104 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
105 return XFS_ERROR(EROFS
);
108 * Cannot set certain attributes.
111 if (mask
& XFS_AT_NOSET
) {
112 return XFS_ERROR(EINVAL
);
115 if (XFS_FORCED_SHUTDOWN(mp
))
116 return XFS_ERROR(EIO
);
119 * Timestamps do not need to be logged and hence do not
120 * need to be done within a transaction.
122 if (mask
& XFS_AT_UPDTIMES
) {
123 ASSERT((mask
& ~XFS_AT_UPDTIMES
) == 0);
124 timeflags
= ((mask
& XFS_AT_UPDATIME
) ? XFS_ICHGTIME_ACC
: 0) |
125 ((mask
& XFS_AT_UPDCTIME
) ? XFS_ICHGTIME_CHG
: 0) |
126 ((mask
& XFS_AT_UPDMTIME
) ? XFS_ICHGTIME_MOD
: 0);
127 xfs_ichgtime(ip
, timeflags
);
131 olddquot1
= olddquot2
= NULL
;
135 * If disk quotas is on, we make sure that the dquots do exist on disk,
136 * before we start any other transactions. Trying to do this later
137 * is messy. We don't care to take a readlock to look at the ids
138 * in inode here, because we can't hold it across the trans_reserve.
139 * If the IDs do change before we take the ilock, we're covered
140 * because the i_*dquot fields will get updated anyway.
142 if (XFS_IS_QUOTA_ON(mp
) &&
143 (mask
& (XFS_AT_UID
|XFS_AT_GID
|XFS_AT_PROJID
))) {
146 if ((mask
& XFS_AT_UID
) && XFS_IS_UQUOTA_ON(mp
)) {
148 qflags
|= XFS_QMOPT_UQUOTA
;
150 uid
= ip
->i_d
.di_uid
;
152 if ((mask
& XFS_AT_GID
) && XFS_IS_GQUOTA_ON(mp
)) {
154 qflags
|= XFS_QMOPT_GQUOTA
;
156 gid
= ip
->i_d
.di_gid
;
158 if ((mask
& XFS_AT_PROJID
) && XFS_IS_PQUOTA_ON(mp
)) {
159 projid
= vap
->va_projid
;
160 qflags
|= XFS_QMOPT_PQUOTA
;
162 projid
= ip
->i_d
.di_projid
;
165 * We take a reference when we initialize udqp and gdqp,
166 * so it is important that we never blindly double trip on
167 * the same variable. See xfs_create() for an example.
169 ASSERT(udqp
== NULL
);
170 ASSERT(gdqp
== NULL
);
171 code
= XFS_QM_DQVOPALLOC(mp
, ip
, uid
, gid
, projid
, qflags
,
178 * For the other attributes, we acquire the inode lock and
179 * first do an error checking pass.
182 lock_flags
= XFS_ILOCK_EXCL
;
183 if (flags
& ATTR_NOLOCK
)
185 if (!(mask
& XFS_AT_SIZE
)) {
186 if ((mask
!= (XFS_AT_CTIME
|XFS_AT_ATIME
|XFS_AT_MTIME
)) ||
187 (mp
->m_flags
& XFS_MOUNT_WSYNC
)) {
188 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SETATTR_NOT_SIZE
);
190 if ((code
= xfs_trans_reserve(tp
, 0,
191 XFS_ICHANGE_LOG_RES(mp
), 0,
198 if (DM_EVENT_ENABLED(ip
, DM_EVENT_TRUNCATE
) &&
199 !(flags
& ATTR_DMI
)) {
200 int dmflags
= AT_DELAY_FLAG(flags
) | DM_SEM_FLAG_WR
;
201 code
= XFS_SEND_DATA(mp
, DM_EVENT_TRUNCATE
, ip
,
202 vap
->va_size
, 0, dmflags
, NULL
);
209 lock_flags
|= XFS_IOLOCK_EXCL
;
212 xfs_ilock(ip
, lock_flags
);
214 /* boolean: are we the file owner? */
215 file_owner
= (current_fsuid(credp
) == ip
->i_d
.di_uid
);
218 * Change various properties of a file.
219 * Only the owner or users with CAP_FOWNER
220 * capability may do these things.
223 (XFS_AT_MODE
|XFS_AT_XFLAGS
|XFS_AT_EXTSIZE
|XFS_AT_UID
|
224 XFS_AT_GID
|XFS_AT_PROJID
)) {
226 * CAP_FOWNER overrides the following restrictions:
228 * The user ID of the calling process must be equal
229 * to the file owner ID, except in cases where the
230 * CAP_FSETID capability is applicable.
232 if (!file_owner
&& !capable(CAP_FOWNER
)) {
233 code
= XFS_ERROR(EPERM
);
238 * CAP_FSETID overrides the following restrictions:
240 * The effective user ID of the calling process shall match
241 * the file owner when setting the set-user-ID and
242 * set-group-ID bits on that file.
244 * The effective group ID or one of the supplementary group
245 * IDs of the calling process shall match the group owner of
246 * the file when setting the set-group-ID bit on that file
248 if (mask
& XFS_AT_MODE
) {
251 if ((vap
->va_mode
& S_ISUID
) && !file_owner
)
253 if ((vap
->va_mode
& S_ISGID
) &&
254 !in_group_p((gid_t
)ip
->i_d
.di_gid
))
257 /* Linux allows this, Irix doesn't. */
258 if ((vap
->va_mode
& S_ISVTX
) && !S_ISDIR(ip
->i_d
.di_mode
))
261 if (m
&& !capable(CAP_FSETID
))
267 * Change file ownership. Must be the owner or privileged.
268 * If the system was configured with the "restricted_chown"
269 * option, the owner is not permitted to give away the file,
270 * and can change the group id only to a group of which he
271 * or she is a member.
273 if (mask
& (XFS_AT_UID
|XFS_AT_GID
|XFS_AT_PROJID
)) {
275 * These IDs could have changed since we last looked at them.
276 * But, we're assured that if the ownership did change
277 * while we didn't have the inode locked, inode's dquot(s)
278 * would have changed also.
280 iuid
= ip
->i_d
.di_uid
;
281 iprojid
= ip
->i_d
.di_projid
;
282 igid
= ip
->i_d
.di_gid
;
283 gid
= (mask
& XFS_AT_GID
) ? vap
->va_gid
: igid
;
284 uid
= (mask
& XFS_AT_UID
) ? vap
->va_uid
: iuid
;
285 projid
= (mask
& XFS_AT_PROJID
) ? (xfs_prid_t
)vap
->va_projid
:
289 * CAP_CHOWN overrides the following restrictions:
291 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
292 * shall override the restriction that a process cannot
293 * change the user ID of a file it owns and the restriction
294 * that the group ID supplied to the chown() function
295 * shall be equal to either the group ID or one of the
296 * supplementary group IDs of the calling process.
298 if (restricted_chown
&&
299 (iuid
!= uid
|| (igid
!= gid
&&
300 !in_group_p((gid_t
)gid
))) &&
301 !capable(CAP_CHOWN
)) {
302 code
= XFS_ERROR(EPERM
);
306 * Do a quota reservation only if uid/projid/gid is actually
309 if ((XFS_IS_UQUOTA_ON(mp
) && iuid
!= uid
) ||
310 (XFS_IS_PQUOTA_ON(mp
) && iprojid
!= projid
) ||
311 (XFS_IS_GQUOTA_ON(mp
) && igid
!= gid
)) {
313 code
= XFS_QM_DQVOPCHOWNRESV(mp
, tp
, ip
, udqp
, gdqp
,
314 capable(CAP_FOWNER
) ?
315 XFS_QMOPT_FORCE_RES
: 0);
316 if (code
) /* out of quota */
322 * Truncate file. Must have write permission and not be a directory.
324 if (mask
& XFS_AT_SIZE
) {
325 /* Short circuit the truncate case for zero length files */
326 if ((vap
->va_size
== 0) &&
327 (ip
->i_size
== 0) && (ip
->i_d
.di_nextents
== 0)) {
328 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
329 lock_flags
&= ~XFS_ILOCK_EXCL
;
330 if (mask
& XFS_AT_CTIME
)
331 xfs_ichgtime(ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
336 if (S_ISDIR(ip
->i_d
.di_mode
)) {
337 code
= XFS_ERROR(EISDIR
);
339 } else if (!S_ISREG(ip
->i_d
.di_mode
)) {
340 code
= XFS_ERROR(EINVAL
);
344 * Make sure that the dquots are attached to the inode.
346 if ((code
= XFS_QM_DQATTACH(mp
, ip
, XFS_QMOPT_ILOCKED
)))
351 * Change file access or modified times.
353 if (mask
& (XFS_AT_ATIME
|XFS_AT_MTIME
)) {
355 if ((flags
& ATTR_UTIME
) &&
356 !capable(CAP_FOWNER
)) {
357 code
= XFS_ERROR(EPERM
);
364 * Change extent size or realtime flag.
366 if (mask
& (XFS_AT_EXTSIZE
|XFS_AT_XFLAGS
)) {
368 * Can't change extent size if any extents are allocated.
370 if (ip
->i_d
.di_nextents
&& (mask
& XFS_AT_EXTSIZE
) &&
371 ((ip
->i_d
.di_extsize
<< mp
->m_sb
.sb_blocklog
) !=
373 code
= XFS_ERROR(EINVAL
); /* EFBIG? */
378 * Can't change realtime flag if any extents are allocated.
380 if ((ip
->i_d
.di_nextents
|| ip
->i_delayed_blks
) &&
381 (mask
& XFS_AT_XFLAGS
) &&
382 (XFS_IS_REALTIME_INODE(ip
)) !=
383 (vap
->va_xflags
& XFS_XFLAG_REALTIME
)) {
384 code
= XFS_ERROR(EINVAL
); /* EFBIG? */
388 * Extent size must be a multiple of the appropriate block
389 * size, if set at all.
391 if ((mask
& XFS_AT_EXTSIZE
) && vap
->va_extsize
!= 0) {
394 if (XFS_IS_REALTIME_INODE(ip
) ||
395 ((mask
& XFS_AT_XFLAGS
) &&
396 (vap
->va_xflags
& XFS_XFLAG_REALTIME
))) {
397 size
= mp
->m_sb
.sb_rextsize
<<
398 mp
->m_sb
.sb_blocklog
;
400 size
= mp
->m_sb
.sb_blocksize
;
402 if (vap
->va_extsize
% size
) {
403 code
= XFS_ERROR(EINVAL
);
408 * If realtime flag is set then must have realtime data.
410 if ((mask
& XFS_AT_XFLAGS
) &&
411 (vap
->va_xflags
& XFS_XFLAG_REALTIME
)) {
412 if ((mp
->m_sb
.sb_rblocks
== 0) ||
413 (mp
->m_sb
.sb_rextsize
== 0) ||
414 (ip
->i_d
.di_extsize
% mp
->m_sb
.sb_rextsize
)) {
415 code
= XFS_ERROR(EINVAL
);
421 * Can't modify an immutable/append-only file unless
422 * we have appropriate permission.
424 if ((mask
& XFS_AT_XFLAGS
) &&
426 (XFS_DIFLAG_IMMUTABLE
|XFS_DIFLAG_APPEND
) ||
428 (XFS_XFLAG_IMMUTABLE
| XFS_XFLAG_APPEND
))) &&
429 !capable(CAP_LINUX_IMMUTABLE
)) {
430 code
= XFS_ERROR(EPERM
);
436 * Now we can make the changes. Before we join the inode
437 * to the transaction, if XFS_AT_SIZE is set then take care of
438 * the part of the truncation that must be done without the
439 * inode lock. This needs to be done before joining the inode
440 * to the transaction, because the inode cannot be unlocked
441 * once it is a part of the transaction.
443 if (mask
& XFS_AT_SIZE
) {
445 if ((vap
->va_size
> ip
->i_size
) &&
446 (flags
& ATTR_NOSIZETOK
) == 0) {
447 code
= xfs_igrow_start(ip
, vap
->va_size
, credp
);
449 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
452 * We are going to log the inode size change in this
453 * transaction so any previous writes that are beyond the on
454 * disk EOF and the new EOF that have not been written out need
455 * to be written here. If we do not write the data out, we
456 * expose ourselves to the null files problem.
458 * Only flush from the on disk size to the smaller of the in
459 * memory file size or the new size as that's the range we
460 * really care about here and prevents waiting for other data
461 * not within the range we care about here.
464 (ip
->i_size
!= ip
->i_d
.di_size
) &&
465 (vap
->va_size
> ip
->i_d
.di_size
)) {
466 code
= xfs_flush_pages(ip
,
467 ip
->i_d
.di_size
, vap
->va_size
,
468 XFS_B_ASYNC
, FI_NONE
);
471 /* wait for all I/O to complete */
475 code
= xfs_itruncate_data(ip
, vap
->va_size
);
478 lock_flags
&= ~XFS_ILOCK_EXCL
;
479 ASSERT(lock_flags
== XFS_IOLOCK_EXCL
);
482 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SETATTR_SIZE
);
483 if ((code
= xfs_trans_reserve(tp
, 0,
484 XFS_ITRUNCATE_LOG_RES(mp
), 0,
485 XFS_TRANS_PERM_LOG_RES
,
486 XFS_ITRUNCATE_LOG_COUNT
))) {
487 xfs_trans_cancel(tp
, 0);
489 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
492 commit_flags
= XFS_TRANS_RELEASE_LOG_RES
;
493 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
497 xfs_trans_ijoin(tp
, ip
, lock_flags
);
498 xfs_trans_ihold(tp
, ip
);
502 * Truncate file. Must have write permission and not be a directory.
504 if (mask
& XFS_AT_SIZE
) {
506 * Only change the c/mtime if we are changing the size
507 * or we are explicitly asked to change it. This handles
508 * the semantic difference between truncate() and ftruncate()
509 * as implemented in the VFS.
511 if (vap
->va_size
!= ip
->i_size
|| (mask
& XFS_AT_CTIME
))
512 timeflags
|= XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
;
514 if (vap
->va_size
> ip
->i_size
) {
515 xfs_igrow_finish(tp
, ip
, vap
->va_size
,
516 !(flags
& ATTR_DMI
));
517 } else if ((vap
->va_size
<= ip
->i_size
) ||
518 ((vap
->va_size
== 0) && ip
->i_d
.di_nextents
)) {
520 * signal a sync transaction unless
521 * we're truncating an already unlinked
522 * file on a wsync filesystem
524 code
= xfs_itruncate_finish(&tp
, ip
,
525 (xfs_fsize_t
)vap
->va_size
,
527 ((ip
->i_d
.di_nlink
!= 0 ||
528 !(mp
->m_flags
& XFS_MOUNT_WSYNC
))
533 * Truncated "down", so we're removing references
534 * to old data here - if we now delay flushing for
535 * a long time, we expose ourselves unduly to the
536 * notorious NULL files problem. So, we mark this
537 * vnode and flush it when the file is closed, and
538 * do not wait the usual (long) time for writeout.
540 xfs_iflags_set(ip
, XFS_ITRUNCATED
);
545 * Change file access modes.
547 if (mask
& XFS_AT_MODE
) {
548 ip
->i_d
.di_mode
&= S_IFMT
;
549 ip
->i_d
.di_mode
|= vap
->va_mode
& ~S_IFMT
;
551 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
552 timeflags
|= XFS_ICHGTIME_CHG
;
556 * Change file ownership. Must be the owner or privileged.
557 * If the system was configured with the "restricted_chown"
558 * option, the owner is not permitted to give away the file,
559 * and can change the group id only to a group of which he
560 * or she is a member.
562 if (mask
& (XFS_AT_UID
|XFS_AT_GID
|XFS_AT_PROJID
)) {
564 * CAP_FSETID overrides the following restrictions:
566 * The set-user-ID and set-group-ID bits of a file will be
567 * cleared upon successful return from chown()
569 if ((ip
->i_d
.di_mode
& (S_ISUID
|S_ISGID
)) &&
570 !capable(CAP_FSETID
)) {
571 ip
->i_d
.di_mode
&= ~(S_ISUID
|S_ISGID
);
575 * Change the ownerships and register quota modifications
576 * in the transaction.
579 if (XFS_IS_UQUOTA_ON(mp
)) {
580 ASSERT(mask
& XFS_AT_UID
);
582 olddquot1
= XFS_QM_DQVOPCHOWN(mp
, tp
, ip
,
583 &ip
->i_udquot
, udqp
);
585 ip
->i_d
.di_uid
= uid
;
588 if (XFS_IS_GQUOTA_ON(mp
)) {
589 ASSERT(!XFS_IS_PQUOTA_ON(mp
));
590 ASSERT(mask
& XFS_AT_GID
);
592 olddquot2
= XFS_QM_DQVOPCHOWN(mp
, tp
, ip
,
593 &ip
->i_gdquot
, gdqp
);
595 ip
->i_d
.di_gid
= gid
;
597 if (iprojid
!= projid
) {
598 if (XFS_IS_PQUOTA_ON(mp
)) {
599 ASSERT(!XFS_IS_GQUOTA_ON(mp
));
600 ASSERT(mask
& XFS_AT_PROJID
);
602 olddquot2
= XFS_QM_DQVOPCHOWN(mp
, tp
, ip
,
603 &ip
->i_gdquot
, gdqp
);
605 ip
->i_d
.di_projid
= projid
;
607 * We may have to rev the inode as well as
608 * the superblock version number since projids didn't
609 * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
611 if (ip
->i_d
.di_version
== XFS_DINODE_VERSION_1
)
612 xfs_bump_ino_vers2(tp
, ip
);
615 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
616 timeflags
|= XFS_ICHGTIME_CHG
;
621 * Change file access or modified times.
623 if (mask
& (XFS_AT_ATIME
|XFS_AT_MTIME
)) {
624 if (mask
& XFS_AT_ATIME
) {
625 ip
->i_d
.di_atime
.t_sec
= vap
->va_atime
.tv_sec
;
626 ip
->i_d
.di_atime
.t_nsec
= vap
->va_atime
.tv_nsec
;
627 ip
->i_update_core
= 1;
628 timeflags
&= ~XFS_ICHGTIME_ACC
;
630 if (mask
& XFS_AT_MTIME
) {
631 ip
->i_d
.di_mtime
.t_sec
= vap
->va_mtime
.tv_sec
;
632 ip
->i_d
.di_mtime
.t_nsec
= vap
->va_mtime
.tv_nsec
;
633 timeflags
&= ~XFS_ICHGTIME_MOD
;
634 timeflags
|= XFS_ICHGTIME_CHG
;
636 if (tp
&& (flags
& ATTR_UTIME
))
637 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
641 * Change XFS-added attributes.
643 if (mask
& (XFS_AT_EXTSIZE
|XFS_AT_XFLAGS
)) {
644 if (mask
& XFS_AT_EXTSIZE
) {
646 * Converting bytes to fs blocks.
648 ip
->i_d
.di_extsize
= vap
->va_extsize
>>
649 mp
->m_sb
.sb_blocklog
;
651 if (mask
& XFS_AT_XFLAGS
) {
654 /* can't set PREALLOC this way, just preserve it */
655 di_flags
= (ip
->i_d
.di_flags
& XFS_DIFLAG_PREALLOC
);
656 if (vap
->va_xflags
& XFS_XFLAG_IMMUTABLE
)
657 di_flags
|= XFS_DIFLAG_IMMUTABLE
;
658 if (vap
->va_xflags
& XFS_XFLAG_APPEND
)
659 di_flags
|= XFS_DIFLAG_APPEND
;
660 if (vap
->va_xflags
& XFS_XFLAG_SYNC
)
661 di_flags
|= XFS_DIFLAG_SYNC
;
662 if (vap
->va_xflags
& XFS_XFLAG_NOATIME
)
663 di_flags
|= XFS_DIFLAG_NOATIME
;
664 if (vap
->va_xflags
& XFS_XFLAG_NODUMP
)
665 di_flags
|= XFS_DIFLAG_NODUMP
;
666 if (vap
->va_xflags
& XFS_XFLAG_PROJINHERIT
)
667 di_flags
|= XFS_DIFLAG_PROJINHERIT
;
668 if (vap
->va_xflags
& XFS_XFLAG_NODEFRAG
)
669 di_flags
|= XFS_DIFLAG_NODEFRAG
;
670 if (vap
->va_xflags
& XFS_XFLAG_FILESTREAM
)
671 di_flags
|= XFS_DIFLAG_FILESTREAM
;
672 if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFDIR
) {
673 if (vap
->va_xflags
& XFS_XFLAG_RTINHERIT
)
674 di_flags
|= XFS_DIFLAG_RTINHERIT
;
675 if (vap
->va_xflags
& XFS_XFLAG_NOSYMLINKS
)
676 di_flags
|= XFS_DIFLAG_NOSYMLINKS
;
677 if (vap
->va_xflags
& XFS_XFLAG_EXTSZINHERIT
)
678 di_flags
|= XFS_DIFLAG_EXTSZINHERIT
;
679 } else if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) {
680 if (vap
->va_xflags
& XFS_XFLAG_REALTIME
)
681 di_flags
|= XFS_DIFLAG_REALTIME
;
682 if (vap
->va_xflags
& XFS_XFLAG_EXTSIZE
)
683 di_flags
|= XFS_DIFLAG_EXTSIZE
;
685 ip
->i_d
.di_flags
= di_flags
;
687 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
688 timeflags
|= XFS_ICHGTIME_CHG
;
692 * Change file inode change time only if XFS_AT_CTIME set
693 * AND we have been called by a DMI function.
696 if ( (flags
& ATTR_DMI
) && (mask
& XFS_AT_CTIME
) ) {
697 ip
->i_d
.di_ctime
.t_sec
= vap
->va_ctime
.tv_sec
;
698 ip
->i_d
.di_ctime
.t_nsec
= vap
->va_ctime
.tv_nsec
;
699 ip
->i_update_core
= 1;
700 timeflags
&= ~XFS_ICHGTIME_CHG
;
704 * Send out timestamp changes that need to be set to the
705 * current time. Not done when called by a DMI function.
707 if (timeflags
&& !(flags
& ATTR_DMI
))
708 xfs_ichgtime(ip
, timeflags
);
710 XFS_STATS_INC(xs_ig_attrchg
);
713 * If this is a synchronous mount, make sure that the
714 * transaction goes to disk before returning to the user.
715 * This is slightly sub-optimal in that truncates require
716 * two sync transactions instead of one for wsync filesystems.
717 * One for the truncate and one for the timestamps since we
718 * don't want to change the timestamps unless we're sure the
719 * truncate worked. Truncates are less than 1% of the laddis
720 * mix so this probably isn't worth the trouble to optimize.
724 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
725 xfs_trans_set_sync(tp
);
727 code
= xfs_trans_commit(tp
, commit_flags
);
730 xfs_iunlock(ip
, lock_flags
);
733 * Release any dquot(s) the inode had kept before chown.
735 XFS_QM_DQRELE(mp
, olddquot1
);
736 XFS_QM_DQRELE(mp
, olddquot2
);
737 XFS_QM_DQRELE(mp
, udqp
);
738 XFS_QM_DQRELE(mp
, gdqp
);
744 if (DM_EVENT_ENABLED(ip
, DM_EVENT_ATTRIBUTE
) &&
745 !(flags
& ATTR_DMI
)) {
746 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_ATTRIBUTE
, ip
, DM_RIGHT_NULL
,
747 NULL
, DM_RIGHT_NULL
, NULL
, NULL
,
748 0, 0, AT_DELAY_FLAG(flags
));
753 commit_flags
|= XFS_TRANS_ABORT
;
756 XFS_QM_DQRELE(mp
, udqp
);
757 XFS_QM_DQRELE(mp
, gdqp
);
759 xfs_trans_cancel(tp
, commit_flags
);
761 if (lock_flags
!= 0) {
762 xfs_iunlock(ip
, lock_flags
);
768 * The maximum pathlen is 1024 bytes. Since the minimum file system
769 * blocksize is 512 bytes, we can get a max of 2 extents back from
772 #define SYMLINK_MAPS 2
779 xfs_mount_t
*mp
= ip
->i_mount
;
780 int pathlen
= ip
->i_d
.di_size
;
781 int nmaps
= SYMLINK_MAPS
;
782 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
789 error
= xfs_bmapi(NULL
, ip
, 0, XFS_B_TO_FSB(mp
, pathlen
), 0, NULL
, 0,
790 mval
, &nmaps
, NULL
, NULL
);
794 for (n
= 0; n
< nmaps
; n
++) {
795 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
796 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
798 bp
= xfs_buf_read(mp
->m_ddev_targp
, d
, BTOBB(byte_cnt
), 0);
799 error
= XFS_BUF_GETERROR(bp
);
801 xfs_ioerror_alert("xfs_readlink",
802 ip
->i_mount
, bp
, XFS_BUF_ADDR(bp
));
806 if (pathlen
< byte_cnt
)
810 memcpy(link
, XFS_BUF_PTR(bp
), byte_cnt
);
814 link
[ip
->i_d
.di_size
] = '\0';
826 xfs_mount_t
*mp
= ip
->i_mount
;
830 xfs_itrace_entry(ip
);
832 if (XFS_FORCED_SHUTDOWN(mp
))
833 return XFS_ERROR(EIO
);
835 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
837 ASSERT((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
);
838 ASSERT(ip
->i_d
.di_size
<= MAXPATHLEN
);
840 pathlen
= ip
->i_d
.di_size
;
844 if (ip
->i_df
.if_flags
& XFS_IFINLINE
) {
845 memcpy(link
, ip
->i_df
.if_u1
.if_data
, pathlen
);
846 link
[pathlen
] = '\0';
848 error
= xfs_readlink_bmap(ip
, link
);
852 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
859 * This is called to sync the inode and its data out to disk.
860 * We need to hold the I/O lock while flushing the data, and
861 * the inode lock while flushing the inode. The inode lock CANNOT
862 * be held while flushing the data, so acquire after we're done
874 int log_flushed
= 0, changed
= 1;
876 xfs_itrace_entry(ip
);
878 ASSERT(start
>= 0 && stop
>= -1);
880 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
881 return XFS_ERROR(EIO
);
883 if (flag
& FSYNC_DATA
)
884 filemap_fdatawait(vn_to_inode(XFS_ITOV(ip
))->i_mapping
);
887 * We always need to make sure that the required inode state
888 * is safe on disk. The vnode might be clean but because
889 * of committed transactions that haven't hit the disk yet.
890 * Likewise, there could be unflushed non-transactional
891 * changes to the inode core that have to go to disk.
893 * The following code depends on one assumption: that
894 * any transaction that changes an inode logs the core
895 * because it has to change some field in the inode core
896 * (typically nextents or nblocks). That assumption
897 * implies that any transactions against an inode will
898 * catch any non-transactional updates. If inode-altering
899 * transactions exist that violate this assumption, the
900 * code breaks. Right now, it figures that if the involved
901 * update_* field is clear and the inode is unpinned, the
902 * inode is clean. Either it's been flushed or it's been
903 * committed and the commit has hit the disk unpinning the inode.
904 * (Note that xfs_inode_item_format() called at commit clears
905 * the update_* fields.)
907 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
909 /* If we are flushing data then we care about update_size
910 * being set, otherwise we care about update_core
912 if ((flag
& FSYNC_DATA
) ?
913 (ip
->i_update_size
== 0) :
914 (ip
->i_update_core
== 0)) {
916 * Timestamps/size haven't changed since last inode
917 * flush or inode transaction commit. That means
918 * either nothing got written or a transaction
919 * committed which caught the updates. If the
920 * latter happened and the transaction hasn't
921 * hit the disk yet, the inode will be still
922 * be pinned. If it is, force the log.
925 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
927 if (xfs_ipincount(ip
)) {
928 _xfs_log_force(ip
->i_mount
, (xfs_lsn_t
)0,
935 * If the inode is not pinned and nothing
936 * has changed we don't need to flush the
944 * Kick off a transaction to log the inode
945 * core to get the updates. Make it
946 * sync if FSYNC_WAIT is passed in (which
947 * is done by everybody but specfs). The
948 * sync transaction will also force the log.
950 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
951 tp
= xfs_trans_alloc(ip
->i_mount
, XFS_TRANS_FSYNC_TS
);
952 if ((error
= xfs_trans_reserve(tp
, 0,
953 XFS_FSYNC_TS_LOG_RES(ip
->i_mount
),
955 xfs_trans_cancel(tp
, 0);
958 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
961 * Note - it's possible that we might have pushed
962 * ourselves out of the way during trans_reserve
963 * which would flush the inode. But there's no
964 * guarantee that the inode buffer has actually
965 * gone out yet (it's delwri). Plus the buffer
966 * could be pinned anyway if it's part of an
967 * inode in another recent transaction. So we
968 * play it safe and fire off the transaction anyway.
970 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
971 xfs_trans_ihold(tp
, ip
);
972 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
973 if (flag
& FSYNC_WAIT
)
974 xfs_trans_set_sync(tp
);
975 error
= _xfs_trans_commit(tp
, 0, &log_flushed
);
977 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
980 if ((ip
->i_mount
->m_flags
& XFS_MOUNT_BARRIER
) && changed
) {
982 * If the log write didn't issue an ordered tag we need
983 * to flush the disk cache for the data device now.
986 xfs_blkdev_issue_flush(ip
->i_mount
->m_ddev_targp
);
989 * If this inode is on the RT dev we need to flush that
992 if (XFS_IS_REALTIME_INODE(ip
))
993 xfs_blkdev_issue_flush(ip
->i_mount
->m_rtdev_targp
);
1000 * This is called by xfs_inactive to free any blocks beyond eof
1001 * when the link count isn't zero and by xfs_dm_punch_hole() when
1002 * punching a hole to EOF.
1012 xfs_fileoff_t end_fsb
;
1013 xfs_fileoff_t last_fsb
;
1014 xfs_filblks_t map_len
;
1016 xfs_bmbt_irec_t imap
;
1017 int use_iolock
= (flags
& XFS_FREE_EOF_LOCK
);
1020 * Figure out if there are any blocks beyond the end
1021 * of the file. If not, then there is nothing to do.
1023 end_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)ip
->i_size
));
1024 last_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_MAXIOFFSET(mp
));
1025 map_len
= last_fsb
- end_fsb
;
1030 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
1031 error
= xfs_bmapi(NULL
, ip
, end_fsb
, map_len
, 0,
1032 NULL
, 0, &imap
, &nimaps
, NULL
, NULL
);
1033 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
1035 if (!error
&& (nimaps
!= 0) &&
1036 (imap
.br_startblock
!= HOLESTARTBLOCK
||
1037 ip
->i_delayed_blks
)) {
1039 * Attach the dquots to the inode up front.
1041 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
1045 * There are blocks after the end of file.
1046 * Free them up now by truncating the file to
1049 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1052 * Do the xfs_itruncate_start() call before
1053 * reserving any log space because
1054 * itruncate_start will call into the buffer
1055 * cache and we can't
1056 * do that within a transaction.
1059 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
1060 error
= xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
,
1063 xfs_trans_cancel(tp
, 0);
1065 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1069 error
= xfs_trans_reserve(tp
, 0,
1070 XFS_ITRUNCATE_LOG_RES(mp
),
1071 0, XFS_TRANS_PERM_LOG_RES
,
1072 XFS_ITRUNCATE_LOG_COUNT
);
1074 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1075 xfs_trans_cancel(tp
, 0);
1076 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1080 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1081 xfs_trans_ijoin(tp
, ip
,
1084 xfs_trans_ihold(tp
, ip
);
1086 error
= xfs_itruncate_finish(&tp
, ip
,
1091 * If we get an error at this point we
1092 * simply don't bother truncating the file.
1095 xfs_trans_cancel(tp
,
1096 (XFS_TRANS_RELEASE_LOG_RES
|
1099 error
= xfs_trans_commit(tp
,
1100 XFS_TRANS_RELEASE_LOG_RES
);
1102 xfs_iunlock(ip
, (use_iolock
? (XFS_IOLOCK_EXCL
|XFS_ILOCK_EXCL
)
1109 * Free a symlink that has blocks associated with it.
1112 xfs_inactive_symlink_rmt(
1120 xfs_fsblock_t first_block
;
1121 xfs_bmap_free_t free_list
;
1124 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
1132 ASSERT(ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
));
1134 * We're freeing a symlink that has some
1135 * blocks allocated to it. Free the
1136 * blocks here. We know that we've got
1137 * either 1 or 2 extents and that we can
1138 * free them all in one bunmapi call.
1140 ASSERT(ip
->i_d
.di_nextents
> 0 && ip
->i_d
.di_nextents
<= 2);
1141 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
1142 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
1143 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1144 xfs_trans_cancel(tp
, 0);
1149 * Lock the inode, fix the size, and join it to the transaction.
1150 * Hold it so in the normal path, we still have it locked for
1151 * the second transaction. In the error paths we need it
1152 * held so the cancel won't rele it, see below.
1154 xfs_ilock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1155 size
= (int)ip
->i_d
.di_size
;
1156 ip
->i_d
.di_size
= 0;
1157 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1158 xfs_trans_ihold(tp
, ip
);
1159 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1161 * Find the block(s) so we can inval and unmap them.
1164 XFS_BMAP_INIT(&free_list
, &first_block
);
1165 nmaps
= ARRAY_SIZE(mval
);
1166 if ((error
= xfs_bmapi(tp
, ip
, 0, XFS_B_TO_FSB(mp
, size
),
1167 XFS_BMAPI_METADATA
, &first_block
, 0, mval
, &nmaps
,
1171 * Invalidate the block(s).
1173 for (i
= 0; i
< nmaps
; i
++) {
1174 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
1175 XFS_FSB_TO_DADDR(mp
, mval
[i
].br_startblock
),
1176 XFS_FSB_TO_BB(mp
, mval
[i
].br_blockcount
), 0);
1177 xfs_trans_binval(tp
, bp
);
1180 * Unmap the dead block(s) to the free_list.
1182 if ((error
= xfs_bunmapi(tp
, ip
, 0, size
, XFS_BMAPI_METADATA
, nmaps
,
1183 &first_block
, &free_list
, NULL
, &done
)))
1187 * Commit the first transaction. This logs the EFI and the inode.
1189 if ((error
= xfs_bmap_finish(&tp
, &free_list
, &committed
)))
1192 * The transaction must have been committed, since there were
1193 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1194 * The new tp has the extent freeing and EFDs.
1198 * The first xact was committed, so add the inode to the new one.
1199 * Mark it dirty so it will be logged and moved forward in the log as
1200 * part of every commit.
1202 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1203 xfs_trans_ihold(tp
, ip
);
1204 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1206 * Get a new, empty transaction to return to our caller.
1208 ntp
= xfs_trans_dup(tp
);
1210 * Commit the transaction containing extent freeing and EFDs.
1211 * If we get an error on the commit here or on the reserve below,
1212 * we need to unlock the inode since the new transaction doesn't
1213 * have the inode attached.
1215 error
= xfs_trans_commit(tp
, 0);
1218 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1222 * Remove the memory for extent descriptions (just bookkeeping).
1224 if (ip
->i_df
.if_bytes
)
1225 xfs_idata_realloc(ip
, -ip
->i_df
.if_bytes
, XFS_DATA_FORK
);
1226 ASSERT(ip
->i_df
.if_bytes
== 0);
1228 * Put an itruncate log reservation in the new transaction
1231 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
1232 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
1233 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1237 * Return with the inode locked but not joined to the transaction.
1243 xfs_bmap_cancel(&free_list
);
1246 * Have to come here with the inode locked and either
1247 * (held and in the transaction) or (not in the transaction).
1248 * If the inode isn't held then cancel would iput it, but
1249 * that's wrong since this is inactive and the vnode ref
1250 * count is 0 already.
1251 * Cancel won't do anything to the inode if held, but it still
1252 * needs to be locked until the cancel is done, if it was
1253 * joined to the transaction.
1255 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1256 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1263 xfs_inactive_symlink_local(
1269 ASSERT(ip
->i_d
.di_size
<= XFS_IFORK_DSIZE(ip
));
1271 * We're freeing a symlink which fit into
1272 * the inode. Just free the memory used
1273 * to hold the old symlink.
1275 error
= xfs_trans_reserve(*tpp
, 0,
1276 XFS_ITRUNCATE_LOG_RES(ip
->i_mount
),
1277 0, XFS_TRANS_PERM_LOG_RES
,
1278 XFS_ITRUNCATE_LOG_COUNT
);
1281 xfs_trans_cancel(*tpp
, 0);
1285 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1288 * Zero length symlinks _can_ exist.
1290 if (ip
->i_df
.if_bytes
> 0) {
1291 xfs_idata_realloc(ip
,
1292 -(ip
->i_df
.if_bytes
),
1294 ASSERT(ip
->i_df
.if_bytes
== 0);
1308 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
1311 ASSERT(ip
->i_d
.di_forkoff
!= 0);
1312 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1313 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1317 error
= xfs_attr_inactive(ip
);
1321 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1322 error
= xfs_trans_reserve(tp
, 0,
1323 XFS_IFREE_LOG_RES(mp
),
1324 0, XFS_TRANS_PERM_LOG_RES
,
1325 XFS_INACTIVE_LOG_COUNT
);
1329 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1330 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1331 xfs_trans_ihold(tp
, ip
);
1332 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
1334 ASSERT(ip
->i_d
.di_anextents
== 0);
1340 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1341 xfs_trans_cancel(tp
, 0);
1344 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1352 bhv_vnode_t
*vp
= XFS_ITOV(ip
);
1353 xfs_mount_t
*mp
= ip
->i_mount
;
1356 if (!S_ISREG(ip
->i_d
.di_mode
) || (ip
->i_d
.di_mode
== 0))
1359 /* If this is a read-only mount, don't do this (would generate I/O) */
1360 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
1363 if (!XFS_FORCED_SHUTDOWN(mp
)) {
1367 * If we are using filestreams, and we have an unlinked
1368 * file that we are processing the last close on, then nothing
1369 * will be able to reopen and write to this file. Purge this
1370 * inode from the filestreams cache so that it doesn't delay
1371 * teardown of the inode.
1373 if ((ip
->i_d
.di_nlink
== 0) && xfs_inode_is_filestream(ip
))
1374 xfs_filestream_deassociate(ip
);
1377 * If we previously truncated this file and removed old data
1378 * in the process, we want to initiate "early" writeout on
1379 * the last close. This is an attempt to combat the notorious
1380 * NULL files problem which is particularly noticable from a
1381 * truncate down, buffered (re-)write (delalloc), followed by
1382 * a crash. What we are effectively doing here is
1383 * significantly reducing the time window where we'd otherwise
1384 * be exposed to that problem.
1386 truncated
= xfs_iflags_test_and_clear(ip
, XFS_ITRUNCATED
);
1387 if (truncated
&& VN_DIRTY(vp
) && ip
->i_delayed_blks
> 0)
1388 xfs_flush_pages(ip
, 0, -1, XFS_B_ASYNC
, FI_NONE
);
1391 if (ip
->i_d
.di_nlink
!= 0) {
1392 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
1393 ((ip
->i_size
> 0) || (VN_CACHED(vp
) > 0 ||
1394 ip
->i_delayed_blks
> 0)) &&
1395 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
)) &&
1396 (!(ip
->i_d
.di_flags
&
1397 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)))) {
1398 error
= xfs_free_eofblocks(mp
, ip
, XFS_FREE_EOF_LOCK
);
1410 * This is called when the vnode reference count for the vnode
1411 * goes to zero. If the file has been unlinked, then it must
1412 * now be truncated. Also, we clear all of the read-ahead state
1413 * kept for the inode here since the file is now closed.
1419 bhv_vnode_t
*vp
= XFS_ITOV(ip
);
1420 xfs_bmap_free_t free_list
;
1421 xfs_fsblock_t first_block
;
1428 xfs_itrace_entry(ip
);
1431 * If the inode is already free, then there can be nothing
1434 if (ip
->i_d
.di_mode
== 0 || VN_BAD(vp
)) {
1435 ASSERT(ip
->i_df
.if_real_bytes
== 0);
1436 ASSERT(ip
->i_df
.if_broot_bytes
== 0);
1437 return VN_INACTIVE_CACHE
;
1441 * Only do a truncate if it's a regular file with
1442 * some actual space in it. It's OK to look at the
1443 * inode's fields without the lock because we're the
1444 * only one with a reference to the inode.
1446 truncate
= ((ip
->i_d
.di_nlink
== 0) &&
1447 ((ip
->i_d
.di_size
!= 0) || (ip
->i_size
!= 0) ||
1448 (ip
->i_d
.di_nextents
> 0) || (ip
->i_delayed_blks
> 0)) &&
1449 ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
));
1453 if (ip
->i_d
.di_nlink
== 0 && DM_EVENT_ENABLED(ip
, DM_EVENT_DESTROY
))
1454 XFS_SEND_DESTROY(mp
, ip
, DM_RIGHT_NULL
);
1458 /* If this is a read-only mount, don't do this (would generate I/O) */
1459 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
1462 if (ip
->i_d
.di_nlink
!= 0) {
1463 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
1464 ((ip
->i_size
> 0) || (VN_CACHED(vp
) > 0 ||
1465 ip
->i_delayed_blks
> 0)) &&
1466 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
) &&
1467 (!(ip
->i_d
.di_flags
&
1468 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)) ||
1469 (ip
->i_delayed_blks
!= 0)))) {
1470 error
= xfs_free_eofblocks(mp
, ip
, XFS_FREE_EOF_LOCK
);
1472 return VN_INACTIVE_CACHE
;
1477 ASSERT(ip
->i_d
.di_nlink
== 0);
1479 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
1480 return VN_INACTIVE_CACHE
;
1482 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1485 * Do the xfs_itruncate_start() call before
1486 * reserving any log space because itruncate_start
1487 * will call into the buffer cache and we can't
1488 * do that within a transaction.
1490 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
1492 error
= xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
, 0);
1494 xfs_trans_cancel(tp
, 0);
1495 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1496 return VN_INACTIVE_CACHE
;
1499 error
= xfs_trans_reserve(tp
, 0,
1500 XFS_ITRUNCATE_LOG_RES(mp
),
1501 0, XFS_TRANS_PERM_LOG_RES
,
1502 XFS_ITRUNCATE_LOG_COUNT
);
1504 /* Don't call itruncate_cleanup */
1505 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1506 xfs_trans_cancel(tp
, 0);
1507 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1508 return VN_INACTIVE_CACHE
;
1511 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1512 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1513 xfs_trans_ihold(tp
, ip
);
1516 * normally, we have to run xfs_itruncate_finish sync.
1517 * But if filesystem is wsync and we're in the inactive
1518 * path, then we know that nlink == 0, and that the
1519 * xaction that made nlink == 0 is permanently committed
1520 * since xfs_remove runs as a synchronous transaction.
1522 error
= xfs_itruncate_finish(&tp
, ip
, 0, XFS_DATA_FORK
,
1523 (!(mp
->m_flags
& XFS_MOUNT_WSYNC
) ? 1 : 0));
1526 xfs_trans_cancel(tp
,
1527 XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1528 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1529 return VN_INACTIVE_CACHE
;
1531 } else if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
) {
1534 * If we get an error while cleaning up a
1535 * symlink we bail out.
1537 error
= (ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
)) ?
1538 xfs_inactive_symlink_rmt(ip
, &tp
) :
1539 xfs_inactive_symlink_local(ip
, &tp
);
1543 return VN_INACTIVE_CACHE
;
1546 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1547 xfs_trans_ihold(tp
, ip
);
1549 error
= xfs_trans_reserve(tp
, 0,
1550 XFS_IFREE_LOG_RES(mp
),
1551 0, XFS_TRANS_PERM_LOG_RES
,
1552 XFS_INACTIVE_LOG_COUNT
);
1554 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1555 xfs_trans_cancel(tp
, 0);
1556 return VN_INACTIVE_CACHE
;
1559 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1560 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1561 xfs_trans_ihold(tp
, ip
);
1565 * If there are attributes associated with the file
1566 * then blow them away now. The code calls a routine
1567 * that recursively deconstructs the attribute fork.
1568 * We need to just commit the current transaction
1569 * because we can't use it for xfs_attr_inactive().
1571 if (ip
->i_d
.di_anextents
> 0) {
1572 error
= xfs_inactive_attrs(ip
, &tp
);
1574 * If we got an error, the transaction is already
1575 * cancelled, and the inode is unlocked. Just get out.
1578 return VN_INACTIVE_CACHE
;
1579 } else if (ip
->i_afp
) {
1580 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
1586 XFS_BMAP_INIT(&free_list
, &first_block
);
1587 error
= xfs_ifree(tp
, ip
, &free_list
);
1590 * If we fail to free the inode, shut down. The cancel
1591 * might do that, we need to make sure. Otherwise the
1592 * inode might be lost for a long time or forever.
1594 if (!XFS_FORCED_SHUTDOWN(mp
)) {
1596 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1597 error
, mp
->m_fsname
);
1598 xfs_force_shutdown(mp
, SHUTDOWN_META_IO_ERROR
);
1600 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
1603 * Credit the quota account(s). The inode is gone.
1605 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, XFS_TRANS_DQ_ICOUNT
, -1);
1608 * Just ignore errors at this point. There is nothing we can
1609 * do except to try to keep going. Make sure it's not a silent
1612 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1614 xfs_fs_cmn_err(CE_NOTE
, mp
, "xfs_inactive: "
1615 "xfs_bmap_finish() returned error %d", error
);
1616 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1618 xfs_fs_cmn_err(CE_NOTE
, mp
, "xfs_inactive: "
1619 "xfs_trans_commit() returned error %d", error
);
1622 * Release the dquots held by inode, if any.
1624 XFS_QM_DQDETACH(mp
, ip
);
1626 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1629 return VN_INACTIVE_CACHE
;
1636 struct xfs_name
*name
,
1643 xfs_itrace_entry(dp
);
1645 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
1646 return XFS_ERROR(EIO
);
1648 lock_mode
= xfs_ilock_map_shared(dp
);
1649 error
= xfs_dir_lookup(NULL
, dp
, name
, &inum
);
1650 xfs_iunlock_map_shared(dp
, lock_mode
);
1655 error
= xfs_iget(dp
->i_mount
, NULL
, inum
, 0, 0, ipp
, 0);
1659 xfs_itrace_ref(*ipp
);
1670 struct xfs_name
*name
,
1676 xfs_mount_t
*mp
= dp
->i_mount
;
1680 xfs_bmap_free_t free_list
;
1681 xfs_fsblock_t first_block
;
1682 boolean_t unlock_dp_on_error
= B_FALSE
;
1683 int dm_event_sent
= 0;
1687 struct xfs_dquot
*udqp
, *gdqp
;
1691 xfs_itrace_entry(dp
);
1693 if (DM_EVENT_ENABLED(dp
, DM_EVENT_CREATE
)) {
1694 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_CREATE
,
1695 dp
, DM_RIGHT_NULL
, NULL
,
1696 DM_RIGHT_NULL
, name
->name
, NULL
,
1704 if (XFS_FORCED_SHUTDOWN(mp
))
1705 return XFS_ERROR(EIO
);
1707 /* Return through std_return after this point. */
1710 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
1711 prid
= dp
->i_d
.di_projid
;
1713 prid
= (xfs_prid_t
)dfltprid
;
1716 * Make sure that we have allocated dquot(s) on disk.
1718 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
1719 current_fsuid(credp
), current_fsgid(credp
), prid
,
1720 XFS_QMOPT_QUOTALL
|XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
1726 tp
= xfs_trans_alloc(mp
, XFS_TRANS_CREATE
);
1727 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1728 resblks
= XFS_CREATE_SPACE_RES(mp
, name
->len
);
1730 * Initially assume that the file does not exist and
1731 * reserve the resources for that case. If that is not
1732 * the case we'll drop the one we have and get a more
1733 * appropriate transaction later.
1735 error
= xfs_trans_reserve(tp
, resblks
, XFS_CREATE_LOG_RES(mp
), 0,
1736 XFS_TRANS_PERM_LOG_RES
, XFS_CREATE_LOG_COUNT
);
1737 if (error
== ENOSPC
) {
1739 error
= xfs_trans_reserve(tp
, 0, XFS_CREATE_LOG_RES(mp
), 0,
1740 XFS_TRANS_PERM_LOG_RES
, XFS_CREATE_LOG_COUNT
);
1747 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
1748 unlock_dp_on_error
= B_TRUE
;
1750 XFS_BMAP_INIT(&free_list
, &first_block
);
1755 * Reserve disk quota and the inode.
1757 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
1761 error
= xfs_dir_canenter(tp
, dp
, name
, resblks
);
1764 error
= xfs_dir_ialloc(&tp
, dp
, mode
, 1,
1765 rdev
, credp
, prid
, resblks
> 0,
1768 if (error
== ENOSPC
)
1775 * At this point, we've gotten a newly allocated inode.
1776 * It is locked (and joined to the transaction).
1779 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1782 * Now we join the directory inode to the transaction. We do not do it
1783 * earlier because xfs_dir_ialloc might commit the previous transaction
1784 * (and release all the locks). An error from here on will result in
1785 * the transaction cancel unlocking dp so don't do it explicitly in the
1789 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
1790 unlock_dp_on_error
= B_FALSE
;
1792 error
= xfs_dir_createname(tp
, dp
, name
, ip
->i_ino
,
1793 &first_block
, &free_list
, resblks
?
1794 resblks
- XFS_IALLOC_SPACE_RES(mp
) : 0);
1796 ASSERT(error
!= ENOSPC
);
1799 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1800 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1803 * If this is a synchronous mount, make sure that the
1804 * create transaction goes to disk before returning to
1807 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
1808 xfs_trans_set_sync(tp
);
1814 * Attach the dquot(s) to the inodes and modify them incore.
1815 * These ids of the inode couldn't have changed since the new
1816 * inode has been locked ever since it was created.
1818 XFS_QM_DQVOPCREATE(mp
, tp
, ip
, udqp
, gdqp
);
1821 * xfs_trans_commit normally decrements the vnode ref count
1822 * when it unlocks the inode. Since we want to return the
1823 * vnode to the caller, we bump the vnode ref count now.
1827 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1829 xfs_bmap_cancel(&free_list
);
1833 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1840 XFS_QM_DQRELE(mp
, udqp
);
1841 XFS_QM_DQRELE(mp
, gdqp
);
1845 /* Fallthrough to std_return with error = 0 */
1848 if ((*ipp
|| (error
!= 0 && dm_event_sent
!= 0)) &&
1849 DM_EVENT_ENABLED(dp
, DM_EVENT_POSTCREATE
)) {
1850 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTCREATE
,
1853 DM_RIGHT_NULL
, name
->name
, NULL
,
1859 cancel_flags
|= XFS_TRANS_ABORT
;
1864 xfs_trans_cancel(tp
, cancel_flags
);
1866 XFS_QM_DQRELE(mp
, udqp
);
1867 XFS_QM_DQRELE(mp
, gdqp
);
1869 if (unlock_dp_on_error
)
1870 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
1876 * Wait until after the current transaction is aborted to
1877 * release the inode. This prevents recursive transactions
1878 * and deadlocks from xfs_inactive.
1880 cancel_flags
|= XFS_TRANS_ABORT
;
1881 xfs_trans_cancel(tp
, cancel_flags
);
1884 XFS_QM_DQRELE(mp
, udqp
);
1885 XFS_QM_DQRELE(mp
, gdqp
);
1892 * Some counters to see if (and how often) we are hitting some deadlock
1893 * prevention code paths.
1897 int xfs_rm_lock_delays
;
1898 int xfs_rm_attempts
;
1902 * The following routine will lock the inodes associated with the
1903 * directory and the named entry in the directory. The locks are
1904 * acquired in increasing inode number.
1906 * If the entry is "..", then only the directory is locked. The
1907 * vnode ref count will still include that from the .. entry in
1910 * There is a deadlock we need to worry about. If the locked directory is
1911 * in the AIL, it might be blocking up the log. The next inode we lock
1912 * could be already locked by another thread waiting for log space (e.g
1913 * a permanent log reservation with a long running transaction (see
1914 * xfs_itruncate_finish)). To solve this, we must check if the directory
1915 * is in the ail and use lock_nowait. If we can't lock, we need to
1916 * drop the inode lock on the directory and try again. xfs_iunlock will
1917 * potentially push the tail if we were holding up the log.
1920 xfs_lock_dir_and_entry(
1922 xfs_inode_t
*ip
) /* inode of entry 'name' */
1926 xfs_inode_t
*ips
[2];
1935 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
1942 * We want to lock in increasing inum. Since we've already
1943 * acquired the lock on the directory, we may need to release
1944 * if if the inum of the entry turns out to be less.
1946 if (e_inum
> dp
->i_ino
) {
1948 * We are already in the right order, so just
1949 * lock on the inode of the entry.
1950 * We need to use nowait if dp is in the AIL.
1953 lp
= (xfs_log_item_t
*)dp
->i_itemp
;
1954 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
1955 if (!xfs_ilock_nowait(ip
, XFS_ILOCK_EXCL
)) {
1962 * Unlock dp and try again.
1963 * xfs_iunlock will try to push the tail
1964 * if the inode is in the AIL.
1967 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
1969 if ((attempts
% 5) == 0) {
1970 delay(1); /* Don't just spin the CPU */
1972 xfs_rm_lock_delays
++;
1978 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1980 } else if (e_inum
< dp
->i_ino
) {
1981 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
1985 xfs_lock_inodes(ips
, 2, XFS_ILOCK_EXCL
);
1987 /* else e_inum == dp->i_ino */
1988 /* This can happen if we're asked to lock /x/..
1989 * the entry is "..", which is also the parent directory.
1997 int xfs_small_retries
;
1998 int xfs_middle_retries
;
1999 int xfs_lots_retries
;
2000 int xfs_lock_delays
;
2004 * Bump the subclass so xfs_lock_inodes() acquires each lock with
2008 xfs_lock_inumorder(int lock_mode
, int subclass
)
2010 if (lock_mode
& (XFS_IOLOCK_SHARED
|XFS_IOLOCK_EXCL
))
2011 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_IOLOCK_SHIFT
;
2012 if (lock_mode
& (XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
))
2013 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_ILOCK_SHIFT
;
2019 * The following routine will lock n inodes in exclusive mode.
2020 * We assume the caller calls us with the inodes in i_ino order.
2022 * We need to detect deadlock where an inode that we lock
2023 * is in the AIL and we start waiting for another inode that is locked
2024 * by a thread in a long running transaction (such as truncate). This can
2025 * result in deadlock since the long running trans might need to wait
2026 * for the inode we just locked in order to push the tail and free space
2035 int attempts
= 0, i
, j
, try_lock
;
2038 ASSERT(ips
&& (inodes
>= 2)); /* we need at least two */
2044 for (; i
< inodes
; i
++) {
2047 if (i
&& (ips
[i
] == ips
[i
-1])) /* Already locked */
2051 * If try_lock is not set yet, make sure all locked inodes
2052 * are not in the AIL.
2053 * If any are, set try_lock to be used later.
2057 for (j
= (i
- 1); j
>= 0 && !try_lock
; j
--) {
2058 lp
= (xfs_log_item_t
*)ips
[j
]->i_itemp
;
2059 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
2066 * If any of the previous locks we have locked is in the AIL,
2067 * we must TRY to get the second and subsequent locks. If
2068 * we can't get any, we must release all we have
2073 /* try_lock must be 0 if i is 0. */
2075 * try_lock means we have an inode locked
2076 * that is in the AIL.
2079 if (!xfs_ilock_nowait(ips
[i
], xfs_lock_inumorder(lock_mode
, i
))) {
2083 * Unlock all previous guys and try again.
2084 * xfs_iunlock will try to push the tail
2085 * if the inode is in the AIL.
2088 for(j
= i
- 1; j
>= 0; j
--) {
2091 * Check to see if we've already
2092 * unlocked this one.
2093 * Not the first one going back,
2094 * and the inode ptr is the same.
2096 if ((j
!= (i
- 1)) && ips
[j
] ==
2100 xfs_iunlock(ips
[j
], lock_mode
);
2103 if ((attempts
% 5) == 0) {
2104 delay(1); /* Don't just spin the CPU */
2114 xfs_ilock(ips
[i
], xfs_lock_inumorder(lock_mode
, i
));
2120 if (attempts
< 5) xfs_small_retries
++;
2121 else if (attempts
< 100) xfs_middle_retries
++;
2122 else xfs_lots_retries
++;
2130 #define REMOVE_DEBUG_TRACE(x) {remove_which_error_return = (x);}
2131 int remove_which_error_return
= 0;
2133 #define REMOVE_DEBUG_TRACE(x)
2134 #endif /* ! DEBUG */
2139 struct xfs_name
*name
,
2142 xfs_mount_t
*mp
= dp
->i_mount
;
2143 xfs_trans_t
*tp
= NULL
;
2145 xfs_bmap_free_t free_list
;
2146 xfs_fsblock_t first_block
;
2152 xfs_itrace_entry(dp
);
2154 if (XFS_FORCED_SHUTDOWN(mp
))
2155 return XFS_ERROR(EIO
);
2157 if (DM_EVENT_ENABLED(dp
, DM_EVENT_REMOVE
)) {
2158 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_REMOVE
, dp
, DM_RIGHT_NULL
,
2159 NULL
, DM_RIGHT_NULL
, name
->name
, NULL
,
2160 ip
->i_d
.di_mode
, 0, 0);
2165 xfs_itrace_entry(ip
);
2168 error
= XFS_QM_DQATTACH(mp
, dp
, 0);
2170 error
= XFS_QM_DQATTACH(mp
, ip
, 0);
2172 REMOVE_DEBUG_TRACE(__LINE__
);
2176 tp
= xfs_trans_alloc(mp
, XFS_TRANS_REMOVE
);
2177 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2179 * We try to get the real space reservation first,
2180 * allowing for directory btree deletion(s) implying
2181 * possible bmap insert(s). If we can't get the space
2182 * reservation then we use 0 instead, and avoid the bmap
2183 * btree insert(s) in the directory code by, if the bmap
2184 * insert tries to happen, instead trimming the LAST
2185 * block from the directory.
2187 resblks
= XFS_REMOVE_SPACE_RES(mp
);
2188 error
= xfs_trans_reserve(tp
, resblks
, XFS_REMOVE_LOG_RES(mp
), 0,
2189 XFS_TRANS_PERM_LOG_RES
, XFS_REMOVE_LOG_COUNT
);
2190 if (error
== ENOSPC
) {
2192 error
= xfs_trans_reserve(tp
, 0, XFS_REMOVE_LOG_RES(mp
), 0,
2193 XFS_TRANS_PERM_LOG_RES
, XFS_REMOVE_LOG_COUNT
);
2196 ASSERT(error
!= ENOSPC
);
2197 REMOVE_DEBUG_TRACE(__LINE__
);
2198 xfs_trans_cancel(tp
, 0);
2202 error
= xfs_lock_dir_and_entry(dp
, ip
);
2204 REMOVE_DEBUG_TRACE(__LINE__
);
2205 xfs_trans_cancel(tp
, cancel_flags
);
2210 * At this point, we've gotten both the directory and the entry
2214 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
2217 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
2220 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2222 XFS_BMAP_INIT(&free_list
, &first_block
);
2223 error
= xfs_dir_removename(tp
, dp
, name
, ip
->i_ino
,
2224 &first_block
, &free_list
, resblks
);
2226 ASSERT(error
!= ENOENT
);
2227 REMOVE_DEBUG_TRACE(__LINE__
);
2230 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2233 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2235 error
= xfs_droplink(tp
, ip
);
2237 REMOVE_DEBUG_TRACE(__LINE__
);
2241 /* Determine if this is the last link while
2242 * we are in the transaction.
2244 link_zero
= (ip
)->i_d
.di_nlink
==0;
2247 * If this is a synchronous mount, make sure that the
2248 * remove transaction goes to disk before returning to
2251 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2252 xfs_trans_set_sync(tp
);
2255 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2257 REMOVE_DEBUG_TRACE(__LINE__
);
2261 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2266 * If we are using filestreams, kill the stream association.
2267 * If the file is still open it may get a new one but that
2268 * will get killed on last close in xfs_close() so we don't
2269 * have to worry about that.
2271 if (link_zero
&& xfs_inode_is_filestream(ip
))
2272 xfs_filestream_deassociate(ip
);
2274 xfs_itrace_exit(ip
);
2276 /* Fall through to std_return with error = 0 */
2278 if (DM_EVENT_ENABLED(dp
, DM_EVENT_POSTREMOVE
)) {
2279 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTREMOVE
,
2281 NULL
, DM_RIGHT_NULL
,
2282 name
->name
, NULL
, ip
->i_d
.di_mode
, error
, 0);
2287 xfs_bmap_cancel(&free_list
);
2288 cancel_flags
|= XFS_TRANS_ABORT
;
2289 xfs_trans_cancel(tp
, cancel_flags
);
2294 * In this case make sure to not release the inode until after
2295 * the current transaction is aborted. Releasing it beforehand
2296 * can cause us to go to xfs_inactive and start a recursive
2297 * transaction which can easily deadlock with the current one.
2299 xfs_bmap_cancel(&free_list
);
2300 cancel_flags
|= XFS_TRANS_ABORT
;
2301 xfs_trans_cancel(tp
, cancel_flags
);
2310 struct xfs_name
*target_name
)
2312 xfs_mount_t
*mp
= tdp
->i_mount
;
2314 xfs_inode_t
*ips
[2];
2316 xfs_bmap_free_t free_list
;
2317 xfs_fsblock_t first_block
;
2322 xfs_itrace_entry(tdp
);
2323 xfs_itrace_entry(sip
);
2325 ASSERT(!S_ISDIR(sip
->i_d
.di_mode
));
2327 if (XFS_FORCED_SHUTDOWN(mp
))
2328 return XFS_ERROR(EIO
);
2330 if (DM_EVENT_ENABLED(tdp
, DM_EVENT_LINK
)) {
2331 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_LINK
,
2334 target_name
->name
, NULL
, 0, 0, 0);
2339 /* Return through std_return after this point. */
2341 error
= XFS_QM_DQATTACH(mp
, sip
, 0);
2342 if (!error
&& sip
!= tdp
)
2343 error
= XFS_QM_DQATTACH(mp
, tdp
, 0);
2347 tp
= xfs_trans_alloc(mp
, XFS_TRANS_LINK
);
2348 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2349 resblks
= XFS_LINK_SPACE_RES(mp
, target_name
->len
);
2350 error
= xfs_trans_reserve(tp
, resblks
, XFS_LINK_LOG_RES(mp
), 0,
2351 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
2352 if (error
== ENOSPC
) {
2354 error
= xfs_trans_reserve(tp
, 0, XFS_LINK_LOG_RES(mp
), 0,
2355 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
2362 if (sip
->i_ino
< tdp
->i_ino
) {
2370 xfs_lock_inodes(ips
, 2, XFS_ILOCK_EXCL
);
2373 * Increment vnode ref counts since xfs_trans_commit &
2374 * xfs_trans_cancel will both unlock the inodes and
2375 * decrement the associated ref counts.
2379 xfs_trans_ijoin(tp
, sip
, XFS_ILOCK_EXCL
);
2380 xfs_trans_ijoin(tp
, tdp
, XFS_ILOCK_EXCL
);
2383 * If the source has too many links, we can't make any more to it.
2385 if (sip
->i_d
.di_nlink
>= XFS_MAXLINK
) {
2386 error
= XFS_ERROR(EMLINK
);
2391 * If we are using project inheritance, we only allow hard link
2392 * creation in our tree when the project IDs are the same; else
2393 * the tree quota mechanism could be circumvented.
2395 if (unlikely((tdp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
) &&
2396 (tdp
->i_d
.di_projid
!= sip
->i_d
.di_projid
))) {
2397 error
= XFS_ERROR(EXDEV
);
2401 error
= xfs_dir_canenter(tp
, tdp
, target_name
, resblks
);
2405 XFS_BMAP_INIT(&free_list
, &first_block
);
2407 error
= xfs_dir_createname(tp
, tdp
, target_name
, sip
->i_ino
,
2408 &first_block
, &free_list
, resblks
);
2411 xfs_ichgtime(tdp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2413 xfs_trans_log_inode(tp
, tdp
, XFS_ILOG_CORE
);
2415 error
= xfs_bumplink(tp
, sip
);
2420 * If this is a synchronous mount, make sure that the
2421 * link transaction goes to disk before returning to
2424 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2425 xfs_trans_set_sync(tp
);
2428 error
= xfs_bmap_finish (&tp
, &free_list
, &committed
);
2430 xfs_bmap_cancel(&free_list
);
2434 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2438 /* Fall through to std_return with error = 0. */
2440 if (DM_EVENT_ENABLED(sip
, DM_EVENT_POSTLINK
)) {
2441 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTLINK
,
2444 target_name
->name
, NULL
, 0, error
, 0);
2449 cancel_flags
|= XFS_TRANS_ABORT
;
2453 xfs_trans_cancel(tp
, cancel_flags
);
2461 struct xfs_name
*dir_name
,
2466 xfs_mount_t
*mp
= dp
->i_mount
;
2467 xfs_inode_t
*cdp
; /* inode of created dir */
2472 xfs_bmap_free_t free_list
;
2473 xfs_fsblock_t first_block
;
2474 boolean_t unlock_dp_on_error
= B_FALSE
;
2475 boolean_t created
= B_FALSE
;
2476 int dm_event_sent
= 0;
2478 struct xfs_dquot
*udqp
, *gdqp
;
2481 if (XFS_FORCED_SHUTDOWN(mp
))
2482 return XFS_ERROR(EIO
);
2486 if (DM_EVENT_ENABLED(dp
, DM_EVENT_CREATE
)) {
2487 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_CREATE
,
2488 dp
, DM_RIGHT_NULL
, NULL
,
2489 DM_RIGHT_NULL
, dir_name
->name
, NULL
,
2496 /* Return through std_return after this point. */
2498 xfs_itrace_entry(dp
);
2502 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
2503 prid
= dp
->i_d
.di_projid
;
2505 prid
= (xfs_prid_t
)dfltprid
;
2508 * Make sure that we have allocated dquot(s) on disk.
2510 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
2511 current_fsuid(credp
), current_fsgid(credp
), prid
,
2512 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
2516 tp
= xfs_trans_alloc(mp
, XFS_TRANS_MKDIR
);
2517 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2518 resblks
= XFS_MKDIR_SPACE_RES(mp
, dir_name
->len
);
2519 error
= xfs_trans_reserve(tp
, resblks
, XFS_MKDIR_LOG_RES(mp
), 0,
2520 XFS_TRANS_PERM_LOG_RES
, XFS_MKDIR_LOG_COUNT
);
2521 if (error
== ENOSPC
) {
2523 error
= xfs_trans_reserve(tp
, 0, XFS_MKDIR_LOG_RES(mp
), 0,
2524 XFS_TRANS_PERM_LOG_RES
,
2525 XFS_MKDIR_LOG_COUNT
);
2532 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
2533 unlock_dp_on_error
= B_TRUE
;
2536 * Check for directory link count overflow.
2538 if (dp
->i_d
.di_nlink
>= XFS_MAXLINK
) {
2539 error
= XFS_ERROR(EMLINK
);
2544 * Reserve disk quota and the inode.
2546 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
2550 error
= xfs_dir_canenter(tp
, dp
, dir_name
, resblks
);
2554 * create the directory inode.
2556 error
= xfs_dir_ialloc(&tp
, dp
, mode
, 2,
2557 0, credp
, prid
, resblks
> 0,
2560 if (error
== ENOSPC
)
2564 xfs_itrace_ref(cdp
);
2567 * Now we add the directory inode to the transaction.
2568 * We waited until now since xfs_dir_ialloc might start
2569 * a new transaction. Had we joined the transaction
2570 * earlier, the locks might have gotten released. An error
2571 * from here on will result in the transaction cancel
2572 * unlocking dp so don't do it explicitly in the error path.
2575 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
2576 unlock_dp_on_error
= B_FALSE
;
2578 XFS_BMAP_INIT(&free_list
, &first_block
);
2580 error
= xfs_dir_createname(tp
, dp
, dir_name
, cdp
->i_ino
,
2581 &first_block
, &free_list
, resblks
?
2582 resblks
- XFS_IALLOC_SPACE_RES(mp
) : 0);
2584 ASSERT(error
!= ENOSPC
);
2587 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2590 * Bump the in memory version number of the parent directory
2591 * so that other processes accessing it will recognize that
2592 * the directory has changed.
2596 error
= xfs_dir_init(tp
, cdp
, dp
);
2601 error
= xfs_bumplink(tp
, dp
);
2611 * Attach the dquots to the new inode and modify the icount incore.
2613 XFS_QM_DQVOPCREATE(mp
, tp
, cdp
, udqp
, gdqp
);
2616 * If this is a synchronous mount, make sure that the
2617 * mkdir transaction goes to disk before returning to
2620 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2621 xfs_trans_set_sync(tp
);
2624 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2630 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2631 XFS_QM_DQRELE(mp
, udqp
);
2632 XFS_QM_DQRELE(mp
, gdqp
);
2637 /* Fall through to std_return with error = 0 or errno from
2638 * xfs_trans_commit. */
2641 if ((created
|| (error
!= 0 && dm_event_sent
!= 0)) &&
2642 DM_EVENT_ENABLED(dp
, DM_EVENT_POSTCREATE
)) {
2643 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTCREATE
,
2645 created
? cdp
: NULL
,
2647 dir_name
->name
, NULL
,
2654 xfs_bmap_cancel(&free_list
);
2656 cancel_flags
|= XFS_TRANS_ABORT
;
2658 xfs_trans_cancel(tp
, cancel_flags
);
2659 XFS_QM_DQRELE(mp
, udqp
);
2660 XFS_QM_DQRELE(mp
, gdqp
);
2662 if (unlock_dp_on_error
)
2663 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
2671 struct xfs_name
*name
,
2674 xfs_mount_t
*mp
= dp
->i_mount
;
2677 xfs_bmap_free_t free_list
;
2678 xfs_fsblock_t first_block
;
2684 xfs_itrace_entry(dp
);
2686 if (XFS_FORCED_SHUTDOWN(mp
))
2687 return XFS_ERROR(EIO
);
2689 if (DM_EVENT_ENABLED(dp
, DM_EVENT_REMOVE
)) {
2690 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_REMOVE
,
2692 NULL
, DM_RIGHT_NULL
, name
->name
,
2693 NULL
, cdp
->i_d
.di_mode
, 0, 0);
2695 return XFS_ERROR(error
);
2699 * Get the dquots for the inodes.
2701 error
= XFS_QM_DQATTACH(mp
, dp
, 0);
2703 error
= XFS_QM_DQATTACH(mp
, cdp
, 0);
2705 REMOVE_DEBUG_TRACE(__LINE__
);
2709 tp
= xfs_trans_alloc(mp
, XFS_TRANS_RMDIR
);
2710 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2712 * We try to get the real space reservation first,
2713 * allowing for directory btree deletion(s) implying
2714 * possible bmap insert(s). If we can't get the space
2715 * reservation then we use 0 instead, and avoid the bmap
2716 * btree insert(s) in the directory code by, if the bmap
2717 * insert tries to happen, instead trimming the LAST
2718 * block from the directory.
2720 resblks
= XFS_REMOVE_SPACE_RES(mp
);
2721 error
= xfs_trans_reserve(tp
, resblks
, XFS_REMOVE_LOG_RES(mp
), 0,
2722 XFS_TRANS_PERM_LOG_RES
, XFS_DEFAULT_LOG_COUNT
);
2723 if (error
== ENOSPC
) {
2725 error
= xfs_trans_reserve(tp
, 0, XFS_REMOVE_LOG_RES(mp
), 0,
2726 XFS_TRANS_PERM_LOG_RES
, XFS_DEFAULT_LOG_COUNT
);
2729 ASSERT(error
!= ENOSPC
);
2733 XFS_BMAP_INIT(&free_list
, &first_block
);
2736 * Now lock the child directory inode and the parent directory
2737 * inode in the proper order. This will take care of validating
2738 * that the directory entry for the child directory inode has
2739 * not changed while we were obtaining a log reservation.
2741 error
= xfs_lock_dir_and_entry(dp
, cdp
);
2743 xfs_trans_cancel(tp
, cancel_flags
);
2748 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
2751 xfs_trans_ijoin(tp
, cdp
, XFS_ILOCK_EXCL
);
2753 ASSERT(cdp
->i_d
.di_nlink
>= 2);
2754 if (cdp
->i_d
.di_nlink
!= 2) {
2755 error
= XFS_ERROR(ENOTEMPTY
);
2758 if (!xfs_dir_isempty(cdp
)) {
2759 error
= XFS_ERROR(ENOTEMPTY
);
2763 error
= xfs_dir_removename(tp
, dp
, name
, cdp
->i_ino
,
2764 &first_block
, &free_list
, resblks
);
2768 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2771 * Bump the in memory generation count on the parent
2772 * directory so that other can know that it has changed.
2777 * Drop the link from cdp's "..".
2779 error
= xfs_droplink(tp
, dp
);
2785 * Drop the link from dp to cdp.
2787 error
= xfs_droplink(tp
, cdp
);
2793 * Drop the "." link from cdp to self.
2795 error
= xfs_droplink(tp
, cdp
);
2800 /* Determine these before committing transaction */
2801 last_cdp_link
= (cdp
)->i_d
.di_nlink
==0;
2804 * If this is a synchronous mount, make sure that the
2805 * rmdir transaction goes to disk before returning to
2808 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2809 xfs_trans_set_sync(tp
);
2812 error
= xfs_bmap_finish (&tp
, &free_list
, &committed
);
2814 xfs_bmap_cancel(&free_list
);
2815 xfs_trans_cancel(tp
, (XFS_TRANS_RELEASE_LOG_RES
|
2820 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2826 /* Fall through to std_return with error = 0 or the errno
2827 * from xfs_trans_commit. */
2829 if (DM_EVENT_ENABLED(dp
, DM_EVENT_POSTREMOVE
)) {
2830 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTREMOVE
,
2832 NULL
, DM_RIGHT_NULL
,
2833 name
->name
, NULL
, cdp
->i_d
.di_mode
,
2839 xfs_bmap_cancel(&free_list
);
2840 cancel_flags
|= XFS_TRANS_ABORT
;
2844 xfs_trans_cancel(tp
, cancel_flags
);
2851 struct xfs_name
*link_name
,
2852 const char *target_path
,
2857 xfs_mount_t
*mp
= dp
->i_mount
;
2862 xfs_bmap_free_t free_list
;
2863 xfs_fsblock_t first_block
;
2864 boolean_t unlock_dp_on_error
= B_FALSE
;
2867 xfs_fileoff_t first_fsb
;
2868 xfs_filblks_t fs_blocks
;
2870 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
2872 const char *cur_chunk
;
2877 struct xfs_dquot
*udqp
, *gdqp
;
2885 xfs_itrace_entry(dp
);
2887 if (XFS_FORCED_SHUTDOWN(mp
))
2888 return XFS_ERROR(EIO
);
2891 * Check component lengths of the target path name.
2893 pathlen
= strlen(target_path
);
2894 if (pathlen
>= MAXPATHLEN
) /* total string too long */
2895 return XFS_ERROR(ENAMETOOLONG
);
2897 if (DM_EVENT_ENABLED(dp
, DM_EVENT_SYMLINK
)) {
2898 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_SYMLINK
, dp
,
2899 DM_RIGHT_NULL
, NULL
, DM_RIGHT_NULL
,
2900 link_name
->name
, target_path
, 0, 0, 0);
2905 /* Return through std_return after this point. */
2908 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
2909 prid
= dp
->i_d
.di_projid
;
2911 prid
= (xfs_prid_t
)dfltprid
;
2914 * Make sure that we have allocated dquot(s) on disk.
2916 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
2917 current_fsuid(credp
), current_fsgid(credp
), prid
,
2918 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
2922 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SYMLINK
);
2923 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2925 * The symlink will fit into the inode data fork?
2926 * There can't be any attributes so we get the whole variable part.
2928 if (pathlen
<= XFS_LITINO(mp
))
2931 fs_blocks
= XFS_B_TO_FSB(mp
, pathlen
);
2932 resblks
= XFS_SYMLINK_SPACE_RES(mp
, link_name
->len
, fs_blocks
);
2933 error
= xfs_trans_reserve(tp
, resblks
, XFS_SYMLINK_LOG_RES(mp
), 0,
2934 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
2935 if (error
== ENOSPC
&& fs_blocks
== 0) {
2937 error
= xfs_trans_reserve(tp
, 0, XFS_SYMLINK_LOG_RES(mp
), 0,
2938 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
2945 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
2946 unlock_dp_on_error
= B_TRUE
;
2949 * Check whether the directory allows new symlinks or not.
2951 if (dp
->i_d
.di_flags
& XFS_DIFLAG_NOSYMLINKS
) {
2952 error
= XFS_ERROR(EPERM
);
2957 * Reserve disk quota : blocks and inode.
2959 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
2964 * Check for ability to enter directory entry, if no space reserved.
2966 error
= xfs_dir_canenter(tp
, dp
, link_name
, resblks
);
2970 * Initialize the bmap freelist prior to calling either
2971 * bmapi or the directory create code.
2973 XFS_BMAP_INIT(&free_list
, &first_block
);
2976 * Allocate an inode for the symlink.
2978 error
= xfs_dir_ialloc(&tp
, dp
, S_IFLNK
| (mode
& ~S_IFMT
),
2979 1, 0, credp
, prid
, resblks
> 0, &ip
, NULL
);
2981 if (error
== ENOSPC
)
2988 * An error after we've joined dp to the transaction will result in the
2989 * transaction cancel unlocking dp so don't do it explicitly in the
2993 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
2994 unlock_dp_on_error
= B_FALSE
;
2997 * Also attach the dquot(s) to it, if applicable.
2999 XFS_QM_DQVOPCREATE(mp
, tp
, ip
, udqp
, gdqp
);
3002 resblks
-= XFS_IALLOC_SPACE_RES(mp
);
3004 * If the symlink will fit into the inode, write it inline.
3006 if (pathlen
<= XFS_IFORK_DSIZE(ip
)) {
3007 xfs_idata_realloc(ip
, pathlen
, XFS_DATA_FORK
);
3008 memcpy(ip
->i_df
.if_u1
.if_data
, target_path
, pathlen
);
3009 ip
->i_d
.di_size
= pathlen
;
3012 * The inode was initially created in extent format.
3014 ip
->i_df
.if_flags
&= ~(XFS_IFEXTENTS
| XFS_IFBROOT
);
3015 ip
->i_df
.if_flags
|= XFS_IFINLINE
;
3017 ip
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
3018 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_DDATA
| XFS_ILOG_CORE
);
3022 nmaps
= SYMLINK_MAPS
;
3024 error
= xfs_bmapi(tp
, ip
, first_fsb
, fs_blocks
,
3025 XFS_BMAPI_WRITE
| XFS_BMAPI_METADATA
,
3026 &first_block
, resblks
, mval
, &nmaps
,
3033 resblks
-= fs_blocks
;
3034 ip
->i_d
.di_size
= pathlen
;
3035 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
3037 cur_chunk
= target_path
;
3038 for (n
= 0; n
< nmaps
; n
++) {
3039 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
3040 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
3041 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
3042 BTOBB(byte_cnt
), 0);
3043 ASSERT(bp
&& !XFS_BUF_GETERROR(bp
));
3044 if (pathlen
< byte_cnt
) {
3047 pathlen
-= byte_cnt
;
3049 memcpy(XFS_BUF_PTR(bp
), cur_chunk
, byte_cnt
);
3050 cur_chunk
+= byte_cnt
;
3052 xfs_trans_log_buf(tp
, bp
, 0, byte_cnt
- 1);
3057 * Create the directory entry for the symlink.
3059 error
= xfs_dir_createname(tp
, dp
, link_name
, ip
->i_ino
,
3060 &first_block
, &free_list
, resblks
);
3063 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
3064 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
3067 * Bump the in memory version number of the parent directory
3068 * so that other processes accessing it will recognize that
3069 * the directory has changed.
3074 * If this is a synchronous mount, make sure that the
3075 * symlink transaction goes to disk before returning to
3078 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
3079 xfs_trans_set_sync(tp
);
3083 * xfs_trans_commit normally decrements the vnode ref count
3084 * when it unlocks the inode. Since we want to return the
3085 * vnode to the caller, we bump the vnode ref count now.
3089 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
3093 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3094 XFS_QM_DQRELE(mp
, udqp
);
3095 XFS_QM_DQRELE(mp
, gdqp
);
3097 /* Fall through to std_return with error = 0 or errno from
3098 * xfs_trans_commit */
3100 if (DM_EVENT_ENABLED(dp
, DM_EVENT_POSTSYMLINK
)) {
3101 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTSYMLINK
,
3104 DM_RIGHT_NULL
, link_name
->name
,
3105 target_path
, 0, error
, 0);
3115 xfs_bmap_cancel(&free_list
);
3116 cancel_flags
|= XFS_TRANS_ABORT
;
3118 xfs_trans_cancel(tp
, cancel_flags
);
3119 XFS_QM_DQRELE(mp
, udqp
);
3120 XFS_QM_DQRELE(mp
, gdqp
);
3122 if (unlock_dp_on_error
)
3123 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
3133 xfs_mount_t
*mp
= ip
->i_mount
;
3136 if (XFS_FORCED_SHUTDOWN(mp
))
3137 return XFS_ERROR(EIO
);
3140 * Bypass inodes which have already been cleaned by
3141 * the inode flush clustering code inside xfs_iflush
3143 if (xfs_inode_clean(ip
))
3147 * We make this non-blocking if the inode is contended,
3148 * return EAGAIN to indicate to the caller that they
3149 * did not succeed. This prevents the flush path from
3150 * blocking on inodes inside another operation right
3151 * now, they get caught later by xfs_sync.
3153 if (flags
& FLUSH_SYNC
) {
3154 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
3156 } else if (xfs_ilock_nowait(ip
, XFS_ILOCK_SHARED
)) {
3157 if (xfs_ipincount(ip
) || !xfs_iflock_nowait(ip
)) {
3158 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
3165 error
= xfs_iflush(ip
, (flags
& FLUSH_SYNC
) ? XFS_IFLUSH_SYNC
3166 : XFS_IFLUSH_ASYNC_NOBLOCK
);
3167 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
3179 xfs_mount_t
*mp
= ip
->i_mount
;
3183 if (!capable(CAP_SYS_ADMIN
))
3184 return XFS_ERROR(EPERM
);
3186 if (XFS_FORCED_SHUTDOWN(mp
))
3187 return XFS_ERROR(EIO
);
3189 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SET_DMATTRS
);
3190 error
= xfs_trans_reserve(tp
, 0, XFS_ICHANGE_LOG_RES (mp
), 0, 0, 0);
3192 xfs_trans_cancel(tp
, 0);
3195 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3196 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3198 ip
->i_d
.di_dmevmask
= evmask
;
3199 ip
->i_d
.di_dmstate
= state
;
3201 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
3203 error
= xfs_trans_commit(tp
, 0);
3212 bhv_vnode_t
*vp
= XFS_ITOV(ip
);
3214 xfs_itrace_entry(ip
);
3216 ASSERT(!VN_MAPPED(vp
));
3218 /* bad inode, get out here ASAP */
3226 ASSERT(XFS_FORCED_SHUTDOWN(ip
->i_mount
) || ip
->i_delayed_blks
== 0);
3229 * Make sure the atime in the XFS inode is correct before freeing the
3232 xfs_synchronize_atime(ip
);
3235 * If we have nothing to flush with this inode then complete the
3236 * teardown now, otherwise break the link between the xfs inode and the
3237 * linux inode and clean up the xfs inode later. This avoids flushing
3238 * the inode to disk during the delete operation itself.
3240 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
3241 * first to ensure that xfs_iunpin() will never see an xfs inode
3242 * that has a linux inode being reclaimed. Synchronisation is provided
3243 * by the i_flags_lock.
3245 if (!ip
->i_update_core
&& (ip
->i_itemp
== NULL
)) {
3246 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3248 return xfs_finish_reclaim(ip
, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC
);
3250 xfs_mount_t
*mp
= ip
->i_mount
;
3252 /* Protect sync and unpin from us */
3253 XFS_MOUNT_ILOCK(mp
);
3254 spin_lock(&ip
->i_flags_lock
);
3255 __xfs_iflags_set(ip
, XFS_IRECLAIMABLE
);
3256 vn_to_inode(vp
)->i_private
= NULL
;
3258 spin_unlock(&ip
->i_flags_lock
);
3259 list_add_tail(&ip
->i_reclaim
, &mp
->m_del_inodes
);
3260 XFS_MOUNT_IUNLOCK(mp
);
3271 xfs_perag_t
*pag
= xfs_get_perag(ip
->i_mount
, ip
->i_ino
);
3272 bhv_vnode_t
*vp
= XFS_ITOV_NULL(ip
);
3275 if (vp
&& VN_BAD(vp
))
3278 /* The hash lock here protects a thread in xfs_iget_core from
3279 * racing with us on linking the inode back with a vnode.
3280 * Once we have the XFS_IRECLAIM flag set it will not touch
3283 write_lock(&pag
->pag_ici_lock
);
3284 spin_lock(&ip
->i_flags_lock
);
3285 if (__xfs_iflags_test(ip
, XFS_IRECLAIM
) ||
3286 (!__xfs_iflags_test(ip
, XFS_IRECLAIMABLE
) && vp
== NULL
)) {
3287 spin_unlock(&ip
->i_flags_lock
);
3288 write_unlock(&pag
->pag_ici_lock
);
3291 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3295 __xfs_iflags_set(ip
, XFS_IRECLAIM
);
3296 spin_unlock(&ip
->i_flags_lock
);
3297 write_unlock(&pag
->pag_ici_lock
);
3298 xfs_put_perag(ip
->i_mount
, pag
);
3301 * If the inode is still dirty, then flush it out. If the inode
3302 * is not in the AIL, then it will be OK to flush it delwri as
3303 * long as xfs_iflush() does not keep any references to the inode.
3304 * We leave that decision up to xfs_iflush() since it has the
3305 * knowledge of whether it's OK to simply do a delwri flush of
3306 * the inode or whether we need to wait until the inode is
3307 * pulled from the AIL.
3308 * We get the flush lock regardless, though, just to make sure
3309 * we don't free it while it is being flushed.
3312 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3316 if (!XFS_FORCED_SHUTDOWN(ip
->i_mount
)) {
3317 if (ip
->i_update_core
||
3318 ((ip
->i_itemp
!= NULL
) &&
3319 (ip
->i_itemp
->ili_format
.ilf_fields
!= 0))) {
3320 error
= xfs_iflush(ip
, sync_mode
);
3322 * If we hit an error, typically because of filesystem
3323 * shutdown, we don't need to let vn_reclaim to know
3324 * because we're gonna reclaim the inode anyway.
3327 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3330 xfs_iflock(ip
); /* synchronize with xfs_iflush_done */
3333 ASSERT(ip
->i_update_core
== 0);
3334 ASSERT(ip
->i_itemp
== NULL
||
3335 ip
->i_itemp
->ili_format
.ilf_fields
== 0);
3339 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3347 xfs_finish_reclaim_all(xfs_mount_t
*mp
, int noblock
)
3350 xfs_inode_t
*ip
, *n
;
3355 XFS_MOUNT_ILOCK(mp
);
3356 list_for_each_entry_safe(ip
, n
, &mp
->m_del_inodes
, i_reclaim
) {
3358 if (xfs_ilock_nowait(ip
, XFS_ILOCK_EXCL
) == 0)
3360 if (xfs_ipincount(ip
) ||
3361 !xfs_iflock_nowait(ip
)) {
3362 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3366 XFS_MOUNT_IUNLOCK(mp
);
3367 if (xfs_finish_reclaim(ip
, noblock
,
3368 XFS_IFLUSH_DELWRI_ELSE_ASYNC
))
3377 XFS_MOUNT_IUNLOCK(mp
);
3382 * xfs_alloc_file_space()
3383 * This routine allocates disk space for the given file.
3385 * If alloc_type == 0, this request is for an ALLOCSP type
3386 * request which will change the file size. In this case, no
3387 * DMAPI event will be generated by the call. A TRUNCATE event
3388 * will be generated later by xfs_setattr.
3390 * If alloc_type != 0, this request is for a RESVSP type
3391 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3392 * lower block boundary byte address is less than the file's
3401 xfs_alloc_file_space(
3408 xfs_mount_t
*mp
= ip
->i_mount
;
3410 xfs_filblks_t allocated_fsb
;
3411 xfs_filblks_t allocatesize_fsb
;
3412 xfs_extlen_t extsz
, temp
;
3413 xfs_fileoff_t startoffset_fsb
;
3414 xfs_fsblock_t firstfsb
;
3420 xfs_bmbt_irec_t imaps
[1], *imapp
;
3421 xfs_bmap_free_t free_list
;
3422 uint qblocks
, resblks
, resrtextents
;
3426 xfs_itrace_entry(ip
);
3428 if (XFS_FORCED_SHUTDOWN(mp
))
3429 return XFS_ERROR(EIO
);
3431 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
3435 return XFS_ERROR(EINVAL
);
3437 rt
= XFS_IS_REALTIME_INODE(ip
);
3438 extsz
= xfs_get_extsz_hint(ip
);
3443 bmapi_flag
= XFS_BMAPI_WRITE
| (alloc_type
? XFS_BMAPI_PREALLOC
: 0);
3444 startoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
3445 allocatesize_fsb
= XFS_B_TO_FSB(mp
, count
);
3447 /* Generate a DMAPI event if needed. */
3448 if (alloc_type
!= 0 && offset
< ip
->i_size
&&
3449 (attr_flags
&ATTR_DMI
) == 0 &&
3450 DM_EVENT_ENABLED(ip
, DM_EVENT_WRITE
)) {
3451 xfs_off_t end_dmi_offset
;
3453 end_dmi_offset
= offset
+len
;
3454 if (end_dmi_offset
> ip
->i_size
)
3455 end_dmi_offset
= ip
->i_size
;
3456 error
= XFS_SEND_DATA(mp
, DM_EVENT_WRITE
, ip
, offset
,
3457 end_dmi_offset
- offset
, 0, NULL
);
3463 * Allocate file space until done or until there is an error
3466 while (allocatesize_fsb
&& !error
) {
3470 * Determine space reservations for data/realtime.
3472 if (unlikely(extsz
)) {
3473 s
= startoffset_fsb
;
3476 e
= startoffset_fsb
+ allocatesize_fsb
;
3477 if ((temp
= do_mod(startoffset_fsb
, extsz
)))
3479 if ((temp
= do_mod(e
, extsz
)))
3483 e
= allocatesize_fsb
;
3487 resrtextents
= qblocks
= (uint
)(e
- s
);
3488 resrtextents
/= mp
->m_sb
.sb_rextsize
;
3489 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
3490 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
3493 resblks
= qblocks
= \
3494 XFS_DIOSTRAT_SPACE_RES(mp
, (uint
)(e
- s
));
3495 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
3499 * Allocate and setup the transaction.
3501 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
3502 error
= xfs_trans_reserve(tp
, resblks
,
3503 XFS_WRITE_LOG_RES(mp
), resrtextents
,
3504 XFS_TRANS_PERM_LOG_RES
,
3505 XFS_WRITE_LOG_COUNT
);
3507 * Check for running out of space
3511 * Free the transaction structure.
3513 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
3514 xfs_trans_cancel(tp
, 0);
3517 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3518 error
= XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, tp
, ip
,
3519 qblocks
, 0, quota_flag
);
3523 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3524 xfs_trans_ihold(tp
, ip
);
3527 * Issue the xfs_bmapi() call to allocate the blocks
3529 XFS_BMAP_INIT(&free_list
, &firstfsb
);
3530 error
= xfs_bmapi(tp
, ip
, startoffset_fsb
,
3531 allocatesize_fsb
, bmapi_flag
,
3532 &firstfsb
, 0, imapp
, &nimaps
,
3539 * Complete the transaction
3541 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
3546 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3547 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3552 allocated_fsb
= imapp
->br_blockcount
;
3555 error
= XFS_ERROR(ENOSPC
);
3559 startoffset_fsb
+= allocated_fsb
;
3560 allocatesize_fsb
-= allocated_fsb
;
3563 if (error
== ENOSPC
&& (attr_flags
& ATTR_DMI
) == 0 &&
3564 DM_EVENT_ENABLED(ip
, DM_EVENT_NOSPACE
)) {
3565 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_NOSPACE
,
3568 NULL
, NULL
, 0, 0, 0); /* Delay flag intentionally unused */
3570 goto retry
; /* Maybe DMAPI app. has made space */
3571 /* else fall through with error from XFS_SEND_DATA */
3576 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
3577 xfs_bmap_cancel(&free_list
);
3578 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp
, tp
, ip
, qblocks
, 0, quota_flag
);
3580 error1
: /* Just cancel transaction */
3581 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
3582 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3583 goto dmapi_enospc_check
;
3587 * Zero file bytes between startoff and endoff inclusive.
3588 * The iolock is held exclusive and no blocks are buffered.
3591 xfs_zero_remaining_bytes(
3596 xfs_bmbt_irec_t imap
;
3597 xfs_fileoff_t offset_fsb
;
3598 xfs_off_t lastoffset
;
3601 xfs_mount_t
*mp
= ip
->i_mount
;
3605 bp
= xfs_buf_get_noaddr(mp
->m_sb
.sb_blocksize
,
3606 XFS_IS_REALTIME_INODE(ip
) ?
3607 mp
->m_rtdev_targp
: mp
->m_ddev_targp
);
3609 for (offset
= startoff
; offset
<= endoff
; offset
= lastoffset
+ 1) {
3610 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
3612 error
= xfs_bmapi(NULL
, ip
, offset_fsb
, 1, 0,
3613 NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
3614 if (error
|| nimap
< 1)
3616 ASSERT(imap
.br_blockcount
>= 1);
3617 ASSERT(imap
.br_startoff
== offset_fsb
);
3618 lastoffset
= XFS_FSB_TO_B(mp
, imap
.br_startoff
+ 1) - 1;
3619 if (lastoffset
> endoff
)
3620 lastoffset
= endoff
;
3621 if (imap
.br_startblock
== HOLESTARTBLOCK
)
3623 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
3624 if (imap
.br_state
== XFS_EXT_UNWRITTEN
)
3627 XFS_BUF_UNWRITE(bp
);
3629 XFS_BUF_SET_ADDR(bp
, XFS_FSB_TO_DB(ip
, imap
.br_startblock
));
3631 error
= xfs_iowait(bp
);
3633 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3634 mp
, bp
, XFS_BUF_ADDR(bp
));
3637 memset(XFS_BUF_PTR(bp
) +
3638 (offset
- XFS_FSB_TO_B(mp
, imap
.br_startoff
)),
3639 0, lastoffset
- offset
+ 1);
3644 error
= xfs_iowait(bp
);
3646 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3647 mp
, bp
, XFS_BUF_ADDR(bp
));
3656 * xfs_free_file_space()
3657 * This routine frees disk space for the given file.
3659 * This routine is only called by xfs_change_file_space
3660 * for an UNRESVSP type call.
3668 xfs_free_file_space(
3677 xfs_off_t end_dmi_offset
;
3678 xfs_fileoff_t endoffset_fsb
;
3680 xfs_fsblock_t firstfsb
;
3681 xfs_bmap_free_t free_list
;
3682 xfs_bmbt_irec_t imap
;
3690 xfs_fileoff_t startoffset_fsb
;
3692 int need_iolock
= 1;
3697 xfs_itrace_entry(ip
);
3699 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
3703 if (len
<= 0) /* if nothing being freed */
3705 rt
= XFS_IS_REALTIME_INODE(ip
);
3706 startoffset_fsb
= XFS_B_TO_FSB(mp
, offset
);
3707 end_dmi_offset
= offset
+ len
;
3708 endoffset_fsb
= XFS_B_TO_FSBT(mp
, end_dmi_offset
);
3710 if (offset
< ip
->i_size
&& (attr_flags
& ATTR_DMI
) == 0 &&
3711 DM_EVENT_ENABLED(ip
, DM_EVENT_WRITE
)) {
3712 if (end_dmi_offset
> ip
->i_size
)
3713 end_dmi_offset
= ip
->i_size
;
3714 error
= XFS_SEND_DATA(mp
, DM_EVENT_WRITE
, ip
,
3715 offset
, end_dmi_offset
- offset
,
3716 AT_DELAY_FLAG(attr_flags
), NULL
);
3721 if (attr_flags
& ATTR_NOLOCK
)
3724 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
3725 vn_iowait(ip
); /* wait for the completion of any pending DIOs */
3728 rounding
= max_t(uint
, 1 << mp
->m_sb
.sb_blocklog
, PAGE_CACHE_SIZE
);
3729 ioffset
= offset
& ~(rounding
- 1);
3731 if (VN_CACHED(vp
) != 0) {
3732 xfs_inval_cached_trace(ip
, ioffset
, -1, ioffset
, -1);
3733 error
= xfs_flushinval_pages(ip
, ioffset
, -1, FI_REMAPF_LOCKED
);
3735 goto out_unlock_iolock
;
3739 * Need to zero the stuff we're not freeing, on disk.
3740 * If its a realtime file & can't use unwritten extents then we
3741 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3742 * will take care of it for us.
3744 if (rt
&& !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
3746 error
= xfs_bmapi(NULL
, ip
, startoffset_fsb
,
3747 1, 0, NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
3749 goto out_unlock_iolock
;
3750 ASSERT(nimap
== 0 || nimap
== 1);
3751 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
3754 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
3755 block
= imap
.br_startblock
;
3756 mod
= do_div(block
, mp
->m_sb
.sb_rextsize
);
3758 startoffset_fsb
+= mp
->m_sb
.sb_rextsize
- mod
;
3761 error
= xfs_bmapi(NULL
, ip
, endoffset_fsb
- 1,
3762 1, 0, NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
3764 goto out_unlock_iolock
;
3765 ASSERT(nimap
== 0 || nimap
== 1);
3766 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
3767 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
3769 if (mod
&& (mod
!= mp
->m_sb
.sb_rextsize
))
3770 endoffset_fsb
-= mod
;
3773 if ((done
= (endoffset_fsb
<= startoffset_fsb
)))
3775 * One contiguous piece to clear
3777 error
= xfs_zero_remaining_bytes(ip
, offset
, offset
+ len
- 1);
3780 * Some full blocks, possibly two pieces to clear
3782 if (offset
< XFS_FSB_TO_B(mp
, startoffset_fsb
))
3783 error
= xfs_zero_remaining_bytes(ip
, offset
,
3784 XFS_FSB_TO_B(mp
, startoffset_fsb
) - 1);
3786 XFS_FSB_TO_B(mp
, endoffset_fsb
) < offset
+ len
)
3787 error
= xfs_zero_remaining_bytes(ip
,
3788 XFS_FSB_TO_B(mp
, endoffset_fsb
),
3793 * free file space until done or until there is an error
3795 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
3796 while (!error
&& !done
) {
3799 * allocate and setup the transaction. Allow this
3800 * transaction to dip into the reserve blocks to ensure
3801 * the freeing of the space succeeds at ENOSPC.
3803 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
3804 tp
->t_flags
|= XFS_TRANS_RESERVE
;
3805 error
= xfs_trans_reserve(tp
,
3807 XFS_WRITE_LOG_RES(mp
),
3809 XFS_TRANS_PERM_LOG_RES
,
3810 XFS_WRITE_LOG_COUNT
);
3813 * check for running out of space
3817 * Free the transaction structure.
3819 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
3820 xfs_trans_cancel(tp
, 0);
3823 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3824 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
,
3825 ip
->i_udquot
, ip
->i_gdquot
, resblks
, 0,
3826 XFS_QMOPT_RES_REGBLKS
);
3830 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3831 xfs_trans_ihold(tp
, ip
);
3834 * issue the bunmapi() call to free the blocks
3836 XFS_BMAP_INIT(&free_list
, &firstfsb
);
3837 error
= xfs_bunmapi(tp
, ip
, startoffset_fsb
,
3838 endoffset_fsb
- startoffset_fsb
,
3839 0, 2, &firstfsb
, &free_list
, NULL
, &done
);
3845 * complete the transaction
3847 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
3852 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3853 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3858 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
3862 xfs_bmap_cancel(&free_list
);
3864 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
3865 xfs_iunlock(ip
, need_iolock
? (XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
) :
3871 * xfs_change_file_space()
3872 * This routine allocates or frees disk space for the given file.
3873 * The user specified parameters are checked for alignment and size
3882 xfs_change_file_space(
3890 xfs_mount_t
*mp
= ip
->i_mount
;
3895 xfs_off_t startoffset
;
3900 xfs_itrace_entry(ip
);
3902 if (!S_ISREG(ip
->i_d
.di_mode
))
3903 return XFS_ERROR(EINVAL
);
3905 switch (bf
->l_whence
) {
3906 case 0: /*SEEK_SET*/
3908 case 1: /*SEEK_CUR*/
3909 bf
->l_start
+= offset
;
3911 case 2: /*SEEK_END*/
3912 bf
->l_start
+= ip
->i_size
;
3915 return XFS_ERROR(EINVAL
);
3918 llen
= bf
->l_len
> 0 ? bf
->l_len
- 1 : bf
->l_len
;
3920 if ( (bf
->l_start
< 0)
3921 || (bf
->l_start
> XFS_MAXIOFFSET(mp
))
3922 || (bf
->l_start
+ llen
< 0)
3923 || (bf
->l_start
+ llen
> XFS_MAXIOFFSET(mp
)))
3924 return XFS_ERROR(EINVAL
);
3928 startoffset
= bf
->l_start
;
3932 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3934 * These calls do NOT zero the data space allocated to the file,
3935 * nor do they change the file size.
3937 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3939 * These calls cause the new file data to be zeroed and the file
3940 * size to be changed.
3942 setprealloc
= clrprealloc
= 0;
3945 case XFS_IOC_RESVSP
:
3946 case XFS_IOC_RESVSP64
:
3947 error
= xfs_alloc_file_space(ip
, startoffset
, bf
->l_len
,
3954 case XFS_IOC_UNRESVSP
:
3955 case XFS_IOC_UNRESVSP64
:
3956 if ((error
= xfs_free_file_space(ip
, startoffset
, bf
->l_len
,
3961 case XFS_IOC_ALLOCSP
:
3962 case XFS_IOC_ALLOCSP64
:
3963 case XFS_IOC_FREESP
:
3964 case XFS_IOC_FREESP64
:
3965 if (startoffset
> fsize
) {
3966 error
= xfs_alloc_file_space(ip
, fsize
,
3967 startoffset
- fsize
, 0, attr_flags
);
3972 va
.va_mask
= XFS_AT_SIZE
;
3973 va
.va_size
= startoffset
;
3975 error
= xfs_setattr(ip
, &va
, attr_flags
, credp
);
3985 return XFS_ERROR(EINVAL
);
3989 * update the inode timestamp, mode, and prealloc flag bits
3991 tp
= xfs_trans_alloc(mp
, XFS_TRANS_WRITEID
);
3993 if ((error
= xfs_trans_reserve(tp
, 0, XFS_WRITEID_LOG_RES(mp
),
3996 xfs_trans_cancel(tp
, 0);
4000 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
4002 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
4003 xfs_trans_ihold(tp
, ip
);
4005 if ((attr_flags
& ATTR_DMI
) == 0) {
4006 ip
->i_d
.di_mode
&= ~S_ISUID
;
4009 * Note that we don't have to worry about mandatory
4010 * file locking being disabled here because we only
4011 * clear the S_ISGID bit if the Group execute bit is
4012 * on, but if it was on then mandatory locking wouldn't
4013 * have been enabled.
4015 if (ip
->i_d
.di_mode
& S_IXGRP
)
4016 ip
->i_d
.di_mode
&= ~S_ISGID
;
4018 xfs_ichgtime(ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
4021 ip
->i_d
.di_flags
|= XFS_DIFLAG_PREALLOC
;
4022 else if (clrprealloc
)
4023 ip
->i_d
.di_flags
&= ~XFS_DIFLAG_PREALLOC
;
4025 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
4026 xfs_trans_set_sync(tp
);
4028 error
= xfs_trans_commit(tp
, 0);
4030 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);