]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_inode_fork.c
xfs_repair: initialize non-leaf finobt blocks with correct magic
[thirdparty/xfsprogs-dev.git] / libxfs / xfs_inode_fork.c
CommitLineData
37b3b4d6 1// SPDX-License-Identifier: GPL-2.0
5d90ab5a
DC
2/*
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
4 * All Rights Reserved.
5d90ab5a 5 */
9c799827 6#include "libxfs_priv.h"
b626fb59
DC
7#include "xfs_fs.h"
8#include "xfs_format.h"
9#include "xfs_log_format.h"
10#include "xfs_trans_resv.h"
11#include "xfs_mount.h"
12#include "xfs_inode.h"
13#include "xfs_trans.h"
81bf5e85 14#include "xfs_btree.h"
b626fb59
DC
15#include "xfs_bmap_btree.h"
16#include "xfs_bmap.h"
17#include "xfs_trace.h"
18#include "xfs_attr_sf.h"
9a5fc886 19#include "xfs_da_format.h"
fc1d6454
DW
20#include "xfs_da_btree.h"
21#include "xfs_dir2_priv.h"
20e882d4
DW
22#include "xfs_attr_leaf.h"
23#include "xfs_shared.h"
5d90ab5a 24
eae096c5 25
5d90ab5a
DC
26kmem_zone_t *xfs_ifork_zone;
27
28STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
29STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
30STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
31
5d90ab5a 32/*
551174eb
CH
33 * Copy inode type and data and attr format specific information from the
34 * on-disk inode to the in-core inode and fork structures. For fifos, devices,
35 * and sockets this means set i_rdev to the proper value. For files,
36 * directories, and symlinks this means to bring in the in-line data or extent
37 * pointers as well as the attribute fork. For a fork in B-tree format, only
38 * the root is immediately brought in-core. The rest will be read in later when
39 * first referenced (see xfs_iread_extents()).
5d90ab5a
DC
40 */
41int
42xfs_iformat_fork(
551174eb
CH
43 struct xfs_inode *ip,
44 struct xfs_dinode *dip)
5d90ab5a 45{
551174eb
CH
46 struct inode *inode = VFS_I(ip);
47 struct xfs_attr_shortform *atp;
5d90ab5a
DC
48 int size;
49 int error = 0;
50 xfs_fsize_t di_size;
51
551174eb 52 switch (inode->i_mode & S_IFMT) {
5d90ab5a
DC
53 case S_IFIFO:
54 case S_IFCHR:
55 case S_IFBLK:
56 case S_IFSOCK:
5d90ab5a 57 ip->i_d.di_size = 0;
551174eb 58 inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
5d90ab5a
DC
59 break;
60
61 case S_IFREG:
62 case S_IFLNK:
63 case S_IFDIR:
64 switch (dip->di_format) {
65 case XFS_DINODE_FMT_LOCAL:
5d90ab5a 66 di_size = be64_to_cpu(dip->di_size);
5d90ab5a
DC
67 size = (int)di_size;
68 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
69 break;
70 case XFS_DINODE_FMT_EXTENTS:
71 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
72 break;
73 case XFS_DINODE_FMT_BTREE:
74 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
75 break;
76 default:
12b53197 77 return -EFSCORRUPTED;
5d90ab5a
DC
78 }
79 break;
80
81 default:
12b53197 82 return -EFSCORRUPTED;
5d90ab5a 83 }
cb8a004a 84 if (error)
5d90ab5a 85 return error;
cb8a004a
DW
86
87 if (xfs_is_reflink_inode(ip)) {
88 ASSERT(ip->i_cowfp == NULL);
89 xfs_ifork_init_cow(ip);
5d90ab5a 90 }
cb8a004a 91
5d90ab5a
DC
92 if (!XFS_DFORK_Q(dip))
93 return 0;
94
95 ASSERT(ip->i_afp == NULL);
96 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
97
98 switch (dip->di_aformat) {
99 case XFS_DINODE_FMT_LOCAL:
100 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
101 size = be16_to_cpu(atp->hdr.totsize);
102
5d90ab5a
DC
103 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
104 break;
105 case XFS_DINODE_FMT_EXTENTS:
106 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
107 break;
108 case XFS_DINODE_FMT_BTREE:
109 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
110 break;
111 default:
12b53197 112 error = -EFSCORRUPTED;
5d90ab5a
DC
113 break;
114 }
115 if (error) {
116 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
117 ip->i_afp = NULL;
cb8a004a
DW
118 if (ip->i_cowfp)
119 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
120 ip->i_cowfp = NULL;
5d90ab5a
DC
121 xfs_idestroy_fork(ip, XFS_DATA_FORK);
122 }
123 return error;
124}
125
219eec76
CH
126void
127xfs_init_local_fork(
128 struct xfs_inode *ip,
129 int whichfork,
130 const void *data,
131 int size)
132{
133 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
4cab415f
CH
134 int mem_size = size, real_size = 0;
135 bool zero_terminate;
136
137 /*
138 * If we are using the local fork to store a symlink body we need to
139 * zero-terminate it so that we can pass it back to the VFS directly.
140 * Overallocate the in-memory fork by one for that and add a zero
141 * to terminate it below.
142 */
143 zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
144 if (zero_terminate)
145 mem_size++;
219eec76 146
07fe4665 147 if (size) {
4cab415f 148 real_size = roundup(mem_size, 4);
219eec76 149 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
219eec76 150 memcpy(ifp->if_u1.if_data, data, size);
4cab415f
CH
151 if (zero_terminate)
152 ifp->if_u1.if_data[size] = '\0';
07fe4665
CH
153 } else {
154 ifp->if_u1.if_data = NULL;
4cab415f 155 }
219eec76
CH
156
157 ifp->if_bytes = size;
219eec76
CH
158 ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
159 ifp->if_flags |= XFS_IFINLINE;
160}
161
5d90ab5a
DC
162/*
163 * The file is in-lined in the on-disk inode.
5d90ab5a
DC
164 */
165STATIC int
166xfs_iformat_local(
167 xfs_inode_t *ip,
168 xfs_dinode_t *dip,
169 int whichfork,
170 int size)
171{
5d90ab5a
DC
172 /*
173 * If the size is unreasonable, then something
174 * is wrong and we just bail out rather than crash in
175 * kmem_alloc() or memcpy() below.
176 */
177 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
178 xfs_warn(ip->i_mount,
179 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
180 (unsigned long long) ip->i_ino, size,
181 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
b02a2c9e
DW
182 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
183 "xfs_iformat_local", dip, sizeof(*dip),
184 __this_address);
12b53197 185 return -EFSCORRUPTED;
5d90ab5a 186 }
219eec76
CH
187
188 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
5d90ab5a
DC
189 return 0;
190}
191
192/*
1f2a4478 193 * The file consists of a set of extents all of which fit into the on-disk
07fe4665 194 * inode.
5d90ab5a
DC
195 */
196STATIC int
197xfs_iformat_extents(
1f2a4478
CH
198 struct xfs_inode *ip,
199 struct xfs_dinode *dip,
200 int whichfork)
5d90ab5a 201{
1f2a4478
CH
202 struct xfs_mount *mp = ip->i_mount;
203 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
322fd804 204 int state = xfs_bmap_fork_to_state(whichfork);
1f2a4478
CH
205 int nex = XFS_DFORK_NEXTENTS(dip, whichfork);
206 int size = nex * sizeof(xfs_bmbt_rec_t);
9788e059 207 struct xfs_iext_cursor icur;
1f2a4478 208 struct xfs_bmbt_rec *dp;
b37d753d 209 struct xfs_bmbt_irec new;
1f2a4478 210 int i;
5d90ab5a
DC
211
212 /*
1f2a4478
CH
213 * If the number of extents is unreasonable, then something is wrong and
214 * we just bail out rather than crash in kmem_alloc() or memcpy() below.
5d90ab5a 215 */
1f2a4478 216 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
5d90ab5a
DC
217 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
218 (unsigned long long) ip->i_ino, nex);
b02a2c9e
DW
219 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
220 "xfs_iformat_extents(1)", dip, sizeof(*dip),
221 __this_address);
12b53197 222 return -EFSCORRUPTED;
5d90ab5a
DC
223 }
224
b37d753d
CH
225 ifp->if_bytes = 0;
226 ifp->if_u1.if_root = NULL;
227 ifp->if_height = 0;
5d90ab5a
DC
228 if (size) {
229 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
9788e059
CH
230
231 xfs_iext_first(ifp, &icur);
5d90ab5a 232 for (i = 0; i < nex; i++, dp++) {
0cf6a3a9
DW
233 xfs_failaddr_t fa;
234
080f0c71 235 xfs_bmbt_disk_get_all(dp, &new);
0cf6a3a9
DW
236 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
237 if (fa) {
238 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
239 "xfs_iformat_extents(2)",
240 dp, sizeof(*dp), fa);
1f2a4478
CH
241 return -EFSCORRUPTED;
242 }
9788e059 243
26a75f67 244 xfs_iext_insert(ip, &icur, &new, state);
9788e059
CH
245 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
246 xfs_iext_next(ifp, &icur);
5d90ab5a 247 }
5d90ab5a
DC
248 }
249 ifp->if_flags |= XFS_IFEXTENTS;
250 return 0;
251}
252
253/*
254 * The file has too many extents to fit into
255 * the inode, so they are in B-tree format.
256 * Allocate a buffer for the root of the B-tree
257 * and copy the root into it. The i_extents
258 * field will remain NULL until all of the
259 * extents are read in (when they are needed).
260 */
261STATIC int
262xfs_iformat_btree(
263 xfs_inode_t *ip,
264 xfs_dinode_t *dip,
265 int whichfork)
266{
267 struct xfs_mount *mp = ip->i_mount;
268 xfs_bmdr_block_t *dfp;
e07055b8 269 struct xfs_ifork *ifp;
5d90ab5a
DC
270 /* REFERENCED */
271 int nrecs;
272 int size;
81bf5e85 273 int level;
5d90ab5a
DC
274
275 ifp = XFS_IFORK_PTR(ip, whichfork);
276 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
277 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
278 nrecs = be16_to_cpu(dfp->bb_numrecs);
81bf5e85 279 level = be16_to_cpu(dfp->bb_level);
5d90ab5a
DC
280
281 /*
282 * blow out if -- fork has less extents than can fit in
283 * fork (fork shouldn't be a btree format), root btree
284 * block has more records than can fit into the fork,
285 * or the number of extents is greater than the number of
286 * blocks.
287 */
288 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
289 XFS_IFORK_MAXEXT(ip, whichfork) ||
e4b963e9 290 nrecs == 0 ||
5d90ab5a
DC
291 XFS_BMDR_SPACE_CALC(nrecs) >
292 XFS_DFORK_SIZE(dip, mp, whichfork) ||
81bf5e85
DW
293 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) ||
294 level == 0 || level > XFS_BTREE_MAXLEVELS) {
5d90ab5a
DC
295 xfs_warn(mp, "corrupt inode %Lu (btree).",
296 (unsigned long long) ip->i_ino);
b02a2c9e
DW
297 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
298 "xfs_iformat_btree", dfp, size,
299 __this_address);
12b53197 300 return -EFSCORRUPTED;
5d90ab5a
DC
301 }
302
303 ifp->if_broot_bytes = size;
304 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
305 ASSERT(ifp->if_broot != NULL);
306 /*
307 * Copy and convert from the on-disk structure
308 * to the in-memory structure.
309 */
310 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
311 ifp->if_broot, size);
312 ifp->if_flags &= ~XFS_IFEXTENTS;
313 ifp->if_flags |= XFS_IFBROOT;
314
b37d753d
CH
315 ifp->if_bytes = 0;
316 ifp->if_u1.if_root = NULL;
317 ifp->if_height = 0;
5d90ab5a
DC
318 return 0;
319}
320
5d90ab5a
DC
321/*
322 * Reallocate the space for if_broot based on the number of records
323 * being added or deleted as indicated in rec_diff. Move the records
324 * and pointers in if_broot to fit the new size. When shrinking this
325 * will eliminate holes between the records and pointers created by
326 * the caller. When growing this will create holes to be filled in
327 * by the caller.
328 *
329 * The caller must not request to add more records than would fit in
330 * the on-disk inode root. If the if_broot is currently NULL, then
e6d77a21 331 * if we are adding records, one will be allocated. The caller must also
5d90ab5a
DC
332 * not request that the number of records go below zero, although
333 * it can go to zero.
334 *
335 * ip -- the inode whose if_broot area is changing
336 * ext_diff -- the change in the number of records, positive or negative,
337 * requested for the if_broot array.
338 */
339void
340xfs_iroot_realloc(
341 xfs_inode_t *ip,
342 int rec_diff,
343 int whichfork)
344{
345 struct xfs_mount *mp = ip->i_mount;
346 int cur_max;
e07055b8 347 struct xfs_ifork *ifp;
5d90ab5a
DC
348 struct xfs_btree_block *new_broot;
349 int new_max;
350 size_t new_size;
351 char *np;
352 char *op;
353
354 /*
355 * Handle the degenerate case quietly.
356 */
357 if (rec_diff == 0) {
358 return;
359 }
360
361 ifp = XFS_IFORK_PTR(ip, whichfork);
362 if (rec_diff > 0) {
363 /*
364 * If there wasn't any memory allocated before, just
365 * allocate it now and get out.
366 */
367 if (ifp->if_broot_bytes == 0) {
368 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
369 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
370 ifp->if_broot_bytes = (int)new_size;
371 return;
372 }
373
374 /*
375 * If there is already an existing if_broot, then we need
376 * to realloc() it and shift the pointers to their new
377 * location. The records don't change location because
378 * they are kept butted up against the btree block header.
379 */
380 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
381 new_max = cur_max + rec_diff;
382 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
383 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
5d90ab5a
DC
384 KM_SLEEP | KM_NOFS);
385 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
386 ifp->if_broot_bytes);
387 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
388 (int)new_size);
389 ifp->if_broot_bytes = (int)new_size;
390 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
391 XFS_IFORK_SIZE(ip, whichfork));
5a35bf2c 392 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
5d90ab5a
DC
393 return;
394 }
395
396 /*
397 * rec_diff is less than 0. In this case, we are shrinking the
398 * if_broot buffer. It must already exist. If we go to zero
399 * records, just get rid of the root and clear the status bit.
400 */
401 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
402 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
403 new_max = cur_max + rec_diff;
404 ASSERT(new_max >= 0);
405 if (new_max > 0)
406 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
407 else
408 new_size = 0;
409 if (new_size > 0) {
410 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
411 /*
412 * First copy over the btree block header.
413 */
414 memcpy(new_broot, ifp->if_broot,
415 XFS_BMBT_BLOCK_LEN(ip->i_mount));
416 } else {
417 new_broot = NULL;
418 ifp->if_flags &= ~XFS_IFBROOT;
419 }
420
421 /*
422 * Only copy the records and pointers if there are any.
423 */
424 if (new_max > 0) {
425 /*
426 * First copy the records.
427 */
428 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
429 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
430 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
431
432 /*
433 * Then copy the pointers.
434 */
435 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
436 ifp->if_broot_bytes);
437 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
438 (int)new_size);
5a35bf2c 439 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
5d90ab5a
DC
440 }
441 kmem_free(ifp->if_broot);
442 ifp->if_broot = new_broot;
443 ifp->if_broot_bytes = (int)new_size;
444 if (ifp->if_broot)
445 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
446 XFS_IFORK_SIZE(ip, whichfork));
447 return;
448}
449
450
451/*
452 * This is called when the amount of space needed for if_data
453 * is increased or decreased. The change in size is indicated by
454 * the number of bytes that need to be added or deleted in the
455 * byte_diff parameter.
456 *
457 * If the amount of space needed has decreased below the size of the
458 * inline buffer, then switch to using the inline buffer. Otherwise,
459 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
460 * to what is needed.
461 *
462 * ip -- the inode whose if_data area is changing
463 * byte_diff -- the change in the number of bytes, positive or negative,
464 * requested for the if_data array.
465 */
466void
467xfs_idata_realloc(
2282d1d1
CH
468 struct xfs_inode *ip,
469 int byte_diff,
470 int whichfork)
5d90ab5a 471{
2282d1d1
CH
472 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
473 int new_size = (int)ifp->if_bytes + byte_diff;
5d90ab5a 474
5d90ab5a 475 ASSERT(new_size >= 0);
2282d1d1
CH
476 ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));
477
478 if (byte_diff == 0)
479 return;
5d90ab5a
DC
480
481 if (new_size == 0) {
07fe4665 482 kmem_free(ifp->if_u1.if_data);
5d90ab5a 483 ifp->if_u1.if_data = NULL;
2282d1d1
CH
484 ifp->if_bytes = 0;
485 return;
5d90ab5a 486 }
2282d1d1
CH
487
488 /*
489 * For inline data, the underlying buffer must be a multiple of 4 bytes
490 * in size so that it can be logged and stay on word boundaries.
491 * We enforce that here.
492 */
493 ifp->if_u1.if_data = kmem_realloc(ifp->if_u1.if_data,
494 roundup(new_size, 4), KM_SLEEP | KM_NOFS);
5d90ab5a 495 ifp->if_bytes = new_size;
5d90ab5a
DC
496}
497
498void
499xfs_idestroy_fork(
500 xfs_inode_t *ip,
501 int whichfork)
502{
e07055b8 503 struct xfs_ifork *ifp;
5d90ab5a
DC
504
505 ifp = XFS_IFORK_PTR(ip, whichfork);
506 if (ifp->if_broot != NULL) {
507 kmem_free(ifp->if_broot);
508 ifp->if_broot = NULL;
509 }
510
511 /*
512 * If the format is local, then we can't have an extents
513 * array so just look for an inline data array. If we're
514 * not local then we may or may not have an extents list,
515 * so check and free it up if we do.
516 */
517 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
07fe4665 518 if (ifp->if_u1.if_data != NULL) {
5d90ab5a
DC
519 kmem_free(ifp->if_u1.if_data);
520 ifp->if_u1.if_data = NULL;
5d90ab5a 521 }
b37d753d 522 } else if ((ifp->if_flags & XFS_IFEXTENTS) && ifp->if_height) {
5d90ab5a
DC
523 xfs_iext_destroy(ifp);
524 }
b37d753d 525
5d90ab5a
DC
526 if (whichfork == XFS_ATTR_FORK) {
527 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
528 ip->i_afp = NULL;
cb8a004a
DW
529 } else if (whichfork == XFS_COW_FORK) {
530 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
531 ip->i_cowfp = NULL;
5d90ab5a
DC
532 }
533}
534
535/*
ff105f75 536 * Convert in-core extents to on-disk form
5d90ab5a 537 *
ff105f75
DC
538 * In the case of the data fork, the in-core and on-disk fork sizes can be
539 * different due to delayed allocation extents. We only copy on-disk extents
540 * here, so callers must always use the physical fork size to determine the
541 * size of the buffer passed to this routine. We will return the size actually
542 * used.
5d90ab5a
DC
543 */
544int
545xfs_iextents_copy(
f11e7501
CH
546 struct xfs_inode *ip,
547 struct xfs_bmbt_rec *dp,
5d90ab5a
DC
548 int whichfork)
549{
322fd804 550 int state = xfs_bmap_fork_to_state(whichfork);
f11e7501 551 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
9788e059 552 struct xfs_iext_cursor icur;
f11e7501 553 struct xfs_bmbt_irec rec;
9788e059 554 int copied = 0;
5d90ab5a 555
f11e7501 556 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
5d90ab5a
DC
557 ASSERT(ifp->if_bytes > 0);
558
9788e059 559 for_each_xfs_iext(ifp, &icur, &rec) {
f11e7501 560 if (isnullstartblock(rec.br_startblock))
5d90ab5a 561 continue;
0cf6a3a9 562 ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
f11e7501 563 xfs_bmbt_disk_set_all(dp, &rec);
9788e059 564 trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
f11e7501 565 copied += sizeof(struct xfs_bmbt_rec);
5d90ab5a 566 dp++;
5d90ab5a 567 }
5d90ab5a 568
f11e7501
CH
569 ASSERT(copied > 0);
570 ASSERT(copied <= ifp->if_bytes);
571 return copied;
5d90ab5a
DC
572}
573
574/*
575 * Each of the following cases stores data into the same region
576 * of the on-disk inode, so only one of them can be valid at
577 * any given time. While it is possible to have conflicting formats
578 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
579 * in EXTENTS format, this can only happen when the fork has
580 * changed formats after being modified but before being flushed.
581 * In these cases, the format always takes precedence, because the
582 * format indicates the current state of the fork.
583 */
d15188a1 584void
5d90ab5a
DC
585xfs_iflush_fork(
586 xfs_inode_t *ip,
587 xfs_dinode_t *dip,
588 xfs_inode_log_item_t *iip,
ff105f75 589 int whichfork)
5d90ab5a
DC
590{
591 char *cp;
e07055b8 592 struct xfs_ifork *ifp;
5d90ab5a
DC
593 xfs_mount_t *mp;
594 static const short brootflag[2] =
595 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
596 static const short dataflag[2] =
597 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
598 static const short extflag[2] =
599 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
600
601 if (!iip)
d15188a1 602 return;
5d90ab5a
DC
603 ifp = XFS_IFORK_PTR(ip, whichfork);
604 /*
605 * This can happen if we gave up in iformat in an error path,
606 * for the attribute fork.
607 */
608 if (!ifp) {
609 ASSERT(whichfork == XFS_ATTR_FORK);
d15188a1 610 return;
5d90ab5a
DC
611 }
612 cp = XFS_DFORK_PTR(dip, whichfork);
613 mp = ip->i_mount;
614 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
615 case XFS_DINODE_FMT_LOCAL:
616 if ((iip->ili_fields & dataflag[whichfork]) &&
617 (ifp->if_bytes > 0)) {
618 ASSERT(ifp->if_u1.if_data != NULL);
619 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
620 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
621 }
622 break;
623
624 case XFS_DINODE_FMT_EXTENTS:
625 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
626 !(iip->ili_fields & extflag[whichfork]));
627 if ((iip->ili_fields & extflag[whichfork]) &&
628 (ifp->if_bytes > 0)) {
5d90ab5a
DC
629 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
630 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
631 whichfork);
632 }
633 break;
634
635 case XFS_DINODE_FMT_BTREE:
636 if ((iip->ili_fields & brootflag[whichfork]) &&
637 (ifp->if_broot_bytes > 0)) {
638 ASSERT(ifp->if_broot != NULL);
639 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
640 XFS_IFORK_SIZE(ip, whichfork));
641 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
642 (xfs_bmdr_block_t *)cp,
643 XFS_DFORK_SIZE(dip, mp, whichfork));
644 }
645 break;
646
647 case XFS_DINODE_FMT_DEV:
648 if (iip->ili_fields & XFS_ILOG_DEV) {
649 ASSERT(whichfork == XFS_DATA_FORK);
dc9b1f58
CH
650 xfs_dinode_put_rdev(dip,
651 linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
5d90ab5a
DC
652 }
653 break;
654
5d90ab5a
DC
655 default:
656 ASSERT(0);
657 break;
658 }
659}
660
cb8a004a
DW
661/* Convert bmap state flags to an inode fork. */
662struct xfs_ifork *
663xfs_iext_state_to_fork(
664 struct xfs_inode *ip,
665 int state)
666{
667 if (state & BMAP_COWFORK)
668 return ip->i_cowfp;
669 else if (state & BMAP_ATTRFORK)
670 return ip->i_afp;
671 return &ip->i_df;
672}
673
cb8a004a
DW
674/*
675 * Initialize an inode's copy-on-write fork.
676 */
677void
678xfs_ifork_init_cow(
679 struct xfs_inode *ip)
680{
681 if (ip->i_cowfp)
682 return;
683
684 ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
685 KM_SLEEP | KM_NOFS);
686 ip->i_cowfp->if_flags = XFS_IFEXTENTS;
687 ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
688 ip->i_cnextents = 0;
689}
20e882d4
DW
690
691/* Default fork content verifiers. */
692struct xfs_ifork_ops xfs_default_ifork_ops = {
693 .verify_attr = xfs_attr_shortform_verify,
694 .verify_dir = xfs_dir2_sf_verify,
695 .verify_symlink = xfs_symlink_shortform_verify,
696};
697
698/* Verify the inline contents of the data fork of an inode. */
699xfs_failaddr_t
700xfs_ifork_verify_data(
701 struct xfs_inode *ip,
702 struct xfs_ifork_ops *ops)
703{
704 /* Non-local data fork, we're done. */
705 if (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
706 return NULL;
707
708 /* Check the inline data fork if there is one. */
709 switch (VFS_I(ip)->i_mode & S_IFMT) {
710 case S_IFDIR:
711 return ops->verify_dir(ip);
712 case S_IFLNK:
713 return ops->verify_symlink(ip);
714 default:
715 return NULL;
716 }
717}
718
719/* Verify the inline contents of the attr fork of an inode. */
720xfs_failaddr_t
721xfs_ifork_verify_attr(
722 struct xfs_inode *ip,
723 struct xfs_ifork_ops *ops)
724{
725 /* There has to be an attr fork allocated if aformat is local. */
726 if (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
727 return NULL;
728 if (!XFS_IFORK_PTR(ip, XFS_ATTR_FORK))
729 return __this_address;
730 return ops->verify_attr(ip);
731}