2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "xfs_types.h"
25 #include "xfs_trans.h"
29 #include "xfs_dmapi.h"
30 #include "xfs_mount.h"
31 #include "xfs_da_btree.h"
32 #include "xfs_bmap_btree.h"
33 #include "xfs_alloc_btree.h"
34 #include "xfs_ialloc_btree.h"
35 #include "xfs_dir2_sf.h"
36 #include "xfs_attr_sf.h"
37 #include "xfs_dinode.h"
38 #include "xfs_inode.h"
39 #include "xfs_inode_item.h"
40 #include "xfs_itable.h"
41 #include "xfs_btree.h"
42 #include "xfs_ialloc.h"
43 #include "xfs_alloc.h"
47 #include "xfs_error.h"
48 #include "xfs_quota.h"
49 #include "xfs_utils.h"
50 #include "xfs_rtalloc.h"
51 #include "xfs_trans_space.h"
52 #include "xfs_log_priv.h"
53 #include "xfs_filestream.h"
54 #include "xfs_vnodeops.h"
62 xfs_mount_t
*mp
= ip
->i_mount
;
63 struct inode
*inode
= VFS_I(ip
);
64 int mask
= iattr
->ia_valid
;
72 struct xfs_dquot
*udqp
, *gdqp
, *olddquot1
, *olddquot2
;
78 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
79 return XFS_ERROR(EROFS
);
81 if (XFS_FORCED_SHUTDOWN(mp
))
82 return XFS_ERROR(EIO
);
84 olddquot1
= olddquot2
= NULL
;
88 * If disk quotas is on, we make sure that the dquots do exist on disk,
89 * before we start any other transactions. Trying to do this later
90 * is messy. We don't care to take a readlock to look at the ids
91 * in inode here, because we can't hold it across the trans_reserve.
92 * If the IDs do change before we take the ilock, we're covered
93 * because the i_*dquot fields will get updated anyway.
95 if (XFS_IS_QUOTA_ON(mp
) && (mask
& (ATTR_UID
|ATTR_GID
))) {
98 if ((mask
& ATTR_UID
) && XFS_IS_UQUOTA_ON(mp
)) {
100 qflags
|= XFS_QMOPT_UQUOTA
;
102 uid
= ip
->i_d
.di_uid
;
104 if ((mask
& ATTR_GID
) && XFS_IS_GQUOTA_ON(mp
)) {
106 qflags
|= XFS_QMOPT_GQUOTA
;
108 gid
= ip
->i_d
.di_gid
;
112 * We take a reference when we initialize udqp and gdqp,
113 * so it is important that we never blindly double trip on
114 * the same variable. See xfs_create() for an example.
116 ASSERT(udqp
== NULL
);
117 ASSERT(gdqp
== NULL
);
118 code
= XFS_QM_DQVOPALLOC(mp
, ip
, uid
, gid
, ip
->i_d
.di_projid
,
119 qflags
, &udqp
, &gdqp
);
125 * For the other attributes, we acquire the inode lock and
126 * first do an error checking pass.
129 lock_flags
= XFS_ILOCK_EXCL
;
130 if (flags
& XFS_ATTR_NOLOCK
)
132 if (!(mask
& ATTR_SIZE
)) {
133 if ((mask
!= (ATTR_CTIME
|ATTR_ATIME
|ATTR_MTIME
)) ||
134 (mp
->m_flags
& XFS_MOUNT_WSYNC
)) {
135 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SETATTR_NOT_SIZE
);
137 if ((code
= xfs_trans_reserve(tp
, 0,
138 XFS_ICHANGE_LOG_RES(mp
), 0,
145 if (DM_EVENT_ENABLED(ip
, DM_EVENT_TRUNCATE
) &&
146 !(flags
& XFS_ATTR_DMI
)) {
147 int dmflags
= AT_DELAY_FLAG(flags
) | DM_SEM_FLAG_WR
;
148 code
= XFS_SEND_DATA(mp
, DM_EVENT_TRUNCATE
, ip
,
149 iattr
->ia_size
, 0, dmflags
, NULL
);
156 lock_flags
|= XFS_IOLOCK_EXCL
;
159 xfs_ilock(ip
, lock_flags
);
161 /* boolean: are we the file owner? */
162 file_owner
= (current_fsuid() == ip
->i_d
.di_uid
);
165 * Change various properties of a file.
166 * Only the owner or users with CAP_FOWNER
167 * capability may do these things.
169 if (mask
& (ATTR_MODE
|ATTR_UID
|ATTR_GID
)) {
171 * CAP_FOWNER overrides the following restrictions:
173 * The user ID of the calling process must be equal
174 * to the file owner ID, except in cases where the
175 * CAP_FSETID capability is applicable.
177 if (!file_owner
&& !capable(CAP_FOWNER
)) {
178 code
= XFS_ERROR(EPERM
);
183 * CAP_FSETID overrides the following restrictions:
185 * The effective user ID of the calling process shall match
186 * the file owner when setting the set-user-ID and
187 * set-group-ID bits on that file.
189 * The effective group ID or one of the supplementary group
190 * IDs of the calling process shall match the group owner of
191 * the file when setting the set-group-ID bit on that file
193 if (mask
& ATTR_MODE
) {
196 if ((iattr
->ia_mode
& S_ISUID
) && !file_owner
)
198 if ((iattr
->ia_mode
& S_ISGID
) &&
199 !in_group_p((gid_t
)ip
->i_d
.di_gid
))
202 /* Linux allows this, Irix doesn't. */
203 if ((iattr
->ia_mode
& S_ISVTX
) && !S_ISDIR(ip
->i_d
.di_mode
))
206 if (m
&& !capable(CAP_FSETID
))
207 iattr
->ia_mode
&= ~m
;
212 * Change file ownership. Must be the owner or privileged.
214 if (mask
& (ATTR_UID
|ATTR_GID
)) {
216 * These IDs could have changed since we last looked at them.
217 * But, we're assured that if the ownership did change
218 * while we didn't have the inode locked, inode's dquot(s)
219 * would have changed also.
221 iuid
= ip
->i_d
.di_uid
;
222 igid
= ip
->i_d
.di_gid
;
223 gid
= (mask
& ATTR_GID
) ? iattr
->ia_gid
: igid
;
224 uid
= (mask
& ATTR_UID
) ? iattr
->ia_uid
: iuid
;
227 * CAP_CHOWN overrides the following restrictions:
229 * If _POSIX_CHOWN_RESTRICTED is defined, this capability
230 * shall override the restriction that a process cannot
231 * change the user ID of a file it owns and the restriction
232 * that the group ID supplied to the chown() function
233 * shall be equal to either the group ID or one of the
234 * supplementary group IDs of the calling process.
237 (igid
!= gid
&& !in_group_p((gid_t
)gid
))) &&
238 !capable(CAP_CHOWN
)) {
239 code
= XFS_ERROR(EPERM
);
243 * Do a quota reservation only if uid/gid is actually
246 if ((XFS_IS_UQUOTA_ON(mp
) && iuid
!= uid
) ||
247 (XFS_IS_GQUOTA_ON(mp
) && igid
!= gid
)) {
249 code
= XFS_QM_DQVOPCHOWNRESV(mp
, tp
, ip
, udqp
, gdqp
,
250 capable(CAP_FOWNER
) ?
251 XFS_QMOPT_FORCE_RES
: 0);
252 if (code
) /* out of quota */
258 * Truncate file. Must have write permission and not be a directory.
260 if (mask
& ATTR_SIZE
) {
261 /* Short circuit the truncate case for zero length files */
262 if (iattr
->ia_size
== 0 &&
263 ip
->i_size
== 0 && ip
->i_d
.di_nextents
== 0) {
264 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
265 lock_flags
&= ~XFS_ILOCK_EXCL
;
266 if (mask
& ATTR_CTIME
)
267 xfs_ichgtime(ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
272 if (S_ISDIR(ip
->i_d
.di_mode
)) {
273 code
= XFS_ERROR(EISDIR
);
275 } else if (!S_ISREG(ip
->i_d
.di_mode
)) {
276 code
= XFS_ERROR(EINVAL
);
280 * Make sure that the dquots are attached to the inode.
282 if ((code
= XFS_QM_DQATTACH(mp
, ip
, XFS_QMOPT_ILOCKED
)))
287 * Change file access or modified times.
289 if (mask
& (ATTR_ATIME
|ATTR_MTIME
)) {
291 if ((mask
& (ATTR_MTIME_SET
|ATTR_ATIME_SET
)) &&
292 !capable(CAP_FOWNER
)) {
293 code
= XFS_ERROR(EPERM
);
300 * Now we can make the changes. Before we join the inode
301 * to the transaction, if ATTR_SIZE is set then take care of
302 * the part of the truncation that must be done without the
303 * inode lock. This needs to be done before joining the inode
304 * to the transaction, because the inode cannot be unlocked
305 * once it is a part of the transaction.
307 if (mask
& ATTR_SIZE
) {
309 if (iattr
->ia_size
> ip
->i_size
) {
311 * Do the first part of growing a file: zero any data
312 * in the last block that is beyond the old EOF. We
313 * need to do this before the inode is joined to the
314 * transaction to modify the i_size.
316 code
= xfs_zero_eof(ip
, iattr
->ia_size
, ip
->i_size
);
318 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
321 * We are going to log the inode size change in this
322 * transaction so any previous writes that are beyond the on
323 * disk EOF and the new EOF that have not been written out need
324 * to be written here. If we do not write the data out, we
325 * expose ourselves to the null files problem.
327 * Only flush from the on disk size to the smaller of the in
328 * memory file size or the new size as that's the range we
329 * really care about here and prevents waiting for other data
330 * not within the range we care about here.
333 ip
->i_size
!= ip
->i_d
.di_size
&&
334 iattr
->ia_size
> ip
->i_d
.di_size
) {
335 code
= xfs_flush_pages(ip
,
336 ip
->i_d
.di_size
, iattr
->ia_size
,
337 XFS_B_ASYNC
, FI_NONE
);
340 /* wait for all I/O to complete */
344 code
= xfs_itruncate_data(ip
, iattr
->ia_size
);
347 lock_flags
&= ~XFS_ILOCK_EXCL
;
348 ASSERT(lock_flags
== XFS_IOLOCK_EXCL
);
351 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SETATTR_SIZE
);
352 if ((code
= xfs_trans_reserve(tp
, 0,
353 XFS_ITRUNCATE_LOG_RES(mp
), 0,
354 XFS_TRANS_PERM_LOG_RES
,
355 XFS_ITRUNCATE_LOG_COUNT
))) {
356 xfs_trans_cancel(tp
, 0);
358 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
361 commit_flags
= XFS_TRANS_RELEASE_LOG_RES
;
362 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
366 xfs_trans_ijoin(tp
, ip
, lock_flags
);
367 xfs_trans_ihold(tp
, ip
);
371 * Truncate file. Must have write permission and not be a directory.
373 if (mask
& ATTR_SIZE
) {
375 * Only change the c/mtime if we are changing the size
376 * or we are explicitly asked to change it. This handles
377 * the semantic difference between truncate() and ftruncate()
378 * as implemented in the VFS.
380 if (iattr
->ia_size
!= ip
->i_size
|| (mask
& ATTR_CTIME
))
381 timeflags
|= XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
;
383 if (iattr
->ia_size
> ip
->i_size
) {
384 ip
->i_d
.di_size
= iattr
->ia_size
;
385 ip
->i_size
= iattr
->ia_size
;
386 if (!(flags
& XFS_ATTR_DMI
))
387 xfs_ichgtime(ip
, XFS_ICHGTIME_CHG
);
388 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
389 } else if (iattr
->ia_size
<= ip
->i_size
||
390 (iattr
->ia_size
== 0 && ip
->i_d
.di_nextents
)) {
392 * signal a sync transaction unless
393 * we're truncating an already unlinked
394 * file on a wsync filesystem
396 code
= xfs_itruncate_finish(&tp
, ip
, iattr
->ia_size
,
398 ((ip
->i_d
.di_nlink
!= 0 ||
399 !(mp
->m_flags
& XFS_MOUNT_WSYNC
))
404 * Truncated "down", so we're removing references
405 * to old data here - if we now delay flushing for
406 * a long time, we expose ourselves unduly to the
407 * notorious NULL files problem. So, we mark this
408 * vnode and flush it when the file is closed, and
409 * do not wait the usual (long) time for writeout.
411 xfs_iflags_set(ip
, XFS_ITRUNCATED
);
416 * Change file access modes.
418 if (mask
& ATTR_MODE
) {
419 ip
->i_d
.di_mode
&= S_IFMT
;
420 ip
->i_d
.di_mode
|= iattr
->ia_mode
& ~S_IFMT
;
422 inode
->i_mode
&= S_IFMT
;
423 inode
->i_mode
|= iattr
->ia_mode
& ~S_IFMT
;
425 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
426 timeflags
|= XFS_ICHGTIME_CHG
;
430 * Change file ownership. Must be the owner or privileged.
432 if (mask
& (ATTR_UID
|ATTR_GID
)) {
434 * CAP_FSETID overrides the following restrictions:
436 * The set-user-ID and set-group-ID bits of a file will be
437 * cleared upon successful return from chown()
439 if ((ip
->i_d
.di_mode
& (S_ISUID
|S_ISGID
)) &&
440 !capable(CAP_FSETID
)) {
441 ip
->i_d
.di_mode
&= ~(S_ISUID
|S_ISGID
);
445 * Change the ownerships and register quota modifications
446 * in the transaction.
449 if (XFS_IS_UQUOTA_ON(mp
)) {
450 ASSERT(mask
& ATTR_UID
);
452 olddquot1
= XFS_QM_DQVOPCHOWN(mp
, tp
, ip
,
453 &ip
->i_udquot
, udqp
);
455 ip
->i_d
.di_uid
= uid
;
459 if (XFS_IS_GQUOTA_ON(mp
)) {
460 ASSERT(!XFS_IS_PQUOTA_ON(mp
));
461 ASSERT(mask
& ATTR_GID
);
463 olddquot2
= XFS_QM_DQVOPCHOWN(mp
, tp
, ip
,
464 &ip
->i_gdquot
, gdqp
);
466 ip
->i_d
.di_gid
= gid
;
470 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
471 timeflags
|= XFS_ICHGTIME_CHG
;
476 * Change file access or modified times.
478 if (mask
& (ATTR_ATIME
|ATTR_MTIME
)) {
479 if (mask
& ATTR_ATIME
) {
480 inode
->i_atime
= iattr
->ia_atime
;
481 ip
->i_d
.di_atime
.t_sec
= iattr
->ia_atime
.tv_sec
;
482 ip
->i_d
.di_atime
.t_nsec
= iattr
->ia_atime
.tv_nsec
;
483 ip
->i_update_core
= 1;
485 if (mask
& ATTR_MTIME
) {
486 inode
->i_mtime
= iattr
->ia_mtime
;
487 ip
->i_d
.di_mtime
.t_sec
= iattr
->ia_mtime
.tv_sec
;
488 ip
->i_d
.di_mtime
.t_nsec
= iattr
->ia_mtime
.tv_nsec
;
489 timeflags
&= ~XFS_ICHGTIME_MOD
;
490 timeflags
|= XFS_ICHGTIME_CHG
;
492 if (tp
&& (mask
& (ATTR_MTIME_SET
|ATTR_ATIME_SET
)))
493 xfs_trans_log_inode (tp
, ip
, XFS_ILOG_CORE
);
497 * Change file inode change time only if ATTR_CTIME set
498 * AND we have been called by a DMI function.
501 if ((flags
& XFS_ATTR_DMI
) && (mask
& ATTR_CTIME
)) {
502 inode
->i_ctime
= iattr
->ia_ctime
;
503 ip
->i_d
.di_ctime
.t_sec
= iattr
->ia_ctime
.tv_sec
;
504 ip
->i_d
.di_ctime
.t_nsec
= iattr
->ia_ctime
.tv_nsec
;
505 ip
->i_update_core
= 1;
506 timeflags
&= ~XFS_ICHGTIME_CHG
;
510 * Send out timestamp changes that need to be set to the
511 * current time. Not done when called by a DMI function.
513 if (timeflags
&& !(flags
& XFS_ATTR_DMI
))
514 xfs_ichgtime(ip
, timeflags
);
516 XFS_STATS_INC(xs_ig_attrchg
);
519 * If this is a synchronous mount, make sure that the
520 * transaction goes to disk before returning to the user.
521 * This is slightly sub-optimal in that truncates require
522 * two sync transactions instead of one for wsync filesystems.
523 * One for the truncate and one for the timestamps since we
524 * don't want to change the timestamps unless we're sure the
525 * truncate worked. Truncates are less than 1% of the laddis
526 * mix so this probably isn't worth the trouble to optimize.
530 if (mp
->m_flags
& XFS_MOUNT_WSYNC
)
531 xfs_trans_set_sync(tp
);
533 code
= xfs_trans_commit(tp
, commit_flags
);
536 xfs_iunlock(ip
, lock_flags
);
539 * Release any dquot(s) the inode had kept before chown.
541 XFS_QM_DQRELE(mp
, olddquot1
);
542 XFS_QM_DQRELE(mp
, olddquot2
);
543 XFS_QM_DQRELE(mp
, udqp
);
544 XFS_QM_DQRELE(mp
, gdqp
);
550 if (DM_EVENT_ENABLED(ip
, DM_EVENT_ATTRIBUTE
) &&
551 !(flags
& XFS_ATTR_DMI
)) {
552 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_ATTRIBUTE
, ip
, DM_RIGHT_NULL
,
553 NULL
, DM_RIGHT_NULL
, NULL
, NULL
,
554 0, 0, AT_DELAY_FLAG(flags
));
559 commit_flags
|= XFS_TRANS_ABORT
;
562 XFS_QM_DQRELE(mp
, udqp
);
563 XFS_QM_DQRELE(mp
, gdqp
);
565 xfs_trans_cancel(tp
, commit_flags
);
567 if (lock_flags
!= 0) {
568 xfs_iunlock(ip
, lock_flags
);
574 * The maximum pathlen is 1024 bytes. Since the minimum file system
575 * blocksize is 512 bytes, we can get a max of 2 extents back from
578 #define SYMLINK_MAPS 2
585 xfs_mount_t
*mp
= ip
->i_mount
;
586 int pathlen
= ip
->i_d
.di_size
;
587 int nmaps
= SYMLINK_MAPS
;
588 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
595 error
= xfs_bmapi(NULL
, ip
, 0, XFS_B_TO_FSB(mp
, pathlen
), 0, NULL
, 0,
596 mval
, &nmaps
, NULL
, NULL
);
600 for (n
= 0; n
< nmaps
; n
++) {
601 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
602 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
604 bp
= xfs_buf_read(mp
->m_ddev_targp
, d
, BTOBB(byte_cnt
), 0);
605 error
= XFS_BUF_GETERROR(bp
);
607 xfs_ioerror_alert("xfs_readlink",
608 ip
->i_mount
, bp
, XFS_BUF_ADDR(bp
));
612 if (pathlen
< byte_cnt
)
616 memcpy(link
, XFS_BUF_PTR(bp
), byte_cnt
);
620 link
[ip
->i_d
.di_size
] = '\0';
632 xfs_mount_t
*mp
= ip
->i_mount
;
636 xfs_itrace_entry(ip
);
638 if (XFS_FORCED_SHUTDOWN(mp
))
639 return XFS_ERROR(EIO
);
641 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
643 ASSERT((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
);
644 ASSERT(ip
->i_d
.di_size
<= MAXPATHLEN
);
646 pathlen
= ip
->i_d
.di_size
;
650 if (ip
->i_df
.if_flags
& XFS_IFINLINE
) {
651 memcpy(link
, ip
->i_df
.if_u1
.if_data
, pathlen
);
652 link
[pathlen
] = '\0';
654 error
= xfs_readlink_bmap(ip
, link
);
658 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
665 * This is called to sync the inode and its data out to disk. We need to hold
666 * the I/O lock while flushing the data, and the inode lock while flushing the
667 * inode. The inode lock CANNOT be held while flushing the data, so acquire
668 * after we're done with that.
676 int log_flushed
= 0, changed
= 1;
678 xfs_itrace_entry(ip
);
680 if (XFS_FORCED_SHUTDOWN(ip
->i_mount
))
681 return XFS_ERROR(EIO
);
683 /* capture size updates in I/O completion before writing the inode. */
684 error
= xfs_wait_on_pages(ip
, 0, -1);
686 return XFS_ERROR(error
);
689 * We always need to make sure that the required inode state is safe on
690 * disk. The vnode might be clean but we still might need to force the
691 * log because of committed transactions that haven't hit the disk yet.
692 * Likewise, there could be unflushed non-transactional changes to the
693 * inode core that have to go to disk and this requires us to issue
694 * a synchronous transaction to capture these changes correctly.
696 * This code relies on the assumption that if the update_* fields
697 * of the inode are clear and the inode is unpinned then it is clean
698 * and no action is required.
700 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
702 if (!(ip
->i_update_size
|| ip
->i_update_core
)) {
704 * Timestamps/size haven't changed since last inode flush or
705 * inode transaction commit. That means either nothing got
706 * written or a transaction committed which caught the updates.
707 * If the latter happened and the transaction hasn't hit the
708 * disk yet, the inode will be still be pinned. If it is,
712 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
714 if (xfs_ipincount(ip
)) {
715 error
= _xfs_log_force(ip
->i_mount
, (xfs_lsn_t
)0,
716 XFS_LOG_FORCE
| XFS_LOG_SYNC
,
720 * If the inode is not pinned and nothing has changed
721 * we don't need to flush the cache.
727 * Kick off a transaction to log the inode core to get the
728 * updates. The sync transaction will also force the log.
730 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
731 tp
= xfs_trans_alloc(ip
->i_mount
, XFS_TRANS_FSYNC_TS
);
732 error
= xfs_trans_reserve(tp
, 0,
733 XFS_FSYNC_TS_LOG_RES(ip
->i_mount
), 0, 0, 0);
735 xfs_trans_cancel(tp
, 0);
738 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
741 * Note - it's possible that we might have pushed ourselves out
742 * of the way during trans_reserve which would flush the inode.
743 * But there's no guarantee that the inode buffer has actually
744 * gone out yet (it's delwri). Plus the buffer could be pinned
745 * anyway if it's part of an inode in another recent
746 * transaction. So we play it safe and fire off the
747 * transaction anyway.
749 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
750 xfs_trans_ihold(tp
, ip
);
751 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
752 xfs_trans_set_sync(tp
);
753 error
= _xfs_trans_commit(tp
, 0, &log_flushed
);
755 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
758 if ((ip
->i_mount
->m_flags
& XFS_MOUNT_BARRIER
) && changed
) {
760 * If the log write didn't issue an ordered tag we need
761 * to flush the disk cache for the data device now.
764 xfs_blkdev_issue_flush(ip
->i_mount
->m_ddev_targp
);
767 * If this inode is on the RT dev we need to flush that
770 if (XFS_IS_REALTIME_INODE(ip
))
771 xfs_blkdev_issue_flush(ip
->i_mount
->m_rtdev_targp
);
778 * This is called by xfs_inactive to free any blocks beyond eof
779 * when the link count isn't zero and by xfs_dm_punch_hole() when
780 * punching a hole to EOF.
790 xfs_fileoff_t end_fsb
;
791 xfs_fileoff_t last_fsb
;
792 xfs_filblks_t map_len
;
794 xfs_bmbt_irec_t imap
;
795 int use_iolock
= (flags
& XFS_FREE_EOF_LOCK
);
798 * Figure out if there are any blocks beyond the end
799 * of the file. If not, then there is nothing to do.
801 end_fsb
= XFS_B_TO_FSB(mp
, ((xfs_ufsize_t
)ip
->i_size
));
802 last_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_MAXIOFFSET(mp
));
803 map_len
= last_fsb
- end_fsb
;
808 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
809 error
= xfs_bmapi(NULL
, ip
, end_fsb
, map_len
, 0,
810 NULL
, 0, &imap
, &nimaps
, NULL
, NULL
);
811 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
813 if (!error
&& (nimaps
!= 0) &&
814 (imap
.br_startblock
!= HOLESTARTBLOCK
||
815 ip
->i_delayed_blks
)) {
817 * Attach the dquots to the inode up front.
819 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
823 * There are blocks after the end of file.
824 * Free them up now by truncating the file to
827 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
830 * Do the xfs_itruncate_start() call before
831 * reserving any log space because
832 * itruncate_start will call into the buffer
834 * do that within a transaction.
837 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
838 error
= xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
,
841 xfs_trans_cancel(tp
, 0);
843 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
847 error
= xfs_trans_reserve(tp
, 0,
848 XFS_ITRUNCATE_LOG_RES(mp
),
849 0, XFS_TRANS_PERM_LOG_RES
,
850 XFS_ITRUNCATE_LOG_COUNT
);
852 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
853 xfs_trans_cancel(tp
, 0);
854 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
858 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
859 xfs_trans_ijoin(tp
, ip
,
862 xfs_trans_ihold(tp
, ip
);
864 error
= xfs_itruncate_finish(&tp
, ip
,
869 * If we get an error at this point we
870 * simply don't bother truncating the file.
874 (XFS_TRANS_RELEASE_LOG_RES
|
877 error
= xfs_trans_commit(tp
,
878 XFS_TRANS_RELEASE_LOG_RES
);
880 xfs_iunlock(ip
, (use_iolock
? (XFS_IOLOCK_EXCL
|XFS_ILOCK_EXCL
)
887 * Free a symlink that has blocks associated with it.
890 xfs_inactive_symlink_rmt(
898 xfs_fsblock_t first_block
;
899 xfs_bmap_free_t free_list
;
902 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
910 ASSERT(ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
));
912 * We're freeing a symlink that has some
913 * blocks allocated to it. Free the
914 * blocks here. We know that we've got
915 * either 1 or 2 extents and that we can
916 * free them all in one bunmapi call.
918 ASSERT(ip
->i_d
.di_nextents
> 0 && ip
->i_d
.di_nextents
<= 2);
919 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
920 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
921 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
922 xfs_trans_cancel(tp
, 0);
927 * Lock the inode, fix the size, and join it to the transaction.
928 * Hold it so in the normal path, we still have it locked for
929 * the second transaction. In the error paths we need it
930 * held so the cancel won't rele it, see below.
932 xfs_ilock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
933 size
= (int)ip
->i_d
.di_size
;
935 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
936 xfs_trans_ihold(tp
, ip
);
937 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
939 * Find the block(s) so we can inval and unmap them.
942 XFS_BMAP_INIT(&free_list
, &first_block
);
943 nmaps
= ARRAY_SIZE(mval
);
944 if ((error
= xfs_bmapi(tp
, ip
, 0, XFS_B_TO_FSB(mp
, size
),
945 XFS_BMAPI_METADATA
, &first_block
, 0, mval
, &nmaps
,
949 * Invalidate the block(s).
951 for (i
= 0; i
< nmaps
; i
++) {
952 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
,
953 XFS_FSB_TO_DADDR(mp
, mval
[i
].br_startblock
),
954 XFS_FSB_TO_BB(mp
, mval
[i
].br_blockcount
), 0);
955 xfs_trans_binval(tp
, bp
);
958 * Unmap the dead block(s) to the free_list.
960 if ((error
= xfs_bunmapi(tp
, ip
, 0, size
, XFS_BMAPI_METADATA
, nmaps
,
961 &first_block
, &free_list
, NULL
, &done
)))
965 * Commit the first transaction. This logs the EFI and the inode.
967 if ((error
= xfs_bmap_finish(&tp
, &free_list
, &committed
)))
970 * The transaction must have been committed, since there were
971 * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
972 * The new tp has the extent freeing and EFDs.
976 * The first xact was committed, so add the inode to the new one.
977 * Mark it dirty so it will be logged and moved forward in the log as
978 * part of every commit.
980 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
981 xfs_trans_ihold(tp
, ip
);
982 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
984 * Get a new, empty transaction to return to our caller.
986 ntp
= xfs_trans_dup(tp
);
988 * Commit the transaction containing extent freeing and EFDs.
989 * If we get an error on the commit here or on the reserve below,
990 * we need to unlock the inode since the new transaction doesn't
991 * have the inode attached.
993 error
= xfs_trans_commit(tp
, 0);
996 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1000 * transaction commit worked ok so we can drop the extra ticket
1001 * reference that we gained in xfs_trans_dup()
1003 xfs_log_ticket_put(tp
->t_ticket
);
1006 * Remove the memory for extent descriptions (just bookkeeping).
1008 if (ip
->i_df
.if_bytes
)
1009 xfs_idata_realloc(ip
, -ip
->i_df
.if_bytes
, XFS_DATA_FORK
);
1010 ASSERT(ip
->i_df
.if_bytes
== 0);
1012 * Put an itruncate log reservation in the new transaction
1015 if ((error
= xfs_trans_reserve(tp
, 0, XFS_ITRUNCATE_LOG_RES(mp
), 0,
1016 XFS_TRANS_PERM_LOG_RES
, XFS_ITRUNCATE_LOG_COUNT
))) {
1017 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1021 * Return with the inode locked but not joined to the transaction.
1027 xfs_bmap_cancel(&free_list
);
1030 * Have to come here with the inode locked and either
1031 * (held and in the transaction) or (not in the transaction).
1032 * If the inode isn't held then cancel would iput it, but
1033 * that's wrong since this is inactive and the vnode ref
1034 * count is 0 already.
1035 * Cancel won't do anything to the inode if held, but it still
1036 * needs to be locked until the cancel is done, if it was
1037 * joined to the transaction.
1039 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1040 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1047 xfs_inactive_symlink_local(
1053 ASSERT(ip
->i_d
.di_size
<= XFS_IFORK_DSIZE(ip
));
1055 * We're freeing a symlink which fit into
1056 * the inode. Just free the memory used
1057 * to hold the old symlink.
1059 error
= xfs_trans_reserve(*tpp
, 0,
1060 XFS_ITRUNCATE_LOG_RES(ip
->i_mount
),
1061 0, XFS_TRANS_PERM_LOG_RES
,
1062 XFS_ITRUNCATE_LOG_COUNT
);
1065 xfs_trans_cancel(*tpp
, 0);
1069 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1072 * Zero length symlinks _can_ exist.
1074 if (ip
->i_df
.if_bytes
> 0) {
1075 xfs_idata_realloc(ip
,
1076 -(ip
->i_df
.if_bytes
),
1078 ASSERT(ip
->i_df
.if_bytes
== 0);
1092 ASSERT(xfs_isilocked(ip
, XFS_IOLOCK_EXCL
));
1095 ASSERT(ip
->i_d
.di_forkoff
!= 0);
1096 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1097 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1101 error
= xfs_attr_inactive(ip
);
1105 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1106 error
= xfs_trans_reserve(tp
, 0,
1107 XFS_IFREE_LOG_RES(mp
),
1108 0, XFS_TRANS_PERM_LOG_RES
,
1109 XFS_INACTIVE_LOG_COUNT
);
1113 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1114 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1115 xfs_trans_ihold(tp
, ip
);
1116 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
1118 ASSERT(ip
->i_d
.di_anextents
== 0);
1124 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1125 xfs_trans_cancel(tp
, 0);
1128 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1136 xfs_mount_t
*mp
= ip
->i_mount
;
1139 if (!S_ISREG(ip
->i_d
.di_mode
) || (ip
->i_d
.di_mode
== 0))
1142 /* If this is a read-only mount, don't do this (would generate I/O) */
1143 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
1146 if (!XFS_FORCED_SHUTDOWN(mp
)) {
1150 * If we are using filestreams, and we have an unlinked
1151 * file that we are processing the last close on, then nothing
1152 * will be able to reopen and write to this file. Purge this
1153 * inode from the filestreams cache so that it doesn't delay
1154 * teardown of the inode.
1156 if ((ip
->i_d
.di_nlink
== 0) && xfs_inode_is_filestream(ip
))
1157 xfs_filestream_deassociate(ip
);
1160 * If we previously truncated this file and removed old data
1161 * in the process, we want to initiate "early" writeout on
1162 * the last close. This is an attempt to combat the notorious
1163 * NULL files problem which is particularly noticable from a
1164 * truncate down, buffered (re-)write (delalloc), followed by
1165 * a crash. What we are effectively doing here is
1166 * significantly reducing the time window where we'd otherwise
1167 * be exposed to that problem.
1169 truncated
= xfs_iflags_test_and_clear(ip
, XFS_ITRUNCATED
);
1170 if (truncated
&& VN_DIRTY(VFS_I(ip
)) && ip
->i_delayed_blks
> 0)
1171 xfs_flush_pages(ip
, 0, -1, XFS_B_ASYNC
, FI_NONE
);
1174 if (ip
->i_d
.di_nlink
!= 0) {
1175 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
1176 ((ip
->i_size
> 0) || (VN_CACHED(VFS_I(ip
)) > 0 ||
1177 ip
->i_delayed_blks
> 0)) &&
1178 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
)) &&
1179 (!(ip
->i_d
.di_flags
&
1180 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)))) {
1181 error
= xfs_free_eofblocks(mp
, ip
, XFS_FREE_EOF_LOCK
);
1193 * This is called when the vnode reference count for the vnode
1194 * goes to zero. If the file has been unlinked, then it must
1195 * now be truncated. Also, we clear all of the read-ahead state
1196 * kept for the inode here since the file is now closed.
1202 xfs_bmap_free_t free_list
;
1203 xfs_fsblock_t first_block
;
1210 xfs_itrace_entry(ip
);
1213 * If the inode is already free, then there can be nothing
1216 if (ip
->i_d
.di_mode
== 0 || VN_BAD(VFS_I(ip
))) {
1217 ASSERT(ip
->i_df
.if_real_bytes
== 0);
1218 ASSERT(ip
->i_df
.if_broot_bytes
== 0);
1219 return VN_INACTIVE_CACHE
;
1223 * Only do a truncate if it's a regular file with
1224 * some actual space in it. It's OK to look at the
1225 * inode's fields without the lock because we're the
1226 * only one with a reference to the inode.
1228 truncate
= ((ip
->i_d
.di_nlink
== 0) &&
1229 ((ip
->i_d
.di_size
!= 0) || (ip
->i_size
!= 0) ||
1230 (ip
->i_d
.di_nextents
> 0) || (ip
->i_delayed_blks
> 0)) &&
1231 ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
));
1235 if (ip
->i_d
.di_nlink
== 0 && DM_EVENT_ENABLED(ip
, DM_EVENT_DESTROY
))
1236 XFS_SEND_DESTROY(mp
, ip
, DM_RIGHT_NULL
);
1240 /* If this is a read-only mount, don't do this (would generate I/O) */
1241 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
1244 if (ip
->i_d
.di_nlink
!= 0) {
1245 if ((((ip
->i_d
.di_mode
& S_IFMT
) == S_IFREG
) &&
1246 ((ip
->i_size
> 0) || (VN_CACHED(VFS_I(ip
)) > 0 ||
1247 ip
->i_delayed_blks
> 0)) &&
1248 (ip
->i_df
.if_flags
& XFS_IFEXTENTS
) &&
1249 (!(ip
->i_d
.di_flags
&
1250 (XFS_DIFLAG_PREALLOC
| XFS_DIFLAG_APPEND
)) ||
1251 (ip
->i_delayed_blks
!= 0)))) {
1252 error
= xfs_free_eofblocks(mp
, ip
, XFS_FREE_EOF_LOCK
);
1254 return VN_INACTIVE_CACHE
;
1259 ASSERT(ip
->i_d
.di_nlink
== 0);
1261 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
1262 return VN_INACTIVE_CACHE
;
1264 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
1267 * Do the xfs_itruncate_start() call before
1268 * reserving any log space because itruncate_start
1269 * will call into the buffer cache and we can't
1270 * do that within a transaction.
1272 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
1274 error
= xfs_itruncate_start(ip
, XFS_ITRUNC_DEFINITE
, 0);
1276 xfs_trans_cancel(tp
, 0);
1277 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1278 return VN_INACTIVE_CACHE
;
1281 error
= xfs_trans_reserve(tp
, 0,
1282 XFS_ITRUNCATE_LOG_RES(mp
),
1283 0, XFS_TRANS_PERM_LOG_RES
,
1284 XFS_ITRUNCATE_LOG_COUNT
);
1286 /* Don't call itruncate_cleanup */
1287 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1288 xfs_trans_cancel(tp
, 0);
1289 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1290 return VN_INACTIVE_CACHE
;
1293 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1294 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1295 xfs_trans_ihold(tp
, ip
);
1298 * normally, we have to run xfs_itruncate_finish sync.
1299 * But if filesystem is wsync and we're in the inactive
1300 * path, then we know that nlink == 0, and that the
1301 * xaction that made nlink == 0 is permanently committed
1302 * since xfs_remove runs as a synchronous transaction.
1304 error
= xfs_itruncate_finish(&tp
, ip
, 0, XFS_DATA_FORK
,
1305 (!(mp
->m_flags
& XFS_MOUNT_WSYNC
) ? 1 : 0));
1308 xfs_trans_cancel(tp
,
1309 XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1310 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1311 return VN_INACTIVE_CACHE
;
1313 } else if ((ip
->i_d
.di_mode
& S_IFMT
) == S_IFLNK
) {
1316 * If we get an error while cleaning up a
1317 * symlink we bail out.
1319 error
= (ip
->i_d
.di_size
> XFS_IFORK_DSIZE(ip
)) ?
1320 xfs_inactive_symlink_rmt(ip
, &tp
) :
1321 xfs_inactive_symlink_local(ip
, &tp
);
1325 return VN_INACTIVE_CACHE
;
1328 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1329 xfs_trans_ihold(tp
, ip
);
1331 error
= xfs_trans_reserve(tp
, 0,
1332 XFS_IFREE_LOG_RES(mp
),
1333 0, XFS_TRANS_PERM_LOG_RES
,
1334 XFS_INACTIVE_LOG_COUNT
);
1336 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
1337 xfs_trans_cancel(tp
, 0);
1338 return VN_INACTIVE_CACHE
;
1341 xfs_ilock(ip
, XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
);
1342 xfs_trans_ijoin(tp
, ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1343 xfs_trans_ihold(tp
, ip
);
1347 * If there are attributes associated with the file
1348 * then blow them away now. The code calls a routine
1349 * that recursively deconstructs the attribute fork.
1350 * We need to just commit the current transaction
1351 * because we can't use it for xfs_attr_inactive().
1353 if (ip
->i_d
.di_anextents
> 0) {
1354 error
= xfs_inactive_attrs(ip
, &tp
);
1356 * If we got an error, the transaction is already
1357 * cancelled, and the inode is unlocked. Just get out.
1360 return VN_INACTIVE_CACHE
;
1361 } else if (ip
->i_afp
) {
1362 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
1368 XFS_BMAP_INIT(&free_list
, &first_block
);
1369 error
= xfs_ifree(tp
, ip
, &free_list
);
1372 * If we fail to free the inode, shut down. The cancel
1373 * might do that, we need to make sure. Otherwise the
1374 * inode might be lost for a long time or forever.
1376 if (!XFS_FORCED_SHUTDOWN(mp
)) {
1378 "xfs_inactive: xfs_ifree() returned an error = %d on %s",
1379 error
, mp
->m_fsname
);
1380 xfs_force_shutdown(mp
, SHUTDOWN_META_IO_ERROR
);
1382 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
1385 * Credit the quota account(s). The inode is gone.
1387 XFS_TRANS_MOD_DQUOT_BYINO(mp
, tp
, ip
, XFS_TRANS_DQ_ICOUNT
, -1);
1390 * Just ignore errors at this point. There is nothing we can
1391 * do except to try to keep going. Make sure it's not a silent
1394 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1396 xfs_fs_cmn_err(CE_NOTE
, mp
, "xfs_inactive: "
1397 "xfs_bmap_finish() returned error %d", error
);
1398 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1400 xfs_fs_cmn_err(CE_NOTE
, mp
, "xfs_inactive: "
1401 "xfs_trans_commit() returned error %d", error
);
1404 * Release the dquots held by inode, if any.
1406 XFS_QM_DQDETACH(mp
, ip
);
1408 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
| XFS_ILOCK_EXCL
);
1411 return VN_INACTIVE_CACHE
;
1415 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
1416 * is allowed, otherwise it has to be an exact match. If a CI match is found,
1417 * ci_name->name will point to a the actual name (caller must free) or
1418 * will be set to NULL if an exact match is found.
1423 struct xfs_name
*name
,
1425 struct xfs_name
*ci_name
)
1431 xfs_itrace_entry(dp
);
1433 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
1434 return XFS_ERROR(EIO
);
1436 lock_mode
= xfs_ilock_map_shared(dp
);
1437 error
= xfs_dir_lookup(NULL
, dp
, name
, &inum
, ci_name
);
1438 xfs_iunlock_map_shared(dp
, lock_mode
);
1443 error
= xfs_iget(dp
->i_mount
, NULL
, inum
, 0, 0, ipp
, 0);
1447 xfs_itrace_ref(*ipp
);
1452 kmem_free(ci_name
->name
);
1461 struct xfs_name
*name
,
1467 xfs_mount_t
*mp
= dp
->i_mount
;
1471 xfs_bmap_free_t free_list
;
1472 xfs_fsblock_t first_block
;
1473 boolean_t unlock_dp_on_error
= B_FALSE
;
1474 int dm_event_sent
= 0;
1478 struct xfs_dquot
*udqp
, *gdqp
;
1482 xfs_itrace_entry(dp
);
1484 if (DM_EVENT_ENABLED(dp
, DM_EVENT_CREATE
)) {
1485 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_CREATE
,
1486 dp
, DM_RIGHT_NULL
, NULL
,
1487 DM_RIGHT_NULL
, name
->name
, NULL
,
1495 if (XFS_FORCED_SHUTDOWN(mp
))
1496 return XFS_ERROR(EIO
);
1498 /* Return through std_return after this point. */
1501 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
1502 prid
= dp
->i_d
.di_projid
;
1504 prid
= (xfs_prid_t
)dfltprid
;
1507 * Make sure that we have allocated dquot(s) on disk.
1509 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
1510 current_fsuid(), current_fsgid(), prid
,
1511 XFS_QMOPT_QUOTALL
|XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
1517 tp
= xfs_trans_alloc(mp
, XFS_TRANS_CREATE
);
1518 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1519 resblks
= XFS_CREATE_SPACE_RES(mp
, name
->len
);
1521 * Initially assume that the file does not exist and
1522 * reserve the resources for that case. If that is not
1523 * the case we'll drop the one we have and get a more
1524 * appropriate transaction later.
1526 error
= xfs_trans_reserve(tp
, resblks
, XFS_CREATE_LOG_RES(mp
), 0,
1527 XFS_TRANS_PERM_LOG_RES
, XFS_CREATE_LOG_COUNT
);
1528 if (error
== ENOSPC
) {
1530 error
= xfs_trans_reserve(tp
, 0, XFS_CREATE_LOG_RES(mp
), 0,
1531 XFS_TRANS_PERM_LOG_RES
, XFS_CREATE_LOG_COUNT
);
1538 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
1539 unlock_dp_on_error
= B_TRUE
;
1541 XFS_BMAP_INIT(&free_list
, &first_block
);
1546 * Reserve disk quota and the inode.
1548 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
1552 error
= xfs_dir_canenter(tp
, dp
, name
, resblks
);
1555 error
= xfs_dir_ialloc(&tp
, dp
, mode
, 1,
1556 rdev
, credp
, prid
, resblks
> 0,
1559 if (error
== ENOSPC
)
1566 * At this point, we've gotten a newly allocated inode.
1567 * It is locked (and joined to the transaction).
1570 ASSERT(xfs_isilocked(ip
, XFS_ILOCK_EXCL
));
1573 * Now we join the directory inode to the transaction. We do not do it
1574 * earlier because xfs_dir_ialloc might commit the previous transaction
1575 * (and release all the locks). An error from here on will result in
1576 * the transaction cancel unlocking dp so don't do it explicitly in the
1580 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
1581 unlock_dp_on_error
= B_FALSE
;
1583 error
= xfs_dir_createname(tp
, dp
, name
, ip
->i_ino
,
1584 &first_block
, &free_list
, resblks
?
1585 resblks
- XFS_IALLOC_SPACE_RES(mp
) : 0);
1587 ASSERT(error
!= ENOSPC
);
1590 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1591 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1594 * If this is a synchronous mount, make sure that the
1595 * create transaction goes to disk before returning to
1598 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
1599 xfs_trans_set_sync(tp
);
1605 * Attach the dquot(s) to the inodes and modify them incore.
1606 * These ids of the inode couldn't have changed since the new
1607 * inode has been locked ever since it was created.
1609 XFS_QM_DQVOPCREATE(mp
, tp
, ip
, udqp
, gdqp
);
1612 * xfs_trans_commit normally decrements the vnode ref count
1613 * when it unlocks the inode. Since we want to return the
1614 * vnode to the caller, we bump the vnode ref count now.
1618 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1620 xfs_bmap_cancel(&free_list
);
1624 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1631 XFS_QM_DQRELE(mp
, udqp
);
1632 XFS_QM_DQRELE(mp
, gdqp
);
1636 /* Fallthrough to std_return with error = 0 */
1639 if ((*ipp
|| (error
!= 0 && dm_event_sent
!= 0)) &&
1640 DM_EVENT_ENABLED(dp
, DM_EVENT_POSTCREATE
)) {
1641 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTCREATE
,
1644 DM_RIGHT_NULL
, name
->name
, NULL
,
1650 cancel_flags
|= XFS_TRANS_ABORT
;
1655 xfs_trans_cancel(tp
, cancel_flags
);
1657 XFS_QM_DQRELE(mp
, udqp
);
1658 XFS_QM_DQRELE(mp
, gdqp
);
1660 if (unlock_dp_on_error
)
1661 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
1667 * Wait until after the current transaction is aborted to
1668 * release the inode. This prevents recursive transactions
1669 * and deadlocks from xfs_inactive.
1671 cancel_flags
|= XFS_TRANS_ABORT
;
1672 xfs_trans_cancel(tp
, cancel_flags
);
1675 XFS_QM_DQRELE(mp
, udqp
);
1676 XFS_QM_DQRELE(mp
, gdqp
);
1683 int xfs_small_retries
;
1684 int xfs_middle_retries
;
1685 int xfs_lots_retries
;
1686 int xfs_lock_delays
;
1690 * Bump the subclass so xfs_lock_inodes() acquires each lock with
1694 xfs_lock_inumorder(int lock_mode
, int subclass
)
1696 if (lock_mode
& (XFS_IOLOCK_SHARED
|XFS_IOLOCK_EXCL
))
1697 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_IOLOCK_SHIFT
;
1698 if (lock_mode
& (XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
))
1699 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_ILOCK_SHIFT
;
1705 * The following routine will lock n inodes in exclusive mode.
1706 * We assume the caller calls us with the inodes in i_ino order.
1708 * We need to detect deadlock where an inode that we lock
1709 * is in the AIL and we start waiting for another inode that is locked
1710 * by a thread in a long running transaction (such as truncate). This can
1711 * result in deadlock since the long running trans might need to wait
1712 * for the inode we just locked in order to push the tail and free space
1721 int attempts
= 0, i
, j
, try_lock
;
1724 ASSERT(ips
&& (inodes
>= 2)); /* we need at least two */
1730 for (; i
< inodes
; i
++) {
1733 if (i
&& (ips
[i
] == ips
[i
-1])) /* Already locked */
1737 * If try_lock is not set yet, make sure all locked inodes
1738 * are not in the AIL.
1739 * If any are, set try_lock to be used later.
1743 for (j
= (i
- 1); j
>= 0 && !try_lock
; j
--) {
1744 lp
= (xfs_log_item_t
*)ips
[j
]->i_itemp
;
1745 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
1752 * If any of the previous locks we have locked is in the AIL,
1753 * we must TRY to get the second and subsequent locks. If
1754 * we can't get any, we must release all we have
1759 /* try_lock must be 0 if i is 0. */
1761 * try_lock means we have an inode locked
1762 * that is in the AIL.
1765 if (!xfs_ilock_nowait(ips
[i
], xfs_lock_inumorder(lock_mode
, i
))) {
1769 * Unlock all previous guys and try again.
1770 * xfs_iunlock will try to push the tail
1771 * if the inode is in the AIL.
1774 for(j
= i
- 1; j
>= 0; j
--) {
1777 * Check to see if we've already
1778 * unlocked this one.
1779 * Not the first one going back,
1780 * and the inode ptr is the same.
1782 if ((j
!= (i
- 1)) && ips
[j
] ==
1786 xfs_iunlock(ips
[j
], lock_mode
);
1789 if ((attempts
% 5) == 0) {
1790 delay(1); /* Don't just spin the CPU */
1800 xfs_ilock(ips
[i
], xfs_lock_inumorder(lock_mode
, i
));
1806 if (attempts
< 5) xfs_small_retries
++;
1807 else if (attempts
< 100) xfs_middle_retries
++;
1808 else xfs_lots_retries
++;
1816 * xfs_lock_two_inodes() can only be used to lock one type of lock
1817 * at a time - the iolock or the ilock, but not both at once. If
1818 * we lock both at once, lockdep will report false positives saying
1819 * we have violated locking orders.
1822 xfs_lock_two_inodes(
1831 if (lock_mode
& (XFS_IOLOCK_SHARED
|XFS_IOLOCK_EXCL
))
1832 ASSERT((lock_mode
& (XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
)) == 0);
1833 ASSERT(ip0
->i_ino
!= ip1
->i_ino
);
1835 if (ip0
->i_ino
> ip1
->i_ino
) {
1842 xfs_ilock(ip0
, xfs_lock_inumorder(lock_mode
, 0));
1845 * If the first lock we have locked is in the AIL, we must TRY to get
1846 * the second lock. If we can't get it, we must release the first one
1849 lp
= (xfs_log_item_t
*)ip0
->i_itemp
;
1850 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
1851 if (!xfs_ilock_nowait(ip1
, xfs_lock_inumorder(lock_mode
, 1))) {
1852 xfs_iunlock(ip0
, lock_mode
);
1853 if ((++attempts
% 5) == 0)
1854 delay(1); /* Don't just spin the CPU */
1858 xfs_ilock(ip1
, xfs_lock_inumorder(lock_mode
, 1));
1865 struct xfs_name
*name
,
1868 xfs_mount_t
*mp
= dp
->i_mount
;
1869 xfs_trans_t
*tp
= NULL
;
1870 int is_dir
= S_ISDIR(ip
->i_d
.di_mode
);
1872 xfs_bmap_free_t free_list
;
1873 xfs_fsblock_t first_block
;
1880 xfs_itrace_entry(dp
);
1881 xfs_itrace_entry(ip
);
1883 if (XFS_FORCED_SHUTDOWN(mp
))
1884 return XFS_ERROR(EIO
);
1886 if (DM_EVENT_ENABLED(dp
, DM_EVENT_REMOVE
)) {
1887 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_REMOVE
, dp
, DM_RIGHT_NULL
,
1888 NULL
, DM_RIGHT_NULL
, name
->name
, NULL
,
1889 ip
->i_d
.di_mode
, 0, 0);
1894 error
= XFS_QM_DQATTACH(mp
, dp
, 0);
1898 error
= XFS_QM_DQATTACH(mp
, ip
, 0);
1903 tp
= xfs_trans_alloc(mp
, XFS_TRANS_RMDIR
);
1904 log_count
= XFS_DEFAULT_LOG_COUNT
;
1906 tp
= xfs_trans_alloc(mp
, XFS_TRANS_REMOVE
);
1907 log_count
= XFS_REMOVE_LOG_COUNT
;
1909 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1912 * We try to get the real space reservation first,
1913 * allowing for directory btree deletion(s) implying
1914 * possible bmap insert(s). If we can't get the space
1915 * reservation then we use 0 instead, and avoid the bmap
1916 * btree insert(s) in the directory code by, if the bmap
1917 * insert tries to happen, instead trimming the LAST
1918 * block from the directory.
1920 resblks
= XFS_REMOVE_SPACE_RES(mp
);
1921 error
= xfs_trans_reserve(tp
, resblks
, XFS_REMOVE_LOG_RES(mp
), 0,
1922 XFS_TRANS_PERM_LOG_RES
, log_count
);
1923 if (error
== ENOSPC
) {
1925 error
= xfs_trans_reserve(tp
, 0, XFS_REMOVE_LOG_RES(mp
), 0,
1926 XFS_TRANS_PERM_LOG_RES
, log_count
);
1929 ASSERT(error
!= ENOSPC
);
1931 goto out_trans_cancel
;
1934 xfs_lock_two_inodes(dp
, ip
, XFS_ILOCK_EXCL
);
1937 * At this point, we've gotten both the directory and the entry
1941 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
1944 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1947 * If we're removing a directory perform some additional validation.
1950 ASSERT(ip
->i_d
.di_nlink
>= 2);
1951 if (ip
->i_d
.di_nlink
!= 2) {
1952 error
= XFS_ERROR(ENOTEMPTY
);
1953 goto out_trans_cancel
;
1955 if (!xfs_dir_isempty(ip
)) {
1956 error
= XFS_ERROR(ENOTEMPTY
);
1957 goto out_trans_cancel
;
1961 XFS_BMAP_INIT(&free_list
, &first_block
);
1962 error
= xfs_dir_removename(tp
, dp
, name
, ip
->i_ino
,
1963 &first_block
, &free_list
, resblks
);
1965 ASSERT(error
!= ENOENT
);
1966 goto out_bmap_cancel
;
1968 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1971 * Bump the in memory generation count on the parent
1972 * directory so that other can know that it has changed.
1975 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
1979 * Drop the link from ip's "..".
1981 error
= xfs_droplink(tp
, dp
);
1983 goto out_bmap_cancel
;
1986 * Drop the "." link from ip to self.
1988 error
= xfs_droplink(tp
, ip
);
1990 goto out_bmap_cancel
;
1993 * When removing a non-directory we need to log the parent
1994 * inode here for the i_gen update. For a directory this is
1995 * done implicitly by the xfs_droplink call for the ".." entry.
1997 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2001 * Drop the link from dp to ip.
2003 error
= xfs_droplink(tp
, ip
);
2005 goto out_bmap_cancel
;
2008 * Determine if this is the last link while
2009 * we are in the transaction.
2011 link_zero
= (ip
->i_d
.di_nlink
== 0);
2014 * If this is a synchronous mount, make sure that the
2015 * remove transaction goes to disk before returning to
2018 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
))
2019 xfs_trans_set_sync(tp
);
2021 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2023 goto out_bmap_cancel
;
2025 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2030 * If we are using filestreams, kill the stream association.
2031 * If the file is still open it may get a new one but that
2032 * will get killed on last close in xfs_close() so we don't
2033 * have to worry about that.
2035 if (!is_dir
&& link_zero
&& xfs_inode_is_filestream(ip
))
2036 xfs_filestream_deassociate(ip
);
2038 xfs_itrace_exit(ip
);
2039 xfs_itrace_exit(dp
);
2042 if (DM_EVENT_ENABLED(dp
, DM_EVENT_POSTREMOVE
)) {
2043 XFS_SEND_NAMESP(mp
, DM_EVENT_POSTREMOVE
, dp
, DM_RIGHT_NULL
,
2044 NULL
, DM_RIGHT_NULL
, name
->name
, NULL
,
2045 ip
->i_d
.di_mode
, error
, 0);
2051 xfs_bmap_cancel(&free_list
);
2052 cancel_flags
|= XFS_TRANS_ABORT
;
2054 xfs_trans_cancel(tp
, cancel_flags
);
2062 struct xfs_name
*target_name
)
2064 xfs_mount_t
*mp
= tdp
->i_mount
;
2067 xfs_bmap_free_t free_list
;
2068 xfs_fsblock_t first_block
;
2073 xfs_itrace_entry(tdp
);
2074 xfs_itrace_entry(sip
);
2076 ASSERT(!S_ISDIR(sip
->i_d
.di_mode
));
2078 if (XFS_FORCED_SHUTDOWN(mp
))
2079 return XFS_ERROR(EIO
);
2081 if (DM_EVENT_ENABLED(tdp
, DM_EVENT_LINK
)) {
2082 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_LINK
,
2085 target_name
->name
, NULL
, 0, 0, 0);
2090 /* Return through std_return after this point. */
2092 error
= XFS_QM_DQATTACH(mp
, sip
, 0);
2093 if (!error
&& sip
!= tdp
)
2094 error
= XFS_QM_DQATTACH(mp
, tdp
, 0);
2098 tp
= xfs_trans_alloc(mp
, XFS_TRANS_LINK
);
2099 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2100 resblks
= XFS_LINK_SPACE_RES(mp
, target_name
->len
);
2101 error
= xfs_trans_reserve(tp
, resblks
, XFS_LINK_LOG_RES(mp
), 0,
2102 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
2103 if (error
== ENOSPC
) {
2105 error
= xfs_trans_reserve(tp
, 0, XFS_LINK_LOG_RES(mp
), 0,
2106 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
2113 xfs_lock_two_inodes(sip
, tdp
, XFS_ILOCK_EXCL
);
2116 * Increment vnode ref counts since xfs_trans_commit &
2117 * xfs_trans_cancel will both unlock the inodes and
2118 * decrement the associated ref counts.
2122 xfs_trans_ijoin(tp
, sip
, XFS_ILOCK_EXCL
);
2123 xfs_trans_ijoin(tp
, tdp
, XFS_ILOCK_EXCL
);
2126 * If the source has too many links, we can't make any more to it.
2128 if (sip
->i_d
.di_nlink
>= XFS_MAXLINK
) {
2129 error
= XFS_ERROR(EMLINK
);
2134 * If we are using project inheritance, we only allow hard link
2135 * creation in our tree when the project IDs are the same; else
2136 * the tree quota mechanism could be circumvented.
2138 if (unlikely((tdp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
) &&
2139 (tdp
->i_d
.di_projid
!= sip
->i_d
.di_projid
))) {
2140 error
= XFS_ERROR(EXDEV
);
2144 error
= xfs_dir_canenter(tp
, tdp
, target_name
, resblks
);
2148 XFS_BMAP_INIT(&free_list
, &first_block
);
2150 error
= xfs_dir_createname(tp
, tdp
, target_name
, sip
->i_ino
,
2151 &first_block
, &free_list
, resblks
);
2154 xfs_ichgtime(tdp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2156 xfs_trans_log_inode(tp
, tdp
, XFS_ILOG_CORE
);
2158 error
= xfs_bumplink(tp
, sip
);
2163 * If this is a synchronous mount, make sure that the
2164 * link transaction goes to disk before returning to
2167 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2168 xfs_trans_set_sync(tp
);
2171 error
= xfs_bmap_finish (&tp
, &free_list
, &committed
);
2173 xfs_bmap_cancel(&free_list
);
2177 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2181 /* Fall through to std_return with error = 0. */
2183 if (DM_EVENT_ENABLED(sip
, DM_EVENT_POSTLINK
)) {
2184 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTLINK
,
2187 target_name
->name
, NULL
, 0, error
, 0);
2192 cancel_flags
|= XFS_TRANS_ABORT
;
2196 xfs_trans_cancel(tp
, cancel_flags
);
2204 struct xfs_name
*dir_name
,
2209 xfs_mount_t
*mp
= dp
->i_mount
;
2210 xfs_inode_t
*cdp
; /* inode of created dir */
2215 xfs_bmap_free_t free_list
;
2216 xfs_fsblock_t first_block
;
2217 boolean_t unlock_dp_on_error
= B_FALSE
;
2218 boolean_t created
= B_FALSE
;
2219 int dm_event_sent
= 0;
2221 struct xfs_dquot
*udqp
, *gdqp
;
2224 if (XFS_FORCED_SHUTDOWN(mp
))
2225 return XFS_ERROR(EIO
);
2229 if (DM_EVENT_ENABLED(dp
, DM_EVENT_CREATE
)) {
2230 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_CREATE
,
2231 dp
, DM_RIGHT_NULL
, NULL
,
2232 DM_RIGHT_NULL
, dir_name
->name
, NULL
,
2239 /* Return through std_return after this point. */
2241 xfs_itrace_entry(dp
);
2245 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
2246 prid
= dp
->i_d
.di_projid
;
2248 prid
= (xfs_prid_t
)dfltprid
;
2251 * Make sure that we have allocated dquot(s) on disk.
2253 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
2254 current_fsuid(), current_fsgid(), prid
,
2255 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
2259 tp
= xfs_trans_alloc(mp
, XFS_TRANS_MKDIR
);
2260 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2261 resblks
= XFS_MKDIR_SPACE_RES(mp
, dir_name
->len
);
2262 error
= xfs_trans_reserve(tp
, resblks
, XFS_MKDIR_LOG_RES(mp
), 0,
2263 XFS_TRANS_PERM_LOG_RES
, XFS_MKDIR_LOG_COUNT
);
2264 if (error
== ENOSPC
) {
2266 error
= xfs_trans_reserve(tp
, 0, XFS_MKDIR_LOG_RES(mp
), 0,
2267 XFS_TRANS_PERM_LOG_RES
,
2268 XFS_MKDIR_LOG_COUNT
);
2275 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
2276 unlock_dp_on_error
= B_TRUE
;
2279 * Check for directory link count overflow.
2281 if (dp
->i_d
.di_nlink
>= XFS_MAXLINK
) {
2282 error
= XFS_ERROR(EMLINK
);
2287 * Reserve disk quota and the inode.
2289 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
2293 error
= xfs_dir_canenter(tp
, dp
, dir_name
, resblks
);
2297 * create the directory inode.
2299 error
= xfs_dir_ialloc(&tp
, dp
, mode
, 2,
2300 0, credp
, prid
, resblks
> 0,
2303 if (error
== ENOSPC
)
2307 xfs_itrace_ref(cdp
);
2310 * Now we add the directory inode to the transaction.
2311 * We waited until now since xfs_dir_ialloc might start
2312 * a new transaction. Had we joined the transaction
2313 * earlier, the locks might have gotten released. An error
2314 * from here on will result in the transaction cancel
2315 * unlocking dp so don't do it explicitly in the error path.
2318 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
2319 unlock_dp_on_error
= B_FALSE
;
2321 XFS_BMAP_INIT(&free_list
, &first_block
);
2323 error
= xfs_dir_createname(tp
, dp
, dir_name
, cdp
->i_ino
,
2324 &first_block
, &free_list
, resblks
?
2325 resblks
- XFS_IALLOC_SPACE_RES(mp
) : 0);
2327 ASSERT(error
!= ENOSPC
);
2330 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2333 * Bump the in memory version number of the parent directory
2334 * so that other processes accessing it will recognize that
2335 * the directory has changed.
2339 error
= xfs_dir_init(tp
, cdp
, dp
);
2344 error
= xfs_bumplink(tp
, dp
);
2354 * Attach the dquots to the new inode and modify the icount incore.
2356 XFS_QM_DQVOPCREATE(mp
, tp
, cdp
, udqp
, gdqp
);
2359 * If this is a synchronous mount, make sure that the
2360 * mkdir transaction goes to disk before returning to
2363 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2364 xfs_trans_set_sync(tp
);
2367 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2373 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2374 XFS_QM_DQRELE(mp
, udqp
);
2375 XFS_QM_DQRELE(mp
, gdqp
);
2380 /* Fall through to std_return with error = 0 or errno from
2381 * xfs_trans_commit. */
2384 if ((created
|| (error
!= 0 && dm_event_sent
!= 0)) &&
2385 DM_EVENT_ENABLED(dp
, DM_EVENT_POSTCREATE
)) {
2386 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTCREATE
,
2388 created
? cdp
: NULL
,
2390 dir_name
->name
, NULL
,
2397 xfs_bmap_cancel(&free_list
);
2399 cancel_flags
|= XFS_TRANS_ABORT
;
2401 xfs_trans_cancel(tp
, cancel_flags
);
2402 XFS_QM_DQRELE(mp
, udqp
);
2403 XFS_QM_DQRELE(mp
, gdqp
);
2405 if (unlock_dp_on_error
)
2406 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
2414 struct xfs_name
*link_name
,
2415 const char *target_path
,
2420 xfs_mount_t
*mp
= dp
->i_mount
;
2425 xfs_bmap_free_t free_list
;
2426 xfs_fsblock_t first_block
;
2427 boolean_t unlock_dp_on_error
= B_FALSE
;
2430 xfs_fileoff_t first_fsb
;
2431 xfs_filblks_t fs_blocks
;
2433 xfs_bmbt_irec_t mval
[SYMLINK_MAPS
];
2435 const char *cur_chunk
;
2440 struct xfs_dquot
*udqp
, *gdqp
;
2448 xfs_itrace_entry(dp
);
2450 if (XFS_FORCED_SHUTDOWN(mp
))
2451 return XFS_ERROR(EIO
);
2454 * Check component lengths of the target path name.
2456 pathlen
= strlen(target_path
);
2457 if (pathlen
>= MAXPATHLEN
) /* total string too long */
2458 return XFS_ERROR(ENAMETOOLONG
);
2460 if (DM_EVENT_ENABLED(dp
, DM_EVENT_SYMLINK
)) {
2461 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_SYMLINK
, dp
,
2462 DM_RIGHT_NULL
, NULL
, DM_RIGHT_NULL
,
2463 link_name
->name
, target_path
, 0, 0, 0);
2468 /* Return through std_return after this point. */
2471 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
2472 prid
= dp
->i_d
.di_projid
;
2474 prid
= (xfs_prid_t
)dfltprid
;
2477 * Make sure that we have allocated dquot(s) on disk.
2479 error
= XFS_QM_DQVOPALLOC(mp
, dp
,
2480 current_fsuid(), current_fsgid(), prid
,
2481 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
, &udqp
, &gdqp
);
2485 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SYMLINK
);
2486 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
2488 * The symlink will fit into the inode data fork?
2489 * There can't be any attributes so we get the whole variable part.
2491 if (pathlen
<= XFS_LITINO(mp
))
2494 fs_blocks
= XFS_B_TO_FSB(mp
, pathlen
);
2495 resblks
= XFS_SYMLINK_SPACE_RES(mp
, link_name
->len
, fs_blocks
);
2496 error
= xfs_trans_reserve(tp
, resblks
, XFS_SYMLINK_LOG_RES(mp
), 0,
2497 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
2498 if (error
== ENOSPC
&& fs_blocks
== 0) {
2500 error
= xfs_trans_reserve(tp
, 0, XFS_SYMLINK_LOG_RES(mp
), 0,
2501 XFS_TRANS_PERM_LOG_RES
, XFS_SYMLINK_LOG_COUNT
);
2508 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
2509 unlock_dp_on_error
= B_TRUE
;
2512 * Check whether the directory allows new symlinks or not.
2514 if (dp
->i_d
.di_flags
& XFS_DIFLAG_NOSYMLINKS
) {
2515 error
= XFS_ERROR(EPERM
);
2520 * Reserve disk quota : blocks and inode.
2522 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
, udqp
, gdqp
, resblks
, 1, 0);
2527 * Check for ability to enter directory entry, if no space reserved.
2529 error
= xfs_dir_canenter(tp
, dp
, link_name
, resblks
);
2533 * Initialize the bmap freelist prior to calling either
2534 * bmapi or the directory create code.
2536 XFS_BMAP_INIT(&free_list
, &first_block
);
2539 * Allocate an inode for the symlink.
2541 error
= xfs_dir_ialloc(&tp
, dp
, S_IFLNK
| (mode
& ~S_IFMT
),
2542 1, 0, credp
, prid
, resblks
> 0, &ip
, NULL
);
2544 if (error
== ENOSPC
)
2551 * An error after we've joined dp to the transaction will result in the
2552 * transaction cancel unlocking dp so don't do it explicitly in the
2556 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
2557 unlock_dp_on_error
= B_FALSE
;
2560 * Also attach the dquot(s) to it, if applicable.
2562 XFS_QM_DQVOPCREATE(mp
, tp
, ip
, udqp
, gdqp
);
2565 resblks
-= XFS_IALLOC_SPACE_RES(mp
);
2567 * If the symlink will fit into the inode, write it inline.
2569 if (pathlen
<= XFS_IFORK_DSIZE(ip
)) {
2570 xfs_idata_realloc(ip
, pathlen
, XFS_DATA_FORK
);
2571 memcpy(ip
->i_df
.if_u1
.if_data
, target_path
, pathlen
);
2572 ip
->i_d
.di_size
= pathlen
;
2575 * The inode was initially created in extent format.
2577 ip
->i_df
.if_flags
&= ~(XFS_IFEXTENTS
| XFS_IFBROOT
);
2578 ip
->i_df
.if_flags
|= XFS_IFINLINE
;
2580 ip
->i_d
.di_format
= XFS_DINODE_FMT_LOCAL
;
2581 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_DDATA
| XFS_ILOG_CORE
);
2585 nmaps
= SYMLINK_MAPS
;
2587 error
= xfs_bmapi(tp
, ip
, first_fsb
, fs_blocks
,
2588 XFS_BMAPI_WRITE
| XFS_BMAPI_METADATA
,
2589 &first_block
, resblks
, mval
, &nmaps
,
2596 resblks
-= fs_blocks
;
2597 ip
->i_d
.di_size
= pathlen
;
2598 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2600 cur_chunk
= target_path
;
2601 for (n
= 0; n
< nmaps
; n
++) {
2602 d
= XFS_FSB_TO_DADDR(mp
, mval
[n
].br_startblock
);
2603 byte_cnt
= XFS_FSB_TO_B(mp
, mval
[n
].br_blockcount
);
2604 bp
= xfs_trans_get_buf(tp
, mp
->m_ddev_targp
, d
,
2605 BTOBB(byte_cnt
), 0);
2606 ASSERT(bp
&& !XFS_BUF_GETERROR(bp
));
2607 if (pathlen
< byte_cnt
) {
2610 pathlen
-= byte_cnt
;
2612 memcpy(XFS_BUF_PTR(bp
), cur_chunk
, byte_cnt
);
2613 cur_chunk
+= byte_cnt
;
2615 xfs_trans_log_buf(tp
, bp
, 0, byte_cnt
- 1);
2620 * Create the directory entry for the symlink.
2622 error
= xfs_dir_createname(tp
, dp
, link_name
, ip
->i_ino
,
2623 &first_block
, &free_list
, resblks
);
2626 xfs_ichgtime(dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
2627 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
2630 * Bump the in memory version number of the parent directory
2631 * so that other processes accessing it will recognize that
2632 * the directory has changed.
2637 * If this is a synchronous mount, make sure that the
2638 * symlink transaction goes to disk before returning to
2641 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
2642 xfs_trans_set_sync(tp
);
2646 * xfs_trans_commit normally decrements the vnode ref count
2647 * when it unlocks the inode. Since we want to return the
2648 * vnode to the caller, we bump the vnode ref count now.
2652 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2656 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2657 XFS_QM_DQRELE(mp
, udqp
);
2658 XFS_QM_DQRELE(mp
, gdqp
);
2660 /* Fall through to std_return with error = 0 or errno from
2661 * xfs_trans_commit */
2663 if (DM_EVENT_ENABLED(dp
, DM_EVENT_POSTSYMLINK
)) {
2664 (void) XFS_SEND_NAMESP(mp
, DM_EVENT_POSTSYMLINK
,
2667 DM_RIGHT_NULL
, link_name
->name
,
2668 target_path
, 0, error
, 0);
2678 xfs_bmap_cancel(&free_list
);
2679 cancel_flags
|= XFS_TRANS_ABORT
;
2681 xfs_trans_cancel(tp
, cancel_flags
);
2682 XFS_QM_DQRELE(mp
, udqp
);
2683 XFS_QM_DQRELE(mp
, gdqp
);
2685 if (unlock_dp_on_error
)
2686 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
2696 xfs_mount_t
*mp
= ip
->i_mount
;
2699 if (XFS_FORCED_SHUTDOWN(mp
))
2700 return XFS_ERROR(EIO
);
2703 * Bypass inodes which have already been cleaned by
2704 * the inode flush clustering code inside xfs_iflush
2706 if (xfs_inode_clean(ip
))
2710 * We make this non-blocking if the inode is contended,
2711 * return EAGAIN to indicate to the caller that they
2712 * did not succeed. This prevents the flush path from
2713 * blocking on inodes inside another operation right
2714 * now, they get caught later by xfs_sync.
2716 if (flags
& FLUSH_SYNC
) {
2717 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
2719 } else if (xfs_ilock_nowait(ip
, XFS_ILOCK_SHARED
)) {
2720 if (xfs_ipincount(ip
) || !xfs_iflock_nowait(ip
)) {
2721 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
2728 error
= xfs_iflush(ip
, (flags
& FLUSH_SYNC
) ? XFS_IFLUSH_SYNC
2729 : XFS_IFLUSH_ASYNC_NOBLOCK
);
2730 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
2742 xfs_mount_t
*mp
= ip
->i_mount
;
2746 if (!capable(CAP_SYS_ADMIN
))
2747 return XFS_ERROR(EPERM
);
2749 if (XFS_FORCED_SHUTDOWN(mp
))
2750 return XFS_ERROR(EIO
);
2752 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SET_DMATTRS
);
2753 error
= xfs_trans_reserve(tp
, 0, XFS_ICHANGE_LOG_RES (mp
), 0, 0, 0);
2755 xfs_trans_cancel(tp
, 0);
2758 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2759 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
2761 ip
->i_d
.di_dmevmask
= evmask
;
2762 ip
->i_d
.di_dmstate
= state
;
2764 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
2766 error
= xfs_trans_commit(tp
, 0);
2776 xfs_itrace_entry(ip
);
2778 ASSERT(!VN_MAPPED(VFS_I(ip
)));
2780 /* bad inode, get out here ASAP */
2781 if (VN_BAD(VFS_I(ip
))) {
2788 ASSERT(XFS_FORCED_SHUTDOWN(ip
->i_mount
) || ip
->i_delayed_blks
== 0);
2791 * Make sure the atime in the XFS inode is correct before freeing the
2794 xfs_synchronize_atime(ip
);
2797 * If we have nothing to flush with this inode then complete the
2798 * teardown now, otherwise break the link between the xfs inode and the
2799 * linux inode and clean up the xfs inode later. This avoids flushing
2800 * the inode to disk during the delete operation itself.
2802 * When breaking the link, we need to set the XFS_IRECLAIMABLE flag
2803 * first to ensure that xfs_iunpin() will never see an xfs inode
2804 * that has a linux inode being reclaimed. Synchronisation is provided
2805 * by the i_flags_lock.
2807 if (!ip
->i_update_core
&& (ip
->i_itemp
== NULL
)) {
2808 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2810 xfs_iflags_set(ip
, XFS_IRECLAIMABLE
);
2811 return xfs_reclaim_inode(ip
, 1, XFS_IFLUSH_DELWRI_ELSE_SYNC
);
2813 xfs_inode_set_reclaim_tag(ip
);
2818 * xfs_alloc_file_space()
2819 * This routine allocates disk space for the given file.
2821 * If alloc_type == 0, this request is for an ALLOCSP type
2822 * request which will change the file size. In this case, no
2823 * DMAPI event will be generated by the call. A TRUNCATE event
2824 * will be generated later by xfs_setattr.
2826 * If alloc_type != 0, this request is for a RESVSP type
2827 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
2828 * lower block boundary byte address is less than the file's
2837 xfs_alloc_file_space(
2844 xfs_mount_t
*mp
= ip
->i_mount
;
2846 xfs_filblks_t allocated_fsb
;
2847 xfs_filblks_t allocatesize_fsb
;
2848 xfs_extlen_t extsz
, temp
;
2849 xfs_fileoff_t startoffset_fsb
;
2850 xfs_fsblock_t firstfsb
;
2856 xfs_bmbt_irec_t imaps
[1], *imapp
;
2857 xfs_bmap_free_t free_list
;
2858 uint qblocks
, resblks
, resrtextents
;
2862 xfs_itrace_entry(ip
);
2864 if (XFS_FORCED_SHUTDOWN(mp
))
2865 return XFS_ERROR(EIO
);
2867 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
2871 return XFS_ERROR(EINVAL
);
2873 rt
= XFS_IS_REALTIME_INODE(ip
);
2874 extsz
= xfs_get_extsz_hint(ip
);
2879 bmapi_flag
= XFS_BMAPI_WRITE
| (alloc_type
? XFS_BMAPI_PREALLOC
: 0);
2880 startoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
2881 allocatesize_fsb
= XFS_B_TO_FSB(mp
, count
);
2883 /* Generate a DMAPI event if needed. */
2884 if (alloc_type
!= 0 && offset
< ip
->i_size
&&
2885 (attr_flags
& XFS_ATTR_DMI
) == 0 &&
2886 DM_EVENT_ENABLED(ip
, DM_EVENT_WRITE
)) {
2887 xfs_off_t end_dmi_offset
;
2889 end_dmi_offset
= offset
+len
;
2890 if (end_dmi_offset
> ip
->i_size
)
2891 end_dmi_offset
= ip
->i_size
;
2892 error
= XFS_SEND_DATA(mp
, DM_EVENT_WRITE
, ip
, offset
,
2893 end_dmi_offset
- offset
, 0, NULL
);
2899 * Allocate file space until done or until there is an error
2902 while (allocatesize_fsb
&& !error
) {
2906 * Determine space reservations for data/realtime.
2908 if (unlikely(extsz
)) {
2909 s
= startoffset_fsb
;
2912 e
= startoffset_fsb
+ allocatesize_fsb
;
2913 if ((temp
= do_mod(startoffset_fsb
, extsz
)))
2915 if ((temp
= do_mod(e
, extsz
)))
2919 e
= allocatesize_fsb
;
2923 resrtextents
= qblocks
= (uint
)(e
- s
);
2924 resrtextents
/= mp
->m_sb
.sb_rextsize
;
2925 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
2926 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
2929 resblks
= qblocks
= \
2930 XFS_DIOSTRAT_SPACE_RES(mp
, (uint
)(e
- s
));
2931 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
2935 * Allocate and setup the transaction.
2937 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
2938 error
= xfs_trans_reserve(tp
, resblks
,
2939 XFS_WRITE_LOG_RES(mp
), resrtextents
,
2940 XFS_TRANS_PERM_LOG_RES
,
2941 XFS_WRITE_LOG_COUNT
);
2943 * Check for running out of space
2947 * Free the transaction structure.
2949 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
2950 xfs_trans_cancel(tp
, 0);
2953 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
2954 error
= XFS_TRANS_RESERVE_QUOTA_NBLKS(mp
, tp
, ip
,
2955 qblocks
, 0, quota_flag
);
2959 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
2960 xfs_trans_ihold(tp
, ip
);
2963 * Issue the xfs_bmapi() call to allocate the blocks
2965 XFS_BMAP_INIT(&free_list
, &firstfsb
);
2966 error
= xfs_bmapi(tp
, ip
, startoffset_fsb
,
2967 allocatesize_fsb
, bmapi_flag
,
2968 &firstfsb
, 0, imapp
, &nimaps
,
2975 * Complete the transaction
2977 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
2982 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
2983 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
2988 allocated_fsb
= imapp
->br_blockcount
;
2991 error
= XFS_ERROR(ENOSPC
);
2995 startoffset_fsb
+= allocated_fsb
;
2996 allocatesize_fsb
-= allocated_fsb
;
2999 if (error
== ENOSPC
&& (attr_flags
& XFS_ATTR_DMI
) == 0 &&
3000 DM_EVENT_ENABLED(ip
, DM_EVENT_NOSPACE
)) {
3001 error
= XFS_SEND_NAMESP(mp
, DM_EVENT_NOSPACE
,
3004 NULL
, NULL
, 0, 0, 0); /* Delay flag intentionally unused */
3006 goto retry
; /* Maybe DMAPI app. has made space */
3007 /* else fall through with error from XFS_SEND_DATA */
3012 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
3013 xfs_bmap_cancel(&free_list
);
3014 XFS_TRANS_UNRESERVE_QUOTA_NBLKS(mp
, tp
, ip
, qblocks
, 0, quota_flag
);
3016 error1
: /* Just cancel transaction */
3017 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
3018 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3019 goto dmapi_enospc_check
;
3023 * Zero file bytes between startoff and endoff inclusive.
3024 * The iolock is held exclusive and no blocks are buffered.
3026 * This function is used by xfs_free_file_space() to zero
3027 * partial blocks when the range to free is not block aligned.
3028 * When unreserving space with boundaries that are not block
3029 * aligned we round up the start and round down the end
3030 * boundaries and then use this function to zero the parts of
3031 * the blocks that got dropped during the rounding.
3034 xfs_zero_remaining_bytes(
3039 xfs_bmbt_irec_t imap
;
3040 xfs_fileoff_t offset_fsb
;
3041 xfs_off_t lastoffset
;
3044 xfs_mount_t
*mp
= ip
->i_mount
;
3049 * Avoid doing I/O beyond eof - it's not necessary
3050 * since nothing can read beyond eof. The space will
3051 * be zeroed when the file is extended anyway.
3053 if (startoff
>= ip
->i_size
)
3056 if (endoff
> ip
->i_size
)
3057 endoff
= ip
->i_size
;
3059 bp
= xfs_buf_get_noaddr(mp
->m_sb
.sb_blocksize
,
3060 XFS_IS_REALTIME_INODE(ip
) ?
3061 mp
->m_rtdev_targp
: mp
->m_ddev_targp
);
3063 for (offset
= startoff
; offset
<= endoff
; offset
= lastoffset
+ 1) {
3064 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
3066 error
= xfs_bmapi(NULL
, ip
, offset_fsb
, 1, 0,
3067 NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
3068 if (error
|| nimap
< 1)
3070 ASSERT(imap
.br_blockcount
>= 1);
3071 ASSERT(imap
.br_startoff
== offset_fsb
);
3072 lastoffset
= XFS_FSB_TO_B(mp
, imap
.br_startoff
+ 1) - 1;
3073 if (lastoffset
> endoff
)
3074 lastoffset
= endoff
;
3075 if (imap
.br_startblock
== HOLESTARTBLOCK
)
3077 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
3078 if (imap
.br_state
== XFS_EXT_UNWRITTEN
)
3081 XFS_BUF_UNWRITE(bp
);
3083 XFS_BUF_SET_ADDR(bp
, XFS_FSB_TO_DB(ip
, imap
.br_startblock
));
3085 error
= xfs_iowait(bp
);
3087 xfs_ioerror_alert("xfs_zero_remaining_bytes(read)",
3088 mp
, bp
, XFS_BUF_ADDR(bp
));
3091 memset(XFS_BUF_PTR(bp
) +
3092 (offset
- XFS_FSB_TO_B(mp
, imap
.br_startoff
)),
3093 0, lastoffset
- offset
+ 1);
3098 error
= xfs_iowait(bp
);
3100 xfs_ioerror_alert("xfs_zero_remaining_bytes(write)",
3101 mp
, bp
, XFS_BUF_ADDR(bp
));
3110 * xfs_free_file_space()
3111 * This routine frees disk space for the given file.
3113 * This routine is only called by xfs_change_file_space
3114 * for an UNRESVSP type call.
3122 xfs_free_file_space(
3130 xfs_off_t end_dmi_offset
;
3131 xfs_fileoff_t endoffset_fsb
;
3133 xfs_fsblock_t firstfsb
;
3134 xfs_bmap_free_t free_list
;
3135 xfs_bmbt_irec_t imap
;
3143 xfs_fileoff_t startoffset_fsb
;
3145 int need_iolock
= 1;
3149 xfs_itrace_entry(ip
);
3151 if ((error
= XFS_QM_DQATTACH(mp
, ip
, 0)))
3155 if (len
<= 0) /* if nothing being freed */
3157 rt
= XFS_IS_REALTIME_INODE(ip
);
3158 startoffset_fsb
= XFS_B_TO_FSB(mp
, offset
);
3159 end_dmi_offset
= offset
+ len
;
3160 endoffset_fsb
= XFS_B_TO_FSBT(mp
, end_dmi_offset
);
3162 if (offset
< ip
->i_size
&& (attr_flags
& XFS_ATTR_DMI
) == 0 &&
3163 DM_EVENT_ENABLED(ip
, DM_EVENT_WRITE
)) {
3164 if (end_dmi_offset
> ip
->i_size
)
3165 end_dmi_offset
= ip
->i_size
;
3166 error
= XFS_SEND_DATA(mp
, DM_EVENT_WRITE
, ip
,
3167 offset
, end_dmi_offset
- offset
,
3168 AT_DELAY_FLAG(attr_flags
), NULL
);
3173 if (attr_flags
& XFS_ATTR_NOLOCK
)
3176 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
3177 vn_iowait(ip
); /* wait for the completion of any pending DIOs */
3180 rounding
= max_t(uint
, 1 << mp
->m_sb
.sb_blocklog
, PAGE_CACHE_SIZE
);
3181 ioffset
= offset
& ~(rounding
- 1);
3183 if (VN_CACHED(VFS_I(ip
)) != 0) {
3184 xfs_inval_cached_trace(ip
, ioffset
, -1, ioffset
, -1);
3185 error
= xfs_flushinval_pages(ip
, ioffset
, -1, FI_REMAPF_LOCKED
);
3187 goto out_unlock_iolock
;
3191 * Need to zero the stuff we're not freeing, on disk.
3192 * If its a realtime file & can't use unwritten extents then we
3193 * actually need to zero the extent edges. Otherwise xfs_bunmapi
3194 * will take care of it for us.
3196 if (rt
&& !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
3198 error
= xfs_bmapi(NULL
, ip
, startoffset_fsb
,
3199 1, 0, NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
3201 goto out_unlock_iolock
;
3202 ASSERT(nimap
== 0 || nimap
== 1);
3203 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
3206 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
3207 block
= imap
.br_startblock
;
3208 mod
= do_div(block
, mp
->m_sb
.sb_rextsize
);
3210 startoffset_fsb
+= mp
->m_sb
.sb_rextsize
- mod
;
3213 error
= xfs_bmapi(NULL
, ip
, endoffset_fsb
- 1,
3214 1, 0, NULL
, 0, &imap
, &nimap
, NULL
, NULL
);
3216 goto out_unlock_iolock
;
3217 ASSERT(nimap
== 0 || nimap
== 1);
3218 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
3219 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
3221 if (mod
&& (mod
!= mp
->m_sb
.sb_rextsize
))
3222 endoffset_fsb
-= mod
;
3225 if ((done
= (endoffset_fsb
<= startoffset_fsb
)))
3227 * One contiguous piece to clear
3229 error
= xfs_zero_remaining_bytes(ip
, offset
, offset
+ len
- 1);
3232 * Some full blocks, possibly two pieces to clear
3234 if (offset
< XFS_FSB_TO_B(mp
, startoffset_fsb
))
3235 error
= xfs_zero_remaining_bytes(ip
, offset
,
3236 XFS_FSB_TO_B(mp
, startoffset_fsb
) - 1);
3238 XFS_FSB_TO_B(mp
, endoffset_fsb
) < offset
+ len
)
3239 error
= xfs_zero_remaining_bytes(ip
,
3240 XFS_FSB_TO_B(mp
, endoffset_fsb
),
3245 * free file space until done or until there is an error
3247 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
3248 while (!error
&& !done
) {
3251 * allocate and setup the transaction. Allow this
3252 * transaction to dip into the reserve blocks to ensure
3253 * the freeing of the space succeeds at ENOSPC.
3255 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
3256 tp
->t_flags
|= XFS_TRANS_RESERVE
;
3257 error
= xfs_trans_reserve(tp
,
3259 XFS_WRITE_LOG_RES(mp
),
3261 XFS_TRANS_PERM_LOG_RES
,
3262 XFS_WRITE_LOG_COUNT
);
3265 * check for running out of space
3269 * Free the transaction structure.
3271 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
3272 xfs_trans_cancel(tp
, 0);
3275 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3276 error
= XFS_TRANS_RESERVE_QUOTA(mp
, tp
,
3277 ip
->i_udquot
, ip
->i_gdquot
, resblks
, 0,
3278 XFS_QMOPT_RES_REGBLKS
);
3282 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3283 xfs_trans_ihold(tp
, ip
);
3286 * issue the bunmapi() call to free the blocks
3288 XFS_BMAP_INIT(&free_list
, &firstfsb
);
3289 error
= xfs_bunmapi(tp
, ip
, startoffset_fsb
,
3290 endoffset_fsb
- startoffset_fsb
,
3291 0, 2, &firstfsb
, &free_list
, NULL
, &done
);
3297 * complete the transaction
3299 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
3304 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
3305 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
3310 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
3314 xfs_bmap_cancel(&free_list
);
3316 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
3317 xfs_iunlock(ip
, need_iolock
? (XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
) :
3323 * xfs_change_file_space()
3324 * This routine allocates or frees disk space for the given file.
3325 * The user specified parameters are checked for alignment and size
3334 xfs_change_file_space(
3341 xfs_mount_t
*mp
= ip
->i_mount
;
3346 xfs_off_t startoffset
;
3351 xfs_itrace_entry(ip
);
3353 if (!S_ISREG(ip
->i_d
.di_mode
))
3354 return XFS_ERROR(EINVAL
);
3356 switch (bf
->l_whence
) {
3357 case 0: /*SEEK_SET*/
3359 case 1: /*SEEK_CUR*/
3360 bf
->l_start
+= offset
;
3362 case 2: /*SEEK_END*/
3363 bf
->l_start
+= ip
->i_size
;
3366 return XFS_ERROR(EINVAL
);
3369 llen
= bf
->l_len
> 0 ? bf
->l_len
- 1 : bf
->l_len
;
3371 if ( (bf
->l_start
< 0)
3372 || (bf
->l_start
> XFS_MAXIOFFSET(mp
))
3373 || (bf
->l_start
+ llen
< 0)
3374 || (bf
->l_start
+ llen
> XFS_MAXIOFFSET(mp
)))
3375 return XFS_ERROR(EINVAL
);
3379 startoffset
= bf
->l_start
;
3383 * XFS_IOC_RESVSP and XFS_IOC_UNRESVSP will reserve or unreserve
3385 * These calls do NOT zero the data space allocated to the file,
3386 * nor do they change the file size.
3388 * XFS_IOC_ALLOCSP and XFS_IOC_FREESP will allocate and free file
3390 * These calls cause the new file data to be zeroed and the file
3391 * size to be changed.
3393 setprealloc
= clrprealloc
= 0;
3396 case XFS_IOC_RESVSP
:
3397 case XFS_IOC_RESVSP64
:
3398 error
= xfs_alloc_file_space(ip
, startoffset
, bf
->l_len
,
3405 case XFS_IOC_UNRESVSP
:
3406 case XFS_IOC_UNRESVSP64
:
3407 if ((error
= xfs_free_file_space(ip
, startoffset
, bf
->l_len
,
3412 case XFS_IOC_ALLOCSP
:
3413 case XFS_IOC_ALLOCSP64
:
3414 case XFS_IOC_FREESP
:
3415 case XFS_IOC_FREESP64
:
3416 if (startoffset
> fsize
) {
3417 error
= xfs_alloc_file_space(ip
, fsize
,
3418 startoffset
- fsize
, 0, attr_flags
);
3423 iattr
.ia_valid
= ATTR_SIZE
;
3424 iattr
.ia_size
= startoffset
;
3426 error
= xfs_setattr(ip
, &iattr
, attr_flags
);
3436 return XFS_ERROR(EINVAL
);
3440 * update the inode timestamp, mode, and prealloc flag bits
3442 tp
= xfs_trans_alloc(mp
, XFS_TRANS_WRITEID
);
3444 if ((error
= xfs_trans_reserve(tp
, 0, XFS_WRITEID_LOG_RES(mp
),
3447 xfs_trans_cancel(tp
, 0);
3451 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
3453 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
3454 xfs_trans_ihold(tp
, ip
);
3456 if ((attr_flags
& XFS_ATTR_DMI
) == 0) {
3457 ip
->i_d
.di_mode
&= ~S_ISUID
;
3460 * Note that we don't have to worry about mandatory
3461 * file locking being disabled here because we only
3462 * clear the S_ISGID bit if the Group execute bit is
3463 * on, but if it was on then mandatory locking wouldn't
3464 * have been enabled.
3466 if (ip
->i_d
.di_mode
& S_IXGRP
)
3467 ip
->i_d
.di_mode
&= ~S_ISGID
;
3469 xfs_ichgtime(ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
3472 ip
->i_d
.di_flags
|= XFS_DIFLAG_PREALLOC
;
3473 else if (clrprealloc
)
3474 ip
->i_d
.di_flags
&= ~XFS_DIFLAG_PREALLOC
;
3476 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
3477 xfs_trans_set_sync(tp
);
3479 error
= xfs_trans_commit(tp
, 0);
3481 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);