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