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