2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * Copyright (c) 2012 Red Hat, Inc.
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it would be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #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"
42 #include "xfs_error.h"
43 #include "xfs_quota.h"
44 #include "xfs_utils.h"
45 #include "xfs_rtalloc.h"
46 #include "xfs_trans_space.h"
47 #include "xfs_log_priv.h"
48 #include "xfs_filestream.h"
49 #include "xfs_vnodeops.h"
50 #include "xfs_trace.h"
51 #include "xfs_icache.h"
52 #include "xfs_symlink.h"
56 * This is called by xfs_inactive to free any blocks beyond eof
57 * when the link count isn't zero and by xfs_dm_punch_hole() when
58 * punching a hole to EOF.
68 xfs_fileoff_t end_fsb
;
69 xfs_fileoff_t last_fsb
;
70 xfs_filblks_t map_len
;
75 * Figure out if there are any blocks beyond the end
76 * of the file. If not, then there is nothing to do.
78 end_fsb
= XFS_B_TO_FSB(mp
, (xfs_ufsize_t
)XFS_ISIZE(ip
));
79 last_fsb
= XFS_B_TO_FSB(mp
, mp
->m_super
->s_maxbytes
);
80 if (last_fsb
<= end_fsb
)
82 map_len
= last_fsb
- end_fsb
;
85 xfs_ilock(ip
, XFS_ILOCK_SHARED
);
86 error
= xfs_bmapi_read(ip
, end_fsb
, map_len
, &imap
, &nimaps
, 0);
87 xfs_iunlock(ip
, XFS_ILOCK_SHARED
);
89 if (!error
&& (nimaps
!= 0) &&
90 (imap
.br_startblock
!= HOLESTARTBLOCK
||
91 ip
->i_delayed_blks
)) {
93 * Attach the dquots to the inode up front.
95 error
= xfs_qm_dqattach(ip
, 0);
100 * There are blocks after the end of file.
101 * Free them up now by truncating the file to
104 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
107 if (!xfs_ilock_nowait(ip
, XFS_IOLOCK_EXCL
)) {
108 xfs_trans_cancel(tp
, 0);
113 error
= xfs_trans_reserve(tp
, 0,
114 XFS_ITRUNCATE_LOG_RES(mp
),
115 0, XFS_TRANS_PERM_LOG_RES
,
116 XFS_ITRUNCATE_LOG_COUNT
);
118 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
119 xfs_trans_cancel(tp
, 0);
121 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
125 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
126 xfs_trans_ijoin(tp
, ip
, 0);
129 * Do not update the on-disk file size. If we update the
130 * on-disk file size and then the system crashes before the
131 * contents of the file are flushed to disk then the files
132 * may be full of holes (ie NULL files bug).
134 error
= xfs_itruncate_extents(&tp
, ip
, XFS_DATA_FORK
,
138 * If we get an error at this point we simply don't
139 * bother truncating the file.
142 (XFS_TRANS_RELEASE_LOG_RES
|
145 error
= xfs_trans_commit(tp
,
146 XFS_TRANS_RELEASE_LOG_RES
);
148 xfs_inode_clear_eofblocks_tag(ip
);
151 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
153 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
162 xfs_mount_t
*mp
= ip
->i_mount
;
165 if (!S_ISREG(ip
->i_d
.di_mode
) || (ip
->i_d
.di_mode
== 0))
168 /* If this is a read-only mount, don't do this (would generate I/O) */
169 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
172 if (!XFS_FORCED_SHUTDOWN(mp
)) {
176 * If we are using filestreams, and we have an unlinked
177 * file that we are processing the last close on, then nothing
178 * will be able to reopen and write to this file. Purge this
179 * inode from the filestreams cache so that it doesn't delay
180 * teardown of the inode.
182 if ((ip
->i_d
.di_nlink
== 0) && xfs_inode_is_filestream(ip
))
183 xfs_filestream_deassociate(ip
);
186 * If we previously truncated this file and removed old data
187 * in the process, we want to initiate "early" writeout on
188 * the last close. This is an attempt to combat the notorious
189 * NULL files problem which is particularly noticeable from a
190 * truncate down, buffered (re-)write (delalloc), followed by
191 * a crash. What we are effectively doing here is
192 * significantly reducing the time window where we'd otherwise
193 * be exposed to that problem.
195 truncated
= xfs_iflags_test_and_clear(ip
, XFS_ITRUNCATED
);
197 xfs_iflags_clear(ip
, XFS_IDIRTY_RELEASE
);
198 if (VN_DIRTY(VFS_I(ip
)) && ip
->i_delayed_blks
> 0) {
199 error
= -filemap_flush(VFS_I(ip
)->i_mapping
);
206 if (ip
->i_d
.di_nlink
== 0)
209 if (xfs_can_free_eofblocks(ip
, false)) {
212 * If we can't get the iolock just skip truncating the blocks
213 * past EOF because we could deadlock with the mmap_sem
214 * otherwise. We'll get another chance to drop them once the
215 * last reference to the inode is dropped, so we'll never leak
216 * blocks permanently.
218 * Further, check if the inode is being opened, written and
219 * closed frequently and we have delayed allocation blocks
220 * outstanding (e.g. streaming writes from the NFS server),
221 * truncating the blocks past EOF will cause fragmentation to
224 * In this case don't do the truncation, either, but we have to
225 * be careful how we detect this case. Blocks beyond EOF show
226 * up as i_delayed_blks even when the inode is clean, so we
227 * need to truncate them away first before checking for a dirty
228 * release. Hence on the first dirty close we will still remove
229 * the speculative allocation, but after that we will leave it
232 if (xfs_iflags_test(ip
, XFS_IDIRTY_RELEASE
))
235 error
= xfs_free_eofblocks(mp
, ip
, true);
236 if (error
&& error
!= EAGAIN
)
239 /* delalloc blocks after truncation means it really is dirty */
240 if (ip
->i_delayed_blks
)
241 xfs_iflags_set(ip
, XFS_IDIRTY_RELEASE
);
249 * This is called when the vnode reference count for the vnode
250 * goes to zero. If the file has been unlinked, then it must
251 * now be truncated. Also, we clear all of the read-ahead state
252 * kept for the inode here since the file is now closed.
258 xfs_bmap_free_t free_list
;
259 xfs_fsblock_t first_block
;
267 * If the inode is already free, then there can be nothing
270 if (ip
->i_d
.di_mode
== 0 || is_bad_inode(VFS_I(ip
))) {
271 ASSERT(ip
->i_df
.if_real_bytes
== 0);
272 ASSERT(ip
->i_df
.if_broot_bytes
== 0);
273 return VN_INACTIVE_CACHE
;
280 /* If this is a read-only mount, don't do this (would generate I/O) */
281 if (mp
->m_flags
& XFS_MOUNT_RDONLY
)
284 if (ip
->i_d
.di_nlink
!= 0) {
286 * force is true because we are evicting an inode from the
287 * cache. Post-eof blocks must be freed, lest we end up with
288 * broken free space accounting.
290 if (xfs_can_free_eofblocks(ip
, true)) {
291 error
= xfs_free_eofblocks(mp
, ip
, false);
293 return VN_INACTIVE_CACHE
;
298 if (S_ISREG(ip
->i_d
.di_mode
) &&
299 (ip
->i_d
.di_size
!= 0 || XFS_ISIZE(ip
) != 0 ||
300 ip
->i_d
.di_nextents
> 0 || ip
->i_delayed_blks
> 0))
303 error
= xfs_qm_dqattach(ip
, 0);
305 return VN_INACTIVE_CACHE
;
307 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
308 error
= xfs_trans_reserve(tp
, 0,
309 (truncate
|| S_ISLNK(ip
->i_d
.di_mode
)) ?
310 XFS_ITRUNCATE_LOG_RES(mp
) :
311 XFS_IFREE_LOG_RES(mp
),
313 XFS_TRANS_PERM_LOG_RES
,
314 XFS_ITRUNCATE_LOG_COUNT
);
316 ASSERT(XFS_FORCED_SHUTDOWN(mp
));
317 xfs_trans_cancel(tp
, 0);
318 return VN_INACTIVE_CACHE
;
321 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
322 xfs_trans_ijoin(tp
, ip
, 0);
324 if (S_ISLNK(ip
->i_d
.di_mode
)) {
325 error
= xfs_inactive_symlink(ip
, &tp
);
328 } else if (truncate
) {
330 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
332 error
= xfs_itruncate_extents(&tp
, ip
, XFS_DATA_FORK
, 0);
336 ASSERT(ip
->i_d
.di_nextents
== 0);
340 * If there are attributes associated with the file then blow them away
341 * now. The code calls a routine that recursively deconstructs the
342 * attribute fork. We need to just commit the current transaction
343 * because we can't use it for xfs_attr_inactive().
345 if (ip
->i_d
.di_anextents
> 0) {
346 ASSERT(ip
->i_d
.di_forkoff
!= 0);
348 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
352 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
354 error
= xfs_attr_inactive(ip
);
358 tp
= xfs_trans_alloc(mp
, XFS_TRANS_INACTIVE
);
359 error
= xfs_trans_reserve(tp
, 0,
360 XFS_IFREE_LOG_RES(mp
),
361 0, XFS_TRANS_PERM_LOG_RES
,
362 XFS_INACTIVE_LOG_COUNT
);
364 xfs_trans_cancel(tp
, 0);
368 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
369 xfs_trans_ijoin(tp
, ip
, 0);
373 xfs_idestroy_fork(ip
, XFS_ATTR_FORK
);
375 ASSERT(ip
->i_d
.di_anextents
== 0);
380 xfs_bmap_init(&free_list
, &first_block
);
381 error
= xfs_ifree(tp
, ip
, &free_list
);
384 * If we fail to free the inode, shut down. The cancel
385 * might do that, we need to make sure. Otherwise the
386 * inode might be lost for a long time or forever.
388 if (!XFS_FORCED_SHUTDOWN(mp
)) {
389 xfs_notice(mp
, "%s: xfs_ifree returned error %d",
391 xfs_force_shutdown(mp
, SHUTDOWN_META_IO_ERROR
);
393 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
|XFS_TRANS_ABORT
);
396 * Credit the quota account(s). The inode is gone.
398 xfs_trans_mod_dquot_byino(tp
, ip
, XFS_TRANS_DQ_ICOUNT
, -1);
401 * Just ignore errors at this point. There is nothing we can
402 * do except to try to keep going. Make sure it's not a silent
405 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
407 xfs_notice(mp
, "%s: xfs_bmap_finish returned error %d",
409 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
411 xfs_notice(mp
, "%s: xfs_trans_commit returned error %d",
416 * Release the dquots held by inode, if any.
420 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
422 return VN_INACTIVE_CACHE
;
424 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
429 * Lookups up an inode from "name". If ci_name is not NULL, then a CI match
430 * is allowed, otherwise it has to be an exact match. If a CI match is found,
431 * ci_name->name will point to a the actual name (caller must free) or
432 * will be set to NULL if an exact match is found.
437 struct xfs_name
*name
,
439 struct xfs_name
*ci_name
)
445 trace_xfs_lookup(dp
, name
);
447 if (XFS_FORCED_SHUTDOWN(dp
->i_mount
))
448 return XFS_ERROR(EIO
);
450 lock_mode
= xfs_ilock_map_shared(dp
);
451 error
= xfs_dir_lookup(NULL
, dp
, name
, &inum
, ci_name
);
452 xfs_iunlock_map_shared(dp
, lock_mode
);
457 error
= xfs_iget(dp
->i_mount
, NULL
, inum
, 0, 0, ipp
);
465 kmem_free(ci_name
->name
);
474 struct xfs_name
*name
,
479 int is_dir
= S_ISDIR(mode
);
480 struct xfs_mount
*mp
= dp
->i_mount
;
481 struct xfs_inode
*ip
= NULL
;
482 struct xfs_trans
*tp
= NULL
;
484 xfs_bmap_free_t free_list
;
485 xfs_fsblock_t first_block
;
486 bool unlock_dp_on_error
= false;
490 struct xfs_dquot
*udqp
= NULL
;
491 struct xfs_dquot
*gdqp
= NULL
;
492 struct xfs_dquot
*pdqp
= NULL
;
497 trace_xfs_create(dp
, name
);
499 if (XFS_FORCED_SHUTDOWN(mp
))
500 return XFS_ERROR(EIO
);
502 if (dp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
)
503 prid
= xfs_get_projid(dp
);
505 prid
= XFS_PROJID_DEFAULT
;
508 * Make sure that we have allocated dquot(s) on disk.
510 error
= xfs_qm_vop_dqalloc(dp
, current_fsuid(), current_fsgid(), prid
,
511 XFS_QMOPT_QUOTALL
| XFS_QMOPT_INHERIT
,
512 &udqp
, &gdqp
, &pdqp
);
518 resblks
= XFS_MKDIR_SPACE_RES(mp
, name
->len
);
519 log_res
= XFS_MKDIR_LOG_RES(mp
);
520 log_count
= XFS_MKDIR_LOG_COUNT
;
521 tp
= xfs_trans_alloc(mp
, XFS_TRANS_MKDIR
);
523 resblks
= XFS_CREATE_SPACE_RES(mp
, name
->len
);
524 log_res
= XFS_CREATE_LOG_RES(mp
);
525 log_count
= XFS_CREATE_LOG_COUNT
;
526 tp
= xfs_trans_alloc(mp
, XFS_TRANS_CREATE
);
529 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
532 * Initially assume that the file does not exist and
533 * reserve the resources for that case. If that is not
534 * the case we'll drop the one we have and get a more
535 * appropriate transaction later.
537 error
= xfs_trans_reserve(tp
, resblks
, log_res
, 0,
538 XFS_TRANS_PERM_LOG_RES
, log_count
);
539 if (error
== ENOSPC
) {
540 /* flush outstanding delalloc blocks and retry */
541 xfs_flush_inodes(mp
);
542 error
= xfs_trans_reserve(tp
, resblks
, log_res
, 0,
543 XFS_TRANS_PERM_LOG_RES
, log_count
);
545 if (error
== ENOSPC
) {
546 /* No space at all so try a "no-allocation" reservation */
548 error
= xfs_trans_reserve(tp
, 0, log_res
, 0,
549 XFS_TRANS_PERM_LOG_RES
, log_count
);
553 goto out_trans_cancel
;
556 xfs_ilock(dp
, XFS_ILOCK_EXCL
| XFS_ILOCK_PARENT
);
557 unlock_dp_on_error
= true;
559 xfs_bmap_init(&free_list
, &first_block
);
562 * Reserve disk quota and the inode.
564 error
= xfs_trans_reserve_quota(tp
, mp
, udqp
, gdqp
,
565 pdqp
, resblks
, 1, 0);
567 goto out_trans_cancel
;
569 error
= xfs_dir_canenter(tp
, dp
, name
, resblks
);
571 goto out_trans_cancel
;
574 * A newly created regular or special file just has one directory
575 * entry pointing to them, but a directory also the "." entry
576 * pointing to itself.
578 error
= xfs_dir_ialloc(&tp
, dp
, mode
, is_dir
? 2 : 1, rdev
,
579 prid
, resblks
> 0, &ip
, &committed
);
582 goto out_trans_cancel
;
583 goto out_trans_abort
;
587 * Now we join the directory inode to the transaction. We do not do it
588 * earlier because xfs_dir_ialloc might commit the previous transaction
589 * (and release all the locks). An error from here on will result in
590 * the transaction cancel unlocking dp so don't do it explicitly in the
593 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
594 unlock_dp_on_error
= false;
596 error
= xfs_dir_createname(tp
, dp
, name
, ip
->i_ino
,
597 &first_block
, &free_list
, resblks
?
598 resblks
- XFS_IALLOC_SPACE_RES(mp
) : 0);
600 ASSERT(error
!= ENOSPC
);
601 goto out_trans_abort
;
603 xfs_trans_ichgtime(tp
, dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
604 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
607 error
= xfs_dir_init(tp
, ip
, dp
);
609 goto out_bmap_cancel
;
611 error
= xfs_bumplink(tp
, dp
);
613 goto out_bmap_cancel
;
617 * If this is a synchronous mount, make sure that the
618 * create transaction goes to disk before returning to
621 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
))
622 xfs_trans_set_sync(tp
);
625 * Attach the dquot(s) to the inodes and modify them incore.
626 * These ids of the inode couldn't have changed since the new
627 * inode has been locked ever since it was created.
629 xfs_qm_vop_create_dqattach(tp
, ip
, udqp
, gdqp
, pdqp
);
631 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
633 goto out_bmap_cancel
;
635 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
637 goto out_release_inode
;
647 xfs_bmap_cancel(&free_list
);
649 cancel_flags
|= XFS_TRANS_ABORT
;
651 xfs_trans_cancel(tp
, cancel_flags
);
654 * Wait until after the current transaction is aborted to
655 * release the inode. This prevents recursive transactions
656 * and deadlocks from xfs_inactive.
665 if (unlock_dp_on_error
)
666 xfs_iunlock(dp
, XFS_ILOCK_EXCL
);
672 int xfs_small_retries
;
673 int xfs_middle_retries
;
674 int xfs_lots_retries
;
679 * Bump the subclass so xfs_lock_inodes() acquires each lock with
683 xfs_lock_inumorder(int lock_mode
, int subclass
)
685 if (lock_mode
& (XFS_IOLOCK_SHARED
|XFS_IOLOCK_EXCL
))
686 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_IOLOCK_SHIFT
;
687 if (lock_mode
& (XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
))
688 lock_mode
|= (subclass
+ XFS_LOCK_INUMORDER
) << XFS_ILOCK_SHIFT
;
694 * The following routine will lock n inodes in exclusive mode.
695 * We assume the caller calls us with the inodes in i_ino order.
697 * We need to detect deadlock where an inode that we lock
698 * is in the AIL and we start waiting for another inode that is locked
699 * by a thread in a long running transaction (such as truncate). This can
700 * result in deadlock since the long running trans might need to wait
701 * for the inode we just locked in order to push the tail and free space
710 int attempts
= 0, i
, j
, try_lock
;
713 ASSERT(ips
&& (inodes
>= 2)); /* we need at least two */
719 for (; i
< inodes
; i
++) {
722 if (i
&& (ips
[i
] == ips
[i
-1])) /* Already locked */
726 * If try_lock is not set yet, make sure all locked inodes
727 * are not in the AIL.
728 * If any are, set try_lock to be used later.
732 for (j
= (i
- 1); j
>= 0 && !try_lock
; j
--) {
733 lp
= (xfs_log_item_t
*)ips
[j
]->i_itemp
;
734 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
741 * If any of the previous locks we have locked is in the AIL,
742 * we must TRY to get the second and subsequent locks. If
743 * we can't get any, we must release all we have
748 /* try_lock must be 0 if i is 0. */
750 * try_lock means we have an inode locked
751 * that is in the AIL.
754 if (!xfs_ilock_nowait(ips
[i
], xfs_lock_inumorder(lock_mode
, i
))) {
758 * Unlock all previous guys and try again.
759 * xfs_iunlock will try to push the tail
760 * if the inode is in the AIL.
763 for(j
= i
- 1; j
>= 0; j
--) {
766 * Check to see if we've already
768 * Not the first one going back,
769 * and the inode ptr is the same.
771 if ((j
!= (i
- 1)) && ips
[j
] ==
775 xfs_iunlock(ips
[j
], lock_mode
);
778 if ((attempts
% 5) == 0) {
779 delay(1); /* Don't just spin the CPU */
789 xfs_ilock(ips
[i
], xfs_lock_inumorder(lock_mode
, i
));
795 if (attempts
< 5) xfs_small_retries
++;
796 else if (attempts
< 100) xfs_middle_retries
++;
797 else xfs_lots_retries
++;
805 * xfs_lock_two_inodes() can only be used to lock one type of lock
806 * at a time - the iolock or the ilock, but not both at once. If
807 * we lock both at once, lockdep will report false positives saying
808 * we have violated locking orders.
820 if (lock_mode
& (XFS_IOLOCK_SHARED
|XFS_IOLOCK_EXCL
))
821 ASSERT((lock_mode
& (XFS_ILOCK_SHARED
|XFS_ILOCK_EXCL
)) == 0);
822 ASSERT(ip0
->i_ino
!= ip1
->i_ino
);
824 if (ip0
->i_ino
> ip1
->i_ino
) {
831 xfs_ilock(ip0
, xfs_lock_inumorder(lock_mode
, 0));
834 * If the first lock we have locked is in the AIL, we must TRY to get
835 * the second lock. If we can't get it, we must release the first one
838 lp
= (xfs_log_item_t
*)ip0
->i_itemp
;
839 if (lp
&& (lp
->li_flags
& XFS_LI_IN_AIL
)) {
840 if (!xfs_ilock_nowait(ip1
, xfs_lock_inumorder(lock_mode
, 1))) {
841 xfs_iunlock(ip0
, lock_mode
);
842 if ((++attempts
% 5) == 0)
843 delay(1); /* Don't just spin the CPU */
847 xfs_ilock(ip1
, xfs_lock_inumorder(lock_mode
, 1));
854 struct xfs_name
*name
,
857 xfs_mount_t
*mp
= dp
->i_mount
;
858 xfs_trans_t
*tp
= NULL
;
859 int is_dir
= S_ISDIR(ip
->i_d
.di_mode
);
861 xfs_bmap_free_t free_list
;
862 xfs_fsblock_t first_block
;
869 trace_xfs_remove(dp
, name
);
871 if (XFS_FORCED_SHUTDOWN(mp
))
872 return XFS_ERROR(EIO
);
874 error
= xfs_qm_dqattach(dp
, 0);
878 error
= xfs_qm_dqattach(ip
, 0);
883 tp
= xfs_trans_alloc(mp
, XFS_TRANS_RMDIR
);
884 log_count
= XFS_DEFAULT_LOG_COUNT
;
886 tp
= xfs_trans_alloc(mp
, XFS_TRANS_REMOVE
);
887 log_count
= XFS_REMOVE_LOG_COUNT
;
889 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
892 * We try to get the real space reservation first,
893 * allowing for directory btree deletion(s) implying
894 * possible bmap insert(s). If we can't get the space
895 * reservation then we use 0 instead, and avoid the bmap
896 * btree insert(s) in the directory code by, if the bmap
897 * insert tries to happen, instead trimming the LAST
898 * block from the directory.
900 resblks
= XFS_REMOVE_SPACE_RES(mp
);
901 error
= xfs_trans_reserve(tp
, resblks
, XFS_REMOVE_LOG_RES(mp
), 0,
902 XFS_TRANS_PERM_LOG_RES
, log_count
);
903 if (error
== ENOSPC
) {
905 error
= xfs_trans_reserve(tp
, 0, XFS_REMOVE_LOG_RES(mp
), 0,
906 XFS_TRANS_PERM_LOG_RES
, log_count
);
909 ASSERT(error
!= ENOSPC
);
911 goto out_trans_cancel
;
914 xfs_lock_two_inodes(dp
, ip
, XFS_ILOCK_EXCL
);
916 xfs_trans_ijoin(tp
, dp
, XFS_ILOCK_EXCL
);
917 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
920 * If we're removing a directory perform some additional validation.
923 ASSERT(ip
->i_d
.di_nlink
>= 2);
924 if (ip
->i_d
.di_nlink
!= 2) {
925 error
= XFS_ERROR(ENOTEMPTY
);
926 goto out_trans_cancel
;
928 if (!xfs_dir_isempty(ip
)) {
929 error
= XFS_ERROR(ENOTEMPTY
);
930 goto out_trans_cancel
;
934 xfs_bmap_init(&free_list
, &first_block
);
935 error
= xfs_dir_removename(tp
, dp
, name
, ip
->i_ino
,
936 &first_block
, &free_list
, resblks
);
938 ASSERT(error
!= ENOENT
);
939 goto out_bmap_cancel
;
941 xfs_trans_ichgtime(tp
, dp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
945 * Drop the link from ip's "..".
947 error
= xfs_droplink(tp
, dp
);
949 goto out_bmap_cancel
;
952 * Drop the "." link from ip to self.
954 error
= xfs_droplink(tp
, ip
);
956 goto out_bmap_cancel
;
959 * When removing a non-directory we need to log the parent
960 * inode here. For a directory this is done implicitly
961 * by the xfs_droplink call for the ".." entry.
963 xfs_trans_log_inode(tp
, dp
, XFS_ILOG_CORE
);
967 * Drop the link from dp to ip.
969 error
= xfs_droplink(tp
, ip
);
971 goto out_bmap_cancel
;
974 * Determine if this is the last link while
975 * we are in the transaction.
977 link_zero
= (ip
->i_d
.di_nlink
== 0);
980 * If this is a synchronous mount, make sure that the
981 * remove transaction goes to disk before returning to
984 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
))
985 xfs_trans_set_sync(tp
);
987 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
989 goto out_bmap_cancel
;
991 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
996 * If we are using filestreams, kill the stream association.
997 * If the file is still open it may get a new one but that
998 * will get killed on last close in xfs_close() so we don't
999 * have to worry about that.
1001 if (!is_dir
&& link_zero
&& xfs_inode_is_filestream(ip
))
1002 xfs_filestream_deassociate(ip
);
1007 xfs_bmap_cancel(&free_list
);
1008 cancel_flags
|= XFS_TRANS_ABORT
;
1010 xfs_trans_cancel(tp
, cancel_flags
);
1019 struct xfs_name
*target_name
)
1021 xfs_mount_t
*mp
= tdp
->i_mount
;
1024 xfs_bmap_free_t free_list
;
1025 xfs_fsblock_t first_block
;
1030 trace_xfs_link(tdp
, target_name
);
1032 ASSERT(!S_ISDIR(sip
->i_d
.di_mode
));
1034 if (XFS_FORCED_SHUTDOWN(mp
))
1035 return XFS_ERROR(EIO
);
1037 error
= xfs_qm_dqattach(sip
, 0);
1041 error
= xfs_qm_dqattach(tdp
, 0);
1045 tp
= xfs_trans_alloc(mp
, XFS_TRANS_LINK
);
1046 cancel_flags
= XFS_TRANS_RELEASE_LOG_RES
;
1047 resblks
= XFS_LINK_SPACE_RES(mp
, target_name
->len
);
1048 error
= xfs_trans_reserve(tp
, resblks
, XFS_LINK_LOG_RES(mp
), 0,
1049 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
1050 if (error
== ENOSPC
) {
1052 error
= xfs_trans_reserve(tp
, 0, XFS_LINK_LOG_RES(mp
), 0,
1053 XFS_TRANS_PERM_LOG_RES
, XFS_LINK_LOG_COUNT
);
1060 xfs_lock_two_inodes(sip
, tdp
, XFS_ILOCK_EXCL
);
1062 xfs_trans_ijoin(tp
, sip
, XFS_ILOCK_EXCL
);
1063 xfs_trans_ijoin(tp
, tdp
, XFS_ILOCK_EXCL
);
1066 * If we are using project inheritance, we only allow hard link
1067 * creation in our tree when the project IDs are the same; else
1068 * the tree quota mechanism could be circumvented.
1070 if (unlikely((tdp
->i_d
.di_flags
& XFS_DIFLAG_PROJINHERIT
) &&
1071 (xfs_get_projid(tdp
) != xfs_get_projid(sip
)))) {
1072 error
= XFS_ERROR(EXDEV
);
1076 error
= xfs_dir_canenter(tp
, tdp
, target_name
, resblks
);
1080 xfs_bmap_init(&free_list
, &first_block
);
1082 error
= xfs_dir_createname(tp
, tdp
, target_name
, sip
->i_ino
,
1083 &first_block
, &free_list
, resblks
);
1086 xfs_trans_ichgtime(tp
, tdp
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1087 xfs_trans_log_inode(tp
, tdp
, XFS_ILOG_CORE
);
1089 error
= xfs_bumplink(tp
, sip
);
1094 * If this is a synchronous mount, make sure that the
1095 * link transaction goes to disk before returning to
1098 if (mp
->m_flags
& (XFS_MOUNT_WSYNC
|XFS_MOUNT_DIRSYNC
)) {
1099 xfs_trans_set_sync(tp
);
1102 error
= xfs_bmap_finish (&tp
, &free_list
, &committed
);
1104 xfs_bmap_cancel(&free_list
);
1108 return xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1111 cancel_flags
|= XFS_TRANS_ABORT
;
1113 xfs_trans_cancel(tp
, cancel_flags
);
1124 xfs_mount_t
*mp
= ip
->i_mount
;
1128 if (!capable(CAP_SYS_ADMIN
))
1129 return XFS_ERROR(EPERM
);
1131 if (XFS_FORCED_SHUTDOWN(mp
))
1132 return XFS_ERROR(EIO
);
1134 tp
= xfs_trans_alloc(mp
, XFS_TRANS_SET_DMATTRS
);
1135 error
= xfs_trans_reserve(tp
, 0, XFS_ICHANGE_LOG_RES (mp
), 0, 0, 0);
1137 xfs_trans_cancel(tp
, 0);
1140 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1141 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1143 ip
->i_d
.di_dmevmask
= evmask
;
1144 ip
->i_d
.di_dmstate
= state
;
1146 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1147 error
= xfs_trans_commit(tp
, 0);
1153 * xfs_alloc_file_space()
1154 * This routine allocates disk space for the given file.
1156 * If alloc_type == 0, this request is for an ALLOCSP type
1157 * request which will change the file size. In this case, no
1158 * DMAPI event will be generated by the call. A TRUNCATE event
1159 * will be generated later by xfs_setattr.
1161 * If alloc_type != 0, this request is for a RESVSP type
1162 * request, and a DMAPI DM_EVENT_WRITE will be generated if the
1163 * lower block boundary byte address is less than the file's
1172 xfs_alloc_file_space(
1179 xfs_mount_t
*mp
= ip
->i_mount
;
1181 xfs_filblks_t allocated_fsb
;
1182 xfs_filblks_t allocatesize_fsb
;
1183 xfs_extlen_t extsz
, temp
;
1184 xfs_fileoff_t startoffset_fsb
;
1185 xfs_fsblock_t firstfsb
;
1190 xfs_bmbt_irec_t imaps
[1], *imapp
;
1191 xfs_bmap_free_t free_list
;
1192 uint qblocks
, resblks
, resrtextents
;
1196 trace_xfs_alloc_file_space(ip
);
1198 if (XFS_FORCED_SHUTDOWN(mp
))
1199 return XFS_ERROR(EIO
);
1201 error
= xfs_qm_dqattach(ip
, 0);
1206 return XFS_ERROR(EINVAL
);
1208 rt
= XFS_IS_REALTIME_INODE(ip
);
1209 extsz
= xfs_get_extsz_hint(ip
);
1214 startoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
1215 allocatesize_fsb
= XFS_B_TO_FSB(mp
, count
);
1218 * Allocate file space until done or until there is an error
1220 while (allocatesize_fsb
&& !error
) {
1224 * Determine space reservations for data/realtime.
1226 if (unlikely(extsz
)) {
1227 s
= startoffset_fsb
;
1230 e
= startoffset_fsb
+ allocatesize_fsb
;
1231 if ((temp
= do_mod(startoffset_fsb
, extsz
)))
1233 if ((temp
= do_mod(e
, extsz
)))
1237 e
= allocatesize_fsb
;
1241 * The transaction reservation is limited to a 32-bit block
1242 * count, hence we need to limit the number of blocks we are
1243 * trying to reserve to avoid an overflow. We can't allocate
1244 * more than @nimaps extents, and an extent is limited on disk
1245 * to MAXEXTLEN (21 bits), so use that to enforce the limit.
1247 resblks
= min_t(xfs_fileoff_t
, (e
- s
), (MAXEXTLEN
* nimaps
));
1249 resrtextents
= qblocks
= resblks
;
1250 resrtextents
/= mp
->m_sb
.sb_rextsize
;
1251 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
1252 quota_flag
= XFS_QMOPT_RES_RTBLKS
;
1255 resblks
= qblocks
= XFS_DIOSTRAT_SPACE_RES(mp
, resblks
);
1256 quota_flag
= XFS_QMOPT_RES_REGBLKS
;
1260 * Allocate and setup the transaction.
1262 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
1263 error
= xfs_trans_reserve(tp
, resblks
,
1264 XFS_WRITE_LOG_RES(mp
), resrtextents
,
1265 XFS_TRANS_PERM_LOG_RES
,
1266 XFS_WRITE_LOG_COUNT
);
1268 * Check for running out of space
1272 * Free the transaction structure.
1274 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
1275 xfs_trans_cancel(tp
, 0);
1278 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1279 error
= xfs_trans_reserve_quota_nblks(tp
, ip
, qblocks
,
1284 xfs_trans_ijoin(tp
, ip
, 0);
1286 xfs_bmap_init(&free_list
, &firstfsb
);
1287 error
= xfs_bmapi_write(tp
, ip
, startoffset_fsb
,
1288 allocatesize_fsb
, alloc_type
, &firstfsb
,
1289 0, imapp
, &nimaps
, &free_list
);
1295 * Complete the transaction
1297 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1302 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1303 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1308 allocated_fsb
= imapp
->br_blockcount
;
1311 error
= XFS_ERROR(ENOSPC
);
1315 startoffset_fsb
+= allocated_fsb
;
1316 allocatesize_fsb
-= allocated_fsb
;
1321 error0
: /* Cancel bmap, unlock inode, unreserve quota blocks, cancel trans */
1322 xfs_bmap_cancel(&free_list
);
1323 xfs_trans_unreserve_quota_nblks(tp
, ip
, (long)qblocks
, 0, quota_flag
);
1325 error1
: /* Just cancel transaction */
1326 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1327 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1332 * Zero file bytes between startoff and endoff inclusive.
1333 * The iolock is held exclusive and no blocks are buffered.
1335 * This function is used by xfs_free_file_space() to zero
1336 * partial blocks when the range to free is not block aligned.
1337 * When unreserving space with boundaries that are not block
1338 * aligned we round up the start and round down the end
1339 * boundaries and then use this function to zero the parts of
1340 * the blocks that got dropped during the rounding.
1343 xfs_zero_remaining_bytes(
1348 xfs_bmbt_irec_t imap
;
1349 xfs_fileoff_t offset_fsb
;
1350 xfs_off_t lastoffset
;
1353 xfs_mount_t
*mp
= ip
->i_mount
;
1358 * Avoid doing I/O beyond eof - it's not necessary
1359 * since nothing can read beyond eof. The space will
1360 * be zeroed when the file is extended anyway.
1362 if (startoff
>= XFS_ISIZE(ip
))
1365 if (endoff
> XFS_ISIZE(ip
))
1366 endoff
= XFS_ISIZE(ip
);
1368 bp
= xfs_buf_get_uncached(XFS_IS_REALTIME_INODE(ip
) ?
1369 mp
->m_rtdev_targp
: mp
->m_ddev_targp
,
1370 BTOBB(mp
->m_sb
.sb_blocksize
), 0);
1372 return XFS_ERROR(ENOMEM
);
1376 for (offset
= startoff
; offset
<= endoff
; offset
= lastoffset
+ 1) {
1377 offset_fsb
= XFS_B_TO_FSBT(mp
, offset
);
1379 error
= xfs_bmapi_read(ip
, offset_fsb
, 1, &imap
, &nimap
, 0);
1380 if (error
|| nimap
< 1)
1382 ASSERT(imap
.br_blockcount
>= 1);
1383 ASSERT(imap
.br_startoff
== offset_fsb
);
1384 lastoffset
= XFS_FSB_TO_B(mp
, imap
.br_startoff
+ 1) - 1;
1385 if (lastoffset
> endoff
)
1386 lastoffset
= endoff
;
1387 if (imap
.br_startblock
== HOLESTARTBLOCK
)
1389 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
1390 if (imap
.br_state
== XFS_EXT_UNWRITTEN
)
1393 XFS_BUF_UNWRITE(bp
);
1395 XFS_BUF_SET_ADDR(bp
, xfs_fsb_to_db(ip
, imap
.br_startblock
));
1397 error
= xfs_buf_iowait(bp
);
1399 xfs_buf_ioerror_alert(bp
,
1400 "xfs_zero_remaining_bytes(read)");
1404 (offset
- XFS_FSB_TO_B(mp
, imap
.br_startoff
)),
1405 0, lastoffset
- offset
+ 1);
1410 error
= xfs_buf_iowait(bp
);
1412 xfs_buf_ioerror_alert(bp
,
1413 "xfs_zero_remaining_bytes(write)");
1422 * xfs_free_file_space()
1423 * This routine frees disk space for the given file.
1425 * This routine is only called by xfs_change_file_space
1426 * for an UNRESVSP type call.
1434 xfs_free_file_space(
1442 xfs_fileoff_t endoffset_fsb
;
1444 xfs_fsblock_t firstfsb
;
1445 xfs_bmap_free_t free_list
;
1446 xfs_bmbt_irec_t imap
;
1454 xfs_fileoff_t startoffset_fsb
;
1456 int need_iolock
= 1;
1460 trace_xfs_free_file_space(ip
);
1462 error
= xfs_qm_dqattach(ip
, 0);
1467 if (len
<= 0) /* if nothing being freed */
1469 rt
= XFS_IS_REALTIME_INODE(ip
);
1470 startoffset_fsb
= XFS_B_TO_FSB(mp
, offset
);
1471 endoffset_fsb
= XFS_B_TO_FSBT(mp
, offset
+ len
);
1473 if (attr_flags
& XFS_ATTR_NOLOCK
)
1476 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
1477 /* wait for the completion of any pending DIOs */
1478 inode_dio_wait(VFS_I(ip
));
1481 rounding
= max_t(xfs_off_t
, 1 << mp
->m_sb
.sb_blocklog
, PAGE_CACHE_SIZE
);
1482 ioffset
= offset
& ~(rounding
- 1);
1483 error
= -filemap_write_and_wait_range(VFS_I(ip
)->i_mapping
,
1486 goto out_unlock_iolock
;
1487 truncate_pagecache_range(VFS_I(ip
), ioffset
, -1);
1490 * Need to zero the stuff we're not freeing, on disk.
1491 * If it's a realtime file & can't use unwritten extents then we
1492 * actually need to zero the extent edges. Otherwise xfs_bunmapi
1493 * will take care of it for us.
1495 if (rt
&& !xfs_sb_version_hasextflgbit(&mp
->m_sb
)) {
1497 error
= xfs_bmapi_read(ip
, startoffset_fsb
, 1,
1500 goto out_unlock_iolock
;
1501 ASSERT(nimap
== 0 || nimap
== 1);
1502 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
1505 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
1506 block
= imap
.br_startblock
;
1507 mod
= do_div(block
, mp
->m_sb
.sb_rextsize
);
1509 startoffset_fsb
+= mp
->m_sb
.sb_rextsize
- mod
;
1512 error
= xfs_bmapi_read(ip
, endoffset_fsb
- 1, 1,
1515 goto out_unlock_iolock
;
1516 ASSERT(nimap
== 0 || nimap
== 1);
1517 if (nimap
&& imap
.br_startblock
!= HOLESTARTBLOCK
) {
1518 ASSERT(imap
.br_startblock
!= DELAYSTARTBLOCK
);
1520 if (mod
&& (mod
!= mp
->m_sb
.sb_rextsize
))
1521 endoffset_fsb
-= mod
;
1524 if ((done
= (endoffset_fsb
<= startoffset_fsb
)))
1526 * One contiguous piece to clear
1528 error
= xfs_zero_remaining_bytes(ip
, offset
, offset
+ len
- 1);
1531 * Some full blocks, possibly two pieces to clear
1533 if (offset
< XFS_FSB_TO_B(mp
, startoffset_fsb
))
1534 error
= xfs_zero_remaining_bytes(ip
, offset
,
1535 XFS_FSB_TO_B(mp
, startoffset_fsb
) - 1);
1537 XFS_FSB_TO_B(mp
, endoffset_fsb
) < offset
+ len
)
1538 error
= xfs_zero_remaining_bytes(ip
,
1539 XFS_FSB_TO_B(mp
, endoffset_fsb
),
1544 * free file space until done or until there is an error
1546 resblks
= XFS_DIOSTRAT_SPACE_RES(mp
, 0);
1547 while (!error
&& !done
) {
1550 * allocate and setup the transaction. Allow this
1551 * transaction to dip into the reserve blocks to ensure
1552 * the freeing of the space succeeds at ENOSPC.
1554 tp
= xfs_trans_alloc(mp
, XFS_TRANS_DIOSTRAT
);
1555 tp
->t_flags
|= XFS_TRANS_RESERVE
;
1556 error
= xfs_trans_reserve(tp
,
1558 XFS_WRITE_LOG_RES(mp
),
1560 XFS_TRANS_PERM_LOG_RES
,
1561 XFS_WRITE_LOG_COUNT
);
1564 * check for running out of space
1568 * Free the transaction structure.
1570 ASSERT(error
== ENOSPC
|| XFS_FORCED_SHUTDOWN(mp
));
1571 xfs_trans_cancel(tp
, 0);
1574 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1575 error
= xfs_trans_reserve_quota(tp
, mp
,
1576 ip
->i_udquot
, ip
->i_gdquot
, ip
->i_pdquot
,
1577 resblks
, 0, XFS_QMOPT_RES_REGBLKS
);
1581 xfs_trans_ijoin(tp
, ip
, 0);
1584 * issue the bunmapi() call to free the blocks
1586 xfs_bmap_init(&free_list
, &firstfsb
);
1587 error
= xfs_bunmapi(tp
, ip
, startoffset_fsb
,
1588 endoffset_fsb
- startoffset_fsb
,
1589 0, 2, &firstfsb
, &free_list
, &done
);
1595 * complete the transaction
1597 error
= xfs_bmap_finish(&tp
, &free_list
, &committed
);
1602 error
= xfs_trans_commit(tp
, XFS_TRANS_RELEASE_LOG_RES
);
1603 xfs_iunlock(ip
, XFS_ILOCK_EXCL
);
1608 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1612 xfs_bmap_cancel(&free_list
);
1614 xfs_trans_cancel(tp
, XFS_TRANS_RELEASE_LOG_RES
| XFS_TRANS_ABORT
);
1615 xfs_iunlock(ip
, need_iolock
? (XFS_ILOCK_EXCL
| XFS_IOLOCK_EXCL
) :
1622 xfs_zero_file_space(
1623 struct xfs_inode
*ip
,
1628 struct xfs_mount
*mp
= ip
->i_mount
;
1630 xfs_off_t start_boundary
;
1631 xfs_off_t end_boundary
;
1634 granularity
= max_t(uint
, 1 << mp
->m_sb
.sb_blocklog
, PAGE_CACHE_SIZE
);
1637 * Round the range of extents we are going to convert inwards. If the
1638 * offset is aligned, then it doesn't get changed so we zero from the
1639 * start of the block offset points to.
1641 start_boundary
= round_up(offset
, granularity
);
1642 end_boundary
= round_down(offset
+ len
, granularity
);
1644 ASSERT(start_boundary
>= offset
);
1645 ASSERT(end_boundary
<= offset
+ len
);
1647 if (!(attr_flags
& XFS_ATTR_NOLOCK
))
1648 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
1650 if (start_boundary
< end_boundary
- 1) {
1651 /* punch out the page cache over the conversion range */
1652 truncate_pagecache_range(VFS_I(ip
), start_boundary
,
1654 /* convert the blocks */
1655 error
= xfs_alloc_file_space(ip
, start_boundary
,
1656 end_boundary
- start_boundary
- 1,
1657 XFS_BMAPI_PREALLOC
| XFS_BMAPI_CONVERT
,
1662 /* We've handled the interior of the range, now for the edges */
1663 if (start_boundary
!= offset
)
1664 error
= xfs_iozero(ip
, offset
, start_boundary
- offset
);
1668 if (end_boundary
!= offset
+ len
)
1669 error
= xfs_iozero(ip
, end_boundary
,
1670 offset
+ len
- end_boundary
);
1674 * It's either a sub-granularity range or the range spanned lies
1675 * partially across two adjacent blocks.
1677 error
= xfs_iozero(ip
, offset
, len
);
1681 if (!(attr_flags
& XFS_ATTR_NOLOCK
))
1682 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1688 * xfs_change_file_space()
1689 * This routine allocates or frees disk space for the given file.
1690 * The user specified parameters are checked for alignment and size
1699 xfs_change_file_space(
1706 xfs_mount_t
*mp
= ip
->i_mount
;
1711 xfs_off_t startoffset
;
1715 if (!S_ISREG(ip
->i_d
.di_mode
))
1716 return XFS_ERROR(EINVAL
);
1718 switch (bf
->l_whence
) {
1719 case 0: /*SEEK_SET*/
1721 case 1: /*SEEK_CUR*/
1722 bf
->l_start
+= offset
;
1724 case 2: /*SEEK_END*/
1725 bf
->l_start
+= XFS_ISIZE(ip
);
1728 return XFS_ERROR(EINVAL
);
1732 * length of <= 0 for resv/unresv/zero is invalid. length for
1733 * alloc/free is ignored completely and we have no idea what userspace
1734 * might have set it to, so set it to zero to allow range
1738 case XFS_IOC_ZERO_RANGE
:
1739 case XFS_IOC_RESVSP
:
1740 case XFS_IOC_RESVSP64
:
1741 case XFS_IOC_UNRESVSP
:
1742 case XFS_IOC_UNRESVSP64
:
1744 return XFS_ERROR(EINVAL
);
1751 if (bf
->l_start
< 0 ||
1752 bf
->l_start
> mp
->m_super
->s_maxbytes
||
1753 bf
->l_start
+ bf
->l_len
< 0 ||
1754 bf
->l_start
+ bf
->l_len
>= mp
->m_super
->s_maxbytes
)
1755 return XFS_ERROR(EINVAL
);
1759 startoffset
= bf
->l_start
;
1760 fsize
= XFS_ISIZE(ip
);
1762 setprealloc
= clrprealloc
= 0;
1764 case XFS_IOC_ZERO_RANGE
:
1765 error
= xfs_zero_file_space(ip
, startoffset
, bf
->l_len
,
1772 case XFS_IOC_RESVSP
:
1773 case XFS_IOC_RESVSP64
:
1774 error
= xfs_alloc_file_space(ip
, startoffset
, bf
->l_len
,
1775 XFS_BMAPI_PREALLOC
, attr_flags
);
1781 case XFS_IOC_UNRESVSP
:
1782 case XFS_IOC_UNRESVSP64
:
1783 if ((error
= xfs_free_file_space(ip
, startoffset
, bf
->l_len
,
1788 case XFS_IOC_ALLOCSP
:
1789 case XFS_IOC_ALLOCSP64
:
1790 case XFS_IOC_FREESP
:
1791 case XFS_IOC_FREESP64
:
1793 * These operations actually do IO when extending the file, but
1794 * the allocation is done seperately to the zeroing that is
1795 * done. This set of operations need to be serialised against
1796 * other IO operations, such as truncate and buffered IO. We
1797 * need to take the IOLOCK here to serialise the allocation and
1798 * zeroing IO to prevent other IOLOCK holders (e.g. getbmap,
1799 * truncate, direct IO) from racing against the transient
1800 * allocated but not written state we can have here.
1802 xfs_ilock(ip
, XFS_IOLOCK_EXCL
);
1803 if (startoffset
> fsize
) {
1804 error
= xfs_alloc_file_space(ip
, fsize
,
1805 startoffset
- fsize
, 0,
1806 attr_flags
| XFS_ATTR_NOLOCK
);
1808 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1813 iattr
.ia_valid
= ATTR_SIZE
;
1814 iattr
.ia_size
= startoffset
;
1816 error
= xfs_setattr_size(ip
, &iattr
,
1817 attr_flags
| XFS_ATTR_NOLOCK
);
1818 xfs_iunlock(ip
, XFS_IOLOCK_EXCL
);
1828 return XFS_ERROR(EINVAL
);
1832 * update the inode timestamp, mode, and prealloc flag bits
1834 tp
= xfs_trans_alloc(mp
, XFS_TRANS_WRITEID
);
1836 if ((error
= xfs_trans_reserve(tp
, 0, XFS_WRITEID_LOG_RES(mp
),
1839 xfs_trans_cancel(tp
, 0);
1843 xfs_ilock(ip
, XFS_ILOCK_EXCL
);
1844 xfs_trans_ijoin(tp
, ip
, XFS_ILOCK_EXCL
);
1846 if ((attr_flags
& XFS_ATTR_DMI
) == 0) {
1847 ip
->i_d
.di_mode
&= ~S_ISUID
;
1850 * Note that we don't have to worry about mandatory
1851 * file locking being disabled here because we only
1852 * clear the S_ISGID bit if the Group execute bit is
1853 * on, but if it was on then mandatory locking wouldn't
1854 * have been enabled.
1856 if (ip
->i_d
.di_mode
& S_IXGRP
)
1857 ip
->i_d
.di_mode
&= ~S_ISGID
;
1859 xfs_trans_ichgtime(tp
, ip
, XFS_ICHGTIME_MOD
| XFS_ICHGTIME_CHG
);
1862 ip
->i_d
.di_flags
|= XFS_DIFLAG_PREALLOC
;
1863 else if (clrprealloc
)
1864 ip
->i_d
.di_flags
&= ~XFS_DIFLAG_PREALLOC
;
1866 xfs_trans_log_inode(tp
, ip
, XFS_ILOG_CORE
);
1867 if (attr_flags
& XFS_ATTR_SYNC
)
1868 xfs_trans_set_sync(tp
);
1869 return xfs_trans_commit(tp
, 0);