1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
6 #include <linux/log2.h>
10 #include "xfs_shared.h"
11 #include "xfs_format.h"
12 #include "xfs_log_format.h"
13 #include "xfs_trans_resv.h"
14 #include "xfs_mount.h"
15 #include "xfs_inode.h"
16 #include "xfs_trans.h"
17 #include "xfs_inode_item.h"
18 #include "xfs_btree.h"
19 #include "xfs_bmap_btree.h"
21 #include "xfs_error.h"
22 #include "xfs_trace.h"
23 #include "xfs_attr_sf.h"
24 #include "xfs_da_format.h"
25 #include "xfs_da_btree.h"
26 #include "xfs_dir2_priv.h"
27 #include "xfs_attr_leaf.h"
28 #include "xfs_shared.h"
30 kmem_zone_t *xfs_ifork_zone;
32 STATIC int xfs_iformat_local(xfs_inode_t *, xfs_dinode_t *, int, int);
33 STATIC int xfs_iformat_extents(xfs_inode_t *, xfs_dinode_t *, int);
34 STATIC int xfs_iformat_btree(xfs_inode_t *, xfs_dinode_t *, int);
37 * Copy inode type and data and attr format specific information from the
38 * on-disk inode to the in-core inode and fork structures. For fifos, devices,
39 * and sockets this means set i_rdev to the proper value. For files,
40 * directories, and symlinks this means to bring in the in-line data or extent
41 * pointers as well as the attribute fork. For a fork in B-tree format, only
42 * the root is immediately brought in-core. The rest will be read in later when
43 * first referenced (see xfs_iread_extents()).
48 struct xfs_dinode *dip)
50 struct inode *inode = VFS_I(ip);
51 struct xfs_attr_shortform *atp;
56 switch (inode->i_mode & S_IFMT) {
62 inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
68 switch (dip->di_format) {
69 case XFS_DINODE_FMT_LOCAL:
70 di_size = be64_to_cpu(dip->di_size);
72 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK, size);
74 case XFS_DINODE_FMT_EXTENTS:
75 error = xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
77 case XFS_DINODE_FMT_BTREE:
78 error = xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
91 if (xfs_is_reflink_inode(ip)) {
92 ASSERT(ip->i_cowfp == NULL);
93 xfs_ifork_init_cow(ip);
96 if (!XFS_DFORK_Q(dip))
99 ASSERT(ip->i_afp == NULL);
100 ip->i_afp = kmem_zone_zalloc(xfs_ifork_zone, KM_SLEEP | KM_NOFS);
102 switch (dip->di_aformat) {
103 case XFS_DINODE_FMT_LOCAL:
104 atp = (xfs_attr_shortform_t *)XFS_DFORK_APTR(dip);
105 size = be16_to_cpu(atp->hdr.totsize);
107 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK, size);
109 case XFS_DINODE_FMT_EXTENTS:
110 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
112 case XFS_DINODE_FMT_BTREE:
113 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
116 error = -EFSCORRUPTED;
120 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
123 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
125 xfs_idestroy_fork(ip, XFS_DATA_FORK);
132 struct xfs_inode *ip,
137 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
138 int mem_size = size, real_size = 0;
142 * If we are using the local fork to store a symlink body we need to
143 * zero-terminate it so that we can pass it back to the VFS directly.
144 * Overallocate the in-memory fork by one for that and add a zero
145 * to terminate it below.
147 zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
152 real_size = roundup(mem_size, 4);
153 ifp->if_u1.if_data = kmem_alloc(real_size, KM_SLEEP | KM_NOFS);
154 memcpy(ifp->if_u1.if_data, data, size);
156 ifp->if_u1.if_data[size] = '\0';
158 ifp->if_u1.if_data = NULL;
161 ifp->if_bytes = size;
162 ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
163 ifp->if_flags |= XFS_IFINLINE;
167 * The file is in-lined in the on-disk inode.
177 * If the size is unreasonable, then something
178 * is wrong and we just bail out rather than crash in
179 * kmem_alloc() or memcpy() below.
181 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
182 xfs_warn(ip->i_mount,
183 "corrupt inode %Lu (bad size %d for local fork, size = %d).",
184 (unsigned long long) ip->i_ino, size,
185 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
186 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
187 "xfs_iformat_local", dip, sizeof(*dip),
189 return -EFSCORRUPTED;
192 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
197 * The file consists of a set of extents all of which fit into the on-disk
202 struct xfs_inode *ip,
203 struct xfs_dinode *dip,
206 struct xfs_mount *mp = ip->i_mount;
207 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
208 int state = xfs_bmap_fork_to_state(whichfork);
209 int nex = XFS_DFORK_NEXTENTS(dip, whichfork);
210 int size = nex * sizeof(xfs_bmbt_rec_t);
211 struct xfs_iext_cursor icur;
212 struct xfs_bmbt_rec *dp;
213 struct xfs_bmbt_irec new;
217 * If the number of extents is unreasonable, then something is wrong and
218 * we just bail out rather than crash in kmem_alloc() or memcpy() below.
220 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
221 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
222 (unsigned long long) ip->i_ino, nex);
223 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
224 "xfs_iformat_extents(1)", dip, sizeof(*dip),
226 return -EFSCORRUPTED;
230 ifp->if_u1.if_root = NULL;
233 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
235 xfs_iext_first(ifp, &icur);
236 for (i = 0; i < nex; i++, dp++) {
239 xfs_bmbt_disk_get_all(dp, &new);
240 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
242 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
243 "xfs_iformat_extents(2)",
244 dp, sizeof(*dp), fa);
245 return -EFSCORRUPTED;
248 xfs_iext_insert(ip, &icur, &new, state);
249 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
250 xfs_iext_next(ifp, &icur);
253 ifp->if_flags |= XFS_IFEXTENTS;
258 * The file has too many extents to fit into
259 * the inode, so they are in B-tree format.
260 * Allocate a buffer for the root of the B-tree
261 * and copy the root into it. The i_extents
262 * field will remain NULL until all of the
263 * extents are read in (when they are needed).
271 struct xfs_mount *mp = ip->i_mount;
272 xfs_bmdr_block_t *dfp;
273 struct xfs_ifork *ifp;
279 ifp = XFS_IFORK_PTR(ip, whichfork);
280 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
281 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
282 nrecs = be16_to_cpu(dfp->bb_numrecs);
283 level = be16_to_cpu(dfp->bb_level);
286 * blow out if -- fork has less extents than can fit in
287 * fork (fork shouldn't be a btree format), root btree
288 * block has more records than can fit into the fork,
289 * or the number of extents is greater than the number of
292 if (unlikely(XFS_IFORK_NEXTENTS(ip, whichfork) <=
293 XFS_IFORK_MAXEXT(ip, whichfork) ||
295 XFS_BMDR_SPACE_CALC(nrecs) >
296 XFS_DFORK_SIZE(dip, mp, whichfork) ||
297 XFS_IFORK_NEXTENTS(ip, whichfork) > ip->i_d.di_nblocks) ||
298 level == 0 || level > XFS_BTREE_MAXLEVELS) {
299 xfs_warn(mp, "corrupt inode %Lu (btree).",
300 (unsigned long long) ip->i_ino);
301 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
302 "xfs_iformat_btree", dfp, size,
304 return -EFSCORRUPTED;
307 ifp->if_broot_bytes = size;
308 ifp->if_broot = kmem_alloc(size, KM_SLEEP | KM_NOFS);
309 ASSERT(ifp->if_broot != NULL);
311 * Copy and convert from the on-disk structure
312 * to the in-memory structure.
314 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
315 ifp->if_broot, size);
316 ifp->if_flags &= ~XFS_IFEXTENTS;
317 ifp->if_flags |= XFS_IFBROOT;
320 ifp->if_u1.if_root = NULL;
326 * Reallocate the space for if_broot based on the number of records
327 * being added or deleted as indicated in rec_diff. Move the records
328 * and pointers in if_broot to fit the new size. When shrinking this
329 * will eliminate holes between the records and pointers created by
330 * the caller. When growing this will create holes to be filled in
333 * The caller must not request to add more records than would fit in
334 * the on-disk inode root. If the if_broot is currently NULL, then
335 * if we are adding records, one will be allocated. The caller must also
336 * not request that the number of records go below zero, although
339 * ip -- the inode whose if_broot area is changing
340 * ext_diff -- the change in the number of records, positive or negative,
341 * requested for the if_broot array.
349 struct xfs_mount *mp = ip->i_mount;
351 struct xfs_ifork *ifp;
352 struct xfs_btree_block *new_broot;
359 * Handle the degenerate case quietly.
365 ifp = XFS_IFORK_PTR(ip, whichfork);
368 * If there wasn't any memory allocated before, just
369 * allocate it now and get out.
371 if (ifp->if_broot_bytes == 0) {
372 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
373 ifp->if_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
374 ifp->if_broot_bytes = (int)new_size;
379 * If there is already an existing if_broot, then we need
380 * to realloc() it and shift the pointers to their new
381 * location. The records don't change location because
382 * they are kept butted up against the btree block header.
384 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
385 new_max = cur_max + rec_diff;
386 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
387 ifp->if_broot = kmem_realloc(ifp->if_broot, new_size,
389 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
390 ifp->if_broot_bytes);
391 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
393 ifp->if_broot_bytes = (int)new_size;
394 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
395 XFS_IFORK_SIZE(ip, whichfork));
396 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
401 * rec_diff is less than 0. In this case, we are shrinking the
402 * if_broot buffer. It must already exist. If we go to zero
403 * records, just get rid of the root and clear the status bit.
405 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
406 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
407 new_max = cur_max + rec_diff;
408 ASSERT(new_max >= 0);
410 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
414 new_broot = kmem_alloc(new_size, KM_SLEEP | KM_NOFS);
416 * First copy over the btree block header.
418 memcpy(new_broot, ifp->if_broot,
419 XFS_BMBT_BLOCK_LEN(ip->i_mount));
422 ifp->if_flags &= ~XFS_IFBROOT;
426 * Only copy the records and pointers if there are any.
430 * First copy the records.
432 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
433 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
434 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
437 * Then copy the pointers.
439 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
440 ifp->if_broot_bytes);
441 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
443 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
445 kmem_free(ifp->if_broot);
446 ifp->if_broot = new_broot;
447 ifp->if_broot_bytes = (int)new_size;
449 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
450 XFS_IFORK_SIZE(ip, whichfork));
456 * This is called when the amount of space needed for if_data
457 * is increased or decreased. The change in size is indicated by
458 * the number of bytes that need to be added or deleted in the
459 * byte_diff parameter.
461 * If the amount of space needed has decreased below the size of the
462 * inline buffer, then switch to using the inline buffer. Otherwise,
463 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
466 * ip -- the inode whose if_data area is changing
467 * byte_diff -- the change in the number of bytes, positive or negative,
468 * requested for the if_data array.
472 struct xfs_inode *ip,
476 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
477 int new_size = (int)ifp->if_bytes + byte_diff;
479 ASSERT(new_size >= 0);
480 ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));
486 kmem_free(ifp->if_u1.if_data);
487 ifp->if_u1.if_data = NULL;
493 * For inline data, the underlying buffer must be a multiple of 4 bytes
494 * in size so that it can be logged and stay on word boundaries.
495 * We enforce that here.
497 ifp->if_u1.if_data = kmem_realloc(ifp->if_u1.if_data,
498 roundup(new_size, 4), KM_SLEEP | KM_NOFS);
499 ifp->if_bytes = new_size;
507 struct xfs_ifork *ifp;
509 ifp = XFS_IFORK_PTR(ip, whichfork);
510 if (ifp->if_broot != NULL) {
511 kmem_free(ifp->if_broot);
512 ifp->if_broot = NULL;
516 * If the format is local, then we can't have an extents
517 * array so just look for an inline data array. If we're
518 * not local then we may or may not have an extents list,
519 * so check and free it up if we do.
521 if (XFS_IFORK_FORMAT(ip, whichfork) == XFS_DINODE_FMT_LOCAL) {
522 if (ifp->if_u1.if_data != NULL) {
523 kmem_free(ifp->if_u1.if_data);
524 ifp->if_u1.if_data = NULL;
526 } else if ((ifp->if_flags & XFS_IFEXTENTS) && ifp->if_height) {
527 xfs_iext_destroy(ifp);
530 if (whichfork == XFS_ATTR_FORK) {
531 kmem_zone_free(xfs_ifork_zone, ip->i_afp);
533 } else if (whichfork == XFS_COW_FORK) {
534 kmem_zone_free(xfs_ifork_zone, ip->i_cowfp);
540 * Convert in-core extents to on-disk form
542 * In the case of the data fork, the in-core and on-disk fork sizes can be
543 * different due to delayed allocation extents. We only copy on-disk extents
544 * here, so callers must always use the physical fork size to determine the
545 * size of the buffer passed to this routine. We will return the size actually
550 struct xfs_inode *ip,
551 struct xfs_bmbt_rec *dp,
554 int state = xfs_bmap_fork_to_state(whichfork);
555 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
556 struct xfs_iext_cursor icur;
557 struct xfs_bmbt_irec rec;
560 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
561 ASSERT(ifp->if_bytes > 0);
563 for_each_xfs_iext(ifp, &icur, &rec) {
564 if (isnullstartblock(rec.br_startblock))
566 ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
567 xfs_bmbt_disk_set_all(dp, &rec);
568 trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
569 copied += sizeof(struct xfs_bmbt_rec);
574 ASSERT(copied <= ifp->if_bytes);
579 * Each of the following cases stores data into the same region
580 * of the on-disk inode, so only one of them can be valid at
581 * any given time. While it is possible to have conflicting formats
582 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
583 * in EXTENTS format, this can only happen when the fork has
584 * changed formats after being modified but before being flushed.
585 * In these cases, the format always takes precedence, because the
586 * format indicates the current state of the fork.
592 xfs_inode_log_item_t *iip,
596 struct xfs_ifork *ifp;
598 static const short brootflag[2] =
599 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
600 static const short dataflag[2] =
601 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
602 static const short extflag[2] =
603 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
607 ifp = XFS_IFORK_PTR(ip, whichfork);
609 * This can happen if we gave up in iformat in an error path,
610 * for the attribute fork.
613 ASSERT(whichfork == XFS_ATTR_FORK);
616 cp = XFS_DFORK_PTR(dip, whichfork);
618 switch (XFS_IFORK_FORMAT(ip, whichfork)) {
619 case XFS_DINODE_FMT_LOCAL:
620 if ((iip->ili_fields & dataflag[whichfork]) &&
621 (ifp->if_bytes > 0)) {
622 ASSERT(ifp->if_u1.if_data != NULL);
623 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
624 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
628 case XFS_DINODE_FMT_EXTENTS:
629 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
630 !(iip->ili_fields & extflag[whichfork]));
631 if ((iip->ili_fields & extflag[whichfork]) &&
632 (ifp->if_bytes > 0)) {
633 ASSERT(XFS_IFORK_NEXTENTS(ip, whichfork) > 0);
634 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
639 case XFS_DINODE_FMT_BTREE:
640 if ((iip->ili_fields & brootflag[whichfork]) &&
641 (ifp->if_broot_bytes > 0)) {
642 ASSERT(ifp->if_broot != NULL);
643 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
644 XFS_IFORK_SIZE(ip, whichfork));
645 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
646 (xfs_bmdr_block_t *)cp,
647 XFS_DFORK_SIZE(dip, mp, whichfork));
651 case XFS_DINODE_FMT_DEV:
652 if (iip->ili_fields & XFS_ILOG_DEV) {
653 ASSERT(whichfork == XFS_DATA_FORK);
654 xfs_dinode_put_rdev(dip,
655 linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
665 /* Convert bmap state flags to an inode fork. */
667 xfs_iext_state_to_fork(
668 struct xfs_inode *ip,
671 if (state & BMAP_COWFORK)
673 else if (state & BMAP_ATTRFORK)
679 * Initialize an inode's copy-on-write fork.
683 struct xfs_inode *ip)
688 ip->i_cowfp = kmem_zone_zalloc(xfs_ifork_zone,
690 ip->i_cowfp->if_flags = XFS_IFEXTENTS;
691 ip->i_cformat = XFS_DINODE_FMT_EXTENTS;
695 /* Default fork content verifiers. */
696 struct xfs_ifork_ops xfs_default_ifork_ops = {
697 .verify_attr = xfs_attr_shortform_verify,
698 .verify_dir = xfs_dir2_sf_verify,
699 .verify_symlink = xfs_symlink_shortform_verify,
702 /* Verify the inline contents of the data fork of an inode. */
704 xfs_ifork_verify_data(
705 struct xfs_inode *ip,
706 struct xfs_ifork_ops *ops)
708 /* Non-local data fork, we're done. */
709 if (ip->i_d.di_format != XFS_DINODE_FMT_LOCAL)
712 /* Check the inline data fork if there is one. */
713 switch (VFS_I(ip)->i_mode & S_IFMT) {
715 return ops->verify_dir(ip);
717 return ops->verify_symlink(ip);
723 /* Verify the inline contents of the attr fork of an inode. */
725 xfs_ifork_verify_attr(
726 struct xfs_inode *ip,
727 struct xfs_ifork_ops *ops)
729 /* There has to be an attr fork allocated if aformat is local. */
730 if (ip->i_d.di_aformat != XFS_DINODE_FMT_LOCAL)
732 if (!XFS_IFORK_PTR(ip, XFS_ATTR_FORK))
733 return __this_address;
734 return ops->verify_attr(ip);