blob: 86a97f8a9de38ad7b13392b6cc086e7700ef203d [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_mount.h"
Dave Chinner5c4d97d2013-08-12 20:49:33 +100026#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110027#include "xfs_trans.h"
Dave Chinner5c4d97d2013-08-12 20:49:33 +100028#include "xfs_inode_item.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110029#include "xfs_bmap_btree.h"
Dave Chinner5c4d97d2013-08-12 20:49:33 +100030#include "xfs_bmap.h"
31#include "xfs_error.h"
Dave Chinner5c4d97d2013-08-12 20:49:33 +100032#include "xfs_trace.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110033#include "xfs_attr_sf.h"
Darrick J. Wong244efea2016-02-08 15:00:01 +110034#include "xfs_da_format.h"
Dave Chinner5c4d97d2013-08-12 20:49:33 +100035
36kmem_zone_t *xfs_ifork_zone;
37
38STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
39STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
40STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
41
42#ifdef DEBUG
43/*
44 * Make sure that the extents in the given memory buffer
45 * are valid.
46 */
47void
48xfs_validate_extents(
49 xfs_ifork_t *ifp,
50 int nrecs,
51 xfs_exntfmt_t fmt)
52{
53 xfs_bmbt_irec_t irec;
54 xfs_bmbt_rec_host_t rec;
55 int i;
56
57 for (i = 0; i < nrecs; i++) {
58 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
59 rec.l0 = get_unaligned(&ep->l0);
60 rec.l1 = get_unaligned(&ep->l1);
61 xfs_bmbt_get_all(&rec, &irec);
62 if (fmt == XFS_EXTFMT_NOSTATE)
63 ASSERT(irec.br_state == XFS_EXT_NORM);
64 }
65}
66#else /* DEBUG */
67#define xfs_validate_extents(ifp, nrecs, fmt)
68#endif /* DEBUG */
69
70
71/*
72 * Move inode type and inode format specific information from the
73 * on-disk inode to the in-core inode. For fifos, devs, and sockets
74 * this means set if_rdev to the proper value. For files, directories,
75 * and symlinks this means to bring in the in-line data or extent
76 * pointers. For a file in B-tree format, only the root is immediately
77 * brought in-core. The rest will be in-lined in if_extents when it
78 * is first referenced (see xfs_iread_extents()).
79 */
80int
81xfs_iformat_fork(
82 xfs_inode_t *ip,
83 xfs_dinode_t *dip)
84{
85 xfs_attr_shortform_t *atp;
86 int size;
87 int error = 0;
88 xfs_fsize_t di_size;
89
90 if (unlikely(be32_to_cpu(dip->di_nextents) +
91 be16_to_cpu(dip->di_anextents) >
92 be64_to_cpu(dip->di_nblocks))) {
93 xfs_warn(ip->i_mount,
94 "corrupt dinode %Lu, extent total = %d, nblocks = %Lu.",
95 (unsigned long long)ip->i_ino,
96 (int)(be32_to_cpu(dip->di_nextents) +
97 be16_to_cpu(dip->di_anextents)),
98 (unsigned long long)
99 be64_to_cpu(dip->di_nblocks));
100 XFS_CORRUPTION_ERROR("xfs_iformat(1)", XFS_ERRLEVEL_LOW,
101 ip->i_mount, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000102 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000103 }
104
105 if (unlikely(dip->di_forkoff > ip->i_mount->m_sb.sb_inodesize)) {
106 xfs_warn(ip->i_mount, "corrupt dinode %Lu, forkoff = 0x%x.",
107 (unsigned long long)ip->i_ino,
108 dip->di_forkoff);
109 XFS_CORRUPTION_ERROR("xfs_iformat(2)", XFS_ERRLEVEL_LOW,
110 ip->i_mount, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000111 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000112 }
113
114 if (unlikely((ip->i_d.di_flags & XFS_DIFLAG_REALTIME) &&
115 !ip->i_mount->m_rtdev_targp)) {
116 xfs_warn(ip->i_mount,
117 "corrupt dinode %Lu, has realtime flag set.",
118 ip->i_ino);
119 XFS_CORRUPTION_ERROR("xfs_iformat(realtime)",
120 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000121 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000122 }
123
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100124 switch (VFS_I(ip)->i_mode & S_IFMT) {
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000125 case S_IFIFO:
126 case S_IFCHR:
127 case S_IFBLK:
128 case S_IFSOCK:
129 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
130 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
131 ip->i_mount, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000132 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000133 }
134 ip->i_d.di_size = 0;
135 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
136 break;
137
138 case S_IFREG:
139 case S_IFLNK:
140 case S_IFDIR:
141 switch (dip->di_format) {
142 case XFS_DINODE_FMT_LOCAL:
143 /*
144 * no local regular files yet
145 */
146 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
147 xfs_warn(ip->i_mount,
148 "corrupt inode %Lu (local format for regular file).",
149 (unsigned long long) ip->i_ino);
150 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
151 XFS_ERRLEVEL_LOW,
152 ip->i_mount, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000153 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000154 }
155
156 di_size = be64_to_cpu(dip->di_size);
Dan Carpenter0d0ab122013-08-15 08:53:38 +0300157 if (unlikely(di_size < 0 ||
158 di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000159 xfs_warn(ip->i_mount,
160 "corrupt inode %Lu (bad size %Ld for local inode).",
161 (unsigned long long) ip->i_ino,
162 (long long) di_size);
163 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
164 XFS_ERRLEVEL_LOW,
165 ip->i_mount, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000166 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000167 }
168
169 size = (int)di_size;
170 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
171 break;
172 case XFS_DINODE_FMT_EXTENTS:
173 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
174 break;
175 case XFS_DINODE_FMT_BTREE:
176 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
177 break;
178 default:
179 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
180 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +1000181 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000182 }
183 break;
184
185 default:
186 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +1000187 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000188 }
189 if (error) {
190 return error;
191 }
192 if (!XFS_DFORK_Q(dip))
193 return 0;
194
195 ASSERT(ip->i_afp == NULL);
196 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
197
198 switch (dip->di_aformat) {
199 case XFS_DINODE_FMT_LOCAL:
200 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
201 size = be16_to_cpu(atp->hdr.totsize);
202
203 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
204 xfs_warn(ip->i_mount,
205 "corrupt inode %Lu (bad attr fork size %Ld).",
206 (unsigned long long) ip->i_ino,
207 (long long) size);
208 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
209 XFS_ERRLEVEL_LOW,
210 ip->i_mount, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000211 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000212 }
213
214 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
215 break;
216 case XFS_DINODE_FMT_EXTENTS:
217 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
218 break;
219 case XFS_DINODE_FMT_BTREE:
220 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
221 break;
222 default:
Dave Chinner24513372014-06-25 14:58:08 +1000223 error = -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000224 break;
225 }
226 if (error) {
227 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
228 ip->i_afp = NULL;
229 xfs_idestroy_fork(ip, XFS_DATA_FORK);
230 }
231 return error;
232}
233
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000234void
235xfs_init_local_fork(
236 struct xfs_inode *ip,
237 int whichfork,
238 const void *data,
239 int size)
240{
241 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
242 int real_size = 0;
243
244 if (size == 0)
245 ifp->if_u1.if_data = NULL;
246 else if (size <= sizeof(ifp->if_u2.if_inline_data))
247 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
248 else {
249 real_size = roundup(size, 4);
250 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
251 }
252
253 if (size)
254 memcpy(ifp->if_u1.if_data, data, size);
255
256 ifp->if_bytes = size;
257 ifp->if_real_bytes = real_size;
258 ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
259 ifp->if_flags |= XFS_IFINLINE;
260}
261
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000262/*
263 * The file is in-lined in the on-disk inode.
264 * If it fits into if_inline_data, then copy
265 * it there, otherwise allocate a buffer for it
266 * and copy the data there. Either way, set
267 * if_data to point at the data.
268 * If we allocate a buffer for the data, make
269 * sure that its size is a multiple of 4 and
270 * record the real size in i_real_bytes.
271 */
272STATIC int
273xfs_iformat_local(
274 xfs_inode_t *ip,
275 xfs_dinode_t *dip,
276 int whichfork,
277 int size)
278{
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000279
280 /*
281 * If the size is unreasonable, then something
282 * is wrong and we just bail out rather than crash in
283 * kmem_alloc() or memcpy() below.
284 */
285 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
286 xfs_warn(ip->i_mount,
287 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
288 (unsigned long long) ip->i_ino, size,
289 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
290 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
291 ip->i_mount, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000292 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000293 }
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000294
295 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000296 return 0;
297}
298
299/*
300 * The file consists of a set of extents all
301 * of which fit into the on-disk inode.
302 * If there are few enough extents to fit into
303 * the if_inline_ext, then copy them there.
304 * Otherwise allocate a buffer for them and copy
305 * them into it. Either way, set if_extents
306 * to point at the extents.
307 */
308STATIC int
309xfs_iformat_extents(
310 xfs_inode_t *ip,
311 xfs_dinode_t *dip,
312 int whichfork)
313{
314 xfs_bmbt_rec_t *dp;
315 xfs_ifork_t *ifp;
316 int nex;
317 int size;
318 int i;
319
320 ifp = XFS_IFORK_PTR(ip, whichfork);
321 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
322 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
323
324 /*
325 * If the number of extents is unreasonable, then something
326 * is wrong and we just bail out rather than crash in
327 * kmem_alloc() or memcpy() below.
328 */
329 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
330 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
331 (unsigned long long) ip->i_ino, nex);
332 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
333 ip->i_mount, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000334 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000335 }
336
337 ifp->if_real_bytes = 0;
338 if (nex == 0)
339 ifp->if_u1.if_extents = NULL;
340 else if (nex <= XFS_INLINE_EXTS)
341 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
342 else
343 xfs_iext_add(ifp, 0, nex);
344
345 ifp->if_bytes = size;
346 if (size) {
347 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
348 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
349 for (i = 0; i < nex; i++, dp++) {
350 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
351 ep->l0 = get_unaligned_be64(&dp->l0);
352 ep->l1 = get_unaligned_be64(&dp->l1);
353 }
354 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
355 if (whichfork != XFS_DATA_FORK ||
356 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
357 if (unlikely(xfs_check_nostate_extents(
358 ifp, 0, nex))) {
359 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
360 XFS_ERRLEVEL_LOW,
361 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +1000362 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000363 }
364 }
365 ifp->if_flags |= XFS_IFEXTENTS;
366 return 0;
367}
368
369/*
370 * The file has too many extents to fit into
371 * the inode, so they are in B-tree format.
372 * Allocate a buffer for the root of the B-tree
373 * and copy the root into it. The i_extents
374 * field will remain NULL until all of the
375 * extents are read in (when they are needed).
376 */
377STATIC int
378xfs_iformat_btree(
379 xfs_inode_t *ip,
380 xfs_dinode_t *dip,
381 int whichfork)
382{
383 struct xfs_mount *mp = ip->i_mount;
384 xfs_bmdr_block_t *dfp;
385 xfs_ifork_t *ifp;
386 /* REFERENCED */
387 int nrecs;
388 int size;
389
390 ifp = XFS_IFORK_PTR(ip, whichfork);
391 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
392 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
393 nrecs = be16_to_cpu(dfp->bb_numrecs);
394
395 /*
396 * blow out if -- fork has less extents than can fit in
397 * fork (fork shouldn't be a btree format), root btree
398 * block has more records than can fit into the fork,
399 * or the number of extents is greater than the number of
400 * blocks.
401 */
402 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
403 XFS_IFORK_MAXEXT(ip, whichfork) ||
404 XFS_BMDR_SPACE_CALC(nrecs) >
405 XFS_DFORK_SIZE(dip, mp, whichfork) ||
406 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
407 xfs_warn(mp, "corrupt inode %Lu (btree).",
408 (unsigned long long) ip->i_ino);
409 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
410 mp, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000411 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000412 }
413
414 ifp->if_broot_bytes = size;
415 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
416 ASSERT(ifp->if_broot != NULL);
417 /*
418 * Copy and convert from the on-disk structure
419 * to the in-memory structure.
420 */
421 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
422 ifp->if_broot, size);
423 ifp->if_flags &= ~XFS_IFEXTENTS;
424 ifp->if_flags |= XFS_IFBROOT;
425
426 return 0;
427}
428
429/*
430 * Read in extents from a btree-format inode.
431 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
432 */
433int
434xfs_iread_extents(
435 xfs_trans_t *tp,
436 xfs_inode_t *ip,
437 int whichfork)
438{
439 int error;
440 xfs_ifork_t *ifp;
441 xfs_extnum_t nextents;
442
Christoph Hellwigeef334e2013-12-06 12:30:17 -0800443 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
444
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000445 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);
Dave Chinner24513372014-06-25 14:58:08 +1000448 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000449 }
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
Zhi Yong Wuf6c27342013-08-07 10:11:04 +0000478 * if we are adding records, one will be allocated. The caller must also
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000479 * 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));
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000540 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000541 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);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000587 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000588 }
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/*
Christoph Hellwigda776502013-12-13 11:34:04 +1100735 * Convert in-core extents to on-disk form
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000736 *
Christoph Hellwigda776502013-12-13 11:34:04 +1100737 * For either the data or attr fork in extent format, we need to endian convert
738 * the in-core extent as we place them into the on-disk inode.
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000739 *
Christoph Hellwigda776502013-12-13 11:34:04 +1100740 * In the case of the data fork, the in-core and on-disk fork sizes can be
741 * different due to delayed allocation extents. We only copy on-disk extents
742 * here, so callers must always use the physical fork size to determine the
743 * size of the buffer passed to this routine. We will return the size actually
744 * used.
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000745 */
746int
747xfs_iextents_copy(
748 xfs_inode_t *ip,
749 xfs_bmbt_rec_t *dp,
750 int whichfork)
751{
752 int copied;
753 int i;
754 xfs_ifork_t *ifp;
755 int nrecs;
756 xfs_fsblock_t start_block;
757
758 ifp = XFS_IFORK_PTR(ip, whichfork);
759 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
760 ASSERT(ifp->if_bytes > 0);
761
762 nrecs = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
763 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
764 ASSERT(nrecs > 0);
765
766 /*
767 * There are some delayed allocation extents in the
768 * inode, so copy the extents one at a time and skip
769 * the delayed ones. There must be at least one
770 * non-delayed extent.
771 */
772 copied = 0;
773 for (i = 0; i < nrecs; i++) {
774 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
775 start_block = xfs_bmbt_get_startblock(ep);
776 if (isnullstartblock(start_block)) {
777 /*
778 * It's a delayed allocation extent, so skip it.
779 */
780 continue;
781 }
782
783 /* Translate to on disk format */
Dave Chinnerc5c249b2013-08-12 20:49:43 +1000784 put_unaligned_be64(ep->l0, &dp->l0);
785 put_unaligned_be64(ep->l1, &dp->l1);
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000786 dp++;
787 copied++;
788 }
789 ASSERT(copied != 0);
790 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
791
792 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
793}
794
795/*
796 * Each of the following cases stores data into the same region
797 * of the on-disk inode, so only one of them can be valid at
798 * any given time. While it is possible to have conflicting formats
799 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
800 * in EXTENTS format, this can only happen when the fork has
801 * changed formats after being modified but before being flushed.
802 * In these cases, the format always takes precedence, because the
803 * format indicates the current state of the fork.
804 */
805void
806xfs_iflush_fork(
807 xfs_inode_t *ip,
808 xfs_dinode_t *dip,
809 xfs_inode_log_item_t *iip,
Eric Sandeenfd9fdba2014-04-14 19:04:46 +1000810 int whichfork)
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000811{
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 {
Jie Liubb86d212013-10-25 14:52:44 +08001035 uint count = ext_diff;
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001036
1037 while (count) {
1038 erp = xfs_iext_irec_new(ifp, erp_idx);
Jie Liubb86d212013-10-25 14:52:44 +08001039 erp->er_extcount = min(count, XFS_LINEAR_EXTS);
1040 count -= erp->er_extcount;
1041 if (count)
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001042 erp_idx++;
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001043 }
1044 }
1045 }
1046 ifp->if_bytes = new_size;
1047}
1048
1049/*
1050 * This is called when incore extents are being added to the indirection
1051 * array and the new extents do not fit in the target extent list. The
1052 * erp_idx parameter contains the irec index for the target extent list
1053 * in the indirection array, and the idx parameter contains the extent
1054 * index within the list. The number of extents being added is stored
1055 * in the count parameter.
1056 *
1057 * |-------| |-------|
1058 * | | | | idx - number of extents before idx
1059 * | idx | | count |
1060 * | | | | count - number of extents being inserted at idx
1061 * |-------| |-------|
1062 * | count | | nex2 | nex2 - number of extents after idx + count
1063 * |-------| |-------|
1064 */
1065void
1066xfs_iext_add_indirect_multi(
1067 xfs_ifork_t *ifp, /* inode fork pointer */
1068 int erp_idx, /* target extent irec index */
1069 xfs_extnum_t idx, /* index within target list */
1070 int count) /* new extents being added */
1071{
1072 int byte_diff; /* new bytes being added */
1073 xfs_ext_irec_t *erp; /* pointer to irec entry */
1074 xfs_extnum_t ext_diff; /* number of extents to add */
1075 xfs_extnum_t ext_cnt; /* new extents still needed */
1076 xfs_extnum_t nex2; /* extents after idx + count */
1077 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
1078 int nlists; /* number of irec's (lists) */
1079
1080 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1081 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1082 nex2 = erp->er_extcount - idx;
1083 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1084
1085 /*
1086 * Save second part of target extent list
1087 * (all extents past */
1088 if (nex2) {
1089 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1090 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
1091 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
1092 erp->er_extcount -= nex2;
1093 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
1094 memset(&erp->er_extbuf[idx], 0, byte_diff);
1095 }
1096
1097 /*
1098 * Add the new extents to the end of the target
1099 * list, then allocate new irec record(s) and
1100 * extent buffer(s) as needed to store the rest
1101 * of the new extents.
1102 */
1103 ext_cnt = count;
1104 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
1105 if (ext_diff) {
1106 erp->er_extcount += ext_diff;
1107 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1108 ext_cnt -= ext_diff;
1109 }
1110 while (ext_cnt) {
1111 erp_idx++;
1112 erp = xfs_iext_irec_new(ifp, erp_idx);
1113 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
1114 erp->er_extcount = ext_diff;
1115 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1116 ext_cnt -= ext_diff;
1117 }
1118
1119 /* Add nex2 extents back to indirection array */
1120 if (nex2) {
1121 xfs_extnum_t ext_avail;
1122 int i;
1123
1124 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1125 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
1126 i = 0;
1127 /*
1128 * If nex2 extents fit in the current page, append
1129 * nex2_ep after the new extents.
1130 */
1131 if (nex2 <= ext_avail) {
1132 i = erp->er_extcount;
1133 }
1134 /*
1135 * Otherwise, check if space is available in the
1136 * next page.
1137 */
1138 else if ((erp_idx < nlists - 1) &&
1139 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
1140 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
1141 erp_idx++;
1142 erp++;
1143 /* Create a hole for nex2 extents */
1144 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
1145 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
1146 }
1147 /*
1148 * Final choice, create a new extent page for
1149 * nex2 extents.
1150 */
1151 else {
1152 erp_idx++;
1153 erp = xfs_iext_irec_new(ifp, erp_idx);
1154 }
1155 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
1156 kmem_free(nex2_ep);
1157 erp->er_extcount += nex2;
1158 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
1159 }
1160}
1161
1162/*
1163 * This is called when the amount of space required for incore file
1164 * extents needs to be decreased. The ext_diff parameter stores the
1165 * number of extents to be removed and the idx parameter contains
1166 * the extent index where the extents will be removed from.
1167 *
1168 * If the amount of space needed has decreased below the linear
1169 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1170 * extent array. Otherwise, use kmem_realloc() to adjust the
1171 * size to what is needed.
1172 */
1173void
1174xfs_iext_remove(
1175 xfs_inode_t *ip, /* incore inode pointer */
1176 xfs_extnum_t idx, /* index to begin removing exts */
1177 int ext_diff, /* number of extents to remove */
1178 int state) /* type of extent conversion */
1179{
1180 xfs_ifork_t *ifp = (state & BMAP_ATTRFORK) ? ip->i_afp : &ip->i_df;
1181 xfs_extnum_t nextents; /* number of extents in file */
1182 int new_size; /* size of extents after removal */
1183
1184 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
1185
1186 ASSERT(ext_diff > 0);
1187 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1188 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
1189
1190 if (new_size == 0) {
1191 xfs_iext_destroy(ifp);
1192 } else if (ifp->if_flags & XFS_IFEXTIREC) {
1193 xfs_iext_remove_indirect(ifp, idx, ext_diff);
1194 } else if (ifp->if_real_bytes) {
1195 xfs_iext_remove_direct(ifp, idx, ext_diff);
1196 } else {
1197 xfs_iext_remove_inline(ifp, idx, ext_diff);
1198 }
1199 ifp->if_bytes = new_size;
1200}
1201
1202/*
1203 * This removes ext_diff extents from the inline buffer, beginning
1204 * at extent index idx.
1205 */
1206void
1207xfs_iext_remove_inline(
1208 xfs_ifork_t *ifp, /* inode fork pointer */
1209 xfs_extnum_t idx, /* index to begin removing exts */
1210 int ext_diff) /* number of extents to remove */
1211{
1212 int nextents; /* number of extents in file */
1213
1214 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1215 ASSERT(idx < XFS_INLINE_EXTS);
1216 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1217 ASSERT(((nextents - ext_diff) > 0) &&
1218 (nextents - ext_diff) < XFS_INLINE_EXTS);
1219
1220 if (idx + ext_diff < nextents) {
1221 memmove(&ifp->if_u2.if_inline_ext[idx],
1222 &ifp->if_u2.if_inline_ext[idx + ext_diff],
1223 (nextents - (idx + ext_diff)) *
1224 sizeof(xfs_bmbt_rec_t));
1225 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
1226 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1227 } else {
1228 memset(&ifp->if_u2.if_inline_ext[idx], 0,
1229 ext_diff * sizeof(xfs_bmbt_rec_t));
1230 }
1231}
1232
1233/*
1234 * This removes ext_diff extents from a linear (direct) extent list,
1235 * beginning at extent index idx. If the extents are being removed
1236 * from the end of the list (ie. truncate) then we just need to re-
1237 * allocate the list to remove the extra space. Otherwise, if the
1238 * extents are being removed from the middle of the existing extent
1239 * entries, then we first need to move the extent records beginning
1240 * at idx + ext_diff up in the list to overwrite the records being
1241 * removed, then remove the extra space via kmem_realloc.
1242 */
1243void
1244xfs_iext_remove_direct(
1245 xfs_ifork_t *ifp, /* inode fork pointer */
1246 xfs_extnum_t idx, /* index to begin removing exts */
1247 int ext_diff) /* number of extents to remove */
1248{
1249 xfs_extnum_t nextents; /* number of extents in file */
1250 int new_size; /* size of extents after removal */
1251
1252 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1253 new_size = ifp->if_bytes -
1254 (ext_diff * sizeof(xfs_bmbt_rec_t));
1255 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1256
1257 if (new_size == 0) {
1258 xfs_iext_destroy(ifp);
1259 return;
1260 }
1261 /* Move extents up in the list (if needed) */
1262 if (idx + ext_diff < nextents) {
1263 memmove(&ifp->if_u1.if_extents[idx],
1264 &ifp->if_u1.if_extents[idx + ext_diff],
1265 (nextents - (idx + ext_diff)) *
1266 sizeof(xfs_bmbt_rec_t));
1267 }
1268 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
1269 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1270 /*
1271 * Reallocate the direct extent list. If the extents
1272 * will fit inside the inode then xfs_iext_realloc_direct
1273 * will switch from direct to inline extent allocation
1274 * mode for us.
1275 */
1276 xfs_iext_realloc_direct(ifp, new_size);
1277 ifp->if_bytes = new_size;
1278}
1279
1280/*
1281 * This is called when incore extents are being removed from the
1282 * indirection array and the extents being removed span multiple extent
1283 * buffers. The idx parameter contains the file extent index where we
1284 * want to begin removing extents, and the count parameter contains
1285 * how many extents need to be removed.
1286 *
1287 * |-------| |-------|
1288 * | nex1 | | | nex1 - number of extents before idx
1289 * |-------| | count |
1290 * | | | | count - number of extents being removed at idx
1291 * | count | |-------|
1292 * | | | nex2 | nex2 - number of extents after idx + count
1293 * |-------| |-------|
1294 */
1295void
1296xfs_iext_remove_indirect(
1297 xfs_ifork_t *ifp, /* inode fork pointer */
1298 xfs_extnum_t idx, /* index to begin removing extents */
1299 int count) /* number of extents to remove */
1300{
1301 xfs_ext_irec_t *erp; /* indirection array pointer */
1302 int erp_idx = 0; /* indirection array index */
1303 xfs_extnum_t ext_cnt; /* extents left to remove */
1304 xfs_extnum_t ext_diff; /* extents to remove in current list */
1305 xfs_extnum_t nex1; /* number of extents before idx */
1306 xfs_extnum_t nex2; /* extents after idx + count */
1307 int page_idx = idx; /* index in target extent list */
1308
1309 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1310 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
1311 ASSERT(erp != NULL);
1312 nex1 = page_idx;
1313 ext_cnt = count;
1314 while (ext_cnt) {
1315 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
1316 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
1317 /*
1318 * Check for deletion of entire list;
1319 * xfs_iext_irec_remove() updates extent offsets.
1320 */
1321 if (ext_diff == erp->er_extcount) {
1322 xfs_iext_irec_remove(ifp, erp_idx);
1323 ext_cnt -= ext_diff;
1324 nex1 = 0;
1325 if (ext_cnt) {
1326 ASSERT(erp_idx < ifp->if_real_bytes /
1327 XFS_IEXT_BUFSZ);
1328 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1329 nex1 = 0;
1330 continue;
1331 } else {
1332 break;
1333 }
1334 }
1335 /* Move extents up (if needed) */
1336 if (nex2) {
1337 memmove(&erp->er_extbuf[nex1],
1338 &erp->er_extbuf[nex1 + ext_diff],
1339 nex2 * sizeof(xfs_bmbt_rec_t));
1340 }
1341 /* Zero out rest of page */
1342 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
1343 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
1344 /* Update remaining counters */
1345 erp->er_extcount -= ext_diff;
1346 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
1347 ext_cnt -= ext_diff;
1348 nex1 = 0;
1349 erp_idx++;
1350 erp++;
1351 }
1352 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
1353 xfs_iext_irec_compact(ifp);
1354}
1355
1356/*
1357 * Create, destroy, or resize a linear (direct) block of extents.
1358 */
1359void
1360xfs_iext_realloc_direct(
1361 xfs_ifork_t *ifp, /* inode fork pointer */
Jie Liu17ec81c2013-09-22 16:25:15 +08001362 int new_size) /* new size of extents after adding */
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001363{
1364 int rnew_size; /* real new size of extents */
1365
1366 rnew_size = new_size;
1367
1368 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
1369 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
1370 (new_size != ifp->if_real_bytes)));
1371
1372 /* Free extent records */
1373 if (new_size == 0) {
1374 xfs_iext_destroy(ifp);
1375 }
1376 /* Resize direct extent list and zero any new bytes */
1377 else if (ifp->if_real_bytes) {
1378 /* Check if extents will fit inside the inode */
1379 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
1380 xfs_iext_direct_to_inline(ifp, new_size /
1381 (uint)sizeof(xfs_bmbt_rec_t));
1382 ifp->if_bytes = new_size;
1383 return;
1384 }
1385 if (!is_power_of_2(new_size)){
1386 rnew_size = roundup_pow_of_two(new_size);
1387 }
1388 if (rnew_size != ifp->if_real_bytes) {
1389 ifp->if_u1.if_extents =
1390 kmem_realloc(ifp->if_u1.if_extents,
1391 rnew_size,
1392 ifp->if_real_bytes, KM_NOFS);
1393 }
1394 if (rnew_size > ifp->if_real_bytes) {
1395 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
1396 (uint)sizeof(xfs_bmbt_rec_t)], 0,
1397 rnew_size - ifp->if_real_bytes);
1398 }
1399 }
Jie Liu17ec81c2013-09-22 16:25:15 +08001400 /* Switch from the inline extent buffer to a direct extent list */
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001401 else {
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001402 if (!is_power_of_2(new_size)) {
1403 rnew_size = roundup_pow_of_two(new_size);
1404 }
1405 xfs_iext_inline_to_direct(ifp, rnew_size);
1406 }
1407 ifp->if_real_bytes = rnew_size;
1408 ifp->if_bytes = new_size;
1409}
1410
1411/*
1412 * Switch from linear (direct) extent records to inline buffer.
1413 */
1414void
1415xfs_iext_direct_to_inline(
1416 xfs_ifork_t *ifp, /* inode fork pointer */
1417 xfs_extnum_t nextents) /* number of extents in file */
1418{
1419 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1420 ASSERT(nextents <= XFS_INLINE_EXTS);
1421 /*
1422 * The inline buffer was zeroed when we switched
1423 * from inline to direct extent allocation mode,
1424 * so we don't need to clear it here.
1425 */
1426 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
1427 nextents * sizeof(xfs_bmbt_rec_t));
1428 kmem_free(ifp->if_u1.if_extents);
1429 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1430 ifp->if_real_bytes = 0;
1431}
1432
1433/*
1434 * Switch from inline buffer to linear (direct) extent records.
1435 * new_size should already be rounded up to the next power of 2
1436 * by the caller (when appropriate), so use new_size as it is.
1437 * However, since new_size may be rounded up, we can't update
1438 * if_bytes here. It is the caller's responsibility to update
1439 * if_bytes upon return.
1440 */
1441void
1442xfs_iext_inline_to_direct(
1443 xfs_ifork_t *ifp, /* inode fork pointer */
1444 int new_size) /* number of extents in file */
1445{
1446 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
1447 memset(ifp->if_u1.if_extents, 0, new_size);
1448 if (ifp->if_bytes) {
1449 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
1450 ifp->if_bytes);
1451 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1452 sizeof(xfs_bmbt_rec_t));
1453 }
1454 ifp->if_real_bytes = new_size;
1455}
1456
1457/*
1458 * Resize an extent indirection array to new_size bytes.
1459 */
1460STATIC void
1461xfs_iext_realloc_indirect(
1462 xfs_ifork_t *ifp, /* inode fork pointer */
1463 int new_size) /* new indirection array size */
1464{
1465 int nlists; /* number of irec's (ex lists) */
1466 int size; /* current indirection array size */
1467
1468 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1469 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1470 size = nlists * sizeof(xfs_ext_irec_t);
1471 ASSERT(ifp->if_real_bytes);
1472 ASSERT((new_size >= 0) && (new_size != size));
1473 if (new_size == 0) {
1474 xfs_iext_destroy(ifp);
1475 } else {
1476 ifp->if_u1.if_ext_irec = (xfs_ext_irec_t *)
1477 kmem_realloc(ifp->if_u1.if_ext_irec,
1478 new_size, size, KM_NOFS);
1479 }
1480}
1481
1482/*
1483 * Switch from indirection array to linear (direct) extent allocations.
1484 */
1485STATIC void
1486xfs_iext_indirect_to_direct(
1487 xfs_ifork_t *ifp) /* inode fork pointer */
1488{
1489 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1490 xfs_extnum_t nextents; /* number of extents in file */
1491 int size; /* size of file extents */
1492
1493 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1494 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1495 ASSERT(nextents <= XFS_LINEAR_EXTS);
1496 size = nextents * sizeof(xfs_bmbt_rec_t);
1497
1498 xfs_iext_irec_compact_pages(ifp);
1499 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
1500
1501 ep = ifp->if_u1.if_ext_irec->er_extbuf;
1502 kmem_free(ifp->if_u1.if_ext_irec);
1503 ifp->if_flags &= ~XFS_IFEXTIREC;
1504 ifp->if_u1.if_extents = ep;
1505 ifp->if_bytes = size;
1506 if (nextents < XFS_LINEAR_EXTS) {
1507 xfs_iext_realloc_direct(ifp, size);
1508 }
1509}
1510
1511/*
1512 * Free incore file extents.
1513 */
1514void
1515xfs_iext_destroy(
1516 xfs_ifork_t *ifp) /* inode fork pointer */
1517{
1518 if (ifp->if_flags & XFS_IFEXTIREC) {
1519 int erp_idx;
1520 int nlists;
1521
1522 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1523 for (erp_idx = nlists - 1; erp_idx >= 0 ; erp_idx--) {
1524 xfs_iext_irec_remove(ifp, erp_idx);
1525 }
1526 ifp->if_flags &= ~XFS_IFEXTIREC;
1527 } else if (ifp->if_real_bytes) {
1528 kmem_free(ifp->if_u1.if_extents);
1529 } else if (ifp->if_bytes) {
1530 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1531 sizeof(xfs_bmbt_rec_t));
1532 }
1533 ifp->if_u1.if_extents = NULL;
1534 ifp->if_real_bytes = 0;
1535 ifp->if_bytes = 0;
1536}
1537
1538/*
1539 * Return a pointer to the extent record for file system block bno.
1540 */
1541xfs_bmbt_rec_host_t * /* pointer to found extent record */
1542xfs_iext_bno_to_ext(
1543 xfs_ifork_t *ifp, /* inode fork pointer */
1544 xfs_fileoff_t bno, /* block number to search for */
1545 xfs_extnum_t *idxp) /* index of target extent */
1546{
1547 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
1548 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
1549 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
1550 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1551 int high; /* upper boundary in search */
1552 xfs_extnum_t idx = 0; /* index of target extent */
1553 int low; /* lower boundary in search */
1554 xfs_extnum_t nextents; /* number of file extents */
1555 xfs_fileoff_t startoff = 0; /* start offset of extent */
1556
1557 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1558 if (nextents == 0) {
1559 *idxp = 0;
1560 return NULL;
1561 }
1562 low = 0;
1563 if (ifp->if_flags & XFS_IFEXTIREC) {
1564 /* Find target extent list */
1565 int erp_idx = 0;
1566 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
1567 base = erp->er_extbuf;
1568 high = erp->er_extcount - 1;
1569 } else {
1570 base = ifp->if_u1.if_extents;
1571 high = nextents - 1;
1572 }
1573 /* Binary search extent records */
1574 while (low <= high) {
1575 idx = (low + high) >> 1;
1576 ep = base + idx;
1577 startoff = xfs_bmbt_get_startoff(ep);
1578 blockcount = xfs_bmbt_get_blockcount(ep);
1579 if (bno < startoff) {
1580 high = idx - 1;
1581 } else if (bno >= startoff + blockcount) {
1582 low = idx + 1;
1583 } else {
1584 /* Convert back to file-based extent index */
1585 if (ifp->if_flags & XFS_IFEXTIREC) {
1586 idx += erp->er_extoff;
1587 }
1588 *idxp = idx;
1589 return ep;
1590 }
1591 }
1592 /* Convert back to file-based extent index */
1593 if (ifp->if_flags & XFS_IFEXTIREC) {
1594 idx += erp->er_extoff;
1595 }
1596 if (bno >= startoff + blockcount) {
1597 if (++idx == nextents) {
1598 ep = NULL;
1599 } else {
1600 ep = xfs_iext_get_ext(ifp, idx);
1601 }
1602 }
1603 *idxp = idx;
1604 return ep;
1605}
1606
1607/*
1608 * Return a pointer to the indirection array entry containing the
1609 * extent record for filesystem block bno. Store the index of the
1610 * target irec in *erp_idxp.
1611 */
1612xfs_ext_irec_t * /* pointer to found extent record */
1613xfs_iext_bno_to_irec(
1614 xfs_ifork_t *ifp, /* inode fork pointer */
1615 xfs_fileoff_t bno, /* block number to search for */
1616 int *erp_idxp) /* irec index of target ext list */
1617{
1618 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1619 xfs_ext_irec_t *erp_next; /* next indirection array entry */
1620 int erp_idx; /* indirection array index */
1621 int nlists; /* number of extent irec's (lists) */
1622 int high; /* binary search upper limit */
1623 int low; /* binary search lower limit */
1624
1625 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1626 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1627 erp_idx = 0;
1628 low = 0;
1629 high = nlists - 1;
1630 while (low <= high) {
1631 erp_idx = (low + high) >> 1;
1632 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1633 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
1634 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
1635 high = erp_idx - 1;
1636 } else if (erp_next && bno >=
1637 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
1638 low = erp_idx + 1;
1639 } else {
1640 break;
1641 }
1642 }
1643 *erp_idxp = erp_idx;
1644 return erp;
1645}
1646
1647/*
1648 * Return a pointer to the indirection array entry containing the
1649 * extent record at file extent index *idxp. Store the index of the
1650 * target irec in *erp_idxp and store the page index of the target
1651 * extent record in *idxp.
1652 */
1653xfs_ext_irec_t *
1654xfs_iext_idx_to_irec(
1655 xfs_ifork_t *ifp, /* inode fork pointer */
1656 xfs_extnum_t *idxp, /* extent index (file -> page) */
1657 int *erp_idxp, /* pointer to target irec */
1658 int realloc) /* new bytes were just added */
1659{
1660 xfs_ext_irec_t *prev; /* pointer to previous irec */
1661 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
1662 int erp_idx; /* indirection array index */
1663 int nlists; /* number of irec's (ex lists) */
1664 int high; /* binary search upper limit */
1665 int low; /* binary search lower limit */
1666 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
1667
1668 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1669 ASSERT(page_idx >= 0);
1670 ASSERT(page_idx <= ifp->if_bytes / sizeof(xfs_bmbt_rec_t));
1671 ASSERT(page_idx < ifp->if_bytes / sizeof(xfs_bmbt_rec_t) || realloc);
1672
1673 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1674 erp_idx = 0;
1675 low = 0;
1676 high = nlists - 1;
1677
1678 /* Binary search extent irec's */
1679 while (low <= high) {
1680 erp_idx = (low + high) >> 1;
1681 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1682 prev = erp_idx > 0 ? erp - 1 : NULL;
1683 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
1684 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
1685 high = erp_idx - 1;
1686 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
1687 (page_idx == erp->er_extoff + erp->er_extcount &&
1688 !realloc)) {
1689 low = erp_idx + 1;
1690 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
1691 erp->er_extcount == XFS_LINEAR_EXTS) {
1692 ASSERT(realloc);
1693 page_idx = 0;
1694 erp_idx++;
1695 erp = erp_idx < nlists ? erp + 1 : NULL;
1696 break;
1697 } else {
1698 page_idx -= erp->er_extoff;
1699 break;
1700 }
1701 }
1702 *idxp = page_idx;
1703 *erp_idxp = erp_idx;
Eric Sandeend99831f2014-06-22 15:03:54 +10001704 return erp;
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001705}
1706
1707/*
1708 * Allocate and initialize an indirection array once the space needed
1709 * for incore extents increases above XFS_IEXT_BUFSZ.
1710 */
1711void
1712xfs_iext_irec_init(
1713 xfs_ifork_t *ifp) /* inode fork pointer */
1714{
1715 xfs_ext_irec_t *erp; /* indirection array pointer */
1716 xfs_extnum_t nextents; /* number of extents in file */
1717
1718 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1719 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1720 ASSERT(nextents <= XFS_LINEAR_EXTS);
1721
1722 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
1723
1724 if (nextents == 0) {
1725 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1726 } else if (!ifp->if_real_bytes) {
1727 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
1728 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
1729 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
1730 }
1731 erp->er_extbuf = ifp->if_u1.if_extents;
1732 erp->er_extcount = nextents;
1733 erp->er_extoff = 0;
1734
1735 ifp->if_flags |= XFS_IFEXTIREC;
1736 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
1737 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
1738 ifp->if_u1.if_ext_irec = erp;
1739
1740 return;
1741}
1742
1743/*
1744 * Allocate and initialize a new entry in the indirection array.
1745 */
1746xfs_ext_irec_t *
1747xfs_iext_irec_new(
1748 xfs_ifork_t *ifp, /* inode fork pointer */
1749 int erp_idx) /* index for new irec */
1750{
1751 xfs_ext_irec_t *erp; /* indirection array pointer */
1752 int i; /* loop counter */
1753 int nlists; /* number of irec's (ex lists) */
1754
1755 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1756 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1757
1758 /* Resize indirection array */
1759 xfs_iext_realloc_indirect(ifp, ++nlists *
1760 sizeof(xfs_ext_irec_t));
1761 /*
1762 * Move records down in the array so the
1763 * new page can use erp_idx.
1764 */
1765 erp = ifp->if_u1.if_ext_irec;
1766 for (i = nlists - 1; i > erp_idx; i--) {
1767 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
1768 }
1769 ASSERT(i == erp_idx);
1770
1771 /* Initialize new extent record */
1772 erp = ifp->if_u1.if_ext_irec;
1773 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1774 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1775 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
1776 erp[erp_idx].er_extcount = 0;
1777 erp[erp_idx].er_extoff = erp_idx > 0 ?
1778 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
1779 return (&erp[erp_idx]);
1780}
1781
1782/*
1783 * Remove a record from the indirection array.
1784 */
1785void
1786xfs_iext_irec_remove(
1787 xfs_ifork_t *ifp, /* inode fork pointer */
1788 int erp_idx) /* irec index to remove */
1789{
1790 xfs_ext_irec_t *erp; /* indirection array pointer */
1791 int i; /* loop counter */
1792 int nlists; /* number of irec's (ex lists) */
1793
1794 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1795 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1796 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1797 if (erp->er_extbuf) {
1798 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
1799 -erp->er_extcount);
1800 kmem_free(erp->er_extbuf);
1801 }
1802 /* Compact extent records */
1803 erp = ifp->if_u1.if_ext_irec;
1804 for (i = erp_idx; i < nlists - 1; i++) {
1805 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
1806 }
1807 /*
1808 * Manually free the last extent record from the indirection
1809 * array. A call to xfs_iext_realloc_indirect() with a size
1810 * of zero would result in a call to xfs_iext_destroy() which
1811 * would in turn call this function again, creating a nasty
1812 * infinite loop.
1813 */
1814 if (--nlists) {
1815 xfs_iext_realloc_indirect(ifp,
1816 nlists * sizeof(xfs_ext_irec_t));
1817 } else {
1818 kmem_free(ifp->if_u1.if_ext_irec);
1819 }
1820 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1821}
1822
1823/*
1824 * This is called to clean up large amounts of unused memory allocated
1825 * by the indirection array. Before compacting anything though, verify
1826 * that the indirection array is still needed and switch back to the
1827 * linear extent list (or even the inline buffer) if possible. The
1828 * compaction policy is as follows:
1829 *
1830 * Full Compaction: Extents fit into a single page (or inline buffer)
1831 * Partial Compaction: Extents occupy less than 50% of allocated space
1832 * No Compaction: Extents occupy at least 50% of allocated space
1833 */
1834void
1835xfs_iext_irec_compact(
1836 xfs_ifork_t *ifp) /* inode fork pointer */
1837{
1838 xfs_extnum_t nextents; /* number of extents in file */
1839 int nlists; /* number of irec's (ex lists) */
1840
1841 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1842 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1843 nextents = ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
1844
1845 if (nextents == 0) {
1846 xfs_iext_destroy(ifp);
1847 } else if (nextents <= XFS_INLINE_EXTS) {
1848 xfs_iext_indirect_to_direct(ifp);
1849 xfs_iext_direct_to_inline(ifp, nextents);
1850 } else if (nextents <= XFS_LINEAR_EXTS) {
1851 xfs_iext_indirect_to_direct(ifp);
1852 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
1853 xfs_iext_irec_compact_pages(ifp);
1854 }
1855}
1856
1857/*
1858 * Combine extents from neighboring extent pages.
1859 */
1860void
1861xfs_iext_irec_compact_pages(
1862 xfs_ifork_t *ifp) /* inode fork pointer */
1863{
1864 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
1865 int erp_idx = 0; /* indirection array index */
1866 int nlists; /* number of irec's (ex lists) */
1867
1868 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1869 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1870 while (erp_idx < nlists - 1) {
1871 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1872 erp_next = erp + 1;
1873 if (erp_next->er_extcount <=
1874 (XFS_LINEAR_EXTS - erp->er_extcount)) {
1875 memcpy(&erp->er_extbuf[erp->er_extcount],
1876 erp_next->er_extbuf, erp_next->er_extcount *
1877 sizeof(xfs_bmbt_rec_t));
1878 erp->er_extcount += erp_next->er_extcount;
1879 /*
1880 * Free page before removing extent record
1881 * so er_extoffs don't get modified in
1882 * xfs_iext_irec_remove.
1883 */
1884 kmem_free(erp_next->er_extbuf);
1885 erp_next->er_extbuf = NULL;
1886 xfs_iext_irec_remove(ifp, erp_idx + 1);
1887 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1888 } else {
1889 erp_idx++;
1890 }
1891 }
1892}
1893
1894/*
1895 * This is called to update the er_extoff field in the indirection
1896 * array when extents have been added or removed from one of the
1897 * extent lists. erp_idx contains the irec index to begin updating
1898 * at and ext_diff contains the number of extents that were added
1899 * or removed.
1900 */
1901void
1902xfs_iext_irec_update_extoffs(
1903 xfs_ifork_t *ifp, /* inode fork pointer */
1904 int erp_idx, /* irec index to update */
1905 int ext_diff) /* number of new extents */
1906{
1907 int i; /* loop counter */
1908 int nlists; /* number of irec's (ex lists */
1909
1910 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1911 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1912 for (i = erp_idx; i < nlists; i++) {
1913 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
1914 }
1915}