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