Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 : * Copyright (c) 2012 Red Hat, Inc.
5 : * All Rights Reserved.
6 : */
7 : #include "xfs.h"
8 : #include "xfs_fs.h"
9 : #include "xfs_shared.h"
10 : #include "xfs_format.h"
11 : #include "xfs_log_format.h"
12 : #include "xfs_trans_resv.h"
13 : #include "xfs_bit.h"
14 : #include "xfs_mount.h"
15 : #include "xfs_defer.h"
16 : #include "xfs_inode.h"
17 : #include "xfs_btree.h"
18 : #include "xfs_trans.h"
19 : #include "xfs_alloc.h"
20 : #include "xfs_bmap.h"
21 : #include "xfs_bmap_util.h"
22 : #include "xfs_bmap_btree.h"
23 : #include "xfs_rtalloc.h"
24 : #include "xfs_error.h"
25 : #include "xfs_quota.h"
26 : #include "xfs_trans_space.h"
27 : #include "xfs_trace.h"
28 : #include "xfs_icache.h"
29 : #include "xfs_iomap.h"
30 : #include "xfs_reflink.h"
31 : #include "xfs_swapext.h"
32 : #include "xfs_rtbitmap.h"
33 : #include "xfs_rtrmap_btree.h"
34 : #include "xfs_rtrefcount_btree.h"
35 : #include "xfs_health.h"
36 : #include "xfs_alloc_btree.h"
37 : #include "xfs_rmap.h"
38 : #include "xfs_ag.h"
39 : #include "xfs_rtbitmap.h"
40 :
41 : /* Kernel only BMAP related definitions and functions */
42 :
43 : /*
44 : * Convert the given file system block to a disk block. We have to treat it
45 : * differently based on whether the file is a real time file or not, because the
46 : * bmap code does.
47 : */
48 : xfs_daddr_t
49 699769796 : xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
50 : {
51 699769796 : if (XFS_IS_REALTIME_INODE(ip))
52 416283071 : return XFS_FSB_TO_BB(ip->i_mount, fsb);
53 283486725 : return XFS_FSB_TO_DADDR(ip->i_mount, fsb);
54 : }
55 :
56 : /*
57 : * Routine to zero an extent on disk allocated to the specific inode.
58 : *
59 : * The VFS functions take a linearised filesystem block offset, so we have to
60 : * convert the sparse xfs fsb to the right format first.
61 : * VFS types are real funky, too.
62 : */
63 : int
64 361515 : xfs_zero_extent(
65 : struct xfs_inode *ip,
66 : xfs_fsblock_t start_fsb,
67 : xfs_off_t count_fsb)
68 : {
69 361515 : struct xfs_mount *mp = ip->i_mount;
70 361515 : struct xfs_buftarg *target = xfs_inode_buftarg(ip);
71 361515 : xfs_daddr_t sector = xfs_fsb_to_db(ip, start_fsb);
72 361514 : sector_t block = XFS_BB_TO_FSBT(mp, sector);
73 :
74 1446056 : return xfs_buftarg_zeroout(target,
75 361514 : block << (mp->m_super->s_blocksize_bits - 9),
76 361514 : count_fsb << (mp->m_super->s_blocksize_bits - 9),
77 : GFP_NOFS, 0);
78 : }
79 :
80 : #ifdef CONFIG_XFS_RT
81 :
82 : /* Update all inode and quota accounting for the allocation we just did. */
83 : static void
84 86767147 : xfs_bmap_rtalloc_accounting(
85 : struct xfs_bmalloca *ap)
86 : {
87 86767147 : if (ap->flags & XFS_BMAPI_COWFORK) {
88 : /*
89 : * COW fork blocks are in-core only and thus are treated as
90 : * in-core quota reservation (like delalloc blocks) even when
91 : * converted to real blocks. The quota reservation is not
92 : * accounted to disk until blocks are remapped to the data
93 : * fork. So if these blocks were previously delalloc, we
94 : * already have quota reservation and there's nothing to do
95 : * yet.
96 : */
97 871131 : if (ap->wasdel) {
98 0 : xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)ap->length);
99 0 : return;
100 : }
101 :
102 : /*
103 : * Otherwise, we've allocated blocks in a hole. The transaction
104 : * has acquired in-core quota reservation for this extent.
105 : * Rather than account these as real blocks, however, we reduce
106 : * the transaction quota reservation based on the allocation.
107 : * This essentially transfers the transaction quota reservation
108 : * to that of a delalloc extent.
109 : */
110 871131 : ap->ip->i_delayed_blks += ap->length;
111 871131 : xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
112 871131 : XFS_TRANS_DQ_RES_RTBLKS, -(long)ap->length);
113 871131 : return;
114 : }
115 :
116 : /* data fork only */
117 85896016 : ap->ip->i_nblocks += ap->length;
118 85896016 : xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
119 85896016 : if (ap->wasdel) {
120 0 : ap->ip->i_delayed_blks -= ap->length;
121 0 : xfs_mod_delalloc(ap->ip->i_mount, -(int64_t)ap->length);
122 : }
123 :
124 : /* Adjust the disk quota also. This was reserved earlier. */
125 85896016 : xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
126 85896016 : ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
127 85896016 : XFS_TRANS_DQ_RTBCOUNT, ap->length);
128 : }
129 :
130 : int
131 86695081 : xfs_bmap_rtalloc(
132 : struct xfs_bmalloca *ap)
133 : {
134 86695081 : struct xfs_mount *mp = ap->ip->i_mount;
135 86695081 : xfs_fileoff_t orig_offset = ap->offset;
136 86695081 : xfs_rtxnum_t rtx;
137 86695081 : xfs_rtxlen_t prod = 0; /* product factor for allocators */
138 86695081 : xfs_extlen_t mod = 0; /* product factor for allocators */
139 86695081 : xfs_rtxlen_t ralen = 0; /* realtime allocation length */
140 86695081 : xfs_extlen_t align; /* minimum allocation alignment */
141 86695081 : xfs_extlen_t orig_length = ap->length;
142 86695081 : xfs_extlen_t minlen = mp->m_sb.sb_rextsize;
143 86695081 : xfs_rtxlen_t raminlen;
144 86695081 : bool rtlocked = false;
145 86695081 : bool ignore_locality = false;
146 86695081 : int error;
147 :
148 86695081 : if (ap->flags & XFS_BMAPI_COWFORK)
149 871118 : align = xfs_get_cowextsz_hint(ap->ip);
150 : else
151 85823963 : align = xfs_get_extsz_hint(ap->ip);
152 : retry:
153 86663234 : prod = xfs_extlen_to_rtxlen(mp, align);
154 259956756 : error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
155 86652252 : align, 1, ap->eof, 0,
156 86652252 : ap->conv, &ap->offset, &ap->length);
157 86639374 : if (error)
158 0 : return error;
159 86639374 : ASSERT(ap->length);
160 86639374 : ASSERT(xfs_extlen_to_rtxmod(mp, ap->length) == 0);
161 :
162 : /*
163 : * If we shifted the file offset downward to satisfy an extent size
164 : * hint, increase minlen by that amount so that the allocator won't
165 : * give us an allocation that's too short to cover at least one of the
166 : * blocks that the caller asked for.
167 : */
168 86603910 : if (ap->offset != orig_offset)
169 2577104 : minlen += orig_offset - ap->offset;
170 :
171 : /*
172 : * If the offset & length are not perfectly aligned
173 : * then kill prod, it will just get us in trouble.
174 : */
175 86603910 : div_u64_rem(ap->offset, align, &mod);
176 86611779 : if (mod || ap->length % align)
177 : prod = 1;
178 : /*
179 : * Set ralen to be the actual requested length in rtextents.
180 : *
181 : * If the old value was close enough to XFS_BMBT_MAX_EXTLEN that
182 : * we rounded up to it, cut it back so it's valid again.
183 : * Note that if it's a really large request (bigger than
184 : * XFS_BMBT_MAX_EXTLEN), we don't hear about that number, and can't
185 : * adjust the starting point to match it.
186 : */
187 86611779 : ralen = xfs_extlen_to_rtxlen(mp, min(ap->length, XFS_MAX_BMBT_EXTLEN));
188 :
189 : /*
190 : * Lock out modifications to both the RT bitmap and summary inodes
191 : */
192 86636495 : if (!rtlocked) {
193 86636083 : xfs_rtbitmap_lock(ap->tp, mp);
194 86636083 : rtlocked = true;
195 : }
196 :
197 : /*
198 : * If it's an allocation to an empty file at offset 0,
199 : * pick an extent that will space things out in the rt area.
200 : */
201 86767569 : if (ap->eof && ap->offset == 0) {
202 6310155 : error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
203 6310155 : if (error)
204 0 : return error;
205 6310155 : ap->blkno = xfs_rtx_to_rtb(mp, rtx);
206 : } else {
207 80457414 : ap->blkno = 0;
208 : }
209 :
210 86767569 : xfs_bmap_adjacent(ap);
211 :
212 : /*
213 : * Realtime allocation, done through xfs_rtallocate_extent.
214 : */
215 86767569 : if (ignore_locality)
216 32 : rtx = 0;
217 : else
218 86767537 : rtx = xfs_rtb_to_rtxt(mp, ap->blkno);
219 86767569 : raminlen = max_t(xfs_rtxlen_t, 1, xfs_extlen_to_rtxlen(mp, minlen));
220 173535138 : error = xfs_rtallocate_extent(ap->tp, rtx, raminlen, ralen, &ralen,
221 86767569 : ap->wasdel, prod, &rtx);
222 86767569 : if (error)
223 10 : return error;
224 :
225 86767559 : if (rtx != NULLRTEXTNO) {
226 86767147 : ap->blkno = xfs_rtx_to_rtb(mp, rtx);
227 86767147 : ap->length = xfs_rtxlen_to_extlen(mp, ralen);
228 86767147 : xfs_bmap_rtalloc_accounting(ap);
229 86767147 : return 0;
230 : }
231 :
232 412 : if (align > mp->m_sb.sb_rextsize) {
233 : /*
234 : * We previously enlarged the request length to try to satisfy
235 : * an extent size hint. The allocator didn't return anything,
236 : * so reset the parameters to the original values and try again
237 : * without alignment criteria.
238 : */
239 380 : ap->offset = orig_offset;
240 380 : ap->length = orig_length;
241 380 : minlen = align = mp->m_sb.sb_rextsize;
242 380 : goto retry;
243 : }
244 :
245 32 : if (!ignore_locality && ap->blkno != 0) {
246 : /*
247 : * If we can't allocate near a specific rt extent, try again
248 : * without locality criteria.
249 : */
250 32 : ignore_locality = true;
251 32 : goto retry;
252 : }
253 :
254 0 : ap->blkno = NULLFSBLOCK;
255 0 : ap->length = 0;
256 0 : return 0;
257 : }
258 : #endif /* CONFIG_XFS_RT */
259 :
260 : /*
261 : * Extent tree block counting routines.
262 : */
263 :
264 : /*
265 : * Count leaf blocks given a range of extent records. Delayed allocation
266 : * extents are not counted towards the totals.
267 : */
268 : xfs_extnum_t
269 175787085 : xfs_bmap_count_leaves(
270 : struct xfs_ifork *ifp,
271 : xfs_filblks_t *count)
272 : {
273 175787085 : struct xfs_iext_cursor icur;
274 175787085 : struct xfs_bmbt_irec got;
275 175787085 : xfs_extnum_t numrecs = 0;
276 :
277 1687473063 : for_each_xfs_iext(ifp, &icur, &got) {
278 1511685978 : if (!isnullstartblock(got.br_startblock)) {
279 1511681246 : *count += got.br_blockcount;
280 1511681246 : numrecs++;
281 : }
282 : }
283 :
284 175686600 : return numrecs;
285 : }
286 :
287 : /*
288 : * Count fsblocks of the given fork. Delayed allocation extents are
289 : * not counted towards the totals.
290 : */
291 : int
292 269967316 : xfs_bmap_count_blocks(
293 : struct xfs_trans *tp,
294 : struct xfs_inode *ip,
295 : int whichfork,
296 : xfs_extnum_t *nextents,
297 : xfs_filblks_t *count)
298 : {
299 269967316 : struct xfs_mount *mp = ip->i_mount;
300 269967316 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
301 270026372 : struct xfs_btree_cur *cur;
302 270026372 : xfs_extlen_t btblocks = 0;
303 270026372 : int error;
304 :
305 270026372 : *nextents = 0;
306 270026372 : *count = 0;
307 :
308 270026372 : if (!ifp)
309 : return 0;
310 :
311 270026372 : switch (ifp->if_format) {
312 2499807 : case XFS_DINODE_FMT_BTREE:
313 2499807 : error = xfs_iread_extents(tp, ip, whichfork);
314 2499826 : if (error)
315 : return error;
316 :
317 2499830 : cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
318 2499980 : error = xfs_btree_count_blocks(cur, &btblocks);
319 2499878 : xfs_btree_del_cursor(cur, error);
320 2499794 : if (error)
321 : return error;
322 :
323 : /*
324 : * xfs_btree_count_blocks includes the root block contained in
325 : * the inode fork in @btblocks, so subtract one because we're
326 : * only interested in allocated disk blocks.
327 : */
328 2499794 : *count += btblocks - 1;
329 :
330 78553294 : fallthrough;
331 78553294 : case XFS_DINODE_FMT_EXTENTS:
332 78553294 : *nextents = xfs_bmap_count_leaves(ifp, count);
333 78532353 : break;
334 : }
335 :
336 : return 0;
337 : }
338 :
339 : static int
340 7237153 : xfs_getbmap_report_one(
341 : struct xfs_inode *ip,
342 : struct getbmapx *bmv,
343 : struct kgetbmap *out,
344 : int64_t bmv_end,
345 : struct xfs_bmbt_irec *got)
346 : {
347 7237153 : struct kgetbmap *p = out + bmv->bmv_entries;
348 7237153 : bool shared = false;
349 7237153 : int error;
350 :
351 7237153 : error = xfs_reflink_trim_around_shared(ip, got, &shared);
352 7237115 : if (error)
353 : return error;
354 :
355 7237115 : if (isnullstartblock(got->br_startblock) ||
356 : got->br_startblock == DELAYSTARTBLOCK) {
357 : /*
358 : * Take the flush completion as being a point-in-time snapshot
359 : * where there are no delalloc extents, and if any new ones
360 : * have been created racily, just skip them as being 'after'
361 : * the flush and so don't get reported.
362 : */
363 21399 : if (!(bmv->bmv_iflags & BMV_IF_DELALLOC))
364 : return 0;
365 :
366 21399 : p->bmv_oflags |= BMV_OF_DELALLOC;
367 21399 : p->bmv_block = -2;
368 : } else {
369 7215716 : p->bmv_block = xfs_fsb_to_db(ip, got->br_startblock);
370 : }
371 :
372 7237114 : if (got->br_state == XFS_EXT_UNWRITTEN &&
373 2684643 : (bmv->bmv_iflags & BMV_IF_PREALLOC))
374 444590 : p->bmv_oflags |= BMV_OF_PREALLOC;
375 :
376 7237114 : if (shared)
377 897282 : p->bmv_oflags |= BMV_OF_SHARED;
378 :
379 7237114 : p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, got->br_startoff);
380 7237114 : p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, got->br_blockcount);
381 :
382 7237114 : bmv->bmv_offset = p->bmv_offset + p->bmv_length;
383 7237114 : bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
384 7237114 : bmv->bmv_entries++;
385 7237114 : return 0;
386 : }
387 :
388 : static void
389 3684073 : xfs_getbmap_report_hole(
390 : struct xfs_inode *ip,
391 : struct getbmapx *bmv,
392 : struct kgetbmap *out,
393 : int64_t bmv_end,
394 : xfs_fileoff_t bno,
395 : xfs_fileoff_t end)
396 : {
397 3684073 : struct kgetbmap *p = out + bmv->bmv_entries;
398 :
399 3684073 : if (bmv->bmv_iflags & BMV_IF_NO_HOLES)
400 : return;
401 :
402 147998 : p->bmv_block = -1;
403 147998 : p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, bno);
404 147998 : p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, end - bno);
405 :
406 147998 : bmv->bmv_offset = p->bmv_offset + p->bmv_length;
407 147998 : bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
408 147998 : bmv->bmv_entries++;
409 : }
410 :
411 : static inline bool
412 : xfs_getbmap_full(
413 : struct getbmapx *bmv)
414 : {
415 17967432 : return bmv->bmv_length == 0 || bmv->bmv_entries >= bmv->bmv_count - 1;
416 : }
417 :
418 : static bool
419 6959892 : xfs_getbmap_next_rec(
420 : struct xfs_bmbt_irec *rec,
421 : xfs_fileoff_t total_end)
422 : {
423 6959892 : xfs_fileoff_t end = rec->br_startoff + rec->br_blockcount;
424 :
425 6959892 : if (end == total_end)
426 : return false;
427 :
428 62603 : rec->br_startoff += rec->br_blockcount;
429 62603 : if (!isnullstartblock(rec->br_startblock) &&
430 : rec->br_startblock != DELAYSTARTBLOCK)
431 62603 : rec->br_startblock += rec->br_blockcount;
432 62603 : rec->br_blockcount = total_end - end;
433 62603 : return true;
434 : }
435 :
436 : /*
437 : * Get inode's extents as described in bmv, and format for output.
438 : * Calls formatter to fill the user's buffer until all extents
439 : * are mapped, until the passed-in bmv->bmv_count slots have
440 : * been filled, or until the formatter short-circuits the loop,
441 : * if it is tracking filled-in extents on its own.
442 : */
443 : int /* error code */
444 1314309 : xfs_getbmap(
445 : struct xfs_inode *ip,
446 : struct getbmapx *bmv, /* user bmap structure */
447 : struct kgetbmap *out)
448 : {
449 1314309 : struct xfs_mount *mp = ip->i_mount;
450 1314309 : int iflags = bmv->bmv_iflags;
451 1314309 : int whichfork, lock, error = 0;
452 1314309 : int64_t bmv_end, max_len;
453 1314309 : xfs_fileoff_t bno, first_bno;
454 1314309 : struct xfs_ifork *ifp;
455 1314309 : struct xfs_bmbt_irec got, rec;
456 1314309 : xfs_filblks_t len;
457 1314309 : struct xfs_iext_cursor icur;
458 :
459 1314309 : if (bmv->bmv_iflags & ~BMV_IF_VALID)
460 : return -EINVAL;
461 : #ifndef DEBUG
462 : /* Only allow CoW fork queries if we're debugging. */
463 : if (iflags & BMV_IF_COWFORK)
464 : return -EINVAL;
465 : #endif
466 1314309 : if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK))
467 : return -EINVAL;
468 :
469 1314309 : if (bmv->bmv_length < -1)
470 : return -EINVAL;
471 1314309 : bmv->bmv_entries = 0;
472 1314309 : if (bmv->bmv_length == 0)
473 : return 0;
474 :
475 1239196 : if (iflags & BMV_IF_ATTRFORK)
476 : whichfork = XFS_ATTR_FORK;
477 1239180 : else if (iflags & BMV_IF_COWFORK)
478 : whichfork = XFS_COW_FORK;
479 : else
480 1238930 : whichfork = XFS_DATA_FORK;
481 :
482 1239196 : xfs_ilock(ip, XFS_IOLOCK_SHARED);
483 1239172 : switch (whichfork) {
484 9 : case XFS_ATTR_FORK:
485 9 : lock = xfs_ilock_attr_map_shared(ip);
486 9 : if (!xfs_inode_has_attr_fork(ip))
487 0 : goto out_unlock_ilock;
488 :
489 : max_len = 1LL << 32;
490 : break;
491 235 : case XFS_COW_FORK:
492 235 : lock = XFS_ILOCK_SHARED;
493 235 : xfs_ilock(ip, lock);
494 :
495 : /* No CoW fork? Just return */
496 235 : if (!xfs_ifork_ptr(ip, whichfork))
497 67 : goto out_unlock_ilock;
498 :
499 168 : if (xfs_get_cowextsz_hint(ip))
500 168 : max_len = mp->m_super->s_maxbytes;
501 : else
502 0 : max_len = XFS_ISIZE(ip);
503 : break;
504 1238928 : case XFS_DATA_FORK:
505 1238928 : if (!(iflags & BMV_IF_DELALLOC) &&
506 494729 : (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_disk_size)) {
507 22493 : error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
508 22500 : if (error)
509 0 : goto out_unlock_iolock;
510 :
511 : /*
512 : * Even after flushing the inode, there can still be
513 : * delalloc blocks on the inode beyond EOF due to
514 : * speculative preallocation. These are not removed
515 : * until the release function is called or the inode
516 : * is inactivated. Hence we cannot assert here that
517 : * ip->i_delayed_blks == 0.
518 : */
519 : }
520 :
521 1238935 : if (xfs_get_extsz_hint(ip) ||
522 832249 : (ip->i_diflags &
523 : (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))
524 615940 : max_len = mp->m_super->s_maxbytes;
525 : else
526 622926 : max_len = XFS_ISIZE(ip);
527 :
528 1238866 : lock = xfs_ilock_data_map_shared(ip);
529 1238925 : break;
530 : }
531 :
532 1239102 : ifp = xfs_ifork_ptr(ip, whichfork);
533 :
534 1239105 : switch (ifp->if_format) {
535 : case XFS_DINODE_FMT_EXTENTS:
536 : case XFS_DINODE_FMT_BTREE:
537 943990 : break;
538 295115 : case XFS_DINODE_FMT_LOCAL:
539 : /* Local format inode forks report no extents. */
540 295115 : goto out_unlock_ilock;
541 0 : default:
542 0 : error = -EINVAL;
543 0 : goto out_unlock_ilock;
544 : }
545 :
546 943990 : if (bmv->bmv_length == -1) {
547 930749 : max_len = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, max_len));
548 930749 : bmv->bmv_length = max(0LL, max_len - bmv->bmv_offset);
549 : }
550 :
551 943990 : bmv_end = bmv->bmv_offset + bmv->bmv_length;
552 :
553 943990 : first_bno = bno = XFS_BB_TO_FSBT(mp, bmv->bmv_offset);
554 943990 : len = XFS_BB_TO_FSB(mp, bmv->bmv_length);
555 :
556 943990 : error = xfs_iread_extents(NULL, ip, whichfork);
557 943942 : if (error)
558 0 : goto out_unlock_ilock;
559 :
560 943942 : if (!xfs_iext_lookup_extent(ip, ifp, bno, &icur, &got)) {
561 : /*
562 : * Report a whole-file hole if the delalloc flag is set to
563 : * stay compatible with the old implementation.
564 : */
565 150335 : if (iflags & BMV_IF_DELALLOC)
566 385941 : xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
567 257294 : XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
568 150335 : goto out_unlock_ilock;
569 : }
570 :
571 14349783 : while (!xfs_getbmap_full(bmv)) {
572 7174891 : xfs_trim_extent(&got, first_bno, len);
573 :
574 : /*
575 : * Report an entry for a hole if this extent doesn't directly
576 : * follow the previous one.
577 : */
578 7174846 : if (got.br_startoff > bno) {
579 3434791 : xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
580 : got.br_startoff);
581 6869582 : if (xfs_getbmap_full(bmv))
582 : break;
583 : }
584 :
585 : /*
586 : * In order to report shared extents accurately, we report each
587 : * distinct shared / unshared part of a single bmbt record with
588 : * an individual getbmapx record.
589 : */
590 7174532 : bno = got.br_startoff + got.br_blockcount;
591 7174532 : rec = got;
592 7237135 : do {
593 7237135 : error = xfs_getbmap_report_one(ip, bmv, out, bmv_end,
594 : &rec);
595 14474229 : if (error || xfs_getbmap_full(bmv))
596 277222 : goto out_unlock_ilock;
597 6959893 : } while (xfs_getbmap_next_rec(&rec, bno));
598 :
599 6897289 : if (!xfs_iext_next_extent(ifp, &icur, &got)) {
600 1032116 : xfs_fileoff_t end = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
601 :
602 516058 : if (bmv->bmv_entries > 0)
603 516058 : out[bmv->bmv_entries - 1].bmv_oflags |=
604 : BMV_OF_LAST;
605 :
606 636693 : if (whichfork != XFS_ATTR_FORK && bno < end &&
607 : !xfs_getbmap_full(bmv)) {
608 120635 : xfs_getbmap_report_hole(ip, bmv, out, bmv_end,
609 : bno, end);
610 : }
611 : break;
612 : }
613 :
614 6381232 : if (bno >= first_bno + len)
615 : break;
616 : }
617 :
618 314 : out_unlock_ilock:
619 1239111 : xfs_iunlock(ip, lock);
620 1239142 : out_unlock_iolock:
621 1239142 : xfs_iunlock(ip, XFS_IOLOCK_SHARED);
622 1239142 : return error;
623 : }
624 :
625 : /*
626 : * Dead simple method of punching delalyed allocation blocks from a range in
627 : * the inode. This will always punch out both the start and end blocks, even
628 : * if the ranges only partially overlap them, so it is up to the caller to
629 : * ensure that partial blocks are not passed in.
630 : */
631 : int
632 44188 : xfs_bmap_punch_delalloc_range(
633 : struct xfs_inode *ip,
634 : xfs_off_t start_byte,
635 : xfs_off_t end_byte)
636 : {
637 44188 : struct xfs_mount *mp = ip->i_mount;
638 44188 : struct xfs_ifork *ifp = &ip->i_df;
639 44188 : xfs_fileoff_t start_fsb = XFS_B_TO_FSBT(mp, start_byte);
640 44188 : xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, end_byte);
641 44188 : struct xfs_bmbt_irec got, del;
642 44188 : struct xfs_iext_cursor icur;
643 44188 : int error = 0;
644 :
645 44188 : ASSERT(!xfs_need_iread_extents(ifp));
646 :
647 44188 : xfs_ilock(ip, XFS_ILOCK_EXCL);
648 44189 : if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
649 344 : goto out_unlock;
650 :
651 84484 : while (got.br_startoff + got.br_blockcount > start_fsb) {
652 48258 : del = got;
653 48258 : xfs_trim_extent(&del, start_fsb, end_fsb - start_fsb);
654 :
655 : /*
656 : * A delete can push the cursor forward. Step back to the
657 : * previous extent on non-delalloc or extents outside the
658 : * target range.
659 : */
660 48258 : if (!del.br_blockcount ||
661 44462 : !isnullstartblock(del.br_startblock)) {
662 21304 : if (!xfs_iext_prev_extent(ifp, &icur, &got))
663 : break;
664 15813 : continue;
665 : }
666 :
667 26954 : error = xfs_bmap_del_extent_delay(ip, XFS_DATA_FORK, &icur,
668 : &got, &del);
669 26956 : if (error || !xfs_iext_get_extent(ifp, &icur, &got))
670 : break;
671 : }
672 :
673 43846 : out_unlock:
674 44190 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
675 44189 : return error;
676 : }
677 :
678 : /*
679 : * Test whether it is appropriate to check an inode for and free post EOF
680 : * blocks. The 'force' parameter determines whether we should also consider
681 : * regular files that are marked preallocated or append-only.
682 : */
683 : bool
684 1067437427 : xfs_can_free_eofblocks(
685 : struct xfs_inode *ip,
686 : bool force)
687 : {
688 1067437427 : struct xfs_bmbt_irec imap;
689 1067437427 : struct xfs_mount *mp = ip->i_mount;
690 1067437427 : xfs_fileoff_t end_fsb;
691 1067437427 : xfs_fileoff_t last_fsb;
692 1067437427 : int nimaps = 1;
693 1067437427 : int error;
694 :
695 : /*
696 : * Caller must either hold the exclusive io lock; or be inactivating
697 : * the inode, which guarantees there are no other users of the inode.
698 : */
699 1067437427 : ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL) ||
700 : (VFS_I(ip)->i_state & I_FREEING));
701 :
702 : /* prealloc/delalloc exists only on regular files */
703 1066969770 : if (!S_ISREG(VFS_I(ip)->i_mode))
704 : return false;
705 :
706 : /*
707 : * Zero sized files with no cached pages and delalloc blocks will not
708 : * have speculative prealloc/delalloc blocks to remove.
709 : */
710 813915274 : if (VFS_I(ip)->i_size == 0 &&
711 213836906 : VFS_I(ip)->i_mapping->nrpages == 0 &&
712 213896289 : ip->i_delayed_blks == 0)
713 : return false;
714 :
715 : /* If we haven't read in the extent list, then don't do it now. */
716 600023912 : if (xfs_need_iread_extents(&ip->i_df))
717 : return false;
718 :
719 : /*
720 : * Do not free extent size hints, real preallocated or append-only files
721 : * unless the file has delalloc blocks and we are forced to remove
722 : * them.
723 : */
724 587335804 : if (xfs_get_extsz_hint(ip) ||
725 348326163 : (ip->i_diflags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))) {
726 390840134 : if (!force || ip->i_delayed_blks == 0)
727 : return false;
728 : }
729 :
730 : /*
731 : * Do not try to free post-EOF blocks if EOF is beyond the end of the
732 : * range supported by the page cache, because the truncation will loop
733 : * forever.
734 : */
735 396451774 : end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
736 198225887 : if (XFS_IS_REALTIME_INODE(ip) && mp->m_sb.sb_rextsize > 1)
737 20530 : end_fsb = xfs_rtb_roundup_rtx(mp, end_fsb);
738 198225887 : last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
739 198225887 : if (last_fsb <= end_fsb)
740 : return false;
741 :
742 : /*
743 : * Look up the mapping for the first block past EOF. If we can't find
744 : * it, there's nothing to free.
745 : */
746 198208190 : xfs_ilock(ip, XFS_ILOCK_SHARED);
747 198225427 : error = xfs_bmapi_read(ip, end_fsb, last_fsb - end_fsb, &imap, &nimaps,
748 : 0);
749 198257511 : xfs_iunlock(ip, XFS_ILOCK_SHARED);
750 198247807 : if (error || nimaps == 0)
751 : return false;
752 :
753 : /*
754 : * If there's a real mapping there or there are delayed allocation
755 : * reservations, then we have post-EOF blocks to try to free.
756 : */
757 338950127 : return imap.br_startblock != HOLESTARTBLOCK || ip->i_delayed_blks;
758 : }
759 :
760 : /*
761 : * This is called to free any blocks beyond eof. The caller must hold
762 : * IOLOCK_EXCL unless we are in the inode reclaim path and have the only
763 : * reference to the inode.
764 : */
765 : int
766 13689686 : xfs_free_eofblocks(
767 : struct xfs_inode *ip)
768 : {
769 13689686 : struct xfs_trans *tp;
770 13689686 : struct xfs_mount *mp = ip->i_mount;
771 13689686 : int error;
772 :
773 : /* Attach the dquots to the inode up front. */
774 13689686 : error = xfs_qm_dqattach(ip);
775 13763655 : if (error)
776 : return error;
777 :
778 : /* Wait on dio to ensure i_size has settled. */
779 13773906 : inode_dio_wait(VFS_I(ip));
780 :
781 13699598 : error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
782 13906703 : if (error) {
783 0 : ASSERT(xfs_is_shutdown(mp));
784 0 : return error;
785 : }
786 :
787 13906703 : xfs_ilock(ip, XFS_ILOCK_EXCL);
788 13895823 : xfs_trans_ijoin(tp, ip, 0);
789 :
790 : /*
791 : * Do not update the on-disk file size. If we update the on-disk file
792 : * size and then the system crashes before the contents of the file are
793 : * flushed to disk then the files may be full of holes (ie NULL files
794 : * bug).
795 : */
796 27750458 : error = xfs_itruncate_extents_flags(&tp, ip, XFS_DATA_FORK,
797 : XFS_ISIZE(ip), XFS_BMAPI_NODISCARD);
798 13909423 : if (error)
799 35 : goto err_cancel;
800 :
801 13909388 : error = xfs_trans_commit(tp);
802 13903890 : if (error)
803 0 : goto out_unlock;
804 :
805 13903890 : xfs_inode_clear_eofblocks_tag(ip);
806 13895123 : goto out_unlock;
807 :
808 : err_cancel:
809 : /*
810 : * If we get an error at this point we simply don't
811 : * bother truncating the file.
812 : */
813 35 : xfs_trans_cancel(tp);
814 13895158 : out_unlock:
815 13895158 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
816 13895158 : return error;
817 : }
818 :
819 : int
820 13779649 : xfs_alloc_file_space(
821 : struct xfs_inode *ip,
822 : xfs_off_t offset,
823 : xfs_off_t len)
824 : {
825 13779649 : xfs_mount_t *mp = ip->i_mount;
826 13779649 : xfs_off_t count;
827 13779649 : xfs_filblks_t allocated_fsb;
828 13779649 : xfs_filblks_t allocatesize_fsb;
829 13779649 : xfs_extlen_t extsz, temp;
830 13779649 : xfs_fileoff_t startoffset_fsb;
831 13779649 : xfs_fileoff_t endoffset_fsb;
832 13779649 : int nimaps;
833 13779649 : int rt;
834 13779649 : xfs_trans_t *tp;
835 13779649 : xfs_bmbt_irec_t imaps[1], *imapp;
836 13779649 : int error;
837 :
838 13779649 : trace_xfs_alloc_file_space(ip, offset, len);
839 :
840 27558992 : if (xfs_is_shutdown(mp))
841 : return -EIO;
842 :
843 13779493 : error = xfs_qm_dqattach(ip);
844 13779461 : if (error)
845 : return error;
846 :
847 13779461 : if (len <= 0)
848 : return -EINVAL;
849 :
850 13779461 : rt = XFS_IS_REALTIME_INODE(ip);
851 13779461 : extsz = xfs_get_extsz_hint(ip);
852 :
853 13779501 : count = len;
854 13779501 : imapp = &imaps[0];
855 13779501 : nimaps = 1;
856 13779501 : startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
857 13779501 : endoffset_fsb = XFS_B_TO_FSB(mp, offset + count);
858 13779501 : allocatesize_fsb = endoffset_fsb - startoffset_fsb;
859 :
860 : /*
861 : * Allocate file space until done or until there is an error
862 : */
863 44379083 : while (allocatesize_fsb && !error) {
864 31029974 : xfs_fileoff_t s, e;
865 31029974 : unsigned int dblocks, rblocks, resblks;
866 :
867 : /*
868 : * Determine space reservations for data/realtime.
869 : */
870 31029974 : if (unlikely(extsz)) {
871 18159738 : s = startoffset_fsb;
872 18159738 : do_div(s, extsz);
873 18159738 : s *= extsz;
874 18159738 : e = startoffset_fsb + allocatesize_fsb;
875 18159738 : div_u64_rem(startoffset_fsb, extsz, &temp);
876 18159729 : if (temp)
877 1922558 : e += temp;
878 18159729 : div_u64_rem(e, extsz, &temp);
879 18159735 : if (temp)
880 2659659 : e += extsz - temp;
881 : } else {
882 : s = 0;
883 : e = allocatesize_fsb;
884 : }
885 :
886 : /*
887 : * The transaction reservation is limited to a 32-bit block
888 : * count, hence we need to limit the number of blocks we are
889 : * trying to reserve to avoid an overflow. We can't allocate
890 : * more than @nimaps extents, and an extent is limited on disk
891 : * to XFS_BMBT_MAX_EXTLEN (21 bits), so use that to enforce the
892 : * limit.
893 : */
894 31029971 : resblks = min_t(xfs_fileoff_t, (e - s),
895 : (XFS_MAX_BMBT_EXTLEN * nimaps));
896 31029971 : if (unlikely(rt)) {
897 18158545 : dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
898 18158545 : rblocks = resblks;
899 : } else {
900 12871426 : dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
901 12871426 : rblocks = 0;
902 : }
903 :
904 31029971 : error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write,
905 : dblocks, rblocks, false, &tp);
906 31029699 : if (error)
907 : break;
908 :
909 30599531 : error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
910 : XFS_IEXT_ADD_NOSPLIT_CNT);
911 30598925 : if (error == -EFBIG)
912 20 : error = xfs_iext_count_upgrade(tp, ip,
913 : XFS_IEXT_ADD_NOSPLIT_CNT);
914 30598925 : if (error)
915 20 : goto error;
916 :
917 30598905 : error = xfs_bmapi_write(tp, ip, startoffset_fsb,
918 : allocatesize_fsb, XFS_BMAPI_PREALLOC, 0, imapp,
919 : &nimaps);
920 30599330 : if (error)
921 208 : goto error;
922 :
923 30599122 : ip->i_diflags |= XFS_DIFLAG_PREALLOC;
924 30599122 : xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
925 :
926 30599693 : error = xfs_trans_commit(tp);
927 30599984 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
928 30599850 : if (error)
929 : break;
930 :
931 30599645 : allocated_fsb = imapp->br_blockcount;
932 :
933 30599645 : if (nimaps == 0) {
934 : error = -ENOSPC;
935 : break;
936 : }
937 :
938 30599582 : startoffset_fsb += allocated_fsb;
939 30599582 : allocatesize_fsb -= allocated_fsb;
940 : }
941 :
942 : return error;
943 :
944 228 : error:
945 228 : xfs_trans_cancel(tp);
946 228 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
947 228 : return error;
948 : }
949 :
950 : static int
951 46117736 : xfs_unmap_extent(
952 : struct xfs_inode *ip,
953 : xfs_fileoff_t startoffset_fsb,
954 : xfs_filblks_t len_fsb,
955 : int *done)
956 : {
957 46117736 : struct xfs_mount *mp = ip->i_mount;
958 46117736 : struct xfs_trans *tp;
959 46117736 : uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
960 46117736 : int error;
961 :
962 46117736 : error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks, 0,
963 : false, &tp);
964 46118367 : if (error)
965 : return error;
966 :
967 46041767 : error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
968 : XFS_IEXT_PUNCH_HOLE_CNT);
969 46041159 : if (error == -EFBIG)
970 128 : error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
971 46041159 : if (error)
972 128 : goto out_trans_cancel;
973 :
974 46041031 : error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, done);
975 46041552 : if (error)
976 6 : goto out_trans_cancel;
977 :
978 46041546 : error = xfs_trans_commit(tp);
979 46041905 : out_unlock:
980 46041905 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
981 46041905 : return error;
982 :
983 134 : out_trans_cancel:
984 134 : xfs_trans_cancel(tp);
985 134 : goto out_unlock;
986 : }
987 :
988 : /* Caller must first wait for the completion of any pending DIOs if required. */
989 : int
990 296106379 : xfs_flush_unmap_range(
991 : struct xfs_inode *ip,
992 : xfs_off_t offset,
993 : xfs_off_t len)
994 : {
995 296106379 : struct xfs_mount *mp = ip->i_mount;
996 296106379 : struct inode *inode = VFS_I(ip);
997 296106379 : xfs_off_t rounding, start, end;
998 296106379 : int error;
999 :
1000 296106379 : rounding = max_t(xfs_off_t, mp->m_sb.sb_blocksize, PAGE_SIZE);
1001 296106379 : start = round_down(offset, rounding);
1002 296106379 : end = round_up(offset + len, rounding) - 1;
1003 :
1004 296106379 : error = filemap_write_and_wait_range(inode->i_mapping, start, end);
1005 296089993 : if (error)
1006 : return error;
1007 296090103 : truncate_pagecache_range(inode, start, end);
1008 296090103 : return 0;
1009 : }
1010 :
1011 : int
1012 43859289 : xfs_free_file_space(
1013 : struct xfs_inode *ip,
1014 : xfs_off_t offset,
1015 : xfs_off_t len)
1016 : {
1017 43859289 : struct xfs_mount *mp = ip->i_mount;
1018 43859289 : xfs_fileoff_t startoffset_fsb;
1019 43859289 : xfs_fileoff_t endoffset_fsb;
1020 43859289 : int done = 0, error;
1021 :
1022 43859289 : trace_xfs_free_file_space(ip, offset, len);
1023 :
1024 43858994 : error = xfs_qm_dqattach(ip);
1025 43859162 : if (error)
1026 : return error;
1027 :
1028 43859162 : if (len <= 0) /* if nothing being freed */
1029 : return 0;
1030 :
1031 43859162 : startoffset_fsb = XFS_B_TO_FSB(mp, offset);
1032 43859162 : endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
1033 :
1034 : /* We can only free complete realtime extents. */
1035 43859162 : if (xfs_inode_has_bigrtextents(ip)) {
1036 2478994 : startoffset_fsb = xfs_rtb_roundup_rtx(mp, startoffset_fsb);
1037 2479001 : endoffset_fsb = xfs_rtb_rounddown_rtx(mp, endoffset_fsb);
1038 : }
1039 :
1040 : /*
1041 : * Need to zero the stuff we're not freeing, on disk.
1042 : */
1043 43859166 : if (endoffset_fsb > startoffset_fsb) {
1044 88890032 : while (!done) {
1045 46117790 : error = xfs_unmap_extent(ip, startoffset_fsb,
1046 : endoffset_fsb - startoffset_fsb, &done);
1047 46118457 : if (error)
1048 76748 : return error;
1049 : }
1050 : }
1051 :
1052 : /*
1053 : * Now that we've unmap all full blocks we'll have to zero out any
1054 : * partial block at the beginning and/or end. xfs_zero_range is smart
1055 : * enough to skip any holes, including those we just created, but we
1056 : * must take care not to zero beyond EOF and enlarge i_size.
1057 : */
1058 87566170 : if (offset >= XFS_ISIZE(ip))
1059 : return 0;
1060 40912576 : if (offset + len > XFS_ISIZE(ip))
1061 785165 : len = XFS_ISIZE(ip) - offset;
1062 40912576 : error = xfs_zero_range(ip, offset, len, NULL);
1063 40912501 : if (error)
1064 : return error;
1065 :
1066 : /*
1067 : * If we zeroed right up to EOF and EOF straddles a page boundary we
1068 : * must make sure that the post-EOF area is also zeroed because the
1069 : * page could be mmap'd and xfs_zero_range doesn't do that for us.
1070 : * Writeback of the eof page will do this, albeit clumsily.
1071 : */
1072 81822024 : if (offset + len >= XFS_ISIZE(ip) && offset_in_page(offset + len) > 0) {
1073 900729 : error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
1074 : round_down(offset + len, PAGE_SIZE), LLONG_MAX);
1075 : }
1076 :
1077 : return error;
1078 : }
1079 :
1080 : static int
1081 4319627 : xfs_prepare_shift(
1082 : struct xfs_inode *ip,
1083 : loff_t offset)
1084 : {
1085 4319627 : struct xfs_mount *mp = ip->i_mount;
1086 4319627 : int error;
1087 :
1088 : /*
1089 : * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
1090 : * into the accessible region of the file.
1091 : */
1092 4319627 : if (xfs_can_free_eofblocks(ip, true)) {
1093 2029551 : error = xfs_free_eofblocks(ip);
1094 2029559 : if (error)
1095 : return error;
1096 : }
1097 :
1098 : /*
1099 : * Shift operations must stabilize the start block offset boundary along
1100 : * with the full range of the operation. If we don't, a COW writeback
1101 : * completion could race with an insert, front merge with the start
1102 : * extent (after split) during the shift and corrupt the file. Start
1103 : * with the block just prior to the start to stabilize the boundary.
1104 : */
1105 4319635 : offset = round_down(offset, mp->m_sb.sb_blocksize);
1106 4319635 : if (offset)
1107 4149476 : offset -= mp->m_sb.sb_blocksize;
1108 :
1109 : /*
1110 : * Writeback and invalidate cache for the remainder of the file as we're
1111 : * about to shift down every extent from offset to EOF.
1112 : */
1113 8639270 : error = xfs_flush_unmap_range(ip, offset, XFS_ISIZE(ip));
1114 4319654 : if (error)
1115 : return error;
1116 :
1117 : /*
1118 : * Clean out anything hanging around in the cow fork now that
1119 : * we've flushed all the dirty data out to disk to avoid having
1120 : * CoW extents at the wrong offsets.
1121 : */
1122 8639186 : if (xfs_inode_has_cow_data(ip)) {
1123 1741281 : error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF,
1124 : true);
1125 1741281 : if (error)
1126 0 : return error;
1127 : }
1128 :
1129 : return 0;
1130 : }
1131 :
1132 : /*
1133 : * xfs_collapse_file_space()
1134 : * This routine frees disk space and shift extent for the given file.
1135 : * The first thing we do is to free data blocks in the specified range
1136 : * by calling xfs_free_file_space(). It would also sync dirty data
1137 : * and invalidate page cache over the region on which collapse range
1138 : * is working. And Shift extent records to the left to cover a hole.
1139 : * RETURNS:
1140 : * 0 on success
1141 : * errno on error
1142 : *
1143 : */
1144 : int
1145 2461605 : xfs_collapse_file_space(
1146 : struct xfs_inode *ip,
1147 : xfs_off_t offset,
1148 : xfs_off_t len)
1149 : {
1150 2461605 : struct xfs_mount *mp = ip->i_mount;
1151 2461605 : struct xfs_trans *tp;
1152 2461605 : int error;
1153 2461605 : xfs_fileoff_t next_fsb = XFS_B_TO_FSB(mp, offset + len);
1154 2461605 : xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len);
1155 2461605 : bool done = false;
1156 :
1157 2461605 : ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1158 2461592 : ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
1159 :
1160 2461597 : trace_xfs_collapse_file_space(ip, offset, len);
1161 :
1162 2461598 : error = xfs_free_file_space(ip, offset, len);
1163 2461602 : if (error)
1164 : return error;
1165 :
1166 2458829 : error = xfs_prepare_shift(ip, offset);
1167 2458839 : if (error)
1168 : return error;
1169 :
1170 2458794 : error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1171 2458795 : if (error)
1172 : return error;
1173 :
1174 2458794 : xfs_ilock(ip, XFS_ILOCK_EXCL);
1175 2458794 : xfs_trans_ijoin(tp, ip, 0);
1176 :
1177 106555234 : while (!done) {
1178 106555232 : error = xfs_bmap_collapse_extents(tp, ip, &next_fsb, shift_fsb,
1179 : &done);
1180 106555236 : if (error)
1181 1 : goto out_trans_cancel;
1182 106555235 : if (done)
1183 : break;
1184 :
1185 : /* finish any deferred frees and roll the transaction */
1186 104096442 : error = xfs_defer_finish(&tp);
1187 104096440 : if (error)
1188 0 : goto out_trans_cancel;
1189 : }
1190 :
1191 2458793 : error = xfs_trans_commit(tp);
1192 2458793 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1193 2458793 : return error;
1194 :
1195 1 : out_trans_cancel:
1196 1 : xfs_trans_cancel(tp);
1197 1 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1198 1 : return error;
1199 : }
1200 :
1201 : /*
1202 : * xfs_insert_file_space()
1203 : * This routine create hole space by shifting extents for the given file.
1204 : * The first thing we do is to sync dirty data and invalidate page cache
1205 : * over the region on which insert range is working. And split an extent
1206 : * to two extents at given offset by calling xfs_bmap_split_extent.
1207 : * And shift all extent records which are laying between [offset,
1208 : * last allocated extent] to the right to reserve hole range.
1209 : * RETURNS:
1210 : * 0 on success
1211 : * errno on error
1212 : */
1213 : int
1214 1860815 : xfs_insert_file_space(
1215 : struct xfs_inode *ip,
1216 : loff_t offset,
1217 : loff_t len)
1218 : {
1219 1860815 : struct xfs_mount *mp = ip->i_mount;
1220 1860815 : struct xfs_trans *tp;
1221 1860815 : int error;
1222 1860815 : xfs_fileoff_t stop_fsb = XFS_B_TO_FSB(mp, offset);
1223 1860815 : xfs_fileoff_t next_fsb = NULLFSBLOCK;
1224 1860815 : xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len);
1225 1860815 : bool done = false;
1226 :
1227 1860815 : ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1228 1860804 : ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
1229 :
1230 1860806 : trace_xfs_insert_file_space(ip, offset, len);
1231 :
1232 1860804 : error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb);
1233 1860815 : if (error)
1234 : return error;
1235 :
1236 1860814 : error = xfs_prepare_shift(ip, offset);
1237 1860818 : if (error)
1238 : return error;
1239 :
1240 1860801 : error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
1241 1860801 : XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
1242 1860800 : if (error)
1243 : return error;
1244 :
1245 1857579 : xfs_ilock(ip, XFS_ILOCK_EXCL);
1246 1857580 : xfs_trans_ijoin(tp, ip, 0);
1247 :
1248 1857579 : error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
1249 : XFS_IEXT_PUNCH_HOLE_CNT);
1250 1857577 : if (error == -EFBIG)
1251 11 : error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
1252 1857577 : if (error)
1253 11 : goto out_trans_cancel;
1254 :
1255 : /*
1256 : * The extent shifting code works on extent granularity. So, if stop_fsb
1257 : * is not the starting block of extent, we need to split the extent at
1258 : * stop_fsb.
1259 : */
1260 1857566 : error = xfs_bmap_split_extent(tp, ip, stop_fsb);
1261 1857567 : if (error)
1262 1 : goto out_trans_cancel;
1263 :
1264 10798747 : do {
1265 10798747 : error = xfs_defer_finish(&tp);
1266 10798743 : if (error)
1267 3 : goto out_trans_cancel;
1268 :
1269 10798740 : error = xfs_bmap_insert_extents(tp, ip, &next_fsb, shift_fsb,
1270 : &done, stop_fsb);
1271 10798746 : if (error)
1272 1 : goto out_trans_cancel;
1273 10798745 : } while (!done);
1274 :
1275 1857564 : error = xfs_trans_commit(tp);
1276 1857564 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1277 1857564 : return error;
1278 :
1279 16 : out_trans_cancel:
1280 16 : xfs_trans_cancel(tp);
1281 16 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1282 16 : return error;
1283 : }
1284 :
1285 : /*
1286 : * Reserve space and quota to this transaction to map in as much free space
1287 : * as we can. Callers should set @len to the amount of space desired; this
1288 : * function will shorten that quantity if it can't get space.
1289 : */
1290 : STATIC int
1291 680 : xfs_map_free_reserve_more(
1292 : struct xfs_trans *tp,
1293 : struct xfs_inode *ip,
1294 : xfs_extlen_t *len)
1295 : {
1296 680 : struct xfs_mount *mp = ip->i_mount;
1297 680 : unsigned int dblocks;
1298 680 : unsigned int rblocks;
1299 680 : unsigned int min_len;
1300 680 : bool isrt = XFS_IS_REALTIME_INODE(ip);
1301 680 : int error;
1302 :
1303 680 : if (*len > XFS_MAX_BMBT_EXTLEN)
1304 20 : *len = XFS_MAX_BMBT_EXTLEN;
1305 680 : min_len = isrt ? mp->m_sb.sb_rextsize : 1;
1306 :
1307 1225 : again:
1308 1225 : if (isrt) {
1309 517 : dblocks = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
1310 517 : rblocks = *len;
1311 : } else {
1312 708 : dblocks = XFS_DIOSTRAT_SPACE_RES(mp, *len);
1313 708 : rblocks = 0;
1314 : }
1315 1225 : error = xfs_trans_reserve_more_inode(tp, ip, dblocks, rblocks, false);
1316 1225 : if (error == -ENOSPC && *len > min_len) {
1317 545 : *len >>= 1;
1318 545 : goto again;
1319 : }
1320 680 : if (error) {
1321 5 : trace_xfs_map_free_reserve_more_fail(ip, error, _RET_IP_);
1322 5 : return error;
1323 : }
1324 :
1325 : return 0;
1326 : }
1327 :
1328 : /* Find a free extent in this AG and map it into the file. */
1329 : STATIC int
1330 709 : xfs_map_free_extent(
1331 : struct xfs_inode *ip,
1332 : struct xfs_perag *pag,
1333 : xfs_agblock_t *cursor,
1334 : xfs_agblock_t end_agbno,
1335 : xfs_agblock_t *last_enospc_agbno)
1336 : {
1337 709 : struct xfs_bmbt_irec irec;
1338 709 : struct xfs_mount *mp = ip->i_mount;
1339 709 : struct xfs_trans *tp;
1340 709 : xfs_off_t endpos;
1341 709 : xfs_fsblock_t fsbno;
1342 709 : xfs_extlen_t len;
1343 709 : int error;
1344 :
1345 709 : if (fatal_signal_pending(current))
1346 : return -EINTR;
1347 :
1348 709 : error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, 0, 0, false,
1349 : &tp);
1350 709 : if (error)
1351 : return error;
1352 :
1353 709 : error = xfs_alloc_find_freesp(tp, pag, cursor, end_agbno, &len);
1354 709 : if (error)
1355 0 : goto out_cancel;
1356 :
1357 : /* Bail out if the cursor is beyond what we asked for. */
1358 709 : if (*cursor >= end_agbno)
1359 546 : goto out_cancel;
1360 :
1361 163 : error = xfs_map_free_reserve_more(tp, ip, &len);
1362 163 : if (error)
1363 5 : goto out_cancel;
1364 :
1365 158 : fsbno = XFS_AGB_TO_FSB(mp, pag->pag_agno, *cursor);
1366 816 : do {
1367 816 : error = xfs_bmapi_freesp(tp, ip, fsbno, len, &irec);
1368 816 : if (error == -EAGAIN) {
1369 : /* Failed to map space but were told to try again. */
1370 0 : error = xfs_trans_commit(tp);
1371 0 : goto out;
1372 : }
1373 816 : if (error != -ENOSPC)
1374 : break;
1375 : /*
1376 : * If we can't get the space, try asking for successively less
1377 : * space in case we're bumping up against per-AG metadata
1378 : * reservation limits...
1379 : */
1380 704 : len >>= 1;
1381 704 : } while (len > 0);
1382 158 : if (error == -ENOSPC && *last_enospc_agbno != *cursor) {
1383 : /*
1384 : * ...but even that might not work if an AGFL fixup allocated
1385 : * the block at *cursor. The first time this happens, remember
1386 : * that we ran out of space here, and try again.
1387 : */
1388 23 : *last_enospc_agbno = *cursor;
1389 23 : error = 0;
1390 23 : goto out_cancel;
1391 : }
1392 135 : if (error)
1393 23 : goto out_cancel;
1394 :
1395 : /* Update isize if needed. */
1396 112 : endpos = XFS_FSB_TO_B(mp, irec.br_startoff + irec.br_blockcount);
1397 112 : if (endpos > i_size_read(VFS_I(ip))) {
1398 7 : i_size_write(VFS_I(ip), endpos);
1399 7 : ip->i_disk_size = endpos;
1400 7 : xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1401 : }
1402 :
1403 112 : error = xfs_trans_commit(tp);
1404 112 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1405 112 : if (error)
1406 : return error;
1407 :
1408 112 : *cursor += irec.br_blockcount;
1409 112 : return 0;
1410 597 : out_cancel:
1411 597 : xfs_trans_cancel(tp);
1412 597 : out:
1413 597 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1414 597 : return error;
1415 : }
1416 :
1417 : /*
1418 : * Allocate all free physical space between off and len and map it to this
1419 : * regular non-realtime file.
1420 : */
1421 : int
1422 612 : xfs_map_free_space(
1423 : struct xfs_inode *ip,
1424 : xfs_off_t off,
1425 : xfs_off_t len)
1426 : {
1427 612 : struct xfs_mount *mp = ip->i_mount;
1428 612 : struct xfs_perag *pag = NULL;
1429 612 : xfs_daddr_t off_daddr = BTOBB(off);
1430 612 : xfs_daddr_t end_daddr = BTOBBT(off + len);
1431 612 : xfs_fsblock_t off_fsb = XFS_DADDR_TO_FSB(mp, off_daddr);
1432 612 : xfs_fsblock_t end_fsb = XFS_DADDR_TO_FSB(mp, end_daddr);
1433 612 : xfs_agnumber_t off_agno = XFS_FSB_TO_AGNO(mp, off_fsb);
1434 612 : xfs_agnumber_t end_agno = XFS_FSB_TO_AGNO(mp, end_fsb);
1435 612 : xfs_agnumber_t agno;
1436 612 : int error = 0;
1437 :
1438 612 : trace_xfs_map_free_space(ip, off, len);
1439 :
1440 612 : agno = off_agno;
1441 1194 : for_each_perag_range(mp, agno, end_agno, pag) {
1442 610 : xfs_agblock_t off_agbno = 0;
1443 610 : xfs_agblock_t end_agbno;
1444 610 : xfs_agblock_t last_enospc_agbno = NULLAGBLOCK;
1445 :
1446 610 : end_agbno = xfs_ag_block_count(mp, pag->pag_agno);
1447 :
1448 610 : if (pag->pag_agno == off_agno)
1449 606 : off_agbno = XFS_FSB_TO_AGBNO(mp, off_fsb);
1450 610 : if (pag->pag_agno == end_agno)
1451 598 : end_agbno = XFS_FSB_TO_AGBNO(mp, end_fsb);
1452 :
1453 1291 : while (off_agbno < end_agbno) {
1454 709 : error = xfs_map_free_extent(ip, pag, &off_agbno,
1455 : end_agbno, &last_enospc_agbno);
1456 709 : if (error)
1457 28 : goto out;
1458 : }
1459 : }
1460 :
1461 584 : out:
1462 612 : if (pag)
1463 28 : xfs_perag_rele(pag);
1464 612 : if (error == -ENOSPC)
1465 28 : return 0;
1466 : return error;
1467 : }
1468 :
1469 : #ifdef CONFIG_XFS_RT
1470 : STATIC int
1471 520 : xfs_map_free_rt_extent(
1472 : struct xfs_inode *ip,
1473 : xfs_rtxnum_t *cursor,
1474 : xfs_rtxnum_t end_rtx)
1475 : {
1476 520 : struct xfs_bmbt_irec irec;
1477 520 : struct xfs_mount *mp = ip->i_mount;
1478 520 : struct xfs_trans *tp;
1479 520 : xfs_off_t endpos;
1480 520 : xfs_rtblock_t rtbno;
1481 520 : xfs_rtxnum_t add;
1482 520 : xfs_rtxlen_t len_rtx;
1483 520 : xfs_extlen_t len;
1484 520 : uint32_t mod;
1485 520 : int error;
1486 :
1487 520 : if (fatal_signal_pending(current))
1488 : return -EINTR;
1489 :
1490 520 : error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, 0, 0, false,
1491 : &tp);
1492 520 : if (error)
1493 : return error;
1494 :
1495 520 : xfs_rtbitmap_lock(tp, mp);
1496 520 : error = xfs_rtalloc_find_freesp(tp, cursor, end_rtx, &len_rtx);
1497 520 : if (error)
1498 0 : goto out_cancel;
1499 :
1500 : /*
1501 : * If off_rtx is beyond the end of the rt device or is past what the
1502 : * user asked for, bail out.
1503 : */
1504 520 : if (*cursor >= end_rtx)
1505 3 : goto out_cancel;
1506 :
1507 517 : len = xfs_rtx_to_rtb(mp, len_rtx);
1508 517 : error = xfs_map_free_reserve_more(tp, ip, &len);
1509 517 : if (error)
1510 0 : goto out_cancel;
1511 :
1512 517 : rtbno = xfs_rtx_to_rtb(mp, *cursor);
1513 517 : error = xfs_bmapi_freesp(tp, ip, rtbno, len, &irec);
1514 517 : if (error)
1515 0 : goto out_cancel;
1516 :
1517 : /* Update isize if needed. */
1518 517 : endpos = XFS_FSB_TO_B(mp, irec.br_startoff + irec.br_blockcount);
1519 517 : if (endpos > i_size_read(VFS_I(ip))) {
1520 9 : i_size_write(VFS_I(ip), endpos);
1521 9 : ip->i_disk_size = endpos;
1522 9 : xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1523 : }
1524 :
1525 517 : error = xfs_trans_commit(tp);
1526 517 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1527 517 : if (error)
1528 : return error;
1529 :
1530 517 : add = xfs_rtb_to_rtx(mp, irec.br_blockcount, &mod);
1531 517 : if (mod)
1532 : return -EFSCORRUPTED;
1533 :
1534 517 : *cursor += add;
1535 517 : return 0;
1536 3 : out_cancel:
1537 3 : xfs_trans_cancel(tp);
1538 3 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1539 3 : return error;
1540 : }
1541 :
1542 : /*
1543 : * Allocate all free physical space between off and len and map it to this
1544 : * regular non-realtime file.
1545 : */
1546 : int
1547 510 : xfs_map_free_rt_space(
1548 : struct xfs_inode *ip,
1549 : xfs_off_t off,
1550 : xfs_off_t len)
1551 : {
1552 510 : struct xfs_mount *mp = ip->i_mount;
1553 510 : xfs_rtblock_t off_rtb = XFS_B_TO_FSB(mp, off);
1554 510 : xfs_rtblock_t end_rtb = XFS_B_TO_FSBT(mp, off + len);
1555 510 : xfs_rtxnum_t off_rtx;
1556 510 : xfs_rtxnum_t end_rtx;
1557 510 : uint32_t mod;
1558 510 : int error = 0;
1559 :
1560 : /* Compute rt extents from the input parameters. */
1561 510 : off_rtx = xfs_rtb_to_rtx(mp, off_rtb, &mod);
1562 510 : if (mod)
1563 178 : off_rtx++;
1564 510 : end_rtx = xfs_rtb_to_rtxt(mp, end_rtb);
1565 :
1566 510 : if (off_rtx >= mp->m_sb.sb_rextents)
1567 : return 0;
1568 505 : if (end_rtx >= mp->m_sb.sb_rextents)
1569 1 : end_rtx = mp->m_sb.sb_rextents - 1;
1570 :
1571 505 : trace_xfs_map_free_rt_space(ip, off, len);
1572 :
1573 1025 : while (off_rtx < end_rtx) {
1574 520 : error = xfs_map_free_rt_extent(ip, &off_rtx, end_rtx);
1575 520 : if (error)
1576 : break;
1577 : }
1578 :
1579 505 : if (error == -ENOSPC)
1580 0 : return 0;
1581 : return error;
1582 : }
1583 : #endif
|