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 ifp->if_u1.if_data = kmem_alloc(mem_size, KM_NOFS);
54 memcpy(ifp->if_u1.if_data, data, size);
56 ifp->if_u1.if_data[size] = '\0';
58 ifp->if_u1.if_data = NULL;
65 * The file is in-lined in the on-disk inode.
70 struct xfs_dinode *dip,
75 * If the size is unreasonable, then something
76 * is wrong and we just bail out rather than crash in
77 * kmem_alloc() or memcpy() below.
79 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
81 "corrupt inode %Lu (bad size %d for local fork, size = %zd).",
82 (unsigned long long) ip->i_ino, size,
83 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
84 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
85 "xfs_iformat_local", dip, sizeof(*dip),
90 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
95 * The file consists of a set of extents all of which fit into the on-disk
100 struct xfs_inode *ip,
101 struct xfs_dinode *dip,
104 struct xfs_mount *mp = ip->i_mount;
105 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
106 int state = xfs_bmap_fork_to_state(whichfork);
107 xfs_extnum_t nex = xfs_dfork_nextents(dip, whichfork);
108 int size = nex * sizeof(xfs_bmbt_rec_t);
109 struct xfs_iext_cursor icur;
110 struct xfs_bmbt_rec *dp;
111 struct xfs_bmbt_irec new;
115 * If the number of extents is unreasonable, then something is wrong and
116 * we just bail out rather than crash in kmem_alloc() or memcpy() below.
118 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
119 xfs_warn(ip->i_mount, "corrupt inode %llu ((a)extents = %llu).",
121 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
122 "xfs_iformat_extents(1)", dip, sizeof(*dip),
124 return -EFSCORRUPTED;
128 ifp->if_u1.if_root = NULL;
131 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
133 xfs_iext_first(ifp, &icur);
134 for (i = 0; i < nex; i++, dp++) {
137 xfs_bmbt_disk_get_all(dp, &new);
138 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
140 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
141 "xfs_iformat_extents(2)",
142 dp, sizeof(*dp), fa);
143 return -EFSCORRUPTED;
146 xfs_iext_insert(ip, &icur, &new, state);
147 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
148 xfs_iext_next(ifp, &icur);
155 * The file has too many extents to fit into
156 * the inode, so they are in B-tree format.
157 * Allocate a buffer for the root of the B-tree
158 * and copy the root into it. The i_extents
159 * field will remain NULL until all of the
160 * extents are read in (when they are needed).
164 struct xfs_inode *ip,
165 struct xfs_dinode *dip,
168 struct xfs_mount *mp = ip->i_mount;
169 xfs_bmdr_block_t *dfp;
170 struct xfs_ifork *ifp;
176 ifp = XFS_IFORK_PTR(ip, whichfork);
177 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
178 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
179 nrecs = be16_to_cpu(dfp->bb_numrecs);
180 level = be16_to_cpu(dfp->bb_level);
183 * blow out if -- fork has less extents than can fit in
184 * fork (fork shouldn't be a btree format), root btree
185 * block has more records than can fit into the fork,
186 * or the number of extents is greater than the number of
189 if (unlikely(ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork) ||
191 XFS_BMDR_SPACE_CALC(nrecs) >
192 XFS_DFORK_SIZE(dip, mp, whichfork) ||
193 ifp->if_nextents > ip->i_nblocks) ||
194 level == 0 || level > XFS_BM_MAXLEVELS(mp, whichfork)) {
195 xfs_warn(mp, "corrupt inode %Lu (btree).",
196 (unsigned long long) ip->i_ino);
197 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
198 "xfs_iformat_btree", dfp, size,
200 return -EFSCORRUPTED;
203 ifp->if_broot_bytes = size;
204 ifp->if_broot = kmem_alloc(size, KM_NOFS);
205 ASSERT(ifp->if_broot != NULL);
207 * Copy and convert from the on-disk structure
208 * to the in-memory structure.
210 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
211 ifp->if_broot, size);
214 ifp->if_u1.if_root = NULL;
220 xfs_iformat_data_fork(
221 struct xfs_inode *ip,
222 struct xfs_dinode *dip)
224 struct inode *inode = VFS_I(ip);
228 * Initialize the extent count early, as the per-format routines may
231 ip->i_df.if_format = dip->di_format;
232 ip->i_df.if_nextents = xfs_dfork_data_extents(dip);
234 switch (inode->i_mode & S_IFMT) {
240 inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
245 switch (ip->i_df.if_format) {
246 case XFS_DINODE_FMT_LOCAL:
247 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK,
248 be64_to_cpu(dip->di_size));
250 error = xfs_ifork_verify_local_data(ip);
252 case XFS_DINODE_FMT_EXTENTS:
253 return xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
254 case XFS_DINODE_FMT_BTREE:
255 return xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
257 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
258 dip, sizeof(*dip), __this_address);
259 return -EFSCORRUPTED;
263 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
264 sizeof(*dip), __this_address);
265 return -EFSCORRUPTED;
270 xfs_dfork_attr_shortform_size(
271 struct xfs_dinode *dip)
273 struct xfs_attr_shortform *atp =
274 (struct xfs_attr_shortform *)XFS_DFORK_APTR(dip);
276 return be16_to_cpu(atp->hdr.totsize);
281 enum xfs_dinode_fmt format,
282 xfs_extnum_t nextents)
284 struct xfs_ifork *ifp;
286 ifp = kmem_cache_zalloc(xfs_ifork_cache, GFP_NOFS | __GFP_NOFAIL);
287 ifp->if_format = format;
288 ifp->if_nextents = nextents;
293 xfs_iformat_attr_fork(
294 struct xfs_inode *ip,
295 struct xfs_dinode *dip)
297 xfs_extnum_t naextents = xfs_dfork_attr_extents(dip);
301 * Initialize the extent count early, as the per-format routines may
304 ip->i_afp = xfs_ifork_alloc(dip->di_aformat, naextents);
306 switch (ip->i_afp->if_format) {
307 case XFS_DINODE_FMT_LOCAL:
308 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK,
309 xfs_dfork_attr_shortform_size(dip));
311 error = xfs_ifork_verify_local_attr(ip);
313 case XFS_DINODE_FMT_EXTENTS:
314 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
316 case XFS_DINODE_FMT_BTREE:
317 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
320 xfs_inode_verifier_error(ip, error, __func__, dip,
321 sizeof(*dip), __this_address);
322 error = -EFSCORRUPTED;
327 kmem_cache_free(xfs_ifork_cache, ip->i_afp);
334 * Reallocate the space for if_broot based on the number of records
335 * being added or deleted as indicated in rec_diff. Move the records
336 * and pointers in if_broot to fit the new size. When shrinking this
337 * will eliminate holes between the records and pointers created by
338 * the caller. When growing this will create holes to be filled in
341 * The caller must not request to add more records than would fit in
342 * the on-disk inode root. If the if_broot is currently NULL, then
343 * if we are adding records, one will be allocated. The caller must also
344 * not request that the number of records go below zero, although
347 * ip -- the inode whose if_broot area is changing
348 * ext_diff -- the change in the number of records, positive or negative,
349 * requested for the if_broot array.
357 struct xfs_mount *mp = ip->i_mount;
359 struct xfs_ifork *ifp;
360 struct xfs_btree_block *new_broot;
367 * Handle the degenerate case quietly.
373 ifp = XFS_IFORK_PTR(ip, whichfork);
376 * If there wasn't any memory allocated before, just
377 * allocate it now and get out.
379 if (ifp->if_broot_bytes == 0) {
380 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
381 ifp->if_broot = kmem_alloc(new_size, KM_NOFS);
382 ifp->if_broot_bytes = (int)new_size;
387 * If there is already an existing if_broot, then we need
388 * to realloc() it and shift the pointers to their new
389 * location. The records don't change location because
390 * they are kept butted up against the btree block header.
392 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
393 new_max = cur_max + rec_diff;
394 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
395 ifp->if_broot = krealloc(ifp->if_broot, new_size,
396 GFP_NOFS | __GFP_NOFAIL);
397 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
398 ifp->if_broot_bytes);
399 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
401 ifp->if_broot_bytes = (int)new_size;
402 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
403 XFS_IFORK_SIZE(ip, whichfork));
404 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
409 * rec_diff is less than 0. In this case, we are shrinking the
410 * if_broot buffer. It must already exist. If we go to zero
411 * records, just get rid of the root and clear the status bit.
413 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
414 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
415 new_max = cur_max + rec_diff;
416 ASSERT(new_max >= 0);
418 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
422 new_broot = kmem_alloc(new_size, KM_NOFS);
424 * First copy over the btree block header.
426 memcpy(new_broot, ifp->if_broot,
427 XFS_BMBT_BLOCK_LEN(ip->i_mount));
433 * Only copy the records and pointers if there are any.
437 * First copy the records.
439 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
440 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
441 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
444 * Then copy the pointers.
446 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
447 ifp->if_broot_bytes);
448 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
450 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
452 kmem_free(ifp->if_broot);
453 ifp->if_broot = new_broot;
454 ifp->if_broot_bytes = (int)new_size;
456 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
457 XFS_IFORK_SIZE(ip, whichfork));
463 * This is called when the amount of space needed for if_data
464 * is increased or decreased. The change in size is indicated by
465 * the number of bytes that need to be added or deleted in the
466 * byte_diff parameter.
468 * If the amount of space needed has decreased below the size of the
469 * inline buffer, then switch to using the inline buffer. Otherwise,
470 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
473 * ip -- the inode whose if_data area is changing
474 * byte_diff -- the change in the number of bytes, positive or negative,
475 * requested for the if_data array.
479 struct xfs_inode *ip,
483 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
484 int64_t new_size = ifp->if_bytes + byte_diff;
486 ASSERT(new_size >= 0);
487 ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));
493 kmem_free(ifp->if_u1.if_data);
494 ifp->if_u1.if_data = NULL;
499 ifp->if_u1.if_data = krealloc(ifp->if_u1.if_data, new_size,
500 GFP_NOFS | __GFP_NOFAIL);
501 ifp->if_bytes = new_size;
506 struct xfs_ifork *ifp)
508 if (ifp->if_broot != NULL) {
509 kmem_free(ifp->if_broot);
510 ifp->if_broot = NULL;
513 switch (ifp->if_format) {
514 case XFS_DINODE_FMT_LOCAL:
515 kmem_free(ifp->if_u1.if_data);
516 ifp->if_u1.if_data = NULL;
518 case XFS_DINODE_FMT_EXTENTS:
519 case XFS_DINODE_FMT_BTREE:
521 xfs_iext_destroy(ifp);
527 * Convert in-core extents to on-disk form
529 * In the case of the data fork, the in-core and on-disk fork sizes can be
530 * different due to delayed allocation extents. We only copy on-disk extents
531 * here, so callers must always use the physical fork size to determine the
532 * size of the buffer passed to this routine. We will return the size actually
537 struct xfs_inode *ip,
538 struct xfs_bmbt_rec *dp,
541 int state = xfs_bmap_fork_to_state(whichfork);
542 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
543 struct xfs_iext_cursor icur;
544 struct xfs_bmbt_irec rec;
547 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
548 ASSERT(ifp->if_bytes > 0);
550 for_each_xfs_iext(ifp, &icur, &rec) {
551 if (isnullstartblock(rec.br_startblock))
553 ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
554 xfs_bmbt_disk_set_all(dp, &rec);
555 trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
556 copied += sizeof(struct xfs_bmbt_rec);
561 ASSERT(copied <= ifp->if_bytes);
566 * Each of the following cases stores data into the same region
567 * of the on-disk inode, so only one of them can be valid at
568 * any given time. While it is possible to have conflicting formats
569 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
570 * in EXTENTS format, this can only happen when the fork has
571 * changed formats after being modified but before being flushed.
572 * In these cases, the format always takes precedence, because the
573 * format indicates the current state of the fork.
577 struct xfs_inode *ip,
578 struct xfs_dinode *dip,
579 struct xfs_inode_log_item *iip,
583 struct xfs_ifork *ifp;
585 static const short brootflag[2] =
586 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
587 static const short dataflag[2] =
588 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
589 static const short extflag[2] =
590 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
594 ifp = XFS_IFORK_PTR(ip, whichfork);
596 * This can happen if we gave up in iformat in an error path,
597 * for the attribute fork.
600 ASSERT(whichfork == XFS_ATTR_FORK);
603 cp = XFS_DFORK_PTR(dip, whichfork);
605 switch (ifp->if_format) {
606 case XFS_DINODE_FMT_LOCAL:
607 if ((iip->ili_fields & dataflag[whichfork]) &&
608 (ifp->if_bytes > 0)) {
609 ASSERT(ifp->if_u1.if_data != NULL);
610 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
611 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
615 case XFS_DINODE_FMT_EXTENTS:
616 if ((iip->ili_fields & extflag[whichfork]) &&
617 (ifp->if_bytes > 0)) {
618 ASSERT(ifp->if_nextents > 0);
619 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
624 case XFS_DINODE_FMT_BTREE:
625 if ((iip->ili_fields & brootflag[whichfork]) &&
626 (ifp->if_broot_bytes > 0)) {
627 ASSERT(ifp->if_broot != NULL);
628 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
629 XFS_IFORK_SIZE(ip, whichfork));
630 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
631 (xfs_bmdr_block_t *)cp,
632 XFS_DFORK_SIZE(dip, mp, whichfork));
636 case XFS_DINODE_FMT_DEV:
637 if (iip->ili_fields & XFS_ILOG_DEV) {
638 ASSERT(whichfork == XFS_DATA_FORK);
639 xfs_dinode_put_rdev(dip,
640 linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
650 /* Convert bmap state flags to an inode fork. */
652 xfs_iext_state_to_fork(
653 struct xfs_inode *ip,
656 if (state & BMAP_COWFORK)
658 else if (state & BMAP_ATTRFORK)
664 * Initialize an inode's copy-on-write fork.
668 struct xfs_inode *ip)
673 ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_cache,
674 GFP_NOFS | __GFP_NOFAIL);
675 ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
678 /* Verify the inline contents of the data fork of an inode. */
680 xfs_ifork_verify_local_data(
681 struct xfs_inode *ip)
683 xfs_failaddr_t fa = NULL;
685 switch (VFS_I(ip)->i_mode & S_IFMT) {
687 fa = xfs_dir2_sf_verify(ip);
690 fa = xfs_symlink_shortform_verify(ip);
697 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
698 ip->i_df.if_u1.if_data, ip->i_df.if_bytes, fa);
699 return -EFSCORRUPTED;
705 /* Verify the inline contents of the attr fork of an inode. */
707 xfs_ifork_verify_local_attr(
708 struct xfs_inode *ip)
710 struct xfs_ifork *ifp = ip->i_afp;
716 fa = xfs_attr_shortform_verify(ip);
719 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
720 ifp ? ifp->if_u1.if_data : NULL,
721 ifp ? ifp->if_bytes : 0, fa);
722 return -EFSCORRUPTED;
729 xfs_iext_count_may_overflow(
730 struct xfs_inode *ip,
734 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
738 if (whichfork == XFS_COW_FORK)
741 max_exts = xfs_iext_max_nextents(xfs_inode_has_large_extent_counts(ip),
744 if (XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
747 nr_exts = ifp->if_nextents + nr_to_add;
748 if (nr_exts < ifp->if_nextents || nr_exts > max_exts)
755 * Upgrade this inode's extent counter fields to be able to handle a potential
756 * increase in the extent count by nr_to_add. Normally this is the same
757 * quantity that caused xfs_iext_count_may_overflow() to return -EFBIG.
760 xfs_iext_count_upgrade(
761 struct xfs_trans *tp,
762 struct xfs_inode *ip,
765 ASSERT(nr_to_add <= XFS_MAX_EXTCNT_UPGRADE_NR);
767 if (!xfs_has_large_extent_counts(ip->i_mount) ||
768 xfs_inode_has_large_extent_counts(ip) ||
769 XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
772 ip->i_diflags2 |= XFS_DIFLAG2_NREXT64;
773 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);