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