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