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