1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_types.h"
21#include "xfs_bit.h"
22#include "xfs_log.h"
23#include "xfs_inum.h"
24#include "xfs_trans.h"
25#include "xfs_sb.h"
26#include "xfs_ag.h"
27#include "xfs_dir2.h"
28#include "xfs_da_btree.h"
29#include "xfs_bmap_btree.h"
30#include "xfs_alloc_btree.h"
31#include "xfs_ialloc_btree.h"
32#include "xfs_dinode.h"
33#include "xfs_inode.h"
34#include "xfs_btree.h"
35#include "xfs_mount.h"
36#include "xfs_itable.h"
37#include "xfs_inode_item.h"
38#include "xfs_extfree_item.h"
39#include "xfs_alloc.h"
40#include "xfs_bmap.h"
41#include "xfs_rtalloc.h"
42#include "xfs_error.h"
43#include "xfs_attr_leaf.h"
44#include "xfs_quota.h"
45#include "xfs_trans_space.h"
46#include "xfs_buf_item.h"
47#include "xfs_filestream.h"
48#include "xfs_vnodeops.h"
49#include "xfs_trace.h"
50
51
52kmem_zone_t *xfs_bmap_free_item_zone;
53
54
55
56
57
58#ifdef DEBUG
59STATIC void
60xfs_bmap_check_leaf_extents(
61 struct xfs_btree_cur *cur,
62 struct xfs_inode *ip,
63 int whichfork);
64#else
65#define xfs_bmap_check_leaf_extents(cur, ip, whichfork) do { } while (0)
66#endif
67
68
69
70
71
72STATIC int
73xfs_bmap_add_attrfork_extents(
74 xfs_trans_t *tp,
75 xfs_inode_t *ip,
76 xfs_fsblock_t *firstblock,
77 xfs_bmap_free_t *flist,
78 int *flags);
79
80
81
82
83STATIC int
84xfs_bmap_add_attrfork_local(
85 xfs_trans_t *tp,
86 xfs_inode_t *ip,
87 xfs_fsblock_t *firstblock,
88 xfs_bmap_free_t *flist,
89 int *flags);
90
91
92
93
94
95STATIC int
96xfs_bmap_alloc(
97 xfs_bmalloca_t *ap);
98
99
100
101
102
103
104
105STATIC int
106xfs_bmap_btree_to_extents(
107 xfs_trans_t *tp,
108 xfs_inode_t *ip,
109 xfs_btree_cur_t *cur,
110 int *logflagsp,
111 int whichfork);
112
113
114
115
116
117STATIC void
118xfs_bmap_del_free(
119 xfs_bmap_free_t *flist,
120 xfs_bmap_free_item_t *prev,
121 xfs_bmap_free_item_t *free);
122
123
124
125
126
127STATIC int
128xfs_bmap_extents_to_btree(
129 xfs_trans_t *tp,
130 xfs_inode_t *ip,
131 xfs_fsblock_t *firstblock,
132 xfs_bmap_free_t *flist,
133 xfs_btree_cur_t **curp,
134 int wasdel,
135 int *logflagsp,
136 int whichfork);
137
138
139
140
141
142
143STATIC int
144xfs_bmap_local_to_extents(
145 xfs_trans_t *tp,
146 xfs_inode_t *ip,
147 xfs_fsblock_t *firstblock,
148 xfs_extlen_t total,
149 int *logflagsp,
150 int whichfork,
151 void (*init_fn)(struct xfs_buf *bp,
152 struct xfs_inode *ip,
153 struct xfs_ifork *ifp));
154
155
156
157
158
159
160
161
162STATIC xfs_bmbt_rec_host_t *
163xfs_bmap_search_extents(
164 xfs_inode_t *ip,
165 xfs_fileoff_t bno,
166 int whichfork,
167 int *eofp,
168 xfs_extnum_t *lastxp,
169 xfs_bmbt_irec_t *gotp,
170 xfs_bmbt_irec_t *prevp);
171
172
173
174
175
176STATIC xfs_filblks_t
177xfs_bmap_worst_indlen(
178 xfs_inode_t *ip,
179 xfs_filblks_t len);
180
181#ifdef DEBUG
182
183
184
185
186STATIC void
187xfs_bmap_validate_ret(
188 xfs_fileoff_t bno,
189 xfs_filblks_t len,
190 int flags,
191 xfs_bmbt_irec_t *mval,
192 int nmap,
193 int ret_nmap);
194#else
195#define xfs_bmap_validate_ret(bno,len,flags,mval,onmap,nmap)
196#endif
197
198STATIC int
199xfs_bmap_count_tree(
200 xfs_mount_t *mp,
201 xfs_trans_t *tp,
202 xfs_ifork_t *ifp,
203 xfs_fsblock_t blockno,
204 int levelin,
205 int *count);
206
207STATIC void
208xfs_bmap_count_leaves(
209 xfs_ifork_t *ifp,
210 xfs_extnum_t idx,
211 int numrecs,
212 int *count);
213
214STATIC void
215xfs_bmap_disk_count_leaves(
216 struct xfs_mount *mp,
217 struct xfs_btree_block *block,
218 int numrecs,
219 int *count);
220
221
222
223
224
225STATIC int
226xfs_bmbt_lookup_eq(
227 struct xfs_btree_cur *cur,
228 xfs_fileoff_t off,
229 xfs_fsblock_t bno,
230 xfs_filblks_t len,
231 int *stat)
232{
233 cur->bc_rec.b.br_startoff = off;
234 cur->bc_rec.b.br_startblock = bno;
235 cur->bc_rec.b.br_blockcount = len;
236 return xfs_btree_lookup(cur, XFS_LOOKUP_EQ, stat);
237}
238
239STATIC int
240xfs_bmbt_lookup_ge(
241 struct xfs_btree_cur *cur,
242 xfs_fileoff_t off,
243 xfs_fsblock_t bno,
244 xfs_filblks_t len,
245 int *stat)
246{
247 cur->bc_rec.b.br_startoff = off;
248 cur->bc_rec.b.br_startblock = bno;
249 cur->bc_rec.b.br_blockcount = len;
250 return xfs_btree_lookup(cur, XFS_LOOKUP_GE, stat);
251}
252
253
254
255
256static inline bool xfs_bmap_needs_btree(struct xfs_inode *ip, int whichfork)
257{
258 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
259 XFS_IFORK_NEXTENTS(ip, whichfork) >
260 XFS_IFORK_MAXEXT(ip, whichfork);
261}
262
263
264
265
266static inline bool xfs_bmap_wants_extents(struct xfs_inode *ip, int whichfork)
267{
268 return XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
269 XFS_IFORK_NEXTENTS(ip, whichfork) <=
270 XFS_IFORK_MAXEXT(ip, whichfork);
271}
272
273
274
275
276
277
278STATIC int
279xfs_bmbt_update(
280 struct xfs_btree_cur *cur,
281 xfs_fileoff_t off,
282 xfs_fsblock_t bno,
283 xfs_filblks_t len,
284 xfs_exntst_t state)
285{
286 union xfs_btree_rec rec;
287
288 xfs_bmbt_disk_set_allf(&rec.bmbt, off, bno, len, state);
289 return xfs_btree_update(cur, &rec);
290}
291
292
293
294
295STATIC int
296xfs_bmap_add_attrfork_btree(
297 xfs_trans_t *tp,
298 xfs_inode_t *ip,
299 xfs_fsblock_t *firstblock,
300 xfs_bmap_free_t *flist,
301 int *flags)
302{
303 xfs_btree_cur_t *cur;
304 int error;
305 xfs_mount_t *mp;
306 int stat;
307
308 mp = ip->i_mount;
309 if (ip->i_df.if_broot_bytes <= XFS_IFORK_DSIZE(ip))
310 *flags |= XFS_ILOG_DBROOT;
311 else {
312 cur = xfs_bmbt_init_cursor(mp, tp, ip, XFS_DATA_FORK);
313 cur->bc_private.b.flist = flist;
314 cur->bc_private.b.firstblock = *firstblock;
315 if ((error = xfs_bmbt_lookup_ge(cur, 0, 0, 0, &stat)))
316 goto error0;
317
318 XFS_WANT_CORRUPTED_GOTO(stat == 1, error0);
319 if ((error = xfs_btree_new_iroot(cur, flags, &stat)))
320 goto error0;
321 if (stat == 0) {
322 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
323 return XFS_ERROR(ENOSPC);
324 }
325 *firstblock = cur->bc_private.b.firstblock;
326 cur->bc_private.b.allocated = 0;
327 xfs_btree_del_cursor(cur, XFS_BTREE_NOERROR);
328 }
329 return 0;
330error0:
331 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
332 return error;
333}
334
335
336
337
338STATIC int
339xfs_bmap_add_attrfork_extents(
340 xfs_trans_t *tp,
341 xfs_inode_t *ip,
342 xfs_fsblock_t *firstblock,
343 xfs_bmap_free_t *flist,
344 int *flags)
345{
346 xfs_btree_cur_t *cur;
347 int error;
348
349 if (ip->i_d.di_nextents * sizeof(xfs_bmbt_rec_t) <= XFS_IFORK_DSIZE(ip))
350 return 0;
351 cur = NULL;
352 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist, &cur, 0,
353 flags, XFS_DATA_FORK);
354 if (cur) {
355 cur->bc_private.b.allocated = 0;
356 xfs_btree_del_cursor(cur,
357 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
358 }
359 return error;
360}
361
362
363
364
365
366
367STATIC void
368xfs_bmap_local_to_extents_init_fn(
369 struct xfs_buf *bp,
370 struct xfs_inode *ip,
371 struct xfs_ifork *ifp)
372{
373 bp->b_ops = &xfs_bmbt_buf_ops;
374 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
375}
376
377STATIC void
378xfs_symlink_local_to_remote(
379 struct xfs_buf *bp,
380 struct xfs_inode *ip,
381 struct xfs_ifork *ifp)
382{
383
384 bp->b_ops = NULL;
385 memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
386}
387
388
389
390
391
392
393
394
395
396
397
398
399
400STATIC int
401xfs_bmap_add_attrfork_local(
402 xfs_trans_t *tp,
403 xfs_inode_t *ip,
404 xfs_fsblock_t *firstblock,
405 xfs_bmap_free_t *flist,
406 int *flags)
407{
408 xfs_da_args_t dargs;
409
410 if (ip->i_df.if_bytes <= XFS_IFORK_DSIZE(ip))
411 return 0;
412
413 if (S_ISDIR(ip->i_d.di_mode)) {
414 memset(&dargs, 0, sizeof(dargs));
415 dargs.dp = ip;
416 dargs.firstblock = firstblock;
417 dargs.flist = flist;
418 dargs.total = ip->i_mount->m_dirblkfsbs;
419 dargs.whichfork = XFS_DATA_FORK;
420 dargs.trans = tp;
421 return xfs_dir2_sf_to_block(&dargs);
422 }
423
424 if (S_ISLNK(ip->i_d.di_mode))
425 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1,
426 flags, XFS_DATA_FORK,
427 xfs_symlink_local_to_remote);
428
429 return xfs_bmap_local_to_extents(tp, ip, firstblock, 1, flags,
430 XFS_DATA_FORK,
431 xfs_bmap_local_to_extents_init_fn);
432}
433
434
435
436
437STATIC int
438xfs_bmap_add_extent_delay_real(
439 struct xfs_bmalloca *bma)
440{
441 struct xfs_bmbt_irec *new = &bma->got;
442 int diff;
443 xfs_bmbt_rec_host_t *ep;
444 int error;
445 int i;
446 xfs_ifork_t *ifp;
447 xfs_fileoff_t new_endoff;
448 xfs_bmbt_irec_t r[3];
449
450 int rval=0;
451 int state = 0;
452 xfs_filblks_t da_new;
453 xfs_filblks_t da_old;
454 xfs_filblks_t temp=0;
455 xfs_filblks_t temp2=0;
456 int tmp_rval;
457
458 ifp = XFS_IFORK_PTR(bma->ip, XFS_DATA_FORK);
459
460 ASSERT(bma->idx >= 0);
461 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
462 ASSERT(!isnullstartblock(new->br_startblock));
463 ASSERT(!bma->cur ||
464 (bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
465
466 XFS_STATS_INC(xs_add_exlist);
467
468#define LEFT r[0]
469#define RIGHT r[1]
470#define PREV r[2]
471
472
473
474
475 ep = xfs_iext_get_ext(ifp, bma->idx);
476 xfs_bmbt_get_all(ep, &PREV);
477 new_endoff = new->br_startoff + new->br_blockcount;
478 ASSERT(PREV.br_startoff <= new->br_startoff);
479 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
480
481 da_old = startblockval(PREV.br_startblock);
482 da_new = 0;
483
484
485
486
487
488 if (PREV.br_startoff == new->br_startoff)
489 state |= BMAP_LEFT_FILLING;
490 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
491 state |= BMAP_RIGHT_FILLING;
492
493
494
495
496
497 if (bma->idx > 0) {
498 state |= BMAP_LEFT_VALID;
499 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &LEFT);
500
501 if (isnullstartblock(LEFT.br_startblock))
502 state |= BMAP_LEFT_DELAY;
503 }
504
505 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
506 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
507 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
508 LEFT.br_state == new->br_state &&
509 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
510 state |= BMAP_LEFT_CONTIG;
511
512
513
514
515
516
517 if (bma->idx < bma->ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
518 state |= BMAP_RIGHT_VALID;
519 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx + 1), &RIGHT);
520
521 if (isnullstartblock(RIGHT.br_startblock))
522 state |= BMAP_RIGHT_DELAY;
523 }
524
525 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
526 new_endoff == RIGHT.br_startoff &&
527 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
528 new->br_state == RIGHT.br_state &&
529 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
530 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
531 BMAP_RIGHT_FILLING)) !=
532 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
533 BMAP_RIGHT_FILLING) ||
534 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
535 <= MAXEXTLEN))
536 state |= BMAP_RIGHT_CONTIG;
537
538 error = 0;
539
540
541
542 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
543 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
544 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
545 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
546
547
548
549
550 bma->idx--;
551 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
552 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
553 LEFT.br_blockcount + PREV.br_blockcount +
554 RIGHT.br_blockcount);
555 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
556
557 xfs_iext_remove(bma->ip, bma->idx + 1, 2, state);
558 bma->ip->i_d.di_nextents--;
559 if (bma->cur == NULL)
560 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
561 else {
562 rval = XFS_ILOG_CORE;
563 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
564 RIGHT.br_startblock,
565 RIGHT.br_blockcount, &i);
566 if (error)
567 goto done;
568 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
569 error = xfs_btree_delete(bma->cur, &i);
570 if (error)
571 goto done;
572 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
573 error = xfs_btree_decrement(bma->cur, 0, &i);
574 if (error)
575 goto done;
576 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
577 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
578 LEFT.br_startblock,
579 LEFT.br_blockcount +
580 PREV.br_blockcount +
581 RIGHT.br_blockcount, LEFT.br_state);
582 if (error)
583 goto done;
584 }
585 break;
586
587 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
588
589
590
591
592 bma->idx--;
593
594 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
595 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
596 LEFT.br_blockcount + PREV.br_blockcount);
597 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
598
599 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
600 if (bma->cur == NULL)
601 rval = XFS_ILOG_DEXT;
602 else {
603 rval = 0;
604 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
605 LEFT.br_startblock, LEFT.br_blockcount,
606 &i);
607 if (error)
608 goto done;
609 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
610 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
611 LEFT.br_startblock,
612 LEFT.br_blockcount +
613 PREV.br_blockcount, LEFT.br_state);
614 if (error)
615 goto done;
616 }
617 break;
618
619 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
620
621
622
623
624 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
625 xfs_bmbt_set_startblock(ep, new->br_startblock);
626 xfs_bmbt_set_blockcount(ep,
627 PREV.br_blockcount + RIGHT.br_blockcount);
628 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
629
630 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
631 if (bma->cur == NULL)
632 rval = XFS_ILOG_DEXT;
633 else {
634 rval = 0;
635 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
636 RIGHT.br_startblock,
637 RIGHT.br_blockcount, &i);
638 if (error)
639 goto done;
640 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
641 error = xfs_bmbt_update(bma->cur, PREV.br_startoff,
642 new->br_startblock,
643 PREV.br_blockcount +
644 RIGHT.br_blockcount, PREV.br_state);
645 if (error)
646 goto done;
647 }
648 break;
649
650 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
651
652
653
654
655
656 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
657 xfs_bmbt_set_startblock(ep, new->br_startblock);
658 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
659
660 bma->ip->i_d.di_nextents++;
661 if (bma->cur == NULL)
662 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
663 else {
664 rval = XFS_ILOG_CORE;
665 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
666 new->br_startblock, new->br_blockcount,
667 &i);
668 if (error)
669 goto done;
670 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
671 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
672 error = xfs_btree_insert(bma->cur, &i);
673 if (error)
674 goto done;
675 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
676 }
677 break;
678
679 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
680
681
682
683
684 trace_xfs_bmap_pre_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
685 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx - 1),
686 LEFT.br_blockcount + new->br_blockcount);
687 xfs_bmbt_set_startoff(ep,
688 PREV.br_startoff + new->br_blockcount);
689 trace_xfs_bmap_post_update(bma->ip, bma->idx - 1, state, _THIS_IP_);
690
691 temp = PREV.br_blockcount - new->br_blockcount;
692 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
693 xfs_bmbt_set_blockcount(ep, temp);
694 if (bma->cur == NULL)
695 rval = XFS_ILOG_DEXT;
696 else {
697 rval = 0;
698 error = xfs_bmbt_lookup_eq(bma->cur, LEFT.br_startoff,
699 LEFT.br_startblock, LEFT.br_blockcount,
700 &i);
701 if (error)
702 goto done;
703 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
704 error = xfs_bmbt_update(bma->cur, LEFT.br_startoff,
705 LEFT.br_startblock,
706 LEFT.br_blockcount +
707 new->br_blockcount,
708 LEFT.br_state);
709 if (error)
710 goto done;
711 }
712 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
713 startblockval(PREV.br_startblock));
714 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
715 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
716
717 bma->idx--;
718 break;
719
720 case BMAP_LEFT_FILLING:
721
722
723
724
725 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
726 xfs_bmbt_set_startoff(ep, new_endoff);
727 temp = PREV.br_blockcount - new->br_blockcount;
728 xfs_bmbt_set_blockcount(ep, temp);
729 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
730 bma->ip->i_d.di_nextents++;
731 if (bma->cur == NULL)
732 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
733 else {
734 rval = XFS_ILOG_CORE;
735 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
736 new->br_startblock, new->br_blockcount,
737 &i);
738 if (error)
739 goto done;
740 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
741 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
742 error = xfs_btree_insert(bma->cur, &i);
743 if (error)
744 goto done;
745 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
746 }
747
748 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
749 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
750 bma->firstblock, bma->flist,
751 &bma->cur, 1, &tmp_rval, XFS_DATA_FORK);
752 rval |= tmp_rval;
753 if (error)
754 goto done;
755 }
756 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
757 startblockval(PREV.br_startblock) -
758 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
759 ep = xfs_iext_get_ext(ifp, bma->idx + 1);
760 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
761 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
762 break;
763
764 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
765
766
767
768
769 temp = PREV.br_blockcount - new->br_blockcount;
770 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
771 xfs_bmbt_set_blockcount(ep, temp);
772 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx + 1),
773 new->br_startoff, new->br_startblock,
774 new->br_blockcount + RIGHT.br_blockcount,
775 RIGHT.br_state);
776 trace_xfs_bmap_post_update(bma->ip, bma->idx + 1, state, _THIS_IP_);
777 if (bma->cur == NULL)
778 rval = XFS_ILOG_DEXT;
779 else {
780 rval = 0;
781 error = xfs_bmbt_lookup_eq(bma->cur, RIGHT.br_startoff,
782 RIGHT.br_startblock,
783 RIGHT.br_blockcount, &i);
784 if (error)
785 goto done;
786 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
787 error = xfs_bmbt_update(bma->cur, new->br_startoff,
788 new->br_startblock,
789 new->br_blockcount +
790 RIGHT.br_blockcount,
791 RIGHT.br_state);
792 if (error)
793 goto done;
794 }
795
796 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
797 startblockval(PREV.br_startblock));
798 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
799 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
800 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
801
802 bma->idx++;
803 break;
804
805 case BMAP_RIGHT_FILLING:
806
807
808
809
810 temp = PREV.br_blockcount - new->br_blockcount;
811 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
812 xfs_bmbt_set_blockcount(ep, temp);
813 xfs_iext_insert(bma->ip, bma->idx + 1, 1, new, state);
814 bma->ip->i_d.di_nextents++;
815 if (bma->cur == NULL)
816 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
817 else {
818 rval = XFS_ILOG_CORE;
819 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
820 new->br_startblock, new->br_blockcount,
821 &i);
822 if (error)
823 goto done;
824 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
825 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
826 error = xfs_btree_insert(bma->cur, &i);
827 if (error)
828 goto done;
829 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
830 }
831
832 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
833 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
834 bma->firstblock, bma->flist, &bma->cur, 1,
835 &tmp_rval, XFS_DATA_FORK);
836 rval |= tmp_rval;
837 if (error)
838 goto done;
839 }
840 da_new = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(bma->ip, temp),
841 startblockval(PREV.br_startblock) -
842 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
843 ep = xfs_iext_get_ext(ifp, bma->idx);
844 xfs_bmbt_set_startblock(ep, nullstartblock(da_new));
845 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
846
847 bma->idx++;
848 break;
849
850 case 0:
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871 temp = new->br_startoff - PREV.br_startoff;
872 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
873 trace_xfs_bmap_pre_update(bma->ip, bma->idx, 0, _THIS_IP_);
874 xfs_bmbt_set_blockcount(ep, temp);
875 LEFT = *new;
876 RIGHT.br_state = PREV.br_state;
877 RIGHT.br_startblock = nullstartblock(
878 (int)xfs_bmap_worst_indlen(bma->ip, temp2));
879 RIGHT.br_startoff = new_endoff;
880 RIGHT.br_blockcount = temp2;
881
882 xfs_iext_insert(bma->ip, bma->idx + 1, 2, &LEFT, state);
883 bma->ip->i_d.di_nextents++;
884 if (bma->cur == NULL)
885 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
886 else {
887 rval = XFS_ILOG_CORE;
888 error = xfs_bmbt_lookup_eq(bma->cur, new->br_startoff,
889 new->br_startblock, new->br_blockcount,
890 &i);
891 if (error)
892 goto done;
893 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
894 bma->cur->bc_rec.b.br_state = XFS_EXT_NORM;
895 error = xfs_btree_insert(bma->cur, &i);
896 if (error)
897 goto done;
898 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
899 }
900
901 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
902 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
903 bma->firstblock, bma->flist, &bma->cur,
904 1, &tmp_rval, XFS_DATA_FORK);
905 rval |= tmp_rval;
906 if (error)
907 goto done;
908 }
909 temp = xfs_bmap_worst_indlen(bma->ip, temp);
910 temp2 = xfs_bmap_worst_indlen(bma->ip, temp2);
911 diff = (int)(temp + temp2 - startblockval(PREV.br_startblock) -
912 (bma->cur ? bma->cur->bc_private.b.allocated : 0));
913 if (diff > 0) {
914 error = xfs_icsb_modify_counters(bma->ip->i_mount,
915 XFS_SBS_FDBLOCKS,
916 -((int64_t)diff), 0);
917 ASSERT(!error);
918 if (error)
919 goto done;
920 }
921
922 ep = xfs_iext_get_ext(ifp, bma->idx);
923 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
924 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
925 trace_xfs_bmap_pre_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
926 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, bma->idx + 2),
927 nullstartblock((int)temp2));
928 trace_xfs_bmap_post_update(bma->ip, bma->idx + 2, state, _THIS_IP_);
929
930 bma->idx++;
931 da_new = temp + temp2;
932 break;
933
934 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
935 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
936 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
937 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
938 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
939 case BMAP_LEFT_CONTIG:
940 case BMAP_RIGHT_CONTIG:
941
942
943
944 ASSERT(0);
945 }
946
947
948 if (xfs_bmap_needs_btree(bma->ip, XFS_DATA_FORK)) {
949 int tmp_logflags;
950
951 ASSERT(bma->cur == NULL);
952 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
953 bma->firstblock, bma->flist, &bma->cur,
954 da_old > 0, &tmp_logflags, XFS_DATA_FORK);
955 bma->logflags |= tmp_logflags;
956 if (error)
957 goto done;
958 }
959
960
961 if (da_old || da_new) {
962 temp = da_new;
963 if (bma->cur)
964 temp += bma->cur->bc_private.b.allocated;
965 ASSERT(temp <= da_old);
966 if (temp < da_old)
967 xfs_icsb_modify_counters(bma->ip->i_mount,
968 XFS_SBS_FDBLOCKS,
969 (int64_t)(da_old - temp), 0);
970 }
971
972
973 if (bma->cur)
974 bma->cur->bc_private.b.allocated = 0;
975
976 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, XFS_DATA_FORK);
977done:
978 bma->logflags |= rval;
979 return error;
980#undef LEFT
981#undef RIGHT
982#undef PREV
983}
984
985
986
987
988STATIC int
989xfs_bmap_add_extent_unwritten_real(
990 struct xfs_trans *tp,
991 xfs_inode_t *ip,
992 xfs_extnum_t *idx,
993 xfs_btree_cur_t **curp,
994 xfs_bmbt_irec_t *new,
995 xfs_fsblock_t *first,
996 xfs_bmap_free_t *flist,
997 int *logflagsp)
998{
999 xfs_btree_cur_t *cur;
1000 xfs_bmbt_rec_host_t *ep;
1001 int error;
1002 int i;
1003 xfs_ifork_t *ifp;
1004 xfs_fileoff_t new_endoff;
1005 xfs_exntst_t newext;
1006 xfs_exntst_t oldext;
1007 xfs_bmbt_irec_t r[3];
1008
1009 int rval=0;
1010 int state = 0;
1011
1012 *logflagsp = 0;
1013
1014 cur = *curp;
1015 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1016
1017 ASSERT(*idx >= 0);
1018 ASSERT(*idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
1019 ASSERT(!isnullstartblock(new->br_startblock));
1020
1021 XFS_STATS_INC(xs_add_exlist);
1022
1023#define LEFT r[0]
1024#define RIGHT r[1]
1025#define PREV r[2]
1026
1027
1028
1029
1030 error = 0;
1031 ep = xfs_iext_get_ext(ifp, *idx);
1032 xfs_bmbt_get_all(ep, &PREV);
1033 newext = new->br_state;
1034 oldext = (newext == XFS_EXT_UNWRITTEN) ?
1035 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
1036 ASSERT(PREV.br_state == oldext);
1037 new_endoff = new->br_startoff + new->br_blockcount;
1038 ASSERT(PREV.br_startoff <= new->br_startoff);
1039 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
1040
1041
1042
1043
1044
1045 if (PREV.br_startoff == new->br_startoff)
1046 state |= BMAP_LEFT_FILLING;
1047 if (PREV.br_startoff + PREV.br_blockcount == new_endoff)
1048 state |= BMAP_RIGHT_FILLING;
1049
1050
1051
1052
1053
1054 if (*idx > 0) {
1055 state |= BMAP_LEFT_VALID;
1056 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &LEFT);
1057
1058 if (isnullstartblock(LEFT.br_startblock))
1059 state |= BMAP_LEFT_DELAY;
1060 }
1061
1062 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1063 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
1064 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
1065 LEFT.br_state == newext &&
1066 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1067 state |= BMAP_LEFT_CONTIG;
1068
1069
1070
1071
1072
1073
1074 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1) {
1075 state |= BMAP_RIGHT_VALID;
1076 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx + 1), &RIGHT);
1077 if (isnullstartblock(RIGHT.br_startblock))
1078 state |= BMAP_RIGHT_DELAY;
1079 }
1080
1081 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1082 new_endoff == RIGHT.br_startoff &&
1083 new->br_startblock + new->br_blockcount == RIGHT.br_startblock &&
1084 newext == RIGHT.br_state &&
1085 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
1086 ((state & (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1087 BMAP_RIGHT_FILLING)) !=
1088 (BMAP_LEFT_CONTIG | BMAP_LEFT_FILLING |
1089 BMAP_RIGHT_FILLING) ||
1090 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
1091 <= MAXEXTLEN))
1092 state |= BMAP_RIGHT_CONTIG;
1093
1094
1095
1096
1097 switch (state & (BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1098 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG)) {
1099 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG |
1100 BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1101
1102
1103
1104
1105 --*idx;
1106
1107 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1108 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1109 LEFT.br_blockcount + PREV.br_blockcount +
1110 RIGHT.br_blockcount);
1111 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1112
1113 xfs_iext_remove(ip, *idx + 1, 2, state);
1114 ip->i_d.di_nextents -= 2;
1115 if (cur == NULL)
1116 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1117 else {
1118 rval = XFS_ILOG_CORE;
1119 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1120 RIGHT.br_startblock,
1121 RIGHT.br_blockcount, &i)))
1122 goto done;
1123 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1124 if ((error = xfs_btree_delete(cur, &i)))
1125 goto done;
1126 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1127 if ((error = xfs_btree_decrement(cur, 0, &i)))
1128 goto done;
1129 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1130 if ((error = xfs_btree_delete(cur, &i)))
1131 goto done;
1132 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1133 if ((error = xfs_btree_decrement(cur, 0, &i)))
1134 goto done;
1135 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1136 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1137 LEFT.br_startblock,
1138 LEFT.br_blockcount + PREV.br_blockcount +
1139 RIGHT.br_blockcount, LEFT.br_state)))
1140 goto done;
1141 }
1142 break;
1143
1144 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1145
1146
1147
1148
1149 --*idx;
1150
1151 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1152 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx),
1153 LEFT.br_blockcount + PREV.br_blockcount);
1154 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1155
1156 xfs_iext_remove(ip, *idx + 1, 1, state);
1157 ip->i_d.di_nextents--;
1158 if (cur == NULL)
1159 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1160 else {
1161 rval = XFS_ILOG_CORE;
1162 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1163 PREV.br_startblock, PREV.br_blockcount,
1164 &i)))
1165 goto done;
1166 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1167 if ((error = xfs_btree_delete(cur, &i)))
1168 goto done;
1169 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1170 if ((error = xfs_btree_decrement(cur, 0, &i)))
1171 goto done;
1172 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1173 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
1174 LEFT.br_startblock,
1175 LEFT.br_blockcount + PREV.br_blockcount,
1176 LEFT.br_state)))
1177 goto done;
1178 }
1179 break;
1180
1181 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1182
1183
1184
1185
1186 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1187 xfs_bmbt_set_blockcount(ep,
1188 PREV.br_blockcount + RIGHT.br_blockcount);
1189 xfs_bmbt_set_state(ep, newext);
1190 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1191 xfs_iext_remove(ip, *idx + 1, 1, state);
1192 ip->i_d.di_nextents--;
1193 if (cur == NULL)
1194 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1195 else {
1196 rval = XFS_ILOG_CORE;
1197 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
1198 RIGHT.br_startblock,
1199 RIGHT.br_blockcount, &i)))
1200 goto done;
1201 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1202 if ((error = xfs_btree_delete(cur, &i)))
1203 goto done;
1204 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1205 if ((error = xfs_btree_decrement(cur, 0, &i)))
1206 goto done;
1207 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1208 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1209 new->br_startblock,
1210 new->br_blockcount + RIGHT.br_blockcount,
1211 newext)))
1212 goto done;
1213 }
1214 break;
1215
1216 case BMAP_LEFT_FILLING | BMAP_RIGHT_FILLING:
1217
1218
1219
1220
1221
1222 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1223 xfs_bmbt_set_state(ep, newext);
1224 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1225
1226 if (cur == NULL)
1227 rval = XFS_ILOG_DEXT;
1228 else {
1229 rval = 0;
1230 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1231 new->br_startblock, new->br_blockcount,
1232 &i)))
1233 goto done;
1234 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1235 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1236 new->br_startblock, new->br_blockcount,
1237 newext)))
1238 goto done;
1239 }
1240 break;
1241
1242 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG:
1243
1244
1245
1246
1247 trace_xfs_bmap_pre_update(ip, *idx - 1, state, _THIS_IP_);
1248 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx - 1),
1249 LEFT.br_blockcount + new->br_blockcount);
1250 xfs_bmbt_set_startoff(ep,
1251 PREV.br_startoff + new->br_blockcount);
1252 trace_xfs_bmap_post_update(ip, *idx - 1, state, _THIS_IP_);
1253
1254 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1255 xfs_bmbt_set_startblock(ep,
1256 new->br_startblock + new->br_blockcount);
1257 xfs_bmbt_set_blockcount(ep,
1258 PREV.br_blockcount - new->br_blockcount);
1259 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1260
1261 --*idx;
1262
1263 if (cur == NULL)
1264 rval = XFS_ILOG_DEXT;
1265 else {
1266 rval = 0;
1267 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1268 PREV.br_startblock, PREV.br_blockcount,
1269 &i)))
1270 goto done;
1271 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1272 if ((error = xfs_bmbt_update(cur,
1273 PREV.br_startoff + new->br_blockcount,
1274 PREV.br_startblock + new->br_blockcount,
1275 PREV.br_blockcount - new->br_blockcount,
1276 oldext)))
1277 goto done;
1278 if ((error = xfs_btree_decrement(cur, 0, &i)))
1279 goto done;
1280 error = xfs_bmbt_update(cur, LEFT.br_startoff,
1281 LEFT.br_startblock,
1282 LEFT.br_blockcount + new->br_blockcount,
1283 LEFT.br_state);
1284 if (error)
1285 goto done;
1286 }
1287 break;
1288
1289 case BMAP_LEFT_FILLING:
1290
1291
1292
1293
1294 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1295 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1296 xfs_bmbt_set_startoff(ep, new_endoff);
1297 xfs_bmbt_set_blockcount(ep,
1298 PREV.br_blockcount - new->br_blockcount);
1299 xfs_bmbt_set_startblock(ep,
1300 new->br_startblock + new->br_blockcount);
1301 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1302
1303 xfs_iext_insert(ip, *idx, 1, new, state);
1304 ip->i_d.di_nextents++;
1305 if (cur == NULL)
1306 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1307 else {
1308 rval = XFS_ILOG_CORE;
1309 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1310 PREV.br_startblock, PREV.br_blockcount,
1311 &i)))
1312 goto done;
1313 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1314 if ((error = xfs_bmbt_update(cur,
1315 PREV.br_startoff + new->br_blockcount,
1316 PREV.br_startblock + new->br_blockcount,
1317 PREV.br_blockcount - new->br_blockcount,
1318 oldext)))
1319 goto done;
1320 cur->bc_rec.b = *new;
1321 if ((error = xfs_btree_insert(cur, &i)))
1322 goto done;
1323 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1324 }
1325 break;
1326
1327 case BMAP_RIGHT_FILLING | BMAP_RIGHT_CONTIG:
1328
1329
1330
1331
1332 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1333 xfs_bmbt_set_blockcount(ep,
1334 PREV.br_blockcount - new->br_blockcount);
1335 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1336
1337 ++*idx;
1338
1339 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1340 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1341 new->br_startoff, new->br_startblock,
1342 new->br_blockcount + RIGHT.br_blockcount, newext);
1343 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1344
1345 if (cur == NULL)
1346 rval = XFS_ILOG_DEXT;
1347 else {
1348 rval = 0;
1349 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1350 PREV.br_startblock,
1351 PREV.br_blockcount, &i)))
1352 goto done;
1353 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1354 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1355 PREV.br_startblock,
1356 PREV.br_blockcount - new->br_blockcount,
1357 oldext)))
1358 goto done;
1359 if ((error = xfs_btree_increment(cur, 0, &i)))
1360 goto done;
1361 if ((error = xfs_bmbt_update(cur, new->br_startoff,
1362 new->br_startblock,
1363 new->br_blockcount + RIGHT.br_blockcount,
1364 newext)))
1365 goto done;
1366 }
1367 break;
1368
1369 case BMAP_RIGHT_FILLING:
1370
1371
1372
1373
1374 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1375 xfs_bmbt_set_blockcount(ep,
1376 PREV.br_blockcount - new->br_blockcount);
1377 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1378
1379 ++*idx;
1380 xfs_iext_insert(ip, *idx, 1, new, state);
1381
1382 ip->i_d.di_nextents++;
1383 if (cur == NULL)
1384 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1385 else {
1386 rval = XFS_ILOG_CORE;
1387 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1388 PREV.br_startblock, PREV.br_blockcount,
1389 &i)))
1390 goto done;
1391 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1392 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
1393 PREV.br_startblock,
1394 PREV.br_blockcount - new->br_blockcount,
1395 oldext)))
1396 goto done;
1397 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1398 new->br_startblock, new->br_blockcount,
1399 &i)))
1400 goto done;
1401 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1402 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1403 if ((error = xfs_btree_insert(cur, &i)))
1404 goto done;
1405 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1406 }
1407 break;
1408
1409 case 0:
1410
1411
1412
1413
1414
1415 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1416 xfs_bmbt_set_blockcount(ep,
1417 new->br_startoff - PREV.br_startoff);
1418 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1419
1420 r[0] = *new;
1421 r[1].br_startoff = new_endoff;
1422 r[1].br_blockcount =
1423 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1424 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1425 r[1].br_state = oldext;
1426
1427 ++*idx;
1428 xfs_iext_insert(ip, *idx, 2, &r[0], state);
1429
1430 ip->i_d.di_nextents += 2;
1431 if (cur == NULL)
1432 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1433 else {
1434 rval = XFS_ILOG_CORE;
1435 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1436 PREV.br_startblock, PREV.br_blockcount,
1437 &i)))
1438 goto done;
1439 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1440
1441 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
1442 r[1].br_startblock, r[1].br_blockcount,
1443 r[1].br_state)))
1444 goto done;
1445
1446 cur->bc_rec.b = PREV;
1447 cur->bc_rec.b.br_blockcount =
1448 new->br_startoff - PREV.br_startoff;
1449 if ((error = xfs_btree_insert(cur, &i)))
1450 goto done;
1451 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1452
1453
1454
1455
1456
1457 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1458 new->br_startblock, new->br_blockcount,
1459 &i)))
1460 goto done;
1461 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1462
1463 cur->bc_rec.b.br_state = new->br_state;
1464 if ((error = xfs_btree_insert(cur, &i)))
1465 goto done;
1466 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1467 }
1468 break;
1469
1470 case BMAP_LEFT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1471 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1472 case BMAP_LEFT_FILLING | BMAP_RIGHT_CONTIG:
1473 case BMAP_RIGHT_FILLING | BMAP_LEFT_CONTIG:
1474 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1475 case BMAP_LEFT_CONTIG:
1476 case BMAP_RIGHT_CONTIG:
1477
1478
1479
1480 ASSERT(0);
1481 }
1482
1483
1484 if (xfs_bmap_needs_btree(ip, XFS_DATA_FORK)) {
1485 int tmp_logflags;
1486
1487 ASSERT(cur == NULL);
1488 error = xfs_bmap_extents_to_btree(tp, ip, first, flist, &cur,
1489 0, &tmp_logflags, XFS_DATA_FORK);
1490 *logflagsp |= tmp_logflags;
1491 if (error)
1492 goto done;
1493 }
1494
1495
1496 if (cur) {
1497 cur->bc_private.b.allocated = 0;
1498 *curp = cur;
1499 }
1500
1501 xfs_bmap_check_leaf_extents(*curp, ip, XFS_DATA_FORK);
1502done:
1503 *logflagsp |= rval;
1504 return error;
1505#undef LEFT
1506#undef RIGHT
1507#undef PREV
1508}
1509
1510
1511
1512
1513STATIC void
1514xfs_bmap_add_extent_hole_delay(
1515 xfs_inode_t *ip,
1516 xfs_extnum_t *idx,
1517 xfs_bmbt_irec_t *new)
1518{
1519 xfs_ifork_t *ifp;
1520 xfs_bmbt_irec_t left;
1521 xfs_filblks_t newlen=0;
1522 xfs_filblks_t oldlen=0;
1523 xfs_bmbt_irec_t right;
1524 int state;
1525 xfs_filblks_t temp=0;
1526
1527 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
1528 state = 0;
1529 ASSERT(isnullstartblock(new->br_startblock));
1530
1531
1532
1533
1534 if (*idx > 0) {
1535 state |= BMAP_LEFT_VALID;
1536 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx - 1), &left);
1537
1538 if (isnullstartblock(left.br_startblock))
1539 state |= BMAP_LEFT_DELAY;
1540 }
1541
1542
1543
1544
1545
1546 if (*idx < ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1547 state |= BMAP_RIGHT_VALID;
1548 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *idx), &right);
1549
1550 if (isnullstartblock(right.br_startblock))
1551 state |= BMAP_RIGHT_DELAY;
1552 }
1553
1554
1555
1556
1557
1558 if ((state & BMAP_LEFT_VALID) && (state & BMAP_LEFT_DELAY) &&
1559 left.br_startoff + left.br_blockcount == new->br_startoff &&
1560 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1561 state |= BMAP_LEFT_CONTIG;
1562
1563 if ((state & BMAP_RIGHT_VALID) && (state & BMAP_RIGHT_DELAY) &&
1564 new->br_startoff + new->br_blockcount == right.br_startoff &&
1565 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1566 (!(state & BMAP_LEFT_CONTIG) ||
1567 (left.br_blockcount + new->br_blockcount +
1568 right.br_blockcount <= MAXEXTLEN)))
1569 state |= BMAP_RIGHT_CONTIG;
1570
1571
1572
1573
1574 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1575 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1576
1577
1578
1579
1580
1581 --*idx;
1582 temp = left.br_blockcount + new->br_blockcount +
1583 right.br_blockcount;
1584
1585 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1586 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
1587 oldlen = startblockval(left.br_startblock) +
1588 startblockval(new->br_startblock) +
1589 startblockval(right.br_startblock);
1590 newlen = xfs_bmap_worst_indlen(ip, temp);
1591 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
1592 nullstartblock((int)newlen));
1593 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1594
1595 xfs_iext_remove(ip, *idx + 1, 1, state);
1596 break;
1597
1598 case BMAP_LEFT_CONTIG:
1599
1600
1601
1602
1603
1604 --*idx;
1605 temp = left.br_blockcount + new->br_blockcount;
1606
1607 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1608 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, *idx), temp);
1609 oldlen = startblockval(left.br_startblock) +
1610 startblockval(new->br_startblock);
1611 newlen = xfs_bmap_worst_indlen(ip, temp);
1612 xfs_bmbt_set_startblock(xfs_iext_get_ext(ifp, *idx),
1613 nullstartblock((int)newlen));
1614 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1615 break;
1616
1617 case BMAP_RIGHT_CONTIG:
1618
1619
1620
1621
1622
1623 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
1624 temp = new->br_blockcount + right.br_blockcount;
1625 oldlen = startblockval(new->br_startblock) +
1626 startblockval(right.br_startblock);
1627 newlen = xfs_bmap_worst_indlen(ip, temp);
1628 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, *idx),
1629 new->br_startoff,
1630 nullstartblock((int)newlen), temp, right.br_state);
1631 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
1632 break;
1633
1634 case 0:
1635
1636
1637
1638
1639
1640 oldlen = newlen = 0;
1641 xfs_iext_insert(ip, *idx, 1, new, state);
1642 break;
1643 }
1644 if (oldlen != newlen) {
1645 ASSERT(oldlen > newlen);
1646 xfs_icsb_modify_counters(ip->i_mount, XFS_SBS_FDBLOCKS,
1647 (int64_t)(oldlen - newlen), 0);
1648
1649
1650
1651 }
1652}
1653
1654
1655
1656
1657STATIC int
1658xfs_bmap_add_extent_hole_real(
1659 struct xfs_bmalloca *bma,
1660 int whichfork)
1661{
1662 struct xfs_bmbt_irec *new = &bma->got;
1663 int error;
1664 int i;
1665 xfs_ifork_t *ifp;
1666 xfs_bmbt_irec_t left;
1667 xfs_bmbt_irec_t right;
1668 int rval=0;
1669 int state;
1670
1671 ifp = XFS_IFORK_PTR(bma->ip, whichfork);
1672
1673 ASSERT(bma->idx >= 0);
1674 ASSERT(bma->idx <= ifp->if_bytes / sizeof(struct xfs_bmbt_rec));
1675 ASSERT(!isnullstartblock(new->br_startblock));
1676 ASSERT(!bma->cur ||
1677 !(bma->cur->bc_private.b.flags & XFS_BTCUR_BPRV_WASDEL));
1678
1679 XFS_STATS_INC(xs_add_exlist);
1680
1681 state = 0;
1682 if (whichfork == XFS_ATTR_FORK)
1683 state |= BMAP_ATTRFORK;
1684
1685
1686
1687
1688 if (bma->idx > 0) {
1689 state |= BMAP_LEFT_VALID;
1690 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1), &left);
1691 if (isnullstartblock(left.br_startblock))
1692 state |= BMAP_LEFT_DELAY;
1693 }
1694
1695
1696
1697
1698
1699 if (bma->idx < ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)) {
1700 state |= BMAP_RIGHT_VALID;
1701 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &right);
1702 if (isnullstartblock(right.br_startblock))
1703 state |= BMAP_RIGHT_DELAY;
1704 }
1705
1706
1707
1708
1709
1710 if ((state & BMAP_LEFT_VALID) && !(state & BMAP_LEFT_DELAY) &&
1711 left.br_startoff + left.br_blockcount == new->br_startoff &&
1712 left.br_startblock + left.br_blockcount == new->br_startblock &&
1713 left.br_state == new->br_state &&
1714 left.br_blockcount + new->br_blockcount <= MAXEXTLEN)
1715 state |= BMAP_LEFT_CONTIG;
1716
1717 if ((state & BMAP_RIGHT_VALID) && !(state & BMAP_RIGHT_DELAY) &&
1718 new->br_startoff + new->br_blockcount == right.br_startoff &&
1719 new->br_startblock + new->br_blockcount == right.br_startblock &&
1720 new->br_state == right.br_state &&
1721 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1722 (!(state & BMAP_LEFT_CONTIG) ||
1723 left.br_blockcount + new->br_blockcount +
1724 right.br_blockcount <= MAXEXTLEN))
1725 state |= BMAP_RIGHT_CONTIG;
1726
1727 error = 0;
1728
1729
1730
1731 switch (state & (BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG)) {
1732 case BMAP_LEFT_CONTIG | BMAP_RIGHT_CONTIG:
1733
1734
1735
1736
1737
1738 --bma->idx;
1739 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1740 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1741 left.br_blockcount + new->br_blockcount +
1742 right.br_blockcount);
1743 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1744
1745 xfs_iext_remove(bma->ip, bma->idx + 1, 1, state);
1746
1747 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
1748 XFS_IFORK_NEXTENTS(bma->ip, whichfork) - 1);
1749 if (bma->cur == NULL) {
1750 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1751 } else {
1752 rval = XFS_ILOG_CORE;
1753 error = xfs_bmbt_lookup_eq(bma->cur, right.br_startoff,
1754 right.br_startblock, right.br_blockcount,
1755 &i);
1756 if (error)
1757 goto done;
1758 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1759 error = xfs_btree_delete(bma->cur, &i);
1760 if (error)
1761 goto done;
1762 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1763 error = xfs_btree_decrement(bma->cur, 0, &i);
1764 if (error)
1765 goto done;
1766 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1767 error = xfs_bmbt_update(bma->cur, left.br_startoff,
1768 left.br_startblock,
1769 left.br_blockcount +
1770 new->br_blockcount +
1771 right.br_blockcount,
1772 left.br_state);
1773 if (error)
1774 goto done;
1775 }
1776 break;
1777
1778 case BMAP_LEFT_CONTIG:
1779
1780
1781
1782
1783
1784 --bma->idx;
1785 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1786 xfs_bmbt_set_blockcount(xfs_iext_get_ext(ifp, bma->idx),
1787 left.br_blockcount + new->br_blockcount);
1788 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1789
1790 if (bma->cur == NULL) {
1791 rval = xfs_ilog_fext(whichfork);
1792 } else {
1793 rval = 0;
1794 error = xfs_bmbt_lookup_eq(bma->cur, left.br_startoff,
1795 left.br_startblock, left.br_blockcount,
1796 &i);
1797 if (error)
1798 goto done;
1799 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1800 error = xfs_bmbt_update(bma->cur, left.br_startoff,
1801 left.br_startblock,
1802 left.br_blockcount +
1803 new->br_blockcount,
1804 left.br_state);
1805 if (error)
1806 goto done;
1807 }
1808 break;
1809
1810 case BMAP_RIGHT_CONTIG:
1811
1812
1813
1814
1815
1816 trace_xfs_bmap_pre_update(bma->ip, bma->idx, state, _THIS_IP_);
1817 xfs_bmbt_set_allf(xfs_iext_get_ext(ifp, bma->idx),
1818 new->br_startoff, new->br_startblock,
1819 new->br_blockcount + right.br_blockcount,
1820 right.br_state);
1821 trace_xfs_bmap_post_update(bma->ip, bma->idx, state, _THIS_IP_);
1822
1823 if (bma->cur == NULL) {
1824 rval = xfs_ilog_fext(whichfork);
1825 } else {
1826 rval = 0;
1827 error = xfs_bmbt_lookup_eq(bma->cur,
1828 right.br_startoff,
1829 right.br_startblock,
1830 right.br_blockcount, &i);
1831 if (error)
1832 goto done;
1833 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1834 error = xfs_bmbt_update(bma->cur, new->br_startoff,
1835 new->br_startblock,
1836 new->br_blockcount +
1837 right.br_blockcount,
1838 right.br_state);
1839 if (error)
1840 goto done;
1841 }
1842 break;
1843
1844 case 0:
1845
1846
1847
1848
1849
1850 xfs_iext_insert(bma->ip, bma->idx, 1, new, state);
1851 XFS_IFORK_NEXT_SET(bma->ip, whichfork,
1852 XFS_IFORK_NEXTENTS(bma->ip, whichfork) + 1);
1853 if (bma->cur == NULL) {
1854 rval = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
1855 } else {
1856 rval = XFS_ILOG_CORE;
1857 error = xfs_bmbt_lookup_eq(bma->cur,
1858 new->br_startoff,
1859 new->br_startblock,
1860 new->br_blockcount, &i);
1861 if (error)
1862 goto done;
1863 XFS_WANT_CORRUPTED_GOTO(i == 0, done);
1864 bma->cur->bc_rec.b.br_state = new->br_state;
1865 error = xfs_btree_insert(bma->cur, &i);
1866 if (error)
1867 goto done;
1868 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
1869 }
1870 break;
1871 }
1872
1873
1874 if (xfs_bmap_needs_btree(bma->ip, whichfork)) {
1875 int tmp_logflags;
1876
1877 ASSERT(bma->cur == NULL);
1878 error = xfs_bmap_extents_to_btree(bma->tp, bma->ip,
1879 bma->firstblock, bma->flist, &bma->cur,
1880 0, &tmp_logflags, whichfork);
1881 bma->logflags |= tmp_logflags;
1882 if (error)
1883 goto done;
1884 }
1885
1886
1887 if (bma->cur)
1888 bma->cur->bc_private.b.allocated = 0;
1889
1890 xfs_bmap_check_leaf_extents(bma->cur, bma->ip, whichfork);
1891done:
1892 bma->logflags |= rval;
1893 return error;
1894}
1895
1896
1897
1898
1899STATIC int
1900xfs_bmap_extsize_align(
1901 xfs_mount_t *mp,
1902 xfs_bmbt_irec_t *gotp,
1903 xfs_bmbt_irec_t *prevp,
1904 xfs_extlen_t extsz,
1905 int rt,
1906 int eof,
1907 int delay,
1908 int convert,
1909 xfs_fileoff_t *offp,
1910 xfs_extlen_t *lenp)
1911{
1912 xfs_fileoff_t orig_off;
1913 xfs_extlen_t orig_alen;
1914 xfs_fileoff_t orig_end;
1915 xfs_fileoff_t nexto;
1916 xfs_fileoff_t prevo;
1917 xfs_fileoff_t align_off;
1918 xfs_extlen_t align_alen;
1919 xfs_extlen_t temp;
1920
1921 if (convert)
1922 return 0;
1923
1924 orig_off = align_off = *offp;
1925 orig_alen = align_alen = *lenp;
1926 orig_end = orig_off + orig_alen;
1927
1928
1929
1930
1931
1932 if (!delay && !eof &&
1933 (orig_off >= gotp->br_startoff) &&
1934 (orig_end <= gotp->br_startoff + gotp->br_blockcount)) {
1935 return 0;
1936 }
1937
1938
1939
1940
1941
1942
1943
1944
1945 temp = do_mod(orig_off, extsz);
1946 if (temp) {
1947 align_alen += temp;
1948 align_off -= temp;
1949 }
1950
1951
1952
1953 if ((temp = (align_alen % extsz))) {
1954 align_alen += extsz - temp;
1955 }
1956
1957
1958
1959
1960 if (prevp->br_startoff != NULLFILEOFF) {
1961 if (prevp->br_startblock == HOLESTARTBLOCK)
1962 prevo = prevp->br_startoff;
1963 else
1964 prevo = prevp->br_startoff + prevp->br_blockcount;
1965 } else
1966 prevo = 0;
1967 if (align_off != orig_off && align_off < prevo)
1968 align_off = prevo;
1969
1970
1971
1972
1973
1974
1975
1976
1977 if (!eof && gotp->br_startoff != NULLFILEOFF) {
1978 if ((delay && gotp->br_startblock == HOLESTARTBLOCK) ||
1979 (!delay && gotp->br_startblock == DELAYSTARTBLOCK))
1980 nexto = gotp->br_startoff + gotp->br_blockcount;
1981 else
1982 nexto = gotp->br_startoff;
1983 } else
1984 nexto = NULLFILEOFF;
1985 if (!eof &&
1986 align_off + align_alen != orig_end &&
1987 align_off + align_alen > nexto)
1988 align_off = nexto > align_alen ? nexto - align_alen : 0;
1989
1990
1991
1992
1993
1994
1995 if (align_off != orig_off && align_off < prevo)
1996 align_off = prevo;
1997 if (align_off + align_alen != orig_end &&
1998 align_off + align_alen > nexto &&
1999 nexto != NULLFILEOFF) {
2000 ASSERT(nexto > prevo);
2001 align_alen = nexto - align_off;
2002 }
2003
2004
2005
2006
2007
2008 if (rt && (temp = (align_alen % mp->m_sb.sb_rextsize))) {
2009
2010
2011
2012
2013 if (orig_off < align_off ||
2014 orig_end > align_off + align_alen ||
2015 align_alen - temp < orig_alen)
2016 return XFS_ERROR(EINVAL);
2017
2018
2019
2020 if (align_off + temp <= orig_off) {
2021 align_alen -= temp;
2022 align_off += temp;
2023 }
2024
2025
2026
2027 else if (align_off + align_alen - temp >= orig_end)
2028 align_alen -= temp;
2029
2030
2031
2032 else {
2033 align_alen -= orig_off - align_off;
2034 align_off = orig_off;
2035 align_alen -= align_alen % mp->m_sb.sb_rextsize;
2036 }
2037
2038
2039
2040 if (orig_off < align_off || orig_end > align_off + align_alen)
2041 return XFS_ERROR(EINVAL);
2042 } else {
2043 ASSERT(orig_off >= align_off);
2044 ASSERT(orig_end <= align_off + align_alen);
2045 }
2046
2047#ifdef DEBUG
2048 if (!eof && gotp->br_startoff != NULLFILEOFF)
2049 ASSERT(align_off + align_alen <= gotp->br_startoff);
2050 if (prevp->br_startoff != NULLFILEOFF)
2051 ASSERT(align_off >= prevp->br_startoff + prevp->br_blockcount);
2052#endif
2053
2054 *lenp = align_alen;
2055 *offp = align_off;
2056 return 0;
2057}
2058
2059#define XFS_ALLOC_GAP_UNITS 4
2060
2061STATIC void
2062xfs_bmap_adjacent(
2063 xfs_bmalloca_t *ap)
2064{
2065 xfs_fsblock_t adjust;
2066 xfs_agnumber_t fb_agno;
2067 xfs_mount_t *mp;
2068 int nullfb;
2069 int rt;
2070
2071#define ISVALID(x,y) \
2072 (rt ? \
2073 (x) < mp->m_sb.sb_rblocks : \
2074 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
2075 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
2076 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
2077
2078 mp = ap->ip->i_mount;
2079 nullfb = *ap->firstblock == NULLFSBLOCK;
2080 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2081 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
2082
2083
2084
2085
2086 if (ap->eof && ap->prev.br_startoff != NULLFILEOFF &&
2087 !isnullstartblock(ap->prev.br_startblock) &&
2088 ISVALID(ap->prev.br_startblock + ap->prev.br_blockcount,
2089 ap->prev.br_startblock)) {
2090 ap->blkno = ap->prev.br_startblock + ap->prev.br_blockcount;
2091
2092
2093
2094 adjust = ap->offset -
2095 (ap->prev.br_startoff + ap->prev.br_blockcount);
2096 if (adjust &&
2097 ISVALID(ap->blkno + adjust, ap->prev.br_startblock))
2098 ap->blkno += adjust;
2099 }
2100
2101
2102
2103
2104
2105 else if (!ap->eof) {
2106 xfs_fsblock_t gotbno;
2107 xfs_fsblock_t gotdiff=0;
2108 xfs_fsblock_t prevbno;
2109 xfs_fsblock_t prevdiff=0;
2110
2111
2112
2113
2114
2115 if (ap->prev.br_startoff != NULLFILEOFF &&
2116 !isnullstartblock(ap->prev.br_startblock) &&
2117 (prevbno = ap->prev.br_startblock +
2118 ap->prev.br_blockcount) &&
2119 ISVALID(prevbno, ap->prev.br_startblock)) {
2120
2121
2122
2123 adjust = prevdiff = ap->offset -
2124 (ap->prev.br_startoff +
2125 ap->prev.br_blockcount);
2126
2127
2128
2129
2130
2131
2132
2133
2134 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
2135 ISVALID(prevbno + prevdiff,
2136 ap->prev.br_startblock))
2137 prevbno += adjust;
2138 else
2139 prevdiff += adjust;
2140
2141
2142
2143
2144 if (!rt && !nullfb &&
2145 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
2146 prevbno = NULLFSBLOCK;
2147 }
2148
2149
2150
2151 else
2152 prevbno = NULLFSBLOCK;
2153
2154
2155
2156
2157 if (!isnullstartblock(ap->got.br_startblock)) {
2158
2159
2160
2161 adjust = gotdiff = ap->got.br_startoff - ap->offset;
2162
2163
2164
2165
2166 gotbno = ap->got.br_startblock;
2167
2168
2169
2170
2171
2172
2173
2174 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->length &&
2175 ISVALID(gotbno - gotdiff, gotbno))
2176 gotbno -= adjust;
2177 else if (ISVALID(gotbno - ap->length, gotbno)) {
2178 gotbno -= ap->length;
2179 gotdiff += adjust - ap->length;
2180 } else
2181 gotdiff += adjust;
2182
2183
2184
2185
2186 if (!rt && !nullfb &&
2187 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
2188 gotbno = NULLFSBLOCK;
2189 }
2190
2191
2192
2193 else
2194 gotbno = NULLFSBLOCK;
2195
2196
2197
2198
2199 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
2200 ap->blkno = prevdiff <= gotdiff ? prevbno : gotbno;
2201 else if (prevbno != NULLFSBLOCK)
2202 ap->blkno = prevbno;
2203 else if (gotbno != NULLFSBLOCK)
2204 ap->blkno = gotbno;
2205 }
2206#undef ISVALID
2207}
2208
2209STATIC int
2210xfs_bmap_rtalloc(
2211 xfs_bmalloca_t *ap)
2212{
2213 xfs_alloctype_t atype = 0;
2214 int error;
2215 xfs_mount_t *mp;
2216 xfs_extlen_t prod = 0;
2217 xfs_extlen_t ralen = 0;
2218 xfs_extlen_t align;
2219 xfs_rtblock_t rtb;
2220
2221 mp = ap->ip->i_mount;
2222 align = xfs_get_extsz_hint(ap->ip);
2223 prod = align / mp->m_sb.sb_rextsize;
2224 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
2225 align, 1, ap->eof, 0,
2226 ap->conv, &ap->offset, &ap->length);
2227 if (error)
2228 return error;
2229 ASSERT(ap->length);
2230 ASSERT(ap->length % mp->m_sb.sb_rextsize == 0);
2231
2232
2233
2234
2235
2236 if (do_mod(ap->offset, align) || ap->length % align)
2237 prod = 1;
2238
2239
2240
2241 ralen = ap->length / mp->m_sb.sb_rextsize;
2242
2243
2244
2245
2246
2247
2248
2249 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
2250 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
2251
2252
2253
2254
2255 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
2256 xfs_trans_ijoin(ap->tp, mp->m_rbmip, XFS_ILOCK_EXCL);
2257
2258
2259
2260
2261
2262 if (ap->eof && ap->offset == 0) {
2263 xfs_rtblock_t uninitialized_var(rtx);
2264
2265 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
2266 if (error)
2267 return error;
2268 ap->blkno = rtx * mp->m_sb.sb_rextsize;
2269 } else {
2270 ap->blkno = 0;
2271 }
2272
2273 xfs_bmap_adjacent(ap);
2274
2275
2276
2277
2278 atype = ap->blkno == 0 ? XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
2279 do_div(ap->blkno, mp->m_sb.sb_rextsize);
2280 rtb = ap->blkno;
2281 ap->length = ralen;
2282 if ((error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1, ap->length,
2283 &ralen, atype, ap->wasdel, prod, &rtb)))
2284 return error;
2285 if (rtb == NULLFSBLOCK && prod > 1 &&
2286 (error = xfs_rtallocate_extent(ap->tp, ap->blkno, 1,
2287 ap->length, &ralen, atype,
2288 ap->wasdel, 1, &rtb)))
2289 return error;
2290 ap->blkno = rtb;
2291 if (ap->blkno != NULLFSBLOCK) {
2292 ap->blkno *= mp->m_sb.sb_rextsize;
2293 ralen *= mp->m_sb.sb_rextsize;
2294 ap->length = ralen;
2295 ap->ip->i_d.di_nblocks += ralen;
2296 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2297 if (ap->wasdel)
2298 ap->ip->i_delayed_blks -= ralen;
2299
2300
2301
2302
2303 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2304 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2305 XFS_TRANS_DQ_RTBCOUNT, (long) ralen);
2306 } else {
2307 ap->length = 0;
2308 }
2309 return 0;
2310}
2311
2312STATIC int
2313xfs_bmap_btalloc_nullfb(
2314 struct xfs_bmalloca *ap,
2315 struct xfs_alloc_arg *args,
2316 xfs_extlen_t *blen)
2317{
2318 struct xfs_mount *mp = ap->ip->i_mount;
2319 struct xfs_perag *pag;
2320 xfs_agnumber_t ag, startag;
2321 int notinit = 0;
2322 int error;
2323
2324 if (ap->userdata && xfs_inode_is_filestream(ap->ip))
2325 args->type = XFS_ALLOCTYPE_NEAR_BNO;
2326 else
2327 args->type = XFS_ALLOCTYPE_START_BNO;
2328 args->total = ap->total;
2329
2330
2331
2332
2333
2334
2335 startag = ag = XFS_FSB_TO_AGNO(mp, args->fsbno);
2336 if (startag == NULLAGNUMBER)
2337 startag = ag = 0;
2338
2339 pag = xfs_perag_get(mp, ag);
2340 while (*blen < args->maxlen) {
2341 if (!pag->pagf_init) {
2342 error = xfs_alloc_pagf_init(mp, args->tp, ag,
2343 XFS_ALLOC_FLAG_TRYLOCK);
2344 if (error) {
2345 xfs_perag_put(pag);
2346 return error;
2347 }
2348 }
2349
2350
2351
2352
2353 if (pag->pagf_init) {
2354 xfs_extlen_t longest;
2355 longest = xfs_alloc_longest_free_extent(mp, pag);
2356 if (*blen < longest)
2357 *blen = longest;
2358 } else
2359 notinit = 1;
2360
2361 if (xfs_inode_is_filestream(ap->ip)) {
2362 if (*blen >= args->maxlen)
2363 break;
2364
2365 if (ap->userdata) {
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375 if (startag == NULLAGNUMBER)
2376 break;
2377
2378 error = xfs_filestream_new_ag(ap, &ag);
2379 xfs_perag_put(pag);
2380 if (error)
2381 return error;
2382
2383
2384 startag = NULLAGNUMBER;
2385 pag = xfs_perag_get(mp, ag);
2386 continue;
2387 }
2388 }
2389 if (++ag == mp->m_sb.sb_agcount)
2390 ag = 0;
2391 if (ag == startag)
2392 break;
2393 xfs_perag_put(pag);
2394 pag = xfs_perag_get(mp, ag);
2395 }
2396 xfs_perag_put(pag);
2397
2398
2399
2400
2401
2402 if (notinit || *blen < ap->minlen)
2403 args->minlen = ap->minlen;
2404
2405
2406
2407
2408 else if (*blen < args->maxlen)
2409 args->minlen = *blen;
2410
2411
2412
2413
2414 else
2415 args->minlen = args->maxlen;
2416
2417
2418
2419
2420
2421 if (xfs_inode_is_filestream(ap->ip))
2422 ap->blkno = args->fsbno = XFS_AGB_TO_FSB(mp, ag, 0);
2423
2424 return 0;
2425}
2426
2427STATIC int
2428xfs_bmap_btalloc(
2429 xfs_bmalloca_t *ap)
2430{
2431 xfs_mount_t *mp;
2432 xfs_alloctype_t atype = 0;
2433 xfs_extlen_t align;
2434 xfs_agnumber_t fb_agno;
2435 xfs_agnumber_t ag;
2436 xfs_alloc_arg_t args;
2437 xfs_extlen_t blen;
2438 xfs_extlen_t nextminlen = 0;
2439 int nullfb;
2440 int isaligned;
2441 int tryagain;
2442 int error;
2443
2444 ASSERT(ap->length);
2445
2446 mp = ap->ip->i_mount;
2447 align = ap->userdata ? xfs_get_extsz_hint(ap->ip) : 0;
2448 if (unlikely(align)) {
2449 error = xfs_bmap_extsize_align(mp, &ap->got, &ap->prev,
2450 align, 0, ap->eof, 0, ap->conv,
2451 &ap->offset, &ap->length);
2452 ASSERT(!error);
2453 ASSERT(ap->length);
2454 }
2455 nullfb = *ap->firstblock == NULLFSBLOCK;
2456 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, *ap->firstblock);
2457 if (nullfb) {
2458 if (ap->userdata && xfs_inode_is_filestream(ap->ip)) {
2459 ag = xfs_filestream_lookup_ag(ap->ip);
2460 ag = (ag != NULLAGNUMBER) ? ag : 0;
2461 ap->blkno = XFS_AGB_TO_FSB(mp, ag, 0);
2462 } else {
2463 ap->blkno = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
2464 }
2465 } else
2466 ap->blkno = *ap->firstblock;
2467
2468 xfs_bmap_adjacent(ap);
2469
2470
2471
2472
2473
2474 if (nullfb || XFS_FSB_TO_AGNO(mp, ap->blkno) == fb_agno)
2475 ;
2476 else
2477 ap->blkno = *ap->firstblock;
2478
2479
2480
2481 tryagain = isaligned = 0;
2482 memset(&args, 0, sizeof(args));
2483 args.tp = ap->tp;
2484 args.mp = mp;
2485 args.fsbno = ap->blkno;
2486
2487
2488 args.maxlen = MIN(ap->length, XFS_ALLOC_AG_MAX_USABLE(mp));
2489 args.firstblock = *ap->firstblock;
2490 blen = 0;
2491 if (nullfb) {
2492 error = xfs_bmap_btalloc_nullfb(ap, &args, &blen);
2493 if (error)
2494 return error;
2495 } else if (ap->flist->xbf_low) {
2496 if (xfs_inode_is_filestream(ap->ip))
2497 args.type = XFS_ALLOCTYPE_FIRST_AG;
2498 else
2499 args.type = XFS_ALLOCTYPE_START_BNO;
2500 args.total = args.minlen = ap->minlen;
2501 } else {
2502 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2503 args.total = ap->total;
2504 args.minlen = ap->minlen;
2505 }
2506
2507 if (unlikely(align)) {
2508 args.prod = align;
2509 if ((args.mod = (xfs_extlen_t)do_mod(ap->offset, args.prod)))
2510 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2511 } else if (mp->m_sb.sb_blocksize >= PAGE_CACHE_SIZE) {
2512 args.prod = 1;
2513 args.mod = 0;
2514 } else {
2515 args.prod = PAGE_CACHE_SIZE >> mp->m_sb.sb_blocklog;
2516 if ((args.mod = (xfs_extlen_t)(do_mod(ap->offset, args.prod))))
2517 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2518 }
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528 if (!ap->flist->xbf_low && ap->aeof) {
2529 if (!ap->offset) {
2530 args.alignment = mp->m_dalign;
2531 atype = args.type;
2532 isaligned = 1;
2533
2534
2535
2536 if (blen > args.alignment && blen <= args.maxlen)
2537 args.minlen = blen - args.alignment;
2538 args.minalignslop = 0;
2539 } else {
2540
2541
2542
2543
2544
2545 atype = args.type;
2546 tryagain = 1;
2547 args.type = XFS_ALLOCTYPE_THIS_BNO;
2548 args.alignment = 1;
2549
2550
2551
2552
2553
2554
2555 if (blen > mp->m_dalign && blen <= args.maxlen)
2556 nextminlen = blen - mp->m_dalign;
2557 else
2558 nextminlen = args.minlen;
2559 if (nextminlen + mp->m_dalign > args.minlen + 1)
2560 args.minalignslop =
2561 nextminlen + mp->m_dalign -
2562 args.minlen - 1;
2563 else
2564 args.minalignslop = 0;
2565 }
2566 } else {
2567 args.alignment = 1;
2568 args.minalignslop = 0;
2569 }
2570 args.minleft = ap->minleft;
2571 args.wasdel = ap->wasdel;
2572 args.isfl = 0;
2573 args.userdata = ap->userdata;
2574 if ((error = xfs_alloc_vextent(&args)))
2575 return error;
2576 if (tryagain && args.fsbno == NULLFSBLOCK) {
2577
2578
2579
2580
2581 args.type = atype;
2582 args.fsbno = ap->blkno;
2583 args.alignment = mp->m_dalign;
2584 args.minlen = nextminlen;
2585 args.minalignslop = 0;
2586 isaligned = 1;
2587 if ((error = xfs_alloc_vextent(&args)))
2588 return error;
2589 }
2590 if (isaligned && args.fsbno == NULLFSBLOCK) {
2591
2592
2593
2594
2595 args.type = atype;
2596 args.fsbno = ap->blkno;
2597 args.alignment = 0;
2598 if ((error = xfs_alloc_vextent(&args)))
2599 return error;
2600 }
2601 if (args.fsbno == NULLFSBLOCK && nullfb &&
2602 args.minlen > ap->minlen) {
2603 args.minlen = ap->minlen;
2604 args.type = XFS_ALLOCTYPE_START_BNO;
2605 args.fsbno = ap->blkno;
2606 if ((error = xfs_alloc_vextent(&args)))
2607 return error;
2608 }
2609 if (args.fsbno == NULLFSBLOCK && nullfb) {
2610 args.fsbno = 0;
2611 args.type = XFS_ALLOCTYPE_FIRST_AG;
2612 args.total = ap->minlen;
2613 args.minleft = 0;
2614 if ((error = xfs_alloc_vextent(&args)))
2615 return error;
2616 ap->flist->xbf_low = 1;
2617 }
2618 if (args.fsbno != NULLFSBLOCK) {
2619
2620
2621
2622
2623 ASSERT(*ap->firstblock == NULLFSBLOCK ||
2624 XFS_FSB_TO_AGNO(mp, *ap->firstblock) ==
2625 XFS_FSB_TO_AGNO(mp, args.fsbno) ||
2626 (ap->flist->xbf_low &&
2627 XFS_FSB_TO_AGNO(mp, *ap->firstblock) <
2628 XFS_FSB_TO_AGNO(mp, args.fsbno)));
2629
2630 ap->blkno = args.fsbno;
2631 if (*ap->firstblock == NULLFSBLOCK)
2632 *ap->firstblock = args.fsbno;
2633 ASSERT(nullfb || fb_agno == args.agno ||
2634 (ap->flist->xbf_low && fb_agno < args.agno));
2635 ap->length = args.len;
2636 ap->ip->i_d.di_nblocks += args.len;
2637 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2638 if (ap->wasdel)
2639 ap->ip->i_delayed_blks -= args.len;
2640
2641
2642
2643
2644 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2645 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2646 XFS_TRANS_DQ_BCOUNT,
2647 (long) args.len);
2648 } else {
2649 ap->blkno = NULLFSBLOCK;
2650 ap->length = 0;
2651 }
2652 return 0;
2653}
2654
2655
2656
2657
2658
2659STATIC int
2660xfs_bmap_alloc(
2661 xfs_bmalloca_t *ap)
2662{
2663 if (XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata)
2664 return xfs_bmap_rtalloc(ap);
2665 return xfs_bmap_btalloc(ap);
2666}
2667
2668
2669
2670
2671
2672
2673
2674STATIC int
2675xfs_bmap_btree_to_extents(
2676 xfs_trans_t *tp,
2677 xfs_inode_t *ip,
2678 xfs_btree_cur_t *cur,
2679 int *logflagsp,
2680 int whichfork)
2681{
2682
2683 struct xfs_btree_block *cblock;
2684 xfs_fsblock_t cbno;
2685 xfs_buf_t *cbp;
2686 int error;
2687 xfs_ifork_t *ifp;
2688 xfs_mount_t *mp;
2689 __be64 *pp;
2690 struct xfs_btree_block *rblock;
2691
2692 mp = ip->i_mount;
2693 ifp = XFS_IFORK_PTR(ip, whichfork);
2694 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2695 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2696 rblock = ifp->if_broot;
2697 ASSERT(be16_to_cpu(rblock->bb_level) == 1);
2698 ASSERT(be16_to_cpu(rblock->bb_numrecs) == 1);
2699 ASSERT(xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0) == 1);
2700 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, rblock, 1, ifp->if_broot_bytes);
2701 cbno = be64_to_cpu(*pp);
2702 *logflagsp = 0;
2703#ifdef DEBUG
2704 if ((error = xfs_btree_check_lptr(cur, cbno, 1)))
2705 return error;
2706#endif
2707 error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp, XFS_BMAP_BTREE_REF,
2708 &xfs_bmbt_buf_ops);
2709 if (error)
2710 return error;
2711 cblock = XFS_BUF_TO_BLOCK(cbp);
2712 if ((error = xfs_btree_check_block(cur, cblock, 0, cbp)))
2713 return error;
2714 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2715 ip->i_d.di_nblocks--;
2716 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2717 xfs_trans_binval(tp, cbp);
2718 if (cur->bc_bufs[0] == cbp)
2719 cur->bc_bufs[0] = NULL;
2720 xfs_iroot_realloc(ip, -1, whichfork);
2721 ASSERT(ifp->if_broot == NULL);
2722 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2723 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2724 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fext(whichfork);
2725 return 0;
2726}
2727
2728
2729
2730
2731
2732STATIC int
2733xfs_bmap_del_extent(
2734 xfs_inode_t *ip,
2735 xfs_trans_t *tp,
2736 xfs_extnum_t *idx,
2737 xfs_bmap_free_t *flist,
2738 xfs_btree_cur_t *cur,
2739 xfs_bmbt_irec_t *del,
2740 int *logflagsp,
2741 int whichfork)
2742{
2743 xfs_filblks_t da_new;
2744 xfs_filblks_t da_old;
2745 xfs_fsblock_t del_endblock=0;
2746 xfs_fileoff_t del_endoff;
2747 int delay;
2748 int do_fx;
2749 xfs_bmbt_rec_host_t *ep;
2750 int error;
2751 int flags;
2752 xfs_bmbt_irec_t got;
2753 xfs_fileoff_t got_endoff;
2754 int i;
2755 xfs_ifork_t *ifp;
2756 xfs_mount_t *mp;
2757 xfs_filblks_t nblks;
2758 xfs_bmbt_irec_t new;
2759
2760 uint qfield;
2761 xfs_filblks_t temp;
2762 xfs_filblks_t temp2;
2763 int state = 0;
2764
2765 XFS_STATS_INC(xs_del_exlist);
2766
2767 if (whichfork == XFS_ATTR_FORK)
2768 state |= BMAP_ATTRFORK;
2769
2770 mp = ip->i_mount;
2771 ifp = XFS_IFORK_PTR(ip, whichfork);
2772 ASSERT((*idx >= 0) && (*idx < ifp->if_bytes /
2773 (uint)sizeof(xfs_bmbt_rec_t)));
2774 ASSERT(del->br_blockcount > 0);
2775 ep = xfs_iext_get_ext(ifp, *idx);
2776 xfs_bmbt_get_all(ep, &got);
2777 ASSERT(got.br_startoff <= del->br_startoff);
2778 del_endoff = del->br_startoff + del->br_blockcount;
2779 got_endoff = got.br_startoff + got.br_blockcount;
2780 ASSERT(got_endoff >= del_endoff);
2781 delay = isnullstartblock(got.br_startblock);
2782 ASSERT(isnullstartblock(del->br_startblock) == delay);
2783 flags = 0;
2784 qfield = 0;
2785 error = 0;
2786
2787
2788
2789 if (!delay) {
2790 flags = XFS_ILOG_CORE;
2791
2792
2793
2794 if (whichfork == XFS_DATA_FORK && XFS_IS_REALTIME_INODE(ip)) {
2795 xfs_fsblock_t bno;
2796 xfs_filblks_t len;
2797
2798 ASSERT(do_mod(del->br_blockcount,
2799 mp->m_sb.sb_rextsize) == 0);
2800 ASSERT(do_mod(del->br_startblock,
2801 mp->m_sb.sb_rextsize) == 0);
2802 bno = del->br_startblock;
2803 len = del->br_blockcount;
2804 do_div(bno, mp->m_sb.sb_rextsize);
2805 do_div(len, mp->m_sb.sb_rextsize);
2806 error = xfs_rtfree_extent(tp, bno, (xfs_extlen_t)len);
2807 if (error)
2808 goto done;
2809 do_fx = 0;
2810 nblks = len * mp->m_sb.sb_rextsize;
2811 qfield = XFS_TRANS_DQ_RTBCOUNT;
2812 }
2813
2814
2815
2816 else {
2817 do_fx = 1;
2818 nblks = del->br_blockcount;
2819 qfield = XFS_TRANS_DQ_BCOUNT;
2820 }
2821
2822
2823
2824 del_endblock = del->br_startblock + del->br_blockcount;
2825 if (cur) {
2826 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2827 got.br_startblock, got.br_blockcount,
2828 &i)))
2829 goto done;
2830 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2831 }
2832 da_old = da_new = 0;
2833 } else {
2834 da_old = startblockval(got.br_startblock);
2835 da_new = 0;
2836 nblks = 0;
2837 do_fx = 0;
2838 }
2839
2840
2841
2842
2843 switch (((got.br_startoff == del->br_startoff) << 1) |
2844 (got_endoff == del_endoff)) {
2845 case 3:
2846
2847
2848
2849 xfs_iext_remove(ip, *idx, 1,
2850 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0);
2851 --*idx;
2852 if (delay)
2853 break;
2854
2855 XFS_IFORK_NEXT_SET(ip, whichfork,
2856 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2857 flags |= XFS_ILOG_CORE;
2858 if (!cur) {
2859 flags |= xfs_ilog_fext(whichfork);
2860 break;
2861 }
2862 if ((error = xfs_btree_delete(cur, &i)))
2863 goto done;
2864 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2865 break;
2866
2867 case 2:
2868
2869
2870
2871 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2872 xfs_bmbt_set_startoff(ep, del_endoff);
2873 temp = got.br_blockcount - del->br_blockcount;
2874 xfs_bmbt_set_blockcount(ep, temp);
2875 if (delay) {
2876 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2877 da_old);
2878 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2879 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2880 da_new = temp;
2881 break;
2882 }
2883 xfs_bmbt_set_startblock(ep, del_endblock);
2884 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2885 if (!cur) {
2886 flags |= xfs_ilog_fext(whichfork);
2887 break;
2888 }
2889 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2890 got.br_blockcount - del->br_blockcount,
2891 got.br_state)))
2892 goto done;
2893 break;
2894
2895 case 1:
2896
2897
2898
2899 temp = got.br_blockcount - del->br_blockcount;
2900 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2901 xfs_bmbt_set_blockcount(ep, temp);
2902 if (delay) {
2903 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2904 da_old);
2905 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2906 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2907 da_new = temp;
2908 break;
2909 }
2910 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
2911 if (!cur) {
2912 flags |= xfs_ilog_fext(whichfork);
2913 break;
2914 }
2915 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2916 got.br_startblock,
2917 got.br_blockcount - del->br_blockcount,
2918 got.br_state)))
2919 goto done;
2920 break;
2921
2922 case 0:
2923
2924
2925
2926 temp = del->br_startoff - got.br_startoff;
2927 trace_xfs_bmap_pre_update(ip, *idx, state, _THIS_IP_);
2928 xfs_bmbt_set_blockcount(ep, temp);
2929 new.br_startoff = del_endoff;
2930 temp2 = got_endoff - del_endoff;
2931 new.br_blockcount = temp2;
2932 new.br_state = got.br_state;
2933 if (!delay) {
2934 new.br_startblock = del_endblock;
2935 flags |= XFS_ILOG_CORE;
2936 if (cur) {
2937 if ((error = xfs_bmbt_update(cur,
2938 got.br_startoff,
2939 got.br_startblock, temp,
2940 got.br_state)))
2941 goto done;
2942 if ((error = xfs_btree_increment(cur, 0, &i)))
2943 goto done;
2944 cur->bc_rec.b = new;
2945 error = xfs_btree_insert(cur, &i);
2946 if (error && error != ENOSPC)
2947 goto done;
2948
2949
2950
2951
2952
2953
2954 if (error == ENOSPC) {
2955
2956
2957
2958
2959 if ((error = xfs_bmbt_lookup_eq(cur,
2960 got.br_startoff,
2961 got.br_startblock,
2962 temp, &i)))
2963 goto done;
2964 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2965
2966
2967
2968
2969 if ((error = xfs_bmbt_update(cur,
2970 got.br_startoff,
2971 got.br_startblock,
2972 got.br_blockcount,
2973 got.br_state)))
2974 goto done;
2975
2976
2977
2978
2979 xfs_bmbt_set_blockcount(ep,
2980 got.br_blockcount);
2981 flags = 0;
2982 error = XFS_ERROR(ENOSPC);
2983 goto done;
2984 }
2985 XFS_WANT_CORRUPTED_GOTO(i == 1, done);
2986 } else
2987 flags |= xfs_ilog_fext(whichfork);
2988 XFS_IFORK_NEXT_SET(ip, whichfork,
2989 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2990 } else {
2991 ASSERT(whichfork == XFS_DATA_FORK);
2992 temp = xfs_bmap_worst_indlen(ip, temp);
2993 xfs_bmbt_set_startblock(ep, nullstartblock((int)temp));
2994 temp2 = xfs_bmap_worst_indlen(ip, temp2);
2995 new.br_startblock = nullstartblock((int)temp2);
2996 da_new = temp + temp2;
2997 while (da_new > da_old) {
2998 if (temp) {
2999 temp--;
3000 da_new--;
3001 xfs_bmbt_set_startblock(ep,
3002 nullstartblock((int)temp));
3003 }
3004 if (da_new == da_old)
3005 break;
3006 if (temp2) {
3007 temp2--;
3008 da_new--;
3009 new.br_startblock =
3010 nullstartblock((int)temp2);
3011 }
3012 }
3013 }
3014 trace_xfs_bmap_post_update(ip, *idx, state, _THIS_IP_);
3015 xfs_iext_insert(ip, *idx + 1, 1, &new, state);
3016 ++*idx;
3017 break;
3018 }
3019
3020
3021
3022 if (do_fx)
3023 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
3024 mp);
3025
3026
3027
3028 if (nblks)
3029 ip->i_d.di_nblocks -= nblks;
3030
3031
3032
3033 if (qfield)
3034 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
3035
3036
3037
3038
3039
3040 ASSERT(da_old >= da_new);
3041 if (da_old > da_new) {
3042 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
3043 (int64_t)(da_old - da_new), 0);
3044 }
3045done:
3046 *logflagsp = flags;
3047 return error;
3048}
3049
3050
3051
3052
3053
3054STATIC void
3055xfs_bmap_del_free(
3056 xfs_bmap_free_t *flist,
3057 xfs_bmap_free_item_t *prev,
3058 xfs_bmap_free_item_t *free)
3059{
3060 if (prev)
3061 prev->xbfi_next = free->xbfi_next;
3062 else
3063 flist->xbf_first = free->xbfi_next;
3064 flist->xbf_count--;
3065 kmem_zone_free(xfs_bmap_free_item_zone, free);
3066}
3067
3068
3069
3070
3071
3072STATIC int
3073xfs_bmap_extents_to_btree(
3074 xfs_trans_t *tp,
3075 xfs_inode_t *ip,
3076 xfs_fsblock_t *firstblock,
3077 xfs_bmap_free_t *flist,
3078 xfs_btree_cur_t **curp,
3079 int wasdel,
3080 int *logflagsp,
3081 int whichfork)
3082{
3083 struct xfs_btree_block *ablock;
3084 xfs_buf_t *abp;
3085 xfs_alloc_arg_t args;
3086 xfs_bmbt_rec_t *arp;
3087 struct xfs_btree_block *block;
3088 xfs_btree_cur_t *cur;
3089 xfs_bmbt_rec_host_t *ep;
3090 int error;
3091 xfs_extnum_t i, cnt;
3092 xfs_ifork_t *ifp;
3093 xfs_bmbt_key_t *kp;
3094 xfs_mount_t *mp;
3095 xfs_extnum_t nextents;
3096 xfs_bmbt_ptr_t *pp;
3097
3098 ifp = XFS_IFORK_PTR(ip, whichfork);
3099 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
3100
3101
3102
3103
3104 xfs_iroot_realloc(ip, 1, whichfork);
3105 ifp->if_flags |= XFS_IFBROOT;
3106
3107
3108
3109
3110 block = ifp->if_broot;
3111 block->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3112 block->bb_level = cpu_to_be16(1);
3113 block->bb_numrecs = cpu_to_be16(1);
3114 block->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3115 block->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3116
3117
3118
3119
3120 mp = ip->i_mount;
3121 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
3122 cur->bc_private.b.firstblock = *firstblock;
3123 cur->bc_private.b.flist = flist;
3124 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
3125
3126
3127
3128 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
3129 memset(&args, 0, sizeof(args));
3130 args.tp = tp;
3131 args.mp = mp;
3132 args.firstblock = *firstblock;
3133 if (*firstblock == NULLFSBLOCK) {
3134 args.type = XFS_ALLOCTYPE_START_BNO;
3135 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
3136 } else if (flist->xbf_low) {
3137 args.type = XFS_ALLOCTYPE_START_BNO;
3138 args.fsbno = *firstblock;
3139 } else {
3140 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3141 args.fsbno = *firstblock;
3142 }
3143 args.minlen = args.maxlen = args.prod = 1;
3144 args.wasdel = wasdel;
3145 *logflagsp = 0;
3146 if ((error = xfs_alloc_vextent(&args))) {
3147 xfs_iroot_realloc(ip, -1, whichfork);
3148 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
3149 return error;
3150 }
3151
3152
3153
3154 ASSERT(args.fsbno != NULLFSBLOCK);
3155 ASSERT(*firstblock == NULLFSBLOCK ||
3156 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
3157 (flist->xbf_low &&
3158 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
3159 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
3160 cur->bc_private.b.allocated++;
3161 ip->i_d.di_nblocks++;
3162 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
3163 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
3164
3165
3166
3167 abp->b_ops = &xfs_bmbt_buf_ops;
3168 ablock = XFS_BUF_TO_BLOCK(abp);
3169 ablock->bb_magic = cpu_to_be32(XFS_BMAP_MAGIC);
3170 ablock->bb_level = 0;
3171 ablock->bb_u.l.bb_leftsib = cpu_to_be64(NULLDFSBNO);
3172 ablock->bb_u.l.bb_rightsib = cpu_to_be64(NULLDFSBNO);
3173 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3174 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3175 for (cnt = i = 0; i < nextents; i++) {
3176 ep = xfs_iext_get_ext(ifp, i);
3177 if (!isnullstartblock(xfs_bmbt_get_startblock(ep))) {
3178 arp->l0 = cpu_to_be64(ep->l0);
3179 arp->l1 = cpu_to_be64(ep->l1);
3180 arp++; cnt++;
3181 }
3182 }
3183 ASSERT(cnt == XFS_IFORK_NEXTENTS(ip, whichfork));
3184 xfs_btree_set_numrecs(ablock, cnt);
3185
3186
3187
3188
3189 kp = XFS_BMBT_KEY_ADDR(mp, block, 1);
3190 arp = XFS_BMBT_REC_ADDR(mp, ablock, 1);
3191 kp->br_startoff = cpu_to_be64(xfs_bmbt_disk_get_startoff(arp));
3192 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, xfs_bmbt_get_maxrecs(cur,
3193 be16_to_cpu(block->bb_level)));
3194 *pp = cpu_to_be64(args.fsbno);
3195
3196
3197
3198
3199
3200 xfs_btree_log_block(cur, abp, XFS_BB_ALL_BITS);
3201 xfs_btree_log_recs(cur, abp, 1, be16_to_cpu(ablock->bb_numrecs));
3202 ASSERT(*curp == NULL);
3203 *curp = cur;
3204 *logflagsp = XFS_ILOG_CORE | xfs_ilog_fbroot(whichfork);
3205 return 0;
3206}
3207
3208
3209
3210
3211uint
3212xfs_default_attroffset(
3213 struct xfs_inode *ip)
3214{
3215 struct xfs_mount *mp = ip->i_mount;
3216 uint offset;
3217
3218 if (mp->m_sb.sb_inodesize == 256) {
3219 offset = XFS_LITINO(mp) -
3220 XFS_BMDR_SPACE_CALC(MINABTPTRS);
3221 } else {
3222 offset = XFS_BMDR_SPACE_CALC(6 * MINABTPTRS);
3223 }
3224
3225 ASSERT(offset < XFS_LITINO(mp));
3226 return offset;
3227}
3228
3229
3230
3231
3232
3233
3234STATIC void
3235xfs_bmap_forkoff_reset(
3236 xfs_mount_t *mp,
3237 xfs_inode_t *ip,
3238 int whichfork)
3239{
3240 if (whichfork == XFS_ATTR_FORK &&
3241 ip->i_d.di_format != XFS_DINODE_FMT_DEV &&
3242 ip->i_d.di_format != XFS_DINODE_FMT_UUID &&
3243 ip->i_d.di_format != XFS_DINODE_FMT_BTREE) {
3244 uint dfl_forkoff = xfs_default_attroffset(ip) >> 3;
3245
3246 if (dfl_forkoff > ip->i_d.di_forkoff)
3247 ip->i_d.di_forkoff = dfl_forkoff;
3248 }
3249}
3250
3251
3252
3253
3254
3255
3256
3257STATIC int
3258xfs_bmap_local_to_extents(
3259 xfs_trans_t *tp,
3260 xfs_inode_t *ip,
3261 xfs_fsblock_t *firstblock,
3262 xfs_extlen_t total,
3263 int *logflagsp,
3264 int whichfork,
3265 void (*init_fn)(struct xfs_buf *bp,
3266 struct xfs_inode *ip,
3267 struct xfs_ifork *ifp))
3268{
3269 int error;
3270 int flags;
3271 xfs_ifork_t *ifp;
3272
3273
3274
3275
3276
3277 ASSERT(!(S_ISREG(ip->i_d.di_mode) && whichfork == XFS_DATA_FORK));
3278 ifp = XFS_IFORK_PTR(ip, whichfork);
3279 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3280 flags = 0;
3281 error = 0;
3282 if (ifp->if_bytes) {
3283 xfs_alloc_arg_t args;
3284 xfs_buf_t *bp;
3285 xfs_bmbt_rec_host_t *ep;
3286
3287 ASSERT((ifp->if_flags &
3288 (XFS_IFINLINE|XFS_IFEXTENTS|XFS_IFEXTIREC)) == XFS_IFINLINE);
3289 memset(&args, 0, sizeof(args));
3290 args.tp = tp;
3291 args.mp = ip->i_mount;
3292 args.firstblock = *firstblock;
3293
3294
3295
3296
3297 if (*firstblock == NULLFSBLOCK) {
3298 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
3299 args.type = XFS_ALLOCTYPE_START_BNO;
3300 } else {
3301 args.fsbno = *firstblock;
3302 args.type = XFS_ALLOCTYPE_NEAR_BNO;
3303 }
3304 args.total = total;
3305 args.minlen = args.maxlen = args.prod = 1;
3306 error = xfs_alloc_vextent(&args);
3307 if (error)
3308 goto done;
3309
3310
3311 ASSERT(args.fsbno != NULLFSBLOCK);
3312 ASSERT(args.len == 1);
3313 *firstblock = args.fsbno;
3314 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
3315
3316
3317 init_fn(bp, ip, ifp);
3318
3319
3320 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
3321 xfs_bmap_forkoff_reset(args.mp, ip, whichfork);
3322 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
3323 xfs_iext_add(ifp, 0, 1);
3324 ep = xfs_iext_get_ext(ifp, 0);
3325 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
3326 trace_xfs_bmap_post_update(ip, 0,
3327 whichfork == XFS_ATTR_FORK ? BMAP_ATTRFORK : 0,
3328 _THIS_IP_);
3329 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
3330 ip->i_d.di_nblocks = 1;
3331 xfs_trans_mod_dquot_byino(tp, ip,
3332 XFS_TRANS_DQ_BCOUNT, 1L);
3333 flags |= xfs_ilog_fext(whichfork);
3334 } else {
3335 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
3336 xfs_bmap_forkoff_reset(ip->i_mount, ip, whichfork);
3337 }
3338 ifp->if_flags &= ~XFS_IFINLINE;
3339 ifp->if_flags |= XFS_IFEXTENTS;
3340 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
3341 flags |= XFS_ILOG_CORE;
3342done:
3343 *logflagsp = flags;
3344 return error;
3345}
3346
3347
3348
3349
3350
3351
3352
3353
3354STATIC xfs_bmbt_rec_host_t *
3355xfs_bmap_search_multi_extents(
3356 xfs_ifork_t *ifp,
3357 xfs_fileoff_t bno,
3358 int *eofp,
3359 xfs_extnum_t *lastxp,
3360 xfs_bmbt_irec_t *gotp,
3361 xfs_bmbt_irec_t *prevp)
3362{
3363 xfs_bmbt_rec_host_t *ep;
3364 xfs_extnum_t lastx;
3365
3366
3367
3368
3369
3370 gotp->br_startoff = 0xffa5a5a5a5a5a5a5LL;
3371 gotp->br_blockcount = 0xa55a5a5a5a5a5a5aLL;
3372 gotp->br_state = XFS_EXT_INVALID;
3373#if XFS_BIG_BLKNOS
3374 gotp->br_startblock = 0xffffa5a5a5a5a5a5LL;
3375#else
3376 gotp->br_startblock = 0xffffa5a5;
3377#endif
3378 prevp->br_startoff = NULLFILEOFF;
3379
3380 ep = xfs_iext_bno_to_ext(ifp, bno, &lastx);
3381 if (lastx > 0) {
3382 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx - 1), prevp);
3383 }
3384 if (lastx < (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
3385 xfs_bmbt_get_all(ep, gotp);
3386 *eofp = 0;
3387 } else {
3388 if (lastx > 0) {
3389 *gotp = *prevp;
3390 }
3391 *eofp = 1;
3392 ep = NULL;
3393 }
3394 *lastxp = lastx;
3395 return ep;
3396}
3397
3398
3399
3400
3401
3402
3403
3404
3405STATIC xfs_bmbt_rec_host_t *
3406xfs_bmap_search_extents(
3407 xfs_inode_t *ip,
3408 xfs_fileoff_t bno,
3409 int fork,
3410 int *eofp,
3411 xfs_extnum_t *lastxp,
3412 xfs_bmbt_irec_t *gotp,
3413 xfs_bmbt_irec_t *prevp)
3414{
3415 xfs_ifork_t *ifp;
3416 xfs_bmbt_rec_host_t *ep;
3417
3418 XFS_STATS_INC(xs_look_exlist);
3419 ifp = XFS_IFORK_PTR(ip, fork);
3420
3421 ep = xfs_bmap_search_multi_extents(ifp, bno, eofp, lastxp, gotp, prevp);
3422
3423 if (unlikely(!(gotp->br_startblock) && (*lastxp != NULLEXTNUM) &&
3424 !(XFS_IS_REALTIME_INODE(ip) && fork == XFS_DATA_FORK))) {
3425 xfs_alert_tag(ip->i_mount, XFS_PTAG_FSBLOCK_ZERO,
3426 "Access to block zero in inode %llu "
3427 "start_block: %llx start_off: %llx "
3428 "blkcnt: %llx extent-state: %x lastx: %x\n",
3429 (unsigned long long)ip->i_ino,
3430 (unsigned long long)gotp->br_startblock,
3431 (unsigned long long)gotp->br_startoff,
3432 (unsigned long long)gotp->br_blockcount,
3433 gotp->br_state, *lastxp);
3434 *lastxp = NULLEXTNUM;
3435 *eofp = 1;
3436 return NULL;
3437 }
3438 return ep;
3439}
3440
3441
3442
3443
3444
3445STATIC xfs_filblks_t
3446xfs_bmap_worst_indlen(
3447 xfs_inode_t *ip,
3448 xfs_filblks_t len)
3449{
3450 int level;
3451 int maxrecs;
3452 xfs_mount_t *mp;
3453 xfs_filblks_t rval;
3454
3455 mp = ip->i_mount;
3456 maxrecs = mp->m_bmap_dmxr[0];
3457 for (level = 0, rval = 0;
3458 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3459 level++) {
3460 len += maxrecs - 1;
3461 do_div(len, maxrecs);
3462 rval += len;
3463 if (len == 1)
3464 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3465 level - 1;
3466 if (level == 0)
3467 maxrecs = mp->m_bmap_dmxr[1];
3468 }
3469 return rval;
3470}
3471
3472
3473
3474
3475
3476int
3477xfs_bmap_add_attrfork(
3478 xfs_inode_t *ip,
3479 int size,
3480 int rsvd)
3481{
3482 xfs_fsblock_t firstblock;
3483 xfs_bmap_free_t flist;
3484 xfs_mount_t *mp;
3485 xfs_trans_t *tp;
3486 int blks;
3487 int version = 1;
3488 int committed;
3489 int logflags;
3490 int error;
3491
3492 ASSERT(XFS_IFORK_Q(ip) == 0);
3493
3494 mp = ip->i_mount;
3495 ASSERT(!XFS_NOT_DQATTACHED(mp, ip));
3496 tp = xfs_trans_alloc(mp, XFS_TRANS_ADDAFORK);
3497 blks = XFS_ADDAFORK_SPACE_RES(mp);
3498 if (rsvd)
3499 tp->t_flags |= XFS_TRANS_RESERVE;
3500 if ((error = xfs_trans_reserve(tp, blks, XFS_ADDAFORK_LOG_RES(mp), 0,
3501 XFS_TRANS_PERM_LOG_RES, XFS_ADDAFORK_LOG_COUNT)))
3502 goto error0;
3503 xfs_ilock(ip, XFS_ILOCK_EXCL);
3504 error = xfs_trans_reserve_quota_nblks(tp, ip, blks, 0, rsvd ?
3505 XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
3506 XFS_QMOPT_RES_REGBLKS);
3507 if (error) {
3508 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3509 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES);
3510 return error;
3511 }
3512 if (XFS_IFORK_Q(ip))
3513 goto error1;
3514 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS) {
3515
3516
3517
3518 ASSERT(ip->i_d.di_aformat == 0);
3519 ip->i_d.di_aformat = XFS_DINODE_FMT_EXTENTS;
3520 }
3521 ASSERT(ip->i_d.di_anextents == 0);
3522
3523 xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
3524 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
3525
3526 switch (ip->i_d.di_format) {
3527 case XFS_DINODE_FMT_DEV:
3528 ip->i_d.di_forkoff = roundup(sizeof(xfs_dev_t), 8) >> 3;
3529 break;
3530 case XFS_DINODE_FMT_UUID:
3531 ip->i_d.di_forkoff = roundup(sizeof(uuid_t), 8) >> 3;
3532 break;
3533 case XFS_DINODE_FMT_LOCAL:
3534 case XFS_DINODE_FMT_EXTENTS:
3535 case XFS_DINODE_FMT_BTREE:
3536 ip->i_d.di_forkoff = xfs_attr_shortform_bytesfit(ip, size);
3537 if (!ip->i_d.di_forkoff)
3538 ip->i_d.di_forkoff = xfs_default_attroffset(ip) >> 3;
3539 else if (mp->m_flags & XFS_MOUNT_ATTR2)
3540 version = 2;
3541 break;
3542 default:
3543 ASSERT(0);
3544 error = XFS_ERROR(EINVAL);
3545 goto error1;
3546 }
3547
3548 ASSERT(ip->i_afp == NULL);
3549 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP);
3550 ip->i_afp->if_flags = XFS_IFEXTENTS;
3551 logflags = 0;
3552 xfs_bmap_init(&flist, &firstblock);
3553 switch (ip->i_d.di_format) {
3554 case XFS_DINODE_FMT_LOCAL:
3555 error = xfs_bmap_add_attrfork_local(tp, ip, &firstblock, &flist,
3556 &logflags);
3557 break;
3558 case XFS_DINODE_FMT_EXTENTS:
3559 error = xfs_bmap_add_attrfork_extents(tp, ip, &firstblock,
3560 &flist, &logflags);
3561 break;
3562 case XFS_DINODE_FMT_BTREE:
3563 error = xfs_bmap_add_attrfork_btree(tp, ip, &firstblock, &flist,
3564 &logflags);
3565 break;
3566 default:
3567 error = 0;
3568 break;
3569 }
3570 if (logflags)
3571 xfs_trans_log_inode(tp, ip, logflags);
3572 if (error)
3573 goto error2;
3574 if (!xfs_sb_version_hasattr(&mp->m_sb) ||
3575 (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2)) {
3576 __int64_t sbfields = 0;
3577
3578 spin_lock(&mp->m_sb_lock);
3579 if (!xfs_sb_version_hasattr(&mp->m_sb)) {
3580 xfs_sb_version_addattr(&mp->m_sb);
3581 sbfields |= XFS_SB_VERSIONNUM;
3582 }
3583 if (!xfs_sb_version_hasattr2(&mp->m_sb) && version == 2) {
3584 xfs_sb_version_addattr2(&mp->m_sb);
3585 sbfields |= (XFS_SB_VERSIONNUM | XFS_SB_FEATURES2);
3586 }
3587 if (sbfields) {
3588 spin_unlock(&mp->m_sb_lock);
3589 xfs_mod_sb(tp, sbfields);
3590 } else
3591 spin_unlock(&mp->m_sb_lock);
3592 }
3593
3594 error = xfs_bmap_finish(&tp, &flist, &committed);
3595 if (error)
3596 goto error2;
3597 return xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
3598error2:
3599 xfs_bmap_cancel(&flist);
3600error1:
3601 xfs_iunlock(ip, XFS_ILOCK_EXCL);
3602error0:
3603 xfs_trans_cancel(tp, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
3604 return error;
3605}
3606
3607
3608
3609
3610
3611
3612void
3613xfs_bmap_add_free(
3614 xfs_fsblock_t bno,
3615 xfs_filblks_t len,
3616 xfs_bmap_free_t *flist,
3617 xfs_mount_t *mp)
3618{
3619 xfs_bmap_free_item_t *cur;
3620 xfs_bmap_free_item_t *new;
3621 xfs_bmap_free_item_t *prev;
3622#ifdef DEBUG
3623 xfs_agnumber_t agno;
3624 xfs_agblock_t agbno;
3625
3626 ASSERT(bno != NULLFSBLOCK);
3627 ASSERT(len > 0);
3628 ASSERT(len <= MAXEXTLEN);
3629 ASSERT(!isnullstartblock(bno));
3630 agno = XFS_FSB_TO_AGNO(mp, bno);
3631 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3632 ASSERT(agno < mp->m_sb.sb_agcount);
3633 ASSERT(agbno < mp->m_sb.sb_agblocks);
3634 ASSERT(len < mp->m_sb.sb_agblocks);
3635 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3636#endif
3637 ASSERT(xfs_bmap_free_item_zone != NULL);
3638 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3639 new->xbfi_startblock = bno;
3640 new->xbfi_blockcount = (xfs_extlen_t)len;
3641 for (prev = NULL, cur = flist->xbf_first;
3642 cur != NULL;
3643 prev = cur, cur = cur->xbfi_next) {
3644 if (cur->xbfi_startblock >= bno)
3645 break;
3646 }
3647 if (prev)
3648 prev->xbfi_next = new;
3649 else
3650 flist->xbf_first = new;
3651 new->xbfi_next = cur;
3652 flist->xbf_count++;
3653}
3654
3655
3656
3657
3658
3659void
3660xfs_bmap_compute_maxlevels(
3661 xfs_mount_t *mp,
3662 int whichfork)
3663{
3664 int level;
3665 uint maxblocks;
3666 uint maxleafents;
3667 int maxrootrecs;
3668 int minleafrecs;
3669 int minnoderecs;
3670 int sz;
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686 if (whichfork == XFS_DATA_FORK) {
3687 maxleafents = MAXEXTNUM;
3688 sz = XFS_BMDR_SPACE_CALC(MINDBTPTRS);
3689 } else {
3690 maxleafents = MAXAEXTNUM;
3691 sz = XFS_BMDR_SPACE_CALC(MINABTPTRS);
3692 }
3693 maxrootrecs = xfs_bmdr_maxrecs(mp, sz, 0);
3694 minleafrecs = mp->m_bmap_dmnr[0];
3695 minnoderecs = mp->m_bmap_dmnr[1];
3696 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3697 for (level = 1; maxblocks > 1; level++) {
3698 if (maxblocks <= maxrootrecs)
3699 maxblocks = 1;
3700 else
3701 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3702 }
3703 mp->m_bm_maxlevels[whichfork] = level;
3704}
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715int
3716xfs_bmap_finish(
3717 xfs_trans_t **tp,
3718 xfs_bmap_free_t *flist,
3719 int *committed)
3720{
3721 xfs_efd_log_item_t *efd;
3722 xfs_efi_log_item_t *efi;
3723 int error;
3724 xfs_bmap_free_item_t *free;
3725 unsigned int logres;
3726 unsigned int logcount;
3727 xfs_mount_t *mp;
3728 xfs_bmap_free_item_t *next;
3729 xfs_trans_t *ntp;
3730
3731 ASSERT((*tp)->t_flags & XFS_TRANS_PERM_LOG_RES);
3732 if (flist->xbf_count == 0) {
3733 *committed = 0;
3734 return 0;
3735 }
3736 ntp = *tp;
3737 efi = xfs_trans_get_efi(ntp, flist->xbf_count);
3738 for (free = flist->xbf_first; free; free = free->xbfi_next)
3739 xfs_trans_log_efi_extent(ntp, efi, free->xbfi_startblock,
3740 free->xbfi_blockcount);
3741 logres = ntp->t_log_res;
3742 logcount = ntp->t_log_count;
3743 ntp = xfs_trans_dup(*tp);
3744 error = xfs_trans_commit(*tp, 0);
3745 *tp = ntp;
3746 *committed = 1;
3747
3748
3749
3750
3751 if (error)
3752 return error;
3753
3754
3755
3756
3757
3758 xfs_log_ticket_put(ntp->t_ticket);
3759
3760 if ((error = xfs_trans_reserve(ntp, 0, logres, 0, XFS_TRANS_PERM_LOG_RES,
3761 logcount)))
3762 return error;
3763 efd = xfs_trans_get_efd(ntp, efi, flist->xbf_count);
3764 for (free = flist->xbf_first; free != NULL; free = next) {
3765 next = free->xbfi_next;
3766 if ((error = xfs_free_extent(ntp, free->xbfi_startblock,
3767 free->xbfi_blockcount))) {
3768
3769
3770
3771
3772
3773
3774
3775
3776 mp = ntp->t_mountp;
3777 if (!XFS_FORCED_SHUTDOWN(mp))
3778 xfs_force_shutdown(mp,
3779 (error == EFSCORRUPTED) ?
3780 SHUTDOWN_CORRUPT_INCORE :
3781 SHUTDOWN_META_IO_ERROR);
3782 return error;
3783 }
3784 xfs_trans_log_efd_extent(ntp, efd, free->xbfi_startblock,
3785 free->xbfi_blockcount);
3786 xfs_bmap_del_free(flist, NULL, free);
3787 }
3788 return 0;
3789}
3790
3791
3792
3793
3794void
3795xfs_bmap_cancel(
3796 xfs_bmap_free_t *flist)
3797{
3798 xfs_bmap_free_item_t *free;
3799 xfs_bmap_free_item_t *next;
3800
3801 if (flist->xbf_count == 0)
3802 return;
3803 ASSERT(flist->xbf_first != NULL);
3804 for (free = flist->xbf_first; free; free = next) {
3805 next = free->xbfi_next;
3806 xfs_bmap_del_free(flist, NULL, free);
3807 }
3808 ASSERT(flist->xbf_count == 0);
3809}
3810
3811
3812
3813
3814
3815
3816
3817
3818int
3819xfs_bmap_first_unused(
3820 xfs_trans_t *tp,
3821 xfs_inode_t *ip,
3822 xfs_extlen_t len,
3823 xfs_fileoff_t *first_unused,
3824 int whichfork)
3825{
3826 int error;
3827 int idx;
3828 xfs_ifork_t *ifp;
3829 xfs_fileoff_t lastaddr;
3830 xfs_fileoff_t lowest;
3831 xfs_fileoff_t max;
3832 xfs_fileoff_t off;
3833 xfs_extnum_t nextents;
3834
3835 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3836 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3837 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3838 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3839 *first_unused = 0;
3840 return 0;
3841 }
3842 ifp = XFS_IFORK_PTR(ip, whichfork);
3843 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3844 (error = xfs_iread_extents(tp, ip, whichfork)))
3845 return error;
3846 lowest = *first_unused;
3847 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3848 for (idx = 0, lastaddr = 0, max = lowest; idx < nextents; idx++) {
3849 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, idx);
3850 off = xfs_bmbt_get_startoff(ep);
3851
3852
3853
3854 if (off >= lowest + len && off - max >= len) {
3855 *first_unused = max;
3856 return 0;
3857 }
3858 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3859 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3860 }
3861 *first_unused = max;
3862 return 0;
3863}
3864
3865
3866
3867
3868
3869
3870
3871int
3872xfs_bmap_last_before(
3873 xfs_trans_t *tp,
3874 xfs_inode_t *ip,
3875 xfs_fileoff_t *last_block,
3876 int whichfork)
3877{
3878 xfs_fileoff_t bno;
3879 int eof;
3880 xfs_bmbt_rec_host_t *ep;
3881 int error;
3882 xfs_bmbt_irec_t got;
3883 xfs_ifork_t *ifp;
3884 xfs_extnum_t lastx;
3885 xfs_bmbt_irec_t prev;
3886
3887 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3888 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3889 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3890 return XFS_ERROR(EIO);
3891 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3892 *last_block = 0;
3893 return 0;
3894 }
3895 ifp = XFS_IFORK_PTR(ip, whichfork);
3896 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3897 (error = xfs_iread_extents(tp, ip, whichfork)))
3898 return error;
3899 bno = *last_block - 1;
3900 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3901 &prev);
3902 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3903 if (prev.br_startoff == NULLFILEOFF)
3904 *last_block = 0;
3905 else
3906 *last_block = prev.br_startoff + prev.br_blockcount;
3907 }
3908
3909
3910
3911 return 0;
3912}
3913
3914STATIC int
3915xfs_bmap_last_extent(
3916 struct xfs_trans *tp,
3917 struct xfs_inode *ip,
3918 int whichfork,
3919 struct xfs_bmbt_irec *rec,
3920 int *is_empty)
3921{
3922 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
3923 int error;
3924 int nextents;
3925
3926 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
3927 error = xfs_iread_extents(tp, ip, whichfork);
3928 if (error)
3929 return error;
3930 }
3931
3932 nextents = ifp->if_bytes / sizeof(xfs_bmbt_rec_t);
3933 if (nextents == 0) {
3934 *is_empty = 1;
3935 return 0;
3936 }
3937
3938 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, nextents - 1), rec);
3939 *is_empty = 0;
3940 return 0;
3941}
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952STATIC int
3953xfs_bmap_isaeof(
3954 struct xfs_bmalloca *bma,
3955 int whichfork)
3956{
3957 struct xfs_bmbt_irec rec;
3958 int is_empty;
3959 int error;
3960
3961 bma->aeof = 0;
3962 error = xfs_bmap_last_extent(NULL, bma->ip, whichfork, &rec,
3963 &is_empty);
3964 if (error || is_empty)
3965 return error;
3966
3967
3968
3969
3970
3971 bma->aeof = bma->offset >= rec.br_startoff + rec.br_blockcount ||
3972 (bma->offset >= rec.br_startoff &&
3973 isnullstartblock(rec.br_startblock));
3974 return 0;
3975}
3976
3977
3978
3979
3980
3981
3982int
3983xfs_bmap_eof(
3984 struct xfs_inode *ip,
3985 xfs_fileoff_t endoff,
3986 int whichfork,
3987 int *eof)
3988{
3989 struct xfs_bmbt_irec rec;
3990 int error;
3991
3992 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, eof);
3993 if (error || *eof)
3994 return error;
3995
3996 *eof = endoff >= rec.br_startoff + rec.br_blockcount;
3997 return 0;
3998}
3999
4000
4001
4002
4003
4004
4005int
4006xfs_bmap_last_offset(
4007 struct xfs_trans *tp,
4008 struct xfs_inode *ip,
4009 xfs_fileoff_t *last_block,
4010 int whichfork)
4011{
4012 struct xfs_bmbt_irec rec;
4013 int is_empty;
4014 int error;
4015
4016 *last_block = 0;
4017
4018 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL)
4019 return 0;
4020
4021 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4022 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4023 return XFS_ERROR(EIO);
4024
4025 error = xfs_bmap_last_extent(NULL, ip, whichfork, &rec, &is_empty);
4026 if (error || is_empty)
4027 return error;
4028
4029 *last_block = rec.br_startoff + rec.br_blockcount;
4030 return 0;
4031}
4032
4033
4034
4035
4036
4037
4038int
4039xfs_bmap_one_block(
4040 xfs_inode_t *ip,
4041 int whichfork)
4042{
4043 xfs_bmbt_rec_host_t *ep;
4044 xfs_ifork_t *ifp;
4045 int rval;
4046 xfs_bmbt_irec_t s;
4047
4048#ifndef DEBUG
4049 if (whichfork == XFS_DATA_FORK)
4050 return XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize;
4051#endif
4052 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
4053 return 0;
4054 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4055 return 0;
4056 ifp = XFS_IFORK_PTR(ip, whichfork);
4057 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
4058 ep = xfs_iext_get_ext(ifp, 0);
4059 xfs_bmbt_get_all(ep, &s);
4060 rval = s.br_startoff == 0 && s.br_blockcount == 1;
4061 if (rval && whichfork == XFS_DATA_FORK)
4062 ASSERT(XFS_ISIZE(ip) == ip->i_mount->m_sb.sb_blocksize);
4063 return rval;
4064}
4065
4066STATIC int
4067xfs_bmap_sanity_check(
4068 struct xfs_mount *mp,
4069 struct xfs_buf *bp,
4070 int level)
4071{
4072 struct xfs_btree_block *block = XFS_BUF_TO_BLOCK(bp);
4073
4074 if (block->bb_magic != cpu_to_be32(XFS_BMAP_MAGIC) ||
4075 be16_to_cpu(block->bb_level) != level ||
4076 be16_to_cpu(block->bb_numrecs) == 0 ||
4077 be16_to_cpu(block->bb_numrecs) > mp->m_bmap_dmxr[level != 0])
4078 return 0;
4079 return 1;
4080}
4081
4082
4083
4084
4085
4086
4087
4088int
4089xfs_bmap_read_extents(
4090 xfs_trans_t *tp,
4091 xfs_inode_t *ip,
4092 int whichfork)
4093{
4094 struct xfs_btree_block *block;
4095 xfs_fsblock_t bno;
4096 xfs_buf_t *bp;
4097 int error;
4098 xfs_exntfmt_t exntf;
4099 xfs_extnum_t i, j;
4100 xfs_ifork_t *ifp;
4101 int level;
4102 xfs_mount_t *mp;
4103 __be64 *pp;
4104
4105 xfs_extnum_t room;
4106
4107 bno = NULLFSBLOCK;
4108 mp = ip->i_mount;
4109 ifp = XFS_IFORK_PTR(ip, whichfork);
4110 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
4111 XFS_EXTFMT_INODE(ip);
4112 block = ifp->if_broot;
4113
4114
4115
4116 level = be16_to_cpu(block->bb_level);
4117 ASSERT(level > 0);
4118 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
4119 bno = be64_to_cpu(*pp);
4120 ASSERT(bno != NULLDFSBNO);
4121 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
4122 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
4123
4124
4125
4126
4127 while (level-- > 0) {
4128 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4129 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
4130 if (error)
4131 return error;
4132 block = XFS_BUF_TO_BLOCK(bp);
4133 XFS_WANT_CORRUPTED_GOTO(
4134 xfs_bmap_sanity_check(mp, bp, level),
4135 error0);
4136 if (level == 0)
4137 break;
4138 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
4139 bno = be64_to_cpu(*pp);
4140 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
4141 xfs_trans_brelse(tp, bp);
4142 }
4143
4144
4145
4146 room = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4147 i = 0;
4148
4149
4150
4151 for (;;) {
4152 xfs_bmbt_rec_t *frp;
4153 xfs_fsblock_t nextbno;
4154 xfs_extnum_t num_recs;
4155 xfs_extnum_t start;
4156
4157 num_recs = xfs_btree_get_numrecs(block);
4158 if (unlikely(i + num_recs > room)) {
4159 ASSERT(i + num_recs <= room);
4160 xfs_warn(ip->i_mount,
4161 "corrupt dinode %Lu, (btree extents).",
4162 (unsigned long long) ip->i_ino);
4163 XFS_CORRUPTION_ERROR("xfs_bmap_read_extents(1)",
4164 XFS_ERRLEVEL_LOW, ip->i_mount, block);
4165 goto error0;
4166 }
4167 XFS_WANT_CORRUPTED_GOTO(
4168 xfs_bmap_sanity_check(mp, bp, 0),
4169 error0);
4170
4171
4172
4173 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
4174 if (nextbno != NULLFSBLOCK)
4175 xfs_btree_reada_bufl(mp, nextbno, 1,
4176 &xfs_bmbt_buf_ops);
4177
4178
4179
4180 frp = XFS_BMBT_REC_ADDR(mp, block, 1);
4181 start = i;
4182 for (j = 0; j < num_recs; j++, i++, frp++) {
4183 xfs_bmbt_rec_host_t *trp = xfs_iext_get_ext(ifp, i);
4184 trp->l0 = be64_to_cpu(frp->l0);
4185 trp->l1 = be64_to_cpu(frp->l1);
4186 }
4187 if (exntf == XFS_EXTFMT_NOSTATE) {
4188
4189
4190
4191
4192
4193 if (unlikely(xfs_check_nostate_extents(ifp,
4194 start, num_recs))) {
4195 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
4196 XFS_ERRLEVEL_LOW,
4197 ip->i_mount);
4198 goto error0;
4199 }
4200 }
4201 xfs_trans_brelse(tp, bp);
4202 bno = nextbno;
4203
4204
4205
4206 if (bno == NULLFSBLOCK)
4207 break;
4208 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
4209 XFS_BMAP_BTREE_REF, &xfs_bmbt_buf_ops);
4210 if (error)
4211 return error;
4212 block = XFS_BUF_TO_BLOCK(bp);
4213 }
4214 ASSERT(i == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4215 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
4216 XFS_BMAP_TRACE_EXLIST(ip, i, whichfork);
4217 return 0;
4218error0:
4219 xfs_trans_brelse(tp, bp);
4220 return XFS_ERROR(EFSCORRUPTED);
4221}
4222
4223#ifdef DEBUG
4224
4225
4226
4227void
4228xfs_bmap_trace_exlist(
4229 xfs_inode_t *ip,
4230 xfs_extnum_t cnt,
4231 int whichfork,
4232 unsigned long caller_ip)
4233{
4234 xfs_extnum_t idx;
4235 xfs_ifork_t *ifp;
4236 int state = 0;
4237
4238 if (whichfork == XFS_ATTR_FORK)
4239 state |= BMAP_ATTRFORK;
4240
4241 ifp = XFS_IFORK_PTR(ip, whichfork);
4242 ASSERT(cnt == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t)));
4243 for (idx = 0; idx < cnt; idx++)
4244 trace_xfs_extlist(ip, idx, whichfork, caller_ip);
4245}
4246
4247
4248
4249
4250
4251
4252
4253STATIC void
4254xfs_bmap_validate_ret(
4255 xfs_fileoff_t bno,
4256 xfs_filblks_t len,
4257 int flags,
4258 xfs_bmbt_irec_t *mval,
4259 int nmap,
4260 int ret_nmap)
4261{
4262 int i;
4263
4264 ASSERT(ret_nmap <= nmap);
4265
4266 for (i = 0; i < ret_nmap; i++) {
4267 ASSERT(mval[i].br_blockcount > 0);
4268 if (!(flags & XFS_BMAPI_ENTIRE)) {
4269 ASSERT(mval[i].br_startoff >= bno);
4270 ASSERT(mval[i].br_blockcount <= len);
4271 ASSERT(mval[i].br_startoff + mval[i].br_blockcount <=
4272 bno + len);
4273 } else {
4274 ASSERT(mval[i].br_startoff < bno + len);
4275 ASSERT(mval[i].br_startoff + mval[i].br_blockcount >
4276 bno);
4277 }
4278 ASSERT(i == 0 ||
4279 mval[i - 1].br_startoff + mval[i - 1].br_blockcount ==
4280 mval[i].br_startoff);
4281 ASSERT(mval[i].br_startblock != DELAYSTARTBLOCK &&
4282 mval[i].br_startblock != HOLESTARTBLOCK);
4283 ASSERT(mval[i].br_state == XFS_EXT_NORM ||
4284 mval[i].br_state == XFS_EXT_UNWRITTEN);
4285 }
4286}
4287#endif
4288
4289
4290
4291
4292
4293STATIC void
4294xfs_bmapi_trim_map(
4295 struct xfs_bmbt_irec *mval,
4296 struct xfs_bmbt_irec *got,
4297 xfs_fileoff_t *bno,
4298 xfs_filblks_t len,
4299 xfs_fileoff_t obno,
4300 xfs_fileoff_t end,
4301 int n,
4302 int flags)
4303{
4304 if ((flags & XFS_BMAPI_ENTIRE) ||
4305 got->br_startoff + got->br_blockcount <= obno) {
4306 *mval = *got;
4307 if (isnullstartblock(got->br_startblock))
4308 mval->br_startblock = DELAYSTARTBLOCK;
4309 return;
4310 }
4311
4312 if (obno > *bno)
4313 *bno = obno;
4314 ASSERT((*bno >= obno) || (n == 0));
4315 ASSERT(*bno < end);
4316 mval->br_startoff = *bno;
4317 if (isnullstartblock(got->br_startblock))
4318 mval->br_startblock = DELAYSTARTBLOCK;
4319 else
4320 mval->br_startblock = got->br_startblock +
4321 (*bno - got->br_startoff);
4322
4323
4324
4325
4326
4327
4328
4329 mval->br_blockcount = XFS_FILBLKS_MIN(end - *bno,
4330 got->br_blockcount - (*bno - got->br_startoff));
4331 mval->br_state = got->br_state;
4332 ASSERT(mval->br_blockcount <= len);
4333 return;
4334}
4335
4336
4337
4338
4339STATIC void
4340xfs_bmapi_update_map(
4341 struct xfs_bmbt_irec **map,
4342 xfs_fileoff_t *bno,
4343 xfs_filblks_t *len,
4344 xfs_fileoff_t obno,
4345 xfs_fileoff_t end,
4346 int *n,
4347 int flags)
4348{
4349 xfs_bmbt_irec_t *mval = *map;
4350
4351 ASSERT((flags & XFS_BMAPI_ENTIRE) ||
4352 ((mval->br_startoff + mval->br_blockcount) <= end));
4353 ASSERT((flags & XFS_BMAPI_ENTIRE) || (mval->br_blockcount <= *len) ||
4354 (mval->br_startoff < obno));
4355
4356 *bno = mval->br_startoff + mval->br_blockcount;
4357 *len = end - *bno;
4358 if (*n > 0 && mval->br_startoff == mval[-1].br_startoff) {
4359
4360 ASSERT(mval->br_startblock == mval[-1].br_startblock);
4361 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
4362 ASSERT(mval->br_state == mval[-1].br_state);
4363 mval[-1].br_blockcount = mval->br_blockcount;
4364 mval[-1].br_state = mval->br_state;
4365 } else if (*n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
4366 mval[-1].br_startblock != DELAYSTARTBLOCK &&
4367 mval[-1].br_startblock != HOLESTARTBLOCK &&
4368 mval->br_startblock == mval[-1].br_startblock +
4369 mval[-1].br_blockcount &&
4370 ((flags & XFS_BMAPI_IGSTATE) ||
4371 mval[-1].br_state == mval->br_state)) {
4372 ASSERT(mval->br_startoff ==
4373 mval[-1].br_startoff + mval[-1].br_blockcount);
4374 mval[-1].br_blockcount += mval->br_blockcount;
4375 } else if (*n > 0 &&
4376 mval->br_startblock == DELAYSTARTBLOCK &&
4377 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4378 mval->br_startoff ==
4379 mval[-1].br_startoff + mval[-1].br_blockcount) {
4380 mval[-1].br_blockcount += mval->br_blockcount;
4381 mval[-1].br_state = mval->br_state;
4382 } else if (!((*n == 0) &&
4383 ((mval->br_startoff + mval->br_blockcount) <=
4384 obno))) {
4385 mval++;
4386 (*n)++;
4387 }
4388 *map = mval;
4389}
4390
4391
4392
4393
4394int
4395xfs_bmapi_read(
4396 struct xfs_inode *ip,
4397 xfs_fileoff_t bno,
4398 xfs_filblks_t len,
4399 struct xfs_bmbt_irec *mval,
4400 int *nmap,
4401 int flags)
4402{
4403 struct xfs_mount *mp = ip->i_mount;
4404 struct xfs_ifork *ifp;
4405 struct xfs_bmbt_irec got;
4406 struct xfs_bmbt_irec prev;
4407 xfs_fileoff_t obno;
4408 xfs_fileoff_t end;
4409 xfs_extnum_t lastx;
4410 int error;
4411 int eof;
4412 int n = 0;
4413 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4414 XFS_ATTR_FORK : XFS_DATA_FORK;
4415
4416 ASSERT(*nmap >= 1);
4417 ASSERT(!(flags & ~(XFS_BMAPI_ATTRFORK|XFS_BMAPI_ENTIRE|
4418 XFS_BMAPI_IGSTATE)));
4419
4420 if (unlikely(XFS_TEST_ERROR(
4421 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4422 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE),
4423 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4424 XFS_ERROR_REPORT("xfs_bmapi_read", XFS_ERRLEVEL_LOW, mp);
4425 return XFS_ERROR(EFSCORRUPTED);
4426 }
4427
4428 if (XFS_FORCED_SHUTDOWN(mp))
4429 return XFS_ERROR(EIO);
4430
4431 XFS_STATS_INC(xs_blk_mapr);
4432
4433 ifp = XFS_IFORK_PTR(ip, whichfork);
4434
4435 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4436 error = xfs_iread_extents(NULL, ip, whichfork);
4437 if (error)
4438 return error;
4439 }
4440
4441 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got, &prev);
4442 end = bno + len;
4443 obno = bno;
4444
4445 while (bno < end && n < *nmap) {
4446
4447 if (eof)
4448 got.br_startoff = end;
4449 if (got.br_startoff > bno) {
4450
4451 mval->br_startoff = bno;
4452 mval->br_startblock = HOLESTARTBLOCK;
4453 mval->br_blockcount =
4454 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
4455 mval->br_state = XFS_EXT_NORM;
4456 bno += mval->br_blockcount;
4457 len -= mval->br_blockcount;
4458 mval++;
4459 n++;
4460 continue;
4461 }
4462
4463
4464 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4465 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4466
4467
4468 if (bno >= end || n >= *nmap)
4469 break;
4470
4471
4472 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4473 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4474 else
4475 eof = 1;
4476 }
4477 *nmap = n;
4478 return 0;
4479}
4480
4481STATIC int
4482xfs_bmapi_reserve_delalloc(
4483 struct xfs_inode *ip,
4484 xfs_fileoff_t aoff,
4485 xfs_filblks_t len,
4486 struct xfs_bmbt_irec *got,
4487 struct xfs_bmbt_irec *prev,
4488 xfs_extnum_t *lastx,
4489 int eof)
4490{
4491 struct xfs_mount *mp = ip->i_mount;
4492 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4493 xfs_extlen_t alen;
4494 xfs_extlen_t indlen;
4495 char rt = XFS_IS_REALTIME_INODE(ip);
4496 xfs_extlen_t extsz;
4497 int error;
4498
4499 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN);
4500 if (!eof)
4501 alen = XFS_FILBLKS_MIN(alen, got->br_startoff - aoff);
4502
4503
4504 extsz = xfs_get_extsz_hint(ip);
4505 if (extsz) {
4506
4507
4508
4509
4510
4511
4512 alen = XFS_FILBLKS_MIN(len, MAXEXTLEN - (2 * extsz - 1));
4513 error = xfs_bmap_extsize_align(mp, got, prev, extsz, rt, eof,
4514 1, 0, &aoff, &alen);
4515 ASSERT(!error);
4516 }
4517
4518 if (rt)
4519 extsz = alen / mp->m_sb.sb_rextsize;
4520
4521
4522
4523
4524
4525
4526 error = xfs_trans_reserve_quota_nblks(NULL, ip, (long)alen, 0,
4527 rt ? XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4528 if (error)
4529 return error;
4530
4531
4532
4533
4534
4535 indlen = (xfs_extlen_t)xfs_bmap_worst_indlen(ip, alen);
4536 ASSERT(indlen > 0);
4537
4538 if (rt) {
4539 error = xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
4540 -((int64_t)extsz), 0);
4541 } else {
4542 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4543 -((int64_t)alen), 0);
4544 }
4545
4546 if (error)
4547 goto out_unreserve_quota;
4548
4549 error = xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
4550 -((int64_t)indlen), 0);
4551 if (error)
4552 goto out_unreserve_blocks;
4553
4554
4555 ip->i_delayed_blks += alen;
4556
4557 got->br_startoff = aoff;
4558 got->br_startblock = nullstartblock(indlen);
4559 got->br_blockcount = alen;
4560 got->br_state = XFS_EXT_NORM;
4561 xfs_bmap_add_extent_hole_delay(ip, lastx, got);
4562
4563
4564
4565
4566
4567 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, *lastx), got);
4568
4569 ASSERT(got->br_startoff <= aoff);
4570 ASSERT(got->br_startoff + got->br_blockcount >= aoff + alen);
4571 ASSERT(isnullstartblock(got->br_startblock));
4572 ASSERT(got->br_state == XFS_EXT_NORM);
4573 return 0;
4574
4575out_unreserve_blocks:
4576 if (rt)
4577 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS, extsz, 0);
4578 else
4579 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS, alen, 0);
4580out_unreserve_quota:
4581 if (XFS_IS_QUOTA_ON(mp))
4582 xfs_trans_unreserve_quota_nblks(NULL, ip, (long)alen, 0, rt ?
4583 XFS_QMOPT_RES_RTBLKS : XFS_QMOPT_RES_REGBLKS);
4584 return error;
4585}
4586
4587
4588
4589
4590int
4591xfs_bmapi_delay(
4592 struct xfs_inode *ip,
4593 xfs_fileoff_t bno,
4594 xfs_filblks_t len,
4595 struct xfs_bmbt_irec *mval,
4596 int *nmap,
4597 int flags)
4598{
4599 struct xfs_mount *mp = ip->i_mount;
4600 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
4601 struct xfs_bmbt_irec got;
4602 struct xfs_bmbt_irec prev;
4603 xfs_fileoff_t obno;
4604 xfs_fileoff_t end;
4605 xfs_extnum_t lastx;
4606 int eof;
4607 int n = 0;
4608 int error = 0;
4609
4610 ASSERT(*nmap >= 1);
4611 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4612 ASSERT(!(flags & ~XFS_BMAPI_ENTIRE));
4613
4614 if (unlikely(XFS_TEST_ERROR(
4615 (XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_EXTENTS &&
4616 XFS_IFORK_FORMAT(ip, XFS_DATA_FORK) != XFS_DINODE_FMT_BTREE),
4617 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4618 XFS_ERROR_REPORT("xfs_bmapi_delay", XFS_ERRLEVEL_LOW, mp);
4619 return XFS_ERROR(EFSCORRUPTED);
4620 }
4621
4622 if (XFS_FORCED_SHUTDOWN(mp))
4623 return XFS_ERROR(EIO);
4624
4625 XFS_STATS_INC(xs_blk_mapw);
4626
4627 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
4628 error = xfs_iread_extents(NULL, ip, XFS_DATA_FORK);
4629 if (error)
4630 return error;
4631 }
4632
4633 xfs_bmap_search_extents(ip, bno, XFS_DATA_FORK, &eof, &lastx, &got, &prev);
4634 end = bno + len;
4635 obno = bno;
4636
4637 while (bno < end && n < *nmap) {
4638 if (eof || got.br_startoff > bno) {
4639 error = xfs_bmapi_reserve_delalloc(ip, bno, len, &got,
4640 &prev, &lastx, eof);
4641 if (error) {
4642 if (n == 0) {
4643 *nmap = 0;
4644 return error;
4645 }
4646 break;
4647 }
4648 }
4649
4650
4651 xfs_bmapi_trim_map(mval, &got, &bno, len, obno, end, n, flags);
4652 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
4653
4654
4655 if (bno >= end || n >= *nmap)
4656 break;
4657
4658
4659 prev = got;
4660 if (++lastx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t))
4661 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, lastx), &got);
4662 else
4663 eof = 1;
4664 }
4665
4666 *nmap = n;
4667 return 0;
4668}
4669
4670
4671STATIC int
4672__xfs_bmapi_allocate(
4673 struct xfs_bmalloca *bma)
4674{
4675 struct xfs_mount *mp = bma->ip->i_mount;
4676 int whichfork = (bma->flags & XFS_BMAPI_ATTRFORK) ?
4677 XFS_ATTR_FORK : XFS_DATA_FORK;
4678 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4679 int tmp_logflags = 0;
4680 int error;
4681 int rt;
4682
4683 ASSERT(bma->length > 0);
4684
4685 rt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(bma->ip);
4686
4687
4688
4689
4690
4691 if (bma->wasdel) {
4692 bma->length = (xfs_extlen_t)bma->got.br_blockcount;
4693 bma->offset = bma->got.br_startoff;
4694 if (bma->idx != NULLEXTNUM && bma->idx) {
4695 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx - 1),
4696 &bma->prev);
4697 }
4698 } else {
4699 bma->length = XFS_FILBLKS_MIN(bma->length, MAXEXTLEN);
4700 if (!bma->eof)
4701 bma->length = XFS_FILBLKS_MIN(bma->length,
4702 bma->got.br_startoff - bma->offset);
4703 }
4704
4705
4706
4707
4708
4709 if (!(bma->flags & XFS_BMAPI_METADATA)) {
4710 bma->userdata = (bma->offset == 0) ?
4711 XFS_ALLOC_INITIAL_USER_DATA : XFS_ALLOC_USERDATA;
4712 }
4713
4714 bma->minlen = (bma->flags & XFS_BMAPI_CONTIG) ? bma->length : 1;
4715
4716
4717
4718
4719
4720 if (mp->m_dalign && bma->length >= mp->m_dalign &&
4721 !(bma->flags & XFS_BMAPI_METADATA) && whichfork == XFS_DATA_FORK) {
4722 error = xfs_bmap_isaeof(bma, whichfork);
4723 if (error)
4724 return error;
4725 }
4726
4727 error = xfs_bmap_alloc(bma);
4728 if (error)
4729 return error;
4730
4731 if (bma->flist->xbf_low)
4732 bma->minleft = 0;
4733 if (bma->cur)
4734 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4735 if (bma->blkno == NULLFSBLOCK)
4736 return 0;
4737 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4738 bma->cur = xfs_bmbt_init_cursor(mp, bma->tp, bma->ip, whichfork);
4739 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4740 bma->cur->bc_private.b.flist = bma->flist;
4741 }
4742
4743
4744
4745
4746 bma->nallocs++;
4747
4748 if (bma->cur)
4749 bma->cur->bc_private.b.flags =
4750 bma->wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
4751
4752 bma->got.br_startoff = bma->offset;
4753 bma->got.br_startblock = bma->blkno;
4754 bma->got.br_blockcount = bma->length;
4755 bma->got.br_state = XFS_EXT_NORM;
4756
4757
4758
4759
4760
4761 if (!bma->wasdel && (bma->flags & XFS_BMAPI_PREALLOC) &&
4762 xfs_sb_version_hasextflgbit(&mp->m_sb))
4763 bma->got.br_state = XFS_EXT_UNWRITTEN;
4764
4765 if (bma->wasdel)
4766 error = xfs_bmap_add_extent_delay_real(bma);
4767 else
4768 error = xfs_bmap_add_extent_hole_real(bma, whichfork);
4769
4770 bma->logflags |= tmp_logflags;
4771 if (error)
4772 return error;
4773
4774
4775
4776
4777
4778
4779 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4780
4781 ASSERT(bma->got.br_startoff <= bma->offset);
4782 ASSERT(bma->got.br_startoff + bma->got.br_blockcount >=
4783 bma->offset + bma->length);
4784 ASSERT(bma->got.br_state == XFS_EXT_NORM ||
4785 bma->got.br_state == XFS_EXT_UNWRITTEN);
4786 return 0;
4787}
4788
4789static void
4790xfs_bmapi_allocate_worker(
4791 struct work_struct *work)
4792{
4793 struct xfs_bmalloca *args = container_of(work,
4794 struct xfs_bmalloca, work);
4795 unsigned long pflags;
4796
4797
4798 current_set_flags_nested(&pflags, PF_FSTRANS);
4799
4800 args->result = __xfs_bmapi_allocate(args);
4801 complete(args->done);
4802
4803 current_restore_flags_nested(&pflags, PF_FSTRANS);
4804}
4805
4806
4807
4808
4809
4810
4811int
4812xfs_bmapi_allocate(
4813 struct xfs_bmalloca *args)
4814{
4815 DECLARE_COMPLETION_ONSTACK(done);
4816
4817 if (!args->stack_switch)
4818 return __xfs_bmapi_allocate(args);
4819
4820
4821 args->done = &done;
4822 INIT_WORK_ONSTACK(&args->work, xfs_bmapi_allocate_worker);
4823 queue_work(xfs_alloc_wq, &args->work);
4824 wait_for_completion(&done);
4825 return args->result;
4826}
4827
4828STATIC int
4829xfs_bmapi_convert_unwritten(
4830 struct xfs_bmalloca *bma,
4831 struct xfs_bmbt_irec *mval,
4832 xfs_filblks_t len,
4833 int flags)
4834{
4835 int whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4836 XFS_ATTR_FORK : XFS_DATA_FORK;
4837 struct xfs_ifork *ifp = XFS_IFORK_PTR(bma->ip, whichfork);
4838 int tmp_logflags = 0;
4839 int error;
4840
4841
4842 if (mval->br_state == XFS_EXT_UNWRITTEN &&
4843 (flags & XFS_BMAPI_PREALLOC))
4844 return 0;
4845
4846
4847 if (mval->br_state == XFS_EXT_NORM &&
4848 (flags & (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT)) !=
4849 (XFS_BMAPI_PREALLOC | XFS_BMAPI_CONVERT))
4850 return 0;
4851
4852
4853
4854
4855 ASSERT(mval->br_blockcount <= len);
4856 if ((ifp->if_flags & XFS_IFBROOT) && !bma->cur) {
4857 bma->cur = xfs_bmbt_init_cursor(bma->ip->i_mount, bma->tp,
4858 bma->ip, whichfork);
4859 bma->cur->bc_private.b.firstblock = *bma->firstblock;
4860 bma->cur->bc_private.b.flist = bma->flist;
4861 }
4862 mval->br_state = (mval->br_state == XFS_EXT_UNWRITTEN)
4863 ? XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
4864
4865 error = xfs_bmap_add_extent_unwritten_real(bma->tp, bma->ip, &bma->idx,
4866 &bma->cur, mval, bma->firstblock, bma->flist,
4867 &tmp_logflags);
4868 bma->logflags |= tmp_logflags;
4869 if (error)
4870 return error;
4871
4872
4873
4874
4875
4876
4877 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma->idx), &bma->got);
4878
4879
4880
4881
4882
4883 if (mval->br_blockcount < len)
4884 return EAGAIN;
4885 return 0;
4886}
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900int
4901xfs_bmapi_write(
4902 struct xfs_trans *tp,
4903 struct xfs_inode *ip,
4904 xfs_fileoff_t bno,
4905 xfs_filblks_t len,
4906 int flags,
4907 xfs_fsblock_t *firstblock,
4908
4909 xfs_extlen_t total,
4910 struct xfs_bmbt_irec *mval,
4911 int *nmap,
4912 struct xfs_bmap_free *flist)
4913{
4914 struct xfs_mount *mp = ip->i_mount;
4915 struct xfs_ifork *ifp;
4916 struct xfs_bmalloca bma = { 0 };
4917 xfs_fileoff_t end;
4918 int eof;
4919 int error;
4920 int n;
4921 xfs_fileoff_t obno;
4922 int whichfork;
4923 char inhole;
4924 char wasdelay;
4925
4926#ifdef DEBUG
4927 xfs_fileoff_t orig_bno;
4928 int orig_flags;
4929 xfs_filblks_t orig_len;
4930 struct xfs_bmbt_irec *orig_mval;
4931 int orig_nmap;
4932
4933 orig_bno = bno;
4934 orig_len = len;
4935 orig_flags = flags;
4936 orig_mval = mval;
4937 orig_nmap = *nmap;
4938#endif
4939
4940 ASSERT(*nmap >= 1);
4941 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP);
4942 ASSERT(!(flags & XFS_BMAPI_IGSTATE));
4943 ASSERT(tp != NULL);
4944 ASSERT(len > 0);
4945
4946 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4947 XFS_ATTR_FORK : XFS_DATA_FORK;
4948
4949 if (unlikely(XFS_TEST_ERROR(
4950 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4951 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4952 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
4953 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
4954 XFS_ERROR_REPORT("xfs_bmapi_write", XFS_ERRLEVEL_LOW, mp);
4955 return XFS_ERROR(EFSCORRUPTED);
4956 }
4957
4958 if (XFS_FORCED_SHUTDOWN(mp))
4959 return XFS_ERROR(EIO);
4960
4961 ifp = XFS_IFORK_PTR(ip, whichfork);
4962
4963 XFS_STATS_INC(xs_blk_mapw);
4964
4965 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989 error = xfs_bmap_local_to_extents(tp, ip, firstblock, total,
4990 &bma.logflags, whichfork,
4991 xfs_bmap_local_to_extents_init_fn);
4992 if (error)
4993 goto error0;
4994 }
4995
4996 if (*firstblock == NULLFSBLOCK) {
4997 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
4998 bma.minleft = be16_to_cpu(ifp->if_broot->bb_level) + 1;
4999 else
5000 bma.minleft = 1;
5001 } else {
5002 bma.minleft = 0;
5003 }
5004
5005 if (!(ifp->if_flags & XFS_IFEXTENTS)) {
5006 error = xfs_iread_extents(tp, ip, whichfork);
5007 if (error)
5008 goto error0;
5009 }
5010
5011 xfs_bmap_search_extents(ip, bno, whichfork, &eof, &bma.idx, &bma.got,
5012 &bma.prev);
5013 n = 0;
5014 end = bno + len;
5015 obno = bno;
5016
5017 bma.tp = tp;
5018 bma.ip = ip;
5019 bma.total = total;
5020 bma.userdata = 0;
5021 bma.flist = flist;
5022 bma.firstblock = firstblock;
5023
5024 if (flags & XFS_BMAPI_STACK_SWITCH)
5025 bma.stack_switch = 1;
5026
5027 while (bno < end && n < *nmap) {
5028 inhole = eof || bma.got.br_startoff > bno;
5029 wasdelay = !inhole && isnullstartblock(bma.got.br_startblock);
5030
5031
5032
5033
5034
5035 if (inhole || wasdelay) {
5036 bma.eof = eof;
5037 bma.conv = !!(flags & XFS_BMAPI_CONVERT);
5038 bma.wasdel = wasdelay;
5039 bma.offset = bno;
5040 bma.flags = flags;
5041
5042
5043
5044
5045
5046
5047
5048
5049 if (len > (xfs_filblks_t)MAXEXTLEN)
5050 bma.length = MAXEXTLEN;
5051 else
5052 bma.length = len;
5053
5054 ASSERT(len > 0);
5055 ASSERT(bma.length > 0);
5056 error = xfs_bmapi_allocate(&bma);
5057 if (error)
5058 goto error0;
5059 if (bma.blkno == NULLFSBLOCK)
5060 break;
5061 }
5062
5063
5064 xfs_bmapi_trim_map(mval, &bma.got, &bno, len, obno,
5065 end, n, flags);
5066
5067
5068 error = xfs_bmapi_convert_unwritten(&bma, mval, len, flags);
5069 if (error == EAGAIN)
5070 continue;
5071 if (error)
5072 goto error0;
5073
5074
5075 xfs_bmapi_update_map(&mval, &bno, &len, obno, end, &n, flags);
5076
5077
5078
5079
5080
5081
5082 if (bno >= end || n >= *nmap || bma.nallocs >= *nmap)
5083 break;
5084
5085
5086 bma.prev = bma.got;
5087 if (++bma.idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t)) {
5088 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, bma.idx),
5089 &bma.got);
5090 } else
5091 eof = 1;
5092 }
5093 *nmap = n;
5094
5095
5096
5097
5098 if (xfs_bmap_wants_extents(ip, whichfork)) {
5099 int tmp_logflags = 0;
5100
5101 ASSERT(bma.cur);
5102 error = xfs_bmap_btree_to_extents(tp, ip, bma.cur,
5103 &tmp_logflags, whichfork);
5104 bma.logflags |= tmp_logflags;
5105 if (error)
5106 goto error0;
5107 }
5108
5109 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
5110 XFS_IFORK_NEXTENTS(ip, whichfork) >
5111 XFS_IFORK_MAXEXT(ip, whichfork));
5112 error = 0;
5113error0:
5114
5115
5116
5117
5118 if ((bma.logflags & xfs_ilog_fext(whichfork)) &&
5119 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5120 bma.logflags &= ~xfs_ilog_fext(whichfork);
5121 else if ((bma.logflags & xfs_ilog_fbroot(whichfork)) &&
5122 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5123 bma.logflags &= ~xfs_ilog_fbroot(whichfork);
5124
5125
5126
5127
5128
5129 if (bma.logflags)
5130 xfs_trans_log_inode(tp, ip, bma.logflags);
5131
5132 if (bma.cur) {
5133 if (!error) {
5134 ASSERT(*firstblock == NULLFSBLOCK ||
5135 XFS_FSB_TO_AGNO(mp, *firstblock) ==
5136 XFS_FSB_TO_AGNO(mp,
5137 bma.cur->bc_private.b.firstblock) ||
5138 (flist->xbf_low &&
5139 XFS_FSB_TO_AGNO(mp, *firstblock) <
5140 XFS_FSB_TO_AGNO(mp,
5141 bma.cur->bc_private.b.firstblock)));
5142 *firstblock = bma.cur->bc_private.b.firstblock;
5143 }
5144 xfs_btree_del_cursor(bma.cur,
5145 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5146 }
5147 if (!error)
5148 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
5149 orig_nmap, *nmap);
5150 return error;
5151}
5152
5153
5154
5155
5156
5157
5158
5159int
5160xfs_bunmapi(
5161 xfs_trans_t *tp,
5162 struct xfs_inode *ip,
5163 xfs_fileoff_t bno,
5164 xfs_filblks_t len,
5165 int flags,
5166 xfs_extnum_t nexts,
5167 xfs_fsblock_t *firstblock,
5168
5169 xfs_bmap_free_t *flist,
5170 int *done)
5171{
5172 xfs_btree_cur_t *cur;
5173 xfs_bmbt_irec_t del;
5174 int eof;
5175 xfs_bmbt_rec_host_t *ep;
5176 int error;
5177 xfs_extnum_t extno;
5178 xfs_bmbt_irec_t got;
5179 xfs_ifork_t *ifp;
5180 int isrt;
5181 xfs_extnum_t lastx;
5182 int logflags;
5183 xfs_extlen_t mod;
5184 xfs_mount_t *mp;
5185 xfs_extnum_t nextents;
5186 xfs_bmbt_irec_t prev;
5187 xfs_fileoff_t start;
5188 int tmp_logflags;
5189 int wasdel;
5190 int whichfork;
5191 xfs_fsblock_t sum;
5192
5193 trace_xfs_bunmap(ip, bno, len, flags, _RET_IP_);
5194
5195 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
5196 XFS_ATTR_FORK : XFS_DATA_FORK;
5197 ifp = XFS_IFORK_PTR(ip, whichfork);
5198 if (unlikely(
5199 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
5200 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
5201 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
5202 ip->i_mount);
5203 return XFS_ERROR(EFSCORRUPTED);
5204 }
5205 mp = ip->i_mount;
5206 if (XFS_FORCED_SHUTDOWN(mp))
5207 return XFS_ERROR(EIO);
5208
5209 ASSERT(len > 0);
5210 ASSERT(nexts >= 0);
5211
5212 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
5213 (error = xfs_iread_extents(tp, ip, whichfork)))
5214 return error;
5215 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
5216 if (nextents == 0) {
5217 *done = 1;
5218 return 0;
5219 }
5220 XFS_STATS_INC(xs_blk_unmap);
5221 isrt = (whichfork == XFS_DATA_FORK) && XFS_IS_REALTIME_INODE(ip);
5222 start = bno;
5223 bno = start + len - 1;
5224 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
5225 &prev);
5226
5227
5228
5229
5230
5231 if (eof) {
5232 ep = xfs_iext_get_ext(ifp, --lastx);
5233 xfs_bmbt_get_all(ep, &got);
5234 bno = got.br_startoff + got.br_blockcount - 1;
5235 }
5236 logflags = 0;
5237 if (ifp->if_flags & XFS_IFBROOT) {
5238 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
5239 cur = xfs_bmbt_init_cursor(mp, tp, ip, whichfork);
5240 cur->bc_private.b.firstblock = *firstblock;
5241 cur->bc_private.b.flist = flist;
5242 cur->bc_private.b.flags = 0;
5243 } else
5244 cur = NULL;
5245
5246 if (isrt) {
5247
5248
5249
5250 xfs_ilock(mp->m_rbmip, XFS_ILOCK_EXCL);
5251 xfs_trans_ijoin(tp, mp->m_rbmip, XFS_ILOCK_EXCL);
5252 }
5253
5254 extno = 0;
5255 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
5256 (nexts == 0 || extno < nexts)) {
5257
5258
5259
5260
5261 if (got.br_startoff > bno) {
5262 if (--lastx < 0)
5263 break;
5264 ep = xfs_iext_get_ext(ifp, lastx);
5265 xfs_bmbt_get_all(ep, &got);
5266 }
5267
5268
5269
5270
5271 bno = XFS_FILEOFF_MIN(bno,
5272 got.br_startoff + got.br_blockcount - 1);
5273 if (bno < start)
5274 break;
5275
5276
5277
5278
5279 ASSERT(ep != NULL);
5280 del = got;
5281 wasdel = isnullstartblock(del.br_startblock);
5282 if (got.br_startoff < start) {
5283 del.br_startoff = start;
5284 del.br_blockcount -= start - got.br_startoff;
5285 if (!wasdel)
5286 del.br_startblock += start - got.br_startoff;
5287 }
5288 if (del.br_startoff + del.br_blockcount > bno + 1)
5289 del.br_blockcount = bno + 1 - del.br_startoff;
5290 sum = del.br_startblock + del.br_blockcount;
5291 if (isrt &&
5292 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
5293
5294
5295
5296
5297
5298
5299
5300 if (del.br_state == XFS_EXT_UNWRITTEN ||
5301 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5302
5303
5304
5305
5306 ASSERT(bno >= mod);
5307 bno -= mod > del.br_blockcount ?
5308 del.br_blockcount : mod;
5309 if (bno < got.br_startoff) {
5310 if (--lastx >= 0)
5311 xfs_bmbt_get_all(xfs_iext_get_ext(
5312 ifp, lastx), &got);
5313 }
5314 continue;
5315 }
5316
5317
5318
5319
5320 ASSERT(del.br_state == XFS_EXT_NORM);
5321 ASSERT(xfs_trans_get_block_res(tp) > 0);
5322
5323
5324
5325
5326 if (del.br_blockcount > mod) {
5327 del.br_startoff += del.br_blockcount - mod;
5328 del.br_startblock += del.br_blockcount - mod;
5329 del.br_blockcount = mod;
5330 }
5331 del.br_state = XFS_EXT_UNWRITTEN;
5332 error = xfs_bmap_add_extent_unwritten_real(tp, ip,
5333 &lastx, &cur, &del, firstblock, flist,
5334 &logflags);
5335 if (error)
5336 goto error0;
5337 goto nodelete;
5338 }
5339 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
5340
5341
5342
5343
5344
5345 mod = mp->m_sb.sb_rextsize - mod;
5346 if (del.br_blockcount > mod) {
5347 del.br_blockcount -= mod;
5348 del.br_startoff += mod;
5349 del.br_startblock += mod;
5350 } else if ((del.br_startoff == start &&
5351 (del.br_state == XFS_EXT_UNWRITTEN ||
5352 xfs_trans_get_block_res(tp) == 0)) ||
5353 !xfs_sb_version_hasextflgbit(&mp->m_sb)) {
5354
5355
5356
5357
5358 ASSERT(bno >= del.br_blockcount);
5359 bno -= del.br_blockcount;
5360 if (got.br_startoff > bno) {
5361 if (--lastx >= 0) {
5362 ep = xfs_iext_get_ext(ifp,
5363 lastx);
5364 xfs_bmbt_get_all(ep, &got);
5365 }
5366 }
5367 continue;
5368 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
5369
5370
5371
5372
5373
5374
5375 ASSERT(lastx > 0);
5376 xfs_bmbt_get_all(xfs_iext_get_ext(ifp,
5377 lastx - 1), &prev);
5378 ASSERT(prev.br_state == XFS_EXT_NORM);
5379 ASSERT(!isnullstartblock(prev.br_startblock));
5380 ASSERT(del.br_startblock ==
5381 prev.br_startblock + prev.br_blockcount);
5382 if (prev.br_startoff < start) {
5383 mod = start - prev.br_startoff;
5384 prev.br_blockcount -= mod;
5385 prev.br_startblock += mod;
5386 prev.br_startoff = start;
5387 }
5388 prev.br_state = XFS_EXT_UNWRITTEN;
5389 lastx--;
5390 error = xfs_bmap_add_extent_unwritten_real(tp,
5391 ip, &lastx, &cur, &prev,
5392 firstblock, flist, &logflags);
5393 if (error)
5394 goto error0;
5395 goto nodelete;
5396 } else {
5397 ASSERT(del.br_state == XFS_EXT_NORM);
5398 del.br_state = XFS_EXT_UNWRITTEN;
5399 error = xfs_bmap_add_extent_unwritten_real(tp,
5400 ip, &lastx, &cur, &del,
5401 firstblock, flist, &logflags);
5402 if (error)
5403 goto error0;
5404 goto nodelete;
5405 }
5406 }
5407 if (wasdel) {
5408 ASSERT(startblockval(del.br_startblock) > 0);
5409
5410 if (isrt) {
5411 xfs_filblks_t rtexts;
5412
5413 rtexts = XFS_FSB_TO_B(mp, del.br_blockcount);
5414 do_div(rtexts, mp->m_sb.sb_rextsize);
5415 xfs_mod_incore_sb(mp, XFS_SBS_FREXTENTS,
5416 (int64_t)rtexts, 0);
5417 (void)xfs_trans_reserve_quota_nblks(NULL,
5418 ip, -((long)del.br_blockcount), 0,
5419 XFS_QMOPT_RES_RTBLKS);
5420 } else {
5421 xfs_icsb_modify_counters(mp, XFS_SBS_FDBLOCKS,
5422 (int64_t)del.br_blockcount, 0);
5423 (void)xfs_trans_reserve_quota_nblks(NULL,
5424 ip, -((long)del.br_blockcount), 0,
5425 XFS_QMOPT_RES_REGBLKS);
5426 }
5427 ip->i_delayed_blks -= del.br_blockcount;
5428 if (cur)
5429 cur->bc_private.b.flags |=
5430 XFS_BTCUR_BPRV_WASDEL;
5431 } else if (cur)
5432 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
5445 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
5446 XFS_IFORK_NEXTENTS(ip, whichfork) >=
5447 XFS_IFORK_MAXEXT(ip, whichfork) &&
5448 del.br_startoff > got.br_startoff &&
5449 del.br_startoff + del.br_blockcount <
5450 got.br_startoff + got.br_blockcount) {
5451 error = XFS_ERROR(ENOSPC);
5452 goto error0;
5453 }
5454 error = xfs_bmap_del_extent(ip, tp, &lastx, flist, cur, &del,
5455 &tmp_logflags, whichfork);
5456 logflags |= tmp_logflags;
5457 if (error)
5458 goto error0;
5459 bno = del.br_startoff - 1;
5460nodelete:
5461
5462
5463
5464 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
5465 if (lastx >= 0) {
5466 ep = xfs_iext_get_ext(ifp, lastx);
5467 if (xfs_bmbt_get_startoff(ep) > bno) {
5468 if (--lastx >= 0)
5469 ep = xfs_iext_get_ext(ifp,
5470 lastx);
5471 }
5472 xfs_bmbt_get_all(ep, &got);
5473 }
5474 extno++;
5475 }
5476 }
5477 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
5478
5479
5480
5481
5482 if (xfs_bmap_needs_btree(ip, whichfork)) {
5483 ASSERT(cur == NULL);
5484 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
5485 &cur, 0, &tmp_logflags, whichfork);
5486 logflags |= tmp_logflags;
5487 if (error)
5488 goto error0;
5489 }
5490
5491
5492
5493 else if (xfs_bmap_wants_extents(ip, whichfork)) {
5494 ASSERT(cur != NULL);
5495 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
5496 whichfork);
5497 logflags |= tmp_logflags;
5498 if (error)
5499 goto error0;
5500 }
5501
5502
5503
5504 error = 0;
5505error0:
5506
5507
5508
5509
5510 if ((logflags & xfs_ilog_fext(whichfork)) &&
5511 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
5512 logflags &= ~xfs_ilog_fext(whichfork);
5513 else if ((logflags & xfs_ilog_fbroot(whichfork)) &&
5514 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
5515 logflags &= ~xfs_ilog_fbroot(whichfork);
5516
5517
5518
5519
5520 if (logflags)
5521 xfs_trans_log_inode(tp, ip, logflags);
5522 if (cur) {
5523 if (!error) {
5524 *firstblock = cur->bc_private.b.firstblock;
5525 cur->bc_private.b.allocated = 0;
5526 }
5527 xfs_btree_del_cursor(cur,
5528 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
5529 }
5530 return error;
5531}
5532
5533
5534
5535
5536STATIC int
5537xfs_getbmapx_fix_eof_hole(
5538 xfs_inode_t *ip,
5539 struct getbmapx *out,
5540 int prealloced,
5541
5542 __int64_t end,
5543 xfs_fsblock_t startblock)
5544{
5545 __int64_t fixlen;
5546 xfs_mount_t *mp;
5547 xfs_ifork_t *ifp;
5548 xfs_extnum_t lastx;
5549 xfs_fileoff_t fileblock;
5550
5551 if (startblock == HOLESTARTBLOCK) {
5552 mp = ip->i_mount;
5553 out->bmv_block = -1;
5554 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, XFS_ISIZE(ip)));
5555 fixlen -= out->bmv_offset;
5556 if (prealloced && out->bmv_offset + out->bmv_length == end) {
5557
5558 if (fixlen <= 0)
5559 return 0;
5560 out->bmv_length = fixlen;
5561 }
5562 } else {
5563 if (startblock == DELAYSTARTBLOCK)
5564 out->bmv_block = -2;
5565 else
5566 out->bmv_block = xfs_fsb_to_db(ip, startblock);
5567 fileblock = XFS_BB_TO_FSB(ip->i_mount, out->bmv_offset);
5568 ifp = XFS_IFORK_PTR(ip, XFS_DATA_FORK);
5569 if (xfs_iext_bno_to_ext(ifp, fileblock, &lastx) &&
5570 (lastx == (ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))-1))
5571 out->bmv_oflags |= BMV_OF_LAST;
5572 }
5573
5574 return 1;
5575}
5576
5577
5578
5579
5580
5581
5582
5583
5584int
5585xfs_getbmap(
5586 xfs_inode_t *ip,
5587 struct getbmapx *bmv,
5588 xfs_bmap_format_t formatter,
5589 void *arg)
5590{
5591 __int64_t bmvend;
5592 int error = 0;
5593 __int64_t fixlen;
5594 int i;
5595 int lock;
5596 xfs_bmbt_irec_t *map;
5597 xfs_mount_t *mp;
5598 int nex;
5599 int nexleft;
5600 int subnex;
5601 int nmap;
5602 struct getbmapx *out;
5603 int whichfork;
5604 int prealloced;
5605
5606 int iflags;
5607 int bmapi_flags;
5608 int cur_ext = 0;
5609
5610 mp = ip->i_mount;
5611 iflags = bmv->bmv_iflags;
5612 whichfork = iflags & BMV_IF_ATTRFORK ? XFS_ATTR_FORK : XFS_DATA_FORK;
5613
5614 if (whichfork == XFS_ATTR_FORK) {
5615 if (XFS_IFORK_Q(ip)) {
5616 if (ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS &&
5617 ip->i_d.di_aformat != XFS_DINODE_FMT_BTREE &&
5618 ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
5619 return XFS_ERROR(EINVAL);
5620 } else if (unlikely(
5621 ip->i_d.di_aformat != 0 &&
5622 ip->i_d.di_aformat != XFS_DINODE_FMT_EXTENTS)) {
5623 XFS_ERROR_REPORT("xfs_getbmap", XFS_ERRLEVEL_LOW,
5624 ip->i_mount);
5625 return XFS_ERROR(EFSCORRUPTED);
5626 }
5627
5628 prealloced = 0;
5629 fixlen = 1LL << 32;
5630 } else {
5631 if (ip->i_d.di_format != XFS_DINODE_FMT_EXTENTS &&
5632 ip->i_d.di_format != XFS_DINODE_FMT_BTREE &&
5633 ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
5634 return XFS_ERROR(EINVAL);
5635
5636 if (xfs_get_extsz_hint(ip) ||
5637 ip->i_d.di_flags & (XFS_DIFLAG_PREALLOC|XFS_DIFLAG_APPEND)){
5638 prealloced = 1;
5639 fixlen = mp->m_super->s_maxbytes;
5640 } else {
5641 prealloced = 0;
5642 fixlen = XFS_ISIZE(ip);
5643 }
5644 }
5645
5646 if (bmv->bmv_length == -1) {
5647 fixlen = XFS_FSB_TO_BB(mp, XFS_B_TO_FSB(mp, fixlen));
5648 bmv->bmv_length =
5649 max_t(__int64_t, fixlen - bmv->bmv_offset, 0);
5650 } else if (bmv->bmv_length == 0) {
5651 bmv->bmv_entries = 0;
5652 return 0;
5653 } else if (bmv->bmv_length < 0) {
5654 return XFS_ERROR(EINVAL);
5655 }
5656
5657 nex = bmv->bmv_count - 1;
5658 if (nex <= 0)
5659 return XFS_ERROR(EINVAL);
5660 bmvend = bmv->bmv_offset + bmv->bmv_length;
5661
5662
5663 if (bmv->bmv_count > ULONG_MAX / sizeof(struct getbmapx))
5664 return XFS_ERROR(ENOMEM);
5665 out = kmem_zalloc(bmv->bmv_count * sizeof(struct getbmapx), KM_MAYFAIL);
5666 if (!out) {
5667 out = kmem_zalloc_large(bmv->bmv_count *
5668 sizeof(struct getbmapx));
5669 if (!out)
5670 return XFS_ERROR(ENOMEM);
5671 }
5672
5673 xfs_ilock(ip, XFS_IOLOCK_SHARED);
5674 if (whichfork == XFS_DATA_FORK && !(iflags & BMV_IF_DELALLOC)) {
5675 if (ip->i_delayed_blks || XFS_ISIZE(ip) > ip->i_d.di_size) {
5676 error = -filemap_write_and_wait(VFS_I(ip)->i_mapping);
5677 if (error)
5678 goto out_unlock_iolock;
5679 }
5680
5681
5682
5683
5684
5685
5686
5687 }
5688
5689 lock = xfs_ilock_map_shared(ip);
5690
5691
5692
5693
5694
5695 if (nex > XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1)
5696 nex = XFS_IFORK_NEXTENTS(ip, whichfork) * 2 + 1;
5697
5698 bmapi_flags = xfs_bmapi_aflag(whichfork);
5699 if (!(iflags & BMV_IF_PREALLOC))
5700 bmapi_flags |= XFS_BMAPI_IGSTATE;
5701
5702
5703
5704
5705 error = ENOMEM;
5706 subnex = 16;
5707 map = kmem_alloc(subnex * sizeof(*map), KM_MAYFAIL | KM_NOFS);
5708 if (!map)
5709 goto out_unlock_ilock;
5710
5711 bmv->bmv_entries = 0;
5712
5713 if (XFS_IFORK_NEXTENTS(ip, whichfork) == 0 &&
5714 (whichfork == XFS_ATTR_FORK || !(iflags & BMV_IF_DELALLOC))) {
5715 error = 0;
5716 goto out_free_map;
5717 }
5718
5719 nexleft = nex;
5720
5721 do {
5722 nmap = (nexleft > subnex) ? subnex : nexleft;
5723 error = xfs_bmapi_read(ip, XFS_BB_TO_FSBT(mp, bmv->bmv_offset),
5724 XFS_BB_TO_FSB(mp, bmv->bmv_length),
5725 map, &nmap, bmapi_flags);
5726 if (error)
5727 goto out_free_map;
5728 ASSERT(nmap <= subnex);
5729
5730 for (i = 0; i < nmap && nexleft && bmv->bmv_length; i++) {
5731 out[cur_ext].bmv_oflags = 0;
5732 if (map[i].br_state == XFS_EXT_UNWRITTEN)
5733 out[cur_ext].bmv_oflags |= BMV_OF_PREALLOC;
5734 else if (map[i].br_startblock == DELAYSTARTBLOCK)
5735 out[cur_ext].bmv_oflags |= BMV_OF_DELALLOC;
5736 out[cur_ext].bmv_offset =
5737 XFS_FSB_TO_BB(mp, map[i].br_startoff);
5738 out[cur_ext].bmv_length =
5739 XFS_FSB_TO_BB(mp, map[i].br_blockcount);
5740 out[cur_ext].bmv_unused1 = 0;
5741 out[cur_ext].bmv_unused2 = 0;
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752 if (map[i].br_startblock == DELAYSTARTBLOCK &&
5753 map[i].br_startoff <= XFS_B_TO_FSB(mp, XFS_ISIZE(ip)))
5754 ASSERT((iflags & BMV_IF_DELALLOC) != 0);
5755
5756 if (map[i].br_startblock == HOLESTARTBLOCK &&
5757 whichfork == XFS_ATTR_FORK) {
5758
5759 out[cur_ext].bmv_oflags |= BMV_OF_LAST;
5760 goto out_free_map;
5761 }
5762
5763 if (!xfs_getbmapx_fix_eof_hole(ip, &out[cur_ext],
5764 prealloced, bmvend,
5765 map[i].br_startblock))
5766 goto out_free_map;
5767
5768 bmv->bmv_offset =
5769 out[cur_ext].bmv_offset +
5770 out[cur_ext].bmv_length;
5771 bmv->bmv_length =
5772 max_t(__int64_t, 0, bmvend - bmv->bmv_offset);
5773
5774
5775
5776
5777
5778
5779 if ((iflags & BMV_IF_NO_HOLES) &&
5780 map[i].br_startblock == HOLESTARTBLOCK) {
5781 memset(&out[cur_ext], 0, sizeof(out[cur_ext]));
5782 continue;
5783 }
5784
5785 nexleft--;
5786 bmv->bmv_entries++;
5787 cur_ext++;
5788 }
5789 } while (nmap && nexleft && bmv->bmv_length);
5790
5791 out_free_map:
5792 kmem_free(map);
5793 out_unlock_ilock:
5794 xfs_iunlock_map_shared(ip, lock);
5795 out_unlock_iolock:
5796 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
5797
5798 for (i = 0; i < cur_ext; i++) {
5799 int full = 0;
5800
5801
5802 error = formatter(&arg, &out[i], &full);
5803 if (error || full)
5804 break;
5805 }
5806
5807 if (is_vmalloc_addr(out))
5808 kmem_free_large(out);
5809 else
5810 kmem_free(out);
5811 return error;
5812}
5813
5814#ifdef DEBUG
5815STATIC struct xfs_buf *
5816xfs_bmap_get_bp(
5817 struct xfs_btree_cur *cur,
5818 xfs_fsblock_t bno)
5819{
5820 struct xfs_log_item_desc *lidp;
5821 int i;
5822
5823 if (!cur)
5824 return NULL;
5825
5826 for (i = 0; i < XFS_BTREE_MAXLEVELS; i++) {
5827 if (!cur->bc_bufs[i])
5828 break;
5829 if (XFS_BUF_ADDR(cur->bc_bufs[i]) == bno)
5830 return cur->bc_bufs[i];
5831 }
5832
5833
5834 list_for_each_entry(lidp, &cur->bc_tp->t_items, lid_trans) {
5835 struct xfs_buf_log_item *bip;
5836 bip = (struct xfs_buf_log_item *)lidp->lid_item;
5837 if (bip->bli_item.li_type == XFS_LI_BUF &&
5838 XFS_BUF_ADDR(bip->bli_buf) == bno)
5839 return bip->bli_buf;
5840 }
5841
5842 return NULL;
5843}
5844
5845STATIC void
5846xfs_check_block(
5847 struct xfs_btree_block *block,
5848 xfs_mount_t *mp,
5849 int root,
5850 short sz)
5851{
5852 int i, j, dmxr;
5853 __be64 *pp, *thispa;
5854 xfs_bmbt_key_t *prevp, *keyp;
5855
5856 ASSERT(be16_to_cpu(block->bb_level) > 0);
5857
5858 prevp = NULL;
5859 for( i = 1; i <= xfs_btree_get_numrecs(block); i++) {
5860 dmxr = mp->m_bmap_dmxr[0];
5861 keyp = XFS_BMBT_KEY_ADDR(mp, block, i);
5862
5863 if (prevp) {
5864 ASSERT(be64_to_cpu(prevp->br_startoff) <
5865 be64_to_cpu(keyp->br_startoff));
5866 }
5867 prevp = keyp;
5868
5869
5870
5871
5872 if (root)
5873 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, i, sz);
5874 else
5875 pp = XFS_BMBT_PTR_ADDR(mp, block, i, dmxr);
5876
5877 for (j = i+1; j <= be16_to_cpu(block->bb_numrecs); j++) {
5878 if (root)
5879 thispa = XFS_BMAP_BROOT_PTR_ADDR(mp, block, j, sz);
5880 else
5881 thispa = XFS_BMBT_PTR_ADDR(mp, block, j, dmxr);
5882 if (*thispa == *pp) {
5883 xfs_warn(mp, "%s: thispa(%d) == pp(%d) %Ld",
5884 __func__, j, i,
5885 (unsigned long long)be64_to_cpu(*thispa));
5886 panic("%s: ptrs are equal in node\n",
5887 __func__);
5888 }
5889 }
5890 }
5891}
5892
5893
5894
5895
5896
5897
5898STATIC void
5899xfs_bmap_check_leaf_extents(
5900 xfs_btree_cur_t *cur,
5901 xfs_inode_t *ip,
5902 int whichfork)
5903{
5904 struct xfs_btree_block *block;
5905 xfs_fsblock_t bno;
5906 xfs_buf_t *bp;
5907 int error;
5908 xfs_extnum_t i=0, j;
5909 xfs_ifork_t *ifp;
5910 int level;
5911 xfs_mount_t *mp;
5912 __be64 *pp;
5913 xfs_bmbt_rec_t *ep;
5914 xfs_bmbt_rec_t last = {0, 0};
5915 xfs_bmbt_rec_t *nextp;
5916 int bp_release = 0;
5917
5918 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE) {
5919 return;
5920 }
5921
5922 bno = NULLFSBLOCK;
5923 mp = ip->i_mount;
5924 ifp = XFS_IFORK_PTR(ip, whichfork);
5925 block = ifp->if_broot;
5926
5927
5928
5929 level = be16_to_cpu(block->bb_level);
5930 ASSERT(level > 0);
5931 xfs_check_block(block, mp, 1, ifp->if_broot_bytes);
5932 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
5933 bno = be64_to_cpu(*pp);
5934
5935 ASSERT(bno != NULLDFSBNO);
5936 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
5937 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
5938
5939
5940
5941
5942
5943 while (level-- > 0) {
5944
5945 bp_release = 0;
5946 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
5947 if (!bp) {
5948 bp_release = 1;
5949 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
5950 XFS_BMAP_BTREE_REF,
5951 &xfs_bmbt_buf_ops);
5952 if (error)
5953 goto error_norelse;
5954 }
5955 block = XFS_BUF_TO_BLOCK(bp);
5956 XFS_WANT_CORRUPTED_GOTO(
5957 xfs_bmap_sanity_check(mp, bp, level),
5958 error0);
5959 if (level == 0)
5960 break;
5961
5962
5963
5964
5965
5966
5967 xfs_check_block(block, mp, 0, 0);
5968 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
5969 bno = be64_to_cpu(*pp);
5970 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, bno), error0);
5971 if (bp_release) {
5972 bp_release = 0;
5973 xfs_trans_brelse(NULL, bp);
5974 }
5975 }
5976
5977
5978
5979
5980 i = 0;
5981
5982
5983
5984
5985 for (;;) {
5986 xfs_fsblock_t nextbno;
5987 xfs_extnum_t num_recs;
5988
5989
5990 num_recs = xfs_btree_get_numrecs(block);
5991
5992
5993
5994
5995
5996 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
5997
5998
5999
6000
6001
6002
6003
6004 ep = XFS_BMBT_REC_ADDR(mp, block, 1);
6005 if (i) {
6006 ASSERT(xfs_bmbt_disk_get_startoff(&last) +
6007 xfs_bmbt_disk_get_blockcount(&last) <=
6008 xfs_bmbt_disk_get_startoff(ep));
6009 }
6010 for (j = 1; j < num_recs; j++) {
6011 nextp = XFS_BMBT_REC_ADDR(mp, block, j + 1);
6012 ASSERT(xfs_bmbt_disk_get_startoff(ep) +
6013 xfs_bmbt_disk_get_blockcount(ep) <=
6014 xfs_bmbt_disk_get_startoff(nextp));
6015 ep = nextp;
6016 }
6017
6018 last = *ep;
6019 i += num_recs;
6020 if (bp_release) {
6021 bp_release = 0;
6022 xfs_trans_brelse(NULL, bp);
6023 }
6024 bno = nextbno;
6025
6026
6027
6028 if (bno == NULLFSBLOCK)
6029 break;
6030
6031 bp_release = 0;
6032 bp = xfs_bmap_get_bp(cur, XFS_FSB_TO_DADDR(mp, bno));
6033 if (!bp) {
6034 bp_release = 1;
6035 error = xfs_btree_read_bufl(mp, NULL, bno, 0, &bp,
6036 XFS_BMAP_BTREE_REF,
6037 &xfs_bmbt_buf_ops);
6038 if (error)
6039 goto error_norelse;
6040 }
6041 block = XFS_BUF_TO_BLOCK(bp);
6042 }
6043 if (bp_release) {
6044 bp_release = 0;
6045 xfs_trans_brelse(NULL, bp);
6046 }
6047 return;
6048
6049error0:
6050 xfs_warn(mp, "%s: at error0", __func__);
6051 if (bp_release)
6052 xfs_trans_brelse(NULL, bp);
6053error_norelse:
6054 xfs_warn(mp, "%s: BAD after btree leaves for %d extents",
6055 __func__, i);
6056 panic("%s: CORRUPTED BTREE OR SOMETHING", __func__);
6057 return;
6058}
6059#endif
6060
6061
6062
6063
6064int
6065xfs_bmap_count_blocks(
6066 xfs_trans_t *tp,
6067 xfs_inode_t *ip,
6068 int whichfork,
6069 int *count)
6070{
6071 struct xfs_btree_block *block;
6072 xfs_fsblock_t bno;
6073 xfs_ifork_t *ifp;
6074 int level;
6075 xfs_mount_t *mp;
6076 __be64 *pp;
6077
6078 bno = NULLFSBLOCK;
6079 mp = ip->i_mount;
6080 ifp = XFS_IFORK_PTR(ip, whichfork);
6081 if ( XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ) {
6082 xfs_bmap_count_leaves(ifp, 0,
6083 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t),
6084 count);
6085 return 0;
6086 }
6087
6088
6089
6090
6091 block = ifp->if_broot;
6092 level = be16_to_cpu(block->bb_level);
6093 ASSERT(level > 0);
6094 pp = XFS_BMAP_BROOT_PTR_ADDR(mp, block, 1, ifp->if_broot_bytes);
6095 bno = be64_to_cpu(*pp);
6096 ASSERT(bno != NULLDFSBNO);
6097 ASSERT(XFS_FSB_TO_AGNO(mp, bno) < mp->m_sb.sb_agcount);
6098 ASSERT(XFS_FSB_TO_AGBNO(mp, bno) < mp->m_sb.sb_agblocks);
6099
6100 if (unlikely(xfs_bmap_count_tree(mp, tp, ifp, bno, level, count) < 0)) {
6101 XFS_ERROR_REPORT("xfs_bmap_count_blocks(2)", XFS_ERRLEVEL_LOW,
6102 mp);
6103 return XFS_ERROR(EFSCORRUPTED);
6104 }
6105
6106 return 0;
6107}
6108
6109
6110
6111
6112
6113STATIC int
6114xfs_bmap_count_tree(
6115 xfs_mount_t *mp,
6116 xfs_trans_t *tp,
6117 xfs_ifork_t *ifp,
6118 xfs_fsblock_t blockno,
6119 int levelin,
6120 int *count)
6121{
6122 int error;
6123 xfs_buf_t *bp, *nbp;
6124 int level = levelin;
6125 __be64 *pp;
6126 xfs_fsblock_t bno = blockno;
6127 xfs_fsblock_t nextbno;
6128 struct xfs_btree_block *block, *nextblock;
6129 int numrecs;
6130
6131 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp, XFS_BMAP_BTREE_REF,
6132 &xfs_bmbt_buf_ops);
6133 if (error)
6134 return error;
6135 *count += 1;
6136 block = XFS_BUF_TO_BLOCK(bp);
6137
6138 if (--level) {
6139
6140 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6141 while (nextbno != NULLFSBLOCK) {
6142 error = xfs_btree_read_bufl(mp, tp, nextbno, 0, &nbp,
6143 XFS_BMAP_BTREE_REF,
6144 &xfs_bmbt_buf_ops);
6145 if (error)
6146 return error;
6147 *count += 1;
6148 nextblock = XFS_BUF_TO_BLOCK(nbp);
6149 nextbno = be64_to_cpu(nextblock->bb_u.l.bb_rightsib);
6150 xfs_trans_brelse(tp, nbp);
6151 }
6152
6153
6154 pp = XFS_BMBT_PTR_ADDR(mp, block, 1, mp->m_bmap_dmxr[1]);
6155 bno = be64_to_cpu(*pp);
6156 if (unlikely((error =
6157 xfs_bmap_count_tree(mp, tp, ifp, bno, level, count)) < 0)) {
6158 xfs_trans_brelse(tp, bp);
6159 XFS_ERROR_REPORT("xfs_bmap_count_tree(1)",
6160 XFS_ERRLEVEL_LOW, mp);
6161 return XFS_ERROR(EFSCORRUPTED);
6162 }
6163 xfs_trans_brelse(tp, bp);
6164 } else {
6165
6166 for (;;) {
6167 nextbno = be64_to_cpu(block->bb_u.l.bb_rightsib);
6168 numrecs = be16_to_cpu(block->bb_numrecs);
6169 xfs_bmap_disk_count_leaves(mp, block, numrecs, count);
6170 xfs_trans_brelse(tp, bp);
6171 if (nextbno == NULLFSBLOCK)
6172 break;
6173 bno = nextbno;
6174 error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
6175 XFS_BMAP_BTREE_REF,
6176 &xfs_bmbt_buf_ops);
6177 if (error)
6178 return error;
6179 *count += 1;
6180 block = XFS_BUF_TO_BLOCK(bp);
6181 }
6182 }
6183 return 0;
6184}
6185
6186
6187
6188
6189STATIC void
6190xfs_bmap_count_leaves(
6191 xfs_ifork_t *ifp,
6192 xfs_extnum_t idx,
6193 int numrecs,
6194 int *count)
6195{
6196 int b;
6197
6198 for (b = 0; b < numrecs; b++) {
6199 xfs_bmbt_rec_host_t *frp = xfs_iext_get_ext(ifp, idx + b);
6200 *count += xfs_bmbt_get_blockcount(frp);
6201 }
6202}
6203
6204
6205
6206
6207
6208STATIC void
6209xfs_bmap_disk_count_leaves(
6210 struct xfs_mount *mp,
6211 struct xfs_btree_block *block,
6212 int numrecs,
6213 int *count)
6214{
6215 int b;
6216 xfs_bmbt_rec_t *frp;
6217
6218 for (b = 1; b <= numrecs; b++) {
6219 frp = XFS_BMBT_REC_ADDR(mp, block, b);
6220 *count += xfs_bmbt_disk_get_blockcount(frp);
6221 }
6222}
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232int
6233xfs_bmap_punch_delalloc_range(
6234 struct xfs_inode *ip,
6235 xfs_fileoff_t start_fsb,
6236 xfs_fileoff_t length)
6237{
6238 xfs_fileoff_t remaining = length;
6239 int error = 0;
6240
6241 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
6242
6243 do {
6244 int done;
6245 xfs_bmbt_irec_t imap;
6246 int nimaps = 1;
6247 xfs_fsblock_t firstblock;
6248 xfs_bmap_free_t flist;
6249
6250
6251
6252
6253
6254
6255
6256 error = xfs_bmapi_read(ip, start_fsb, 1, &imap, &nimaps,
6257 XFS_BMAPI_ENTIRE);
6258
6259 if (error) {
6260
6261 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
6262 xfs_alert(ip->i_mount,
6263 "Failed delalloc mapping lookup ino %lld fsb %lld.",
6264 ip->i_ino, start_fsb);
6265 }
6266 break;
6267 }
6268 if (!nimaps) {
6269
6270 goto next_block;
6271 }
6272 if (imap.br_startblock != DELAYSTARTBLOCK) {
6273
6274 goto next_block;
6275 }
6276 WARN_ON(imap.br_blockcount == 0);
6277
6278
6279
6280
6281
6282
6283
6284 xfs_bmap_init(&flist, &firstblock);
6285 error = xfs_bunmapi(NULL, ip, start_fsb, 1, 0, 1, &firstblock,
6286 &flist, &done);
6287 if (error)
6288 break;
6289
6290 ASSERT(!flist.xbf_count && !flist.xbf_first);
6291next_block:
6292 start_fsb++;
6293 remaining--;
6294 } while(remaining > 0);
6295
6296 return error;
6297}
6298
6299
6300
6301
6302
6303
6304xfs_daddr_t
6305xfs_fsb_to_db(struct xfs_inode *ip, xfs_fsblock_t fsb)
6306{
6307 return (XFS_IS_REALTIME_INODE(ip) ? \
6308 (xfs_daddr_t)XFS_FSB_TO_BB((ip)->i_mount, (fsb)) : \
6309 XFS_FSB_TO_DADDR((ip)->i_mount, (fsb)));
6310}
6311