blob: 22c9837c5d4beb680514213369aa0722d189503f [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"
Dave Chinner239880e2013-10-23 10:50:10 +110023#include "xfs_log_format.h"
24#include "xfs_trans_resv.h"
Dave Chinner5c4d97d2013-08-12 20:49:33 +100025#include "xfs_inum.h"
Dave Chinner5c4d97d2013-08-12 20:49:33 +100026#include "xfs_sb.h"
27#include "xfs_ag.h"
28#include "xfs_mount.h"
Dave Chinner5c4d97d2013-08-12 20:49:33 +100029#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110030#include "xfs_trans.h"
Dave Chinner5c4d97d2013-08-12 20:49:33 +100031#include "xfs_inode_item.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110032#include "xfs_bmap_btree.h"
Dave Chinner5c4d97d2013-08-12 20:49:33 +100033#include "xfs_bmap.h"
34#include "xfs_error.h"
Dave Chinner5c4d97d2013-08-12 20:49:33 +100035#include "xfs_trace.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110036#include "xfs_attr_sf.h"
37#include "xfs_dinode.h"
Dave Chinner5c4d97d2013-08-12 20:49:33 +100038
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);
Dan Carpenter0d0ab122013-08-15 08:53:38 +0300160 if (unlikely(di_size < 0 ||
161 di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000162 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
Zhi Yong Wuf6c27342013-08-07 10:11:04 +0000467 * if we are adding records, one will be allocated. The caller must also
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000468 * 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 */
Dave Chinnerc5c249b2013-08-12 20:49:43 +1000772 put_unaligned_be64(ep->l0, &dp->l0);
773 put_unaligned_be64(ep->l1, &dp->l1);
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000774 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 {
1024 int count = ext_diff;
1025
1026 while (count) {
1027 erp = xfs_iext_irec_new(ifp, erp_idx);
1028 erp->er_extcount = count;
1029 count -= MIN(count, (int)XFS_LINEAR_EXTS);
1030 if (count) {
1031 erp_idx++;
1032 }
1033 }
1034 }
1035 }
1036 ifp->if_bytes = new_size;
1037}
1038
1039/*
1040 * This is called when incore extents are being added to the indirection
1041 * array and the new extents do not fit in the target extent list. The
1042 * erp_idx parameter contains the irec index for the target extent list
1043 * in the indirection array, and the idx parameter contains the extent
1044 * index within the list. The number of extents being added is stored
1045 * in the count parameter.
1046 *
1047 * |-------| |-------|
1048 * | | | | idx - number of extents before idx
1049 * | idx | | count |
1050 * | | | | count - number of extents being inserted at idx
1051 * |-------| |-------|
1052 * | count | | nex2 | nex2 - number of extents after idx + count
1053 * |-------| |-------|
1054 */
1055void
1056xfs_iext_add_indirect_multi(
1057 xfs_ifork_t *ifp, /* inode fork pointer */
1058 int erp_idx, /* target extent irec index */
1059 xfs_extnum_t idx, /* index within target list */
1060 int count) /* new extents being added */
1061{
1062 int byte_diff; /* new bytes being added */
1063 xfs_ext_irec_t *erp; /* pointer to irec entry */
1064 xfs_extnum_t ext_diff; /* number of extents to add */
1065 xfs_extnum_t ext_cnt; /* new extents still needed */
1066 xfs_extnum_t nex2; /* extents after idx + count */
1067 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
1068 int nlists; /* number of irec's (lists) */
1069
1070 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1071 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1072 nex2 = erp->er_extcount - idx;
1073 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1074
1075 /*
1076 * Save second part of target extent list
1077 * (all extents past */
1078 if (nex2) {
1079 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1080 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
1081 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
1082 erp->er_extcount -= nex2;
1083 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
1084 memset(&erp->er_extbuf[idx], 0, byte_diff);
1085 }
1086
1087 /*
1088 * Add the new extents to the end of the target
1089 * list, then allocate new irec record(s) and
1090 * extent buffer(s) as needed to store the rest
1091 * of the new extents.
1092 */
1093 ext_cnt = count;
1094 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
1095 if (ext_diff) {
1096 erp->er_extcount += ext_diff;
1097 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1098 ext_cnt -= ext_diff;
1099 }
1100 while (ext_cnt) {
1101 erp_idx++;
1102 erp = xfs_iext_irec_new(ifp, erp_idx);
1103 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
1104 erp->er_extcount = ext_diff;
1105 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1106 ext_cnt -= ext_diff;
1107 }
1108
1109 /* Add nex2 extents back to indirection array */
1110 if (nex2) {
1111 xfs_extnum_t ext_avail;
1112 int i;
1113
1114 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1115 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
1116 i = 0;
1117 /*
1118 * If nex2 extents fit in the current page, append
1119 * nex2_ep after the new extents.
1120 */
1121 if (nex2 <= ext_avail) {
1122 i = erp->er_extcount;
1123 }
1124 /*
1125 * Otherwise, check if space is available in the
1126 * next page.
1127 */
1128 else if ((erp_idx < nlists - 1) &&
1129 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
1130 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
1131 erp_idx++;
1132 erp++;
1133 /* Create a hole for nex2 extents */
1134 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
1135 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
1136 }
1137 /*
1138 * Final choice, create a new extent page for
1139 * nex2 extents.
1140 */
1141 else {
1142 erp_idx++;
1143 erp = xfs_iext_irec_new(ifp, erp_idx);
1144 }
1145 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
1146 kmem_free(nex2_ep);
1147 erp->er_extcount += nex2;
1148 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
1149 }
1150}
1151
1152/*
1153 * This is called when the amount of space required for incore file
1154 * extents needs to be decreased. The ext_diff parameter stores the
1155 * number of extents to be removed and the idx parameter contains
1156 * the extent index where the extents will be removed from.
1157 *
1158 * If the amount of space needed has decreased below the linear
1159 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1160 * extent array. Otherwise, use kmem_realloc() to adjust the
1161 * size to what is needed.
1162 */
1163void
1164xfs_iext_remove(
1165 xfs_inode_t *ip, /* incore inode pointer */
1166 xfs_extnum_t idx, /* index to begin removing exts */
1167 int ext_diff, /* number of extents to remove */
1168 int state) /* type of extent conversion */
1169{
1170 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
1171 xfs_extnum_t nextents; /* number of extents in file */
1172 int new_size; /* size of extents after removal */
1173
1174 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
1175
1176 ASSERT(ext_diff > 0);
1177 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1178 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
1179
1180 if (new_size == 0) {
1181 xfs_iext_destroy(ifp);
1182 } else if (ifp->if_flags & XFS_IFEXTIREC) {
1183 xfs_iext_remove_indirect(ifp, idx, ext_diff);
1184 } else if (ifp->if_real_bytes) {
1185 xfs_iext_remove_direct(ifp, idx, ext_diff);
1186 } else {
1187 xfs_iext_remove_inline(ifp, idx, ext_diff);
1188 }
1189 ifp->if_bytes = new_size;
1190}
1191
1192/*
1193 * This removes ext_diff extents from the inline buffer, beginning
1194 * at extent index idx.
1195 */
1196void
1197xfs_iext_remove_inline(
1198 xfs_ifork_t *ifp, /* inode fork pointer */
1199 xfs_extnum_t idx, /* index to begin removing exts */
1200 int ext_diff) /* number of extents to remove */
1201{
1202 int nextents; /* number of extents in file */
1203
1204 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1205 ASSERT(idx < XFS_INLINE_EXTS);
1206 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1207 ASSERT(((nextents - ext_diff) > 0) &&
1208 (nextents - ext_diff) < XFS_INLINE_EXTS);
1209
1210 if (idx + ext_diff < nextents) {
1211 memmove(&ifp->if_u2.if_inline_ext[idx],
1212 &ifp->if_u2.if_inline_ext[idx + ext_diff],
1213 (nextents - (idx + ext_diff)) *
1214 sizeof(xfs_bmbt_rec_t));
1215 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
1216 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1217 } else {
1218 memset(&ifp->if_u2.if_inline_ext[idx], 0,
1219 ext_diff * sizeof(xfs_bmbt_rec_t));
1220 }
1221}
1222
1223/*
1224 * This removes ext_diff extents from a linear (direct) extent list,
1225 * beginning at extent index idx. If the extents are being removed
1226 * from the end of the list (ie. truncate) then we just need to re-
1227 * allocate the list to remove the extra space. Otherwise, if the
1228 * extents are being removed from the middle of the existing extent
1229 * entries, then we first need to move the extent records beginning
1230 * at idx + ext_diff up in the list to overwrite the records being
1231 * removed, then remove the extra space via kmem_realloc.
1232 */
1233void
1234xfs_iext_remove_direct(
1235 xfs_ifork_t *ifp, /* inode fork pointer */
1236 xfs_extnum_t idx, /* index to begin removing exts */
1237 int ext_diff) /* number of extents to remove */
1238{
1239 xfs_extnum_t nextents; /* number of extents in file */
1240 int new_size; /* size of extents after removal */
1241
1242 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1243 new_size = ifp->if_bytes -
1244 (ext_diff * sizeof(xfs_bmbt_rec_t));
1245 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1246
1247 if (new_size == 0) {
1248 xfs_iext_destroy(ifp);
1249 return;
1250 }
1251 /* Move extents up in the list (if needed) */
1252 if (idx + ext_diff < nextents) {
1253 memmove(&ifp->if_u1.if_extents[idx],
1254 &ifp->if_u1.if_extents[idx + ext_diff],
1255 (nextents - (idx + ext_diff)) *
1256 sizeof(xfs_bmbt_rec_t));
1257 }
1258 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
1259 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1260 /*
1261 * Reallocate the direct extent list. If the extents
1262 * will fit inside the inode then xfs_iext_realloc_direct
1263 * will switch from direct to inline extent allocation
1264 * mode for us.
1265 */
1266 xfs_iext_realloc_direct(ifp, new_size);
1267 ifp->if_bytes = new_size;
1268}
1269
1270/*
1271 * This is called when incore extents are being removed from the
1272 * indirection array and the extents being removed span multiple extent
1273 * buffers. The idx parameter contains the file extent index where we
1274 * want to begin removing extents, and the count parameter contains
1275 * how many extents need to be removed.
1276 *
1277 * |-------| |-------|
1278 * | nex1 | | | nex1 - number of extents before idx
1279 * |-------| | count |
1280 * | | | | count - number of extents being removed at idx
1281 * | count | |-------|
1282 * | | | nex2 | nex2 - number of extents after idx + count
1283 * |-------| |-------|
1284 */
1285void
1286xfs_iext_remove_indirect(
1287 xfs_ifork_t *ifp, /* inode fork pointer */
1288 xfs_extnum_t idx, /* index to begin removing extents */
1289 int count) /* number of extents to remove */
1290{
1291 xfs_ext_irec_t *erp; /* indirection array pointer */
1292 int erp_idx = 0; /* indirection array index */
1293 xfs_extnum_t ext_cnt; /* extents left to remove */
1294 xfs_extnum_t ext_diff; /* extents to remove in current list */
1295 xfs_extnum_t nex1; /* number of extents before idx */
1296 xfs_extnum_t nex2; /* extents after idx + count */
1297 int page_idx = idx; /* index in target extent list */
1298
1299 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1300 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
1301 ASSERT(erp != NULL);
1302 nex1 = page_idx;
1303 ext_cnt = count;
1304 while (ext_cnt) {
1305 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
1306 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
1307 /*
1308 * Check for deletion of entire list;
1309 * xfs_iext_irec_remove() updates extent offsets.
1310 */
1311 if (ext_diff == erp->er_extcount) {
1312 xfs_iext_irec_remove(ifp, erp_idx);
1313 ext_cnt -= ext_diff;
1314 nex1 = 0;
1315 if (ext_cnt) {
1316 ASSERT(erp_idx < ifp->if_real_bytes /
1317 XFS_IEXT_BUFSZ);
1318 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1319 nex1 = 0;
1320 continue;
1321 } else {
1322 break;
1323 }
1324 }
1325 /* Move extents up (if needed) */
1326 if (nex2) {
1327 memmove(&erp->er_extbuf[nex1],
1328 &erp->er_extbuf[nex1 + ext_diff],
1329 nex2 * sizeof(xfs_bmbt_rec_t));
1330 }
1331 /* Zero out rest of page */
1332 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
1333 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
1334 /* Update remaining counters */
1335 erp->er_extcount -= ext_diff;
1336 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
1337 ext_cnt -= ext_diff;
1338 nex1 = 0;
1339 erp_idx++;
1340 erp++;
1341 }
1342 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
1343 xfs_iext_irec_compact(ifp);
1344}
1345
1346/*
1347 * Create, destroy, or resize a linear (direct) block of extents.
1348 */
1349void
1350xfs_iext_realloc_direct(
1351 xfs_ifork_t *ifp, /* inode fork pointer */
Jie Liu17ec81c2013-09-22 16:25:15 +08001352 int new_size) /* new size of extents after adding */
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001353{
1354 int rnew_size; /* real new size of extents */
1355
1356 rnew_size = new_size;
1357
1358 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
1359 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
1360 (new_size != ifp->if_real_bytes)));
1361
1362 /* Free extent records */
1363 if (new_size == 0) {
1364 xfs_iext_destroy(ifp);
1365 }
1366 /* Resize direct extent list and zero any new bytes */
1367 else if (ifp->if_real_bytes) {
1368 /* Check if extents will fit inside the inode */
1369 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
1370 xfs_iext_direct_to_inline(ifp, new_size /
1371 (uint)sizeof(xfs_bmbt_rec_t));
1372 ifp->if_bytes = new_size;
1373 return;
1374 }
1375 if (!is_power_of_2(new_size)){
1376 rnew_size = roundup_pow_of_two(new_size);
1377 }
1378 if (rnew_size != ifp->if_real_bytes) {
1379 ifp->if_u1.if_extents =
1380 kmem_realloc(ifp->if_u1.if_extents,
1381 rnew_size,
1382 ifp->if_real_bytes, KM_NOFS);
1383 }
1384 if (rnew_size > ifp->if_real_bytes) {
1385 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
1386 (uint)sizeof(xfs_bmbt_rec_t)], 0,
1387 rnew_size - ifp->if_real_bytes);
1388 }
1389 }
Jie Liu17ec81c2013-09-22 16:25:15 +08001390 /* Switch from the inline extent buffer to a direct extent list */
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001391 else {
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001392 if (!is_power_of_2(new_size)) {
1393 rnew_size = roundup_pow_of_two(new_size);
1394 }
1395 xfs_iext_inline_to_direct(ifp, rnew_size);
1396 }
1397 ifp->if_real_bytes = rnew_size;
1398 ifp->if_bytes = new_size;
1399}
1400
1401/*
1402 * Switch from linear (direct) extent records to inline buffer.
1403 */
1404void
1405xfs_iext_direct_to_inline(
1406 xfs_ifork_t *ifp, /* inode fork pointer */
1407 xfs_extnum_t nextents) /* number of extents in file */
1408{
1409 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1410 ASSERT(nextents <= XFS_INLINE_EXTS);
1411 /*
1412 * The inline buffer was zeroed when we switched
1413 * from inline to direct extent allocation mode,
1414 * so we don't need to clear it here.
1415 */
1416 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
1417 nextents * sizeof(xfs_bmbt_rec_t));
1418 kmem_free(ifp->if_u1.if_extents);
1419 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1420 ifp->if_real_bytes = 0;
1421}
1422
1423/*
1424 * Switch from inline buffer to linear (direct) extent records.
1425 * new_size should already be rounded up to the next power of 2
1426 * by the caller (when appropriate), so use new_size as it is.
1427 * However, since new_size may be rounded up, we can't update
1428 * if_bytes here. It is the caller's responsibility to update
1429 * if_bytes upon return.
1430 */
1431void
1432xfs_iext_inline_to_direct(
1433 xfs_ifork_t *ifp, /* inode fork pointer */
1434 int new_size) /* number of extents in file */
1435{
1436 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
1437 memset(ifp->if_u1.if_extents, 0, new_size);
1438 if (ifp->if_bytes) {
1439 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
1440 ifp->if_bytes);
1441 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1442 sizeof(xfs_bmbt_rec_t));
1443 }
1444 ifp->if_real_bytes = new_size;
1445}
1446
1447/*
1448 * Resize an extent indirection array to new_size bytes.
1449 */
1450STATIC void
1451xfs_iext_realloc_indirect(
1452 xfs_ifork_t *ifp, /* inode fork pointer */
1453 int new_size) /* new indirection array size */
1454{
1455 int nlists; /* number of irec's (ex lists) */
1456 int size; /* current indirection array size */
1457
1458 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1459 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1460 size = nlists * sizeof(xfs_ext_irec_t);
1461 ASSERT(ifp->if_real_bytes);
1462 ASSERT((new_size >= 0) && (new_size != size));
1463 if (new_size == 0) {
1464 xfs_iext_destroy(ifp);
1465 } else {
1466 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
1467 kmem_realloc(ifp->if_u1.if_ext_irec,
1468 new_size, size, KM_NOFS);
1469 }
1470}
1471
1472/*
1473 * Switch from indirection array to linear (direct) extent allocations.
1474 */
1475STATIC void
1476xfs_iext_indirect_to_direct(
1477 xfs_ifork_t *ifp) /* inode fork pointer */
1478{
1479 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1480 xfs_extnum_t nextents; /* number of extents in file */
1481 int size; /* size of file extents */
1482
1483 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1484 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1485 ASSERT(nextents <= XFS_LINEAR_EXTS);
1486 size = nextents * sizeof(xfs_bmbt_rec_t);
1487
1488 xfs_iext_irec_compact_pages(ifp);
1489 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
1490
1491 ep = ifp->if_u1.if_ext_irec->er_extbuf;
1492 kmem_free(ifp->if_u1.if_ext_irec);
1493 ifp->if_flags &= ~XFS_IFEXTIREC;
1494 ifp->if_u1.if_extents = ep;
1495 ifp->if_bytes = size;
1496 if (nextents < XFS_LINEAR_EXTS) {
1497 xfs_iext_realloc_direct(ifp, size);
1498 }
1499}
1500
1501/*
1502 * Free incore file extents.
1503 */
1504void
1505xfs_iext_destroy(
1506 xfs_ifork_t *ifp) /* inode fork pointer */
1507{
1508 if (ifp->if_flags & XFS_IFEXTIREC) {
1509 int erp_idx;
1510 int nlists;
1511
1512 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1513 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
1514 xfs_iext_irec_remove(ifp, erp_idx);
1515 }
1516 ifp->if_flags &= ~XFS_IFEXTIREC;
1517 } else if (ifp->if_real_bytes) {
1518 kmem_free(ifp->if_u1.if_extents);
1519 } else if (ifp->if_bytes) {
1520 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1521 sizeof(xfs_bmbt_rec_t));
1522 }
1523 ifp->if_u1.if_extents = NULL;
1524 ifp->if_real_bytes = 0;
1525 ifp->if_bytes = 0;
1526}
1527
1528/*
1529 * Return a pointer to the extent record for file system block bno.
1530 */
1531xfs_bmbt_rec_host_t * /* pointer to found extent record */
1532xfs_iext_bno_to_ext(
1533 xfs_ifork_t *ifp, /* inode fork pointer */
1534 xfs_fileoff_t bno, /* block number to search for */
1535 xfs_extnum_t *idxp) /* index of target extent */
1536{
1537 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
1538 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
1539 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
1540 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1541 int high; /* upper boundary in search */
1542 xfs_extnum_t idx = 0; /* index of target extent */
1543 int low; /* lower boundary in search */
1544 xfs_extnum_t nextents; /* number of file extents */
1545 xfs_fileoff_t startoff = 0; /* start offset of extent */
1546
1547 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1548 if (nextents == 0) {
1549 *idxp = 0;
1550 return NULL;
1551 }
1552 low = 0;
1553 if (ifp->if_flags & XFS_IFEXTIREC) {
1554 /* Find target extent list */
1555 int erp_idx = 0;
1556 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
1557 base = erp->er_extbuf;
1558 high = erp->er_extcount - 1;
1559 } else {
1560 base = ifp->if_u1.if_extents;
1561 high = nextents - 1;
1562 }
1563 /* Binary search extent records */
1564 while (low <= high) {
1565 idx = (low + high) >> 1;
1566 ep = base + idx;
1567 startoff = xfs_bmbt_get_startoff(ep);
1568 blockcount = xfs_bmbt_get_blockcount(ep);
1569 if (bno < startoff) {
1570 high = idx - 1;
1571 } else if (bno >= startoff + blockcount) {
1572 low = idx + 1;
1573 } else {
1574 /* Convert back to file-based extent index */
1575 if (ifp->if_flags & XFS_IFEXTIREC) {
1576 idx += erp->er_extoff;
1577 }
1578 *idxp = idx;
1579 return ep;
1580 }
1581 }
1582 /* Convert back to file-based extent index */
1583 if (ifp->if_flags & XFS_IFEXTIREC) {
1584 idx += erp->er_extoff;
1585 }
1586 if (bno >= startoff + blockcount) {
1587 if (++idx == nextents) {
1588 ep = NULL;
1589 } else {
1590 ep = xfs_iext_get_ext(ifp, idx);
1591 }
1592 }
1593 *idxp = idx;
1594 return ep;
1595}
1596
1597/*
1598 * Return a pointer to the indirection array entry containing the
1599 * extent record for filesystem block bno. Store the index of the
1600 * target irec in *erp_idxp.
1601 */
1602xfs_ext_irec_t * /* pointer to found extent record */
1603xfs_iext_bno_to_irec(
1604 xfs_ifork_t *ifp, /* inode fork pointer */
1605 xfs_fileoff_t bno, /* block number to search for */
1606 int *erp_idxp) /* irec index of target ext list */
1607{
1608 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1609 xfs_ext_irec_t *erp_next; /* next indirection array entry */
1610 int erp_idx; /* indirection array index */
1611 int nlists; /* number of extent irec's (lists) */
1612 int high; /* binary search upper limit */
1613 int low; /* binary search lower limit */
1614
1615 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1616 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1617 erp_idx = 0;
1618 low = 0;
1619 high = nlists - 1;
1620 while (low <= high) {
1621 erp_idx = (low + high) >> 1;
1622 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1623 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
1624 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
1625 high = erp_idx - 1;
1626 } else if (erp_next && bno >=
1627 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
1628 low = erp_idx + 1;
1629 } else {
1630 break;
1631 }
1632 }
1633 *erp_idxp = erp_idx;
1634 return erp;
1635}
1636
1637/*
1638 * Return a pointer to the indirection array entry containing the
1639 * extent record at file extent index *idxp. Store the index of the
1640 * target irec in *erp_idxp and store the page index of the target
1641 * extent record in *idxp.
1642 */
1643xfs_ext_irec_t *
1644xfs_iext_idx_to_irec(
1645 xfs_ifork_t *ifp, /* inode fork pointer */
1646 xfs_extnum_t *idxp, /* extent index (file -> page) */
1647 int *erp_idxp, /* pointer to target irec */
1648 int realloc) /* new bytes were just added */
1649{
1650 xfs_ext_irec_t *prev; /* pointer to previous irec */
1651 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
1652 int erp_idx; /* indirection array index */
1653 int nlists; /* number of irec's (ex lists) */
1654 int high; /* binary search upper limit */
1655 int low; /* binary search lower limit */
1656 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
1657
1658 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1659 ASSERT(page_idx >= 0);
1660 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
1661 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
1662
1663 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1664 erp_idx = 0;
1665 low = 0;
1666 high = nlists - 1;
1667
1668 /* Binary search extent irec's */
1669 while (low <= high) {
1670 erp_idx = (low + high) >> 1;
1671 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1672 prev = erp_idx > 0 ? erp - 1 : NULL;
1673 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
1674 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
1675 high = erp_idx - 1;
1676 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
1677 (page_idx == erp->er_extoff + erp->er_extcount &&
1678 !realloc)) {
1679 low = erp_idx + 1;
1680 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
1681 erp->er_extcount == XFS_LINEAR_EXTS) {
1682 ASSERT(realloc);
1683 page_idx = 0;
1684 erp_idx++;
1685 erp = erp_idx < nlists ? erp + 1 : NULL;
1686 break;
1687 } else {
1688 page_idx -= erp->er_extoff;
1689 break;
1690 }
1691 }
1692 *idxp = page_idx;
1693 *erp_idxp = erp_idx;
1694 return(erp);
1695}
1696
1697/*
1698 * Allocate and initialize an indirection array once the space needed
1699 * for incore extents increases above XFS_IEXT_BUFSZ.
1700 */
1701void
1702xfs_iext_irec_init(
1703 xfs_ifork_t *ifp) /* inode fork pointer */
1704{
1705 xfs_ext_irec_t *erp; /* indirection array pointer */
1706 xfs_extnum_t nextents; /* number of extents in file */
1707
1708 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1709 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1710 ASSERT(nextents <= XFS_LINEAR_EXTS);
1711
1712 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
1713
1714 if (nextents == 0) {
1715 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1716 } else if (!ifp->if_real_bytes) {
1717 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
1718 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
1719 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
1720 }
1721 erp->er_extbuf = ifp->if_u1.if_extents;
1722 erp->er_extcount = nextents;
1723 erp->er_extoff = 0;
1724
1725 ifp->if_flags |= XFS_IFEXTIREC;
1726 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
1727 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
1728 ifp->if_u1.if_ext_irec = erp;
1729
1730 return;
1731}
1732
1733/*
1734 * Allocate and initialize a new entry in the indirection array.
1735 */
1736xfs_ext_irec_t *
1737xfs_iext_irec_new(
1738 xfs_ifork_t *ifp, /* inode fork pointer */
1739 int erp_idx) /* index for new irec */
1740{
1741 xfs_ext_irec_t *erp; /* indirection array pointer */
1742 int i; /* loop counter */
1743 int nlists; /* number of irec's (ex lists) */
1744
1745 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1746 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1747
1748 /* Resize indirection array */
1749 xfs_iext_realloc_indirect(ifp, ++nlists *
1750 sizeof(xfs_ext_irec_t));
1751 /*
1752 * Move records down in the array so the
1753 * new page can use erp_idx.
1754 */
1755 erp = ifp->if_u1.if_ext_irec;
1756 for (i = nlists - 1; i > erp_idx; i--) {
1757 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
1758 }
1759 ASSERT(i == erp_idx);
1760
1761 /* Initialize new extent record */
1762 erp = ifp->if_u1.if_ext_irec;
1763 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1764 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1765 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
1766 erp[erp_idx].er_extcount = 0;
1767 erp[erp_idx].er_extoff = erp_idx > 0 ?
1768 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
1769 return (&erp[erp_idx]);
1770}
1771
1772/*
1773 * Remove a record from the indirection array.
1774 */
1775void
1776xfs_iext_irec_remove(
1777 xfs_ifork_t *ifp, /* inode fork pointer */
1778 int erp_idx) /* irec index to remove */
1779{
1780 xfs_ext_irec_t *erp; /* indirection array pointer */
1781 int i; /* loop counter */
1782 int nlists; /* number of irec's (ex lists) */
1783
1784 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1785 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1786 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1787 if (erp->er_extbuf) {
1788 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
1789 -erp->er_extcount);
1790 kmem_free(erp->er_extbuf);
1791 }
1792 /* Compact extent records */
1793 erp = ifp->if_u1.if_ext_irec;
1794 for (i = erp_idx; i < nlists - 1; i++) {
1795 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
1796 }
1797 /*
1798 * Manually free the last extent record from the indirection
1799 * array. A call to xfs_iext_realloc_indirect() with a size
1800 * of zero would result in a call to xfs_iext_destroy() which
1801 * would in turn call this function again, creating a nasty
1802 * infinite loop.
1803 */
1804 if (--nlists) {
1805 xfs_iext_realloc_indirect(ifp,
1806 nlists * sizeof(xfs_ext_irec_t));
1807 } else {
1808 kmem_free(ifp->if_u1.if_ext_irec);
1809 }
1810 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1811}
1812
1813/*
1814 * This is called to clean up large amounts of unused memory allocated
1815 * by the indirection array. Before compacting anything though, verify
1816 * that the indirection array is still needed and switch back to the
1817 * linear extent list (or even the inline buffer) if possible. The
1818 * compaction policy is as follows:
1819 *
1820 * Full Compaction: Extents fit into a single page (or inline buffer)
1821 * Partial Compaction: Extents occupy less than 50% of allocated space
1822 * No Compaction: Extents occupy at least 50% of allocated space
1823 */
1824void
1825xfs_iext_irec_compact(
1826 xfs_ifork_t *ifp) /* inode fork pointer */
1827{
1828 xfs_extnum_t nextents; /* number of extents in file */
1829 int nlists; /* number of irec's (ex lists) */
1830
1831 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1832 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1833 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1834
1835 if (nextents == 0) {
1836 xfs_iext_destroy(ifp);
1837 } else if (nextents <= XFS_INLINE_EXTS) {
1838 xfs_iext_indirect_to_direct(ifp);
1839 xfs_iext_direct_to_inline(ifp, nextents);
1840 } else if (nextents <= XFS_LINEAR_EXTS) {
1841 xfs_iext_indirect_to_direct(ifp);
1842 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
1843 xfs_iext_irec_compact_pages(ifp);
1844 }
1845}
1846
1847/*
1848 * Combine extents from neighboring extent pages.
1849 */
1850void
1851xfs_iext_irec_compact_pages(
1852 xfs_ifork_t *ifp) /* inode fork pointer */
1853{
1854 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
1855 int erp_idx = 0; /* indirection array index */
1856 int nlists; /* number of irec's (ex lists) */
1857
1858 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1859 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1860 while (erp_idx < nlists - 1) {
1861 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1862 erp_next = erp + 1;
1863 if (erp_next->er_extcount <=
1864 (XFS_LINEAR_EXTS - erp->er_extcount)) {
1865 memcpy(&erp->er_extbuf[erp->er_extcount],
1866 erp_next->er_extbuf, erp_next->er_extcount *
1867 sizeof(xfs_bmbt_rec_t));
1868 erp->er_extcount += erp_next->er_extcount;
1869 /*
1870 * Free page before removing extent record
1871 * so er_extoffs don't get modified in
1872 * xfs_iext_irec_remove.
1873 */
1874 kmem_free(erp_next->er_extbuf);
1875 erp_next->er_extbuf = NULL;
1876 xfs_iext_irec_remove(ifp, erp_idx + 1);
1877 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1878 } else {
1879 erp_idx++;
1880 }
1881 }
1882}
1883
1884/*
1885 * This is called to update the er_extoff field in the indirection
1886 * array when extents have been added or removed from one of the
1887 * extent lists. erp_idx contains the irec index to begin updating
1888 * at and ext_diff contains the number of extents that were added
1889 * or removed.
1890 */
1891void
1892xfs_iext_irec_update_extoffs(
1893 xfs_ifork_t *ifp, /* inode fork pointer */
1894 int erp_idx, /* irec index to update */
1895 int ext_diff) /* number of new extents */
1896{
1897 int i; /* loop counter */
1898 int nlists; /* number of irec's (ex lists */
1899
1900 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1901 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1902 for (i = erp_idx; i < nlists; i++) {
1903 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
1904 }
1905}