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
);
84 xfs_mount_t
*mp
= ip
->i_mount
;
85 struct inode
*inode
= VFS_I(ip
);
86 int mask
= iattr
->ia_valid
;
94 struct xfs_dquot
*udqp
, *gdqp
, *olddquot1
, *olddquot2
;
100 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
101 return XFS_ERROR(EROFS
);
103 if (XFS_FORCED_SHUTDOWN(mp
))
104 return XFS_ERROR(EIO
);
106 olddquot1
= olddquot2
= NULL
;
110 * If disk quotas is on, we make sure that the dquots do exist on disk,
111 * before we start any other transactions. Trying to do this later
112 * is messy. We don't care to take a readlock to look at the ids
113 * in inode here, because we can't hold it across the trans_reserve.
114 * If the IDs do change before we take the ilock, we're covered
115 * because the i_*dquot fields will get updated anyway.
117 if (XFS_IS_QUOTA_ON(mp
) && (mask
& (ATTR_UID
|ATTR_GID
))) {
120 if ((mask
& ATTR_UID
) && XFS_IS_UQUOTA_ON(mp
)) {
122 qflags
|= XFS_QMOPT_UQUOTA
;
124 uid
= ip
->i_d
.di_uid
;
126 if ((mask
& ATTR_GID
) && XFS_IS_GQUOTA_ON(mp
)) {
128 qflags
|= XFS_QMOPT_GQUOTA
;
130 gid
= ip
->i_d
.di_gid
;
134 * We take a reference when we initialize udqp and gdqp,
135 * so it is important that we never blindly double trip on
136 * the same variable. See xfs_create() for an example.
138 ASSERT(udqp
== NULL
);
139 ASSERT(gdqp
== NULL
);
140 code
= XFS_QM_DQVOPALLOC(mp
, ip
, uid
, gid
, ip
->i_d
.di_projid
,
141 qflags
, &udqp
, &gdqp
);
147 * For the other attributes, we acquire the inode lock and
148 * first do an error checking pass.
151 lock_flags
= XFS_ILOCK_EXCL
;
152 if (flags
& XFS_ATTR_NOLOCK
)
154 if (!(mask
& ATTR_SIZE
)) {
155 if ((mask
!= (ATTR_CTIME
|ATTR_ATIME
|ATTR_MTIME
)) ||
156 (mp
->m_flags
& XFS_MOUNT_WSYNC
)) {
157 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SETATTR_NOT_SIZE
);
159 if ((code
= xfs_trans_reserve(tp
, 0,
160 XFS_ICHANGE_LOG_RES(mp
), 0,
167 if (DM_EVENT_ENABLED(ip
, DM_EVENT_TRUNCATE
) &&
168 !(flags
& XFS_ATTR_DMI
)) {
169 int dmflags
= AT_DELAY_FLAG(flags
) | DM_SEM_FLAG_WR
;
170 code
= XFS_SEND_DATA(mp
, DM_EVENT_TRUNCATE
, ip
,
171 iattr
->ia_size
, 0, dmflags
, NULL
);
178 lock_flags
|= XFS_IOLOCK_EXCL
;
181 xfs_ilock(ip
, lock_flags
);
183 /* boolean: are we the file owner? */
184 file_owner
= (current_fsuid() == ip
->i_d
.di_uid
);
187 * Change various properties of a file.
188 * Only the owner or users with CAP_FOWNER
189 * capability may do these things.
191 if (mask
& (ATTR_MODE
|ATTR_UID
|ATTR_GID
)) {
193 * CAP_FOWNER overrides the following restrictions:
195 * The user ID of the calling process must be equal
196 * to the file owner ID, except in cases where the
197 * CAP_FSETID capability is applicable.
199 if (!file_owner
&& !capable(CAP_FOWNER
)) {
200 code
= XFS_ERROR(EPERM
);
205 * CAP_FSETID overrides the following restrictions:
207 * The effective user ID of the calling process shall match
208 * the file owner when setting the set-user-ID and
209 * set-group-ID bits on that file.
211 * The effective group ID or one of the supplementary group
212 * IDs of the calling process shall match the group owner of
213 * the file when setting the set-group-ID bit on that file
215 if (mask
& ATTR_MODE
) {
218 if ((iattr
->ia_mode
& S_ISUID
) && !file_owner
)
220 if ((iattr
->ia_mode
& S_ISGID
) &&
221 !in_group_p((gid_t
)ip
->i_d
.di_gid
))
224 /* Linux allows this, Irix doesn't. */
225 if ((iattr
->ia_mode
& S_ISVTX
) && !S_ISDIR(ip
->i_d
.di_mode
))
228 if (m
&& !capable(CAP_FSETID
))
229 iattr
->ia_mode
&= ~m
;
234 * Change file ownership. Must be the owner or privileged.
235 * If the system was configured with the "restricted_chown"
236 * option, the owner is not permitted to give away the file,
237 * and can change the group id only to a group of which he
238 * or she is a member.
240 if (mask
& (ATTR_UID
|ATTR_GID
)) {
242 * These IDs could have changed since we last looked at them.
243 * But, we're assured that if the ownership did change
244 * while we didn't have the inode locked, inode's dquot(s)
245 * would have changed also.
247 iuid
= ip
->i_d
.di_uid
;
248 igid
= ip
->i_d
.di_gid
;
249 gid
= (mask
& ATTR_GID
) ? iattr
->ia_gid
: igid
;
250 uid
= (mask
& ATTR_UID
) ? iattr
->ia_uid
: iuid
;
253 * CAP_CHOWN overrides the following restrictions:
255 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
256 * shall override the restriction that a process cannot
257 * change the user ID of a file it owns and the restriction
258 * that the group ID supplied to the chown() function
259 * shall be equal to either the group ID or one of the
260 * supplementary group IDs of the calling process.
262 if (restricted_chown
&&
263 (iuid
!= uid
|| (igid
!= gid
&&
264 !in_group_p((gid_t
)gid
))) &&
265 !capable(CAP_CHOWN
)) {
266 code
= XFS_ERROR(EPERM
);
270 * Do a quota reservation only if uid/gid is actually
273 if ((XFS_IS_UQUOTA_ON(mp
) && iuid
!= uid
) ||
274 (XFS_IS_GQUOTA_ON(mp
) && igid
!= gid
)) {
276 code
= XFS_QM_DQVOPCHOWNRESV(mp
, tp
, ip
, udqp
, gdqp
,
277 capable(CAP_FOWNER
) ?
278 XFS_QMOPT_FORCE_RES
: 0);
279 if (code
) /* out of quota */
285 * Truncate file. Must have write permission and not be a directory.
287 if (mask
& ATTR_SIZE
) {
288 /* Short circuit the truncate case for zero length files */
289 if (iattr
->ia_size
== 0 &&
290 ip
->i_size
== 0 && ip
->i_d
.di_nextents
== 0) {
291 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
292 lock_flags
&= ~XFS_ILOCK_EXCL
;
293 if (mask
& ATTR_CTIME
)
294 xfs_ichgtime(ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
299 if (S_ISDIR(ip
->i_d
.di_mode
)) {
300 code
= XFS_ERROR(EISDIR
);
302 } else if (!S_ISREG(ip
->i_d
.di_mode
)) {
303 code
= XFS_ERROR(EINVAL
);
307 * Make sure that the dquots are attached to the inode.
309 if ((code
= XFS_QM_DQATTACH(mp
, ip
, XFS_QMOPT_ILOCKED
)))
314 * Change file access or modified times.
316 if (mask
& (ATTR_ATIME
|ATTR_MTIME
)) {
318 if ((mask
& (ATTR_MTIME_SET
|ATTR_ATIME_SET
)) &&
319 !capable(CAP_FOWNER
)) {
320 code
= XFS_ERROR(EPERM
);
327 * Now we can make the changes. Before we join the inode
328 * to the transaction, if ATTR_SIZE is set then take care of
329 * the part of the truncation that must be done without the
330 * inode lock. This needs to be done before joining the inode
331 * to the transaction, because the inode cannot be unlocked
332 * once it is a part of the transaction.
334 if (mask
& ATTR_SIZE
) {
336 if (iattr
->ia_size
> ip
->i_size
) {
338 * Do the first part of growing a file: zero any data
339 * in the last block that is beyond the old EOF. We
340 * need to do this before the inode is joined to the
341 * transaction to modify the i_size.
343 code
= xfs_zero_eof(ip
, iattr
->ia_size
, ip
->i_size
);
345 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
348 * We are going to log the inode size change in this
349 * transaction so any previous writes that are beyond the on
350 * disk EOF and the new EOF that have not been written out need
351 * to be written here. If we do not write the data out, we
352 * expose ourselves to the null files problem.
354 * Only flush from the on disk size to the smaller of the in
355 * memory file size or the new size as that's the range we
356 * really care about here and prevents waiting for other data
357 * not within the range we care about here.
360 ip
->i_size
!= ip
->i_d
.di_size
&&
361 iattr
->ia_size
> ip
->i_d
.di_size
) {
362 code
= xfs_flush_pages(ip
,
363 ip
->i_d
.di_size
, iattr
->ia_size
,
364 XFS_B_ASYNC
, FI_NONE
);
367 /* wait for all I/O to complete */
371 code
= xfs_itruncate_data(ip
, iattr
->ia_size
);
374 lock_flags
&= ~XFS_ILOCK_EXCL
;
375 ASSERT(lock_flags
== XFS_IOLOCK_EXCL
);
378 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SETATTR_SIZE
);
379 if ((code
= xfs_trans_reserve(tp
, 0,
380 XFS_ITRUNCATE_LOG_RES(mp
), 0,
381 XFS_TRANS_PERM_LOG_RES
,
382 XFS_ITRUNCATE_LOG_COUNT
))) {
383 xfs_trans_cancel(tp
, 0);
385 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
388 commit_flags
= XFS_TRANS_RELEASE_LOG_RES
;
389 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
393 xfs_trans_ijoin(tp
, ip
, lock_flags
);
394 xfs_trans_ihold(tp
, ip
);
398 * Truncate file. Must have write permission and not be a directory.
400 if (mask
& ATTR_SIZE
) {
402 * Only change the c/mtime if we are changing the size
403 * or we are explicitly asked to change it. This handles
404 * the semantic difference between truncate() and ftruncate()
405 * as implemented in the VFS.
407 if (iattr
->ia_size
!= ip
->i_size
|| (mask
& ATTR_CTIME
))
408 timeflags
|= XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
;
410 if (iattr
->ia_size
> ip
->i_size
) {
411 ip
->i_d
.di_size
= iattr
->ia_size
;
412 ip
->i_size
= iattr
->ia_size
;
413 if (!(flags
& XFS_ATTR_DMI
))
414 xfs_ichgtime(ip
, XFS_ICHGTIME_CHG
);
415 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
416 } else if (iattr
->ia_size
<= ip
->i_size
||
417 (iattr
->ia_size
== 0 && ip
->i_d
.di_nextents
)) {
419 * signal a sync transaction unless
420 * we're truncating an already unlinked
421 * file on a wsync filesystem
423 code
= xfs_itruncate_finish(&tp
, ip
, iattr
->ia_size
,
425 ((ip
->i_d
.di_nlink
!= 0 ||
426 !(mp
->m_flags
& XFS_MOUNT_WSYNC
))
431 * Truncated "down", so we're removing references
432 * to old data here - if we now delay flushing for
433 * a long time, we expose ourselves unduly to the
434 * notorious NULL files problem. So, we mark this
435 * vnode and flush it when the file is closed, and
436 * do not wait the usual (long) time for writeout.
438 xfs_iflags_set(ip
, XFS_ITRUNCATED
);
443 * Change file access modes.
445 if (mask
& ATTR_MODE
) {
446 ip
->i_d
.di_mode
&= S_IFMT
;
447 ip
->i_d
.di_mode
|= iattr
->ia_mode
& ~S_IFMT
;
449 inode
->i_mode
&= S_IFMT
;
450 inode
->i_mode
|= iattr
->ia_mode
& ~S_IFMT
;
452 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
453 timeflags
|= XFS_ICHGTIME_CHG
;
457 * Change file ownership. Must be the owner or privileged.
458 * If the system was configured with the "restricted_chown"
459 * option, the owner is not permitted to give away the file,
460 * and can change the group id only to a group of which he
461 * or she is a member.
463 if (mask
& (ATTR_UID
|ATTR_GID
)) {
465 * CAP_FSETID overrides the following restrictions:
467 * The set-user-ID and set-group-ID bits of a file will be
468 * cleared upon successful return from chown()
470 if ((ip
->i_d
.di_mode
& (S_ISUID
|S_ISGID
)) &&
471 !capable(CAP_FSETID
)) {
472 ip
->i_d
.di_mode
&= ~(S_ISUID
|S_ISGID
);
476 * Change the ownerships and register quota modifications
477 * in the transaction.
480 if (XFS_IS_UQUOTA_ON(mp
)) {
481 ASSERT(mask
& ATTR_UID
);
483 olddquot1
= XFS_QM_DQVOPCHOWN(mp
, tp
, ip
,
484 &ip
->i_udquot
, udqp
);
486 ip
->i_d
.di_uid
= uid
;
490 if (XFS_IS_GQUOTA_ON(mp
)) {
491 ASSERT(!XFS_IS_PQUOTA_ON(mp
));
492 ASSERT(mask
& ATTR_GID
);
494 olddquot2
= XFS_QM_DQVOPCHOWN(mp
, tp
, ip
,
495 &ip
->i_gdquot
, gdqp
);
497 ip
->i_d
.di_gid
= gid
;
501 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
502 timeflags
|= XFS_ICHGTIME_CHG
;
507 * Change file access or modified times.
509 if (mask
& (ATTR_ATIME
|ATTR_MTIME
)) {
510 if (mask
& ATTR_ATIME
) {
511 inode
->i_atime
= iattr
->ia_atime
;
512 ip
->i_d
.di_atime
.t_sec
= iattr
->ia_atime
.tv_sec
;
513 ip
->i_d
.di_atime
.t_nsec
= iattr
->ia_atime
.tv_nsec
;
514 ip
->i_update_core
= 1;
516 if (mask
& ATTR_MTIME
) {
517 inode
->i_mtime
= iattr
->ia_mtime
;
518 ip
->i_d
.di_mtime
.t_sec
= iattr
->ia_mtime
.tv_sec
;
519 ip
->i_d
.di_mtime
.t_nsec
= iattr
->ia_mtime
.tv_nsec
;
520 timeflags
&= ~XFS_ICHGTIME_MOD
;
521 timeflags
|= XFS_ICHGTIME_CHG
;
523 if (tp
&& (mask
& (ATTR_MTIME_SET
|ATTR_ATIME_SET
)))
524 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
528 * Change file inode change time only if ATTR_CTIME set
529 * AND we have been called by a DMI function.
532 if ((flags
& XFS_ATTR_DMI
) && (mask
& ATTR_CTIME
)) {
533 inode
->i_ctime
= iattr
->ia_ctime
;
534 ip
->i_d
.di_ctime
.t_sec
= iattr
->ia_ctime
.tv_sec
;
535 ip
->i_d
.di_ctime
.t_nsec
= iattr
->ia_ctime
.tv_nsec
;
536 ip
->i_update_core
= 1;
537 timeflags
&= ~XFS_ICHGTIME_CHG
;
541 * Send out timestamp changes that need to be set to the
542 * current time. Not done when called by a DMI function.
544 if (timeflags
&& !(flags
& XFS_ATTR_DMI
))
545 xfs_ichgtime(ip
, timeflags
);
547 XFS_STATS_INC(xs_ig_attrchg
);
550 * If this is a synchronous mount, make sure that the
551 * transaction goes to disk before returning to the user.
552 * This is slightly sub-optimal in that truncates require
553 * two sync transactions instead of one for wsync filesystems.
554 * One for the truncate and one for the timestamps since we
555 * don't want to change the timestamps unless we're sure the
556 * truncate worked. Truncates are less than 1% of the laddis
557 * mix so this probably isn't worth the trouble to optimize.
561 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
562 xfs_trans_set_sync(tp
);
564 code
= xfs_trans_commit(tp
, commit_flags
);
567 xfs_iunlock(ip
, lock_flags
);
570 * Release any dquot(s) the inode had kept before chown.
572 XFS_QM_DQRELE(mp
, olddquot1
);
573 XFS_QM_DQRELE(mp
, olddquot2
);
574 XFS_QM_DQRELE(mp
, udqp
);
575 XFS_QM_DQRELE(mp
, gdqp
);
581 if (DM_EVENT_ENABLED(ip
, DM_EVENT_ATTRIBUTE
) &&
582 !(flags
& XFS_ATTR_DMI
)) {
583 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_ATTRIBUTE
, ip
, DM_RIGHT_NULL
,
584 NULL
, DM_RIGHT_NULL
, NULL
, NULL
,
585 0, 0, AT_DELAY_FLAG(flags
));
590 commit_flags
|= XFS_TRANS_ABORT
;
593 XFS_QM_DQRELE(mp
, udqp
);
594 XFS_QM_DQRELE(mp
, gdqp
);
596 xfs_trans_cancel(tp
, commit_flags
);
598 if (lock_flags
!= 0) {
599 xfs_iunlock(ip
, lock_flags
);
605 * The maximum pathlen is 1024 bytes. Since the minimum file system
606 * blocksize is 512 bytes, we can get a max of 2 extents back from
609 #define SYMLINK_MAPS 2
616 xfs_mount_t
*mp
= ip
->i_mount
;
617 int pathlen
= ip
->i_d
.di_size
;
618 int nmaps
= SYMLINK_MAPS
;
619 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
626 error
= xfs_bmapi(NULL
, ip
, 0, XFS_B_TO_FSB(mp
, pathlen
), 0, NULL
, 0,
627 mval
, &nmaps
, NULL
, NULL
);
631 for (n
= 0; n
< nmaps
; n
++) {
632 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
633 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
635 bp
= xfs_buf_read(mp
->m_ddev_targp
, d
, BTOBB(byte_cnt
), 0);
636 error
= XFS_BUF_GETERROR(bp
);
638 xfs_ioerror_alert("xfs_readlink",
639 ip
->i_mount
, bp
, XFS_BUF_ADDR(bp
));
643 if (pathlen
< byte_cnt
)
647 memcpy(link
, XFS_BUF_PTR(bp
), byte_cnt
);
651 link
[ip
->i_d
.di_size
] = '\0';
663 xfs_mount_t
*mp
= ip
->i_mount
;
667 xfs_itrace_entry(ip
);
669 if (XFS_FORCED_SHUTDOWN(mp
))
670 return XFS_ERROR(EIO
);
672 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
674 ASSERT((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
);
675 ASSERT(ip
->i_d
.di_size
<= MAXPATHLEN
);
677 pathlen
= ip
->i_d
.di_size
;
681 if (ip
->i_df
.if_flags
& XFS_IFINLINE
) {
682 memcpy(link
, ip
->i_df
.if_u1
.if_data
, pathlen
);
683 link
[pathlen
] = '\0';
685 error
= xfs_readlink_bmap(ip
, link
);
689 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
696 * This is called to sync the inode and its data out to disk. We need to hold
697 * the I/O lock while flushing the data, and the inode lock while flushing the
698 * inode. The inode lock CANNOT be held while flushing the data, so acquire
699 * after we're done with that.
707 int log_flushed
= 0, changed
= 1;
709 xfs_itrace_entry(ip
);
711 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
712 return XFS_ERROR(EIO
);
714 /* capture size updates in I/O completion before writing the inode. */
715 error
= filemap_fdatawait(VFS_I(ip
)->i_mapping
);
717 return XFS_ERROR(error
);
720 * We always need to make sure that the required inode state is safe on
721 * disk. The vnode might be clean but we still might need to force the
722 * log because of committed transactions that haven't hit the disk yet.
723 * Likewise, there could be unflushed non-transactional changes to the
724 * inode core that have to go to disk and this requires us to issue
725 * a synchronous transaction to capture these changes correctly.
727 * This code relies on the assumption that if the update_* fields
728 * of the inode are clear and the inode is unpinned then it is clean
729 * and no action is required.
731 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
733 if (!(ip
->i_update_size
|| ip
->i_update_core
)) {
735 * Timestamps/size haven't changed since last inode flush or
736 * inode transaction commit. That means either nothing got
737 * written or a transaction committed which caught the updates.
738 * If the latter happened and the transaction hasn't hit the
739 * disk yet, the inode will be still be pinned. If it is,
743 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
745 if (xfs_ipincount(ip
)) {
746 error
= _xfs_log_force(ip
->i_mount
, (xfs_lsn_t
)0,
747 XFS_LOG_FORCE
| XFS_LOG_SYNC
,
751 * If the inode is not pinned and nothing has changed
752 * we don't need to flush the cache.
758 * Kick off a transaction to log the inode core to get the
759 * updates. The sync transaction will also force the log.
761 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
762 tp
= xfs_trans_alloc(ip
->i_mount
, XFS_TRANS_FSYNC_TS
);
763 error
= xfs_trans_reserve(tp
, 0,
764 XFS_FSYNC_TS_LOG_RES(ip
->i_mount
), 0, 0, 0);
766 xfs_trans_cancel(tp
, 0);
769 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
772 * Note - it's possible that we might have pushed ourselves out
773 * of the way during trans_reserve which would flush the inode.
774 * But there's no guarantee that the inode buffer has actually
775 * gone out yet (it's delwri). Plus the buffer could be pinned
776 * anyway if it's part of an inode in another recent
777 * transaction. So we play it safe and fire off the
778 * transaction anyway.
780 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
781 xfs_trans_ihold(tp
, ip
);
782 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
783 xfs_trans_set_sync(tp
);
784 error
= _xfs_trans_commit(tp
, 0, &log_flushed
);
786 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
789 if ((ip
->i_mount
->m_flags
& XFS_MOUNT_BARRIER
) && changed
) {
791 * If the log write didn't issue an ordered tag we need
792 * to flush the disk cache for the data device now.
795 xfs_blkdev_issue_flush(ip
->i_mount
->m_ddev_targp
);
798 * If this inode is on the RT dev we need to flush that
801 if (XFS_IS_REALTIME_INODE(ip
))
802 xfs_blkdev_issue_flush(ip
->i_mount
->m_rtdev_targp
);
809 * This is called by xfs_inactive to free any blocks beyond eof
810 * when the link count isn't zero and by xfs_dm_punch_hole() when
811 * punching a hole to EOF.
821 xfs_fileoff_t end_fsb
;
822 xfs_fileoff_t last_fsb
;
823 xfs_filblks_t map_len
;
825 xfs_bmbt_irec_t imap
;
826 int use_iolock
= (flags
& XFS_FREE_EOF_LOCK
);
829 * Figure out if there are any blocks beyond the end
830 * of the file. If not, then there is nothing to do.
832 end_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)ip
->i_size
));
833 last_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_MAXIOFFSET(mp
));
834 map_len
= last_fsb
- end_fsb
;
839 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
840 error
= xfs_bmapi(NULL
, ip
, end_fsb
, map_len
, 0,
841 NULL
, 0, &imap
, &nimaps
, NULL
, NULL
);
842 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
844 if (!error
&& (nimaps
!= 0) &&
845 (imap
.br_startblock
!= HOLESTARTBLOCK
||
846 ip
->i_delayed_blks
)) {
848 * Attach the dquots to the inode up front.
850 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
854 * There are blocks after the end of file.
855 * Free them up now by truncating the file to
858 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
861 * Do the xfs_itruncate_start() call before
862 * reserving any log space because
863 * itruncate_start will call into the buffer
865 * do that within a transaction.
868 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
869 error
= xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
,
872 xfs_trans_cancel(tp
, 0);
874 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
878 error
= xfs_trans_reserve(tp
, 0,
879 XFS_ITRUNCATE_LOG_RES(mp
),
880 0, XFS_TRANS_PERM_LOG_RES
,
881 XFS_ITRUNCATE_LOG_COUNT
);
883 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
884 xfs_trans_cancel(tp
, 0);
885 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
889 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
890 xfs_trans_ijoin(tp
, ip
,
893 xfs_trans_ihold(tp
, ip
);
895 error
= xfs_itruncate_finish(&tp
, ip
,
900 * If we get an error at this point we
901 * simply don't bother truncating the file.
905 (XFS_TRANS_RELEASE_LOG_RES
|
908 error
= xfs_trans_commit(tp
,
909 XFS_TRANS_RELEASE_LOG_RES
);
911 xfs_iunlock(ip
, (use_iolock
? (XFS_IOLOCK_EXCL
|XFS_ILOCK_EXCL
)
918 * Free a symlink that has blocks associated with it.
921 xfs_inactive_symlink_rmt(
929 xfs_fsblock_t first_block
;
930 xfs_bmap_free_t free_list
;
933 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
941 ASSERT(ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
));
943 * We're freeing a symlink that has some
944 * blocks allocated to it. Free the
945 * blocks here. We know that we've got
946 * either 1 or 2 extents and that we can
947 * free them all in one bunmapi call.
949 ASSERT(ip
->i_d
.di_nextents
> 0 && ip
->i_d
.di_nextents
<= 2);
950 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
951 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
952 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
953 xfs_trans_cancel(tp
, 0);
958 * Lock the inode, fix the size, and join it to the transaction.
959 * Hold it so in the normal path, we still have it locked for
960 * the second transaction. In the error paths we need it
961 * held so the cancel won't rele it, see below.
963 xfs_ilock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
964 size
= (int)ip
->i_d
.di_size
;
966 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
967 xfs_trans_ihold(tp
, ip
);
968 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
970 * Find the block(s) so we can inval and unmap them.
973 XFS_BMAP_INIT(&free_list
, &first_block
);
974 nmaps
= ARRAY_SIZE(mval
);
975 if ((error
= xfs_bmapi(tp
, ip
, 0, XFS_B_TO_FSB(mp
, size
),
976 XFS_BMAPI_METADATA
, &first_block
, 0, mval
, &nmaps
,
980 * Invalidate the block(s).
982 for (i
= 0; i
< nmaps
; i
++) {
983 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
984 XFS_FSB_TO_DADDR(mp
, mval
[i
].br_startblock
),
985 XFS_FSB_TO_BB(mp
, mval
[i
].br_blockcount
), 0);
986 xfs_trans_binval(tp
, bp
);
989 * Unmap the dead block(s) to the free_list.
991 if ((error
= xfs_bunmapi(tp
, ip
, 0, size
, XFS_BMAPI_METADATA
, nmaps
,
992 &first_block
, &free_list
, NULL
, &done
)))
996 * Commit the first transaction. This logs the EFI and the inode.
998 if ((error
= xfs_bmap_finish(&tp
, &free_list
, &committed
)))
1001 * The transaction must have been committed, since there were
1002 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1003 * The new tp has the extent freeing and EFDs.
1007 * The first xact was committed, so add the inode to the new one.
1008 * Mark it dirty so it will be logged and moved forward in the log as
1009 * part of every commit.
1011 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1012 xfs_trans_ihold(tp
, ip
);
1013 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1015 * Get a new, empty transaction to return to our caller.
1017 ntp
= xfs_trans_dup(tp
);
1019 * Commit the transaction containing extent freeing and EFDs.
1020 * If we get an error on the commit here or on the reserve below,
1021 * we need to unlock the inode since the new transaction doesn't
1022 * have the inode attached.
1024 error
= xfs_trans_commit(tp
, 0);
1027 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1031 * Remove the memory for extent descriptions (just bookkeeping).
1033 if (ip
->i_df
.if_bytes
)
1034 xfs_idata_realloc(ip
, -ip
->i_df
.if_bytes
, XFS_DATA_FORK
);
1035 ASSERT(ip
->i_df
.if_bytes
== 0);
1037 * Put an itruncate log reservation in the new transaction
1040 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
1041 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
1042 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1046 * Return with the inode locked but not joined to the transaction.
1052 xfs_bmap_cancel(&free_list
);
1055 * Have to come here with the inode locked and either
1056 * (held and in the transaction) or (not in the transaction).
1057 * If the inode isn't held then cancel would iput it, but
1058 * that's wrong since this is inactive and the vnode ref
1059 * count is 0 already.
1060 * Cancel won't do anything to the inode if held, but it still
1061 * needs to be locked until the cancel is done, if it was
1062 * joined to the transaction.
1064 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1065 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1072 xfs_inactive_symlink_local(
1078 ASSERT(ip
->i_d
.di_size
<= XFS_IFORK_DSIZE(ip
));
1080 * We're freeing a symlink which fit into
1081 * the inode. Just free the memory used
1082 * to hold the old symlink.
1084 error
= xfs_trans_reserve(*tpp
, 0,
1085 XFS_ITRUNCATE_LOG_RES(ip
->i_mount
),
1086 0, XFS_TRANS_PERM_LOG_RES
,
1087 XFS_ITRUNCATE_LOG_COUNT
);
1090 xfs_trans_cancel(*tpp
, 0);
1094 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1097 * Zero length symlinks _can_ exist.
1099 if (ip
->i_df
.if_bytes
> 0) {
1100 xfs_idata_realloc(ip
,
1101 -(ip
->i_df
.if_bytes
),
1103 ASSERT(ip
->i_df
.if_bytes
== 0);
1117 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
1120 ASSERT(ip
->i_d
.di_forkoff
!= 0);
1121 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1122 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1126 error
= xfs_attr_inactive(ip
);
1130 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1131 error
= xfs_trans_reserve(tp
, 0,
1132 XFS_IFREE_LOG_RES(mp
),
1133 0, XFS_TRANS_PERM_LOG_RES
,
1134 XFS_INACTIVE_LOG_COUNT
);
1138 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1139 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1140 xfs_trans_ihold(tp
, ip
);
1141 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
1143 ASSERT(ip
->i_d
.di_anextents
== 0);
1149 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1150 xfs_trans_cancel(tp
, 0);
1153 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1161 xfs_mount_t
*mp
= ip
->i_mount
;
1164 if (!S_ISREG(ip
->i_d
.di_mode
) || (ip
->i_d
.di_mode
== 0))
1167 /* If this is a read-only mount, don't do this (would generate I/O) */
1168 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
1171 if (!XFS_FORCED_SHUTDOWN(mp
)) {
1175 * If we are using filestreams, and we have an unlinked
1176 * file that we are processing the last close on, then nothing
1177 * will be able to reopen and write to this file. Purge this
1178 * inode from the filestreams cache so that it doesn't delay
1179 * teardown of the inode.
1181 if ((ip
->i_d
.di_nlink
== 0) && xfs_inode_is_filestream(ip
))
1182 xfs_filestream_deassociate(ip
);
1185 * If we previously truncated this file and removed old data
1186 * in the process, we want to initiate "early" writeout on
1187 * the last close. This is an attempt to combat the notorious
1188 * NULL files problem which is particularly noticable from a
1189 * truncate down, buffered (re-)write (delalloc), followed by
1190 * a crash. What we are effectively doing here is
1191 * significantly reducing the time window where we'd otherwise
1192 * be exposed to that problem.
1194 truncated
= xfs_iflags_test_and_clear(ip
, XFS_ITRUNCATED
);
1195 if (truncated
&& VN_DIRTY(VFS_I(ip
)) && ip
->i_delayed_blks
> 0)
1196 xfs_flush_pages(ip
, 0, -1, XFS_B_ASYNC
, FI_NONE
);
1199 if (ip
->i_d
.di_nlink
!= 0) {
1200 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
1201 ((ip
->i_size
> 0) || (VN_CACHED(VFS_I(ip
)) > 0 ||
1202 ip
->i_delayed_blks
> 0)) &&
1203 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
)) &&
1204 (!(ip
->i_d
.di_flags
&
1205 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)))) {
1206 error
= xfs_free_eofblocks(mp
, ip
, XFS_FREE_EOF_LOCK
);
1218 * This is called when the vnode reference count for the vnode
1219 * goes to zero. If the file has been unlinked, then it must
1220 * now be truncated. Also, we clear all of the read-ahead state
1221 * kept for the inode here since the file is now closed.
1227 xfs_bmap_free_t free_list
;
1228 xfs_fsblock_t first_block
;
1235 xfs_itrace_entry(ip
);
1238 * If the inode is already free, then there can be nothing
1241 if (ip
->i_d
.di_mode
== 0 || VN_BAD(VFS_I(ip
))) {
1242 ASSERT(ip
->i_df
.if_real_bytes
== 0);
1243 ASSERT(ip
->i_df
.if_broot_bytes
== 0);
1244 return VN_INACTIVE_CACHE
;
1248 * Only do a truncate if it's a regular file with
1249 * some actual space in it. It's OK to look at the
1250 * inode's fields without the lock because we're the
1251 * only one with a reference to the inode.
1253 truncate
= ((ip
->i_d
.di_nlink
== 0) &&
1254 ((ip
->i_d
.di_size
!= 0) || (ip
->i_size
!= 0) ||
1255 (ip
->i_d
.di_nextents
> 0) || (ip
->i_delayed_blks
> 0)) &&
1256 ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
));
1260 if (ip
->i_d
.di_nlink
== 0 && DM_EVENT_ENABLED(ip
, DM_EVENT_DESTROY
))
1261 XFS_SEND_DESTROY(mp
, ip
, DM_RIGHT_NULL
);
1265 /* If this is a read-only mount, don't do this (would generate I/O) */
1266 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
1269 if (ip
->i_d
.di_nlink
!= 0) {
1270 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
1271 ((ip
->i_size
> 0) || (VN_CACHED(VFS_I(ip
)) > 0 ||
1272 ip
->i_delayed_blks
> 0)) &&
1273 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
) &&
1274 (!(ip
->i_d
.di_flags
&
1275 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)) ||
1276 (ip
->i_delayed_blks
!= 0)))) {
1277 error
= xfs_free_eofblocks(mp
, ip
, XFS_FREE_EOF_LOCK
);
1279 return VN_INACTIVE_CACHE
;
1284 ASSERT(ip
->i_d
.di_nlink
== 0);
1286 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
1287 return VN_INACTIVE_CACHE
;
1289 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1292 * Do the xfs_itruncate_start() call before
1293 * reserving any log space because itruncate_start
1294 * will call into the buffer cache and we can't
1295 * do that within a transaction.
1297 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
1299 error
= xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
, 0);
1301 xfs_trans_cancel(tp
, 0);
1302 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1303 return VN_INACTIVE_CACHE
;
1306 error
= xfs_trans_reserve(tp
, 0,
1307 XFS_ITRUNCATE_LOG_RES(mp
),
1308 0, XFS_TRANS_PERM_LOG_RES
,
1309 XFS_ITRUNCATE_LOG_COUNT
);
1311 /* Don't call itruncate_cleanup */
1312 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1313 xfs_trans_cancel(tp
, 0);
1314 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1315 return VN_INACTIVE_CACHE
;
1318 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1319 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1320 xfs_trans_ihold(tp
, ip
);
1323 * normally, we have to run xfs_itruncate_finish sync.
1324 * But if filesystem is wsync and we're in the inactive
1325 * path, then we know that nlink == 0, and that the
1326 * xaction that made nlink == 0 is permanently committed
1327 * since xfs_remove runs as a synchronous transaction.
1329 error
= xfs_itruncate_finish(&tp
, ip
, 0, XFS_DATA_FORK
,
1330 (!(mp
->m_flags
& XFS_MOUNT_WSYNC
) ? 1 : 0));
1333 xfs_trans_cancel(tp
,
1334 XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1335 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1336 return VN_INACTIVE_CACHE
;
1338 } else if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
) {
1341 * If we get an error while cleaning up a
1342 * symlink we bail out.
1344 error
= (ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
)) ?
1345 xfs_inactive_symlink_rmt(ip
, &tp
) :
1346 xfs_inactive_symlink_local(ip
, &tp
);
1350 return VN_INACTIVE_CACHE
;
1353 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1354 xfs_trans_ihold(tp
, ip
);
1356 error
= xfs_trans_reserve(tp
, 0,
1357 XFS_IFREE_LOG_RES(mp
),
1358 0, XFS_TRANS_PERM_LOG_RES
,
1359 XFS_INACTIVE_LOG_COUNT
);
1361 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1362 xfs_trans_cancel(tp
, 0);
1363 return VN_INACTIVE_CACHE
;
1366 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1367 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1368 xfs_trans_ihold(tp
, ip
);
1372 * If there are attributes associated with the file
1373 * then blow them away now. The code calls a routine
1374 * that recursively deconstructs the attribute fork.
1375 * We need to just commit the current transaction
1376 * because we can't use it for xfs_attr_inactive().
1378 if (ip
->i_d
.di_anextents
> 0) {
1379 error
= xfs_inactive_attrs(ip
, &tp
);
1381 * If we got an error, the transaction is already
1382 * cancelled, and the inode is unlocked. Just get out.
1385 return VN_INACTIVE_CACHE
;
1386 } else if (ip
->i_afp
) {
1387 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
1393 XFS_BMAP_INIT(&free_list
, &first_block
);
1394 error
= xfs_ifree(tp
, ip
, &free_list
);
1397 * If we fail to free the inode, shut down. The cancel
1398 * might do that, we need to make sure. Otherwise the
1399 * inode might be lost for a long time or forever.
1401 if (!XFS_FORCED_SHUTDOWN(mp
)) {
1403 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1404 error
, mp
->m_fsname
);
1405 xfs_force_shutdown(mp
, SHUTDOWN_META_IO_ERROR
);
1407 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
1410 * Credit the quota account(s). The inode is gone.
1412 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, XFS_TRANS_DQ_ICOUNT
, -1);
1415 * Just ignore errors at this point. There is nothing we can
1416 * do except to try to keep going. Make sure it's not a silent
1419 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1421 xfs_fs_cmn_err(CE_NOTE
, mp
, "xfs_inactive: "
1422 "xfs_bmap_finish() returned error %d", error
);
1423 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1425 xfs_fs_cmn_err(CE_NOTE
, mp
, "xfs_inactive: "
1426 "xfs_trans_commit() returned error %d", error
);
1429 * Release the dquots held by inode, if any.
1431 XFS_QM_DQDETACH(mp
, ip
);
1433 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1436 return VN_INACTIVE_CACHE
;
1440 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1441 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1442 * ci_name->name will point to a the actual name (caller must free) or
1443 * will be set to NULL if an exact match is found.
1448 struct xfs_name
*name
,
1450 struct xfs_name
*ci_name
)
1456 xfs_itrace_entry(dp
);
1458 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
1459 return XFS_ERROR(EIO
);
1461 lock_mode
= xfs_ilock_map_shared(dp
);
1462 error
= xfs_dir_lookup(NULL
, dp
, name
, &inum
, ci_name
);
1463 xfs_iunlock_map_shared(dp
, lock_mode
);
1468 error
= xfs_iget(dp
->i_mount
, NULL
, inum
, 0, 0, ipp
, 0);
1472 xfs_itrace_ref(*ipp
);
1477 kmem_free(ci_name
->name
);
1486 struct xfs_name
*name
,
1492 xfs_mount_t
*mp
= dp
->i_mount
;
1496 xfs_bmap_free_t free_list
;
1497 xfs_fsblock_t first_block
;
1498 boolean_t unlock_dp_on_error
= B_FALSE
;
1499 int dm_event_sent
= 0;
1503 struct xfs_dquot
*udqp
, *gdqp
;
1507 xfs_itrace_entry(dp
);
1509 if (DM_EVENT_ENABLED(dp
, DM_EVENT_CREATE
)) {
1510 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_CREATE
,
1511 dp
, DM_RIGHT_NULL
, NULL
,
1512 DM_RIGHT_NULL
, name
->name
, NULL
,
1520 if (XFS_FORCED_SHUTDOWN(mp
))
1521 return XFS_ERROR(EIO
);
1523 /* Return through std_return after this point. */
1526 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
1527 prid
= dp
->i_d
.di_projid
;
1529 prid
= (xfs_prid_t
)dfltprid
;
1532 * Make sure that we have allocated dquot(s) on disk.
1534 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
1535 current_fsuid(), current_fsgid(), prid
,
1536 XFS_QMOPT_QUOTALL
|XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
1542 tp
= xfs_trans_alloc(mp
, XFS_TRANS_CREATE
);
1543 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1544 resblks
= XFS_CREATE_SPACE_RES(mp
, name
->len
);
1546 * Initially assume that the file does not exist and
1547 * reserve the resources for that case. If that is not
1548 * the case we'll drop the one we have and get a more
1549 * appropriate transaction later.
1551 error
= xfs_trans_reserve(tp
, resblks
, XFS_CREATE_LOG_RES(mp
), 0,
1552 XFS_TRANS_PERM_LOG_RES
, XFS_CREATE_LOG_COUNT
);
1553 if (error
== ENOSPC
) {
1555 error
= xfs_trans_reserve(tp
, 0, XFS_CREATE_LOG_RES(mp
), 0,
1556 XFS_TRANS_PERM_LOG_RES
, XFS_CREATE_LOG_COUNT
);
1563 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
1564 unlock_dp_on_error
= B_TRUE
;
1566 XFS_BMAP_INIT(&free_list
, &first_block
);
1571 * Reserve disk quota and the inode.
1573 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
1577 error
= xfs_dir_canenter(tp
, dp
, name
, resblks
);
1580 error
= xfs_dir_ialloc(&tp
, dp
, mode
, 1,
1581 rdev
, credp
, prid
, resblks
> 0,
1584 if (error
== ENOSPC
)
1591 * At this point, we've gotten a newly allocated inode.
1592 * It is locked (and joined to the transaction).
1595 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1598 * Now we join the directory inode to the transaction. We do not do it
1599 * earlier because xfs_dir_ialloc might commit the previous transaction
1600 * (and release all the locks). An error from here on will result in
1601 * the transaction cancel unlocking dp so don't do it explicitly in the
1605 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
1606 unlock_dp_on_error
= B_FALSE
;
1608 error
= xfs_dir_createname(tp
, dp
, name
, ip
->i_ino
,
1609 &first_block
, &free_list
, resblks
?
1610 resblks
- XFS_IALLOC_SPACE_RES(mp
) : 0);
1612 ASSERT(error
!= ENOSPC
);
1615 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1616 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1619 * If this is a synchronous mount, make sure that the
1620 * create transaction goes to disk before returning to
1623 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
1624 xfs_trans_set_sync(tp
);
1630 * Attach the dquot(s) to the inodes and modify them incore.
1631 * These ids of the inode couldn't have changed since the new
1632 * inode has been locked ever since it was created.
1634 XFS_QM_DQVOPCREATE(mp
, tp
, ip
, udqp
, gdqp
);
1637 * xfs_trans_commit normally decrements the vnode ref count
1638 * when it unlocks the inode. Since we want to return the
1639 * vnode to the caller, we bump the vnode ref count now.
1643 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1645 xfs_bmap_cancel(&free_list
);
1649 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1656 XFS_QM_DQRELE(mp
, udqp
);
1657 XFS_QM_DQRELE(mp
, gdqp
);
1661 /* Fallthrough to std_return with error = 0 */
1664 if ((*ipp
|| (error
!= 0 && dm_event_sent
!= 0)) &&
1665 DM_EVENT_ENABLED(dp
, DM_EVENT_POSTCREATE
)) {
1666 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTCREATE
,
1669 DM_RIGHT_NULL
, name
->name
, NULL
,
1675 cancel_flags
|= XFS_TRANS_ABORT
;
1680 xfs_trans_cancel(tp
, cancel_flags
);
1682 XFS_QM_DQRELE(mp
, udqp
);
1683 XFS_QM_DQRELE(mp
, gdqp
);
1685 if (unlock_dp_on_error
)
1686 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
1692 * Wait until after the current transaction is aborted to
1693 * release the inode. This prevents recursive transactions
1694 * and deadlocks from xfs_inactive.
1696 cancel_flags
|= XFS_TRANS_ABORT
;
1697 xfs_trans_cancel(tp
, cancel_flags
);
1700 XFS_QM_DQRELE(mp
, udqp
);
1701 XFS_QM_DQRELE(mp
, gdqp
);
1708 int xfs_small_retries
;
1709 int xfs_middle_retries
;
1710 int xfs_lots_retries
;
1711 int xfs_lock_delays
;
1715 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1719 xfs_lock_inumorder(int lock_mode
, int subclass
)
1721 if (lock_mode
& (XFS_IOLOCK_SHARED
|XFS_IOLOCK_EXCL
))
1722 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_IOLOCK_SHIFT
;
1723 if (lock_mode
& (XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
))
1724 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_ILOCK_SHIFT
;
1730 * The following routine will lock n inodes in exclusive mode.
1731 * We assume the caller calls us with the inodes in i_ino order.
1733 * We need to detect deadlock where an inode that we lock
1734 * is in the AIL and we start waiting for another inode that is locked
1735 * by a thread in a long running transaction (such as truncate). This can
1736 * result in deadlock since the long running trans might need to wait
1737 * for the inode we just locked in order to push the tail and free space
1746 int attempts
= 0, i
, j
, try_lock
;
1749 ASSERT(ips
&& (inodes
>= 2)); /* we need at least two */
1755 for (; i
< inodes
; i
++) {
1758 if (i
&& (ips
[i
] == ips
[i
-1])) /* Already locked */
1762 * If try_lock is not set yet, make sure all locked inodes
1763 * are not in the AIL.
1764 * If any are, set try_lock to be used later.
1768 for (j
= (i
- 1); j
>= 0 && !try_lock
; j
--) {
1769 lp
= (xfs_log_item_t
*)ips
[j
]->i_itemp
;
1770 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
1777 * If any of the previous locks we have locked is in the AIL,
1778 * we must TRY to get the second and subsequent locks. If
1779 * we can't get any, we must release all we have
1784 /* try_lock must be 0 if i is 0. */
1786 * try_lock means we have an inode locked
1787 * that is in the AIL.
1790 if (!xfs_ilock_nowait(ips
[i
], xfs_lock_inumorder(lock_mode
, i
))) {
1794 * Unlock all previous guys and try again.
1795 * xfs_iunlock will try to push the tail
1796 * if the inode is in the AIL.
1799 for(j
= i
- 1; j
>= 0; j
--) {
1802 * Check to see if we've already
1803 * unlocked this one.
1804 * Not the first one going back,
1805 * and the inode ptr is the same.
1807 if ((j
!= (i
- 1)) && ips
[j
] ==
1811 xfs_iunlock(ips
[j
], lock_mode
);
1814 if ((attempts
% 5) == 0) {
1815 delay(1); /* Don't just spin the CPU */
1825 xfs_ilock(ips
[i
], xfs_lock_inumorder(lock_mode
, i
));
1831 if (attempts
< 5) xfs_small_retries
++;
1832 else if (attempts
< 100) xfs_middle_retries
++;
1833 else xfs_lots_retries
++;
1841 * xfs_lock_two_inodes() can only be used to lock one type of lock
1842 * at a time - the iolock or the ilock, but not both at once. If
1843 * we lock both at once, lockdep will report false positives saying
1844 * we have violated locking orders.
1847 xfs_lock_two_inodes(
1856 if (lock_mode
& (XFS_IOLOCK_SHARED
|XFS_IOLOCK_EXCL
))
1857 ASSERT((lock_mode
& (XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
)) == 0);
1858 ASSERT(ip0
->i_ino
!= ip1
->i_ino
);
1860 if (ip0
->i_ino
> ip1
->i_ino
) {
1867 xfs_ilock(ip0
, xfs_lock_inumorder(lock_mode
, 0));
1870 * If the first lock we have locked is in the AIL, we must TRY to get
1871 * the second lock. If we can't get it, we must release the first one
1874 lp
= (xfs_log_item_t
*)ip0
->i_itemp
;
1875 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
1876 if (!xfs_ilock_nowait(ip1
, xfs_lock_inumorder(lock_mode
, 1))) {
1877 xfs_iunlock(ip0
, lock_mode
);
1878 if ((++attempts
% 5) == 0)
1879 delay(1); /* Don't just spin the CPU */
1883 xfs_ilock(ip1
, xfs_lock_inumorder(lock_mode
, 1));
1890 struct xfs_name
*name
,
1893 xfs_mount_t
*mp
= dp
->i_mount
;
1894 xfs_trans_t
*tp
= NULL
;
1895 int is_dir
= S_ISDIR(ip
->i_d
.di_mode
);
1897 xfs_bmap_free_t free_list
;
1898 xfs_fsblock_t first_block
;
1905 xfs_itrace_entry(dp
);
1906 xfs_itrace_entry(ip
);
1908 if (XFS_FORCED_SHUTDOWN(mp
))
1909 return XFS_ERROR(EIO
);
1911 if (DM_EVENT_ENABLED(dp
, DM_EVENT_REMOVE
)) {
1912 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_REMOVE
, dp
, DM_RIGHT_NULL
,
1913 NULL
, DM_RIGHT_NULL
, name
->name
, NULL
,
1914 ip
->i_d
.di_mode
, 0, 0);
1919 error
= XFS_QM_DQATTACH(mp
, dp
, 0);
1923 error
= XFS_QM_DQATTACH(mp
, ip
, 0);
1928 tp
= xfs_trans_alloc(mp
, XFS_TRANS_RMDIR
);
1929 log_count
= XFS_DEFAULT_LOG_COUNT
;
1931 tp
= xfs_trans_alloc(mp
, XFS_TRANS_REMOVE
);
1932 log_count
= XFS_REMOVE_LOG_COUNT
;
1934 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1937 * We try to get the real space reservation first,
1938 * allowing for directory btree deletion(s) implying
1939 * possible bmap insert(s). If we can't get the space
1940 * reservation then we use 0 instead, and avoid the bmap
1941 * btree insert(s) in the directory code by, if the bmap
1942 * insert tries to happen, instead trimming the LAST
1943 * block from the directory.
1945 resblks
= XFS_REMOVE_SPACE_RES(mp
);
1946 error
= xfs_trans_reserve(tp
, resblks
, XFS_REMOVE_LOG_RES(mp
), 0,
1947 XFS_TRANS_PERM_LOG_RES
, log_count
);
1948 if (error
== ENOSPC
) {
1950 error
= xfs_trans_reserve(tp
, 0, XFS_REMOVE_LOG_RES(mp
), 0,
1951 XFS_TRANS_PERM_LOG_RES
, log_count
);
1954 ASSERT(error
!= ENOSPC
);
1956 goto out_trans_cancel
;
1959 xfs_lock_two_inodes(dp
, ip
, XFS_ILOCK_EXCL
);
1962 * At this point, we've gotten both the directory and the entry
1966 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
1969 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1972 * If we're removing a directory perform some additional validation.
1975 ASSERT(ip
->i_d
.di_nlink
>= 2);
1976 if (ip
->i_d
.di_nlink
!= 2) {
1977 error
= XFS_ERROR(ENOTEMPTY
);
1978 goto out_trans_cancel
;
1980 if (!xfs_dir_isempty(ip
)) {
1981 error
= XFS_ERROR(ENOTEMPTY
);
1982 goto out_trans_cancel
;
1986 XFS_BMAP_INIT(&free_list
, &first_block
);
1987 error
= xfs_dir_removename(tp
, dp
, name
, ip
->i_ino
,
1988 &first_block
, &free_list
, resblks
);
1990 ASSERT(error
!= ENOENT
);
1991 goto out_bmap_cancel
;
1993 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1996 * Bump the in memory generation count on the parent
1997 * directory so that other can know that it has changed.
2000 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2004 * Drop the link from ip's "..".
2006 error
= xfs_droplink(tp
, dp
);
2008 goto out_bmap_cancel
;
2011 * Drop the "." link from ip to self.
2013 error
= xfs_droplink(tp
, ip
);
2015 goto out_bmap_cancel
;
2018 * When removing a non-directory we need to log the parent
2019 * inode here for the i_gen update. For a directory this is
2020 * done implicitly by the xfs_droplink call for the ".." entry.
2022 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2026 * Drop the link from dp to ip.
2028 error
= xfs_droplink(tp
, ip
);
2030 goto out_bmap_cancel
;
2033 * Determine if this is the last link while
2034 * we are in the transaction.
2036 link_zero
= (ip
->i_d
.di_nlink
== 0);
2039 * If this is a synchronous mount, make sure that the
2040 * remove transaction goes to disk before returning to
2043 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
))
2044 xfs_trans_set_sync(tp
);
2046 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2048 goto out_bmap_cancel
;
2050 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2055 * If we are using filestreams, kill the stream association.
2056 * If the file is still open it may get a new one but that
2057 * will get killed on last close in xfs_close() so we don't
2058 * have to worry about that.
2060 if (!is_dir
&& link_zero
&& xfs_inode_is_filestream(ip
))
2061 xfs_filestream_deassociate(ip
);
2063 xfs_itrace_exit(ip
);
2064 xfs_itrace_exit(dp
);
2067 if (DM_EVENT_ENABLED(dp
, DM_EVENT_POSTREMOVE
)) {
2068 XFS_SEND_NAMESP(mp
, DM_EVENT_POSTREMOVE
, dp
, DM_RIGHT_NULL
,
2069 NULL
, DM_RIGHT_NULL
, name
->name
, NULL
,
2070 ip
->i_d
.di_mode
, error
, 0);
2076 xfs_bmap_cancel(&free_list
);
2077 cancel_flags
|= XFS_TRANS_ABORT
;
2079 xfs_trans_cancel(tp
, cancel_flags
);
2087 struct xfs_name
*target_name
)
2089 xfs_mount_t
*mp
= tdp
->i_mount
;
2092 xfs_bmap_free_t free_list
;
2093 xfs_fsblock_t first_block
;
2098 xfs_itrace_entry(tdp
);
2099 xfs_itrace_entry(sip
);
2101 ASSERT(!S_ISDIR(sip
->i_d
.di_mode
));
2103 if (XFS_FORCED_SHUTDOWN(mp
))
2104 return XFS_ERROR(EIO
);
2106 if (DM_EVENT_ENABLED(tdp
, DM_EVENT_LINK
)) {
2107 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_LINK
,
2110 target_name
->name
, NULL
, 0, 0, 0);
2115 /* Return through std_return after this point. */
2117 error
= XFS_QM_DQATTACH(mp
, sip
, 0);
2118 if (!error
&& sip
!= tdp
)
2119 error
= XFS_QM_DQATTACH(mp
, tdp
, 0);
2123 tp
= xfs_trans_alloc(mp
, XFS_TRANS_LINK
);
2124 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2125 resblks
= XFS_LINK_SPACE_RES(mp
, target_name
->len
);
2126 error
= xfs_trans_reserve(tp
, resblks
, XFS_LINK_LOG_RES(mp
), 0,
2127 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
2128 if (error
== ENOSPC
) {
2130 error
= xfs_trans_reserve(tp
, 0, XFS_LINK_LOG_RES(mp
), 0,
2131 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
2138 xfs_lock_two_inodes(sip
, tdp
, XFS_ILOCK_EXCL
);
2141 * Increment vnode ref counts since xfs_trans_commit &
2142 * xfs_trans_cancel will both unlock the inodes and
2143 * decrement the associated ref counts.
2147 xfs_trans_ijoin(tp
, sip
, XFS_ILOCK_EXCL
);
2148 xfs_trans_ijoin(tp
, tdp
, XFS_ILOCK_EXCL
);
2151 * If the source has too many links, we can't make any more to it.
2153 if (sip
->i_d
.di_nlink
>= XFS_MAXLINK
) {
2154 error
= XFS_ERROR(EMLINK
);
2159 * If we are using project inheritance, we only allow hard link
2160 * creation in our tree when the project IDs are the same; else
2161 * the tree quota mechanism could be circumvented.
2163 if (unlikely((tdp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
) &&
2164 (tdp
->i_d
.di_projid
!= sip
->i_d
.di_projid
))) {
2165 error
= XFS_ERROR(EXDEV
);
2169 error
= xfs_dir_canenter(tp
, tdp
, target_name
, resblks
);
2173 XFS_BMAP_INIT(&free_list
, &first_block
);
2175 error
= xfs_dir_createname(tp
, tdp
, target_name
, sip
->i_ino
,
2176 &first_block
, &free_list
, resblks
);
2179 xfs_ichgtime(tdp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2181 xfs_trans_log_inode(tp
, tdp
, XFS_ILOG_CORE
);
2183 error
= xfs_bumplink(tp
, sip
);
2188 * If this is a synchronous mount, make sure that the
2189 * link transaction goes to disk before returning to
2192 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2193 xfs_trans_set_sync(tp
);
2196 error
= xfs_bmap_finish (&tp
, &free_list
, &committed
);
2198 xfs_bmap_cancel(&free_list
);
2202 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2206 /* Fall through to std_return with error = 0. */
2208 if (DM_EVENT_ENABLED(sip
, DM_EVENT_POSTLINK
)) {
2209 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTLINK
,
2212 target_name
->name
, NULL
, 0, error
, 0);
2217 cancel_flags
|= XFS_TRANS_ABORT
;
2221 xfs_trans_cancel(tp
, cancel_flags
);
2229 struct xfs_name
*dir_name
,
2234 xfs_mount_t
*mp
= dp
->i_mount
;
2235 xfs_inode_t
*cdp
; /* inode of created dir */
2240 xfs_bmap_free_t free_list
;
2241 xfs_fsblock_t first_block
;
2242 boolean_t unlock_dp_on_error
= B_FALSE
;
2243 boolean_t created
= B_FALSE
;
2244 int dm_event_sent
= 0;
2246 struct xfs_dquot
*udqp
, *gdqp
;
2249 if (XFS_FORCED_SHUTDOWN(mp
))
2250 return XFS_ERROR(EIO
);
2254 if (DM_EVENT_ENABLED(dp
, DM_EVENT_CREATE
)) {
2255 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_CREATE
,
2256 dp
, DM_RIGHT_NULL
, NULL
,
2257 DM_RIGHT_NULL
, dir_name
->name
, NULL
,
2264 /* Return through std_return after this point. */
2266 xfs_itrace_entry(dp
);
2270 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
2271 prid
= dp
->i_d
.di_projid
;
2273 prid
= (xfs_prid_t
)dfltprid
;
2276 * Make sure that we have allocated dquot(s) on disk.
2278 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
2279 current_fsuid(), current_fsgid(), prid
,
2280 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
2284 tp
= xfs_trans_alloc(mp
, XFS_TRANS_MKDIR
);
2285 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2286 resblks
= XFS_MKDIR_SPACE_RES(mp
, dir_name
->len
);
2287 error
= xfs_trans_reserve(tp
, resblks
, XFS_MKDIR_LOG_RES(mp
), 0,
2288 XFS_TRANS_PERM_LOG_RES
, XFS_MKDIR_LOG_COUNT
);
2289 if (error
== ENOSPC
) {
2291 error
= xfs_trans_reserve(tp
, 0, XFS_MKDIR_LOG_RES(mp
), 0,
2292 XFS_TRANS_PERM_LOG_RES
,
2293 XFS_MKDIR_LOG_COUNT
);
2300 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
2301 unlock_dp_on_error
= B_TRUE
;
2304 * Check for directory link count overflow.
2306 if (dp
->i_d
.di_nlink
>= XFS_MAXLINK
) {
2307 error
= XFS_ERROR(EMLINK
);
2312 * Reserve disk quota and the inode.
2314 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
2318 error
= xfs_dir_canenter(tp
, dp
, dir_name
, resblks
);
2322 * create the directory inode.
2324 error
= xfs_dir_ialloc(&tp
, dp
, mode
, 2,
2325 0, credp
, prid
, resblks
> 0,
2328 if (error
== ENOSPC
)
2332 xfs_itrace_ref(cdp
);
2335 * Now we add the directory inode to the transaction.
2336 * We waited until now since xfs_dir_ialloc might start
2337 * a new transaction. Had we joined the transaction
2338 * earlier, the locks might have gotten released. An error
2339 * from here on will result in the transaction cancel
2340 * unlocking dp so don't do it explicitly in the error path.
2343 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
2344 unlock_dp_on_error
= B_FALSE
;
2346 XFS_BMAP_INIT(&free_list
, &first_block
);
2348 error
= xfs_dir_createname(tp
, dp
, dir_name
, cdp
->i_ino
,
2349 &first_block
, &free_list
, resblks
?
2350 resblks
- XFS_IALLOC_SPACE_RES(mp
) : 0);
2352 ASSERT(error
!= ENOSPC
);
2355 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2358 * Bump the in memory version number of the parent directory
2359 * so that other processes accessing it will recognize that
2360 * the directory has changed.
2364 error
= xfs_dir_init(tp
, cdp
, dp
);
2369 error
= xfs_bumplink(tp
, dp
);
2379 * Attach the dquots to the new inode and modify the icount incore.
2381 XFS_QM_DQVOPCREATE(mp
, tp
, cdp
, udqp
, gdqp
);
2384 * If this is a synchronous mount, make sure that the
2385 * mkdir transaction goes to disk before returning to
2388 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2389 xfs_trans_set_sync(tp
);
2392 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2398 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2399 XFS_QM_DQRELE(mp
, udqp
);
2400 XFS_QM_DQRELE(mp
, gdqp
);
2405 /* Fall through to std_return with error = 0 or errno from
2406 * xfs_trans_commit. */
2409 if ((created
|| (error
!= 0 && dm_event_sent
!= 0)) &&
2410 DM_EVENT_ENABLED(dp
, DM_EVENT_POSTCREATE
)) {
2411 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTCREATE
,
2413 created
? cdp
: NULL
,
2415 dir_name
->name
, NULL
,
2422 xfs_bmap_cancel(&free_list
);
2424 cancel_flags
|= XFS_TRANS_ABORT
;
2426 xfs_trans_cancel(tp
, cancel_flags
);
2427 XFS_QM_DQRELE(mp
, udqp
);
2428 XFS_QM_DQRELE(mp
, gdqp
);
2430 if (unlock_dp_on_error
)
2431 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
2439 struct xfs_name
*link_name
,
2440 const char *target_path
,
2445 xfs_mount_t
*mp
= dp
->i_mount
;
2450 xfs_bmap_free_t free_list
;
2451 xfs_fsblock_t first_block
;
2452 boolean_t unlock_dp_on_error
= B_FALSE
;
2455 xfs_fileoff_t first_fsb
;
2456 xfs_filblks_t fs_blocks
;
2458 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
2460 const char *cur_chunk
;
2465 struct xfs_dquot
*udqp
, *gdqp
;
2473 xfs_itrace_entry(dp
);
2475 if (XFS_FORCED_SHUTDOWN(mp
))
2476 return XFS_ERROR(EIO
);
2479 * Check component lengths of the target path name.
2481 pathlen
= strlen(target_path
);
2482 if (pathlen
>= MAXPATHLEN
) /* total string too long */
2483 return XFS_ERROR(ENAMETOOLONG
);
2485 if (DM_EVENT_ENABLED(dp
, DM_EVENT_SYMLINK
)) {
2486 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_SYMLINK
, dp
,
2487 DM_RIGHT_NULL
, NULL
, DM_RIGHT_NULL
,
2488 link_name
->name
, target_path
, 0, 0, 0);
2493 /* Return through std_return after this point. */
2496 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
2497 prid
= dp
->i_d
.di_projid
;
2499 prid
= (xfs_prid_t
)dfltprid
;
2502 * Make sure that we have allocated dquot(s) on disk.
2504 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
2505 current_fsuid(), current_fsgid(), prid
,
2506 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
2510 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SYMLINK
);
2511 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2513 * The symlink will fit into the inode data fork?
2514 * There can't be any attributes so we get the whole variable part.
2516 if (pathlen
<= XFS_LITINO(mp
))
2519 fs_blocks
= XFS_B_TO_FSB(mp
, pathlen
);
2520 resblks
= XFS_SYMLINK_SPACE_RES(mp
, link_name
->len
, fs_blocks
);
2521 error
= xfs_trans_reserve(tp
, resblks
, XFS_SYMLINK_LOG_RES(mp
), 0,
2522 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
2523 if (error
== ENOSPC
&& fs_blocks
== 0) {
2525 error
= xfs_trans_reserve(tp
, 0, XFS_SYMLINK_LOG_RES(mp
), 0,
2526 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
2533 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
2534 unlock_dp_on_error
= B_TRUE
;
2537 * Check whether the directory allows new symlinks or not.
2539 if (dp
->i_d
.di_flags
& XFS_DIFLAG_NOSYMLINKS
) {
2540 error
= XFS_ERROR(EPERM
);
2545 * Reserve disk quota : blocks and inode.
2547 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
2552 * Check for ability to enter directory entry, if no space reserved.
2554 error
= xfs_dir_canenter(tp
, dp
, link_name
, resblks
);
2558 * Initialize the bmap freelist prior to calling either
2559 * bmapi or the directory create code.
2561 XFS_BMAP_INIT(&free_list
, &first_block
);
2564 * Allocate an inode for the symlink.
2566 error
= xfs_dir_ialloc(&tp
, dp
, S_IFLNK
| (mode
& ~S_IFMT
),
2567 1, 0, credp
, prid
, resblks
> 0, &ip
, NULL
);
2569 if (error
== ENOSPC
)
2576 * An error after we've joined dp to the transaction will result in the
2577 * transaction cancel unlocking dp so don't do it explicitly in the
2581 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
2582 unlock_dp_on_error
= B_FALSE
;
2585 * Also attach the dquot(s) to it, if applicable.
2587 XFS_QM_DQVOPCREATE(mp
, tp
, ip
, udqp
, gdqp
);
2590 resblks
-= XFS_IALLOC_SPACE_RES(mp
);
2592 * If the symlink will fit into the inode, write it inline.
2594 if (pathlen
<= XFS_IFORK_DSIZE(ip
)) {
2595 xfs_idata_realloc(ip
, pathlen
, XFS_DATA_FORK
);
2596 memcpy(ip
->i_df
.if_u1
.if_data
, target_path
, pathlen
);
2597 ip
->i_d
.di_size
= pathlen
;
2600 * The inode was initially created in extent format.
2602 ip
->i_df
.if_flags
&= ~(XFS_IFEXTENTS
| XFS_IFBROOT
);
2603 ip
->i_df
.if_flags
|= XFS_IFINLINE
;
2605 ip
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
2606 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_DDATA
| XFS_ILOG_CORE
);
2610 nmaps
= SYMLINK_MAPS
;
2612 error
= xfs_bmapi(tp
, ip
, first_fsb
, fs_blocks
,
2613 XFS_BMAPI_WRITE
| XFS_BMAPI_METADATA
,
2614 &first_block
, resblks
, mval
, &nmaps
,
2621 resblks
-= fs_blocks
;
2622 ip
->i_d
.di_size
= pathlen
;
2623 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2625 cur_chunk
= target_path
;
2626 for (n
= 0; n
< nmaps
; n
++) {
2627 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
2628 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
2629 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
2630 BTOBB(byte_cnt
), 0);
2631 ASSERT(bp
&& !XFS_BUF_GETERROR(bp
));
2632 if (pathlen
< byte_cnt
) {
2635 pathlen
-= byte_cnt
;
2637 memcpy(XFS_BUF_PTR(bp
), cur_chunk
, byte_cnt
);
2638 cur_chunk
+= byte_cnt
;
2640 xfs_trans_log_buf(tp
, bp
, 0, byte_cnt
- 1);
2645 * Create the directory entry for the symlink.
2647 error
= xfs_dir_createname(tp
, dp
, link_name
, ip
->i_ino
,
2648 &first_block
, &free_list
, resblks
);
2651 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2652 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2655 * Bump the in memory version number of the parent directory
2656 * so that other processes accessing it will recognize that
2657 * the directory has changed.
2662 * If this is a synchronous mount, make sure that the
2663 * symlink transaction goes to disk before returning to
2666 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2667 xfs_trans_set_sync(tp
);
2671 * xfs_trans_commit normally decrements the vnode ref count
2672 * when it unlocks the inode. Since we want to return the
2673 * vnode to the caller, we bump the vnode ref count now.
2677 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2681 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2682 XFS_QM_DQRELE(mp
, udqp
);
2683 XFS_QM_DQRELE(mp
, gdqp
);
2685 /* Fall through to std_return with error = 0 or errno from
2686 * xfs_trans_commit */
2688 if (DM_EVENT_ENABLED(dp
, DM_EVENT_POSTSYMLINK
)) {
2689 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTSYMLINK
,
2692 DM_RIGHT_NULL
, link_name
->name
,
2693 target_path
, 0, error
, 0);
2703 xfs_bmap_cancel(&free_list
);
2704 cancel_flags
|= XFS_TRANS_ABORT
;
2706 xfs_trans_cancel(tp
, cancel_flags
);
2707 XFS_QM_DQRELE(mp
, udqp
);
2708 XFS_QM_DQRELE(mp
, gdqp
);
2710 if (unlock_dp_on_error
)
2711 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
2721 xfs_mount_t
*mp
= ip
->i_mount
;
2724 if (XFS_FORCED_SHUTDOWN(mp
))
2725 return XFS_ERROR(EIO
);
2728 * Bypass inodes which have already been cleaned by
2729 * the inode flush clustering code inside xfs_iflush
2731 if (xfs_inode_clean(ip
))
2735 * We make this non-blocking if the inode is contended,
2736 * return EAGAIN to indicate to the caller that they
2737 * did not succeed. This prevents the flush path from
2738 * blocking on inodes inside another operation right
2739 * now, they get caught later by xfs_sync.
2741 if (flags
& FLUSH_SYNC
) {
2742 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
2744 } else if (xfs_ilock_nowait(ip
, XFS_ILOCK_SHARED
)) {
2745 if (xfs_ipincount(ip
) || !xfs_iflock_nowait(ip
)) {
2746 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
2753 error
= xfs_iflush(ip
, (flags
& FLUSH_SYNC
) ? XFS_IFLUSH_SYNC
2754 : XFS_IFLUSH_ASYNC_NOBLOCK
);
2755 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
2767 xfs_mount_t
*mp
= ip
->i_mount
;
2771 if (!capable(CAP_SYS_ADMIN
))
2772 return XFS_ERROR(EPERM
);
2774 if (XFS_FORCED_SHUTDOWN(mp
))
2775 return XFS_ERROR(EIO
);
2777 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SET_DMATTRS
);
2778 error
= xfs_trans_reserve(tp
, 0, XFS_ICHANGE_LOG_RES (mp
), 0, 0, 0);
2780 xfs_trans_cancel(tp
, 0);
2783 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2784 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
2786 ip
->i_d
.di_dmevmask
= evmask
;
2787 ip
->i_d
.di_dmstate
= state
;
2789 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2791 error
= xfs_trans_commit(tp
, 0);
2801 xfs_itrace_entry(ip
);
2803 ASSERT(!VN_MAPPED(VFS_I(ip
)));
2805 /* bad inode, get out here ASAP */
2806 if (VN_BAD(VFS_I(ip
))) {
2813 ASSERT(XFS_FORCED_SHUTDOWN(ip
->i_mount
) || ip
->i_delayed_blks
== 0);
2816 * Make sure the atime in the XFS inode is correct before freeing the
2819 xfs_synchronize_atime(ip
);
2822 * If we have nothing to flush with this inode then complete the
2823 * teardown now, otherwise break the link between the xfs inode and the
2824 * linux inode and clean up the xfs inode later. This avoids flushing
2825 * the inode to disk during the delete operation itself.
2827 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2828 * first to ensure that xfs_iunpin() will never see an xfs inode
2829 * that has a linux inode being reclaimed. Synchronisation is provided
2830 * by the i_flags_lock.
2832 if (!ip
->i_update_core
&& (ip
->i_itemp
== NULL
)) {
2833 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2835 xfs_iflags_set(ip
, XFS_IRECLAIMABLE
);
2836 return xfs_reclaim_inode(ip
, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC
);
2838 xfs_inode_set_reclaim_tag(ip
);
2843 * xfs_alloc_file_space()
2844 * This routine allocates disk space for the given file.
2846 * If alloc_type == 0, this request is for an ALLOCSP type
2847 * request which will change the file size. In this case, no
2848 * DMAPI event will be generated by the call. A TRUNCATE event
2849 * will be generated later by xfs_setattr.
2851 * If alloc_type != 0, this request is for a RESVSP type
2852 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2853 * lower block boundary byte address is less than the file's
2862 xfs_alloc_file_space(
2869 xfs_mount_t
*mp
= ip
->i_mount
;
2871 xfs_filblks_t allocated_fsb
;
2872 xfs_filblks_t allocatesize_fsb
;
2873 xfs_extlen_t extsz
, temp
;
2874 xfs_fileoff_t startoffset_fsb
;
2875 xfs_fsblock_t firstfsb
;
2881 xfs_bmbt_irec_t imaps
[1], *imapp
;
2882 xfs_bmap_free_t free_list
;
2883 uint qblocks
, resblks
, resrtextents
;
2887 xfs_itrace_entry(ip
);
2889 if (XFS_FORCED_SHUTDOWN(mp
))
2890 return XFS_ERROR(EIO
);
2892 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
2896 return XFS_ERROR(EINVAL
);
2898 rt
= XFS_IS_REALTIME_INODE(ip
);
2899 extsz
= xfs_get_extsz_hint(ip
);
2904 bmapi_flag
= XFS_BMAPI_WRITE
| (alloc_type
? XFS_BMAPI_PREALLOC
: 0);
2905 startoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
2906 allocatesize_fsb
= XFS_B_TO_FSB(mp
, count
);
2908 /* Generate a DMAPI event if needed. */
2909 if (alloc_type
!= 0 && offset
< ip
->i_size
&&
2910 (attr_flags
& XFS_ATTR_DMI
) == 0 &&
2911 DM_EVENT_ENABLED(ip
, DM_EVENT_WRITE
)) {
2912 xfs_off_t end_dmi_offset
;
2914 end_dmi_offset
= offset
+len
;
2915 if (end_dmi_offset
> ip
->i_size
)
2916 end_dmi_offset
= ip
->i_size
;
2917 error
= XFS_SEND_DATA(mp
, DM_EVENT_WRITE
, ip
, offset
,
2918 end_dmi_offset
- offset
, 0, NULL
);
2924 * Allocate file space until done or until there is an error
2927 while (allocatesize_fsb
&& !error
) {
2931 * Determine space reservations for data/realtime.
2933 if (unlikely(extsz
)) {
2934 s
= startoffset_fsb
;
2937 e
= startoffset_fsb
+ allocatesize_fsb
;
2938 if ((temp
= do_mod(startoffset_fsb
, extsz
)))
2940 if ((temp
= do_mod(e
, extsz
)))
2944 e
= allocatesize_fsb
;
2948 resrtextents
= qblocks
= (uint
)(e
- s
);
2949 resrtextents
/= mp
->m_sb
.sb_rextsize
;
2950 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
2951 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
2954 resblks
= qblocks
= \
2955 XFS_DIOSTRAT_SPACE_RES(mp
, (uint
)(e
- s
));
2956 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
2960 * Allocate and setup the transaction.
2962 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
2963 error
= xfs_trans_reserve(tp
, resblks
,
2964 XFS_WRITE_LOG_RES(mp
), resrtextents
,
2965 XFS_TRANS_PERM_LOG_RES
,
2966 XFS_WRITE_LOG_COUNT
);
2968 * Check for running out of space
2972 * Free the transaction structure.
2974 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
2975 xfs_trans_cancel(tp
, 0);
2978 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2979 error
= XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, tp
, ip
,
2980 qblocks
, 0, quota_flag
);
2984 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
2985 xfs_trans_ihold(tp
, ip
);
2988 * Issue the xfs_bmapi() call to allocate the blocks
2990 XFS_BMAP_INIT(&free_list
, &firstfsb
);
2991 error
= xfs_bmapi(tp
, ip
, startoffset_fsb
,
2992 allocatesize_fsb
, bmapi_flag
,
2993 &firstfsb
, 0, imapp
, &nimaps
,
3000 * Complete the transaction
3002 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
3007 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3008 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3013 allocated_fsb
= imapp
->br_blockcount
;
3016 error
= XFS_ERROR(ENOSPC
);
3020 startoffset_fsb
+= allocated_fsb
;
3021 allocatesize_fsb
-= allocated_fsb
;
3024 if (error
== ENOSPC
&& (attr_flags
& XFS_ATTR_DMI
) == 0 &&
3025 DM_EVENT_ENABLED(ip
, DM_EVENT_NOSPACE
)) {
3026 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_NOSPACE
,
3029 NULL
, NULL
, 0, 0, 0); /* Delay flag intentionally unused */
3031 goto retry
; /* Maybe DMAPI app. has made space */
3032 /* else fall through with error from XFS_SEND_DATA */
3037 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
3038 xfs_bmap_cancel(&free_list
);
3039 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp
, tp
, ip
, qblocks
, 0, quota_flag
);
3041 error1
: /* Just cancel transaction */
3042 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
3043 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3044 goto dmapi_enospc_check
;
3048 * Zero file bytes between startoff and endoff inclusive.
3049 * The iolock is held exclusive and no blocks are buffered.
3051 * This function is used by xfs_free_file_space() to zero
3052 * partial blocks when the range to free is not block aligned.
3053 * When unreserving space with boundaries that are not block
3054 * aligned we round up the start and round down the end
3055 * boundaries and then use this function to zero the parts of
3056 * the blocks that got dropped during the rounding.
3059 xfs_zero_remaining_bytes(
3064 xfs_bmbt_irec_t imap
;
3065 xfs_fileoff_t offset_fsb
;
3066 xfs_off_t lastoffset
;
3069 xfs_mount_t
*mp
= ip
->i_mount
;
3074 * Avoid doing I/O beyond eof - it's not necessary
3075 * since nothing can read beyond eof. The space will
3076 * be zeroed when the file is extended anyway.
3078 if (startoff
>= ip
->i_size
)
3081 if (endoff
> ip
->i_size
)
3082 endoff
= ip
->i_size
;
3084 bp
= xfs_buf_get_noaddr(mp
->m_sb
.sb_blocksize
,
3085 XFS_IS_REALTIME_INODE(ip
) ?
3086 mp
->m_rtdev_targp
: mp
->m_ddev_targp
);
3088 for (offset
= startoff
; offset
<= endoff
; offset
= lastoffset
+ 1) {
3089 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
3091 error
= xfs_bmapi(NULL
, ip
, offset_fsb
, 1, 0,
3092 NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
3093 if (error
|| nimap
< 1)
3095 ASSERT(imap
.br_blockcount
>= 1);
3096 ASSERT(imap
.br_startoff
== offset_fsb
);
3097 lastoffset
= XFS_FSB_TO_B(mp
, imap
.br_startoff
+ 1) - 1;
3098 if (lastoffset
> endoff
)
3099 lastoffset
= endoff
;
3100 if (imap
.br_startblock
== HOLESTARTBLOCK
)
3102 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
3103 if (imap
.br_state
== XFS_EXT_UNWRITTEN
)
3106 XFS_BUF_UNWRITE(bp
);
3108 XFS_BUF_SET_ADDR(bp
, XFS_FSB_TO_DB(ip
, imap
.br_startblock
));
3110 error
= xfs_iowait(bp
);
3112 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3113 mp
, bp
, XFS_BUF_ADDR(bp
));
3116 memset(XFS_BUF_PTR(bp
) +
3117 (offset
- XFS_FSB_TO_B(mp
, imap
.br_startoff
)),
3118 0, lastoffset
- offset
+ 1);
3123 error
= xfs_iowait(bp
);
3125 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3126 mp
, bp
, XFS_BUF_ADDR(bp
));
3135 * xfs_free_file_space()
3136 * This routine frees disk space for the given file.
3138 * This routine is only called by xfs_change_file_space
3139 * for an UNRESVSP type call.
3147 xfs_free_file_space(
3155 xfs_off_t end_dmi_offset
;
3156 xfs_fileoff_t endoffset_fsb
;
3158 xfs_fsblock_t firstfsb
;
3159 xfs_bmap_free_t free_list
;
3160 xfs_bmbt_irec_t imap
;
3168 xfs_fileoff_t startoffset_fsb
;
3170 int need_iolock
= 1;
3174 xfs_itrace_entry(ip
);
3176 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
3180 if (len
<= 0) /* if nothing being freed */
3182 rt
= XFS_IS_REALTIME_INODE(ip
);
3183 startoffset_fsb
= XFS_B_TO_FSB(mp
, offset
);
3184 end_dmi_offset
= offset
+ len
;
3185 endoffset_fsb
= XFS_B_TO_FSBT(mp
, end_dmi_offset
);
3187 if (offset
< ip
->i_size
&& (attr_flags
& XFS_ATTR_DMI
) == 0 &&
3188 DM_EVENT_ENABLED(ip
, DM_EVENT_WRITE
)) {
3189 if (end_dmi_offset
> ip
->i_size
)
3190 end_dmi_offset
= ip
->i_size
;
3191 error
= XFS_SEND_DATA(mp
, DM_EVENT_WRITE
, ip
,
3192 offset
, end_dmi_offset
- offset
,
3193 AT_DELAY_FLAG(attr_flags
), NULL
);
3198 if (attr_flags
& XFS_ATTR_NOLOCK
)
3201 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
3202 vn_iowait(ip
); /* wait for the completion of any pending DIOs */
3205 rounding
= max_t(uint
, 1 << mp
->m_sb
.sb_blocklog
, PAGE_CACHE_SIZE
);
3206 ioffset
= offset
& ~(rounding
- 1);
3208 if (VN_CACHED(VFS_I(ip
)) != 0) {
3209 xfs_inval_cached_trace(ip
, ioffset
, -1, ioffset
, -1);
3210 error
= xfs_flushinval_pages(ip
, ioffset
, -1, FI_REMAPF_LOCKED
);
3212 goto out_unlock_iolock
;
3216 * Need to zero the stuff we're not freeing, on disk.
3217 * If its a realtime file & can't use unwritten extents then we
3218 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3219 * will take care of it for us.
3221 if (rt
&& !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
3223 error
= xfs_bmapi(NULL
, ip
, startoffset_fsb
,
3224 1, 0, NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
3226 goto out_unlock_iolock
;
3227 ASSERT(nimap
== 0 || nimap
== 1);
3228 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
3231 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
3232 block
= imap
.br_startblock
;
3233 mod
= do_div(block
, mp
->m_sb
.sb_rextsize
);
3235 startoffset_fsb
+= mp
->m_sb
.sb_rextsize
- mod
;
3238 error
= xfs_bmapi(NULL
, ip
, endoffset_fsb
- 1,
3239 1, 0, NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
3241 goto out_unlock_iolock
;
3242 ASSERT(nimap
== 0 || nimap
== 1);
3243 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
3244 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
3246 if (mod
&& (mod
!= mp
->m_sb
.sb_rextsize
))
3247 endoffset_fsb
-= mod
;
3250 if ((done
= (endoffset_fsb
<= startoffset_fsb
)))
3252 * One contiguous piece to clear
3254 error
= xfs_zero_remaining_bytes(ip
, offset
, offset
+ len
- 1);
3257 * Some full blocks, possibly two pieces to clear
3259 if (offset
< XFS_FSB_TO_B(mp
, startoffset_fsb
))
3260 error
= xfs_zero_remaining_bytes(ip
, offset
,
3261 XFS_FSB_TO_B(mp
, startoffset_fsb
) - 1);
3263 XFS_FSB_TO_B(mp
, endoffset_fsb
) < offset
+ len
)
3264 error
= xfs_zero_remaining_bytes(ip
,
3265 XFS_FSB_TO_B(mp
, endoffset_fsb
),
3270 * free file space until done or until there is an error
3272 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
3273 while (!error
&& !done
) {
3276 * allocate and setup the transaction. Allow this
3277 * transaction to dip into the reserve blocks to ensure
3278 * the freeing of the space succeeds at ENOSPC.
3280 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
3281 tp
->t_flags
|= XFS_TRANS_RESERVE
;
3282 error
= xfs_trans_reserve(tp
,
3284 XFS_WRITE_LOG_RES(mp
),
3286 XFS_TRANS_PERM_LOG_RES
,
3287 XFS_WRITE_LOG_COUNT
);
3290 * check for running out of space
3294 * Free the transaction structure.
3296 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
3297 xfs_trans_cancel(tp
, 0);
3300 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3301 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
,
3302 ip
->i_udquot
, ip
->i_gdquot
, resblks
, 0,
3303 XFS_QMOPT_RES_REGBLKS
);
3307 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3308 xfs_trans_ihold(tp
, ip
);
3311 * issue the bunmapi() call to free the blocks
3313 XFS_BMAP_INIT(&free_list
, &firstfsb
);
3314 error
= xfs_bunmapi(tp
, ip
, startoffset_fsb
,
3315 endoffset_fsb
- startoffset_fsb
,
3316 0, 2, &firstfsb
, &free_list
, NULL
, &done
);
3322 * complete the transaction
3324 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
3329 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3330 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3335 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
3339 xfs_bmap_cancel(&free_list
);
3341 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
3342 xfs_iunlock(ip
, need_iolock
? (XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
) :
3348 * xfs_change_file_space()
3349 * This routine allocates or frees disk space for the given file.
3350 * The user specified parameters are checked for alignment and size
3359 xfs_change_file_space(
3366 xfs_mount_t
*mp
= ip
->i_mount
;
3371 xfs_off_t startoffset
;
3376 xfs_itrace_entry(ip
);
3378 if (!S_ISREG(ip
->i_d
.di_mode
))
3379 return XFS_ERROR(EINVAL
);
3381 switch (bf
->l_whence
) {
3382 case 0: /*SEEK_SET*/
3384 case 1: /*SEEK_CUR*/
3385 bf
->l_start
+= offset
;
3387 case 2: /*SEEK_END*/
3388 bf
->l_start
+= ip
->i_size
;
3391 return XFS_ERROR(EINVAL
);
3394 llen
= bf
->l_len
> 0 ? bf
->l_len
- 1 : bf
->l_len
;
3396 if ( (bf
->l_start
< 0)
3397 || (bf
->l_start
> XFS_MAXIOFFSET(mp
))
3398 || (bf
->l_start
+ llen
< 0)
3399 || (bf
->l_start
+ llen
> XFS_MAXIOFFSET(mp
)))
3400 return XFS_ERROR(EINVAL
);
3404 startoffset
= bf
->l_start
;
3408 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3410 * These calls do NOT zero the data space allocated to the file,
3411 * nor do they change the file size.
3413 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3415 * These calls cause the new file data to be zeroed and the file
3416 * size to be changed.
3418 setprealloc
= clrprealloc
= 0;
3421 case XFS_IOC_RESVSP
:
3422 case XFS_IOC_RESVSP64
:
3423 error
= xfs_alloc_file_space(ip
, startoffset
, bf
->l_len
,
3430 case XFS_IOC_UNRESVSP
:
3431 case XFS_IOC_UNRESVSP64
:
3432 if ((error
= xfs_free_file_space(ip
, startoffset
, bf
->l_len
,
3437 case XFS_IOC_ALLOCSP
:
3438 case XFS_IOC_ALLOCSP64
:
3439 case XFS_IOC_FREESP
:
3440 case XFS_IOC_FREESP64
:
3441 if (startoffset
> fsize
) {
3442 error
= xfs_alloc_file_space(ip
, fsize
,
3443 startoffset
- fsize
, 0, attr_flags
);
3448 iattr
.ia_valid
= ATTR_SIZE
;
3449 iattr
.ia_size
= startoffset
;
3451 error
= xfs_setattr(ip
, &iattr
, attr_flags
);
3461 return XFS_ERROR(EINVAL
);
3465 * update the inode timestamp, mode, and prealloc flag bits
3467 tp
= xfs_trans_alloc(mp
, XFS_TRANS_WRITEID
);
3469 if ((error
= xfs_trans_reserve(tp
, 0, XFS_WRITEID_LOG_RES(mp
),
3472 xfs_trans_cancel(tp
, 0);
3476 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3478 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3479 xfs_trans_ihold(tp
, ip
);
3481 if ((attr_flags
& XFS_ATTR_DMI
) == 0) {
3482 ip
->i_d
.di_mode
&= ~S_ISUID
;
3485 * Note that we don't have to worry about mandatory
3486 * file locking being disabled here because we only
3487 * clear the S_ISGID bit if the Group execute bit is
3488 * on, but if it was on then mandatory locking wouldn't
3489 * have been enabled.
3491 if (ip
->i_d
.di_mode
& S_IXGRP
)
3492 ip
->i_d
.di_mode
&= ~S_ISGID
;
3494 xfs_ichgtime(ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
3497 ip
->i_d
.di_flags
|= XFS_DIFLAG_PREALLOC
;
3498 else if (clrprealloc
)
3499 ip
->i_d
.di_flags
&= ~XFS_DIFLAG_PREALLOC
;
3501 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
3502 xfs_trans_set_sync(tp
);
3504 error
= xfs_trans_commit(tp
, 0);
3506 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);