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