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