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