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 185716278 : xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
50 : {
51 185716278 : if (XFS_IS_REALTIME_INODE(ip))
52 110699759 : return XFS_FSB_TO_BB(ip->i_mount, fsb);
53 75016519 : 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 54169 : xfs_zero_extent(
65 : struct xfs_inode *ip,
66 : xfs_fsblock_t start_fsb,
67 : xfs_off_t count_fsb)
68 : {
69 54169 : struct xfs_mount *mp = ip->i_mount;
70 54169 : struct xfs_buftarg *target = xfs_inode_buftarg(ip);
71 54169 : xfs_daddr_t sector = xfs_fsb_to_db(ip, start_fsb);
72 54169 : sector_t block = XFS_BB_TO_FSBT(mp, sector);
73 :
74 108338 : return xfs_buftarg_zeroout(target,
75 54169 : block << (mp->m_super->s_blocksize_bits - 9),
76 54169 : 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 23767042 : xfs_bmap_rtalloc_accounting(
85 : struct xfs_bmalloca *ap)
86 : {
87 23767042 : 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 313361 : 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 313361 : ap->ip->i_delayed_blks += ap->length;
111 313361 : xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
112 313361 : XFS_TRANS_DQ_RES_RTBLKS, -(long)ap->length);
113 313361 : return;
114 : }
115 :
116 : /* data fork only */
117 23453681 : ap->ip->i_nblocks += ap->length;
118 23453681 : xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
119 23453681 : 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 46907362 : xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
126 23453681 : ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
127 23453681 : XFS_TRANS_DQ_RTBCOUNT, ap->length);
128 : }
129 :
130 : int
131 23766568 : xfs_bmap_rtalloc(
132 : struct xfs_bmalloca *ap)
133 : {
134 23766568 : struct xfs_mount *mp = ap->ip->i_mount;
135 23766568 : xfs_fileoff_t orig_offset = ap->offset;
136 23766568 : xfs_rtxnum_t rtx;
137 23766568 : xfs_rtxlen_t prod = 0; /* product factor for allocators */
138 23766568 : xfs_extlen_t mod = 0; /* product factor for allocators */
139 23766568 : xfs_rtxlen_t ralen = 0; /* realtime allocation length */
140 23766568 : xfs_extlen_t align; /* minimum allocation alignment */
141 23766568 : xfs_extlen_t orig_length = ap->length;
142 23766568 : xfs_extlen_t minlen = mp->m_sb.sb_rextsize;
143 23766568 : xfs_rtxlen_t raminlen;
144 23766568 : bool rtlocked = false;
145 23766568 : bool ignore_locality = false;
146 23766568 : int error;
147 :
148 23766568 : if (ap->flags & XFS_BMAPI_COWFORK)
149 313361 : align = xfs_get_cowextsz_hint(ap->ip);
150 : else
151 23453207 : align = xfs_get_extsz_hint(ap->ip);
152 : retry:
153 23766489 : prod = xfs_extlen_to_rtxlen(mp, align);
154 23766489 : error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
155 23766489 : align, 1, ap->eof, 0,
156 23766489 : ap->conv, &ap->offset, &ap->length);
157 23766569 : if (error)
158 0 : return error;
159 23766569 : ASSERT(ap->length);
160 47533138 : 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 23766569 : if (ap->offset != orig_offset)
169 293171 : 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 23766569 : div_u64_rem(ap->offset, align, &mod);
176 23766569 : if (mod || ap->length % align)
177 2474 : 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 23766569 : 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 23766569 : if (!rtlocked) {
193 23766427 : xfs_rtbitmap_lock(ap->tp, mp);
194 23766427 : 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 23767184 : if (ap->eof && ap->offset == 0) {
202 32118 : error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
203 32118 : if (error)
204 0 : return error;
205 64236 : ap->blkno = xfs_rtx_to_rtb(mp, rtx);
206 : } else {
207 23735066 : ap->blkno = 0;
208 : }
209 :
210 23767184 : xfs_bmap_adjacent(ap);
211 :
212 : /*
213 : * Realtime allocation, done through xfs_rtallocate_extent.
214 : */
215 23767042 : if (ignore_locality)
216 0 : rtx = 0;
217 : else
218 47534084 : rtx = xfs_rtb_to_rtxt(mp, ap->blkno);
219 23767042 : raminlen = max_t(xfs_rtxlen_t, 1, xfs_extlen_to_rtxlen(mp, minlen));
220 23767042 : error = xfs_rtallocate_extent(ap->tp, rtx, raminlen, ralen, &ralen,
221 23767042 : ap->wasdel, prod, &rtx);
222 23766850 : if (error)
223 0 : return error;
224 :
225 23766850 : if (rtx != NULLRTEXTNO) {
226 23767042 : ap->blkno = xfs_rtx_to_rtb(mp, rtx);
227 23767042 : ap->length = xfs_rtxlen_to_extlen(mp, ralen);
228 23767042 : xfs_bmap_rtalloc_accounting(ap);
229 23767042 : return 0;
230 : }
231 :
232 0 : 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 0 : ap->offset = orig_offset;
240 0 : ap->length = orig_length;
241 0 : minlen = align = mp->m_sb.sb_rextsize;
242 0 : goto retry;
243 : }
244 :
245 0 : 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 0 : ignore_locality = true;
251 0 : 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 48486740 : xfs_bmap_count_leaves(
270 : struct xfs_ifork *ifp,
271 : xfs_filblks_t *count)
272 : {
273 48486740 : struct xfs_iext_cursor icur;
274 48486740 : struct xfs_bmbt_irec got;
275 48486740 : xfs_extnum_t numrecs = 0;
276 :
277 690266655 : for_each_xfs_iext(ifp, &icur, &got) {
278 641779915 : if (!isnullstartblock(got.br_startblock)) {
279 641777526 : *count += got.br_blockcount;
280 641777526 : numrecs++;
281 : }
282 : }
283 :
284 48486682 : 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 156085627 : 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 156085627 : struct xfs_mount *mp = ip->i_mount;
300 156085627 : struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
301 156086151 : struct xfs_btree_cur *cur;
302 156086151 : xfs_extlen_t btblocks = 0;
303 156086151 : int error;
304 :
305 156086151 : *nextents = 0;
306 156086151 : *count = 0;
307 :
308 156086151 : if (!ifp)
309 : return 0;
310 :
311 156086151 : switch (ifp->if_format) {
312 1415427 : case XFS_DINODE_FMT_BTREE:
313 1415427 : error = xfs_iread_extents(tp, ip, whichfork);
314 1415427 : if (error)
315 : return error;
316 :
317 1415427 : cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
318 1415426 : error = xfs_btree_count_blocks(cur, &btblocks);
319 1415427 : xfs_btree_del_cursor(cur, error);
320 1415426 : 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 1415426 : *count += btblocks - 1;
329 :
330 33954786 : fallthrough;
331 33954786 : case XFS_DINODE_FMT_EXTENTS:
332 33954786 : *nextents = xfs_bmap_count_leaves(ifp, count);
333 33954678 : break;
334 : }
335 :
336 : return 0;
337 : }
338 :
339 : static int
340 5398885 : 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 5398885 : struct kgetbmap *p = out + bmv->bmv_entries;
348 5398885 : bool shared = false;
349 5398885 : int error;
350 :
351 5398885 : error = xfs_reflink_trim_around_shared(ip, got, &shared);
352 5398882 : if (error)
353 : return error;
354 :
355 5398882 : 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 6257 : if (!(bmv->bmv_iflags & BMV_IF_DELALLOC))
364 : return 0;
365 :
366 6257 : p->bmv_oflags |= BMV_OF_DELALLOC;
367 6257 : p->bmv_block = -2;
368 : } else {
369 5392625 : p->bmv_block = xfs_fsb_to_db(ip, got->br_startblock);
370 : }
371 :
372 5398882 : if (got->br_state == XFS_EXT_UNWRITTEN &&
373 1551128 : (bmv->bmv_iflags & BMV_IF_PREALLOC))
374 77576 : p->bmv_oflags |= BMV_OF_PREALLOC;
375 :
376 5398882 : if (shared)
377 452624 : p->bmv_oflags |= BMV_OF_SHARED;
378 :
379 5398882 : p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, got->br_startoff);
380 5398882 : p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, got->br_blockcount);
381 :
382 5398882 : bmv->bmv_offset = p->bmv_offset + p->bmv_length;
383 5398882 : bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
384 5398882 : bmv->bmv_entries++;
385 5398882 : return 0;
386 : }
387 :
388 : static void
389 3861842 : 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 3861842 : struct kgetbmap *p = out + bmv->bmv_entries;
398 :
399 3861842 : if (bmv->bmv_iflags & BMV_IF_NO_HOLES)
400 : return;
401 :
402 17452 : p->bmv_block = -1;
403 17452 : p->bmv_offset = XFS_FSB_TO_BB(ip->i_mount, bno);
404 17452 : p->bmv_length = XFS_FSB_TO_BB(ip->i_mount, end - bno);
405 :
406 17452 : bmv->bmv_offset = p->bmv_offset + p->bmv_length;
407 17452 : bmv->bmv_length = max(0LL, bmv_end - bmv->bmv_offset);
408 17452 : bmv->bmv_entries++;
409 : }
410 :
411 : static inline bool
412 : xfs_getbmap_full(
413 : struct getbmapx *bmv)
414 : {
415 14495760 : return bmv->bmv_length == 0 || bmv->bmv_entries >= bmv->bmv_count - 1;
416 : }
417 :
418 : static bool
419 5205783 : xfs_getbmap_next_rec(
420 : struct xfs_bmbt_irec *rec,
421 : xfs_fileoff_t total_end)
422 : {
423 5205783 : xfs_fileoff_t end = rec->br_startoff + rec->br_blockcount;
424 :
425 5205783 : if (end == total_end)
426 : return false;
427 :
428 35860 : rec->br_startoff += rec->br_blockcount;
429 35860 : if (!isnullstartblock(rec->br_startblock) &&
430 : rec->br_startblock != DELAYSTARTBLOCK)
431 35860 : rec->br_startblock += rec->br_blockcount;
432 35860 : rec->br_blockcount = total_end - end;
433 35860 : 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 1196067 : xfs_getbmap(
445 : struct xfs_inode *ip,
446 : struct getbmapx *bmv, /* user bmap structure */
447 : struct kgetbmap *out)
448 : {
449 1196067 : struct xfs_mount *mp = ip->i_mount;
450 1196067 : int iflags = bmv->bmv_iflags;
451 1196067 : int whichfork, lock, error = 0;
452 1196067 : int64_t bmv_end, max_len;
453 1196067 : xfs_fileoff_t bno, first_bno;
454 1196067 : struct xfs_ifork *ifp;
455 1196067 : struct xfs_bmbt_irec got, rec;
456 1196067 : xfs_filblks_t len;
457 1196067 : struct xfs_iext_cursor icur;
458 :
459 1196067 : 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 1196067 : if ((iflags & BMV_IF_ATTRFORK) && (iflags & BMV_IF_COWFORK))
467 : return -EINVAL;
468 :
469 1196067 : if (bmv->bmv_length < -1)
470 : return -EINVAL;
471 1196067 : bmv->bmv_entries = 0;
472 1196067 : if (bmv->bmv_length == 0)
473 : return 0;
474 :
475 1070979 : if (iflags & BMV_IF_ATTRFORK)
476 : whichfork = XFS_ATTR_FORK;
477 1070971 : else if (iflags & BMV_IF_COWFORK)
478 : whichfork = XFS_COW_FORK;
479 : else
480 1070947 : whichfork = XFS_DATA_FORK;
481 :
482 1070979 : xfs_ilock(ip, XFS_IOLOCK_SHARED);
483 1071021 : switch (whichfork) {
484 2 : case XFS_ATTR_FORK:
485 2 : lock = xfs_ilock_attr_map_shared(ip);
486 2 : if (!xfs_inode_has_attr_fork(ip))
487 0 : goto out_unlock_ilock;
488 :
489 : max_len = 1LL << 32;
490 : break;
491 54 : case XFS_COW_FORK:
492 54 : lock = XFS_ILOCK_SHARED;
493 54 : xfs_ilock(ip, lock);
494 :
495 : /* No CoW fork? Just return */
496 54 : if (!xfs_ifork_ptr(ip, whichfork))
497 14 : goto out_unlock_ilock;
498 :
499 40 : if (xfs_get_cowextsz_hint(ip))
500 40 : max_len = mp->m_super->s_maxbytes;
501 : else
502 0 : max_len = XFS_ISIZE(ip);
503 : break;
504 1070965 : case XFS_DATA_FORK:
505 1070965 : if (!(iflags & BMV_IF_DELALLOC) &&
506 89186 : (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_disk_size)) {
507 4191 : error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
508 4193 : 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 1070967 : if (xfs_get_extsz_hint(ip) ||
522 855631 : (ip->i_diflags &
523 : (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND)))
524 458025 : max_len = mp->m_super->s_maxbytes;
525 : else
526 612890 : max_len = XFS_ISIZE(ip);
527 :
528 1070915 : lock = xfs_ilock_data_map_shared(ip);
529 1070937 : break;
530 : }
531 :
532 1070979 : ifp = xfs_ifork_ptr(ip, whichfork);
533 :
534 1070979 : switch (ifp->if_format) {
535 : case XFS_DINODE_FMT_EXTENTS:
536 : case XFS_DINODE_FMT_BTREE:
537 757290 : break;
538 313689 : case XFS_DINODE_FMT_LOCAL:
539 : /* Local format inode forks report no extents. */
540 313689 : goto out_unlock_ilock;
541 0 : default:
542 0 : error = -EINVAL;
543 0 : goto out_unlock_ilock;
544 : }
545 :
546 757290 : if (bmv->bmv_length == -1) {
547 749731 : max_len = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, max_len));
548 749731 : bmv->bmv_length = max(0LL, max_len - bmv->bmv_offset);
549 : }
550 :
551 757290 : bmv_end = bmv->bmv_offset + bmv->bmv_length;
552 :
553 757290 : first_bno = bno = XFS_BB_TO_FSBT(mp, bmv->bmv_offset);
554 757290 : len = XFS_BB_TO_FSB(mp, bmv->bmv_length);
555 :
556 757290 : error = xfs_iread_extents(NULL, ip, whichfork);
557 757256 : if (error)
558 0 : goto out_unlock_ilock;
559 :
560 757256 : 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 128601 : if (iflags & BMV_IF_DELALLOC)
566 128077 : xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
567 128077 : XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
568 128601 : goto out_unlock_ilock;
569 : }
570 :
571 10726229 : while (!xfs_getbmap_full(bmv)) {
572 5363117 : 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 5363112 : if (got.br_startoff > bno) {
579 3553371 : xfs_getbmap_report_hole(ip, bmv, out, bmv_end, bno,
580 : got.br_startoff);
581 7106742 : 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 5363080 : bno = got.br_startoff + got.br_blockcount;
591 5363080 : rec = got;
592 5398940 : do {
593 5398940 : error = xfs_getbmap_report_one(ip, bmv, out, bmv_end,
594 : &rec);
595 10797766 : if (error || xfs_getbmap_full(bmv))
596 193101 : goto out_unlock_ilock;
597 5205782 : } while (xfs_getbmap_next_rec(&rec, bno));
598 :
599 5169922 : if (!xfs_iext_next_extent(ifp, &icur, &got)) {
600 435522 : xfs_fileoff_t end = XFS_B_TO_FSB(mp, XFS_ISIZE(ip));
601 :
602 435522 : if (bmv->bmv_entries > 0)
603 435522 : out[bmv->bmv_entries - 1].bmv_oflags |=
604 : BMV_OF_LAST;
605 :
606 615916 : if (whichfork != XFS_ATTR_FORK && bno < end &&
607 : !xfs_getbmap_full(bmv)) {
608 180394 : xfs_getbmap_report_hole(ip, bmv, out, bmv_end,
609 : bno, end);
610 : }
611 : break;
612 : }
613 :
614 4734400 : if (bno >= first_bno + len)
615 : break;
616 : }
617 :
618 32 : out_unlock_ilock:
619 1070959 : xfs_iunlock(ip, lock);
620 1070973 : out_unlock_iolock:
621 1070973 : xfs_iunlock(ip, XFS_IOLOCK_SHARED);
622 1070973 : 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 7417 : xfs_bmap_punch_delalloc_range(
633 : struct xfs_inode *ip,
634 : xfs_off_t start_byte,
635 : xfs_off_t end_byte)
636 : {
637 7417 : struct xfs_mount *mp = ip->i_mount;
638 7417 : struct xfs_ifork *ifp = &ip->i_df;
639 7417 : xfs_fileoff_t start_fsb = XFS_B_TO_FSBT(mp, start_byte);
640 7417 : xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, end_byte);
641 7417 : struct xfs_bmbt_irec got, del;
642 7417 : struct xfs_iext_cursor icur;
643 7417 : int error = 0;
644 :
645 7417 : ASSERT(!xfs_need_iread_extents(ifp));
646 :
647 7417 : xfs_ilock(ip, XFS_ILOCK_EXCL);
648 7417 : if (!xfs_iext_lookup_extent_before(ip, ifp, &end_fsb, &icur, &got))
649 0 : goto out_unlock;
650 :
651 14150 : while (got.br_startoff + got.br_blockcount > start_fsb) {
652 7943 : del = got;
653 7943 : 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 7943 : if (!del.br_blockcount ||
661 7515 : !isnullstartblock(del.br_startblock)) {
662 2805 : if (!xfs_iext_prev_extent(ifp, &icur, &got))
663 : break;
664 2218 : continue;
665 : }
666 :
667 5138 : error = xfs_bmap_del_extent_delay(ip, XFS_DATA_FORK, &icur,
668 : &got, &del);
669 5138 : if (error || !xfs_iext_get_extent(ifp, &icur, &got))
670 : break;
671 : }
672 :
673 7417 : out_unlock:
674 7417 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
675 7417 : 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 1131325403 : xfs_can_free_eofblocks(
685 : struct xfs_inode *ip,
686 : bool force)
687 : {
688 1131325403 : struct xfs_bmbt_irec imap;
689 1131325403 : struct xfs_mount *mp = ip->i_mount;
690 1131325403 : xfs_fileoff_t end_fsb;
691 1131325403 : xfs_fileoff_t last_fsb;
692 1131325403 : int nimaps = 1;
693 1131325403 : 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 1131325403 : 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 1131355234 : 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 628358648 : if (VFS_I(ip)->i_size == 0 &&
711 163870534 : VFS_I(ip)->i_mapping->nrpages == 0 &&
712 163871961 : 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 464487722 : 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 449999302 : if (xfs_get_extsz_hint(ip) ||
725 302213693 : (ip->i_diflags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))) {
726 291217514 : 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 158937750 : end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
736 158937750 : if (XFS_IS_REALTIME_INODE(ip) && mp->m_sb.sb_rextsize > 1)
737 0 : end_fsb = xfs_rtb_roundup_rtx(mp, end_fsb);
738 158937750 : last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
739 158937750 : 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 158938941 : xfs_ilock(ip, XFS_ILOCK_SHARED);
747 158937934 : error = xfs_bmapi_read(ip, end_fsb, last_fsb - end_fsb, &imap, &nimaps,
748 : 0);
749 158936997 : xfs_iunlock(ip, XFS_ILOCK_SHARED);
750 158938565 : 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 293667044 : 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 6167447 : xfs_free_eofblocks(
767 : struct xfs_inode *ip)
768 : {
769 6167447 : struct xfs_trans *tp;
770 6167447 : struct xfs_mount *mp = ip->i_mount;
771 6167447 : int error;
772 :
773 : /* Attach the dquots to the inode up front. */
774 6167447 : error = xfs_qm_dqattach(ip);
775 6167504 : if (error)
776 : return error;
777 :
778 : /* Wait on dio to ensure i_size has settled. */
779 6167116 : inode_dio_wait(VFS_I(ip));
780 :
781 6167016 : error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
782 6168941 : if (error) {
783 0 : ASSERT(xfs_is_shutdown(mp));
784 0 : return error;
785 : }
786 :
787 6168941 : xfs_ilock(ip, XFS_ILOCK_EXCL);
788 6168805 : 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 12337114 : error = xfs_itruncate_extents_flags(&tp, ip, XFS_DATA_FORK,
797 : XFS_ISIZE(ip), XFS_BMAPI_NODISCARD);
798 6168470 : if (error)
799 27 : goto err_cancel;
800 :
801 6168443 : error = xfs_trans_commit(tp);
802 6168507 : if (error)
803 0 : goto out_unlock;
804 :
805 6168507 : xfs_inode_clear_eofblocks_tag(ip);
806 6168869 : 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 27 : xfs_trans_cancel(tp);
814 6168896 : out_unlock:
815 6168896 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
816 6168896 : return error;
817 : }
818 :
819 : int
820 5981169 : xfs_alloc_file_space(
821 : struct xfs_inode *ip,
822 : xfs_off_t offset,
823 : xfs_off_t len)
824 : {
825 5981169 : xfs_mount_t *mp = ip->i_mount;
826 5981169 : xfs_off_t count;
827 5981169 : xfs_filblks_t allocated_fsb;
828 5981169 : xfs_filblks_t allocatesize_fsb;
829 5981169 : xfs_extlen_t extsz, temp;
830 5981169 : xfs_fileoff_t startoffset_fsb;
831 5981169 : xfs_fileoff_t endoffset_fsb;
832 5981169 : int nimaps;
833 5981169 : int rt;
834 5981169 : xfs_trans_t *tp;
835 5981169 : xfs_bmbt_irec_t imaps[1], *imapp;
836 5981169 : int error;
837 :
838 5981169 : trace_xfs_alloc_file_space(ip, offset, len);
839 :
840 11962346 : if (xfs_is_shutdown(mp))
841 : return -EIO;
842 :
843 5981171 : error = xfs_qm_dqattach(ip);
844 5981171 : if (error)
845 : return error;
846 :
847 5981171 : if (len <= 0)
848 : return -EINVAL;
849 :
850 5981171 : rt = XFS_IS_REALTIME_INODE(ip);
851 5981171 : extsz = xfs_get_extsz_hint(ip);
852 :
853 5981171 : count = len;
854 5981171 : imapp = &imaps[0];
855 5981171 : nimaps = 1;
856 5981171 : startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
857 5981171 : endoffset_fsb = XFS_B_TO_FSB(mp, offset + count);
858 5981171 : allocatesize_fsb = endoffset_fsb - startoffset_fsb;
859 :
860 : /*
861 : * Allocate file space until done or until there is an error
862 : */
863 20304969 : while (allocatesize_fsb && !error) {
864 14423730 : xfs_fileoff_t s, e;
865 14423730 : unsigned int dblocks, rblocks, resblks;
866 :
867 : /*
868 : * Determine space reservations for data/realtime.
869 : */
870 14423730 : if (unlikely(extsz)) {
871 8372007 : s = startoffset_fsb;
872 8372007 : do_div(s, extsz);
873 8372007 : s *= extsz;
874 8372007 : e = startoffset_fsb + allocatesize_fsb;
875 8372007 : div_u64_rem(startoffset_fsb, extsz, &temp);
876 8372007 : if (temp)
877 16 : e += temp;
878 8372007 : div_u64_rem(e, extsz, &temp);
879 8372007 : if (temp)
880 24 : 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 14423730 : resblks = min_t(xfs_fileoff_t, (e - s),
895 : (XFS_MAX_BMBT_EXTLEN * nimaps));
896 14423730 : if (unlikely(rt)) {
897 8371595 : dblocks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
898 8371595 : rblocks = resblks;
899 : } else {
900 6052135 : dblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
901 6052135 : rblocks = 0;
902 : }
903 :
904 14423730 : error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write,
905 : dblocks, rblocks, false, &tp);
906 14423748 : if (error)
907 : break;
908 :
909 14324139 : error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
910 : XFS_IEXT_ADD_NOSPLIT_CNT);
911 14324130 : if (error == -EFBIG)
912 4 : error = xfs_iext_count_upgrade(tp, ip,
913 : XFS_IEXT_ADD_NOSPLIT_CNT);
914 14324130 : if (error)
915 4 : goto error;
916 :
917 14324126 : error = xfs_bmapi_write(tp, ip, startoffset_fsb,
918 : allocatesize_fsb, XFS_BMAPI_PREALLOC, 0, imapp,
919 : &nimaps);
920 14324097 : if (error)
921 200 : goto error;
922 :
923 14323897 : ip->i_diflags |= XFS_DIFLAG_PREALLOC;
924 14323897 : xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
925 :
926 14323910 : error = xfs_trans_commit(tp);
927 14323925 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
928 14323922 : if (error)
929 : break;
930 :
931 14323805 : allocated_fsb = imapp->br_blockcount;
932 :
933 14323805 : if (nimaps == 0) {
934 : error = -ENOSPC;
935 : break;
936 : }
937 :
938 14323798 : startoffset_fsb += allocated_fsb;
939 14323798 : allocatesize_fsb -= allocated_fsb;
940 : }
941 :
942 : return error;
943 :
944 204 : error:
945 204 : xfs_trans_cancel(tp);
946 204 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
947 204 : return error;
948 : }
949 :
950 : static int
951 11401435 : 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 11401435 : struct xfs_mount *mp = ip->i_mount;
958 11401435 : struct xfs_trans *tp;
959 11401435 : uint resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
960 11401435 : int error;
961 :
962 11401435 : error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, resblks, 0,
963 : false, &tp);
964 11401448 : if (error)
965 : return error;
966 :
967 11377302 : error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
968 : XFS_IEXT_PUNCH_HOLE_CNT);
969 11377292 : if (error == -EFBIG)
970 24 : error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
971 11377292 : if (error)
972 24 : goto out_trans_cancel;
973 :
974 11377268 : error = xfs_bunmapi(tp, ip, startoffset_fsb, len_fsb, 0, 2, done);
975 11377269 : if (error)
976 6 : goto out_trans_cancel;
977 :
978 11377263 : error = xfs_trans_commit(tp);
979 11377305 : out_unlock:
980 11377305 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
981 11377305 : return error;
982 :
983 30 : out_trans_cancel:
984 30 : xfs_trans_cancel(tp);
985 30 : goto out_unlock;
986 : }
987 :
988 : /* Caller must first wait for the completion of any pending DIOs if required. */
989 : int
990 210075235 : xfs_flush_unmap_range(
991 : struct xfs_inode *ip,
992 : xfs_off_t offset,
993 : xfs_off_t len)
994 : {
995 210075235 : struct xfs_mount *mp = ip->i_mount;
996 210075235 : struct inode *inode = VFS_I(ip);
997 210075235 : xfs_off_t rounding, start, end;
998 210075235 : int error;
999 :
1000 210075235 : rounding = max_t(xfs_off_t, mp->m_sb.sb_blocksize, PAGE_SIZE);
1001 210075235 : start = round_down(offset, rounding);
1002 210075235 : end = round_up(offset + len, rounding) - 1;
1003 :
1004 210075235 : error = filemap_write_and_wait_range(inode->i_mapping, start, end);
1005 210082518 : if (error)
1006 : return error;
1007 210083838 : truncate_pagecache_range(inode, start, end);
1008 210083838 : return 0;
1009 : }
1010 :
1011 : int
1012 10770035 : xfs_free_file_space(
1013 : struct xfs_inode *ip,
1014 : xfs_off_t offset,
1015 : xfs_off_t len)
1016 : {
1017 10770035 : struct xfs_mount *mp = ip->i_mount;
1018 10770035 : xfs_fileoff_t startoffset_fsb;
1019 10770035 : xfs_fileoff_t endoffset_fsb;
1020 10770035 : int done = 0, error;
1021 :
1022 10770035 : trace_xfs_free_file_space(ip, offset, len);
1023 :
1024 10770031 : error = xfs_qm_dqattach(ip);
1025 10770028 : if (error)
1026 : return error;
1027 :
1028 10770028 : if (len <= 0) /* if nothing being freed */
1029 : return 0;
1030 :
1031 10770028 : startoffset_fsb = XFS_B_TO_FSB(mp, offset);
1032 10770028 : endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
1033 :
1034 : /* We can only free complete realtime extents. */
1035 10770028 : if (xfs_inode_has_bigrtextents(ip)) {
1036 28 : startoffset_fsb = xfs_rtb_roundup_rtx(mp, startoffset_fsb);
1037 28 : 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 10770028 : if (endoffset_fsb > startoffset_fsb) {
1044 21976444 : while (!done) {
1045 11401432 : error = xfs_unmap_extent(ip, startoffset_fsb,
1046 : endoffset_fsb - startoffset_fsb, &done);
1047 11401450 : if (error)
1048 24199 : 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 21491694 : if (offset >= XFS_ISIZE(ip))
1059 : return 0;
1060 9381414 : if (offset + len > XFS_ISIZE(ip))
1061 315288 : len = XFS_ISIZE(ip) - offset;
1062 9381414 : error = xfs_zero_range(ip, offset, len, NULL);
1063 9381409 : 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 18762614 : if (offset + len >= XFS_ISIZE(ip) && offset_in_page(offset + len) > 0) {
1073 392634 : 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 877134 : xfs_prepare_shift(
1082 : struct xfs_inode *ip,
1083 : loff_t offset)
1084 : {
1085 877134 : struct xfs_mount *mp = ip->i_mount;
1086 877134 : int error;
1087 :
1088 : /*
1089 : * Trim eofblocks to avoid shifting uninitialized post-eof preallocation
1090 : * into the accessible region of the file.
1091 : */
1092 877134 : if (xfs_can_free_eofblocks(ip, true)) {
1093 170109 : error = xfs_free_eofblocks(ip);
1094 170109 : 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 877134 : offset = round_down(offset, mp->m_sb.sb_blocksize);
1106 877134 : if (offset)
1107 804517 : 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 1754268 : error = xfs_flush_unmap_range(ip, offset, XFS_ISIZE(ip));
1114 877134 : 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 1754088 : if (xfs_inode_has_cow_data(ip)) {
1123 75474 : error = xfs_reflink_cancel_cow_range(ip, offset, NULLFILEOFF,
1124 : true);
1125 75474 : 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 474536 : xfs_collapse_file_space(
1146 : struct xfs_inode *ip,
1147 : xfs_off_t offset,
1148 : xfs_off_t len)
1149 : {
1150 474536 : struct xfs_mount *mp = ip->i_mount;
1151 474536 : struct xfs_trans *tp;
1152 474536 : int error;
1153 474536 : xfs_fileoff_t next_fsb = XFS_B_TO_FSB(mp, offset + len);
1154 474536 : xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len);
1155 474536 : bool done = false;
1156 :
1157 474536 : ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1158 474536 : ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
1159 :
1160 474536 : trace_xfs_collapse_file_space(ip, offset, len);
1161 :
1162 474536 : error = xfs_free_file_space(ip, offset, len);
1163 474536 : if (error)
1164 : return error;
1165 :
1166 472792 : error = xfs_prepare_shift(ip, offset);
1167 472792 : if (error)
1168 : return error;
1169 :
1170 472733 : error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write, 0, 0, 0, &tp);
1171 472733 : if (error)
1172 : return error;
1173 :
1174 472733 : xfs_ilock(ip, XFS_ILOCK_EXCL);
1175 472733 : xfs_trans_ijoin(tp, ip, 0);
1176 :
1177 19351633 : while (!done) {
1178 19351633 : error = xfs_bmap_collapse_extents(tp, ip, &next_fsb, shift_fsb,
1179 : &done);
1180 19351633 : if (error)
1181 1 : goto out_trans_cancel;
1182 19351632 : if (done)
1183 : break;
1184 :
1185 : /* finish any deferred frees and roll the transaction */
1186 18878903 : error = xfs_defer_finish(&tp);
1187 18878903 : if (error)
1188 3 : goto out_trans_cancel;
1189 : }
1190 :
1191 472729 : error = xfs_trans_commit(tp);
1192 472729 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1193 472729 : return error;
1194 :
1195 4 : out_trans_cancel:
1196 4 : xfs_trans_cancel(tp);
1197 4 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1198 4 : 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 404342 : xfs_insert_file_space(
1215 : struct xfs_inode *ip,
1216 : loff_t offset,
1217 : loff_t len)
1218 : {
1219 404342 : struct xfs_mount *mp = ip->i_mount;
1220 404342 : struct xfs_trans *tp;
1221 404342 : int error;
1222 404342 : xfs_fileoff_t stop_fsb = XFS_B_TO_FSB(mp, offset);
1223 404342 : xfs_fileoff_t next_fsb = NULLFSBLOCK;
1224 404342 : xfs_fileoff_t shift_fsb = XFS_B_TO_FSB(mp, len);
1225 404342 : bool done = false;
1226 :
1227 404342 : ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1228 404342 : ASSERT(xfs_isilocked(ip, XFS_MMAPLOCK_EXCL));
1229 :
1230 404342 : trace_xfs_insert_file_space(ip, offset, len);
1231 :
1232 404342 : error = xfs_bmap_can_insert_extents(ip, stop_fsb, shift_fsb);
1233 404342 : if (error)
1234 : return error;
1235 :
1236 404342 : error = xfs_prepare_shift(ip, offset);
1237 404342 : if (error)
1238 : return error;
1239 :
1240 404311 : error = xfs_trans_alloc(mp, &M_RES(mp)->tr_write,
1241 404311 : XFS_DIOSTRAT_SPACE_RES(mp, 0), 0, 0, &tp);
1242 404311 : if (error)
1243 : return error;
1244 :
1245 402567 : xfs_ilock(ip, XFS_ILOCK_EXCL);
1246 402567 : xfs_trans_ijoin(tp, ip, 0);
1247 :
1248 402567 : error = xfs_iext_count_may_overflow(ip, XFS_DATA_FORK,
1249 : XFS_IEXT_PUNCH_HOLE_CNT);
1250 402567 : if (error == -EFBIG)
1251 2 : error = xfs_iext_count_upgrade(tp, ip, XFS_IEXT_PUNCH_HOLE_CNT);
1252 402567 : if (error)
1253 2 : 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 402565 : error = xfs_bmap_split_extent(tp, ip, stop_fsb);
1261 402565 : if (error)
1262 0 : goto out_trans_cancel;
1263 :
1264 1997485 : do {
1265 1997485 : error = xfs_defer_finish(&tp);
1266 1997485 : if (error)
1267 1 : goto out_trans_cancel;
1268 :
1269 1997484 : error = xfs_bmap_insert_extents(tp, ip, &next_fsb, shift_fsb,
1270 : &done, stop_fsb);
1271 1997484 : if (error)
1272 1 : goto out_trans_cancel;
1273 1997483 : } while (!done);
1274 :
1275 402563 : error = xfs_trans_commit(tp);
1276 402563 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1277 402563 : return error;
1278 :
1279 4 : out_trans_cancel:
1280 4 : xfs_trans_cancel(tp);
1281 4 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1282 4 : 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 44 : xfs_map_free_reserve_more(
1292 : struct xfs_trans *tp,
1293 : struct xfs_inode *ip,
1294 : xfs_extlen_t *len)
1295 : {
1296 44 : struct xfs_mount *mp = ip->i_mount;
1297 44 : unsigned int dblocks;
1298 44 : unsigned int rblocks;
1299 44 : unsigned int min_len;
1300 44 : bool isrt = XFS_IS_REALTIME_INODE(ip);
1301 44 : int error;
1302 :
1303 44 : if (*len > XFS_MAX_BMBT_EXTLEN)
1304 4 : *len = XFS_MAX_BMBT_EXTLEN;
1305 44 : min_len = isrt ? mp->m_sb.sb_rextsize : 1;
1306 :
1307 172 : again:
1308 172 : if (isrt) {
1309 0 : dblocks = XFS_EXTENTADD_SPACE_RES(mp, XFS_DATA_FORK);
1310 0 : rblocks = *len;
1311 : } else {
1312 172 : dblocks = XFS_DIOSTRAT_SPACE_RES(mp, *len);
1313 172 : rblocks = 0;
1314 : }
1315 172 : error = xfs_trans_reserve_more_inode(tp, ip, dblocks, rblocks, false);
1316 172 : if (error == -ENOSPC && *len > min_len) {
1317 128 : *len >>= 1;
1318 128 : goto again;
1319 : }
1320 44 : if (error) {
1321 2 : trace_xfs_map_free_reserve_more_fail(ip, error, _RET_IP_);
1322 2 : 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 226 : 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 226 : struct xfs_bmbt_irec irec;
1338 226 : struct xfs_mount *mp = ip->i_mount;
1339 226 : struct xfs_trans *tp;
1340 226 : xfs_off_t endpos;
1341 226 : xfs_fsblock_t fsbno;
1342 226 : xfs_extlen_t len;
1343 226 : int error;
1344 :
1345 226 : if (fatal_signal_pending(current))
1346 : return -EINTR;
1347 :
1348 226 : error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, 0, 0, false,
1349 : &tp);
1350 226 : if (error)
1351 : return error;
1352 :
1353 226 : error = xfs_alloc_find_freesp(tp, pag, cursor, end_agbno, &len);
1354 226 : if (error)
1355 0 : goto out_cancel;
1356 :
1357 : /* Bail out if the cursor is beyond what we asked for. */
1358 226 : if (*cursor >= end_agbno)
1359 182 : goto out_cancel;
1360 :
1361 44 : error = xfs_map_free_reserve_more(tp, ip, &len);
1362 44 : if (error)
1363 2 : goto out_cancel;
1364 :
1365 42 : fsbno = XFS_AGB_TO_FSB(mp, pag->pag_agno, *cursor);
1366 210 : do {
1367 210 : error = xfs_bmapi_freesp(tp, ip, fsbno, len, &irec);
1368 210 : 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 210 : 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 180 : len >>= 1;
1381 180 : } while (len > 0);
1382 42 : 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 6 : *last_enospc_agbno = *cursor;
1389 6 : error = 0;
1390 6 : goto out_cancel;
1391 : }
1392 36 : if (error)
1393 6 : goto out_cancel;
1394 :
1395 : /* Update isize if needed. */
1396 30 : endpos = XFS_FSB_TO_B(mp, irec.br_startoff + irec.br_blockcount);
1397 30 : if (endpos > i_size_read(VFS_I(ip))) {
1398 2 : i_size_write(VFS_I(ip), endpos);
1399 2 : ip->i_disk_size = endpos;
1400 2 : xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1401 : }
1402 :
1403 30 : error = xfs_trans_commit(tp);
1404 30 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1405 30 : if (error)
1406 : return error;
1407 :
1408 30 : *cursor += irec.br_blockcount;
1409 30 : return 0;
1410 196 : out_cancel:
1411 196 : xfs_trans_cancel(tp);
1412 196 : out:
1413 196 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1414 196 : 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 204 : xfs_map_free_space(
1423 : struct xfs_inode *ip,
1424 : xfs_off_t off,
1425 : xfs_off_t len)
1426 : {
1427 204 : struct xfs_mount *mp = ip->i_mount;
1428 204 : struct xfs_perag *pag = NULL;
1429 204 : xfs_daddr_t off_daddr = BTOBB(off);
1430 204 : xfs_daddr_t end_daddr = BTOBBT(off + len);
1431 204 : xfs_fsblock_t off_fsb = XFS_DADDR_TO_FSB(mp, off_daddr);
1432 204 : xfs_fsblock_t end_fsb = XFS_DADDR_TO_FSB(mp, end_daddr);
1433 204 : xfs_agnumber_t off_agno = XFS_FSB_TO_AGNO(mp, off_fsb);
1434 204 : xfs_agnumber_t end_agno = XFS_FSB_TO_AGNO(mp, end_fsb);
1435 204 : xfs_agnumber_t agno;
1436 204 : int error = 0;
1437 :
1438 204 : trace_xfs_map_free_space(ip, off, len);
1439 :
1440 204 : agno = off_agno;
1441 398 : for_each_perag_range(mp, agno, end_agno, pag) {
1442 202 : xfs_agblock_t off_agbno = 0;
1443 202 : xfs_agblock_t end_agbno;
1444 202 : xfs_agblock_t last_enospc_agbno = NULLAGBLOCK;
1445 :
1446 202 : end_agbno = xfs_ag_block_count(mp, pag->pag_agno);
1447 :
1448 202 : if (pag->pag_agno == off_agno)
1449 202 : off_agbno = XFS_FSB_TO_AGBNO(mp, off_fsb);
1450 202 : if (pag->pag_agno == end_agno)
1451 202 : end_agbno = XFS_FSB_TO_AGBNO(mp, end_fsb);
1452 :
1453 420 : while (off_agbno < end_agbno) {
1454 226 : error = xfs_map_free_extent(ip, pag, &off_agbno,
1455 : end_agbno, &last_enospc_agbno);
1456 226 : if (error)
1457 8 : goto out;
1458 : }
1459 : }
1460 :
1461 196 : out:
1462 204 : if (pag)
1463 8 : xfs_perag_rele(pag);
1464 204 : if (error == -ENOSPC)
1465 8 : return 0;
1466 : return error;
1467 : }
1468 :
1469 : #ifdef CONFIG_XFS_RT
1470 : STATIC int
1471 0 : xfs_map_free_rt_extent(
1472 : struct xfs_inode *ip,
1473 : xfs_rtxnum_t *cursor,
1474 : xfs_rtxnum_t end_rtx)
1475 : {
1476 0 : struct xfs_bmbt_irec irec;
1477 0 : struct xfs_mount *mp = ip->i_mount;
1478 0 : struct xfs_trans *tp;
1479 0 : xfs_off_t endpos;
1480 0 : xfs_rtblock_t rtbno;
1481 0 : xfs_rtxnum_t add;
1482 0 : xfs_rtxlen_t len_rtx;
1483 0 : xfs_extlen_t len;
1484 0 : uint32_t mod;
1485 0 : int error;
1486 :
1487 0 : if (fatal_signal_pending(current))
1488 : return -EINTR;
1489 :
1490 0 : error = xfs_trans_alloc_inode(ip, &M_RES(mp)->tr_write, 0, 0, false,
1491 : &tp);
1492 0 : if (error)
1493 : return error;
1494 :
1495 0 : xfs_rtbitmap_lock(tp, mp);
1496 0 : error = xfs_rtalloc_find_freesp(tp, cursor, end_rtx, &len_rtx);
1497 0 : 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 0 : if (*cursor >= end_rtx)
1505 0 : goto out_cancel;
1506 :
1507 0 : len = xfs_rtx_to_rtb(mp, len_rtx);
1508 0 : error = xfs_map_free_reserve_more(tp, ip, &len);
1509 0 : if (error)
1510 0 : goto out_cancel;
1511 :
1512 0 : rtbno = xfs_rtx_to_rtb(mp, *cursor);
1513 0 : error = xfs_bmapi_freesp(tp, ip, rtbno, len, &irec);
1514 0 : if (error)
1515 0 : goto out_cancel;
1516 :
1517 : /* Update isize if needed. */
1518 0 : endpos = XFS_FSB_TO_B(mp, irec.br_startoff + irec.br_blockcount);
1519 0 : if (endpos > i_size_read(VFS_I(ip))) {
1520 0 : i_size_write(VFS_I(ip), endpos);
1521 0 : ip->i_disk_size = endpos;
1522 0 : xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
1523 : }
1524 :
1525 0 : error = xfs_trans_commit(tp);
1526 0 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1527 0 : if (error)
1528 : return error;
1529 :
1530 0 : add = xfs_rtb_to_rtx(mp, irec.br_blockcount, &mod);
1531 0 : if (mod)
1532 : return -EFSCORRUPTED;
1533 :
1534 0 : *cursor += add;
1535 0 : return 0;
1536 0 : out_cancel:
1537 0 : xfs_trans_cancel(tp);
1538 0 : xfs_iunlock(ip, XFS_ILOCK_EXCL);
1539 0 : 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 0 : xfs_map_free_rt_space(
1548 : struct xfs_inode *ip,
1549 : xfs_off_t off,
1550 : xfs_off_t len)
1551 : {
1552 0 : struct xfs_mount *mp = ip->i_mount;
1553 0 : xfs_rtblock_t off_rtb = XFS_B_TO_FSB(mp, off);
1554 0 : xfs_rtblock_t end_rtb = XFS_B_TO_FSBT(mp, off + len);
1555 0 : xfs_rtxnum_t off_rtx;
1556 0 : xfs_rtxnum_t end_rtx;
1557 0 : uint32_t mod;
1558 0 : int error = 0;
1559 :
1560 : /* Compute rt extents from the input parameters. */
1561 0 : off_rtx = xfs_rtb_to_rtx(mp, off_rtb, &mod);
1562 0 : if (mod)
1563 0 : off_rtx++;
1564 0 : end_rtx = xfs_rtb_to_rtxt(mp, end_rtb);
1565 :
1566 0 : if (off_rtx >= mp->m_sb.sb_rextents)
1567 : return 0;
1568 0 : if (end_rtx >= mp->m_sb.sb_rextents)
1569 0 : end_rtx = mp->m_sb.sb_rextents - 1;
1570 :
1571 0 : trace_xfs_map_free_rt_space(ip, off, len);
1572 :
1573 0 : while (off_rtx < end_rtx) {
1574 0 : error = xfs_map_free_rt_extent(ip, &off_rtx, end_rtx);
1575 0 : if (error)
1576 : break;
1577 : }
1578 :
1579 0 : if (error == -ENOSPC)
1580 0 : return 0;
1581 : return error;
1582 : }
1583 : #endif
|