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 kmem_zone_t *xfs_ifork_zone;
38 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
39 int mem_size = size, real_size = 0;
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 real_size = roundup(mem_size, 4);
54 ifp->if_u1.if_data = kmem_alloc(real_size, KM_NOFS);
55 memcpy(ifp->if_u1.if_data, data, size);
57 ifp->if_u1.if_data[size] = '\0';
59 ifp->if_u1.if_data = NULL;
63 ifp->if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
64 ifp->if_flags |= XFS_IFINLINE;
68 * The file is in-lined in the on-disk inode.
78 * If the size is unreasonable, then something
79 * is wrong and we just bail out rather than crash in
80 * kmem_alloc() or memcpy() below.
82 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
84 "corrupt inode %Lu (bad size %d for local fork, size = %zd).",
85 (unsigned long long) ip->i_ino, size,
86 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
87 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
88 "xfs_iformat_local", dip, sizeof(*dip),
93 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
98 * The file consists of a set of extents all of which fit into the on-disk
103 struct xfs_inode *ip,
104 struct xfs_dinode *dip,
107 struct xfs_mount *mp = ip->i_mount;
108 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
109 int state = xfs_bmap_fork_to_state(whichfork);
110 int nex = XFS_DFORK_NEXTENTS(dip, whichfork);
111 int size = nex * sizeof(xfs_bmbt_rec_t);
112 struct xfs_iext_cursor icur;
113 struct xfs_bmbt_rec *dp;
114 struct xfs_bmbt_irec new;
118 * If the number of extents is unreasonable, then something is wrong and
119 * we just bail out rather than crash in kmem_alloc() or memcpy() below.
121 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
122 xfs_warn(ip->i_mount, "corrupt inode %Lu ((a)extents = %d).",
123 (unsigned long long) ip->i_ino, nex);
124 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
125 "xfs_iformat_extents(1)", dip, sizeof(*dip),
127 return -EFSCORRUPTED;
131 ifp->if_u1.if_root = NULL;
134 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
136 xfs_iext_first(ifp, &icur);
137 for (i = 0; i < nex; i++, dp++) {
140 xfs_bmbt_disk_get_all(dp, &new);
141 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
143 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
144 "xfs_iformat_extents(2)",
145 dp, sizeof(*dp), fa);
146 return -EFSCORRUPTED;
149 xfs_iext_insert(ip, &icur, &new, state);
150 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
151 xfs_iext_next(ifp, &icur);
154 ifp->if_flags |= XFS_IFEXTENTS;
159 * The file has too many extents to fit into
160 * the inode, so they are in B-tree format.
161 * Allocate a buffer for the root of the B-tree
162 * and copy the root into it. The i_extents
163 * field will remain NULL until all of the
164 * extents are read in (when they are needed).
172 struct xfs_mount *mp = ip->i_mount;
173 xfs_bmdr_block_t *dfp;
174 struct xfs_ifork *ifp;
180 ifp = XFS_IFORK_PTR(ip, whichfork);
181 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
182 size = XFS_BMAP_BROOT_SPACE(mp, dfp);
183 nrecs = be16_to_cpu(dfp->bb_numrecs);
184 level = be16_to_cpu(dfp->bb_level);
187 * blow out if -- fork has less extents than can fit in
188 * fork (fork shouldn't be a btree format), root btree
189 * block has more records than can fit into the fork,
190 * or the number of extents is greater than the number of
193 if (unlikely(ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork) ||
195 XFS_BMDR_SPACE_CALC(nrecs) >
196 XFS_DFORK_SIZE(dip, mp, whichfork) ||
197 ifp->if_nextents > ip->i_nblocks) ||
198 level == 0 || level > XFS_BM_MAXLEVELS(mp, whichfork)) {
199 xfs_warn(mp, "corrupt inode %Lu (btree).",
200 (unsigned long long) ip->i_ino);
201 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
202 "xfs_iformat_btree", dfp, size,
204 return -EFSCORRUPTED;
207 ifp->if_broot_bytes = size;
208 ifp->if_broot = kmem_alloc(size, KM_NOFS);
209 ASSERT(ifp->if_broot != NULL);
211 * Copy and convert from the on-disk structure
212 * to the in-memory structure.
214 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
215 ifp->if_broot, size);
216 ifp->if_flags &= ~XFS_IFEXTENTS;
217 ifp->if_flags |= XFS_IFBROOT;
220 ifp->if_u1.if_root = NULL;
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
237 ip->i_df.if_format = dip->di_format;
238 ip->i_df.if_nextents = be32_to_cpu(dip->di_nextents);
240 switch (inode->i_mode & S_IFMT) {
246 inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
251 switch (ip->i_df.if_format) {
252 case XFS_DINODE_FMT_LOCAL:
253 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK,
254 be64_to_cpu(dip->di_size));
256 error = xfs_ifork_verify_local_data(ip);
258 case XFS_DINODE_FMT_EXTENTS:
259 return xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
260 case XFS_DINODE_FMT_BTREE:
261 return xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
263 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
264 dip, sizeof(*dip), __this_address);
265 return -EFSCORRUPTED;
269 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
270 sizeof(*dip), __this_address);
271 return -EFSCORRUPTED;
276 xfs_dfork_attr_shortform_size(
277 struct xfs_dinode *dip)
279 struct xfs_attr_shortform *atp =
280 (struct xfs_attr_shortform *)XFS_DFORK_APTR(dip);
282 return be16_to_cpu(atp->hdr.totsize);
287 enum xfs_dinode_fmt format,
288 xfs_extnum_t nextents)
290 struct xfs_ifork *ifp;
292 ifp = kmem_cache_zalloc(xfs_ifork_zone, GFP_NOFS | __GFP_NOFAIL);
293 ifp->if_format = format;
294 ifp->if_nextents = nextents;
299 xfs_iformat_attr_fork(
300 struct xfs_inode *ip,
301 struct xfs_dinode *dip)
306 * Initialize the extent count early, as the per-format routines may
309 ip->i_afp = xfs_ifork_alloc(dip->di_aformat,
310 be16_to_cpu(dip->di_anextents));
312 switch (ip->i_afp->if_format) {
313 case XFS_DINODE_FMT_LOCAL:
314 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK,
315 xfs_dfork_attr_shortform_size(dip));
317 error = xfs_ifork_verify_local_attr(ip);
319 case XFS_DINODE_FMT_EXTENTS:
320 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
322 case XFS_DINODE_FMT_BTREE:
323 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
326 xfs_inode_verifier_error(ip, error, __func__, dip,
327 sizeof(*dip), __this_address);
328 error = -EFSCORRUPTED;
333 kmem_cache_free(xfs_ifork_zone, ip->i_afp);
340 * Reallocate the space for if_broot based on the number of records
341 * being added or deleted as indicated in rec_diff. Move the records
342 * and pointers in if_broot to fit the new size. When shrinking this
343 * will eliminate holes between the records and pointers created by
344 * the caller. When growing this will create holes to be filled in
347 * The caller must not request to add more records than would fit in
348 * the on-disk inode root. If the if_broot is currently NULL, then
349 * if we are adding records, one will be allocated. The caller must also
350 * not request that the number of records go below zero, although
353 * ip -- the inode whose if_broot area is changing
354 * ext_diff -- the change in the number of records, positive or negative,
355 * requested for the if_broot array.
363 struct xfs_mount *mp = ip->i_mount;
365 struct xfs_ifork *ifp;
366 struct xfs_btree_block *new_broot;
373 * Handle the degenerate case quietly.
379 ifp = XFS_IFORK_PTR(ip, whichfork);
382 * If there wasn't any memory allocated before, just
383 * allocate it now and get out.
385 if (ifp->if_broot_bytes == 0) {
386 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
387 ifp->if_broot = kmem_alloc(new_size, KM_NOFS);
388 ifp->if_broot_bytes = (int)new_size;
393 * If there is already an existing if_broot, then we need
394 * to realloc() it and shift the pointers to their new
395 * location. The records don't change location because
396 * they are kept butted up against the btree block header.
398 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
399 new_max = cur_max + rec_diff;
400 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
401 ifp->if_broot = krealloc(ifp->if_broot, new_size,
402 GFP_NOFS | __GFP_NOFAIL);
403 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
404 ifp->if_broot_bytes);
405 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
407 ifp->if_broot_bytes = (int)new_size;
408 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
409 XFS_IFORK_SIZE(ip, whichfork));
410 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
415 * rec_diff is less than 0. In this case, we are shrinking the
416 * if_broot buffer. It must already exist. If we go to zero
417 * records, just get rid of the root and clear the status bit.
419 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
420 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
421 new_max = cur_max + rec_diff;
422 ASSERT(new_max >= 0);
424 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
428 new_broot = kmem_alloc(new_size, KM_NOFS);
430 * First copy over the btree block header.
432 memcpy(new_broot, ifp->if_broot,
433 XFS_BMBT_BLOCK_LEN(ip->i_mount));
436 ifp->if_flags &= ~XFS_IFBROOT;
440 * Only copy the records and pointers if there are any.
444 * First copy the records.
446 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
447 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
448 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
451 * Then copy the pointers.
453 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
454 ifp->if_broot_bytes);
455 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
457 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
459 kmem_free(ifp->if_broot);
460 ifp->if_broot = new_broot;
461 ifp->if_broot_bytes = (int)new_size;
463 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
464 XFS_IFORK_SIZE(ip, whichfork));
470 * This is called when the amount of space needed for if_data
471 * is increased or decreased. The change in size is indicated by
472 * the number of bytes that need to be added or deleted in the
473 * byte_diff parameter.
475 * If the amount of space needed has decreased below the size of the
476 * inline buffer, then switch to using the inline buffer. Otherwise,
477 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
480 * ip -- the inode whose if_data area is changing
481 * byte_diff -- the change in the number of bytes, positive or negative,
482 * requested for the if_data array.
486 struct xfs_inode *ip,
490 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
491 int64_t new_size = ifp->if_bytes + byte_diff;
493 ASSERT(new_size >= 0);
494 ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));
500 kmem_free(ifp->if_u1.if_data);
501 ifp->if_u1.if_data = NULL;
507 * For inline data, the underlying buffer must be a multiple of 4 bytes
508 * in size so that it can be logged and stay on word boundaries.
509 * We enforce that here.
511 ifp->if_u1.if_data = krealloc(ifp->if_u1.if_data, roundup(new_size, 4),
512 GFP_NOFS | __GFP_NOFAIL);
513 ifp->if_bytes = new_size;
518 struct xfs_ifork *ifp)
520 if (ifp->if_broot != NULL) {
521 kmem_free(ifp->if_broot);
522 ifp->if_broot = NULL;
525 switch (ifp->if_format) {
526 case XFS_DINODE_FMT_LOCAL:
527 kmem_free(ifp->if_u1.if_data);
528 ifp->if_u1.if_data = NULL;
530 case XFS_DINODE_FMT_EXTENTS:
531 case XFS_DINODE_FMT_BTREE:
533 xfs_iext_destroy(ifp);
539 * Convert in-core extents to on-disk form
541 * In the case of the data fork, the in-core and on-disk fork sizes can be
542 * different due to delayed allocation extents. We only copy on-disk extents
543 * here, so callers must always use the physical fork size to determine the
544 * size of the buffer passed to this routine. We will return the size actually
549 struct xfs_inode *ip,
550 struct xfs_bmbt_rec *dp,
553 int state = xfs_bmap_fork_to_state(whichfork);
554 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
555 struct xfs_iext_cursor icur;
556 struct xfs_bmbt_irec rec;
559 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
560 ASSERT(ifp->if_bytes > 0);
562 for_each_xfs_iext(ifp, &icur, &rec) {
563 if (isnullstartblock(rec.br_startblock))
565 ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
566 xfs_bmbt_disk_set_all(dp, &rec);
567 trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
568 copied += sizeof(struct xfs_bmbt_rec);
573 ASSERT(copied <= ifp->if_bytes);
578 * Each of the following cases stores data into the same region
579 * of the on-disk inode, so only one of them can be valid at
580 * any given time. While it is possible to have conflicting formats
581 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
582 * in EXTENTS format, this can only happen when the fork has
583 * changed formats after being modified but before being flushed.
584 * In these cases, the format always takes precedence, because the
585 * format indicates the current state of the fork.
591 struct xfs_inode_log_item *iip,
595 struct xfs_ifork *ifp;
597 static const short brootflag[2] =
598 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
599 static const short dataflag[2] =
600 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
601 static const short extflag[2] =
602 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
606 ifp = XFS_IFORK_PTR(ip, whichfork);
608 * This can happen if we gave up in iformat in an error path,
609 * for the attribute fork.
612 ASSERT(whichfork == XFS_ATTR_FORK);
615 cp = XFS_DFORK_PTR(dip, whichfork);
617 switch (ifp->if_format) {
618 case XFS_DINODE_FMT_LOCAL:
619 if ((iip->ili_fields & dataflag[whichfork]) &&
620 (ifp->if_bytes > 0)) {
621 ASSERT(ifp->if_u1.if_data != NULL);
622 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
623 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
627 case XFS_DINODE_FMT_EXTENTS:
628 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
629 !(iip->ili_fields & extflag[whichfork]));
630 if ((iip->ili_fields & extflag[whichfork]) &&
631 (ifp->if_bytes > 0)) {
632 ASSERT(ifp->if_nextents > 0);
633 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
638 case XFS_DINODE_FMT_BTREE:
639 if ((iip->ili_fields & brootflag[whichfork]) &&
640 (ifp->if_broot_bytes > 0)) {
641 ASSERT(ifp->if_broot != NULL);
642 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
643 XFS_IFORK_SIZE(ip, whichfork));
644 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
645 (xfs_bmdr_block_t *)cp,
646 XFS_DFORK_SIZE(dip, mp, whichfork));
650 case XFS_DINODE_FMT_DEV:
651 if (iip->ili_fields & XFS_ILOG_DEV) {
652 ASSERT(whichfork == XFS_DATA_FORK);
653 xfs_dinode_put_rdev(dip,
654 linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
664 /* Convert bmap state flags to an inode fork. */
666 xfs_iext_state_to_fork(
667 struct xfs_inode *ip,
670 if (state & BMAP_COWFORK)
672 else if (state & BMAP_ATTRFORK)
678 * Initialize an inode's copy-on-write fork.
682 struct xfs_inode *ip)
687 ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_zone,
688 GFP_NOFS | __GFP_NOFAIL);
689 ip->i_cowfp->if_flags = XFS_IFEXTENTS;
690 ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
693 /* Verify the inline contents of the data fork of an inode. */
695 xfs_ifork_verify_local_data(
696 struct xfs_inode *ip)
698 xfs_failaddr_t fa = NULL;
700 switch (VFS_I(ip)->i_mode & S_IFMT) {
702 fa = xfs_dir2_sf_verify(ip);
705 fa = xfs_symlink_shortform_verify(ip);
712 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
713 ip->i_df.if_u1.if_data, ip->i_df.if_bytes, fa);
714 return -EFSCORRUPTED;
720 /* Verify the inline contents of the attr fork of an inode. */
722 xfs_ifork_verify_local_attr(
723 struct xfs_inode *ip)
725 struct xfs_ifork *ifp = ip->i_afp;
731 fa = xfs_attr_shortform_verify(ip);
734 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
735 ifp ? ifp->if_u1.if_data : NULL,
736 ifp ? ifp->if_bytes : 0, fa);
737 return -EFSCORRUPTED;
744 xfs_iext_count_may_overflow(
745 struct xfs_inode *ip,
749 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
753 if (whichfork == XFS_COW_FORK)
756 max_exts = (whichfork == XFS_ATTR_FORK) ? MAXAEXTNUM : MAXEXTNUM;
758 if (XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
761 nr_exts = ifp->if_nextents + nr_to_add;
762 if (nr_exts < ifp->if_nextents || nr_exts > max_exts)