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 = kmem_alloc(mem_size, KM_NOFS);
55 memcpy(new_data, data, size);
57 new_data[size] = '\0';
59 ifp->if_data = new_data;
68 * The file is in-lined in the on-disk inode.
73 struct xfs_dinode *dip,
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 %llu (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 xfs_extnum_t 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 %llu ((a)extents = %llu).",
124 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
125 "xfs_iformat_extents(1)", dip, sizeof(*dip),
127 return -EFSCORRUPTED;
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 xfs_bmap_complain_bad_rec(ip, whichfork,
150 xfs_iext_insert(ip, &icur, &new, state);
151 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
152 xfs_iext_next(ifp, &icur);
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).
168 struct xfs_inode *ip,
169 struct xfs_dinode *dip,
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 %llu (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);
224 xfs_iformat_data_fork(
225 struct xfs_inode *ip,
226 struct xfs_dinode *dip)
228 struct inode *inode = VFS_I(ip);
232 * Initialize the extent count early, as the per-format routines may
233 * depend on it. Use release semantics to set needextents /after/ we
234 * set the format. This ensures that we can use acquire semantics on
235 * needextents in xfs_need_iread_extents() and be guaranteed to see a
236 * valid format value after that load.
238 ip->i_df.if_format = dip->di_format;
239 ip->i_df.if_nextents = xfs_dfork_data_extents(dip);
240 smp_store_release(&ip->i_df.if_needextents,
241 ip->i_df.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0);
243 switch (inode->i_mode & S_IFMT) {
249 inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
254 switch (ip->i_df.if_format) {
255 case XFS_DINODE_FMT_LOCAL:
256 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK,
257 be64_to_cpu(dip->di_size));
259 error = xfs_ifork_verify_local_data(ip);
261 case XFS_DINODE_FMT_EXTENTS:
262 return xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
263 case XFS_DINODE_FMT_BTREE:
264 return xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
266 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
267 dip, sizeof(*dip), __this_address);
268 return -EFSCORRUPTED;
272 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
273 sizeof(*dip), __this_address);
274 return -EFSCORRUPTED;
279 xfs_dfork_attr_shortform_size(
280 struct xfs_dinode *dip)
282 struct xfs_attr_sf_hdr *sf = XFS_DFORK_APTR(dip);
284 return be16_to_cpu(sf->totsize);
289 struct xfs_inode *ip,
290 enum xfs_dinode_fmt format,
291 xfs_extnum_t nextents)
294 * Initialize the extent count early, as the per-format routines may
295 * depend on it. Use release semantics to set needextents /after/ we
296 * set the format. This ensures that we can use acquire semantics on
297 * needextents in xfs_need_iread_extents() and be guaranteed to see a
298 * valid format value after that load.
300 ip->i_af.if_format = format;
301 ip->i_af.if_nextents = nextents;
302 smp_store_release(&ip->i_af.if_needextents,
303 ip->i_af.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0);
308 struct xfs_inode *ip)
310 xfs_idestroy_fork(&ip->i_af);
311 memset(&ip->i_af, 0, sizeof(struct xfs_ifork));
312 ip->i_af.if_format = XFS_DINODE_FMT_EXTENTS;
316 xfs_iformat_attr_fork(
317 struct xfs_inode *ip,
318 struct xfs_dinode *dip)
320 xfs_extnum_t naextents = xfs_dfork_attr_extents(dip);
324 * Initialize the extent count early, as the per-format routines may
327 xfs_ifork_init_attr(ip, dip->di_aformat, naextents);
329 switch (ip->i_af.if_format) {
330 case XFS_DINODE_FMT_LOCAL:
331 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK,
332 xfs_dfork_attr_shortform_size(dip));
334 error = xfs_ifork_verify_local_attr(ip);
336 case XFS_DINODE_FMT_EXTENTS:
337 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
339 case XFS_DINODE_FMT_BTREE:
340 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
343 xfs_inode_verifier_error(ip, error, __func__, dip,
344 sizeof(*dip), __this_address);
345 error = -EFSCORRUPTED;
350 xfs_ifork_zap_attr(ip);
355 * Reallocate the space for if_broot based on the number of records
356 * being added or deleted as indicated in rec_diff. Move the records
357 * and pointers in if_broot to fit the new size. When shrinking this
358 * will eliminate holes between the records and pointers created by
359 * the caller. When growing this will create holes to be filled in
362 * The caller must not request to add more records than would fit in
363 * the on-disk inode root. If the if_broot is currently NULL, then
364 * if we are adding records, one will be allocated. The caller must also
365 * not request that the number of records go below zero, although
368 * ip -- the inode whose if_broot area is changing
369 * ext_diff -- the change in the number of records, positive or negative,
370 * requested for the if_broot array.
378 struct xfs_mount *mp = ip->i_mount;
380 struct xfs_ifork *ifp;
381 struct xfs_btree_block *new_broot;
388 * Handle the degenerate case quietly.
394 ifp = xfs_ifork_ptr(ip, whichfork);
397 * If there wasn't any memory allocated before, just
398 * allocate it now and get out.
400 if (ifp->if_broot_bytes == 0) {
401 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, rec_diff);
402 ifp->if_broot = kmem_alloc(new_size, KM_NOFS);
403 ifp->if_broot_bytes = (int)new_size;
408 * If there is already an existing if_broot, then we need
409 * to realloc() it and shift the pointers to their new
410 * location. The records don't change location because
411 * they are kept butted up against the btree block header.
413 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
414 new_max = cur_max + rec_diff;
415 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
416 ifp->if_broot = krealloc(ifp->if_broot, new_size,
417 GFP_NOFS | __GFP_NOFAIL);
418 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
419 ifp->if_broot_bytes);
420 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
422 ifp->if_broot_bytes = (int)new_size;
423 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
424 xfs_inode_fork_size(ip, whichfork));
425 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
430 * rec_diff is less than 0. In this case, we are shrinking the
431 * if_broot buffer. It must already exist. If we go to zero
432 * records, just get rid of the root and clear the status bit.
434 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
435 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, 0);
436 new_max = cur_max + rec_diff;
437 ASSERT(new_max >= 0);
439 new_size = XFS_BMAP_BROOT_SPACE_CALC(mp, new_max);
443 new_broot = kmem_alloc(new_size, KM_NOFS);
445 * First copy over the btree block header.
447 memcpy(new_broot, ifp->if_broot,
448 XFS_BMBT_BLOCK_LEN(ip->i_mount));
454 * Only copy the records and pointers if there are any.
458 * First copy the records.
460 op = (char *)XFS_BMBT_REC_ADDR(mp, ifp->if_broot, 1);
461 np = (char *)XFS_BMBT_REC_ADDR(mp, new_broot, 1);
462 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_rec_t));
465 * Then copy the pointers.
467 op = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, ifp->if_broot, 1,
468 ifp->if_broot_bytes);
469 np = (char *)XFS_BMAP_BROOT_PTR_ADDR(mp, new_broot, 1,
471 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
473 kmem_free(ifp->if_broot);
474 ifp->if_broot = new_broot;
475 ifp->if_broot_bytes = (int)new_size;
477 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
478 xfs_inode_fork_size(ip, whichfork));
484 * This is called when the amount of space needed for if_data
485 * is increased or decreased. The change in size is indicated by
486 * the number of bytes that need to be added or deleted in the
487 * byte_diff parameter.
489 * If the amount of space needed has decreased below the size of the
490 * inline buffer, then switch to using the inline buffer. Otherwise,
491 * use kmem_realloc() or kmem_alloc() to adjust the size of the buffer
494 * ip -- the inode whose if_data area is changing
495 * byte_diff -- the change in the number of bytes, positive or negative,
496 * requested for the if_data array.
500 struct xfs_inode *ip,
504 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
505 int64_t new_size = ifp->if_bytes + byte_diff;
507 ASSERT(new_size >= 0);
508 ASSERT(new_size <= xfs_inode_fork_size(ip, whichfork));
511 ifp->if_data = krealloc(ifp->if_data, new_size,
512 GFP_NOFS | __GFP_NOFAIL);
515 ifp->if_bytes = new_size;
521 /* Free all memory and reset a fork back to its initial state. */
524 struct xfs_ifork *ifp)
526 if (ifp->if_broot != NULL) {
527 kmem_free(ifp->if_broot);
528 ifp->if_broot = NULL;
531 switch (ifp->if_format) {
532 case XFS_DINODE_FMT_LOCAL:
533 kmem_free(ifp->if_data);
536 case XFS_DINODE_FMT_EXTENTS:
537 case XFS_DINODE_FMT_BTREE:
539 xfs_iext_destroy(ifp);
545 * Convert in-core extents to on-disk form
547 * In the case of the data fork, the in-core and on-disk fork sizes can be
548 * different due to delayed allocation extents. We only copy on-disk extents
549 * here, so callers must always use the physical fork size to determine the
550 * size of the buffer passed to this routine. We will return the size actually
555 struct xfs_inode *ip,
556 struct xfs_bmbt_rec *dp,
559 int state = xfs_bmap_fork_to_state(whichfork);
560 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
561 struct xfs_iext_cursor icur;
562 struct xfs_bmbt_irec rec;
565 ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED));
566 ASSERT(ifp->if_bytes > 0);
568 for_each_xfs_iext(ifp, &icur, &rec) {
569 if (isnullstartblock(rec.br_startblock))
571 ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
572 xfs_bmbt_disk_set_all(dp, &rec);
573 trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
574 copied += sizeof(struct xfs_bmbt_rec);
579 ASSERT(copied <= ifp->if_bytes);
584 * Each of the following cases stores data into the same region
585 * of the on-disk inode, so only one of them can be valid at
586 * any given time. While it is possible to have conflicting formats
587 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
588 * in EXTENTS format, this can only happen when the fork has
589 * changed formats after being modified but before being flushed.
590 * In these cases, the format always takes precedence, because the
591 * format indicates the current state of the fork.
595 struct xfs_inode *ip,
596 struct xfs_dinode *dip,
597 struct xfs_inode_log_item *iip,
601 struct xfs_ifork *ifp;
603 static const short brootflag[2] =
604 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
605 static const short dataflag[2] =
606 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
607 static const short extflag[2] =
608 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
612 ifp = xfs_ifork_ptr(ip, whichfork);
614 * This can happen if we gave up in iformat in an error path,
615 * for the attribute fork.
618 ASSERT(whichfork == XFS_ATTR_FORK);
621 cp = XFS_DFORK_PTR(dip, whichfork);
623 switch (ifp->if_format) {
624 case XFS_DINODE_FMT_LOCAL:
625 if ((iip->ili_fields & dataflag[whichfork]) &&
626 (ifp->if_bytes > 0)) {
627 ASSERT(ifp->if_data != NULL);
628 ASSERT(ifp->if_bytes <= xfs_inode_fork_size(ip, whichfork));
629 memcpy(cp, ifp->if_data, ifp->if_bytes);
633 case XFS_DINODE_FMT_EXTENTS:
634 if ((iip->ili_fields & extflag[whichfork]) &&
635 (ifp->if_bytes > 0)) {
636 ASSERT(ifp->if_nextents > 0);
637 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
642 case XFS_DINODE_FMT_BTREE:
643 if ((iip->ili_fields & brootflag[whichfork]) &&
644 (ifp->if_broot_bytes > 0)) {
645 ASSERT(ifp->if_broot != NULL);
646 ASSERT(XFS_BMAP_BMDR_SPACE(ifp->if_broot) <=
647 xfs_inode_fork_size(ip, whichfork));
648 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
649 (xfs_bmdr_block_t *)cp,
650 XFS_DFORK_SIZE(dip, mp, whichfork));
654 case XFS_DINODE_FMT_DEV:
655 if (iip->ili_fields & XFS_ILOG_DEV) {
656 ASSERT(whichfork == XFS_DATA_FORK);
657 xfs_dinode_put_rdev(dip,
658 linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
668 /* Convert bmap state flags to an inode fork. */
670 xfs_iext_state_to_fork(
671 struct xfs_inode *ip,
674 if (state & BMAP_COWFORK)
676 else if (state & BMAP_ATTRFORK)
682 * Initialize an inode's copy-on-write fork.
686 struct xfs_inode *ip)
691 ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_cache,
692 GFP_NOFS | __GFP_NOFAIL);
693 ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
696 /* Verify the inline contents of the data fork of an inode. */
698 xfs_ifork_verify_local_data(
699 struct xfs_inode *ip)
701 xfs_failaddr_t fa = NULL;
703 switch (VFS_I(ip)->i_mode & S_IFMT) {
705 struct xfs_mount *mp = ip->i_mount;
706 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
707 struct xfs_dir2_sf_hdr *sfp = ifp->if_data;
709 fa = xfs_dir2_sf_verify(mp, sfp, ifp->if_bytes);
713 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
715 fa = xfs_symlink_shortform_verify(ifp->if_data, ifp->if_bytes);
723 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
724 ip->i_df.if_data, ip->i_df.if_bytes, fa);
725 return -EFSCORRUPTED;
731 /* Verify the inline contents of the attr fork of an inode. */
733 xfs_ifork_verify_local_attr(
734 struct xfs_inode *ip)
736 struct xfs_ifork *ifp = &ip->i_af;
739 if (!xfs_inode_has_attr_fork(ip)) {
742 struct xfs_ifork *ifp = &ip->i_af;
744 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
745 fa = xfs_attr_shortform_verify(ifp->if_data, ifp->if_bytes);
748 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
749 ifp->if_data, ifp->if_bytes, fa);
750 return -EFSCORRUPTED;
757 xfs_iext_count_may_overflow(
758 struct xfs_inode *ip,
762 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
766 if (whichfork == XFS_COW_FORK)
769 max_exts = xfs_iext_max_nextents(xfs_inode_has_large_extent_counts(ip),
772 if (XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
775 nr_exts = ifp->if_nextents + nr_to_add;
776 if (nr_exts < ifp->if_nextents || nr_exts > max_exts)
783 * Upgrade this inode's extent counter fields to be able to handle a potential
784 * increase in the extent count by nr_to_add. Normally this is the same
785 * quantity that caused xfs_iext_count_may_overflow() to return -EFBIG.
788 xfs_iext_count_upgrade(
789 struct xfs_trans *tp,
790 struct xfs_inode *ip,
793 ASSERT(nr_to_add <= XFS_MAX_EXTCNT_UPGRADE_NR);
795 if (!xfs_has_large_extent_counts(ip->i_mount) ||
796 xfs_inode_has_large_extent_counts(ip) ||
797 XFS_TEST_ERROR(false, ip->i_mount, XFS_ERRTAG_REDUCE_MAX_IEXTENTS))
800 ip->i_diflags2 |= XFS_DIFLAG2_NREXT64;
801 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);