1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include "xfs.h"
20#include "xfs_fs.h"
21#include "xfs_shared.h"
22#include "xfs_format.h"
23#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
25#include "xfs_bit.h"
26#include "xfs_mount.h"
27#include "xfs_da_format.h"
28#include "xfs_inode.h"
29#include "xfs_btree.h"
30#include "xfs_trans.h"
31#include "xfs_extfree_item.h"
32#include "xfs_alloc.h"
33#include "xfs_bmap.h"
34#include "xfs_bmap_util.h"
35#include "xfs_bmap_btree.h"
36#include "xfs_rtalloc.h"
37#include "xfs_error.h"
38#include "xfs_quota.h"
39#include "xfs_trans_space.h"
40#include "xfs_trace.h"
41#include "xfs_icache.h"
42#include "xfs_log.h"
43
44
45
46
47
48
49
50
51xfs_daddr_t
52xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
53{
54 return (XFS_IS_REALTIME_INODE(ip) ? \
55 (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
56 XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
57}
58
59
60
61
62
63
64
65
66
67
68int
69xfs_bmap_finish(
70 xfs_trans_t **tp,
71 xfs_bmap_free_t *flist,
72 int *committed)
73{
74 xfs_efd_log_item_t *efd;
75 xfs_efi_log_item_t *efi;
76 int error;
77 xfs_bmap_free_item_t *free;
78 struct xfs_trans_res tres;
79 xfs_mount_t *mp;
80 xfs_bmap_free_item_t *next;
81 xfs_trans_t *ntp;
82
83 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
84 if (flist->xbf_count == 0) {
85 *committed = 0;
86 return 0;
87 }
88 ntp = *tp;
89 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
90 for (free = flist->xbf_first; free; free = free->xbfi_next)
91 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
92 free->xbfi_blockcount);
93
94 tres.tr_logres = ntp->t_log_res;
95 tres.tr_logcount = ntp->t_log_count;
96 tres.tr_logflags = XFS_TRANS_PERM_LOG_RES;
97 ntp = xfs_trans_dup(*tp);
98 error = xfs_trans_commit(*tp, 0);
99 *tp = ntp;
100 *committed = 1;
101
102
103
104
105 if (error)
106 return error;
107
108
109
110
111
112 xfs_log_ticket_put(ntp->t_ticket);
113
114 error = xfs_trans_reserve(ntp, &tres, 0, 0);
115 if (error)
116 return error;
117 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
118 for (free = flist->xbf_first; free != NULL; free = next) {
119 next = free->xbfi_next;
120 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
121 free->xbfi_blockcount))) {
122
123
124
125
126
127
128
129
130 mp = ntp->t_mountp;
131 if (!XFS_FORCED_SHUTDOWN(mp))
132 xfs_force_shutdown(mp,
133 (error == -EFSCORRUPTED) ?
134 SHUTDOWN_CORRUPT_INCORE :
135 SHUTDOWN_META_IO_ERROR);
136 return error;
137 }
138 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
139 free->xbfi_blockcount);
140 xfs_bmap_del_free(flist, NULL, free);
141 }
142 return 0;
143}
144
145int
146xfs_bmap_rtalloc(
147 struct xfs_bmalloca *ap)
148{
149 xfs_alloctype_t atype = 0;
150 int error;
151 xfs_mount_t *mp;
152 xfs_extlen_t prod = 0;
153 xfs_extlen_t ralen = 0;
154 xfs_extlen_t align;
155 xfs_rtblock_t rtb;
156
157 mp = ap->ip->i_mount;
158 align = xfs_get_extsz_hint(ap->ip);
159 prod = align / mp->m_sb.sb_rextsize;
160 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
161 align, 1, ap->eof, 0,
162 ap->conv, &ap->offset, &ap->length);
163 if (error)
164 return error;
165 ASSERT(ap->length);
166 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
167
168
169
170
171
172 if (do_mod(ap->offset, align) || ap->length % align)
173 prod = 1;
174
175
176
177 ralen = ap->length / mp->m_sb.sb_rextsize;
178
179
180
181
182
183
184
185 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
186 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
187
188
189
190
191 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
192 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
193
194
195
196
197
198 if (ap->eof && ap->offset == 0) {
199 xfs_rtblock_t uninitialized_var(rtx);
200
201 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
202 if (error)
203 return error;
204 ap->blkno = rtx * mp->m_sb.sb_rextsize;
205 } else {
206 ap->blkno = 0;
207 }
208
209 xfs_bmap_adjacent(ap);
210
211
212
213
214 atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
215 do_div(ap->blkno, mp->m_sb.sb_rextsize);
216 rtb = ap->blkno;
217 ap->length = ralen;
218 if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
219 &ralen, atype, ap->wasdel, prod, &rtb)))
220 return error;
221 if (rtb == NULLFSBLOCK && prod > 1 &&
222 (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
223 ap->length, &ralen, atype,
224 ap->wasdel, 1, &rtb)))
225 return error;
226 ap->blkno = rtb;
227 if (ap->blkno != NULLFSBLOCK) {
228 ap->blkno *= mp->m_sb.sb_rextsize;
229 ralen *= mp->m_sb.sb_rextsize;
230 ap->length = ralen;
231 ap->ip->i_d.di_nblocks += ralen;
232 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
233 if (ap->wasdel)
234 ap->ip->i_delayed_blks -= ralen;
235
236
237
238
239 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
240 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
241 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
242 } else {
243 ap->length = 0;
244 }
245 return 0;
246}
247
248
249
250
251
252
253int
254xfs_bmap_eof(
255 struct xfs_inode *ip,
256 xfs_fileoff_t endoff,
257 int whichfork,
258 int *eof)
259{
260 struct xfs_bmbt_irec rec;
261 int error;
262
263 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
264 if (error || *eof)
265 return error;
266
267 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
268 return 0;
269}
270
271
272
273
274
275
276
277
278STATIC void
279xfs_bmap_count_leaves(
280 xfs_ifork_t *ifp,
281 xfs_extnum_t idx,
282 int numrecs,
283 int *count)
284{
285 int b;
286
287 for (b = 0; b < numrecs; b++) {
288 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
289 *count += xfs_bmbt_get_blockcount(frp);
290 }
291}
292
293
294
295
296
297STATIC void
298xfs_bmap_disk_count_leaves(
299 struct xfs_mount *mp,
300 struct xfs_btree_block *block,
301 int numrecs,
302 int *count)
303{
304 int b;
305 xfs_bmbt_rec_t *frp;
306
307 for (b = 1; b <= numrecs; b++) {
308 frp = XFS_BMBT_REC_ADDR(mp, block, b);
309 *count += xfs_bmbt_disk_get_blockcount(frp);
310 }
311}
312
313
314
315
316
317STATIC int
318xfs_bmap_count_tree(
319 xfs_mount_t *mp,
320 xfs_trans_t *tp,
321 xfs_ifork_t *ifp,
322 xfs_fsblock_t blockno,
323 int levelin,
324 int *count)
325{
326 int error;
327 xfs_buf_t *bp, *nbp;
328 int level = levelin;
329 __be64 *pp;
330 xfs_fsblock_t bno = blockno;
331 xfs_fsblock_t nextbno;
332 struct xfs_btree_block *block, *nextblock;
333 int numrecs;
334
335 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
336 &xfs_bmbt_buf_ops);
337 if (error)
338 return error;
339 *count += 1;
340 block = XFS_BUF_TO_BLOCK(bp);
341
342 if (--level) {
343
344 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
345 while (nextbno != NULLFSBLOCK) {
346 error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
347 XFS_BMAP_BTREE_REF,
348 &xfs_bmbt_buf_ops);
349 if (error)
350 return error;
351 *count += 1;
352 nextblock = XFS_BUF_TO_BLOCK(nbp);
353 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
354 xfs_trans_brelse(tp, nbp);
355 }
356
357
358 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
359 bno = be64_to_cpu(*pp);
360 if (unlikely((error =
361 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
362 xfs_trans_brelse(tp, bp);
363 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
364 XFS_ERRLEVEL_LOW, mp);
365 return -EFSCORRUPTED;
366 }
367 xfs_trans_brelse(tp, bp);
368 } else {
369
370 for (;;) {
371 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
372 numrecs = be16_to_cpu(block->bb_numrecs);
373 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
374 xfs_trans_brelse(tp, bp);
375 if (nextbno == NULLFSBLOCK)
376 break;
377 bno = nextbno;
378 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
379 XFS_BMAP_BTREE_REF,
380 &xfs_bmbt_buf_ops);
381 if (error)
382 return error;
383 *count += 1;
384 block = XFS_BUF_TO_BLOCK(bp);
385 }
386 }
387 return 0;
388}
389
390
391
392
393int
394xfs_bmap_count_blocks(
395 xfs_trans_t *tp,
396 xfs_inode_t *ip,
397 int whichfork,
398 int *count)
399{
400 struct xfs_btree_block *block;
401 xfs_fsblock_t bno;
402 xfs_ifork_t *ifp;
403 int level;
404 xfs_mount_t *mp;
405 __be64 *pp;
406
407 bno = NULLFSBLOCK;
408 mp = ip->i_mount;
409 ifp = XFS_IFORK_PTR(ip, whichfork);
410 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
411 xfs_bmap_count_leaves(ifp, 0,
412 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
413 count);
414 return 0;
415 }
416
417
418
419
420 block = ifp->if_broot;
421 level = be16_to_cpu(block->bb_level);
422 ASSERT(level > 0);
423 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
424 bno = be64_to_cpu(*pp);
425 ASSERT(bno != NULLFSBLOCK);
426 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
427 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
428
429 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
430 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
431 mp);
432 return -EFSCORRUPTED;
433 }
434
435 return 0;
436}
437
438
439
440
441STATIC int
442xfs_getbmapx_fix_eof_hole(
443 xfs_inode_t *ip,
444 struct getbmapx *out,
445 int prealloced,
446
447 __int64_t end,
448 xfs_fsblock_t startblock)
449{
450 __int64_t fixlen;
451 xfs_mount_t *mp;
452 xfs_ifork_t *ifp;
453 xfs_extnum_t lastx;
454 xfs_fileoff_t fileblock;
455
456 if (startblock == HOLESTARTBLOCK) {
457 mp = ip->i_mount;
458 out->bmv_block = -1;
459 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
460 fixlen -= out->bmv_offset;
461 if (prealloced && out->bmv_offset + out->bmv_length == end) {
462
463 if (fixlen <= 0)
464 return 0;
465 out->bmv_length = fixlen;
466 }
467 } else {
468 if (startblock == DELAYSTARTBLOCK)
469 out->bmv_block = -2;
470 else
471 out->bmv_block = xfs_fsb_to_db(ip, startblock);
472 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
473 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
474 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
475 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
476 out->bmv_oflags |= BMV_OF_LAST;
477 }
478
479 return 1;
480}
481
482
483
484
485
486
487
488
489int
490xfs_getbmap(
491 xfs_inode_t *ip,
492 struct getbmapx *bmv,
493 xfs_bmap_format_t formatter,
494 void *arg)
495{
496 __int64_t bmvend;
497 int error = 0;
498 __int64_t fixlen;
499 int i;
500 int lock;
501 xfs_bmbt_irec_t *map;
502 xfs_mount_t *mp;
503 int nex;
504 int nexleft;
505 int subnex;
506 int nmap;
507 struct getbmapx *out;
508 int whichfork;
509 int prealloced;
510
511 int iflags;
512 int bmapi_flags;
513 int cur_ext = 0;
514
515 mp = ip->i_mount;
516 iflags = bmv->bmv_iflags;
517 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
518
519 if (whichfork == XFS_ATTR_FORK) {
520 if (XFS_IFORK_Q(ip)) {
521 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
522 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
523 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
524 return -EINVAL;
525 } else if (unlikely(
526 ip->i_d.di_aformat != 0 &&
527 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
528 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
529 ip->i_mount);
530 return -EFSCORRUPTED;
531 }
532
533 prealloced = 0;
534 fixlen = 1LL << 32;
535 } else {
536 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
537 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
538 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
539 return -EINVAL;
540
541 if (xfs_get_extsz_hint(ip) ||
542 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
543 prealloced = 1;
544 fixlen = mp->m_super->s_maxbytes;
545 } else {
546 prealloced = 0;
547 fixlen = XFS_ISIZE(ip);
548 }
549 }
550
551 if (bmv->bmv_length == -1) {
552 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
553 bmv->bmv_length =
554 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
555 } else if (bmv->bmv_length == 0) {
556 bmv->bmv_entries = 0;
557 return 0;
558 } else if (bmv->bmv_length < 0) {
559 return -EINVAL;
560 }
561
562 nex = bmv->bmv_count - 1;
563 if (nex <= 0)
564 return -EINVAL;
565 bmvend = bmv->bmv_offset + bmv->bmv_length;
566
567
568 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
569 return -ENOMEM;
570 out = kmem_zalloc_large(bmv->bmv_count * sizeof(struct getbmapx), 0);
571 if (!out)
572 return -ENOMEM;
573
574 xfs_ilock(ip, XFS_IOLOCK_SHARED);
575 if (whichfork == XFS_DATA_FORK) {
576 if (!(iflags & BMV_IF_DELALLOC) &&
577 (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size)) {
578 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
579 if (error)
580 goto out_unlock_iolock;
581
582
583
584
585
586
587
588
589
590 }
591
592 lock = xfs_ilock_data_map_shared(ip);
593 } else {
594 lock = xfs_ilock_attr_map_shared(ip);
595 }
596
597
598
599
600
601 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
602 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
603
604 bmapi_flags = xfs_bmapi_aflag(whichfork);
605 if (!(iflags & BMV_IF_PREALLOC))
606 bmapi_flags |= XFS_BMAPI_IGSTATE;
607
608
609
610
611 error = -ENOMEM;
612 subnex = 16;
613 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
614 if (!map)
615 goto out_unlock_ilock;
616
617 bmv->bmv_entries = 0;
618
619 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
620 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
621 error = 0;
622 goto out_free_map;
623 }
624
625 nexleft = nex;
626
627 do {
628 nmap = (nexleft > subnex) ? subnex : nexleft;
629 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
630 XFS_BB_TO_FSB(mp, bmv->bmv_length),
631 map, &nmap, bmapi_flags);
632 if (error)
633 goto out_free_map;
634 ASSERT(nmap <= subnex);
635
636 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
637 out[cur_ext].bmv_oflags = 0;
638 if (map[i].br_state == XFS_EXT_UNWRITTEN)
639 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
640 else if (map[i].br_startblock == DELAYSTARTBLOCK)
641 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
642 out[cur_ext].bmv_offset =
643 XFS_FSB_TO_BB(mp, map[i].br_startoff);
644 out[cur_ext].bmv_length =
645 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
646 out[cur_ext].bmv_unused1 = 0;
647 out[cur_ext].bmv_unused2 = 0;
648
649
650
651
652
653
654
655
656
657
658 if (map[i].br_startblock == DELAYSTARTBLOCK &&
659 map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
660 ASSERT((iflags & BMV_IF_DELALLOC) != 0);
661
662 if (map[i].br_startblock == HOLESTARTBLOCK &&
663 whichfork == XFS_ATTR_FORK) {
664
665 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
666 goto out_free_map;
667 }
668
669 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
670 prealloced, bmvend,
671 map[i].br_startblock))
672 goto out_free_map;
673
674 bmv->bmv_offset =
675 out[cur_ext].bmv_offset +
676 out[cur_ext].bmv_length;
677 bmv->bmv_length =
678 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
679
680
681
682
683
684
685 if ((iflags & BMV_IF_NO_HOLES) &&
686 map[i].br_startblock == HOLESTARTBLOCK) {
687 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
688 continue;
689 }
690
691 nexleft--;
692 bmv->bmv_entries++;
693 cur_ext++;
694 }
695 } while (nmap && nexleft && bmv->bmv_length);
696
697 out_free_map:
698 kmem_free(map);
699 out_unlock_ilock:
700 xfs_iunlock(ip, lock);
701 out_unlock_iolock:
702 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
703
704 for (i = 0; i < cur_ext; i++) {
705 int full = 0;
706
707
708 error = formatter(&arg, &out[i], &full);
709 if (error || full)
710 break;
711 }
712
713 kmem_free(out);
714 return error;
715}
716
717
718
719
720
721
722
723
724
725int
726xfs_bmap_punch_delalloc_range(
727 struct xfs_inode *ip,
728 xfs_fileoff_t start_fsb,
729 xfs_fileoff_t length)
730{
731 xfs_fileoff_t remaining = length;
732 int error = 0;
733
734 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
735
736 do {
737 int done;
738 xfs_bmbt_irec_t imap;
739 int nimaps = 1;
740 xfs_fsblock_t firstblock;
741 xfs_bmap_free_t flist;
742
743
744
745
746
747
748
749 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
750 XFS_BMAPI_ENTIRE);
751
752 if (error) {
753
754 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
755 xfs_alert(ip->i_mount,
756 "Failed delalloc mapping lookup ino %lld fsb %lld.",
757 ip->i_ino, start_fsb);
758 }
759 break;
760 }
761 if (!nimaps) {
762
763 goto next_block;
764 }
765 if (imap.br_startblock != DELAYSTARTBLOCK) {
766
767 goto next_block;
768 }
769 WARN_ON(imap.br_blockcount == 0);
770
771
772
773
774
775
776
777 xfs_bmap_init(&flist, &firstblock);
778 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
779 &flist, &done);
780 if (error)
781 break;
782
783 ASSERT(!flist.xbf_count && !flist.xbf_first);
784next_block:
785 start_fsb++;
786 remaining--;
787 } while(remaining > 0);
788
789 return error;
790}
791
792
793
794
795
796
797bool
798xfs_can_free_eofblocks(struct xfs_inode *ip, bool force)
799{
800
801 if (!S_ISREG(ip->i_d.di_mode))
802 return false;
803
804
805
806
807
808 if (VFS_I(ip)->i_size == 0 &&
809 VFS_I(ip)->i_mapping->nrpages == 0 &&
810 ip->i_delayed_blks == 0)
811 return false;
812
813
814 if (!(ip->i_df.if_flags & XFS_IFEXTENTS))
815 return false;
816
817
818
819
820
821 if (ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC | XFS_DIFLAG_APPEND))
822 if (!force || ip->i_delayed_blks == 0)
823 return false;
824
825 return true;
826}
827
828
829
830
831
832
833int
834xfs_free_eofblocks(
835 xfs_mount_t *mp,
836 xfs_inode_t *ip,
837 bool need_iolock)
838{
839 xfs_trans_t *tp;
840 int error;
841 xfs_fileoff_t end_fsb;
842 xfs_fileoff_t last_fsb;
843 xfs_filblks_t map_len;
844 int nimaps;
845 xfs_bmbt_irec_t imap;
846
847
848
849
850
851 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)XFS_ISIZE(ip));
852 last_fsb = XFS_B_TO_FSB(mp, mp->m_super->s_maxbytes);
853 if (last_fsb <= end_fsb)
854 return 0;
855 map_len = last_fsb - end_fsb;
856
857 nimaps = 1;
858 xfs_ilock(ip, XFS_ILOCK_SHARED);
859 error = xfs_bmapi_read(ip, end_fsb, map_len, &imap, &nimaps, 0);
860 xfs_iunlock(ip, XFS_ILOCK_SHARED);
861
862 if (!error && (nimaps != 0) &&
863 (imap.br_startblock != HOLESTARTBLOCK ||
864 ip->i_delayed_blks)) {
865
866
867
868 error = xfs_qm_dqattach(ip, 0);
869 if (error)
870 return error;
871
872
873
874
875
876
877 tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
878
879 if (need_iolock) {
880 if (!xfs_ilock_nowait(ip, XFS_IOLOCK_EXCL)) {
881 xfs_trans_cancel(tp, 0);
882 return -EAGAIN;
883 }
884 }
885
886 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
887 if (error) {
888 ASSERT(XFS_FORCED_SHUTDOWN(mp));
889 xfs_trans_cancel(tp, 0);
890 if (need_iolock)
891 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
892 return error;
893 }
894
895 xfs_ilock(ip, XFS_ILOCK_EXCL);
896 xfs_trans_ijoin(tp, ip, 0);
897
898
899
900
901
902
903
904 error = xfs_itruncate_extents(&tp, ip, XFS_DATA_FORK,
905 XFS_ISIZE(ip));
906 if (error) {
907
908
909
910
911 xfs_trans_cancel(tp,
912 (XFS_TRANS_RELEASE_LOG_RES |
913 XFS_TRANS_ABORT));
914 } else {
915 error = xfs_trans_commit(tp,
916 XFS_TRANS_RELEASE_LOG_RES);
917 if (!error)
918 xfs_inode_clear_eofblocks_tag(ip);
919 }
920
921 xfs_iunlock(ip, XFS_ILOCK_EXCL);
922 if (need_iolock)
923 xfs_iunlock(ip, XFS_IOLOCK_EXCL);
924 }
925 return error;
926}
927
928int
929xfs_alloc_file_space(
930 struct xfs_inode *ip,
931 xfs_off_t offset,
932 xfs_off_t len,
933 int alloc_type)
934{
935 xfs_mount_t *mp = ip->i_mount;
936 xfs_off_t count;
937 xfs_filblks_t allocated_fsb;
938 xfs_filblks_t allocatesize_fsb;
939 xfs_extlen_t extsz, temp;
940 xfs_fileoff_t startoffset_fsb;
941 xfs_fsblock_t firstfsb;
942 int nimaps;
943 int quota_flag;
944 int rt;
945 xfs_trans_t *tp;
946 xfs_bmbt_irec_t imaps[1], *imapp;
947 xfs_bmap_free_t free_list;
948 uint qblocks, resblks, resrtextents;
949 int committed;
950 int error;
951
952 trace_xfs_alloc_file_space(ip);
953
954 if (XFS_FORCED_SHUTDOWN(mp))
955 return -EIO;
956
957 error = xfs_qm_dqattach(ip, 0);
958 if (error)
959 return error;
960
961 if (len <= 0)
962 return -EINVAL;
963
964 rt = XFS_IS_REALTIME_INODE(ip);
965 extsz = xfs_get_extsz_hint(ip);
966
967 count = len;
968 imapp = &imaps[0];
969 nimaps = 1;
970 startoffset_fsb = XFS_B_TO_FSBT(mp, offset);
971 allocatesize_fsb = XFS_B_TO_FSB(mp, count);
972
973
974
975
976 while (allocatesize_fsb && !error) {
977 xfs_fileoff_t s, e;
978
979
980
981
982 if (unlikely(extsz)) {
983 s = startoffset_fsb;
984 do_div(s, extsz);
985 s *= extsz;
986 e = startoffset_fsb + allocatesize_fsb;
987 if ((temp = do_mod(startoffset_fsb, extsz)))
988 e += temp;
989 if ((temp = do_mod(e, extsz)))
990 e += extsz - temp;
991 } else {
992 s = 0;
993 e = allocatesize_fsb;
994 }
995
996
997
998
999
1000
1001
1002
1003 resblks = min_t(xfs_fileoff_t, (e - s), (MAXEXTLEN * nimaps));
1004 if (unlikely(rt)) {
1005 resrtextents = qblocks = resblks;
1006 resrtextents /= mp->m_sb.sb_rextsize;
1007 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1008 quota_flag = XFS_QMOPT_RES_RTBLKS;
1009 } else {
1010 resrtextents = 0;
1011 resblks = qblocks = XFS_DIOSTRAT_SPACE_RES(mp, resblks);
1012 quota_flag = XFS_QMOPT_RES_REGBLKS;
1013 }
1014
1015
1016
1017
1018 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
1019 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
1020 resblks, resrtextents);
1021
1022
1023
1024 if (error) {
1025
1026
1027
1028 ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1029 xfs_trans_cancel(tp, 0);
1030 break;
1031 }
1032 xfs_ilock(ip, XFS_ILOCK_EXCL);
1033 error = xfs_trans_reserve_quota_nblks(tp, ip, qblocks,
1034 0, quota_flag);
1035 if (error)
1036 goto error1;
1037
1038 xfs_trans_ijoin(tp, ip, 0);
1039
1040 xfs_bmap_init(&free_list, &firstfsb);
1041 error = xfs_bmapi_write(tp, ip, startoffset_fsb,
1042 allocatesize_fsb, alloc_type, &firstfsb,
1043 0, imapp, &nimaps, &free_list);
1044 if (error) {
1045 goto error0;
1046 }
1047
1048
1049
1050
1051 error = xfs_bmap_finish(&tp, &free_list, &committed);
1052 if (error) {
1053 goto error0;
1054 }
1055
1056 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1057 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1058 if (error) {
1059 break;
1060 }
1061
1062 allocated_fsb = imapp->br_blockcount;
1063
1064 if (nimaps == 0) {
1065 error = -ENOSPC;
1066 break;
1067 }
1068
1069 startoffset_fsb += allocated_fsb;
1070 allocatesize_fsb -= allocated_fsb;
1071 }
1072
1073 return error;
1074
1075error0:
1076 xfs_bmap_cancel(&free_list);
1077 xfs_trans_unreserve_quota_nblks(tp, ip, (long)qblocks, 0, quota_flag);
1078
1079error1:
1080 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1081 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1082 return error;
1083}
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096STATIC int
1097xfs_zero_remaining_bytes(
1098 xfs_inode_t *ip,
1099 xfs_off_t startoff,
1100 xfs_off_t endoff)
1101{
1102 xfs_bmbt_irec_t imap;
1103 xfs_fileoff_t offset_fsb;
1104 xfs_off_t lastoffset;
1105 xfs_off_t offset;
1106 xfs_buf_t *bp;
1107 xfs_mount_t *mp = ip->i_mount;
1108 int nimap;
1109 int error = 0;
1110
1111
1112
1113
1114
1115
1116 if (startoff >= XFS_ISIZE(ip))
1117 return 0;
1118
1119 if (endoff > XFS_ISIZE(ip))
1120 endoff = XFS_ISIZE(ip);
1121
1122 for (offset = startoff; offset <= endoff; offset = lastoffset + 1) {
1123 uint lock_mode;
1124
1125 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1126 nimap = 1;
1127
1128 lock_mode = xfs_ilock_data_map_shared(ip);
1129 error = xfs_bmapi_read(ip, offset_fsb, 1, &imap, &nimap, 0);
1130 xfs_iunlock(ip, lock_mode);
1131
1132 if (error || nimap < 1)
1133 break;
1134 ASSERT(imap.br_blockcount >= 1);
1135 ASSERT(imap.br_startoff == offset_fsb);
1136 lastoffset = XFS_FSB_TO_B(mp, imap.br_startoff + 1) - 1;
1137 if (lastoffset > endoff)
1138 lastoffset = endoff;
1139 if (imap.br_startblock == HOLESTARTBLOCK)
1140 continue;
1141 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1142 if (imap.br_state == XFS_EXT_UNWRITTEN)
1143 continue;
1144
1145 error = xfs_buf_read_uncached(XFS_IS_REALTIME_INODE(ip) ?
1146 mp->m_rtdev_targp : mp->m_ddev_targp,
1147 xfs_fsb_to_db(ip, imap.br_startblock),
1148 BTOBB(mp->m_sb.sb_blocksize),
1149 0, &bp, NULL);
1150 if (error)
1151 return error;
1152
1153 memset(bp->b_addr +
1154 (offset - XFS_FSB_TO_B(mp, imap.br_startoff)),
1155 0, lastoffset - offset + 1);
1156
1157 error = xfs_bwrite(bp);
1158 xfs_buf_relse(bp);
1159 if (error)
1160 return error;
1161 }
1162 return error;
1163}
1164
1165int
1166xfs_free_file_space(
1167 struct xfs_inode *ip,
1168 xfs_off_t offset,
1169 xfs_off_t len)
1170{
1171 int committed;
1172 int done;
1173 xfs_fileoff_t endoffset_fsb;
1174 int error;
1175 xfs_fsblock_t firstfsb;
1176 xfs_bmap_free_t free_list;
1177 xfs_bmbt_irec_t imap;
1178 xfs_off_t ioffset;
1179 xfs_off_t iendoffset;
1180 xfs_extlen_t mod=0;
1181 xfs_mount_t *mp;
1182 int nimap;
1183 uint resblks;
1184 xfs_off_t rounding;
1185 int rt;
1186 xfs_fileoff_t startoffset_fsb;
1187 xfs_trans_t *tp;
1188
1189 mp = ip->i_mount;
1190
1191 trace_xfs_free_file_space(ip);
1192
1193 error = xfs_qm_dqattach(ip, 0);
1194 if (error)
1195 return error;
1196
1197 error = 0;
1198 if (len <= 0)
1199 return error;
1200 rt = XFS_IS_REALTIME_INODE(ip);
1201 startoffset_fsb = XFS_B_TO_FSB(mp, offset);
1202 endoffset_fsb = XFS_B_TO_FSBT(mp, offset + len);
1203
1204
1205 inode_dio_wait(VFS_I(ip));
1206
1207 rounding = max_t(xfs_off_t, 1 << mp->m_sb.sb_blocklog, PAGE_CACHE_SIZE);
1208 ioffset = round_down(offset, rounding);
1209 iendoffset = round_up(offset + len, rounding) - 1;
1210 error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping, ioffset,
1211 iendoffset);
1212 if (error)
1213 goto out;
1214 truncate_pagecache_range(VFS_I(ip), ioffset, iendoffset);
1215
1216
1217
1218
1219
1220
1221
1222 if (rt && !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
1223 nimap = 1;
1224 error = xfs_bmapi_read(ip, startoffset_fsb, 1,
1225 &imap, &nimap, 0);
1226 if (error)
1227 goto out;
1228 ASSERT(nimap == 0 || nimap == 1);
1229 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
1230 xfs_daddr_t block;
1231
1232 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1233 block = imap.br_startblock;
1234 mod = do_div(block, mp->m_sb.sb_rextsize);
1235 if (mod)
1236 startoffset_fsb += mp->m_sb.sb_rextsize - mod;
1237 }
1238 nimap = 1;
1239 error = xfs_bmapi_read(ip, endoffset_fsb - 1, 1,
1240 &imap, &nimap, 0);
1241 if (error)
1242 goto out;
1243 ASSERT(nimap == 0 || nimap == 1);
1244 if (nimap && imap.br_startblock != HOLESTARTBLOCK) {
1245 ASSERT(imap.br_startblock != DELAYSTARTBLOCK);
1246 mod++;
1247 if (mod && (mod != mp->m_sb.sb_rextsize))
1248 endoffset_fsb -= mod;
1249 }
1250 }
1251 if ((done = (endoffset_fsb <= startoffset_fsb)))
1252
1253
1254
1255 error = xfs_zero_remaining_bytes(ip, offset, offset + len - 1);
1256 else {
1257
1258
1259
1260 if (offset < XFS_FSB_TO_B(mp, startoffset_fsb))
1261 error = xfs_zero_remaining_bytes(ip, offset,
1262 XFS_FSB_TO_B(mp, startoffset_fsb) - 1);
1263 if (!error &&
1264 XFS_FSB_TO_B(mp, endoffset_fsb) < offset + len)
1265 error = xfs_zero_remaining_bytes(ip,
1266 XFS_FSB_TO_B(mp, endoffset_fsb),
1267 offset + len - 1);
1268 }
1269
1270
1271
1272
1273 resblks = XFS_DIOSTRAT_SPACE_RES(mp, 0);
1274 while (!error && !done) {
1275
1276
1277
1278
1279
1280
1281 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
1282 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write, resblks, 0);
1283
1284
1285
1286
1287 if (error) {
1288
1289
1290
1291 ASSERT(error == -ENOSPC || XFS_FORCED_SHUTDOWN(mp));
1292 xfs_trans_cancel(tp, 0);
1293 break;
1294 }
1295 xfs_ilock(ip, XFS_ILOCK_EXCL);
1296 error = xfs_trans_reserve_quota(tp, mp,
1297 ip->i_udquot, ip->i_gdquot, ip->i_pdquot,
1298 resblks, 0, XFS_QMOPT_RES_REGBLKS);
1299 if (error)
1300 goto error1;
1301
1302 xfs_trans_ijoin(tp, ip, 0);
1303
1304
1305
1306
1307 xfs_bmap_init(&free_list, &firstfsb);
1308 error = xfs_bunmapi(tp, ip, startoffset_fsb,
1309 endoffset_fsb - startoffset_fsb,
1310 0, 2, &firstfsb, &free_list, &done);
1311 if (error) {
1312 goto error0;
1313 }
1314
1315
1316
1317
1318 error = xfs_bmap_finish(&tp, &free_list, &committed);
1319 if (error) {
1320 goto error0;
1321 }
1322
1323 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1324 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1325 }
1326
1327 out:
1328 return error;
1329
1330 error0:
1331 xfs_bmap_cancel(&free_list);
1332 error1:
1333 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1334 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1335 goto out;
1336}
1337
1338
1339
1340
1341
1342int
1343xfs_zero_file_space(
1344 struct xfs_inode *ip,
1345 xfs_off_t offset,
1346 xfs_off_t len)
1347{
1348 struct xfs_mount *mp = ip->i_mount;
1349 uint blksize;
1350 int error;
1351
1352 trace_xfs_zero_file_space(ip);
1353
1354 blksize = 1 << mp->m_sb.sb_blocklog;
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365 error = xfs_free_file_space(ip, offset, len);
1366 if (error)
1367 goto out;
1368
1369 error = xfs_alloc_file_space(ip, round_down(offset, blksize),
1370 round_up(offset + len, blksize) -
1371 round_down(offset, blksize),
1372 XFS_BMAPI_PREALLOC);
1373out:
1374 return error;
1375
1376}
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390int
1391xfs_collapse_file_space(
1392 struct xfs_inode *ip,
1393 xfs_off_t offset,
1394 xfs_off_t len)
1395{
1396 int done = 0;
1397 struct xfs_mount *mp = ip->i_mount;
1398 struct xfs_trans *tp;
1399 int error;
1400 struct xfs_bmap_free free_list;
1401 xfs_fsblock_t first_block;
1402 int committed;
1403 xfs_fileoff_t start_fsb;
1404 xfs_fileoff_t next_fsb;
1405 xfs_fileoff_t shift_fsb;
1406
1407 ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL));
1408
1409 trace_xfs_collapse_file_space(ip);
1410
1411 next_fsb = XFS_B_TO_FSB(mp, offset + len);
1412 shift_fsb = XFS_B_TO_FSB(mp, len);
1413
1414 error = xfs_free_file_space(ip, offset, len);
1415 if (error)
1416 return error;
1417
1418
1419
1420
1421
1422 if (xfs_can_free_eofblocks(ip, true)) {
1423 error = xfs_free_eofblocks(mp, ip, false);
1424 if (error)
1425 return error;
1426 }
1427
1428
1429
1430
1431
1432
1433
1434
1435 error = filemap_write_and_wait_range(VFS_I(ip)->i_mapping,
1436 offset + len, -1);
1437 if (error)
1438 return error;
1439 error = invalidate_inode_pages2_range(VFS_I(ip)->i_mapping,
1440 (offset + len) >> PAGE_CACHE_SHIFT, -1);
1441 if (error)
1442 return error;
1443
1444 while (!error && !done) {
1445 tp = xfs_trans_alloc(mp, XFS_TRANS_DIOSTRAT);
1446
1447
1448
1449
1450
1451
1452 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_write,
1453 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0);
1454 if (error) {
1455 xfs_trans_cancel(tp, 0);
1456 break;
1457 }
1458
1459 xfs_ilock(ip, XFS_ILOCK_EXCL);
1460 error = xfs_trans_reserve_quota(tp, mp, ip->i_udquot,
1461 ip->i_gdquot, ip->i_pdquot,
1462 XFS_DIOSTRAT_SPACE_RES(mp, 0), 0,
1463 XFS_QMOPT_RES_REGBLKS);
1464 if (error)
1465 goto out;
1466
1467 xfs_trans_ijoin(tp, ip, 0);
1468
1469 xfs_bmap_init(&free_list, &first_block);
1470
1471
1472
1473
1474
1475 start_fsb = next_fsb;
1476 error = xfs_bmap_shift_extents(tp, ip, start_fsb, shift_fsb,
1477 &done, &next_fsb, &first_block, &free_list,
1478 XFS_BMAP_MAX_SHIFT_EXTENTS);
1479 if (error)
1480 goto out;
1481
1482 error = xfs_bmap_finish(&tp, &free_list, &committed);
1483 if (error)
1484 goto out;
1485
1486 error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
1487 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1488 }
1489
1490 return error;
1491
1492out:
1493 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES | XFS_TRANS_ABORT);
1494 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1495 return error;
1496}
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518static int
1519xfs_swap_extents_check_format(
1520 xfs_inode_t *ip,
1521 xfs_inode_t *tip)
1522{
1523
1524
1525 if (ip->i_d.di_format == XFS_DINODE_FMT_LOCAL ||
1526 tip->i_d.di_format == XFS_DINODE_FMT_LOCAL)
1527 return -EINVAL;
1528
1529
1530
1531
1532
1533 if (ip->i_d.di_nextents < tip->i_d.di_nextents)
1534 return -EINVAL;
1535
1536
1537
1538
1539
1540
1541 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1542 tip->i_d.di_format == XFS_DINODE_FMT_BTREE)
1543 return -EINVAL;
1544
1545
1546 if (tip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1547 XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) >
1548 XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
1549 return -EINVAL;
1550
1551
1552 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
1553 XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) >
1554 XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
1555 return -EINVAL;
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566 if (tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
1567 if (XFS_IFORK_BOFF(ip) &&
1568 XFS_BMAP_BMDR_SPACE(tip->i_df.if_broot) > XFS_IFORK_BOFF(ip))
1569 return -EINVAL;
1570 if (XFS_IFORK_NEXTENTS(tip, XFS_DATA_FORK) <=
1571 XFS_IFORK_MAXEXT(ip, XFS_DATA_FORK))
1572 return -EINVAL;
1573 }
1574
1575
1576 if (ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
1577 if (XFS_IFORK_BOFF(tip) &&
1578 XFS_BMAP_BMDR_SPACE(ip->i_df.if_broot) > XFS_IFORK_BOFF(tip))
1579 return -EINVAL;
1580 if (XFS_IFORK_NEXTENTS(ip, XFS_DATA_FORK) <=
1581 XFS_IFORK_MAXEXT(tip, XFS_DATA_FORK))
1582 return -EINVAL;
1583 }
1584
1585 return 0;
1586}
1587
1588static int
1589xfs_swap_extent_flush(
1590 struct xfs_inode *ip)
1591{
1592 int error;
1593
1594 error = filemap_write_and_wait(VFS_I(ip)->i_mapping);
1595 if (error)
1596 return error;
1597 truncate_pagecache_range(VFS_I(ip), 0, -1);
1598
1599
1600 if (VFS_I(ip)->i_mapping->nrpages)
1601 return -EINVAL;
1602
1603
1604
1605
1606
1607 if (mapping_mapped(VFS_I(ip)->i_mapping))
1608 return -EBUSY;
1609 return 0;
1610}
1611
1612int
1613xfs_swap_extents(
1614 xfs_inode_t *ip,
1615 xfs_inode_t *tip,
1616 xfs_swapext_t *sxp)
1617{
1618 xfs_mount_t *mp = ip->i_mount;
1619 xfs_trans_t *tp;
1620 xfs_bstat_t *sbp = &sxp->sx_stat;
1621 xfs_ifork_t *tempifp, *ifp, *tifp;
1622 int src_log_flags, target_log_flags;
1623 int error = 0;
1624 int aforkblks = 0;
1625 int taforkblks = 0;
1626 __uint64_t tmp;
1627 int lock_flags;
1628
1629 tempifp = kmem_alloc(sizeof(xfs_ifork_t), KM_MAYFAIL);
1630 if (!tempifp) {
1631 error = -ENOMEM;
1632 goto out;
1633 }
1634
1635
1636
1637
1638
1639
1640
1641 lock_flags = XFS_IOLOCK_EXCL;
1642 xfs_lock_two_inodes(ip, tip, XFS_IOLOCK_EXCL);
1643
1644
1645 if ((ip->i_d.di_mode & S_IFMT) != (tip->i_d.di_mode & S_IFMT)) {
1646 error = -EINVAL;
1647 goto out_unlock;
1648 }
1649
1650
1651 if (XFS_IS_REALTIME_INODE(ip) != XFS_IS_REALTIME_INODE(tip)) {
1652 error = -EINVAL;
1653 goto out_unlock;
1654 }
1655
1656 error = xfs_swap_extent_flush(ip);
1657 if (error)
1658 goto out_unlock;
1659 error = xfs_swap_extent_flush(tip);
1660 if (error)
1661 goto out_unlock;
1662
1663 tp = xfs_trans_alloc(mp, XFS_TRANS_SWAPEXT);
1664 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_ichange, 0, 0);
1665 if (error) {
1666 xfs_trans_cancel(tp, 0);
1667 goto out_unlock;
1668 }
1669 xfs_lock_two_inodes(ip, tip, XFS_ILOCK_EXCL);
1670 lock_flags |= XFS_ILOCK_EXCL;
1671
1672
1673 if (sxp->sx_offset != 0 ||
1674 sxp->sx_length != ip->i_d.di_size ||
1675 sxp->sx_length != tip->i_d.di_size) {
1676 error = -EFAULT;
1677 goto out_trans_cancel;
1678 }
1679
1680 trace_xfs_swap_extent_before(ip, 0);
1681 trace_xfs_swap_extent_before(tip, 1);
1682
1683
1684 error = xfs_swap_extents_check_format(ip, tip);
1685 if (error) {
1686 xfs_notice(mp,
1687 "%s: inode 0x%llx format is incompatible for exchanging.",
1688 __func__, ip->i_ino);
1689 goto out_trans_cancel;
1690 }
1691
1692
1693
1694
1695
1696
1697
1698
1699 if ((sbp->bs_ctime.tv_sec != VFS_I(ip)->i_ctime.tv_sec) ||
1700 (sbp->bs_ctime.tv_nsec != VFS_I(ip)->i_ctime.tv_nsec) ||
1701 (sbp->bs_mtime.tv_sec != VFS_I(ip)->i_mtime.tv_sec) ||
1702 (sbp->bs_mtime.tv_nsec != VFS_I(ip)->i_mtime.tv_nsec)) {
1703 error = -EBUSY;
1704 goto out_trans_cancel;
1705 }
1706
1707
1708
1709 if ( ((XFS_IFORK_Q(ip) != 0) && (ip->i_d.di_anextents > 0)) &&
1710 (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
1711 error = xfs_bmap_count_blocks(tp, ip, XFS_ATTR_FORK, &aforkblks);
1712 if (error)
1713 goto out_trans_cancel;
1714 }
1715 if ( ((XFS_IFORK_Q(tip) != 0) && (tip->i_d.di_anextents > 0)) &&
1716 (tip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)) {
1717 error = xfs_bmap_count_blocks(tp, tip, XFS_ATTR_FORK,
1718 &taforkblks);
1719 if (error)
1720 goto out_trans_cancel;
1721 }
1722
1723 xfs_trans_ijoin(tp, ip, lock_flags);
1724 xfs_trans_ijoin(tp, tip, lock_flags);
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739 src_log_flags = XFS_ILOG_CORE;
1740 target_log_flags = XFS_ILOG_CORE;
1741 if (ip->i_d.di_version == 3 &&
1742 ip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
1743 target_log_flags |= XFS_ILOG_DOWNER;
1744 error = xfs_bmbt_change_owner(tp, ip, XFS_DATA_FORK,
1745 tip->i_ino, NULL);
1746 if (error)
1747 goto out_trans_cancel;
1748 }
1749
1750 if (tip->i_d.di_version == 3 &&
1751 tip->i_d.di_format == XFS_DINODE_FMT_BTREE) {
1752 src_log_flags |= XFS_ILOG_DOWNER;
1753 error = xfs_bmbt_change_owner(tp, tip, XFS_DATA_FORK,
1754 ip->i_ino, NULL);
1755 if (error)
1756 goto out_trans_cancel;
1757 }
1758
1759
1760
1761
1762 ifp = &ip->i_df;
1763 tifp = &tip->i_df;
1764 *tempifp = *ifp;
1765 *ifp = *tifp;
1766 *tifp = *tempifp;
1767
1768
1769
1770
1771 tmp = (__uint64_t)ip->i_d.di_nblocks;
1772 ip->i_d.di_nblocks = tip->i_d.di_nblocks - taforkblks + aforkblks;
1773 tip->i_d.di_nblocks = tmp + taforkblks - aforkblks;
1774
1775 tmp = (__uint64_t) ip->i_d.di_nextents;
1776 ip->i_d.di_nextents = tip->i_d.di_nextents;
1777 tip->i_d.di_nextents = tmp;
1778
1779 tmp = (__uint64_t) ip->i_d.di_format;
1780 ip->i_d.di_format = tip->i_d.di_format;
1781 tip->i_d.di_format = tmp;
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792 ASSERT(tip->i_delayed_blks == 0);
1793 tip->i_delayed_blks = ip->i_delayed_blks;
1794 ip->i_delayed_blks = 0;
1795
1796 switch (ip->i_d.di_format) {
1797 case XFS_DINODE_FMT_EXTENTS:
1798
1799
1800
1801
1802 if (ip->i_d.di_nextents <= XFS_INLINE_EXTS) {
1803 ifp->if_u1.if_extents =
1804 ifp->if_u2.if_inline_ext;
1805 }
1806 src_log_flags |= XFS_ILOG_DEXT;
1807 break;
1808 case XFS_DINODE_FMT_BTREE:
1809 ASSERT(ip->i_d.di_version < 3 ||
1810 (src_log_flags & XFS_ILOG_DOWNER));
1811 src_log_flags |= XFS_ILOG_DBROOT;
1812 break;
1813 }
1814
1815 switch (tip->i_d.di_format) {
1816 case XFS_DINODE_FMT_EXTENTS:
1817
1818
1819
1820
1821 if (tip->i_d.di_nextents <= XFS_INLINE_EXTS) {
1822 tifp->if_u1.if_extents =
1823 tifp->if_u2.if_inline_ext;
1824 }
1825 target_log_flags |= XFS_ILOG_DEXT;
1826 break;
1827 case XFS_DINODE_FMT_BTREE:
1828 target_log_flags |= XFS_ILOG_DBROOT;
1829 ASSERT(tip->i_d.di_version < 3 ||
1830 (target_log_flags & XFS_ILOG_DOWNER));
1831 break;
1832 }
1833
1834 xfs_trans_log_inode(tp, ip, src_log_flags);
1835 xfs_trans_log_inode(tp, tip, target_log_flags);
1836
1837
1838
1839
1840
1841 if (mp->m_flags & XFS_MOUNT_WSYNC)
1842 xfs_trans_set_sync(tp);
1843
1844 error = xfs_trans_commit(tp, 0);
1845
1846 trace_xfs_swap_extent_after(ip, 0);
1847 trace_xfs_swap_extent_after(tip, 1);
1848out:
1849 kmem_free(tempifp);
1850 return error;
1851
1852out_unlock:
1853 xfs_iunlock(ip, lock_flags);
1854 xfs_iunlock(tip, lock_flags);
1855 goto out;
1856
1857out_trans_cancel:
1858 xfs_trans_cancel(tp, 0);
1859 goto out_unlock;
1860}
1861