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_mount.h"
30 #include "xfs_da_btree.h"
31 #include "xfs_bmap_btree.h"
32 #include "xfs_ialloc_btree.h"
33 #include "xfs_dinode.h"
34 #include "xfs_inode.h"
35 #include "xfs_inode_item.h"
36 #include "xfs_itable.h"
37 #include "xfs_ialloc.h"
38 #include "xfs_alloc.h"
43 #include "xfs_error.h"
44 #include "xfs_quota.h"
45 #include "xfs_utils.h"
46 #include "xfs_rtalloc.h"
47 #include "xfs_trans_space.h"
48 #include "xfs_log_priv.h"
49 #include "xfs_filestream.h"
50 #include "xfs_vnodeops.h"
51 #include "xfs_trace.h"
59 xfs_mount_t
*mp
= ip
->i_mount
;
60 struct inode
*inode
= VFS_I(ip
);
61 int mask
= iattr
->ia_valid
;
68 struct xfs_dquot
*udqp
, *gdqp
, *olddquot1
, *olddquot2
;
71 trace_xfs_setattr(ip
);
73 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
74 return XFS_ERROR(EROFS
);
76 if (XFS_FORCED_SHUTDOWN(mp
))
77 return XFS_ERROR(EIO
);
79 code
= -inode_change_ok(inode
, iattr
);
83 olddquot1
= olddquot2
= NULL
;
87 * If disk quotas is on, we make sure that the dquots do exist on disk,
88 * before we start any other transactions. Trying to do this later
89 * is messy. We don't care to take a readlock to look at the ids
90 * in inode here, because we can't hold it across the trans_reserve.
91 * If the IDs do change before we take the ilock, we're covered
92 * because the i_*dquot fields will get updated anyway.
94 if (XFS_IS_QUOTA_ON(mp
) && (mask
& (ATTR_UID
|ATTR_GID
))) {
97 if ((mask
& ATTR_UID
) && XFS_IS_UQUOTA_ON(mp
)) {
99 qflags
|= XFS_QMOPT_UQUOTA
;
101 uid
= ip
->i_d
.di_uid
;
103 if ((mask
& ATTR_GID
) && XFS_IS_GQUOTA_ON(mp
)) {
105 qflags
|= XFS_QMOPT_GQUOTA
;
107 gid
= ip
->i_d
.di_gid
;
111 * We take a reference when we initialize udqp and gdqp,
112 * so it is important that we never blindly double trip on
113 * the same variable. See xfs_create() for an example.
115 ASSERT(udqp
== NULL
);
116 ASSERT(gdqp
== NULL
);
117 code
= xfs_qm_vop_dqalloc(ip
, uid
, gid
, xfs_get_projid(ip
),
118 qflags
, &udqp
, &gdqp
);
124 * For the other attributes, we acquire the inode lock and
125 * first do an error checking pass.
128 lock_flags
= XFS_ILOCK_EXCL
;
129 if (flags
& XFS_ATTR_NOLOCK
)
131 if (!(mask
& ATTR_SIZE
)) {
132 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SETATTR_NOT_SIZE
);
134 code
= xfs_trans_reserve(tp
, 0, XFS_ICHANGE_LOG_RES(mp
),
142 lock_flags
|= XFS_IOLOCK_EXCL
;
145 xfs_ilock(ip
, lock_flags
);
148 * Change file ownership. Must be the owner or privileged.
150 if (mask
& (ATTR_UID
|ATTR_GID
)) {
152 * These IDs could have changed since we last looked at them.
153 * But, we're assured that if the ownership did change
154 * while we didn't have the inode locked, inode's dquot(s)
155 * would have changed also.
157 iuid
= ip
->i_d
.di_uid
;
158 igid
= ip
->i_d
.di_gid
;
159 gid
= (mask
& ATTR_GID
) ? iattr
->ia_gid
: igid
;
160 uid
= (mask
& ATTR_UID
) ? iattr
->ia_uid
: iuid
;
163 * Do a quota reservation only if uid/gid is actually
166 if (XFS_IS_QUOTA_RUNNING(mp
) &&
167 ((XFS_IS_UQUOTA_ON(mp
) && iuid
!= uid
) ||
168 (XFS_IS_GQUOTA_ON(mp
) && igid
!= gid
))) {
170 code
= xfs_qm_vop_chown_reserve(tp
, ip
, udqp
, gdqp
,
171 capable(CAP_FOWNER
) ?
172 XFS_QMOPT_FORCE_RES
: 0);
173 if (code
) /* out of quota */
179 * Truncate file. Must have write permission and not be a directory.
181 if (mask
& ATTR_SIZE
) {
182 /* Short circuit the truncate case for zero length files */
183 if (iattr
->ia_size
== 0 &&
184 ip
->i_size
== 0 && ip
->i_d
.di_nextents
== 0) {
185 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
186 lock_flags
&= ~XFS_ILOCK_EXCL
;
187 if (mask
& ATTR_CTIME
) {
188 inode
->i_mtime
= inode
->i_ctime
=
189 current_fs_time(inode
->i_sb
);
190 xfs_mark_inode_dirty_sync(ip
);
196 if (S_ISDIR(ip
->i_d
.di_mode
)) {
197 code
= XFS_ERROR(EISDIR
);
199 } else if (!S_ISREG(ip
->i_d
.di_mode
)) {
200 code
= XFS_ERROR(EINVAL
);
205 * Make sure that the dquots are attached to the inode.
207 code
= xfs_qm_dqattach_locked(ip
, 0);
212 * Now we can make the changes. Before we join the inode
213 * to the transaction, if ATTR_SIZE is set then take care of
214 * the part of the truncation that must be done without the
215 * inode lock. This needs to be done before joining the inode
216 * to the transaction, because the inode cannot be unlocked
217 * once it is a part of the transaction.
219 if (iattr
->ia_size
> ip
->i_size
) {
221 * Do the first part of growing a file: zero any data
222 * in the last block that is beyond the old EOF. We
223 * need to do this before the inode is joined to the
224 * transaction to modify the i_size.
226 code
= xfs_zero_eof(ip
, iattr
->ia_size
, ip
->i_size
);
230 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
231 lock_flags
&= ~XFS_ILOCK_EXCL
;
234 * We are going to log the inode size change in this
235 * transaction so any previous writes that are beyond the on
236 * disk EOF and the new EOF that have not been written out need
237 * to be written here. If we do not write the data out, we
238 * expose ourselves to the null files problem.
240 * Only flush from the on disk size to the smaller of the in
241 * memory file size or the new size as that's the range we
242 * really care about here and prevents waiting for other data
243 * not within the range we care about here.
245 if (ip
->i_size
!= ip
->i_d
.di_size
&&
246 iattr
->ia_size
> ip
->i_d
.di_size
) {
247 code
= xfs_flush_pages(ip
,
248 ip
->i_d
.di_size
, iattr
->ia_size
,
254 /* wait for all I/O to complete */
257 code
= -block_truncate_page(inode
->i_mapping
, iattr
->ia_size
,
262 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SETATTR_SIZE
);
263 code
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
264 XFS_TRANS_PERM_LOG_RES
,
265 XFS_ITRUNCATE_LOG_COUNT
);
269 truncate_setsize(inode
, iattr
->ia_size
);
271 commit_flags
= XFS_TRANS_RELEASE_LOG_RES
;
272 lock_flags
|= XFS_ILOCK_EXCL
;
274 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
276 xfs_trans_ijoin(tp
, ip
);
279 * Only change the c/mtime if we are changing the size
280 * or we are explicitly asked to change it. This handles
281 * the semantic difference between truncate() and ftruncate()
282 * as implemented in the VFS.
284 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME
285 * is a special case where we need to update the times despite
286 * not having these flags set. For all other operations the
287 * VFS set these flags explicitly if it wants a timestamp
290 if (iattr
->ia_size
!= ip
->i_size
&&
291 (!(mask
& (ATTR_CTIME
| ATTR_MTIME
)))) {
292 iattr
->ia_ctime
= iattr
->ia_mtime
=
293 current_fs_time(inode
->i_sb
);
294 mask
|= ATTR_CTIME
| ATTR_MTIME
;
297 if (iattr
->ia_size
> ip
->i_size
) {
298 ip
->i_d
.di_size
= iattr
->ia_size
;
299 ip
->i_size
= iattr
->ia_size
;
300 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
301 } else if (iattr
->ia_size
<= ip
->i_size
||
302 (iattr
->ia_size
== 0 && ip
->i_d
.di_nextents
)) {
304 * signal a sync transaction unless
305 * we're truncating an already unlinked
306 * file on a wsync filesystem
308 code
= xfs_itruncate_finish(&tp
, ip
, iattr
->ia_size
,
310 ((ip
->i_d
.di_nlink
!= 0 ||
311 !(mp
->m_flags
& XFS_MOUNT_WSYNC
))
316 * Truncated "down", so we're removing references
317 * to old data here - if we now delay flushing for
318 * a long time, we expose ourselves unduly to the
319 * notorious NULL files problem. So, we mark this
320 * vnode and flush it when the file is closed, and
321 * do not wait the usual (long) time for writeout.
323 xfs_iflags_set(ip
, XFS_ITRUNCATED
);
326 xfs_trans_ijoin(tp
, ip
);
330 * Change file ownership. Must be the owner or privileged.
332 if (mask
& (ATTR_UID
|ATTR_GID
)) {
334 * CAP_FSETID overrides the following restrictions:
336 * The set-user-ID and set-group-ID bits of a file will be
337 * cleared upon successful return from chown()
339 if ((ip
->i_d
.di_mode
& (S_ISUID
|S_ISGID
)) &&
340 !capable(CAP_FSETID
)) {
341 ip
->i_d
.di_mode
&= ~(S_ISUID
|S_ISGID
);
345 * Change the ownerships and register quota modifications
346 * in the transaction.
349 if (XFS_IS_QUOTA_RUNNING(mp
) && XFS_IS_UQUOTA_ON(mp
)) {
350 ASSERT(mask
& ATTR_UID
);
352 olddquot1
= xfs_qm_vop_chown(tp
, ip
,
353 &ip
->i_udquot
, udqp
);
355 ip
->i_d
.di_uid
= uid
;
359 if (XFS_IS_QUOTA_RUNNING(mp
) && XFS_IS_GQUOTA_ON(mp
)) {
360 ASSERT(!XFS_IS_PQUOTA_ON(mp
));
361 ASSERT(mask
& ATTR_GID
);
363 olddquot2
= xfs_qm_vop_chown(tp
, ip
,
364 &ip
->i_gdquot
, gdqp
);
366 ip
->i_d
.di_gid
= gid
;
372 * Change file access modes.
374 if (mask
& ATTR_MODE
) {
375 umode_t mode
= iattr
->ia_mode
;
377 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
380 ip
->i_d
.di_mode
&= S_IFMT
;
381 ip
->i_d
.di_mode
|= mode
& ~S_IFMT
;
383 inode
->i_mode
&= S_IFMT
;
384 inode
->i_mode
|= mode
& ~S_IFMT
;
388 * Change file access or modified times.
390 if (mask
& ATTR_ATIME
) {
391 inode
->i_atime
= iattr
->ia_atime
;
392 ip
->i_d
.di_atime
.t_sec
= iattr
->ia_atime
.tv_sec
;
393 ip
->i_d
.di_atime
.t_nsec
= iattr
->ia_atime
.tv_nsec
;
394 ip
->i_update_core
= 1;
396 if (mask
& ATTR_CTIME
) {
397 inode
->i_ctime
= iattr
->ia_ctime
;
398 ip
->i_d
.di_ctime
.t_sec
= iattr
->ia_ctime
.tv_sec
;
399 ip
->i_d
.di_ctime
.t_nsec
= iattr
->ia_ctime
.tv_nsec
;
400 ip
->i_update_core
= 1;
402 if (mask
& ATTR_MTIME
) {
403 inode
->i_mtime
= iattr
->ia_mtime
;
404 ip
->i_d
.di_mtime
.t_sec
= iattr
->ia_mtime
.tv_sec
;
405 ip
->i_d
.di_mtime
.t_nsec
= iattr
->ia_mtime
.tv_nsec
;
406 ip
->i_update_core
= 1;
410 * And finally, log the inode core if any attribute in it
413 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
|
414 ATTR_ATIME
|ATTR_CTIME
|ATTR_MTIME
))
415 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
417 XFS_STATS_INC(xs_ig_attrchg
);
420 * If this is a synchronous mount, make sure that the
421 * transaction goes to disk before returning to the user.
422 * This is slightly sub-optimal in that truncates require
423 * two sync transactions instead of one for wsync filesystems.
424 * One for the truncate and one for the timestamps since we
425 * don't want to change the timestamps unless we're sure the
426 * truncate worked. Truncates are less than 1% of the laddis
427 * mix so this probably isn't worth the trouble to optimize.
430 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
431 xfs_trans_set_sync(tp
);
433 code
= xfs_trans_commit(tp
, commit_flags
);
435 xfs_iunlock(ip
, lock_flags
);
438 * Release any dquot(s) the inode had kept before chown.
440 xfs_qm_dqrele(olddquot1
);
441 xfs_qm_dqrele(olddquot2
);
449 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
450 * update. We could avoid this with linked transactions
451 * and passing down the transaction pointer all the way
452 * to attr_set. No previous user of the generic
453 * Posix ACL code seems to care about this issue either.
455 if ((mask
& ATTR_MODE
) && !(flags
& XFS_ATTR_NOACL
)) {
456 code
= -xfs_acl_chmod(inode
);
458 return XFS_ERROR(code
);
464 commit_flags
|= XFS_TRANS_ABORT
;
469 xfs_trans_cancel(tp
, commit_flags
);
471 if (lock_flags
!= 0) {
472 xfs_iunlock(ip
, lock_flags
);
478 * The maximum pathlen is 1024 bytes. Since the minimum file system
479 * blocksize is 512 bytes, we can get a max of 2 extents back from
482 #define SYMLINK_MAPS 2
489 xfs_mount_t
*mp
= ip
->i_mount
;
490 int pathlen
= ip
->i_d
.di_size
;
491 int nmaps
= SYMLINK_MAPS
;
492 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
499 error
= xfs_bmapi(NULL
, ip
, 0, XFS_B_TO_FSB(mp
, pathlen
), 0, NULL
, 0,
504 for (n
= 0; n
< nmaps
; n
++) {
505 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
506 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
508 bp
= xfs_buf_read(mp
->m_ddev_targp
, d
, BTOBB(byte_cnt
),
509 XBF_LOCK
| XBF_MAPPED
| XBF_DONT_BLOCK
);
510 error
= XFS_BUF_GETERROR(bp
);
512 xfs_ioerror_alert("xfs_readlink",
513 ip
->i_mount
, bp
, XFS_BUF_ADDR(bp
));
517 if (pathlen
< byte_cnt
)
521 memcpy(link
, XFS_BUF_PTR(bp
), byte_cnt
);
525 link
[ip
->i_d
.di_size
] = '\0';
537 xfs_mount_t
*mp
= ip
->i_mount
;
541 trace_xfs_readlink(ip
);
543 if (XFS_FORCED_SHUTDOWN(mp
))
544 return XFS_ERROR(EIO
);
546 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
548 ASSERT((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
);
549 ASSERT(ip
->i_d
.di_size
<= MAXPATHLEN
);
551 pathlen
= ip
->i_d
.di_size
;
555 if (ip
->i_df
.if_flags
& XFS_IFINLINE
) {
556 memcpy(link
, ip
->i_df
.if_u1
.if_data
, pathlen
);
557 link
[pathlen
] = '\0';
559 error
= xfs_readlink_bmap(ip
, link
);
563 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
568 * Flags for xfs_free_eofblocks
570 #define XFS_FREE_EOF_TRYLOCK (1<<0)
573 * This is called by xfs_inactive to free any blocks beyond eof
574 * when the link count isn't zero and by xfs_dm_punch_hole() when
575 * punching a hole to EOF.
585 xfs_fileoff_t end_fsb
;
586 xfs_fileoff_t last_fsb
;
587 xfs_filblks_t map_len
;
589 xfs_bmbt_irec_t imap
;
592 * Figure out if there are any blocks beyond the end
593 * of the file. If not, then there is nothing to do.
595 end_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)ip
->i_size
));
596 last_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_MAXIOFFSET(mp
));
597 if (last_fsb
<= end_fsb
)
599 map_len
= last_fsb
- end_fsb
;
602 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
603 error
= xfs_bmapi(NULL
, ip
, end_fsb
, map_len
, 0,
604 NULL
, 0, &imap
, &nimaps
, NULL
);
605 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
607 if (!error
&& (nimaps
!= 0) &&
608 (imap
.br_startblock
!= HOLESTARTBLOCK
||
609 ip
->i_delayed_blks
)) {
611 * Attach the dquots to the inode up front.
613 error
= xfs_qm_dqattach(ip
, 0);
618 * There are blocks after the end of file.
619 * Free them up now by truncating the file to
622 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
625 * Do the xfs_itruncate_start() call before
626 * reserving any log space because
627 * itruncate_start will call into the buffer
629 * do that within a transaction.
631 if (flags
& XFS_FREE_EOF_TRYLOCK
) {
632 if (!xfs_ilock_nowait(ip
, XFS_IOLOCK_EXCL
)) {
633 xfs_trans_cancel(tp
, 0);
637 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
639 error
= xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
,
642 xfs_trans_cancel(tp
, 0);
643 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
647 error
= xfs_trans_reserve(tp
, 0,
648 XFS_ITRUNCATE_LOG_RES(mp
),
649 0, XFS_TRANS_PERM_LOG_RES
,
650 XFS_ITRUNCATE_LOG_COUNT
);
652 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
653 xfs_trans_cancel(tp
, 0);
654 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
658 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
659 xfs_trans_ijoin(tp
, ip
);
661 error
= xfs_itruncate_finish(&tp
, ip
,
666 * If we get an error at this point we
667 * simply don't bother truncating the file.
671 (XFS_TRANS_RELEASE_LOG_RES
|
674 error
= xfs_trans_commit(tp
,
675 XFS_TRANS_RELEASE_LOG_RES
);
677 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
|XFS_ILOCK_EXCL
);
683 * Free a symlink that has blocks associated with it.
686 xfs_inactive_symlink_rmt(
694 xfs_fsblock_t first_block
;
695 xfs_bmap_free_t free_list
;
698 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
706 ASSERT(ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
));
708 * We're freeing a symlink that has some
709 * blocks allocated to it. Free the
710 * blocks here. We know that we've got
711 * either 1 or 2 extents and that we can
712 * free them all in one bunmapi call.
714 ASSERT(ip
->i_d
.di_nextents
> 0 && ip
->i_d
.di_nextents
<= 2);
715 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
716 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
717 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
718 xfs_trans_cancel(tp
, 0);
723 * Lock the inode, fix the size, and join it to the transaction.
724 * Hold it so in the normal path, we still have it locked for
725 * the second transaction. In the error paths we need it
726 * held so the cancel won't rele it, see below.
728 xfs_ilock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
729 size
= (int)ip
->i_d
.di_size
;
731 xfs_trans_ijoin(tp
, ip
);
732 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
734 * Find the block(s) so we can inval and unmap them.
737 xfs_bmap_init(&free_list
, &first_block
);
738 nmaps
= ARRAY_SIZE(mval
);
739 if ((error
= xfs_bmapi(tp
, ip
, 0, XFS_B_TO_FSB(mp
, size
),
740 XFS_BMAPI_METADATA
, &first_block
, 0, mval
, &nmaps
,
744 * Invalidate the block(s).
746 for (i
= 0; i
< nmaps
; i
++) {
747 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
748 XFS_FSB_TO_DADDR(mp
, mval
[i
].br_startblock
),
749 XFS_FSB_TO_BB(mp
, mval
[i
].br_blockcount
), 0);
750 xfs_trans_binval(tp
, bp
);
753 * Unmap the dead block(s) to the free_list.
755 if ((error
= xfs_bunmapi(tp
, ip
, 0, size
, XFS_BMAPI_METADATA
, nmaps
,
756 &first_block
, &free_list
, &done
)))
760 * Commit the first transaction. This logs the EFI and the inode.
762 if ((error
= xfs_bmap_finish(&tp
, &free_list
, &committed
)))
765 * The transaction must have been committed, since there were
766 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
767 * The new tp has the extent freeing and EFDs.
771 * The first xact was committed, so add the inode to the new one.
772 * Mark it dirty so it will be logged and moved forward in the log as
773 * part of every commit.
775 xfs_trans_ijoin(tp
, ip
);
776 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
778 * Get a new, empty transaction to return to our caller.
780 ntp
= xfs_trans_dup(tp
);
782 * Commit the transaction containing extent freeing and EFDs.
783 * If we get an error on the commit here or on the reserve below,
784 * we need to unlock the inode since the new transaction doesn't
785 * have the inode attached.
787 error
= xfs_trans_commit(tp
, 0);
790 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
794 * transaction commit worked ok so we can drop the extra ticket
795 * reference that we gained in xfs_trans_dup()
797 xfs_log_ticket_put(tp
->t_ticket
);
800 * Remove the memory for extent descriptions (just bookkeeping).
802 if (ip
->i_df
.if_bytes
)
803 xfs_idata_realloc(ip
, -ip
->i_df
.if_bytes
, XFS_DATA_FORK
);
804 ASSERT(ip
->i_df
.if_bytes
== 0);
806 * Put an itruncate log reservation in the new transaction
809 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
810 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
811 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
815 * Return with the inode locked but not joined to the transaction.
821 xfs_bmap_cancel(&free_list
);
824 * Have to come here with the inode locked and either
825 * (held and in the transaction) or (not in the transaction).
826 * If the inode isn't held then cancel would iput it, but
827 * that's wrong since this is inactive and the vnode ref
828 * count is 0 already.
829 * Cancel won't do anything to the inode if held, but it still
830 * needs to be locked until the cancel is done, if it was
831 * joined to the transaction.
833 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
834 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
841 xfs_inactive_symlink_local(
847 ASSERT(ip
->i_d
.di_size
<= XFS_IFORK_DSIZE(ip
));
849 * We're freeing a symlink which fit into
850 * the inode. Just free the memory used
851 * to hold the old symlink.
853 error
= xfs_trans_reserve(*tpp
, 0,
854 XFS_ITRUNCATE_LOG_RES(ip
->i_mount
),
855 0, XFS_TRANS_PERM_LOG_RES
,
856 XFS_ITRUNCATE_LOG_COUNT
);
859 xfs_trans_cancel(*tpp
, 0);
863 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
866 * Zero length symlinks _can_ exist.
868 if (ip
->i_df
.if_bytes
> 0) {
869 xfs_idata_realloc(ip
,
870 -(ip
->i_df
.if_bytes
),
872 ASSERT(ip
->i_df
.if_bytes
== 0);
886 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
889 ASSERT(ip
->i_d
.di_forkoff
!= 0);
890 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
891 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
895 error
= xfs_attr_inactive(ip
);
899 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
900 error
= xfs_trans_reserve(tp
, 0,
901 XFS_IFREE_LOG_RES(mp
),
902 0, XFS_TRANS_PERM_LOG_RES
,
903 XFS_INACTIVE_LOG_COUNT
);
907 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
908 xfs_trans_ijoin(tp
, ip
);
909 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
911 ASSERT(ip
->i_d
.di_anextents
== 0);
917 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
918 xfs_trans_cancel(tp
, 0);
921 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
929 xfs_mount_t
*mp
= ip
->i_mount
;
932 if (!S_ISREG(ip
->i_d
.di_mode
) || (ip
->i_d
.di_mode
== 0))
935 /* If this is a read-only mount, don't do this (would generate I/O) */
936 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
939 if (!XFS_FORCED_SHUTDOWN(mp
)) {
943 * If we are using filestreams, and we have an unlinked
944 * file that we are processing the last close on, then nothing
945 * will be able to reopen and write to this file. Purge this
946 * inode from the filestreams cache so that it doesn't delay
947 * teardown of the inode.
949 if ((ip
->i_d
.di_nlink
== 0) && xfs_inode_is_filestream(ip
))
950 xfs_filestream_deassociate(ip
);
953 * If we previously truncated this file and removed old data
954 * in the process, we want to initiate "early" writeout on
955 * the last close. This is an attempt to combat the notorious
956 * NULL files problem which is particularly noticable from a
957 * truncate down, buffered (re-)write (delalloc), followed by
958 * a crash. What we are effectively doing here is
959 * significantly reducing the time window where we'd otherwise
960 * be exposed to that problem.
962 truncated
= xfs_iflags_test_and_clear(ip
, XFS_ITRUNCATED
);
963 if (truncated
&& VN_DIRTY(VFS_I(ip
)) && ip
->i_delayed_blks
> 0)
964 xfs_flush_pages(ip
, 0, -1, XBF_ASYNC
, FI_NONE
);
967 if (ip
->i_d
.di_nlink
== 0)
970 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
971 ((ip
->i_size
> 0) || (VN_CACHED(VFS_I(ip
)) > 0 ||
972 ip
->i_delayed_blks
> 0)) &&
973 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
)) &&
974 (!(ip
->i_d
.di_flags
& (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)))) {
977 * If we can't get the iolock just skip truncating the blocks
978 * past EOF because we could deadlock with the mmap_sem
979 * otherwise. We'll get another chance to drop them once the
980 * last reference to the inode is dropped, so we'll never leak
981 * blocks permanently.
983 * Further, check if the inode is being opened, written and
984 * closed frequently and we have delayed allocation blocks
985 * oustanding (e.g. streaming writes from the NFS server),
986 * truncating the blocks past EOF will cause fragmentation to
989 * In this case don't do the truncation, either, but we have to
990 * be careful how we detect this case. Blocks beyond EOF show
991 * up as i_delayed_blks even when the inode is clean, so we
992 * need to truncate them away first before checking for a dirty
993 * release. Hence on the first dirty close we will still remove
994 * the speculative allocation, but after that we will leave it
997 if (xfs_iflags_test(ip
, XFS_IDIRTY_RELEASE
))
1000 error
= xfs_free_eofblocks(mp
, ip
,
1001 XFS_FREE_EOF_TRYLOCK
);
1005 /* delalloc blocks after truncation means it really is dirty */
1006 if (ip
->i_delayed_blks
)
1007 xfs_iflags_set(ip
, XFS_IDIRTY_RELEASE
);
1015 * This is called when the vnode reference count for the vnode
1016 * goes to zero. If the file has been unlinked, then it must
1017 * now be truncated. Also, we clear all of the read-ahead state
1018 * kept for the inode here since the file is now closed.
1024 xfs_bmap_free_t free_list
;
1025 xfs_fsblock_t first_block
;
1033 * If the inode is already free, then there can be nothing
1036 if (ip
->i_d
.di_mode
== 0 || is_bad_inode(VFS_I(ip
))) {
1037 ASSERT(ip
->i_df
.if_real_bytes
== 0);
1038 ASSERT(ip
->i_df
.if_broot_bytes
== 0);
1039 return VN_INACTIVE_CACHE
;
1043 * Only do a truncate if it's a regular file with
1044 * some actual space in it. It's OK to look at the
1045 * inode's fields without the lock because we're the
1046 * only one with a reference to the inode.
1048 truncate
= ((ip
->i_d
.di_nlink
== 0) &&
1049 ((ip
->i_d
.di_size
!= 0) || (ip
->i_size
!= 0) ||
1050 (ip
->i_d
.di_nextents
> 0) || (ip
->i_delayed_blks
> 0)) &&
1051 ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
));
1057 /* If this is a read-only mount, don't do this (would generate I/O) */
1058 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
1061 if (ip
->i_d
.di_nlink
!= 0) {
1062 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
1063 ((ip
->i_size
> 0) || (VN_CACHED(VFS_I(ip
)) > 0 ||
1064 ip
->i_delayed_blks
> 0)) &&
1065 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
) &&
1066 (!(ip
->i_d
.di_flags
&
1067 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)) ||
1068 (ip
->i_delayed_blks
!= 0)))) {
1069 error
= xfs_free_eofblocks(mp
, ip
, 0);
1071 return VN_INACTIVE_CACHE
;
1076 ASSERT(ip
->i_d
.di_nlink
== 0);
1078 error
= xfs_qm_dqattach(ip
, 0);
1080 return VN_INACTIVE_CACHE
;
1082 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1085 * Do the xfs_itruncate_start() call before
1086 * reserving any log space because itruncate_start
1087 * will call into the buffer cache and we can't
1088 * do that within a transaction.
1090 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
1092 error
= xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
, 0);
1094 xfs_trans_cancel(tp
, 0);
1095 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1096 return VN_INACTIVE_CACHE
;
1099 error
= xfs_trans_reserve(tp
, 0,
1100 XFS_ITRUNCATE_LOG_RES(mp
),
1101 0, XFS_TRANS_PERM_LOG_RES
,
1102 XFS_ITRUNCATE_LOG_COUNT
);
1104 /* Don't call itruncate_cleanup */
1105 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1106 xfs_trans_cancel(tp
, 0);
1107 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1108 return VN_INACTIVE_CACHE
;
1111 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1112 xfs_trans_ijoin(tp
, ip
);
1115 * normally, we have to run xfs_itruncate_finish sync.
1116 * But if filesystem is wsync and we're in the inactive
1117 * path, then we know that nlink == 0, and that the
1118 * xaction that made nlink == 0 is permanently committed
1119 * since xfs_remove runs as a synchronous transaction.
1121 error
= xfs_itruncate_finish(&tp
, ip
, 0, XFS_DATA_FORK
,
1122 (!(mp
->m_flags
& XFS_MOUNT_WSYNC
) ? 1 : 0));
1125 xfs_trans_cancel(tp
,
1126 XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1127 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1128 return VN_INACTIVE_CACHE
;
1130 } else if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
) {
1133 * If we get an error while cleaning up a
1134 * symlink we bail out.
1136 error
= (ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
)) ?
1137 xfs_inactive_symlink_rmt(ip
, &tp
) :
1138 xfs_inactive_symlink_local(ip
, &tp
);
1142 return VN_INACTIVE_CACHE
;
1145 xfs_trans_ijoin(tp
, ip
);
1147 error
= xfs_trans_reserve(tp
, 0,
1148 XFS_IFREE_LOG_RES(mp
),
1149 0, XFS_TRANS_PERM_LOG_RES
,
1150 XFS_INACTIVE_LOG_COUNT
);
1152 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1153 xfs_trans_cancel(tp
, 0);
1154 return VN_INACTIVE_CACHE
;
1157 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1158 xfs_trans_ijoin(tp
, ip
);
1162 * If there are attributes associated with the file
1163 * then blow them away now. The code calls a routine
1164 * that recursively deconstructs the attribute fork.
1165 * We need to just commit the current transaction
1166 * because we can't use it for xfs_attr_inactive().
1168 if (ip
->i_d
.di_anextents
> 0) {
1169 error
= xfs_inactive_attrs(ip
, &tp
);
1171 * If we got an error, the transaction is already
1172 * cancelled, and the inode is unlocked. Just get out.
1175 return VN_INACTIVE_CACHE
;
1176 } else if (ip
->i_afp
) {
1177 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
1183 xfs_bmap_init(&free_list
, &first_block
);
1184 error
= xfs_ifree(tp
, ip
, &free_list
);
1187 * If we fail to free the inode, shut down. The cancel
1188 * might do that, we need to make sure. Otherwise the
1189 * inode might be lost for a long time or forever.
1191 if (!XFS_FORCED_SHUTDOWN(mp
)) {
1192 xfs_notice(mp
, "%s: xfs_ifree returned error %d",
1194 xfs_force_shutdown(mp
, SHUTDOWN_META_IO_ERROR
);
1196 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
1199 * Credit the quota account(s). The inode is gone.
1201 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_ICOUNT
, -1);
1204 * Just ignore errors at this point. There is nothing we can
1205 * do except to try to keep going. Make sure it's not a silent
1208 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1210 xfs_notice(mp
, "%s: xfs_bmap_finish returned error %d",
1212 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1214 xfs_notice(mp
, "%s: xfs_trans_commit returned error %d",
1219 * Release the dquots held by inode, if any.
1221 xfs_qm_dqdetach(ip
);
1222 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1225 return VN_INACTIVE_CACHE
;
1229 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1230 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1231 * ci_name->name will point to a the actual name (caller must free) or
1232 * will be set to NULL if an exact match is found.
1237 struct xfs_name
*name
,
1239 struct xfs_name
*ci_name
)
1245 trace_xfs_lookup(dp
, name
);
1247 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
1248 return XFS_ERROR(EIO
);
1250 lock_mode
= xfs_ilock_map_shared(dp
);
1251 error
= xfs_dir_lookup(NULL
, dp
, name
, &inum
, ci_name
);
1252 xfs_iunlock_map_shared(dp
, lock_mode
);
1257 error
= xfs_iget(dp
->i_mount
, NULL
, inum
, 0, 0, ipp
);
1265 kmem_free(ci_name
->name
);
1274 struct xfs_name
*name
,
1279 int is_dir
= S_ISDIR(mode
);
1280 struct xfs_mount
*mp
= dp
->i_mount
;
1281 struct xfs_inode
*ip
= NULL
;
1282 struct xfs_trans
*tp
= NULL
;
1284 xfs_bmap_free_t free_list
;
1285 xfs_fsblock_t first_block
;
1286 boolean_t unlock_dp_on_error
= B_FALSE
;
1290 struct xfs_dquot
*udqp
= NULL
;
1291 struct xfs_dquot
*gdqp
= NULL
;
1296 trace_xfs_create(dp
, name
);
1298 if (XFS_FORCED_SHUTDOWN(mp
))
1299 return XFS_ERROR(EIO
);
1301 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
1302 prid
= xfs_get_projid(dp
);
1304 prid
= XFS_PROJID_DEFAULT
;
1307 * Make sure that we have allocated dquot(s) on disk.
1309 error
= xfs_qm_vop_dqalloc(dp
, current_fsuid(), current_fsgid(), prid
,
1310 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
1316 resblks
= XFS_MKDIR_SPACE_RES(mp
, name
->len
);
1317 log_res
= XFS_MKDIR_LOG_RES(mp
);
1318 log_count
= XFS_MKDIR_LOG_COUNT
;
1319 tp
= xfs_trans_alloc(mp
, XFS_TRANS_MKDIR
);
1321 resblks
= XFS_CREATE_SPACE_RES(mp
, name
->len
);
1322 log_res
= XFS_CREATE_LOG_RES(mp
);
1323 log_count
= XFS_CREATE_LOG_COUNT
;
1324 tp
= xfs_trans_alloc(mp
, XFS_TRANS_CREATE
);
1327 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1330 * Initially assume that the file does not exist and
1331 * reserve the resources for that case. If that is not
1332 * the case we'll drop the one we have and get a more
1333 * appropriate transaction later.
1335 error
= xfs_trans_reserve(tp
, resblks
, log_res
, 0,
1336 XFS_TRANS_PERM_LOG_RES
, log_count
);
1337 if (error
== ENOSPC
) {
1338 /* flush outstanding delalloc blocks and retry */
1339 xfs_flush_inodes(dp
);
1340 error
= xfs_trans_reserve(tp
, resblks
, log_res
, 0,
1341 XFS_TRANS_PERM_LOG_RES
, log_count
);
1343 if (error
== ENOSPC
) {
1344 /* No space at all so try a "no-allocation" reservation */
1346 error
= xfs_trans_reserve(tp
, 0, log_res
, 0,
1347 XFS_TRANS_PERM_LOG_RES
, log_count
);
1351 goto out_trans_cancel
;
1354 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
1355 unlock_dp_on_error
= B_TRUE
;
1358 * Check for directory link count overflow.
1360 if (is_dir
&& dp
->i_d
.di_nlink
>= XFS_MAXLINK
) {
1361 error
= XFS_ERROR(EMLINK
);
1362 goto out_trans_cancel
;
1365 xfs_bmap_init(&free_list
, &first_block
);
1368 * Reserve disk quota and the inode.
1370 error
= xfs_trans_reserve_quota(tp
, mp
, udqp
, gdqp
, resblks
, 1, 0);
1372 goto out_trans_cancel
;
1374 error
= xfs_dir_canenter(tp
, dp
, name
, resblks
);
1376 goto out_trans_cancel
;
1379 * A newly created regular or special file just has one directory
1380 * entry pointing to them, but a directory also the "." entry
1381 * pointing to itself.
1383 error
= xfs_dir_ialloc(&tp
, dp
, mode
, is_dir
? 2 : 1, rdev
,
1384 prid
, resblks
> 0, &ip
, &committed
);
1386 if (error
== ENOSPC
)
1387 goto out_trans_cancel
;
1388 goto out_trans_abort
;
1392 * Now we join the directory inode to the transaction. We do not do it
1393 * earlier because xfs_dir_ialloc might commit the previous transaction
1394 * (and release all the locks). An error from here on will result in
1395 * the transaction cancel unlocking dp so don't do it explicitly in the
1398 xfs_trans_ijoin_ref(tp
, dp
, XFS_ILOCK_EXCL
);
1399 unlock_dp_on_error
= B_FALSE
;
1401 error
= xfs_dir_createname(tp
, dp
, name
, ip
->i_ino
,
1402 &first_block
, &free_list
, resblks
?
1403 resblks
- XFS_IALLOC_SPACE_RES(mp
) : 0);
1405 ASSERT(error
!= ENOSPC
);
1406 goto out_trans_abort
;
1408 xfs_trans_ichgtime(tp
, dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1409 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1412 error
= xfs_dir_init(tp
, ip
, dp
);
1414 goto out_bmap_cancel
;
1416 error
= xfs_bumplink(tp
, dp
);
1418 goto out_bmap_cancel
;
1422 * If this is a synchronous mount, make sure that the
1423 * create transaction goes to disk before returning to
1426 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
))
1427 xfs_trans_set_sync(tp
);
1430 * Attach the dquot(s) to the inodes and modify them incore.
1431 * These ids of the inode couldn't have changed since the new
1432 * inode has been locked ever since it was created.
1434 xfs_qm_vop_create_dqattach(tp
, ip
, udqp
, gdqp
);
1436 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1438 goto out_bmap_cancel
;
1440 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1442 goto out_release_inode
;
1444 xfs_qm_dqrele(udqp
);
1445 xfs_qm_dqrele(gdqp
);
1451 xfs_bmap_cancel(&free_list
);
1453 cancel_flags
|= XFS_TRANS_ABORT
;
1455 xfs_trans_cancel(tp
, cancel_flags
);
1458 * Wait until after the current transaction is aborted to
1459 * release the inode. This prevents recursive transactions
1460 * and deadlocks from xfs_inactive.
1465 xfs_qm_dqrele(udqp
);
1466 xfs_qm_dqrele(gdqp
);
1468 if (unlock_dp_on_error
)
1469 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
1475 int xfs_small_retries
;
1476 int xfs_middle_retries
;
1477 int xfs_lots_retries
;
1478 int xfs_lock_delays
;
1482 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1486 xfs_lock_inumorder(int lock_mode
, int subclass
)
1488 if (lock_mode
& (XFS_IOLOCK_SHARED
|XFS_IOLOCK_EXCL
))
1489 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_IOLOCK_SHIFT
;
1490 if (lock_mode
& (XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
))
1491 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_ILOCK_SHIFT
;
1497 * The following routine will lock n inodes in exclusive mode.
1498 * We assume the caller calls us with the inodes in i_ino order.
1500 * We need to detect deadlock where an inode that we lock
1501 * is in the AIL and we start waiting for another inode that is locked
1502 * by a thread in a long running transaction (such as truncate). This can
1503 * result in deadlock since the long running trans might need to wait
1504 * for the inode we just locked in order to push the tail and free space
1513 int attempts
= 0, i
, j
, try_lock
;
1516 ASSERT(ips
&& (inodes
>= 2)); /* we need at least two */
1522 for (; i
< inodes
; i
++) {
1525 if (i
&& (ips
[i
] == ips
[i
-1])) /* Already locked */
1529 * If try_lock is not set yet, make sure all locked inodes
1530 * are not in the AIL.
1531 * If any are, set try_lock to be used later.
1535 for (j
= (i
- 1); j
>= 0 && !try_lock
; j
--) {
1536 lp
= (xfs_log_item_t
*)ips
[j
]->i_itemp
;
1537 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
1544 * If any of the previous locks we have locked is in the AIL,
1545 * we must TRY to get the second and subsequent locks. If
1546 * we can't get any, we must release all we have
1551 /* try_lock must be 0 if i is 0. */
1553 * try_lock means we have an inode locked
1554 * that is in the AIL.
1557 if (!xfs_ilock_nowait(ips
[i
], xfs_lock_inumorder(lock_mode
, i
))) {
1561 * Unlock all previous guys and try again.
1562 * xfs_iunlock will try to push the tail
1563 * if the inode is in the AIL.
1566 for(j
= i
- 1; j
>= 0; j
--) {
1569 * Check to see if we've already
1570 * unlocked this one.
1571 * Not the first one going back,
1572 * and the inode ptr is the same.
1574 if ((j
!= (i
- 1)) && ips
[j
] ==
1578 xfs_iunlock(ips
[j
], lock_mode
);
1581 if ((attempts
% 5) == 0) {
1582 delay(1); /* Don't just spin the CPU */
1592 xfs_ilock(ips
[i
], xfs_lock_inumorder(lock_mode
, i
));
1598 if (attempts
< 5) xfs_small_retries
++;
1599 else if (attempts
< 100) xfs_middle_retries
++;
1600 else xfs_lots_retries
++;
1608 * xfs_lock_two_inodes() can only be used to lock one type of lock
1609 * at a time - the iolock or the ilock, but not both at once. If
1610 * we lock both at once, lockdep will report false positives saying
1611 * we have violated locking orders.
1614 xfs_lock_two_inodes(
1623 if (lock_mode
& (XFS_IOLOCK_SHARED
|XFS_IOLOCK_EXCL
))
1624 ASSERT((lock_mode
& (XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
)) == 0);
1625 ASSERT(ip0
->i_ino
!= ip1
->i_ino
);
1627 if (ip0
->i_ino
> ip1
->i_ino
) {
1634 xfs_ilock(ip0
, xfs_lock_inumorder(lock_mode
, 0));
1637 * If the first lock we have locked is in the AIL, we must TRY to get
1638 * the second lock. If we can't get it, we must release the first one
1641 lp
= (xfs_log_item_t
*)ip0
->i_itemp
;
1642 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
1643 if (!xfs_ilock_nowait(ip1
, xfs_lock_inumorder(lock_mode
, 1))) {
1644 xfs_iunlock(ip0
, lock_mode
);
1645 if ((++attempts
% 5) == 0)
1646 delay(1); /* Don't just spin the CPU */
1650 xfs_ilock(ip1
, xfs_lock_inumorder(lock_mode
, 1));
1657 struct xfs_name
*name
,
1660 xfs_mount_t
*mp
= dp
->i_mount
;
1661 xfs_trans_t
*tp
= NULL
;
1662 int is_dir
= S_ISDIR(ip
->i_d
.di_mode
);
1664 xfs_bmap_free_t free_list
;
1665 xfs_fsblock_t first_block
;
1672 trace_xfs_remove(dp
, name
);
1674 if (XFS_FORCED_SHUTDOWN(mp
))
1675 return XFS_ERROR(EIO
);
1677 error
= xfs_qm_dqattach(dp
, 0);
1681 error
= xfs_qm_dqattach(ip
, 0);
1686 tp
= xfs_trans_alloc(mp
, XFS_TRANS_RMDIR
);
1687 log_count
= XFS_DEFAULT_LOG_COUNT
;
1689 tp
= xfs_trans_alloc(mp
, XFS_TRANS_REMOVE
);
1690 log_count
= XFS_REMOVE_LOG_COUNT
;
1692 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1695 * We try to get the real space reservation first,
1696 * allowing for directory btree deletion(s) implying
1697 * possible bmap insert(s). If we can't get the space
1698 * reservation then we use 0 instead, and avoid the bmap
1699 * btree insert(s) in the directory code by, if the bmap
1700 * insert tries to happen, instead trimming the LAST
1701 * block from the directory.
1703 resblks
= XFS_REMOVE_SPACE_RES(mp
);
1704 error
= xfs_trans_reserve(tp
, resblks
, XFS_REMOVE_LOG_RES(mp
), 0,
1705 XFS_TRANS_PERM_LOG_RES
, log_count
);
1706 if (error
== ENOSPC
) {
1708 error
= xfs_trans_reserve(tp
, 0, XFS_REMOVE_LOG_RES(mp
), 0,
1709 XFS_TRANS_PERM_LOG_RES
, log_count
);
1712 ASSERT(error
!= ENOSPC
);
1714 goto out_trans_cancel
;
1717 xfs_lock_two_inodes(dp
, ip
, XFS_ILOCK_EXCL
);
1719 xfs_trans_ijoin_ref(tp
, dp
, XFS_ILOCK_EXCL
);
1720 xfs_trans_ijoin_ref(tp
, ip
, XFS_ILOCK_EXCL
);
1723 * If we're removing a directory perform some additional validation.
1726 ASSERT(ip
->i_d
.di_nlink
>= 2);
1727 if (ip
->i_d
.di_nlink
!= 2) {
1728 error
= XFS_ERROR(ENOTEMPTY
);
1729 goto out_trans_cancel
;
1731 if (!xfs_dir_isempty(ip
)) {
1732 error
= XFS_ERROR(ENOTEMPTY
);
1733 goto out_trans_cancel
;
1737 xfs_bmap_init(&free_list
, &first_block
);
1738 error
= xfs_dir_removename(tp
, dp
, name
, ip
->i_ino
,
1739 &first_block
, &free_list
, resblks
);
1741 ASSERT(error
!= ENOENT
);
1742 goto out_bmap_cancel
;
1744 xfs_trans_ichgtime(tp
, dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1748 * Drop the link from ip's "..".
1750 error
= xfs_droplink(tp
, dp
);
1752 goto out_bmap_cancel
;
1755 * Drop the "." link from ip to self.
1757 error
= xfs_droplink(tp
, ip
);
1759 goto out_bmap_cancel
;
1762 * When removing a non-directory we need to log the parent
1763 * inode here. For a directory this is done implicitly
1764 * by the xfs_droplink call for the ".." entry.
1766 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1770 * Drop the link from dp to ip.
1772 error
= xfs_droplink(tp
, ip
);
1774 goto out_bmap_cancel
;
1777 * Determine if this is the last link while
1778 * we are in the transaction.
1780 link_zero
= (ip
->i_d
.di_nlink
== 0);
1783 * If this is a synchronous mount, make sure that the
1784 * remove transaction goes to disk before returning to
1787 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
))
1788 xfs_trans_set_sync(tp
);
1790 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1792 goto out_bmap_cancel
;
1794 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1799 * If we are using filestreams, kill the stream association.
1800 * If the file is still open it may get a new one but that
1801 * will get killed on last close in xfs_close() so we don't
1802 * have to worry about that.
1804 if (!is_dir
&& link_zero
&& xfs_inode_is_filestream(ip
))
1805 xfs_filestream_deassociate(ip
);
1810 xfs_bmap_cancel(&free_list
);
1811 cancel_flags
|= XFS_TRANS_ABORT
;
1813 xfs_trans_cancel(tp
, cancel_flags
);
1822 struct xfs_name
*target_name
)
1824 xfs_mount_t
*mp
= tdp
->i_mount
;
1827 xfs_bmap_free_t free_list
;
1828 xfs_fsblock_t first_block
;
1833 trace_xfs_link(tdp
, target_name
);
1835 ASSERT(!S_ISDIR(sip
->i_d
.di_mode
));
1837 if (XFS_FORCED_SHUTDOWN(mp
))
1838 return XFS_ERROR(EIO
);
1840 error
= xfs_qm_dqattach(sip
, 0);
1844 error
= xfs_qm_dqattach(tdp
, 0);
1848 tp
= xfs_trans_alloc(mp
, XFS_TRANS_LINK
);
1849 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1850 resblks
= XFS_LINK_SPACE_RES(mp
, target_name
->len
);
1851 error
= xfs_trans_reserve(tp
, resblks
, XFS_LINK_LOG_RES(mp
), 0,
1852 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
1853 if (error
== ENOSPC
) {
1855 error
= xfs_trans_reserve(tp
, 0, XFS_LINK_LOG_RES(mp
), 0,
1856 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
1863 xfs_lock_two_inodes(sip
, tdp
, XFS_ILOCK_EXCL
);
1865 xfs_trans_ijoin_ref(tp
, sip
, XFS_ILOCK_EXCL
);
1866 xfs_trans_ijoin_ref(tp
, tdp
, XFS_ILOCK_EXCL
);
1869 * If the source has too many links, we can't make any more to it.
1871 if (sip
->i_d
.di_nlink
>= XFS_MAXLINK
) {
1872 error
= XFS_ERROR(EMLINK
);
1877 * If we are using project inheritance, we only allow hard link
1878 * creation in our tree when the project IDs are the same; else
1879 * the tree quota mechanism could be circumvented.
1881 if (unlikely((tdp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
) &&
1882 (xfs_get_projid(tdp
) != xfs_get_projid(sip
)))) {
1883 error
= XFS_ERROR(EXDEV
);
1887 error
= xfs_dir_canenter(tp
, tdp
, target_name
, resblks
);
1891 xfs_bmap_init(&free_list
, &first_block
);
1893 error
= xfs_dir_createname(tp
, tdp
, target_name
, sip
->i_ino
,
1894 &first_block
, &free_list
, resblks
);
1897 xfs_trans_ichgtime(tp
, tdp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1898 xfs_trans_log_inode(tp
, tdp
, XFS_ILOG_CORE
);
1900 error
= xfs_bumplink(tp
, sip
);
1905 * If this is a synchronous mount, make sure that the
1906 * link transaction goes to disk before returning to
1909 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
1910 xfs_trans_set_sync(tp
);
1913 error
= xfs_bmap_finish (&tp
, &free_list
, &committed
);
1915 xfs_bmap_cancel(&free_list
);
1919 return xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1922 cancel_flags
|= XFS_TRANS_ABORT
;
1924 xfs_trans_cancel(tp
, cancel_flags
);
1932 struct xfs_name
*link_name
,
1933 const char *target_path
,
1937 xfs_mount_t
*mp
= dp
->i_mount
;
1942 xfs_bmap_free_t free_list
;
1943 xfs_fsblock_t first_block
;
1944 boolean_t unlock_dp_on_error
= B_FALSE
;
1947 xfs_fileoff_t first_fsb
;
1948 xfs_filblks_t fs_blocks
;
1950 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
1952 const char *cur_chunk
;
1957 struct xfs_dquot
*udqp
, *gdqp
;
1965 trace_xfs_symlink(dp
, link_name
);
1967 if (XFS_FORCED_SHUTDOWN(mp
))
1968 return XFS_ERROR(EIO
);
1971 * Check component lengths of the target path name.
1973 pathlen
= strlen(target_path
);
1974 if (pathlen
>= MAXPATHLEN
) /* total string too long */
1975 return XFS_ERROR(ENAMETOOLONG
);
1978 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
1979 prid
= xfs_get_projid(dp
);
1981 prid
= XFS_PROJID_DEFAULT
;
1984 * Make sure that we have allocated dquot(s) on disk.
1986 error
= xfs_qm_vop_dqalloc(dp
, current_fsuid(), current_fsgid(), prid
,
1987 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
1991 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SYMLINK
);
1992 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1994 * The symlink will fit into the inode data fork?
1995 * There can't be any attributes so we get the whole variable part.
1997 if (pathlen
<= XFS_LITINO(mp
))
2000 fs_blocks
= XFS_B_TO_FSB(mp
, pathlen
);
2001 resblks
= XFS_SYMLINK_SPACE_RES(mp
, link_name
->len
, fs_blocks
);
2002 error
= xfs_trans_reserve(tp
, resblks
, XFS_SYMLINK_LOG_RES(mp
), 0,
2003 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
2004 if (error
== ENOSPC
&& fs_blocks
== 0) {
2006 error
= xfs_trans_reserve(tp
, 0, XFS_SYMLINK_LOG_RES(mp
), 0,
2007 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
2014 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
2015 unlock_dp_on_error
= B_TRUE
;
2018 * Check whether the directory allows new symlinks or not.
2020 if (dp
->i_d
.di_flags
& XFS_DIFLAG_NOSYMLINKS
) {
2021 error
= XFS_ERROR(EPERM
);
2026 * Reserve disk quota : blocks and inode.
2028 error
= xfs_trans_reserve_quota(tp
, mp
, udqp
, gdqp
, resblks
, 1, 0);
2033 * Check for ability to enter directory entry, if no space reserved.
2035 error
= xfs_dir_canenter(tp
, dp
, link_name
, resblks
);
2039 * Initialize the bmap freelist prior to calling either
2040 * bmapi or the directory create code.
2042 xfs_bmap_init(&free_list
, &first_block
);
2045 * Allocate an inode for the symlink.
2047 error
= xfs_dir_ialloc(&tp
, dp
, S_IFLNK
| (mode
& ~S_IFMT
), 1, 0,
2048 prid
, resblks
> 0, &ip
, NULL
);
2050 if (error
== ENOSPC
)
2056 * An error after we've joined dp to the transaction will result in the
2057 * transaction cancel unlocking dp so don't do it explicitly in the
2060 xfs_trans_ijoin_ref(tp
, dp
, XFS_ILOCK_EXCL
);
2061 unlock_dp_on_error
= B_FALSE
;
2064 * Also attach the dquot(s) to it, if applicable.
2066 xfs_qm_vop_create_dqattach(tp
, ip
, udqp
, gdqp
);
2069 resblks
-= XFS_IALLOC_SPACE_RES(mp
);
2071 * If the symlink will fit into the inode, write it inline.
2073 if (pathlen
<= XFS_IFORK_DSIZE(ip
)) {
2074 xfs_idata_realloc(ip
, pathlen
, XFS_DATA_FORK
);
2075 memcpy(ip
->i_df
.if_u1
.if_data
, target_path
, pathlen
);
2076 ip
->i_d
.di_size
= pathlen
;
2079 * The inode was initially created in extent format.
2081 ip
->i_df
.if_flags
&= ~(XFS_IFEXTENTS
| XFS_IFBROOT
);
2082 ip
->i_df
.if_flags
|= XFS_IFINLINE
;
2084 ip
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
2085 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_DDATA
| XFS_ILOG_CORE
);
2089 nmaps
= SYMLINK_MAPS
;
2091 error
= xfs_bmapi(tp
, ip
, first_fsb
, fs_blocks
,
2092 XFS_BMAPI_WRITE
| XFS_BMAPI_METADATA
,
2093 &first_block
, resblks
, mval
, &nmaps
,
2099 resblks
-= fs_blocks
;
2100 ip
->i_d
.di_size
= pathlen
;
2101 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2103 cur_chunk
= target_path
;
2104 for (n
= 0; n
< nmaps
; n
++) {
2105 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
2106 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
2107 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
2108 BTOBB(byte_cnt
), 0);
2109 ASSERT(bp
&& !XFS_BUF_GETERROR(bp
));
2110 if (pathlen
< byte_cnt
) {
2113 pathlen
-= byte_cnt
;
2115 memcpy(XFS_BUF_PTR(bp
), cur_chunk
, byte_cnt
);
2116 cur_chunk
+= byte_cnt
;
2118 xfs_trans_log_buf(tp
, bp
, 0, byte_cnt
- 1);
2123 * Create the directory entry for the symlink.
2125 error
= xfs_dir_createname(tp
, dp
, link_name
, ip
->i_ino
,
2126 &first_block
, &free_list
, resblks
);
2129 xfs_trans_ichgtime(tp
, dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2130 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2133 * If this is a synchronous mount, make sure that the
2134 * symlink transaction goes to disk before returning to
2137 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2138 xfs_trans_set_sync(tp
);
2141 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2145 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2146 xfs_qm_dqrele(udqp
);
2147 xfs_qm_dqrele(gdqp
);
2155 xfs_bmap_cancel(&free_list
);
2156 cancel_flags
|= XFS_TRANS_ABORT
;
2158 xfs_trans_cancel(tp
, cancel_flags
);
2159 xfs_qm_dqrele(udqp
);
2160 xfs_qm_dqrele(gdqp
);
2162 if (unlock_dp_on_error
)
2163 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
2174 xfs_mount_t
*mp
= ip
->i_mount
;
2178 if (!capable(CAP_SYS_ADMIN
))
2179 return XFS_ERROR(EPERM
);
2181 if (XFS_FORCED_SHUTDOWN(mp
))
2182 return XFS_ERROR(EIO
);
2184 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SET_DMATTRS
);
2185 error
= xfs_trans_reserve(tp
, 0, XFS_ICHANGE_LOG_RES (mp
), 0, 0, 0);
2187 xfs_trans_cancel(tp
, 0);
2190 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2191 xfs_trans_ijoin_ref(tp
, ip
, XFS_ILOCK_EXCL
);
2193 ip
->i_d
.di_dmevmask
= evmask
;
2194 ip
->i_d
.di_dmstate
= state
;
2196 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2197 error
= xfs_trans_commit(tp
, 0);
2203 * xfs_alloc_file_space()
2204 * This routine allocates disk space for the given file.
2206 * If alloc_type == 0, this request is for an ALLOCSP type
2207 * request which will change the file size. In this case, no
2208 * DMAPI event will be generated by the call. A TRUNCATE event
2209 * will be generated later by xfs_setattr.
2211 * If alloc_type != 0, this request is for a RESVSP type
2212 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2213 * lower block boundary byte address is less than the file's
2222 xfs_alloc_file_space(
2229 xfs_mount_t
*mp
= ip
->i_mount
;
2231 xfs_filblks_t allocated_fsb
;
2232 xfs_filblks_t allocatesize_fsb
;
2233 xfs_extlen_t extsz
, temp
;
2234 xfs_fileoff_t startoffset_fsb
;
2235 xfs_fsblock_t firstfsb
;
2241 xfs_bmbt_irec_t imaps
[1], *imapp
;
2242 xfs_bmap_free_t free_list
;
2243 uint qblocks
, resblks
, resrtextents
;
2247 trace_xfs_alloc_file_space(ip
);
2249 if (XFS_FORCED_SHUTDOWN(mp
))
2250 return XFS_ERROR(EIO
);
2252 error
= xfs_qm_dqattach(ip
, 0);
2257 return XFS_ERROR(EINVAL
);
2259 rt
= XFS_IS_REALTIME_INODE(ip
);
2260 extsz
= xfs_get_extsz_hint(ip
);
2265 bmapi_flag
= XFS_BMAPI_WRITE
| alloc_type
;
2266 startoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
2267 allocatesize_fsb
= XFS_B_TO_FSB(mp
, count
);
2270 * Allocate file space until done or until there is an error
2272 while (allocatesize_fsb
&& !error
) {
2276 * Determine space reservations for data/realtime.
2278 if (unlikely(extsz
)) {
2279 s
= startoffset_fsb
;
2282 e
= startoffset_fsb
+ allocatesize_fsb
;
2283 if ((temp
= do_mod(startoffset_fsb
, extsz
)))
2285 if ((temp
= do_mod(e
, extsz
)))
2289 e
= allocatesize_fsb
;
2293 * The transaction reservation is limited to a 32-bit block
2294 * count, hence we need to limit the number of blocks we are
2295 * trying to reserve to avoid an overflow. We can't allocate
2296 * more than @nimaps extents, and an extent is limited on disk
2297 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
2299 resblks
= min_t(xfs_fileoff_t
, (e
- s
), (MAXEXTLEN
* nimaps
));
2301 resrtextents
= qblocks
= resblks
;
2302 resrtextents
/= mp
->m_sb
.sb_rextsize
;
2303 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
2304 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
2307 resblks
= qblocks
= XFS_DIOSTRAT_SPACE_RES(mp
, resblks
);
2308 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
2312 * Allocate and setup the transaction.
2314 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
2315 error
= xfs_trans_reserve(tp
, resblks
,
2316 XFS_WRITE_LOG_RES(mp
), resrtextents
,
2317 XFS_TRANS_PERM_LOG_RES
,
2318 XFS_WRITE_LOG_COUNT
);
2320 * Check for running out of space
2324 * Free the transaction structure.
2326 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
2327 xfs_trans_cancel(tp
, 0);
2330 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2331 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, qblocks
,
2336 xfs_trans_ijoin(tp
, ip
);
2339 * Issue the xfs_bmapi() call to allocate the blocks
2341 xfs_bmap_init(&free_list
, &firstfsb
);
2342 error
= xfs_bmapi(tp
, ip
, startoffset_fsb
,
2343 allocatesize_fsb
, bmapi_flag
,
2344 &firstfsb
, 0, imapp
, &nimaps
,
2351 * Complete the transaction
2353 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2358 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2359 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
2364 allocated_fsb
= imapp
->br_blockcount
;
2367 error
= XFS_ERROR(ENOSPC
);
2371 startoffset_fsb
+= allocated_fsb
;
2372 allocatesize_fsb
-= allocated_fsb
;
2377 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
2378 xfs_bmap_cancel(&free_list
);
2379 xfs_trans_unreserve_quota_nblks(tp
, ip
, qblocks
, 0, quota_flag
);
2381 error1
: /* Just cancel transaction */
2382 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
2383 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
2388 * Zero file bytes between startoff and endoff inclusive.
2389 * The iolock is held exclusive and no blocks are buffered.
2391 * This function is used by xfs_free_file_space() to zero
2392 * partial blocks when the range to free is not block aligned.
2393 * When unreserving space with boundaries that are not block
2394 * aligned we round up the start and round down the end
2395 * boundaries and then use this function to zero the parts of
2396 * the blocks that got dropped during the rounding.
2399 xfs_zero_remaining_bytes(
2404 xfs_bmbt_irec_t imap
;
2405 xfs_fileoff_t offset_fsb
;
2406 xfs_off_t lastoffset
;
2409 xfs_mount_t
*mp
= ip
->i_mount
;
2414 * Avoid doing I/O beyond eof - it's not necessary
2415 * since nothing can read beyond eof. The space will
2416 * be zeroed when the file is extended anyway.
2418 if (startoff
>= ip
->i_size
)
2421 if (endoff
> ip
->i_size
)
2422 endoff
= ip
->i_size
;
2424 bp
= xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip
) ?
2425 mp
->m_rtdev_targp
: mp
->m_ddev_targp
,
2426 mp
->m_sb
.sb_blocksize
, XBF_DONT_BLOCK
);
2428 return XFS_ERROR(ENOMEM
);
2430 for (offset
= startoff
; offset
<= endoff
; offset
= lastoffset
+ 1) {
2431 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
2433 error
= xfs_bmapi(NULL
, ip
, offset_fsb
, 1, 0,
2434 NULL
, 0, &imap
, &nimap
, NULL
);
2435 if (error
|| nimap
< 1)
2437 ASSERT(imap
.br_blockcount
>= 1);
2438 ASSERT(imap
.br_startoff
== offset_fsb
);
2439 lastoffset
= XFS_FSB_TO_B(mp
, imap
.br_startoff
+ 1) - 1;
2440 if (lastoffset
> endoff
)
2441 lastoffset
= endoff
;
2442 if (imap
.br_startblock
== HOLESTARTBLOCK
)
2444 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
2445 if (imap
.br_state
== XFS_EXT_UNWRITTEN
)
2448 XFS_BUF_UNWRITE(bp
);
2450 XFS_BUF_SET_ADDR(bp
, xfs_fsb_to_db(ip
, imap
.br_startblock
));
2452 error
= xfs_buf_iowait(bp
);
2454 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2455 mp
, bp
, XFS_BUF_ADDR(bp
));
2458 memset(XFS_BUF_PTR(bp
) +
2459 (offset
- XFS_FSB_TO_B(mp
, imap
.br_startoff
)),
2460 0, lastoffset
- offset
+ 1);
2465 error
= xfs_buf_iowait(bp
);
2467 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2468 mp
, bp
, XFS_BUF_ADDR(bp
));
2477 * xfs_free_file_space()
2478 * This routine frees disk space for the given file.
2480 * This routine is only called by xfs_change_file_space
2481 * for an UNRESVSP type call.
2489 xfs_free_file_space(
2497 xfs_fileoff_t endoffset_fsb
;
2499 xfs_fsblock_t firstfsb
;
2500 xfs_bmap_free_t free_list
;
2501 xfs_bmbt_irec_t imap
;
2509 xfs_fileoff_t startoffset_fsb
;
2511 int need_iolock
= 1;
2515 trace_xfs_free_file_space(ip
);
2517 error
= xfs_qm_dqattach(ip
, 0);
2522 if (len
<= 0) /* if nothing being freed */
2524 rt
= XFS_IS_REALTIME_INODE(ip
);
2525 startoffset_fsb
= XFS_B_TO_FSB(mp
, offset
);
2526 endoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
+ len
);
2528 if (attr_flags
& XFS_ATTR_NOLOCK
)
2531 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
2532 /* wait for the completion of any pending DIOs */
2536 rounding
= max_t(uint
, 1 << mp
->m_sb
.sb_blocklog
, PAGE_CACHE_SIZE
);
2537 ioffset
= offset
& ~(rounding
- 1);
2539 if (VN_CACHED(VFS_I(ip
)) != 0) {
2540 error
= xfs_flushinval_pages(ip
, ioffset
, -1, FI_REMAPF_LOCKED
);
2542 goto out_unlock_iolock
;
2546 * Need to zero the stuff we're not freeing, on disk.
2547 * If it's a realtime file & can't use unwritten extents then we
2548 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2549 * will take care of it for us.
2551 if (rt
&& !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
2553 error
= xfs_bmapi(NULL
, ip
, startoffset_fsb
,
2554 1, 0, NULL
, 0, &imap
, &nimap
, NULL
);
2556 goto out_unlock_iolock
;
2557 ASSERT(nimap
== 0 || nimap
== 1);
2558 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
2561 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
2562 block
= imap
.br_startblock
;
2563 mod
= do_div(block
, mp
->m_sb
.sb_rextsize
);
2565 startoffset_fsb
+= mp
->m_sb
.sb_rextsize
- mod
;
2568 error
= xfs_bmapi(NULL
, ip
, endoffset_fsb
- 1,
2569 1, 0, NULL
, 0, &imap
, &nimap
, NULL
);
2571 goto out_unlock_iolock
;
2572 ASSERT(nimap
== 0 || nimap
== 1);
2573 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
2574 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
2576 if (mod
&& (mod
!= mp
->m_sb
.sb_rextsize
))
2577 endoffset_fsb
-= mod
;
2580 if ((done
= (endoffset_fsb
<= startoffset_fsb
)))
2582 * One contiguous piece to clear
2584 error
= xfs_zero_remaining_bytes(ip
, offset
, offset
+ len
- 1);
2587 * Some full blocks, possibly two pieces to clear
2589 if (offset
< XFS_FSB_TO_B(mp
, startoffset_fsb
))
2590 error
= xfs_zero_remaining_bytes(ip
, offset
,
2591 XFS_FSB_TO_B(mp
, startoffset_fsb
) - 1);
2593 XFS_FSB_TO_B(mp
, endoffset_fsb
) < offset
+ len
)
2594 error
= xfs_zero_remaining_bytes(ip
,
2595 XFS_FSB_TO_B(mp
, endoffset_fsb
),
2600 * free file space until done or until there is an error
2602 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
2603 while (!error
&& !done
) {
2606 * allocate and setup the transaction. Allow this
2607 * transaction to dip into the reserve blocks to ensure
2608 * the freeing of the space succeeds at ENOSPC.
2610 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
2611 tp
->t_flags
|= XFS_TRANS_RESERVE
;
2612 error
= xfs_trans_reserve(tp
,
2614 XFS_WRITE_LOG_RES(mp
),
2616 XFS_TRANS_PERM_LOG_RES
,
2617 XFS_WRITE_LOG_COUNT
);
2620 * check for running out of space
2624 * Free the transaction structure.
2626 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
2627 xfs_trans_cancel(tp
, 0);
2630 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2631 error
= xfs_trans_reserve_quota(tp
, mp
,
2632 ip
->i_udquot
, ip
->i_gdquot
,
2633 resblks
, 0, XFS_QMOPT_RES_REGBLKS
);
2637 xfs_trans_ijoin(tp
, ip
);
2640 * issue the bunmapi() call to free the blocks
2642 xfs_bmap_init(&free_list
, &firstfsb
);
2643 error
= xfs_bunmapi(tp
, ip
, startoffset_fsb
,
2644 endoffset_fsb
- startoffset_fsb
,
2645 0, 2, &firstfsb
, &free_list
, &done
);
2651 * complete the transaction
2653 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2658 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2659 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
2664 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
2668 xfs_bmap_cancel(&free_list
);
2670 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
2671 xfs_iunlock(ip
, need_iolock
? (XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
) :
2677 * xfs_change_file_space()
2678 * This routine allocates or frees disk space for the given file.
2679 * The user specified parameters are checked for alignment and size
2688 xfs_change_file_space(
2695 xfs_mount_t
*mp
= ip
->i_mount
;
2700 xfs_off_t startoffset
;
2706 if (!S_ISREG(ip
->i_d
.di_mode
))
2707 return XFS_ERROR(EINVAL
);
2709 switch (bf
->l_whence
) {
2710 case 0: /*SEEK_SET*/
2712 case 1: /*SEEK_CUR*/
2713 bf
->l_start
+= offset
;
2715 case 2: /*SEEK_END*/
2716 bf
->l_start
+= ip
->i_size
;
2719 return XFS_ERROR(EINVAL
);
2722 llen
= bf
->l_len
> 0 ? bf
->l_len
- 1 : bf
->l_len
;
2724 if ( (bf
->l_start
< 0)
2725 || (bf
->l_start
> XFS_MAXIOFFSET(mp
))
2726 || (bf
->l_start
+ llen
< 0)
2727 || (bf
->l_start
+ llen
> XFS_MAXIOFFSET(mp
)))
2728 return XFS_ERROR(EINVAL
);
2732 startoffset
= bf
->l_start
;
2736 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
2738 * These calls do NOT zero the data space allocated to the file,
2739 * nor do they change the file size.
2741 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
2743 * These calls cause the new file data to be zeroed and the file
2744 * size to be changed.
2746 setprealloc
= clrprealloc
= 0;
2747 prealloc_type
= XFS_BMAPI_PREALLOC
;
2750 case XFS_IOC_ZERO_RANGE
:
2751 prealloc_type
|= XFS_BMAPI_CONVERT
;
2752 xfs_tosspages(ip
, startoffset
, startoffset
+ bf
->l_len
, 0);
2754 case XFS_IOC_RESVSP
:
2755 case XFS_IOC_RESVSP64
:
2756 error
= xfs_alloc_file_space(ip
, startoffset
, bf
->l_len
,
2757 prealloc_type
, attr_flags
);
2763 case XFS_IOC_UNRESVSP
:
2764 case XFS_IOC_UNRESVSP64
:
2765 if ((error
= xfs_free_file_space(ip
, startoffset
, bf
->l_len
,
2770 case XFS_IOC_ALLOCSP
:
2771 case XFS_IOC_ALLOCSP64
:
2772 case XFS_IOC_FREESP
:
2773 case XFS_IOC_FREESP64
:
2774 if (startoffset
> fsize
) {
2775 error
= xfs_alloc_file_space(ip
, fsize
,
2776 startoffset
- fsize
, 0, attr_flags
);
2781 iattr
.ia_valid
= ATTR_SIZE
;
2782 iattr
.ia_size
= startoffset
;
2784 error
= xfs_setattr(ip
, &iattr
, attr_flags
);
2794 return XFS_ERROR(EINVAL
);
2798 * update the inode timestamp, mode, and prealloc flag bits
2800 tp
= xfs_trans_alloc(mp
, XFS_TRANS_WRITEID
);
2802 if ((error
= xfs_trans_reserve(tp
, 0, XFS_WRITEID_LOG_RES(mp
),
2805 xfs_trans_cancel(tp
, 0);
2809 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2811 xfs_trans_ijoin(tp
, ip
);
2813 if ((attr_flags
& XFS_ATTR_DMI
) == 0) {
2814 ip
->i_d
.di_mode
&= ~S_ISUID
;
2817 * Note that we don't have to worry about mandatory
2818 * file locking being disabled here because we only
2819 * clear the S_ISGID bit if the Group execute bit is
2820 * on, but if it was on then mandatory locking wouldn't
2821 * have been enabled.
2823 if (ip
->i_d
.di_mode
& S_IXGRP
)
2824 ip
->i_d
.di_mode
&= ~S_ISGID
;
2826 xfs_trans_ichgtime(tp
, ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2829 ip
->i_d
.di_flags
|= XFS_DIFLAG_PREALLOC
;
2830 else if (clrprealloc
)
2831 ip
->i_d
.di_flags
&= ~XFS_DIFLAG_PREALLOC
;
2833 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2834 xfs_trans_set_sync(tp
);
2836 error
= xfs_trans_commit(tp
, 0);
2838 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);