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
, ip
->i_d
.di_projid
,
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 xfs_ichgtime(ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
193 if (S_ISDIR(ip
->i_d
.di_mode
)) {
194 code
= XFS_ERROR(EISDIR
);
196 } else if (!S_ISREG(ip
->i_d
.di_mode
)) {
197 code
= XFS_ERROR(EINVAL
);
202 * Make sure that the dquots are attached to the inode.
204 code
= xfs_qm_dqattach_locked(ip
, 0);
209 * Now we can make the changes. Before we join the inode
210 * to the transaction, if ATTR_SIZE is set then take care of
211 * the part of the truncation that must be done without the
212 * inode lock. This needs to be done before joining the inode
213 * to the transaction, because the inode cannot be unlocked
214 * once it is a part of the transaction.
216 if (iattr
->ia_size
> ip
->i_size
) {
218 * Do the first part of growing a file: zero any data
219 * in the last block that is beyond the old EOF. We
220 * need to do this before the inode is joined to the
221 * transaction to modify the i_size.
223 code
= xfs_zero_eof(ip
, iattr
->ia_size
, ip
->i_size
);
225 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
228 * We are going to log the inode size change in this
229 * transaction so any previous writes that are beyond the on
230 * disk EOF and the new EOF that have not been written out need
231 * to be written here. If we do not write the data out, we
232 * expose ourselves to the null files problem.
234 * Only flush from the on disk size to the smaller of the in
235 * memory file size or the new size as that's the range we
236 * really care about here and prevents waiting for other data
237 * not within the range we care about here.
240 ip
->i_size
!= ip
->i_d
.di_size
&&
241 iattr
->ia_size
> ip
->i_d
.di_size
) {
242 code
= xfs_flush_pages(ip
,
243 ip
->i_d
.di_size
, iattr
->ia_size
,
247 /* wait for all I/O to complete */
251 code
= xfs_itruncate_data(ip
, iattr
->ia_size
);
254 lock_flags
&= ~XFS_ILOCK_EXCL
;
255 ASSERT(lock_flags
== XFS_IOLOCK_EXCL
|| !need_iolock
);
258 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SETATTR_SIZE
);
259 if ((code
= xfs_trans_reserve(tp
, 0,
260 XFS_ITRUNCATE_LOG_RES(mp
), 0,
261 XFS_TRANS_PERM_LOG_RES
,
262 XFS_ITRUNCATE_LOG_COUNT
))) {
263 xfs_trans_cancel(tp
, 0);
265 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
268 commit_flags
= XFS_TRANS_RELEASE_LOG_RES
;
269 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
271 xfs_trans_ijoin(tp
, ip
);
274 * Only change the c/mtime if we are changing the size
275 * or we are explicitly asked to change it. This handles
276 * the semantic difference between truncate() and ftruncate()
277 * as implemented in the VFS.
279 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME
280 * is a special case where we need to update the times despite
281 * not having these flags set. For all other operations the
282 * VFS set these flags explicitly if it wants a timestamp
285 if (iattr
->ia_size
!= ip
->i_size
&&
286 (!(mask
& (ATTR_CTIME
| ATTR_MTIME
)))) {
287 iattr
->ia_ctime
= iattr
->ia_mtime
=
288 current_fs_time(inode
->i_sb
);
289 mask
|= ATTR_CTIME
| ATTR_MTIME
;
292 if (iattr
->ia_size
> ip
->i_size
) {
293 ip
->i_d
.di_size
= iattr
->ia_size
;
294 ip
->i_size
= iattr
->ia_size
;
295 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
296 } else if (iattr
->ia_size
<= ip
->i_size
||
297 (iattr
->ia_size
== 0 && ip
->i_d
.di_nextents
)) {
299 * signal a sync transaction unless
300 * we're truncating an already unlinked
301 * file on a wsync filesystem
303 code
= xfs_itruncate_finish(&tp
, ip
, iattr
->ia_size
,
305 ((ip
->i_d
.di_nlink
!= 0 ||
306 !(mp
->m_flags
& XFS_MOUNT_WSYNC
))
311 * Truncated "down", so we're removing references
312 * to old data here - if we now delay flushing for
313 * a long time, we expose ourselves unduly to the
314 * notorious NULL files problem. So, we mark this
315 * vnode and flush it when the file is closed, and
316 * do not wait the usual (long) time for writeout.
318 xfs_iflags_set(ip
, XFS_ITRUNCATED
);
321 xfs_trans_ijoin(tp
, ip
);
325 * Change file ownership. Must be the owner or privileged.
327 if (mask
& (ATTR_UID
|ATTR_GID
)) {
329 * CAP_FSETID overrides the following restrictions:
331 * The set-user-ID and set-group-ID bits of a file will be
332 * cleared upon successful return from chown()
334 if ((ip
->i_d
.di_mode
& (S_ISUID
|S_ISGID
)) &&
335 !capable(CAP_FSETID
)) {
336 ip
->i_d
.di_mode
&= ~(S_ISUID
|S_ISGID
);
340 * Change the ownerships and register quota modifications
341 * in the transaction.
344 if (XFS_IS_QUOTA_RUNNING(mp
) && XFS_IS_UQUOTA_ON(mp
)) {
345 ASSERT(mask
& ATTR_UID
);
347 olddquot1
= xfs_qm_vop_chown(tp
, ip
,
348 &ip
->i_udquot
, udqp
);
350 ip
->i_d
.di_uid
= uid
;
354 if (XFS_IS_QUOTA_RUNNING(mp
) && XFS_IS_GQUOTA_ON(mp
)) {
355 ASSERT(!XFS_IS_PQUOTA_ON(mp
));
356 ASSERT(mask
& ATTR_GID
);
358 olddquot2
= xfs_qm_vop_chown(tp
, ip
,
359 &ip
->i_gdquot
, gdqp
);
361 ip
->i_d
.di_gid
= gid
;
367 * Change file access modes.
369 if (mask
& ATTR_MODE
) {
370 umode_t mode
= iattr
->ia_mode
;
372 if (!in_group_p(inode
->i_gid
) && !capable(CAP_FSETID
))
375 ip
->i_d
.di_mode
&= S_IFMT
;
376 ip
->i_d
.di_mode
|= mode
& ~S_IFMT
;
378 inode
->i_mode
&= S_IFMT
;
379 inode
->i_mode
|= mode
& ~S_IFMT
;
383 * Change file access or modified times.
385 if (mask
& ATTR_ATIME
) {
386 inode
->i_atime
= iattr
->ia_atime
;
387 ip
->i_d
.di_atime
.t_sec
= iattr
->ia_atime
.tv_sec
;
388 ip
->i_d
.di_atime
.t_nsec
= iattr
->ia_atime
.tv_nsec
;
389 ip
->i_update_core
= 1;
391 if (mask
& ATTR_CTIME
) {
392 inode
->i_ctime
= iattr
->ia_ctime
;
393 ip
->i_d
.di_ctime
.t_sec
= iattr
->ia_ctime
.tv_sec
;
394 ip
->i_d
.di_ctime
.t_nsec
= iattr
->ia_ctime
.tv_nsec
;
395 ip
->i_update_core
= 1;
397 if (mask
& ATTR_MTIME
) {
398 inode
->i_mtime
= iattr
->ia_mtime
;
399 ip
->i_d
.di_mtime
.t_sec
= iattr
->ia_mtime
.tv_sec
;
400 ip
->i_d
.di_mtime
.t_nsec
= iattr
->ia_mtime
.tv_nsec
;
401 ip
->i_update_core
= 1;
405 * And finally, log the inode core if any attribute in it
408 if (mask
& (ATTR_UID
|ATTR_GID
|ATTR_MODE
|
409 ATTR_ATIME
|ATTR_CTIME
|ATTR_MTIME
))
410 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
412 XFS_STATS_INC(xs_ig_attrchg
);
415 * If this is a synchronous mount, make sure that the
416 * transaction goes to disk before returning to the user.
417 * This is slightly sub-optimal in that truncates require
418 * two sync transactions instead of one for wsync filesystems.
419 * One for the truncate and one for the timestamps since we
420 * don't want to change the timestamps unless we're sure the
421 * truncate worked. Truncates are less than 1% of the laddis
422 * mix so this probably isn't worth the trouble to optimize.
425 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
426 xfs_trans_set_sync(tp
);
428 code
= xfs_trans_commit(tp
, commit_flags
);
430 xfs_iunlock(ip
, lock_flags
);
433 * Release any dquot(s) the inode had kept before chown.
435 xfs_qm_dqrele(olddquot1
);
436 xfs_qm_dqrele(olddquot2
);
444 * XXX(hch): Updating the ACL entries is not atomic vs the i_mode
445 * update. We could avoid this with linked transactions
446 * and passing down the transaction pointer all the way
447 * to attr_set. No previous user of the generic
448 * Posix ACL code seems to care about this issue either.
450 if ((mask
& ATTR_MODE
) && !(flags
& XFS_ATTR_NOACL
)) {
451 code
= -xfs_acl_chmod(inode
);
453 return XFS_ERROR(code
);
459 commit_flags
|= XFS_TRANS_ABORT
;
464 xfs_trans_cancel(tp
, commit_flags
);
466 if (lock_flags
!= 0) {
467 xfs_iunlock(ip
, lock_flags
);
473 * The maximum pathlen is 1024 bytes. Since the minimum file system
474 * blocksize is 512 bytes, we can get a max of 2 extents back from
477 #define SYMLINK_MAPS 2
484 xfs_mount_t
*mp
= ip
->i_mount
;
485 int pathlen
= ip
->i_d
.di_size
;
486 int nmaps
= SYMLINK_MAPS
;
487 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
494 error
= xfs_bmapi(NULL
, ip
, 0, XFS_B_TO_FSB(mp
, pathlen
), 0, NULL
, 0,
499 for (n
= 0; n
< nmaps
; n
++) {
500 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
501 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
503 bp
= xfs_buf_read(mp
->m_ddev_targp
, d
, BTOBB(byte_cnt
),
504 XBF_LOCK
| XBF_MAPPED
| XBF_DONT_BLOCK
);
505 error
= XFS_BUF_GETERROR(bp
);
507 xfs_ioerror_alert("xfs_readlink",
508 ip
->i_mount
, bp
, XFS_BUF_ADDR(bp
));
512 if (pathlen
< byte_cnt
)
516 memcpy(link
, XFS_BUF_PTR(bp
), byte_cnt
);
520 link
[ip
->i_d
.di_size
] = '\0';
532 xfs_mount_t
*mp
= ip
->i_mount
;
536 trace_xfs_readlink(ip
);
538 if (XFS_FORCED_SHUTDOWN(mp
))
539 return XFS_ERROR(EIO
);
541 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
543 ASSERT((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
);
544 ASSERT(ip
->i_d
.di_size
<= MAXPATHLEN
);
546 pathlen
= ip
->i_d
.di_size
;
550 if (ip
->i_df
.if_flags
& XFS_IFINLINE
) {
551 memcpy(link
, ip
->i_df
.if_u1
.if_data
, pathlen
);
552 link
[pathlen
] = '\0';
554 error
= xfs_readlink_bmap(ip
, link
);
558 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
563 * Flags for xfs_free_eofblocks
565 #define XFS_FREE_EOF_TRYLOCK (1<<0)
568 * This is called by xfs_inactive to free any blocks beyond eof
569 * when the link count isn't zero and by xfs_dm_punch_hole() when
570 * punching a hole to EOF.
580 xfs_fileoff_t end_fsb
;
581 xfs_fileoff_t last_fsb
;
582 xfs_filblks_t map_len
;
584 xfs_bmbt_irec_t imap
;
587 * Figure out if there are any blocks beyond the end
588 * of the file. If not, then there is nothing to do.
590 end_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)ip
->i_size
));
591 last_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_MAXIOFFSET(mp
));
592 if (last_fsb
<= end_fsb
)
594 map_len
= last_fsb
- end_fsb
;
597 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
598 error
= xfs_bmapi(NULL
, ip
, end_fsb
, map_len
, 0,
599 NULL
, 0, &imap
, &nimaps
, NULL
);
600 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
602 if (!error
&& (nimaps
!= 0) &&
603 (imap
.br_startblock
!= HOLESTARTBLOCK
||
604 ip
->i_delayed_blks
)) {
606 * Attach the dquots to the inode up front.
608 error
= xfs_qm_dqattach(ip
, 0);
613 * There are blocks after the end of file.
614 * Free them up now by truncating the file to
617 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
620 * Do the xfs_itruncate_start() call before
621 * reserving any log space because
622 * itruncate_start will call into the buffer
624 * do that within a transaction.
626 if (flags
& XFS_FREE_EOF_TRYLOCK
) {
627 if (!xfs_ilock_nowait(ip
, XFS_IOLOCK_EXCL
)) {
628 xfs_trans_cancel(tp
, 0);
632 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
634 error
= xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
,
637 xfs_trans_cancel(tp
, 0);
638 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
642 error
= xfs_trans_reserve(tp
, 0,
643 XFS_ITRUNCATE_LOG_RES(mp
),
644 0, XFS_TRANS_PERM_LOG_RES
,
645 XFS_ITRUNCATE_LOG_COUNT
);
647 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
648 xfs_trans_cancel(tp
, 0);
649 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
653 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
654 xfs_trans_ijoin(tp
, ip
);
656 error
= xfs_itruncate_finish(&tp
, ip
,
661 * If we get an error at this point we
662 * simply don't bother truncating the file.
666 (XFS_TRANS_RELEASE_LOG_RES
|
669 error
= xfs_trans_commit(tp
,
670 XFS_TRANS_RELEASE_LOG_RES
);
672 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
|XFS_ILOCK_EXCL
);
678 * Free a symlink that has blocks associated with it.
681 xfs_inactive_symlink_rmt(
689 xfs_fsblock_t first_block
;
690 xfs_bmap_free_t free_list
;
693 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
701 ASSERT(ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
));
703 * We're freeing a symlink that has some
704 * blocks allocated to it. Free the
705 * blocks here. We know that we've got
706 * either 1 or 2 extents and that we can
707 * free them all in one bunmapi call.
709 ASSERT(ip
->i_d
.di_nextents
> 0 && ip
->i_d
.di_nextents
<= 2);
710 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
711 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
712 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
713 xfs_trans_cancel(tp
, 0);
718 * Lock the inode, fix the size, and join it to the transaction.
719 * Hold it so in the normal path, we still have it locked for
720 * the second transaction. In the error paths we need it
721 * held so the cancel won't rele it, see below.
723 xfs_ilock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
724 size
= (int)ip
->i_d
.di_size
;
726 xfs_trans_ijoin(tp
, ip
);
727 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
729 * Find the block(s) so we can inval and unmap them.
732 xfs_bmap_init(&free_list
, &first_block
);
733 nmaps
= ARRAY_SIZE(mval
);
734 if ((error
= xfs_bmapi(tp
, ip
, 0, XFS_B_TO_FSB(mp
, size
),
735 XFS_BMAPI_METADATA
, &first_block
, 0, mval
, &nmaps
,
739 * Invalidate the block(s).
741 for (i
= 0; i
< nmaps
; i
++) {
742 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
743 XFS_FSB_TO_DADDR(mp
, mval
[i
].br_startblock
),
744 XFS_FSB_TO_BB(mp
, mval
[i
].br_blockcount
), 0);
745 xfs_trans_binval(tp
, bp
);
748 * Unmap the dead block(s) to the free_list.
750 if ((error
= xfs_bunmapi(tp
, ip
, 0, size
, XFS_BMAPI_METADATA
, nmaps
,
751 &first_block
, &free_list
, &done
)))
755 * Commit the first transaction. This logs the EFI and the inode.
757 if ((error
= xfs_bmap_finish(&tp
, &free_list
, &committed
)))
760 * The transaction must have been committed, since there were
761 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
762 * The new tp has the extent freeing and EFDs.
766 * The first xact was committed, so add the inode to the new one.
767 * Mark it dirty so it will be logged and moved forward in the log as
768 * part of every commit.
770 xfs_trans_ijoin(tp
, ip
);
771 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
773 * Get a new, empty transaction to return to our caller.
775 ntp
= xfs_trans_dup(tp
);
777 * Commit the transaction containing extent freeing and EFDs.
778 * If we get an error on the commit here or on the reserve below,
779 * we need to unlock the inode since the new transaction doesn't
780 * have the inode attached.
782 error
= xfs_trans_commit(tp
, 0);
785 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
789 * transaction commit worked ok so we can drop the extra ticket
790 * reference that we gained in xfs_trans_dup()
792 xfs_log_ticket_put(tp
->t_ticket
);
795 * Remove the memory for extent descriptions (just bookkeeping).
797 if (ip
->i_df
.if_bytes
)
798 xfs_idata_realloc(ip
, -ip
->i_df
.if_bytes
, XFS_DATA_FORK
);
799 ASSERT(ip
->i_df
.if_bytes
== 0);
801 * Put an itruncate log reservation in the new transaction
804 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
805 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
806 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
810 * Return with the inode locked but not joined to the transaction.
816 xfs_bmap_cancel(&free_list
);
819 * Have to come here with the inode locked and either
820 * (held and in the transaction) or (not in the transaction).
821 * If the inode isn't held then cancel would iput it, but
822 * that's wrong since this is inactive and the vnode ref
823 * count is 0 already.
824 * Cancel won't do anything to the inode if held, but it still
825 * needs to be locked until the cancel is done, if it was
826 * joined to the transaction.
828 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
829 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
836 xfs_inactive_symlink_local(
842 ASSERT(ip
->i_d
.di_size
<= XFS_IFORK_DSIZE(ip
));
844 * We're freeing a symlink which fit into
845 * the inode. Just free the memory used
846 * to hold the old symlink.
848 error
= xfs_trans_reserve(*tpp
, 0,
849 XFS_ITRUNCATE_LOG_RES(ip
->i_mount
),
850 0, XFS_TRANS_PERM_LOG_RES
,
851 XFS_ITRUNCATE_LOG_COUNT
);
854 xfs_trans_cancel(*tpp
, 0);
858 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
861 * Zero length symlinks _can_ exist.
863 if (ip
->i_df
.if_bytes
> 0) {
864 xfs_idata_realloc(ip
,
865 -(ip
->i_df
.if_bytes
),
867 ASSERT(ip
->i_df
.if_bytes
== 0);
881 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
884 ASSERT(ip
->i_d
.di_forkoff
!= 0);
885 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
886 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
890 error
= xfs_attr_inactive(ip
);
894 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
895 error
= xfs_trans_reserve(tp
, 0,
896 XFS_IFREE_LOG_RES(mp
),
897 0, XFS_TRANS_PERM_LOG_RES
,
898 XFS_INACTIVE_LOG_COUNT
);
902 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
903 xfs_trans_ijoin(tp
, ip
);
904 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
906 ASSERT(ip
->i_d
.di_anextents
== 0);
912 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
913 xfs_trans_cancel(tp
, 0);
916 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
924 xfs_mount_t
*mp
= ip
->i_mount
;
927 if (!S_ISREG(ip
->i_d
.di_mode
) || (ip
->i_d
.di_mode
== 0))
930 /* If this is a read-only mount, don't do this (would generate I/O) */
931 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
934 if (!XFS_FORCED_SHUTDOWN(mp
)) {
938 * If we are using filestreams, and we have an unlinked
939 * file that we are processing the last close on, then nothing
940 * will be able to reopen and write to this file. Purge this
941 * inode from the filestreams cache so that it doesn't delay
942 * teardown of the inode.
944 if ((ip
->i_d
.di_nlink
== 0) && xfs_inode_is_filestream(ip
))
945 xfs_filestream_deassociate(ip
);
948 * If we previously truncated this file and removed old data
949 * in the process, we want to initiate "early" writeout on
950 * the last close. This is an attempt to combat the notorious
951 * NULL files problem which is particularly noticable from a
952 * truncate down, buffered (re-)write (delalloc), followed by
953 * a crash. What we are effectively doing here is
954 * significantly reducing the time window where we'd otherwise
955 * be exposed to that problem.
957 truncated
= xfs_iflags_test_and_clear(ip
, XFS_ITRUNCATED
);
958 if (truncated
&& VN_DIRTY(VFS_I(ip
)) && ip
->i_delayed_blks
> 0)
959 xfs_flush_pages(ip
, 0, -1, XBF_ASYNC
, FI_NONE
);
962 if (ip
->i_d
.di_nlink
!= 0) {
963 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
964 ((ip
->i_size
> 0) || (VN_CACHED(VFS_I(ip
)) > 0 ||
965 ip
->i_delayed_blks
> 0)) &&
966 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
)) &&
967 (!(ip
->i_d
.di_flags
&
968 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)))) {
971 * If we can't get the iolock just skip truncating
972 * the blocks past EOF because we could deadlock
973 * with the mmap_sem otherwise. We'll get another
974 * chance to drop them once the last reference to
975 * the inode is dropped, so we'll never leak blocks
978 error
= xfs_free_eofblocks(mp
, ip
,
979 XFS_FREE_EOF_TRYLOCK
);
991 * This is called when the vnode reference count for the vnode
992 * goes to zero. If the file has been unlinked, then it must
993 * now be truncated. Also, we clear all of the read-ahead state
994 * kept for the inode here since the file is now closed.
1000 xfs_bmap_free_t free_list
;
1001 xfs_fsblock_t first_block
;
1009 * If the inode is already free, then there can be nothing
1012 if (ip
->i_d
.di_mode
== 0 || is_bad_inode(VFS_I(ip
))) {
1013 ASSERT(ip
->i_df
.if_real_bytes
== 0);
1014 ASSERT(ip
->i_df
.if_broot_bytes
== 0);
1015 return VN_INACTIVE_CACHE
;
1019 * Only do a truncate if it's a regular file with
1020 * some actual space in it. It's OK to look at the
1021 * inode's fields without the lock because we're the
1022 * only one with a reference to the inode.
1024 truncate
= ((ip
->i_d
.di_nlink
== 0) &&
1025 ((ip
->i_d
.di_size
!= 0) || (ip
->i_size
!= 0) ||
1026 (ip
->i_d
.di_nextents
> 0) || (ip
->i_delayed_blks
> 0)) &&
1027 ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
));
1033 /* If this is a read-only mount, don't do this (would generate I/O) */
1034 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
1037 if (ip
->i_d
.di_nlink
!= 0) {
1038 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
1039 ((ip
->i_size
> 0) || (VN_CACHED(VFS_I(ip
)) > 0 ||
1040 ip
->i_delayed_blks
> 0)) &&
1041 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
) &&
1042 (!(ip
->i_d
.di_flags
&
1043 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)) ||
1044 (ip
->i_delayed_blks
!= 0)))) {
1045 error
= xfs_free_eofblocks(mp
, ip
, 0);
1047 return VN_INACTIVE_CACHE
;
1052 ASSERT(ip
->i_d
.di_nlink
== 0);
1054 error
= xfs_qm_dqattach(ip
, 0);
1056 return VN_INACTIVE_CACHE
;
1058 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1061 * Do the xfs_itruncate_start() call before
1062 * reserving any log space because itruncate_start
1063 * will call into the buffer cache and we can't
1064 * do that within a transaction.
1066 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
1068 error
= xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
, 0);
1070 xfs_trans_cancel(tp
, 0);
1071 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1072 return VN_INACTIVE_CACHE
;
1075 error
= xfs_trans_reserve(tp
, 0,
1076 XFS_ITRUNCATE_LOG_RES(mp
),
1077 0, XFS_TRANS_PERM_LOG_RES
,
1078 XFS_ITRUNCATE_LOG_COUNT
);
1080 /* Don't call itruncate_cleanup */
1081 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1082 xfs_trans_cancel(tp
, 0);
1083 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1084 return VN_INACTIVE_CACHE
;
1087 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1088 xfs_trans_ijoin(tp
, ip
);
1091 * normally, we have to run xfs_itruncate_finish sync.
1092 * But if filesystem is wsync and we're in the inactive
1093 * path, then we know that nlink == 0, and that the
1094 * xaction that made nlink == 0 is permanently committed
1095 * since xfs_remove runs as a synchronous transaction.
1097 error
= xfs_itruncate_finish(&tp
, ip
, 0, XFS_DATA_FORK
,
1098 (!(mp
->m_flags
& XFS_MOUNT_WSYNC
) ? 1 : 0));
1101 xfs_trans_cancel(tp
,
1102 XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1103 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1104 return VN_INACTIVE_CACHE
;
1106 } else if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
) {
1109 * If we get an error while cleaning up a
1110 * symlink we bail out.
1112 error
= (ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
)) ?
1113 xfs_inactive_symlink_rmt(ip
, &tp
) :
1114 xfs_inactive_symlink_local(ip
, &tp
);
1118 return VN_INACTIVE_CACHE
;
1121 xfs_trans_ijoin(tp
, ip
);
1123 error
= xfs_trans_reserve(tp
, 0,
1124 XFS_IFREE_LOG_RES(mp
),
1125 0, XFS_TRANS_PERM_LOG_RES
,
1126 XFS_INACTIVE_LOG_COUNT
);
1128 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1129 xfs_trans_cancel(tp
, 0);
1130 return VN_INACTIVE_CACHE
;
1133 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1134 xfs_trans_ijoin(tp
, ip
);
1138 * If there are attributes associated with the file
1139 * then blow them away now. The code calls a routine
1140 * that recursively deconstructs the attribute fork.
1141 * We need to just commit the current transaction
1142 * because we can't use it for xfs_attr_inactive().
1144 if (ip
->i_d
.di_anextents
> 0) {
1145 error
= xfs_inactive_attrs(ip
, &tp
);
1147 * If we got an error, the transaction is already
1148 * cancelled, and the inode is unlocked. Just get out.
1151 return VN_INACTIVE_CACHE
;
1152 } else if (ip
->i_afp
) {
1153 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
1159 xfs_bmap_init(&free_list
, &first_block
);
1160 error
= xfs_ifree(tp
, ip
, &free_list
);
1163 * If we fail to free the inode, shut down. The cancel
1164 * might do that, we need to make sure. Otherwise the
1165 * inode might be lost for a long time or forever.
1167 if (!XFS_FORCED_SHUTDOWN(mp
)) {
1169 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1170 error
, mp
->m_fsname
);
1171 xfs_force_shutdown(mp
, SHUTDOWN_META_IO_ERROR
);
1173 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
1176 * Credit the quota account(s). The inode is gone.
1178 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_ICOUNT
, -1);
1181 * Just ignore errors at this point. There is nothing we can
1182 * do except to try to keep going. Make sure it's not a silent
1185 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1187 xfs_fs_cmn_err(CE_NOTE
, mp
, "xfs_inactive: "
1188 "xfs_bmap_finish() returned error %d", error
);
1189 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1191 xfs_fs_cmn_err(CE_NOTE
, mp
, "xfs_inactive: "
1192 "xfs_trans_commit() returned error %d", error
);
1196 * Release the dquots held by inode, if any.
1198 xfs_qm_dqdetach(ip
);
1199 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1202 return VN_INACTIVE_CACHE
;
1206 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1207 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1208 * ci_name->name will point to a the actual name (caller must free) or
1209 * will be set to NULL if an exact match is found.
1214 struct xfs_name
*name
,
1216 struct xfs_name
*ci_name
)
1222 trace_xfs_lookup(dp
, name
);
1224 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
1225 return XFS_ERROR(EIO
);
1227 lock_mode
= xfs_ilock_map_shared(dp
);
1228 error
= xfs_dir_lookup(NULL
, dp
, name
, &inum
, ci_name
);
1229 xfs_iunlock_map_shared(dp
, lock_mode
);
1234 error
= xfs_iget(dp
->i_mount
, NULL
, inum
, 0, 0, ipp
);
1242 kmem_free(ci_name
->name
);
1251 struct xfs_name
*name
,
1257 int is_dir
= S_ISDIR(mode
);
1258 struct xfs_mount
*mp
= dp
->i_mount
;
1259 struct xfs_inode
*ip
= NULL
;
1260 struct xfs_trans
*tp
= NULL
;
1262 xfs_bmap_free_t free_list
;
1263 xfs_fsblock_t first_block
;
1264 boolean_t unlock_dp_on_error
= B_FALSE
;
1268 struct xfs_dquot
*udqp
= NULL
;
1269 struct xfs_dquot
*gdqp
= NULL
;
1274 trace_xfs_create(dp
, name
);
1276 if (XFS_FORCED_SHUTDOWN(mp
))
1277 return XFS_ERROR(EIO
);
1279 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
1280 prid
= dp
->i_d
.di_projid
;
1285 * Make sure that we have allocated dquot(s) on disk.
1287 error
= xfs_qm_vop_dqalloc(dp
, current_fsuid(), current_fsgid(), prid
,
1288 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
1294 resblks
= XFS_MKDIR_SPACE_RES(mp
, name
->len
);
1295 log_res
= XFS_MKDIR_LOG_RES(mp
);
1296 log_count
= XFS_MKDIR_LOG_COUNT
;
1297 tp
= xfs_trans_alloc(mp
, XFS_TRANS_MKDIR
);
1299 resblks
= XFS_CREATE_SPACE_RES(mp
, name
->len
);
1300 log_res
= XFS_CREATE_LOG_RES(mp
);
1301 log_count
= XFS_CREATE_LOG_COUNT
;
1302 tp
= xfs_trans_alloc(mp
, XFS_TRANS_CREATE
);
1305 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1308 * Initially assume that the file does not exist and
1309 * reserve the resources for that case. If that is not
1310 * the case we'll drop the one we have and get a more
1311 * appropriate transaction later.
1313 error
= xfs_trans_reserve(tp
, resblks
, log_res
, 0,
1314 XFS_TRANS_PERM_LOG_RES
, log_count
);
1315 if (error
== ENOSPC
) {
1316 /* flush outstanding delalloc blocks and retry */
1317 xfs_flush_inodes(dp
);
1318 error
= xfs_trans_reserve(tp
, resblks
, log_res
, 0,
1319 XFS_TRANS_PERM_LOG_RES
, log_count
);
1321 if (error
== ENOSPC
) {
1322 /* No space at all so try a "no-allocation" reservation */
1324 error
= xfs_trans_reserve(tp
, 0, log_res
, 0,
1325 XFS_TRANS_PERM_LOG_RES
, log_count
);
1329 goto out_trans_cancel
;
1332 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
1333 unlock_dp_on_error
= B_TRUE
;
1336 * Check for directory link count overflow.
1338 if (is_dir
&& dp
->i_d
.di_nlink
>= XFS_MAXLINK
) {
1339 error
= XFS_ERROR(EMLINK
);
1340 goto out_trans_cancel
;
1343 xfs_bmap_init(&free_list
, &first_block
);
1346 * Reserve disk quota and the inode.
1348 error
= xfs_trans_reserve_quota(tp
, mp
, udqp
, gdqp
, resblks
, 1, 0);
1350 goto out_trans_cancel
;
1352 error
= xfs_dir_canenter(tp
, dp
, name
, resblks
);
1354 goto out_trans_cancel
;
1357 * A newly created regular or special file just has one directory
1358 * entry pointing to them, but a directory also the "." entry
1359 * pointing to itself.
1361 error
= xfs_dir_ialloc(&tp
, dp
, mode
, is_dir
? 2 : 1, rdev
, credp
,
1362 prid
, resblks
> 0, &ip
, &committed
);
1364 if (error
== ENOSPC
)
1365 goto out_trans_cancel
;
1366 goto out_trans_abort
;
1370 * At this point, we've gotten a newly allocated inode.
1371 * It is locked (and joined to the transaction).
1373 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1376 * Now we join the directory inode to the transaction. We do not do it
1377 * earlier because xfs_dir_ialloc might commit the previous transaction
1378 * (and release all the locks). An error from here on will result in
1379 * the transaction cancel unlocking dp so don't do it explicitly in the
1382 xfs_trans_ijoin_ref(tp
, dp
, XFS_ILOCK_EXCL
);
1383 unlock_dp_on_error
= B_FALSE
;
1385 error
= xfs_dir_createname(tp
, dp
, name
, ip
->i_ino
,
1386 &first_block
, &free_list
, resblks
?
1387 resblks
- XFS_IALLOC_SPACE_RES(mp
) : 0);
1389 ASSERT(error
!= ENOSPC
);
1390 goto out_trans_abort
;
1392 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1393 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1396 error
= xfs_dir_init(tp
, ip
, dp
);
1398 goto out_bmap_cancel
;
1400 error
= xfs_bumplink(tp
, dp
);
1402 goto out_bmap_cancel
;
1406 * If this is a synchronous mount, make sure that the
1407 * create transaction goes to disk before returning to
1410 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
))
1411 xfs_trans_set_sync(tp
);
1414 * Attach the dquot(s) to the inodes and modify them incore.
1415 * These ids of the inode couldn't have changed since the new
1416 * inode has been locked ever since it was created.
1418 xfs_qm_vop_create_dqattach(tp
, ip
, udqp
, gdqp
);
1421 * xfs_trans_commit normally decrements the vnode ref count
1422 * when it unlocks the inode. Since we want to return the
1423 * vnode to the caller, we bump the vnode ref count now.
1427 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1429 goto out_abort_rele
;
1431 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1437 xfs_qm_dqrele(udqp
);
1438 xfs_qm_dqrele(gdqp
);
1444 xfs_bmap_cancel(&free_list
);
1446 cancel_flags
|= XFS_TRANS_ABORT
;
1448 xfs_trans_cancel(tp
, cancel_flags
);
1450 xfs_qm_dqrele(udqp
);
1451 xfs_qm_dqrele(gdqp
);
1453 if (unlock_dp_on_error
)
1454 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
1460 * Wait until after the current transaction is aborted to
1461 * release the inode. This prevents recursive transactions
1462 * and deadlocks from xfs_inactive.
1464 xfs_bmap_cancel(&free_list
);
1465 cancel_flags
|= XFS_TRANS_ABORT
;
1466 xfs_trans_cancel(tp
, cancel_flags
);
1468 unlock_dp_on_error
= B_FALSE
;
1474 int xfs_small_retries
;
1475 int xfs_middle_retries
;
1476 int xfs_lots_retries
;
1477 int xfs_lock_delays
;
1481 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1485 xfs_lock_inumorder(int lock_mode
, int subclass
)
1487 if (lock_mode
& (XFS_IOLOCK_SHARED
|XFS_IOLOCK_EXCL
))
1488 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_IOLOCK_SHIFT
;
1489 if (lock_mode
& (XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
))
1490 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_ILOCK_SHIFT
;
1496 * The following routine will lock n inodes in exclusive mode.
1497 * We assume the caller calls us with the inodes in i_ino order.
1499 * We need to detect deadlock where an inode that we lock
1500 * is in the AIL and we start waiting for another inode that is locked
1501 * by a thread in a long running transaction (such as truncate). This can
1502 * result in deadlock since the long running trans might need to wait
1503 * for the inode we just locked in order to push the tail and free space
1512 int attempts
= 0, i
, j
, try_lock
;
1515 ASSERT(ips
&& (inodes
>= 2)); /* we need at least two */
1521 for (; i
< inodes
; i
++) {
1524 if (i
&& (ips
[i
] == ips
[i
-1])) /* Already locked */
1528 * If try_lock is not set yet, make sure all locked inodes
1529 * are not in the AIL.
1530 * If any are, set try_lock to be used later.
1534 for (j
= (i
- 1); j
>= 0 && !try_lock
; j
--) {
1535 lp
= (xfs_log_item_t
*)ips
[j
]->i_itemp
;
1536 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
1543 * If any of the previous locks we have locked is in the AIL,
1544 * we must TRY to get the second and subsequent locks. If
1545 * we can't get any, we must release all we have
1550 /* try_lock must be 0 if i is 0. */
1552 * try_lock means we have an inode locked
1553 * that is in the AIL.
1556 if (!xfs_ilock_nowait(ips
[i
], xfs_lock_inumorder(lock_mode
, i
))) {
1560 * Unlock all previous guys and try again.
1561 * xfs_iunlock will try to push the tail
1562 * if the inode is in the AIL.
1565 for(j
= i
- 1; j
>= 0; j
--) {
1568 * Check to see if we've already
1569 * unlocked this one.
1570 * Not the first one going back,
1571 * and the inode ptr is the same.
1573 if ((j
!= (i
- 1)) && ips
[j
] ==
1577 xfs_iunlock(ips
[j
], lock_mode
);
1580 if ((attempts
% 5) == 0) {
1581 delay(1); /* Don't just spin the CPU */
1591 xfs_ilock(ips
[i
], xfs_lock_inumorder(lock_mode
, i
));
1597 if (attempts
< 5) xfs_small_retries
++;
1598 else if (attempts
< 100) xfs_middle_retries
++;
1599 else xfs_lots_retries
++;
1607 * xfs_lock_two_inodes() can only be used to lock one type of lock
1608 * at a time - the iolock or the ilock, but not both at once. If
1609 * we lock both at once, lockdep will report false positives saying
1610 * we have violated locking orders.
1613 xfs_lock_two_inodes(
1622 if (lock_mode
& (XFS_IOLOCK_SHARED
|XFS_IOLOCK_EXCL
))
1623 ASSERT((lock_mode
& (XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
)) == 0);
1624 ASSERT(ip0
->i_ino
!= ip1
->i_ino
);
1626 if (ip0
->i_ino
> ip1
->i_ino
) {
1633 xfs_ilock(ip0
, xfs_lock_inumorder(lock_mode
, 0));
1636 * If the first lock we have locked is in the AIL, we must TRY to get
1637 * the second lock. If we can't get it, we must release the first one
1640 lp
= (xfs_log_item_t
*)ip0
->i_itemp
;
1641 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
1642 if (!xfs_ilock_nowait(ip1
, xfs_lock_inumorder(lock_mode
, 1))) {
1643 xfs_iunlock(ip0
, lock_mode
);
1644 if ((++attempts
% 5) == 0)
1645 delay(1); /* Don't just spin the CPU */
1649 xfs_ilock(ip1
, xfs_lock_inumorder(lock_mode
, 1));
1656 struct xfs_name
*name
,
1659 xfs_mount_t
*mp
= dp
->i_mount
;
1660 xfs_trans_t
*tp
= NULL
;
1661 int is_dir
= S_ISDIR(ip
->i_d
.di_mode
);
1663 xfs_bmap_free_t free_list
;
1664 xfs_fsblock_t first_block
;
1671 trace_xfs_remove(dp
, name
);
1673 if (XFS_FORCED_SHUTDOWN(mp
))
1674 return XFS_ERROR(EIO
);
1676 error
= xfs_qm_dqattach(dp
, 0);
1680 error
= xfs_qm_dqattach(ip
, 0);
1685 tp
= xfs_trans_alloc(mp
, XFS_TRANS_RMDIR
);
1686 log_count
= XFS_DEFAULT_LOG_COUNT
;
1688 tp
= xfs_trans_alloc(mp
, XFS_TRANS_REMOVE
);
1689 log_count
= XFS_REMOVE_LOG_COUNT
;
1691 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1694 * We try to get the real space reservation first,
1695 * allowing for directory btree deletion(s) implying
1696 * possible bmap insert(s). If we can't get the space
1697 * reservation then we use 0 instead, and avoid the bmap
1698 * btree insert(s) in the directory code by, if the bmap
1699 * insert tries to happen, instead trimming the LAST
1700 * block from the directory.
1702 resblks
= XFS_REMOVE_SPACE_RES(mp
);
1703 error
= xfs_trans_reserve(tp
, resblks
, XFS_REMOVE_LOG_RES(mp
), 0,
1704 XFS_TRANS_PERM_LOG_RES
, log_count
);
1705 if (error
== ENOSPC
) {
1707 error
= xfs_trans_reserve(tp
, 0, XFS_REMOVE_LOG_RES(mp
), 0,
1708 XFS_TRANS_PERM_LOG_RES
, log_count
);
1711 ASSERT(error
!= ENOSPC
);
1713 goto out_trans_cancel
;
1716 xfs_lock_two_inodes(dp
, ip
, XFS_ILOCK_EXCL
);
1718 xfs_trans_ijoin_ref(tp
, dp
, XFS_ILOCK_EXCL
);
1719 xfs_trans_ijoin_ref(tp
, ip
, XFS_ILOCK_EXCL
);
1722 * If we're removing a directory perform some additional validation.
1725 ASSERT(ip
->i_d
.di_nlink
>= 2);
1726 if (ip
->i_d
.di_nlink
!= 2) {
1727 error
= XFS_ERROR(ENOTEMPTY
);
1728 goto out_trans_cancel
;
1730 if (!xfs_dir_isempty(ip
)) {
1731 error
= XFS_ERROR(ENOTEMPTY
);
1732 goto out_trans_cancel
;
1736 xfs_bmap_init(&free_list
, &first_block
);
1737 error
= xfs_dir_removename(tp
, dp
, name
, ip
->i_ino
,
1738 &first_block
, &free_list
, resblks
);
1740 ASSERT(error
!= ENOENT
);
1741 goto out_bmap_cancel
;
1743 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1747 * Drop the link from ip's "..".
1749 error
= xfs_droplink(tp
, dp
);
1751 goto out_bmap_cancel
;
1754 * Drop the "." link from ip to self.
1756 error
= xfs_droplink(tp
, ip
);
1758 goto out_bmap_cancel
;
1761 * When removing a non-directory we need to log the parent
1762 * inode here. For a directory this is done implicitly
1763 * by the xfs_droplink call for the ".." entry.
1765 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1769 * Drop the link from dp to ip.
1771 error
= xfs_droplink(tp
, ip
);
1773 goto out_bmap_cancel
;
1776 * Determine if this is the last link while
1777 * we are in the transaction.
1779 link_zero
= (ip
->i_d
.di_nlink
== 0);
1782 * If this is a synchronous mount, make sure that the
1783 * remove transaction goes to disk before returning to
1786 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
))
1787 xfs_trans_set_sync(tp
);
1789 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1791 goto out_bmap_cancel
;
1793 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1798 * If we are using filestreams, kill the stream association.
1799 * If the file is still open it may get a new one but that
1800 * will get killed on last close in xfs_close() so we don't
1801 * have to worry about that.
1803 if (!is_dir
&& link_zero
&& xfs_inode_is_filestream(ip
))
1804 xfs_filestream_deassociate(ip
);
1809 xfs_bmap_cancel(&free_list
);
1810 cancel_flags
|= XFS_TRANS_ABORT
;
1812 xfs_trans_cancel(tp
, cancel_flags
);
1821 struct xfs_name
*target_name
)
1823 xfs_mount_t
*mp
= tdp
->i_mount
;
1826 xfs_bmap_free_t free_list
;
1827 xfs_fsblock_t first_block
;
1832 trace_xfs_link(tdp
, target_name
);
1834 ASSERT(!S_ISDIR(sip
->i_d
.di_mode
));
1836 if (XFS_FORCED_SHUTDOWN(mp
))
1837 return XFS_ERROR(EIO
);
1839 error
= xfs_qm_dqattach(sip
, 0);
1843 error
= xfs_qm_dqattach(tdp
, 0);
1847 tp
= xfs_trans_alloc(mp
, XFS_TRANS_LINK
);
1848 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1849 resblks
= XFS_LINK_SPACE_RES(mp
, target_name
->len
);
1850 error
= xfs_trans_reserve(tp
, resblks
, XFS_LINK_LOG_RES(mp
), 0,
1851 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
1852 if (error
== ENOSPC
) {
1854 error
= xfs_trans_reserve(tp
, 0, XFS_LINK_LOG_RES(mp
), 0,
1855 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
1862 xfs_lock_two_inodes(sip
, tdp
, XFS_ILOCK_EXCL
);
1864 xfs_trans_ijoin_ref(tp
, sip
, XFS_ILOCK_EXCL
);
1865 xfs_trans_ijoin_ref(tp
, tdp
, XFS_ILOCK_EXCL
);
1868 * If the source has too many links, we can't make any more to it.
1870 if (sip
->i_d
.di_nlink
>= XFS_MAXLINK
) {
1871 error
= XFS_ERROR(EMLINK
);
1876 * If we are using project inheritance, we only allow hard link
1877 * creation in our tree when the project IDs are the same; else
1878 * the tree quota mechanism could be circumvented.
1880 if (unlikely((tdp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
) &&
1881 (tdp
->i_d
.di_projid
!= sip
->i_d
.di_projid
))) {
1882 error
= XFS_ERROR(EXDEV
);
1886 error
= xfs_dir_canenter(tp
, tdp
, target_name
, resblks
);
1890 xfs_bmap_init(&free_list
, &first_block
);
1892 error
= xfs_dir_createname(tp
, tdp
, target_name
, sip
->i_ino
,
1893 &first_block
, &free_list
, resblks
);
1896 xfs_ichgtime(tdp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1897 xfs_trans_log_inode(tp
, tdp
, XFS_ILOG_CORE
);
1899 error
= xfs_bumplink(tp
, sip
);
1904 * If this is a synchronous mount, make sure that the
1905 * link transaction goes to disk before returning to
1908 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
1909 xfs_trans_set_sync(tp
);
1912 error
= xfs_bmap_finish (&tp
, &free_list
, &committed
);
1914 xfs_bmap_cancel(&free_list
);
1918 return xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1921 cancel_flags
|= XFS_TRANS_ABORT
;
1923 xfs_trans_cancel(tp
, cancel_flags
);
1931 struct xfs_name
*link_name
,
1932 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
= dp
->i_d
.di_projid
;
1981 prid
= (xfs_prid_t
)dfltprid
;
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
),
2048 1, 0, credp
, 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
,
2100 resblks
-= fs_blocks
;
2101 ip
->i_d
.di_size
= pathlen
;
2102 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2104 cur_chunk
= target_path
;
2105 for (n
= 0; n
< nmaps
; n
++) {
2106 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
2107 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
2108 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
2109 BTOBB(byte_cnt
), 0);
2110 ASSERT(bp
&& !XFS_BUF_GETERROR(bp
));
2111 if (pathlen
< byte_cnt
) {
2114 pathlen
-= byte_cnt
;
2116 memcpy(XFS_BUF_PTR(bp
), cur_chunk
, byte_cnt
);
2117 cur_chunk
+= byte_cnt
;
2119 xfs_trans_log_buf(tp
, bp
, 0, byte_cnt
- 1);
2124 * Create the directory entry for the symlink.
2126 error
= xfs_dir_createname(tp
, dp
, link_name
, ip
->i_ino
,
2127 &first_block
, &free_list
, resblks
);
2130 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2131 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2134 * If this is a synchronous mount, make sure that the
2135 * symlink transaction goes to disk before returning to
2138 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2139 xfs_trans_set_sync(tp
);
2143 * xfs_trans_commit normally decrements the vnode ref count
2144 * when it unlocks the inode. Since we want to return the
2145 * vnode to the caller, we bump the vnode ref count now.
2149 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2153 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2154 xfs_qm_dqrele(udqp
);
2155 xfs_qm_dqrele(gdqp
);
2163 xfs_bmap_cancel(&free_list
);
2164 cancel_flags
|= XFS_TRANS_ABORT
;
2166 xfs_trans_cancel(tp
, cancel_flags
);
2167 xfs_qm_dqrele(udqp
);
2168 xfs_qm_dqrele(gdqp
);
2170 if (unlock_dp_on_error
)
2171 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
2182 xfs_mount_t
*mp
= ip
->i_mount
;
2186 if (!capable(CAP_SYS_ADMIN
))
2187 return XFS_ERROR(EPERM
);
2189 if (XFS_FORCED_SHUTDOWN(mp
))
2190 return XFS_ERROR(EIO
);
2192 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SET_DMATTRS
);
2193 error
= xfs_trans_reserve(tp
, 0, XFS_ICHANGE_LOG_RES (mp
), 0, 0, 0);
2195 xfs_trans_cancel(tp
, 0);
2198 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2199 xfs_trans_ijoin_ref(tp
, ip
, XFS_ILOCK_EXCL
);
2201 ip
->i_d
.di_dmevmask
= evmask
;
2202 ip
->i_d
.di_dmstate
= state
;
2204 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2205 error
= xfs_trans_commit(tp
, 0);
2211 * xfs_alloc_file_space()
2212 * This routine allocates disk space for the given file.
2214 * If alloc_type == 0, this request is for an ALLOCSP type
2215 * request which will change the file size. In this case, no
2216 * DMAPI event will be generated by the call. A TRUNCATE event
2217 * will be generated later by xfs_setattr.
2219 * If alloc_type != 0, this request is for a RESVSP type
2220 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2221 * lower block boundary byte address is less than the file's
2230 xfs_alloc_file_space(
2237 xfs_mount_t
*mp
= ip
->i_mount
;
2239 xfs_filblks_t allocated_fsb
;
2240 xfs_filblks_t allocatesize_fsb
;
2241 xfs_extlen_t extsz
, temp
;
2242 xfs_fileoff_t startoffset_fsb
;
2243 xfs_fsblock_t firstfsb
;
2249 xfs_bmbt_irec_t imaps
[1], *imapp
;
2250 xfs_bmap_free_t free_list
;
2251 uint qblocks
, resblks
, resrtextents
;
2255 trace_xfs_alloc_file_space(ip
);
2257 if (XFS_FORCED_SHUTDOWN(mp
))
2258 return XFS_ERROR(EIO
);
2260 error
= xfs_qm_dqattach(ip
, 0);
2265 return XFS_ERROR(EINVAL
);
2267 rt
= XFS_IS_REALTIME_INODE(ip
);
2268 extsz
= xfs_get_extsz_hint(ip
);
2273 bmapi_flag
= XFS_BMAPI_WRITE
| (alloc_type
? XFS_BMAPI_PREALLOC
: 0);
2274 startoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
2275 allocatesize_fsb
= XFS_B_TO_FSB(mp
, count
);
2278 * Allocate file space until done or until there is an error
2280 while (allocatesize_fsb
&& !error
) {
2284 * Determine space reservations for data/realtime.
2286 if (unlikely(extsz
)) {
2287 s
= startoffset_fsb
;
2290 e
= startoffset_fsb
+ allocatesize_fsb
;
2291 if ((temp
= do_mod(startoffset_fsb
, extsz
)))
2293 if ((temp
= do_mod(e
, extsz
)))
2297 e
= allocatesize_fsb
;
2301 resrtextents
= qblocks
= (uint
)(e
- s
);
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
= \
2308 XFS_DIOSTRAT_SPACE_RES(mp
, (uint
)(e
- s
));
2309 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
2313 * Allocate and setup the transaction.
2315 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
2316 error
= xfs_trans_reserve(tp
, resblks
,
2317 XFS_WRITE_LOG_RES(mp
), resrtextents
,
2318 XFS_TRANS_PERM_LOG_RES
,
2319 XFS_WRITE_LOG_COUNT
);
2321 * Check for running out of space
2325 * Free the transaction structure.
2327 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
2328 xfs_trans_cancel(tp
, 0);
2331 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2332 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, qblocks
,
2337 xfs_trans_ijoin(tp
, ip
);
2340 * Issue the xfs_bmapi() call to allocate the blocks
2342 xfs_bmap_init(&free_list
, &firstfsb
);
2343 error
= xfs_bmapi(tp
, ip
, startoffset_fsb
,
2344 allocatesize_fsb
, bmapi_flag
,
2345 &firstfsb
, 0, imapp
, &nimaps
,
2352 * Complete the transaction
2354 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2359 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2360 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
2365 allocated_fsb
= imapp
->br_blockcount
;
2368 error
= XFS_ERROR(ENOSPC
);
2372 startoffset_fsb
+= allocated_fsb
;
2373 allocatesize_fsb
-= allocated_fsb
;
2378 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
2379 xfs_bmap_cancel(&free_list
);
2380 xfs_trans_unreserve_quota_nblks(tp
, ip
, qblocks
, 0, quota_flag
);
2382 error1
: /* Just cancel transaction */
2383 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
2384 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
2389 * Zero file bytes between startoff and endoff inclusive.
2390 * The iolock is held exclusive and no blocks are buffered.
2392 * This function is used by xfs_free_file_space() to zero
2393 * partial blocks when the range to free is not block aligned.
2394 * When unreserving space with boundaries that are not block
2395 * aligned we round up the start and round down the end
2396 * boundaries and then use this function to zero the parts of
2397 * the blocks that got dropped during the rounding.
2400 xfs_zero_remaining_bytes(
2405 xfs_bmbt_irec_t imap
;
2406 xfs_fileoff_t offset_fsb
;
2407 xfs_off_t lastoffset
;
2410 xfs_mount_t
*mp
= ip
->i_mount
;
2415 * Avoid doing I/O beyond eof - it's not necessary
2416 * since nothing can read beyond eof. The space will
2417 * be zeroed when the file is extended anyway.
2419 if (startoff
>= ip
->i_size
)
2422 if (endoff
> ip
->i_size
)
2423 endoff
= ip
->i_size
;
2425 bp
= xfs_buf_get_noaddr(mp
->m_sb
.sb_blocksize
,
2426 XFS_IS_REALTIME_INODE(ip
) ?
2427 mp
->m_rtdev_targp
: mp
->m_ddev_targp
);
2429 return XFS_ERROR(ENOMEM
);
2431 for (offset
= startoff
; offset
<= endoff
; offset
= lastoffset
+ 1) {
2432 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
2434 error
= xfs_bmapi(NULL
, ip
, offset_fsb
, 1, 0,
2435 NULL
, 0, &imap
, &nimap
, NULL
);
2436 if (error
|| nimap
< 1)
2438 ASSERT(imap
.br_blockcount
>= 1);
2439 ASSERT(imap
.br_startoff
== offset_fsb
);
2440 lastoffset
= XFS_FSB_TO_B(mp
, imap
.br_startoff
+ 1) - 1;
2441 if (lastoffset
> endoff
)
2442 lastoffset
= endoff
;
2443 if (imap
.br_startblock
== HOLESTARTBLOCK
)
2445 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
2446 if (imap
.br_state
== XFS_EXT_UNWRITTEN
)
2449 XFS_BUF_UNWRITE(bp
);
2451 XFS_BUF_SET_ADDR(bp
, xfs_fsb_to_db(ip
, imap
.br_startblock
));
2453 error
= xfs_iowait(bp
);
2455 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
2456 mp
, bp
, XFS_BUF_ADDR(bp
));
2459 memset(XFS_BUF_PTR(bp
) +
2460 (offset
- XFS_FSB_TO_B(mp
, imap
.br_startoff
)),
2461 0, lastoffset
- offset
+ 1);
2466 error
= xfs_iowait(bp
);
2468 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
2469 mp
, bp
, XFS_BUF_ADDR(bp
));
2478 * xfs_free_file_space()
2479 * This routine frees disk space for the given file.
2481 * This routine is only called by xfs_change_file_space
2482 * for an UNRESVSP type call.
2490 xfs_free_file_space(
2498 xfs_fileoff_t endoffset_fsb
;
2500 xfs_fsblock_t firstfsb
;
2501 xfs_bmap_free_t free_list
;
2502 xfs_bmbt_irec_t imap
;
2510 xfs_fileoff_t startoffset_fsb
;
2512 int need_iolock
= 1;
2516 trace_xfs_free_file_space(ip
);
2518 error
= xfs_qm_dqattach(ip
, 0);
2523 if (len
<= 0) /* if nothing being freed */
2525 rt
= XFS_IS_REALTIME_INODE(ip
);
2526 startoffset_fsb
= XFS_B_TO_FSB(mp
, offset
);
2527 endoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
+ len
);
2529 if (attr_flags
& XFS_ATTR_NOLOCK
)
2532 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
2533 /* wait for the completion of any pending DIOs */
2537 rounding
= max_t(uint
, 1 << mp
->m_sb
.sb_blocklog
, PAGE_CACHE_SIZE
);
2538 ioffset
= offset
& ~(rounding
- 1);
2540 if (VN_CACHED(VFS_I(ip
)) != 0) {
2541 error
= xfs_flushinval_pages(ip
, ioffset
, -1, FI_REMAPF_LOCKED
);
2543 goto out_unlock_iolock
;
2547 * Need to zero the stuff we're not freeing, on disk.
2548 * If it's a realtime file & can't use unwritten extents then we
2549 * actually need to zero the extent edges. Otherwise xfs_bunmapi
2550 * will take care of it for us.
2552 if (rt
&& !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
2554 error
= xfs_bmapi(NULL
, ip
, startoffset_fsb
,
2555 1, 0, NULL
, 0, &imap
, &nimap
, NULL
);
2557 goto out_unlock_iolock
;
2558 ASSERT(nimap
== 0 || nimap
== 1);
2559 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
2562 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
2563 block
= imap
.br_startblock
;
2564 mod
= do_div(block
, mp
->m_sb
.sb_rextsize
);
2566 startoffset_fsb
+= mp
->m_sb
.sb_rextsize
- mod
;
2569 error
= xfs_bmapi(NULL
, ip
, endoffset_fsb
- 1,
2570 1, 0, NULL
, 0, &imap
, &nimap
, NULL
);
2572 goto out_unlock_iolock
;
2573 ASSERT(nimap
== 0 || nimap
== 1);
2574 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
2575 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
2577 if (mod
&& (mod
!= mp
->m_sb
.sb_rextsize
))
2578 endoffset_fsb
-= mod
;
2581 if ((done
= (endoffset_fsb
<= startoffset_fsb
)))
2583 * One contiguous piece to clear
2585 error
= xfs_zero_remaining_bytes(ip
, offset
, offset
+ len
- 1);
2588 * Some full blocks, possibly two pieces to clear
2590 if (offset
< XFS_FSB_TO_B(mp
, startoffset_fsb
))
2591 error
= xfs_zero_remaining_bytes(ip
, offset
,
2592 XFS_FSB_TO_B(mp
, startoffset_fsb
) - 1);
2594 XFS_FSB_TO_B(mp
, endoffset_fsb
) < offset
+ len
)
2595 error
= xfs_zero_remaining_bytes(ip
,
2596 XFS_FSB_TO_B(mp
, endoffset_fsb
),
2601 * free file space until done or until there is an error
2603 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
2604 while (!error
&& !done
) {
2607 * allocate and setup the transaction. Allow this
2608 * transaction to dip into the reserve blocks to ensure
2609 * the freeing of the space succeeds at ENOSPC.
2611 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
2612 tp
->t_flags
|= XFS_TRANS_RESERVE
;
2613 error
= xfs_trans_reserve(tp
,
2615 XFS_WRITE_LOG_RES(mp
),
2617 XFS_TRANS_PERM_LOG_RES
,
2618 XFS_WRITE_LOG_COUNT
);
2621 * check for running out of space
2625 * Free the transaction structure.
2627 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
2628 xfs_trans_cancel(tp
, 0);
2631 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2632 error
= xfs_trans_reserve_quota(tp
, mp
,
2633 ip
->i_udquot
, ip
->i_gdquot
,
2634 resblks
, 0, XFS_QMOPT_RES_REGBLKS
);
2638 xfs_trans_ijoin(tp
, ip
);
2641 * issue the bunmapi() call to free the blocks
2643 xfs_bmap_init(&free_list
, &firstfsb
);
2644 error
= xfs_bunmapi(tp
, ip
, startoffset_fsb
,
2645 endoffset_fsb
- startoffset_fsb
,
2646 0, 2, &firstfsb
, &free_list
, &done
);
2652 * complete the transaction
2654 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2659 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2660 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
2665 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
2669 xfs_bmap_cancel(&free_list
);
2671 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
2672 xfs_iunlock(ip
, need_iolock
? (XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
) :
2678 * xfs_change_file_space()
2679 * This routine allocates or frees disk space for the given file.
2680 * The user specified parameters are checked for alignment and size
2689 xfs_change_file_space(
2696 xfs_mount_t
*mp
= ip
->i_mount
;
2701 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;
2749 case XFS_IOC_RESVSP
:
2750 case XFS_IOC_RESVSP64
:
2751 error
= xfs_alloc_file_space(ip
, startoffset
, bf
->l_len
,
2758 case XFS_IOC_UNRESVSP
:
2759 case XFS_IOC_UNRESVSP64
:
2760 if ((error
= xfs_free_file_space(ip
, startoffset
, bf
->l_len
,
2765 case XFS_IOC_ALLOCSP
:
2766 case XFS_IOC_ALLOCSP64
:
2767 case XFS_IOC_FREESP
:
2768 case XFS_IOC_FREESP64
:
2769 if (startoffset
> fsize
) {
2770 error
= xfs_alloc_file_space(ip
, fsize
,
2771 startoffset
- fsize
, 0, attr_flags
);
2776 iattr
.ia_valid
= ATTR_SIZE
;
2777 iattr
.ia_size
= startoffset
;
2779 error
= xfs_setattr(ip
, &iattr
, attr_flags
);
2789 return XFS_ERROR(EINVAL
);
2793 * update the inode timestamp, mode, and prealloc flag bits
2795 tp
= xfs_trans_alloc(mp
, XFS_TRANS_WRITEID
);
2797 if ((error
= xfs_trans_reserve(tp
, 0, XFS_WRITEID_LOG_RES(mp
),
2800 xfs_trans_cancel(tp
, 0);
2804 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2806 xfs_trans_ijoin(tp
, ip
);
2808 if ((attr_flags
& XFS_ATTR_DMI
) == 0) {
2809 ip
->i_d
.di_mode
&= ~S_ISUID
;
2812 * Note that we don't have to worry about mandatory
2813 * file locking being disabled here because we only
2814 * clear the S_ISGID bit if the Group execute bit is
2815 * on, but if it was on then mandatory locking wouldn't
2816 * have been enabled.
2818 if (ip
->i_d
.di_mode
& S_IXGRP
)
2819 ip
->i_d
.di_mode
&= ~S_ISGID
;
2821 xfs_ichgtime(ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2824 ip
->i_d
.di_flags
|= XFS_DIFLAG_PREALLOC
;
2825 else if (clrprealloc
)
2826 ip
->i_d
.di_flags
&= ~XFS_DIFLAG_PREALLOC
;
2828 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2829 xfs_trans_set_sync(tp
);
2831 error
= xfs_trans_commit(tp
, 0);
2833 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);