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