]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxfs/xfs_inode_fork.c
xfs: convert to SPDX license tags
[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;
158 ifp->if_real_bytes = real_size;
159 ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
160 ifp->if_flags |= XFS_IFINLINE;
161}
162
5d90ab5a
DC
163/*
164 * The file is in-lined in the on-disk inode.
5d90ab5a
DC
165 */
166STATIC int
167xfs_iformat_local(
168 xfs_inode_t *ip,
169 xfs_dinode_t *dip,
170 int whichfork,
171 int size)
172{
5d90ab5a
DC
173 /*
174 * If the size is unreasonable, then something
175 * is wrong and we just bail out rather than crash in
176 * kmem_alloc() or memcpy() below.
177 */
178 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
179 xfs_warn(ip->i_mount,
180 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
181 (unsigned long long) ip->i_ino, size,
182 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
b02a2c9e
DW
183 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
184 "xfs_iformat_local", dip, sizeof(*dip),
185 __this_address);
12b53197 186 return -EFSCORRUPTED;
5d90ab5a 187 }
219eec76
CH
188
189 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
5d90ab5a
DC
190 return 0;
191}
192
193/*
1f2a4478 194 * The file consists of a set of extents all of which fit into the on-disk
07fe4665 195 * inode.
5d90ab5a
DC
196 */
197STATIC int
198xfs_iformat_extents(
1f2a4478
CH
199 struct xfs_inode *ip,
200 struct xfs_dinode *dip,
201 int whichfork)
5d90ab5a 202{
1f2a4478
CH
203 struct xfs_mount *mp = ip->i_mount;
204 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
322fd804 205 int state = xfs_bmap_fork_to_state(whichfork);
1f2a4478
CH
206 int nex = XFS_DFORK_NEXTENTS(dip, whichfork);
207 int size = nex * sizeof(xfs_bmbt_rec_t);
9788e059 208 struct xfs_iext_cursor icur;
1f2a4478 209 struct xfs_bmbt_rec *dp;
b37d753d 210 struct xfs_bmbt_irec new;
1f2a4478 211 int i;
5d90ab5a
DC
212
213 /*
1f2a4478
CH
214 * If the number of extents is unreasonable, then something is wrong and
215 * we just bail out rather than crash in kmem_alloc() or memcpy() below.
5d90ab5a 216 */
1f2a4478 217 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
5d90ab5a
DC
218 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
219 (unsigned long long) ip->i_ino, nex);
b02a2c9e
DW
220 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
221 "xfs_iformat_extents(1)", dip, sizeof(*dip),
222 __this_address);
12b53197 223 return -EFSCORRUPTED;
5d90ab5a
DC
224 }
225
226 ifp->if_real_bytes = 0;
b37d753d
CH
227 ifp->if_bytes = 0;
228 ifp->if_u1.if_root = NULL;
229 ifp->if_height = 0;
5d90ab5a
DC
230 if (size) {
231 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
9788e059
CH
232
233 xfs_iext_first(ifp, &icur);
5d90ab5a 234 for (i = 0; i < nex; i++, dp++) {
0cf6a3a9
DW
235 xfs_failaddr_t fa;
236
080f0c71 237 xfs_bmbt_disk_get_all(dp, &new);
0cf6a3a9
DW
238 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
239 if (fa) {
240 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
241 "xfs_iformat_extents(2)",
242 dp, sizeof(*dp), fa);
1f2a4478
CH
243 return -EFSCORRUPTED;
244 }
9788e059 245
26a75f67 246 xfs_iext_insert(ip, &icur, &new, state);
9788e059
CH
247 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
248 xfs_iext_next(ifp, &icur);
5d90ab5a 249 }
5d90ab5a
DC
250 }
251 ifp->if_flags |= XFS_IFEXTENTS;
252 return 0;
253}
254
255/*
256 * The file has too many extents to fit into
257 * the inode, so they are in B-tree format.
258 * Allocate a buffer for the root of the B-tree
259 * and copy the root into it. The i_extents
260 * field will remain NULL until all of the
261 * extents are read in (when they are needed).
262 */
263STATIC int
264xfs_iformat_btree(
265 xfs_inode_t *ip,
266 xfs_dinode_t *dip,
267 int whichfork)
268{
269 struct xfs_mount *mp = ip->i_mount;
270 xfs_bmdr_block_t *dfp;
271 xfs_ifork_t *ifp;
272 /* REFERENCED */
273 int nrecs;
274 int size;
81bf5e85 275 int level;
5d90ab5a
DC
276
277 ifp = XFS_IFORK_PTR(ip, whichfork);
278 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
279 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
280 nrecs = be16_to_cpu(dfp->bb_numrecs);
81bf5e85 281 level = be16_to_cpu(dfp->bb_level);
5d90ab5a
DC
282
283 /*
284 * blow out if -- fork has less extents than can fit in
285 * fork (fork shouldn't be a btree format), root btree
286 * block has more records than can fit into the fork,
287 * or the number of extents is greater than the number of
288 * blocks.
289 */
290 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
291 XFS_IFORK_MAXEXT(ip, whichfork) ||
e4b963e9 292 nrecs == 0 ||
5d90ab5a
DC
293 XFS_BMDR_SPACE_CALC(nrecs) >
294 XFS_DFORK_SIZE(dip, mp, whichfork) ||
81bf5e85
DW
295 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) ||
296 level == 0 || level > XFS_BTREE_MAXLEVELS) {
5d90ab5a
DC
297 xfs_warn(mp, "corrupt inode %Lu (btree).",
298 (unsigned long long) ip->i_ino);
b02a2c9e
DW
299 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
300 "xfs_iformat_btree", dfp, size,
301 __this_address);
12b53197 302 return -EFSCORRUPTED;
5d90ab5a
DC
303 }
304
305 ifp->if_broot_bytes = size;
306 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
307 ASSERT(ifp->if_broot != NULL);
308 /*
309 * Copy and convert from the on-disk structure
310 * to the in-memory structure.
311 */
312 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
313 ifp->if_broot, size);
314 ifp->if_flags &= ~XFS_IFEXTENTS;
315 ifp->if_flags |= XFS_IFBROOT;
316
b37d753d
CH
317 ifp->if_real_bytes = 0;
318 ifp->if_bytes = 0;
319 ifp->if_u1.if_root = NULL;
320 ifp->if_height = 0;
5d90ab5a
DC
321 return 0;
322}
323
5d90ab5a
DC
324/*
325 * Reallocate the space for if_broot based on the number of records
326 * being added or deleted as indicated in rec_diff. Move the records
327 * and pointers in if_broot to fit the new size. When shrinking this
328 * will eliminate holes between the records and pointers created by
329 * the caller. When growing this will create holes to be filled in
330 * by the caller.
331 *
332 * The caller must not request to add more records than would fit in
333 * the on-disk inode root. If the if_broot is currently NULL, then
e6d77a21 334 * if we are adding records, one will be allocated. The caller must also
5d90ab5a
DC
335 * not request that the number of records go below zero, although
336 * it can go to zero.
337 *
338 * ip -- the inode whose if_broot area is changing
339 * ext_diff -- the change in the number of records, positive or negative,
340 * requested for the if_broot array.
341 */
342void
343xfs_iroot_realloc(
344 xfs_inode_t *ip,
345 int rec_diff,
346 int whichfork)
347{
348 struct xfs_mount *mp = ip->i_mount;
349 int cur_max;
350 xfs_ifork_t *ifp;
351 struct xfs_btree_block *new_broot;
352 int new_max;
353 size_t new_size;
354 char *np;
355 char *op;
356
357 /*
358 * Handle the degenerate case quietly.
359 */
360 if (rec_diff == 0) {
361 return;
362 }
363
364 ifp = XFS_IFORK_PTR(ip, whichfork);
365 if (rec_diff > 0) {
366 /*
367 * If there wasn't any memory allocated before, just
368 * allocate it now and get out.
369 */
370 if (ifp->if_broot_bytes == 0) {
371 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
372 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
373 ifp->if_broot_bytes = (int)new_size;
374 return;
375 }
376
377 /*
378 * If there is already an existing if_broot, then we need
379 * to realloc() it and shift the pointers to their new
380 * location. The records don't change location because
381 * they are kept butted up against the btree block header.
382 */
383 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
384 new_max = cur_max + rec_diff;
385 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
386 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
5d90ab5a
DC
387 KM_SLEEP | KM_NOFS);
388 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
389 ifp->if_broot_bytes);
390 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
391 (int)new_size);
392 ifp->if_broot_bytes = (int)new_size;
393 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
394 XFS_IFORK_SIZE(ip, whichfork));
5a35bf2c 395 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
5d90ab5a
DC
396 return;
397 }
398
399 /*
400 * rec_diff is less than 0. In this case, we are shrinking the
401 * if_broot buffer. It must already exist. If we go to zero
402 * records, just get rid of the root and clear the status bit.
403 */
404 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
405 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
406 new_max = cur_max + rec_diff;
407 ASSERT(new_max >= 0);
408 if (new_max > 0)
409 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
410 else
411 new_size = 0;
412 if (new_size > 0) {
413 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
414 /*
415 * First copy over the btree block header.
416 */
417 memcpy(new_broot, ifp->if_broot,
418 XFS_BMBT_BLOCK_LEN(ip->i_mount));
419 } else {
420 new_broot = NULL;
421 ifp->if_flags &= ~XFS_IFBROOT;
422 }
423
424 /*
425 * Only copy the records and pointers if there are any.
426 */
427 if (new_max > 0) {
428 /*
429 * First copy the records.
430 */
431 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
432 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
433 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
434
435 /*
436 * Then copy the pointers.
437 */
438 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
439 ifp->if_broot_bytes);
440 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
441 (int)new_size);
5a35bf2c 442 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
5d90ab5a
DC
443 }
444 kmem_free(ifp->if_broot);
445 ifp->if_broot = new_broot;
446 ifp->if_broot_bytes = (int)new_size;
447 if (ifp->if_broot)
448 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
449 XFS_IFORK_SIZE(ip, whichfork));
450 return;
451}
452
453
454/*
455 * This is called when the amount of space needed for if_data
456 * is increased or decreased. The change in size is indicated by
457 * the number of bytes that need to be added or deleted in the
458 * byte_diff parameter.
459 *
460 * If the amount of space needed has decreased below the size of the
461 * inline buffer, then switch to using the inline buffer. Otherwise,
462 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
463 * to what is needed.
464 *
465 * ip -- the inode whose if_data area is changing
466 * byte_diff -- the change in the number of bytes, positive or negative,
467 * requested for the if_data array.
468 */
469void
470xfs_idata_realloc(
471 xfs_inode_t *ip,
472 int byte_diff,
473 int whichfork)
474{
475 xfs_ifork_t *ifp;
476 int new_size;
477 int real_size;
478
479 if (byte_diff == 0) {
480 return;
481 }
482
483 ifp = XFS_IFORK_PTR(ip, whichfork);
484 new_size = (int)ifp->if_bytes + byte_diff;
485 ASSERT(new_size >= 0);
486
487 if (new_size == 0) {
07fe4665 488 kmem_free(ifp->if_u1.if_data);
5d90ab5a
DC
489 ifp->if_u1.if_data = NULL;
490 real_size = 0;
5d90ab5a
DC
491 } else {
492 /*
493 * Stuck with malloc/realloc.
494 * For inline data, the underlying buffer must be
495 * a multiple of 4 bytes in size so that it can be
496 * logged and stay on word boundaries. We enforce
497 * that here.
498 */
499 real_size = roundup(new_size, 4);
500 if (ifp->if_u1.if_data == NULL) {
501 ASSERT(ifp->if_real_bytes == 0);
502 ifp->if_u1.if_data = kmem_alloc(real_size,
503 KM_SLEEP | KM_NOFS);
07fe4665 504 } else {
5d90ab5a
DC
505 /*
506 * Only do the realloc if the underlying size
507 * is really changing.
508 */
509 if (ifp->if_real_bytes != real_size) {
510 ifp->if_u1.if_data =
511 kmem_realloc(ifp->if_u1.if_data,
512 real_size,
5d90ab5a
DC
513 KM_SLEEP | KM_NOFS);
514 }
5d90ab5a
DC
515 }
516 }
517 ifp->if_real_bytes = real_size;
518 ifp->if_bytes = new_size;
519 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
520}
521
522void
523xfs_idestroy_fork(
524 xfs_inode_t *ip,
525 int whichfork)
526{
527 xfs_ifork_t *ifp;
528
529 ifp = XFS_IFORK_PTR(ip, whichfork);
530 if (ifp->if_broot != NULL) {
531 kmem_free(ifp->if_broot);
532 ifp->if_broot = NULL;
533 }
534
535 /*
536 * If the format is local, then we can't have an extents
537 * array so just look for an inline data array. If we're
538 * not local then we may or may not have an extents list,
539 * so check and free it up if we do.
540 */
541 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
07fe4665 542 if (ifp->if_u1.if_data != NULL) {
5d90ab5a
DC
543 ASSERT(ifp->if_real_bytes != 0);
544 kmem_free(ifp->if_u1.if_data);
545 ifp->if_u1.if_data = NULL;
546 ifp->if_real_bytes = 0;
547 }
b37d753d 548 } else if ((ifp->if_flags & XFS_IFEXTENTS) && ifp->if_height) {
5d90ab5a
DC
549 xfs_iext_destroy(ifp);
550 }
b37d753d 551
5d90ab5a 552 ASSERT(ifp->if_real_bytes == 0);
b37d753d 553
5d90ab5a
DC
554 if (whichfork == XFS_ATTR_FORK) {
555 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
556 ip->i_afp = NULL;
cb8a004a
DW
557 } else if (whichfork == XFS_COW_FORK) {
558 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
559 ip->i_cowfp = NULL;
5d90ab5a
DC
560 }
561}
562
563/*
ff105f75 564 * Convert in-core extents to on-disk form
5d90ab5a 565 *
ff105f75
DC
566 * In the case of the data fork, the in-core and on-disk fork sizes can be
567 * different due to delayed allocation extents. We only copy on-disk extents
568 * here, so callers must always use the physical fork size to determine the
569 * size of the buffer passed to this routine. We will return the size actually
570 * used.
5d90ab5a
DC
571 */
572int
573xfs_iextents_copy(
f11e7501
CH
574 struct xfs_inode *ip,
575 struct xfs_bmbt_rec *dp,
5d90ab5a
DC
576 int whichfork)
577{
322fd804 578 int state = xfs_bmap_fork_to_state(whichfork);
f11e7501 579 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
9788e059 580 struct xfs_iext_cursor icur;
f11e7501 581 struct xfs_bmbt_irec rec;
9788e059 582 int copied = 0;
5d90ab5a 583
f11e7501 584 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
5d90ab5a
DC
585 ASSERT(ifp->if_bytes > 0);
586
9788e059 587 for_each_xfs_iext(ifp, &icur, &rec) {
f11e7501 588 if (isnullstartblock(rec.br_startblock))
5d90ab5a 589 continue;
0cf6a3a9 590 ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
f11e7501 591 xfs_bmbt_disk_set_all(dp, &rec);
9788e059 592 trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
f11e7501 593 copied += sizeof(struct xfs_bmbt_rec);
5d90ab5a 594 dp++;
5d90ab5a 595 }
5d90ab5a 596
f11e7501
CH
597 ASSERT(copied > 0);
598 ASSERT(copied <= ifp->if_bytes);
599 return copied;
5d90ab5a
DC
600}
601
602/*
603 * Each of the following cases stores data into the same region
604 * of the on-disk inode, so only one of them can be valid at
605 * any given time. While it is possible to have conflicting formats
606 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
607 * in EXTENTS format, this can only happen when the fork has
608 * changed formats after being modified but before being flushed.
609 * In these cases, the format always takes precedence, because the
610 * format indicates the current state of the fork.
611 */
d15188a1 612void
5d90ab5a
DC
613xfs_iflush_fork(
614 xfs_inode_t *ip,
615 xfs_dinode_t *dip,
616 xfs_inode_log_item_t *iip,
ff105f75 617 int whichfork)
5d90ab5a
DC
618{
619 char *cp;
620 xfs_ifork_t *ifp;
621 xfs_mount_t *mp;
622 static const short brootflag[2] =
623 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
624 static const short dataflag[2] =
625 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
626 static const short extflag[2] =
627 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
628
629 if (!iip)
d15188a1 630 return;
5d90ab5a
DC
631 ifp = XFS_IFORK_PTR(ip, whichfork);
632 /*
633 * This can happen if we gave up in iformat in an error path,
634 * for the attribute fork.
635 */
636 if (!ifp) {
637 ASSERT(whichfork == XFS_ATTR_FORK);
d15188a1 638 return;
5d90ab5a
DC
639 }
640 cp = XFS_DFORK_PTR(dip, whichfork);
641 mp = ip->i_mount;
642 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
643 case XFS_DINODE_FMT_LOCAL:
644 if ((iip->ili_fields & dataflag[whichfork]) &&
645 (ifp->if_bytes > 0)) {
646 ASSERT(ifp->if_u1.if_data != NULL);
647 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
648 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
649 }
650 break;
651
652 case XFS_DINODE_FMT_EXTENTS:
653 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
654 !(iip->ili_fields & extflag[whichfork]));
655 if ((iip->ili_fields & extflag[whichfork]) &&
656 (ifp->if_bytes > 0)) {
5d90ab5a
DC
657 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
658 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
659 whichfork);
660 }
661 break;
662
663 case XFS_DINODE_FMT_BTREE:
664 if ((iip->ili_fields & brootflag[whichfork]) &&
665 (ifp->if_broot_bytes > 0)) {
666 ASSERT(ifp->if_broot != NULL);
667 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
668 XFS_IFORK_SIZE(ip, whichfork));
669 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
670 (xfs_bmdr_block_t *)cp,
671 XFS_DFORK_SIZE(dip, mp, whichfork));
672 }
673 break;
674
675 case XFS_DINODE_FMT_DEV:
676 if (iip->ili_fields & XFS_ILOG_DEV) {
677 ASSERT(whichfork == XFS_DATA_FORK);
dc9b1f58
CH
678 xfs_dinode_put_rdev(dip,
679 linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
5d90ab5a
DC
680 }
681 break;
682
5d90ab5a
DC
683 default:
684 ASSERT(0);
685 break;
686 }
687}
688
cb8a004a
DW
689/* Convert bmap state flags to an inode fork. */
690struct xfs_ifork *
691xfs_iext_state_to_fork(
692 struct xfs_inode *ip,
693 int state)
694{
695 if (state & BMAP_COWFORK)
696 return ip->i_cowfp;
697 else if (state & BMAP_ATTRFORK)
698 return ip->i_afp;
699 return &ip->i_df;
700}
701
cb8a004a
DW
702/*
703 * Initialize an inode's copy-on-write fork.
704 */
705void
706xfs_ifork_init_cow(
707 struct xfs_inode *ip)
708{
709 if (ip->i_cowfp)
710 return;
711
712 ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
713 KM_SLEEP | KM_NOFS);
714 ip->i_cowfp->if_flags = XFS_IFEXTENTS;
715 ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
716 ip->i_cnextents = 0;
717}
20e882d4
DW
718
719/* Default fork content verifiers. */
720struct xfs_ifork_ops xfs_default_ifork_ops = {
721 .verify_attr = xfs_attr_shortform_verify,
722 .verify_dir = xfs_dir2_sf_verify,
723 .verify_symlink = xfs_symlink_shortform_verify,
724};
725
726/* Verify the inline contents of the data fork of an inode. */
727xfs_failaddr_t
728xfs_ifork_verify_data(
729 struct xfs_inode *ip,
730 struct xfs_ifork_ops *ops)
731{
732 /* Non-local data fork, we're done. */
733 if (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
734 return NULL;
735
736 /* Check the inline data fork if there is one. */
737 switch (VFS_I(ip)->i_mode & S_IFMT) {
738 case S_IFDIR:
739 return ops->verify_dir(ip);
740 case S_IFLNK:
741 return ops->verify_symlink(ip);
742 default:
743 return NULL;
744 }
745}
746
747/* Verify the inline contents of the attr fork of an inode. */
748xfs_failaddr_t
749xfs_ifork_verify_attr(
750 struct xfs_inode *ip,
751 struct xfs_ifork_ops *ops)
752{
753 /* There has to be an attr fork allocated if aformat is local. */
754 if (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
755 return NULL;
756 if (!XFS_IFORK_PTR(ip, XFS_ATTR_FORK))
757 return __this_address;
758 return ops->verify_attr(ip);
759}