]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_bmap.c
cmd/xfs/bmap/Makefile 1.8 Renamed to cmd/xfsprogs/bmap/Makefile
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_bmap.c
CommitLineData
2bd0ea18
NS
1/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33#include <xfs.h>
34
35xfs_zone_t *xfs_bmap_free_item_zone;
36
37/*
38 * Called by xfs_bmapi to update extent list structure and the btree
39 * after allocating space (or doing a delayed allocation).
40 */
41STATIC int /* error */
42xfs_bmap_add_extent(
43 xfs_inode_t *ip, /* incore inode pointer */
44 xfs_extnum_t idx, /* extent number to update/insert */
45 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
46 xfs_bmbt_irec_t *new, /* new data to put in extent list */
47 xfs_fsblock_t *first, /* pointer to firstblock variable */
48 xfs_bmap_free_t *flist, /* list of extents to be freed */
49 int *logflagsp, /* inode logging flags */
50 int whichfork, /* data or attr fork */
51 int rsvd) /* OK to use reserved data blocks */
52{
53 xfs_btree_cur_t *cur; /* btree cursor or null */
54 xfs_filblks_t da_new; /* new count del alloc blocks used */
55 xfs_filblks_t da_old; /* old count del alloc blocks used */
56 int error; /* error return value */
57#ifdef XFS_BMAP_TRACE
58 static char fname[] = "xfs_bmap_add_extent";
59#endif
60 xfs_ifork_t *ifp; /* inode fork ptr */
61 int logflags; /* returned value */
62 xfs_extnum_t nextents; /* number of extents in file now */
63
64 XFS_STATS_INC(xs_add_exlist);
65 cur = *curp;
66 ifp = XFS_IFORK_PTR(ip, whichfork);
67 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
68 ASSERT(idx <= nextents);
69 da_old = da_new = 0;
70 error = 0;
71 /*
72 * This is the first extent added to a new/empty file.
73 * Special case this one, so other routines get to assume there are
74 * already extents in the list.
75 */
76 if (nextents == 0) {
77 xfs_bmap_trace_insert(fname, "insert empty", ip, 0, 1, new,
78 NULL, whichfork);
79 xfs_bmap_insert_exlist(ip, 0, 1, new, whichfork);
80 ASSERT(cur == NULL);
81 ifp->if_lastex = 0;
82 if (!ISNULLSTARTBLOCK(new->br_startblock)) {
83 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
84 logflags = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
85 } else
86 logflags = 0;
87 }
88 /*
89 * Any kind of new delayed allocation goes here.
90 */
91 else if (ISNULLSTARTBLOCK(new->br_startblock)) {
92 if (cur)
93 ASSERT((cur->bc_private.b.flags &
94 XFS_BTCUR_BPRV_WASDEL) == 0);
95 if (error = xfs_bmap_add_extent_hole_delay(ip, idx, cur, new,
96 &logflags, rsvd))
97 goto done;
98 }
99 /*
100 * Real allocation off the end of the file.
101 */
102 else if (idx == nextents) {
103 if (cur)
104 ASSERT((cur->bc_private.b.flags &
105 XFS_BTCUR_BPRV_WASDEL) == 0);
106 if (error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
107 &logflags, whichfork))
108 goto done;
109 } else {
110 xfs_bmbt_irec_t prev; /* old extent at offset idx */
111
112 /*
113 * Get the record referred to by idx.
114 */
115 xfs_bmbt_get_all(&ifp->if_u1.if_extents[idx], &prev);
116 /*
117 * If it's a real allocation record, and the new allocation ends
118 * after the start of the referred to record, then we're filling
119 * in a delayed or unwritten allocation with a real one, or
120 * converting real back to unwritten.
121 */
122 if (!ISNULLSTARTBLOCK(new->br_startblock) &&
123 new->br_startoff + new->br_blockcount > prev.br_startoff) {
124 if (prev.br_state != XFS_EXT_UNWRITTEN &&
125 ISNULLSTARTBLOCK(prev.br_startblock)) {
126 da_old = STARTBLOCKVAL(prev.br_startblock);
127 if (cur)
128 ASSERT(cur->bc_private.b.flags &
129 XFS_BTCUR_BPRV_WASDEL);
130 if (error = xfs_bmap_add_extent_delay_real(ip,
131 idx, &cur, new, &da_new, first, flist,
132 &logflags, rsvd))
133 goto done;
134 } else if (new->br_state == XFS_EXT_NORM) {
135 ASSERT(new->br_state == XFS_EXT_NORM);
136 if (error = xfs_bmap_add_extent_unwritten_real(
137 ip, idx, &cur, new, &logflags))
138 goto done;
139 } else {
140 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
141 if (error = xfs_bmap_add_extent_unwritten_real(
142 ip, idx, &cur, new, &logflags))
143 goto done;
144 }
145 ASSERT(*curp == cur || *curp == NULL);
146 }
147 /*
148 * Otherwise we're filling in a hole with an allocation.
149 */
150 else {
151 if (cur)
152 ASSERT((cur->bc_private.b.flags &
153 XFS_BTCUR_BPRV_WASDEL) == 0);
154 if (error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
155 new, &logflags, whichfork))
156 goto done;
157 }
158 }
159
160 ASSERT(*curp == cur || *curp == NULL);
161 /*
162 * Convert to a btree if necessary.
163 */
164 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
165 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
166 int tmp_logflags; /* partial log flag return val */
167
168 ASSERT(cur == NULL);
169 error = xfs_bmap_extents_to_btree(ip->i_transp, ip, first,
170 flist, &cur, da_old > 0, &tmp_logflags, whichfork);
171 logflags |= tmp_logflags;
172 if (error)
173 goto done;
174 }
175 /*
176 * Adjust for changes in reserved delayed indirect blocks.
177 * Nothing to do for disk quotas here.
178 */
179 if (da_old || da_new) {
180 xfs_filblks_t nblks;
181
182 nblks = da_new;
183 if (cur)
184 nblks += cur->bc_private.b.allocated;
185 ASSERT(nblks <= da_old);
186 if (nblks < da_old)
187 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
188 (int)(da_old - nblks), rsvd);
189 }
190 /*
191 * Clear out the allocated field, done with it now in any case.
192 */
193 if (cur) {
194 cur->bc_private.b.allocated = 0;
195 *curp = cur;
196 }
197done:
198#ifdef XFSDEBUG
199 if (!error)
200 xfs_bmap_check_leaf_extents(*curp, ip, whichfork);
201#endif
202 *logflagsp = logflags;
203 return error;
204}
205
206/*
207 * Called by xfs_bmap_add_extent to handle cases converting a delayed
208 * allocation to a real allocation.
209 */
210STATIC int /* error */
211xfs_bmap_add_extent_delay_real(
212 xfs_inode_t *ip, /* incore inode pointer */
213 xfs_extnum_t idx, /* extent number to update/insert */
214 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
215 xfs_bmbt_irec_t *new, /* new data to put in extent list */
216 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
217 xfs_fsblock_t *first, /* pointer to firstblock variable */
218 xfs_bmap_free_t *flist, /* list of extents to be freed */
219 int *logflagsp, /* inode logging flags */
220 int rsvd) /* OK to use reserved data block allocation */
221{
222 xfs_bmbt_rec_t *base; /* base of extent entry list */
223 xfs_btree_cur_t *cur; /* btree cursor */
224 int diff; /* temp value */
225 xfs_bmbt_rec_t *ep; /* extent entry for idx */
226 int error; /* error return value */
227#ifdef XFS_BMAP_TRACE
228 static char fname[] = "xfs_bmap_add_extent_delay_real";
229#endif
230 int i; /* temp state */
231 xfs_fileoff_t new_endoff; /* end offset of new entry */
232 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
233 /* left is 0, right is 1, prev is 2 */
234 int rval; /* return value (logging flags) */
235 int state = 0;/* state bits, accessed thru macros */
236 xfs_filblks_t temp; /* value for dnew calculations */
237 xfs_filblks_t temp2; /* value for dnew calculations */
238 int tmp_rval; /* partial logging flags */
239 enum { /* bit number definitions for state */
240 LEFT_CONTIG, RIGHT_CONTIG,
241 LEFT_FILLING, RIGHT_FILLING,
242 LEFT_DELAY, RIGHT_DELAY,
243 LEFT_VALID, RIGHT_VALID
244 };
245
246#define LEFT r[0]
247#define RIGHT r[1]
248#define PREV r[2]
249#define MASK(b) (1 << (b))
250#define MASK2(a,b) (MASK(a) | MASK(b))
251#define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
252#define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
253#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
254#define STATE_TEST(b) (state & MASK(b))
255#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
256 ((state &= ~MASK(b)), 0))
257#define SWITCH_STATE \
258 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
259
260 /*
261 * Set up a bunch of variables to make the tests simpler.
262 */
263 cur = *curp;
264 base = ip->i_df.if_u1.if_extents;
265 ep = &base[idx];
266 xfs_bmbt_get_all(ep, &PREV);
267 new_endoff = new->br_startoff + new->br_blockcount;
268 ASSERT(PREV.br_startoff <= new->br_startoff);
269 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
270 /*
271 * Set flags determining what part of the previous delayed allocation
272 * extent is being replaced by a real allocation.
273 */
274 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
275 STATE_SET(RIGHT_FILLING,
276 PREV.br_startoff + PREV.br_blockcount == new_endoff);
277 /*
278 * Check and set flags if this segment has a left neighbor.
279 * Don't set contiguous if the combined extent would be too large.
280 */
281 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
282 xfs_bmbt_get_all(ep - 1, &LEFT);
283 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
284 }
285 STATE_SET(LEFT_CONTIG,
286 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
287 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
288 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
289 LEFT.br_state == new->br_state &&
290 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
291 /*
292 * Check and set flags if this segment has a right neighbor.
293 * Don't set contiguous if the combined extent would be too large.
294 * Also check for all-three-contiguous being too large.
295 */
296 if (STATE_SET_TEST(RIGHT_VALID,
297 idx <
298 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
299 xfs_bmbt_get_all(ep + 1, &RIGHT);
300 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
301 }
302 STATE_SET(RIGHT_CONTIG,
303 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
304 new_endoff == RIGHT.br_startoff &&
305 new->br_startblock + new->br_blockcount ==
306 RIGHT.br_startblock &&
307 new->br_state == RIGHT.br_state &&
308 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
309 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
310 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
311 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
312 <= MAXEXTLEN));
313 error = 0;
314 /*
315 * Switch out based on the FILLING and CONTIG state bits.
316 */
317 switch (SWITCH_STATE) {
318
319 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
320 /*
321 * Filling in all of a previously delayed allocation extent.
322 * The left and right neighbors are both contiguous with new.
323 */
324 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
325 XFS_DATA_FORK);
326 xfs_bmbt_set_blockcount(ep - 1,
327 LEFT.br_blockcount + PREV.br_blockcount +
328 RIGHT.br_blockcount);
329 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
330 XFS_DATA_FORK);
331 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
332 XFS_DATA_FORK);
333 xfs_bmap_delete_exlist(ip, idx, 2, XFS_DATA_FORK);
334 ip->i_df.if_lastex = idx - 1;
335 ip->i_d.di_nextents--;
336 if (cur == NULL)
337 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
338 else {
339 rval = XFS_ILOG_CORE;
340 if (error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
341 RIGHT.br_startblock,
342 RIGHT.br_blockcount, &i))
343 goto done;
344 ASSERT(i == 1);
345 if (error = xfs_bmbt_delete(cur, 0, &i))
346 goto done;
347 ASSERT(i == 1);
348 if (error = xfs_bmbt_decrement(cur, 0, &i))
349 goto done;
350 ASSERT(i == 1);
351 if (error = xfs_bmbt_update(cur, LEFT.br_startoff,
352 LEFT.br_startblock,
353 LEFT.br_blockcount +
354 PREV.br_blockcount +
355 RIGHT.br_blockcount, LEFT.br_state))
356 goto done;
357 }
358 *dnew = 0;
359 break;
360
361 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
362 /*
363 * Filling in all of a previously delayed allocation extent.
364 * The left neighbor is contiguous, the right is not.
365 */
366 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
367 XFS_DATA_FORK);
368 xfs_bmbt_set_blockcount(ep - 1,
369 LEFT.br_blockcount + PREV.br_blockcount);
370 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
371 XFS_DATA_FORK);
372 ip->i_df.if_lastex = idx - 1;
373 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
374 XFS_DATA_FORK);
375 xfs_bmap_delete_exlist(ip, idx, 1, XFS_DATA_FORK);
376 if (cur == NULL)
377 rval = XFS_ILOG_DEXT;
378 else {
379 rval = 0;
380 if (error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
381 LEFT.br_startblock, LEFT.br_blockcount,
382 &i))
383 goto done;
384 ASSERT(i == 1);
385 if (error = xfs_bmbt_update(cur, LEFT.br_startoff,
386 LEFT.br_startblock,
387 LEFT.br_blockcount +
388 PREV.br_blockcount, LEFT.br_state))
389 goto done;
390 }
391 *dnew = 0;
392 break;
393
394 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
395 /*
396 * Filling in all of a previously delayed allocation extent.
397 * The right neighbor is contiguous, the left is not.
398 */
399 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
400 XFS_DATA_FORK);
401 xfs_bmbt_set_startblock(ep, new->br_startblock);
402 xfs_bmbt_set_blockcount(ep,
403 PREV.br_blockcount + RIGHT.br_blockcount);
404 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
405 XFS_DATA_FORK);
406 ip->i_df.if_lastex = idx;
407 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
408 XFS_DATA_FORK);
409 xfs_bmap_delete_exlist(ip, idx + 1, 1, XFS_DATA_FORK);
410 if (cur == NULL)
411 rval = XFS_ILOG_DEXT;
412 else {
413 rval = 0;
414 if (error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
415 RIGHT.br_startblock,
416 RIGHT.br_blockcount, &i))
417 goto done;
418 ASSERT(i == 1);
419 if (error = xfs_bmbt_update(cur, PREV.br_startoff,
420 new->br_startblock,
421 PREV.br_blockcount +
422 RIGHT.br_blockcount, PREV.br_state))
423 goto done;
424 }
425 *dnew = 0;
426 break;
427
428 case MASK2(LEFT_FILLING, RIGHT_FILLING):
429 /*
430 * Filling in all of a previously delayed allocation extent.
431 * Neither the left nor right neighbors are contiguous with
432 * the new one.
433 */
434 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
435 XFS_DATA_FORK);
436 xfs_bmbt_set_startblock(ep, new->br_startblock);
437 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
438 XFS_DATA_FORK);
439 ip->i_df.if_lastex = idx;
440 ip->i_d.di_nextents++;
441 if (cur == NULL)
442 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
443 else {
444 rval = XFS_ILOG_CORE;
445 if (error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
446 new->br_startblock, new->br_blockcount,
447 &i))
448 goto done;
449 ASSERT(i == 0);
450 cur->bc_rec.b.br_state = XFS_EXT_NORM;
451 if (error = xfs_bmbt_insert(cur, &i))
452 goto done;
453 ASSERT(i == 1);
454 }
455 *dnew = 0;
456 break;
457
458 case MASK2(LEFT_FILLING, LEFT_CONTIG):
459 /*
460 * Filling in the first part of a previous delayed allocation.
461 * The left neighbor is contiguous.
462 */
463 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
464 XFS_DATA_FORK);
465 xfs_bmbt_set_blockcount(ep - 1,
466 LEFT.br_blockcount + new->br_blockcount);
467 xfs_bmbt_set_startoff(ep,
468 PREV.br_startoff + new->br_blockcount);
469 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
470 XFS_DATA_FORK);
471 temp = PREV.br_blockcount - new->br_blockcount;
472 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
473 XFS_DATA_FORK);
474 xfs_bmbt_set_blockcount(ep, temp);
475 ip->i_df.if_lastex = idx - 1;
476 if (cur == NULL)
477 rval = XFS_ILOG_DEXT;
478 else {
479 rval = 0;
480 if (error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
481 LEFT.br_startblock, LEFT.br_blockcount,
482 &i))
483 goto done;
484 ASSERT(i == 1);
485 if (error = xfs_bmbt_update(cur, LEFT.br_startoff,
486 LEFT.br_startblock,
487 LEFT.br_blockcount +
488 new->br_blockcount,
489 LEFT.br_state))
490 goto done;
491 }
492 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
493 STARTBLOCKVAL(PREV.br_startblock));
494 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
495 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
496 XFS_DATA_FORK);
497 *dnew = temp;
498 break;
499
500 case MASK(LEFT_FILLING):
501 /*
502 * Filling in the first part of a previous delayed allocation.
503 * The left neighbor is not contiguous.
504 */
505 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
506 xfs_bmbt_set_startoff(ep, new_endoff);
507 temp = PREV.br_blockcount - new->br_blockcount;
508 xfs_bmbt_set_blockcount(ep, temp);
509 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
510 XFS_DATA_FORK);
511 xfs_bmap_insert_exlist(ip, idx, 1, new, XFS_DATA_FORK);
512 ip->i_df.if_lastex = idx;
513 ip->i_d.di_nextents++;
514 if (cur == NULL)
515 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
516 else {
517 rval = XFS_ILOG_CORE;
518 if (error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
519 new->br_startblock, new->br_blockcount,
520 &i))
521 goto done;
522 ASSERT(i == 0);
523 cur->bc_rec.b.br_state = XFS_EXT_NORM;
524 if (error = xfs_bmbt_insert(cur, &i))
525 goto done;
526 ASSERT(i == 1);
527 }
528 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
529 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
530 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
531 first, flist, &cur, 1, &tmp_rval,
532 XFS_DATA_FORK);
533 rval |= tmp_rval;
534 if (error)
535 goto done;
536 }
537 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
538 STARTBLOCKVAL(PREV.br_startblock) -
539 (cur ? cur->bc_private.b.allocated : 0));
540 base = ip->i_df.if_u1.if_extents;
541 ep = &base[idx + 1];
542 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
543 xfs_bmap_trace_post_update(fname, "LF", ip, idx + 1,
544 XFS_DATA_FORK);
545 *dnew = temp;
546 break;
547
548 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
549 /*
550 * Filling in the last part of a previous delayed allocation.
551 * The right neighbor is contiguous with the new allocation.
552 */
553 temp = PREV.br_blockcount - new->br_blockcount;
554 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
555 XFS_DATA_FORK);
556 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
557 XFS_DATA_FORK);
558 xfs_bmbt_set_blockcount(ep, temp);
559 xfs_bmbt_set_allf(ep + 1, new->br_startoff, new->br_startblock,
560 new->br_blockcount + RIGHT.br_blockcount,
561 RIGHT.br_state);
562 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
563 XFS_DATA_FORK);
564 ip->i_df.if_lastex = idx + 1;
565 if (cur == NULL)
566 rval = XFS_ILOG_DEXT;
567 else {
568 rval = 0;
569 if (error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
570 RIGHT.br_startblock,
571 RIGHT.br_blockcount, &i))
572 goto done;
573 ASSERT(i == 1);
574 if (error = xfs_bmbt_update(cur, new->br_startoff,
575 new->br_startblock,
576 new->br_blockcount +
577 RIGHT.br_blockcount,
578 RIGHT.br_state))
579 goto done;
580 }
581 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
582 STARTBLOCKVAL(PREV.br_startblock));
583 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
584 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
585 XFS_DATA_FORK);
586 *dnew = temp;
587 break;
588
589 case MASK(RIGHT_FILLING):
590 /*
591 * Filling in the last part of a previous delayed allocation.
592 * The right neighbor is not contiguous.
593 */
594 temp = PREV.br_blockcount - new->br_blockcount;
595 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
596 xfs_bmbt_set_blockcount(ep, temp);
597 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
598 new, NULL, XFS_DATA_FORK);
599 xfs_bmap_insert_exlist(ip, idx + 1, 1, new, XFS_DATA_FORK);
600 ip->i_df.if_lastex = idx + 1;
601 ip->i_d.di_nextents++;
602 if (cur == NULL)
603 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
604 else {
605 rval = XFS_ILOG_CORE;
606 if (error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
607 new->br_startblock, new->br_blockcount,
608 &i))
609 goto done;
610 ASSERT(i == 0);
611 cur->bc_rec.b.br_state = XFS_EXT_NORM;
612 if (error = xfs_bmbt_insert(cur, &i))
613 goto done;
614 ASSERT(i == 1);
615 }
616 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
617 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
618 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
619 first, flist, &cur, 1, &tmp_rval,
620 XFS_DATA_FORK);
621 rval |= tmp_rval;
622 if (error)
623 goto done;
624 }
625 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
626 STARTBLOCKVAL(PREV.br_startblock) -
627 (cur ? cur->bc_private.b.allocated : 0));
628 base = ip->i_df.if_u1.if_extents;
629 ep = &base[idx];
630 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
631 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
632 *dnew = temp;
633 break;
634
635 case 0:
636 /*
637 * Filling in the middle part of a previous delayed allocation.
638 * Contiguity is impossible here.
639 * This case is avoided almost all the time.
640 */
641 temp = new->br_startoff - PREV.br_startoff;
642 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
643 xfs_bmbt_set_blockcount(ep, temp);
644 r[0] = *new;
645 r[1].br_startoff = new_endoff;
646 temp2 = PREV.br_startoff + PREV.br_blockcount - new_endoff;
647 r[1].br_blockcount = temp2;
648 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
649 XFS_DATA_FORK);
650 xfs_bmap_insert_exlist(ip, idx + 1, 2, &r[0], XFS_DATA_FORK);
651 ip->i_df.if_lastex = idx + 1;
652 ip->i_d.di_nextents++;
653 if (cur == NULL)
654 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
655 else {
656 rval = XFS_ILOG_CORE;
657 if (error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
658 new->br_startblock, new->br_blockcount,
659 &i))
660 goto done;
661 ASSERT(i == 0);
662 cur->bc_rec.b.br_state = XFS_EXT_NORM;
663 if (error = xfs_bmbt_insert(cur, &i))
664 goto done;
665 ASSERT(i == 1);
666 }
667 if (ip->i_d.di_format == XFS_DINODE_FMT_EXTENTS &&
668 ip->i_d.di_nextents > ip->i_df.if_ext_max) {
669 error = xfs_bmap_extents_to_btree(ip->i_transp, ip,
670 first, flist, &cur, 1, &tmp_rval,
671 XFS_DATA_FORK);
672 rval |= tmp_rval;
673 if (error)
674 goto done;
675 }
676 temp = xfs_bmap_worst_indlen(ip, temp);
677 temp2 = xfs_bmap_worst_indlen(ip, temp2);
678 diff = (int)(temp + temp2 - STARTBLOCKVAL(PREV.br_startblock) -
679 (cur ? cur->bc_private.b.allocated : 0));
680 if (diff > 0 &&
681 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS, -diff, rsvd)) {
682 /*
683 * Ick gross gag me with a spoon.
684 */
685 ASSERT(0); /* want to see if this ever happens! */
686 while (diff > 0) {
687 if (temp) {
688 temp--;
689 diff--;
690 if (!diff ||
691 !xfs_mod_incore_sb(ip->i_mount,
692 XFS_SBS_FDBLOCKS, -diff, rsvd))
693 break;
694 }
695 if (temp2) {
696 temp2--;
697 diff--;
698 if (!diff ||
699 !xfs_mod_incore_sb(ip->i_mount,
700 XFS_SBS_FDBLOCKS, -diff, rsvd))
701 break;
702 }
703 }
704 }
705 base = ip->i_df.if_u1.if_extents;
706 ep = &base[idx];
707 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
708 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
709 xfs_bmap_trace_pre_update(fname, "0", ip, idx + 2,
710 XFS_DATA_FORK);
711 xfs_bmbt_set_startblock(ep + 2, NULLSTARTBLOCK((int)temp2));
712 xfs_bmap_trace_post_update(fname, "0", ip, idx + 2,
713 XFS_DATA_FORK);
714 *dnew = temp + temp2;
715 break;
716
717 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
718 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
719 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
720 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
721 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
722 case MASK(LEFT_CONTIG):
723 case MASK(RIGHT_CONTIG):
724 /*
725 * These cases are all impossible.
726 */
727 ASSERT(0);
728 }
729 *curp = cur;
730done:
731 *logflagsp = rval;
732 return error;
733#undef LEFT
734#undef RIGHT
735#undef PREV
736#undef MASK
737#undef MASK2
738#undef MASK3
739#undef MASK4
740#undef STATE_SET
741#undef STATE_TEST
742#undef STATE_SET_TEST
743#undef SWITCH_STATE
744}
745
746/*
747 * Called by xfs_bmap_add_extent to handle cases converting an unwritten
748 * allocation to a real allocation or vice versa.
749 */
750STATIC int /* error */
751xfs_bmap_add_extent_unwritten_real(
752 xfs_inode_t *ip, /* incore inode pointer */
753 xfs_extnum_t idx, /* extent number to update/insert */
754 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
755 xfs_bmbt_irec_t *new, /* new data to put in extent list */
756 int *logflagsp) /* inode logging flags */
757{
758 xfs_bmbt_rec_t *base; /* base of extent entry list */
759 xfs_btree_cur_t *cur; /* btree cursor */
760 xfs_bmbt_rec_t *ep; /* extent entry for idx */
761 int error; /* error return value */
762#ifdef XFS_BMAP_TRACE
763 static char fname[] = "xfs_bmap_add_extent_unwritten_real";
764#endif
765 int i; /* temp state */
766 xfs_fileoff_t new_endoff; /* end offset of new entry */
767 xfs_exntst_t newext; /* new extent state */
768 xfs_exntst_t oldext; /* old extent state */
769 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
770 /* left is 0, right is 1, prev is 2 */
771 int rval; /* return value (logging flags) */
772 int state = 0;/* state bits, accessed thru macros */
773 enum { /* bit number definitions for state */
774 LEFT_CONTIG, RIGHT_CONTIG,
775 LEFT_FILLING, RIGHT_FILLING,
776 LEFT_DELAY, RIGHT_DELAY,
777 LEFT_VALID, RIGHT_VALID
778 };
779
780#define LEFT r[0]
781#define RIGHT r[1]
782#define PREV r[2]
783#define MASK(b) (1 << (b))
784#define MASK2(a,b) (MASK(a) | MASK(b))
785#define MASK3(a,b,c) (MASK2(a,b) | MASK(c))
786#define MASK4(a,b,c,d) (MASK3(a,b,c) | MASK(d))
787#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
788#define STATE_TEST(b) (state & MASK(b))
789#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
790 ((state &= ~MASK(b)), 0))
791#define SWITCH_STATE \
792 (state & MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG))
793
794 /*
795 * Set up a bunch of variables to make the tests simpler.
796 */
797 error = 0;
798 cur = *curp;
799 base = ip->i_df.if_u1.if_extents;
800 ep = &base[idx];
801 xfs_bmbt_get_all(ep, &PREV);
802 newext = new->br_state;
803 oldext = (newext == XFS_EXT_UNWRITTEN) ?
804 XFS_EXT_NORM : XFS_EXT_UNWRITTEN;
805 ASSERT(PREV.br_state == oldext);
806 new_endoff = new->br_startoff + new->br_blockcount;
807 ASSERT(PREV.br_startoff <= new->br_startoff);
808 ASSERT(PREV.br_startoff + PREV.br_blockcount >= new_endoff);
809 /*
810 * Set flags determining what part of the previous oldext allocation
811 * extent is being replaced by a newext allocation.
812 */
813 STATE_SET(LEFT_FILLING, PREV.br_startoff == new->br_startoff);
814 STATE_SET(RIGHT_FILLING,
815 PREV.br_startoff + PREV.br_blockcount == new_endoff);
816 /*
817 * Check and set flags if this segment has a left neighbor.
818 * Don't set contiguous if the combined extent would be too large.
819 */
820 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
821 xfs_bmbt_get_all(ep - 1, &LEFT);
822 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(LEFT.br_startblock));
823 }
824 STATE_SET(LEFT_CONTIG,
825 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
826 LEFT.br_startoff + LEFT.br_blockcount == new->br_startoff &&
827 LEFT.br_startblock + LEFT.br_blockcount == new->br_startblock &&
828 LEFT.br_state == newext &&
829 LEFT.br_blockcount + new->br_blockcount <= MAXEXTLEN);
830 /*
831 * Check and set flags if this segment has a right neighbor.
832 * Don't set contiguous if the combined extent would be too large.
833 * Also check for all-three-contiguous being too large.
834 */
835 if (STATE_SET_TEST(RIGHT_VALID,
836 idx <
837 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - 1)) {
838 xfs_bmbt_get_all(ep + 1, &RIGHT);
839 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(RIGHT.br_startblock));
840 }
841 STATE_SET(RIGHT_CONTIG,
842 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
843 new_endoff == RIGHT.br_startoff &&
844 new->br_startblock + new->br_blockcount ==
845 RIGHT.br_startblock &&
846 newext == RIGHT.br_state &&
847 new->br_blockcount + RIGHT.br_blockcount <= MAXEXTLEN &&
848 ((state & MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING)) !=
849 MASK3(LEFT_CONTIG, LEFT_FILLING, RIGHT_FILLING) ||
850 LEFT.br_blockcount + new->br_blockcount + RIGHT.br_blockcount
851 <= MAXEXTLEN));
852 /*
853 * Switch out based on the FILLING and CONTIG state bits.
854 */
855 switch (SWITCH_STATE) {
856
857 case MASK4(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
858 /*
859 * Setting all of a previous oldext extent to newext.
860 * The left and right neighbors are both contiguous with new.
861 */
862 xfs_bmap_trace_pre_update(fname, "LF|RF|LC|RC", ip, idx - 1,
863 XFS_DATA_FORK);
864 xfs_bmbt_set_blockcount(ep - 1,
865 LEFT.br_blockcount + PREV.br_blockcount +
866 RIGHT.br_blockcount);
867 xfs_bmap_trace_post_update(fname, "LF|RF|LC|RC", ip, idx - 1,
868 XFS_DATA_FORK);
869 xfs_bmap_trace_delete(fname, "LF|RF|LC|RC", ip, idx, 2,
870 XFS_DATA_FORK);
871 xfs_bmap_delete_exlist(ip, idx, 2, XFS_DATA_FORK);
872 ip->i_df.if_lastex = idx - 1;
873 ip->i_d.di_nextents -= 2;
874 if (cur == NULL)
875 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
876 else {
877 rval = XFS_ILOG_CORE;
878 if (error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
879 RIGHT.br_startblock,
880 RIGHT.br_blockcount, &i))
881 goto done;
882 ASSERT(i == 1);
883 if (error = xfs_bmbt_delete(cur, 0, &i))
884 goto done;
885 ASSERT(i == 1);
886 if (error = xfs_bmbt_decrement(cur, 0, &i))
887 goto done;
888 ASSERT(i == 1);
889 if (error = xfs_bmbt_delete(cur, 0, &i))
890 goto done;
891 ASSERT(i == 1);
892 if (error = xfs_bmbt_decrement(cur, 0, &i))
893 goto done;
894 ASSERT(i == 1);
895 if (error = xfs_bmbt_update(cur, LEFT.br_startoff,
896 LEFT.br_startblock,
897 LEFT.br_blockcount + PREV.br_blockcount +
898 RIGHT.br_blockcount, LEFT.br_state))
899 goto done;
900 }
901 break;
902
903 case MASK3(LEFT_FILLING, RIGHT_FILLING, LEFT_CONTIG):
904 /*
905 * Setting all of a previous oldext extent to newext.
906 * The left neighbor is contiguous, the right is not.
907 */
908 xfs_bmap_trace_pre_update(fname, "LF|RF|LC", ip, idx - 1,
909 XFS_DATA_FORK);
910 xfs_bmbt_set_blockcount(ep - 1,
911 LEFT.br_blockcount + PREV.br_blockcount);
912 xfs_bmap_trace_post_update(fname, "LF|RF|LC", ip, idx - 1,
913 XFS_DATA_FORK);
914 ip->i_df.if_lastex = idx - 1;
915 xfs_bmap_trace_delete(fname, "LF|RF|LC", ip, idx, 1,
916 XFS_DATA_FORK);
917 xfs_bmap_delete_exlist(ip, idx, 1, XFS_DATA_FORK);
918 ip->i_d.di_nextents--;
919 if (cur == NULL)
920 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
921 else {
922 rval = XFS_ILOG_CORE;
923 if (error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
924 PREV.br_startblock, PREV.br_blockcount,
925 &i))
926 goto done;
927 ASSERT(i == 1);
928 if (error = xfs_bmbt_delete(cur, 0, &i))
929 goto done;
930 ASSERT(i == 1);
931 if (error = xfs_bmbt_decrement(cur, 0, &i))
932 goto done;
933 ASSERT(i == 1);
934 if (error = xfs_bmbt_update(cur, LEFT.br_startoff,
935 LEFT.br_startblock,
936 LEFT.br_blockcount + PREV.br_blockcount,
937 LEFT.br_state))
938 goto done;
939 }
940 break;
941
942 case MASK3(LEFT_FILLING, RIGHT_FILLING, RIGHT_CONTIG):
943 /*
944 * Setting all of a previous oldext extent to newext.
945 * The right neighbor is contiguous, the left is not.
946 */
947 xfs_bmap_trace_pre_update(fname, "LF|RF|RC", ip, idx,
948 XFS_DATA_FORK);
949 xfs_bmbt_set_blockcount(ep,
950 PREV.br_blockcount + RIGHT.br_blockcount);
951 xfs_bmbt_set_state(ep, newext);
952 xfs_bmap_trace_post_update(fname, "LF|RF|RC", ip, idx,
953 XFS_DATA_FORK);
954 ip->i_df.if_lastex = idx;
955 xfs_bmap_trace_delete(fname, "LF|RF|RC", ip, idx + 1, 1,
956 XFS_DATA_FORK);
957 xfs_bmap_delete_exlist(ip, idx + 1, 1, XFS_DATA_FORK);
958 ip->i_d.di_nextents--;
959 if (cur == NULL)
960 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
961 else {
962 rval = XFS_ILOG_CORE;
963 if (error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
964 RIGHT.br_startblock,
965 RIGHT.br_blockcount, &i))
966 goto done;
967 ASSERT(i == 1);
968 if (error = xfs_bmbt_delete(cur, 0, &i))
969 goto done;
970 ASSERT(i == 1);
971 if (error = xfs_bmbt_decrement(cur, 0, &i))
972 goto done;
973 ASSERT(i == 1);
974 if (error = xfs_bmbt_update(cur, new->br_startoff,
975 new->br_startblock,
976 new->br_blockcount + RIGHT.br_blockcount,
977 newext))
978 goto done;
979 }
980 break;
981
982 case MASK2(LEFT_FILLING, RIGHT_FILLING):
983 /*
984 * Setting all of a previous oldext extent to newext.
985 * Neither the left nor right neighbors are contiguous with
986 * the new one.
987 */
988 xfs_bmap_trace_pre_update(fname, "LF|RF", ip, idx,
989 XFS_DATA_FORK);
990 xfs_bmbt_set_state(ep, newext);
991 xfs_bmap_trace_post_update(fname, "LF|RF", ip, idx,
992 XFS_DATA_FORK);
993 ip->i_df.if_lastex = idx;
994 if (cur == NULL)
995 rval = XFS_ILOG_DEXT;
996 else {
997 rval = 0;
998 if (error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
999 new->br_startblock, new->br_blockcount,
1000 &i))
1001 goto done;
1002 ASSERT(i == 1);
1003 if (error = xfs_bmbt_update(cur, new->br_startoff,
1004 new->br_startblock, new->br_blockcount,
1005 newext))
1006 goto done;
1007 }
1008 break;
1009
1010 case MASK2(LEFT_FILLING, LEFT_CONTIG):
1011 /*
1012 * Setting the first part of a previous oldext extent to newext.
1013 * The left neighbor is contiguous.
1014 */
1015 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx - 1,
1016 XFS_DATA_FORK);
1017 xfs_bmbt_set_blockcount(ep - 1,
1018 LEFT.br_blockcount + new->br_blockcount);
1019 xfs_bmbt_set_startoff(ep,
1020 PREV.br_startoff + new->br_blockcount);
1021 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx - 1,
1022 XFS_DATA_FORK);
1023 xfs_bmap_trace_pre_update(fname, "LF|LC", ip, idx,
1024 XFS_DATA_FORK);
1025 xfs_bmbt_set_startblock(ep,
1026 new->br_startblock + new->br_blockcount);
1027 xfs_bmbt_set_blockcount(ep,
1028 PREV.br_blockcount - new->br_blockcount);
1029 xfs_bmap_trace_post_update(fname, "LF|LC", ip, idx,
1030 XFS_DATA_FORK);
1031 ip->i_df.if_lastex = idx - 1;
1032 if (cur == NULL)
1033 rval = XFS_ILOG_DEXT;
1034 else {
1035 rval = 0;
1036 if (error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1037 PREV.br_startblock, PREV.br_blockcount,
1038 &i))
1039 goto done;
1040 ASSERT(i == 1);
1041 if (error = xfs_bmbt_update(cur,
1042 PREV.br_startoff + new->br_blockcount,
1043 PREV.br_startblock + new->br_blockcount,
1044 PREV.br_blockcount - new->br_blockcount,
1045 oldext))
1046 goto done;
1047 if (error = xfs_bmbt_decrement(cur, 0, &i))
1048 goto done;
1049 if (xfs_bmbt_update(cur, LEFT.br_startoff,
1050 LEFT.br_startblock,
1051 LEFT.br_blockcount + new->br_blockcount,
1052 LEFT.br_state))
1053 goto done;
1054 }
1055 break;
1056
1057 case MASK(LEFT_FILLING):
1058 /*
1059 * Setting the first part of a previous oldext extent to newext.
1060 * The left neighbor is not contiguous.
1061 */
1062 xfs_bmap_trace_pre_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1063 ASSERT(ep && xfs_bmbt_get_state(ep) == oldext);
1064 xfs_bmbt_set_startoff(ep, new_endoff);
1065 xfs_bmbt_set_blockcount(ep,
1066 PREV.br_blockcount - new->br_blockcount);
1067 xfs_bmbt_set_startblock(ep,
1068 new->br_startblock + new->br_blockcount);
1069 xfs_bmap_trace_post_update(fname, "LF", ip, idx, XFS_DATA_FORK);
1070 xfs_bmap_trace_insert(fname, "LF", ip, idx, 1, new, NULL,
1071 XFS_DATA_FORK);
1072 xfs_bmap_insert_exlist(ip, idx, 1, new, XFS_DATA_FORK);
1073 ip->i_df.if_lastex = idx;
1074 ip->i_d.di_nextents++;
1075 if (cur == NULL)
1076 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1077 else {
1078 rval = XFS_ILOG_CORE;
1079 if (error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1080 PREV.br_startblock, PREV.br_blockcount,
1081 &i))
1082 goto done;
1083 ASSERT(i == 1);
1084 if (error = xfs_bmbt_update(cur,
1085 PREV.br_startoff + new->br_blockcount,
1086 PREV.br_startblock + new->br_blockcount,
1087 PREV.br_blockcount - new->br_blockcount,
1088 oldext))
1089 goto done;
1090 cur->bc_rec.b = *new;
1091 if (error = xfs_bmbt_insert(cur, &i))
1092 goto done;
1093 ASSERT(i == 1);
1094 }
1095 break;
1096
1097 case MASK2(RIGHT_FILLING, RIGHT_CONTIG):
1098 /*
1099 * Setting the last part of a previous oldext extent to newext.
1100 * The right neighbor is contiguous with the new allocation.
1101 */
1102 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx,
1103 XFS_DATA_FORK);
1104 xfs_bmap_trace_pre_update(fname, "RF|RC", ip, idx + 1,
1105 XFS_DATA_FORK);
1106 xfs_bmbt_set_blockcount(ep,
1107 PREV.br_blockcount - new->br_blockcount);
1108 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx,
1109 XFS_DATA_FORK);
1110 xfs_bmbt_set_allf(ep + 1, new->br_startoff, new->br_startblock,
1111 new->br_blockcount + RIGHT.br_blockcount, newext);
1112 xfs_bmap_trace_post_update(fname, "RF|RC", ip, idx + 1,
1113 XFS_DATA_FORK);
1114 ip->i_df.if_lastex = idx + 1;
1115 if (cur == NULL)
1116 rval = XFS_ILOG_DEXT;
1117 else {
1118 rval = 0;
1119 if (error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1120 PREV.br_startblock,
1121 PREV.br_blockcount, &i))
1122 goto done;
1123 ASSERT(i == 1);
1124 if (error = xfs_bmbt_update(cur, PREV.br_startoff,
1125 PREV.br_startblock,
1126 PREV.br_blockcount - new->br_blockcount,
1127 oldext))
1128 goto done;
1129 if (error = xfs_bmbt_increment(cur, 0, &i))
1130 goto done;
1131 if (error = xfs_bmbt_update(cur, new->br_startoff,
1132 new->br_startblock,
1133 new->br_blockcount + RIGHT.br_blockcount,
1134 newext))
1135 goto done;
1136 }
1137 break;
1138
1139 case MASK(RIGHT_FILLING):
1140 /*
1141 * Setting the last part of a previous oldext extent to newext.
1142 * The right neighbor is not contiguous.
1143 */
1144 xfs_bmap_trace_pre_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1145 xfs_bmbt_set_blockcount(ep,
1146 PREV.br_blockcount - new->br_blockcount);
1147 xfs_bmap_trace_post_update(fname, "RF", ip, idx, XFS_DATA_FORK);
1148 xfs_bmap_trace_insert(fname, "RF", ip, idx + 1, 1,
1149 new, NULL, XFS_DATA_FORK);
1150 xfs_bmap_insert_exlist(ip, idx + 1, 1, new, XFS_DATA_FORK);
1151 ip->i_df.if_lastex = idx + 1;
1152 ip->i_d.di_nextents++;
1153 if (cur == NULL)
1154 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1155 else {
1156 rval = XFS_ILOG_CORE;
1157 if (error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1158 PREV.br_startblock, PREV.br_blockcount,
1159 &i))
1160 goto done;
1161 ASSERT(i == 1);
1162 if (error = xfs_bmbt_update(cur, PREV.br_startoff,
1163 PREV.br_startblock,
1164 PREV.br_blockcount - new->br_blockcount,
1165 oldext))
1166 goto done;
1167 if (error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1168 new->br_startblock, new->br_blockcount,
1169 &i))
1170 goto done;
1171 ASSERT(i == 0);
1172 cur->bc_rec.b.br_state = XFS_EXT_NORM;
1173 if (error = xfs_bmbt_insert(cur, &i))
1174 goto done;
1175 ASSERT(i == 1);
1176 }
1177 break;
1178
1179 case 0:
1180 /*
1181 * Setting the middle part of a previous oldext extent to
1182 * newext. Contiguity is impossible here.
1183 * One extent becomes three extents.
1184 */
1185 xfs_bmap_trace_pre_update(fname, "0", ip, idx, XFS_DATA_FORK);
1186 xfs_bmbt_set_blockcount(ep,
1187 new->br_startoff - PREV.br_startoff);
1188 xfs_bmap_trace_post_update(fname, "0", ip, idx, XFS_DATA_FORK);
1189 r[0] = *new;
1190 r[1].br_startoff = new_endoff;
1191 r[1].br_blockcount =
1192 PREV.br_startoff + PREV.br_blockcount - new_endoff;
1193 r[1].br_startblock = new->br_startblock + new->br_blockcount;
1194 r[1].br_state = oldext;
1195 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 2, &r[0], &r[1],
1196 XFS_DATA_FORK);
1197 xfs_bmap_insert_exlist(ip, idx + 1, 2, &r[0], XFS_DATA_FORK);
1198 ip->i_df.if_lastex = idx + 1;
1199 ip->i_d.di_nextents += 2;
1200 if (cur == NULL)
1201 rval = XFS_ILOG_CORE | XFS_ILOG_DEXT;
1202 else {
1203 rval = XFS_ILOG_CORE;
1204 if (error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
1205 PREV.br_startblock, PREV.br_blockcount,
1206 &i))
1207 goto done;
1208 ASSERT(i == 1);
1209 /* new right extent - oldext */
1210 if (error = xfs_bmbt_update(cur, r[1].br_startoff,
1211 r[1].br_startblock, r[1].br_blockcount,
1212 r[1].br_state))
1213 goto done;
1214 /* new left extent - oldext */
1215 PREV.br_blockcount =
1216 new->br_startoff - PREV.br_startoff;
1217 cur->bc_rec.b = PREV;
1218 if (error = xfs_bmbt_insert(cur, &i))
1219 goto done;
1220 ASSERT(i == 1);
1221 if (error = xfs_bmbt_increment(cur, 0, &i))
1222 goto done;
1223 ASSERT(i == 1);
1224 /* new middle extent - newext */
1225 cur->bc_rec.b = *new;
1226 if (error = xfs_bmbt_insert(cur, &i))
1227 goto done;
1228 ASSERT(i == 1);
1229 }
1230 break;
1231
1232 case MASK3(LEFT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1233 case MASK3(RIGHT_FILLING, LEFT_CONTIG, RIGHT_CONTIG):
1234 case MASK2(LEFT_FILLING, RIGHT_CONTIG):
1235 case MASK2(RIGHT_FILLING, LEFT_CONTIG):
1236 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1237 case MASK(LEFT_CONTIG):
1238 case MASK(RIGHT_CONTIG):
1239 /*
1240 * These cases are all impossible.
1241 */
1242 ASSERT(0);
1243 }
1244 *curp = cur;
1245done:
1246 *logflagsp = rval;
1247 return error;
1248#undef LEFT
1249#undef RIGHT
1250#undef PREV
1251#undef MASK
1252#undef MASK2
1253#undef MASK3
1254#undef MASK4
1255#undef STATE_SET
1256#undef STATE_TEST
1257#undef STATE_SET_TEST
1258#undef SWITCH_STATE
1259}
1260
1261/*
1262 * Called by xfs_bmap_add_extent to handle cases converting a hole
1263 * to a delayed allocation.
1264 */
1265/*ARGSUSED*/
1266STATIC int /* error */
1267xfs_bmap_add_extent_hole_delay(
1268 xfs_inode_t *ip, /* incore inode pointer */
1269 xfs_extnum_t idx, /* extent number to update/insert */
1270 xfs_btree_cur_t *cur, /* if null, not a btree */
1271 xfs_bmbt_irec_t *new, /* new data to put in extent list */
1272 int *logflagsp, /* inode logging flags */
1273 int rsvd) /* OK to allocate reserved blocks */
1274{
1275 xfs_bmbt_rec_t *base; /* base of extent entry list */
1276 xfs_bmbt_rec_t *ep; /* extent list entry for idx */
1277#ifdef XFS_BMAP_TRACE
1278 static char fname[] = "xfs_bmap_add_extent_hole_delay";
1279#endif
1280 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1281 xfs_filblks_t newlen; /* new indirect size */
1282 xfs_filblks_t oldlen; /* old indirect size */
1283 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1284 int state; /* state bits, accessed thru macros */
1285 xfs_filblks_t temp; /* temp for indirect calculations */
1286 enum { /* bit number definitions for state */
1287 LEFT_CONTIG, RIGHT_CONTIG,
1288 LEFT_DELAY, RIGHT_DELAY,
1289 LEFT_VALID, RIGHT_VALID
1290 };
1291
1292#define MASK(b) (1 << (b))
1293#define MASK2(a,b) (MASK(a) | MASK(b))
1294#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1295#define STATE_TEST(b) (state & MASK(b))
1296#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1297 ((state &= ~MASK(b)), 0))
1298#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1299
1300 base = ip->i_df.if_u1.if_extents;
1301 ep = &base[idx];
1302 state = 0;
1303 ASSERT(ISNULLSTARTBLOCK(new->br_startblock));
1304 /*
1305 * Check and set flags if this segment has a left neighbor
1306 */
1307 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1308 xfs_bmbt_get_all(ep - 1, &left);
1309 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1310 }
1311 /*
1312 * Check and set flags if the current (right) segment exists.
1313 * If it doesn't exist, we're converting the hole at end-of-file.
1314 */
1315 if (STATE_SET_TEST(RIGHT_VALID,
1316 idx <
1317 ip->i_df.if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1318 xfs_bmbt_get_all(ep, &right);
1319 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1320 }
1321 /*
1322 * Set contiguity flags on the left and right neighbors.
1323 * Don't let extents get too large, even if the pieces are contiguous.
1324 */
1325 STATE_SET(LEFT_CONTIG,
1326 STATE_TEST(LEFT_VALID) && STATE_TEST(LEFT_DELAY) &&
1327 left.br_startoff + left.br_blockcount == new->br_startoff &&
1328 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1329 STATE_SET(RIGHT_CONTIG,
1330 STATE_TEST(RIGHT_VALID) && STATE_TEST(RIGHT_DELAY) &&
1331 new->br_startoff + new->br_blockcount == right.br_startoff &&
1332 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1333 (!STATE_TEST(LEFT_CONTIG) ||
1334 (left.br_blockcount + new->br_blockcount +
1335 right.br_blockcount <= MAXEXTLEN)));
1336 /*
1337 * Switch out based on the contiguity flags.
1338 */
1339 switch (SWITCH_STATE) {
1340
1341 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1342 /*
1343 * New allocation is contiguous with delayed allocations
1344 * on the left and on the right.
1345 * Merge all three into a single extent list entry.
1346 */
1347 temp = left.br_blockcount + new->br_blockcount +
1348 right.br_blockcount;
1349 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
1350 XFS_DATA_FORK);
1351 xfs_bmbt_set_blockcount(ep - 1, temp);
1352 oldlen = STARTBLOCKVAL(left.br_startblock) +
1353 STARTBLOCKVAL(new->br_startblock) +
1354 STARTBLOCKVAL(right.br_startblock);
1355 newlen = xfs_bmap_worst_indlen(ip, temp);
1356 xfs_bmbt_set_startblock(ep - 1, NULLSTARTBLOCK((int)newlen));
1357 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
1358 XFS_DATA_FORK);
1359 xfs_bmap_trace_delete(fname, "LC|RC", ip, idx, 1,
1360 XFS_DATA_FORK);
1361 xfs_bmap_delete_exlist(ip, idx, 1, XFS_DATA_FORK);
1362 ip->i_df.if_lastex = idx - 1;
1363 break;
1364
1365 case MASK(LEFT_CONTIG):
1366 /*
1367 * New allocation is contiguous with a delayed allocation
1368 * on the left.
1369 * Merge the new allocation with the left neighbor.
1370 */
1371 temp = left.br_blockcount + new->br_blockcount;
1372 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1,
1373 XFS_DATA_FORK);
1374 xfs_bmbt_set_blockcount(ep - 1, temp);
1375 oldlen = STARTBLOCKVAL(left.br_startblock) +
1376 STARTBLOCKVAL(new->br_startblock);
1377 newlen = xfs_bmap_worst_indlen(ip, temp);
1378 xfs_bmbt_set_startblock(ep - 1, NULLSTARTBLOCK((int)newlen));
1379 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1,
1380 XFS_DATA_FORK);
1381 ip->i_df.if_lastex = idx - 1;
1382 break;
1383
1384 case MASK(RIGHT_CONTIG):
1385 /*
1386 * New allocation is contiguous with a delayed allocation
1387 * on the right.
1388 * Merge the new allocation with the right neighbor.
1389 */
1390 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1391 temp = new->br_blockcount + right.br_blockcount;
1392 oldlen = STARTBLOCKVAL(new->br_startblock) +
1393 STARTBLOCKVAL(right.br_startblock);
1394 newlen = xfs_bmap_worst_indlen(ip, temp);
1395 xfs_bmbt_set_allf(ep, new->br_startoff,
1396 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
1397 xfs_bmap_trace_post_update(fname, "RC", ip, idx, XFS_DATA_FORK);
1398 ip->i_df.if_lastex = idx;
1399 break;
1400
1401 case 0:
1402 /*
1403 * New allocation is not contiguous with another
1404 * delayed allocation.
1405 * Insert a new entry.
1406 */
1407 oldlen = newlen = 0;
1408 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
1409 XFS_DATA_FORK);
1410 xfs_bmap_insert_exlist(ip, idx, 1, new, XFS_DATA_FORK);
1411 ip->i_df.if_lastex = idx;
1412 break;
1413 }
1414 if (oldlen != newlen) {
1415 ASSERT(oldlen > newlen);
1416 xfs_mod_incore_sb(ip->i_mount, XFS_SBS_FDBLOCKS,
1417 (int)(oldlen - newlen), rsvd);
1418 /*
1419 * Nothing to do for disk quota accounting here.
1420 */
1421 }
1422 *logflagsp = 0;
1423 return 0;
1424#undef MASK
1425#undef MASK2
1426#undef STATE_SET
1427#undef STATE_TEST
1428#undef STATE_SET_TEST
1429#undef SWITCH_STATE
1430}
1431
1432/*
1433 * Called by xfs_bmap_add_extent to handle cases converting a hole
1434 * to a real allocation.
1435 */
1436STATIC int /* error */
1437xfs_bmap_add_extent_hole_real(
1438 xfs_inode_t *ip, /* incore inode pointer */
1439 xfs_extnum_t idx, /* extent number to update/insert */
1440 xfs_btree_cur_t *cur, /* if null, not a btree */
1441 xfs_bmbt_irec_t *new, /* new data to put in extent list */
1442 int *logflagsp, /* inode logging flags */
1443 int whichfork) /* data or attr fork */
1444{
1445 xfs_bmbt_rec_t *ep; /* pointer to extent entry ins. point */
1446 int error; /* error return value */
1447#ifdef XFS_BMAP_TRACE
1448 static char fname[] = "xfs_bmap_add_extent_hole_real";
1449#endif
1450 int i; /* temp state */
1451 xfs_ifork_t *ifp; /* inode fork pointer */
1452 xfs_bmbt_irec_t left; /* left neighbor extent entry */
1453 xfs_bmbt_irec_t right; /* right neighbor extent entry */
1454 int state; /* state bits, accessed thru macros */
1455 enum { /* bit number definitions for state */
1456 LEFT_CONTIG, RIGHT_CONTIG,
1457 LEFT_DELAY, RIGHT_DELAY,
1458 LEFT_VALID, RIGHT_VALID
1459 };
1460
1461#define MASK(b) (1 << (b))
1462#define MASK2(a,b) (MASK(a) | MASK(b))
1463#define STATE_SET(b,v) ((v) ? (state |= MASK(b)) : (state &= ~MASK(b)))
1464#define STATE_TEST(b) (state & MASK(b))
1465#define STATE_SET_TEST(b,v) ((v) ? ((state |= MASK(b)), 1) : \
1466 ((state &= ~MASK(b)), 0))
1467#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
1468
1469 ifp = XFS_IFORK_PTR(ip, whichfork);
1470 ASSERT(idx <= ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t));
1471 ep = &ifp->if_u1.if_extents[idx];
1472 state = 0;
1473 /*
1474 * Check and set flags if this segment has a left neighbor.
1475 */
1476 if (STATE_SET_TEST(LEFT_VALID, idx > 0)) {
1477 xfs_bmbt_get_all(ep - 1, &left);
1478 STATE_SET(LEFT_DELAY, ISNULLSTARTBLOCK(left.br_startblock));
1479 }
1480 /*
1481 * Check and set flags if this segment has a current value.
1482 * Not true if we're inserting into the "hole" at eof.
1483 */
1484 if (STATE_SET_TEST(RIGHT_VALID,
1485 idx <
1486 ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t))) {
1487 xfs_bmbt_get_all(ep, &right);
1488 STATE_SET(RIGHT_DELAY, ISNULLSTARTBLOCK(right.br_startblock));
1489 }
1490 /*
1491 * We're inserting a real allocation between "left" and "right".
1492 * Set the contiguity flags. Don't let extents get too large.
1493 */
1494 STATE_SET(LEFT_CONTIG,
1495 STATE_TEST(LEFT_VALID) && !STATE_TEST(LEFT_DELAY) &&
1496 left.br_startoff + left.br_blockcount == new->br_startoff &&
1497 left.br_startblock + left.br_blockcount == new->br_startblock &&
1498 left.br_state == new->br_state &&
1499 left.br_blockcount + new->br_blockcount <= MAXEXTLEN);
1500 STATE_SET(RIGHT_CONTIG,
1501 STATE_TEST(RIGHT_VALID) && !STATE_TEST(RIGHT_DELAY) &&
1502 new->br_startoff + new->br_blockcount == right.br_startoff &&
1503 new->br_startblock + new->br_blockcount ==
1504 right.br_startblock &&
1505 new->br_state == right.br_state &&
1506 new->br_blockcount + right.br_blockcount <= MAXEXTLEN &&
1507 (!STATE_TEST(LEFT_CONTIG) ||
1508 left.br_blockcount + new->br_blockcount +
1509 right.br_blockcount <= MAXEXTLEN));
1510
1511 /*
1512 * Select which case we're in here, and implement it.
1513 */
1514 switch (SWITCH_STATE) {
1515
1516 case MASK2(LEFT_CONTIG, RIGHT_CONTIG):
1517 /*
1518 * New allocation is contiguous with real allocations on the
1519 * left and on the right.
1520 * Merge all three into a single extent list entry.
1521 */
1522 xfs_bmap_trace_pre_update(fname, "LC|RC", ip, idx - 1,
1523 whichfork);
1524 xfs_bmbt_set_blockcount(ep - 1,
1525 left.br_blockcount + new->br_blockcount +
1526 right.br_blockcount);
1527 xfs_bmap_trace_post_update(fname, "LC|RC", ip, idx - 1,
1528 whichfork);
1529 xfs_bmap_trace_delete(fname, "LC|RC", ip,
1530 idx, 1, whichfork);
1531 xfs_bmap_delete_exlist(ip, idx, 1, whichfork);
1532 ifp->if_lastex = idx - 1;
1533 XFS_IFORK_NEXT_SET(ip, whichfork,
1534 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
1535 if (cur == NULL) {
1536 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
1537 return 0;
1538 }
1539 *logflagsp = XFS_ILOG_CORE;
1540 if (error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
1541 right.br_startblock, right.br_blockcount, &i))
1542 return error;
1543 ASSERT(i == 1);
1544 if (error = xfs_bmbt_delete(cur, 0, &i))
1545 return error;
1546 ASSERT(i == 1);
1547 if (error = xfs_bmbt_decrement(cur, 0, &i))
1548 return error;
1549 ASSERT(i == 1);
1550 error = xfs_bmbt_update(cur, left.br_startoff,
1551 left.br_startblock,
1552 left.br_blockcount + new->br_blockcount +
1553 right.br_blockcount, left.br_state);
1554 return error;
1555
1556 case MASK(LEFT_CONTIG):
1557 /*
1558 * New allocation is contiguous with a real allocation
1559 * on the left.
1560 * Merge the new allocation with the left neighbor.
1561 */
1562 xfs_bmap_trace_pre_update(fname, "LC", ip, idx - 1, whichfork);
1563 xfs_bmbt_set_blockcount(ep - 1,
1564 left.br_blockcount + new->br_blockcount);
1565 xfs_bmap_trace_post_update(fname, "LC", ip, idx - 1, whichfork);
1566 ifp->if_lastex = idx - 1;
1567 if (cur == NULL) {
1568 *logflagsp = XFS_ILOG_FEXT(whichfork);
1569 return 0;
1570 }
1571 *logflagsp = 0;
1572 if (error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
1573 left.br_startblock, left.br_blockcount, &i))
1574 return error;
1575 ASSERT(i == 1);
1576 error = xfs_bmbt_update(cur, left.br_startoff,
1577 left.br_startblock,
1578 left.br_blockcount + new->br_blockcount,
1579 left.br_state);
1580 return error;
1581
1582 case MASK(RIGHT_CONTIG):
1583 /*
1584 * New allocation is contiguous with a real allocation
1585 * on the right.
1586 * Merge the new allocation with the right neighbor.
1587 */
1588 xfs_bmap_trace_pre_update(fname, "RC", ip, idx, whichfork);
1589 xfs_bmbt_set_allf(ep, new->br_startoff, new->br_startblock,
1590 new->br_blockcount + right.br_blockcount,
1591 right.br_state);
1592 xfs_bmap_trace_post_update(fname, "RC", ip, idx, whichfork);
1593 ifp->if_lastex = idx;
1594 if (cur == NULL) {
1595 *logflagsp = XFS_ILOG_FEXT(whichfork);
1596 return 0;
1597 }
1598 *logflagsp = 0;
1599 if (error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
1600 right.br_startblock, right.br_blockcount, &i))
1601 return error;
1602 ASSERT(i == 1);
1603 error = xfs_bmbt_update(cur, new->br_startoff,
1604 new->br_startblock,
1605 new->br_blockcount + right.br_blockcount,
1606 right.br_state);
1607 return error;
1608
1609 case 0:
1610 /*
1611 * New allocation is not contiguous with another
1612 * real allocation.
1613 * Insert a new entry.
1614 */
1615 xfs_bmap_trace_insert(fname, "0", ip, idx, 1, new, NULL,
1616 whichfork);
1617 xfs_bmap_insert_exlist(ip, idx, 1, new, whichfork);
1618 ifp->if_lastex = idx;
1619 XFS_IFORK_NEXT_SET(ip, whichfork,
1620 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
1621 if (cur == NULL) {
1622 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
1623 return 0;
1624 }
1625 *logflagsp = XFS_ILOG_CORE;
1626 if (error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1627 new->br_startblock, new->br_blockcount, &i))
1628 return error;
1629 ASSERT(i == 0);
1630 cur->bc_rec.b.br_state = new->br_state;
1631 if (error = xfs_bmbt_insert(cur, &i))
1632 return error;
1633 ASSERT(i == 1);
1634 return 0;
1635 }
1636#undef MASK
1637#undef MASK2
1638#undef STATE_SET
1639#undef STATE_TEST
1640#undef STATE_SET_TEST
1641#undef SWITCH_STATE
1642 /* NOTREACHED */
1643 ASSERT(0);
1644 return 0; /* keep gcc quite */
1645}
1646
1647#define XFS_ALLOC_GAP_UNITS 4
1648
1649/*
1650 * xfs_bmap_alloc is called by xfs_bmapi to allocate an extent for a file.
1651 * It figures out where to ask the underlying allocator to put the new extent.
1652 */
1653STATIC int /* error */
1654xfs_bmap_alloc(
1655 xfs_bmalloca_t *ap) /* bmap alloc argument struct */
1656{
1657 xfs_fsblock_t adjust; /* adjustment to block numbers */
1658 xfs_alloctype_t atype; /* type for allocation routines */
1659 int error; /* error return value */
1660 xfs_agnumber_t fb_agno; /* ag number of ap->firstblock */
1661 xfs_mount_t *mp; /* mount point structure */
1662 int nullfb; /* true if ap->firstblock isn't set */
1663 int rt; /* true if inode is realtime */
1664#ifdef __KERNEL__
1665 xfs_extlen_t prod; /* product factor for allocators */
1666 xfs_extlen_t ralen; /* realtime allocation length */
1667#endif
1668
1669#define ISLEGAL(x,y) \
1670 (rt ? \
1671 (x) < mp->m_sb.sb_rblocks : \
1672 XFS_FSB_TO_AGNO(mp, x) == XFS_FSB_TO_AGNO(mp, y) && \
1673 XFS_FSB_TO_AGNO(mp, x) < mp->m_sb.sb_agcount && \
1674 XFS_FSB_TO_AGBNO(mp, x) < mp->m_sb.sb_agblocks)
1675
1676 /*
1677 * Set up variables.
1678 */
1679 mp = ap->ip->i_mount;
1680 nullfb = ap->firstblock == NULLFSBLOCK;
1681 rt = (ap->ip->i_d.di_flags & XFS_DIFLAG_REALTIME) && ap->userdata;
1682 fb_agno = nullfb ? NULLAGNUMBER : XFS_FSB_TO_AGNO(mp, ap->firstblock);
1683#ifdef __KERNEL__
1684 if (rt) {
1685 xfs_extlen_t extsz; /* file extent size for rt */
1686 xfs_fileoff_t nexto; /* next file offset */
1687 xfs_extlen_t orig_alen; /* original ap->alen */
1688 xfs_fileoff_t orig_end; /* original off+len */
1689 xfs_fileoff_t orig_off; /* original ap->off */
1690 xfs_extlen_t mod_off; /* modulus calculations */
1691 xfs_fileoff_t prevo; /* previous file offset */
1692 xfs_rtblock_t rtx; /* realtime extent number */
1693 xfs_extlen_t temp; /* temp for rt calculations */
1694
1695 /*
1696 * Set prod to match the realtime extent size.
1697 */
1698 if (!(extsz = ap->ip->i_d.di_extsize))
1699 extsz = mp->m_sb.sb_rextsize;
1700 prod = extsz / mp->m_sb.sb_rextsize;
1701 orig_off = ap->off;
1702 orig_alen = ap->alen;
1703 orig_end = orig_off + orig_alen;
1704 /*
1705 * If the file offset is unaligned vs. the extent size
1706 * we need to align it. This will be possible unless
1707 * the file was previously written with a kernel that didn't
1708 * perform this alignment.
1709 */
1710 mod_off = do_mod(orig_off, extsz);
1711 if (mod_off) {
1712 ap->alen += mod_off;
1713 ap->off -= mod_off;
1714 }
1715 /*
1716 * Same adjustment for the end of the requested area.
1717 */
1718 if (temp = (ap->alen % extsz))
1719 ap->alen += extsz - temp;
1720 /*
1721 * If the previous block overlaps with this proposed allocation
1722 * then move the start forward without adjusting the length.
1723 */
1724 prevo =
1725 ap->prevp->br_startoff == NULLFILEOFF ?
1726 0 :
1727 (ap->prevp->br_startoff +
1728 ap->prevp->br_blockcount);
1729 if (ap->off != orig_off && ap->off < prevo)
1730 ap->off = prevo;
1731 /*
1732 * If the next block overlaps with this proposed allocation
1733 * then move the start back without adjusting the length,
1734 * but not before offset 0.
1735 * This may of course make the start overlap previous block,
1736 * and if we hit the offset 0 limit then the next block
1737 * can still overlap too.
1738 */
1739 nexto = (ap->eof || ap->gotp->br_startoff == NULLFILEOFF) ?
1740 NULLFILEOFF : ap->gotp->br_startoff;
1741 if (!ap->eof &&
1742 ap->off + ap->alen != orig_end &&
1743 ap->off + ap->alen > nexto)
1744 ap->off = nexto > ap->alen ? nexto - ap->alen : 0;
1745 /*
1746 * If we're now overlapping the next or previous extent that
1747 * means we can't fit an extsz piece in this hole. Just move
1748 * the start forward to the first legal spot and set
1749 * the length so we hit the end.
1750 */
1751 if ((ap->off != orig_off && ap->off < prevo) ||
1752 (ap->off + ap->alen != orig_end &&
1753 ap->off + ap->alen > nexto)) {
1754 ap->off = prevo;
1755 ap->alen = nexto - prevo;
1756 }
1757 /*
1758 * If the result isn't a multiple of rtextents we need to
1759 * remove blocks until it is.
1760 */
1761 if (temp = (ap->alen % mp->m_sb.sb_rextsize)) {
1762 /*
1763 * We're not covering the original request, or
1764 * we won't be able to once we fix the length.
1765 */
1766 if (orig_off < ap->off ||
1767 orig_end > ap->off + ap->alen ||
1768 ap->alen - temp < orig_alen)
1769 return XFS_ERROR(EINVAL);
1770 /*
1771 * Try to fix it by moving the start up.
1772 */
1773 if (ap->off + temp <= orig_off) {
1774 ap->alen -= temp;
1775 ap->off += temp;
1776 }
1777 /*
1778 * Try to fix it by moving the end in.
1779 */
1780 else if (ap->off + ap->alen - temp >= orig_end)
1781 ap->alen -= temp;
1782 /*
1783 * Set the start to the minimum then trim the length.
1784 */
1785 else {
1786 ap->alen -= orig_off - ap->off;
1787 ap->off = orig_off;
1788 ap->alen -= ap->alen % mp->m_sb.sb_rextsize;
1789 }
1790 /*
1791 * Result doesn't cover the request, fail it.
1792 */
1793 if (orig_off < ap->off || orig_end > ap->off + ap->alen)
1794 return XFS_ERROR(EINVAL);
1795 }
1796 ASSERT(ap->alen % mp->m_sb.sb_rextsize == 0);
1797 /*
1798 * If the offset & length are not perfectly aligned
1799 * then kill prod, it will just get us in trouble.
1800 */
1801 if (do_mod(ap->off, extsz) || ap->alen % extsz)
1802 prod = 1;
1803 /*
1804 * Set ralen to be the actual requested length in rtextents.
1805 */
1806 ralen = ap->alen / mp->m_sb.sb_rextsize;
1807 /*
1808 * If the old value was close enough to MAXEXTLEN that
1809 * we rounded up to it, cut it back so it's legal again.
1810 * Note that if it's a really large request (bigger than
1811 * MAXEXTLEN), we don't hear about that number, and can't
1812 * adjust the starting point to match it.
1813 */
1814 if (ralen * mp->m_sb.sb_rextsize >= MAXEXTLEN)
1815 ralen = MAXEXTLEN / mp->m_sb.sb_rextsize;
1816 /*
1817 * If it's an allocation to an empty file at offset 0,
1818 * pick an extent that will space things out in the rt area.
1819 */
1820 if (ap->eof && ap->off == 0) {
1821 error = xfs_rtpick_extent(mp, ap->tp, ralen, &rtx);
1822 if (error)
1823 return error;
1824 ap->rval = rtx * mp->m_sb.sb_rextsize;
1825 } else
1826 ap->rval = 0;
1827 }
1828#else
1829 if (rt)
1830 ap->rval = 0;
1831#endif /* __KERNEL__ */
1832 else if (nullfb)
1833 ap->rval = XFS_INO_TO_FSB(mp, ap->ip->i_ino);
1834 else
1835 ap->rval = ap->firstblock;
1836 /*
1837 * If allocating at eof, and there's a previous real block,
1838 * try to use it's last block as our starting point.
1839 */
1840 if (ap->eof && ap->prevp->br_startoff != NULLFILEOFF &&
1841 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
1842 ISLEGAL(ap->prevp->br_startblock + ap->prevp->br_blockcount,
1843 ap->prevp->br_startblock)) {
1844 ap->rval = ap->prevp->br_startblock + ap->prevp->br_blockcount;
1845 /*
1846 * Adjust for the gap between prevp and us.
1847 */
1848 adjust = ap->off -
1849 (ap->prevp->br_startoff + ap->prevp->br_blockcount);
1850 if (adjust &&
1851 ISLEGAL(ap->rval + adjust, ap->prevp->br_startblock))
1852 ap->rval += adjust;
1853 }
1854 /*
1855 * If not at eof, then compare the two neighbor blocks.
1856 * Figure out whether either one gives us a good starting point,
1857 * and pick the better one.
1858 */
1859 else if (!ap->eof) {
1860 xfs_fsblock_t gotbno; /* right side block number */
1861 xfs_fsblock_t gotdiff; /* right side difference */
1862 xfs_fsblock_t prevbno; /* left side block number */
1863 xfs_fsblock_t prevdiff; /* left side difference */
1864
1865 /*
1866 * If there's a previous (left) block, select a requested
1867 * start block based on it.
1868 */
1869 if (ap->prevp->br_startoff != NULLFILEOFF &&
1870 !ISNULLSTARTBLOCK(ap->prevp->br_startblock) &&
1871 (prevbno = ap->prevp->br_startblock +
1872 ap->prevp->br_blockcount) &&
1873 ISLEGAL(prevbno, ap->prevp->br_startblock)) {
1874 /*
1875 * Calculate gap to end of previous block.
1876 */
1877 adjust = prevdiff = ap->off -
1878 (ap->prevp->br_startoff +
1879 ap->prevp->br_blockcount);
1880 /*
1881 * Figure the startblock based on the previous block's
1882 * end and the gap size.
1883 * Heuristic!
1884 * If the gap is large relative to the piece we're
1885 * allocating, or using it gives us an illegal block
1886 * number, then just use the end of the previous block.
1887 */
1888 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
1889 ISLEGAL(prevbno + prevdiff,
1890 ap->prevp->br_startblock))
1891 prevbno += adjust;
1892 else
1893 prevdiff += adjust;
1894 /*
1895 * If the firstblock forbids it, can't use it,
1896 * must use default.
1897 */
1898 if (!rt && !nullfb &&
1899 XFS_FSB_TO_AGNO(mp, prevbno) != fb_agno)
1900 prevbno = NULLFSBLOCK;
1901 }
1902 /*
1903 * No previous block or can't follow it, just default.
1904 */
1905 else
1906 prevbno = NULLFSBLOCK;
1907 /*
1908 * If there's a following (right) block, select a requested
1909 * start block based on it.
1910 */
1911 if (!ISNULLSTARTBLOCK(ap->gotp->br_startblock)) {
1912 /*
1913 * Calculate gap to start of next block.
1914 */
1915 adjust = gotdiff = ap->gotp->br_startoff - ap->off;
1916 /*
1917 * Figure the startblock based on the next block's
1918 * start and the gap size.
1919 */
1920 gotbno = ap->gotp->br_startblock;
1921 /*
1922 * Heuristic!
1923 * If the gap is large relative to the piece we're
1924 * allocating, or using it gives us an illegal block
1925 * number, then just use the start of the next block
1926 * offset by our length.
1927 */
1928 if (gotdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
1929 ISLEGAL(gotbno - gotdiff, gotbno))
1930 gotbno -= adjust;
1931 else if (ISLEGAL(gotbno - ap->alen, gotbno)) {
1932 gotbno -= ap->alen;
1933 gotdiff += adjust - ap->alen;
1934 } else
1935 gotdiff += adjust;
1936 /*
1937 * If the firstblock forbids it, can't use it,
1938 * must use default.
1939 */
1940 if (!rt && !nullfb &&
1941 XFS_FSB_TO_AGNO(mp, gotbno) != fb_agno)
1942 gotbno = NULLFSBLOCK;
1943 }
1944 /*
1945 * No next block, just default.
1946 */
1947 else
1948 gotbno = NULLFSBLOCK;
1949 /*
1950 * If both valid, pick the better one, else the only good
1951 * one, else ap->rval is already set (to 0 or the inode block).
1952 */
1953 if (prevbno != NULLFSBLOCK && gotbno != NULLFSBLOCK)
1954 ap->rval = prevdiff <= gotdiff ? prevbno : gotbno;
1955 else if (prevbno != NULLFSBLOCK)
1956 ap->rval = prevbno;
1957 else if (gotbno != NULLFSBLOCK)
1958 ap->rval = gotbno;
1959 }
1960 /*
1961 * If allowed, use ap->rval; otherwise must use firstblock since
1962 * it's in the right allocation group.
1963 */
1964 if (nullfb || rt || XFS_FSB_TO_AGNO(mp, ap->rval) == fb_agno)
1965 ;
1966 else
1967 ap->rval = ap->firstblock;
1968 /*
1969 * Realtime allocation, done through xfs_rtallocate_extent.
1970 */
1971 if (rt) {
1972#ifndef __KERNEL__
1973 ASSERT(0);
1974#else
1975 xfs_rtblock_t rtb;
1976
1977 atype = ap->rval == 0 ?
1978 XFS_ALLOCTYPE_ANY_AG : XFS_ALLOCTYPE_NEAR_BNO;
1979 do_div(ap->rval, mp->m_sb.sb_rextsize);
1980 rtb = ap->rval;
1981 ap->alen = ralen;
1982 if (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
1983 &ralen, atype, ap->wasdel, prod, &rtb))
1984 return error;
1985 if (rtb == NULLFSBLOCK && prod > 1 &&
1986 (error = xfs_rtallocate_extent(ap->tp, ap->rval, 1,
1987 ap->alen, &ralen, atype,
1988 ap->wasdel, 1, &rtb)))
1989 return error;
1990 ap->rval = rtb;
1991 if (ap->rval != NULLFSBLOCK) {
1992 ap->rval *= mp->m_sb.sb_rextsize;
1993 ralen *= mp->m_sb.sb_rextsize;
1994 ap->alen = ralen;
1995 ap->ip->i_d.di_nblocks += ralen;
1996 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
1997 if (ap->wasdel)
1998 ap->ip->i_delayed_blks -= ralen;
1999 /*
2000 * Adjust the disk quota also. This was reserved
2001 * earlier.
2002 */
2003 if (XFS_IS_QUOTA_ON(mp) &&
2004 ap->ip->i_ino != mp->m_sb.sb_uquotino &&
2005 ap->ip->i_ino != mp->m_sb.sb_pquotino)
2006 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2007 ap->wasdel ?
2008 XFS_TRANS_DQ_DELRTBCOUNT :
2009 XFS_TRANS_DQ_RTBCOUNT,
2010 (long)ralen);
2011 } else
2012 ap->alen = 0;
2013#endif /* __KERNEL__ */
2014 }
2015 /*
2016 * Normal allocation, done through xfs_alloc_vextent.
2017 */
2018 else {
2019 xfs_agnumber_t ag;
2020 xfs_alloc_arg_t args;
2021 xfs_extlen_t blen;
2022 xfs_extlen_t delta;
2023 int isaligned;
2024 xfs_extlen_t longest;
2025 xfs_extlen_t need;
2026 xfs_extlen_t nextminlen;
2027 int notinit;
2028 xfs_perag_t *pag;
2029 xfs_agnumber_t startag;
2030 int tryagain;
2031
2032 tryagain = isaligned = 0;
2033 args.tp = ap->tp;
2034 args.mp = mp;
2035 args.fsbno = ap->rval;
2036 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2037 blen = 0;
2038 if (nullfb) {
2039 args.type = XFS_ALLOCTYPE_START_BNO;
2040 args.total = ap->total;
2041 /*
2042 * Find the longest available space.
2043 * We're going to try for the whole allocation at once.
2044 */
2045 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2046 notinit = 0;
2047 mrlock(&mp->m_peraglock, MR_ACCESS, PINOD);
2048 while (blen < ap->alen) {
2049 pag = &mp->m_perag[ag];
2050 if (!pag->pagf_init &&
2051 (error = xfs_alloc_pagf_init(mp, args.tp,
2052 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
2053 mrunlock(&mp->m_peraglock);
2054 return error;
2055 }
2056 /*
2057 * See xfs_alloc_fix_freelist...
2058 */
2059 if (pag->pagf_init) {
2060 need = XFS_MIN_FREELIST_PAG(pag, mp);
2061 delta = need > pag->pagf_flcount ?
2062 need - pag->pagf_flcount : 0;
2063 longest = (pag->pagf_longest > delta) ?
2064 (pag->pagf_longest - delta) :
2065 (pag->pagf_flcount > 0 ||
2066 pag->pagf_longest > 0);
2067 if (blen < longest)
2068 blen = longest;
2069 } else
2070 notinit = 1;
2071 if (++ag == mp->m_sb.sb_agcount)
2072 ag = 0;
2073 if (ag == startag)
2074 break;
2075 }
2076 mrunlock(&mp->m_peraglock);
2077 /*
2078 * Since the above loop did a BUF_TRYLOCK, it is
2079 * possible that there is space for this request.
2080 */
2081 if (notinit || blen < ap->minlen)
2082 args.minlen = ap->minlen;
2083 /*
2084 * If the best seen length is less than the request
2085 * length, use the best as the minimum.
2086 */
2087 else if (blen < ap->alen)
2088 args.minlen = blen;
2089 /*
2090 * Otherwise we've seen an extent as big as alen,
2091 * use that as the minimum.
2092 */
2093 else
2094 args.minlen = ap->alen;
2095 } else if (ap->low) {
2096 args.type = XFS_ALLOCTYPE_FIRST_AG;
2097 args.total = args.minlen = ap->minlen;
2098 } else {
2099 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2100 args.total = ap->total;
2101 args.minlen = ap->minlen;
2102 }
2103 if (ap->ip->i_d.di_extsize) {
2104 args.prod = ap->ip->i_d.di_extsize;
2105 if (args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod))
2106 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2107 } else if (mp->m_sb.sb_blocksize >= NBPP) {
2108 args.prod = 1;
2109 args.mod = 0;
2110 } else {
2111 args.prod = NBPP >> mp->m_sb.sb_blocklog;
2112 if (args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod)))
2113 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2114 }
2115 /*
2116 * If we are not low on available data blocks, and the
2117 * underlying logical volume manager is a stripe, and
2118 * the file offset is zero then try to allocate data
2119 * blocks on stripe unit boundary.
2120 * NOTE: ap->aeof is only set if the allocation length
2121 * is >= the stripe unit and the allocation offset is
2122 * at the end of file.
2123 */
2124 if (!ap->low && ap->aeof) {
2125 if (!ap->off) {
2126 args.alignment = mp->m_dalign;
2127 atype = args.type;
2128 isaligned = 1;
2129 /*
2130 * Adjust for alignment
2131 */
2132 if (blen > args.alignment && blen <= ap->alen)
2133 args.minlen = blen - args.alignment;
2134 args.minalignslop = 0;
2135 } else {
2136 /*
2137 * First try an exact bno allocation.
2138 * If it fails then do a near or start bno
2139 * allocation with alignment turned on.
2140 */
2141 atype = args.type;
2142 tryagain = 1;
2143 args.type = XFS_ALLOCTYPE_THIS_BNO;
2144 args.alignment = 1;
2145 /*
2146 * Compute the minlen+alignment for the
2147 * next case. Set slop so that the value
2148 * of minlen+alignment+slop doesn't go up
2149 * between the calls.
2150 */
2151 if (blen > mp->m_dalign && blen <= ap->alen)
2152 nextminlen = blen - mp->m_dalign;
2153 else
2154 nextminlen = args.minlen;
2155 if (nextminlen + mp->m_dalign > args.minlen + 1)
2156 args.minalignslop =
2157 nextminlen + mp->m_dalign -
2158 args.minlen - 1;
2159 else
2160 args.minalignslop = 0;
2161 }
2162 } else {
2163 args.alignment = 1;
2164 args.minalignslop = 0;
2165 }
2166 args.minleft = ap->minleft;
2167 args.wasdel = ap->wasdel;
2168 args.isfl = 0;
2169 args.userdata = ap->userdata;
2170 if (error = xfs_alloc_vextent(&args))
2171 return error;
2172 if (tryagain && args.fsbno == NULLFSBLOCK) {
2173 /*
2174 * Exact allocation failed. Now try with alignment
2175 * turned on.
2176 */
2177 args.type = atype;
2178 args.fsbno = ap->rval;
2179 args.alignment = mp->m_dalign;
2180 args.minlen = nextminlen;
2181 args.minalignslop = 0;
2182 isaligned = 1;
2183 if (error = xfs_alloc_vextent(&args))
2184 return error;
2185 }
2186 if (isaligned && args.fsbno == NULLFSBLOCK) {
2187 /*
2188 * allocation failed, so turn off alignment and
2189 * try again.
2190 */
2191 args.type = atype;
2192 args.fsbno = ap->rval;
2193 args.alignment = 0;
2194 if (error = xfs_alloc_vextent(&args))
2195 return error;
2196 }
2197 if (args.fsbno == NULLFSBLOCK && nullfb &&
2198 args.minlen > ap->minlen) {
2199 args.minlen = ap->minlen;
2200 args.type = XFS_ALLOCTYPE_START_BNO;
2201 args.fsbno = ap->rval;
2202 if (error = xfs_alloc_vextent(&args))
2203 return error;
2204 }
2205 if (args.fsbno == NULLFSBLOCK && nullfb) {
2206 args.fsbno = 0;
2207 args.type = XFS_ALLOCTYPE_FIRST_AG;
2208 args.total = ap->minlen;
2209 args.minleft = 0;
2210 if (error = xfs_alloc_vextent(&args))
2211 return error;
2212 ap->low = 1;
2213 }
2214 if (args.fsbno != NULLFSBLOCK) {
2215 ap->firstblock = ap->rval = args.fsbno;
2216 ASSERT(nullfb || fb_agno == args.agno ||
2217 (ap->low && fb_agno < args.agno));
2218 ap->alen = args.len;
2219 ap->ip->i_d.di_nblocks += args.len;
2220 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2221 if (ap->wasdel)
2222 ap->ip->i_delayed_blks -= args.len;
2223 /*
2224 * Adjust the disk quota also. This was reserved
2225 * earlier.
2226 */
2227 if (XFS_IS_QUOTA_ON(mp) &&
2228 ap->ip->i_ino != mp->m_sb.sb_uquotino &&
2229 ap->ip->i_ino != mp->m_sb.sb_pquotino)
2230 xfs_trans_mod_dquot_byino(ap->tp, ap->ip,
2231 ap->wasdel ?
2232 XFS_TRANS_DQ_DELBCOUNT :
2233 XFS_TRANS_DQ_BCOUNT,
2234 (long)args.len);
2235 } else {
2236 ap->rval = NULLFSBLOCK;
2237 ap->alen = 0;
2238 }
2239 }
2240 return 0;
2241#undef ISLEGAL
2242}
2243
2244/*
2245 * Transform a btree format file with only one leaf node, where the
2246 * extents list will fit in the inode, into an extents format file.
2247 * Since the extent list is already in-core, all we have to do is
2248 * give up the space for the btree root and pitch the leaf block.
2249 */
2250STATIC int /* error */
2251xfs_bmap_btree_to_extents(
2252 xfs_trans_t *tp, /* transaction pointer */
2253 xfs_inode_t *ip, /* incore inode pointer */
2254 xfs_btree_cur_t *cur, /* btree cursor */
2255 int *logflagsp, /* inode logging flags */
2256 int whichfork, /* data or attr fork */
2257 int async) /* xaction can be async */
2258{
2259 /* REFERENCED */
2260 xfs_bmbt_block_t *cblock;/* child btree block */
2261 xfs_fsblock_t cbno; /* child block number */
2262 xfs_buf_t *cbp; /* child block's buffer */
2263 int error; /* error return value */
2264 xfs_ifork_t *ifp; /* inode fork data */
2265 xfs_mount_t *mp; /* mount point structure */
2266 xfs_bmbt_ptr_t *pp; /* ptr to block address */
2267 xfs_bmbt_block_t *rblock;/* root btree block */
2268
2269 ifp = XFS_IFORK_PTR(ip, whichfork);
2270 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2271 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2272 rblock = ifp->if_broot;
2273 ASSERT(INT_GET(rblock->bb_level, ARCH_CONVERT) == 1);
2274 ASSERT(INT_GET(rblock->bb_numrecs, ARCH_CONVERT) == 1);
2275 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
2276 mp = ip->i_mount;
2277 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
2278 *logflagsp = 0;
2279#ifdef DEBUG
2280 if (error = xfs_btree_check_lptr(cur, INT_GET(*pp, ARCH_CONVERT), 1))
2281 return error;
2282#endif
2283 cbno = INT_GET(*pp, ARCH_CONVERT);
2284 if (error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2285 XFS_BMAP_BTREE_REF))
2286 return error;
2287 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
2288 if (error = xfs_btree_check_lblock(cur, cblock, 0, cbp))
2289 return error;
2290 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2291 if (!async)
2292 xfs_trans_set_sync(tp);
2293 ip->i_d.di_nblocks--;
2294 if (XFS_IS_QUOTA_ON(mp) &&
2295 ip->i_ino != mp->m_sb.sb_uquotino &&
2296 ip->i_ino != mp->m_sb.sb_pquotino)
2297 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2298 xfs_trans_binval(tp, cbp);
2299 if (cur->bc_bufs[0] == cbp)
2300 cur->bc_bufs[0] = NULL;
2301 xfs_iroot_realloc(ip, -1, whichfork);
2302 ASSERT(ifp->if_broot == NULL);
2303 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2304 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2305 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2306 return 0;
2307}
2308
2309/*
2310 * Called by xfs_bmapi to update extent list structure and the btree
2311 * after removing space (or undoing a delayed allocation).
2312 */
2313STATIC int /* error */
2314xfs_bmap_del_extent(
2315 xfs_inode_t *ip, /* incore inode pointer */
2316 xfs_trans_t *tp, /* current transaction pointer */
2317 xfs_extnum_t idx, /* extent number to update/delete */
2318 xfs_bmap_free_t *flist, /* list of extents to be freed */
2319 xfs_btree_cur_t *cur, /* if null, not a btree */
2320 xfs_bmbt_irec_t *del, /* data to remove from extent list */
2321 int iflags, /* input flags */
2322 int *logflagsp, /* inode logging flags */
2323 int whichfork, /* data or attr fork */
2324 int rsvd) /* OK to allocate reserved blocks */
2325{
2326 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2327 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2328 xfs_fsblock_t del_endblock; /* first block past del */
2329 xfs_fileoff_t del_endoff; /* first offset past del */
2330 int delay; /* current block is delayed allocated */
2331 int do_fx; /* free extent at end of routine */
2332 xfs_bmbt_rec_t *ep; /* current extent entry pointer */
2333 int error; /* error return value */
2334 int flags; /* inode logging flags */
2335#ifdef XFS_BMAP_TRACE
2336 static char fname[] = "xfs_bmap_del_extent";
2337#endif
2338 xfs_bmbt_irec_t got; /* current extent entry */
2339 xfs_fileoff_t got_endoff; /* first offset past got */
2340 int i; /* temp state */
2341 xfs_ifork_t *ifp; /* inode fork pointer */
2342 xfs_mount_t *mp; /* mount structure */
2343 xfs_filblks_t nblks; /* quota/sb block count */
2344 xfs_bmbt_irec_t new; /* new record to be inserted */
2345 /* REFERENCED */
2346 xfs_extnum_t nextents; /* number of extents in list */
2347 uint qfield; /* quota field to update */
2348 xfs_filblks_t temp; /* for indirect length calculations */
2349 xfs_filblks_t temp2; /* for indirect length calculations */
2350
2351 XFS_STATS_INC(xs_del_exlist);
2352 mp = ip->i_mount;
2353 ifp = XFS_IFORK_PTR(ip, whichfork);
2354 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2355 ASSERT(idx >= 0 && idx < nextents);
2356 ASSERT(del->br_blockcount > 0);
2357 ep = &ifp->if_u1.if_extents[idx];
2358 xfs_bmbt_get_all(ep, &got);
2359 ASSERT(got.br_startoff <= del->br_startoff);
2360 del_endoff = del->br_startoff + del->br_blockcount;
2361 got_endoff = got.br_startoff + got.br_blockcount;
2362 ASSERT(got_endoff >= del_endoff);
2363 delay = ISNULLSTARTBLOCK(got.br_startblock);
2364 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
2365 flags = 0;
2366 qfield = 0;
2367 error = 0;
2368 /*
2369 * If deleting a real allocation, must free up the disk space.
2370 */
2371 if (!delay) {
2372 flags = XFS_ILOG_CORE;
2373 /*
2374 * Realtime allocation. Free it and record di_nblocks update.
2375 */
2376 if (whichfork == XFS_DATA_FORK &&
2377 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
2378 xfs_fsblock_t bno;
2379 xfs_filblks_t len;
2380
2381 ASSERT(do_mod(del->br_blockcount,
2382 mp->m_sb.sb_rextsize) == 0);
2383 ASSERT(do_mod(del->br_startblock,
2384 mp->m_sb.sb_rextsize) == 0);
2385 bno = del->br_startblock;
2386 do_div(bno, mp->m_sb.sb_rextsize);
2387 len = del->br_blockcount;
2388 do_div(len, mp->m_sb.sb_rextsize);
2389 if (error = xfs_rtfree_extent(ip->i_transp, bno,
2390 (xfs_extlen_t)len))
2391 goto done;
2392 do_fx = 0;
2393 nblks = len * mp->m_sb.sb_rextsize;
2394 if (XFS_IS_QUOTA_ON(mp) &&
2395 ip->i_ino != mp->m_sb.sb_uquotino &&
2396 ip->i_ino != mp->m_sb.sb_pquotino)
2397 qfield = XFS_TRANS_DQ_RTBCOUNT;
2398 }
2399 /*
2400 * Ordinary allocation.
2401 */
2402 else {
2403 do_fx = 1;
2404 nblks = del->br_blockcount;
2405 if (XFS_IS_QUOTA_ON(mp) &&
2406 ip->i_ino != mp->m_sb.sb_uquotino &&
2407 ip->i_ino != mp->m_sb.sb_pquotino)
2408 qfield = XFS_TRANS_DQ_BCOUNT;
2409 /*
2410 * If we're freeing meta-data, then the transaction
2411 * that frees the blocks must be synchronous. This
2412 * ensures that noone can reuse the blocks before
2413 * they are permanently free. For regular data
2414 * it is the callers responsibility to make the
2415 * data permanently inaccessible before calling
2416 * here to free it.
2417 */
2418 if (iflags & XFS_BMAPI_METADATA)
2419 xfs_trans_set_sync(tp);
2420 }
2421 /*
2422 * Set up del_endblock and cur for later.
2423 */
2424 del_endblock = del->br_startblock + del->br_blockcount;
2425 if (cur) {
2426 if (error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2427 got.br_startblock, got.br_blockcount,
2428 &i))
2429 goto done;
2430 ASSERT(i == 1);
2431 }
2432 da_old = da_new = 0;
2433 } else {
2434 da_old = STARTBLOCKVAL(got.br_startblock);
2435 da_new = 0;
2436 nblks = 0;
2437 do_fx = 0;
2438 }
2439 /*
2440 * Set flag value to use in switch statement.
2441 * Left-contig is 2, right-contig is 1.
2442 */
2443 switch (((got.br_startoff == del->br_startoff) << 1) |
2444 (got_endoff == del_endoff)) {
2445 case 3:
2446 /*
2447 * Matches the whole extent. Delete the entry.
2448 */
2449 xfs_bmap_trace_delete(fname, "3", ip, idx, 1, whichfork);
2450 xfs_bmap_delete_exlist(ip, idx, 1, whichfork);
2451 ifp->if_lastex = idx;
2452 if (delay)
2453 break;
2454 XFS_IFORK_NEXT_SET(ip, whichfork,
2455 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2456 flags |= XFS_ILOG_CORE;
2457 if (!cur) {
2458 flags |= XFS_ILOG_FEXT(whichfork);
2459 break;
2460 }
2461 if (error = xfs_bmbt_delete(cur, iflags & XFS_BMAPI_ASYNC, &i))
2462 goto done;
2463 ASSERT(i == 1);
2464 break;
2465
2466 case 2:
2467 /*
2468 * Deleting the first part of the extent.
2469 */
2470 xfs_bmap_trace_pre_update(fname, "2", ip, idx, whichfork);
2471 xfs_bmbt_set_startoff(ep, del_endoff);
2472 temp = got.br_blockcount - del->br_blockcount;
2473 xfs_bmbt_set_blockcount(ep, temp);
2474 ifp->if_lastex = idx;
2475 if (delay) {
2476 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2477 da_old);
2478 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
2479 xfs_bmap_trace_post_update(fname, "2", ip, idx,
2480 whichfork);
2481 da_new = temp;
2482 break;
2483 }
2484 xfs_bmbt_set_startblock(ep, del_endblock);
2485 xfs_bmap_trace_post_update(fname, "2", ip, idx, whichfork);
2486 if (!cur) {
2487 flags |= XFS_ILOG_FEXT(whichfork);
2488 break;
2489 }
2490 if (error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2491 got.br_blockcount - del->br_blockcount,
2492 got.br_state))
2493 goto done;
2494 break;
2495
2496 case 1:
2497 /*
2498 * Deleting the last part of the extent.
2499 */
2500 temp = got.br_blockcount - del->br_blockcount;
2501 xfs_bmap_trace_pre_update(fname, "1", ip, idx, whichfork);
2502 xfs_bmbt_set_blockcount(ep, temp);
2503 ifp->if_lastex = idx;
2504 if (delay) {
2505 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2506 da_old);
2507 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
2508 xfs_bmap_trace_post_update(fname, "1", ip, idx,
2509 whichfork);
2510 da_new = temp;
2511 break;
2512 }
2513 xfs_bmap_trace_post_update(fname, "1", ip, idx, whichfork);
2514 if (!cur) {
2515 flags |= XFS_ILOG_FEXT(whichfork);
2516 break;
2517 }
2518 if (error = xfs_bmbt_update(cur, got.br_startoff,
2519 got.br_startblock,
2520 got.br_blockcount - del->br_blockcount,
2521 got.br_state))
2522 goto done;
2523 break;
2524
2525 case 0:
2526 /*
2527 * Deleting the middle of the extent.
2528 */
2529 temp = del->br_startoff - got.br_startoff;
2530 xfs_bmap_trace_pre_update(fname, "0", ip, idx, whichfork);
2531 xfs_bmbt_set_blockcount(ep, temp);
2532 new.br_startoff = del_endoff;
2533 temp2 = got_endoff - del_endoff;
2534 new.br_blockcount = temp2;
2535 new.br_state = got.br_state;
2536 if (!delay) {
2537 new.br_startblock = del_endblock;
2538 flags |= XFS_ILOG_CORE;
2539 if (cur) {
2540 if (error = xfs_bmbt_update(cur,
2541 got.br_startoff,
2542 got.br_startblock, temp,
2543 got.br_state))
2544 goto done;
2545 if (error = xfs_bmbt_increment(cur, 0, &i))
2546 goto done;
2547 cur->bc_rec.b = new;
2548 error = xfs_bmbt_insert(cur, &i);
2549 if (error && error != ENOSPC)
2550 goto done;
2551 /*
2552 * If get no-space back from btree insert,
2553 * it tried a split, and we have a zero
2554 * block reservation.
2555 * Fix up our state and return the error.
2556 */
2557 if (error == ENOSPC) {
2558 /*
2559 * Reset the cursor, don't trust
2560 * it after any insert operation.
2561 */
2562 if (error = xfs_bmbt_lookup_eq(cur,
2563 got.br_startoff,
2564 got.br_startblock,
2565 temp, &i))
2566 goto done;
2567 ASSERT(i == 1);
2568 /*
2569 * Update the btree record back
2570 * to the original value.
2571 */
2572 if (error = xfs_bmbt_update(cur,
2573 got.br_startoff,
2574 got.br_startblock,
2575 got.br_blockcount,
2576 got.br_state))
2577 goto done;
2578 /*
2579 * Reset the extent record back
2580 * to the original value.
2581 */
2582 xfs_bmbt_set_blockcount(ep,
2583 got.br_blockcount);
2584 flags = 0;
2585 error = XFS_ERROR(ENOSPC);
2586 goto done;
2587 }
2588 ASSERT(i == 1);
2589 } else
2590 flags |= XFS_ILOG_FEXT(whichfork);
2591 XFS_IFORK_NEXT_SET(ip, whichfork,
2592 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2593 } else {
2594 ASSERT(whichfork == XFS_DATA_FORK);
2595 temp = xfs_bmap_worst_indlen(ip, temp);
2596 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
2597 temp2 = xfs_bmap_worst_indlen(ip, temp2);
2598 new.br_startblock = NULLSTARTBLOCK((int)temp2);
2599 da_new = temp + temp2;
2600 while (da_new > da_old) {
2601 if (temp) {
2602 temp--;
2603 da_new--;
2604 xfs_bmbt_set_startblock(ep,
2605 NULLSTARTBLOCK((int)temp));
2606 }
2607 if (da_new == da_old)
2608 break;
2609 if (temp2) {
2610 temp2--;
2611 da_new--;
2612 new.br_startblock =
2613 NULLSTARTBLOCK((int)temp2);
2614 }
2615 }
2616 }
2617 xfs_bmap_trace_post_update(fname, "0", ip, idx, whichfork);
2618 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 1, &new, NULL,
2619 whichfork);
2620 xfs_bmap_insert_exlist(ip, idx + 1, 1, &new, whichfork);
2621 ifp->if_lastex = idx + 1;
2622 break;
2623 }
2624 /*
2625 * If we need to, add to list of extents to delete.
2626 */
2627 if (do_fx)
2628 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
2629 mp);
2630 /*
2631 * Adjust inode # blocks in the file.
2632 */
2633 if (nblks)
2634 ip->i_d.di_nblocks -= nblks;
2635 /*
2636 * Adjust quota data.
2637 */
2638 if (qfield)
2639 xfs_trans_mod_dquot_byino(tp, ip, qfield, (long)-nblks);
2640 /*
2641 * Account for change in delayed indirect blocks.
2642 * Nothing to do for disk quota accounting here.
2643 */
2644 ASSERT(da_old >= da_new);
2645 if (da_old > da_new)
2646 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int)(da_old - da_new),
2647 rsvd);
2648done:
2649 *logflagsp = flags;
2650 return error;
2651}
2652
2653/*
2654 * Remove the entry "free" from the free item list. Prev points to the
2655 * previous entry, unless "free" is the head of the list.
2656 */
2657STATIC void
2658xfs_bmap_del_free(
2659 xfs_bmap_free_t *flist, /* free item list header */
2660 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
2661 xfs_bmap_free_item_t *free) /* list item to be freed */
2662{
2663 if (prev)
2664 prev->xbfi_next = free->xbfi_next;
2665 else
2666 flist->xbf_first = free->xbfi_next;
2667 flist->xbf_count--;
2668 kmem_zone_free(xfs_bmap_free_item_zone, free);
2669}
2670
2671/*
2672 * Remove count entries from the extents array for inode "ip", starting
2673 * at index "idx". Copies the remaining items down over the deleted ones,
2674 * and gives back the excess memory.
2675 */
2676STATIC void
2677xfs_bmap_delete_exlist(
2678 xfs_inode_t *ip, /* incore inode pointer */
2679 xfs_extnum_t idx, /* starting delete index */
2680 xfs_extnum_t count, /* count of items to delete */
2681 int whichfork) /* data or attr fork */
2682{
2683 xfs_bmbt_rec_t *base; /* base of extent list */
2684 xfs_ifork_t *ifp; /* inode fork pointer */
2685 xfs_extnum_t nextents; /* number of extents in list after */
2686
2687 ifp = XFS_IFORK_PTR(ip, whichfork);
2688 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2689 base = ifp->if_u1.if_extents;
2690 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - count;
2691 ovbcopy(&base[idx + count], &base[idx],
2692 (nextents - idx) * sizeof(*base));
2693 xfs_iext_realloc(ip, -count, whichfork);
2694}
2695
2696/*
2697 * Convert an extents-format file into a btree-format file.
2698 * The new file will have a root block (in the inode) and a single child block.
2699 */
2700STATIC int /* error */
2701xfs_bmap_extents_to_btree(
2702 xfs_trans_t *tp, /* transaction pointer */
2703 xfs_inode_t *ip, /* incore inode pointer */
2704 xfs_fsblock_t *firstblock, /* first-block-allocated */
2705 xfs_bmap_free_t *flist, /* blocks freed in xaction */
2706 xfs_btree_cur_t **curp, /* cursor returned to caller */
2707 int wasdel, /* converting a delayed alloc */
2708 int *logflagsp, /* inode logging flags */
2709 int whichfork) /* data or attr fork */
2710{
2711 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
2712 xfs_buf_t *abp; /* buffer for ablock */
2713 xfs_alloc_arg_t args; /* allocation arguments */
2714 xfs_bmbt_rec_t *arp; /* child record pointer */
2715 xfs_bmbt_block_t *block; /* btree root block */
2716 xfs_btree_cur_t *cur; /* bmap btree cursor */
2717 xfs_bmbt_rec_t *ep; /* extent list pointer */
2718 int error; /* error return value */
2719 xfs_extnum_t i; /* extent list index */
2720 xfs_ifork_t *ifp; /* inode fork pointer */
2721 xfs_bmbt_key_t *kp; /* root block key pointer */
2722 xfs_mount_t *mp; /* mount structure */
2723 xfs_extnum_t nextents; /* extent list size */
2724 xfs_bmbt_ptr_t *pp; /* root block address pointer */
2725
2726 ifp = XFS_IFORK_PTR(ip, whichfork);
2727 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
2728 ASSERT(ifp->if_ext_max ==
2729 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
2730 /*
2731 * Make space in the inode incore.
2732 */
2733 xfs_iroot_realloc(ip, 1, whichfork);
2734 ifp->if_flags |= XFS_IFBROOT;
2735 /*
2736 * Fill in the root.
2737 */
2738 block = ifp->if_broot;
2739 INT_SET(block->bb_magic, ARCH_CONVERT, XFS_BMAP_MAGIC);
2740 INT_SET(block->bb_level, ARCH_CONVERT, 1);
2741 INT_SET(block->bb_numrecs, ARCH_CONVERT, 1);
2742 INT_SET(block->bb_leftsib, ARCH_CONVERT, NULLDFSBNO);
2743 INT_SET(block->bb_rightsib, ARCH_CONVERT, NULLDFSBNO);
2744 /*
2745 * Need a cursor. Can't allocate until bb_level is filled in.
2746 */
2747 mp = ip->i_mount;
2748 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
2749 whichfork);
2750 cur->bc_private.b.firstblock = *firstblock;
2751 cur->bc_private.b.flist = flist;
2752 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
2753 /*
2754 * Convert to a btree with two levels, one record in root.
2755 */
2756 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
2757 args.tp = tp;
2758 args.mp = mp;
2759 if (*firstblock == NULLFSBLOCK) {
2760 args.type = XFS_ALLOCTYPE_START_BNO;
2761 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
2762 } else if (flist->xbf_low) {
2763 args.type = XFS_ALLOCTYPE_START_BNO;
2764 args.fsbno = *firstblock;
2765 } else {
2766 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2767 args.fsbno = *firstblock;
2768 }
2769 args.minlen = args.maxlen = args.prod = 1;
2770 args.total = args.minleft = args.alignment = args.mod = args.isfl =
2771 args.minalignslop = 0;
2772 args.wasdel = wasdel;
2773 *logflagsp = 0;
2774 if (error = xfs_alloc_vextent(&args)) {
2775 xfs_iroot_realloc(ip, -1, whichfork);
2776 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
2777 return error;
2778 }
2779 /*
2780 * Allocation can't fail, the space was reserved.
2781 */
2782 ASSERT(args.fsbno != NULLFSBLOCK);
2783 ASSERT(*firstblock == NULLFSBLOCK ||
2784 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
2785 (flist->xbf_low &&
2786 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
2787 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
2788 cur->bc_private.b.allocated++;
2789 ip->i_d.di_nblocks++;
2790 if (XFS_IS_QUOTA_ON(mp) &&
2791 ip->i_ino != mp->m_sb.sb_uquotino &&
2792 ip->i_ino != mp->m_sb.sb_pquotino)
2793 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
2794 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
2795 /*
2796 * Fill in the child block.
2797 */
2798 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
2799 INT_SET(ablock->bb_magic, ARCH_CONVERT, XFS_BMAP_MAGIC);
2800 INT_ZERO(ablock->bb_level, ARCH_CONVERT);
2801 INT_ZERO(ablock->bb_numrecs, ARCH_CONVERT);
2802 INT_SET(ablock->bb_leftsib, ARCH_CONVERT, NULLDFSBNO);
2803 INT_SET(ablock->bb_rightsib, ARCH_CONVERT, NULLDFSBNO);
2804 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
2805 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2806 for (ep = ifp->if_u1.if_extents, i = 0; i < nextents; i++, ep++) {
2807 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
2808 *arp++ = *ep;
2809 INT_MOD(ablock->bb_numrecs, ARCH_CONVERT, +1);
2810 }
2811 }
2812 ASSERT(INT_GET(ablock->bb_numrecs, ARCH_CONVERT) == XFS_IFORK_NEXTENTS(ip, whichfork));
2813 /*
2814 * Fill in the root key and pointer.
2815 */
2816 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
2817 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
2818 INT_SET(kp->br_startoff, ARCH_CONVERT, xfs_bmbt_get_startoff(arp));
2819 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
2820 INT_SET(*pp, ARCH_CONVERT, args.fsbno);
2821 /*
2822 * Do all this logging at the end so that
2823 * the root is at the right level.
2824 */
2825 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
2826 xfs_bmbt_log_recs(cur, abp, 1, INT_GET(ablock->bb_numrecs, ARCH_CONVERT));
2827 ASSERT(*curp == NULL);
2828 *curp = cur;
2829 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
2830 return 0;
2831}
2832
2833/*
2834 * Insert new item(s) in the extent list for inode "ip".
2835 * Count new items are inserted at offset idx.
2836 */
2837STATIC void
2838xfs_bmap_insert_exlist(
2839 xfs_inode_t *ip, /* incore inode pointer */
2840 xfs_extnum_t idx, /* starting index of new items */
2841 xfs_extnum_t count, /* number of inserted items */
2842 xfs_bmbt_irec_t *new, /* items to insert */
2843 int whichfork) /* data or attr fork */
2844{
2845 xfs_bmbt_rec_t *base; /* extent list base */
2846 xfs_ifork_t *ifp; /* inode fork pointer */
2847 xfs_extnum_t nextents; /* extent list size */
2848 xfs_extnum_t to; /* extent list index */
2849
2850 ifp = XFS_IFORK_PTR(ip, whichfork);
2851 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2852 xfs_iext_realloc(ip, count, whichfork);
2853 base = ifp->if_u1.if_extents;
2854 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2855 ovbcopy(&base[idx], &base[idx + count],
2856 (nextents - (idx + count)) * sizeof(*base));
2857 for (to = idx; to < idx + count; to++, new++)
2858 xfs_bmbt_set_all(&base[to], new);
2859}
2860
2861/*
2862 * Convert a local file to an extents file.
2863 * This code is out of bounds for data forks of regular files,
2864 * since the file data needs to get logged so things will stay consistent.
2865 * (The bmap-level manipulations are ok, though).
2866 */
2867STATIC int /* error */
2868xfs_bmap_local_to_extents(
2869 xfs_trans_t *tp, /* transaction pointer */
2870 xfs_inode_t *ip, /* incore inode pointer */
2871 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
2872 xfs_extlen_t total, /* total blocks needed by transaction */
2873 int *logflagsp, /* inode logging flags */
2874 int whichfork) /* data or attr fork */
2875{
2876 int error; /* error return value */
2877 int flags; /* logging flags returned */
2878#ifdef XFS_BMAP_TRACE
2879 static char fname[] = "xfs_bmap_local_to_extents";
2880#endif
2881 xfs_ifork_t *ifp; /* inode fork pointer */
2882
2883 /*
2884 * We don't want to deal with the case of keeping inode data inline yet.
2885 * So sending the data fork of a regular inode is illegal.
2886 */
2887 ASSERT(!((ip->i_d.di_mode & IFMT) == IFREG &&
2888 whichfork == XFS_DATA_FORK));
2889 ifp = XFS_IFORK_PTR(ip, whichfork);
2890 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
2891 flags = 0;
2892 error = 0;
2893 if (ifp->if_bytes) {
2894 xfs_alloc_arg_t args; /* allocation arguments */
2895 xfs_buf_t *bp; /* buffer for extent list block */
2896 xfs_bmbt_rec_t *ep; /* extent list pointer */
2897
2898 args.tp = tp;
2899 args.mp = ip->i_mount;
2900 ASSERT(ifp->if_flags & XFS_IFINLINE);
2901 /*
2902 * Allocate a block. We know we need only one, since the
2903 * file currently fits in an inode.
2904 */
2905 if (*firstblock == NULLFSBLOCK) {
2906 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
2907 args.type = XFS_ALLOCTYPE_START_BNO;
2908 } else {
2909 args.fsbno = *firstblock;
2910 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2911 }
2912 args.total = total;
2913 args.mod = args.minleft = args.alignment = args.wasdel =
2914 args.isfl = args.minalignslop = 0;
2915 args.minlen = args.maxlen = args.prod = 1;
2916 if (error = xfs_alloc_vextent(&args))
2917 goto done;
2918 /*
2919 * Can't fail, the space was reserved.
2920 */
2921 ASSERT(args.fsbno != NULLFSBLOCK);
2922 ASSERT(args.len == 1);
2923 *firstblock = args.fsbno;
2924 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
2925 bcopy(ifp->if_u1.if_data, (char *)XFS_BUF_PTR(bp),
2926 ifp->if_bytes);
2927 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
2928 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
2929 xfs_iext_realloc(ip, 1, whichfork);
2930 ep = ifp->if_u1.if_extents;
2931 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
2932 xfs_bmap_trace_post_update(fname, "new", ip, 0, whichfork);
2933 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
2934 ip->i_d.di_nblocks = 1;
2935 if (XFS_IS_QUOTA_ON(args.mp) &&
2936 ip->i_ino != args.mp->m_sb.sb_uquotino &&
2937 ip->i_ino != args.mp->m_sb.sb_pquotino)
2938 xfs_trans_mod_dquot_byino(tp, ip, XFS_TRANS_DQ_BCOUNT,
2939 1L);
2940 flags |= XFS_ILOG_FEXT(whichfork);
2941 } else
2942 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
2943 ifp->if_flags &= ~XFS_IFINLINE;
2944 ifp->if_flags |= XFS_IFEXTENTS;
2945 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2946 flags |= XFS_ILOG_CORE;
2947done:
2948 *logflagsp = flags;
2949 return error;
2950}
2951
2952xfs_bmbt_rec_t * /* pointer to found extent entry */
2953xfs_bmap_do_search_extents(
2954 xfs_bmbt_rec_t *base, /* base of extent list */
2955 xfs_extnum_t lastx, /* last extent index used */
2956 xfs_extnum_t nextents, /* extent list size */
2957 xfs_fileoff_t bno, /* block number searched for */
2958 int *eofp, /* out: end of file found */
2959 xfs_extnum_t *lastxp, /* out: last extent index */
2960 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
2961 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
2962{
2963 xfs_bmbt_rec_t *ep; /* extent list entry pointer */
2964 xfs_bmbt_irec_t got; /* extent list entry, decoded */
2965 int high; /* high index of binary search */
2966 int low; /* low index of binary search */
2967
2968 if (lastx != NULLEXTNUM && lastx < nextents)
2969 ep = base + lastx;
2970 else
2971 ep = NULL;
2972 prevp->br_startoff = NULLFILEOFF;
2973 if (ep && bno >= (got.br_startoff = xfs_bmbt_get_startoff(ep)) &&
2974 bno < got.br_startoff +
2975 (got.br_blockcount = xfs_bmbt_get_blockcount(ep)))
2976 *eofp = 0;
2977 else if (ep && lastx < nextents - 1 &&
2978 bno >= (got.br_startoff = xfs_bmbt_get_startoff(ep + 1)) &&
2979 bno < got.br_startoff +
2980 (got.br_blockcount = xfs_bmbt_get_blockcount(ep + 1))) {
2981 lastx++;
2982 ep++;
2983 *eofp = 0;
2984 } else if (nextents == 0)
2985 *eofp = 1;
2986 else if (bno == 0 &&
2987 (got.br_startoff = xfs_bmbt_get_startoff(base)) == 0) {
2988 ep = base;
2989 lastx = 0;
2990 got.br_blockcount = xfs_bmbt_get_blockcount(ep);
2991 *eofp = 0;
2992 } else {
2993 /* binary search the extents array */
2994 low = 0;
2995 high = nextents - 1;
2996 while (low <= high) {
2997 XFS_STATS_INC(xs_cmp_exlist);
2998 lastx = (low + high) >> 1;
2999 ep = base + lastx;
3000 got.br_startoff = xfs_bmbt_get_startoff(ep);
3001 got.br_blockcount = xfs_bmbt_get_blockcount(ep);
3002 if (bno < got.br_startoff)
3003 high = lastx - 1;
3004 else if (bno >= got.br_startoff + got.br_blockcount)
3005 low = lastx + 1;
3006 else {
3007 got.br_startblock = xfs_bmbt_get_startblock(ep);
3008 got.br_state = xfs_bmbt_get_state(ep);
3009 *eofp = 0;
3010 *lastxp = lastx;
3011 *gotp = got;
3012 return ep;
3013 }
3014 }
3015 if (bno >= got.br_startoff + got.br_blockcount) {
3016 lastx++;
3017 if (lastx == nextents) {
3018 *eofp = 1;
3019 got.br_startblock = xfs_bmbt_get_startblock(ep);
3020 got.br_state = xfs_bmbt_get_state(ep);
3021 *prevp = got;
3022 ep = NULL;
3023 } else {
3024 *eofp = 0;
3025 xfs_bmbt_get_all(ep, prevp);
3026 ep++;
3027 got.br_startoff = xfs_bmbt_get_startoff(ep);
3028 got.br_blockcount = xfs_bmbt_get_blockcount(ep);
3029 }
3030 } else {
3031 *eofp = 0;
3032 if (ep > base)
3033 xfs_bmbt_get_all(ep - 1, prevp);
3034 }
3035 }
3036 if (ep) {
3037 got.br_startblock = xfs_bmbt_get_startblock(ep);
3038 got.br_state = xfs_bmbt_get_state(ep);
3039 }
3040 *lastxp = lastx;
3041 *gotp = got;
3042 return ep;
3043}
3044
3045/*
3046 * Search the extents list for the inode, for the extent containing bno.
3047 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3048 * *eofp will be set, and *prevp will contain the last entry (null if none).
3049 * Else, *lastxp will be set to the index of the found
3050 * entry; *gotp will contain the entry.
3051 */
3052STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
3053xfs_bmap_search_extents(
3054 xfs_inode_t *ip, /* incore inode pointer */
3055 xfs_fileoff_t bno, /* block number searched for */
3056 int whichfork, /* data or attr fork */
3057 int *eofp, /* out: end of file found */
3058 xfs_extnum_t *lastxp, /* out: last extent index */
3059 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3060 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
3061{
3062 xfs_ifork_t *ifp; /* inode fork pointer */
3063 xfs_bmbt_rec_t *base; /* base of extent list */
3064 xfs_extnum_t lastx; /* last extent index used */
3065 xfs_extnum_t nextents; /* extent list size */
3066
3067 XFS_STATS_INC(xs_look_exlist);
3068 ifp = XFS_IFORK_PTR(ip, whichfork);
3069 lastx = ifp->if_lastex;
3070 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3071 base = &ifp->if_u1.if_extents[0];
3072
3073 return xfs_bmap_do_search_extents(base, lastx, nextents, bno, eofp,
3074 lastxp, gotp, prevp);
3075}
3076
3077/*
3078 * Compute the worst-case number of indirect blocks that will be used
3079 * for ip's delayed extent of length "len".
3080 */
3081STATIC xfs_filblks_t
3082xfs_bmap_worst_indlen(
3083 xfs_inode_t *ip, /* incore inode pointer */
3084 xfs_filblks_t len) /* delayed extent length */
3085{
3086 int level; /* btree level number */
3087 int maxrecs; /* maximum record count at this level */
3088 xfs_mount_t *mp; /* mount structure */
3089 xfs_filblks_t rval; /* return value */
3090
3091 mp = ip->i_mount;
3092 maxrecs = mp->m_bmap_dmxr[0];
3093 for (level = 0, rval = 0;
3094 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3095 level++) {
3096 len += maxrecs - 1;
3097 do_div(len, maxrecs);
3098 rval += len;
3099 if (len == 1)
3100 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3101 level - 1;
3102 if (level == 0)
3103 maxrecs = mp->m_bmap_dmxr[1];
3104 }
3105 return rval;
3106}
3107
3108/*
3109 * Add the extent to the list of extents to be free at transaction end.
3110 * The list is maintained sorted (by block number).
3111 */
3112/* ARGSUSED */
3113void
3114xfs_bmap_add_free(
3115 xfs_fsblock_t bno, /* fs block number of extent */
3116 xfs_filblks_t len, /* length of extent */
3117 xfs_bmap_free_t *flist, /* list of extents */
3118 xfs_mount_t *mp) /* mount point structure */
3119{
3120 xfs_bmap_free_item_t *cur; /* current (next) element */
3121 xfs_bmap_free_item_t *new; /* new element */
3122 xfs_bmap_free_item_t *prev; /* previous element */
3123#ifdef DEBUG
3124 xfs_agnumber_t agno;
3125 xfs_agblock_t agbno;
3126
3127 ASSERT(bno != NULLFSBLOCK);
3128 ASSERT(len > 0);
3129 ASSERT(len <= MAXEXTLEN);
3130 ASSERT(!ISNULLSTARTBLOCK(bno));
3131 agno = XFS_FSB_TO_AGNO(mp, bno);
3132 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3133 ASSERT(agno < mp->m_sb.sb_agcount);
3134 ASSERT(agbno < mp->m_sb.sb_agblocks);
3135 ASSERT(len < mp->m_sb.sb_agblocks);
3136 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3137#endif
3138 ASSERT(xfs_bmap_free_item_zone != NULL);
3139 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3140 new->xbfi_startblock = bno;
3141 new->xbfi_blockcount = (xfs_extlen_t)len;
3142 for (prev = NULL, cur = flist->xbf_first;
3143 cur != NULL;
3144 prev = cur, cur = cur->xbfi_next) {
3145 if (cur->xbfi_startblock >= bno)
3146 break;
3147 }
3148 if (prev)
3149 prev->xbfi_next = new;
3150 else
3151 flist->xbf_first = new;
3152 new->xbfi_next = cur;
3153 flist->xbf_count++;
3154}
3155
3156/*
3157 * Compute and fill in the value of the maximum depth of a bmap btree
3158 * in this filesystem. Done once, during mount.
3159 */
3160void
3161xfs_bmap_compute_maxlevels(
3162 xfs_mount_t *mp, /* file system mount structure */
3163 int whichfork) /* data or attr fork */
3164{
3165 int level; /* btree level */
3166 uint maxblocks; /* max blocks at this level */
3167 uint maxleafents; /* max leaf entries possible */
3168 int maxrootrecs; /* max records in root block */
3169 int minleafrecs; /* min records in leaf block */
3170 int minnoderecs; /* min records in node block */
3171 int sz; /* root block size */
3172
3173 /*
3174 * The maximum number of extents in a file, hence the maximum
3175 * number of leaf entries, is controlled by the type of di_nextents
3176 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3177 * (a signed 16-bit number, xfs_aextnum_t).
3178 */
3179 maxleafents = (whichfork == XFS_DATA_FORK) ? MAXEXTNUM : MAXAEXTNUM;
3180 minleafrecs = mp->m_bmap_dmnr[0];
3181 minnoderecs = mp->m_bmap_dmnr[1];
3182 sz = (whichfork == XFS_DATA_FORK) ?
3183 mp->m_attroffset :
3184 mp->m_sb.sb_inodesize - mp->m_attroffset;
3185 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
3186 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3187 for (level = 1; maxblocks > 1; level++) {
3188 if (maxblocks <= maxrootrecs)
3189 maxblocks = 1;
3190 else
3191 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3192 }
3193 mp->m_bm_maxlevels[whichfork] = level;
3194}
3195
3196/*
3197 * Returns the file-relative block number of the first unused block(s)
3198 * in the file with at least "len" logically contiguous blocks free.
3199 * This is the lowest-address hole if the file has holes, else the first block
3200 * past the end of file.
3201 * Return 0 if the file is currently local (in-inode).
3202 */
3203int /* error */
3204xfs_bmap_first_unused(
3205 xfs_trans_t *tp, /* transaction pointer */
3206 xfs_inode_t *ip, /* incore inode */
3207 xfs_extlen_t len, /* size of hole to find */
3208 xfs_fileoff_t *first_unused, /* unused block */
3209 int whichfork) /* data or attr fork */
3210{
3211 xfs_bmbt_rec_t *base; /* base of extent array */
3212 xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
3213 int error; /* error return value */
3214 xfs_ifork_t *ifp; /* inode fork pointer */
3215 xfs_fileoff_t lastaddr; /* last block number seen */
3216 xfs_fileoff_t lowest; /* lowest useful block */
3217 xfs_fileoff_t max; /* starting useful block */
3218 xfs_fileoff_t off; /* offset for this block */
3219 xfs_extnum_t nextents; /* number of extent entries */
3220
3221 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3222 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3223 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3224 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3225 *first_unused = 0;
3226 return 0;
3227 }
3228 ifp = XFS_IFORK_PTR(ip, whichfork);
3229 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3230 (error = xfs_iread_extents(tp, ip, whichfork)))
3231 return error;
3232 lowest = *first_unused;
3233 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3234 base = &ifp->if_u1.if_extents[0];
3235 for (lastaddr = 0, max = lowest, ep = base;
3236 ep < &base[nextents];
3237 ep++) {
3238 off = xfs_bmbt_get_startoff(ep);
3239 /*
3240 * See if the hole before this extent will work.
3241 */
3242 if (off >= lowest + len && off - max >= len) {
3243 *first_unused = max;
3244 return 0;
3245 }
3246 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3247 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3248 }
3249 *first_unused = max;
3250 return 0;
3251}
3252
3253/*
3254 * Returns the file-relative block number of the last block + 1 before
3255 * last_block (input value) in the file.
3256 * This is not based on i_size, it is based on the extent list.
3257 * Returns 0 for local files, as they do not have an extent list.
3258 */
3259int /* error */
3260xfs_bmap_last_before(
3261 xfs_trans_t *tp, /* transaction pointer */
3262 xfs_inode_t *ip, /* incore inode */
3263 xfs_fileoff_t *last_block, /* last block */
3264 int whichfork) /* data or attr fork */
3265{
3266 xfs_fileoff_t bno; /* input file offset */
3267 int eof; /* hit end of file */
3268 xfs_bmbt_rec_t *ep; /* pointer to last extent */
3269 int error; /* error return value */
3270 xfs_bmbt_irec_t got; /* current extent value */
3271 xfs_ifork_t *ifp; /* inode fork pointer */
3272 xfs_extnum_t lastx; /* last extent used */
3273 xfs_bmbt_irec_t prev; /* previous extent value */
3274
3275 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3276 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3277 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3278 return XFS_ERROR(EIO);
3279 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3280 *last_block = 0;
3281 return 0;
3282 }
3283 ifp = XFS_IFORK_PTR(ip, whichfork);
3284 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3285 (error = xfs_iread_extents(tp, ip, whichfork)))
3286 return error;
3287 bno = *last_block - 1;
3288 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3289 &prev);
3290 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3291 if (prev.br_startoff == NULLFILEOFF)
3292 *last_block = 0;
3293 else
3294 *last_block = prev.br_startoff + prev.br_blockcount;
3295 }
3296 /*
3297 * Otherwise *last_block is already the right answer.
3298 */
3299 return 0;
3300}
3301
3302/*
3303 * Returns the file-relative block number of the first block past eof in
3304 * the file. This is not based on i_size, it is based on the extent list.
3305 * Returns 0 for local files, as they do not have an extent list.
3306 */
3307int /* error */
3308xfs_bmap_last_offset(
3309 xfs_trans_t *tp, /* transaction pointer */
3310 xfs_inode_t *ip, /* incore inode */
3311 xfs_fileoff_t *last_block, /* last block */
3312 int whichfork) /* data or attr fork */
3313{
3314 xfs_bmbt_rec_t *base; /* base of extent array */
3315 xfs_bmbt_rec_t *ep; /* pointer to last extent */
3316 int error; /* error return value */
3317 xfs_ifork_t *ifp; /* inode fork pointer */
3318 xfs_extnum_t nextents; /* number of extent entries */
3319
3320 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3321 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3322 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3323 return XFS_ERROR(EIO);
3324 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3325 *last_block = 0;
3326 return 0;
3327 }
3328 ifp = XFS_IFORK_PTR(ip, whichfork);
3329 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3330 (error = xfs_iread_extents(tp, ip, whichfork)))
3331 return error;
3332 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3333 if (!nextents) {
3334 *last_block = 0;
3335 return 0;
3336 }
3337 base = &ifp->if_u1.if_extents[0];
3338 ASSERT(base != NULL);
3339 ep = &base[nextents - 1];
3340 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
3341 return 0;
3342}
3343
3344/*
3345 * Returns whether the selected fork of the inode has exactly one
3346 * block or not. For the data fork we check this matches di_size,
3347 * implying the file's range is 0..bsize-1.
3348 */
3349int /* 1=>1 block, 0=>otherwise */
3350xfs_bmap_one_block(
3351 xfs_inode_t *ip, /* incore inode */
3352 int whichfork) /* data or attr fork */
3353{
3354 xfs_bmbt_rec_t *ep; /* ptr to fork's extent */
3355 xfs_ifork_t *ifp; /* inode fork pointer */
3356 int rval; /* return value */
3357 xfs_bmbt_irec_t s; /* internal version of extent */
3358
3359#ifndef DEBUG
3360 if (whichfork == XFS_DATA_FORK)
3361 return ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize;
3362#endif /* !DEBUG */
3363 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
3364 return 0;
3365 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
3366 return 0;
3367 ifp = XFS_IFORK_PTR(ip, whichfork);
3368 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3369 ep = ifp->if_u1.if_extents;
3370 xfs_bmbt_get_all(ep, &s);
3371 rval = s.br_startoff == 0 && s.br_blockcount == 1;
3372 if (rval && whichfork == XFS_DATA_FORK)
3373 ASSERT(ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
3374 return rval;
3375}
3376
3377/*
3378 * Read in the extents to if_extents.
3379 * All inode fields are set up by caller, we just traverse the btree
3380 * and copy the records in. If the file system cannot contain unwritten
3381 * extents, the records are checked for no "state" flags.
3382 */
3383int /* error */
3384xfs_bmap_read_extents(
3385 xfs_trans_t *tp, /* transaction pointer */
3386 xfs_inode_t *ip, /* incore inode */
3387 int whichfork) /* data or attr fork */
3388{
3389 xfs_bmbt_block_t *block; /* current btree block */
3390 xfs_fsblock_t bno; /* block # of "block" */
3391 xfs_buf_t *bp; /* buffer for "block" */
3392 int error; /* error return value */
3393 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
3394#ifdef XFS_BMAP_TRACE
3395 static char fname[] = "xfs_bmap_read_extents";
3396#endif
3397 xfs_extnum_t i; /* index into the extents list */
3398 xfs_ifork_t *ifp; /* fork structure */
3399 int level; /* btree level, for checking */
3400 xfs_mount_t *mp; /* file system mount structure */
3401 xfs_bmbt_ptr_t *pp; /* pointer to block address */
3402 /* REFERENCED */
3403 xfs_extnum_t room; /* number of entries there's room for */
3404 xfs_bmbt_rec_t *trp; /* target record pointer */
3405
3406 bno = NULLFSBLOCK;
3407 mp = ip->i_mount;
3408 ifp = XFS_IFORK_PTR(ip, whichfork);
3409 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
3410 XFS_EXTFMT_INODE(ip);
3411 block = ifp->if_broot;
3412 /*
3413 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
3414 */
3415 ASSERT(INT_GET(block->bb_level, ARCH_CONVERT) > 0);
3416 level = INT_GET(block->bb_level, ARCH_CONVERT);
3417 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
3418 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
3419 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
3420 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
3421 bno = INT_GET(*pp, ARCH_CONVERT);
3422 /*
3423 * Go down the tree until leaf level is reached, following the first
3424 * pointer (leftmost) at each level.
3425 */
3426 while (level-- > 0) {
3427 if (error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
3428 XFS_BMAP_BTREE_REF))
3429 return error;
3430 block = XFS_BUF_TO_BMBT_BLOCK(bp);
3431 XFS_WANT_CORRUPTED_GOTO(
3432 XFS_BMAP_SANITY_CHECK(mp, block, level),
3433 error0);
3434 if (level == 0)
3435 break;
3436 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
3437 1, mp->m_bmap_dmxr[1]);
3438 XFS_WANT_CORRUPTED_GOTO(XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)), error0);
3439 bno = INT_GET(*pp, ARCH_CONVERT);
3440 xfs_trans_brelse(tp, bp);
3441 }
3442 /*
3443 * Here with bp and block set to the leftmost leaf node in the tree.
3444 */
3445 room = ifp->if_bytes / (uint)sizeof(*trp);
3446 trp = ifp->if_u1.if_extents;
3447 i = 0;
3448 /*
3449 * Loop over all leaf nodes. Copy information to the extent list.
3450 */
3451 for (;;) {
3452 xfs_bmbt_rec_t *frp;
3453 xfs_fsblock_t nextbno;
3454 xfs_extnum_t num_recs;
3455
3456
3457 num_recs = INT_GET(block->bb_numrecs, ARCH_CONVERT);
3458 if (i + num_recs > room) {
3459 ASSERT(i + num_recs <= room);
3460 xfs_fs_cmn_err(CE_WARN, ip->i_mount,
3461 "corrupt dinode %Lu, (btree extents). Unmount and run xfs_repair.",
3462 ip->i_ino);
3463 goto error0;
3464 }
3465 XFS_WANT_CORRUPTED_GOTO(
3466 XFS_BMAP_SANITY_CHECK(mp, block, 0),
3467 error0);
3468 /*
3469 * Read-ahead the next leaf block, if any.
3470 */
3471 nextbno = INT_GET(block->bb_rightsib, ARCH_CONVERT);
3472 if (nextbno != NULLFSBLOCK)
3473 xfs_btree_reada_bufl(mp, nextbno, 1);
3474 /*
3475 * Copy records into the extent list.
3476 */
3477 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
3478 block, 1, mp->m_bmap_dmxr[0]);
3479 bcopy(frp, trp, num_recs * sizeof(*frp));
3480 if (exntf == XFS_EXTFMT_NOSTATE) {
3481 /*
3482 * Check all attribute bmap btree records and
3483 * any "older" data bmap btree records for a
3484 * set bit in the "extent flag" position.
3485 */
3486 if (xfs_check_nostate_extents(trp, num_recs)) {
3487 goto error0;
3488 }
3489 }
3490 trp += num_recs;
3491 i += num_recs;
3492 xfs_trans_brelse(tp, bp);
3493 bno = nextbno;
3494 /*
3495 * If we've reached the end, stop.
3496 */
3497 if (bno == NULLFSBLOCK)
3498 break;
3499 if (error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
3500 XFS_BMAP_BTREE_REF))
3501 return error;
3502 block = XFS_BUF_TO_BMBT_BLOCK(bp);
3503 }
3504 ASSERT(i == ifp->if_bytes / (uint)sizeof(*trp));
3505 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
3506 xfs_bmap_trace_exlist(fname, ip, i, whichfork);
3507 return 0;
3508error0:
3509 xfs_trans_brelse(tp, bp);
3510 return XFS_ERROR(EFSCORRUPTED);
3511}
3512
3513/*
3514 * Map file blocks to filesystem blocks.
3515 * File range is given by the bno/len pair.
3516 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
3517 * into a hole or past eof.
3518 * Only allocates blocks from a single allocation group,
3519 * to avoid locking problems.
3520 * The returned value in "firstblock" from the first call in a transaction
3521 * must be remembered and presented to subsequent calls in "firstblock".
3522 * An upper bound for the number of blocks to be allocated is supplied to
3523 * the first call in "total"; if no allocation group has that many free
3524 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
3525 */
3526int /* error */
3527xfs_bmapi(
3528 xfs_trans_t *tp, /* transaction pointer */
3529 xfs_inode_t *ip, /* incore inode */
3530 xfs_fileoff_t bno, /* starting file offs. mapped */
3531 xfs_filblks_t len, /* length to map in file */
3532 int flags, /* XFS_BMAPI_... */
3533 xfs_fsblock_t *firstblock, /* first allocated block
3534 controls a.g. for allocs */
3535 xfs_extlen_t total, /* total blocks needed */
3536 xfs_bmbt_irec_t *mval, /* output: map values */
3537 int *nmap, /* i/o: mval size/count */
3538 xfs_bmap_free_t *flist) /* i/o: list extents to free */
3539{
3540 xfs_fsblock_t abno; /* allocated block number */
3541 xfs_extlen_t alen; /* allocated extent length */
3542 xfs_fileoff_t aoff; /* allocated file offset */
3543 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
3544 int contig; /* allocation must be one extent */
3545 xfs_btree_cur_t *cur; /* bmap btree cursor */
3546 char delay; /* this request is for delayed alloc */
3547 xfs_fileoff_t end; /* end of mapped file region */
3548 int eof; /* we've hit the end of extent list */
3549 xfs_bmbt_rec_t *ep; /* extent list entry pointer */
3550 int error; /* error return */
3551 char exact; /* don't do all of wasdelayed extent */
3552 xfs_bmbt_irec_t got; /* current extent list record */
3553 xfs_ifork_t *ifp; /* inode fork pointer */
3554 xfs_extlen_t indlen; /* indirect blocks length */
3555 char inhole; /* current location is hole in file */
3556 xfs_extnum_t lastx; /* last useful extent number */
3557 int logflags; /* flags for transaction logging */
3558 xfs_extlen_t minleft; /* min blocks left after allocation */
3559 xfs_extlen_t minlen; /* min allocation size */
3560 xfs_mount_t *mp; /* xfs mount structure */
3561 int n; /* current extent index */
3562 int nallocs; /* number of extents alloc\'d */
3563 xfs_extnum_t nextents; /* number of extents in file */
3564 xfs_fileoff_t obno; /* old block number (offset) */
3565 xfs_bmbt_irec_t prev; /* previous extent list record */
3566 int stateless; /* ignore state flag set */
3567 int tmp_logflags; /* temp flags holder */
3568 char trim; /* output trimmed to match range */
3569 char userdata; /* allocating non-metadata */
3570 char wasdelay; /* old extent was delayed */
3571 int whichfork; /* data or attr fork */
3572 char wr; /* this is a write request */
3573 int rsvd; /* OK to allocate reserved blocks */
3574#ifdef DEBUG
3575 xfs_fileoff_t orig_bno; /* original block number value */
3576 int orig_flags; /* original flags arg value */
3577 xfs_filblks_t orig_len; /* original value of len arg */
3578 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
3579 int orig_nmap; /* original value of *nmap */
3580
3581 orig_bno = bno;
3582 orig_len = len;
3583 orig_flags = flags;
3584 orig_mval = mval;
3585 orig_nmap = *nmap;
3586#endif
3587 ASSERT(*nmap >= 1);
3588 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
3589 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
3590 XFS_ATTR_FORK : XFS_DATA_FORK;
3591 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3592 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3593 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3594 return XFS_ERROR(EFSCORRUPTED);
3595 mp = ip->i_mount;
3596 if (XFS_FORCED_SHUTDOWN(mp))
3597 return XFS_ERROR(EIO);
3598 ifp = XFS_IFORK_PTR(ip, whichfork);
3599 ASSERT(ifp->if_ext_max ==
3600 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
3601 if (wr = (flags & XFS_BMAPI_WRITE) != 0)
3602 XFS_STATS_INC(xs_blk_mapw);
3603 else
3604 XFS_STATS_INC(xs_blk_mapr);
3605 delay = (flags & XFS_BMAPI_DELAY) != 0;
3606 trim = (flags & XFS_BMAPI_ENTIRE) == 0;
3607 userdata = (flags & XFS_BMAPI_METADATA) == 0;
3608 exact = (flags & XFS_BMAPI_EXACT) != 0;
3609 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
3610 contig = (flags & XFS_BMAPI_CONTIG) != 0;
3611 /*
3612 * stateless is used to combine extents which
3613 * differ only due to the state of the extents.
3614 * This technique is used from xfs_getbmap()
3615 * when the caller does not wish to see the
3616 * separation (which is the default).
3617 *
3618 * This technique is also used when writing a
3619 * buffer which has been partially written,
3620 * (usually by being flushed during a chunkread),
3621 * to ensure one write takes place. This also
3622 * prevents a change in the xfs inode extents at
3623 * this time, intentionally. This change occurs
3624 * on completion of the write operation, in
3625 * xfs_strat_comp(), where the xfs_bmapi() call
3626 * is transactioned, and the extents combined.
3627 */
3628 stateless = (flags & XFS_BMAPI_IGSTATE) != 0;
3629 if (stateless && wr) /* if writing unwritten space, no */
3630 wr = 0; /* allocations are allowed */
3631 ASSERT(wr || !delay);
3632 logflags = 0;
3633 nallocs = 0;
3634 cur = NULL;
3635 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3636 ASSERT(wr && tp);
3637 if (error = xfs_bmap_local_to_extents(tp, ip, firstblock, total,
3638 &logflags, whichfork))
3639 goto error0;
3640 }
3641 if (wr && *firstblock == NULLFSBLOCK) {
3642 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
3643 minleft = INT_GET(ifp->if_broot->bb_level, ARCH_CONVERT) + 1;
3644 else
3645 minleft = 1;
3646 } else
3647 minleft = 0;
3648 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3649 (error = xfs_iread_extents(tp, ip, whichfork)))
3650 goto error0;
3651 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3652 &prev);
3653 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3654 n = 0;
3655 end = bno + len;
3656 obno = bno;
3657 bma.ip = NULL;
3658 while (bno < end && n < *nmap) {
3659 /*
3660 * Reading past eof, act as though there's a hole
3661 * up to end.
3662 */
3663 if (eof && !wr)
3664 got.br_startoff = end;
3665 inhole = eof || got.br_startoff > bno;
3666 wasdelay = wr && !inhole && !delay &&
3667 ISNULLSTARTBLOCK(got.br_startblock);
3668 /*
3669 * First, deal with the hole before the allocated space
3670 * that we found, if any.
3671 */
3672 if (wr && (inhole || wasdelay)) {
3673 /*
3674 * For the wasdelay case, we could also just
3675 * allocate the stuff asked for in this bmap call
3676 * but that wouldn't be as good.
3677 */
3678 if (wasdelay && !exact) {
3679 alen = (xfs_extlen_t)got.br_blockcount;
3680 aoff = got.br_startoff;
3681 if (lastx != NULLEXTNUM && lastx) {
3682 ep = &ifp->if_u1.if_extents[lastx - 1];
3683 xfs_bmbt_get_all(ep, &prev);
3684 }
3685 } else if (wasdelay) {
3686 alen = (xfs_extlen_t)
3687 XFS_FILBLKS_MIN(len,
3688 (got.br_startoff +
3689 got.br_blockcount) - bno);
3690 aoff = bno;
3691 } else {
3692 alen = (xfs_extlen_t)
3693 XFS_FILBLKS_MIN(len, MAXEXTLEN);
3694 if (!eof)
3695 alen = (xfs_extlen_t)
3696 XFS_FILBLKS_MIN(alen,
3697 got.br_startoff - bno);
3698 aoff = bno;
3699 }
3700 minlen = contig ? alen : 1;
3701 if (delay) {
3702 indlen = (xfs_extlen_t)
3703 xfs_bmap_worst_indlen(ip, alen);
3704 ASSERT(indlen > 0);
3705 /*
3706 * Make a transaction-less quota reservation for
3707 * delayed allocation blocks. This number gets
3708 * adjusted later.
3709 * We return EDQUOT if we haven't allocated
3710 * blks already inside this loop;
3711 */
3712 if (XFS_IS_QUOTA_ON(ip->i_mount) &&
3713 xfs_trans_reserve_blkquota(NULL, ip,
3714 (long)alen)) {
3715 if (n == 0) {
3716 *nmap = 0;
3717 ASSERT(cur == NULL);
3718 return XFS_ERROR(EDQUOT);
3719 }
3720 break;
3721 }
3722 if (xfs_mod_incore_sb(ip->i_mount,
3723 XFS_SBS_FDBLOCKS,
3724 -(alen + indlen), rsvd)) {
3725 if (XFS_IS_QUOTA_ON(ip->i_mount))
3726 xfs_trans_unreserve_blkquota(
3727 NULL, ip, (long)alen);
3728 break;
3729 }
3730 ip->i_delayed_blks += alen;
3731 abno = NULLSTARTBLOCK(indlen);
3732 } else {
3733 /*
3734 * If first time, allocate and fill in
3735 * once-only bma fields.
3736 */
3737 if (bma.ip == NULL) {
3738 bma.tp = tp;
3739 bma.ip = ip;
3740 bma.prevp = &prev;
3741 bma.gotp = &got;
3742 bma.total = total;
3743 bma.userdata = userdata;
3744 }
3745 /*
3746 * Fill in changeable bma fields.
3747 */
3748 bma.eof = eof;
3749 bma.firstblock = *firstblock;
3750 bma.alen = alen;
3751 bma.off = aoff;
3752 bma.wasdel = wasdelay;
3753 bma.minlen = minlen;
3754 bma.low = flist->xbf_low;
3755 bma.minleft = minleft;
3756 /*
3757 * Only want to do the alignment at the
3758 * eof if it is userdata and allocation length
3759 * is larger than a stripe unit.
3760 */
3761 if (mp->m_dalign && alen >= mp->m_dalign &&
3762 userdata && whichfork == XFS_DATA_FORK) {
3763 if (error = xfs_bmap_isaeof(ip, aoff,
3764 whichfork, &bma.aeof))
3765 goto error0;
3766 } else
3767 bma.aeof = 0;
3768 /*
3769 * Call allocator.
3770 */
3771 if (error = xfs_bmap_alloc(&bma))
3772 goto error0;
3773 /*
3774 * Copy out result fields.
3775 */
3776 abno = bma.rval;
3777 if (flist->xbf_low = bma.low)
3778 minleft = 0;
3779 alen = bma.alen;
3780 aoff = bma.off;
3781 ASSERT(*firstblock == NULLFSBLOCK ||
3782 XFS_FSB_TO_AGNO(ip->i_mount,
3783 *firstblock) ==
3784 XFS_FSB_TO_AGNO(ip->i_mount,
3785 bma.firstblock) ||
3786 (flist->xbf_low &&
3787 XFS_FSB_TO_AGNO(ip->i_mount,
3788 *firstblock) <
3789 XFS_FSB_TO_AGNO(ip->i_mount,
3790 bma.firstblock)));
3791 *firstblock = bma.firstblock;
3792 if (cur)
3793 cur->bc_private.b.firstblock =
3794 *firstblock;
3795 if (abno == NULLFSBLOCK)
3796 break;
3797 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
3798 cur = xfs_btree_init_cursor(ip->i_mount,
3799 tp, NULL, 0, XFS_BTNUM_BMAP,
3800 ip, whichfork);
3801 cur->bc_private.b.firstblock =
3802 *firstblock;
3803 cur->bc_private.b.flist = flist;
3804 }
3805 /*
3806 * Bump the number of extents we've allocated
3807 * in this call.
3808 */
3809 nallocs++;
3810 }
3811 if (cur)
3812 cur->bc_private.b.flags =
3813 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
3814 got.br_startoff = aoff;
3815 got.br_startblock = abno;
3816 got.br_blockcount = alen;
3817 got.br_state = XFS_EXT_NORM; /* assume normal */
3818 /*
3819 * Determine state of extent, and the filesystem.
3820 * A wasdelay extent has been initialized, so
3821 * shouldn't be flagged as unwritten.
3822 */
3823 if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
3824 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
3825 got.br_state = XFS_EXT_UNWRITTEN;
3826 }
3827 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
3828 firstblock, flist, &tmp_logflags, whichfork,
3829 rsvd);
3830 logflags |= tmp_logflags;
3831 if (error)
3832 goto error0;
3833 lastx = ifp->if_lastex;
3834 ep = &ifp->if_u1.if_extents[lastx];
3835 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3836 xfs_bmbt_get_all(ep, &got);
3837 ASSERT(got.br_startoff <= aoff);
3838 ASSERT(got.br_startoff + got.br_blockcount >=
3839 aoff + alen);
3840#ifdef DEBUG
3841 if (delay) {
3842 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
3843 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
3844 }
3845 ASSERT(got.br_state == XFS_EXT_NORM ||
3846 got.br_state == XFS_EXT_UNWRITTEN);
3847#endif
3848 /*
3849 * Fall down into the found allocated space case.
3850 */
3851 } else if (inhole) {
3852 /*
3853 * Reading in a hole.
3854 */
3855 mval->br_startoff = bno;
3856 mval->br_startblock = HOLESTARTBLOCK;
3857 mval->br_blockcount =
3858 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3859 mval->br_state = XFS_EXT_NORM;
3860 bno += mval->br_blockcount;
3861 len -= mval->br_blockcount;
3862 mval++;
3863 n++;
3864 continue;
3865 }
3866 /*
3867 * Then deal with the allocated space we found.
3868 */
3869 ASSERT(ep != NULL);
3870 if (trim && (got.br_startoff + got.br_blockcount > obno)) {
3871 if (obno > bno)
3872 bno = obno;
3873 ASSERT((bno >= obno) || (n == 0));
3874 ASSERT(bno < end);
3875 mval->br_startoff = bno;
3876 if (ISNULLSTARTBLOCK(got.br_startblock)) {
3877 ASSERT(!wr || delay);
3878 mval->br_startblock = DELAYSTARTBLOCK;
3879 } else
3880 mval->br_startblock =
3881 got.br_startblock +
3882 (bno - got.br_startoff);
3883 /*
3884 * Return the minimum of what we got and what we
3885 * asked for for the length. We can use the len
3886 * variable here because it is modified below
3887 * and we could have been there before coming
3888 * here if the first part of the allocation
3889 * didn't overlap what was asked for.
3890 */
3891 mval->br_blockcount =
3892 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
3893 (bno - got.br_startoff));
3894 mval->br_state = got.br_state;
3895 ASSERT(mval->br_blockcount <= len);
3896 } else {
3897 *mval = got;
3898 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
3899 ASSERT(!wr || delay);
3900 mval->br_startblock = DELAYSTARTBLOCK;
3901 }
3902 }
3903
3904 /*
3905 * Check if writing previously allocated but
3906 * unwritten extents.
3907 */
3908 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
3909 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
3910 /*
3911 * Modify (by adding) the state flag, if writing.
3912 */
3913 ASSERT(mval->br_blockcount <= len);
3914 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
3915 cur = xfs_btree_init_cursor(ip->i_mount,
3916 tp, NULL, 0, XFS_BTNUM_BMAP,
3917 ip, whichfork);
3918 cur->bc_private.b.firstblock =
3919 *firstblock;
3920 cur->bc_private.b.flist = flist;
3921 }
3922 mval->br_state = XFS_EXT_NORM;
3923 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
3924 firstblock, flist, &tmp_logflags, whichfork,
3925 rsvd);
3926 logflags |= tmp_logflags;
3927 if (error)
3928 goto error0;
3929 lastx = ifp->if_lastex;
3930 ep = &ifp->if_u1.if_extents[lastx];
3931 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3932 xfs_bmbt_get_all(ep, &got);
3933 /*
3934 * We may have combined previously unwritten
3935 * space with written space, so generate
3936 * another request.
3937 */
3938 if (mval->br_blockcount < len)
3939 continue;
3940 }
3941
3942 ASSERT(!trim ||
3943 ((mval->br_startoff + mval->br_blockcount) <= end));
3944 ASSERT(!trim || (mval->br_blockcount <= len) ||
3945 (mval->br_startoff < obno));
3946 bno = mval->br_startoff + mval->br_blockcount;
3947 len = end - bno;
3948 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3949 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3950 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3951 ASSERT(mval->br_state == mval[-1].br_state);
3952 mval[-1].br_blockcount = mval->br_blockcount;
3953 mval[-1].br_state = mval->br_state;
3954 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3955 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3956 mval[-1].br_startblock != HOLESTARTBLOCK &&
3957 mval->br_startblock ==
3958 mval[-1].br_startblock + mval[-1].br_blockcount &&
3959 (stateless || mval[-1].br_state == mval->br_state)) {
3960 ASSERT(mval->br_startoff ==
3961 mval[-1].br_startoff + mval[-1].br_blockcount);
3962 mval[-1].br_blockcount += mval->br_blockcount;
3963 } else if (n > 0 &&
3964 mval->br_startblock == DELAYSTARTBLOCK &&
3965 mval[-1].br_startblock == DELAYSTARTBLOCK &&
3966 mval->br_startoff ==
3967 mval[-1].br_startoff + mval[-1].br_blockcount) {
3968 mval[-1].br_blockcount += mval->br_blockcount;
3969 mval[-1].br_state = mval->br_state;
3970 } else if (!((n == 0) &&
3971 ((mval->br_startoff + mval->br_blockcount) <=
3972 obno))) {
3973 mval++;
3974 n++;
3975 }
3976 /*
3977 * If we're done, stop now. Stop when we've allocated
3978 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
3979 * the transaction may get too big.
3980 */
3981 if (bno >= end || n >= *nmap || nallocs >= *nmap)
3982 break;
3983 /*
3984 * Else go on to the next record.
3985 */
3986 ep++;
3987 lastx++;
3988 if (lastx >= nextents) {
3989 eof = 1;
3990 prev = got;
3991 } else
3992 xfs_bmbt_get_all(ep, &got);
3993 }
3994 ifp->if_lastex = lastx;
3995 *nmap = n;
3996 /*
3997 * Transform from btree to extents, give it cur.
3998 */
3999 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4000 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4001 ASSERT(wr && cur);
4002 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4003 &tmp_logflags, whichfork, 0);
4004 logflags |= tmp_logflags;
4005 if (error)
4006 goto error0;
4007 }
4008 ASSERT(ifp->if_ext_max ==
4009 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4010 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4011 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4012 error = 0;
4013
4014error0:
4015 /*
4016 * Log everything. Do this after conversion, there's no point in
4017 * logging the extent list if we've converted to btree format.
4018 */
4019 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
4020 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4021 logflags &= ~XFS_ILOG_FEXT(whichfork);
4022 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
4023 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4024 logflags &= ~XFS_ILOG_FBROOT(whichfork);
4025 /*
4026 * Log whatever the flags say, even if error. Otherwise we might miss
4027 * detecting a case where the data is changed, there's an error,
4028 * and it's not logged so we don't shutdown when we should.
4029 */
4030 if (logflags) {
4031 ASSERT(tp && wr);
4032 xfs_trans_log_inode(tp, ip, logflags);
4033 }
4034 if (cur) {
4035 if (!error) {
4036 ASSERT(*firstblock == NULLFSBLOCK ||
4037 XFS_FSB_TO_AGNO(ip->i_mount, *firstblock) ==
4038 XFS_FSB_TO_AGNO(ip->i_mount,
4039 cur->bc_private.b.firstblock) ||
4040 (flist->xbf_low &&
4041 XFS_FSB_TO_AGNO(ip->i_mount, *firstblock) <
4042 XFS_FSB_TO_AGNO(ip->i_mount,
4043 cur->bc_private.b.firstblock)));
4044 *firstblock = cur->bc_private.b.firstblock;
4045 }
4046 xfs_btree_del_cursor(cur,
4047 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4048 }
4049 if (!error)
4050 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4051 orig_nmap, *nmap);
4052 return error;
4053}
4054
4055/*
4056 * Map file blocks to filesystem blocks, simple version.
4057 * One block (extent) only, read-only.
4058 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4059 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4060 * was set and all the others were clear.
4061 */
4062int /* error */
4063xfs_bmapi_single(
4064 xfs_trans_t *tp, /* transaction pointer */
4065 xfs_inode_t *ip, /* incore inode */
4066 int whichfork, /* data or attr fork */
4067 xfs_fsblock_t *fsb, /* output: mapped block */
4068 xfs_fileoff_t bno) /* starting file offs. mapped */
4069{
4070 int eof; /* we've hit the end of extent list */
4071 int error; /* error return */
4072 xfs_bmbt_irec_t got; /* current extent list record */
4073 xfs_ifork_t *ifp; /* inode fork pointer */
4074 xfs_extnum_t lastx; /* last useful extent number */
4075 xfs_bmbt_irec_t prev; /* previous extent list record */
4076
4077 ifp = XFS_IFORK_PTR(ip, whichfork);
4078 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4079 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4080 return XFS_ERROR(EFSCORRUPTED);
4081 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
4082 return XFS_ERROR(EIO);
4083 XFS_STATS_INC(xs_blk_mapr);
4084 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4085 (error = xfs_iread_extents(tp, ip, whichfork)))
4086 return error;
4087 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4088 &prev);
4089 /*
4090 * Reading past eof, act as though there's a hole
4091 * up to end.
4092 */
4093 if (eof || got.br_startoff > bno) {
4094 *fsb = NULLFSBLOCK;
4095 return 0;
4096 }
4097 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
4098 ASSERT(bno < got.br_startoff + got.br_blockcount);
4099 *fsb = got.br_startblock + (bno - got.br_startoff);
4100 ifp->if_lastex = lastx;
4101 return 0;
4102}
4103
4104/*
4105 * Unmap (remove) blocks from a file.
4106 * If nexts is nonzero then the number of extents to remove is limited to
4107 * that value. If not all extents in the block range can be removed then
4108 * *done is set.
4109 */
4110int /* error */
4111xfs_bunmapi(
4112 xfs_trans_t *tp, /* transaction pointer */
4113 struct xfs_inode *ip, /* incore inode */
4114 xfs_fileoff_t bno, /* starting offset to unmap */
4115 xfs_filblks_t len, /* length to unmap in file */
4116 int flags, /* misc flags */
4117 xfs_extnum_t nexts, /* number of extents max */
4118 xfs_fsblock_t *firstblock, /* first allocated block
4119 controls a.g. for allocs */
4120 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4121 int *done) /* set if not done yet */
4122{
4123 int async; /* xactions can be async */
4124 xfs_btree_cur_t *cur; /* bmap btree cursor */
4125 xfs_bmbt_irec_t del; /* extent being deleted */
4126 int eof; /* is deleting at eof */
4127 xfs_bmbt_rec_t *ep; /* extent list entry pointer */
4128 int error; /* error return value */
4129 xfs_extnum_t extno; /* extent number in list */
4130 xfs_bmbt_irec_t got; /* current extent list entry */
4131 xfs_ifork_t *ifp; /* inode fork pointer */
4132 int isrt; /* freeing in rt area */
4133 xfs_extnum_t lastx; /* last extent index used */
4134 int logflags; /* transaction logging flags */
4135 xfs_extlen_t mod; /* rt extent offset */
4136 xfs_mount_t *mp; /* mount structure */
4137 xfs_extnum_t nextents; /* size of extent list */
4138 xfs_bmbt_irec_t prev; /* previous extent list entry */
4139 xfs_fileoff_t start; /* first file offset deleted */
4140 int tmp_logflags; /* partial logging flags */
4141 int wasdel; /* was a delayed alloc extent */
4142 int whichfork; /* data or attribute fork */
4143 int rsvd; /* OK to allocate reserved blocks */
4144 xfs_fsblock_t sum;
4145
4146 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
4147 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4148 XFS_ATTR_FORK : XFS_DATA_FORK;
4149 ifp = XFS_IFORK_PTR(ip, whichfork);
4150 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4151 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4152 return XFS_ERROR(EFSCORRUPTED);
4153 mp = ip->i_mount;
4154 if (XFS_FORCED_SHUTDOWN(mp))
4155 return XFS_ERROR(EIO);
4156 async = flags & XFS_BMAPI_ASYNC;
4157 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
4158 ASSERT(len > 0);
4159 ASSERT(nexts >= 0);
4160 ASSERT(ifp->if_ext_max ==
4161 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4162 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4163 (error = xfs_iread_extents(tp, ip, whichfork)))
4164 return error;
4165 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4166 if (nextents == 0) {
4167 *done = 1;
4168 return 0;
4169 }
4170 XFS_STATS_INC(xs_blk_unmap);
4171 isrt = (whichfork == XFS_DATA_FORK) &&
4172 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4173 start = bno;
4174 bno = start + len - 1;
4175 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4176 &prev);
4177 /*
4178 * Check to see if the given block number is past the end of the
4179 * file, back up to the last block if so...
4180 */
4181 if (eof) {
4182 ep = &ifp->if_u1.if_extents[--lastx];
4183 xfs_bmbt_get_all(ep, &got);
4184 bno = got.br_startoff + got.br_blockcount - 1;
4185 }
4186 logflags = 0;
4187 if (ifp->if_flags & XFS_IFBROOT) {
4188 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
4189 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
4190 whichfork);
4191 cur->bc_private.b.firstblock = *firstblock;
4192 cur->bc_private.b.flist = flist;
4193 cur->bc_private.b.flags = 0;
4194 } else
4195 cur = NULL;
4196 extno = 0;
4197 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
4198 (nexts == 0 || extno < nexts)) {
4199 /*
4200 * Is the found extent after a hole in which bno lives?
4201 * Just back up to the previous extent, if so.
4202 */
4203 if (got.br_startoff > bno) {
4204 if (--lastx < 0)
4205 break;
4206 ep--;
4207 xfs_bmbt_get_all(ep, &got);
4208 }
4209 /*
4210 * Is the last block of this extent before the range
4211 * we're supposed to delete? If so, we're done.
4212 */
4213 bno = XFS_FILEOFF_MIN(bno,
4214 got.br_startoff + got.br_blockcount - 1);
4215 if (bno < start)
4216 break;
4217 /*
4218 * Then deal with the (possibly delayed) allocated space
4219 * we found.
4220 */
4221 ASSERT(ep != NULL);
4222 del = got;
4223 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
4224 if (got.br_startoff < start) {
4225 del.br_startoff = start;
4226 del.br_blockcount -= start - got.br_startoff;
4227 if (!wasdel)
4228 del.br_startblock += start - got.br_startoff;
4229 }
4230 if (del.br_startoff + del.br_blockcount > bno + 1)
4231 del.br_blockcount = bno + 1 - del.br_startoff;
4232 sum = del.br_startblock + del.br_blockcount;
4233 if (isrt &&
4234 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
4235 /*
4236 * Realtime extent not lined up at the end.
4237 * The extent could have been split into written
4238 * and unwritten pieces, or we could just be
4239 * unmapping part of it. But we can't really
4240 * get rid of part of a realtime extent.
4241 */
4242 if (del.br_state == XFS_EXT_UNWRITTEN ||
4243 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4244 /*
4245 * This piece is unwritten, or we're not
4246 * using unwritten extents. Skip over it.
4247 */
4248 ASSERT(bno >= mod);
4249 bno -= mod > del.br_blockcount ?
4250 del.br_blockcount : mod;
4251 if (bno < got.br_startoff) {
4252 if (--lastx >= 0)
4253 xfs_bmbt_get_all(--ep, &got);
4254 }
4255 continue;
4256 }
4257 /*
4258 * It's written, turn it unwritten.
4259 * This is better than zeroing it.
4260 */
4261 ASSERT(del.br_state == XFS_EXT_NORM);
4262 ASSERT(xfs_trans_get_block_res(tp) > 0);
4263 /*
4264 * If this spans a realtime extent boundary,
4265 * chop it back to the start of the one we end at.
4266 */
4267 if (del.br_blockcount > mod) {
4268 del.br_startoff += del.br_blockcount - mod;
4269 del.br_startblock += del.br_blockcount - mod;
4270 del.br_blockcount = mod;
4271 }
4272 del.br_state = XFS_EXT_UNWRITTEN;
4273 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
4274 firstblock, flist, &logflags, XFS_DATA_FORK, 0);
4275 if (error)
4276 goto error0;
4277 goto nodelete;
4278 }
4279 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
4280 /*
4281 * Realtime extent is lined up at the end but not
4282 * at the front. We'll get rid of full extents if
4283 * we can.
4284 */
4285 mod = mp->m_sb.sb_rextsize - mod;
4286 if (del.br_blockcount > mod) {
4287 del.br_blockcount -= mod;
4288 del.br_startoff += mod;
4289 del.br_startblock += mod;
4290 } else if ((del.br_startoff == start &&
4291 (del.br_state == XFS_EXT_UNWRITTEN ||
4292 xfs_trans_get_block_res(tp) == 0)) ||
4293 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4294 /*
4295 * Can't make it unwritten. There isn't
4296 * a full extent here so just skip it.
4297 */
4298 ASSERT(bno >= del.br_blockcount);
4299 bno -= del.br_blockcount;
4300 if (bno < got.br_startoff) {
4301 if (--lastx >= 0)
4302 xfs_bmbt_get_all(--ep, &got);
4303 }
4304 continue;
4305 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
4306 /*
4307 * This one is already unwritten.
4308 * It must have a written left neighbor.
4309 * Unwrite the killed part of that one and
4310 * try again.
4311 */
4312 ASSERT(lastx > 0);
4313 xfs_bmbt_get_all(ep - 1, &prev);
4314 ASSERT(prev.br_state == XFS_EXT_NORM);
4315 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
4316 ASSERT(del.br_startblock ==
4317 prev.br_startblock + prev.br_blockcount);
4318 if (prev.br_startoff < start) {
4319 mod = start - prev.br_startoff;
4320 prev.br_blockcount -= mod;
4321 prev.br_startblock += mod;
4322 prev.br_startoff = start;
4323 }
4324 prev.br_state = XFS_EXT_UNWRITTEN;
4325 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
4326 &prev, firstblock, flist, &logflags,
4327 XFS_DATA_FORK, 0);
4328 if (error)
4329 goto error0;
4330 goto nodelete;
4331 } else {
4332 ASSERT(del.br_state == XFS_EXT_NORM);
4333 del.br_state = XFS_EXT_UNWRITTEN;
4334 error = xfs_bmap_add_extent(ip, lastx, &cur,
4335 &del, firstblock, flist, &logflags,
4336 XFS_DATA_FORK, 0);
4337 if (error)
4338 goto error0;
4339 goto nodelete;
4340 }
4341 }
4342 if (wasdel) {
4343 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
4344 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
4345 (int)del.br_blockcount, rsvd);
4346 if (XFS_IS_QUOTA_ON(ip->i_mount)) {
4347 ASSERT(ip->i_ino != mp->m_sb.sb_uquotino);
4348 ASSERT(ip->i_ino != mp->m_sb.sb_pquotino);
4349 if (!isrt)
4350 xfs_trans_unreserve_blkquota(NULL, ip,
4351 (long)del.br_blockcount);
4352 else
4353 xfs_trans_unreserve_rtblkquota(NULL, ip,
4354 (long)del.br_blockcount);
4355 }
4356 ip->i_delayed_blks -= del.br_blockcount;
4357 if (cur)
4358 cur->bc_private.b.flags |=
4359 XFS_BTCUR_BPRV_WASDEL;
4360 } else if (cur)
4361 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
4362 /*
4363 * If it's the case where the directory code is running
4364 * with no block reservation, and the deleted block is in
4365 * the middle of its extent, and the resulting insert
4366 * of an extent would cause transformation to btree format,
4367 * then reject it. The calling code will then swap
4368 * blocks around instead.
4369 * We have to do this now, rather than waiting for the
4370 * conversion to btree format, since the transaction
4371 * will be dirty.
4372 */
4373 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
4374 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4375 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
4376 del.br_startoff > got.br_startoff &&
4377 del.br_startoff + del.br_blockcount <
4378 got.br_startoff + got.br_blockcount) {
4379 error = XFS_ERROR(ENOSPC);
4380 goto error0;
4381 }
4382 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
4383 flags, &tmp_logflags, whichfork, rsvd);
4384 logflags |= tmp_logflags;
4385 if (error)
4386 goto error0;
4387 bno = del.br_startoff - 1;
4388nodelete:
4389 lastx = ifp->if_lastex;
4390 /*
4391 * If not done go on to the next (previous) record.
4392 * Reset ep in case the extents array was re-alloced.
4393 */
4394 ep = &ifp->if_u1.if_extents[lastx];
4395 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
4396 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
4397 xfs_bmbt_get_startoff(ep) > bno) {
4398 lastx--;
4399 ep--;
4400 }
4401 if (lastx >= 0)
4402 xfs_bmbt_get_all(ep, &got);
4403 extno++;
4404 }
4405 }
4406 ifp->if_lastex = lastx;
4407 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
4408 ASSERT(ifp->if_ext_max ==
4409 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4410 /*
4411 * Convert to a btree if necessary.
4412 */
4413 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4414 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
4415 ASSERT(cur == NULL);
4416 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
4417 &cur, 0, &tmp_logflags, whichfork);
4418 logflags |= tmp_logflags;
4419 if (error)
4420 goto error0;
4421 }
4422 /*
4423 * transform from btree to extents, give it cur
4424 */
4425 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4426 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4427 ASSERT(cur != NULL);
4428 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
4429 whichfork, async);
4430 logflags |= tmp_logflags;
4431 if (error)
4432 goto error0;
4433 }
4434 /*
4435 * transform from extents to local?
4436 */
4437 ASSERT(ifp->if_ext_max ==
4438 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4439 error = 0;
4440error0:
4441 /*
4442 * Log everything. Do this after conversion, there's no point in
4443 * logging the extent list if we've converted to btree format.
4444 */
4445 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
4446 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4447 logflags &= ~XFS_ILOG_FEXT(whichfork);
4448 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
4449 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4450 logflags &= ~XFS_ILOG_FBROOT(whichfork);
4451 /*
4452 * Log inode even in the error case, if the transaction
4453 * is dirty we'll need to shut down the filesystem.
4454 */
4455 if (logflags)
4456 xfs_trans_log_inode(tp, ip, logflags);
4457 if (cur) {
4458 if (!error) {
4459 *firstblock = cur->bc_private.b.firstblock;
4460 cur->bc_private.b.allocated = 0;
4461 }
4462 xfs_btree_del_cursor(cur,
4463 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4464 }
4465 return error;
4466}
4467
4468/*
4469 * Check the last inode extent to determine whether this allocation will result
4470 * in blocks being allocated at the end of the file. When we allocate new data
4471 * blocks at the end of the file which do not start at the previous data block,
4472 * we will try to align the new blocks at stripe unit boundaries.
4473 */
4474int /* error */
4475xfs_bmap_isaeof(
4476 xfs_inode_t *ip, /* incore inode pointer */
4477 xfs_fileoff_t off, /* file offset in fsblocks */
4478 int whichfork, /* data or attribute fork */
4479 int *aeof) /* return value */
4480{
4481 int error; /* error return value */
4482 xfs_ifork_t *ifp; /* inode fork pointer */
4483 xfs_bmbt_rec_t *lastrec; /* extent list entry pointer */
4484 xfs_extnum_t nextents; /* size of extent list */
4485 xfs_bmbt_irec_t s; /* expanded extent list entry */
4486
4487 ASSERT(whichfork == XFS_DATA_FORK);
4488 ifp = XFS_IFORK_PTR(ip, whichfork);
4489 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4490 (error = xfs_iread_extents(NULL, ip, whichfork)))
4491 return error;
4492 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4493 if (nextents == 0) {
4494 *aeof = 1;
4495 return 0;
4496 }
4497 /*
4498 * Go to the last extent
4499 */
4500 lastrec = &ifp->if_u1.if_extents[nextents - 1];
4501 xfs_bmbt_get_all(lastrec, &s);
4502 /*
4503 * Check we are allocating in the last extent (for delayed allocations)
4504 * or past the last extent for non-delayed allocations.
4505 */
4506 *aeof = (off >= s.br_startoff &&
4507 off < s.br_startoff + s.br_blockcount &&
4508 ISNULLSTARTBLOCK(s.br_startblock)) ||
4509 off >= s.br_startoff + s.br_blockcount;
4510 return 0;
4511}