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"
28 #include "xfs_health.h"
29 #include "xfs_symlink_remote.h"
31 struct kmem_cache *xfs_ifork_cache;
40 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
45 * If we are using the local fork to store a symlink body we need to
46 * zero-terminate it so that we can pass it back to the VFS directly.
47 * Overallocate the in-memory fork by one for that and add a zero
48 * to terminate it below.
50 zero_terminate = S_ISLNK(VFS_I(ip)->i_mode);
55 char *new_data = kmalloc(mem_size,
56 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
58 memcpy(new_data, data, size);
60 new_data[size] = '\0';
62 ifp->if_data = new_data;
71 * The file is in-lined in the on-disk inode.
76 struct xfs_dinode *dip,
81 * If the size is unreasonable, then something
82 * is wrong and we just bail out rather than crash in
83 * kmalloc() or memcpy() below.
85 if (unlikely(size > XFS_DFORK_SIZE(dip, ip->i_mount, whichfork))) {
87 "corrupt inode %llu (bad size %d for local fork, size = %zd).",
88 (unsigned long long) ip->i_ino, size,
89 XFS_DFORK_SIZE(dip, ip->i_mount, whichfork));
90 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
91 "xfs_iformat_local", dip, sizeof(*dip),
93 xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
97 xfs_init_local_fork(ip, whichfork, XFS_DFORK_PTR(dip, whichfork), size);
102 * The file consists of a set of extents all of which fit into the on-disk
107 struct xfs_inode *ip,
108 struct xfs_dinode *dip,
111 struct xfs_mount *mp = ip->i_mount;
112 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
113 int state = xfs_bmap_fork_to_state(whichfork);
114 xfs_extnum_t nex = xfs_dfork_nextents(dip, whichfork);
115 int size = nex * sizeof(xfs_bmbt_rec_t);
116 struct xfs_iext_cursor icur;
117 struct xfs_bmbt_rec *dp;
118 struct xfs_bmbt_irec new;
122 * If the number of extents is unreasonable, then something is wrong and
123 * we just bail out rather than crash in kmalloc() or memcpy() below.
125 if (unlikely(size < 0 || size > XFS_DFORK_SIZE(dip, mp, whichfork))) {
126 xfs_warn(ip->i_mount, "corrupt inode %llu ((a)extents = %llu).",
128 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
129 "xfs_iformat_extents(1)", dip, sizeof(*dip),
131 xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
132 return -EFSCORRUPTED;
139 dp = (xfs_bmbt_rec_t *) XFS_DFORK_PTR(dip, whichfork);
141 xfs_iext_first(ifp, &icur);
142 for (i = 0; i < nex; i++, dp++) {
145 xfs_bmbt_disk_get_all(dp, &new);
146 fa = xfs_bmap_validate_extent(ip, whichfork, &new);
148 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
149 "xfs_iformat_extents(2)",
150 dp, sizeof(*dp), fa);
151 xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
152 return xfs_bmap_complain_bad_rec(ip, whichfork,
156 xfs_iext_insert(ip, &icur, &new, state);
157 trace_xfs_read_extent(ip, &icur, state, _THIS_IP_);
158 xfs_iext_next(ifp, &icur);
165 * The file has too many extents to fit into
166 * the inode, so they are in B-tree format.
167 * Allocate a buffer for the root of the B-tree
168 * and copy the root into it. The i_extents
169 * field will remain NULL until all of the
170 * extents are read in (when they are needed).
174 struct xfs_inode *ip,
175 struct xfs_dinode *dip,
178 struct xfs_mount *mp = ip->i_mount;
179 xfs_bmdr_block_t *dfp;
180 struct xfs_ifork *ifp;
186 ifp = xfs_ifork_ptr(ip, whichfork);
187 dfp = (xfs_bmdr_block_t *)XFS_DFORK_PTR(dip, whichfork);
188 size = xfs_bmap_broot_space(mp, dfp);
189 nrecs = be16_to_cpu(dfp->bb_numrecs);
190 level = be16_to_cpu(dfp->bb_level);
193 * blow out if -- fork has less extents than can fit in
194 * fork (fork shouldn't be a btree format), root btree
195 * block has more records than can fit into the fork,
196 * or the number of extents is greater than the number of
199 if (unlikely(ifp->if_nextents <= XFS_IFORK_MAXEXT(ip, whichfork) ||
201 xfs_bmdr_space_calc(nrecs) >
202 XFS_DFORK_SIZE(dip, mp, whichfork) ||
203 ifp->if_nextents > ip->i_nblocks) ||
204 level == 0 || level > XFS_BM_MAXLEVELS(mp, whichfork)) {
205 xfs_warn(mp, "corrupt inode %llu (btree).",
206 (unsigned long long) ip->i_ino);
207 xfs_inode_verifier_error(ip, -EFSCORRUPTED,
208 "xfs_iformat_btree", dfp, size,
210 xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
211 return -EFSCORRUPTED;
214 ifp->if_broot_bytes = size;
215 ifp->if_broot = kmalloc(size,
216 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
217 ASSERT(ifp->if_broot != NULL);
219 * Copy and convert from the on-disk structure
220 * to the in-memory structure.
222 xfs_bmdr_to_bmbt(ip, dfp, XFS_DFORK_SIZE(dip, ip->i_mount, whichfork),
223 ifp->if_broot, size);
232 xfs_iformat_data_fork(
233 struct xfs_inode *ip,
234 struct xfs_dinode *dip)
236 struct inode *inode = VFS_I(ip);
240 * Initialize the extent count early, as the per-format routines may
241 * depend on it. Use release semantics to set needextents /after/ we
242 * set the format. This ensures that we can use acquire semantics on
243 * needextents in xfs_need_iread_extents() and be guaranteed to see a
244 * valid format value after that load.
246 ip->i_df.if_format = dip->di_format;
247 ip->i_df.if_nextents = xfs_dfork_data_extents(dip);
248 smp_store_release(&ip->i_df.if_needextents,
249 ip->i_df.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0);
251 switch (inode->i_mode & S_IFMT) {
257 inode->i_rdev = xfs_to_linux_dev_t(xfs_dinode_get_rdev(dip));
262 switch (ip->i_df.if_format) {
263 case XFS_DINODE_FMT_LOCAL:
264 error = xfs_iformat_local(ip, dip, XFS_DATA_FORK,
265 be64_to_cpu(dip->di_size));
267 error = xfs_ifork_verify_local_data(ip);
269 case XFS_DINODE_FMT_EXTENTS:
270 return xfs_iformat_extents(ip, dip, XFS_DATA_FORK);
271 case XFS_DINODE_FMT_BTREE:
272 return xfs_iformat_btree(ip, dip, XFS_DATA_FORK);
274 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__,
275 dip, sizeof(*dip), __this_address);
276 xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
277 return -EFSCORRUPTED;
281 xfs_inode_verifier_error(ip, -EFSCORRUPTED, __func__, dip,
282 sizeof(*dip), __this_address);
283 xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
284 return -EFSCORRUPTED;
289 xfs_dfork_attr_shortform_size(
290 struct xfs_dinode *dip)
292 struct xfs_attr_sf_hdr *sf = XFS_DFORK_APTR(dip);
294 return be16_to_cpu(sf->totsize);
299 struct xfs_inode *ip,
300 enum xfs_dinode_fmt format,
301 xfs_extnum_t nextents)
304 * Initialize the extent count early, as the per-format routines may
305 * depend on it. Use release semantics to set needextents /after/ we
306 * set the format. This ensures that we can use acquire semantics on
307 * needextents in xfs_need_iread_extents() and be guaranteed to see a
308 * valid format value after that load.
310 ip->i_af.if_format = format;
311 ip->i_af.if_nextents = nextents;
312 smp_store_release(&ip->i_af.if_needextents,
313 ip->i_af.if_format == XFS_DINODE_FMT_BTREE ? 1 : 0);
318 struct xfs_inode *ip)
320 xfs_idestroy_fork(&ip->i_af);
321 memset(&ip->i_af, 0, sizeof(struct xfs_ifork));
322 ip->i_af.if_format = XFS_DINODE_FMT_EXTENTS;
326 xfs_iformat_attr_fork(
327 struct xfs_inode *ip,
328 struct xfs_dinode *dip)
330 xfs_extnum_t naextents = xfs_dfork_attr_extents(dip);
334 * Initialize the extent count early, as the per-format routines may
337 xfs_ifork_init_attr(ip, dip->di_aformat, naextents);
339 switch (ip->i_af.if_format) {
340 case XFS_DINODE_FMT_LOCAL:
341 error = xfs_iformat_local(ip, dip, XFS_ATTR_FORK,
342 xfs_dfork_attr_shortform_size(dip));
344 error = xfs_ifork_verify_local_attr(ip);
346 case XFS_DINODE_FMT_EXTENTS:
347 error = xfs_iformat_extents(ip, dip, XFS_ATTR_FORK);
349 case XFS_DINODE_FMT_BTREE:
350 error = xfs_iformat_btree(ip, dip, XFS_ATTR_FORK);
353 xfs_inode_verifier_error(ip, error, __func__, dip,
354 sizeof(*dip), __this_address);
355 xfs_inode_mark_sick(ip, XFS_SICK_INO_CORE);
356 error = -EFSCORRUPTED;
361 xfs_ifork_zap_attr(ip);
366 * Reallocate the space for if_broot based on the number of records
367 * being added or deleted as indicated in rec_diff. Move the records
368 * and pointers in if_broot to fit the new size. When shrinking this
369 * will eliminate holes between the records and pointers created by
370 * the caller. When growing this will create holes to be filled in
373 * The caller must not request to add more records than would fit in
374 * the on-disk inode root. If the if_broot is currently NULL, then
375 * if we are adding records, one will be allocated. The caller must also
376 * not request that the number of records go below zero, although
379 * ip -- the inode whose if_broot area is changing
380 * ext_diff -- the change in the number of records, positive or negative,
381 * requested for the if_broot array.
389 struct xfs_mount *mp = ip->i_mount;
391 struct xfs_ifork *ifp;
392 struct xfs_btree_block *new_broot;
399 * Handle the degenerate case quietly.
405 ifp = xfs_ifork_ptr(ip, whichfork);
408 * If there wasn't any memory allocated before, just
409 * allocate it now and get out.
411 if (ifp->if_broot_bytes == 0) {
412 new_size = xfs_bmap_broot_space_calc(mp, rec_diff);
413 ifp->if_broot = kmalloc(new_size,
414 GFP_KERNEL | __GFP_NOFAIL);
415 ifp->if_broot_bytes = (int)new_size;
420 * If there is already an existing if_broot, then we need
421 * to realloc() it and shift the pointers to their new
422 * location. The records don't change location because
423 * they are kept butted up against the btree block header.
425 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, false);
426 new_max = cur_max + rec_diff;
427 new_size = xfs_bmap_broot_space_calc(mp, new_max);
428 ifp->if_broot = krealloc(ifp->if_broot, new_size,
429 GFP_KERNEL | __GFP_NOFAIL);
430 op = (char *)xfs_bmap_broot_ptr_addr(mp, ifp->if_broot, 1,
431 ifp->if_broot_bytes);
432 np = (char *)xfs_bmap_broot_ptr_addr(mp, ifp->if_broot, 1,
434 ifp->if_broot_bytes = (int)new_size;
435 ASSERT(xfs_bmap_bmdr_space(ifp->if_broot) <=
436 xfs_inode_fork_size(ip, whichfork));
437 memmove(np, op, cur_max * (uint)sizeof(xfs_fsblock_t));
442 * rec_diff is less than 0. In this case, we are shrinking the
443 * if_broot buffer. It must already exist. If we go to zero
444 * records, just get rid of the root and clear the status bit.
446 ASSERT((ifp->if_broot != NULL) && (ifp->if_broot_bytes > 0));
447 cur_max = xfs_bmbt_maxrecs(mp, ifp->if_broot_bytes, false);
448 new_max = cur_max + rec_diff;
449 ASSERT(new_max >= 0);
451 new_size = xfs_bmap_broot_space_calc(mp, new_max);
455 new_broot = kmalloc(new_size, GFP_KERNEL | __GFP_NOFAIL);
457 * First copy over the btree block header.
459 memcpy(new_broot, ifp->if_broot,
460 xfs_bmbt_block_len(ip->i_mount));
466 * Only copy the keys and pointers if there are any.
470 * First copy the keys.
472 op = (char *)xfs_bmbt_key_addr(mp, ifp->if_broot, 1);
473 np = (char *)xfs_bmbt_key_addr(mp, new_broot, 1);
474 memcpy(np, op, new_max * (uint)sizeof(xfs_bmbt_key_t));
477 * Then copy the pointers.
479 op = (char *)xfs_bmap_broot_ptr_addr(mp, ifp->if_broot, 1,
480 ifp->if_broot_bytes);
481 np = (char *)xfs_bmap_broot_ptr_addr(mp, new_broot, 1,
483 memcpy(np, op, new_max * (uint)sizeof(xfs_fsblock_t));
485 kfree(ifp->if_broot);
486 ifp->if_broot = new_broot;
487 ifp->if_broot_bytes = (int)new_size;
489 ASSERT(xfs_bmap_bmdr_space(ifp->if_broot) <=
490 xfs_inode_fork_size(ip, whichfork));
496 * This is called when the amount of space needed for if_data
497 * is increased or decreased. The change in size is indicated by
498 * the number of bytes that need to be added or deleted in the
499 * byte_diff parameter.
501 * If the amount of space needed has decreased below the size of the
502 * inline buffer, then switch to using the inline buffer. Otherwise,
503 * use krealloc() or kmalloc() to adjust the size of the buffer
506 * ip -- the inode whose if_data area is changing
507 * byte_diff -- the change in the number of bytes, positive or negative,
508 * requested for the if_data array.
512 struct xfs_inode *ip,
516 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
517 int64_t new_size = ifp->if_bytes + byte_diff;
519 ASSERT(new_size >= 0);
520 ASSERT(new_size <= xfs_inode_fork_size(ip, whichfork));
523 ifp->if_data = krealloc(ifp->if_data, new_size,
524 GFP_KERNEL | __GFP_NOFAIL);
527 ifp->if_bytes = new_size;
533 /* Free all memory and reset a fork back to its initial state. */
536 struct xfs_ifork *ifp)
538 if (ifp->if_broot != NULL) {
539 kfree(ifp->if_broot);
540 ifp->if_broot = NULL;
543 switch (ifp->if_format) {
544 case XFS_DINODE_FMT_LOCAL:
548 case XFS_DINODE_FMT_EXTENTS:
549 case XFS_DINODE_FMT_BTREE:
551 xfs_iext_destroy(ifp);
557 * Convert in-core extents to on-disk form
559 * In the case of the data fork, the in-core and on-disk fork sizes can be
560 * different due to delayed allocation extents. We only copy on-disk extents
561 * here, so callers must always use the physical fork size to determine the
562 * size of the buffer passed to this routine. We will return the size actually
567 struct xfs_inode *ip,
568 struct xfs_bmbt_rec *dp,
571 int state = xfs_bmap_fork_to_state(whichfork);
572 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
573 struct xfs_iext_cursor icur;
574 struct xfs_bmbt_irec rec;
577 xfs_assert_ilocked(ip, XFS_ILOCK_EXCL | XFS_ILOCK_SHARED);
578 ASSERT(ifp->if_bytes > 0);
580 for_each_xfs_iext(ifp, &icur, &rec) {
581 if (isnullstartblock(rec.br_startblock))
583 ASSERT(xfs_bmap_validate_extent(ip, whichfork, &rec) == NULL);
584 xfs_bmbt_disk_set_all(dp, &rec);
585 trace_xfs_write_extent(ip, &icur, state, _RET_IP_);
586 copied += sizeof(struct xfs_bmbt_rec);
591 ASSERT(copied <= ifp->if_bytes);
596 * Each of the following cases stores data into the same region
597 * of the on-disk inode, so only one of them can be valid at
598 * any given time. While it is possible to have conflicting formats
599 * and log flags, e.g. having XFS_ILOG_?DATA set when the fork is
600 * in EXTENTS format, this can only happen when the fork has
601 * changed formats after being modified but before being flushed.
602 * In these cases, the format always takes precedence, because the
603 * format indicates the current state of the fork.
607 struct xfs_inode *ip,
608 struct xfs_dinode *dip,
609 struct xfs_inode_log_item *iip,
613 struct xfs_ifork *ifp;
615 static const short brootflag[2] =
616 { XFS_ILOG_DBROOT, XFS_ILOG_ABROOT };
617 static const short dataflag[2] =
618 { XFS_ILOG_DDATA, XFS_ILOG_ADATA };
619 static const short extflag[2] =
620 { XFS_ILOG_DEXT, XFS_ILOG_AEXT };
624 ifp = xfs_ifork_ptr(ip, whichfork);
626 * This can happen if we gave up in iformat in an error path,
627 * for the attribute fork.
630 ASSERT(whichfork == XFS_ATTR_FORK);
633 cp = XFS_DFORK_PTR(dip, whichfork);
635 switch (ifp->if_format) {
636 case XFS_DINODE_FMT_LOCAL:
637 if ((iip->ili_fields & dataflag[whichfork]) &&
638 (ifp->if_bytes > 0)) {
639 ASSERT(ifp->if_data != NULL);
640 ASSERT(ifp->if_bytes <= xfs_inode_fork_size(ip, whichfork));
641 memcpy(cp, ifp->if_data, ifp->if_bytes);
645 case XFS_DINODE_FMT_EXTENTS:
646 if ((iip->ili_fields & extflag[whichfork]) &&
647 (ifp->if_bytes > 0)) {
648 ASSERT(ifp->if_nextents > 0);
649 (void)xfs_iextents_copy(ip, (xfs_bmbt_rec_t *)cp,
654 case XFS_DINODE_FMT_BTREE:
655 if ((iip->ili_fields & brootflag[whichfork]) &&
656 (ifp->if_broot_bytes > 0)) {
657 ASSERT(ifp->if_broot != NULL);
658 ASSERT(xfs_bmap_bmdr_space(ifp->if_broot) <=
659 xfs_inode_fork_size(ip, whichfork));
660 xfs_bmbt_to_bmdr(mp, ifp->if_broot, ifp->if_broot_bytes,
661 (xfs_bmdr_block_t *)cp,
662 XFS_DFORK_SIZE(dip, mp, whichfork));
666 case XFS_DINODE_FMT_DEV:
667 if (iip->ili_fields & XFS_ILOG_DEV) {
668 ASSERT(whichfork == XFS_DATA_FORK);
669 xfs_dinode_put_rdev(dip,
670 linux_to_xfs_dev_t(VFS_I(ip)->i_rdev));
680 /* Convert bmap state flags to an inode fork. */
682 xfs_iext_state_to_fork(
683 struct xfs_inode *ip,
686 if (state & BMAP_COWFORK)
688 else if (state & BMAP_ATTRFORK)
694 * Initialize an inode's copy-on-write fork.
698 struct xfs_inode *ip)
703 ip->i_cowfp = kmem_cache_zalloc(xfs_ifork_cache,
704 GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
705 ip->i_cowfp->if_format = XFS_DINODE_FMT_EXTENTS;
708 /* Verify the inline contents of the data fork of an inode. */
710 xfs_ifork_verify_local_data(
711 struct xfs_inode *ip)
713 xfs_failaddr_t fa = NULL;
715 switch (VFS_I(ip)->i_mode & S_IFMT) {
717 struct xfs_mount *mp = ip->i_mount;
718 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
719 struct xfs_dir2_sf_hdr *sfp = ifp->if_data;
721 fa = xfs_dir2_sf_verify(mp, sfp, ifp->if_bytes);
725 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, XFS_DATA_FORK);
727 fa = xfs_symlink_shortform_verify(ifp->if_data, ifp->if_bytes);
735 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "data fork",
736 ip->i_df.if_data, ip->i_df.if_bytes, fa);
737 return -EFSCORRUPTED;
743 /* Verify the inline contents of the attr fork of an inode. */
745 xfs_ifork_verify_local_attr(
746 struct xfs_inode *ip)
748 struct xfs_ifork *ifp = &ip->i_af;
751 if (!xfs_inode_has_attr_fork(ip)) {
754 struct xfs_ifork *ifp = &ip->i_af;
756 ASSERT(ifp->if_format == XFS_DINODE_FMT_LOCAL);
757 fa = xfs_attr_shortform_verify(ifp->if_data, ifp->if_bytes);
760 xfs_inode_verifier_error(ip, -EFSCORRUPTED, "attr fork",
761 ifp->if_data, ifp->if_bytes, fa);
762 return -EFSCORRUPTED;
769 * Check if the inode fork supports adding nr_to_add more extents.
771 * If it doesn't but we can upgrade it to large extent counters, do the upgrade.
772 * If we can't upgrade or are already using big counters but still can't fit the
773 * additional extents, return -EFBIG.
776 xfs_iext_count_extend(
777 struct xfs_trans *tp,
778 struct xfs_inode *ip,
782 struct xfs_mount *mp = ip->i_mount;
784 xfs_inode_has_large_extent_counts(ip);
785 struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
788 ASSERT(nr_to_add <= XFS_MAX_EXTCNT_UPGRADE_NR);
790 if (whichfork == XFS_COW_FORK)
793 /* no point in upgrading if if_nextents overflows */
794 nr_exts = ifp->if_nextents + nr_to_add;
795 if (nr_exts < ifp->if_nextents)
798 if (XFS_TEST_ERROR(false, mp, XFS_ERRTAG_REDUCE_MAX_IEXTENTS) &&
802 if (nr_exts > xfs_iext_max_nextents(has_large, whichfork)) {
803 if (has_large || !xfs_has_large_extent_counts(mp))
805 ip->i_diflags2 |= XFS_DIFLAG2_NREXT64;
806 xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
811 /* Decide if a file mapping is on the realtime device or not. */
813 xfs_ifork_is_realtime(
814 struct xfs_inode *ip,
817 return XFS_IS_REALTIME_INODE(ip) && whichfork != XFS_ATTR_FORK;