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
= XFS_ITOV(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(credp
) == 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;
516 timeflags
&= ~XFS_ICHGTIME_ACC
;
518 if (mask
& ATTR_MTIME
) {
519 inode
->i_mtime
= iattr
->ia_mtime
;
520 ip
->i_d
.di_mtime
.t_sec
= iattr
->ia_mtime
.tv_sec
;
521 ip
->i_d
.di_mtime
.t_nsec
= iattr
->ia_mtime
.tv_nsec
;
522 timeflags
&= ~XFS_ICHGTIME_MOD
;
523 timeflags
|= XFS_ICHGTIME_CHG
;
525 if (tp
&& (mask
& (ATTR_MTIME_SET
|ATTR_ATIME_SET
)))
526 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
530 * Change file inode change time only if ATTR_CTIME set
531 * AND we have been called by a DMI function.
534 if ((flags
& XFS_ATTR_DMI
) && (mask
& ATTR_CTIME
)) {
535 inode
->i_ctime
= iattr
->ia_ctime
;
536 ip
->i_d
.di_ctime
.t_sec
= iattr
->ia_ctime
.tv_sec
;
537 ip
->i_d
.di_ctime
.t_nsec
= iattr
->ia_ctime
.tv_nsec
;
538 ip
->i_update_core
= 1;
539 timeflags
&= ~XFS_ICHGTIME_CHG
;
543 * Send out timestamp changes that need to be set to the
544 * current time. Not done when called by a DMI function.
546 if (timeflags
&& !(flags
& XFS_ATTR_DMI
))
547 xfs_ichgtime(ip
, timeflags
);
549 XFS_STATS_INC(xs_ig_attrchg
);
552 * If this is a synchronous mount, make sure that the
553 * transaction goes to disk before returning to the user.
554 * This is slightly sub-optimal in that truncates require
555 * two sync transactions instead of one for wsync filesystems.
556 * One for the truncate and one for the timestamps since we
557 * don't want to change the timestamps unless we're sure the
558 * truncate worked. Truncates are less than 1% of the laddis
559 * mix so this probably isn't worth the trouble to optimize.
563 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
564 xfs_trans_set_sync(tp
);
566 code
= xfs_trans_commit(tp
, commit_flags
);
569 xfs_iunlock(ip
, lock_flags
);
572 * Release any dquot(s) the inode had kept before chown.
574 XFS_QM_DQRELE(mp
, olddquot1
);
575 XFS_QM_DQRELE(mp
, olddquot2
);
576 XFS_QM_DQRELE(mp
, udqp
);
577 XFS_QM_DQRELE(mp
, gdqp
);
583 if (DM_EVENT_ENABLED(ip
, DM_EVENT_ATTRIBUTE
) &&
584 !(flags
& XFS_ATTR_DMI
)) {
585 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_ATTRIBUTE
, ip
, DM_RIGHT_NULL
,
586 NULL
, DM_RIGHT_NULL
, NULL
, NULL
,
587 0, 0, AT_DELAY_FLAG(flags
));
592 commit_flags
|= XFS_TRANS_ABORT
;
595 XFS_QM_DQRELE(mp
, udqp
);
596 XFS_QM_DQRELE(mp
, gdqp
);
598 xfs_trans_cancel(tp
, commit_flags
);
600 if (lock_flags
!= 0) {
601 xfs_iunlock(ip
, lock_flags
);
607 * The maximum pathlen is 1024 bytes. Since the minimum file system
608 * blocksize is 512 bytes, we can get a max of 2 extents back from
611 #define SYMLINK_MAPS 2
618 xfs_mount_t
*mp
= ip
->i_mount
;
619 int pathlen
= ip
->i_d
.di_size
;
620 int nmaps
= SYMLINK_MAPS
;
621 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
628 error
= xfs_bmapi(NULL
, ip
, 0, XFS_B_TO_FSB(mp
, pathlen
), 0, NULL
, 0,
629 mval
, &nmaps
, NULL
, NULL
);
633 for (n
= 0; n
< nmaps
; n
++) {
634 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
635 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
637 bp
= xfs_buf_read(mp
->m_ddev_targp
, d
, BTOBB(byte_cnt
), 0);
638 error
= XFS_BUF_GETERROR(bp
);
640 xfs_ioerror_alert("xfs_readlink",
641 ip
->i_mount
, bp
, XFS_BUF_ADDR(bp
));
645 if (pathlen
< byte_cnt
)
649 memcpy(link
, XFS_BUF_PTR(bp
), byte_cnt
);
653 link
[ip
->i_d
.di_size
] = '\0';
665 xfs_mount_t
*mp
= ip
->i_mount
;
669 xfs_itrace_entry(ip
);
671 if (XFS_FORCED_SHUTDOWN(mp
))
672 return XFS_ERROR(EIO
);
674 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
676 ASSERT((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
);
677 ASSERT(ip
->i_d
.di_size
<= MAXPATHLEN
);
679 pathlen
= ip
->i_d
.di_size
;
683 if (ip
->i_df
.if_flags
& XFS_IFINLINE
) {
684 memcpy(link
, ip
->i_df
.if_u1
.if_data
, pathlen
);
685 link
[pathlen
] = '\0';
687 error
= xfs_readlink_bmap(ip
, link
);
691 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
698 * This is called to sync the inode and its data out to disk. We need to hold
699 * the I/O lock while flushing the data, and the inode lock while flushing the
700 * inode. The inode lock CANNOT be held while flushing the data, so acquire
701 * after we're done with that.
709 int log_flushed
= 0, changed
= 1;
711 xfs_itrace_entry(ip
);
713 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
714 return XFS_ERROR(EIO
);
716 /* capture size updates in I/O completion before writing the inode. */
717 error
= filemap_fdatawait(vn_to_inode(XFS_ITOV(ip
))->i_mapping
);
719 return XFS_ERROR(error
);
722 * We always need to make sure that the required inode state is safe on
723 * disk. The vnode might be clean but we still might need to force the
724 * log because of committed transactions that haven't hit the disk yet.
725 * Likewise, there could be unflushed non-transactional changes to the
726 * inode core that have to go to disk and this requires us to issue
727 * a synchronous transaction to capture these changes correctly.
729 * This code relies on the assumption that if the update_* fields
730 * of the inode are clear and the inode is unpinned then it is clean
731 * and no action is required.
733 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
735 if (!(ip
->i_update_size
|| ip
->i_update_core
)) {
737 * Timestamps/size haven't changed since last inode flush or
738 * inode transaction commit. That means either nothing got
739 * written or a transaction committed which caught the updates.
740 * If the latter happened and the transaction hasn't hit the
741 * disk yet, the inode will be still be pinned. If it is,
745 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
747 if (xfs_ipincount(ip
)) {
748 error
= _xfs_log_force(ip
->i_mount
, (xfs_lsn_t
)0,
749 XFS_LOG_FORCE
| XFS_LOG_SYNC
,
753 * If the inode is not pinned and nothing has changed
754 * we don't need to flush the cache.
760 * Kick off a transaction to log the inode core to get the
761 * updates. The sync transaction will also force the log.
763 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
764 tp
= xfs_trans_alloc(ip
->i_mount
, XFS_TRANS_FSYNC_TS
);
765 error
= xfs_trans_reserve(tp
, 0,
766 XFS_FSYNC_TS_LOG_RES(ip
->i_mount
), 0, 0, 0);
768 xfs_trans_cancel(tp
, 0);
771 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
774 * Note - it's possible that we might have pushed ourselves out
775 * of the way during trans_reserve which would flush the inode.
776 * But there's no guarantee that the inode buffer has actually
777 * gone out yet (it's delwri). Plus the buffer could be pinned
778 * anyway if it's part of an inode in another recent
779 * transaction. So we play it safe and fire off the
780 * transaction anyway.
782 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
783 xfs_trans_ihold(tp
, ip
);
784 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
785 xfs_trans_set_sync(tp
);
786 error
= _xfs_trans_commit(tp
, 0, &log_flushed
);
788 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
791 if ((ip
->i_mount
->m_flags
& XFS_MOUNT_BARRIER
) && changed
) {
793 * If the log write didn't issue an ordered tag we need
794 * to flush the disk cache for the data device now.
797 xfs_blkdev_issue_flush(ip
->i_mount
->m_ddev_targp
);
800 * If this inode is on the RT dev we need to flush that
803 if (XFS_IS_REALTIME_INODE(ip
))
804 xfs_blkdev_issue_flush(ip
->i_mount
->m_rtdev_targp
);
811 * This is called by xfs_inactive to free any blocks beyond eof
812 * when the link count isn't zero and by xfs_dm_punch_hole() when
813 * punching a hole to EOF.
823 xfs_fileoff_t end_fsb
;
824 xfs_fileoff_t last_fsb
;
825 xfs_filblks_t map_len
;
827 xfs_bmbt_irec_t imap
;
828 int use_iolock
= (flags
& XFS_FREE_EOF_LOCK
);
831 * Figure out if there are any blocks beyond the end
832 * of the file. If not, then there is nothing to do.
834 end_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)ip
->i_size
));
835 last_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_MAXIOFFSET(mp
));
836 map_len
= last_fsb
- end_fsb
;
841 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
842 error
= xfs_bmapi(NULL
, ip
, end_fsb
, map_len
, 0,
843 NULL
, 0, &imap
, &nimaps
, NULL
, NULL
);
844 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
846 if (!error
&& (nimaps
!= 0) &&
847 (imap
.br_startblock
!= HOLESTARTBLOCK
||
848 ip
->i_delayed_blks
)) {
850 * Attach the dquots to the inode up front.
852 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
856 * There are blocks after the end of file.
857 * Free them up now by truncating the file to
860 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
863 * Do the xfs_itruncate_start() call before
864 * reserving any log space because
865 * itruncate_start will call into the buffer
867 * do that within a transaction.
870 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
871 error
= xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
,
874 xfs_trans_cancel(tp
, 0);
876 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
880 error
= xfs_trans_reserve(tp
, 0,
881 XFS_ITRUNCATE_LOG_RES(mp
),
882 0, XFS_TRANS_PERM_LOG_RES
,
883 XFS_ITRUNCATE_LOG_COUNT
);
885 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
886 xfs_trans_cancel(tp
, 0);
887 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
891 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
892 xfs_trans_ijoin(tp
, ip
,
895 xfs_trans_ihold(tp
, ip
);
897 error
= xfs_itruncate_finish(&tp
, ip
,
902 * If we get an error at this point we
903 * simply don't bother truncating the file.
907 (XFS_TRANS_RELEASE_LOG_RES
|
910 error
= xfs_trans_commit(tp
,
911 XFS_TRANS_RELEASE_LOG_RES
);
913 xfs_iunlock(ip
, (use_iolock
? (XFS_IOLOCK_EXCL
|XFS_ILOCK_EXCL
)
920 * Free a symlink that has blocks associated with it.
923 xfs_inactive_symlink_rmt(
931 xfs_fsblock_t first_block
;
932 xfs_bmap_free_t free_list
;
935 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
943 ASSERT(ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
));
945 * We're freeing a symlink that has some
946 * blocks allocated to it. Free the
947 * blocks here. We know that we've got
948 * either 1 or 2 extents and that we can
949 * free them all in one bunmapi call.
951 ASSERT(ip
->i_d
.di_nextents
> 0 && ip
->i_d
.di_nextents
<= 2);
952 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
953 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
954 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
955 xfs_trans_cancel(tp
, 0);
960 * Lock the inode, fix the size, and join it to the transaction.
961 * Hold it so in the normal path, we still have it locked for
962 * the second transaction. In the error paths we need it
963 * held so the cancel won't rele it, see below.
965 xfs_ilock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
966 size
= (int)ip
->i_d
.di_size
;
968 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
969 xfs_trans_ihold(tp
, ip
);
970 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
972 * Find the block(s) so we can inval and unmap them.
975 XFS_BMAP_INIT(&free_list
, &first_block
);
976 nmaps
= ARRAY_SIZE(mval
);
977 if ((error
= xfs_bmapi(tp
, ip
, 0, XFS_B_TO_FSB(mp
, size
),
978 XFS_BMAPI_METADATA
, &first_block
, 0, mval
, &nmaps
,
982 * Invalidate the block(s).
984 for (i
= 0; i
< nmaps
; i
++) {
985 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
986 XFS_FSB_TO_DADDR(mp
, mval
[i
].br_startblock
),
987 XFS_FSB_TO_BB(mp
, mval
[i
].br_blockcount
), 0);
988 xfs_trans_binval(tp
, bp
);
991 * Unmap the dead block(s) to the free_list.
993 if ((error
= xfs_bunmapi(tp
, ip
, 0, size
, XFS_BMAPI_METADATA
, nmaps
,
994 &first_block
, &free_list
, NULL
, &done
)))
998 * Commit the first transaction. This logs the EFI and the inode.
1000 if ((error
= xfs_bmap_finish(&tp
, &free_list
, &committed
)))
1003 * The transaction must have been committed, since there were
1004 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
1005 * The new tp has the extent freeing and EFDs.
1009 * The first xact was committed, so add the inode to the new one.
1010 * Mark it dirty so it will be logged and moved forward in the log as
1011 * part of every commit.
1013 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1014 xfs_trans_ihold(tp
, ip
);
1015 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1017 * Get a new, empty transaction to return to our caller.
1019 ntp
= xfs_trans_dup(tp
);
1021 * Commit the transaction containing extent freeing and EFDs.
1022 * If we get an error on the commit here or on the reserve below,
1023 * we need to unlock the inode since the new transaction doesn't
1024 * have the inode attached.
1026 error
= xfs_trans_commit(tp
, 0);
1029 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1033 * Remove the memory for extent descriptions (just bookkeeping).
1035 if (ip
->i_df
.if_bytes
)
1036 xfs_idata_realloc(ip
, -ip
->i_df
.if_bytes
, XFS_DATA_FORK
);
1037 ASSERT(ip
->i_df
.if_bytes
== 0);
1039 * Put an itruncate log reservation in the new transaction
1042 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
1043 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
1044 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1048 * Return with the inode locked but not joined to the transaction.
1054 xfs_bmap_cancel(&free_list
);
1057 * Have to come here with the inode locked and either
1058 * (held and in the transaction) or (not in the transaction).
1059 * If the inode isn't held then cancel would iput it, but
1060 * that's wrong since this is inactive and the vnode ref
1061 * count is 0 already.
1062 * Cancel won't do anything to the inode if held, but it still
1063 * needs to be locked until the cancel is done, if it was
1064 * joined to the transaction.
1066 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1067 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1074 xfs_inactive_symlink_local(
1080 ASSERT(ip
->i_d
.di_size
<= XFS_IFORK_DSIZE(ip
));
1082 * We're freeing a symlink which fit into
1083 * the inode. Just free the memory used
1084 * to hold the old symlink.
1086 error
= xfs_trans_reserve(*tpp
, 0,
1087 XFS_ITRUNCATE_LOG_RES(ip
->i_mount
),
1088 0, XFS_TRANS_PERM_LOG_RES
,
1089 XFS_ITRUNCATE_LOG_COUNT
);
1092 xfs_trans_cancel(*tpp
, 0);
1096 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1099 * Zero length symlinks _can_ exist.
1101 if (ip
->i_df
.if_bytes
> 0) {
1102 xfs_idata_realloc(ip
,
1103 -(ip
->i_df
.if_bytes
),
1105 ASSERT(ip
->i_df
.if_bytes
== 0);
1119 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
1122 ASSERT(ip
->i_d
.di_forkoff
!= 0);
1123 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1124 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1128 error
= xfs_attr_inactive(ip
);
1132 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1133 error
= xfs_trans_reserve(tp
, 0,
1134 XFS_IFREE_LOG_RES(mp
),
1135 0, XFS_TRANS_PERM_LOG_RES
,
1136 XFS_INACTIVE_LOG_COUNT
);
1140 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1141 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1142 xfs_trans_ihold(tp
, ip
);
1143 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
1145 ASSERT(ip
->i_d
.di_anextents
== 0);
1151 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1152 xfs_trans_cancel(tp
, 0);
1155 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1163 bhv_vnode_t
*vp
= XFS_ITOV(ip
);
1164 xfs_mount_t
*mp
= ip
->i_mount
;
1167 if (!S_ISREG(ip
->i_d
.di_mode
) || (ip
->i_d
.di_mode
== 0))
1170 /* If this is a read-only mount, don't do this (would generate I/O) */
1171 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
1174 if (!XFS_FORCED_SHUTDOWN(mp
)) {
1178 * If we are using filestreams, and we have an unlinked
1179 * file that we are processing the last close on, then nothing
1180 * will be able to reopen and write to this file. Purge this
1181 * inode from the filestreams cache so that it doesn't delay
1182 * teardown of the inode.
1184 if ((ip
->i_d
.di_nlink
== 0) && xfs_inode_is_filestream(ip
))
1185 xfs_filestream_deassociate(ip
);
1188 * If we previously truncated this file and removed old data
1189 * in the process, we want to initiate "early" writeout on
1190 * the last close. This is an attempt to combat the notorious
1191 * NULL files problem which is particularly noticable from a
1192 * truncate down, buffered (re-)write (delalloc), followed by
1193 * a crash. What we are effectively doing here is
1194 * significantly reducing the time window where we'd otherwise
1195 * be exposed to that problem.
1197 truncated
= xfs_iflags_test_and_clear(ip
, XFS_ITRUNCATED
);
1198 if (truncated
&& VN_DIRTY(vp
) && ip
->i_delayed_blks
> 0)
1199 xfs_flush_pages(ip
, 0, -1, XFS_B_ASYNC
, FI_NONE
);
1202 if (ip
->i_d
.di_nlink
!= 0) {
1203 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
1204 ((ip
->i_size
> 0) || (VN_CACHED(vp
) > 0 ||
1205 ip
->i_delayed_blks
> 0)) &&
1206 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
)) &&
1207 (!(ip
->i_d
.di_flags
&
1208 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)))) {
1209 error
= xfs_free_eofblocks(mp
, ip
, XFS_FREE_EOF_LOCK
);
1221 * This is called when the vnode reference count for the vnode
1222 * goes to zero. If the file has been unlinked, then it must
1223 * now be truncated. Also, we clear all of the read-ahead state
1224 * kept for the inode here since the file is now closed.
1230 bhv_vnode_t
*vp
= XFS_ITOV(ip
);
1231 xfs_bmap_free_t free_list
;
1232 xfs_fsblock_t first_block
;
1239 xfs_itrace_entry(ip
);
1242 * If the inode is already free, then there can be nothing
1245 if (ip
->i_d
.di_mode
== 0 || VN_BAD(vp
)) {
1246 ASSERT(ip
->i_df
.if_real_bytes
== 0);
1247 ASSERT(ip
->i_df
.if_broot_bytes
== 0);
1248 return VN_INACTIVE_CACHE
;
1252 * Only do a truncate if it's a regular file with
1253 * some actual space in it. It's OK to look at the
1254 * inode's fields without the lock because we're the
1255 * only one with a reference to the inode.
1257 truncate
= ((ip
->i_d
.di_nlink
== 0) &&
1258 ((ip
->i_d
.di_size
!= 0) || (ip
->i_size
!= 0) ||
1259 (ip
->i_d
.di_nextents
> 0) || (ip
->i_delayed_blks
> 0)) &&
1260 ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
));
1264 if (ip
->i_d
.di_nlink
== 0 && DM_EVENT_ENABLED(ip
, DM_EVENT_DESTROY
))
1265 XFS_SEND_DESTROY(mp
, ip
, DM_RIGHT_NULL
);
1269 /* If this is a read-only mount, don't do this (would generate I/O) */
1270 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
1273 if (ip
->i_d
.di_nlink
!= 0) {
1274 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
1275 ((ip
->i_size
> 0) || (VN_CACHED(vp
) > 0 ||
1276 ip
->i_delayed_blks
> 0)) &&
1277 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
) &&
1278 (!(ip
->i_d
.di_flags
&
1279 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)) ||
1280 (ip
->i_delayed_blks
!= 0)))) {
1281 error
= xfs_free_eofblocks(mp
, ip
, XFS_FREE_EOF_LOCK
);
1283 return VN_INACTIVE_CACHE
;
1288 ASSERT(ip
->i_d
.di_nlink
== 0);
1290 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
1291 return VN_INACTIVE_CACHE
;
1293 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1296 * Do the xfs_itruncate_start() call before
1297 * reserving any log space because itruncate_start
1298 * will call into the buffer cache and we can't
1299 * do that within a transaction.
1301 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
1303 error
= xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
, 0);
1305 xfs_trans_cancel(tp
, 0);
1306 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1307 return VN_INACTIVE_CACHE
;
1310 error
= xfs_trans_reserve(tp
, 0,
1311 XFS_ITRUNCATE_LOG_RES(mp
),
1312 0, XFS_TRANS_PERM_LOG_RES
,
1313 XFS_ITRUNCATE_LOG_COUNT
);
1315 /* Don't call itruncate_cleanup */
1316 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1317 xfs_trans_cancel(tp
, 0);
1318 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1319 return VN_INACTIVE_CACHE
;
1322 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1323 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1324 xfs_trans_ihold(tp
, ip
);
1327 * normally, we have to run xfs_itruncate_finish sync.
1328 * But if filesystem is wsync and we're in the inactive
1329 * path, then we know that nlink == 0, and that the
1330 * xaction that made nlink == 0 is permanently committed
1331 * since xfs_remove runs as a synchronous transaction.
1333 error
= xfs_itruncate_finish(&tp
, ip
, 0, XFS_DATA_FORK
,
1334 (!(mp
->m_flags
& XFS_MOUNT_WSYNC
) ? 1 : 0));
1337 xfs_trans_cancel(tp
,
1338 XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1339 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1340 return VN_INACTIVE_CACHE
;
1342 } else if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
) {
1345 * If we get an error while cleaning up a
1346 * symlink we bail out.
1348 error
= (ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
)) ?
1349 xfs_inactive_symlink_rmt(ip
, &tp
) :
1350 xfs_inactive_symlink_local(ip
, &tp
);
1354 return VN_INACTIVE_CACHE
;
1357 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1358 xfs_trans_ihold(tp
, ip
);
1360 error
= xfs_trans_reserve(tp
, 0,
1361 XFS_IFREE_LOG_RES(mp
),
1362 0, XFS_TRANS_PERM_LOG_RES
,
1363 XFS_INACTIVE_LOG_COUNT
);
1365 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1366 xfs_trans_cancel(tp
, 0);
1367 return VN_INACTIVE_CACHE
;
1370 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1371 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1372 xfs_trans_ihold(tp
, ip
);
1376 * If there are attributes associated with the file
1377 * then blow them away now. The code calls a routine
1378 * that recursively deconstructs the attribute fork.
1379 * We need to just commit the current transaction
1380 * because we can't use it for xfs_attr_inactive().
1382 if (ip
->i_d
.di_anextents
> 0) {
1383 error
= xfs_inactive_attrs(ip
, &tp
);
1385 * If we got an error, the transaction is already
1386 * cancelled, and the inode is unlocked. Just get out.
1389 return VN_INACTIVE_CACHE
;
1390 } else if (ip
->i_afp
) {
1391 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
1397 XFS_BMAP_INIT(&free_list
, &first_block
);
1398 error
= xfs_ifree(tp
, ip
, &free_list
);
1401 * If we fail to free the inode, shut down. The cancel
1402 * might do that, we need to make sure. Otherwise the
1403 * inode might be lost for a long time or forever.
1405 if (!XFS_FORCED_SHUTDOWN(mp
)) {
1407 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1408 error
, mp
->m_fsname
);
1409 xfs_force_shutdown(mp
, SHUTDOWN_META_IO_ERROR
);
1411 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
1414 * Credit the quota account(s). The inode is gone.
1416 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, XFS_TRANS_DQ_ICOUNT
, -1);
1419 * Just ignore errors at this point. There is nothing we can
1420 * do except to try to keep going. Make sure it's not a silent
1423 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1425 xfs_fs_cmn_err(CE_NOTE
, mp
, "xfs_inactive: "
1426 "xfs_bmap_finish() returned error %d", error
);
1427 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1429 xfs_fs_cmn_err(CE_NOTE
, mp
, "xfs_inactive: "
1430 "xfs_trans_commit() returned error %d", error
);
1433 * Release the dquots held by inode, if any.
1435 XFS_QM_DQDETACH(mp
, ip
);
1437 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1440 return VN_INACTIVE_CACHE
;
1444 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1445 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1446 * ci_name->name will point to a the actual name (caller must free) or
1447 * will be set to NULL if an exact match is found.
1452 struct xfs_name
*name
,
1454 struct xfs_name
*ci_name
)
1460 xfs_itrace_entry(dp
);
1462 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
1463 return XFS_ERROR(EIO
);
1465 lock_mode
= xfs_ilock_map_shared(dp
);
1466 error
= xfs_dir_lookup(NULL
, dp
, name
, &inum
, ci_name
);
1467 xfs_iunlock_map_shared(dp
, lock_mode
);
1472 error
= xfs_iget(dp
->i_mount
, NULL
, inum
, 0, 0, ipp
, 0);
1476 xfs_itrace_ref(*ipp
);
1481 kmem_free(ci_name
->name
);
1490 struct xfs_name
*name
,
1496 xfs_mount_t
*mp
= dp
->i_mount
;
1500 xfs_bmap_free_t free_list
;
1501 xfs_fsblock_t first_block
;
1502 boolean_t unlock_dp_on_error
= B_FALSE
;
1503 int dm_event_sent
= 0;
1507 struct xfs_dquot
*udqp
, *gdqp
;
1511 xfs_itrace_entry(dp
);
1513 if (DM_EVENT_ENABLED(dp
, DM_EVENT_CREATE
)) {
1514 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_CREATE
,
1515 dp
, DM_RIGHT_NULL
, NULL
,
1516 DM_RIGHT_NULL
, name
->name
, NULL
,
1524 if (XFS_FORCED_SHUTDOWN(mp
))
1525 return XFS_ERROR(EIO
);
1527 /* Return through std_return after this point. */
1530 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
1531 prid
= dp
->i_d
.di_projid
;
1533 prid
= (xfs_prid_t
)dfltprid
;
1536 * Make sure that we have allocated dquot(s) on disk.
1538 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
1539 current_fsuid(credp
), current_fsgid(credp
), prid
,
1540 XFS_QMOPT_QUOTALL
|XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
1546 tp
= xfs_trans_alloc(mp
, XFS_TRANS_CREATE
);
1547 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1548 resblks
= XFS_CREATE_SPACE_RES(mp
, name
->len
);
1550 * Initially assume that the file does not exist and
1551 * reserve the resources for that case. If that is not
1552 * the case we'll drop the one we have and get a more
1553 * appropriate transaction later.
1555 error
= xfs_trans_reserve(tp
, resblks
, XFS_CREATE_LOG_RES(mp
), 0,
1556 XFS_TRANS_PERM_LOG_RES
, XFS_CREATE_LOG_COUNT
);
1557 if (error
== ENOSPC
) {
1559 error
= xfs_trans_reserve(tp
, 0, XFS_CREATE_LOG_RES(mp
), 0,
1560 XFS_TRANS_PERM_LOG_RES
, XFS_CREATE_LOG_COUNT
);
1567 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
1568 unlock_dp_on_error
= B_TRUE
;
1570 XFS_BMAP_INIT(&free_list
, &first_block
);
1575 * Reserve disk quota and the inode.
1577 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
1581 error
= xfs_dir_canenter(tp
, dp
, name
, resblks
);
1584 error
= xfs_dir_ialloc(&tp
, dp
, mode
, 1,
1585 rdev
, credp
, prid
, resblks
> 0,
1588 if (error
== ENOSPC
)
1595 * At this point, we've gotten a newly allocated inode.
1596 * It is locked (and joined to the transaction).
1599 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1602 * Now we join the directory inode to the transaction. We do not do it
1603 * earlier because xfs_dir_ialloc might commit the previous transaction
1604 * (and release all the locks). An error from here on will result in
1605 * the transaction cancel unlocking dp so don't do it explicitly in the
1609 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
1610 unlock_dp_on_error
= B_FALSE
;
1612 error
= xfs_dir_createname(tp
, dp
, name
, ip
->i_ino
,
1613 &first_block
, &free_list
, resblks
?
1614 resblks
- XFS_IALLOC_SPACE_RES(mp
) : 0);
1616 ASSERT(error
!= ENOSPC
);
1619 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1620 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1623 * If this is a synchronous mount, make sure that the
1624 * create transaction goes to disk before returning to
1627 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
1628 xfs_trans_set_sync(tp
);
1634 * Attach the dquot(s) to the inodes and modify them incore.
1635 * These ids of the inode couldn't have changed since the new
1636 * inode has been locked ever since it was created.
1638 XFS_QM_DQVOPCREATE(mp
, tp
, ip
, udqp
, gdqp
);
1641 * xfs_trans_commit normally decrements the vnode ref count
1642 * when it unlocks the inode. Since we want to return the
1643 * vnode to the caller, we bump the vnode ref count now.
1647 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1649 xfs_bmap_cancel(&free_list
);
1653 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1660 XFS_QM_DQRELE(mp
, udqp
);
1661 XFS_QM_DQRELE(mp
, gdqp
);
1665 /* Fallthrough to std_return with error = 0 */
1668 if ((*ipp
|| (error
!= 0 && dm_event_sent
!= 0)) &&
1669 DM_EVENT_ENABLED(dp
, DM_EVENT_POSTCREATE
)) {
1670 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTCREATE
,
1673 DM_RIGHT_NULL
, name
->name
, NULL
,
1679 cancel_flags
|= XFS_TRANS_ABORT
;
1684 xfs_trans_cancel(tp
, cancel_flags
);
1686 XFS_QM_DQRELE(mp
, udqp
);
1687 XFS_QM_DQRELE(mp
, gdqp
);
1689 if (unlock_dp_on_error
)
1690 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
1696 * Wait until after the current transaction is aborted to
1697 * release the inode. This prevents recursive transactions
1698 * and deadlocks from xfs_inactive.
1700 cancel_flags
|= XFS_TRANS_ABORT
;
1701 xfs_trans_cancel(tp
, cancel_flags
);
1704 XFS_QM_DQRELE(mp
, udqp
);
1705 XFS_QM_DQRELE(mp
, gdqp
);
1712 * Some counters to see if (and how often) we are hitting some deadlock
1713 * prevention code paths.
1717 int xfs_rm_lock_delays
;
1718 int xfs_rm_attempts
;
1722 * The following routine will lock the inodes associated with the
1723 * directory and the named entry in the directory. The locks are
1724 * acquired in increasing inode number.
1726 * If the entry is "..", then only the directory is locked. The
1727 * vnode ref count will still include that from the .. entry in
1730 * There is a deadlock we need to worry about. If the locked directory is
1731 * in the AIL, it might be blocking up the log. The next inode we lock
1732 * could be already locked by another thread waiting for log space (e.g
1733 * a permanent log reservation with a long running transaction (see
1734 * xfs_itruncate_finish)). To solve this, we must check if the directory
1735 * is in the ail and use lock_nowait. If we can't lock, we need to
1736 * drop the inode lock on the directory and try again. xfs_iunlock will
1737 * potentially push the tail if we were holding up the log.
1740 xfs_lock_dir_and_entry(
1742 xfs_inode_t
*ip
) /* inode of entry 'name' */
1746 xfs_inode_t
*ips
[2];
1755 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
1762 * We want to lock in increasing inum. Since we've already
1763 * acquired the lock on the directory, we may need to release
1764 * if if the inum of the entry turns out to be less.
1766 if (e_inum
> dp
->i_ino
) {
1768 * We are already in the right order, so just
1769 * lock on the inode of the entry.
1770 * We need to use nowait if dp is in the AIL.
1773 lp
= (xfs_log_item_t
*)dp
->i_itemp
;
1774 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
1775 if (!xfs_ilock_nowait(ip
, XFS_ILOCK_EXCL
)) {
1782 * Unlock dp and try again.
1783 * xfs_iunlock will try to push the tail
1784 * if the inode is in the AIL.
1787 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
1789 if ((attempts
% 5) == 0) {
1790 delay(1); /* Don't just spin the CPU */
1792 xfs_rm_lock_delays
++;
1798 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1800 } else if (e_inum
< dp
->i_ino
) {
1801 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
1805 xfs_lock_inodes(ips
, 2, XFS_ILOCK_EXCL
);
1807 /* else e_inum == dp->i_ino */
1808 /* This can happen if we're asked to lock /x/..
1809 * the entry is "..", which is also the parent directory.
1817 int xfs_small_retries
;
1818 int xfs_middle_retries
;
1819 int xfs_lots_retries
;
1820 int xfs_lock_delays
;
1824 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1828 xfs_lock_inumorder(int lock_mode
, int subclass
)
1830 if (lock_mode
& (XFS_IOLOCK_SHARED
|XFS_IOLOCK_EXCL
))
1831 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_IOLOCK_SHIFT
;
1832 if (lock_mode
& (XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
))
1833 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_ILOCK_SHIFT
;
1839 * The following routine will lock n inodes in exclusive mode.
1840 * We assume the caller calls us with the inodes in i_ino order.
1842 * We need to detect deadlock where an inode that we lock
1843 * is in the AIL and we start waiting for another inode that is locked
1844 * by a thread in a long running transaction (such as truncate). This can
1845 * result in deadlock since the long running trans might need to wait
1846 * for the inode we just locked in order to push the tail and free space
1855 int attempts
= 0, i
, j
, try_lock
;
1858 ASSERT(ips
&& (inodes
>= 2)); /* we need at least two */
1864 for (; i
< inodes
; i
++) {
1867 if (i
&& (ips
[i
] == ips
[i
-1])) /* Already locked */
1871 * If try_lock is not set yet, make sure all locked inodes
1872 * are not in the AIL.
1873 * If any are, set try_lock to be used later.
1877 for (j
= (i
- 1); j
>= 0 && !try_lock
; j
--) {
1878 lp
= (xfs_log_item_t
*)ips
[j
]->i_itemp
;
1879 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
1886 * If any of the previous locks we have locked is in the AIL,
1887 * we must TRY to get the second and subsequent locks. If
1888 * we can't get any, we must release all we have
1893 /* try_lock must be 0 if i is 0. */
1895 * try_lock means we have an inode locked
1896 * that is in the AIL.
1899 if (!xfs_ilock_nowait(ips
[i
], xfs_lock_inumorder(lock_mode
, i
))) {
1903 * Unlock all previous guys and try again.
1904 * xfs_iunlock will try to push the tail
1905 * if the inode is in the AIL.
1908 for(j
= i
- 1; j
>= 0; j
--) {
1911 * Check to see if we've already
1912 * unlocked this one.
1913 * Not the first one going back,
1914 * and the inode ptr is the same.
1916 if ((j
!= (i
- 1)) && ips
[j
] ==
1920 xfs_iunlock(ips
[j
], lock_mode
);
1923 if ((attempts
% 5) == 0) {
1924 delay(1); /* Don't just spin the CPU */
1934 xfs_ilock(ips
[i
], xfs_lock_inumorder(lock_mode
, i
));
1940 if (attempts
< 5) xfs_small_retries
++;
1941 else if (attempts
< 100) xfs_middle_retries
++;
1942 else xfs_lots_retries
++;
1952 struct xfs_name
*name
,
1955 xfs_mount_t
*mp
= dp
->i_mount
;
1956 xfs_trans_t
*tp
= NULL
;
1957 int is_dir
= S_ISDIR(ip
->i_d
.di_mode
);
1959 xfs_bmap_free_t free_list
;
1960 xfs_fsblock_t first_block
;
1967 xfs_itrace_entry(dp
);
1968 xfs_itrace_entry(ip
);
1970 if (XFS_FORCED_SHUTDOWN(mp
))
1971 return XFS_ERROR(EIO
);
1973 if (DM_EVENT_ENABLED(dp
, DM_EVENT_REMOVE
)) {
1974 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_REMOVE
, dp
, DM_RIGHT_NULL
,
1975 NULL
, DM_RIGHT_NULL
, name
->name
, NULL
,
1976 ip
->i_d
.di_mode
, 0, 0);
1981 error
= XFS_QM_DQATTACH(mp
, dp
, 0);
1985 error
= XFS_QM_DQATTACH(mp
, ip
, 0);
1990 tp
= xfs_trans_alloc(mp
, XFS_TRANS_RMDIR
);
1991 log_count
= XFS_DEFAULT_LOG_COUNT
;
1993 tp
= xfs_trans_alloc(mp
, XFS_TRANS_REMOVE
);
1994 log_count
= XFS_REMOVE_LOG_COUNT
;
1996 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1999 * We try to get the real space reservation first,
2000 * allowing for directory btree deletion(s) implying
2001 * possible bmap insert(s). If we can't get the space
2002 * reservation then we use 0 instead, and avoid the bmap
2003 * btree insert(s) in the directory code by, if the bmap
2004 * insert tries to happen, instead trimming the LAST
2005 * block from the directory.
2007 resblks
= XFS_REMOVE_SPACE_RES(mp
);
2008 error
= xfs_trans_reserve(tp
, resblks
, XFS_REMOVE_LOG_RES(mp
), 0,
2009 XFS_TRANS_PERM_LOG_RES
, log_count
);
2010 if (error
== ENOSPC
) {
2012 error
= xfs_trans_reserve(tp
, 0, XFS_REMOVE_LOG_RES(mp
), 0,
2013 XFS_TRANS_PERM_LOG_RES
, log_count
);
2016 ASSERT(error
!= ENOSPC
);
2018 goto out_trans_cancel
;
2021 error
= xfs_lock_dir_and_entry(dp
, ip
);
2023 goto out_trans_cancel
;
2026 * At this point, we've gotten both the directory and the entry
2030 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
2033 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
2036 * If we're removing a directory perform some additional validation.
2039 ASSERT(ip
->i_d
.di_nlink
>= 2);
2040 if (ip
->i_d
.di_nlink
!= 2) {
2041 error
= XFS_ERROR(ENOTEMPTY
);
2042 goto out_trans_cancel
;
2044 if (!xfs_dir_isempty(ip
)) {
2045 error
= XFS_ERROR(ENOTEMPTY
);
2046 goto out_trans_cancel
;
2051 * Entry must exist since we did a lookup in xfs_lock_dir_and_entry.
2053 XFS_BMAP_INIT(&free_list
, &first_block
);
2054 error
= xfs_dir_removename(tp
, dp
, name
, ip
->i_ino
,
2055 &first_block
, &free_list
, resblks
);
2057 ASSERT(error
!= ENOENT
);
2058 goto out_bmap_cancel
;
2060 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2063 * Bump the in memory generation count on the parent
2064 * directory so that other can know that it has changed.
2067 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2071 * Drop the link from ip's "..".
2073 error
= xfs_droplink(tp
, dp
);
2075 goto out_bmap_cancel
;
2078 * Drop the link from dp to ip.
2080 error
= xfs_droplink(tp
, ip
);
2082 goto out_bmap_cancel
;
2085 * When removing a non-directory we need to log the parent
2086 * inode here for the i_gen update. For a directory this is
2087 * done implicitly by the xfs_droplink call for the ".." entry.
2089 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2093 * Drop the "." link from ip to self.
2095 error
= xfs_droplink(tp
, ip
);
2097 goto out_bmap_cancel
;
2100 * Determine if this is the last link while
2101 * we are in the transaction.
2103 link_zero
= (ip
->i_d
.di_nlink
== 0);
2106 * If this is a synchronous mount, make sure that the
2107 * remove transaction goes to disk before returning to
2110 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
))
2111 xfs_trans_set_sync(tp
);
2113 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2115 goto out_bmap_cancel
;
2117 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2122 * If we are using filestreams, kill the stream association.
2123 * If the file is still open it may get a new one but that
2124 * will get killed on last close in xfs_close() so we don't
2125 * have to worry about that.
2127 if (!is_dir
&& link_zero
&& xfs_inode_is_filestream(ip
))
2128 xfs_filestream_deassociate(ip
);
2130 xfs_itrace_exit(ip
);
2131 xfs_itrace_exit(dp
);
2134 if (DM_EVENT_ENABLED(dp
, DM_EVENT_POSTREMOVE
)) {
2135 XFS_SEND_NAMESP(mp
, DM_EVENT_POSTREMOVE
, dp
, DM_RIGHT_NULL
,
2136 NULL
, DM_RIGHT_NULL
, name
->name
, NULL
,
2137 ip
->i_d
.di_mode
, error
, 0);
2143 xfs_bmap_cancel(&free_list
);
2144 cancel_flags
|= XFS_TRANS_ABORT
;
2146 xfs_trans_cancel(tp
, cancel_flags
);
2154 struct xfs_name
*target_name
)
2156 xfs_mount_t
*mp
= tdp
->i_mount
;
2158 xfs_inode_t
*ips
[2];
2160 xfs_bmap_free_t free_list
;
2161 xfs_fsblock_t first_block
;
2166 xfs_itrace_entry(tdp
);
2167 xfs_itrace_entry(sip
);
2169 ASSERT(!S_ISDIR(sip
->i_d
.di_mode
));
2171 if (XFS_FORCED_SHUTDOWN(mp
))
2172 return XFS_ERROR(EIO
);
2174 if (DM_EVENT_ENABLED(tdp
, DM_EVENT_LINK
)) {
2175 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_LINK
,
2178 target_name
->name
, NULL
, 0, 0, 0);
2183 /* Return through std_return after this point. */
2185 error
= XFS_QM_DQATTACH(mp
, sip
, 0);
2186 if (!error
&& sip
!= tdp
)
2187 error
= XFS_QM_DQATTACH(mp
, tdp
, 0);
2191 tp
= xfs_trans_alloc(mp
, XFS_TRANS_LINK
);
2192 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2193 resblks
= XFS_LINK_SPACE_RES(mp
, target_name
->len
);
2194 error
= xfs_trans_reserve(tp
, resblks
, XFS_LINK_LOG_RES(mp
), 0,
2195 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
2196 if (error
== ENOSPC
) {
2198 error
= xfs_trans_reserve(tp
, 0, XFS_LINK_LOG_RES(mp
), 0,
2199 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
2206 if (sip
->i_ino
< tdp
->i_ino
) {
2214 xfs_lock_inodes(ips
, 2, XFS_ILOCK_EXCL
);
2217 * Increment vnode ref counts since xfs_trans_commit &
2218 * xfs_trans_cancel will both unlock the inodes and
2219 * decrement the associated ref counts.
2223 xfs_trans_ijoin(tp
, sip
, XFS_ILOCK_EXCL
);
2224 xfs_trans_ijoin(tp
, tdp
, XFS_ILOCK_EXCL
);
2227 * If the source has too many links, we can't make any more to it.
2229 if (sip
->i_d
.di_nlink
>= XFS_MAXLINK
) {
2230 error
= XFS_ERROR(EMLINK
);
2235 * If we are using project inheritance, we only allow hard link
2236 * creation in our tree when the project IDs are the same; else
2237 * the tree quota mechanism could be circumvented.
2239 if (unlikely((tdp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
) &&
2240 (tdp
->i_d
.di_projid
!= sip
->i_d
.di_projid
))) {
2241 error
= XFS_ERROR(EXDEV
);
2245 error
= xfs_dir_canenter(tp
, tdp
, target_name
, resblks
);
2249 XFS_BMAP_INIT(&free_list
, &first_block
);
2251 error
= xfs_dir_createname(tp
, tdp
, target_name
, sip
->i_ino
,
2252 &first_block
, &free_list
, resblks
);
2255 xfs_ichgtime(tdp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2257 xfs_trans_log_inode(tp
, tdp
, XFS_ILOG_CORE
);
2259 error
= xfs_bumplink(tp
, sip
);
2264 * If this is a synchronous mount, make sure that the
2265 * link transaction goes to disk before returning to
2268 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2269 xfs_trans_set_sync(tp
);
2272 error
= xfs_bmap_finish (&tp
, &free_list
, &committed
);
2274 xfs_bmap_cancel(&free_list
);
2278 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2282 /* Fall through to std_return with error = 0. */
2284 if (DM_EVENT_ENABLED(sip
, DM_EVENT_POSTLINK
)) {
2285 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTLINK
,
2288 target_name
->name
, NULL
, 0, error
, 0);
2293 cancel_flags
|= XFS_TRANS_ABORT
;
2297 xfs_trans_cancel(tp
, cancel_flags
);
2305 struct xfs_name
*dir_name
,
2310 xfs_mount_t
*mp
= dp
->i_mount
;
2311 xfs_inode_t
*cdp
; /* inode of created dir */
2316 xfs_bmap_free_t free_list
;
2317 xfs_fsblock_t first_block
;
2318 boolean_t unlock_dp_on_error
= B_FALSE
;
2319 boolean_t created
= B_FALSE
;
2320 int dm_event_sent
= 0;
2322 struct xfs_dquot
*udqp
, *gdqp
;
2325 if (XFS_FORCED_SHUTDOWN(mp
))
2326 return XFS_ERROR(EIO
);
2330 if (DM_EVENT_ENABLED(dp
, DM_EVENT_CREATE
)) {
2331 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_CREATE
,
2332 dp
, DM_RIGHT_NULL
, NULL
,
2333 DM_RIGHT_NULL
, dir_name
->name
, NULL
,
2340 /* Return through std_return after this point. */
2342 xfs_itrace_entry(dp
);
2346 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
2347 prid
= dp
->i_d
.di_projid
;
2349 prid
= (xfs_prid_t
)dfltprid
;
2352 * Make sure that we have allocated dquot(s) on disk.
2354 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
2355 current_fsuid(credp
), current_fsgid(credp
), prid
,
2356 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
2360 tp
= xfs_trans_alloc(mp
, XFS_TRANS_MKDIR
);
2361 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2362 resblks
= XFS_MKDIR_SPACE_RES(mp
, dir_name
->len
);
2363 error
= xfs_trans_reserve(tp
, resblks
, XFS_MKDIR_LOG_RES(mp
), 0,
2364 XFS_TRANS_PERM_LOG_RES
, XFS_MKDIR_LOG_COUNT
);
2365 if (error
== ENOSPC
) {
2367 error
= xfs_trans_reserve(tp
, 0, XFS_MKDIR_LOG_RES(mp
), 0,
2368 XFS_TRANS_PERM_LOG_RES
,
2369 XFS_MKDIR_LOG_COUNT
);
2376 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
2377 unlock_dp_on_error
= B_TRUE
;
2380 * Check for directory link count overflow.
2382 if (dp
->i_d
.di_nlink
>= XFS_MAXLINK
) {
2383 error
= XFS_ERROR(EMLINK
);
2388 * Reserve disk quota and the inode.
2390 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
2394 error
= xfs_dir_canenter(tp
, dp
, dir_name
, resblks
);
2398 * create the directory inode.
2400 error
= xfs_dir_ialloc(&tp
, dp
, mode
, 2,
2401 0, credp
, prid
, resblks
> 0,
2404 if (error
== ENOSPC
)
2408 xfs_itrace_ref(cdp
);
2411 * Now we add the directory inode to the transaction.
2412 * We waited until now since xfs_dir_ialloc might start
2413 * a new transaction. Had we joined the transaction
2414 * earlier, the locks might have gotten released. An error
2415 * from here on will result in the transaction cancel
2416 * unlocking dp so don't do it explicitly in the error path.
2419 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
2420 unlock_dp_on_error
= B_FALSE
;
2422 XFS_BMAP_INIT(&free_list
, &first_block
);
2424 error
= xfs_dir_createname(tp
, dp
, dir_name
, cdp
->i_ino
,
2425 &first_block
, &free_list
, resblks
?
2426 resblks
- XFS_IALLOC_SPACE_RES(mp
) : 0);
2428 ASSERT(error
!= ENOSPC
);
2431 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2434 * Bump the in memory version number of the parent directory
2435 * so that other processes accessing it will recognize that
2436 * the directory has changed.
2440 error
= xfs_dir_init(tp
, cdp
, dp
);
2445 error
= xfs_bumplink(tp
, dp
);
2455 * Attach the dquots to the new inode and modify the icount incore.
2457 XFS_QM_DQVOPCREATE(mp
, tp
, cdp
, udqp
, gdqp
);
2460 * If this is a synchronous mount, make sure that the
2461 * mkdir transaction goes to disk before returning to
2464 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2465 xfs_trans_set_sync(tp
);
2468 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2474 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2475 XFS_QM_DQRELE(mp
, udqp
);
2476 XFS_QM_DQRELE(mp
, gdqp
);
2481 /* Fall through to std_return with error = 0 or errno from
2482 * xfs_trans_commit. */
2485 if ((created
|| (error
!= 0 && dm_event_sent
!= 0)) &&
2486 DM_EVENT_ENABLED(dp
, DM_EVENT_POSTCREATE
)) {
2487 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTCREATE
,
2489 created
? cdp
: NULL
,
2491 dir_name
->name
, NULL
,
2498 xfs_bmap_cancel(&free_list
);
2500 cancel_flags
|= XFS_TRANS_ABORT
;
2502 xfs_trans_cancel(tp
, cancel_flags
);
2503 XFS_QM_DQRELE(mp
, udqp
);
2504 XFS_QM_DQRELE(mp
, gdqp
);
2506 if (unlock_dp_on_error
)
2507 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
2515 struct xfs_name
*link_name
,
2516 const char *target_path
,
2521 xfs_mount_t
*mp
= dp
->i_mount
;
2526 xfs_bmap_free_t free_list
;
2527 xfs_fsblock_t first_block
;
2528 boolean_t unlock_dp_on_error
= B_FALSE
;
2531 xfs_fileoff_t first_fsb
;
2532 xfs_filblks_t fs_blocks
;
2534 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
2536 const char *cur_chunk
;
2541 struct xfs_dquot
*udqp
, *gdqp
;
2549 xfs_itrace_entry(dp
);
2551 if (XFS_FORCED_SHUTDOWN(mp
))
2552 return XFS_ERROR(EIO
);
2555 * Check component lengths of the target path name.
2557 pathlen
= strlen(target_path
);
2558 if (pathlen
>= MAXPATHLEN
) /* total string too long */
2559 return XFS_ERROR(ENAMETOOLONG
);
2561 if (DM_EVENT_ENABLED(dp
, DM_EVENT_SYMLINK
)) {
2562 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_SYMLINK
, dp
,
2563 DM_RIGHT_NULL
, NULL
, DM_RIGHT_NULL
,
2564 link_name
->name
, target_path
, 0, 0, 0);
2569 /* Return through std_return after this point. */
2572 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
2573 prid
= dp
->i_d
.di_projid
;
2575 prid
= (xfs_prid_t
)dfltprid
;
2578 * Make sure that we have allocated dquot(s) on disk.
2580 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
2581 current_fsuid(credp
), current_fsgid(credp
), prid
,
2582 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
2586 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SYMLINK
);
2587 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2589 * The symlink will fit into the inode data fork?
2590 * There can't be any attributes so we get the whole variable part.
2592 if (pathlen
<= XFS_LITINO(mp
))
2595 fs_blocks
= XFS_B_TO_FSB(mp
, pathlen
);
2596 resblks
= XFS_SYMLINK_SPACE_RES(mp
, link_name
->len
, fs_blocks
);
2597 error
= xfs_trans_reserve(tp
, resblks
, XFS_SYMLINK_LOG_RES(mp
), 0,
2598 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
2599 if (error
== ENOSPC
&& fs_blocks
== 0) {
2601 error
= xfs_trans_reserve(tp
, 0, XFS_SYMLINK_LOG_RES(mp
), 0,
2602 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
2609 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
2610 unlock_dp_on_error
= B_TRUE
;
2613 * Check whether the directory allows new symlinks or not.
2615 if (dp
->i_d
.di_flags
& XFS_DIFLAG_NOSYMLINKS
) {
2616 error
= XFS_ERROR(EPERM
);
2621 * Reserve disk quota : blocks and inode.
2623 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
2628 * Check for ability to enter directory entry, if no space reserved.
2630 error
= xfs_dir_canenter(tp
, dp
, link_name
, resblks
);
2634 * Initialize the bmap freelist prior to calling either
2635 * bmapi or the directory create code.
2637 XFS_BMAP_INIT(&free_list
, &first_block
);
2640 * Allocate an inode for the symlink.
2642 error
= xfs_dir_ialloc(&tp
, dp
, S_IFLNK
| (mode
& ~S_IFMT
),
2643 1, 0, credp
, prid
, resblks
> 0, &ip
, NULL
);
2645 if (error
== ENOSPC
)
2652 * An error after we've joined dp to the transaction will result in the
2653 * transaction cancel unlocking dp so don't do it explicitly in the
2657 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
2658 unlock_dp_on_error
= B_FALSE
;
2661 * Also attach the dquot(s) to it, if applicable.
2663 XFS_QM_DQVOPCREATE(mp
, tp
, ip
, udqp
, gdqp
);
2666 resblks
-= XFS_IALLOC_SPACE_RES(mp
);
2668 * If the symlink will fit into the inode, write it inline.
2670 if (pathlen
<= XFS_IFORK_DSIZE(ip
)) {
2671 xfs_idata_realloc(ip
, pathlen
, XFS_DATA_FORK
);
2672 memcpy(ip
->i_df
.if_u1
.if_data
, target_path
, pathlen
);
2673 ip
->i_d
.di_size
= pathlen
;
2676 * The inode was initially created in extent format.
2678 ip
->i_df
.if_flags
&= ~(XFS_IFEXTENTS
| XFS_IFBROOT
);
2679 ip
->i_df
.if_flags
|= XFS_IFINLINE
;
2681 ip
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
2682 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_DDATA
| XFS_ILOG_CORE
);
2686 nmaps
= SYMLINK_MAPS
;
2688 error
= xfs_bmapi(tp
, ip
, first_fsb
, fs_blocks
,
2689 XFS_BMAPI_WRITE
| XFS_BMAPI_METADATA
,
2690 &first_block
, resblks
, mval
, &nmaps
,
2697 resblks
-= fs_blocks
;
2698 ip
->i_d
.di_size
= pathlen
;
2699 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2701 cur_chunk
= target_path
;
2702 for (n
= 0; n
< nmaps
; n
++) {
2703 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
2704 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
2705 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
2706 BTOBB(byte_cnt
), 0);
2707 ASSERT(bp
&& !XFS_BUF_GETERROR(bp
));
2708 if (pathlen
< byte_cnt
) {
2711 pathlen
-= byte_cnt
;
2713 memcpy(XFS_BUF_PTR(bp
), cur_chunk
, byte_cnt
);
2714 cur_chunk
+= byte_cnt
;
2716 xfs_trans_log_buf(tp
, bp
, 0, byte_cnt
- 1);
2721 * Create the directory entry for the symlink.
2723 error
= xfs_dir_createname(tp
, dp
, link_name
, ip
->i_ino
,
2724 &first_block
, &free_list
, resblks
);
2727 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2728 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2731 * Bump the in memory version number of the parent directory
2732 * so that other processes accessing it will recognize that
2733 * the directory has changed.
2738 * If this is a synchronous mount, make sure that the
2739 * symlink transaction goes to disk before returning to
2742 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2743 xfs_trans_set_sync(tp
);
2747 * xfs_trans_commit normally decrements the vnode ref count
2748 * when it unlocks the inode. Since we want to return the
2749 * vnode to the caller, we bump the vnode ref count now.
2753 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2757 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2758 XFS_QM_DQRELE(mp
, udqp
);
2759 XFS_QM_DQRELE(mp
, gdqp
);
2761 /* Fall through to std_return with error = 0 or errno from
2762 * xfs_trans_commit */
2764 if (DM_EVENT_ENABLED(dp
, DM_EVENT_POSTSYMLINK
)) {
2765 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTSYMLINK
,
2768 DM_RIGHT_NULL
, link_name
->name
,
2769 target_path
, 0, error
, 0);
2779 xfs_bmap_cancel(&free_list
);
2780 cancel_flags
|= XFS_TRANS_ABORT
;
2782 xfs_trans_cancel(tp
, cancel_flags
);
2783 XFS_QM_DQRELE(mp
, udqp
);
2784 XFS_QM_DQRELE(mp
, gdqp
);
2786 if (unlock_dp_on_error
)
2787 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
2797 xfs_mount_t
*mp
= ip
->i_mount
;
2800 if (XFS_FORCED_SHUTDOWN(mp
))
2801 return XFS_ERROR(EIO
);
2804 * Bypass inodes which have already been cleaned by
2805 * the inode flush clustering code inside xfs_iflush
2807 if (xfs_inode_clean(ip
))
2811 * We make this non-blocking if the inode is contended,
2812 * return EAGAIN to indicate to the caller that they
2813 * did not succeed. This prevents the flush path from
2814 * blocking on inodes inside another operation right
2815 * now, they get caught later by xfs_sync.
2817 if (flags
& FLUSH_SYNC
) {
2818 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
2820 } else if (xfs_ilock_nowait(ip
, XFS_ILOCK_SHARED
)) {
2821 if (xfs_ipincount(ip
) || !xfs_iflock_nowait(ip
)) {
2822 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
2829 error
= xfs_iflush(ip
, (flags
& FLUSH_SYNC
) ? XFS_IFLUSH_SYNC
2830 : XFS_IFLUSH_ASYNC_NOBLOCK
);
2831 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
2843 xfs_mount_t
*mp
= ip
->i_mount
;
2847 if (!capable(CAP_SYS_ADMIN
))
2848 return XFS_ERROR(EPERM
);
2850 if (XFS_FORCED_SHUTDOWN(mp
))
2851 return XFS_ERROR(EIO
);
2853 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SET_DMATTRS
);
2854 error
= xfs_trans_reserve(tp
, 0, XFS_ICHANGE_LOG_RES (mp
), 0, 0, 0);
2856 xfs_trans_cancel(tp
, 0);
2859 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2860 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
2862 ip
->i_d
.di_dmevmask
= evmask
;
2863 ip
->i_d
.di_dmstate
= state
;
2865 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2867 error
= xfs_trans_commit(tp
, 0);
2876 bhv_vnode_t
*vp
= XFS_ITOV(ip
);
2878 xfs_itrace_entry(ip
);
2880 ASSERT(!VN_MAPPED(vp
));
2882 /* bad inode, get out here ASAP */
2890 ASSERT(XFS_FORCED_SHUTDOWN(ip
->i_mount
) || ip
->i_delayed_blks
== 0);
2893 * Make sure the atime in the XFS inode is correct before freeing the
2896 xfs_synchronize_atime(ip
);
2899 * If we have nothing to flush with this inode then complete the
2900 * teardown now, otherwise break the link between the xfs inode and the
2901 * linux inode and clean up the xfs inode later. This avoids flushing
2902 * the inode to disk during the delete operation itself.
2904 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2905 * first to ensure that xfs_iunpin() will never see an xfs inode
2906 * that has a linux inode being reclaimed. Synchronisation is provided
2907 * by the i_flags_lock.
2909 if (!ip
->i_update_core
&& (ip
->i_itemp
== NULL
)) {
2910 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2912 return xfs_finish_reclaim(ip
, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC
);
2914 xfs_mount_t
*mp
= ip
->i_mount
;
2916 /* Protect sync and unpin from us */
2917 XFS_MOUNT_ILOCK(mp
);
2918 spin_lock(&ip
->i_flags_lock
);
2919 __xfs_iflags_set(ip
, XFS_IRECLAIMABLE
);
2920 vn_to_inode(vp
)->i_private
= NULL
;
2922 spin_unlock(&ip
->i_flags_lock
);
2923 list_add_tail(&ip
->i_reclaim
, &mp
->m_del_inodes
);
2924 XFS_MOUNT_IUNLOCK(mp
);
2935 xfs_perag_t
*pag
= xfs_get_perag(ip
->i_mount
, ip
->i_ino
);
2936 bhv_vnode_t
*vp
= XFS_ITOV_NULL(ip
);
2938 if (vp
&& VN_BAD(vp
))
2941 /* The hash lock here protects a thread in xfs_iget_core from
2942 * racing with us on linking the inode back with a vnode.
2943 * Once we have the XFS_IRECLAIM flag set it will not touch
2946 write_lock(&pag
->pag_ici_lock
);
2947 spin_lock(&ip
->i_flags_lock
);
2948 if (__xfs_iflags_test(ip
, XFS_IRECLAIM
) ||
2949 (!__xfs_iflags_test(ip
, XFS_IRECLAIMABLE
) && vp
== NULL
)) {
2950 spin_unlock(&ip
->i_flags_lock
);
2951 write_unlock(&pag
->pag_ici_lock
);
2954 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
2958 __xfs_iflags_set(ip
, XFS_IRECLAIM
);
2959 spin_unlock(&ip
->i_flags_lock
);
2960 write_unlock(&pag
->pag_ici_lock
);
2961 xfs_put_perag(ip
->i_mount
, pag
);
2964 * If the inode is still dirty, then flush it out. If the inode
2965 * is not in the AIL, then it will be OK to flush it delwri as
2966 * long as xfs_iflush() does not keep any references to the inode.
2967 * We leave that decision up to xfs_iflush() since it has the
2968 * knowledge of whether it's OK to simply do a delwri flush of
2969 * the inode or whether we need to wait until the inode is
2970 * pulled from the AIL.
2971 * We get the flush lock regardless, though, just to make sure
2972 * we don't free it while it is being flushed.
2975 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2980 * In the case of a forced shutdown we rely on xfs_iflush() to
2981 * wait for the inode to be unpinned before returning an error.
2983 if (xfs_iflush(ip
, sync_mode
) == 0) {
2984 /* synchronize with xfs_iflush_done */
2989 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
2997 xfs_finish_reclaim_all(xfs_mount_t
*mp
, int noblock
)
3000 xfs_inode_t
*ip
, *n
;
3005 XFS_MOUNT_ILOCK(mp
);
3006 list_for_each_entry_safe(ip
, n
, &mp
->m_del_inodes
, i_reclaim
) {
3008 if (xfs_ilock_nowait(ip
, XFS_ILOCK_EXCL
) == 0)
3010 if (xfs_ipincount(ip
) ||
3011 !xfs_iflock_nowait(ip
)) {
3012 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3016 XFS_MOUNT_IUNLOCK(mp
);
3017 if (xfs_finish_reclaim(ip
, noblock
,
3018 XFS_IFLUSH_DELWRI_ELSE_ASYNC
))
3027 XFS_MOUNT_IUNLOCK(mp
);
3032 * xfs_alloc_file_space()
3033 * This routine allocates disk space for the given file.
3035 * If alloc_type == 0, this request is for an ALLOCSP type
3036 * request which will change the file size. In this case, no
3037 * DMAPI event will be generated by the call. A TRUNCATE event
3038 * will be generated later by xfs_setattr.
3040 * If alloc_type != 0, this request is for a RESVSP type
3041 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
3042 * lower block boundary byte address is less than the file's
3051 xfs_alloc_file_space(
3058 xfs_mount_t
*mp
= ip
->i_mount
;
3060 xfs_filblks_t allocated_fsb
;
3061 xfs_filblks_t allocatesize_fsb
;
3062 xfs_extlen_t extsz
, temp
;
3063 xfs_fileoff_t startoffset_fsb
;
3064 xfs_fsblock_t firstfsb
;
3070 xfs_bmbt_irec_t imaps
[1], *imapp
;
3071 xfs_bmap_free_t free_list
;
3072 uint qblocks
, resblks
, resrtextents
;
3076 xfs_itrace_entry(ip
);
3078 if (XFS_FORCED_SHUTDOWN(mp
))
3079 return XFS_ERROR(EIO
);
3081 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
3085 return XFS_ERROR(EINVAL
);
3087 rt
= XFS_IS_REALTIME_INODE(ip
);
3088 extsz
= xfs_get_extsz_hint(ip
);
3093 bmapi_flag
= XFS_BMAPI_WRITE
| (alloc_type
? XFS_BMAPI_PREALLOC
: 0);
3094 startoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
3095 allocatesize_fsb
= XFS_B_TO_FSB(mp
, count
);
3097 /* Generate a DMAPI event if needed. */
3098 if (alloc_type
!= 0 && offset
< ip
->i_size
&&
3099 (attr_flags
& XFS_ATTR_DMI
) == 0 &&
3100 DM_EVENT_ENABLED(ip
, DM_EVENT_WRITE
)) {
3101 xfs_off_t end_dmi_offset
;
3103 end_dmi_offset
= offset
+len
;
3104 if (end_dmi_offset
> ip
->i_size
)
3105 end_dmi_offset
= ip
->i_size
;
3106 error
= XFS_SEND_DATA(mp
, DM_EVENT_WRITE
, ip
, offset
,
3107 end_dmi_offset
- offset
, 0, NULL
);
3113 * Allocate file space until done or until there is an error
3116 while (allocatesize_fsb
&& !error
) {
3120 * Determine space reservations for data/realtime.
3122 if (unlikely(extsz
)) {
3123 s
= startoffset_fsb
;
3126 e
= startoffset_fsb
+ allocatesize_fsb
;
3127 if ((temp
= do_mod(startoffset_fsb
, extsz
)))
3129 if ((temp
= do_mod(e
, extsz
)))
3133 e
= allocatesize_fsb
;
3137 resrtextents
= qblocks
= (uint
)(e
- s
);
3138 resrtextents
/= mp
->m_sb
.sb_rextsize
;
3139 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
3140 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
3143 resblks
= qblocks
= \
3144 XFS_DIOSTRAT_SPACE_RES(mp
, (uint
)(e
- s
));
3145 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
3149 * Allocate and setup the transaction.
3151 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
3152 error
= xfs_trans_reserve(tp
, resblks
,
3153 XFS_WRITE_LOG_RES(mp
), resrtextents
,
3154 XFS_TRANS_PERM_LOG_RES
,
3155 XFS_WRITE_LOG_COUNT
);
3157 * Check for running out of space
3161 * Free the transaction structure.
3163 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
3164 xfs_trans_cancel(tp
, 0);
3167 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3168 error
= XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, tp
, ip
,
3169 qblocks
, 0, quota_flag
);
3173 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3174 xfs_trans_ihold(tp
, ip
);
3177 * Issue the xfs_bmapi() call to allocate the blocks
3179 XFS_BMAP_INIT(&free_list
, &firstfsb
);
3180 error
= xfs_bmapi(tp
, ip
, startoffset_fsb
,
3181 allocatesize_fsb
, bmapi_flag
,
3182 &firstfsb
, 0, imapp
, &nimaps
,
3189 * Complete the transaction
3191 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
3196 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3197 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3202 allocated_fsb
= imapp
->br_blockcount
;
3205 error
= XFS_ERROR(ENOSPC
);
3209 startoffset_fsb
+= allocated_fsb
;
3210 allocatesize_fsb
-= allocated_fsb
;
3213 if (error
== ENOSPC
&& (attr_flags
& XFS_ATTR_DMI
) == 0 &&
3214 DM_EVENT_ENABLED(ip
, DM_EVENT_NOSPACE
)) {
3215 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_NOSPACE
,
3218 NULL
, NULL
, 0, 0, 0); /* Delay flag intentionally unused */
3220 goto retry
; /* Maybe DMAPI app. has made space */
3221 /* else fall through with error from XFS_SEND_DATA */
3226 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
3227 xfs_bmap_cancel(&free_list
);
3228 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp
, tp
, ip
, qblocks
, 0, quota_flag
);
3230 error1
: /* Just cancel transaction */
3231 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
3232 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3233 goto dmapi_enospc_check
;
3237 * Zero file bytes between startoff and endoff inclusive.
3238 * The iolock is held exclusive and no blocks are buffered.
3241 xfs_zero_remaining_bytes(
3246 xfs_bmbt_irec_t imap
;
3247 xfs_fileoff_t offset_fsb
;
3248 xfs_off_t lastoffset
;
3251 xfs_mount_t
*mp
= ip
->i_mount
;
3255 bp
= xfs_buf_get_noaddr(mp
->m_sb
.sb_blocksize
,
3256 XFS_IS_REALTIME_INODE(ip
) ?
3257 mp
->m_rtdev_targp
: mp
->m_ddev_targp
);
3259 for (offset
= startoff
; offset
<= endoff
; offset
= lastoffset
+ 1) {
3260 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
3262 error
= xfs_bmapi(NULL
, ip
, offset_fsb
, 1, 0,
3263 NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
3264 if (error
|| nimap
< 1)
3266 ASSERT(imap
.br_blockcount
>= 1);
3267 ASSERT(imap
.br_startoff
== offset_fsb
);
3268 lastoffset
= XFS_FSB_TO_B(mp
, imap
.br_startoff
+ 1) - 1;
3269 if (lastoffset
> endoff
)
3270 lastoffset
= endoff
;
3271 if (imap
.br_startblock
== HOLESTARTBLOCK
)
3273 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
3274 if (imap
.br_state
== XFS_EXT_UNWRITTEN
)
3277 XFS_BUF_UNWRITE(bp
);
3279 XFS_BUF_SET_ADDR(bp
, XFS_FSB_TO_DB(ip
, imap
.br_startblock
));
3281 error
= xfs_iowait(bp
);
3283 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3284 mp
, bp
, XFS_BUF_ADDR(bp
));
3287 memset(XFS_BUF_PTR(bp
) +
3288 (offset
- XFS_FSB_TO_B(mp
, imap
.br_startoff
)),
3289 0, lastoffset
- offset
+ 1);
3294 error
= xfs_iowait(bp
);
3296 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3297 mp
, bp
, XFS_BUF_ADDR(bp
));
3306 * xfs_free_file_space()
3307 * This routine frees disk space for the given file.
3309 * This routine is only called by xfs_change_file_space
3310 * for an UNRESVSP type call.
3318 xfs_free_file_space(
3327 xfs_off_t end_dmi_offset
;
3328 xfs_fileoff_t endoffset_fsb
;
3330 xfs_fsblock_t firstfsb
;
3331 xfs_bmap_free_t free_list
;
3332 xfs_bmbt_irec_t imap
;
3340 xfs_fileoff_t startoffset_fsb
;
3342 int need_iolock
= 1;
3347 xfs_itrace_entry(ip
);
3349 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
3353 if (len
<= 0) /* if nothing being freed */
3355 rt
= XFS_IS_REALTIME_INODE(ip
);
3356 startoffset_fsb
= XFS_B_TO_FSB(mp
, offset
);
3357 end_dmi_offset
= offset
+ len
;
3358 endoffset_fsb
= XFS_B_TO_FSBT(mp
, end_dmi_offset
);
3360 if (offset
< ip
->i_size
&& (attr_flags
& XFS_ATTR_DMI
) == 0 &&
3361 DM_EVENT_ENABLED(ip
, DM_EVENT_WRITE
)) {
3362 if (end_dmi_offset
> ip
->i_size
)
3363 end_dmi_offset
= ip
->i_size
;
3364 error
= XFS_SEND_DATA(mp
, DM_EVENT_WRITE
, ip
,
3365 offset
, end_dmi_offset
- offset
,
3366 AT_DELAY_FLAG(attr_flags
), NULL
);
3371 if (attr_flags
& XFS_ATTR_NOLOCK
)
3374 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
3375 vn_iowait(ip
); /* wait for the completion of any pending DIOs */
3378 rounding
= max_t(uint
, 1 << mp
->m_sb
.sb_blocklog
, PAGE_CACHE_SIZE
);
3379 ioffset
= offset
& ~(rounding
- 1);
3381 if (VN_CACHED(vp
) != 0) {
3382 xfs_inval_cached_trace(ip
, ioffset
, -1, ioffset
, -1);
3383 error
= xfs_flushinval_pages(ip
, ioffset
, -1, FI_REMAPF_LOCKED
);
3385 goto out_unlock_iolock
;
3389 * Need to zero the stuff we're not freeing, on disk.
3390 * If its a realtime file & can't use unwritten extents then we
3391 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3392 * will take care of it for us.
3394 if (rt
&& !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
3396 error
= xfs_bmapi(NULL
, ip
, startoffset_fsb
,
3397 1, 0, NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
3399 goto out_unlock_iolock
;
3400 ASSERT(nimap
== 0 || nimap
== 1);
3401 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
3404 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
3405 block
= imap
.br_startblock
;
3406 mod
= do_div(block
, mp
->m_sb
.sb_rextsize
);
3408 startoffset_fsb
+= mp
->m_sb
.sb_rextsize
- mod
;
3411 error
= xfs_bmapi(NULL
, ip
, endoffset_fsb
- 1,
3412 1, 0, NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
3414 goto out_unlock_iolock
;
3415 ASSERT(nimap
== 0 || nimap
== 1);
3416 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
3417 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
3419 if (mod
&& (mod
!= mp
->m_sb
.sb_rextsize
))
3420 endoffset_fsb
-= mod
;
3423 if ((done
= (endoffset_fsb
<= startoffset_fsb
)))
3425 * One contiguous piece to clear
3427 error
= xfs_zero_remaining_bytes(ip
, offset
, offset
+ len
- 1);
3430 * Some full blocks, possibly two pieces to clear
3432 if (offset
< XFS_FSB_TO_B(mp
, startoffset_fsb
))
3433 error
= xfs_zero_remaining_bytes(ip
, offset
,
3434 XFS_FSB_TO_B(mp
, startoffset_fsb
) - 1);
3436 XFS_FSB_TO_B(mp
, endoffset_fsb
) < offset
+ len
)
3437 error
= xfs_zero_remaining_bytes(ip
,
3438 XFS_FSB_TO_B(mp
, endoffset_fsb
),
3443 * free file space until done or until there is an error
3445 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
3446 while (!error
&& !done
) {
3449 * allocate and setup the transaction. Allow this
3450 * transaction to dip into the reserve blocks to ensure
3451 * the freeing of the space succeeds at ENOSPC.
3453 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
3454 tp
->t_flags
|= XFS_TRANS_RESERVE
;
3455 error
= xfs_trans_reserve(tp
,
3457 XFS_WRITE_LOG_RES(mp
),
3459 XFS_TRANS_PERM_LOG_RES
,
3460 XFS_WRITE_LOG_COUNT
);
3463 * check for running out of space
3467 * Free the transaction structure.
3469 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
3470 xfs_trans_cancel(tp
, 0);
3473 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3474 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
,
3475 ip
->i_udquot
, ip
->i_gdquot
, resblks
, 0,
3476 XFS_QMOPT_RES_REGBLKS
);
3480 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3481 xfs_trans_ihold(tp
, ip
);
3484 * issue the bunmapi() call to free the blocks
3486 XFS_BMAP_INIT(&free_list
, &firstfsb
);
3487 error
= xfs_bunmapi(tp
, ip
, startoffset_fsb
,
3488 endoffset_fsb
- startoffset_fsb
,
3489 0, 2, &firstfsb
, &free_list
, NULL
, &done
);
3495 * complete the transaction
3497 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
3502 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3503 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3508 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
3512 xfs_bmap_cancel(&free_list
);
3514 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
3515 xfs_iunlock(ip
, need_iolock
? (XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
) :
3521 * xfs_change_file_space()
3522 * This routine allocates or frees disk space for the given file.
3523 * The user specified parameters are checked for alignment and size
3532 xfs_change_file_space(
3540 xfs_mount_t
*mp
= ip
->i_mount
;
3545 xfs_off_t startoffset
;
3550 xfs_itrace_entry(ip
);
3552 if (!S_ISREG(ip
->i_d
.di_mode
))
3553 return XFS_ERROR(EINVAL
);
3555 switch (bf
->l_whence
) {
3556 case 0: /*SEEK_SET*/
3558 case 1: /*SEEK_CUR*/
3559 bf
->l_start
+= offset
;
3561 case 2: /*SEEK_END*/
3562 bf
->l_start
+= ip
->i_size
;
3565 return XFS_ERROR(EINVAL
);
3568 llen
= bf
->l_len
> 0 ? bf
->l_len
- 1 : bf
->l_len
;
3570 if ( (bf
->l_start
< 0)
3571 || (bf
->l_start
> XFS_MAXIOFFSET(mp
))
3572 || (bf
->l_start
+ llen
< 0)
3573 || (bf
->l_start
+ llen
> XFS_MAXIOFFSET(mp
)))
3574 return XFS_ERROR(EINVAL
);
3578 startoffset
= bf
->l_start
;
3582 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3584 * These calls do NOT zero the data space allocated to the file,
3585 * nor do they change the file size.
3587 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3589 * These calls cause the new file data to be zeroed and the file
3590 * size to be changed.
3592 setprealloc
= clrprealloc
= 0;
3595 case XFS_IOC_RESVSP
:
3596 case XFS_IOC_RESVSP64
:
3597 error
= xfs_alloc_file_space(ip
, startoffset
, bf
->l_len
,
3604 case XFS_IOC_UNRESVSP
:
3605 case XFS_IOC_UNRESVSP64
:
3606 if ((error
= xfs_free_file_space(ip
, startoffset
, bf
->l_len
,
3611 case XFS_IOC_ALLOCSP
:
3612 case XFS_IOC_ALLOCSP64
:
3613 case XFS_IOC_FREESP
:
3614 case XFS_IOC_FREESP64
:
3615 if (startoffset
> fsize
) {
3616 error
= xfs_alloc_file_space(ip
, fsize
,
3617 startoffset
- fsize
, 0, attr_flags
);
3622 iattr
.ia_valid
= ATTR_SIZE
;
3623 iattr
.ia_size
= startoffset
;
3625 error
= xfs_setattr(ip
, &iattr
, attr_flags
, credp
);
3635 return XFS_ERROR(EINVAL
);
3639 * update the inode timestamp, mode, and prealloc flag bits
3641 tp
= xfs_trans_alloc(mp
, XFS_TRANS_WRITEID
);
3643 if ((error
= xfs_trans_reserve(tp
, 0, XFS_WRITEID_LOG_RES(mp
),
3646 xfs_trans_cancel(tp
, 0);
3650 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3652 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3653 xfs_trans_ihold(tp
, ip
);
3655 if ((attr_flags
& XFS_ATTR_DMI
) == 0) {
3656 ip
->i_d
.di_mode
&= ~S_ISUID
;
3659 * Note that we don't have to worry about mandatory
3660 * file locking being disabled here because we only
3661 * clear the S_ISGID bit if the Group execute bit is
3662 * on, but if it was on then mandatory locking wouldn't
3663 * have been enabled.
3665 if (ip
->i_d
.di_mode
& S_IXGRP
)
3666 ip
->i_d
.di_mode
&= ~S_ISGID
;
3668 xfs_ichgtime(ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
3671 ip
->i_d
.di_flags
|= XFS_DIFLAG_PREALLOC
;
3672 else if (clrprealloc
)
3673 ip
->i_d
.di_flags
&= ~XFS_DIFLAG_PREALLOC
;
3675 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
3676 xfs_trans_set_sync(tp
);
3678 error
= xfs_trans_commit(tp
, 0);
3680 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);