blob: 421341f93bea9ab1e965e9cb54316041f1a8b74c [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
Darrick J. Wong11715a22016-10-03 09:11:31 -0700124 if (unlikely(xfs_is_reflink_inode(ip) &&
125 (VFS_I(ip)->i_mode & S_IFMT) != S_IFREG)) {
126 xfs_warn(ip->i_mount,
127 "corrupt dinode %llu, wrong file type for reflink.",
128 ip->i_ino);
129 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
130 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
131 return -EFSCORRUPTED;
132 }
133
134 if (unlikely(xfs_is_reflink_inode(ip) &&
135 (ip->i_d.di_flags & XFS_DIFLAG_REALTIME))) {
136 xfs_warn(ip->i_mount,
137 "corrupt dinode %llu, has reflink+realtime flag set.",
138 ip->i_ino);
139 XFS_CORRUPTION_ERROR("xfs_iformat(reflink)",
140 XFS_ERRLEVEL_LOW, ip->i_mount, dip);
141 return -EFSCORRUPTED;
142 }
143
Dave Chinnerc19b3b052016-02-09 16:54:58 +1100144 switch (VFS_I(ip)->i_mode & S_IFMT) {
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000145 case S_IFIFO:
146 case S_IFCHR:
147 case S_IFBLK:
148 case S_IFSOCK:
149 if (unlikely(dip->di_format != XFS_DINODE_FMT_DEV)) {
150 XFS_CORRUPTION_ERROR("xfs_iformat(3)", XFS_ERRLEVEL_LOW,
151 ip->i_mount, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000152 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000153 }
154 ip->i_d.di_size = 0;
155 ip->i_df.if_u2.if_rdev = xfs_dinode_get_rdev(dip);
156 break;
157
158 case S_IFREG:
159 case S_IFLNK:
160 case S_IFDIR:
161 switch (dip->di_format) {
162 case XFS_DINODE_FMT_LOCAL:
163 /*
164 * no local regular files yet
165 */
166 if (unlikely(S_ISREG(be16_to_cpu(dip->di_mode)))) {
167 xfs_warn(ip->i_mount,
168 "corrupt inode %Lu (local format for regular file).",
169 (unsigned long long) ip->i_ino);
170 XFS_CORRUPTION_ERROR("xfs_iformat(4)",
171 XFS_ERRLEVEL_LOW,
172 ip->i_mount, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000173 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000174 }
175
176 di_size = be64_to_cpu(dip->di_size);
Dan Carpenter0d0ab122013-08-15 08:53:38 +0300177 if (unlikely(di_size < 0 ||
178 di_size > XFS_DFORK_DSIZE(dip, ip->i_mount))) {
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000179 xfs_warn(ip->i_mount,
180 "corrupt inode %Lu (bad size %Ld for local inode).",
181 (unsigned long long) ip->i_ino,
182 (long long) di_size);
183 XFS_CORRUPTION_ERROR("xfs_iformat(5)",
184 XFS_ERRLEVEL_LOW,
185 ip->i_mount, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000186 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000187 }
188
189 size = (int)di_size;
190 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
191 break;
192 case XFS_DINODE_FMT_EXTENTS:
193 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
194 break;
195 case XFS_DINODE_FMT_BTREE:
196 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
197 break;
198 default:
199 XFS_ERROR_REPORT("xfs_iformat(6)", XFS_ERRLEVEL_LOW,
200 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +1000201 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000202 }
203 break;
204
205 default:
206 XFS_ERROR_REPORT("xfs_iformat(7)", XFS_ERRLEVEL_LOW, ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +1000207 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000208 }
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700209 if (error)
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000210 return error;
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700211
212 if (xfs_is_reflink_inode(ip)) {
213 ASSERT(ip->i_cowfp == NULL);
214 xfs_ifork_init_cow(ip);
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000215 }
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700216
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000217 if (!XFS_DFORK_Q(dip))
218 return 0;
219
220 ASSERT(ip->i_afp == NULL);
221 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
222
223 switch (dip->di_aformat) {
224 case XFS_DINODE_FMT_LOCAL:
225 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
226 size = be16_to_cpu(atp->hdr.totsize);
227
228 if (unlikely(size < sizeof(struct xfs_attr_sf_hdr))) {
229 xfs_warn(ip->i_mount,
230 "corrupt inode %Lu (bad attr fork size %Ld).",
231 (unsigned long long) ip->i_ino,
232 (long long) size);
233 XFS_CORRUPTION_ERROR("xfs_iformat(8)",
234 XFS_ERRLEVEL_LOW,
235 ip->i_mount, dip);
Darrick J. Wong11715a22016-10-03 09:11:31 -0700236 error = -EFSCORRUPTED;
237 break;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000238 }
239
240 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
241 break;
242 case XFS_DINODE_FMT_EXTENTS:
243 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
244 break;
245 case XFS_DINODE_FMT_BTREE:
246 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
247 break;
248 default:
Dave Chinner24513372014-06-25 14:58:08 +1000249 error = -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000250 break;
251 }
252 if (error) {
253 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
254 ip->i_afp = NULL;
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700255 if (ip->i_cowfp)
256 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
257 ip->i_cowfp = NULL;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000258 xfs_idestroy_fork(ip, XFS_DATA_FORK);
259 }
260 return error;
261}
262
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000263void
264xfs_init_local_fork(
265 struct xfs_inode *ip,
266 int whichfork,
267 const void *data,
268 int size)
269{
270 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
Christoph Hellwig30ee0522016-04-06 07:53:29 +1000271 int mem_size = size, real_size = 0;
272 bool zero_terminate;
273
274 /*
275 * If we are using the local fork to store a symlink body we need to
276 * zero-terminate it so that we can pass it back to the VFS directly.
277 * Overallocate the in-memory fork by one for that and add a zero
278 * to terminate it below.
279 */
280 zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
281 if (zero_terminate)
282 mem_size++;
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000283
284 if (size == 0)
285 ifp->if_u1.if_data = NULL;
Christoph Hellwig30ee0522016-04-06 07:53:29 +1000286 else if (mem_size <= sizeof(ifp->if_u2.if_inline_data))
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000287 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
288 else {
Christoph Hellwig30ee0522016-04-06 07:53:29 +1000289 real_size = roundup(mem_size, 4);
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000290 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
291 }
292
Christoph Hellwig30ee0522016-04-06 07:53:29 +1000293 if (size) {
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000294 memcpy(ifp->if_u1.if_data, data, size);
Christoph Hellwig30ee0522016-04-06 07:53:29 +1000295 if (zero_terminate)
296 ifp->if_u1.if_data[size] = '\0';
297 }
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000298
299 ifp->if_bytes = size;
300 ifp->if_real_bytes = real_size;
301 ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
302 ifp->if_flags |= XFS_IFINLINE;
303}
304
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000305/*
306 * The file is in-lined in the on-disk inode.
307 * If it fits into if_inline_data, then copy
308 * it there, otherwise allocate a buffer for it
309 * and copy the data there. Either way, set
310 * if_data to point at the data.
311 * If we allocate a buffer for the data, make
312 * sure that its size is a multiple of 4 and
313 * record the real size in i_real_bytes.
314 */
315STATIC int
316xfs_iformat_local(
317 xfs_inode_t *ip,
318 xfs_dinode_t *dip,
319 int whichfork,
320 int size)
321{
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000322
323 /*
324 * If the size is unreasonable, then something
325 * is wrong and we just bail out rather than crash in
326 * kmem_alloc() or memcpy() below.
327 */
328 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
329 xfs_warn(ip->i_mount,
330 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
331 (unsigned long long) ip->i_ino, size,
332 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
333 XFS_CORRUPTION_ERROR("xfs_iformat_local", XFS_ERRLEVEL_LOW,
334 ip->i_mount, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000335 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000336 }
Christoph Hellwig143f4ae2016-04-06 07:41:43 +1000337
338 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000339 return 0;
340}
341
342/*
343 * The file consists of a set of extents all
344 * of which fit into the on-disk inode.
345 * If there are few enough extents to fit into
346 * the if_inline_ext, then copy them there.
347 * Otherwise allocate a buffer for them and copy
348 * them into it. Either way, set if_extents
349 * to point at the extents.
350 */
351STATIC int
352xfs_iformat_extents(
353 xfs_inode_t *ip,
354 xfs_dinode_t *dip,
355 int whichfork)
356{
357 xfs_bmbt_rec_t *dp;
358 xfs_ifork_t *ifp;
359 int nex;
360 int size;
361 int i;
362
363 ifp = XFS_IFORK_PTR(ip, whichfork);
364 nex = XFS_DFORK_NEXTENTS(dip, whichfork);
365 size = nex * (uint)sizeof(xfs_bmbt_rec_t);
366
367 /*
368 * If the number of extents is unreasonable, then something
369 * is wrong and we just bail out rather than crash in
370 * kmem_alloc() or memcpy() below.
371 */
372 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
373 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
374 (unsigned long long) ip->i_ino, nex);
375 XFS_CORRUPTION_ERROR("xfs_iformat_extents(1)", XFS_ERRLEVEL_LOW,
376 ip->i_mount, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000377 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000378 }
379
380 ifp->if_real_bytes = 0;
381 if (nex == 0)
382 ifp->if_u1.if_extents = NULL;
383 else if (nex <= XFS_INLINE_EXTS)
384 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
385 else
386 xfs_iext_add(ifp, 0, nex);
387
388 ifp->if_bytes = size;
389 if (size) {
390 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
391 xfs_validate_extents(ifp, nex, XFS_EXTFMT_INODE(ip));
392 for (i = 0; i < nex; i++, dp++) {
393 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
394 ep->l0 = get_unaligned_be64(&dp->l0);
395 ep->l1 = get_unaligned_be64(&dp->l1);
396 }
397 XFS_BMAP_TRACE_EXLIST(ip, nex, whichfork);
398 if (whichfork != XFS_DATA_FORK ||
399 XFS_EXTFMT_INODE(ip) == XFS_EXTFMT_NOSTATE)
400 if (unlikely(xfs_check_nostate_extents(
401 ifp, 0, nex))) {
402 XFS_ERROR_REPORT("xfs_iformat_extents(2)",
403 XFS_ERRLEVEL_LOW,
404 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +1000405 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000406 }
407 }
408 ifp->if_flags |= XFS_IFEXTENTS;
409 return 0;
410}
411
412/*
413 * The file has too many extents to fit into
414 * the inode, so they are in B-tree format.
415 * Allocate a buffer for the root of the B-tree
416 * and copy the root into it. The i_extents
417 * field will remain NULL until all of the
418 * extents are read in (when they are needed).
419 */
420STATIC int
421xfs_iformat_btree(
422 xfs_inode_t *ip,
423 xfs_dinode_t *dip,
424 int whichfork)
425{
426 struct xfs_mount *mp = ip->i_mount;
427 xfs_bmdr_block_t *dfp;
428 xfs_ifork_t *ifp;
429 /* REFERENCED */
430 int nrecs;
431 int size;
432
433 ifp = XFS_IFORK_PTR(ip, whichfork);
434 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
435 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
436 nrecs = be16_to_cpu(dfp->bb_numrecs);
437
438 /*
439 * blow out if -- fork has less extents than can fit in
440 * fork (fork shouldn't be a btree format), root btree
441 * block has more records than can fit into the fork,
442 * or the number of extents is greater than the number of
443 * blocks.
444 */
445 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
446 XFS_IFORK_MAXEXT(ip, whichfork) ||
447 XFS_BMDR_SPACE_CALC(nrecs) >
448 XFS_DFORK_SIZE(dip, mp, whichfork) ||
449 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks)) {
450 xfs_warn(mp, "corrupt inode %Lu (btree).",
451 (unsigned long long) ip->i_ino);
452 XFS_CORRUPTION_ERROR("xfs_iformat_btree", XFS_ERRLEVEL_LOW,
453 mp, dip);
Dave Chinner24513372014-06-25 14:58:08 +1000454 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000455 }
456
457 ifp->if_broot_bytes = size;
458 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
459 ASSERT(ifp->if_broot != NULL);
460 /*
461 * Copy and convert from the on-disk structure
462 * to the in-memory structure.
463 */
464 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
465 ifp->if_broot, size);
466 ifp->if_flags &= ~XFS_IFEXTENTS;
467 ifp->if_flags |= XFS_IFBROOT;
468
469 return 0;
470}
471
472/*
473 * Read in extents from a btree-format inode.
474 * Allocate and fill in if_extents. Real work is done in xfs_bmap.c.
475 */
476int
477xfs_iread_extents(
478 xfs_trans_t *tp,
479 xfs_inode_t *ip,
480 int whichfork)
481{
482 int error;
483 xfs_ifork_t *ifp;
484 xfs_extnum_t nextents;
485
Christoph Hellwigeef334e2013-12-06 12:30:17 -0800486 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
487
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000488 if (unlikely(XFS_IFORK_FORMAT(ip, whichfork) != XFS_DINODE_FMT_BTREE)) {
489 XFS_ERROR_REPORT("xfs_iread_extents", XFS_ERRLEVEL_LOW,
490 ip->i_mount);
Dave Chinner24513372014-06-25 14:58:08 +1000491 return -EFSCORRUPTED;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000492 }
493 nextents = XFS_IFORK_NEXTENTS(ip, whichfork);
494 ifp = XFS_IFORK_PTR(ip, whichfork);
495
496 /*
497 * We know that the size is valid (it's checked in iformat_btree)
498 */
499 ifp->if_bytes = ifp->if_real_bytes = 0;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000500 xfs_iext_add(ifp, 0, nextents);
501 error = xfs_bmap_read_extents(tp, ip, whichfork);
502 if (error) {
503 xfs_iext_destroy(ifp);
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000504 return error;
505 }
506 xfs_validate_extents(ifp, nextents, XFS_EXTFMT_INODE(ip));
Darrick J. Wong0a6844a2017-02-02 15:13:57 -0800507 ifp->if_flags |= XFS_IFEXTENTS;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000508 return 0;
509}
510/*
511 * Reallocate the space for if_broot based on the number of records
512 * being added or deleted as indicated in rec_diff. Move the records
513 * and pointers in if_broot to fit the new size. When shrinking this
514 * will eliminate holes between the records and pointers created by
515 * the caller. When growing this will create holes to be filled in
516 * by the caller.
517 *
518 * The caller must not request to add more records than would fit in
519 * the on-disk inode root. If the if_broot is currently NULL, then
Zhi Yong Wuf6c27342013-08-07 10:11:04 +0000520 * if we are adding records, one will be allocated. The caller must also
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000521 * not request that the number of records go below zero, although
522 * it can go to zero.
523 *
524 * ip -- the inode whose if_broot area is changing
525 * ext_diff -- the change in the number of records, positive or negative,
526 * requested for the if_broot array.
527 */
528void
529xfs_iroot_realloc(
530 xfs_inode_t *ip,
531 int rec_diff,
532 int whichfork)
533{
534 struct xfs_mount *mp = ip->i_mount;
535 int cur_max;
536 xfs_ifork_t *ifp;
537 struct xfs_btree_block *new_broot;
538 int new_max;
539 size_t new_size;
540 char *np;
541 char *op;
542
543 /*
544 * Handle the degenerate case quietly.
545 */
546 if (rec_diff == 0) {
547 return;
548 }
549
550 ifp = XFS_IFORK_PTR(ip, whichfork);
551 if (rec_diff > 0) {
552 /*
553 * If there wasn't any memory allocated before, just
554 * allocate it now and get out.
555 */
556 if (ifp->if_broot_bytes == 0) {
557 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
558 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
559 ifp->if_broot_bytes = (int)new_size;
560 return;
561 }
562
563 /*
564 * If there is already an existing if_broot, then we need
565 * to realloc() it and shift the pointers to their new
566 * location. The records don't change location because
567 * they are kept butted up against the btree block header.
568 */
569 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
570 new_max = cur_max + rec_diff;
571 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
572 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000573 KM_SLEEP | KM_NOFS);
574 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
575 ifp->if_broot_bytes);
576 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
577 (int)new_size);
578 ifp->if_broot_bytes = (int)new_size;
579 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
580 XFS_IFORK_SIZE(ip, whichfork));
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000581 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000582 return;
583 }
584
585 /*
586 * rec_diff is less than 0. In this case, we are shrinking the
587 * if_broot buffer. It must already exist. If we go to zero
588 * records, just get rid of the root and clear the status bit.
589 */
590 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
591 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
592 new_max = cur_max + rec_diff;
593 ASSERT(new_max >= 0);
594 if (new_max > 0)
595 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
596 else
597 new_size = 0;
598 if (new_size > 0) {
599 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
600 /*
601 * First copy over the btree block header.
602 */
603 memcpy(new_broot, ifp->if_broot,
604 XFS_BMBT_BLOCK_LEN(ip->i_mount));
605 } else {
606 new_broot = NULL;
607 ifp->if_flags &= ~XFS_IFBROOT;
608 }
609
610 /*
611 * Only copy the records and pointers if there are any.
612 */
613 if (new_max > 0) {
614 /*
615 * First copy the records.
616 */
617 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
618 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
619 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
620
621 /*
622 * Then copy the pointers.
623 */
624 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
625 ifp->if_broot_bytes);
626 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
627 (int)new_size);
Christoph Hellwigd5cf09b2014-07-30 09:12:05 +1000628 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000629 }
630 kmem_free(ifp->if_broot);
631 ifp->if_broot = new_broot;
632 ifp->if_broot_bytes = (int)new_size;
633 if (ifp->if_broot)
634 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
635 XFS_IFORK_SIZE(ip, whichfork));
636 return;
637}
638
639
640/*
641 * This is called when the amount of space needed for if_data
642 * is increased or decreased. The change in size is indicated by
643 * the number of bytes that need to be added or deleted in the
644 * byte_diff parameter.
645 *
646 * If the amount of space needed has decreased below the size of the
647 * inline buffer, then switch to using the inline buffer. Otherwise,
648 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
649 * to what is needed.
650 *
651 * ip -- the inode whose if_data area is changing
652 * byte_diff -- the change in the number of bytes, positive or negative,
653 * requested for the if_data array.
654 */
655void
656xfs_idata_realloc(
657 xfs_inode_t *ip,
658 int byte_diff,
659 int whichfork)
660{
661 xfs_ifork_t *ifp;
662 int new_size;
663 int real_size;
664
665 if (byte_diff == 0) {
666 return;
667 }
668
669 ifp = XFS_IFORK_PTR(ip, whichfork);
670 new_size = (int)ifp->if_bytes + byte_diff;
671 ASSERT(new_size >= 0);
672
673 if (new_size == 0) {
674 if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
675 kmem_free(ifp->if_u1.if_data);
676 }
677 ifp->if_u1.if_data = NULL;
678 real_size = 0;
679 } else if (new_size <= sizeof(ifp->if_u2.if_inline_data)) {
680 /*
681 * If the valid extents/data can fit in if_inline_ext/data,
682 * copy them from the malloc'd vector and free it.
683 */
684 if (ifp->if_u1.if_data == NULL) {
685 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
686 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
687 ASSERT(ifp->if_real_bytes != 0);
688 memcpy(ifp->if_u2.if_inline_data, ifp->if_u1.if_data,
689 new_size);
690 kmem_free(ifp->if_u1.if_data);
691 ifp->if_u1.if_data = ifp->if_u2.if_inline_data;
692 }
693 real_size = 0;
694 } else {
695 /*
696 * Stuck with malloc/realloc.
697 * For inline data, the underlying buffer must be
698 * a multiple of 4 bytes in size so that it can be
699 * logged and stay on word boundaries. We enforce
700 * that here.
701 */
702 real_size = roundup(new_size, 4);
703 if (ifp->if_u1.if_data == NULL) {
704 ASSERT(ifp->if_real_bytes == 0);
705 ifp->if_u1.if_data = kmem_alloc(real_size,
706 KM_SLEEP | KM_NOFS);
707 } else if (ifp->if_u1.if_data != ifp->if_u2.if_inline_data) {
708 /*
709 * Only do the realloc if the underlying size
710 * is really changing.
711 */
712 if (ifp->if_real_bytes != real_size) {
713 ifp->if_u1.if_data =
714 kmem_realloc(ifp->if_u1.if_data,
715 real_size,
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000716 KM_SLEEP | KM_NOFS);
717 }
718 } else {
719 ASSERT(ifp->if_real_bytes == 0);
720 ifp->if_u1.if_data = kmem_alloc(real_size,
721 KM_SLEEP | KM_NOFS);
722 memcpy(ifp->if_u1.if_data, ifp->if_u2.if_inline_data,
723 ifp->if_bytes);
724 }
725 }
726 ifp->if_real_bytes = real_size;
727 ifp->if_bytes = new_size;
728 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
729}
730
731void
732xfs_idestroy_fork(
733 xfs_inode_t *ip,
734 int whichfork)
735{
736 xfs_ifork_t *ifp;
737
738 ifp = XFS_IFORK_PTR(ip, whichfork);
739 if (ifp->if_broot != NULL) {
740 kmem_free(ifp->if_broot);
741 ifp->if_broot = NULL;
742 }
743
744 /*
745 * If the format is local, then we can't have an extents
746 * array so just look for an inline data array. If we're
747 * not local then we may or may not have an extents list,
748 * so check and free it up if we do.
749 */
750 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
751 if ((ifp->if_u1.if_data != ifp->if_u2.if_inline_data) &&
752 (ifp->if_u1.if_data != NULL)) {
753 ASSERT(ifp->if_real_bytes != 0);
754 kmem_free(ifp->if_u1.if_data);
755 ifp->if_u1.if_data = NULL;
756 ifp->if_real_bytes = 0;
757 }
758 } else if ((ifp->if_flags & XFS_IFEXTENTS) &&
759 ((ifp->if_flags & XFS_IFEXTIREC) ||
760 ((ifp->if_u1.if_extents != NULL) &&
761 (ifp->if_u1.if_extents != ifp->if_u2.if_inline_ext)))) {
762 ASSERT(ifp->if_real_bytes != 0);
763 xfs_iext_destroy(ifp);
764 }
765 ASSERT(ifp->if_u1.if_extents == NULL ||
766 ifp->if_u1.if_extents == ifp->if_u2.if_inline_ext);
767 ASSERT(ifp->if_real_bytes == 0);
768 if (whichfork == XFS_ATTR_FORK) {
769 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
770 ip->i_afp = NULL;
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700771 } else if (whichfork == XFS_COW_FORK) {
772 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
773 ip->i_cowfp = NULL;
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000774 }
775}
776
Eric Sandeenf380ee72017-01-09 16:38:36 +0100777/* Count number of incore extents based on if_bytes */
778xfs_extnum_t
779xfs_iext_count(struct xfs_ifork *ifp)
780{
781 return ifp->if_bytes / (uint)sizeof(xfs_bmbt_rec_t);
782}
783
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000784/*
Christoph Hellwigda776502013-12-13 11:34:04 +1100785 * Convert in-core extents to on-disk form
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000786 *
Christoph Hellwigda776502013-12-13 11:34:04 +1100787 * For either the data or attr fork in extent format, we need to endian convert
788 * the in-core extent as we place them into the on-disk inode.
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000789 *
Christoph Hellwigda776502013-12-13 11:34:04 +1100790 * In the case of the data fork, the in-core and on-disk fork sizes can be
791 * different due to delayed allocation extents. We only copy on-disk extents
792 * here, so callers must always use the physical fork size to determine the
793 * size of the buffer passed to this routine. We will return the size actually
794 * used.
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000795 */
796int
797xfs_iextents_copy(
798 xfs_inode_t *ip,
799 xfs_bmbt_rec_t *dp,
800 int whichfork)
801{
802 int copied;
803 int i;
804 xfs_ifork_t *ifp;
805 int nrecs;
806 xfs_fsblock_t start_block;
807
808 ifp = XFS_IFORK_PTR(ip, whichfork);
809 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL|XFS_ILOCK_SHARED));
810 ASSERT(ifp->if_bytes > 0);
811
Eric Sandeenf380ee72017-01-09 16:38:36 +0100812 nrecs = xfs_iext_count(ifp);
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000813 XFS_BMAP_TRACE_EXLIST(ip, nrecs, whichfork);
814 ASSERT(nrecs > 0);
815
816 /*
817 * There are some delayed allocation extents in the
818 * inode, so copy the extents one at a time and skip
819 * the delayed ones. There must be at least one
820 * non-delayed extent.
821 */
822 copied = 0;
823 for (i = 0; i < nrecs; i++) {
824 xfs_bmbt_rec_host_t *ep = xfs_iext_get_ext(ifp, i);
825 start_block = xfs_bmbt_get_startblock(ep);
826 if (isnullstartblock(start_block)) {
827 /*
828 * It's a delayed allocation extent, so skip it.
829 */
830 continue;
831 }
832
833 /* Translate to on disk format */
Dave Chinnerc5c249b2013-08-12 20:49:43 +1000834 put_unaligned_be64(ep->l0, &dp->l0);
835 put_unaligned_be64(ep->l1, &dp->l1);
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000836 dp++;
837 copied++;
838 }
839 ASSERT(copied != 0);
840 xfs_validate_extents(ifp, copied, XFS_EXTFMT_INODE(ip));
841
842 return (copied * (uint)sizeof(xfs_bmbt_rec_t));
843}
844
845/*
846 * Each of the following cases stores data into the same region
847 * of the on-disk inode, so only one of them can be valid at
848 * any given time. While it is possible to have conflicting formats
849 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
850 * in EXTENTS format, this can only happen when the fork has
851 * changed formats after being modified but before being flushed.
852 * In these cases, the format always takes precedence, because the
853 * format indicates the current state of the fork.
854 */
855void
856xfs_iflush_fork(
857 xfs_inode_t *ip,
858 xfs_dinode_t *dip,
859 xfs_inode_log_item_t *iip,
Eric Sandeenfd9fdba2014-04-14 19:04:46 +1000860 int whichfork)
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000861{
862 char *cp;
863 xfs_ifork_t *ifp;
864 xfs_mount_t *mp;
865 static const short brootflag[2] =
866 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
867 static const short dataflag[2] =
868 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
869 static const short extflag[2] =
870 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
871
872 if (!iip)
873 return;
874 ifp = XFS_IFORK_PTR(ip, whichfork);
875 /*
876 * This can happen if we gave up in iformat in an error path,
877 * for the attribute fork.
878 */
879 if (!ifp) {
880 ASSERT(whichfork == XFS_ATTR_FORK);
881 return;
882 }
883 cp = XFS_DFORK_PTR(dip, whichfork);
884 mp = ip->i_mount;
885 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
886 case XFS_DINODE_FMT_LOCAL:
887 if ((iip->ili_fields & dataflag[whichfork]) &&
888 (ifp->if_bytes > 0)) {
889 ASSERT(ifp->if_u1.if_data != NULL);
890 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
891 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
892 }
893 break;
894
895 case XFS_DINODE_FMT_EXTENTS:
896 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
897 !(iip->ili_fields & extflag[whichfork]));
898 if ((iip->ili_fields & extflag[whichfork]) &&
899 (ifp->if_bytes > 0)) {
900 ASSERT(xfs_iext_get_ext(ifp, 0));
901 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
902 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
903 whichfork);
904 }
905 break;
906
907 case XFS_DINODE_FMT_BTREE:
908 if ((iip->ili_fields & brootflag[whichfork]) &&
909 (ifp->if_broot_bytes > 0)) {
910 ASSERT(ifp->if_broot != NULL);
911 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
912 XFS_IFORK_SIZE(ip, whichfork));
913 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
914 (xfs_bmdr_block_t *)cp,
915 XFS_DFORK_SIZE(dip, mp, whichfork));
916 }
917 break;
918
919 case XFS_DINODE_FMT_DEV:
920 if (iip->ili_fields & XFS_ILOG_DEV) {
921 ASSERT(whichfork == XFS_DATA_FORK);
922 xfs_dinode_put_rdev(dip, ip->i_df.if_u2.if_rdev);
923 }
924 break;
925
926 case XFS_DINODE_FMT_UUID:
927 if (iip->ili_fields & XFS_ILOG_UUID) {
928 ASSERT(whichfork == XFS_DATA_FORK);
929 memcpy(XFS_DFORK_DPTR(dip),
930 &ip->i_df.if_u2.if_uuid,
931 sizeof(uuid_t));
932 }
933 break;
934
935 default:
936 ASSERT(0);
937 break;
938 }
939}
940
941/*
942 * Return a pointer to the extent record at file index idx.
943 */
944xfs_bmbt_rec_host_t *
945xfs_iext_get_ext(
946 xfs_ifork_t *ifp, /* inode fork pointer */
947 xfs_extnum_t idx) /* index of target extent */
948{
949 ASSERT(idx >= 0);
Eric Sandeenf380ee72017-01-09 16:38:36 +0100950 ASSERT(idx < xfs_iext_count(ifp));
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000951
952 if ((ifp->if_flags & XFS_IFEXTIREC) && (idx == 0)) {
953 return ifp->if_u1.if_ext_irec->er_extbuf;
954 } else if (ifp->if_flags & XFS_IFEXTIREC) {
955 xfs_ext_irec_t *erp; /* irec pointer */
956 int erp_idx = 0; /* irec index */
957 xfs_extnum_t page_idx = idx; /* ext index in target list */
958
959 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
960 return &erp->er_extbuf[page_idx];
961 } else if (ifp->if_bytes) {
962 return &ifp->if_u1.if_extents[idx];
963 } else {
964 return NULL;
965 }
966}
967
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700968/* Convert bmap state flags to an inode fork. */
969struct xfs_ifork *
970xfs_iext_state_to_fork(
971 struct xfs_inode *ip,
972 int state)
973{
974 if (state & BMAP_COWFORK)
975 return ip->i_cowfp;
976 else if (state & BMAP_ATTRFORK)
977 return ip->i_afp;
978 return &ip->i_df;
979}
980
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000981/*
982 * Insert new item(s) into the extent records for incore inode
983 * fork 'ifp'. 'count' new items are inserted at index 'idx'.
984 */
985void
986xfs_iext_insert(
987 xfs_inode_t *ip, /* incore inode pointer */
988 xfs_extnum_t idx, /* starting index of new items */
989 xfs_extnum_t count, /* number of inserted items */
990 xfs_bmbt_irec_t *new, /* items to insert */
991 int state) /* type of extent conversion */
992{
Darrick J. Wong3993bae2016-10-03 09:11:32 -0700993 xfs_ifork_t *ifp = xfs_iext_state_to_fork(ip, state);
Dave Chinner5c4d97d2013-08-12 20:49:33 +1000994 xfs_extnum_t i; /* extent record index */
995
996 trace_xfs_iext_insert(ip, idx, new, state, _RET_IP_);
997
998 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
999 xfs_iext_add(ifp, idx, count);
1000 for (i = idx; i < idx + count; i++, new++)
1001 xfs_bmbt_set_all(xfs_iext_get_ext(ifp, i), new);
1002}
1003
1004/*
1005 * This is called when the amount of space required for incore file
1006 * extents needs to be increased. The ext_diff parameter stores the
1007 * number of new extents being added and the idx parameter contains
1008 * the extent index where the new extents will be added. If the new
1009 * extents are being appended, then we just need to (re)allocate and
1010 * initialize the space. Otherwise, if the new extents are being
1011 * inserted into the middle of the existing entries, a bit more work
1012 * is required to make room for the new extents to be inserted. The
1013 * caller is responsible for filling in the new extent entries upon
1014 * return.
1015 */
1016void
1017xfs_iext_add(
1018 xfs_ifork_t *ifp, /* inode fork pointer */
1019 xfs_extnum_t idx, /* index to begin adding exts */
1020 int ext_diff) /* number of extents to add */
1021{
1022 int byte_diff; /* new bytes being added */
1023 int new_size; /* size of extents after adding */
1024 xfs_extnum_t nextents; /* number of extents in file */
1025
Eric Sandeenf380ee72017-01-09 16:38:36 +01001026 nextents = xfs_iext_count(ifp);
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001027 ASSERT((idx >= 0) && (idx <= nextents));
1028 byte_diff = ext_diff * sizeof(xfs_bmbt_rec_t);
1029 new_size = ifp->if_bytes + byte_diff;
1030 /*
1031 * If the new number of extents (nextents + ext_diff)
1032 * fits inside the inode, then continue to use the inline
1033 * extent buffer.
1034 */
1035 if (nextents + ext_diff <= XFS_INLINE_EXTS) {
1036 if (idx < nextents) {
1037 memmove(&ifp->if_u2.if_inline_ext[idx + ext_diff],
1038 &ifp->if_u2.if_inline_ext[idx],
1039 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
1040 memset(&ifp->if_u2.if_inline_ext[idx], 0, byte_diff);
1041 }
1042 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1043 ifp->if_real_bytes = 0;
1044 }
1045 /*
1046 * Otherwise use a linear (direct) extent list.
1047 * If the extents are currently inside the inode,
1048 * xfs_iext_realloc_direct will switch us from
1049 * inline to direct extent allocation mode.
1050 */
1051 else if (nextents + ext_diff <= XFS_LINEAR_EXTS) {
1052 xfs_iext_realloc_direct(ifp, new_size);
1053 if (idx < nextents) {
1054 memmove(&ifp->if_u1.if_extents[idx + ext_diff],
1055 &ifp->if_u1.if_extents[idx],
1056 (nextents - idx) * sizeof(xfs_bmbt_rec_t));
1057 memset(&ifp->if_u1.if_extents[idx], 0, byte_diff);
1058 }
1059 }
1060 /* Indirection array */
1061 else {
1062 xfs_ext_irec_t *erp;
1063 int erp_idx = 0;
1064 int page_idx = idx;
1065
1066 ASSERT(nextents + ext_diff > XFS_LINEAR_EXTS);
1067 if (ifp->if_flags & XFS_IFEXTIREC) {
1068 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 1);
1069 } else {
1070 xfs_iext_irec_init(ifp);
1071 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1072 erp = ifp->if_u1.if_ext_irec;
1073 }
1074 /* Extents fit in target extent page */
1075 if (erp && erp->er_extcount + ext_diff <= XFS_LINEAR_EXTS) {
1076 if (page_idx < erp->er_extcount) {
1077 memmove(&erp->er_extbuf[page_idx + ext_diff],
1078 &erp->er_extbuf[page_idx],
1079 (erp->er_extcount - page_idx) *
1080 sizeof(xfs_bmbt_rec_t));
1081 memset(&erp->er_extbuf[page_idx], 0, byte_diff);
1082 }
1083 erp->er_extcount += ext_diff;
1084 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1085 }
1086 /* Insert a new extent page */
1087 else if (erp) {
1088 xfs_iext_add_indirect_multi(ifp,
1089 erp_idx, page_idx, ext_diff);
1090 }
1091 /*
1092 * If extent(s) are being appended to the last page in
1093 * the indirection array and the new extent(s) don't fit
1094 * in the page, then erp is NULL and erp_idx is set to
1095 * the next index needed in the indirection array.
1096 */
1097 else {
Jie Liubb86d212013-10-25 14:52:44 +08001098 uint count = ext_diff;
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001099
1100 while (count) {
1101 erp = xfs_iext_irec_new(ifp, erp_idx);
Jie Liubb86d212013-10-25 14:52:44 +08001102 erp->er_extcount = min(count, XFS_LINEAR_EXTS);
1103 count -= erp->er_extcount;
1104 if (count)
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001105 erp_idx++;
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001106 }
1107 }
1108 }
1109 ifp->if_bytes = new_size;
1110}
1111
1112/*
1113 * This is called when incore extents are being added to the indirection
1114 * array and the new extents do not fit in the target extent list. The
1115 * erp_idx parameter contains the irec index for the target extent list
1116 * in the indirection array, and the idx parameter contains the extent
1117 * index within the list. The number of extents being added is stored
1118 * in the count parameter.
1119 *
1120 * |-------| |-------|
1121 * | | | | idx - number of extents before idx
1122 * | idx | | count |
1123 * | | | | count - number of extents being inserted at idx
1124 * |-------| |-------|
1125 * | count | | nex2 | nex2 - number of extents after idx + count
1126 * |-------| |-------|
1127 */
1128void
1129xfs_iext_add_indirect_multi(
1130 xfs_ifork_t *ifp, /* inode fork pointer */
1131 int erp_idx, /* target extent irec index */
1132 xfs_extnum_t idx, /* index within target list */
1133 int count) /* new extents being added */
1134{
1135 int byte_diff; /* new bytes being added */
1136 xfs_ext_irec_t *erp; /* pointer to irec entry */
1137 xfs_extnum_t ext_diff; /* number of extents to add */
1138 xfs_extnum_t ext_cnt; /* new extents still needed */
1139 xfs_extnum_t nex2; /* extents after idx + count */
1140 xfs_bmbt_rec_t *nex2_ep = NULL; /* temp list for nex2 extents */
1141 int nlists; /* number of irec's (lists) */
1142
1143 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1144 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1145 nex2 = erp->er_extcount - idx;
1146 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1147
1148 /*
1149 * Save second part of target extent list
1150 * (all extents past */
1151 if (nex2) {
1152 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1153 nex2_ep = (xfs_bmbt_rec_t *) kmem_alloc(byte_diff, KM_NOFS);
1154 memmove(nex2_ep, &erp->er_extbuf[idx], byte_diff);
1155 erp->er_extcount -= nex2;
1156 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -nex2);
1157 memset(&erp->er_extbuf[idx], 0, byte_diff);
1158 }
1159
1160 /*
1161 * Add the new extents to the end of the target
1162 * list, then allocate new irec record(s) and
1163 * extent buffer(s) as needed to store the rest
1164 * of the new extents.
1165 */
1166 ext_cnt = count;
1167 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS - erp->er_extcount);
1168 if (ext_diff) {
1169 erp->er_extcount += ext_diff;
1170 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1171 ext_cnt -= ext_diff;
1172 }
1173 while (ext_cnt) {
1174 erp_idx++;
1175 erp = xfs_iext_irec_new(ifp, erp_idx);
1176 ext_diff = MIN(ext_cnt, (int)XFS_LINEAR_EXTS);
1177 erp->er_extcount = ext_diff;
1178 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, ext_diff);
1179 ext_cnt -= ext_diff;
1180 }
1181
1182 /* Add nex2 extents back to indirection array */
1183 if (nex2) {
1184 xfs_extnum_t ext_avail;
1185 int i;
1186
1187 byte_diff = nex2 * sizeof(xfs_bmbt_rec_t);
1188 ext_avail = XFS_LINEAR_EXTS - erp->er_extcount;
1189 i = 0;
1190 /*
1191 * If nex2 extents fit in the current page, append
1192 * nex2_ep after the new extents.
1193 */
1194 if (nex2 <= ext_avail) {
1195 i = erp->er_extcount;
1196 }
1197 /*
1198 * Otherwise, check if space is available in the
1199 * next page.
1200 */
1201 else if ((erp_idx < nlists - 1) &&
1202 (nex2 <= (ext_avail = XFS_LINEAR_EXTS -
1203 ifp->if_u1.if_ext_irec[erp_idx+1].er_extcount))) {
1204 erp_idx++;
1205 erp++;
1206 /* Create a hole for nex2 extents */
1207 memmove(&erp->er_extbuf[nex2], erp->er_extbuf,
1208 erp->er_extcount * sizeof(xfs_bmbt_rec_t));
1209 }
1210 /*
1211 * Final choice, create a new extent page for
1212 * nex2 extents.
1213 */
1214 else {
1215 erp_idx++;
1216 erp = xfs_iext_irec_new(ifp, erp_idx);
1217 }
1218 memmove(&erp->er_extbuf[i], nex2_ep, byte_diff);
1219 kmem_free(nex2_ep);
1220 erp->er_extcount += nex2;
1221 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, nex2);
1222 }
1223}
1224
1225/*
1226 * This is called when the amount of space required for incore file
1227 * extents needs to be decreased. The ext_diff parameter stores the
1228 * number of extents to be removed and the idx parameter contains
1229 * the extent index where the extents will be removed from.
1230 *
1231 * If the amount of space needed has decreased below the linear
1232 * limit, XFS_IEXT_BUFSZ, then switch to using the contiguous
1233 * extent array. Otherwise, use kmem_realloc() to adjust the
1234 * size to what is needed.
1235 */
1236void
1237xfs_iext_remove(
1238 xfs_inode_t *ip, /* incore inode pointer */
1239 xfs_extnum_t idx, /* index to begin removing exts */
1240 int ext_diff, /* number of extents to remove */
1241 int state) /* type of extent conversion */
1242{
Darrick J. Wong3993bae2016-10-03 09:11:32 -07001243 xfs_ifork_t *ifp = xfs_iext_state_to_fork(ip, state);
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001244 xfs_extnum_t nextents; /* number of extents in file */
1245 int new_size; /* size of extents after removal */
1246
1247 trace_xfs_iext_remove(ip, idx, state, _RET_IP_);
1248
1249 ASSERT(ext_diff > 0);
Eric Sandeenf380ee72017-01-09 16:38:36 +01001250 nextents = xfs_iext_count(ifp);
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001251 new_size = (nextents - ext_diff) * sizeof(xfs_bmbt_rec_t);
1252
1253 if (new_size == 0) {
1254 xfs_iext_destroy(ifp);
1255 } else if (ifp->if_flags & XFS_IFEXTIREC) {
1256 xfs_iext_remove_indirect(ifp, idx, ext_diff);
1257 } else if (ifp->if_real_bytes) {
1258 xfs_iext_remove_direct(ifp, idx, ext_diff);
1259 } else {
1260 xfs_iext_remove_inline(ifp, idx, ext_diff);
1261 }
1262 ifp->if_bytes = new_size;
1263}
1264
1265/*
1266 * This removes ext_diff extents from the inline buffer, beginning
1267 * at extent index idx.
1268 */
1269void
1270xfs_iext_remove_inline(
1271 xfs_ifork_t *ifp, /* inode fork pointer */
1272 xfs_extnum_t idx, /* index to begin removing exts */
1273 int ext_diff) /* number of extents to remove */
1274{
1275 int nextents; /* number of extents in file */
1276
1277 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1278 ASSERT(idx < XFS_INLINE_EXTS);
Eric Sandeenf380ee72017-01-09 16:38:36 +01001279 nextents = xfs_iext_count(ifp);
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001280 ASSERT(((nextents - ext_diff) > 0) &&
1281 (nextents - ext_diff) < XFS_INLINE_EXTS);
1282
1283 if (idx + ext_diff < nextents) {
1284 memmove(&ifp->if_u2.if_inline_ext[idx],
1285 &ifp->if_u2.if_inline_ext[idx + ext_diff],
1286 (nextents - (idx + ext_diff)) *
1287 sizeof(xfs_bmbt_rec_t));
1288 memset(&ifp->if_u2.if_inline_ext[nextents - ext_diff],
1289 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1290 } else {
1291 memset(&ifp->if_u2.if_inline_ext[idx], 0,
1292 ext_diff * sizeof(xfs_bmbt_rec_t));
1293 }
1294}
1295
1296/*
1297 * This removes ext_diff extents from a linear (direct) extent list,
1298 * beginning at extent index idx. If the extents are being removed
1299 * from the end of the list (ie. truncate) then we just need to re-
1300 * allocate the list to remove the extra space. Otherwise, if the
1301 * extents are being removed from the middle of the existing extent
1302 * entries, then we first need to move the extent records beginning
1303 * at idx + ext_diff up in the list to overwrite the records being
1304 * removed, then remove the extra space via kmem_realloc.
1305 */
1306void
1307xfs_iext_remove_direct(
1308 xfs_ifork_t *ifp, /* inode fork pointer */
1309 xfs_extnum_t idx, /* index to begin removing exts */
1310 int ext_diff) /* number of extents to remove */
1311{
1312 xfs_extnum_t nextents; /* number of extents in file */
1313 int new_size; /* size of extents after removal */
1314
1315 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
1316 new_size = ifp->if_bytes -
1317 (ext_diff * sizeof(xfs_bmbt_rec_t));
Eric Sandeenf380ee72017-01-09 16:38:36 +01001318 nextents = xfs_iext_count(ifp);
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001319
1320 if (new_size == 0) {
1321 xfs_iext_destroy(ifp);
1322 return;
1323 }
1324 /* Move extents up in the list (if needed) */
1325 if (idx + ext_diff < nextents) {
1326 memmove(&ifp->if_u1.if_extents[idx],
1327 &ifp->if_u1.if_extents[idx + ext_diff],
1328 (nextents - (idx + ext_diff)) *
1329 sizeof(xfs_bmbt_rec_t));
1330 }
1331 memset(&ifp->if_u1.if_extents[nextents - ext_diff],
1332 0, ext_diff * sizeof(xfs_bmbt_rec_t));
1333 /*
1334 * Reallocate the direct extent list. If the extents
1335 * will fit inside the inode then xfs_iext_realloc_direct
1336 * will switch from direct to inline extent allocation
1337 * mode for us.
1338 */
1339 xfs_iext_realloc_direct(ifp, new_size);
1340 ifp->if_bytes = new_size;
1341}
1342
1343/*
1344 * This is called when incore extents are being removed from the
1345 * indirection array and the extents being removed span multiple extent
1346 * buffers. The idx parameter contains the file extent index where we
1347 * want to begin removing extents, and the count parameter contains
1348 * how many extents need to be removed.
1349 *
1350 * |-------| |-------|
1351 * | nex1 | | | nex1 - number of extents before idx
1352 * |-------| | count |
1353 * | | | | count - number of extents being removed at idx
1354 * | count | |-------|
1355 * | | | nex2 | nex2 - number of extents after idx + count
1356 * |-------| |-------|
1357 */
1358void
1359xfs_iext_remove_indirect(
1360 xfs_ifork_t *ifp, /* inode fork pointer */
1361 xfs_extnum_t idx, /* index to begin removing extents */
1362 int count) /* number of extents to remove */
1363{
1364 xfs_ext_irec_t *erp; /* indirection array pointer */
1365 int erp_idx = 0; /* indirection array index */
1366 xfs_extnum_t ext_cnt; /* extents left to remove */
1367 xfs_extnum_t ext_diff; /* extents to remove in current list */
1368 xfs_extnum_t nex1; /* number of extents before idx */
1369 xfs_extnum_t nex2; /* extents after idx + count */
1370 int page_idx = idx; /* index in target extent list */
1371
1372 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1373 erp = xfs_iext_idx_to_irec(ifp, &page_idx, &erp_idx, 0);
1374 ASSERT(erp != NULL);
1375 nex1 = page_idx;
1376 ext_cnt = count;
1377 while (ext_cnt) {
1378 nex2 = MAX((erp->er_extcount - (nex1 + ext_cnt)), 0);
1379 ext_diff = MIN(ext_cnt, (erp->er_extcount - nex1));
1380 /*
1381 * Check for deletion of entire list;
1382 * xfs_iext_irec_remove() updates extent offsets.
1383 */
1384 if (ext_diff == erp->er_extcount) {
1385 xfs_iext_irec_remove(ifp, erp_idx);
1386 ext_cnt -= ext_diff;
1387 nex1 = 0;
1388 if (ext_cnt) {
1389 ASSERT(erp_idx < ifp->if_real_bytes /
1390 XFS_IEXT_BUFSZ);
1391 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1392 nex1 = 0;
1393 continue;
1394 } else {
1395 break;
1396 }
1397 }
1398 /* Move extents up (if needed) */
1399 if (nex2) {
1400 memmove(&erp->er_extbuf[nex1],
1401 &erp->er_extbuf[nex1 + ext_diff],
1402 nex2 * sizeof(xfs_bmbt_rec_t));
1403 }
1404 /* Zero out rest of page */
1405 memset(&erp->er_extbuf[nex1 + nex2], 0, (XFS_IEXT_BUFSZ -
1406 ((nex1 + nex2) * sizeof(xfs_bmbt_rec_t))));
1407 /* Update remaining counters */
1408 erp->er_extcount -= ext_diff;
1409 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1, -ext_diff);
1410 ext_cnt -= ext_diff;
1411 nex1 = 0;
1412 erp_idx++;
1413 erp++;
1414 }
1415 ifp->if_bytes -= count * sizeof(xfs_bmbt_rec_t);
1416 xfs_iext_irec_compact(ifp);
1417}
1418
1419/*
1420 * Create, destroy, or resize a linear (direct) block of extents.
1421 */
1422void
1423xfs_iext_realloc_direct(
1424 xfs_ifork_t *ifp, /* inode fork pointer */
Jie Liu17ec81c2013-09-22 16:25:15 +08001425 int new_size) /* new size of extents after adding */
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001426{
1427 int rnew_size; /* real new size of extents */
1428
1429 rnew_size = new_size;
1430
1431 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC) ||
1432 ((new_size >= 0) && (new_size <= XFS_IEXT_BUFSZ) &&
1433 (new_size != ifp->if_real_bytes)));
1434
1435 /* Free extent records */
1436 if (new_size == 0) {
1437 xfs_iext_destroy(ifp);
1438 }
1439 /* Resize direct extent list and zero any new bytes */
1440 else if (ifp->if_real_bytes) {
1441 /* Check if extents will fit inside the inode */
1442 if (new_size <= XFS_INLINE_EXTS * sizeof(xfs_bmbt_rec_t)) {
1443 xfs_iext_direct_to_inline(ifp, new_size /
1444 (uint)sizeof(xfs_bmbt_rec_t));
1445 ifp->if_bytes = new_size;
1446 return;
1447 }
1448 if (!is_power_of_2(new_size)){
1449 rnew_size = roundup_pow_of_two(new_size);
1450 }
1451 if (rnew_size != ifp->if_real_bytes) {
1452 ifp->if_u1.if_extents =
1453 kmem_realloc(ifp->if_u1.if_extents,
Christoph Hellwig664b60f2016-04-06 09:47:01 +10001454 rnew_size, KM_NOFS);
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001455 }
1456 if (rnew_size > ifp->if_real_bytes) {
1457 memset(&ifp->if_u1.if_extents[ifp->if_bytes /
1458 (uint)sizeof(xfs_bmbt_rec_t)], 0,
1459 rnew_size - ifp->if_real_bytes);
1460 }
1461 }
Jie Liu17ec81c2013-09-22 16:25:15 +08001462 /* Switch from the inline extent buffer to a direct extent list */
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001463 else {
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001464 if (!is_power_of_2(new_size)) {
1465 rnew_size = roundup_pow_of_two(new_size);
1466 }
1467 xfs_iext_inline_to_direct(ifp, rnew_size);
1468 }
1469 ifp->if_real_bytes = rnew_size;
1470 ifp->if_bytes = new_size;
1471}
1472
1473/*
1474 * Switch from linear (direct) extent records to inline buffer.
1475 */
1476void
1477xfs_iext_direct_to_inline(
1478 xfs_ifork_t *ifp, /* inode fork pointer */
1479 xfs_extnum_t nextents) /* number of extents in file */
1480{
1481 ASSERT(ifp->if_flags & XFS_IFEXTENTS);
1482 ASSERT(nextents <= XFS_INLINE_EXTS);
1483 /*
1484 * The inline buffer was zeroed when we switched
1485 * from inline to direct extent allocation mode,
1486 * so we don't need to clear it here.
1487 */
1488 memcpy(ifp->if_u2.if_inline_ext, ifp->if_u1.if_extents,
1489 nextents * sizeof(xfs_bmbt_rec_t));
1490 kmem_free(ifp->if_u1.if_extents);
1491 ifp->if_u1.if_extents = ifp->if_u2.if_inline_ext;
1492 ifp->if_real_bytes = 0;
1493}
1494
1495/*
1496 * Switch from inline buffer to linear (direct) extent records.
1497 * new_size should already be rounded up to the next power of 2
1498 * by the caller (when appropriate), so use new_size as it is.
1499 * However, since new_size may be rounded up, we can't update
1500 * if_bytes here. It is the caller's responsibility to update
1501 * if_bytes upon return.
1502 */
1503void
1504xfs_iext_inline_to_direct(
1505 xfs_ifork_t *ifp, /* inode fork pointer */
1506 int new_size) /* number of extents in file */
1507{
1508 ifp->if_u1.if_extents = kmem_alloc(new_size, KM_NOFS);
1509 memset(ifp->if_u1.if_extents, 0, new_size);
1510 if (ifp->if_bytes) {
1511 memcpy(ifp->if_u1.if_extents, ifp->if_u2.if_inline_ext,
1512 ifp->if_bytes);
1513 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1514 sizeof(xfs_bmbt_rec_t));
1515 }
1516 ifp->if_real_bytes = new_size;
1517}
1518
1519/*
1520 * Resize an extent indirection array to new_size bytes.
1521 */
1522STATIC void
1523xfs_iext_realloc_indirect(
1524 xfs_ifork_t *ifp, /* inode fork pointer */
1525 int new_size) /* new indirection array size */
1526{
1527 int nlists; /* number of irec's (ex lists) */
1528 int size; /* current indirection array size */
1529
1530 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1531 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1532 size = nlists * sizeof(xfs_ext_irec_t);
1533 ASSERT(ifp->if_real_bytes);
1534 ASSERT((new_size >= 0) && (new_size != size));
1535 if (new_size == 0) {
1536 xfs_iext_destroy(ifp);
1537 } else {
Christoph Hellwig664b60f2016-04-06 09:47:01 +10001538 ifp->if_u1.if_ext_irec =
1539 kmem_realloc(ifp->if_u1.if_ext_irec, new_size, KM_NOFS);
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001540 }
1541}
1542
1543/*
1544 * Switch from indirection array to linear (direct) extent allocations.
1545 */
1546STATIC void
1547xfs_iext_indirect_to_direct(
1548 xfs_ifork_t *ifp) /* inode fork pointer */
1549{
1550 xfs_bmbt_rec_host_t *ep; /* extent record pointer */
1551 xfs_extnum_t nextents; /* number of extents in file */
1552 int size; /* size of file extents */
1553
1554 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
Eric Sandeenf380ee72017-01-09 16:38:36 +01001555 nextents = xfs_iext_count(ifp);
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001556 ASSERT(nextents <= XFS_LINEAR_EXTS);
1557 size = nextents * sizeof(xfs_bmbt_rec_t);
1558
1559 xfs_iext_irec_compact_pages(ifp);
1560 ASSERT(ifp->if_real_bytes == XFS_IEXT_BUFSZ);
1561
1562 ep = ifp->if_u1.if_ext_irec->er_extbuf;
1563 kmem_free(ifp->if_u1.if_ext_irec);
1564 ifp->if_flags &= ~XFS_IFEXTIREC;
1565 ifp->if_u1.if_extents = ep;
1566 ifp->if_bytes = size;
1567 if (nextents < XFS_LINEAR_EXTS) {
1568 xfs_iext_realloc_direct(ifp, size);
1569 }
1570}
1571
1572/*
Alex Lyakas32b43ab2016-05-18 14:01:52 +10001573 * Remove all records from the indirection array.
1574 */
1575STATIC void
1576xfs_iext_irec_remove_all(
1577 struct xfs_ifork *ifp)
1578{
1579 int nlists;
1580 int i;
1581
1582 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1583 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1584 for (i = 0; i < nlists; i++)
1585 kmem_free(ifp->if_u1.if_ext_irec[i].er_extbuf);
1586 kmem_free(ifp->if_u1.if_ext_irec);
1587 ifp->if_flags &= ~XFS_IFEXTIREC;
1588}
1589
1590/*
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001591 * Free incore file extents.
1592 */
1593void
1594xfs_iext_destroy(
1595 xfs_ifork_t *ifp) /* inode fork pointer */
1596{
1597 if (ifp->if_flags & XFS_IFEXTIREC) {
Alex Lyakas32b43ab2016-05-18 14:01:52 +10001598 xfs_iext_irec_remove_all(ifp);
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001599 } else if (ifp->if_real_bytes) {
1600 kmem_free(ifp->if_u1.if_extents);
1601 } else if (ifp->if_bytes) {
1602 memset(ifp->if_u2.if_inline_ext, 0, XFS_INLINE_EXTS *
1603 sizeof(xfs_bmbt_rec_t));
1604 }
1605 ifp->if_u1.if_extents = NULL;
1606 ifp->if_real_bytes = 0;
1607 ifp->if_bytes = 0;
1608}
1609
1610/*
1611 * Return a pointer to the extent record for file system block bno.
1612 */
1613xfs_bmbt_rec_host_t * /* pointer to found extent record */
1614xfs_iext_bno_to_ext(
1615 xfs_ifork_t *ifp, /* inode fork pointer */
1616 xfs_fileoff_t bno, /* block number to search for */
1617 xfs_extnum_t *idxp) /* index of target extent */
1618{
1619 xfs_bmbt_rec_host_t *base; /* pointer to first extent */
1620 xfs_filblks_t blockcount = 0; /* number of blocks in extent */
1621 xfs_bmbt_rec_host_t *ep = NULL; /* pointer to target extent */
1622 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1623 int high; /* upper boundary in search */
1624 xfs_extnum_t idx = 0; /* index of target extent */
1625 int low; /* lower boundary in search */
1626 xfs_extnum_t nextents; /* number of file extents */
1627 xfs_fileoff_t startoff = 0; /* start offset of extent */
1628
Eric Sandeenf380ee72017-01-09 16:38:36 +01001629 nextents = xfs_iext_count(ifp);
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001630 if (nextents == 0) {
1631 *idxp = 0;
1632 return NULL;
1633 }
1634 low = 0;
1635 if (ifp->if_flags & XFS_IFEXTIREC) {
1636 /* Find target extent list */
1637 int erp_idx = 0;
1638 erp = xfs_iext_bno_to_irec(ifp, bno, &erp_idx);
1639 base = erp->er_extbuf;
1640 high = erp->er_extcount - 1;
1641 } else {
1642 base = ifp->if_u1.if_extents;
1643 high = nextents - 1;
1644 }
1645 /* Binary search extent records */
1646 while (low <= high) {
1647 idx = (low + high) >> 1;
1648 ep = base + idx;
1649 startoff = xfs_bmbt_get_startoff(ep);
1650 blockcount = xfs_bmbt_get_blockcount(ep);
1651 if (bno < startoff) {
1652 high = idx - 1;
1653 } else if (bno >= startoff + blockcount) {
1654 low = idx + 1;
1655 } else {
1656 /* Convert back to file-based extent index */
1657 if (ifp->if_flags & XFS_IFEXTIREC) {
1658 idx += erp->er_extoff;
1659 }
1660 *idxp = idx;
1661 return ep;
1662 }
1663 }
1664 /* Convert back to file-based extent index */
1665 if (ifp->if_flags & XFS_IFEXTIREC) {
1666 idx += erp->er_extoff;
1667 }
1668 if (bno >= startoff + blockcount) {
1669 if (++idx == nextents) {
1670 ep = NULL;
1671 } else {
1672 ep = xfs_iext_get_ext(ifp, idx);
1673 }
1674 }
1675 *idxp = idx;
1676 return ep;
1677}
1678
1679/*
1680 * Return a pointer to the indirection array entry containing the
1681 * extent record for filesystem block bno. Store the index of the
1682 * target irec in *erp_idxp.
1683 */
1684xfs_ext_irec_t * /* pointer to found extent record */
1685xfs_iext_bno_to_irec(
1686 xfs_ifork_t *ifp, /* inode fork pointer */
1687 xfs_fileoff_t bno, /* block number to search for */
1688 int *erp_idxp) /* irec index of target ext list */
1689{
1690 xfs_ext_irec_t *erp = NULL; /* indirection array pointer */
1691 xfs_ext_irec_t *erp_next; /* next indirection array entry */
1692 int erp_idx; /* indirection array index */
1693 int nlists; /* number of extent irec's (lists) */
1694 int high; /* binary search upper limit */
1695 int low; /* binary search lower limit */
1696
1697 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1698 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1699 erp_idx = 0;
1700 low = 0;
1701 high = nlists - 1;
1702 while (low <= high) {
1703 erp_idx = (low + high) >> 1;
1704 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1705 erp_next = erp_idx < nlists - 1 ? erp + 1 : NULL;
1706 if (bno < xfs_bmbt_get_startoff(erp->er_extbuf)) {
1707 high = erp_idx - 1;
1708 } else if (erp_next && bno >=
1709 xfs_bmbt_get_startoff(erp_next->er_extbuf)) {
1710 low = erp_idx + 1;
1711 } else {
1712 break;
1713 }
1714 }
1715 *erp_idxp = erp_idx;
1716 return erp;
1717}
1718
1719/*
1720 * Return a pointer to the indirection array entry containing the
1721 * extent record at file extent index *idxp. Store the index of the
1722 * target irec in *erp_idxp and store the page index of the target
1723 * extent record in *idxp.
1724 */
1725xfs_ext_irec_t *
1726xfs_iext_idx_to_irec(
1727 xfs_ifork_t *ifp, /* inode fork pointer */
1728 xfs_extnum_t *idxp, /* extent index (file -> page) */
1729 int *erp_idxp, /* pointer to target irec */
1730 int realloc) /* new bytes were just added */
1731{
1732 xfs_ext_irec_t *prev; /* pointer to previous irec */
1733 xfs_ext_irec_t *erp = NULL; /* pointer to current irec */
1734 int erp_idx; /* indirection array index */
1735 int nlists; /* number of irec's (ex lists) */
1736 int high; /* binary search upper limit */
1737 int low; /* binary search lower limit */
1738 xfs_extnum_t page_idx = *idxp; /* extent index in target list */
1739
1740 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1741 ASSERT(page_idx >= 0);
Eric Sandeenf380ee72017-01-09 16:38:36 +01001742 ASSERT(page_idx <= xfs_iext_count(ifp));
1743 ASSERT(page_idx < xfs_iext_count(ifp) || realloc);
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001744
1745 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1746 erp_idx = 0;
1747 low = 0;
1748 high = nlists - 1;
1749
1750 /* Binary search extent irec's */
1751 while (low <= high) {
1752 erp_idx = (low + high) >> 1;
1753 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1754 prev = erp_idx > 0 ? erp - 1 : NULL;
1755 if (page_idx < erp->er_extoff || (page_idx == erp->er_extoff &&
1756 realloc && prev && prev->er_extcount < XFS_LINEAR_EXTS)) {
1757 high = erp_idx - 1;
1758 } else if (page_idx > erp->er_extoff + erp->er_extcount ||
1759 (page_idx == erp->er_extoff + erp->er_extcount &&
1760 !realloc)) {
1761 low = erp_idx + 1;
1762 } else if (page_idx == erp->er_extoff + erp->er_extcount &&
1763 erp->er_extcount == XFS_LINEAR_EXTS) {
1764 ASSERT(realloc);
1765 page_idx = 0;
1766 erp_idx++;
1767 erp = erp_idx < nlists ? erp + 1 : NULL;
1768 break;
1769 } else {
1770 page_idx -= erp->er_extoff;
1771 break;
1772 }
1773 }
1774 *idxp = page_idx;
1775 *erp_idxp = erp_idx;
Eric Sandeend99831f2014-06-22 15:03:54 +10001776 return erp;
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001777}
1778
1779/*
1780 * Allocate and initialize an indirection array once the space needed
1781 * for incore extents increases above XFS_IEXT_BUFSZ.
1782 */
1783void
1784xfs_iext_irec_init(
1785 xfs_ifork_t *ifp) /* inode fork pointer */
1786{
1787 xfs_ext_irec_t *erp; /* indirection array pointer */
1788 xfs_extnum_t nextents; /* number of extents in file */
1789
1790 ASSERT(!(ifp->if_flags & XFS_IFEXTIREC));
Eric Sandeenf380ee72017-01-09 16:38:36 +01001791 nextents = xfs_iext_count(ifp);
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001792 ASSERT(nextents <= XFS_LINEAR_EXTS);
1793
1794 erp = kmem_alloc(sizeof(xfs_ext_irec_t), KM_NOFS);
1795
1796 if (nextents == 0) {
1797 ifp->if_u1.if_extents = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1798 } else if (!ifp->if_real_bytes) {
1799 xfs_iext_inline_to_direct(ifp, XFS_IEXT_BUFSZ);
1800 } else if (ifp->if_real_bytes < XFS_IEXT_BUFSZ) {
1801 xfs_iext_realloc_direct(ifp, XFS_IEXT_BUFSZ);
1802 }
1803 erp->er_extbuf = ifp->if_u1.if_extents;
1804 erp->er_extcount = nextents;
1805 erp->er_extoff = 0;
1806
1807 ifp->if_flags |= XFS_IFEXTIREC;
1808 ifp->if_real_bytes = XFS_IEXT_BUFSZ;
1809 ifp->if_bytes = nextents * sizeof(xfs_bmbt_rec_t);
1810 ifp->if_u1.if_ext_irec = erp;
1811
1812 return;
1813}
1814
1815/*
1816 * Allocate and initialize a new entry in the indirection array.
1817 */
1818xfs_ext_irec_t *
1819xfs_iext_irec_new(
1820 xfs_ifork_t *ifp, /* inode fork pointer */
1821 int erp_idx) /* index for new irec */
1822{
1823 xfs_ext_irec_t *erp; /* indirection array pointer */
1824 int i; /* loop counter */
1825 int nlists; /* number of irec's (ex lists) */
1826
1827 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1828 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1829
1830 /* Resize indirection array */
1831 xfs_iext_realloc_indirect(ifp, ++nlists *
1832 sizeof(xfs_ext_irec_t));
1833 /*
1834 * Move records down in the array so the
1835 * new page can use erp_idx.
1836 */
1837 erp = ifp->if_u1.if_ext_irec;
1838 for (i = nlists - 1; i > erp_idx; i--) {
1839 memmove(&erp[i], &erp[i-1], sizeof(xfs_ext_irec_t));
1840 }
1841 ASSERT(i == erp_idx);
1842
1843 /* Initialize new extent record */
1844 erp = ifp->if_u1.if_ext_irec;
1845 erp[erp_idx].er_extbuf = kmem_alloc(XFS_IEXT_BUFSZ, KM_NOFS);
1846 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1847 memset(erp[erp_idx].er_extbuf, 0, XFS_IEXT_BUFSZ);
1848 erp[erp_idx].er_extcount = 0;
1849 erp[erp_idx].er_extoff = erp_idx > 0 ?
1850 erp[erp_idx-1].er_extoff + erp[erp_idx-1].er_extcount : 0;
1851 return (&erp[erp_idx]);
1852}
1853
1854/*
1855 * Remove a record from the indirection array.
1856 */
1857void
1858xfs_iext_irec_remove(
1859 xfs_ifork_t *ifp, /* inode fork pointer */
1860 int erp_idx) /* irec index to remove */
1861{
1862 xfs_ext_irec_t *erp; /* indirection array pointer */
1863 int i; /* loop counter */
1864 int nlists; /* number of irec's (ex lists) */
1865
1866 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1867 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1868 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1869 if (erp->er_extbuf) {
1870 xfs_iext_irec_update_extoffs(ifp, erp_idx + 1,
1871 -erp->er_extcount);
1872 kmem_free(erp->er_extbuf);
1873 }
1874 /* Compact extent records */
1875 erp = ifp->if_u1.if_ext_irec;
1876 for (i = erp_idx; i < nlists - 1; i++) {
1877 memmove(&erp[i], &erp[i+1], sizeof(xfs_ext_irec_t));
1878 }
1879 /*
1880 * Manually free the last extent record from the indirection
1881 * array. A call to xfs_iext_realloc_indirect() with a size
1882 * of zero would result in a call to xfs_iext_destroy() which
1883 * would in turn call this function again, creating a nasty
1884 * infinite loop.
1885 */
1886 if (--nlists) {
1887 xfs_iext_realloc_indirect(ifp,
1888 nlists * sizeof(xfs_ext_irec_t));
1889 } else {
1890 kmem_free(ifp->if_u1.if_ext_irec);
1891 }
1892 ifp->if_real_bytes = nlists * XFS_IEXT_BUFSZ;
1893}
1894
1895/*
1896 * This is called to clean up large amounts of unused memory allocated
1897 * by the indirection array. Before compacting anything though, verify
1898 * that the indirection array is still needed and switch back to the
1899 * linear extent list (or even the inline buffer) if possible. The
1900 * compaction policy is as follows:
1901 *
1902 * Full Compaction: Extents fit into a single page (or inline buffer)
1903 * Partial Compaction: Extents occupy less than 50% of allocated space
1904 * No Compaction: Extents occupy at least 50% of allocated space
1905 */
1906void
1907xfs_iext_irec_compact(
1908 xfs_ifork_t *ifp) /* inode fork pointer */
1909{
1910 xfs_extnum_t nextents; /* number of extents in file */
1911 int nlists; /* number of irec's (ex lists) */
1912
1913 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1914 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
Eric Sandeenf380ee72017-01-09 16:38:36 +01001915 nextents = xfs_iext_count(ifp);
Dave Chinner5c4d97d2013-08-12 20:49:33 +10001916
1917 if (nextents == 0) {
1918 xfs_iext_destroy(ifp);
1919 } else if (nextents <= XFS_INLINE_EXTS) {
1920 xfs_iext_indirect_to_direct(ifp);
1921 xfs_iext_direct_to_inline(ifp, nextents);
1922 } else if (nextents <= XFS_LINEAR_EXTS) {
1923 xfs_iext_indirect_to_direct(ifp);
1924 } else if (nextents < (nlists * XFS_LINEAR_EXTS) >> 1) {
1925 xfs_iext_irec_compact_pages(ifp);
1926 }
1927}
1928
1929/*
1930 * Combine extents from neighboring extent pages.
1931 */
1932void
1933xfs_iext_irec_compact_pages(
1934 xfs_ifork_t *ifp) /* inode fork pointer */
1935{
1936 xfs_ext_irec_t *erp, *erp_next;/* pointers to irec entries */
1937 int erp_idx = 0; /* indirection array index */
1938 int nlists; /* number of irec's (ex lists) */
1939
1940 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1941 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1942 while (erp_idx < nlists - 1) {
1943 erp = &ifp->if_u1.if_ext_irec[erp_idx];
1944 erp_next = erp + 1;
1945 if (erp_next->er_extcount <=
1946 (XFS_LINEAR_EXTS - erp->er_extcount)) {
1947 memcpy(&erp->er_extbuf[erp->er_extcount],
1948 erp_next->er_extbuf, erp_next->er_extcount *
1949 sizeof(xfs_bmbt_rec_t));
1950 erp->er_extcount += erp_next->er_extcount;
1951 /*
1952 * Free page before removing extent record
1953 * so er_extoffs don't get modified in
1954 * xfs_iext_irec_remove.
1955 */
1956 kmem_free(erp_next->er_extbuf);
1957 erp_next->er_extbuf = NULL;
1958 xfs_iext_irec_remove(ifp, erp_idx + 1);
1959 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1960 } else {
1961 erp_idx++;
1962 }
1963 }
1964}
1965
1966/*
1967 * This is called to update the er_extoff field in the indirection
1968 * array when extents have been added or removed from one of the
1969 * extent lists. erp_idx contains the irec index to begin updating
1970 * at and ext_diff contains the number of extents that were added
1971 * or removed.
1972 */
1973void
1974xfs_iext_irec_update_extoffs(
1975 xfs_ifork_t *ifp, /* inode fork pointer */
1976 int erp_idx, /* irec index to update */
1977 int ext_diff) /* number of new extents */
1978{
1979 int i; /* loop counter */
1980 int nlists; /* number of irec's (ex lists */
1981
1982 ASSERT(ifp->if_flags & XFS_IFEXTIREC);
1983 nlists = ifp->if_real_bytes / XFS_IEXT_BUFSZ;
1984 for (i = erp_idx; i < nlists; i++) {
1985 ifp->if_u1.if_ext_irec[i].er_extoff += ext_diff;
1986 }
1987}
Darrick J. Wong3993bae2016-10-03 09:11:32 -07001988
1989/*
1990 * Initialize an inode's copy-on-write fork.
1991 */
1992void
1993xfs_ifork_init_cow(
1994 struct xfs_inode *ip)
1995{
1996 if (ip->i_cowfp)
1997 return;
1998
1999 ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
2000 KM_SLEEP | KM_NOFS);
2001 ip->i_cowfp->if_flags = XFS_IFEXTENTS;
2002 ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
2003 ip->i_cnextents = 0;
2004}
Christoph Hellwig49dc1992017-01-09 16:38:39 +01002005
2006/*
2007 * Lookup the extent covering bno.
2008 *
2009 * If there is an extent covering bno return the extent index, and store the
2010 * expanded extent structure in *gotp, and the extent index in *idx.
2011 * If there is no extent covering bno, but there is an extent after it (e.g.
2012 * it lies in a hole) return that extent in *gotp and its index in *idx
2013 * instead.
2014 * If bno is beyond the last extent return false, and return the index after
2015 * the last valid index in *idxp.
2016 */
2017bool
2018xfs_iext_lookup_extent(
2019 struct xfs_inode *ip,
2020 struct xfs_ifork *ifp,
2021 xfs_fileoff_t bno,
2022 xfs_extnum_t *idxp,
2023 struct xfs_bmbt_irec *gotp)
2024{
2025 struct xfs_bmbt_rec_host *ep;
2026
2027 XFS_STATS_INC(ip->i_mount, xs_look_exlist);
2028
2029 ep = xfs_iext_bno_to_ext(ifp, bno, idxp);
2030 if (!ep)
2031 return false;
2032 xfs_bmbt_get_all(ep, gotp);
2033 return true;
2034}
2035
2036/*
2037 * Return true if there is an extent at index idx, and return the expanded
2038 * extent structure at idx in that case. Else return false.
2039 */
2040bool
2041xfs_iext_get_extent(
2042 struct xfs_ifork *ifp,
2043 xfs_extnum_t idx,
2044 struct xfs_bmbt_irec *gotp)
2045{
2046 if (idx < 0 || idx >= xfs_iext_count(ifp))
2047 return false;
2048 xfs_bmbt_get_all(xfs_iext_get_ext(ifp, idx), gotp);
2049 return true;
2050}