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