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
);
85 xfs_mount_t
*mp
= ip
->i_mount
;
86 struct inode
*inode
= VFS_I(ip
);
87 int mask
= iattr
->ia_valid
;
95 struct xfs_dquot
*udqp
, *gdqp
, *olddquot1
, *olddquot2
;
101 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
102 return XFS_ERROR(EROFS
);
104 if (XFS_FORCED_SHUTDOWN(mp
))
105 return XFS_ERROR(EIO
);
107 olddquot1
= olddquot2
= NULL
;
111 * If disk quotas is on, we make sure that the dquots do exist on disk,
112 * before we start any other transactions. Trying to do this later
113 * is messy. We don't care to take a readlock to look at the ids
114 * in inode here, because we can't hold it across the trans_reserve.
115 * If the IDs do change before we take the ilock, we're covered
116 * because the i_*dquot fields will get updated anyway.
118 if (XFS_IS_QUOTA_ON(mp
) && (mask
& (ATTR_UID
|ATTR_GID
))) {
121 if ((mask
& ATTR_UID
) && XFS_IS_UQUOTA_ON(mp
)) {
123 qflags
|= XFS_QMOPT_UQUOTA
;
125 uid
= ip
->i_d
.di_uid
;
127 if ((mask
& ATTR_GID
) && XFS_IS_GQUOTA_ON(mp
)) {
129 qflags
|= XFS_QMOPT_GQUOTA
;
131 gid
= ip
->i_d
.di_gid
;
135 * We take a reference when we initialize udqp and gdqp,
136 * so it is important that we never blindly double trip on
137 * the same variable. See xfs_create() for an example.
139 ASSERT(udqp
== NULL
);
140 ASSERT(gdqp
== NULL
);
141 code
= XFS_QM_DQVOPALLOC(mp
, ip
, uid
, gid
, ip
->i_d
.di_projid
,
142 qflags
, &udqp
, &gdqp
);
148 * For the other attributes, we acquire the inode lock and
149 * first do an error checking pass.
152 lock_flags
= XFS_ILOCK_EXCL
;
153 if (flags
& XFS_ATTR_NOLOCK
)
155 if (!(mask
& ATTR_SIZE
)) {
156 if ((mask
!= (ATTR_CTIME
|ATTR_ATIME
|ATTR_MTIME
)) ||
157 (mp
->m_flags
& XFS_MOUNT_WSYNC
)) {
158 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SETATTR_NOT_SIZE
);
160 if ((code
= xfs_trans_reserve(tp
, 0,
161 XFS_ICHANGE_LOG_RES(mp
), 0,
168 if (DM_EVENT_ENABLED(ip
, DM_EVENT_TRUNCATE
) &&
169 !(flags
& XFS_ATTR_DMI
)) {
170 int dmflags
= AT_DELAY_FLAG(flags
) | DM_SEM_FLAG_WR
;
171 code
= XFS_SEND_DATA(mp
, DM_EVENT_TRUNCATE
, ip
,
172 iattr
->ia_size
, 0, dmflags
, NULL
);
179 lock_flags
|= XFS_IOLOCK_EXCL
;
182 xfs_ilock(ip
, lock_flags
);
184 /* boolean: are we the file owner? */
185 file_owner
= (current_fsuid() == ip
->i_d
.di_uid
);
188 * Change various properties of a file.
189 * Only the owner or users with CAP_FOWNER
190 * capability may do these things.
192 if (mask
& (ATTR_MODE
|ATTR_UID
|ATTR_GID
)) {
194 * CAP_FOWNER overrides the following restrictions:
196 * The user ID of the calling process must be equal
197 * to the file owner ID, except in cases where the
198 * CAP_FSETID capability is applicable.
200 if (!file_owner
&& !capable(CAP_FOWNER
)) {
201 code
= XFS_ERROR(EPERM
);
206 * CAP_FSETID overrides the following restrictions:
208 * The effective user ID of the calling process shall match
209 * the file owner when setting the set-user-ID and
210 * set-group-ID bits on that file.
212 * The effective group ID or one of the supplementary group
213 * IDs of the calling process shall match the group owner of
214 * the file when setting the set-group-ID bit on that file
216 if (mask
& ATTR_MODE
) {
219 if ((iattr
->ia_mode
& S_ISUID
) && !file_owner
)
221 if ((iattr
->ia_mode
& S_ISGID
) &&
222 !in_group_p((gid_t
)ip
->i_d
.di_gid
))
225 /* Linux allows this, Irix doesn't. */
226 if ((iattr
->ia_mode
& S_ISVTX
) && !S_ISDIR(ip
->i_d
.di_mode
))
229 if (m
&& !capable(CAP_FSETID
))
230 iattr
->ia_mode
&= ~m
;
235 * Change file ownership. Must be the owner or privileged.
236 * If the system was configured with the "restricted_chown"
237 * option, the owner is not permitted to give away the file,
238 * and can change the group id only to a group of which he
239 * or she is a member.
241 if (mask
& (ATTR_UID
|ATTR_GID
)) {
243 * These IDs could have changed since we last looked at them.
244 * But, we're assured that if the ownership did change
245 * while we didn't have the inode locked, inode's dquot(s)
246 * would have changed also.
248 iuid
= ip
->i_d
.di_uid
;
249 igid
= ip
->i_d
.di_gid
;
250 gid
= (mask
& ATTR_GID
) ? iattr
->ia_gid
: igid
;
251 uid
= (mask
& ATTR_UID
) ? iattr
->ia_uid
: iuid
;
254 * CAP_CHOWN overrides the following restrictions:
256 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
257 * shall override the restriction that a process cannot
258 * change the user ID of a file it owns and the restriction
259 * that the group ID supplied to the chown() function
260 * shall be equal to either the group ID or one of the
261 * supplementary group IDs of the calling process.
263 if (restricted_chown
&&
264 (iuid
!= uid
|| (igid
!= gid
&&
265 !in_group_p((gid_t
)gid
))) &&
266 !capable(CAP_CHOWN
)) {
267 code
= XFS_ERROR(EPERM
);
271 * Do a quota reservation only if uid/gid is actually
274 if ((XFS_IS_UQUOTA_ON(mp
) && iuid
!= uid
) ||
275 (XFS_IS_GQUOTA_ON(mp
) && igid
!= gid
)) {
277 code
= XFS_QM_DQVOPCHOWNRESV(mp
, tp
, ip
, udqp
, gdqp
,
278 capable(CAP_FOWNER
) ?
279 XFS_QMOPT_FORCE_RES
: 0);
280 if (code
) /* out of quota */
286 * Truncate file. Must have write permission and not be a directory.
288 if (mask
& ATTR_SIZE
) {
289 /* Short circuit the truncate case for zero length files */
290 if (iattr
->ia_size
== 0 &&
291 ip
->i_size
== 0 && ip
->i_d
.di_nextents
== 0) {
292 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
293 lock_flags
&= ~XFS_ILOCK_EXCL
;
294 if (mask
& ATTR_CTIME
)
295 xfs_ichgtime(ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
300 if (S_ISDIR(ip
->i_d
.di_mode
)) {
301 code
= XFS_ERROR(EISDIR
);
303 } else if (!S_ISREG(ip
->i_d
.di_mode
)) {
304 code
= XFS_ERROR(EINVAL
);
308 * Make sure that the dquots are attached to the inode.
310 if ((code
= XFS_QM_DQATTACH(mp
, ip
, XFS_QMOPT_ILOCKED
)))
315 * Change file access or modified times.
317 if (mask
& (ATTR_ATIME
|ATTR_MTIME
)) {
319 if ((mask
& (ATTR_MTIME_SET
|ATTR_ATIME_SET
)) &&
320 !capable(CAP_FOWNER
)) {
321 code
= XFS_ERROR(EPERM
);
328 * Now we can make the changes. Before we join the inode
329 * to the transaction, if ATTR_SIZE is set then take care of
330 * the part of the truncation that must be done without the
331 * inode lock. This needs to be done before joining the inode
332 * to the transaction, because the inode cannot be unlocked
333 * once it is a part of the transaction.
335 if (mask
& ATTR_SIZE
) {
337 if (iattr
->ia_size
> ip
->i_size
) {
339 * Do the first part of growing a file: zero any data
340 * in the last block that is beyond the old EOF. We
341 * need to do this before the inode is joined to the
342 * transaction to modify the i_size.
344 code
= xfs_zero_eof(ip
, iattr
->ia_size
, ip
->i_size
);
346 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
349 * We are going to log the inode size change in this
350 * transaction so any previous writes that are beyond the on
351 * disk EOF and the new EOF that have not been written out need
352 * to be written here. If we do not write the data out, we
353 * expose ourselves to the null files problem.
355 * Only flush from the on disk size to the smaller of the in
356 * memory file size or the new size as that's the range we
357 * really care about here and prevents waiting for other data
358 * not within the range we care about here.
361 ip
->i_size
!= ip
->i_d
.di_size
&&
362 iattr
->ia_size
> ip
->i_d
.di_size
) {
363 code
= xfs_flush_pages(ip
,
364 ip
->i_d
.di_size
, iattr
->ia_size
,
365 XFS_B_ASYNC
, FI_NONE
);
368 /* wait for all I/O to complete */
372 code
= xfs_itruncate_data(ip
, iattr
->ia_size
);
375 lock_flags
&= ~XFS_ILOCK_EXCL
;
376 ASSERT(lock_flags
== XFS_IOLOCK_EXCL
);
379 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SETATTR_SIZE
);
380 if ((code
= xfs_trans_reserve(tp
, 0,
381 XFS_ITRUNCATE_LOG_RES(mp
), 0,
382 XFS_TRANS_PERM_LOG_RES
,
383 XFS_ITRUNCATE_LOG_COUNT
))) {
384 xfs_trans_cancel(tp
, 0);
386 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
389 commit_flags
= XFS_TRANS_RELEASE_LOG_RES
;
390 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
394 xfs_trans_ijoin(tp
, ip
, lock_flags
);
395 xfs_trans_ihold(tp
, ip
);
399 * Truncate file. Must have write permission and not be a directory.
401 if (mask
& ATTR_SIZE
) {
403 * Only change the c/mtime if we are changing the size
404 * or we are explicitly asked to change it. This handles
405 * the semantic difference between truncate() and ftruncate()
406 * as implemented in the VFS.
408 if (iattr
->ia_size
!= ip
->i_size
|| (mask
& ATTR_CTIME
))
409 timeflags
|= XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
;
411 if (iattr
->ia_size
> ip
->i_size
) {
412 ip
->i_d
.di_size
= iattr
->ia_size
;
413 ip
->i_size
= iattr
->ia_size
;
414 if (!(flags
& XFS_ATTR_DMI
))
415 xfs_ichgtime(ip
, XFS_ICHGTIME_CHG
);
416 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
417 } else if (iattr
->ia_size
<= ip
->i_size
||
418 (iattr
->ia_size
== 0 && ip
->i_d
.di_nextents
)) {
420 * signal a sync transaction unless
421 * we're truncating an already unlinked
422 * file on a wsync filesystem
424 code
= xfs_itruncate_finish(&tp
, ip
, iattr
->ia_size
,
426 ((ip
->i_d
.di_nlink
!= 0 ||
427 !(mp
->m_flags
& XFS_MOUNT_WSYNC
))
432 * Truncated "down", so we're removing references
433 * to old data here - if we now delay flushing for
434 * a long time, we expose ourselves unduly to the
435 * notorious NULL files problem. So, we mark this
436 * vnode and flush it when the file is closed, and
437 * do not wait the usual (long) time for writeout.
439 xfs_iflags_set(ip
, XFS_ITRUNCATED
);
444 * Change file access modes.
446 if (mask
& ATTR_MODE
) {
447 ip
->i_d
.di_mode
&= S_IFMT
;
448 ip
->i_d
.di_mode
|= iattr
->ia_mode
& ~S_IFMT
;
450 inode
->i_mode
&= S_IFMT
;
451 inode
->i_mode
|= iattr
->ia_mode
& ~S_IFMT
;
453 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
454 timeflags
|= XFS_ICHGTIME_CHG
;
458 * Change file ownership. Must be the owner or privileged.
459 * If the system was configured with the "restricted_chown"
460 * option, the owner is not permitted to give away the file,
461 * and can change the group id only to a group of which he
462 * or she is a member.
464 if (mask
& (ATTR_UID
|ATTR_GID
)) {
466 * CAP_FSETID overrides the following restrictions:
468 * The set-user-ID and set-group-ID bits of a file will be
469 * cleared upon successful return from chown()
471 if ((ip
->i_d
.di_mode
& (S_ISUID
|S_ISGID
)) &&
472 !capable(CAP_FSETID
)) {
473 ip
->i_d
.di_mode
&= ~(S_ISUID
|S_ISGID
);
477 * Change the ownerships and register quota modifications
478 * in the transaction.
481 if (XFS_IS_UQUOTA_ON(mp
)) {
482 ASSERT(mask
& ATTR_UID
);
484 olddquot1
= XFS_QM_DQVOPCHOWN(mp
, tp
, ip
,
485 &ip
->i_udquot
, udqp
);
487 ip
->i_d
.di_uid
= uid
;
491 if (XFS_IS_GQUOTA_ON(mp
)) {
492 ASSERT(!XFS_IS_PQUOTA_ON(mp
));
493 ASSERT(mask
& ATTR_GID
);
495 olddquot2
= XFS_QM_DQVOPCHOWN(mp
, tp
, ip
,
496 &ip
->i_gdquot
, gdqp
);
498 ip
->i_d
.di_gid
= gid
;
502 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
503 timeflags
|= XFS_ICHGTIME_CHG
;
508 * Change file access or modified times.
510 if (mask
& (ATTR_ATIME
|ATTR_MTIME
)) {
511 if (mask
& ATTR_ATIME
) {
512 inode
->i_atime
= iattr
->ia_atime
;
513 ip
->i_d
.di_atime
.t_sec
= iattr
->ia_atime
.tv_sec
;
514 ip
->i_d
.di_atime
.t_nsec
= iattr
->ia_atime
.tv_nsec
;
515 ip
->i_update_core
= 1;
517 if (mask
& ATTR_MTIME
) {
518 inode
->i_mtime
= iattr
->ia_mtime
;
519 ip
->i_d
.di_mtime
.t_sec
= iattr
->ia_mtime
.tv_sec
;
520 ip
->i_d
.di_mtime
.t_nsec
= iattr
->ia_mtime
.tv_nsec
;
521 timeflags
&= ~XFS_ICHGTIME_MOD
;
522 timeflags
|= XFS_ICHGTIME_CHG
;
524 if (tp
&& (mask
& (ATTR_MTIME_SET
|ATTR_ATIME_SET
)))
525 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
529 * Change file inode change time only if ATTR_CTIME set
530 * AND we have been called by a DMI function.
533 if ((flags
& XFS_ATTR_DMI
) && (mask
& ATTR_CTIME
)) {
534 inode
->i_ctime
= iattr
->ia_ctime
;
535 ip
->i_d
.di_ctime
.t_sec
= iattr
->ia_ctime
.tv_sec
;
536 ip
->i_d
.di_ctime
.t_nsec
= iattr
->ia_ctime
.tv_nsec
;
537 ip
->i_update_core
= 1;
538 timeflags
&= ~XFS_ICHGTIME_CHG
;
542 * Send out timestamp changes that need to be set to the
543 * current time. Not done when called by a DMI function.
545 if (timeflags
&& !(flags
& XFS_ATTR_DMI
))
546 xfs_ichgtime(ip
, timeflags
);
548 XFS_STATS_INC(xs_ig_attrchg
);
551 * If this is a synchronous mount, make sure that the
552 * transaction goes to disk before returning to the user.
553 * This is slightly sub-optimal in that truncates require
554 * two sync transactions instead of one for wsync filesystems.
555 * One for the truncate and one for the timestamps since we
556 * don't want to change the timestamps unless we're sure the
557 * truncate worked. Truncates are less than 1% of the laddis
558 * mix so this probably isn't worth the trouble to optimize.
562 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
563 xfs_trans_set_sync(tp
);
565 code
= xfs_trans_commit(tp
, commit_flags
);
568 xfs_iunlock(ip
, lock_flags
);
571 * Release any dquot(s) the inode had kept before chown.
573 XFS_QM_DQRELE(mp
, olddquot1
);
574 XFS_QM_DQRELE(mp
, olddquot2
);
575 XFS_QM_DQRELE(mp
, udqp
);
576 XFS_QM_DQRELE(mp
, gdqp
);
582 if (DM_EVENT_ENABLED(ip
, DM_EVENT_ATTRIBUTE
) &&
583 !(flags
& XFS_ATTR_DMI
)) {
584 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_ATTRIBUTE
, ip
, DM_RIGHT_NULL
,
585 NULL
, DM_RIGHT_NULL
, NULL
, NULL
,
586 0, 0, AT_DELAY_FLAG(flags
));
591 commit_flags
|= XFS_TRANS_ABORT
;
594 XFS_QM_DQRELE(mp
, udqp
);
595 XFS_QM_DQRELE(mp
, gdqp
);
597 xfs_trans_cancel(tp
, commit_flags
);
599 if (lock_flags
!= 0) {
600 xfs_iunlock(ip
, lock_flags
);
606 * The maximum pathlen is 1024 bytes. Since the minimum file system
607 * blocksize is 512 bytes, we can get a max of 2 extents back from
610 #define SYMLINK_MAPS 2
617 xfs_mount_t
*mp
= ip
->i_mount
;
618 int pathlen
= ip
->i_d
.di_size
;
619 int nmaps
= SYMLINK_MAPS
;
620 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
627 error
= xfs_bmapi(NULL
, ip
, 0, XFS_B_TO_FSB(mp
, pathlen
), 0, NULL
, 0,
628 mval
, &nmaps
, NULL
, NULL
);
632 for (n
= 0; n
< nmaps
; n
++) {
633 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
634 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
636 bp
= xfs_buf_read(mp
->m_ddev_targp
, d
, BTOBB(byte_cnt
), 0);
637 error
= XFS_BUF_GETERROR(bp
);
639 xfs_ioerror_alert("xfs_readlink",
640 ip
->i_mount
, bp
, XFS_BUF_ADDR(bp
));
644 if (pathlen
< byte_cnt
)
648 memcpy(link
, XFS_BUF_PTR(bp
), byte_cnt
);
652 link
[ip
->i_d
.di_size
] = '\0';
664 xfs_mount_t
*mp
= ip
->i_mount
;
668 xfs_itrace_entry(ip
);
670 if (XFS_FORCED_SHUTDOWN(mp
))
671 return XFS_ERROR(EIO
);
673 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
675 ASSERT((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
);
676 ASSERT(ip
->i_d
.di_size
<= MAXPATHLEN
);
678 pathlen
= ip
->i_d
.di_size
;
682 if (ip
->i_df
.if_flags
& XFS_IFINLINE
) {
683 memcpy(link
, ip
->i_df
.if_u1
.if_data
, pathlen
);
684 link
[pathlen
] = '\0';
686 error
= xfs_readlink_bmap(ip
, link
);
690 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
697 * This is called to sync the inode and its data out to disk. We need to hold
698 * the I/O lock while flushing the data, and the inode lock while flushing the
699 * inode. The inode lock CANNOT be held while flushing the data, so acquire
700 * after we're done with that.
708 int log_flushed
= 0, changed
= 1;
710 xfs_itrace_entry(ip
);
712 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
713 return XFS_ERROR(EIO
);
715 /* capture size updates in I/O completion before writing the inode. */
716 error
= filemap_fdatawait(VFS_I(ip
)->i_mapping
);
718 return XFS_ERROR(error
);
721 * We always need to make sure that the required inode state is safe on
722 * disk. The vnode might be clean but we still might need to force the
723 * log because of committed transactions that haven't hit the disk yet.
724 * Likewise, there could be unflushed non-transactional changes to the
725 * inode core that have to go to disk and this requires us to issue
726 * a synchronous transaction to capture these changes correctly.
728 * This code relies on the assumption that if the update_* fields
729 * of the inode are clear and the inode is unpinned then it is clean
730 * and no action is required.
732 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
734 if (!(ip
->i_update_size
|| ip
->i_update_core
)) {
736 * Timestamps/size haven't changed since last inode flush or
737 * inode transaction commit. That means either nothing got
738 * written or a transaction committed which caught the updates.
739 * If the latter happened and the transaction hasn't hit the
740 * disk yet, the inode will be still be pinned. If it is,
744 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
746 if (xfs_ipincount(ip
)) {
747 error
= _xfs_log_force(ip
->i_mount
, (xfs_lsn_t
)0,
748 XFS_LOG_FORCE
| XFS_LOG_SYNC
,
752 * If the inode is not pinned and nothing has changed
753 * we don't need to flush the cache.
759 * Kick off a transaction to log the inode core to get the
760 * updates. The sync transaction will also force the log.
762 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
763 tp
= xfs_trans_alloc(ip
->i_mount
, XFS_TRANS_FSYNC_TS
);
764 error
= xfs_trans_reserve(tp
, 0,
765 XFS_FSYNC_TS_LOG_RES(ip
->i_mount
), 0, 0, 0);
767 xfs_trans_cancel(tp
, 0);
770 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
773 * Note - it's possible that we might have pushed ourselves out
774 * of the way during trans_reserve which would flush the inode.
775 * But there's no guarantee that the inode buffer has actually
776 * gone out yet (it's delwri). Plus the buffer could be pinned
777 * anyway if it's part of an inode in another recent
778 * transaction. So we play it safe and fire off the
779 * transaction anyway.
781 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
782 xfs_trans_ihold(tp
, ip
);
783 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
784 xfs_trans_set_sync(tp
);
785 error
= _xfs_trans_commit(tp
, 0, &log_flushed
);
787 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
790 if ((ip
->i_mount
->m_flags
& XFS_MOUNT_BARRIER
) && changed
) {
792 * If the log write didn't issue an ordered tag we need
793 * to flush the disk cache for the data device now.
796 xfs_blkdev_issue_flush(ip
->i_mount
->m_ddev_targp
);
799 * If this inode is on the RT dev we need to flush that
802 if (XFS_IS_REALTIME_INODE(ip
))
803 xfs_blkdev_issue_flush(ip
->i_mount
->m_rtdev_targp
);
810 * This is called by xfs_inactive to free any blocks beyond eof
811 * when the link count isn't zero and by xfs_dm_punch_hole() when
812 * punching a hole to EOF.
822 xfs_fileoff_t end_fsb
;
823 xfs_fileoff_t last_fsb
;
824 xfs_filblks_t map_len
;
826 xfs_bmbt_irec_t imap
;
827 int use_iolock
= (flags
& XFS_FREE_EOF_LOCK
);
830 * Figure out if there are any blocks beyond the end
831 * of the file. If not, then there is nothing to do.
833 end_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)ip
->i_size
));
834 last_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_MAXIOFFSET(mp
));
835 map_len
= last_fsb
- end_fsb
;
840 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
841 error
= xfs_bmapi(NULL
, ip
, end_fsb
, map_len
, 0,
842 NULL
, 0, &imap
, &nimaps
, NULL
, NULL
);
843 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
845 if (!error
&& (nimaps
!= 0) &&
846 (imap
.br_startblock
!= HOLESTARTBLOCK
||
847 ip
->i_delayed_blks
)) {
849 * Attach the dquots to the inode up front.
851 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
855 * There are blocks after the end of file.
856 * Free them up now by truncating the file to
859 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
862 * Do the xfs_itruncate_start() call before
863 * reserving any log space because
864 * itruncate_start will call into the buffer
866 * do that within a transaction.
869 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
870 error
= xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
,
873 xfs_trans_cancel(tp
, 0);
875 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
879 error
= xfs_trans_reserve(tp
, 0,
880 XFS_ITRUNCATE_LOG_RES(mp
),
881 0, XFS_TRANS_PERM_LOG_RES
,
882 XFS_ITRUNCATE_LOG_COUNT
);
884 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
885 xfs_trans_cancel(tp
, 0);
886 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
890 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
891 xfs_trans_ijoin(tp
, ip
,
894 xfs_trans_ihold(tp
, ip
);
896 error
= xfs_itruncate_finish(&tp
, ip
,
901 * If we get an error at this point we
902 * simply don't bother truncating the file.
906 (XFS_TRANS_RELEASE_LOG_RES
|
909 error
= xfs_trans_commit(tp
,
910 XFS_TRANS_RELEASE_LOG_RES
);
912 xfs_iunlock(ip
, (use_iolock
? (XFS_IOLOCK_EXCL
|XFS_ILOCK_EXCL
)
919 * Free a symlink that has blocks associated with it.
922 xfs_inactive_symlink_rmt(
930 xfs_fsblock_t first_block
;
931 xfs_bmap_free_t free_list
;
934 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
942 ASSERT(ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
));
944 * We're freeing a symlink that has some
945 * blocks allocated to it. Free the
946 * blocks here. We know that we've got
947 * either 1 or 2 extents and that we can
948 * free them all in one bunmapi call.
950 ASSERT(ip
->i_d
.di_nextents
> 0 && ip
->i_d
.di_nextents
<= 2);
951 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
952 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
953 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
954 xfs_trans_cancel(tp
, 0);
959 * Lock the inode, fix the size, and join it to the transaction.
960 * Hold it so in the normal path, we still have it locked for
961 * the second transaction. In the error paths we need it
962 * held so the cancel won't rele it, see below.
964 xfs_ilock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
965 size
= (int)ip
->i_d
.di_size
;
967 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
968 xfs_trans_ihold(tp
, ip
);
969 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
971 * Find the block(s) so we can inval and unmap them.
974 XFS_BMAP_INIT(&free_list
, &first_block
);
975 nmaps
= ARRAY_SIZE(mval
);
976 if ((error
= xfs_bmapi(tp
, ip
, 0, XFS_B_TO_FSB(mp
, size
),
977 XFS_BMAPI_METADATA
, &first_block
, 0, mval
, &nmaps
,
981 * Invalidate the block(s).
983 for (i
= 0; i
< nmaps
; i
++) {
984 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
985 XFS_FSB_TO_DADDR(mp
, mval
[i
].br_startblock
),
986 XFS_FSB_TO_BB(mp
, mval
[i
].br_blockcount
), 0);
987 xfs_trans_binval(tp
, bp
);
990 * Unmap the dead block(s) to the free_list.
992 if ((error
= xfs_bunmapi(tp
, ip
, 0, size
, XFS_BMAPI_METADATA
, nmaps
,
993 &first_block
, &free_list
, NULL
, &done
)))
997 * Commit the first transaction. This logs the EFI and the inode.
999 if ((error
= xfs_bmap_finish(&tp
, &free_list
, &committed
)))
1002 * The transaction must have been committed, since there were
1003 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1004 * The new tp has the extent freeing and EFDs.
1008 * The first xact was committed, so add the inode to the new one.
1009 * Mark it dirty so it will be logged and moved forward in the log as
1010 * part of every commit.
1012 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1013 xfs_trans_ihold(tp
, ip
);
1014 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1016 * Get a new, empty transaction to return to our caller.
1018 ntp
= xfs_trans_dup(tp
);
1020 * Commit the transaction containing extent freeing and EFDs.
1021 * If we get an error on the commit here or on the reserve below,
1022 * we need to unlock the inode since the new transaction doesn't
1023 * have the inode attached.
1025 error
= xfs_trans_commit(tp
, 0);
1028 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1032 * Remove the memory for extent descriptions (just bookkeeping).
1034 if (ip
->i_df
.if_bytes
)
1035 xfs_idata_realloc(ip
, -ip
->i_df
.if_bytes
, XFS_DATA_FORK
);
1036 ASSERT(ip
->i_df
.if_bytes
== 0);
1038 * Put an itruncate log reservation in the new transaction
1041 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
1042 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
1043 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1047 * Return with the inode locked but not joined to the transaction.
1053 xfs_bmap_cancel(&free_list
);
1056 * Have to come here with the inode locked and either
1057 * (held and in the transaction) or (not in the transaction).
1058 * If the inode isn't held then cancel would iput it, but
1059 * that's wrong since this is inactive and the vnode ref
1060 * count is 0 already.
1061 * Cancel won't do anything to the inode if held, but it still
1062 * needs to be locked until the cancel is done, if it was
1063 * joined to the transaction.
1065 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1066 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1073 xfs_inactive_symlink_local(
1079 ASSERT(ip
->i_d
.di_size
<= XFS_IFORK_DSIZE(ip
));
1081 * We're freeing a symlink which fit into
1082 * the inode. Just free the memory used
1083 * to hold the old symlink.
1085 error
= xfs_trans_reserve(*tpp
, 0,
1086 XFS_ITRUNCATE_LOG_RES(ip
->i_mount
),
1087 0, XFS_TRANS_PERM_LOG_RES
,
1088 XFS_ITRUNCATE_LOG_COUNT
);
1091 xfs_trans_cancel(*tpp
, 0);
1095 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1098 * Zero length symlinks _can_ exist.
1100 if (ip
->i_df
.if_bytes
> 0) {
1101 xfs_idata_realloc(ip
,
1102 -(ip
->i_df
.if_bytes
),
1104 ASSERT(ip
->i_df
.if_bytes
== 0);
1118 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
1121 ASSERT(ip
->i_d
.di_forkoff
!= 0);
1122 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1123 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1127 error
= xfs_attr_inactive(ip
);
1131 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1132 error
= xfs_trans_reserve(tp
, 0,
1133 XFS_IFREE_LOG_RES(mp
),
1134 0, XFS_TRANS_PERM_LOG_RES
,
1135 XFS_INACTIVE_LOG_COUNT
);
1139 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1140 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1141 xfs_trans_ihold(tp
, ip
);
1142 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
1144 ASSERT(ip
->i_d
.di_anextents
== 0);
1150 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1151 xfs_trans_cancel(tp
, 0);
1154 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1162 xfs_mount_t
*mp
= ip
->i_mount
;
1165 if (!S_ISREG(ip
->i_d
.di_mode
) || (ip
->i_d
.di_mode
== 0))
1168 /* If this is a read-only mount, don't do this (would generate I/O) */
1169 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
1172 if (!XFS_FORCED_SHUTDOWN(mp
)) {
1176 * If we are using filestreams, and we have an unlinked
1177 * file that we are processing the last close on, then nothing
1178 * will be able to reopen and write to this file. Purge this
1179 * inode from the filestreams cache so that it doesn't delay
1180 * teardown of the inode.
1182 if ((ip
->i_d
.di_nlink
== 0) && xfs_inode_is_filestream(ip
))
1183 xfs_filestream_deassociate(ip
);
1186 * If we previously truncated this file and removed old data
1187 * in the process, we want to initiate "early" writeout on
1188 * the last close. This is an attempt to combat the notorious
1189 * NULL files problem which is particularly noticable from a
1190 * truncate down, buffered (re-)write (delalloc), followed by
1191 * a crash. What we are effectively doing here is
1192 * significantly reducing the time window where we'd otherwise
1193 * be exposed to that problem.
1195 truncated
= xfs_iflags_test_and_clear(ip
, XFS_ITRUNCATED
);
1196 if (truncated
&& VN_DIRTY(VFS_I(ip
)) && ip
->i_delayed_blks
> 0)
1197 xfs_flush_pages(ip
, 0, -1, XFS_B_ASYNC
, FI_NONE
);
1200 if (ip
->i_d
.di_nlink
!= 0) {
1201 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
1202 ((ip
->i_size
> 0) || (VN_CACHED(VFS_I(ip
)) > 0 ||
1203 ip
->i_delayed_blks
> 0)) &&
1204 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
)) &&
1205 (!(ip
->i_d
.di_flags
&
1206 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)))) {
1207 error
= xfs_free_eofblocks(mp
, ip
, XFS_FREE_EOF_LOCK
);
1219 * This is called when the vnode reference count for the vnode
1220 * goes to zero. If the file has been unlinked, then it must
1221 * now be truncated. Also, we clear all of the read-ahead state
1222 * kept for the inode here since the file is now closed.
1228 xfs_bmap_free_t free_list
;
1229 xfs_fsblock_t first_block
;
1236 xfs_itrace_entry(ip
);
1239 * If the inode is already free, then there can be nothing
1242 if (ip
->i_d
.di_mode
== 0 || VN_BAD(VFS_I(ip
))) {
1243 ASSERT(ip
->i_df
.if_real_bytes
== 0);
1244 ASSERT(ip
->i_df
.if_broot_bytes
== 0);
1245 return VN_INACTIVE_CACHE
;
1249 * Only do a truncate if it's a regular file with
1250 * some actual space in it. It's OK to look at the
1251 * inode's fields without the lock because we're the
1252 * only one with a reference to the inode.
1254 truncate
= ((ip
->i_d
.di_nlink
== 0) &&
1255 ((ip
->i_d
.di_size
!= 0) || (ip
->i_size
!= 0) ||
1256 (ip
->i_d
.di_nextents
> 0) || (ip
->i_delayed_blks
> 0)) &&
1257 ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
));
1261 if (ip
->i_d
.di_nlink
== 0 && DM_EVENT_ENABLED(ip
, DM_EVENT_DESTROY
))
1262 XFS_SEND_DESTROY(mp
, ip
, DM_RIGHT_NULL
);
1266 /* If this is a read-only mount, don't do this (would generate I/O) */
1267 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
1270 if (ip
->i_d
.di_nlink
!= 0) {
1271 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
1272 ((ip
->i_size
> 0) || (VN_CACHED(VFS_I(ip
)) > 0 ||
1273 ip
->i_delayed_blks
> 0)) &&
1274 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
) &&
1275 (!(ip
->i_d
.di_flags
&
1276 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)) ||
1277 (ip
->i_delayed_blks
!= 0)))) {
1278 error
= xfs_free_eofblocks(mp
, ip
, XFS_FREE_EOF_LOCK
);
1280 return VN_INACTIVE_CACHE
;
1285 ASSERT(ip
->i_d
.di_nlink
== 0);
1287 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
1288 return VN_INACTIVE_CACHE
;
1290 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1293 * Do the xfs_itruncate_start() call before
1294 * reserving any log space because itruncate_start
1295 * will call into the buffer cache and we can't
1296 * do that within a transaction.
1298 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
1300 error
= xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
, 0);
1302 xfs_trans_cancel(tp
, 0);
1303 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1304 return VN_INACTIVE_CACHE
;
1307 error
= xfs_trans_reserve(tp
, 0,
1308 XFS_ITRUNCATE_LOG_RES(mp
),
1309 0, XFS_TRANS_PERM_LOG_RES
,
1310 XFS_ITRUNCATE_LOG_COUNT
);
1312 /* Don't call itruncate_cleanup */
1313 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1314 xfs_trans_cancel(tp
, 0);
1315 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1316 return VN_INACTIVE_CACHE
;
1319 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1320 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1321 xfs_trans_ihold(tp
, ip
);
1324 * normally, we have to run xfs_itruncate_finish sync.
1325 * But if filesystem is wsync and we're in the inactive
1326 * path, then we know that nlink == 0, and that the
1327 * xaction that made nlink == 0 is permanently committed
1328 * since xfs_remove runs as a synchronous transaction.
1330 error
= xfs_itruncate_finish(&tp
, ip
, 0, XFS_DATA_FORK
,
1331 (!(mp
->m_flags
& XFS_MOUNT_WSYNC
) ? 1 : 0));
1334 xfs_trans_cancel(tp
,
1335 XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1336 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1337 return VN_INACTIVE_CACHE
;
1339 } else if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
) {
1342 * If we get an error while cleaning up a
1343 * symlink we bail out.
1345 error
= (ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
)) ?
1346 xfs_inactive_symlink_rmt(ip
, &tp
) :
1347 xfs_inactive_symlink_local(ip
, &tp
);
1351 return VN_INACTIVE_CACHE
;
1354 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1355 xfs_trans_ihold(tp
, ip
);
1357 error
= xfs_trans_reserve(tp
, 0,
1358 XFS_IFREE_LOG_RES(mp
),
1359 0, XFS_TRANS_PERM_LOG_RES
,
1360 XFS_INACTIVE_LOG_COUNT
);
1362 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1363 xfs_trans_cancel(tp
, 0);
1364 return VN_INACTIVE_CACHE
;
1367 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1368 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1369 xfs_trans_ihold(tp
, ip
);
1373 * If there are attributes associated with the file
1374 * then blow them away now. The code calls a routine
1375 * that recursively deconstructs the attribute fork.
1376 * We need to just commit the current transaction
1377 * because we can't use it for xfs_attr_inactive().
1379 if (ip
->i_d
.di_anextents
> 0) {
1380 error
= xfs_inactive_attrs(ip
, &tp
);
1382 * If we got an error, the transaction is already
1383 * cancelled, and the inode is unlocked. Just get out.
1386 return VN_INACTIVE_CACHE
;
1387 } else if (ip
->i_afp
) {
1388 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
1394 XFS_BMAP_INIT(&free_list
, &first_block
);
1395 error
= xfs_ifree(tp
, ip
, &free_list
);
1398 * If we fail to free the inode, shut down. The cancel
1399 * might do that, we need to make sure. Otherwise the
1400 * inode might be lost for a long time or forever.
1402 if (!XFS_FORCED_SHUTDOWN(mp
)) {
1404 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1405 error
, mp
->m_fsname
);
1406 xfs_force_shutdown(mp
, SHUTDOWN_META_IO_ERROR
);
1408 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
1411 * Credit the quota account(s). The inode is gone.
1413 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, XFS_TRANS_DQ_ICOUNT
, -1);
1416 * Just ignore errors at this point. There is nothing we can
1417 * do except to try to keep going. Make sure it's not a silent
1420 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1422 xfs_fs_cmn_err(CE_NOTE
, mp
, "xfs_inactive: "
1423 "xfs_bmap_finish() returned error %d", error
);
1424 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1426 xfs_fs_cmn_err(CE_NOTE
, mp
, "xfs_inactive: "
1427 "xfs_trans_commit() returned error %d", error
);
1430 * Release the dquots held by inode, if any.
1432 XFS_QM_DQDETACH(mp
, ip
);
1434 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1437 return VN_INACTIVE_CACHE
;
1441 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1442 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1443 * ci_name->name will point to a the actual name (caller must free) or
1444 * will be set to NULL if an exact match is found.
1449 struct xfs_name
*name
,
1451 struct xfs_name
*ci_name
)
1457 xfs_itrace_entry(dp
);
1459 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
1460 return XFS_ERROR(EIO
);
1462 lock_mode
= xfs_ilock_map_shared(dp
);
1463 error
= xfs_dir_lookup(NULL
, dp
, name
, &inum
, ci_name
);
1464 xfs_iunlock_map_shared(dp
, lock_mode
);
1469 error
= xfs_iget(dp
->i_mount
, NULL
, inum
, 0, 0, ipp
, 0);
1473 xfs_itrace_ref(*ipp
);
1478 kmem_free(ci_name
->name
);
1487 struct xfs_name
*name
,
1493 xfs_mount_t
*mp
= dp
->i_mount
;
1497 xfs_bmap_free_t free_list
;
1498 xfs_fsblock_t first_block
;
1499 boolean_t unlock_dp_on_error
= B_FALSE
;
1500 int dm_event_sent
= 0;
1504 struct xfs_dquot
*udqp
, *gdqp
;
1508 xfs_itrace_entry(dp
);
1510 if (DM_EVENT_ENABLED(dp
, DM_EVENT_CREATE
)) {
1511 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_CREATE
,
1512 dp
, DM_RIGHT_NULL
, NULL
,
1513 DM_RIGHT_NULL
, name
->name
, NULL
,
1521 if (XFS_FORCED_SHUTDOWN(mp
))
1522 return XFS_ERROR(EIO
);
1524 /* Return through std_return after this point. */
1527 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
1528 prid
= dp
->i_d
.di_projid
;
1530 prid
= (xfs_prid_t
)dfltprid
;
1533 * Make sure that we have allocated dquot(s) on disk.
1535 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
1536 current_fsuid(), current_fsgid(), prid
,
1537 XFS_QMOPT_QUOTALL
|XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
1543 tp
= xfs_trans_alloc(mp
, XFS_TRANS_CREATE
);
1544 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1545 resblks
= XFS_CREATE_SPACE_RES(mp
, name
->len
);
1547 * Initially assume that the file does not exist and
1548 * reserve the resources for that case. If that is not
1549 * the case we'll drop the one we have and get a more
1550 * appropriate transaction later.
1552 error
= xfs_trans_reserve(tp
, resblks
, XFS_CREATE_LOG_RES(mp
), 0,
1553 XFS_TRANS_PERM_LOG_RES
, XFS_CREATE_LOG_COUNT
);
1554 if (error
== ENOSPC
) {
1556 error
= xfs_trans_reserve(tp
, 0, XFS_CREATE_LOG_RES(mp
), 0,
1557 XFS_TRANS_PERM_LOG_RES
, XFS_CREATE_LOG_COUNT
);
1564 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
1565 unlock_dp_on_error
= B_TRUE
;
1567 XFS_BMAP_INIT(&free_list
, &first_block
);
1572 * Reserve disk quota and the inode.
1574 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
1578 error
= xfs_dir_canenter(tp
, dp
, name
, resblks
);
1581 error
= xfs_dir_ialloc(&tp
, dp
, mode
, 1,
1582 rdev
, credp
, prid
, resblks
> 0,
1585 if (error
== ENOSPC
)
1592 * At this point, we've gotten a newly allocated inode.
1593 * It is locked (and joined to the transaction).
1596 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1599 * Now we join the directory inode to the transaction. We do not do it
1600 * earlier because xfs_dir_ialloc might commit the previous transaction
1601 * (and release all the locks). An error from here on will result in
1602 * the transaction cancel unlocking dp so don't do it explicitly in the
1606 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
1607 unlock_dp_on_error
= B_FALSE
;
1609 error
= xfs_dir_createname(tp
, dp
, name
, ip
->i_ino
,
1610 &first_block
, &free_list
, resblks
?
1611 resblks
- XFS_IALLOC_SPACE_RES(mp
) : 0);
1613 ASSERT(error
!= ENOSPC
);
1616 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1617 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1620 * If this is a synchronous mount, make sure that the
1621 * create transaction goes to disk before returning to
1624 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
1625 xfs_trans_set_sync(tp
);
1631 * Attach the dquot(s) to the inodes and modify them incore.
1632 * These ids of the inode couldn't have changed since the new
1633 * inode has been locked ever since it was created.
1635 XFS_QM_DQVOPCREATE(mp
, tp
, ip
, udqp
, gdqp
);
1638 * xfs_trans_commit normally decrements the vnode ref count
1639 * when it unlocks the inode. Since we want to return the
1640 * vnode to the caller, we bump the vnode ref count now.
1644 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1646 xfs_bmap_cancel(&free_list
);
1650 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1657 XFS_QM_DQRELE(mp
, udqp
);
1658 XFS_QM_DQRELE(mp
, gdqp
);
1662 /* Fallthrough to std_return with error = 0 */
1665 if ((*ipp
|| (error
!= 0 && dm_event_sent
!= 0)) &&
1666 DM_EVENT_ENABLED(dp
, DM_EVENT_POSTCREATE
)) {
1667 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTCREATE
,
1670 DM_RIGHT_NULL
, name
->name
, NULL
,
1676 cancel_flags
|= XFS_TRANS_ABORT
;
1681 xfs_trans_cancel(tp
, cancel_flags
);
1683 XFS_QM_DQRELE(mp
, udqp
);
1684 XFS_QM_DQRELE(mp
, gdqp
);
1686 if (unlock_dp_on_error
)
1687 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
1693 * Wait until after the current transaction is aborted to
1694 * release the inode. This prevents recursive transactions
1695 * and deadlocks from xfs_inactive.
1697 cancel_flags
|= XFS_TRANS_ABORT
;
1698 xfs_trans_cancel(tp
, cancel_flags
);
1701 XFS_QM_DQRELE(mp
, udqp
);
1702 XFS_QM_DQRELE(mp
, gdqp
);
1709 int xfs_small_retries
;
1710 int xfs_middle_retries
;
1711 int xfs_lots_retries
;
1712 int xfs_lock_delays
;
1716 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1720 xfs_lock_inumorder(int lock_mode
, int subclass
)
1722 if (lock_mode
& (XFS_IOLOCK_SHARED
|XFS_IOLOCK_EXCL
))
1723 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_IOLOCK_SHIFT
;
1724 if (lock_mode
& (XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
))
1725 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_ILOCK_SHIFT
;
1731 * The following routine will lock n inodes in exclusive mode.
1732 * We assume the caller calls us with the inodes in i_ino order.
1734 * We need to detect deadlock where an inode that we lock
1735 * is in the AIL and we start waiting for another inode that is locked
1736 * by a thread in a long running transaction (such as truncate). This can
1737 * result in deadlock since the long running trans might need to wait
1738 * for the inode we just locked in order to push the tail and free space
1747 int attempts
= 0, i
, j
, try_lock
;
1750 ASSERT(ips
&& (inodes
>= 2)); /* we need at least two */
1756 for (; i
< inodes
; i
++) {
1759 if (i
&& (ips
[i
] == ips
[i
-1])) /* Already locked */
1763 * If try_lock is not set yet, make sure all locked inodes
1764 * are not in the AIL.
1765 * If any are, set try_lock to be used later.
1769 for (j
= (i
- 1); j
>= 0 && !try_lock
; j
--) {
1770 lp
= (xfs_log_item_t
*)ips
[j
]->i_itemp
;
1771 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
1778 * If any of the previous locks we have locked is in the AIL,
1779 * we must TRY to get the second and subsequent locks. If
1780 * we can't get any, we must release all we have
1785 /* try_lock must be 0 if i is 0. */
1787 * try_lock means we have an inode locked
1788 * that is in the AIL.
1791 if (!xfs_ilock_nowait(ips
[i
], xfs_lock_inumorder(lock_mode
, i
))) {
1795 * Unlock all previous guys and try again.
1796 * xfs_iunlock will try to push the tail
1797 * if the inode is in the AIL.
1800 for(j
= i
- 1; j
>= 0; j
--) {
1803 * Check to see if we've already
1804 * unlocked this one.
1805 * Not the first one going back,
1806 * and the inode ptr is the same.
1808 if ((j
!= (i
- 1)) && ips
[j
] ==
1812 xfs_iunlock(ips
[j
], lock_mode
);
1815 if ((attempts
% 5) == 0) {
1816 delay(1); /* Don't just spin the CPU */
1826 xfs_ilock(ips
[i
], xfs_lock_inumorder(lock_mode
, i
));
1832 if (attempts
< 5) xfs_small_retries
++;
1833 else if (attempts
< 100) xfs_middle_retries
++;
1834 else xfs_lots_retries
++;
1842 * xfs_lock_two_inodes() can only be used to lock one type of lock
1843 * at a time - the iolock or the ilock, but not both at once. If
1844 * we lock both at once, lockdep will report false positives saying
1845 * we have violated locking orders.
1848 xfs_lock_two_inodes(
1857 if (lock_mode
& (XFS_IOLOCK_SHARED
|XFS_IOLOCK_EXCL
))
1858 ASSERT((lock_mode
& (XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
)) == 0);
1859 ASSERT(ip0
->i_ino
!= ip1
->i_ino
);
1861 if (ip0
->i_ino
> ip1
->i_ino
) {
1868 xfs_ilock(ip0
, xfs_lock_inumorder(lock_mode
, 0));
1871 * If the first lock we have locked is in the AIL, we must TRY to get
1872 * the second lock. If we can't get it, we must release the first one
1875 lp
= (xfs_log_item_t
*)ip0
->i_itemp
;
1876 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
1877 if (!xfs_ilock_nowait(ip1
, xfs_lock_inumorder(lock_mode
, 1))) {
1878 xfs_iunlock(ip0
, lock_mode
);
1879 if ((++attempts
% 5) == 0)
1880 delay(1); /* Don't just spin the CPU */
1884 xfs_ilock(ip1
, xfs_lock_inumorder(lock_mode
, 1));
1891 struct xfs_name
*name
,
1894 xfs_mount_t
*mp
= dp
->i_mount
;
1895 xfs_trans_t
*tp
= NULL
;
1896 int is_dir
= S_ISDIR(ip
->i_d
.di_mode
);
1898 xfs_bmap_free_t free_list
;
1899 xfs_fsblock_t first_block
;
1906 xfs_itrace_entry(dp
);
1907 xfs_itrace_entry(ip
);
1909 if (XFS_FORCED_SHUTDOWN(mp
))
1910 return XFS_ERROR(EIO
);
1912 if (DM_EVENT_ENABLED(dp
, DM_EVENT_REMOVE
)) {
1913 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_REMOVE
, dp
, DM_RIGHT_NULL
,
1914 NULL
, DM_RIGHT_NULL
, name
->name
, NULL
,
1915 ip
->i_d
.di_mode
, 0, 0);
1920 error
= XFS_QM_DQATTACH(mp
, dp
, 0);
1924 error
= XFS_QM_DQATTACH(mp
, ip
, 0);
1929 tp
= xfs_trans_alloc(mp
, XFS_TRANS_RMDIR
);
1930 log_count
= XFS_DEFAULT_LOG_COUNT
;
1932 tp
= xfs_trans_alloc(mp
, XFS_TRANS_REMOVE
);
1933 log_count
= XFS_REMOVE_LOG_COUNT
;
1935 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1938 * We try to get the real space reservation first,
1939 * allowing for directory btree deletion(s) implying
1940 * possible bmap insert(s). If we can't get the space
1941 * reservation then we use 0 instead, and avoid the bmap
1942 * btree insert(s) in the directory code by, if the bmap
1943 * insert tries to happen, instead trimming the LAST
1944 * block from the directory.
1946 resblks
= XFS_REMOVE_SPACE_RES(mp
);
1947 error
= xfs_trans_reserve(tp
, resblks
, XFS_REMOVE_LOG_RES(mp
), 0,
1948 XFS_TRANS_PERM_LOG_RES
, log_count
);
1949 if (error
== ENOSPC
) {
1951 error
= xfs_trans_reserve(tp
, 0, XFS_REMOVE_LOG_RES(mp
), 0,
1952 XFS_TRANS_PERM_LOG_RES
, log_count
);
1955 ASSERT(error
!= ENOSPC
);
1957 goto out_trans_cancel
;
1960 xfs_lock_two_inodes(dp
, ip
, XFS_ILOCK_EXCL
);
1963 * At this point, we've gotten both the directory and the entry
1967 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
1970 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1973 * If we're removing a directory perform some additional validation.
1976 ASSERT(ip
->i_d
.di_nlink
>= 2);
1977 if (ip
->i_d
.di_nlink
!= 2) {
1978 error
= XFS_ERROR(ENOTEMPTY
);
1979 goto out_trans_cancel
;
1981 if (!xfs_dir_isempty(ip
)) {
1982 error
= XFS_ERROR(ENOTEMPTY
);
1983 goto out_trans_cancel
;
1987 XFS_BMAP_INIT(&free_list
, &first_block
);
1988 error
= xfs_dir_removename(tp
, dp
, name
, ip
->i_ino
,
1989 &first_block
, &free_list
, resblks
);
1991 ASSERT(error
!= ENOENT
);
1992 goto out_bmap_cancel
;
1994 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1997 * Bump the in memory generation count on the parent
1998 * directory so that other can know that it has changed.
2001 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2005 * Drop the link from ip's "..".
2007 error
= xfs_droplink(tp
, dp
);
2009 goto out_bmap_cancel
;
2012 * Drop the link from dp to ip.
2014 error
= xfs_droplink(tp
, ip
);
2016 goto out_bmap_cancel
;
2019 * When removing a non-directory we need to log the parent
2020 * inode here for the i_gen update. For a directory this is
2021 * done implicitly by the xfs_droplink call for the ".." entry.
2023 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2027 * Drop the "." link from ip to self.
2029 error
= xfs_droplink(tp
, ip
);
2031 goto out_bmap_cancel
;
2034 * Determine if this is the last link while
2035 * we are in the transaction.
2037 link_zero
= (ip
->i_d
.di_nlink
== 0);
2040 * If this is a synchronous mount, make sure that the
2041 * remove transaction goes to disk before returning to
2044 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
))
2045 xfs_trans_set_sync(tp
);
2047 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2049 goto out_bmap_cancel
;
2051 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2056 * If we are using filestreams, kill the stream association.
2057 * If the file is still open it may get a new one but that
2058 * will get killed on last close in xfs_close() so we don't
2059 * have to worry about that.
2061 if (!is_dir
&& link_zero
&& xfs_inode_is_filestream(ip
))
2062 xfs_filestream_deassociate(ip
);
2064 xfs_itrace_exit(ip
);
2065 xfs_itrace_exit(dp
);
2068 if (DM_EVENT_ENABLED(dp
, DM_EVENT_POSTREMOVE
)) {
2069 XFS_SEND_NAMESP(mp
, DM_EVENT_POSTREMOVE
, dp
, DM_RIGHT_NULL
,
2070 NULL
, DM_RIGHT_NULL
, name
->name
, NULL
,
2071 ip
->i_d
.di_mode
, error
, 0);
2077 xfs_bmap_cancel(&free_list
);
2078 cancel_flags
|= XFS_TRANS_ABORT
;
2080 xfs_trans_cancel(tp
, cancel_flags
);
2088 struct xfs_name
*target_name
)
2090 xfs_mount_t
*mp
= tdp
->i_mount
;
2093 xfs_bmap_free_t free_list
;
2094 xfs_fsblock_t first_block
;
2099 xfs_itrace_entry(tdp
);
2100 xfs_itrace_entry(sip
);
2102 ASSERT(!S_ISDIR(sip
->i_d
.di_mode
));
2104 if (XFS_FORCED_SHUTDOWN(mp
))
2105 return XFS_ERROR(EIO
);
2107 if (DM_EVENT_ENABLED(tdp
, DM_EVENT_LINK
)) {
2108 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_LINK
,
2111 target_name
->name
, NULL
, 0, 0, 0);
2116 /* Return through std_return after this point. */
2118 error
= XFS_QM_DQATTACH(mp
, sip
, 0);
2119 if (!error
&& sip
!= tdp
)
2120 error
= XFS_QM_DQATTACH(mp
, tdp
, 0);
2124 tp
= xfs_trans_alloc(mp
, XFS_TRANS_LINK
);
2125 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2126 resblks
= XFS_LINK_SPACE_RES(mp
, target_name
->len
);
2127 error
= xfs_trans_reserve(tp
, resblks
, XFS_LINK_LOG_RES(mp
), 0,
2128 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
2129 if (error
== ENOSPC
) {
2131 error
= xfs_trans_reserve(tp
, 0, XFS_LINK_LOG_RES(mp
), 0,
2132 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
2139 xfs_lock_two_inodes(sip
, tdp
, XFS_ILOCK_EXCL
);
2142 * Increment vnode ref counts since xfs_trans_commit &
2143 * xfs_trans_cancel will both unlock the inodes and
2144 * decrement the associated ref counts.
2148 xfs_trans_ijoin(tp
, sip
, XFS_ILOCK_EXCL
);
2149 xfs_trans_ijoin(tp
, tdp
, XFS_ILOCK_EXCL
);
2152 * If the source has too many links, we can't make any more to it.
2154 if (sip
->i_d
.di_nlink
>= XFS_MAXLINK
) {
2155 error
= XFS_ERROR(EMLINK
);
2160 * If we are using project inheritance, we only allow hard link
2161 * creation in our tree when the project IDs are the same; else
2162 * the tree quota mechanism could be circumvented.
2164 if (unlikely((tdp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
) &&
2165 (tdp
->i_d
.di_projid
!= sip
->i_d
.di_projid
))) {
2166 error
= XFS_ERROR(EXDEV
);
2170 error
= xfs_dir_canenter(tp
, tdp
, target_name
, resblks
);
2174 XFS_BMAP_INIT(&free_list
, &first_block
);
2176 error
= xfs_dir_createname(tp
, tdp
, target_name
, sip
->i_ino
,
2177 &first_block
, &free_list
, resblks
);
2180 xfs_ichgtime(tdp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2182 xfs_trans_log_inode(tp
, tdp
, XFS_ILOG_CORE
);
2184 error
= xfs_bumplink(tp
, sip
);
2189 * If this is a synchronous mount, make sure that the
2190 * link transaction goes to disk before returning to
2193 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2194 xfs_trans_set_sync(tp
);
2197 error
= xfs_bmap_finish (&tp
, &free_list
, &committed
);
2199 xfs_bmap_cancel(&free_list
);
2203 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2207 /* Fall through to std_return with error = 0. */
2209 if (DM_EVENT_ENABLED(sip
, DM_EVENT_POSTLINK
)) {
2210 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTLINK
,
2213 target_name
->name
, NULL
, 0, error
, 0);
2218 cancel_flags
|= XFS_TRANS_ABORT
;
2222 xfs_trans_cancel(tp
, cancel_flags
);
2230 struct xfs_name
*dir_name
,
2235 xfs_mount_t
*mp
= dp
->i_mount
;
2236 xfs_inode_t
*cdp
; /* inode of created dir */
2241 xfs_bmap_free_t free_list
;
2242 xfs_fsblock_t first_block
;
2243 boolean_t unlock_dp_on_error
= B_FALSE
;
2244 boolean_t created
= B_FALSE
;
2245 int dm_event_sent
= 0;
2247 struct xfs_dquot
*udqp
, *gdqp
;
2250 if (XFS_FORCED_SHUTDOWN(mp
))
2251 return XFS_ERROR(EIO
);
2255 if (DM_EVENT_ENABLED(dp
, DM_EVENT_CREATE
)) {
2256 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_CREATE
,
2257 dp
, DM_RIGHT_NULL
, NULL
,
2258 DM_RIGHT_NULL
, dir_name
->name
, NULL
,
2265 /* Return through std_return after this point. */
2267 xfs_itrace_entry(dp
);
2271 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
2272 prid
= dp
->i_d
.di_projid
;
2274 prid
= (xfs_prid_t
)dfltprid
;
2277 * Make sure that we have allocated dquot(s) on disk.
2279 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
2280 current_fsuid(), current_fsgid(), prid
,
2281 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
2285 tp
= xfs_trans_alloc(mp
, XFS_TRANS_MKDIR
);
2286 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2287 resblks
= XFS_MKDIR_SPACE_RES(mp
, dir_name
->len
);
2288 error
= xfs_trans_reserve(tp
, resblks
, XFS_MKDIR_LOG_RES(mp
), 0,
2289 XFS_TRANS_PERM_LOG_RES
, XFS_MKDIR_LOG_COUNT
);
2290 if (error
== ENOSPC
) {
2292 error
= xfs_trans_reserve(tp
, 0, XFS_MKDIR_LOG_RES(mp
), 0,
2293 XFS_TRANS_PERM_LOG_RES
,
2294 XFS_MKDIR_LOG_COUNT
);
2301 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
2302 unlock_dp_on_error
= B_TRUE
;
2305 * Check for directory link count overflow.
2307 if (dp
->i_d
.di_nlink
>= XFS_MAXLINK
) {
2308 error
= XFS_ERROR(EMLINK
);
2313 * Reserve disk quota and the inode.
2315 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
2319 error
= xfs_dir_canenter(tp
, dp
, dir_name
, resblks
);
2323 * create the directory inode.
2325 error
= xfs_dir_ialloc(&tp
, dp
, mode
, 2,
2326 0, credp
, prid
, resblks
> 0,
2329 if (error
== ENOSPC
)
2333 xfs_itrace_ref(cdp
);
2336 * Now we add the directory inode to the transaction.
2337 * We waited until now since xfs_dir_ialloc might start
2338 * a new transaction. Had we joined the transaction
2339 * earlier, the locks might have gotten released. An error
2340 * from here on will result in the transaction cancel
2341 * unlocking dp so don't do it explicitly in the error path.
2344 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
2345 unlock_dp_on_error
= B_FALSE
;
2347 XFS_BMAP_INIT(&free_list
, &first_block
);
2349 error
= xfs_dir_createname(tp
, dp
, dir_name
, cdp
->i_ino
,
2350 &first_block
, &free_list
, resblks
?
2351 resblks
- XFS_IALLOC_SPACE_RES(mp
) : 0);
2353 ASSERT(error
!= ENOSPC
);
2356 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2359 * Bump the in memory version number of the parent directory
2360 * so that other processes accessing it will recognize that
2361 * the directory has changed.
2365 error
= xfs_dir_init(tp
, cdp
, dp
);
2370 error
= xfs_bumplink(tp
, dp
);
2380 * Attach the dquots to the new inode and modify the icount incore.
2382 XFS_QM_DQVOPCREATE(mp
, tp
, cdp
, udqp
, gdqp
);
2385 * If this is a synchronous mount, make sure that the
2386 * mkdir transaction goes to disk before returning to
2389 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2390 xfs_trans_set_sync(tp
);
2393 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2399 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2400 XFS_QM_DQRELE(mp
, udqp
);
2401 XFS_QM_DQRELE(mp
, gdqp
);
2406 /* Fall through to std_return with error = 0 or errno from
2407 * xfs_trans_commit. */
2410 if ((created
|| (error
!= 0 && dm_event_sent
!= 0)) &&
2411 DM_EVENT_ENABLED(dp
, DM_EVENT_POSTCREATE
)) {
2412 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTCREATE
,
2414 created
? cdp
: NULL
,
2416 dir_name
->name
, NULL
,
2423 xfs_bmap_cancel(&free_list
);
2425 cancel_flags
|= XFS_TRANS_ABORT
;
2427 xfs_trans_cancel(tp
, cancel_flags
);
2428 XFS_QM_DQRELE(mp
, udqp
);
2429 XFS_QM_DQRELE(mp
, gdqp
);
2431 if (unlock_dp_on_error
)
2432 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
2440 struct xfs_name
*link_name
,
2441 const char *target_path
,
2446 xfs_mount_t
*mp
= dp
->i_mount
;
2451 xfs_bmap_free_t free_list
;
2452 xfs_fsblock_t first_block
;
2453 boolean_t unlock_dp_on_error
= B_FALSE
;
2456 xfs_fileoff_t first_fsb
;
2457 xfs_filblks_t fs_blocks
;
2459 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
2461 const char *cur_chunk
;
2466 struct xfs_dquot
*udqp
, *gdqp
;
2474 xfs_itrace_entry(dp
);
2476 if (XFS_FORCED_SHUTDOWN(mp
))
2477 return XFS_ERROR(EIO
);
2480 * Check component lengths of the target path name.
2482 pathlen
= strlen(target_path
);
2483 if (pathlen
>= MAXPATHLEN
) /* total string too long */
2484 return XFS_ERROR(ENAMETOOLONG
);
2486 if (DM_EVENT_ENABLED(dp
, DM_EVENT_SYMLINK
)) {
2487 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_SYMLINK
, dp
,
2488 DM_RIGHT_NULL
, NULL
, DM_RIGHT_NULL
,
2489 link_name
->name
, target_path
, 0, 0, 0);
2494 /* Return through std_return after this point. */
2497 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
2498 prid
= dp
->i_d
.di_projid
;
2500 prid
= (xfs_prid_t
)dfltprid
;
2503 * Make sure that we have allocated dquot(s) on disk.
2505 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
2506 current_fsuid(), current_fsgid(), prid
,
2507 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
2511 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SYMLINK
);
2512 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2514 * The symlink will fit into the inode data fork?
2515 * There can't be any attributes so we get the whole variable part.
2517 if (pathlen
<= XFS_LITINO(mp
))
2520 fs_blocks
= XFS_B_TO_FSB(mp
, pathlen
);
2521 resblks
= XFS_SYMLINK_SPACE_RES(mp
, link_name
->len
, fs_blocks
);
2522 error
= xfs_trans_reserve(tp
, resblks
, XFS_SYMLINK_LOG_RES(mp
), 0,
2523 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
2524 if (error
== ENOSPC
&& fs_blocks
== 0) {
2526 error
= xfs_trans_reserve(tp
, 0, XFS_SYMLINK_LOG_RES(mp
), 0,
2527 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
2534 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
2535 unlock_dp_on_error
= B_TRUE
;
2538 * Check whether the directory allows new symlinks or not.
2540 if (dp
->i_d
.di_flags
& XFS_DIFLAG_NOSYMLINKS
) {
2541 error
= XFS_ERROR(EPERM
);
2546 * Reserve disk quota : blocks and inode.
2548 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
2553 * Check for ability to enter directory entry, if no space reserved.
2555 error
= xfs_dir_canenter(tp
, dp
, link_name
, resblks
);
2559 * Initialize the bmap freelist prior to calling either
2560 * bmapi or the directory create code.
2562 XFS_BMAP_INIT(&free_list
, &first_block
);
2565 * Allocate an inode for the symlink.
2567 error
= xfs_dir_ialloc(&tp
, dp
, S_IFLNK
| (mode
& ~S_IFMT
),
2568 1, 0, credp
, prid
, resblks
> 0, &ip
, NULL
);
2570 if (error
== ENOSPC
)
2577 * An error after we've joined dp to the transaction will result in the
2578 * transaction cancel unlocking dp so don't do it explicitly in the
2582 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
2583 unlock_dp_on_error
= B_FALSE
;
2586 * Also attach the dquot(s) to it, if applicable.
2588 XFS_QM_DQVOPCREATE(mp
, tp
, ip
, udqp
, gdqp
);
2591 resblks
-= XFS_IALLOC_SPACE_RES(mp
);
2593 * If the symlink will fit into the inode, write it inline.
2595 if (pathlen
<= XFS_IFORK_DSIZE(ip
)) {
2596 xfs_idata_realloc(ip
, pathlen
, XFS_DATA_FORK
);
2597 memcpy(ip
->i_df
.if_u1
.if_data
, target_path
, pathlen
);
2598 ip
->i_d
.di_size
= pathlen
;
2601 * The inode was initially created in extent format.
2603 ip
->i_df
.if_flags
&= ~(XFS_IFEXTENTS
| XFS_IFBROOT
);
2604 ip
->i_df
.if_flags
|= XFS_IFINLINE
;
2606 ip
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
2607 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_DDATA
| XFS_ILOG_CORE
);
2611 nmaps
= SYMLINK_MAPS
;
2613 error
= xfs_bmapi(tp
, ip
, first_fsb
, fs_blocks
,
2614 XFS_BMAPI_WRITE
| XFS_BMAPI_METADATA
,
2615 &first_block
, resblks
, mval
, &nmaps
,
2622 resblks
-= fs_blocks
;
2623 ip
->i_d
.di_size
= pathlen
;
2624 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2626 cur_chunk
= target_path
;
2627 for (n
= 0; n
< nmaps
; n
++) {
2628 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
2629 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
2630 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
2631 BTOBB(byte_cnt
), 0);
2632 ASSERT(bp
&& !XFS_BUF_GETERROR(bp
));
2633 if (pathlen
< byte_cnt
) {
2636 pathlen
-= byte_cnt
;
2638 memcpy(XFS_BUF_PTR(bp
), cur_chunk
, byte_cnt
);
2639 cur_chunk
+= byte_cnt
;
2641 xfs_trans_log_buf(tp
, bp
, 0, byte_cnt
- 1);
2646 * Create the directory entry for the symlink.
2648 error
= xfs_dir_createname(tp
, dp
, link_name
, ip
->i_ino
,
2649 &first_block
, &free_list
, resblks
);
2652 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2653 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2656 * Bump the in memory version number of the parent directory
2657 * so that other processes accessing it will recognize that
2658 * the directory has changed.
2663 * If this is a synchronous mount, make sure that the
2664 * symlink transaction goes to disk before returning to
2667 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2668 xfs_trans_set_sync(tp
);
2672 * xfs_trans_commit normally decrements the vnode ref count
2673 * when it unlocks the inode. Since we want to return the
2674 * vnode to the caller, we bump the vnode ref count now.
2678 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2682 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2683 XFS_QM_DQRELE(mp
, udqp
);
2684 XFS_QM_DQRELE(mp
, gdqp
);
2686 /* Fall through to std_return with error = 0 or errno from
2687 * xfs_trans_commit */
2689 if (DM_EVENT_ENABLED(dp
, DM_EVENT_POSTSYMLINK
)) {
2690 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTSYMLINK
,
2693 DM_RIGHT_NULL
, link_name
->name
,
2694 target_path
, 0, error
, 0);
2704 xfs_bmap_cancel(&free_list
);
2705 cancel_flags
|= XFS_TRANS_ABORT
;
2707 xfs_trans_cancel(tp
, cancel_flags
);
2708 XFS_QM_DQRELE(mp
, udqp
);
2709 XFS_QM_DQRELE(mp
, gdqp
);
2711 if (unlock_dp_on_error
)
2712 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
2722 xfs_mount_t
*mp
= ip
->i_mount
;
2725 if (XFS_FORCED_SHUTDOWN(mp
))
2726 return XFS_ERROR(EIO
);
2729 * Bypass inodes which have already been cleaned by
2730 * the inode flush clustering code inside xfs_iflush
2732 if (xfs_inode_clean(ip
))
2736 * We make this non-blocking if the inode is contended,
2737 * return EAGAIN to indicate to the caller that they
2738 * did not succeed. This prevents the flush path from
2739 * blocking on inodes inside another operation right
2740 * now, they get caught later by xfs_sync.
2742 if (flags
& FLUSH_SYNC
) {
2743 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
2745 } else if (xfs_ilock_nowait(ip
, XFS_ILOCK_SHARED
)) {
2746 if (xfs_ipincount(ip
) || !xfs_iflock_nowait(ip
)) {
2747 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
2754 error
= xfs_iflush(ip
, (flags
& FLUSH_SYNC
) ? XFS_IFLUSH_SYNC
2755 : XFS_IFLUSH_ASYNC_NOBLOCK
);
2756 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
2768 xfs_mount_t
*mp
= ip
->i_mount
;
2772 if (!capable(CAP_SYS_ADMIN
))
2773 return XFS_ERROR(EPERM
);
2775 if (XFS_FORCED_SHUTDOWN(mp
))
2776 return XFS_ERROR(EIO
);
2778 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SET_DMATTRS
);
2779 error
= xfs_trans_reserve(tp
, 0, XFS_ICHANGE_LOG_RES (mp
), 0, 0, 0);
2781 xfs_trans_cancel(tp
, 0);
2784 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2785 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
2787 ip
->i_d
.di_dmevmask
= evmask
;
2788 ip
->i_d
.di_dmstate
= state
;
2790 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2792 error
= xfs_trans_commit(tp
, 0);
2802 xfs_itrace_entry(ip
);
2804 ASSERT(!VN_MAPPED(VFS_I(ip
)));
2806 /* bad inode, get out here ASAP */
2807 if (VN_BAD(VFS_I(ip
))) {
2814 ASSERT(XFS_FORCED_SHUTDOWN(ip
->i_mount
) || ip
->i_delayed_blks
== 0);
2817 * Make sure the atime in the XFS inode is correct before freeing the
2820 xfs_synchronize_atime(ip
);
2823 * If we have nothing to flush with this inode then complete the
2824 * teardown now, otherwise break the link between the xfs inode and the
2825 * linux inode and clean up the xfs inode later. This avoids flushing
2826 * the inode to disk during the delete operation itself.
2828 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2829 * first to ensure that xfs_iunpin() will never see an xfs inode
2830 * that has a linux inode being reclaimed. Synchronisation is provided
2831 * by the i_flags_lock.
2833 if (!ip
->i_update_core
&& (ip
->i_itemp
== NULL
)) {
2834 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2836 return xfs_finish_reclaim(ip
, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC
);
2838 xfs_mount_t
*mp
= ip
->i_mount
;
2840 /* Protect sync and unpin from us */
2841 XFS_MOUNT_ILOCK(mp
);
2842 spin_lock(&ip
->i_flags_lock
);
2843 __xfs_iflags_set(ip
, XFS_IRECLAIMABLE
);
2844 VFS_I(ip
)->i_private
= NULL
;
2846 spin_unlock(&ip
->i_flags_lock
);
2847 list_add_tail(&ip
->i_reclaim
, &mp
->m_del_inodes
);
2848 XFS_MOUNT_IUNLOCK(mp
);
2859 xfs_perag_t
*pag
= xfs_get_perag(ip
->i_mount
, ip
->i_ino
);
2860 struct inode
*vp
= VFS_I(ip
);
2862 if (vp
&& VN_BAD(vp
))
2865 /* The hash lock here protects a thread in xfs_iget_core from
2866 * racing with us on linking the inode back with a vnode.
2867 * Once we have the XFS_IRECLAIM flag set it will not touch
2870 write_lock(&pag
->pag_ici_lock
);
2871 spin_lock(&ip
->i_flags_lock
);
2872 if (__xfs_iflags_test(ip
, XFS_IRECLAIM
) ||
2873 (!__xfs_iflags_test(ip
, XFS_IRECLAIMABLE
) && vp
== NULL
)) {
2874 spin_unlock(&ip
->i_flags_lock
);
2875 write_unlock(&pag
->pag_ici_lock
);
2878 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
2882 __xfs_iflags_set(ip
, XFS_IRECLAIM
);
2883 spin_unlock(&ip
->i_flags_lock
);
2884 write_unlock(&pag
->pag_ici_lock
);
2885 xfs_put_perag(ip
->i_mount
, pag
);
2888 * If the inode is still dirty, then flush it out. If the inode
2889 * is not in the AIL, then it will be OK to flush it delwri as
2890 * long as xfs_iflush() does not keep any references to the inode.
2891 * We leave that decision up to xfs_iflush() since it has the
2892 * knowledge of whether it's OK to simply do a delwri flush of
2893 * the inode or whether we need to wait until the inode is
2894 * pulled from the AIL.
2895 * We get the flush lock regardless, though, just to make sure
2896 * we don't free it while it is being flushed.
2899 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2904 * In the case of a forced shutdown we rely on xfs_iflush() to
2905 * wait for the inode to be unpinned before returning an error.
2907 if (xfs_iflush(ip
, sync_mode
) == 0) {
2908 /* synchronize with xfs_iflush_done */
2913 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
2921 xfs_finish_reclaim_all(xfs_mount_t
*mp
, int noblock
)
2924 xfs_inode_t
*ip
, *n
;
2929 XFS_MOUNT_ILOCK(mp
);
2930 list_for_each_entry_safe(ip
, n
, &mp
->m_del_inodes
, i_reclaim
) {
2932 if (xfs_ilock_nowait(ip
, XFS_ILOCK_EXCL
) == 0)
2934 if (xfs_ipincount(ip
) ||
2935 !xfs_iflock_nowait(ip
)) {
2936 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
2940 XFS_MOUNT_IUNLOCK(mp
);
2941 if (xfs_finish_reclaim(ip
, noblock
,
2942 XFS_IFLUSH_DELWRI_ELSE_ASYNC
))
2951 XFS_MOUNT_IUNLOCK(mp
);
2956 * xfs_alloc_file_space()
2957 * This routine allocates disk space for the given file.
2959 * If alloc_type == 0, this request is for an ALLOCSP type
2960 * request which will change the file size. In this case, no
2961 * DMAPI event will be generated by the call. A TRUNCATE event
2962 * will be generated later by xfs_setattr.
2964 * If alloc_type != 0, this request is for a RESVSP type
2965 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2966 * lower block boundary byte address is less than the file's
2975 xfs_alloc_file_space(
2982 xfs_mount_t
*mp
= ip
->i_mount
;
2984 xfs_filblks_t allocated_fsb
;
2985 xfs_filblks_t allocatesize_fsb
;
2986 xfs_extlen_t extsz
, temp
;
2987 xfs_fileoff_t startoffset_fsb
;
2988 xfs_fsblock_t firstfsb
;
2994 xfs_bmbt_irec_t imaps
[1], *imapp
;
2995 xfs_bmap_free_t free_list
;
2996 uint qblocks
, resblks
, resrtextents
;
3000 xfs_itrace_entry(ip
);
3002 if (XFS_FORCED_SHUTDOWN(mp
))
3003 return XFS_ERROR(EIO
);
3005 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
3009 return XFS_ERROR(EINVAL
);
3011 rt
= XFS_IS_REALTIME_INODE(ip
);
3012 extsz
= xfs_get_extsz_hint(ip
);
3017 bmapi_flag
= XFS_BMAPI_WRITE
| (alloc_type
? XFS_BMAPI_PREALLOC
: 0);
3018 startoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
3019 allocatesize_fsb
= XFS_B_TO_FSB(mp
, count
);
3021 /* Generate a DMAPI event if needed. */
3022 if (alloc_type
!= 0 && offset
< ip
->i_size
&&
3023 (attr_flags
& XFS_ATTR_DMI
) == 0 &&
3024 DM_EVENT_ENABLED(ip
, DM_EVENT_WRITE
)) {
3025 xfs_off_t end_dmi_offset
;
3027 end_dmi_offset
= offset
+len
;
3028 if (end_dmi_offset
> ip
->i_size
)
3029 end_dmi_offset
= ip
->i_size
;
3030 error
= XFS_SEND_DATA(mp
, DM_EVENT_WRITE
, ip
, offset
,
3031 end_dmi_offset
- offset
, 0, NULL
);
3037 * Allocate file space until done or until there is an error
3040 while (allocatesize_fsb
&& !error
) {
3044 * Determine space reservations for data/realtime.
3046 if (unlikely(extsz
)) {
3047 s
= startoffset_fsb
;
3050 e
= startoffset_fsb
+ allocatesize_fsb
;
3051 if ((temp
= do_mod(startoffset_fsb
, extsz
)))
3053 if ((temp
= do_mod(e
, extsz
)))
3057 e
= allocatesize_fsb
;
3061 resrtextents
= qblocks
= (uint
)(e
- s
);
3062 resrtextents
/= mp
->m_sb
.sb_rextsize
;
3063 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
3064 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
3067 resblks
= qblocks
= \
3068 XFS_DIOSTRAT_SPACE_RES(mp
, (uint
)(e
- s
));
3069 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
3073 * Allocate and setup the transaction.
3075 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
3076 error
= xfs_trans_reserve(tp
, resblks
,
3077 XFS_WRITE_LOG_RES(mp
), resrtextents
,
3078 XFS_TRANS_PERM_LOG_RES
,
3079 XFS_WRITE_LOG_COUNT
);
3081 * Check for running out of space
3085 * Free the transaction structure.
3087 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
3088 xfs_trans_cancel(tp
, 0);
3091 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3092 error
= XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, tp
, ip
,
3093 qblocks
, 0, quota_flag
);
3097 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3098 xfs_trans_ihold(tp
, ip
);
3101 * Issue the xfs_bmapi() call to allocate the blocks
3103 XFS_BMAP_INIT(&free_list
, &firstfsb
);
3104 error
= xfs_bmapi(tp
, ip
, startoffset_fsb
,
3105 allocatesize_fsb
, bmapi_flag
,
3106 &firstfsb
, 0, imapp
, &nimaps
,
3113 * Complete the transaction
3115 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
3120 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3121 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3126 allocated_fsb
= imapp
->br_blockcount
;
3129 error
= XFS_ERROR(ENOSPC
);
3133 startoffset_fsb
+= allocated_fsb
;
3134 allocatesize_fsb
-= allocated_fsb
;
3137 if (error
== ENOSPC
&& (attr_flags
& XFS_ATTR_DMI
) == 0 &&
3138 DM_EVENT_ENABLED(ip
, DM_EVENT_NOSPACE
)) {
3139 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_NOSPACE
,
3142 NULL
, NULL
, 0, 0, 0); /* Delay flag intentionally unused */
3144 goto retry
; /* Maybe DMAPI app. has made space */
3145 /* else fall through with error from XFS_SEND_DATA */
3150 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
3151 xfs_bmap_cancel(&free_list
);
3152 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp
, tp
, ip
, qblocks
, 0, quota_flag
);
3154 error1
: /* Just cancel transaction */
3155 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
3156 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3157 goto dmapi_enospc_check
;
3161 * Zero file bytes between startoff and endoff inclusive.
3162 * The iolock is held exclusive and no blocks are buffered.
3164 * This function is used by xfs_free_file_space() to zero
3165 * partial blocks when the range to free is not block aligned.
3166 * When unreserving space with boundaries that are not block
3167 * aligned we round up the start and round down the end
3168 * boundaries and then use this function to zero the parts of
3169 * the blocks that got dropped during the rounding.
3172 xfs_zero_remaining_bytes(
3177 xfs_bmbt_irec_t imap
;
3178 xfs_fileoff_t offset_fsb
;
3179 xfs_off_t lastoffset
;
3182 xfs_mount_t
*mp
= ip
->i_mount
;
3187 * Avoid doing I/O beyond eof - it's not necessary
3188 * since nothing can read beyond eof. The space will
3189 * be zeroed when the file is extended anyway.
3191 if (startoff
>= ip
->i_size
)
3194 if (endoff
> ip
->i_size
)
3195 endoff
= ip
->i_size
;
3197 bp
= xfs_buf_get_noaddr(mp
->m_sb
.sb_blocksize
,
3198 XFS_IS_REALTIME_INODE(ip
) ?
3199 mp
->m_rtdev_targp
: mp
->m_ddev_targp
);
3201 for (offset
= startoff
; offset
<= endoff
; offset
= lastoffset
+ 1) {
3202 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
3204 error
= xfs_bmapi(NULL
, ip
, offset_fsb
, 1, 0,
3205 NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
3206 if (error
|| nimap
< 1)
3208 ASSERT(imap
.br_blockcount
>= 1);
3209 ASSERT(imap
.br_startoff
== offset_fsb
);
3210 lastoffset
= XFS_FSB_TO_B(mp
, imap
.br_startoff
+ 1) - 1;
3211 if (lastoffset
> endoff
)
3212 lastoffset
= endoff
;
3213 if (imap
.br_startblock
== HOLESTARTBLOCK
)
3215 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
3216 if (imap
.br_state
== XFS_EXT_UNWRITTEN
)
3219 XFS_BUF_UNWRITE(bp
);
3221 XFS_BUF_SET_ADDR(bp
, XFS_FSB_TO_DB(ip
, imap
.br_startblock
));
3223 error
= xfs_iowait(bp
);
3225 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3226 mp
, bp
, XFS_BUF_ADDR(bp
));
3229 memset(XFS_BUF_PTR(bp
) +
3230 (offset
- XFS_FSB_TO_B(mp
, imap
.br_startoff
)),
3231 0, lastoffset
- offset
+ 1);
3236 error
= xfs_iowait(bp
);
3238 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3239 mp
, bp
, XFS_BUF_ADDR(bp
));
3248 * xfs_free_file_space()
3249 * This routine frees disk space for the given file.
3251 * This routine is only called by xfs_change_file_space
3252 * for an UNRESVSP type call.
3260 xfs_free_file_space(
3268 xfs_off_t end_dmi_offset
;
3269 xfs_fileoff_t endoffset_fsb
;
3271 xfs_fsblock_t firstfsb
;
3272 xfs_bmap_free_t free_list
;
3273 xfs_bmbt_irec_t imap
;
3281 xfs_fileoff_t startoffset_fsb
;
3283 int need_iolock
= 1;
3287 xfs_itrace_entry(ip
);
3289 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
3293 if (len
<= 0) /* if nothing being freed */
3295 rt
= XFS_IS_REALTIME_INODE(ip
);
3296 startoffset_fsb
= XFS_B_TO_FSB(mp
, offset
);
3297 end_dmi_offset
= offset
+ len
;
3298 endoffset_fsb
= XFS_B_TO_FSBT(mp
, end_dmi_offset
);
3300 if (offset
< ip
->i_size
&& (attr_flags
& XFS_ATTR_DMI
) == 0 &&
3301 DM_EVENT_ENABLED(ip
, DM_EVENT_WRITE
)) {
3302 if (end_dmi_offset
> ip
->i_size
)
3303 end_dmi_offset
= ip
->i_size
;
3304 error
= XFS_SEND_DATA(mp
, DM_EVENT_WRITE
, ip
,
3305 offset
, end_dmi_offset
- offset
,
3306 AT_DELAY_FLAG(attr_flags
), NULL
);
3311 if (attr_flags
& XFS_ATTR_NOLOCK
)
3314 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
3315 vn_iowait(ip
); /* wait for the completion of any pending DIOs */
3318 rounding
= max_t(uint
, 1 << mp
->m_sb
.sb_blocklog
, PAGE_CACHE_SIZE
);
3319 ioffset
= offset
& ~(rounding
- 1);
3321 if (VN_CACHED(VFS_I(ip
)) != 0) {
3322 xfs_inval_cached_trace(ip
, ioffset
, -1, ioffset
, -1);
3323 error
= xfs_flushinval_pages(ip
, ioffset
, -1, FI_REMAPF_LOCKED
);
3325 goto out_unlock_iolock
;
3329 * Need to zero the stuff we're not freeing, on disk.
3330 * If its a realtime file & can't use unwritten extents then we
3331 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3332 * will take care of it for us.
3334 if (rt
&& !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
3336 error
= xfs_bmapi(NULL
, ip
, startoffset_fsb
,
3337 1, 0, NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
3339 goto out_unlock_iolock
;
3340 ASSERT(nimap
== 0 || nimap
== 1);
3341 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
3344 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
3345 block
= imap
.br_startblock
;
3346 mod
= do_div(block
, mp
->m_sb
.sb_rextsize
);
3348 startoffset_fsb
+= mp
->m_sb
.sb_rextsize
- mod
;
3351 error
= xfs_bmapi(NULL
, ip
, endoffset_fsb
- 1,
3352 1, 0, NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
3354 goto out_unlock_iolock
;
3355 ASSERT(nimap
== 0 || nimap
== 1);
3356 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
3357 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
3359 if (mod
&& (mod
!= mp
->m_sb
.sb_rextsize
))
3360 endoffset_fsb
-= mod
;
3363 if ((done
= (endoffset_fsb
<= startoffset_fsb
)))
3365 * One contiguous piece to clear
3367 error
= xfs_zero_remaining_bytes(ip
, offset
, offset
+ len
- 1);
3370 * Some full blocks, possibly two pieces to clear
3372 if (offset
< XFS_FSB_TO_B(mp
, startoffset_fsb
))
3373 error
= xfs_zero_remaining_bytes(ip
, offset
,
3374 XFS_FSB_TO_B(mp
, startoffset_fsb
) - 1);
3376 XFS_FSB_TO_B(mp
, endoffset_fsb
) < offset
+ len
)
3377 error
= xfs_zero_remaining_bytes(ip
,
3378 XFS_FSB_TO_B(mp
, endoffset_fsb
),
3383 * free file space until done or until there is an error
3385 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
3386 while (!error
&& !done
) {
3389 * allocate and setup the transaction. Allow this
3390 * transaction to dip into the reserve blocks to ensure
3391 * the freeing of the space succeeds at ENOSPC.
3393 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
3394 tp
->t_flags
|= XFS_TRANS_RESERVE
;
3395 error
= xfs_trans_reserve(tp
,
3397 XFS_WRITE_LOG_RES(mp
),
3399 XFS_TRANS_PERM_LOG_RES
,
3400 XFS_WRITE_LOG_COUNT
);
3403 * check for running out of space
3407 * Free the transaction structure.
3409 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
3410 xfs_trans_cancel(tp
, 0);
3413 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3414 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
,
3415 ip
->i_udquot
, ip
->i_gdquot
, resblks
, 0,
3416 XFS_QMOPT_RES_REGBLKS
);
3420 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3421 xfs_trans_ihold(tp
, ip
);
3424 * issue the bunmapi() call to free the blocks
3426 XFS_BMAP_INIT(&free_list
, &firstfsb
);
3427 error
= xfs_bunmapi(tp
, ip
, startoffset_fsb
,
3428 endoffset_fsb
- startoffset_fsb
,
3429 0, 2, &firstfsb
, &free_list
, NULL
, &done
);
3435 * complete the transaction
3437 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
3442 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3443 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3448 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
3452 xfs_bmap_cancel(&free_list
);
3454 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
3455 xfs_iunlock(ip
, need_iolock
? (XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
) :
3461 * xfs_change_file_space()
3462 * This routine allocates or frees disk space for the given file.
3463 * The user specified parameters are checked for alignment and size
3472 xfs_change_file_space(
3480 xfs_mount_t
*mp
= ip
->i_mount
;
3485 xfs_off_t startoffset
;
3490 xfs_itrace_entry(ip
);
3492 if (!S_ISREG(ip
->i_d
.di_mode
))
3493 return XFS_ERROR(EINVAL
);
3495 switch (bf
->l_whence
) {
3496 case 0: /*SEEK_SET*/
3498 case 1: /*SEEK_CUR*/
3499 bf
->l_start
+= offset
;
3501 case 2: /*SEEK_END*/
3502 bf
->l_start
+= ip
->i_size
;
3505 return XFS_ERROR(EINVAL
);
3508 llen
= bf
->l_len
> 0 ? bf
->l_len
- 1 : bf
->l_len
;
3510 if ( (bf
->l_start
< 0)
3511 || (bf
->l_start
> XFS_MAXIOFFSET(mp
))
3512 || (bf
->l_start
+ llen
< 0)
3513 || (bf
->l_start
+ llen
> XFS_MAXIOFFSET(mp
)))
3514 return XFS_ERROR(EINVAL
);
3518 startoffset
= bf
->l_start
;
3522 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3524 * These calls do NOT zero the data space allocated to the file,
3525 * nor do they change the file size.
3527 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3529 * These calls cause the new file data to be zeroed and the file
3530 * size to be changed.
3532 setprealloc
= clrprealloc
= 0;
3535 case XFS_IOC_RESVSP
:
3536 case XFS_IOC_RESVSP64
:
3537 error
= xfs_alloc_file_space(ip
, startoffset
, bf
->l_len
,
3544 case XFS_IOC_UNRESVSP
:
3545 case XFS_IOC_UNRESVSP64
:
3546 if ((error
= xfs_free_file_space(ip
, startoffset
, bf
->l_len
,
3551 case XFS_IOC_ALLOCSP
:
3552 case XFS_IOC_ALLOCSP64
:
3553 case XFS_IOC_FREESP
:
3554 case XFS_IOC_FREESP64
:
3555 if (startoffset
> fsize
) {
3556 error
= xfs_alloc_file_space(ip
, fsize
,
3557 startoffset
- fsize
, 0, attr_flags
);
3562 iattr
.ia_valid
= ATTR_SIZE
;
3563 iattr
.ia_size
= startoffset
;
3565 error
= xfs_setattr(ip
, &iattr
, attr_flags
, credp
);
3575 return XFS_ERROR(EINVAL
);
3579 * update the inode timestamp, mode, and prealloc flag bits
3581 tp
= xfs_trans_alloc(mp
, XFS_TRANS_WRITEID
);
3583 if ((error
= xfs_trans_reserve(tp
, 0, XFS_WRITEID_LOG_RES(mp
),
3586 xfs_trans_cancel(tp
, 0);
3590 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3592 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3593 xfs_trans_ihold(tp
, ip
);
3595 if ((attr_flags
& XFS_ATTR_DMI
) == 0) {
3596 ip
->i_d
.di_mode
&= ~S_ISUID
;
3599 * Note that we don't have to worry about mandatory
3600 * file locking being disabled here because we only
3601 * clear the S_ISGID bit if the Group execute bit is
3602 * on, but if it was on then mandatory locking wouldn't
3603 * have been enabled.
3605 if (ip
->i_d
.di_mode
& S_IXGRP
)
3606 ip
->i_d
.di_mode
&= ~S_ISGID
;
3608 xfs_ichgtime(ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
3611 ip
->i_d
.di_flags
|= XFS_DIFLAG_PREALLOC
;
3612 else if (clrprealloc
)
3613 ip
->i_d
.di_flags
&= ~XFS_DIFLAG_PREALLOC
;
3615 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
3616 xfs_trans_set_sync(tp
);
3618 error
= xfs_trans_commit(tp
, 0);
3620 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);