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