]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_bmap.c
Fix builds for FreeBSD, thanks to Craig Rodrigues.
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_bmap.c
CommitLineData
2bd0ea18 1/*
eae766ca 2 * Copyright (c) 2000-2003 Silicon Graphics, Inc. All Rights Reserved.
5000d01d 3 *
2bd0ea18
NS
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.
5000d01d 7 *
2bd0ea18
NS
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.
5000d01d 11 *
2bd0ea18
NS
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
dfc130f3 14 * or the like. Any license provided herein, whether implied or
2bd0ea18
NS
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.
5000d01d 18 *
2bd0ea18
NS
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.
5000d01d 22 *
2bd0ea18
NS
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
5000d01d
SL
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
2bd0ea18
NS
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 */
dfc130f3 45 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2bd0ea18 46 xfs_bmbt_irec_t *new, /* new data to put in extent list */
dfc130f3
RC
47 xfs_fsblock_t *first, /* pointer to firstblock variable */
48 xfs_bmap_free_t *flist, /* list of extents to be freed */
2bd0ea18
NS
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
32a82561 64 XFS_STATS_INC(xs_add_exlist);
2bd0ea18
NS
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);
0e266570
NS
95 if ((error = xfs_bmap_add_extent_hole_delay(ip, idx, cur, new,
96 &logflags, rsvd)))
2bd0ea18
NS
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);
0e266570
NS
106 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur, new,
107 &logflags, whichfork)))
2bd0ea18
NS
108 goto done;
109 } else {
dfc130f3 110 xfs_bmbt_irec_t prev; /* old extent at offset idx */
2bd0ea18
NS
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) {
5000d01d 124 if (prev.br_state != XFS_EXT_UNWRITTEN &&
2bd0ea18
NS
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);
0e266570 130 if ((error = xfs_bmap_add_extent_delay_real(ip,
2bd0ea18 131 idx, &cur, new, &da_new, first, flist,
0e266570 132 &logflags, rsvd)))
2bd0ea18
NS
133 goto done;
134 } else if (new->br_state == XFS_EXT_NORM) {
135 ASSERT(new->br_state == XFS_EXT_NORM);
0e266570
NS
136 if ((error = xfs_bmap_add_extent_unwritten_real(
137 ip, idx, &cur, new, &logflags)))
2bd0ea18
NS
138 goto done;
139 } else {
140 ASSERT(new->br_state == XFS_EXT_UNWRITTEN);
0e266570
NS
141 if ((error = xfs_bmap_add_extent_unwritten_real(
142 ip, idx, &cur, new, &logflags)))
2bd0ea18
NS
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);
0e266570
NS
154 if ((error = xfs_bmap_add_extent_hole_real(ip, idx, cur,
155 new, &logflags, whichfork)))
2bd0ea18
NS
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:
062998e3 198#ifdef DEBUG
2bd0ea18
NS
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 */
dfc130f3 214 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2bd0ea18
NS
215 xfs_bmbt_irec_t *new, /* new data to put in extent list */
216 xfs_filblks_t *dnew, /* new delayed-alloc indirect blocks */
dfc130f3
RC
217 xfs_fsblock_t *first, /* pointer to firstblock variable */
218 xfs_bmap_free_t *flist, /* list of extents to be freed */
2bd0ea18
NS
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 */
dfc130f3 234 int rval=0; /* return value (logging flags) */
2bd0ea18
NS
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
dfc130f3
RC
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) : \
2bd0ea18 256 ((state &= ~MASK(b)), 0))
dfc130f3 257#define SWITCH_STATE \
2bd0ea18
NS
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 }
5000d01d 285 STATE_SET(LEFT_CONTIG,
2bd0ea18
NS
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 }
5000d01d 302 STATE_SET(RIGHT_CONTIG,
2bd0ea18
NS
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;
0e266570 340 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2bd0ea18 341 RIGHT.br_startblock,
0e266570 342 RIGHT.br_blockcount, &i)))
2bd0ea18
NS
343 goto done;
344 ASSERT(i == 1);
4ca431fc 345 if ((error = xfs_bmbt_delete(cur, &i)))
2bd0ea18
NS
346 goto done;
347 ASSERT(i == 1);
0e266570 348 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2bd0ea18
NS
349 goto done;
350 ASSERT(i == 1);
0e266570 351 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2bd0ea18
NS
352 LEFT.br_startblock,
353 LEFT.br_blockcount +
354 PREV.br_blockcount +
0e266570 355 RIGHT.br_blockcount, LEFT.br_state)))
2bd0ea18
NS
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;
0e266570 380 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
2bd0ea18 381 LEFT.br_startblock, LEFT.br_blockcount,
0e266570 382 &i)))
2bd0ea18
NS
383 goto done;
384 ASSERT(i == 1);
0e266570 385 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2bd0ea18
NS
386 LEFT.br_startblock,
387 LEFT.br_blockcount +
0e266570 388 PREV.br_blockcount, LEFT.br_state)))
2bd0ea18
NS
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;
0e266570 414 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2bd0ea18 415 RIGHT.br_startblock,
0e266570 416 RIGHT.br_blockcount, &i)))
2bd0ea18
NS
417 goto done;
418 ASSERT(i == 1);
0e266570 419 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
2bd0ea18
NS
420 new->br_startblock,
421 PREV.br_blockcount +
0e266570 422 RIGHT.br_blockcount, PREV.br_state)))
2bd0ea18
NS
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;
0e266570 445 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2bd0ea18 446 new->br_startblock, new->br_blockcount,
0e266570 447 &i)))
2bd0ea18
NS
448 goto done;
449 ASSERT(i == 0);
450 cur->bc_rec.b.br_state = XFS_EXT_NORM;
0e266570 451 if ((error = xfs_bmbt_insert(cur, &i)))
2bd0ea18
NS
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;
0e266570 480 if ((error = xfs_bmbt_lookup_eq(cur, LEFT.br_startoff,
2bd0ea18 481 LEFT.br_startblock, LEFT.br_blockcount,
0e266570 482 &i)))
2bd0ea18
NS
483 goto done;
484 ASSERT(i == 1);
0e266570 485 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2bd0ea18
NS
486 LEFT.br_startblock,
487 LEFT.br_blockcount +
488 new->br_blockcount,
0e266570 489 LEFT.br_state)))
2bd0ea18
NS
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;
0e266570 518 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2bd0ea18 519 new->br_startblock, new->br_blockcount,
0e266570 520 &i)))
2bd0ea18
NS
521 goto done;
522 ASSERT(i == 0);
523 cur->bc_rec.b.br_state = XFS_EXT_NORM;
0e266570 524 if ((error = xfs_bmbt_insert(cur, &i)))
2bd0ea18
NS
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,
5000d01d 560 new->br_blockcount + RIGHT.br_blockcount,
2bd0ea18
NS
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;
0e266570 569 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2bd0ea18 570 RIGHT.br_startblock,
0e266570 571 RIGHT.br_blockcount, &i)))
2bd0ea18
NS
572 goto done;
573 ASSERT(i == 1);
0e266570 574 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2bd0ea18
NS
575 new->br_startblock,
576 new->br_blockcount +
577 RIGHT.br_blockcount,
0e266570 578 RIGHT.br_state)))
2bd0ea18
NS
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;
0e266570 606 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2bd0ea18 607 new->br_startblock, new->br_blockcount,
0e266570 608 &i)))
2bd0ea18
NS
609 goto done;
610 ASSERT(i == 0);
611 cur->bc_rec.b.br_state = XFS_EXT_NORM;
0e266570 612 if ((error = xfs_bmbt_insert(cur, &i)))
2bd0ea18
NS
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;
0e266570 657 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2bd0ea18 658 new->br_startblock, new->br_blockcount,
0e266570 659 &i)))
2bd0ea18
NS
660 goto done;
661 ASSERT(i == 0);
662 cur->bc_rec.b.br_state = XFS_EXT_NORM;
0e266570 663 if ((error = xfs_bmbt_insert(cur, &i)))
2bd0ea18
NS
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 */
dfc130f3 754 xfs_btree_cur_t **curp, /* if *curp is null, not a btree */
2bd0ea18
NS
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 */
dfc130f3
RC
767 xfs_exntst_t newext; /* new extent state */
768 xfs_exntst_t oldext; /* old extent state */
2bd0ea18
NS
769 xfs_bmbt_irec_t r[3]; /* neighbor extent entries */
770 /* left is 0, right is 1, prev is 2 */
dfc130f3 771 int rval=0; /* return value (logging flags) */
2bd0ea18
NS
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
dfc130f3
RC
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) : \
2bd0ea18 790 ((state &= ~MASK(b)), 0))
dfc130f3 791#define SWITCH_STATE \
2bd0ea18
NS
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 }
5000d01d 824 STATE_SET(LEFT_CONTIG,
2bd0ea18
NS
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 }
5000d01d 841 STATE_SET(RIGHT_CONTIG,
2bd0ea18
NS
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;
0e266570 878 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2bd0ea18 879 RIGHT.br_startblock,
0e266570 880 RIGHT.br_blockcount, &i)))
2bd0ea18
NS
881 goto done;
882 ASSERT(i == 1);
4ca431fc 883 if ((error = xfs_bmbt_delete(cur, &i)))
2bd0ea18
NS
884 goto done;
885 ASSERT(i == 1);
0e266570 886 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2bd0ea18
NS
887 goto done;
888 ASSERT(i == 1);
4ca431fc 889 if ((error = xfs_bmbt_delete(cur, &i)))
2bd0ea18
NS
890 goto done;
891 ASSERT(i == 1);
0e266570 892 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2bd0ea18
NS
893 goto done;
894 ASSERT(i == 1);
0e266570 895 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2bd0ea18
NS
896 LEFT.br_startblock,
897 LEFT.br_blockcount + PREV.br_blockcount +
0e266570 898 RIGHT.br_blockcount, LEFT.br_state)))
2bd0ea18
NS
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;
0e266570 923 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2bd0ea18 924 PREV.br_startblock, PREV.br_blockcount,
0e266570 925 &i)))
2bd0ea18
NS
926 goto done;
927 ASSERT(i == 1);
4ca431fc 928 if ((error = xfs_bmbt_delete(cur, &i)))
2bd0ea18
NS
929 goto done;
930 ASSERT(i == 1);
0e266570 931 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2bd0ea18
NS
932 goto done;
933 ASSERT(i == 1);
0e266570 934 if ((error = xfs_bmbt_update(cur, LEFT.br_startoff,
2bd0ea18
NS
935 LEFT.br_startblock,
936 LEFT.br_blockcount + PREV.br_blockcount,
0e266570 937 LEFT.br_state)))
2bd0ea18
NS
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;
0e266570 963 if ((error = xfs_bmbt_lookup_eq(cur, RIGHT.br_startoff,
2bd0ea18 964 RIGHT.br_startblock,
0e266570 965 RIGHT.br_blockcount, &i)))
2bd0ea18
NS
966 goto done;
967 ASSERT(i == 1);
4ca431fc 968 if ((error = xfs_bmbt_delete(cur, &i)))
2bd0ea18
NS
969 goto done;
970 ASSERT(i == 1);
0e266570 971 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2bd0ea18
NS
972 goto done;
973 ASSERT(i == 1);
0e266570 974 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2bd0ea18
NS
975 new->br_startblock,
976 new->br_blockcount + RIGHT.br_blockcount,
0e266570 977 newext)))
2bd0ea18
NS
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;
0e266570 998 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2bd0ea18 999 new->br_startblock, new->br_blockcount,
0e266570 1000 &i)))
2bd0ea18
NS
1001 goto done;
1002 ASSERT(i == 1);
0e266570 1003 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2bd0ea18 1004 new->br_startblock, new->br_blockcount,
0e266570 1005 newext)))
2bd0ea18
NS
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;
0e266570 1036 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2bd0ea18 1037 PREV.br_startblock, PREV.br_blockcount,
0e266570 1038 &i)))
2bd0ea18
NS
1039 goto done;
1040 ASSERT(i == 1);
0e266570 1041 if ((error = xfs_bmbt_update(cur,
2bd0ea18
NS
1042 PREV.br_startoff + new->br_blockcount,
1043 PREV.br_startblock + new->br_blockcount,
1044 PREV.br_blockcount - new->br_blockcount,
0e266570 1045 oldext)))
2bd0ea18 1046 goto done;
0e266570 1047 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2bd0ea18
NS
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;
0e266570 1079 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2bd0ea18 1080 PREV.br_startblock, PREV.br_blockcount,
0e266570 1081 &i)))
2bd0ea18
NS
1082 goto done;
1083 ASSERT(i == 1);
0e266570 1084 if ((error = xfs_bmbt_update(cur,
2bd0ea18
NS
1085 PREV.br_startoff + new->br_blockcount,
1086 PREV.br_startblock + new->br_blockcount,
1087 PREV.br_blockcount - new->br_blockcount,
0e266570 1088 oldext)))
2bd0ea18
NS
1089 goto done;
1090 cur->bc_rec.b = *new;
0e266570 1091 if ((error = xfs_bmbt_insert(cur, &i)))
2bd0ea18
NS
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;
0e266570 1119 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2bd0ea18 1120 PREV.br_startblock,
0e266570 1121 PREV.br_blockcount, &i)))
2bd0ea18
NS
1122 goto done;
1123 ASSERT(i == 1);
0e266570 1124 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
5000d01d 1125 PREV.br_startblock,
2bd0ea18 1126 PREV.br_blockcount - new->br_blockcount,
0e266570 1127 oldext)))
2bd0ea18 1128 goto done;
0e266570 1129 if ((error = xfs_bmbt_increment(cur, 0, &i)))
2bd0ea18 1130 goto done;
0e266570 1131 if ((error = xfs_bmbt_update(cur, new->br_startoff,
2bd0ea18
NS
1132 new->br_startblock,
1133 new->br_blockcount + RIGHT.br_blockcount,
0e266570 1134 newext)))
2bd0ea18
NS
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;
0e266570 1157 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2bd0ea18 1158 PREV.br_startblock, PREV.br_blockcount,
0e266570 1159 &i)))
2bd0ea18
NS
1160 goto done;
1161 ASSERT(i == 1);
0e266570 1162 if ((error = xfs_bmbt_update(cur, PREV.br_startoff,
5000d01d 1163 PREV.br_startblock,
2bd0ea18 1164 PREV.br_blockcount - new->br_blockcount,
0e266570 1165 oldext)))
2bd0ea18 1166 goto done;
0e266570 1167 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
2bd0ea18 1168 new->br_startblock, new->br_blockcount,
0e266570 1169 &i)))
2bd0ea18
NS
1170 goto done;
1171 ASSERT(i == 0);
1172 cur->bc_rec.b.br_state = XFS_EXT_NORM;
0e266570 1173 if ((error = xfs_bmbt_insert(cur, &i)))
2bd0ea18
NS
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;
0e266570 1204 if ((error = xfs_bmbt_lookup_eq(cur, PREV.br_startoff,
2bd0ea18 1205 PREV.br_startblock, PREV.br_blockcount,
0e266570 1206 &i)))
2bd0ea18
NS
1207 goto done;
1208 ASSERT(i == 1);
1209 /* new right extent - oldext */
0e266570 1210 if ((error = xfs_bmbt_update(cur, r[1].br_startoff,
2bd0ea18 1211 r[1].br_startblock, r[1].br_blockcount,
0e266570 1212 r[1].br_state)))
2bd0ea18
NS
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;
0e266570 1218 if ((error = xfs_bmbt_insert(cur, &i)))
2bd0ea18
NS
1219 goto done;
1220 ASSERT(i == 1);
0e266570 1221 if ((error = xfs_bmbt_increment(cur, 0, &i)))
2bd0ea18
NS
1222 goto done;
1223 ASSERT(i == 1);
1224 /* new middle extent - newext */
1225 cur->bc_rec.b = *new;
0e266570 1226 if ((error = xfs_bmbt_insert(cur, &i)))
2bd0ea18
NS
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 */
0e266570
NS
1281 xfs_filblks_t newlen=0; /* new indirect size */
1282 xfs_filblks_t oldlen=0; /* old indirect size */
2bd0ea18 1283 xfs_bmbt_irec_t right; /* right neighbor extent entry */
dfc130f3 1284 int state; /* state bits, accessed thru macros */
2bd0ea18
NS
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
dfc130f3
RC
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) : \
2bd0ea18 1297 ((state &= ~MASK(b)), 0))
dfc130f3 1298#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2bd0ea18
NS
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 */
5000d01d 1325 STATE_SET(LEFT_CONTIG,
2bd0ea18
NS
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,
5000d01d 1396 NULLSTARTBLOCK((int)newlen), temp, right.br_state);
2bd0ea18
NS
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
dfc130f3
RC
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) : \
2bd0ea18 1466 ((state &= ~MASK(b)), 0))
dfc130f3 1467#define SWITCH_STATE (state & MASK2(LEFT_CONTIG, RIGHT_CONTIG))
2bd0ea18
NS
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 */
5000d01d 1494 STATE_SET(LEFT_CONTIG,
2bd0ea18
NS
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;
0e266570
NS
1540 if ((error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
1541 right.br_startblock, right.br_blockcount, &i)))
2bd0ea18
NS
1542 return error;
1543 ASSERT(i == 1);
4ca431fc 1544 if ((error = xfs_bmbt_delete(cur, &i)))
2bd0ea18
NS
1545 return error;
1546 ASSERT(i == 1);
0e266570 1547 if ((error = xfs_bmbt_decrement(cur, 0, &i)))
2bd0ea18
NS
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;
0e266570
NS
1572 if ((error = xfs_bmbt_lookup_eq(cur, left.br_startoff,
1573 left.br_startblock, left.br_blockcount, &i)))
2bd0ea18
NS
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;
0e266570
NS
1599 if ((error = xfs_bmbt_lookup_eq(cur, right.br_startoff,
1600 right.br_startblock, right.br_blockcount, &i)))
2bd0ea18
NS
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;
0e266570
NS
1626 if ((error = xfs_bmbt_lookup_eq(cur, new->br_startoff,
1627 new->br_startblock, new->br_blockcount, &i)))
2bd0ea18
NS
1628 return error;
1629 ASSERT(i == 0);
1630 cur->bc_rec.b.br_state = new->br_state;
0e266570 1631 if ((error = xfs_bmbt_insert(cur, &i)))
2bd0ea18
NS
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 */
dfc130f3 1658 xfs_alloctype_t atype=0; /* type for allocation routines */
2bd0ea18
NS
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__
0e266570
NS
1665 xfs_extlen_t prod=0; /* product factor for allocators */
1666 xfs_extlen_t ralen=0; /* realtime allocation length */
2bd0ea18
NS
1667#endif
1668
05bba5b7 1669#define ISVALID(x,y) \
2bd0ea18
NS
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;
eae766ca 1681 rt = XFS_IS_REALTIME_INODE(ap->ip) && ap->userdata;
2bd0ea18
NS
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
dfc130f3 1706 * we need to align it. This will be possible unless
2bd0ea18
NS
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 */
0e266570 1718 if ((temp = (ap->alen % extsz)))
2bd0ea18
NS
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 */
5000d01d 1739 nexto = (ap->eof || ap->gotp->br_startoff == NULLFILEOFF) ?
2bd0ea18
NS
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
05bba5b7 1748 * the start forward to the first valid spot and set
2bd0ea18
NS
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 */
0e266570 1761 if ((temp = (ap->alen % mp->m_sb.sb_rextsize))) {
2bd0ea18
NS
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
05bba5b7 1809 * we rounded up to it, cut it back so it's valid again.
2bd0ea18
NS
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) &&
05bba5b7 1842 ISVALID(ap->prevp->br_startblock + ap->prevp->br_blockcount,
2bd0ea18
NS
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 &&
05bba5b7 1851 ISVALID(ap->rval + adjust, ap->prevp->br_startblock))
2bd0ea18
NS
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 */
0e266570 1861 xfs_fsblock_t gotdiff=0; /* right side difference */
2bd0ea18 1862 xfs_fsblock_t prevbno; /* left side block number */
0e266570 1863 xfs_fsblock_t prevdiff=0; /* left side difference */
2bd0ea18
NS
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) &&
05bba5b7 1873 ISVALID(prevbno, ap->prevp->br_startblock)) {
2bd0ea18
NS
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
05bba5b7 1885 * allocating, or using it gives us an invalid block
2bd0ea18
NS
1886 * number, then just use the end of the previous block.
1887 */
1888 if (prevdiff <= XFS_ALLOC_GAP_UNITS * ap->alen &&
05bba5b7 1889 ISVALID(prevbno + prevdiff,
2bd0ea18
NS
1890 ap->prevp->br_startblock))
1891 prevbno += adjust;
1892 else
1893 prevdiff += adjust;
1894 /*
5000d01d 1895 * If the firstblock forbids it, can't use it,
2bd0ea18
NS
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
05bba5b7 1924 * allocating, or using it gives us an invalid block
2bd0ea18
NS
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 &&
05bba5b7 1929 ISVALID(gotbno - gotdiff, gotbno))
2bd0ea18 1930 gotbno -= adjust;
05bba5b7 1931 else if (ISVALID(gotbno - ap->alen, gotbno)) {
2bd0ea18
NS
1932 gotbno -= ap->alen;
1933 gotdiff += adjust - ap->alen;
1934 } else
1935 gotdiff += adjust;
1936 /*
5000d01d 1937 * If the firstblock forbids it, can't use it,
2bd0ea18
NS
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;
0e266570
NS
1982 if ((error = xfs_rtallocate_extent(ap->tp, ap->rval, 1, ap->alen,
1983 &ralen, atype, ap->wasdel, prod, &rtb)))
2bd0ea18
NS
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 /*
5000d01d 2000 * Adjust the disk quota also. This was reserved
2bd0ea18
NS
2001 * earlier.
2002 */
eae766ca
NS
2003 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2004 ap->wasdel ? XFS_TRANS_DQ_DELRTBCOUNT :
2bd0ea18 2005 XFS_TRANS_DQ_RTBCOUNT,
eae766ca 2006 (long) ralen);
2bd0ea18
NS
2007 } else
2008 ap->alen = 0;
2009#endif /* __KERNEL__ */
2010 }
2011 /*
2012 * Normal allocation, done through xfs_alloc_vextent.
2013 */
2014 else {
2015 xfs_agnumber_t ag;
dfc130f3 2016 xfs_alloc_arg_t args;
2bd0ea18
NS
2017 xfs_extlen_t blen;
2018 xfs_extlen_t delta;
2019 int isaligned;
2020 xfs_extlen_t longest;
2021 xfs_extlen_t need;
0e266570 2022 xfs_extlen_t nextminlen=0;
2bd0ea18
NS
2023 int notinit;
2024 xfs_perag_t *pag;
2025 xfs_agnumber_t startag;
2026 int tryagain;
2027
2028 tryagain = isaligned = 0;
2029 args.tp = ap->tp;
2030 args.mp = mp;
2031 args.fsbno = ap->rval;
2032 args.maxlen = MIN(ap->alen, mp->m_sb.sb_agblocks);
2033 blen = 0;
2034 if (nullfb) {
2035 args.type = XFS_ALLOCTYPE_START_BNO;
2036 args.total = ap->total;
2037 /*
2038 * Find the longest available space.
2039 * We're going to try for the whole allocation at once.
2040 */
2041 startag = ag = XFS_FSB_TO_AGNO(mp, args.fsbno);
2042 notinit = 0;
e56fcdce 2043 down_read(&mp->m_peraglock);
2bd0ea18
NS
2044 while (blen < ap->alen) {
2045 pag = &mp->m_perag[ag];
2046 if (!pag->pagf_init &&
2047 (error = xfs_alloc_pagf_init(mp, args.tp,
2048 ag, XFS_ALLOC_FLAG_TRYLOCK))) {
e56fcdce 2049 up_read(&mp->m_peraglock);
2bd0ea18
NS
2050 return error;
2051 }
2052 /*
2053 * See xfs_alloc_fix_freelist...
2054 */
2055 if (pag->pagf_init) {
2056 need = XFS_MIN_FREELIST_PAG(pag, mp);
2057 delta = need > pag->pagf_flcount ?
2058 need - pag->pagf_flcount : 0;
2059 longest = (pag->pagf_longest > delta) ?
2060 (pag->pagf_longest - delta) :
2061 (pag->pagf_flcount > 0 ||
2062 pag->pagf_longest > 0);
2063 if (blen < longest)
2064 blen = longest;
2065 } else
2066 notinit = 1;
5000d01d 2067 if (++ag == mp->m_sb.sb_agcount)
2bd0ea18
NS
2068 ag = 0;
2069 if (ag == startag)
2070 break;
2071 }
e56fcdce 2072 up_read(&mp->m_peraglock);
5000d01d 2073 /*
2bd0ea18
NS
2074 * Since the above loop did a BUF_TRYLOCK, it is
2075 * possible that there is space for this request.
5000d01d 2076 */
2bd0ea18 2077 if (notinit || blen < ap->minlen)
5000d01d 2078 args.minlen = ap->minlen;
2bd0ea18
NS
2079 /*
2080 * If the best seen length is less than the request
2081 * length, use the best as the minimum.
2082 */
2083 else if (blen < ap->alen)
2084 args.minlen = blen;
2085 /*
2086 * Otherwise we've seen an extent as big as alen,
2087 * use that as the minimum.
2088 */
5000d01d 2089 else
2bd0ea18
NS
2090 args.minlen = ap->alen;
2091 } else if (ap->low) {
2092 args.type = XFS_ALLOCTYPE_FIRST_AG;
2093 args.total = args.minlen = ap->minlen;
2094 } else {
2095 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2096 args.total = ap->total;
2097 args.minlen = ap->minlen;
2098 }
2099 if (ap->ip->i_d.di_extsize) {
2100 args.prod = ap->ip->i_d.di_extsize;
0e266570 2101 if ((args.mod = (xfs_extlen_t)do_mod(ap->off, args.prod)))
2bd0ea18
NS
2102 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2103 } else if (mp->m_sb.sb_blocksize >= NBPP) {
2104 args.prod = 1;
2105 args.mod = 0;
2106 } else {
2107 args.prod = NBPP >> mp->m_sb.sb_blocklog;
0e266570 2108 if ((args.mod = (xfs_extlen_t)(do_mod(ap->off, args.prod))))
2bd0ea18
NS
2109 args.mod = (xfs_extlen_t)(args.prod - args.mod);
2110 }
2111 /*
5000d01d 2112 * If we are not low on available data blocks, and the
2bd0ea18 2113 * underlying logical volume manager is a stripe, and
5000d01d 2114 * the file offset is zero then try to allocate data
2bd0ea18
NS
2115 * blocks on stripe unit boundary.
2116 * NOTE: ap->aeof is only set if the allocation length
2117 * is >= the stripe unit and the allocation offset is
5000d01d
SL
2118 * at the end of file.
2119 */
2bd0ea18
NS
2120 if (!ap->low && ap->aeof) {
2121 if (!ap->off) {
2122 args.alignment = mp->m_dalign;
2123 atype = args.type;
2124 isaligned = 1;
2125 /*
2126 * Adjust for alignment
2127 */
5000d01d 2128 if (blen > args.alignment && blen <= ap->alen)
2bd0ea18
NS
2129 args.minlen = blen - args.alignment;
2130 args.minalignslop = 0;
2131 } else {
2132 /*
5000d01d 2133 * First try an exact bno allocation.
2bd0ea18
NS
2134 * If it fails then do a near or start bno
2135 * allocation with alignment turned on.
5000d01d 2136 */
2bd0ea18
NS
2137 atype = args.type;
2138 tryagain = 1;
2139 args.type = XFS_ALLOCTYPE_THIS_BNO;
2140 args.alignment = 1;
2141 /*
2142 * Compute the minlen+alignment for the
2143 * next case. Set slop so that the value
2144 * of minlen+alignment+slop doesn't go up
2145 * between the calls.
2146 */
5000d01d 2147 if (blen > mp->m_dalign && blen <= ap->alen)
2bd0ea18
NS
2148 nextminlen = blen - mp->m_dalign;
2149 else
2150 nextminlen = args.minlen;
2151 if (nextminlen + mp->m_dalign > args.minlen + 1)
2152 args.minalignslop =
2153 nextminlen + mp->m_dalign -
2154 args.minlen - 1;
2155 else
2156 args.minalignslop = 0;
2157 }
2158 } else {
2159 args.alignment = 1;
2160 args.minalignslop = 0;
2161 }
2162 args.minleft = ap->minleft;
2163 args.wasdel = ap->wasdel;
2164 args.isfl = 0;
2165 args.userdata = ap->userdata;
0e266570 2166 if ((error = xfs_alloc_vextent(&args)))
2bd0ea18
NS
2167 return error;
2168 if (tryagain && args.fsbno == NULLFSBLOCK) {
2169 /*
2170 * Exact allocation failed. Now try with alignment
2171 * turned on.
2172 */
5000d01d
SL
2173 args.type = atype;
2174 args.fsbno = ap->rval;
2175 args.alignment = mp->m_dalign;
2bd0ea18
NS
2176 args.minlen = nextminlen;
2177 args.minalignslop = 0;
2178 isaligned = 1;
5000d01d
SL
2179 if ((error = xfs_alloc_vextent(&args)))
2180 return error;
2181 }
2bd0ea18 2182 if (isaligned && args.fsbno == NULLFSBLOCK) {
5000d01d 2183 /*
2bd0ea18
NS
2184 * allocation failed, so turn off alignment and
2185 * try again.
2186 */
2187 args.type = atype;
2188 args.fsbno = ap->rval;
2189 args.alignment = 0;
0e266570 2190 if ((error = xfs_alloc_vextent(&args)))
2bd0ea18
NS
2191 return error;
2192 }
2193 if (args.fsbno == NULLFSBLOCK && nullfb &&
2194 args.minlen > ap->minlen) {
2195 args.minlen = ap->minlen;
2196 args.type = XFS_ALLOCTYPE_START_BNO;
2197 args.fsbno = ap->rval;
0e266570 2198 if ((error = xfs_alloc_vextent(&args)))
2bd0ea18
NS
2199 return error;
2200 }
2201 if (args.fsbno == NULLFSBLOCK && nullfb) {
2202 args.fsbno = 0;
2203 args.type = XFS_ALLOCTYPE_FIRST_AG;
2204 args.total = ap->minlen;
2205 args.minleft = 0;
0e266570 2206 if ((error = xfs_alloc_vextent(&args)))
2bd0ea18
NS
2207 return error;
2208 ap->low = 1;
2209 }
2210 if (args.fsbno != NULLFSBLOCK) {
2211 ap->firstblock = ap->rval = args.fsbno;
2212 ASSERT(nullfb || fb_agno == args.agno ||
2213 (ap->low && fb_agno < args.agno));
2214 ap->alen = args.len;
2215 ap->ip->i_d.di_nblocks += args.len;
2216 xfs_trans_log_inode(ap->tp, ap->ip, XFS_ILOG_CORE);
2217 if (ap->wasdel)
2218 ap->ip->i_delayed_blks -= args.len;
2219 /*
5000d01d 2220 * Adjust the disk quota also. This was reserved
2bd0ea18
NS
2221 * earlier.
2222 */
eae766ca
NS
2223 XFS_TRANS_MOD_DQUOT_BYINO(mp, ap->tp, ap->ip,
2224 ap->wasdel ? XFS_TRANS_DQ_DELBCOUNT :
2bd0ea18 2225 XFS_TRANS_DQ_BCOUNT,
eae766ca 2226 (long) args.len);
2bd0ea18
NS
2227 } else {
2228 ap->rval = NULLFSBLOCK;
2229 ap->alen = 0;
2230 }
2231 }
2232 return 0;
05bba5b7 2233#undef ISVALID
2bd0ea18
NS
2234}
2235
2236/*
2237 * Transform a btree format file with only one leaf node, where the
2238 * extents list will fit in the inode, into an extents format file.
2239 * Since the extent list is already in-core, all we have to do is
2240 * give up the space for the btree root and pitch the leaf block.
2241 */
2242STATIC int /* error */
2243xfs_bmap_btree_to_extents(
2244 xfs_trans_t *tp, /* transaction pointer */
2245 xfs_inode_t *ip, /* incore inode pointer */
2246 xfs_btree_cur_t *cur, /* btree cursor */
2247 int *logflagsp, /* inode logging flags */
4ca431fc 2248 int whichfork) /* data or attr fork */
2bd0ea18
NS
2249{
2250 /* REFERENCED */
2251 xfs_bmbt_block_t *cblock;/* child btree block */
2252 xfs_fsblock_t cbno; /* child block number */
7a3bffe4 2253 xfs_buf_t *cbp; /* child block's buffer */
2bd0ea18
NS
2254 int error; /* error return value */
2255 xfs_ifork_t *ifp; /* inode fork data */
2256 xfs_mount_t *mp; /* mount point structure */
2257 xfs_bmbt_ptr_t *pp; /* ptr to block address */
2258 xfs_bmbt_block_t *rblock;/* root btree block */
2259
2260 ifp = XFS_IFORK_PTR(ip, whichfork);
2261 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2262 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
2263 rblock = ifp->if_broot;
2264 ASSERT(INT_GET(rblock->bb_level, ARCH_CONVERT) == 1);
2265 ASSERT(INT_GET(rblock->bb_numrecs, ARCH_CONVERT) == 1);
2266 ASSERT(XFS_BMAP_BROOT_MAXRECS(ifp->if_broot_bytes) == 1);
2267 mp = ip->i_mount;
2268 pp = XFS_BMAP_BROOT_PTR_ADDR(rblock, 1, ifp->if_broot_bytes);
2269 *logflagsp = 0;
2270#ifdef DEBUG
0e266570 2271 if ((error = xfs_btree_check_lptr(cur, INT_GET(*pp, ARCH_CONVERT), 1)))
2bd0ea18
NS
2272 return error;
2273#endif
2274 cbno = INT_GET(*pp, ARCH_CONVERT);
0e266570
NS
2275 if ((error = xfs_btree_read_bufl(mp, tp, cbno, 0, &cbp,
2276 XFS_BMAP_BTREE_REF)))
2bd0ea18
NS
2277 return error;
2278 cblock = XFS_BUF_TO_BMBT_BLOCK(cbp);
0e266570 2279 if ((error = xfs_btree_check_lblock(cur, cblock, 0, cbp)))
2bd0ea18
NS
2280 return error;
2281 xfs_bmap_add_free(cbno, 1, cur->bc_private.b.flist, mp);
2bd0ea18 2282 ip->i_d.di_nblocks--;
eae766ca 2283 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, -1L);
2bd0ea18
NS
2284 xfs_trans_binval(tp, cbp);
2285 if (cur->bc_bufs[0] == cbp)
2286 cur->bc_bufs[0] = NULL;
2287 xfs_iroot_realloc(ip, -1, whichfork);
2288 ASSERT(ifp->if_broot == NULL);
2289 ASSERT((ifp->if_flags & XFS_IFBROOT) == 0);
2290 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2291 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FEXT(whichfork);
2292 return 0;
2293}
2294
2295/*
2296 * Called by xfs_bmapi to update extent list structure and the btree
2297 * after removing space (or undoing a delayed allocation).
2298 */
2299STATIC int /* error */
2300xfs_bmap_del_extent(
2301 xfs_inode_t *ip, /* incore inode pointer */
2302 xfs_trans_t *tp, /* current transaction pointer */
2303 xfs_extnum_t idx, /* extent number to update/delete */
dfc130f3 2304 xfs_bmap_free_t *flist, /* list of extents to be freed */
2bd0ea18
NS
2305 xfs_btree_cur_t *cur, /* if null, not a btree */
2306 xfs_bmbt_irec_t *del, /* data to remove from extent list */
2bd0ea18
NS
2307 int *logflagsp, /* inode logging flags */
2308 int whichfork, /* data or attr fork */
2309 int rsvd) /* OK to allocate reserved blocks */
2310{
dfc130f3
RC
2311 xfs_filblks_t da_new; /* new delay-alloc indirect blocks */
2312 xfs_filblks_t da_old; /* old delay-alloc indirect blocks */
2313 xfs_fsblock_t del_endblock=0; /* first block past del */
2bd0ea18
NS
2314 xfs_fileoff_t del_endoff; /* first offset past del */
2315 int delay; /* current block is delayed allocated */
2316 int do_fx; /* free extent at end of routine */
2317 xfs_bmbt_rec_t *ep; /* current extent entry pointer */
2318 int error; /* error return value */
2319 int flags; /* inode logging flags */
2320#ifdef XFS_BMAP_TRACE
2321 static char fname[] = "xfs_bmap_del_extent";
2322#endif
2323 xfs_bmbt_irec_t got; /* current extent entry */
2324 xfs_fileoff_t got_endoff; /* first offset past got */
2325 int i; /* temp state */
2326 xfs_ifork_t *ifp; /* inode fork pointer */
2327 xfs_mount_t *mp; /* mount structure */
2328 xfs_filblks_t nblks; /* quota/sb block count */
2329 xfs_bmbt_irec_t new; /* new record to be inserted */
2330 /* REFERENCED */
2331 xfs_extnum_t nextents; /* number of extents in list */
dfc130f3 2332 uint qfield; /* quota field to update */
2bd0ea18
NS
2333 xfs_filblks_t temp; /* for indirect length calculations */
2334 xfs_filblks_t temp2; /* for indirect length calculations */
5000d01d 2335
32a82561 2336 XFS_STATS_INC(xs_del_exlist);
5000d01d 2337 mp = ip->i_mount;
2bd0ea18
NS
2338 ifp = XFS_IFORK_PTR(ip, whichfork);
2339 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
2340 ASSERT(idx >= 0 && idx < nextents);
2341 ASSERT(del->br_blockcount > 0);
2342 ep = &ifp->if_u1.if_extents[idx];
2343 xfs_bmbt_get_all(ep, &got);
2344 ASSERT(got.br_startoff <= del->br_startoff);
2345 del_endoff = del->br_startoff + del->br_blockcount;
2346 got_endoff = got.br_startoff + got.br_blockcount;
2347 ASSERT(got_endoff >= del_endoff);
2348 delay = ISNULLSTARTBLOCK(got.br_startblock);
2349 ASSERT(ISNULLSTARTBLOCK(del->br_startblock) == delay);
2350 flags = 0;
2351 qfield = 0;
2352 error = 0;
2353 /*
2354 * If deleting a real allocation, must free up the disk space.
2355 */
2356 if (!delay) {
2357 flags = XFS_ILOG_CORE;
2358 /*
dfc130f3 2359 * Realtime allocation. Free it and record di_nblocks update.
2bd0ea18
NS
2360 */
2361 if (whichfork == XFS_DATA_FORK &&
2362 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME)) {
2363 xfs_fsblock_t bno;
2364 xfs_filblks_t len;
2365
2366 ASSERT(do_mod(del->br_blockcount,
2367 mp->m_sb.sb_rextsize) == 0);
2368 ASSERT(do_mod(del->br_startblock,
2369 mp->m_sb.sb_rextsize) == 0);
2370 bno = del->br_startblock;
2bd0ea18 2371 len = del->br_blockcount;
aeb35bda 2372 do_div(bno, mp->m_sb.sb_rextsize);
2bd0ea18 2373 do_div(len, mp->m_sb.sb_rextsize);
0e266570
NS
2374 if ((error = xfs_rtfree_extent(ip->i_transp, bno,
2375 (xfs_extlen_t)len)))
2bd0ea18
NS
2376 goto done;
2377 do_fx = 0;
2378 nblks = len * mp->m_sb.sb_rextsize;
eae766ca 2379 qfield = XFS_TRANS_DQ_RTBCOUNT;
2bd0ea18
NS
2380 }
2381 /*
2382 * Ordinary allocation.
2383 */
2384 else {
2385 do_fx = 1;
2386 nblks = del->br_blockcount;
eae766ca 2387 qfield = XFS_TRANS_DQ_BCOUNT;
2bd0ea18
NS
2388 }
2389 /*
2390 * Set up del_endblock and cur for later.
2391 */
2392 del_endblock = del->br_startblock + del->br_blockcount;
2393 if (cur) {
0e266570 2394 if ((error = xfs_bmbt_lookup_eq(cur, got.br_startoff,
2bd0ea18 2395 got.br_startblock, got.br_blockcount,
0e266570 2396 &i)))
2bd0ea18
NS
2397 goto done;
2398 ASSERT(i == 1);
2399 }
2400 da_old = da_new = 0;
2401 } else {
2402 da_old = STARTBLOCKVAL(got.br_startblock);
2403 da_new = 0;
2404 nblks = 0;
2405 do_fx = 0;
2406 }
2407 /*
2408 * Set flag value to use in switch statement.
2409 * Left-contig is 2, right-contig is 1.
2410 */
2411 switch (((got.br_startoff == del->br_startoff) << 1) |
2412 (got_endoff == del_endoff)) {
2413 case 3:
2414 /*
2415 * Matches the whole extent. Delete the entry.
2416 */
2417 xfs_bmap_trace_delete(fname, "3", ip, idx, 1, whichfork);
2418 xfs_bmap_delete_exlist(ip, idx, 1, whichfork);
2419 ifp->if_lastex = idx;
2420 if (delay)
2421 break;
2422 XFS_IFORK_NEXT_SET(ip, whichfork,
2423 XFS_IFORK_NEXTENTS(ip, whichfork) - 1);
2424 flags |= XFS_ILOG_CORE;
2425 if (!cur) {
2426 flags |= XFS_ILOG_FEXT(whichfork);
2427 break;
2428 }
4ca431fc 2429 if ((error = xfs_bmbt_delete(cur, &i)))
2bd0ea18
NS
2430 goto done;
2431 ASSERT(i == 1);
2432 break;
2433
2434 case 2:
2435 /*
2436 * Deleting the first part of the extent.
2437 */
2438 xfs_bmap_trace_pre_update(fname, "2", ip, idx, whichfork);
2439 xfs_bmbt_set_startoff(ep, del_endoff);
2440 temp = got.br_blockcount - del->br_blockcount;
2441 xfs_bmbt_set_blockcount(ep, temp);
2442 ifp->if_lastex = idx;
2443 if (delay) {
2444 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2445 da_old);
2446 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
2447 xfs_bmap_trace_post_update(fname, "2", ip, idx,
2448 whichfork);
2449 da_new = temp;
2450 break;
2451 }
2452 xfs_bmbt_set_startblock(ep, del_endblock);
2453 xfs_bmap_trace_post_update(fname, "2", ip, idx, whichfork);
2454 if (!cur) {
2455 flags |= XFS_ILOG_FEXT(whichfork);
2456 break;
2457 }
0e266570 2458 if ((error = xfs_bmbt_update(cur, del_endoff, del_endblock,
2bd0ea18 2459 got.br_blockcount - del->br_blockcount,
0e266570 2460 got.br_state)))
2bd0ea18
NS
2461 goto done;
2462 break;
2463
2464 case 1:
2465 /*
2466 * Deleting the last part of the extent.
2467 */
2468 temp = got.br_blockcount - del->br_blockcount;
2469 xfs_bmap_trace_pre_update(fname, "1", ip, idx, whichfork);
2470 xfs_bmbt_set_blockcount(ep, temp);
2471 ifp->if_lastex = idx;
2472 if (delay) {
2473 temp = XFS_FILBLKS_MIN(xfs_bmap_worst_indlen(ip, temp),
2474 da_old);
2475 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
2476 xfs_bmap_trace_post_update(fname, "1", ip, idx,
2477 whichfork);
2478 da_new = temp;
2479 break;
2480 }
2481 xfs_bmap_trace_post_update(fname, "1", ip, idx, whichfork);
2482 if (!cur) {
2483 flags |= XFS_ILOG_FEXT(whichfork);
2484 break;
2485 }
0e266570 2486 if ((error = xfs_bmbt_update(cur, got.br_startoff,
2bd0ea18
NS
2487 got.br_startblock,
2488 got.br_blockcount - del->br_blockcount,
0e266570 2489 got.br_state)))
2bd0ea18
NS
2490 goto done;
2491 break;
5000d01d 2492
2bd0ea18
NS
2493 case 0:
2494 /*
2495 * Deleting the middle of the extent.
2496 */
2497 temp = del->br_startoff - got.br_startoff;
2498 xfs_bmap_trace_pre_update(fname, "0", ip, idx, whichfork);
2499 xfs_bmbt_set_blockcount(ep, temp);
2500 new.br_startoff = del_endoff;
2501 temp2 = got_endoff - del_endoff;
2502 new.br_blockcount = temp2;
2503 new.br_state = got.br_state;
2504 if (!delay) {
2505 new.br_startblock = del_endblock;
2506 flags |= XFS_ILOG_CORE;
2507 if (cur) {
0e266570 2508 if ((error = xfs_bmbt_update(cur,
2bd0ea18
NS
2509 got.br_startoff,
2510 got.br_startblock, temp,
0e266570 2511 got.br_state)))
2bd0ea18 2512 goto done;
0e266570 2513 if ((error = xfs_bmbt_increment(cur, 0, &i)))
2bd0ea18
NS
2514 goto done;
2515 cur->bc_rec.b = new;
2516 error = xfs_bmbt_insert(cur, &i);
2517 if (error && error != ENOSPC)
2518 goto done;
2519 /*
2520 * If get no-space back from btree insert,
2521 * it tried a split, and we have a zero
2522 * block reservation.
2523 * Fix up our state and return the error.
2524 */
2525 if (error == ENOSPC) {
5000d01d 2526 /*
2bd0ea18
NS
2527 * Reset the cursor, don't trust
2528 * it after any insert operation.
2529 */
0e266570 2530 if ((error = xfs_bmbt_lookup_eq(cur,
2bd0ea18
NS
2531 got.br_startoff,
2532 got.br_startblock,
0e266570 2533 temp, &i)))
2bd0ea18
NS
2534 goto done;
2535 ASSERT(i == 1);
2536 /*
2537 * Update the btree record back
2538 * to the original value.
2539 */
0e266570 2540 if ((error = xfs_bmbt_update(cur,
2bd0ea18
NS
2541 got.br_startoff,
2542 got.br_startblock,
2543 got.br_blockcount,
0e266570 2544 got.br_state)))
2bd0ea18
NS
2545 goto done;
2546 /*
2547 * Reset the extent record back
2548 * to the original value.
2549 */
2550 xfs_bmbt_set_blockcount(ep,
2551 got.br_blockcount);
2552 flags = 0;
2553 error = XFS_ERROR(ENOSPC);
2554 goto done;
2555 }
2556 ASSERT(i == 1);
2557 } else
2558 flags |= XFS_ILOG_FEXT(whichfork);
2559 XFS_IFORK_NEXT_SET(ip, whichfork,
2560 XFS_IFORK_NEXTENTS(ip, whichfork) + 1);
2561 } else {
2562 ASSERT(whichfork == XFS_DATA_FORK);
2563 temp = xfs_bmap_worst_indlen(ip, temp);
2564 xfs_bmbt_set_startblock(ep, NULLSTARTBLOCK((int)temp));
2565 temp2 = xfs_bmap_worst_indlen(ip, temp2);
2566 new.br_startblock = NULLSTARTBLOCK((int)temp2);
2567 da_new = temp + temp2;
2568 while (da_new > da_old) {
2569 if (temp) {
2570 temp--;
2571 da_new--;
2572 xfs_bmbt_set_startblock(ep,
2573 NULLSTARTBLOCK((int)temp));
2574 }
2575 if (da_new == da_old)
2576 break;
2577 if (temp2) {
2578 temp2--;
2579 da_new--;
5000d01d 2580 new.br_startblock =
2bd0ea18
NS
2581 NULLSTARTBLOCK((int)temp2);
2582 }
2583 }
2584 }
2585 xfs_bmap_trace_post_update(fname, "0", ip, idx, whichfork);
2586 xfs_bmap_trace_insert(fname, "0", ip, idx + 1, 1, &new, NULL,
2587 whichfork);
2588 xfs_bmap_insert_exlist(ip, idx + 1, 1, &new, whichfork);
2589 ifp->if_lastex = idx + 1;
2590 break;
2591 }
2592 /*
2593 * If we need to, add to list of extents to delete.
2594 */
2595 if (do_fx)
2596 xfs_bmap_add_free(del->br_startblock, del->br_blockcount, flist,
2597 mp);
2598 /*
2599 * Adjust inode # blocks in the file.
2600 */
2601 if (nblks)
2602 ip->i_d.di_nblocks -= nblks;
2603 /*
2604 * Adjust quota data.
2605 */
2606 if (qfield)
eae766ca
NS
2607 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, qfield, (long)-nblks);
2608
2bd0ea18
NS
2609 /*
2610 * Account for change in delayed indirect blocks.
2611 * Nothing to do for disk quota accounting here.
2612 */
2613 ASSERT(da_old >= da_new);
5000d01d 2614 if (da_old > da_new)
2bd0ea18
NS
2615 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS, (int)(da_old - da_new),
2616 rsvd);
2617done:
2618 *logflagsp = flags;
2619 return error;
2620}
2621
2622/*
2623 * Remove the entry "free" from the free item list. Prev points to the
2624 * previous entry, unless "free" is the head of the list.
2625 */
2626STATIC void
2627xfs_bmap_del_free(
dfc130f3 2628 xfs_bmap_free_t *flist, /* free item list header */
2bd0ea18
NS
2629 xfs_bmap_free_item_t *prev, /* previous item on list, if any */
2630 xfs_bmap_free_item_t *free) /* list item to be freed */
2631{
2632 if (prev)
2633 prev->xbfi_next = free->xbfi_next;
2634 else
2635 flist->xbf_first = free->xbfi_next;
2636 flist->xbf_count--;
2637 kmem_zone_free(xfs_bmap_free_item_zone, free);
2638}
2639
2640/*
2641 * Remove count entries from the extents array for inode "ip", starting
2642 * at index "idx". Copies the remaining items down over the deleted ones,
2643 * and gives back the excess memory.
2644 */
2645STATIC void
2646xfs_bmap_delete_exlist(
2647 xfs_inode_t *ip, /* incore inode pointer */
2648 xfs_extnum_t idx, /* starting delete index */
2649 xfs_extnum_t count, /* count of items to delete */
2650 int whichfork) /* data or attr fork */
2651{
2652 xfs_bmbt_rec_t *base; /* base of extent list */
2653 xfs_ifork_t *ifp; /* inode fork pointer */
2654 xfs_extnum_t nextents; /* number of extents in list after */
2655
2656 ifp = XFS_IFORK_PTR(ip, whichfork);
2657 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2658 base = ifp->if_u1.if_extents;
2659 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t) - count;
32181a02 2660 memmove(&base[idx], &base[idx + count],
2bd0ea18
NS
2661 (nextents - idx) * sizeof(*base));
2662 xfs_iext_realloc(ip, -count, whichfork);
2663}
2664
2665/*
2666 * Convert an extents-format file into a btree-format file.
2667 * The new file will have a root block (in the inode) and a single child block.
2668 */
2669STATIC int /* error */
2670xfs_bmap_extents_to_btree(
2671 xfs_trans_t *tp, /* transaction pointer */
2672 xfs_inode_t *ip, /* incore inode pointer */
2673 xfs_fsblock_t *firstblock, /* first-block-allocated */
2674 xfs_bmap_free_t *flist, /* blocks freed in xaction */
2675 xfs_btree_cur_t **curp, /* cursor returned to caller */
2676 int wasdel, /* converting a delayed alloc */
2677 int *logflagsp, /* inode logging flags */
2678 int whichfork) /* data or attr fork */
2679{
2680 xfs_bmbt_block_t *ablock; /* allocated (child) bt block */
7a3bffe4 2681 xfs_buf_t *abp; /* buffer for ablock */
2bd0ea18
NS
2682 xfs_alloc_arg_t args; /* allocation arguments */
2683 xfs_bmbt_rec_t *arp; /* child record pointer */
2684 xfs_bmbt_block_t *block; /* btree root block */
2685 xfs_btree_cur_t *cur; /* bmap btree cursor */
2686 xfs_bmbt_rec_t *ep; /* extent list pointer */
2687 int error; /* error return value */
f9e56f43 2688 xfs_extnum_t i, cnt; /* extent list index */
2bd0ea18
NS
2689 xfs_ifork_t *ifp; /* inode fork pointer */
2690 xfs_bmbt_key_t *kp; /* root block key pointer */
2691 xfs_mount_t *mp; /* mount structure */
2692 xfs_extnum_t nextents; /* extent list size */
2693 xfs_bmbt_ptr_t *pp; /* root block address pointer */
2694
2695 ifp = XFS_IFORK_PTR(ip, whichfork);
2696 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS);
2697 ASSERT(ifp->if_ext_max ==
2698 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
2699 /*
2700 * Make space in the inode incore.
2701 */
2702 xfs_iroot_realloc(ip, 1, whichfork);
2703 ifp->if_flags |= XFS_IFBROOT;
2704 /*
2705 * Fill in the root.
2706 */
2707 block = ifp->if_broot;
2708 INT_SET(block->bb_magic, ARCH_CONVERT, XFS_BMAP_MAGIC);
2709 INT_SET(block->bb_level, ARCH_CONVERT, 1);
2710 INT_SET(block->bb_numrecs, ARCH_CONVERT, 1);
2711 INT_SET(block->bb_leftsib, ARCH_CONVERT, NULLDFSBNO);
5000d01d 2712 INT_SET(block->bb_rightsib, ARCH_CONVERT, NULLDFSBNO);
2bd0ea18
NS
2713 /*
2714 * Need a cursor. Can't allocate until bb_level is filled in.
2715 */
2716 mp = ip->i_mount;
2717 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
2718 whichfork);
2719 cur->bc_private.b.firstblock = *firstblock;
2720 cur->bc_private.b.flist = flist;
2721 cur->bc_private.b.flags = wasdel ? XFS_BTCUR_BPRV_WASDEL : 0;
2722 /*
2723 * Convert to a btree with two levels, one record in root.
2724 */
2725 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_BTREE);
2726 args.tp = tp;
2727 args.mp = mp;
2728 if (*firstblock == NULLFSBLOCK) {
2729 args.type = XFS_ALLOCTYPE_START_BNO;
2730 args.fsbno = XFS_INO_TO_FSB(mp, ip->i_ino);
2731 } else if (flist->xbf_low) {
2732 args.type = XFS_ALLOCTYPE_START_BNO;
2733 args.fsbno = *firstblock;
2734 } else {
2735 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2736 args.fsbno = *firstblock;
2737 }
2738 args.minlen = args.maxlen = args.prod = 1;
2739 args.total = args.minleft = args.alignment = args.mod = args.isfl =
2740 args.minalignslop = 0;
2741 args.wasdel = wasdel;
2742 *logflagsp = 0;
0e266570 2743 if ((error = xfs_alloc_vextent(&args))) {
2bd0ea18
NS
2744 xfs_iroot_realloc(ip, -1, whichfork);
2745 xfs_btree_del_cursor(cur, XFS_BTREE_ERROR);
2746 return error;
2747 }
2748 /*
2749 * Allocation can't fail, the space was reserved.
2750 */
2751 ASSERT(args.fsbno != NULLFSBLOCK);
2752 ASSERT(*firstblock == NULLFSBLOCK ||
2753 args.agno == XFS_FSB_TO_AGNO(mp, *firstblock) ||
2754 (flist->xbf_low &&
5000d01d 2755 args.agno > XFS_FSB_TO_AGNO(mp, *firstblock)));
2bd0ea18
NS
2756 *firstblock = cur->bc_private.b.firstblock = args.fsbno;
2757 cur->bc_private.b.allocated++;
2758 ip->i_d.di_nblocks++;
eae766ca 2759 XFS_TRANS_MOD_DQUOT_BYINO(mp, tp, ip, XFS_TRANS_DQ_BCOUNT, 1L);
2bd0ea18
NS
2760 abp = xfs_btree_get_bufl(mp, tp, args.fsbno, 0);
2761 /*
2762 * Fill in the child block.
2763 */
2764 ablock = XFS_BUF_TO_BMBT_BLOCK(abp);
2765 INT_SET(ablock->bb_magic, ARCH_CONVERT, XFS_BMAP_MAGIC);
46eca962 2766 ablock->bb_level = 0;
2bd0ea18 2767 INT_SET(ablock->bb_leftsib, ARCH_CONVERT, NULLDFSBNO);
5000d01d 2768 INT_SET(ablock->bb_rightsib, ARCH_CONVERT, NULLDFSBNO);
2bd0ea18
NS
2769 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
2770 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
f9e56f43 2771 for (ep = ifp->if_u1.if_extents, cnt = i = 0; i < nextents; i++, ep++) {
2bd0ea18 2772 if (!ISNULLSTARTBLOCK(xfs_bmbt_get_startblock(ep))) {
f9e56f43
NS
2773 arp->l0 = INT_GET(ep->l0, ARCH_CONVERT);
2774 arp->l1 = INT_GET(ep->l1, ARCH_CONVERT);
2775 arp++; cnt++;
2bd0ea18
NS
2776 }
2777 }
f9e56f43 2778 INT_SET(ablock->bb_numrecs, ARCH_CONVERT, cnt);
2bd0ea18
NS
2779 ASSERT(INT_GET(ablock->bb_numrecs, ARCH_CONVERT) == XFS_IFORK_NEXTENTS(ip, whichfork));
2780 /*
2781 * Fill in the root key and pointer.
2782 */
2783 kp = XFS_BMAP_KEY_IADDR(block, 1, cur);
2784 arp = XFS_BMAP_REC_IADDR(ablock, 1, cur);
f9e56f43 2785 INT_SET(kp->br_startoff, ARCH_CONVERT, xfs_bmbt_disk_get_startoff(arp));
2bd0ea18
NS
2786 pp = XFS_BMAP_PTR_IADDR(block, 1, cur);
2787 INT_SET(*pp, ARCH_CONVERT, args.fsbno);
2788 /*
5000d01d 2789 * Do all this logging at the end so that
2bd0ea18
NS
2790 * the root is at the right level.
2791 */
2792 xfs_bmbt_log_block(cur, abp, XFS_BB_ALL_BITS);
2793 xfs_bmbt_log_recs(cur, abp, 1, INT_GET(ablock->bb_numrecs, ARCH_CONVERT));
2794 ASSERT(*curp == NULL);
2795 *curp = cur;
2796 *logflagsp = XFS_ILOG_CORE | XFS_ILOG_FBROOT(whichfork);
2797 return 0;
2798}
2799
2800/*
2801 * Insert new item(s) in the extent list for inode "ip".
2802 * Count new items are inserted at offset idx.
2803 */
2804STATIC void
2805xfs_bmap_insert_exlist(
2806 xfs_inode_t *ip, /* incore inode pointer */
2807 xfs_extnum_t idx, /* starting index of new items */
2808 xfs_extnum_t count, /* number of inserted items */
dfc130f3 2809 xfs_bmbt_irec_t *new, /* items to insert */
2bd0ea18
NS
2810 int whichfork) /* data or attr fork */
2811{
2812 xfs_bmbt_rec_t *base; /* extent list base */
2813 xfs_ifork_t *ifp; /* inode fork pointer */
2814 xfs_extnum_t nextents; /* extent list size */
2815 xfs_extnum_t to; /* extent list index */
2816
2817 ifp = XFS_IFORK_PTR(ip, whichfork);
2818 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
2819 xfs_iext_realloc(ip, count, whichfork);
2820 base = ifp->if_u1.if_extents;
2821 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
32181a02 2822 memmove(&base[idx + count], &base[idx],
2bd0ea18
NS
2823 (nextents - (idx + count)) * sizeof(*base));
2824 for (to = idx; to < idx + count; to++, new++)
2825 xfs_bmbt_set_all(&base[to], new);
2826}
2827
2828/*
2829 * Convert a local file to an extents file.
2830 * This code is out of bounds for data forks of regular files,
2831 * since the file data needs to get logged so things will stay consistent.
2832 * (The bmap-level manipulations are ok, though).
2833 */
2834STATIC int /* error */
2835xfs_bmap_local_to_extents(
2836 xfs_trans_t *tp, /* transaction pointer */
2837 xfs_inode_t *ip, /* incore inode pointer */
2838 xfs_fsblock_t *firstblock, /* first block allocated in xaction */
2839 xfs_extlen_t total, /* total blocks needed by transaction */
2840 int *logflagsp, /* inode logging flags */
2841 int whichfork) /* data or attr fork */
2842{
2843 int error; /* error return value */
2844 int flags; /* logging flags returned */
2845#ifdef XFS_BMAP_TRACE
2846 static char fname[] = "xfs_bmap_local_to_extents";
2847#endif
2848 xfs_ifork_t *ifp; /* inode fork pointer */
2849
2850 /*
2851 * We don't want to deal with the case of keeping inode data inline yet.
05bba5b7 2852 * So sending the data fork of a regular inode is invalid.
2bd0ea18 2853 */
322f2a29 2854 ASSERT(!((ip->i_d.di_mode & S_IFMT) == S_IFREG &&
2bd0ea18
NS
2855 whichfork == XFS_DATA_FORK));
2856 ifp = XFS_IFORK_PTR(ip, whichfork);
2857 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
2858 flags = 0;
2859 error = 0;
2860 if (ifp->if_bytes) {
dfc130f3 2861 xfs_alloc_arg_t args; /* allocation arguments */
7a3bffe4 2862 xfs_buf_t *bp; /* buffer for extent list block */
2bd0ea18
NS
2863 xfs_bmbt_rec_t *ep; /* extent list pointer */
2864
2865 args.tp = tp;
2866 args.mp = ip->i_mount;
2867 ASSERT(ifp->if_flags & XFS_IFINLINE);
2868 /*
2869 * Allocate a block. We know we need only one, since the
2870 * file currently fits in an inode.
2871 */
2872 if (*firstblock == NULLFSBLOCK) {
2873 args.fsbno = XFS_INO_TO_FSB(args.mp, ip->i_ino);
2874 args.type = XFS_ALLOCTYPE_START_BNO;
2875 } else {
2876 args.fsbno = *firstblock;
2877 args.type = XFS_ALLOCTYPE_NEAR_BNO;
2878 }
2879 args.total = total;
2880 args.mod = args.minleft = args.alignment = args.wasdel =
2881 args.isfl = args.minalignslop = 0;
2882 args.minlen = args.maxlen = args.prod = 1;
0e266570 2883 if ((error = xfs_alloc_vextent(&args)))
2bd0ea18 2884 goto done;
5000d01d 2885 /*
2bd0ea18
NS
2886 * Can't fail, the space was reserved.
2887 */
2888 ASSERT(args.fsbno != NULLFSBLOCK);
2889 ASSERT(args.len == 1);
2890 *firstblock = args.fsbno;
2891 bp = xfs_btree_get_bufl(args.mp, tp, args.fsbno, 0);
32181a02 2892 memcpy((char *)XFS_BUF_PTR(bp), ifp->if_u1.if_data,
2bd0ea18
NS
2893 ifp->if_bytes);
2894 xfs_trans_log_buf(tp, bp, 0, ifp->if_bytes - 1);
2895 xfs_idata_realloc(ip, -ifp->if_bytes, whichfork);
2896 xfs_iext_realloc(ip, 1, whichfork);
2897 ep = ifp->if_u1.if_extents;
2898 xfs_bmbt_set_allf(ep, 0, args.fsbno, 1, XFS_EXT_NORM);
2899 xfs_bmap_trace_post_update(fname, "new", ip, 0, whichfork);
2900 XFS_IFORK_NEXT_SET(ip, whichfork, 1);
2901 ip->i_d.di_nblocks = 1;
eae766ca
NS
2902 XFS_TRANS_MOD_DQUOT_BYINO(args.mp, tp, ip,
2903 XFS_TRANS_DQ_BCOUNT, 1L);
2bd0ea18
NS
2904 flags |= XFS_ILOG_FEXT(whichfork);
2905 } else
2906 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) == 0);
2907 ifp->if_flags &= ~XFS_IFINLINE;
2908 ifp->if_flags |= XFS_IFEXTENTS;
2909 XFS_IFORK_FMT_SET(ip, whichfork, XFS_DINODE_FMT_EXTENTS);
2910 flags |= XFS_ILOG_CORE;
2911done:
2912 *logflagsp = flags;
2913 return error;
2914}
2915
2916xfs_bmbt_rec_t * /* pointer to found extent entry */
2917xfs_bmap_do_search_extents(
2918 xfs_bmbt_rec_t *base, /* base of extent list */
2919 xfs_extnum_t lastx, /* last extent index used */
2920 xfs_extnum_t nextents, /* extent list size */
2921 xfs_fileoff_t bno, /* block number searched for */
2922 int *eofp, /* out: end of file found */
2923 xfs_extnum_t *lastxp, /* out: last extent index */
dfc130f3
RC
2924 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
2925 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
2bd0ea18
NS
2926{
2927 xfs_bmbt_rec_t *ep; /* extent list entry pointer */
dfc130f3 2928 xfs_bmbt_irec_t got; /* extent list entry, decoded */
2bd0ea18
NS
2929 int high; /* high index of binary search */
2930 int low; /* low index of binary search */
2931
062998e3
NS
2932 /*
2933 * Initialize the extent entry structure to catch access to
2934 * uninitialized br_startblock field.
2935 */
2936 got.br_startoff = 0xffa5a5a5a5a5a5a5LL;
2937 got.br_blockcount = 0xa55a5a5a5a5a5a5aLL;
2938 got.br_state = XFS_EXT_INVALID;
2939
2940#if XFS_BIG_BLKNOS
2941 got.br_startblock = 0xffffa5a5a5a5a5a5LL;
2942#else
2943 got.br_startblock = 0xffffa5a5;
2944#endif
2945
2bd0ea18
NS
2946 if (lastx != NULLEXTNUM && lastx < nextents)
2947 ep = base + lastx;
2948 else
2949 ep = NULL;
2950 prevp->br_startoff = NULLFILEOFF;
2951 if (ep && bno >= (got.br_startoff = xfs_bmbt_get_startoff(ep)) &&
2952 bno < got.br_startoff +
2953 (got.br_blockcount = xfs_bmbt_get_blockcount(ep)))
2954 *eofp = 0;
2955 else if (ep && lastx < nextents - 1 &&
2956 bno >= (got.br_startoff = xfs_bmbt_get_startoff(ep + 1)) &&
2957 bno < got.br_startoff +
2958 (got.br_blockcount = xfs_bmbt_get_blockcount(ep + 1))) {
2959 lastx++;
2960 ep++;
2961 *eofp = 0;
2962 } else if (nextents == 0)
2963 *eofp = 1;
2964 else if (bno == 0 &&
2965 (got.br_startoff = xfs_bmbt_get_startoff(base)) == 0) {
2966 ep = base;
2967 lastx = 0;
2968 got.br_blockcount = xfs_bmbt_get_blockcount(ep);
2969 *eofp = 0;
2970 } else {
2971 /* binary search the extents array */
2972 low = 0;
2973 high = nextents - 1;
2974 while (low <= high) {
32a82561 2975 XFS_STATS_INC(xs_cmp_exlist);
2bd0ea18
NS
2976 lastx = (low + high) >> 1;
2977 ep = base + lastx;
2978 got.br_startoff = xfs_bmbt_get_startoff(ep);
2979 got.br_blockcount = xfs_bmbt_get_blockcount(ep);
2980 if (bno < got.br_startoff)
2981 high = lastx - 1;
2982 else if (bno >= got.br_startoff + got.br_blockcount)
2983 low = lastx + 1;
2984 else {
2985 got.br_startblock = xfs_bmbt_get_startblock(ep);
2986 got.br_state = xfs_bmbt_get_state(ep);
2987 *eofp = 0;
2988 *lastxp = lastx;
2989 *gotp = got;
2990 return ep;
2991 }
2992 }
2993 if (bno >= got.br_startoff + got.br_blockcount) {
2994 lastx++;
2995 if (lastx == nextents) {
2996 *eofp = 1;
2997 got.br_startblock = xfs_bmbt_get_startblock(ep);
2998 got.br_state = xfs_bmbt_get_state(ep);
2999 *prevp = got;
3000 ep = NULL;
3001 } else {
3002 *eofp = 0;
3003 xfs_bmbt_get_all(ep, prevp);
3004 ep++;
3005 got.br_startoff = xfs_bmbt_get_startoff(ep);
3006 got.br_blockcount = xfs_bmbt_get_blockcount(ep);
3007 }
3008 } else {
3009 *eofp = 0;
3010 if (ep > base)
3011 xfs_bmbt_get_all(ep - 1, prevp);
3012 }
3013 }
3014 if (ep) {
3015 got.br_startblock = xfs_bmbt_get_startblock(ep);
3016 got.br_state = xfs_bmbt_get_state(ep);
3017 }
3018 *lastxp = lastx;
3019 *gotp = got;
3020 return ep;
3021}
3022
3023/*
3024 * Search the extents list for the inode, for the extent containing bno.
3025 * If bno lies in a hole, point to the next entry. If bno lies past eof,
3026 * *eofp will be set, and *prevp will contain the last entry (null if none).
3027 * Else, *lastxp will be set to the index of the found
3028 * entry; *gotp will contain the entry.
3029 */
dfc130f3 3030STATIC xfs_bmbt_rec_t * /* pointer to found extent entry */
2bd0ea18 3031xfs_bmap_search_extents(
dfc130f3
RC
3032 xfs_inode_t *ip, /* incore inode pointer */
3033 xfs_fileoff_t bno, /* block number searched for */
3034 int whichfork, /* data or attr fork */
3035 int *eofp, /* out: end of file found */
3036 xfs_extnum_t *lastxp, /* out: last extent index */
3037 xfs_bmbt_irec_t *gotp, /* out: extent entry found */
3038 xfs_bmbt_irec_t *prevp) /* out: previous extent entry found */
5000d01d 3039{
2bd0ea18 3040 xfs_ifork_t *ifp; /* inode fork pointer */
dfc130f3
RC
3041 xfs_bmbt_rec_t *base; /* base of extent list */
3042 xfs_extnum_t lastx; /* last extent index used */
3043 xfs_extnum_t nextents; /* extent list size */
062998e3
NS
3044 xfs_bmbt_rec_t *ep; /* extent list entry pointer */
3045 int rt; /* realtime flag */
2bd0ea18 3046
32a82561 3047 XFS_STATS_INC(xs_look_exlist);
2bd0ea18
NS
3048 ifp = XFS_IFORK_PTR(ip, whichfork);
3049 lastx = ifp->if_lastex;
3050 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3051 base = &ifp->if_u1.if_extents[0];
3052
062998e3 3053 ep = xfs_bmap_do_search_extents(base, lastx, nextents, bno, eofp,
2bd0ea18 3054 lastxp, gotp, prevp);
062998e3
NS
3055 rt = ip->i_d.di_flags & XFS_DIFLAG_REALTIME;
3056 if(!rt && !gotp->br_startblock && (*lastxp != NULLEXTNUM)) {
3057 cmn_err(CE_PANIC,"Access to block zero: fs: <%s> inode: %lld "
3058 "start_block : %llx start_off : %llx blkcnt : %llx "
3059 "extent-state : %x \n",
3060 (ip->i_mount)->m_fsname,(long long)ip->i_ino,
3061 gotp->br_startblock, gotp->br_startoff,
3062 gotp->br_blockcount,gotp->br_state);
3063 }
3064 return ep;
2bd0ea18
NS
3065}
3066
3067/*
3068 * Compute the worst-case number of indirect blocks that will be used
3069 * for ip's delayed extent of length "len".
3070 */
3071STATIC xfs_filblks_t
3072xfs_bmap_worst_indlen(
3073 xfs_inode_t *ip, /* incore inode pointer */
3074 xfs_filblks_t len) /* delayed extent length */
3075{
3076 int level; /* btree level number */
3077 int maxrecs; /* maximum record count at this level */
3078 xfs_mount_t *mp; /* mount structure */
3079 xfs_filblks_t rval; /* return value */
3080
3081 mp = ip->i_mount;
3082 maxrecs = mp->m_bmap_dmxr[0];
3083 for (level = 0, rval = 0;
3084 level < XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK);
3085 level++) {
3086 len += maxrecs - 1;
3087 do_div(len, maxrecs);
3088 rval += len;
3089 if (len == 1)
3090 return rval + XFS_BM_MAXLEVELS(mp, XFS_DATA_FORK) -
3091 level - 1;
3092 if (level == 0)
3093 maxrecs = mp->m_bmap_dmxr[1];
3094 }
3095 return rval;
3096}
3097
3098/*
3099 * Add the extent to the list of extents to be free at transaction end.
3100 * The list is maintained sorted (by block number).
3101 */
3102/* ARGSUSED */
3103void
3104xfs_bmap_add_free(
3105 xfs_fsblock_t bno, /* fs block number of extent */
3106 xfs_filblks_t len, /* length of extent */
3107 xfs_bmap_free_t *flist, /* list of extents */
3108 xfs_mount_t *mp) /* mount point structure */
3109{
3110 xfs_bmap_free_item_t *cur; /* current (next) element */
3111 xfs_bmap_free_item_t *new; /* new element */
3112 xfs_bmap_free_item_t *prev; /* previous element */
3113#ifdef DEBUG
3114 xfs_agnumber_t agno;
3115 xfs_agblock_t agbno;
3116
3117 ASSERT(bno != NULLFSBLOCK);
3118 ASSERT(len > 0);
3119 ASSERT(len <= MAXEXTLEN);
3120 ASSERT(!ISNULLSTARTBLOCK(bno));
3121 agno = XFS_FSB_TO_AGNO(mp, bno);
3122 agbno = XFS_FSB_TO_AGBNO(mp, bno);
3123 ASSERT(agno < mp->m_sb.sb_agcount);
3124 ASSERT(agbno < mp->m_sb.sb_agblocks);
3125 ASSERT(len < mp->m_sb.sb_agblocks);
3126 ASSERT(agbno + len <= mp->m_sb.sb_agblocks);
3127#endif
3128 ASSERT(xfs_bmap_free_item_zone != NULL);
3129 new = kmem_zone_alloc(xfs_bmap_free_item_zone, KM_SLEEP);
3130 new->xbfi_startblock = bno;
3131 new->xbfi_blockcount = (xfs_extlen_t)len;
3132 for (prev = NULL, cur = flist->xbf_first;
3133 cur != NULL;
3134 prev = cur, cur = cur->xbfi_next) {
3135 if (cur->xbfi_startblock >= bno)
3136 break;
3137 }
3138 if (prev)
3139 prev->xbfi_next = new;
3140 else
3141 flist->xbf_first = new;
3142 new->xbfi_next = cur;
3143 flist->xbf_count++;
3144}
3145
5000d01d 3146/*
2bd0ea18 3147 * Compute and fill in the value of the maximum depth of a bmap btree
dfc130f3 3148 * in this filesystem. Done once, during mount.
2bd0ea18
NS
3149 */
3150void
3151xfs_bmap_compute_maxlevels(
3152 xfs_mount_t *mp, /* file system mount structure */
3153 int whichfork) /* data or attr fork */
3154{
3155 int level; /* btree level */
3156 uint maxblocks; /* max blocks at this level */
3157 uint maxleafents; /* max leaf entries possible */
3158 int maxrootrecs; /* max records in root block */
3159 int minleafrecs; /* min records in leaf block */
3160 int minnoderecs; /* min records in node block */
3161 int sz; /* root block size */
3162
3163 /*
3164 * The maximum number of extents in a file, hence the maximum
3165 * number of leaf entries, is controlled by the type of di_nextents
3166 * (a signed 32-bit number, xfs_extnum_t), or by di_anextents
3167 * (a signed 16-bit number, xfs_aextnum_t).
3168 */
3169 maxleafents = (whichfork == XFS_DATA_FORK) ? MAXEXTNUM : MAXAEXTNUM;
3170 minleafrecs = mp->m_bmap_dmnr[0];
3171 minnoderecs = mp->m_bmap_dmnr[1];
3172 sz = (whichfork == XFS_DATA_FORK) ?
3173 mp->m_attroffset :
3174 mp->m_sb.sb_inodesize - mp->m_attroffset;
3175 maxrootrecs = (int)XFS_BTREE_BLOCK_MAXRECS(sz, xfs_bmdr, 0);
3176 maxblocks = (maxleafents + minleafrecs - 1) / minleafrecs;
3177 for (level = 1; maxblocks > 1; level++) {
3178 if (maxblocks <= maxrootrecs)
3179 maxblocks = 1;
3180 else
3181 maxblocks = (maxblocks + minnoderecs - 1) / minnoderecs;
3182 }
3183 mp->m_bm_maxlevels[whichfork] = level;
3184}
3185
3186/*
3187 * Returns the file-relative block number of the first unused block(s)
3188 * in the file with at least "len" logically contiguous blocks free.
3189 * This is the lowest-address hole if the file has holes, else the first block
3190 * past the end of file.
3191 * Return 0 if the file is currently local (in-inode).
3192 */
3193int /* error */
3194xfs_bmap_first_unused(
3195 xfs_trans_t *tp, /* transaction pointer */
3196 xfs_inode_t *ip, /* incore inode */
3197 xfs_extlen_t len, /* size of hole to find */
3198 xfs_fileoff_t *first_unused, /* unused block */
3199 int whichfork) /* data or attr fork */
3200{
3201 xfs_bmbt_rec_t *base; /* base of extent array */
3202 xfs_bmbt_rec_t *ep; /* pointer to an extent entry */
3203 int error; /* error return value */
3204 xfs_ifork_t *ifp; /* inode fork pointer */
3205 xfs_fileoff_t lastaddr; /* last block number seen */
3206 xfs_fileoff_t lowest; /* lowest useful block */
3207 xfs_fileoff_t max; /* starting useful block */
3208 xfs_fileoff_t off; /* offset for this block */
3209 xfs_extnum_t nextents; /* number of extent entries */
3210
3211 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE ||
3212 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS ||
3213 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL);
3214 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3215 *first_unused = 0;
3216 return 0;
3217 }
3218 ifp = XFS_IFORK_PTR(ip, whichfork);
3219 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3220 (error = xfs_iread_extents(tp, ip, whichfork)))
3221 return error;
3222 lowest = *first_unused;
3223 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3224 base = &ifp->if_u1.if_extents[0];
3225 for (lastaddr = 0, max = lowest, ep = base;
3226 ep < &base[nextents];
3227 ep++) {
3228 off = xfs_bmbt_get_startoff(ep);
3229 /*
3230 * See if the hole before this extent will work.
3231 */
3232 if (off >= lowest + len && off - max >= len) {
3233 *first_unused = max;
3234 return 0;
3235 }
3236 lastaddr = off + xfs_bmbt_get_blockcount(ep);
3237 max = XFS_FILEOFF_MAX(lastaddr, lowest);
3238 }
3239 *first_unused = max;
3240 return 0;
3241}
3242
3243/*
3244 * Returns the file-relative block number of the last block + 1 before
3245 * last_block (input value) in the file.
3246 * This is not based on i_size, it is based on the extent list.
3247 * Returns 0 for local files, as they do not have an extent list.
3248 */
3249int /* error */
3250xfs_bmap_last_before(
3251 xfs_trans_t *tp, /* transaction pointer */
3252 xfs_inode_t *ip, /* incore inode */
3253 xfs_fileoff_t *last_block, /* last block */
3254 int whichfork) /* data or attr fork */
3255{
3256 xfs_fileoff_t bno; /* input file offset */
3257 int eof; /* hit end of file */
3258 xfs_bmbt_rec_t *ep; /* pointer to last extent */
3259 int error; /* error return value */
dfc130f3 3260 xfs_bmbt_irec_t got; /* current extent value */
2bd0ea18
NS
3261 xfs_ifork_t *ifp; /* inode fork pointer */
3262 xfs_extnum_t lastx; /* last extent used */
dfc130f3 3263 xfs_bmbt_irec_t prev; /* previous extent value */
2bd0ea18
NS
3264
3265 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3266 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3267 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3268 return XFS_ERROR(EIO);
3269 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3270 *last_block = 0;
3271 return 0;
3272 }
3273 ifp = XFS_IFORK_PTR(ip, whichfork);
3274 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3275 (error = xfs_iread_extents(tp, ip, whichfork)))
3276 return error;
3277 bno = *last_block - 1;
3278 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3279 &prev);
3280 if (eof || xfs_bmbt_get_startoff(ep) > bno) {
3281 if (prev.br_startoff == NULLFILEOFF)
3282 *last_block = 0;
3283 else
3284 *last_block = prev.br_startoff + prev.br_blockcount;
3285 }
3286 /*
3287 * Otherwise *last_block is already the right answer.
3288 */
3289 return 0;
3290}
3291
3292/*
3293 * Returns the file-relative block number of the first block past eof in
3294 * the file. This is not based on i_size, it is based on the extent list.
3295 * Returns 0 for local files, as they do not have an extent list.
3296 */
3297int /* error */
3298xfs_bmap_last_offset(
3299 xfs_trans_t *tp, /* transaction pointer */
3300 xfs_inode_t *ip, /* incore inode */
3301 xfs_fileoff_t *last_block, /* last block */
3302 int whichfork) /* data or attr fork */
3303{
3304 xfs_bmbt_rec_t *base; /* base of extent array */
3305 xfs_bmbt_rec_t *ep; /* pointer to last extent */
3306 int error; /* error return value */
3307 xfs_ifork_t *ifp; /* inode fork pointer */
3308 xfs_extnum_t nextents; /* number of extent entries */
3309
3310 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3311 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3312 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL)
3313 return XFS_ERROR(EIO);
3314 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3315 *last_block = 0;
3316 return 0;
3317 }
3318 ifp = XFS_IFORK_PTR(ip, whichfork);
3319 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3320 (error = xfs_iread_extents(tp, ip, whichfork)))
3321 return error;
3322 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3323 if (!nextents) {
3324 *last_block = 0;
3325 return 0;
3326 }
3327 base = &ifp->if_u1.if_extents[0];
3328 ASSERT(base != NULL);
3329 ep = &base[nextents - 1];
3330 *last_block = xfs_bmbt_get_startoff(ep) + xfs_bmbt_get_blockcount(ep);
3331 return 0;
3332}
3333
3334/*
3335 * Returns whether the selected fork of the inode has exactly one
3336 * block or not. For the data fork we check this matches di_size,
3337 * implying the file's range is 0..bsize-1.
3338 */
3339int /* 1=>1 block, 0=>otherwise */
3340xfs_bmap_one_block(
3341 xfs_inode_t *ip, /* incore inode */
3342 int whichfork) /* data or attr fork */
3343{
3344 xfs_bmbt_rec_t *ep; /* ptr to fork's extent */
3345 xfs_ifork_t *ifp; /* inode fork pointer */
3346 int rval; /* return value */
dfc130f3 3347 xfs_bmbt_irec_t s; /* internal version of extent */
2bd0ea18
NS
3348
3349#ifndef DEBUG
3350 if (whichfork == XFS_DATA_FORK)
3351 return ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize;
3352#endif /* !DEBUG */
3353 if (XFS_IFORK_NEXTENTS(ip, whichfork) != 1)
3354 return 0;
3355 if (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
3356 return 0;
3357 ifp = XFS_IFORK_PTR(ip, whichfork);
3358 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
3359 ep = ifp->if_u1.if_extents;
3360 xfs_bmbt_get_all(ep, &s);
3361 rval = s.br_startoff == 0 && s.br_blockcount == 1;
3362 if (rval && whichfork == XFS_DATA_FORK)
3363 ASSERT(ip->i_d.di_size == ip->i_mount->m_sb.sb_blocksize);
3364 return rval;
3365}
3366
3367/*
3368 * Read in the extents to if_extents.
3369 * All inode fields are set up by caller, we just traverse the btree
3370 * and copy the records in. If the file system cannot contain unwritten
3371 * extents, the records are checked for no "state" flags.
3372 */
3373int /* error */
3374xfs_bmap_read_extents(
3375 xfs_trans_t *tp, /* transaction pointer */
3376 xfs_inode_t *ip, /* incore inode */
3377 int whichfork) /* data or attr fork */
3378{
dfc130f3 3379 xfs_bmbt_block_t *block; /* current btree block */
2bd0ea18 3380 xfs_fsblock_t bno; /* block # of "block" */
7a3bffe4 3381 xfs_buf_t *bp; /* buffer for "block" */
2bd0ea18
NS
3382 int error; /* error return value */
3383 xfs_exntfmt_t exntf; /* XFS_EXTFMT_NOSTATE, if checking */
3384#ifdef XFS_BMAP_TRACE
3385 static char fname[] = "xfs_bmap_read_extents";
3386#endif
f9e56f43 3387 xfs_extnum_t i, j; /* index into the extents list */
2bd0ea18
NS
3388 xfs_ifork_t *ifp; /* fork structure */
3389 int level; /* btree level, for checking */
3390 xfs_mount_t *mp; /* file system mount structure */
3391 xfs_bmbt_ptr_t *pp; /* pointer to block address */
3392 /* REFERENCED */
3393 xfs_extnum_t room; /* number of entries there's room for */
3394 xfs_bmbt_rec_t *trp; /* target record pointer */
3395
3396 bno = NULLFSBLOCK;
3397 mp = ip->i_mount;
3398 ifp = XFS_IFORK_PTR(ip, whichfork);
3399 exntf = (whichfork != XFS_DATA_FORK) ? XFS_EXTFMT_NOSTATE :
3400 XFS_EXTFMT_INODE(ip);
3401 block = ifp->if_broot;
3402 /*
3403 * Root level must use BMAP_BROOT_PTR_ADDR macro to get ptr out.
3404 */
3405 ASSERT(INT_GET(block->bb_level, ARCH_CONVERT) > 0);
3406 level = INT_GET(block->bb_level, ARCH_CONVERT);
3407 pp = XFS_BMAP_BROOT_PTR_ADDR(block, 1, ifp->if_broot_bytes);
3408 ASSERT(INT_GET(*pp, ARCH_CONVERT) != NULLDFSBNO);
3409 ASSERT(XFS_FSB_TO_AGNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agcount);
3410 ASSERT(XFS_FSB_TO_AGBNO(mp, INT_GET(*pp, ARCH_CONVERT)) < mp->m_sb.sb_agblocks);
3411 bno = INT_GET(*pp, ARCH_CONVERT);
3412 /*
3413 * Go down the tree until leaf level is reached, following the first
3414 * pointer (leftmost) at each level.
3415 */
3416 while (level-- > 0) {
0e266570
NS
3417 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
3418 XFS_BMAP_BTREE_REF)))
2bd0ea18
NS
3419 return error;
3420 block = XFS_BUF_TO_BMBT_BLOCK(bp);
3421 XFS_WANT_CORRUPTED_GOTO(
3422 XFS_BMAP_SANITY_CHECK(mp, block, level),
3423 error0);
3424 if (level == 0)
3425 break;
3426 pp = XFS_BTREE_PTR_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt, block,
3427 1, mp->m_bmap_dmxr[1]);
63be04eb
NS
3428 XFS_WANT_CORRUPTED_GOTO(
3429 XFS_FSB_SANITY_CHECK(mp, INT_GET(*pp, ARCH_CONVERT)),
3430 error0);
2bd0ea18
NS
3431 bno = INT_GET(*pp, ARCH_CONVERT);
3432 xfs_trans_brelse(tp, bp);
3433 }
3434 /*
3435 * Here with bp and block set to the leftmost leaf node in the tree.
3436 */
3437 room = ifp->if_bytes / (uint)sizeof(*trp);
3438 trp = ifp->if_u1.if_extents;
3439 i = 0;
3440 /*
3441 * Loop over all leaf nodes. Copy information to the extent list.
3442 */
3443 for (;;) {
f9e56f43 3444 xfs_bmbt_rec_t *frp, *temp;
2bd0ea18
NS
3445 xfs_fsblock_t nextbno;
3446 xfs_extnum_t num_recs;
3447
3448
3449 num_recs = INT_GET(block->bb_numrecs, ARCH_CONVERT);
4ca431fc 3450 if (unlikely(i + num_recs > room)) {
2bd0ea18
NS
3451 ASSERT(i + num_recs <= room);
3452 xfs_fs_cmn_err(CE_WARN, ip->i_mount,
4ca431fc 3453 "corrupt dinode %Lu, (btree extents). Unmount and run xfs_repair.",
31c5308f 3454 (unsigned long long) ip->i_ino);
4ca431fc
NS
3455 XFS_ERROR_REPORT("xfs_bmap_read_extents(1)",
3456 XFS_ERRLEVEL_LOW,
3457 ip->i_mount);
2bd0ea18
NS
3458 goto error0;
3459 }
3460 XFS_WANT_CORRUPTED_GOTO(
3461 XFS_BMAP_SANITY_CHECK(mp, block, 0),
3462 error0);
3463 /*
3464 * Read-ahead the next leaf block, if any.
3465 */
3466 nextbno = INT_GET(block->bb_rightsib, ARCH_CONVERT);
3467 if (nextbno != NULLFSBLOCK)
3468 xfs_btree_reada_bufl(mp, nextbno, 1);
3469 /*
3470 * Copy records into the extent list.
3471 */
3472 frp = XFS_BTREE_REC_ADDR(mp->m_sb.sb_blocksize, xfs_bmbt,
3473 block, 1, mp->m_bmap_dmxr[0]);
f9e56f43
NS
3474 temp = trp;
3475 for (j = 0; j < num_recs; j++, frp++, trp++) {
3476 trp->l0 = INT_GET(frp->l0, ARCH_CONVERT);
3477 trp->l1 = INT_GET(frp->l1, ARCH_CONVERT);
3478 }
2bd0ea18
NS
3479 if (exntf == XFS_EXTFMT_NOSTATE) {
3480 /*
3481 * Check all attribute bmap btree records and
5000d01d 3482 * any "older" data bmap btree records for a
2bd0ea18
NS
3483 * set bit in the "extent flag" position.
3484 */
4ca431fc
NS
3485 if (unlikely(xfs_check_nostate_extents(temp, num_recs))) {
3486 XFS_ERROR_REPORT("xfs_bmap_read_extents(2)",
3487 XFS_ERRLEVEL_LOW,
3488 ip->i_mount);
2bd0ea18
NS
3489 goto error0;
3490 }
3491 }
2bd0ea18
NS
3492 i += num_recs;
3493 xfs_trans_brelse(tp, bp);
3494 bno = nextbno;
3495 /*
3496 * If we've reached the end, stop.
3497 */
3498 if (bno == NULLFSBLOCK)
3499 break;
0e266570
NS
3500 if ((error = xfs_btree_read_bufl(mp, tp, bno, 0, &bp,
3501 XFS_BMAP_BTREE_REF)))
2bd0ea18
NS
3502 return error;
3503 block = XFS_BUF_TO_BMBT_BLOCK(bp);
3504 }
3505 ASSERT(i == ifp->if_bytes / (uint)sizeof(*trp));
3506 ASSERT(i == XFS_IFORK_NEXTENTS(ip, whichfork));
3507 xfs_bmap_trace_exlist(fname, ip, i, whichfork);
3508 return 0;
3509error0:
3510 xfs_trans_brelse(tp, bp);
3511 return XFS_ERROR(EFSCORRUPTED);
3512}
3513
3514/*
3515 * Map file blocks to filesystem blocks.
3516 * File range is given by the bno/len pair.
3517 * Adds blocks to file if a write ("flags & XFS_BMAPI_WRITE" set)
3518 * into a hole or past eof.
3519 * Only allocates blocks from a single allocation group,
3520 * to avoid locking problems.
3521 * The returned value in "firstblock" from the first call in a transaction
3522 * must be remembered and presented to subsequent calls in "firstblock".
3523 * An upper bound for the number of blocks to be allocated is supplied to
3524 * the first call in "total"; if no allocation group has that many free
3525 * blocks then the call will fail (return NULLFSBLOCK in "firstblock").
3526 */
3527int /* error */
3528xfs_bmapi(
3529 xfs_trans_t *tp, /* transaction pointer */
3530 xfs_inode_t *ip, /* incore inode */
3531 xfs_fileoff_t bno, /* starting file offs. mapped */
3532 xfs_filblks_t len, /* length to map in file */
3533 int flags, /* XFS_BMAPI_... */
3534 xfs_fsblock_t *firstblock, /* first allocated block
3535 controls a.g. for allocs */
3536 xfs_extlen_t total, /* total blocks needed */
dfc130f3 3537 xfs_bmbt_irec_t *mval, /* output: map values */
2bd0ea18 3538 int *nmap, /* i/o: mval size/count */
dfc130f3 3539 xfs_bmap_free_t *flist) /* i/o: list extents to free */
2bd0ea18
NS
3540{
3541 xfs_fsblock_t abno; /* allocated block number */
3542 xfs_extlen_t alen; /* allocated extent length */
3543 xfs_fileoff_t aoff; /* allocated file offset */
3544 xfs_bmalloca_t bma; /* args for xfs_bmap_alloc */
85a875e9 3545 char contig; /* allocation must be one extent */
dfc130f3 3546 xfs_btree_cur_t *cur; /* bmap btree cursor */
2bd0ea18
NS
3547 char delay; /* this request is for delayed alloc */
3548 xfs_fileoff_t end; /* end of mapped file region */
3549 int eof; /* we've hit the end of extent list */
3550 xfs_bmbt_rec_t *ep; /* extent list entry pointer */
3551 int error; /* error return */
3552 char exact; /* don't do all of wasdelayed extent */
dfc130f3 3553 xfs_bmbt_irec_t got; /* current extent list record */
2bd0ea18
NS
3554 xfs_ifork_t *ifp; /* inode fork pointer */
3555 xfs_extlen_t indlen; /* indirect blocks length */
3556 char inhole; /* current location is hole in file */
3557 xfs_extnum_t lastx; /* last useful extent number */
3558 int logflags; /* flags for transaction logging */
3559 xfs_extlen_t minleft; /* min blocks left after allocation */
3560 xfs_extlen_t minlen; /* min allocation size */
3561 xfs_mount_t *mp; /* xfs mount structure */
3562 int n; /* current extent index */
3563 int nallocs; /* number of extents alloc\'d */
3564 xfs_extnum_t nextents; /* number of extents in file */
3565 xfs_fileoff_t obno; /* old block number (offset) */
dfc130f3 3566 xfs_bmbt_irec_t prev; /* previous extent list record */
85a875e9 3567 char stateless; /* ignore state flag set */
2bd0ea18
NS
3568 int tmp_logflags; /* temp flags holder */
3569 char trim; /* output trimmed to match range */
3570 char userdata; /* allocating non-metadata */
3571 char wasdelay; /* old extent was delayed */
3572 int whichfork; /* data or attr fork */
3573 char wr; /* this is a write request */
85a875e9 3574 char rsvd; /* OK to allocate reserved blocks */
2bd0ea18
NS
3575#ifdef DEBUG
3576 xfs_fileoff_t orig_bno; /* original block number value */
3577 int orig_flags; /* original flags arg value */
3578 xfs_filblks_t orig_len; /* original value of len arg */
dfc130f3 3579 xfs_bmbt_irec_t *orig_mval; /* original value of mval */
2bd0ea18
NS
3580 int orig_nmap; /* original value of *nmap */
3581
3582 orig_bno = bno;
3583 orig_len = len;
3584 orig_flags = flags;
3585 orig_mval = mval;
3586 orig_nmap = *nmap;
3587#endif
3588 ASSERT(*nmap >= 1);
3589 ASSERT(*nmap <= XFS_BMAP_MAX_NMAP || !(flags & XFS_BMAPI_WRITE));
3590 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
3591 XFS_ATTR_FORK : XFS_DATA_FORK;
4ca431fc
NS
3592 mp = ip->i_mount;
3593 if (unlikely(XFS_TEST_ERROR(
3594 (XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
3595 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
3596 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_LOCAL),
3597 mp, XFS_ERRTAG_BMAPIFORMAT, XFS_RANDOM_BMAPIFORMAT))) {
3598 XFS_ERROR_REPORT("xfs_bmapi", XFS_ERRLEVEL_LOW, mp);
2bd0ea18 3599 return XFS_ERROR(EFSCORRUPTED);
63be04eb 3600 }
2bd0ea18
NS
3601 if (XFS_FORCED_SHUTDOWN(mp))
3602 return XFS_ERROR(EIO);
3603 ifp = XFS_IFORK_PTR(ip, whichfork);
3604 ASSERT(ifp->if_ext_max ==
3605 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
0e266570 3606 if ((wr = (flags & XFS_BMAPI_WRITE)) != 0)
32a82561 3607 XFS_STATS_INC(xs_blk_mapw);
2bd0ea18 3608 else
32a82561 3609 XFS_STATS_INC(xs_blk_mapr);
2bd0ea18
NS
3610 delay = (flags & XFS_BMAPI_DELAY) != 0;
3611 trim = (flags & XFS_BMAPI_ENTIRE) == 0;
3612 userdata = (flags & XFS_BMAPI_METADATA) == 0;
3613 exact = (flags & XFS_BMAPI_EXACT) != 0;
3614 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
3615 contig = (flags & XFS_BMAPI_CONTIG) != 0;
3616 /*
3617 * stateless is used to combine extents which
3618 * differ only due to the state of the extents.
3619 * This technique is used from xfs_getbmap()
3620 * when the caller does not wish to see the
3621 * separation (which is the default).
3622 *
5000d01d 3623 * This technique is also used when writing a
2bd0ea18
NS
3624 * buffer which has been partially written,
3625 * (usually by being flushed during a chunkread),
3626 * to ensure one write takes place. This also
3627 * prevents a change in the xfs inode extents at
3628 * this time, intentionally. This change occurs
3629 * on completion of the write operation, in
3630 * xfs_strat_comp(), where the xfs_bmapi() call
3631 * is transactioned, and the extents combined.
3632 */
3633 stateless = (flags & XFS_BMAPI_IGSTATE) != 0;
3634 if (stateless && wr) /* if writing unwritten space, no */
3635 wr = 0; /* allocations are allowed */
3636 ASSERT(wr || !delay);
3637 logflags = 0;
3638 nallocs = 0;
3639 cur = NULL;
3640 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
3641 ASSERT(wr && tp);
eae766ca
NS
3642 if ((error = xfs_bmap_local_to_extents(tp, ip,
3643 firstblock, total, &logflags, whichfork)))
2bd0ea18
NS
3644 goto error0;
3645 }
3646 if (wr && *firstblock == NULLFSBLOCK) {
3647 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE)
3648 minleft = INT_GET(ifp->if_broot->bb_level, ARCH_CONVERT) + 1;
3649 else
3650 minleft = 1;
3651 } else
3652 minleft = 0;
3653 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
3654 (error = xfs_iread_extents(tp, ip, whichfork)))
3655 goto error0;
3656 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
3657 &prev);
3658 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3659 n = 0;
3660 end = bno + len;
3661 obno = bno;
3662 bma.ip = NULL;
3663 while (bno < end && n < *nmap) {
5000d01d 3664 /*
2bd0ea18
NS
3665 * Reading past eof, act as though there's a hole
3666 * up to end.
3667 */
3668 if (eof && !wr)
3669 got.br_startoff = end;
3670 inhole = eof || got.br_startoff > bno;
3671 wasdelay = wr && !inhole && !delay &&
3672 ISNULLSTARTBLOCK(got.br_startblock);
3673 /*
5000d01d 3674 * First, deal with the hole before the allocated space
2bd0ea18
NS
3675 * that we found, if any.
3676 */
3677 if (wr && (inhole || wasdelay)) {
3678 /*
3679 * For the wasdelay case, we could also just
3680 * allocate the stuff asked for in this bmap call
3681 * but that wouldn't be as good.
3682 */
3683 if (wasdelay && !exact) {
3684 alen = (xfs_extlen_t)got.br_blockcount;
3685 aoff = got.br_startoff;
3686 if (lastx != NULLEXTNUM && lastx) {
3687 ep = &ifp->if_u1.if_extents[lastx - 1];
3688 xfs_bmbt_get_all(ep, &prev);
3689 }
3690 } else if (wasdelay) {
3691 alen = (xfs_extlen_t)
3692 XFS_FILBLKS_MIN(len,
3693 (got.br_startoff +
3694 got.br_blockcount) - bno);
3695 aoff = bno;
3696 } else {
3697 alen = (xfs_extlen_t)
3698 XFS_FILBLKS_MIN(len, MAXEXTLEN);
3699 if (!eof)
3700 alen = (xfs_extlen_t)
3701 XFS_FILBLKS_MIN(alen,
3702 got.br_startoff - bno);
3703 aoff = bno;
3704 }
3705 minlen = contig ? alen : 1;
3706 if (delay) {
3707 indlen = (xfs_extlen_t)
3708 xfs_bmap_worst_indlen(ip, alen);
3709 ASSERT(indlen > 0);
3710 /*
3711 * Make a transaction-less quota reservation for
3712 * delayed allocation blocks. This number gets
3713 * adjusted later.
3714 * We return EDQUOT if we haven't allocated
3715 * blks already inside this loop;
3716 */
eae766ca
NS
3717 if (XFS_TRANS_RESERVE_BLKQUOTA(
3718 mp, NULL, ip, (long)alen)) {
2bd0ea18
NS
3719 if (n == 0) {
3720 *nmap = 0;
3721 ASSERT(cur == NULL);
3722 return XFS_ERROR(EDQUOT);
3723 }
3724 break;
3725 }
062998e3
NS
3726
3727 /*
3728 * Split changing sb for alen and indlen since
3729 * they could be coming from different places.
3730 */
3731 if (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) {
3732 xfs_extlen_t extsz;
3733 xfs_extlen_t ralen;
3734 if (!(extsz = ip->i_d.di_extsize))
3735 extsz = mp->m_sb.sb_rextsize;
3736 ralen = roundup(alen, extsz);
3737 ralen = ralen / mp->m_sb.sb_rextsize;
3738 if (xfs_mod_incore_sb(mp,
3739 XFS_SBS_FREXTENTS,
3740 -(ralen), rsvd)) {
3741 if (XFS_IS_QUOTA_ON(ip->i_mount))
3742 XFS_TRANS_UNRESERVE_BLKQUOTA(
3743 mp, NULL, ip,
3744 (long)alen);
3745 break;
3746 }
3747 } else {
3748 if (xfs_mod_incore_sb(mp,
3749 XFS_SBS_FDBLOCKS,
3750 -(alen), rsvd)) {
3751 if (XFS_IS_QUOTA_ON(ip->i_mount))
3752 XFS_TRANS_UNRESERVE_BLKQUOTA(
3753 mp, NULL, ip,
3754 (long)alen);
3755 break;
3756 }
3757 }
3758
eae766ca 3759 if (xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
062998e3 3760 -(indlen), rsvd)) {
eae766ca
NS
3761 XFS_TRANS_UNRESERVE_BLKQUOTA(
3762 mp, NULL, ip, (long)alen);
2bd0ea18
NS
3763 break;
3764 }
3765 ip->i_delayed_blks += alen;
3766 abno = NULLSTARTBLOCK(indlen);
3767 } else {
3768 /*
3769 * If first time, allocate and fill in
3770 * once-only bma fields.
3771 */
3772 if (bma.ip == NULL) {
3773 bma.tp = tp;
3774 bma.ip = ip;
3775 bma.prevp = &prev;
3776 bma.gotp = &got;
3777 bma.total = total;
34317449
NS
3778 bma.userdata = 0;
3779 }
3780 /* Indicate if this is the first user data
3781 * in the file, or just any user data.
3782 */
3783 if (userdata) {
5000d01d
SL
3784 bma.userdata = (aoff == 0) ?
3785 XFS_ALLOC_INITIAL_USER_DATA :
34317449 3786 XFS_ALLOC_USERDATA;
2bd0ea18
NS
3787 }
3788 /*
3789 * Fill in changeable bma fields.
3790 */
3791 bma.eof = eof;
3792 bma.firstblock = *firstblock;
3793 bma.alen = alen;
3794 bma.off = aoff;
3795 bma.wasdel = wasdelay;
3796 bma.minlen = minlen;
3797 bma.low = flist->xbf_low;
3798 bma.minleft = minleft;
3799 /*
3800 * Only want to do the alignment at the
5000d01d 3801 * eof if it is userdata and allocation length
2bd0ea18
NS
3802 * is larger than a stripe unit.
3803 */
3804 if (mp->m_dalign && alen >= mp->m_dalign &&
3805 userdata && whichfork == XFS_DATA_FORK) {
0e266570
NS
3806 if ((error = xfs_bmap_isaeof(ip, aoff,
3807 whichfork, &bma.aeof)))
2bd0ea18
NS
3808 goto error0;
3809 } else
3810 bma.aeof = 0;
3811 /*
3812 * Call allocator.
3813 */
0e266570 3814 if ((error = xfs_bmap_alloc(&bma)))
2bd0ea18
NS
3815 goto error0;
3816 /*
3817 * Copy out result fields.
3818 */
3819 abno = bma.rval;
0e266570 3820 if ((flist->xbf_low = bma.low))
2bd0ea18
NS
3821 minleft = 0;
3822 alen = bma.alen;
3823 aoff = bma.off;
3824 ASSERT(*firstblock == NULLFSBLOCK ||
eae766ca
NS
3825 XFS_FSB_TO_AGNO(mp, *firstblock) ==
3826 XFS_FSB_TO_AGNO(mp, bma.firstblock) ||
2bd0ea18 3827 (flist->xbf_low &&
eae766ca
NS
3828 XFS_FSB_TO_AGNO(mp, *firstblock) <
3829 XFS_FSB_TO_AGNO(mp, bma.firstblock)));
2bd0ea18
NS
3830 *firstblock = bma.firstblock;
3831 if (cur)
3832 cur->bc_private.b.firstblock =
3833 *firstblock;
3834 if (abno == NULLFSBLOCK)
3835 break;
3836 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
eae766ca 3837 cur = xfs_btree_init_cursor(mp,
2bd0ea18
NS
3838 tp, NULL, 0, XFS_BTNUM_BMAP,
3839 ip, whichfork);
3840 cur->bc_private.b.firstblock =
3841 *firstblock;
3842 cur->bc_private.b.flist = flist;
3843 }
3844 /*
3845 * Bump the number of extents we've allocated
3846 * in this call.
3847 */
3848 nallocs++;
3849 }
3850 if (cur)
3851 cur->bc_private.b.flags =
3852 wasdelay ? XFS_BTCUR_BPRV_WASDEL : 0;
3853 got.br_startoff = aoff;
3854 got.br_startblock = abno;
3855 got.br_blockcount = alen;
3856 got.br_state = XFS_EXT_NORM; /* assume normal */
3857 /*
3858 * Determine state of extent, and the filesystem.
5000d01d 3859 * A wasdelay extent has been initialized, so
2bd0ea18
NS
3860 * shouldn't be flagged as unwritten.
3861 */
3862 if (wr && XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
3863 if (!wasdelay && (flags & XFS_BMAPI_PREALLOC))
3864 got.br_state = XFS_EXT_UNWRITTEN;
3865 }
3866 error = xfs_bmap_add_extent(ip, lastx, &cur, &got,
3867 firstblock, flist, &tmp_logflags, whichfork,
3868 rsvd);
3869 logflags |= tmp_logflags;
3870 if (error)
3871 goto error0;
3872 lastx = ifp->if_lastex;
3873 ep = &ifp->if_u1.if_extents[lastx];
3874 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3875 xfs_bmbt_get_all(ep, &got);
3876 ASSERT(got.br_startoff <= aoff);
3877 ASSERT(got.br_startoff + got.br_blockcount >=
3878 aoff + alen);
3879#ifdef DEBUG
3880 if (delay) {
3881 ASSERT(ISNULLSTARTBLOCK(got.br_startblock));
3882 ASSERT(STARTBLOCKVAL(got.br_startblock) > 0);
3883 }
3884 ASSERT(got.br_state == XFS_EXT_NORM ||
3885 got.br_state == XFS_EXT_UNWRITTEN);
3886#endif
3887 /*
3888 * Fall down into the found allocated space case.
3889 */
3890 } else if (inhole) {
3891 /*
3892 * Reading in a hole.
3893 */
3894 mval->br_startoff = bno;
3895 mval->br_startblock = HOLESTARTBLOCK;
3896 mval->br_blockcount =
3897 XFS_FILBLKS_MIN(len, got.br_startoff - bno);
3898 mval->br_state = XFS_EXT_NORM;
3899 bno += mval->br_blockcount;
3900 len -= mval->br_blockcount;
3901 mval++;
3902 n++;
3903 continue;
3904 }
3905 /*
3906 * Then deal with the allocated space we found.
3907 */
3908 ASSERT(ep != NULL);
3909 if (trim && (got.br_startoff + got.br_blockcount > obno)) {
3910 if (obno > bno)
3911 bno = obno;
3912 ASSERT((bno >= obno) || (n == 0));
3913 ASSERT(bno < end);
3914 mval->br_startoff = bno;
3915 if (ISNULLSTARTBLOCK(got.br_startblock)) {
3916 ASSERT(!wr || delay);
3917 mval->br_startblock = DELAYSTARTBLOCK;
3918 } else
3919 mval->br_startblock =
3920 got.br_startblock +
3921 (bno - got.br_startoff);
3922 /*
3923 * Return the minimum of what we got and what we
3924 * asked for for the length. We can use the len
3925 * variable here because it is modified below
3926 * and we could have been there before coming
3927 * here if the first part of the allocation
3928 * didn't overlap what was asked for.
3929 */
3930 mval->br_blockcount =
3931 XFS_FILBLKS_MIN(end - bno, got.br_blockcount -
3932 (bno - got.br_startoff));
3933 mval->br_state = got.br_state;
3934 ASSERT(mval->br_blockcount <= len);
3935 } else {
3936 *mval = got;
3937 if (ISNULLSTARTBLOCK(mval->br_startblock)) {
3938 ASSERT(!wr || delay);
3939 mval->br_startblock = DELAYSTARTBLOCK;
3940 }
3941 }
3942
3943 /*
3944 * Check if writing previously allocated but
3945 * unwritten extents.
3946 */
3947 if (wr && mval->br_state == XFS_EXT_UNWRITTEN &&
3948 ((flags & (XFS_BMAPI_PREALLOC|XFS_BMAPI_DELAY)) == 0)) {
3949 /*
3950 * Modify (by adding) the state flag, if writing.
3951 */
3952 ASSERT(mval->br_blockcount <= len);
3953 if ((ifp->if_flags & XFS_IFBROOT) && !cur) {
eae766ca 3954 cur = xfs_btree_init_cursor(mp,
2bd0ea18
NS
3955 tp, NULL, 0, XFS_BTNUM_BMAP,
3956 ip, whichfork);
3957 cur->bc_private.b.firstblock =
3958 *firstblock;
3959 cur->bc_private.b.flist = flist;
3960 }
3961 mval->br_state = XFS_EXT_NORM;
3962 error = xfs_bmap_add_extent(ip, lastx, &cur, mval,
3963 firstblock, flist, &tmp_logflags, whichfork,
3964 rsvd);
3965 logflags |= tmp_logflags;
3966 if (error)
3967 goto error0;
3968 lastx = ifp->if_lastex;
3969 ep = &ifp->if_u1.if_extents[lastx];
3970 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
3971 xfs_bmbt_get_all(ep, &got);
3972 /*
5000d01d
SL
3973 * We may have combined previously unwritten
3974 * space with written space, so generate
2bd0ea18
NS
3975 * another request.
3976 */
3977 if (mval->br_blockcount < len)
3978 continue;
3979 }
3980
3981 ASSERT(!trim ||
3982 ((mval->br_startoff + mval->br_blockcount) <= end));
3983 ASSERT(!trim || (mval->br_blockcount <= len) ||
3984 (mval->br_startoff < obno));
3985 bno = mval->br_startoff + mval->br_blockcount;
3986 len = end - bno;
3987 if (n > 0 && mval->br_startoff == mval[-1].br_startoff) {
3988 ASSERT(mval->br_startblock == mval[-1].br_startblock);
3989 ASSERT(mval->br_blockcount > mval[-1].br_blockcount);
3990 ASSERT(mval->br_state == mval[-1].br_state);
3991 mval[-1].br_blockcount = mval->br_blockcount;
3992 mval[-1].br_state = mval->br_state;
3993 } else if (n > 0 && mval->br_startblock != DELAYSTARTBLOCK &&
3994 mval[-1].br_startblock != DELAYSTARTBLOCK &&
3995 mval[-1].br_startblock != HOLESTARTBLOCK &&
3996 mval->br_startblock ==
3997 mval[-1].br_startblock + mval[-1].br_blockcount &&
3998 (stateless || mval[-1].br_state == mval->br_state)) {
3999 ASSERT(mval->br_startoff ==
4000 mval[-1].br_startoff + mval[-1].br_blockcount);
4001 mval[-1].br_blockcount += mval->br_blockcount;
4002 } else if (n > 0 &&
4003 mval->br_startblock == DELAYSTARTBLOCK &&
4004 mval[-1].br_startblock == DELAYSTARTBLOCK &&
4005 mval->br_startoff ==
4006 mval[-1].br_startoff + mval[-1].br_blockcount) {
4007 mval[-1].br_blockcount += mval->br_blockcount;
4008 mval[-1].br_state = mval->br_state;
4009 } else if (!((n == 0) &&
4010 ((mval->br_startoff + mval->br_blockcount) <=
4011 obno))) {
4012 mval++;
4013 n++;
4014 }
4015 /*
4016 * If we're done, stop now. Stop when we've allocated
4017 * XFS_BMAP_MAX_NMAP extents no matter what. Otherwise
4018 * the transaction may get too big.
4019 */
4020 if (bno >= end || n >= *nmap || nallocs >= *nmap)
4021 break;
4022 /*
4023 * Else go on to the next record.
4024 */
4025 ep++;
4026 lastx++;
4027 if (lastx >= nextents) {
4028 eof = 1;
4029 prev = got;
4030 } else
4031 xfs_bmbt_get_all(ep, &got);
4032 }
4033 ifp->if_lastex = lastx;
4034 *nmap = n;
4035 /*
4036 * Transform from btree to extents, give it cur.
4037 */
4038 if (tp && XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4039 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4040 ASSERT(wr && cur);
4041 error = xfs_bmap_btree_to_extents(tp, ip, cur,
4ca431fc 4042 &tmp_logflags, whichfork);
2bd0ea18
NS
4043 logflags |= tmp_logflags;
4044 if (error)
4045 goto error0;
4046 }
4047 ASSERT(ifp->if_ext_max ==
4048 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4049 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE ||
4050 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max);
4051 error = 0;
4052
4053error0:
4054 /*
4055 * Log everything. Do this after conversion, there's no point in
4056 * logging the extent list if we've converted to btree format.
4057 */
4058 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
4059 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4060 logflags &= ~XFS_ILOG_FEXT(whichfork);
4061 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
4062 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4063 logflags &= ~XFS_ILOG_FBROOT(whichfork);
4064 /*
4065 * Log whatever the flags say, even if error. Otherwise we might miss
4066 * detecting a case where the data is changed, there's an error,
4067 * and it's not logged so we don't shutdown when we should.
4068 */
4069 if (logflags) {
4070 ASSERT(tp && wr);
4071 xfs_trans_log_inode(tp, ip, logflags);
4072 }
4073 if (cur) {
4074 if (!error) {
4075 ASSERT(*firstblock == NULLFSBLOCK ||
eae766ca
NS
4076 XFS_FSB_TO_AGNO(mp, *firstblock) ==
4077 XFS_FSB_TO_AGNO(mp,
2bd0ea18
NS
4078 cur->bc_private.b.firstblock) ||
4079 (flist->xbf_low &&
eae766ca
NS
4080 XFS_FSB_TO_AGNO(mp, *firstblock) <
4081 XFS_FSB_TO_AGNO(mp,
2bd0ea18
NS
4082 cur->bc_private.b.firstblock)));
4083 *firstblock = cur->bc_private.b.firstblock;
4084 }
4085 xfs_btree_del_cursor(cur,
4086 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4087 }
4088 if (!error)
4089 xfs_bmap_validate_ret(orig_bno, orig_len, orig_flags, orig_mval,
4090 orig_nmap, *nmap);
4091 return error;
4092}
4093
4094/*
4095 * Map file blocks to filesystem blocks, simple version.
4096 * One block (extent) only, read-only.
4097 * For flags, only the XFS_BMAPI_ATTRFORK flag is examined.
4098 * For the other flag values, the effect is as if XFS_BMAPI_METADATA
4099 * was set and all the others were clear.
4100 */
4101int /* error */
4102xfs_bmapi_single(
4103 xfs_trans_t *tp, /* transaction pointer */
4104 xfs_inode_t *ip, /* incore inode */
4105 int whichfork, /* data or attr fork */
4106 xfs_fsblock_t *fsb, /* output: mapped block */
4107 xfs_fileoff_t bno) /* starting file offs. mapped */
4108{
4109 int eof; /* we've hit the end of extent list */
4110 int error; /* error return */
dfc130f3 4111 xfs_bmbt_irec_t got; /* current extent list record */
2bd0ea18
NS
4112 xfs_ifork_t *ifp; /* inode fork pointer */
4113 xfs_extnum_t lastx; /* last useful extent number */
dfc130f3 4114 xfs_bmbt_irec_t prev; /* previous extent list record */
2bd0ea18
NS
4115
4116 ifp = XFS_IFORK_PTR(ip, whichfork);
4ca431fc
NS
4117 if (unlikely(
4118 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE &&
4119 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)) {
4120 XFS_ERROR_REPORT("xfs_bmapi_single", XFS_ERRLEVEL_LOW,
4121 ip->i_mount);
2bd0ea18 4122 return XFS_ERROR(EFSCORRUPTED);
63be04eb 4123 }
2bd0ea18
NS
4124 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
4125 return XFS_ERROR(EIO);
32a82561 4126 XFS_STATS_INC(xs_blk_mapr);
2bd0ea18
NS
4127 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4128 (error = xfs_iread_extents(tp, ip, whichfork)))
4129 return error;
4130 (void)xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4131 &prev);
5000d01d 4132 /*
2bd0ea18
NS
4133 * Reading past eof, act as though there's a hole
4134 * up to end.
4135 */
4136 if (eof || got.br_startoff > bno) {
4137 *fsb = NULLFSBLOCK;
4138 return 0;
4139 }
4140 ASSERT(!ISNULLSTARTBLOCK(got.br_startblock));
4141 ASSERT(bno < got.br_startoff + got.br_blockcount);
4142 *fsb = got.br_startblock + (bno - got.br_startoff);
4143 ifp->if_lastex = lastx;
4144 return 0;
4145}
4146
4147/*
4148 * Unmap (remove) blocks from a file.
4149 * If nexts is nonzero then the number of extents to remove is limited to
dfc130f3 4150 * that value. If not all extents in the block range can be removed then
2bd0ea18
NS
4151 * *done is set.
4152 */
4153int /* error */
4154xfs_bunmapi(
4155 xfs_trans_t *tp, /* transaction pointer */
4156 struct xfs_inode *ip, /* incore inode */
4157 xfs_fileoff_t bno, /* starting offset to unmap */
4158 xfs_filblks_t len, /* length to unmap in file */
5000d01d 4159 int flags, /* misc flags */
2bd0ea18
NS
4160 xfs_extnum_t nexts, /* number of extents max */
4161 xfs_fsblock_t *firstblock, /* first allocated block
4162 controls a.g. for allocs */
4163 xfs_bmap_free_t *flist, /* i/o: list extents to free */
4164 int *done) /* set if not done yet */
4165{
2bd0ea18
NS
4166 xfs_btree_cur_t *cur; /* bmap btree cursor */
4167 xfs_bmbt_irec_t del; /* extent being deleted */
4168 int eof; /* is deleting at eof */
4169 xfs_bmbt_rec_t *ep; /* extent list entry pointer */
4170 int error; /* error return value */
4171 xfs_extnum_t extno; /* extent number in list */
4172 xfs_bmbt_irec_t got; /* current extent list entry */
4173 xfs_ifork_t *ifp; /* inode fork pointer */
4174 int isrt; /* freeing in rt area */
4175 xfs_extnum_t lastx; /* last extent index used */
4176 int logflags; /* transaction logging flags */
4177 xfs_extlen_t mod; /* rt extent offset */
4178 xfs_mount_t *mp; /* mount structure */
4179 xfs_extnum_t nextents; /* size of extent list */
4180 xfs_bmbt_irec_t prev; /* previous extent list entry */
4181 xfs_fileoff_t start; /* first file offset deleted */
4182 int tmp_logflags; /* partial logging flags */
4183 int wasdel; /* was a delayed alloc extent */
4184 int whichfork; /* data or attribute fork */
4185 int rsvd; /* OK to allocate reserved blocks */
4186 xfs_fsblock_t sum;
4187
4188 xfs_bunmap_trace(ip, bno, len, flags, (inst_t *)__return_address);
4189 whichfork = (flags & XFS_BMAPI_ATTRFORK) ?
4190 XFS_ATTR_FORK : XFS_DATA_FORK;
4191 ifp = XFS_IFORK_PTR(ip, whichfork);
4ca431fc
NS
4192 if (unlikely(
4193 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS &&
4194 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
4195 XFS_ERROR_REPORT("xfs_bunmapi", XFS_ERRLEVEL_LOW,
4196 ip->i_mount);
2bd0ea18 4197 return XFS_ERROR(EFSCORRUPTED);
63be04eb 4198 }
2bd0ea18
NS
4199 mp = ip->i_mount;
4200 if (XFS_FORCED_SHUTDOWN(mp))
4201 return XFS_ERROR(EIO);
2bd0ea18
NS
4202 rsvd = (flags & XFS_BMAPI_RSVBLOCKS) != 0;
4203 ASSERT(len > 0);
4204 ASSERT(nexts >= 0);
4205 ASSERT(ifp->if_ext_max ==
4206 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4207 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4208 (error = xfs_iread_extents(tp, ip, whichfork)))
4209 return error;
4210 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4211 if (nextents == 0) {
4212 *done = 1;
4213 return 0;
4214 }
32a82561 4215 XFS_STATS_INC(xs_blk_unmap);
2bd0ea18
NS
4216 isrt = (whichfork == XFS_DATA_FORK) &&
4217 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME);
4218 start = bno;
4219 bno = start + len - 1;
4220 ep = xfs_bmap_search_extents(ip, bno, whichfork, &eof, &lastx, &got,
4221 &prev);
4222 /*
4223 * Check to see if the given block number is past the end of the
4224 * file, back up to the last block if so...
4225 */
4226 if (eof) {
4227 ep = &ifp->if_u1.if_extents[--lastx];
4228 xfs_bmbt_get_all(ep, &got);
4229 bno = got.br_startoff + got.br_blockcount - 1;
4230 }
4231 logflags = 0;
4232 if (ifp->if_flags & XFS_IFBROOT) {
4233 ASSERT(XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE);
4234 cur = xfs_btree_init_cursor(mp, tp, NULL, 0, XFS_BTNUM_BMAP, ip,
4235 whichfork);
4236 cur->bc_private.b.firstblock = *firstblock;
4237 cur->bc_private.b.flist = flist;
4238 cur->bc_private.b.flags = 0;
4239 } else
4240 cur = NULL;
4241 extno = 0;
4242 while (bno != (xfs_fileoff_t)-1 && bno >= start && lastx >= 0 &&
4243 (nexts == 0 || extno < nexts)) {
4244 /*
4245 * Is the found extent after a hole in which bno lives?
4246 * Just back up to the previous extent, if so.
4247 */
4248 if (got.br_startoff > bno) {
4249 if (--lastx < 0)
4250 break;
4251 ep--;
4252 xfs_bmbt_get_all(ep, &got);
4253 }
4254 /*
4255 * Is the last block of this extent before the range
4256 * we're supposed to delete? If so, we're done.
4257 */
4258 bno = XFS_FILEOFF_MIN(bno,
4259 got.br_startoff + got.br_blockcount - 1);
4260 if (bno < start)
4261 break;
4262 /*
4263 * Then deal with the (possibly delayed) allocated space
4264 * we found.
4265 */
4266 ASSERT(ep != NULL);
4267 del = got;
4268 wasdel = ISNULLSTARTBLOCK(del.br_startblock);
4269 if (got.br_startoff < start) {
4270 del.br_startoff = start;
4271 del.br_blockcount -= start - got.br_startoff;
4272 if (!wasdel)
4273 del.br_startblock += start - got.br_startoff;
4274 }
4275 if (del.br_startoff + del.br_blockcount > bno + 1)
4276 del.br_blockcount = bno + 1 - del.br_startoff;
4277 sum = del.br_startblock + del.br_blockcount;
4278 if (isrt &&
4279 (mod = do_mod(sum, mp->m_sb.sb_rextsize))) {
4280 /*
4281 * Realtime extent not lined up at the end.
4282 * The extent could have been split into written
4283 * and unwritten pieces, or we could just be
4284 * unmapping part of it. But we can't really
4285 * get rid of part of a realtime extent.
4286 */
4287 if (del.br_state == XFS_EXT_UNWRITTEN ||
4288 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4289 /*
4290 * This piece is unwritten, or we're not
4291 * using unwritten extents. Skip over it.
4292 */
4293 ASSERT(bno >= mod);
4294 bno -= mod > del.br_blockcount ?
4295 del.br_blockcount : mod;
4296 if (bno < got.br_startoff) {
4297 if (--lastx >= 0)
4298 xfs_bmbt_get_all(--ep, &got);
4299 }
4300 continue;
4301 }
4302 /*
4303 * It's written, turn it unwritten.
4304 * This is better than zeroing it.
4305 */
4306 ASSERT(del.br_state == XFS_EXT_NORM);
4307 ASSERT(xfs_trans_get_block_res(tp) > 0);
4308 /*
4309 * If this spans a realtime extent boundary,
4310 * chop it back to the start of the one we end at.
4311 */
4312 if (del.br_blockcount > mod) {
4313 del.br_startoff += del.br_blockcount - mod;
4314 del.br_startblock += del.br_blockcount - mod;
4315 del.br_blockcount = mod;
4316 }
4317 del.br_state = XFS_EXT_UNWRITTEN;
4318 error = xfs_bmap_add_extent(ip, lastx, &cur, &del,
4319 firstblock, flist, &logflags, XFS_DATA_FORK, 0);
4320 if (error)
4321 goto error0;
4322 goto nodelete;
4323 }
4324 if (isrt && (mod = do_mod(del.br_startblock, mp->m_sb.sb_rextsize))) {
4325 /*
4326 * Realtime extent is lined up at the end but not
4327 * at the front. We'll get rid of full extents if
4328 * we can.
4329 */
4330 mod = mp->m_sb.sb_rextsize - mod;
4331 if (del.br_blockcount > mod) {
4332 del.br_blockcount -= mod;
4333 del.br_startoff += mod;
4334 del.br_startblock += mod;
4335 } else if ((del.br_startoff == start &&
4336 (del.br_state == XFS_EXT_UNWRITTEN ||
4337 xfs_trans_get_block_res(tp) == 0)) ||
4338 !XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb)) {
4339 /*
4340 * Can't make it unwritten. There isn't
4341 * a full extent here so just skip it.
4342 */
4343 ASSERT(bno >= del.br_blockcount);
4344 bno -= del.br_blockcount;
4345 if (bno < got.br_startoff) {
4346 if (--lastx >= 0)
4347 xfs_bmbt_get_all(--ep, &got);
4348 }
4349 continue;
4350 } else if (del.br_state == XFS_EXT_UNWRITTEN) {
4351 /*
4352 * This one is already unwritten.
4353 * It must have a written left neighbor.
4354 * Unwrite the killed part of that one and
4355 * try again.
4356 */
4357 ASSERT(lastx > 0);
4358 xfs_bmbt_get_all(ep - 1, &prev);
4359 ASSERT(prev.br_state == XFS_EXT_NORM);
4360 ASSERT(!ISNULLSTARTBLOCK(prev.br_startblock));
4361 ASSERT(del.br_startblock ==
4362 prev.br_startblock + prev.br_blockcount);
4363 if (prev.br_startoff < start) {
4364 mod = start - prev.br_startoff;
4365 prev.br_blockcount -= mod;
4366 prev.br_startblock += mod;
4367 prev.br_startoff = start;
4368 }
4369 prev.br_state = XFS_EXT_UNWRITTEN;
4370 error = xfs_bmap_add_extent(ip, lastx - 1, &cur,
4371 &prev, firstblock, flist, &logflags,
4372 XFS_DATA_FORK, 0);
4373 if (error)
4374 goto error0;
4375 goto nodelete;
4376 } else {
4377 ASSERT(del.br_state == XFS_EXT_NORM);
4378 del.br_state = XFS_EXT_UNWRITTEN;
4379 error = xfs_bmap_add_extent(ip, lastx, &cur,
4380 &del, firstblock, flist, &logflags,
4381 XFS_DATA_FORK, 0);
4382 if (error)
4383 goto error0;
4384 goto nodelete;
4385 }
4386 }
4387 if (wasdel) {
4388 ASSERT(STARTBLOCKVAL(del.br_startblock) > 0);
4389 xfs_mod_incore_sb(mp, XFS_SBS_FDBLOCKS,
4390 (int)del.br_blockcount, rsvd);
eae766ca
NS
4391 /* Unreserve our quota space */
4392 XFS_TRANS_RESERVE_QUOTA_NBLKS(
4393 mp, NULL, ip, -((long)del.br_blockcount), 0,
4394 isrt ? XFS_QMOPT_RES_RTBLKS :
4395 XFS_QMOPT_RES_REGBLKS);
2bd0ea18
NS
4396 ip->i_delayed_blks -= del.br_blockcount;
4397 if (cur)
4398 cur->bc_private.b.flags |=
4399 XFS_BTCUR_BPRV_WASDEL;
4400 } else if (cur)
4401 cur->bc_private.b.flags &= ~XFS_BTCUR_BPRV_WASDEL;
4402 /*
4403 * If it's the case where the directory code is running
5000d01d 4404 * with no block reservation, and the deleted block is in
2bd0ea18
NS
4405 * the middle of its extent, and the resulting insert
4406 * of an extent would cause transformation to btree format,
4407 * then reject it. The calling code will then swap
4408 * blocks around instead.
4409 * We have to do this now, rather than waiting for the
4410 * conversion to btree format, since the transaction
4411 * will be dirty.
4412 */
4413 if (!wasdel && xfs_trans_get_block_res(tp) == 0 &&
4414 XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4415 XFS_IFORK_NEXTENTS(ip, whichfork) >= ifp->if_ext_max &&
4416 del.br_startoff > got.br_startoff &&
5000d01d 4417 del.br_startoff + del.br_blockcount <
2bd0ea18
NS
4418 got.br_startoff + got.br_blockcount) {
4419 error = XFS_ERROR(ENOSPC);
4420 goto error0;
4421 }
4422 error = xfs_bmap_del_extent(ip, tp, lastx, flist, cur, &del,
4ca431fc 4423 &tmp_logflags, whichfork, rsvd);
2bd0ea18
NS
4424 logflags |= tmp_logflags;
4425 if (error)
4426 goto error0;
4427 bno = del.br_startoff - 1;
4428nodelete:
4429 lastx = ifp->if_lastex;
4430 /*
4431 * If not done go on to the next (previous) record.
4432 * Reset ep in case the extents array was re-alloced.
4433 */
4434 ep = &ifp->if_u1.if_extents[lastx];
4435 if (bno != (xfs_fileoff_t)-1 && bno >= start) {
4436 if (lastx >= XFS_IFORK_NEXTENTS(ip, whichfork) ||
4437 xfs_bmbt_get_startoff(ep) > bno) {
4438 lastx--;
4439 ep--;
4440 }
4441 if (lastx >= 0)
4442 xfs_bmbt_get_all(ep, &got);
4443 extno++;
4444 }
4445 }
4446 ifp->if_lastex = lastx;
4447 *done = bno == (xfs_fileoff_t)-1 || bno < start || lastx < 0;
4448 ASSERT(ifp->if_ext_max ==
4449 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4450 /*
4451 * Convert to a btree if necessary.
4452 */
4453 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_EXTENTS &&
4454 XFS_IFORK_NEXTENTS(ip, whichfork) > ifp->if_ext_max) {
4455 ASSERT(cur == NULL);
4456 error = xfs_bmap_extents_to_btree(tp, ip, firstblock, flist,
4457 &cur, 0, &tmp_logflags, whichfork);
4458 logflags |= tmp_logflags;
4459 if (error)
4460 goto error0;
4461 }
4462 /*
4463 * transform from btree to extents, give it cur
4464 */
4465 else if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_BTREE &&
4466 XFS_IFORK_NEXTENTS(ip, whichfork) <= ifp->if_ext_max) {
4467 ASSERT(cur != NULL);
4468 error = xfs_bmap_btree_to_extents(tp, ip, cur, &tmp_logflags,
4ca431fc 4469 whichfork);
2bd0ea18
NS
4470 logflags |= tmp_logflags;
4471 if (error)
4472 goto error0;
4473 }
4474 /*
4475 * transform from extents to local?
4476 */
4477 ASSERT(ifp->if_ext_max ==
4478 XFS_IFORK_SIZE(ip, whichfork) / (uint)sizeof(xfs_bmbt_rec_t));
4479 error = 0;
4480error0:
4481 /*
4482 * Log everything. Do this after conversion, there's no point in
4483 * logging the extent list if we've converted to btree format.
4484 */
4485 if ((logflags & XFS_ILOG_FEXT(whichfork)) &&
4486 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_EXTENTS)
4487 logflags &= ~XFS_ILOG_FEXT(whichfork);
4488 else if ((logflags & XFS_ILOG_FBROOT(whichfork)) &&
4489 XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)
4490 logflags &= ~XFS_ILOG_FBROOT(whichfork);
4491 /*
5000d01d 4492 * Log inode even in the error case, if the transaction
2bd0ea18
NS
4493 * is dirty we'll need to shut down the filesystem.
4494 */
4495 if (logflags)
4496 xfs_trans_log_inode(tp, ip, logflags);
4497 if (cur) {
4498 if (!error) {
4499 *firstblock = cur->bc_private.b.firstblock;
4500 cur->bc_private.b.allocated = 0;
4501 }
4502 xfs_btree_del_cursor(cur,
4503 error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
4504 }
4505 return error;
4506}
4507
4508/*
5000d01d
SL
4509 * Check the last inode extent to determine whether this allocation will result
4510 * in blocks being allocated at the end of the file. When we allocate new data
2bd0ea18
NS
4511 * blocks at the end of the file which do not start at the previous data block,
4512 * we will try to align the new blocks at stripe unit boundaries.
4513 */
4514int /* error */
4515xfs_bmap_isaeof(
4516 xfs_inode_t *ip, /* incore inode pointer */
dfc130f3
RC
4517 xfs_fileoff_t off, /* file offset in fsblocks */
4518 int whichfork, /* data or attribute fork */
85a875e9 4519 char *aeof) /* return value */
2bd0ea18
NS
4520{
4521 int error; /* error return value */
4522 xfs_ifork_t *ifp; /* inode fork pointer */
4523 xfs_bmbt_rec_t *lastrec; /* extent list entry pointer */
4524 xfs_extnum_t nextents; /* size of extent list */
dfc130f3 4525 xfs_bmbt_irec_t s; /* expanded extent list entry */
2bd0ea18
NS
4526
4527 ASSERT(whichfork == XFS_DATA_FORK);
4528 ifp = XFS_IFORK_PTR(ip, whichfork);
4529 if (!(ifp->if_flags & XFS_IFEXTENTS) &&
4530 (error = xfs_iread_extents(NULL, ip, whichfork)))
4531 return error;
4532 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
4533 if (nextents == 0) {
4534 *aeof = 1;
4535 return 0;
4536 }
4537 /*
4538 * Go to the last extent
4539 */
4540 lastrec = &ifp->if_u1.if_extents[nextents - 1];
4541 xfs_bmbt_get_all(lastrec, &s);
4542 /*
4543 * Check we are allocating in the last extent (for delayed allocations)
4544 * or past the last extent for non-delayed allocations.
5000d01d 4545 */
2bd0ea18
NS
4546 *aeof = (off >= s.br_startoff &&
4547 off < s.br_startoff + s.br_blockcount &&
4548 ISNULLSTARTBLOCK(s.br_startblock)) ||
4549 off >= s.br_startoff + s.br_blockcount;
4550 return 0;
4551}