]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxfs/xfs_inode_fork.c
xfs: fix the wrong new_size/rnew_size at xfs_iext_realloc_direct()
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_inode_fork.c
1 /*
2 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18 #include <xfs.h>
19
20 kmem_zone_t *xfs_ifork_zone;
21
22 STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
23 STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
24 STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
25
26 #ifdef DEBUG
27 /*
28 * Make sure that the extents in the given memory buffer
29 * are valid.
30 */
31 void
32 xfs_validate_extents(
33 xfs_ifork_t *ifp,
34 int nrecs,
35 xfs_exntfmt_t fmt)
36 {
37 xfs_bmbt_irec_t irec;
38 xfs_bmbt_rec_host_t rec;
39 int i;
40
41 for (i = 0; i < nrecs; i++) {
42 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
43 rec.l0 = get_unaligned(&ep->l0);
44 rec.l1 = get_unaligned(&ep->l1);
45 xfs_bmbt_get_all(&rec, &irec);
46 if (fmt == XFS_EXTFMT_NOSTATE)
47 ASSERT(irec.br_state == XFS_EXT_NORM);
48 }
49 }
50 #else /* DEBUG */
51 #define xfs_validate_extents(ifp, nrecs, fmt)
52 #endif /* DEBUG */
53
54
55 /*
56 * Move inode type and inode format specific information from the
57 * on-disk inode to the in-core inode. For fifos, devs, and sockets
58 * this means set if_rdev to the proper value. For files, directories,
59 * and symlinks this means to bring in the in-line data or extent
60 * pointers. For a file in B-tree format, only the root is immediately
61 * brought in-core. The rest will be in-lined in if_extents when it
62 * is first referenced (see xfs_iread_extents()).
63 */
64 int
65 xfs_iformat_fork(
66 xfs_inode_t *ip,
67 xfs_dinode_t *dip)
68 {
69 xfs_attr_shortform_t *atp;
70 int size;
71 int error = 0;
72 xfs_fsize_t di_size;
73
74 if (unlikely(be32_to_cpu(dip->di_nextents) +
75 be16_to_cpu(dip->di_anextents) >
76 be64_to_cpu(dip->di_nblocks))) {
77 xfs_warn(ip->i_mount,
78 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
79 (unsigned long long)ip->i_ino,
80 (int)(be32_to_cpu(dip->di_nextents) +
81 be16_to_cpu(dip->di_anextents)),
82 (unsigned long long)
83 be64_to_cpu(dip->di_nblocks));
84 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
85 ip->i_mount, dip);
86 return XFS_ERROR(EFSCORRUPTED);
87 }
88
89 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
90 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
91 (unsigned long long)ip->i_ino,
92 dip->di_forkoff);
93 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
94 ip->i_mount, dip);
95 return XFS_ERROR(EFSCORRUPTED);
96 }
97
98 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
99 !ip->i_mount->m_rtdev_targp)) {
100 xfs_warn(ip->i_mount,
101 "corrupt dinode %Lu, has realtime flag set.",
102 ip->i_ino);
103 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
104 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
105 return XFS_ERROR(EFSCORRUPTED);
106 }
107
108 switch (ip->i_d.di_mode & S_IFMT) {
109 case S_IFIFO:
110 case S_IFCHR:
111 case S_IFBLK:
112 case S_IFSOCK:
113 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
114 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
115 ip->i_mount, dip);
116 return XFS_ERROR(EFSCORRUPTED);
117 }
118 ip->i_d.di_size = 0;
119 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
120 break;
121
122 case S_IFREG:
123 case S_IFLNK:
124 case S_IFDIR:
125 switch (dip->di_format) {
126 case XFS_DINODE_FMT_LOCAL:
127 /*
128 * no local regular files yet
129 */
130 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
131 xfs_warn(ip->i_mount,
132 "corrupt inode %Lu (local format for regular file).",
133 (unsigned long long) ip->i_ino);
134 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
135 XFS_ERRLEVEL_LOW,
136 ip->i_mount, dip);
137 return XFS_ERROR(EFSCORRUPTED);
138 }
139
140 di_size = be64_to_cpu(dip->di_size);
141 if (unlikely(di_size < 0 ||
142 di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
143 xfs_warn(ip->i_mount,
144 "corrupt inode %Lu (bad size %Ld for local inode).",
145 (unsigned long long) ip->i_ino,
146 (long long) di_size);
147 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
148 XFS_ERRLEVEL_LOW,
149 ip->i_mount, dip);
150 return XFS_ERROR(EFSCORRUPTED);
151 }
152
153 size = (int)di_size;
154 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
155 break;
156 case XFS_DINODE_FMT_EXTENTS:
157 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
158 break;
159 case XFS_DINODE_FMT_BTREE:
160 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
161 break;
162 default:
163 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
164 ip->i_mount);
165 return XFS_ERROR(EFSCORRUPTED);
166 }
167 break;
168
169 default:
170 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
171 return XFS_ERROR(EFSCORRUPTED);
172 }
173 if (error) {
174 return error;
175 }
176 if (!XFS_DFORK_Q(dip))
177 return 0;
178
179 ASSERT(ip->i_afp == NULL);
180 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
181
182 switch (dip->di_aformat) {
183 case XFS_DINODE_FMT_LOCAL:
184 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
185 size = be16_to_cpu(atp->hdr.totsize);
186
187 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
188 xfs_warn(ip->i_mount,
189 "corrupt inode %Lu (bad attr fork size %Ld).",
190 (unsigned long long) ip->i_ino,
191 (long long) size);
192 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
193 XFS_ERRLEVEL_LOW,
194 ip->i_mount, dip);
195 return XFS_ERROR(EFSCORRUPTED);
196 }
197
198 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
199 break;
200 case XFS_DINODE_FMT_EXTENTS:
201 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
202 break;
203 case XFS_DINODE_FMT_BTREE:
204 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
205 break;
206 default:
207 error = XFS_ERROR(EFSCORRUPTED);
208 break;
209 }
210 if (error) {
211 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
212 ip->i_afp = NULL;
213 xfs_idestroy_fork(ip, XFS_DATA_FORK);
214 }
215 return error;
216 }
217
218 /*
219 * The file is in-lined in the on-disk inode.
220 * If it fits into if_inline_data, then copy
221 * it there, otherwise allocate a buffer for it
222 * and copy the data there. Either way, set
223 * if_data to point at the data.
224 * If we allocate a buffer for the data, make
225 * sure that its size is a multiple of 4 and
226 * record the real size in i_real_bytes.
227 */
228 STATIC int
229 xfs_iformat_local(
230 xfs_inode_t *ip,
231 xfs_dinode_t *dip,
232 int whichfork,
233 int size)
234 {
235 xfs_ifork_t *ifp;
236 int real_size;
237
238 /*
239 * If the size is unreasonable, then something
240 * is wrong and we just bail out rather than crash in
241 * kmem_alloc() or memcpy() below.
242 */
243 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
244 xfs_warn(ip->i_mount,
245 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
246 (unsigned long long) ip->i_ino, size,
247 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
248 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
249 ip->i_mount, dip);
250 return XFS_ERROR(EFSCORRUPTED);
251 }
252 ifp = XFS_IFORK_PTR(ip, whichfork);
253 real_size = 0;
254 if (size == 0)
255 ifp->if_u1.if_data = NULL;
256 else if (size <= sizeof(ifp->if_u2.if_inline_data))
257 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
258 else {
259 real_size = roundup(size, 4);
260 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
261 }
262 ifp->if_bytes = size;
263 ifp->if_real_bytes = real_size;
264 if (size)
265 memcpy(ifp->if_u1.if_data, XFS_DFORK_PTR(dip, whichfork), size);
266 ifp->if_flags &= ~XFS_IFEXTENTS;
267 ifp->if_flags |= XFS_IFINLINE;
268 return 0;
269 }
270
271 /*
272 * The file consists of a set of extents all
273 * of which fit into the on-disk inode.
274 * If there are few enough extents to fit into
275 * the if_inline_ext, then copy them there.
276 * Otherwise allocate a buffer for them and copy
277 * them into it. Either way, set if_extents
278 * to point at the extents.
279 */
280 STATIC int
281 xfs_iformat_extents(
282 xfs_inode_t *ip,
283 xfs_dinode_t *dip,
284 int whichfork)
285 {
286 xfs_bmbt_rec_t *dp;
287 xfs_ifork_t *ifp;
288 int nex;
289 int size;
290 int i;
291
292 ifp = XFS_IFORK_PTR(ip, whichfork);
293 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
294 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
295
296 /*
297 * If the number of extents is unreasonable, then something
298 * is wrong and we just bail out rather than crash in
299 * kmem_alloc() or memcpy() below.
300 */
301 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
302 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
303 (unsigned long long) ip->i_ino, nex);
304 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
305 ip->i_mount, dip);
306 return XFS_ERROR(EFSCORRUPTED);
307 }
308
309 ifp->if_real_bytes = 0;
310 if (nex == 0)
311 ifp->if_u1.if_extents = NULL;
312 else if (nex <= XFS_INLINE_EXTS)
313 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
314 else
315 xfs_iext_add(ifp, 0, nex);
316
317 ifp->if_bytes = size;
318 if (size) {
319 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
320 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
321 for (i = 0; i < nex; i++, dp++) {
322 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
323 ep->l0 = get_unaligned_be64(&dp->l0);
324 ep->l1 = get_unaligned_be64(&dp->l1);
325 }
326 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
327 if (whichfork != XFS_DATA_FORK ||
328 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
329 if (unlikely(xfs_check_nostate_extents(
330 ifp, 0, nex))) {
331 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
332 XFS_ERRLEVEL_LOW,
333 ip->i_mount);
334 return XFS_ERROR(EFSCORRUPTED);
335 }
336 }
337 ifp->if_flags |= XFS_IFEXTENTS;
338 return 0;
339 }
340
341 /*
342 * The file has too many extents to fit into
343 * the inode, so they are in B-tree format.
344 * Allocate a buffer for the root of the B-tree
345 * and copy the root into it. The i_extents
346 * field will remain NULL until all of the
347 * extents are read in (when they are needed).
348 */
349 STATIC int
350 xfs_iformat_btree(
351 xfs_inode_t *ip,
352 xfs_dinode_t *dip,
353 int whichfork)
354 {
355 struct xfs_mount *mp = ip->i_mount;
356 xfs_bmdr_block_t *dfp;
357 xfs_ifork_t *ifp;
358 /* REFERENCED */
359 int nrecs;
360 int size;
361
362 ifp = XFS_IFORK_PTR(ip, whichfork);
363 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
364 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
365 nrecs = be16_to_cpu(dfp->bb_numrecs);
366
367 /*
368 * blow out if -- fork has less extents than can fit in
369 * fork (fork shouldn't be a btree format), root btree
370 * block has more records than can fit into the fork,
371 * or the number of extents is greater than the number of
372 * blocks.
373 */
374 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
375 XFS_IFORK_MAXEXT(ip, whichfork) ||
376 XFS_BMDR_SPACE_CALC(nrecs) >
377 XFS_DFORK_SIZE(dip, mp, whichfork) ||
378 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
379 xfs_warn(mp, "corrupt inode %Lu (btree).",
380 (unsigned long long) ip->i_ino);
381 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
382 mp, dip);
383 return XFS_ERROR(EFSCORRUPTED);
384 }
385
386 ifp->if_broot_bytes = size;
387 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
388 ASSERT(ifp->if_broot != NULL);
389 /*
390 * Copy and convert from the on-disk structure
391 * to the in-memory structure.
392 */
393 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
394 ifp->if_broot, size);
395 ifp->if_flags &= ~XFS_IFEXTENTS;
396 ifp->if_flags |= XFS_IFBROOT;
397
398 return 0;
399 }
400
401 /*
402 * Read in extents from a btree-format inode.
403 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
404 */
405 int
406 xfs_iread_extents(
407 xfs_trans_t *tp,
408 xfs_inode_t *ip,
409 int whichfork)
410 {
411 int error;
412 xfs_ifork_t *ifp;
413 xfs_extnum_t nextents;
414
415 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
416 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
417 ip->i_mount);
418 return XFS_ERROR(EFSCORRUPTED);
419 }
420 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
421 ifp = XFS_IFORK_PTR(ip, whichfork);
422
423 /*
424 * We know that the size is valid (it's checked in iformat_btree)
425 */
426 ifp->if_bytes = ifp->if_real_bytes = 0;
427 ifp->if_flags |= XFS_IFEXTENTS;
428 xfs_iext_add(ifp, 0, nextents);
429 error = xfs_bmap_read_extents(tp, ip, whichfork);
430 if (error) {
431 xfs_iext_destroy(ifp);
432 ifp->if_flags &= ~XFS_IFEXTENTS;
433 return error;
434 }
435 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
436 return 0;
437 }
438 /*
439 * Reallocate the space for if_broot based on the number of records
440 * being added or deleted as indicated in rec_diff. Move the records
441 * and pointers in if_broot to fit the new size. When shrinking this
442 * will eliminate holes between the records and pointers created by
443 * the caller. When growing this will create holes to be filled in
444 * by the caller.
445 *
446 * The caller must not request to add more records than would fit in
447 * the on-disk inode root. If the if_broot is currently NULL, then
448 * if we are adding records, one will be allocated. The caller must also
449 * not request that the number of records go below zero, although
450 * it can go to zero.
451 *
452 * ip -- the inode whose if_broot area is changing
453 * ext_diff -- the change in the number of records, positive or negative,
454 * requested for the if_broot array.
455 */
456 void
457 xfs_iroot_realloc(
458 xfs_inode_t *ip,
459 int rec_diff,
460 int whichfork)
461 {
462 struct xfs_mount *mp = ip->i_mount;
463 int cur_max;
464 xfs_ifork_t *ifp;
465 struct xfs_btree_block *new_broot;
466 int new_max;
467 size_t new_size;
468 char *np;
469 char *op;
470
471 /*
472 * Handle the degenerate case quietly.
473 */
474 if (rec_diff == 0) {
475 return;
476 }
477
478 ifp = XFS_IFORK_PTR(ip, whichfork);
479 if (rec_diff > 0) {
480 /*
481 * If there wasn't any memory allocated before, just
482 * allocate it now and get out.
483 */
484 if (ifp->if_broot_bytes == 0) {
485 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
486 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
487 ifp->if_broot_bytes = (int)new_size;
488 return;
489 }
490
491 /*
492 * If there is already an existing if_broot, then we need
493 * to realloc() it and shift the pointers to their new
494 * location. The records don't change location because
495 * they are kept butted up against the btree block header.
496 */
497 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
498 new_max = cur_max + rec_diff;
499 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
500 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
501 XFS_BMAP_BROOT_SPACE_CALC(mp, cur_max),
502 KM_SLEEP | KM_NOFS);
503 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
504 ifp->if_broot_bytes);
505 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
506 (int)new_size);
507 ifp->if_broot_bytes = (int)new_size;
508 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
509 XFS_IFORK_SIZE(ip, whichfork));
510 memmove(np, op, cur_max * (uint)sizeof(xfs_dfsbno_t));
511 return;
512 }
513
514 /*
515 * rec_diff is less than 0. In this case, we are shrinking the
516 * if_broot buffer. It must already exist. If we go to zero
517 * records, just get rid of the root and clear the status bit.
518 */
519 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
520 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
521 new_max = cur_max + rec_diff;
522 ASSERT(new_max >= 0);
523 if (new_max > 0)
524 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
525 else
526 new_size = 0;
527 if (new_size > 0) {
528 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
529 /*
530 * First copy over the btree block header.
531 */
532 memcpy(new_broot, ifp->if_broot,
533 XFS_BMBT_BLOCK_LEN(ip->i_mount));
534 } else {
535 new_broot = NULL;
536 ifp->if_flags &= ~XFS_IFBROOT;
537 }
538
539 /*
540 * Only copy the records and pointers if there are any.
541 */
542 if (new_max > 0) {
543 /*
544 * First copy the records.
545 */
546 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
547 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
548 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
549
550 /*
551 * Then copy the pointers.
552 */
553 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
554 ifp->if_broot_bytes);
555 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
556 (int)new_size);
557 memcpy(np, op, new_max * (uint)sizeof(xfs_dfsbno_t));
558 }
559 kmem_free(ifp->if_broot);
560 ifp->if_broot = new_broot;
561 ifp->if_broot_bytes = (int)new_size;
562 if (ifp->if_broot)
563 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
564 XFS_IFORK_SIZE(ip, whichfork));
565 return;
566 }
567
568
569 /*
570 * This is called when the amount of space needed for if_data
571 * is increased or decreased. The change in size is indicated by
572 * the number of bytes that need to be added or deleted in the
573 * byte_diff parameter.
574 *
575 * If the amount of space needed has decreased below the size of the
576 * inline buffer, then switch to using the inline buffer. Otherwise,
577 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
578 * to what is needed.
579 *
580 * ip -- the inode whose if_data area is changing
581 * byte_diff -- the change in the number of bytes, positive or negative,
582 * requested for the if_data array.
583 */
584 void
585 xfs_idata_realloc(
586 xfs_inode_t *ip,
587 int byte_diff,
588 int whichfork)
589 {
590 xfs_ifork_t *ifp;
591 int new_size;
592 int real_size;
593
594 if (byte_diff == 0) {
595 return;
596 }
597
598 ifp = XFS_IFORK_PTR(ip, whichfork);
599 new_size = (int)ifp->if_bytes + byte_diff;
600 ASSERT(new_size >= 0);
601
602 if (new_size == 0) {
603 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
604 kmem_free(ifp->if_u1.if_data);
605 }
606 ifp->if_u1.if_data = NULL;
607 real_size = 0;
608 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
609 /*
610 * If the valid extents/data can fit in if_inline_ext/data,
611 * copy them from the malloc'd vector and free it.
612 */
613 if (ifp->if_u1.if_data == NULL) {
614 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
615 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
616 ASSERT(ifp->if_real_bytes != 0);
617 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
618 new_size);
619 kmem_free(ifp->if_u1.if_data);
620 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
621 }
622 real_size = 0;
623 } else {
624 /*
625 * Stuck with malloc/realloc.
626 * For inline data, the underlying buffer must be
627 * a multiple of 4 bytes in size so that it can be
628 * logged and stay on word boundaries. We enforce
629 * that here.
630 */
631 real_size = roundup(new_size, 4);
632 if (ifp->if_u1.if_data == NULL) {
633 ASSERT(ifp->if_real_bytes == 0);
634 ifp->if_u1.if_data = kmem_alloc(real_size,
635 KM_SLEEP | KM_NOFS);
636 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
637 /*
638 * Only do the realloc if the underlying size
639 * is really changing.
640 */
641 if (ifp->if_real_bytes != real_size) {
642 ifp->if_u1.if_data =
643 kmem_realloc(ifp->if_u1.if_data,
644 real_size,
645 ifp->if_real_bytes,
646 KM_SLEEP | KM_NOFS);
647 }
648 } else {
649 ASSERT(ifp->if_real_bytes == 0);
650 ifp->if_u1.if_data = kmem_alloc(real_size,
651 KM_SLEEP | KM_NOFS);
652 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
653 ifp->if_bytes);
654 }
655 }
656 ifp->if_real_bytes = real_size;
657 ifp->if_bytes = new_size;
658 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
659 }
660
661 void
662 xfs_idestroy_fork(
663 xfs_inode_t *ip,
664 int whichfork)
665 {
666 xfs_ifork_t *ifp;
667
668 ifp = XFS_IFORK_PTR(ip, whichfork);
669 if (ifp->if_broot != NULL) {
670 kmem_free(ifp->if_broot);
671 ifp->if_broot = NULL;
672 }
673
674 /*
675 * If the format is local, then we can't have an extents
676 * array so just look for an inline data array. If we're
677 * not local then we may or may not have an extents list,
678 * so check and free it up if we do.
679 */
680 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
681 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
682 (ifp->if_u1.if_data != NULL)) {
683 ASSERT(ifp->if_real_bytes != 0);
684 kmem_free(ifp->if_u1.if_data);
685 ifp->if_u1.if_data = NULL;
686 ifp->if_real_bytes = 0;
687 }
688 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
689 ((ifp->if_flags & XFS_IFEXTIREC) ||
690 ((ifp->if_u1.if_extents != NULL) &&
691 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
692 ASSERT(ifp->if_real_bytes != 0);
693 xfs_iext_destroy(ifp);
694 }
695 ASSERT(ifp->if_u1.if_extents == NULL ||
696 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
697 ASSERT(ifp->if_real_bytes == 0);
698 if (whichfork == XFS_ATTR_FORK) {
699 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
700 ip->i_afp = NULL;
701 }
702 }
703
704 /*
705 * xfs_iextents_copy()
706 *
707 * This is called to copy the REAL extents (as opposed to the delayed
708 * allocation extents) from the inode into the given buffer. It
709 * returns the number of bytes copied into the buffer.
710 *
711 * If there are no delayed allocation extents, then we can just
712 * memcpy() the extents into the buffer. Otherwise, we need to
713 * examine each extent in turn and skip those which are delayed.
714 */
715 int
716 xfs_iextents_copy(
717 xfs_inode_t *ip,
718 xfs_bmbt_rec_t *dp,
719 int whichfork)
720 {
721 int copied;
722 int i;
723 xfs_ifork_t *ifp;
724 int nrecs;
725 xfs_fsblock_t start_block;
726
727 ifp = XFS_IFORK_PTR(ip, whichfork);
728 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
729 ASSERT(ifp->if_bytes > 0);
730
731 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
732 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
733 ASSERT(nrecs > 0);
734
735 /*
736 * There are some delayed allocation extents in the
737 * inode, so copy the extents one at a time and skip
738 * the delayed ones. There must be at least one
739 * non-delayed extent.
740 */
741 copied = 0;
742 for (i = 0; i < nrecs; i++) {
743 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
744 start_block = xfs_bmbt_get_startblock(ep);
745 if (isnullstartblock(start_block)) {
746 /*
747 * It's a delayed allocation extent, so skip it.
748 */
749 continue;
750 }
751
752 /* Translate to on disk format */
753 put_unaligned_be64(ep->l0, &dp->l0);
754 put_unaligned_be64(ep->l1, &dp->l1);
755 dp++;
756 copied++;
757 }
758 ASSERT(copied != 0);
759 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
760
761 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
762 }
763
764 /*
765 * Each of the following cases stores data into the same region
766 * of the on-disk inode, so only one of them can be valid at
767 * any given time. While it is possible to have conflicting formats
768 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
769 * in EXTENTS format, this can only happen when the fork has
770 * changed formats after being modified but before being flushed.
771 * In these cases, the format always takes precedence, because the
772 * format indicates the current state of the fork.
773 */
774 void
775 xfs_iflush_fork(
776 xfs_inode_t *ip,
777 xfs_dinode_t *dip,
778 xfs_inode_log_item_t *iip,
779 int whichfork,
780 xfs_buf_t *bp)
781 {
782 char *cp;
783 xfs_ifork_t *ifp;
784 xfs_mount_t *mp;
785 static const short brootflag[2] =
786 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
787 static const short dataflag[2] =
788 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
789 static const short extflag[2] =
790 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
791
792 if (!iip)
793 return;
794 ifp = XFS_IFORK_PTR(ip, whichfork);
795 /*
796 * This can happen if we gave up in iformat in an error path,
797 * for the attribute fork.
798 */
799 if (!ifp) {
800 ASSERT(whichfork == XFS_ATTR_FORK);
801 return;
802 }
803 cp = XFS_DFORK_PTR(dip, whichfork);
804 mp = ip->i_mount;
805 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
806 case XFS_DINODE_FMT_LOCAL:
807 if ((iip->ili_fields & dataflag[whichfork]) &&
808 (ifp->if_bytes > 0)) {
809 ASSERT(ifp->if_u1.if_data != NULL);
810 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
811 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
812 }
813 break;
814
815 case XFS_DINODE_FMT_EXTENTS:
816 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
817 !(iip->ili_fields & extflag[whichfork]));
818 if ((iip->ili_fields & extflag[whichfork]) &&
819 (ifp->if_bytes > 0)) {
820 ASSERT(xfs_iext_get_ext(ifp, 0));
821 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
822 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
823 whichfork);
824 }
825 break;
826
827 case XFS_DINODE_FMT_BTREE:
828 if ((iip->ili_fields & brootflag[whichfork]) &&
829 (ifp->if_broot_bytes > 0)) {
830 ASSERT(ifp->if_broot != NULL);
831 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
832 XFS_IFORK_SIZE(ip, whichfork));
833 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
834 (xfs_bmdr_block_t *)cp,
835 XFS_DFORK_SIZE(dip, mp, whichfork));
836 }
837 break;
838
839 case XFS_DINODE_FMT_DEV:
840 if (iip->ili_fields & XFS_ILOG_DEV) {
841 ASSERT(whichfork == XFS_DATA_FORK);
842 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
843 }
844 break;
845
846 case XFS_DINODE_FMT_UUID:
847 if (iip->ili_fields & XFS_ILOG_UUID) {
848 ASSERT(whichfork == XFS_DATA_FORK);
849 memcpy(XFS_DFORK_DPTR(dip),
850 &ip->i_df.if_u2.if_uuid,
851 sizeof(uuid_t));
852 }
853 break;
854
855 default:
856 ASSERT(0);
857 break;
858 }
859 }
860
861 /*
862 * Return a pointer to the extent record at file index idx.
863 */
864 xfs_bmbt_rec_host_t *
865 xfs_iext_get_ext(
866 xfs_ifork_t *ifp, /* inode fork pointer */
867 xfs_extnum_t idx) /* index of target extent */
868 {
869 ASSERT(idx >= 0);
870 ASSERT(idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
871
872 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
873 return ifp->if_u1.if_ext_irec->er_extbuf;
874 } else if (ifp->if_flags & XFS_IFEXTIREC) {
875 xfs_ext_irec_t *erp; /* irec pointer */
876 int erp_idx = 0; /* irec index */
877 xfs_extnum_t page_idx = idx; /* ext index in target list */
878
879 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
880 return &erp->er_extbuf[page_idx];
881 } else if (ifp->if_bytes) {
882 return &ifp->if_u1.if_extents[idx];
883 } else {
884 return NULL;
885 }
886 }
887
888 /*
889 * Insert new item(s) into the extent records for incore inode
890 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
891 */
892 void
893 xfs_iext_insert(
894 xfs_inode_t *ip, /* incore inode pointer */
895 xfs_extnum_t idx, /* starting index of new items */
896 xfs_extnum_t count, /* number of inserted items */
897 xfs_bmbt_irec_t *new, /* items to insert */
898 int state) /* type of extent conversion */
899 {
900 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
901 xfs_extnum_t i; /* extent record index */
902
903 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
904
905 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
906 xfs_iext_add(ifp, idx, count);
907 for (i = idx; i < idx + count; i++, new++)
908 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
909 }
910
911 /*
912 * This is called when the amount of space required for incore file
913 * extents needs to be increased. The ext_diff parameter stores the
914 * number of new extents being added and the idx parameter contains
915 * the extent index where the new extents will be added. If the new
916 * extents are being appended, then we just need to (re)allocate and
917 * initialize the space. Otherwise, if the new extents are being
918 * inserted into the middle of the existing entries, a bit more work
919 * is required to make room for the new extents to be inserted. The
920 * caller is responsible for filling in the new extent entries upon
921 * return.
922 */
923 void
924 xfs_iext_add(
925 xfs_ifork_t *ifp, /* inode fork pointer */
926 xfs_extnum_t idx, /* index to begin adding exts */
927 int ext_diff) /* number of extents to add */
928 {
929 int byte_diff; /* new bytes being added */
930 int new_size; /* size of extents after adding */
931 xfs_extnum_t nextents; /* number of extents in file */
932
933 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
934 ASSERT((idx >= 0) && (idx <= nextents));
935 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
936 new_size = ifp->if_bytes + byte_diff;
937 /*
938 * If the new number of extents (nextents + ext_diff)
939 * fits inside the inode, then continue to use the inline
940 * extent buffer.
941 */
942 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
943 if (idx < nextents) {
944 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
945 &ifp->if_u2.if_inline_ext[idx],
946 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
947 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
948 }
949 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
950 ifp->if_real_bytes = 0;
951 }
952 /*
953 * Otherwise use a linear (direct) extent list.
954 * If the extents are currently inside the inode,
955 * xfs_iext_realloc_direct will switch us from
956 * inline to direct extent allocation mode.
957 */
958 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
959 xfs_iext_realloc_direct(ifp, new_size);
960 if (idx < nextents) {
961 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
962 &ifp->if_u1.if_extents[idx],
963 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
964 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
965 }
966 }
967 /* Indirection array */
968 else {
969 xfs_ext_irec_t *erp;
970 int erp_idx = 0;
971 int page_idx = idx;
972
973 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
974 if (ifp->if_flags & XFS_IFEXTIREC) {
975 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
976 } else {
977 xfs_iext_irec_init(ifp);
978 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
979 erp = ifp->if_u1.if_ext_irec;
980 }
981 /* Extents fit in target extent page */
982 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
983 if (page_idx < erp->er_extcount) {
984 memmove(&erp->er_extbuf[page_idx + ext_diff],
985 &erp->er_extbuf[page_idx],
986 (erp->er_extcount - page_idx) *
987 sizeof(xfs_bmbt_rec_t));
988 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
989 }
990 erp->er_extcount += ext_diff;
991 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
992 }
993 /* Insert a new extent page */
994 else if (erp) {
995 xfs_iext_add_indirect_multi(ifp,
996 erp_idx, page_idx, ext_diff);
997 }
998 /*
999 * If extent(s) are being appended to the last page in
1000 * the indirection array and the new extent(s) don't fit
1001 * in the page, then erp is NULL and erp_idx is set to
1002 * the next index needed in the indirection array.
1003 */
1004 else {
1005 int count = ext_diff;
1006
1007 while (count) {
1008 erp = xfs_iext_irec_new(ifp, erp_idx);
1009 erp->er_extcount = count;
1010 count -= MIN(count, (int)XFS_LINEAR_EXTS);
1011 if (count) {
1012 erp_idx++;
1013 }
1014 }
1015 }
1016 }
1017 ifp->if_bytes = new_size;
1018 }
1019
1020 /*
1021 * This is called when incore extents are being added to the indirection
1022 * array and the new extents do not fit in the target extent list. The
1023 * erp_idx parameter contains the irec index for the target extent list
1024 * in the indirection array, and the idx parameter contains the extent
1025 * index within the list. The number of extents being added is stored
1026 * in the count parameter.
1027 *
1028 * |-------| |-------|
1029 * | | | | idx - number of extents before idx
1030 * | idx | | count |
1031 * | | | | count - number of extents being inserted at idx
1032 * |-------| |-------|
1033 * | count | | nex2 | nex2 - number of extents after idx + count
1034 * |-------| |-------|
1035 */
1036 void
1037 xfs_iext_add_indirect_multi(
1038 xfs_ifork_t *ifp, /* inode fork pointer */
1039 int erp_idx, /* target extent irec index */
1040 xfs_extnum_t idx, /* index within target list */
1041 int count) /* new extents being added */
1042 {
1043 int byte_diff; /* new bytes being added */
1044 xfs_ext_irec_t *erp; /* pointer to irec entry */
1045 xfs_extnum_t ext_diff; /* number of extents to add */
1046 xfs_extnum_t ext_cnt; /* new extents still needed */
1047 xfs_extnum_t nex2; /* extents after idx + count */
1048 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
1049 int nlists; /* number of irec's (lists) */
1050
1051 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1052 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1053 nex2 = erp->er_extcount - idx;
1054 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1055
1056 /*
1057 * Save second part of target extent list
1058 * (all extents past */
1059 if (nex2) {
1060 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1061 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
1062 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
1063 erp->er_extcount -= nex2;
1064 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
1065 memset(&erp->er_extbuf[idx], 0, byte_diff);
1066 }
1067
1068 /*
1069 * Add the new extents to the end of the target
1070 * list, then allocate new irec record(s) and
1071 * extent buffer(s) as needed to store the rest
1072 * of the new extents.
1073 */
1074 ext_cnt = count;
1075 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
1076 if (ext_diff) {
1077 erp->er_extcount += ext_diff;
1078 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1079 ext_cnt -= ext_diff;
1080 }
1081 while (ext_cnt) {
1082 erp_idx++;
1083 erp = xfs_iext_irec_new(ifp, erp_idx);
1084 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
1085 erp->er_extcount = ext_diff;
1086 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1087 ext_cnt -= ext_diff;
1088 }
1089
1090 /* Add nex2 extents back to indirection array */
1091 if (nex2) {
1092 xfs_extnum_t ext_avail;
1093 int i;
1094
1095 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1096 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
1097 i = 0;
1098 /*
1099 * If nex2 extents fit in the current page, append
1100 * nex2_ep after the new extents.
1101 */
1102 if (nex2 <= ext_avail) {
1103 i = erp->er_extcount;
1104 }
1105 /*
1106 * Otherwise, check if space is available in the
1107 * next page.
1108 */
1109 else if ((erp_idx < nlists - 1) &&
1110 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
1111 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
1112 erp_idx++;
1113 erp++;
1114 /* Create a hole for nex2 extents */
1115 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
1116 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
1117 }
1118 /*
1119 * Final choice, create a new extent page for
1120 * nex2 extents.
1121 */
1122 else {
1123 erp_idx++;
1124 erp = xfs_iext_irec_new(ifp, erp_idx);
1125 }
1126 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
1127 kmem_free(nex2_ep);
1128 erp->er_extcount += nex2;
1129 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
1130 }
1131 }
1132
1133 /*
1134 * This is called when the amount of space required for incore file
1135 * extents needs to be decreased. The ext_diff parameter stores the
1136 * number of extents to be removed and the idx parameter contains
1137 * the extent index where the extents will be removed from.
1138 *
1139 * If the amount of space needed has decreased below the linear
1140 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1141 * extent array. Otherwise, use kmem_realloc() to adjust the
1142 * size to what is needed.
1143 */
1144 void
1145 xfs_iext_remove(
1146 xfs_inode_t *ip, /* incore inode pointer */
1147 xfs_extnum_t idx, /* index to begin removing exts */
1148 int ext_diff, /* number of extents to remove */
1149 int state) /* type of extent conversion */
1150 {
1151 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
1152 xfs_extnum_t nextents; /* number of extents in file */
1153 int new_size; /* size of extents after removal */
1154
1155 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
1156
1157 ASSERT(ext_diff > 0);
1158 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1159 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
1160
1161 if (new_size == 0) {
1162 xfs_iext_destroy(ifp);
1163 } else if (ifp->if_flags & XFS_IFEXTIREC) {
1164 xfs_iext_remove_indirect(ifp, idx, ext_diff);
1165 } else if (ifp->if_real_bytes) {
1166 xfs_iext_remove_direct(ifp, idx, ext_diff);
1167 } else {
1168 xfs_iext_remove_inline(ifp, idx, ext_diff);
1169 }
1170 ifp->if_bytes = new_size;
1171 }
1172
1173 /*
1174 * This removes ext_diff extents from the inline buffer, beginning
1175 * at extent index idx.
1176 */
1177 void
1178 xfs_iext_remove_inline(
1179 xfs_ifork_t *ifp, /* inode fork pointer */
1180 xfs_extnum_t idx, /* index to begin removing exts */
1181 int ext_diff) /* number of extents to remove */
1182 {
1183 int nextents; /* number of extents in file */
1184
1185 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1186 ASSERT(idx < XFS_INLINE_EXTS);
1187 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1188 ASSERT(((nextents - ext_diff) > 0) &&
1189 (nextents - ext_diff) < XFS_INLINE_EXTS);
1190
1191 if (idx + ext_diff < nextents) {
1192 memmove(&ifp->if_u2.if_inline_ext[idx],
1193 &ifp->if_u2.if_inline_ext[idx + ext_diff],
1194 (nextents - (idx + ext_diff)) *
1195 sizeof(xfs_bmbt_rec_t));
1196 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
1197 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1198 } else {
1199 memset(&ifp->if_u2.if_inline_ext[idx], 0,
1200 ext_diff * sizeof(xfs_bmbt_rec_t));
1201 }
1202 }
1203
1204 /*
1205 * This removes ext_diff extents from a linear (direct) extent list,
1206 * beginning at extent index idx. If the extents are being removed
1207 * from the end of the list (ie. truncate) then we just need to re-
1208 * allocate the list to remove the extra space. Otherwise, if the
1209 * extents are being removed from the middle of the existing extent
1210 * entries, then we first need to move the extent records beginning
1211 * at idx + ext_diff up in the list to overwrite the records being
1212 * removed, then remove the extra space via kmem_realloc.
1213 */
1214 void
1215 xfs_iext_remove_direct(
1216 xfs_ifork_t *ifp, /* inode fork pointer */
1217 xfs_extnum_t idx, /* index to begin removing exts */
1218 int ext_diff) /* number of extents to remove */
1219 {
1220 xfs_extnum_t nextents; /* number of extents in file */
1221 int new_size; /* size of extents after removal */
1222
1223 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1224 new_size = ifp->if_bytes -
1225 (ext_diff * sizeof(xfs_bmbt_rec_t));
1226 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1227
1228 if (new_size == 0) {
1229 xfs_iext_destroy(ifp);
1230 return;
1231 }
1232 /* Move extents up in the list (if needed) */
1233 if (idx + ext_diff < nextents) {
1234 memmove(&ifp->if_u1.if_extents[idx],
1235 &ifp->if_u1.if_extents[idx + ext_diff],
1236 (nextents - (idx + ext_diff)) *
1237 sizeof(xfs_bmbt_rec_t));
1238 }
1239 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
1240 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1241 /*
1242 * Reallocate the direct extent list. If the extents
1243 * will fit inside the inode then xfs_iext_realloc_direct
1244 * will switch from direct to inline extent allocation
1245 * mode for us.
1246 */
1247 xfs_iext_realloc_direct(ifp, new_size);
1248 ifp->if_bytes = new_size;
1249 }
1250
1251 /*
1252 * This is called when incore extents are being removed from the
1253 * indirection array and the extents being removed span multiple extent
1254 * buffers. The idx parameter contains the file extent index where we
1255 * want to begin removing extents, and the count parameter contains
1256 * how many extents need to be removed.
1257 *
1258 * |-------| |-------|
1259 * | nex1 | | | nex1 - number of extents before idx
1260 * |-------| | count |
1261 * | | | | count - number of extents being removed at idx
1262 * | count | |-------|
1263 * | | | nex2 | nex2 - number of extents after idx + count
1264 * |-------| |-------|
1265 */
1266 void
1267 xfs_iext_remove_indirect(
1268 xfs_ifork_t *ifp, /* inode fork pointer */
1269 xfs_extnum_t idx, /* index to begin removing extents */
1270 int count) /* number of extents to remove */
1271 {
1272 xfs_ext_irec_t *erp; /* indirection array pointer */
1273 int erp_idx = 0; /* indirection array index */
1274 xfs_extnum_t ext_cnt; /* extents left to remove */
1275 xfs_extnum_t ext_diff; /* extents to remove in current list */
1276 xfs_extnum_t nex1; /* number of extents before idx */
1277 xfs_extnum_t nex2; /* extents after idx + count */
1278 int page_idx = idx; /* index in target extent list */
1279
1280 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1281 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
1282 ASSERT(erp != NULL);
1283 nex1 = page_idx;
1284 ext_cnt = count;
1285 while (ext_cnt) {
1286 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
1287 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
1288 /*
1289 * Check for deletion of entire list;
1290 * xfs_iext_irec_remove() updates extent offsets.
1291 */
1292 if (ext_diff == erp->er_extcount) {
1293 xfs_iext_irec_remove(ifp, erp_idx);
1294 ext_cnt -= ext_diff;
1295 nex1 = 0;
1296 if (ext_cnt) {
1297 ASSERT(erp_idx < ifp->if_real_bytes /
1298 XFS_IEXT_BUFSZ);
1299 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1300 nex1 = 0;
1301 continue;
1302 } else {
1303 break;
1304 }
1305 }
1306 /* Move extents up (if needed) */
1307 if (nex2) {
1308 memmove(&erp->er_extbuf[nex1],
1309 &erp->er_extbuf[nex1 + ext_diff],
1310 nex2 * sizeof(xfs_bmbt_rec_t));
1311 }
1312 /* Zero out rest of page */
1313 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
1314 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
1315 /* Update remaining counters */
1316 erp->er_extcount -= ext_diff;
1317 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
1318 ext_cnt -= ext_diff;
1319 nex1 = 0;
1320 erp_idx++;
1321 erp++;
1322 }
1323 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
1324 xfs_iext_irec_compact(ifp);
1325 }
1326
1327 /*
1328 * Create, destroy, or resize a linear (direct) block of extents.
1329 */
1330 void
1331 xfs_iext_realloc_direct(
1332 xfs_ifork_t *ifp, /* inode fork pointer */
1333 int new_size) /* new size of extents after adding */
1334 {
1335 int rnew_size; /* real new size of extents */
1336
1337 rnew_size = new_size;
1338
1339 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
1340 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
1341 (new_size != ifp->if_real_bytes)));
1342
1343 /* Free extent records */
1344 if (new_size == 0) {
1345 xfs_iext_destroy(ifp);
1346 }
1347 /* Resize direct extent list and zero any new bytes */
1348 else if (ifp->if_real_bytes) {
1349 /* Check if extents will fit inside the inode */
1350 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
1351 xfs_iext_direct_to_inline(ifp, new_size /
1352 (uint)sizeof(xfs_bmbt_rec_t));
1353 ifp->if_bytes = new_size;
1354 return;
1355 }
1356 if (!is_power_of_2(new_size)){
1357 rnew_size = roundup_pow_of_two(new_size);
1358 }
1359 if (rnew_size != ifp->if_real_bytes) {
1360 ifp->if_u1.if_extents =
1361 kmem_realloc(ifp->if_u1.if_extents,
1362 rnew_size,
1363 ifp->if_real_bytes, KM_NOFS);
1364 }
1365 if (rnew_size > ifp->if_real_bytes) {
1366 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
1367 (uint)sizeof(xfs_bmbt_rec_t)], 0,
1368 rnew_size - ifp->if_real_bytes);
1369 }
1370 }
1371 /* Switch from the inline extent buffer to a direct extent list */
1372 else {
1373 if (!is_power_of_2(new_size)) {
1374 rnew_size = roundup_pow_of_two(new_size);
1375 }
1376 xfs_iext_inline_to_direct(ifp, rnew_size);
1377 }
1378 ifp->if_real_bytes = rnew_size;
1379 ifp->if_bytes = new_size;
1380 }
1381
1382 /*
1383 * Switch from linear (direct) extent records to inline buffer.
1384 */
1385 void
1386 xfs_iext_direct_to_inline(
1387 xfs_ifork_t *ifp, /* inode fork pointer */
1388 xfs_extnum_t nextents) /* number of extents in file */
1389 {
1390 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1391 ASSERT(nextents <= XFS_INLINE_EXTS);
1392 /*
1393 * The inline buffer was zeroed when we switched
1394 * from inline to direct extent allocation mode,
1395 * so we don't need to clear it here.
1396 */
1397 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
1398 nextents * sizeof(xfs_bmbt_rec_t));
1399 kmem_free(ifp->if_u1.if_extents);
1400 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1401 ifp->if_real_bytes = 0;
1402 }
1403
1404 /*
1405 * Switch from inline buffer to linear (direct) extent records.
1406 * new_size should already be rounded up to the next power of 2
1407 * by the caller (when appropriate), so use new_size as it is.
1408 * However, since new_size may be rounded up, we can't update
1409 * if_bytes here. It is the caller's responsibility to update
1410 * if_bytes upon return.
1411 */
1412 void
1413 xfs_iext_inline_to_direct(
1414 xfs_ifork_t *ifp, /* inode fork pointer */
1415 int new_size) /* number of extents in file */
1416 {
1417 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
1418 memset(ifp->if_u1.if_extents, 0, new_size);
1419 if (ifp->if_bytes) {
1420 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
1421 ifp->if_bytes);
1422 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1423 sizeof(xfs_bmbt_rec_t));
1424 }
1425 ifp->if_real_bytes = new_size;
1426 }
1427
1428 /*
1429 * Resize an extent indirection array to new_size bytes.
1430 */
1431 STATIC void
1432 xfs_iext_realloc_indirect(
1433 xfs_ifork_t *ifp, /* inode fork pointer */
1434 int new_size) /* new indirection array size */
1435 {
1436 int nlists; /* number of irec's (ex lists) */
1437 int size; /* current indirection array size */
1438
1439 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1440 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1441 size = nlists * sizeof(xfs_ext_irec_t);
1442 ASSERT(ifp->if_real_bytes);
1443 ASSERT((new_size >= 0) && (new_size != size));
1444 if (new_size == 0) {
1445 xfs_iext_destroy(ifp);
1446 } else {
1447 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
1448 kmem_realloc(ifp->if_u1.if_ext_irec,
1449 new_size, size, KM_NOFS);
1450 }
1451 }
1452
1453 /*
1454 * Switch from indirection array to linear (direct) extent allocations.
1455 */
1456 STATIC void
1457 xfs_iext_indirect_to_direct(
1458 xfs_ifork_t *ifp) /* inode fork pointer */
1459 {
1460 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1461 xfs_extnum_t nextents; /* number of extents in file */
1462 int size; /* size of file extents */
1463
1464 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1465 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1466 ASSERT(nextents <= XFS_LINEAR_EXTS);
1467 size = nextents * sizeof(xfs_bmbt_rec_t);
1468
1469 xfs_iext_irec_compact_pages(ifp);
1470 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
1471
1472 ep = ifp->if_u1.if_ext_irec->er_extbuf;
1473 kmem_free(ifp->if_u1.if_ext_irec);
1474 ifp->if_flags &= ~XFS_IFEXTIREC;
1475 ifp->if_u1.if_extents = ep;
1476 ifp->if_bytes = size;
1477 if (nextents < XFS_LINEAR_EXTS) {
1478 xfs_iext_realloc_direct(ifp, size);
1479 }
1480 }
1481
1482 /*
1483 * Free incore file extents.
1484 */
1485 void
1486 xfs_iext_destroy(
1487 xfs_ifork_t *ifp) /* inode fork pointer */
1488 {
1489 if (ifp->if_flags & XFS_IFEXTIREC) {
1490 int erp_idx;
1491 int nlists;
1492
1493 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1494 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
1495 xfs_iext_irec_remove(ifp, erp_idx);
1496 }
1497 ifp->if_flags &= ~XFS_IFEXTIREC;
1498 } else if (ifp->if_real_bytes) {
1499 kmem_free(ifp->if_u1.if_extents);
1500 } else if (ifp->if_bytes) {
1501 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1502 sizeof(xfs_bmbt_rec_t));
1503 }
1504 ifp->if_u1.if_extents = NULL;
1505 ifp->if_real_bytes = 0;
1506 ifp->if_bytes = 0;
1507 }
1508
1509 /*
1510 * Return a pointer to the extent record for file system block bno.
1511 */
1512 xfs_bmbt_rec_host_t * /* pointer to found extent record */
1513 xfs_iext_bno_to_ext(
1514 xfs_ifork_t *ifp, /* inode fork pointer */
1515 xfs_fileoff_t bno, /* block number to search for */
1516 xfs_extnum_t *idxp) /* index of target extent */
1517 {
1518 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
1519 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
1520 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
1521 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1522 int high; /* upper boundary in search */
1523 xfs_extnum_t idx = 0; /* index of target extent */
1524 int low; /* lower boundary in search */
1525 xfs_extnum_t nextents; /* number of file extents */
1526 xfs_fileoff_t startoff = 0; /* start offset of extent */
1527
1528 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1529 if (nextents == 0) {
1530 *idxp = 0;
1531 return NULL;
1532 }
1533 low = 0;
1534 if (ifp->if_flags & XFS_IFEXTIREC) {
1535 /* Find target extent list */
1536 int erp_idx = 0;
1537 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
1538 base = erp->er_extbuf;
1539 high = erp->er_extcount - 1;
1540 } else {
1541 base = ifp->if_u1.if_extents;
1542 high = nextents - 1;
1543 }
1544 /* Binary search extent records */
1545 while (low <= high) {
1546 idx = (low + high) >> 1;
1547 ep = base + idx;
1548 startoff = xfs_bmbt_get_startoff(ep);
1549 blockcount = xfs_bmbt_get_blockcount(ep);
1550 if (bno < startoff) {
1551 high = idx - 1;
1552 } else if (bno >= startoff + blockcount) {
1553 low = idx + 1;
1554 } else {
1555 /* Convert back to file-based extent index */
1556 if (ifp->if_flags & XFS_IFEXTIREC) {
1557 idx += erp->er_extoff;
1558 }
1559 *idxp = idx;
1560 return ep;
1561 }
1562 }
1563 /* Convert back to file-based extent index */
1564 if (ifp->if_flags & XFS_IFEXTIREC) {
1565 idx += erp->er_extoff;
1566 }
1567 if (bno >= startoff + blockcount) {
1568 if (++idx == nextents) {
1569 ep = NULL;
1570 } else {
1571 ep = xfs_iext_get_ext(ifp, idx);
1572 }
1573 }
1574 *idxp = idx;
1575 return ep;
1576 }
1577
1578 /*
1579 * Return a pointer to the indirection array entry containing the
1580 * extent record for filesystem block bno. Store the index of the
1581 * target irec in *erp_idxp.
1582 */
1583 xfs_ext_irec_t * /* pointer to found extent record */
1584 xfs_iext_bno_to_irec(
1585 xfs_ifork_t *ifp, /* inode fork pointer */
1586 xfs_fileoff_t bno, /* block number to search for */
1587 int *erp_idxp) /* irec index of target ext list */
1588 {
1589 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1590 xfs_ext_irec_t *erp_next; /* next indirection array entry */
1591 int erp_idx; /* indirection array index */
1592 int nlists; /* number of extent irec's (lists) */
1593 int high; /* binary search upper limit */
1594 int low; /* binary search lower limit */
1595
1596 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1597 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1598 erp_idx = 0;
1599 low = 0;
1600 high = nlists - 1;
1601 while (low <= high) {
1602 erp_idx = (low + high) >> 1;
1603 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1604 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
1605 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
1606 high = erp_idx - 1;
1607 } else if (erp_next && bno >=
1608 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
1609 low = erp_idx + 1;
1610 } else {
1611 break;
1612 }
1613 }
1614 *erp_idxp = erp_idx;
1615 return erp;
1616 }
1617
1618 /*
1619 * Return a pointer to the indirection array entry containing the
1620 * extent record at file extent index *idxp. Store the index of the
1621 * target irec in *erp_idxp and store the page index of the target
1622 * extent record in *idxp.
1623 */
1624 xfs_ext_irec_t *
1625 xfs_iext_idx_to_irec(
1626 xfs_ifork_t *ifp, /* inode fork pointer */
1627 xfs_extnum_t *idxp, /* extent index (file -> page) */
1628 int *erp_idxp, /* pointer to target irec */
1629 int realloc) /* new bytes were just added */
1630 {
1631 xfs_ext_irec_t *prev; /* pointer to previous irec */
1632 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
1633 int erp_idx; /* indirection array index */
1634 int nlists; /* number of irec's (ex lists) */
1635 int high; /* binary search upper limit */
1636 int low; /* binary search lower limit */
1637 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
1638
1639 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1640 ASSERT(page_idx >= 0);
1641 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
1642 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
1643
1644 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1645 erp_idx = 0;
1646 low = 0;
1647 high = nlists - 1;
1648
1649 /* Binary search extent irec's */
1650 while (low <= high) {
1651 erp_idx = (low + high) >> 1;
1652 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1653 prev = erp_idx > 0 ? erp - 1 : NULL;
1654 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
1655 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
1656 high = erp_idx - 1;
1657 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
1658 (page_idx == erp->er_extoff + erp->er_extcount &&
1659 !realloc)) {
1660 low = erp_idx + 1;
1661 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
1662 erp->er_extcount == XFS_LINEAR_EXTS) {
1663 ASSERT(realloc);
1664 page_idx = 0;
1665 erp_idx++;
1666 erp = erp_idx < nlists ? erp + 1 : NULL;
1667 break;
1668 } else {
1669 page_idx -= erp->er_extoff;
1670 break;
1671 }
1672 }
1673 *idxp = page_idx;
1674 *erp_idxp = erp_idx;
1675 return(erp);
1676 }
1677
1678 /*
1679 * Allocate and initialize an indirection array once the space needed
1680 * for incore extents increases above XFS_IEXT_BUFSZ.
1681 */
1682 void
1683 xfs_iext_irec_init(
1684 xfs_ifork_t *ifp) /* inode fork pointer */
1685 {
1686 xfs_ext_irec_t *erp; /* indirection array pointer */
1687 xfs_extnum_t nextents; /* number of extents in file */
1688
1689 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1690 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1691 ASSERT(nextents <= XFS_LINEAR_EXTS);
1692
1693 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
1694
1695 if (nextents == 0) {
1696 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1697 } else if (!ifp->if_real_bytes) {
1698 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
1699 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
1700 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
1701 }
1702 erp->er_extbuf = ifp->if_u1.if_extents;
1703 erp->er_extcount = nextents;
1704 erp->er_extoff = 0;
1705
1706 ifp->if_flags |= XFS_IFEXTIREC;
1707 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
1708 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
1709 ifp->if_u1.if_ext_irec = erp;
1710
1711 return;
1712 }
1713
1714 /*
1715 * Allocate and initialize a new entry in the indirection array.
1716 */
1717 xfs_ext_irec_t *
1718 xfs_iext_irec_new(
1719 xfs_ifork_t *ifp, /* inode fork pointer */
1720 int erp_idx) /* index for new irec */
1721 {
1722 xfs_ext_irec_t *erp; /* indirection array pointer */
1723 int i; /* loop counter */
1724 int nlists; /* number of irec's (ex lists) */
1725
1726 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1727 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1728
1729 /* Resize indirection array */
1730 xfs_iext_realloc_indirect(ifp, ++nlists *
1731 sizeof(xfs_ext_irec_t));
1732 /*
1733 * Move records down in the array so the
1734 * new page can use erp_idx.
1735 */
1736 erp = ifp->if_u1.if_ext_irec;
1737 for (i = nlists - 1; i > erp_idx; i--) {
1738 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
1739 }
1740 ASSERT(i == erp_idx);
1741
1742 /* Initialize new extent record */
1743 erp = ifp->if_u1.if_ext_irec;
1744 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1745 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1746 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
1747 erp[erp_idx].er_extcount = 0;
1748 erp[erp_idx].er_extoff = erp_idx > 0 ?
1749 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
1750 return (&erp[erp_idx]);
1751 }
1752
1753 /*
1754 * Remove a record from the indirection array.
1755 */
1756 void
1757 xfs_iext_irec_remove(
1758 xfs_ifork_t *ifp, /* inode fork pointer */
1759 int erp_idx) /* irec index to remove */
1760 {
1761 xfs_ext_irec_t *erp; /* indirection array pointer */
1762 int i; /* loop counter */
1763 int nlists; /* number of irec's (ex lists) */
1764
1765 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1766 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1767 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1768 if (erp->er_extbuf) {
1769 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
1770 -erp->er_extcount);
1771 kmem_free(erp->er_extbuf);
1772 }
1773 /* Compact extent records */
1774 erp = ifp->if_u1.if_ext_irec;
1775 for (i = erp_idx; i < nlists - 1; i++) {
1776 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
1777 }
1778 /*
1779 * Manually free the last extent record from the indirection
1780 * array. A call to xfs_iext_realloc_indirect() with a size
1781 * of zero would result in a call to xfs_iext_destroy() which
1782 * would in turn call this function again, creating a nasty
1783 * infinite loop.
1784 */
1785 if (--nlists) {
1786 xfs_iext_realloc_indirect(ifp,
1787 nlists * sizeof(xfs_ext_irec_t));
1788 } else {
1789 kmem_free(ifp->if_u1.if_ext_irec);
1790 }
1791 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1792 }
1793
1794 /*
1795 * This is called to clean up large amounts of unused memory allocated
1796 * by the indirection array. Before compacting anything though, verify
1797 * that the indirection array is still needed and switch back to the
1798 * linear extent list (or even the inline buffer) if possible. The
1799 * compaction policy is as follows:
1800 *
1801 * Full Compaction: Extents fit into a single page (or inline buffer)
1802 * Partial Compaction: Extents occupy less than 50% of allocated space
1803 * No Compaction: Extents occupy at least 50% of allocated space
1804 */
1805 void
1806 xfs_iext_irec_compact(
1807 xfs_ifork_t *ifp) /* inode fork pointer */
1808 {
1809 xfs_extnum_t nextents; /* number of extents in file */
1810 int nlists; /* number of irec's (ex lists) */
1811
1812 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1813 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1814 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1815
1816 if (nextents == 0) {
1817 xfs_iext_destroy(ifp);
1818 } else if (nextents <= XFS_INLINE_EXTS) {
1819 xfs_iext_indirect_to_direct(ifp);
1820 xfs_iext_direct_to_inline(ifp, nextents);
1821 } else if (nextents <= XFS_LINEAR_EXTS) {
1822 xfs_iext_indirect_to_direct(ifp);
1823 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
1824 xfs_iext_irec_compact_pages(ifp);
1825 }
1826 }
1827
1828 /*
1829 * Combine extents from neighboring extent pages.
1830 */
1831 void
1832 xfs_iext_irec_compact_pages(
1833 xfs_ifork_t *ifp) /* inode fork pointer */
1834 {
1835 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
1836 int erp_idx = 0; /* indirection array index */
1837 int nlists; /* number of irec's (ex lists) */
1838
1839 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1840 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1841 while (erp_idx < nlists - 1) {
1842 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1843 erp_next = erp + 1;
1844 if (erp_next->er_extcount <=
1845 (XFS_LINEAR_EXTS - erp->er_extcount)) {
1846 memcpy(&erp->er_extbuf[erp->er_extcount],
1847 erp_next->er_extbuf, erp_next->er_extcount *
1848 sizeof(xfs_bmbt_rec_t));
1849 erp->er_extcount += erp_next->er_extcount;
1850 /*
1851 * Free page before removing extent record
1852 * so er_extoffs don't get modified in
1853 * xfs_iext_irec_remove.
1854 */
1855 kmem_free(erp_next->er_extbuf);
1856 erp_next->er_extbuf = NULL;
1857 xfs_iext_irec_remove(ifp, erp_idx + 1);
1858 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1859 } else {
1860 erp_idx++;
1861 }
1862 }
1863 }
1864
1865 /*
1866 * This is called to update the er_extoff field in the indirection
1867 * array when extents have been added or removed from one of the
1868 * extent lists. erp_idx contains the irec index to begin updating
1869 * at and ext_diff contains the number of extents that were added
1870 * or removed.
1871 */
1872 void
1873 xfs_iext_irec_update_extoffs(
1874 xfs_ifork_t *ifp, /* inode fork pointer */
1875 int erp_idx, /* irec index to update */
1876 int ext_diff) /* number of new extents */
1877 {
1878 int i; /* loop counter */
1879 int nlists; /* number of irec's (ex lists */
1880
1881 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1882 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1883 for (i = erp_idx; i < nlists; i++) {
1884 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
1885 }
1886 }