1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
9 #include "xfs_shared.h"
10 #include "xfs_format.h"
11 #include "xfs_log_format.h"
12 #include "xfs_trans_resv.h"
13 #include "xfs_mount.h"
14 #include "xfs_inode.h"
15 #include "xfs_trans.h"
16 #include "xfs_inode_item.h"
17 #include "xfs_btree.h"
18 #include "xfs_bmap_btree.h"
20 #include "xfs_error.h"
21 #include "xfs_trace.h"
22 #include "xfs_da_format.h"
23 #include "xfs_da_btree.h"
24 #include "xfs_dir2_priv.h"
25 #include "xfs_attr_leaf.h"
26 #include "xfs_types.h"
27 #include "xfs_errortag.h"
29 struct kmem_cache *xfs_ifork_cache;
38 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
43 * If we are using the local fork to store a symlink body we need to
44 * zero-terminate it so that we can pass it back to the VFS directly.
45 * Overallocate the in-memory fork by one for that and add a zero
46 * to terminate it below.
48 zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
53 char *new_data = kmalloc(mem_size,
54 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
56 memcpy(new_data, data, size);
58 new_data[size] = '\0';
60 ifp->if_data = new_data;
69 * The file is in-lined in the on-disk inode.
74 struct xfs_dinode *dip,
79 * If the size is unreasonable, then something
80 * is wrong and we just bail out rather than crash in
81 * kmalloc() or memcpy() below.
83 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
85 "corrupt inode %llu (bad size %d for local fork, size = %zd).",
86 (unsigned long long) ip->i_ino, size,
87 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
88 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
89 "xfs_iformat_local", dip, sizeof(*dip),
94 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
99 * The file consists of a set of extents all of which fit into the on-disk
104 struct xfs_inode *ip,
105 struct xfs_dinode *dip,
108 struct xfs_mount *mp = ip->i_mount;
109 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
110 int state = xfs_bmap_fork_to_state(whichfork);
111 xfs_extnum_t nex = xfs_dfork_nextents(dip, whichfork);
112 int size = nex * sizeof(xfs_bmbt_rec_t);
113 struct xfs_iext_cursor icur;
114 struct xfs_bmbt_rec *dp;
115 struct xfs_bmbt_irec new;
119 * If the number of extents is unreasonable, then something is wrong and
120 * we just bail out rather than crash in kmalloc() or memcpy() below.
122 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
123 xfs_warn(ip->i_mount, "corrupt inode %llu ((a)extents = %llu).",
125 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
126 "xfs_iformat_extents(1)", dip, sizeof(*dip),
128 return -EFSCORRUPTED;
135 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
137 xfs_iext_first(ifp, &icur);
138 for (i = 0; i < nex; i++, dp++) {
141 xfs_bmbt_disk_get_all(dp, &new);
142 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
144 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
145 "xfs_iformat_extents(2)",
146 dp, sizeof(*dp), fa);
147 return xfs_bmap_complain_bad_rec(ip, whichfork,
151 xfs_iext_insert(ip, &icur, &new, state);
152 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
153 xfs_iext_next(ifp, &icur);
160 * The file has too many extents to fit into
161 * the inode, so they are in B-tree format.
162 * Allocate a buffer for the root of the B-tree
163 * and copy the root into it. The i_extents
164 * field will remain NULL until all of the
165 * extents are read in (when they are needed).
169 struct xfs_inode *ip,
170 struct xfs_dinode *dip,
173 struct xfs_mount *mp = ip->i_mount;
174 xfs_bmdr_block_t *dfp;
175 struct xfs_ifork *ifp;
181 ifp = xfs_ifork_ptr(ip, whichfork);
182 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
183 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
184 nrecs = be16_to_cpu(dfp->bb_numrecs);
185 level = be16_to_cpu(dfp->bb_level);
188 * blow out if -- fork has less extents than can fit in
189 * fork (fork shouldn't be a btree format), root btree
190 * block has more records than can fit into the fork,
191 * or the number of extents is greater than the number of
194 if (unlikely(ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork) ||
196 XFS_BMDR_SPACE_CALC(nrecs) >
197 XFS_DFORK_SIZE(dip, mp, whichfork) ||
198 ifp->if_nextents > ip->i_nblocks) ||
199 level == 0 || level > XFS_BM_MAXLEVELS(mp, whichfork)) {
200 xfs_warn(mp, "corrupt inode %llu (btree).",
201 (unsigned long long) ip->i_ino);
202 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
203 "xfs_iformat_btree", dfp, size,
205 return -EFSCORRUPTED;
208 ifp->if_broot_bytes = size;
209 ifp->if_broot = kmalloc(size,
210 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
211 ASSERT(ifp->if_broot != NULL);
213 * Copy and convert from the on-disk structure
214 * to the in-memory structure.
216 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
217 ifp->if_broot, size);
226 xfs_iformat_data_fork(
227 struct xfs_inode *ip,
228 struct xfs_dinode *dip)
230 struct inode *inode = VFS_I(ip);
234 * Initialize the extent count early, as the per-format routines may
235 * depend on it. Use release semantics to set needextents /after/ we
236 * set the format. This ensures that we can use acquire semantics on
237 * needextents in xfs_need_iread_extents() and be guaranteed to see a
238 * valid format value after that load.
240 ip->i_df.if_format = dip->di_format;
241 ip->i_df.if_nextents = xfs_dfork_data_extents(dip);
242 smp_store_release(&ip->i_df.if_needextents,
243 ip->i_df.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0);
245 switch (inode->i_mode & S_IFMT) {
251 inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
256 switch (ip->i_df.if_format) {
257 case XFS_DINODE_FMT_LOCAL:
258 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK,
259 be64_to_cpu(dip->di_size));
261 error = xfs_ifork_verify_local_data(ip);
263 case XFS_DINODE_FMT_EXTENTS:
264 return xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
265 case XFS_DINODE_FMT_BTREE:
266 return xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
268 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
269 dip, sizeof(*dip), __this_address);
270 return -EFSCORRUPTED;
274 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
275 sizeof(*dip), __this_address);
276 return -EFSCORRUPTED;
281 xfs_dfork_attr_shortform_size(
282 struct xfs_dinode *dip)
284 struct xfs_attr_sf_hdr *sf = XFS_DFORK_APTR(dip);
286 return be16_to_cpu(sf->totsize);
291 struct xfs_inode *ip,
292 enum xfs_dinode_fmt format,
293 xfs_extnum_t nextents)
296 * Initialize the extent count early, as the per-format routines may
297 * depend on it. Use release semantics to set needextents /after/ we
298 * set the format. This ensures that we can use acquire semantics on
299 * needextents in xfs_need_iread_extents() and be guaranteed to see a
300 * valid format value after that load.
302 ip->i_af.if_format = format;
303 ip->i_af.if_nextents = nextents;
304 smp_store_release(&ip->i_af.if_needextents,
305 ip->i_af.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0);
310 struct xfs_inode *ip)
312 xfs_idestroy_fork(&ip->i_af);
313 memset(&ip->i_af, 0, sizeof(struct xfs_ifork));
314 ip->i_af.if_format = XFS_DINODE_FMT_EXTENTS;
318 xfs_iformat_attr_fork(
319 struct xfs_inode *ip,
320 struct xfs_dinode *dip)
322 xfs_extnum_t naextents = xfs_dfork_attr_extents(dip);
326 * Initialize the extent count early, as the per-format routines may
329 xfs_ifork_init_attr(ip, dip->di_aformat, naextents);
331 switch (ip->i_af.if_format) {
332 case XFS_DINODE_FMT_LOCAL:
333 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK,
334 xfs_dfork_attr_shortform_size(dip));
336 error = xfs_ifork_verify_local_attr(ip);
338 case XFS_DINODE_FMT_EXTENTS:
339 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
341 case XFS_DINODE_FMT_BTREE:
342 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
345 xfs_inode_verifier_error(ip, error, __func__, dip,
346 sizeof(*dip), __this_address);
347 error = -EFSCORRUPTED;
352 xfs_ifork_zap_attr(ip);
357 * Reallocate the space for if_broot based on the number of records
358 * being added or deleted as indicated in rec_diff. Move the records
359 * and pointers in if_broot to fit the new size. When shrinking this
360 * will eliminate holes between the records and pointers created by
361 * the caller. When growing this will create holes to be filled in
364 * The caller must not request to add more records than would fit in
365 * the on-disk inode root. If the if_broot is currently NULL, then
366 * if we are adding records, one will be allocated. The caller must also
367 * not request that the number of records go below zero, although
370 * ip -- the inode whose if_broot area is changing
371 * ext_diff -- the change in the number of records, positive or negative,
372 * requested for the if_broot array.
380 struct xfs_mount *mp = ip->i_mount;
382 struct xfs_ifork *ifp;
383 struct xfs_btree_block *new_broot;
390 * Handle the degenerate case quietly.
396 ifp = xfs_ifork_ptr(ip, whichfork);
399 * If there wasn't any memory allocated before, just
400 * allocate it now and get out.
402 if (ifp->if_broot_bytes == 0) {
403 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
404 ifp->if_broot = kmalloc(new_size,
405 GFP_KERNEL | __GFP_NOFAIL);
406 ifp->if_broot_bytes = (int)new_size;
411 * If there is already an existing if_broot, then we need
412 * to realloc() it and shift the pointers to their new
413 * location. The records don't change location because
414 * they are kept butted up against the btree block header.
416 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
417 new_max = cur_max + rec_diff;
418 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
419 ifp->if_broot = krealloc(ifp->if_broot, new_size,
420 GFP_KERNEL | __GFP_NOFAIL);
421 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
422 ifp->if_broot_bytes);
423 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
425 ifp->if_broot_bytes = (int)new_size;
426 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
427 xfs_inode_fork_size(ip, whichfork));
428 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
433 * rec_diff is less than 0. In this case, we are shrinking the
434 * if_broot buffer. It must already exist. If we go to zero
435 * records, just get rid of the root and clear the status bit.
437 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
438 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
439 new_max = cur_max + rec_diff;
440 ASSERT(new_max >= 0);
442 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
446 new_broot = kmalloc(new_size, GFP_KERNEL | __GFP_NOFAIL);
448 * First copy over the btree block header.
450 memcpy(new_broot, ifp->if_broot,
451 XFS_BMBT_BLOCK_LEN(ip->i_mount));
457 * Only copy the records and pointers if there are any.
461 * First copy the records.
463 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
464 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
465 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
468 * Then copy the pointers.
470 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
471 ifp->if_broot_bytes);
472 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
474 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
476 kfree(ifp->if_broot);
477 ifp->if_broot = new_broot;
478 ifp->if_broot_bytes = (int)new_size;
480 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
481 xfs_inode_fork_size(ip, whichfork));
487 * This is called when the amount of space needed for if_data
488 * is increased or decreased. The change in size is indicated by
489 * the number of bytes that need to be added or deleted in the
490 * byte_diff parameter.
492 * If the amount of space needed has decreased below the size of the
493 * inline buffer, then switch to using the inline buffer. Otherwise,
494 * use krealloc() or kmalloc() to adjust the size of the buffer
497 * ip -- the inode whose if_data area is changing
498 * byte_diff -- the change in the number of bytes, positive or negative,
499 * requested for the if_data array.
503 struct xfs_inode *ip,
507 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
508 int64_t new_size = ifp->if_bytes + byte_diff;
510 ASSERT(new_size >= 0);
511 ASSERT(new_size <= xfs_inode_fork_size(ip, whichfork));
514 ifp->if_data = krealloc(ifp->if_data, new_size,
515 GFP_KERNEL | __GFP_NOFAIL);
518 ifp->if_bytes = new_size;
524 /* Free all memory and reset a fork back to its initial state. */
527 struct xfs_ifork *ifp)
529 if (ifp->if_broot != NULL) {
530 kfree(ifp->if_broot);
531 ifp->if_broot = NULL;
534 switch (ifp->if_format) {
535 case XFS_DINODE_FMT_LOCAL:
539 case XFS_DINODE_FMT_EXTENTS:
540 case XFS_DINODE_FMT_BTREE:
542 xfs_iext_destroy(ifp);
548 * Convert in-core extents to on-disk form
550 * In the case of the data fork, the in-core and on-disk fork sizes can be
551 * different due to delayed allocation extents. We only copy on-disk extents
552 * here, so callers must always use the physical fork size to determine the
553 * size of the buffer passed to this routine. We will return the size actually
558 struct xfs_inode *ip,
559 struct xfs_bmbt_rec *dp,
562 int state = xfs_bmap_fork_to_state(whichfork);
563 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
564 struct xfs_iext_cursor icur;
565 struct xfs_bmbt_irec rec;
568 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED);
569 ASSERT(ifp->if_bytes > 0);
571 for_each_xfs_iext(ifp, &icur, &rec) {
572 if (isnullstartblock(rec.br_startblock))
574 ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
575 xfs_bmbt_disk_set_all(dp, &rec);
576 trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
577 copied += sizeof(struct xfs_bmbt_rec);
582 ASSERT(copied <= ifp->if_bytes);
587 * Each of the following cases stores data into the same region
588 * of the on-disk inode, so only one of them can be valid at
589 * any given time. While it is possible to have conflicting formats
590 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
591 * in EXTENTS format, this can only happen when the fork has
592 * changed formats after being modified but before being flushed.
593 * In these cases, the format always takes precedence, because the
594 * format indicates the current state of the fork.
598 struct xfs_inode *ip,
599 struct xfs_dinode *dip,
600 struct xfs_inode_log_item *iip,
604 struct xfs_ifork *ifp;
606 static const short brootflag[2] =
607 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
608 static const short dataflag[2] =
609 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
610 static const short extflag[2] =
611 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
615 ifp = xfs_ifork_ptr(ip, whichfork);
617 * This can happen if we gave up in iformat in an error path,
618 * for the attribute fork.
621 ASSERT(whichfork == XFS_ATTR_FORK);
624 cp = XFS_DFORK_PTR(dip, whichfork);
626 switch (ifp->if_format) {
627 case XFS_DINODE_FMT_LOCAL:
628 if ((iip->ili_fields & dataflag[whichfork]) &&
629 (ifp->if_bytes > 0)) {
630 ASSERT(ifp->if_data != NULL);
631 ASSERT(ifp->if_bytes <= xfs_inode_fork_size(ip, whichfork));
632 memcpy(cp, ifp->if_data, ifp->if_bytes);
636 case XFS_DINODE_FMT_EXTENTS:
637 if ((iip->ili_fields & extflag[whichfork]) &&
638 (ifp->if_bytes > 0)) {
639 ASSERT(ifp->if_nextents > 0);
640 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
645 case XFS_DINODE_FMT_BTREE:
646 if ((iip->ili_fields & brootflag[whichfork]) &&
647 (ifp->if_broot_bytes > 0)) {
648 ASSERT(ifp->if_broot != NULL);
649 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
650 xfs_inode_fork_size(ip, whichfork));
651 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
652 (xfs_bmdr_block_t *)cp,
653 XFS_DFORK_SIZE(dip, mp, whichfork));
657 case XFS_DINODE_FMT_DEV:
658 if (iip->ili_fields & XFS_ILOG_DEV) {
659 ASSERT(whichfork == XFS_DATA_FORK);
660 xfs_dinode_put_rdev(dip,
661 linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
671 /* Convert bmap state flags to an inode fork. */
673 xfs_iext_state_to_fork(
674 struct xfs_inode *ip,
677 if (state & BMAP_COWFORK)
679 else if (state & BMAP_ATTRFORK)
685 * Initialize an inode's copy-on-write fork.
689 struct xfs_inode *ip)
694 ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_cache,
695 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
696 ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
699 /* Verify the inline contents of the data fork of an inode. */
701 xfs_ifork_verify_local_data(
702 struct xfs_inode *ip)
704 xfs_failaddr_t fa = NULL;
706 switch (VFS_I(ip)->i_mode & S_IFMT) {
708 struct xfs_mount *mp = ip->i_mount;
709 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
710 struct xfs_dir2_sf_hdr *sfp = ifp->if_data;
712 fa = xfs_dir2_sf_verify(mp, sfp, ifp->if_bytes);
716 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
718 fa = xfs_symlink_shortform_verify(ifp->if_data, ifp->if_bytes);
726 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
727 ip->i_df.if_data, ip->i_df.if_bytes, fa);
728 return -EFSCORRUPTED;
734 /* Verify the inline contents of the attr fork of an inode. */
736 xfs_ifork_verify_local_attr(
737 struct xfs_inode *ip)
739 struct xfs_ifork *ifp = &ip->i_af;
742 if (!xfs_inode_has_attr_fork(ip)) {
745 struct xfs_ifork *ifp = &ip->i_af;
747 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
748 fa = xfs_attr_shortform_verify(ifp->if_data, ifp->if_bytes);
751 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
752 ifp->if_data, ifp->if_bytes, fa);
753 return -EFSCORRUPTED;
760 xfs_iext_count_may_overflow(
761 struct xfs_inode *ip,
765 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
769 if (whichfork == XFS_COW_FORK)
772 max_exts = xfs_iext_max_nextents(xfs_inode_has_large_extent_counts(ip),
775 if (XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
778 nr_exts = ifp->if_nextents + nr_to_add;
779 if (nr_exts < ifp->if_nextents || nr_exts > max_exts)
786 * Upgrade this inode's extent counter fields to be able to handle a potential
787 * increase in the extent count by nr_to_add. Normally this is the same
788 * quantity that caused xfs_iext_count_may_overflow() to return -EFBIG.
791 xfs_iext_count_upgrade(
792 struct xfs_trans *tp,
793 struct xfs_inode *ip,
796 ASSERT(nr_to_add <= XFS_MAX_EXTCNT_UPGRADE_NR);
798 if (!xfs_has_large_extent_counts(ip->i_mount) ||
799 xfs_inode_has_large_extent_counts(ip) ||
800 XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
803 ip->i_diflags2 |= XFS_DIFLAG2_NREXT64;
804 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);