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