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_d.di_nblocks) ||
198 level == 0 || level > XFS_BTREE_MAXLEVELS) {
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);
286 xfs_iformat_attr_fork(
287 struct xfs_inode *ip,
288 struct xfs_dinode *dip)
293 * Initialize the extent count early, as the per-format routines may
296 ip->i_afp = kmem_cache_zalloc(xfs_ifork_zone, GFP_NOFS | __GFP_NOFAIL);
297 ip->i_afp->if_format = dip->di_aformat;
298 if (unlikely(ip->i_afp->if_format == 0)) /* pre IRIX 6.2 file system */
299 ip->i_afp->if_format = XFS_DINODE_FMT_EXTENTS;
300 ip->i_afp->if_nextents = be16_to_cpu(dip->di_anextents);
302 switch (ip->i_afp->if_format) {
303 case XFS_DINODE_FMT_LOCAL:
304 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK,
305 xfs_dfork_attr_shortform_size(dip));
307 error = xfs_ifork_verify_local_attr(ip);
309 case XFS_DINODE_FMT_EXTENTS:
310 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
312 case XFS_DINODE_FMT_BTREE:
313 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
316 xfs_inode_verifier_error(ip, error, __func__, dip,
317 sizeof(*dip), __this_address);
318 error = -EFSCORRUPTED;
323 kmem_cache_free(xfs_ifork_zone, ip->i_afp);
330 * Reallocate the space for if_broot based on the number of records
331 * being added or deleted as indicated in rec_diff. Move the records
332 * and pointers in if_broot to fit the new size. When shrinking this
333 * will eliminate holes between the records and pointers created by
334 * the caller. When growing this will create holes to be filled in
337 * The caller must not request to add more records than would fit in
338 * the on-disk inode root. If the if_broot is currently NULL, then
339 * if we are adding records, one will be allocated. The caller must also
340 * not request that the number of records go below zero, although
343 * ip -- the inode whose if_broot area is changing
344 * ext_diff -- the change in the number of records, positive or negative,
345 * requested for the if_broot array.
353 struct xfs_mount *mp = ip->i_mount;
355 struct xfs_ifork *ifp;
356 struct xfs_btree_block *new_broot;
363 * Handle the degenerate case quietly.
369 ifp = XFS_IFORK_PTR(ip, whichfork);
372 * If there wasn't any memory allocated before, just
373 * allocate it now and get out.
375 if (ifp->if_broot_bytes == 0) {
376 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
377 ifp->if_broot = kmem_alloc(new_size, KM_NOFS);
378 ifp->if_broot_bytes = (int)new_size;
383 * If there is already an existing if_broot, then we need
384 * to realloc() it and shift the pointers to their new
385 * location. The records don't change location because
386 * they are kept butted up against the btree block header.
388 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
389 new_max = cur_max + rec_diff;
390 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
391 ifp->if_broot = krealloc(ifp->if_broot, new_size,
392 GFP_NOFS | __GFP_NOFAIL);
393 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
394 ifp->if_broot_bytes);
395 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
397 ifp->if_broot_bytes = (int)new_size;
398 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
399 XFS_IFORK_SIZE(ip, whichfork));
400 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
405 * rec_diff is less than 0. In this case, we are shrinking the
406 * if_broot buffer. It must already exist. If we go to zero
407 * records, just get rid of the root and clear the status bit.
409 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
410 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
411 new_max = cur_max + rec_diff;
412 ASSERT(new_max >= 0);
414 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
418 new_broot = kmem_alloc(new_size, KM_NOFS);
420 * First copy over the btree block header.
422 memcpy(new_broot, ifp->if_broot,
423 XFS_BMBT_BLOCK_LEN(ip->i_mount));
426 ifp->if_flags &= ~XFS_IFBROOT;
430 * Only copy the records and pointers if there are any.
434 * First copy the records.
436 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
437 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
438 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
441 * Then copy the pointers.
443 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
444 ifp->if_broot_bytes);
445 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
447 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
449 kmem_free(ifp->if_broot);
450 ifp->if_broot = new_broot;
451 ifp->if_broot_bytes = (int)new_size;
453 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
454 XFS_IFORK_SIZE(ip, whichfork));
460 * This is called when the amount of space needed for if_data
461 * is increased or decreased. The change in size is indicated by
462 * the number of bytes that need to be added or deleted in the
463 * byte_diff parameter.
465 * If the amount of space needed has decreased below the size of the
466 * inline buffer, then switch to using the inline buffer. Otherwise,
467 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
470 * ip -- the inode whose if_data area is changing
471 * byte_diff -- the change in the number of bytes, positive or negative,
472 * requested for the if_data array.
476 struct xfs_inode *ip,
480 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
481 int64_t new_size = ifp->if_bytes + byte_diff;
483 ASSERT(new_size >= 0);
484 ASSERT(new_size <= XFS_IFORK_SIZE(ip, whichfork));
490 kmem_free(ifp->if_u1.if_data);
491 ifp->if_u1.if_data = NULL;
497 * For inline data, the underlying buffer must be a multiple of 4 bytes
498 * in size so that it can be logged and stay on word boundaries.
499 * We enforce that here.
501 ifp->if_u1.if_data = krealloc(ifp->if_u1.if_data, roundup(new_size, 4),
502 GFP_NOFS | __GFP_NOFAIL);
503 ifp->if_bytes = new_size;
508 struct xfs_ifork *ifp)
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 array so just
517 * look for an inline data array. If we're not local then we may or may
518 * not have an extents list, so check and free it up if we do.
520 if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
521 kmem_free(ifp->if_u1.if_data);
522 ifp->if_u1.if_data = NULL;
523 } else if (ifp->if_flags & XFS_IFEXTENTS) {
525 xfs_iext_destroy(ifp);
530 * Convert in-core extents to on-disk form
532 * In the case of the data fork, the in-core and on-disk fork sizes can be
533 * different due to delayed allocation extents. We only copy on-disk extents
534 * here, so callers must always use the physical fork size to determine the
535 * size of the buffer passed to this routine. We will return the size actually
540 struct xfs_inode *ip,
541 struct xfs_bmbt_rec *dp,
544 int state = xfs_bmap_fork_to_state(whichfork);
545 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
546 struct xfs_iext_cursor icur;
547 struct xfs_bmbt_irec rec;
550 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
551 ASSERT(ifp->if_bytes > 0);
553 for_each_xfs_iext(ifp, &icur, &rec) {
554 if (isnullstartblock(rec.br_startblock))
556 ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
557 xfs_bmbt_disk_set_all(dp, &rec);
558 trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
559 copied += sizeof(struct xfs_bmbt_rec);
564 ASSERT(copied <= ifp->if_bytes);
569 * Each of the following cases stores data into the same region
570 * of the on-disk inode, so only one of them can be valid at
571 * any given time. While it is possible to have conflicting formats
572 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
573 * in EXTENTS format, this can only happen when the fork has
574 * changed formats after being modified but before being flushed.
575 * In these cases, the format always takes precedence, because the
576 * format indicates the current state of the fork.
582 struct xfs_inode_log_item *iip,
586 struct xfs_ifork *ifp;
588 static const short brootflag[2] =
589 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
590 static const short dataflag[2] =
591 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
592 static const short extflag[2] =
593 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
597 ifp = XFS_IFORK_PTR(ip, whichfork);
599 * This can happen if we gave up in iformat in an error path,
600 * for the attribute fork.
603 ASSERT(whichfork == XFS_ATTR_FORK);
606 cp = XFS_DFORK_PTR(dip, whichfork);
608 switch (ifp->if_format) {
609 case XFS_DINODE_FMT_LOCAL:
610 if ((iip->ili_fields & dataflag[whichfork]) &&
611 (ifp->if_bytes > 0)) {
612 ASSERT(ifp->if_u1.if_data != NULL);
613 ASSERT(ifp->if_bytes <= XFS_IFORK_SIZE(ip, whichfork));
614 memcpy(cp, ifp->if_u1.if_data, ifp->if_bytes);
618 case XFS_DINODE_FMT_EXTENTS:
619 ASSERT((ifp->if_flags & XFS_IFEXTENTS) ||
620 !(iip->ili_fields & extflag[whichfork]));
621 if ((iip->ili_fields & extflag[whichfork]) &&
622 (ifp->if_bytes > 0)) {
623 ASSERT(ifp->if_nextents > 0);
624 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
629 case XFS_DINODE_FMT_BTREE:
630 if ((iip->ili_fields & brootflag[whichfork]) &&
631 (ifp->if_broot_bytes > 0)) {
632 ASSERT(ifp->if_broot != NULL);
633 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
634 XFS_IFORK_SIZE(ip, whichfork));
635 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
636 (xfs_bmdr_block_t *)cp,
637 XFS_DFORK_SIZE(dip, mp, whichfork));
641 case XFS_DINODE_FMT_DEV:
642 if (iip->ili_fields & XFS_ILOG_DEV) {
643 ASSERT(whichfork == XFS_DATA_FORK);
644 xfs_dinode_put_rdev(dip,
645 linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
655 /* Convert bmap state flags to an inode fork. */
657 xfs_iext_state_to_fork(
658 struct xfs_inode *ip,
661 if (state & BMAP_COWFORK)
663 else if (state & BMAP_ATTRFORK)
669 * Initialize an inode's copy-on-write fork.
673 struct xfs_inode *ip)
678 ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_zone,
679 GFP_NOFS | __GFP_NOFAIL);
680 ip->i_cowfp->if_flags = XFS_IFEXTENTS;
681 ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
684 /* Verify the inline contents of the data fork of an inode. */
686 xfs_ifork_verify_local_data(
687 struct xfs_inode *ip)
689 xfs_failaddr_t fa = NULL;
691 switch (VFS_I(ip)->i_mode & S_IFMT) {
693 fa = xfs_dir2_sf_verify(ip);
696 fa = xfs_symlink_shortform_verify(ip);
703 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
704 ip->i_df.if_u1.if_data, ip->i_df.if_bytes, fa);
705 return -EFSCORRUPTED;
711 /* Verify the inline contents of the attr fork of an inode. */
713 xfs_ifork_verify_local_attr(
714 struct xfs_inode *ip)
716 struct xfs_ifork *ifp = ip->i_afp;
722 fa = xfs_attr_shortform_verify(ip);
725 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
726 ifp ? ifp->if_u1.if_data : NULL,
727 ifp ? ifp->if_bytes : 0, fa);
728 return -EFSCORRUPTED;
735 xfs_iext_count_may_overflow(
736 struct xfs_inode *ip,
740 struct xfs_ifork *ifp = XFS_IFORK_PTR(ip, whichfork);
744 if (whichfork == XFS_COW_FORK)
747 max_exts = (whichfork == XFS_ATTR_FORK) ? MAXAEXTNUM : MAXEXTNUM;
749 if (XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
752 nr_exts = ifp->if_nextents + nr_to_add;
753 if (nr_exts < ifp->if_nextents || nr_exts > max_exts)