Skip to content

fix(posix): move the tree size along with items moved on disk - #853

Open
NickWalters wants to merge 5 commits into
opencloud-eu:mainfrom
NickWalters:fix/posix-on-disk-move-treesize
Open

NickWalters wants to merge 5 commits into
opencloud-eu:mainfrom
NickWalters:fix/posix-on-disk-move-treesize

Conversation

@NickWalters

@NickWalters NickWalters commented Oct 6, 2026 •

Copy link
Copy Markdown

Fixes opencloud-eu/opencloud#3109, together with #849. Fixes #886.

Problem

On a posixfs storage with the watcher on, a file or folder moved to another folder can leave its size in the old folder. The new folder never gets it. The space total stays right, but folder sizes drift apart. In cs3org#3109 one folder shows 51 GB for 49 GB on disk, and another shows much less than it holds.

There are three ways to get there:

  1. A move on disk, for example mv a/x b/. The watcher assimilates the moved item, but updateFile only propagates a change of the item's own size. For a move that is 0.
  2. A move through the API whose request is cancelled after the rename. Tree.Move renames first. With a cancelled context, IDCache.DeleteByPath fails and Move returns before it sets the parent id or moves the size. The watcher then handles it like case 1. The log in Add missing condition for permission checks on spaces cs3org/reva#3109 shows context canceled errors during large moves in the web UI.
  3. A move on disk while nothing watches, for example while OpenCloud is stopped. The startup scan sets the tree sizes from disk, but the item keeps its old parent id.

A restart fixes the sizes on 7.2.1 and later. On 7.2.0 (reva v2.46.6) the startup scan skipped the whole storage root, which is probably why a restart didn't help the reporter.

How to reproduce

  1. On a posixfs space with the watcher on, create the folders a and b, and put a file in a.
  2. Run mv a/file b/ on disk.
  3. a keeps the size of the file, and b stays at 0.

Fix

  • updateFile treats a stored parent id that differs from the parent on disk as a move. Like Tree.Move, it moves the size of the item from the old parents to the new ones. It adds the current size to the new parents first, so a file that changed during the move is counted with its new size. Then it takes the stored size off the old parents.
  • Tree.Move writes the parent id and name right after the rename. Until the id cache is updated, newNode resolves to the old path, so the write addresses the item by path. A watcher assimilation that runs during the id cache update (slow for large folders) now sees a known item and doesn't move the size a second time. After the rename, Move uses context.WithoutCancel, like upload.go does, so a cancelled request can't leave the move half done.
  • A full WarmupIDCache walk stores the new parent id of an item that was moved while nothing watched, if the walk also covers the old parent. Otherwise the next assimilation of that item would move its size again.
  • 7fbd8a8: when a size change can't be propagated, the parents are marked dirty and recalculated from disk with the dirty-only warmup. For a move these are the new parent and the old parent. Before, blobsize and the parent id already had their new values, so the change or the move was lost until a full scan. This is the alternative to fix(posix): retry propagating a file's size when propagation fails #860 discussed below. If you prefer fix(posix): retry propagating a file's size when propagation fails #860, I'll drop this commit.
  • 38a42e6: Tree.Move reads the size it moves while it still holds the item's lock, and from the metadata backend instead of the node the request loaded. Before, a size change that propagated through a moved folder right after the unlock was counted twice in the new parent (posix: Tree.Move reads the moved folder's tree size outside its lock #886).

Tests

Eight new specs. All of them fail without this change and pass with it:

  • tree_test.go, through the watcher: a folder and a file moved on disk to another folder, and a file that changes size right after its move.
  • tree_non_watching_test.go: Tree.Move with a cancelled context, and a move on disk that a full walk picks up before the file is edited. The second one calls assimilate through a new export_test.go.
  • tree_test.go, for 7fbd8a8: a propagation that fails for an edited file, for a move into a folder, and for a move out of a folder. Each breaks a folder's tree size so that propagating to it fails, and checks that the folders get the right size without the file being touched again. They pass 30 of 30 runs.
  • tree_non_watching_test.go, for 38a42e6: a folder whose size changes after the request loaded it is moved with its current size.

I also ran API moves of a folder with 1500 files while the watcher was on. Before the Tree.Move change, the size moved twice in about one run of five. After it, 7 of 7 runs were correct. That check depends on timing, so it is not part of the PR.

go test -race ./pkg/storage/fs/posix/... passes locally, and golangci-lint is clean on the package.

Notes

@butonic

butonic commented Oct 8, 2026

Copy link
Copy Markdown
Member

@NickWalters I see that you created a lot of PRs related to POSIX metadata inconsistencies. Thx for the time to look into this. I'll set the labels. Can I get your opinion on #860 which tries to address aflakyness we see in CI runs. It conflicts with this PR and you might already have a stronger opinion which way to move forward.

Personally, I think the propagation inconsistencies at least are better addressed by the async propagation implementation but @aduffeck ran into issues with it as well.

In any case we should try to review these as they improve and fix bugs in our core usecase:

@NickWalters

NickWalters commented Oct 11, 2026 •

Copy link
Copy Markdown
Author

Thanks for the labels and the summary.

Short version: #860 can still count a file's size twice. I'd recalculate the folder from disk instead.

The problem with #860

Only the watcher updates propagatedsize. An API upload or a rescan changes the folder sizes without it, so it goes stale, and the next edit on disk adds a change that was already counted:

Step File Parent treesize propagatedsize
Created on disk 5 bytes 5 5
Uploaded through the API 12 bytes 12 5 (stale)
Edited on disk 13 bytes 20 (should be 13) 13

A rescan from disk ends the same way. Both specs below fail on #860 in every run, and spec 1 passes on main. Revision restore has the same gap, and Tree.Move moves blobsize instead of the counted size.

What I'd do instead

When a size change can't be propagated, mark the parent dirty and let the existing dirty-folder rescan recalculate it from disk. That needs no new attribute, and blobsize stays the only baseline. For a move, it marks both the new and the old parent.

That is commit e2cc6b9 in this PR, with three specs that break the propagation: an edited file, a move into a broken folder and a move out of one. If you prefer #860, I'll drop it. Neither fix recovers a failure above the direct parent: that waits for a full rescan.

If you'd rather keep the checkpoint, the upload, restore and rescan paths have to update it too, and Move has to use it the way Delete does.

Spec 1: API upload (tree_test.go, "Watching tree" / "of files")
It("keeps the tree size right after an API upload of a file assimilated from disk", func() {
	fileRef := &provider.Reference{ResourceId: env.SpaceRootRes, Path: subtree + "/api.txt"}
	dirRef := &provider.Reference{ResourceId: env.SpaceRootRes, Path: subtree}
	treeSize := func(g Gomega) uint64 {
		dir, err := env.Lookup.NodeFromResource(env.Ctx, dirRef)
		g.Expect(err).ToNot(HaveOccurred())
		size, err := dir.GetTreeSize(env.Ctx)
		g.Expect(err).ToNot(HaveOccurred())
		return size
	}
	realBS, err := posixbs.New(env.Root)
	Expect(err).ToNot(HaveOccurred())
	env.Blobstore.On("Upload", mock.Anything, mock.Anything, mock.Anything).Return(func(n *node.Node, src, ct string) error { return realBS.Upload(n, src, ct) })
	env.Permissions.On("AssemblePermissions", mock.Anything, mock.Anything, mock.Anything).Return(node.OwnerPermissions(), nil)

	Expect(os.WriteFile(root+"/api.txt", []byte("hello"), 0600)).To(Succeed())
	Eventually(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(5))) }).Should(Succeed())

	content := []byte("hello world!")
	ids, err := env.Fs.InitiateUpload(env.Ctx, fileRef, int64(len(content)), map[string]string{})
	Expect(err).ToNot(HaveOccurred())
	_, err = env.Fs.Upload(env.Ctx, storage.UploadRequest{
		Ref:    &provider.Reference{Path: "/" + ids["simple"]},
		Body:   io.NopCloser(bytes.NewReader(content)),
		Length: int64(len(content)),
	}, nil)
	Expect(err).ToNot(HaveOccurred())
	_, err = env.Lookup.NodeFromResource(env.Ctx, fileRef)
	Expect(err).ToNot(HaveOccurred())
	Eventually(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(12))) }).Should(Succeed())
	Consistently(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(12))) }, 3*time.Second).Should(Succeed())

	Expect(os.WriteFile(root+"/api.txt", []byte("hello world!!"), 0600)).To(Succeed())
	Eventually(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(13))) }).Should(Succeed())
	Consistently(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(13))) }, 2*time.Second).Should(Succeed())
})

Needs bytes, io, github.com/stretchr/testify/mock, .../pkg/storage, posixbs ".../fs/posix/blobstore" and .../decomposedfs/node in the imports.

Spec 2: heal from disk (same place)

Updated: it now waits for treesize 13 before Consistently. The first version raced the propagation, because assimilation writes blobsize first.

It("keeps the tree size right after a heal from disk", func() {
	fileRef := &provider.Reference{ResourceId: env.SpaceRootRes, Path: subtree + "/grow.txt"}
	dirRef := &provider.Reference{ResourceId: env.SpaceRootRes, Path: subtree}
	treeSize := func(g Gomega) uint64 {
		dir, err := env.Lookup.NodeFromResource(env.Ctx, dirRef)
		g.Expect(err).ToNot(HaveOccurred())
		size, err := dir.GetTreeSize(env.Ctx)
		g.Expect(err).ToNot(HaveOccurred())
		return size
	}
	Expect(os.WriteFile(root+"/grow.txt", []byte("hello"), 0600)).To(Succeed())
	Eventually(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(5))) }).Should(Succeed())

	dir, err := env.Lookup.NodeFromResource(env.Ctx, dirRef)
	Expect(err).ToNot(HaveOccurred())
	Expect(env.Lookup.MetadataBackend().Set(env.Ctx, dir, prefixes.TreesizeAttr, []byte("not-a-number"))).To(Succeed())
	Expect(os.WriteFile(root+"/grow.txt", []byte("hello world!"), 0600)).To(Succeed())
	Eventually(func(g Gomega) {
		n, err := env.Lookup.NodeFromResource(env.Ctx, fileRef)
		g.Expect(err).ToNot(HaveOccurred())
		p, err := n.Xattr(env.Ctx, prefixes.PropagatedSizeAttr)
		g.Expect(err).ToNot(HaveOccurred())
		g.Expect(n.Blobsize).To(Equal(int64(12)))
		g.Expect(string(p)).To(Equal("5"))
	}).Should(Succeed())

	// heal from disk the way startup does
	Expect(env.Tree.WarmupIDCache(root, true, false)).To(Succeed())
	Eventually(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(12))) }).Should(Succeed())

	Expect(os.WriteFile(root+"/grow.txt", []byte("hello world!!"), 0600)).To(Succeed())
	Eventually(func(g Gomega) {
		n, err := env.Lookup.NodeFromResource(env.Ctx, fileRef)
		g.Expect(err).ToNot(HaveOccurred())
		g.Expect(n.Blobsize).To(Equal(int64(13)))
	}).Should(Succeed())
	Eventually(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(13))) }).Should(Succeed())
	Consistently(func(g Gomega) { g.Expect(treeSize(g)).To(Equal(uint64(13))) }, 3*time.Second).Should(Succeed())
})

@NickWalters

NickWalters commented Oct 14, 2026 •

Copy link
Copy Markdown
Author

I pushed 8c9cb1e with fixes from another review:

  • Tree.Move holds the item's lock from the rename until the new parent id is written, so the watcher can't count the move a second time.
  • With a cold id cache, a full walk could visit the new parent before the old one and keep the old parent id. It now retries those items after the walk.
  • Recalculations after a failed propagation run one at a time, so two of them can't apply the same diff.
  • Two specs now wait for the common ancestor before Consistently.
  • 523ec16: if deleting the old id cache entry fails, Move now logs it and still moves the sizes. Before, it returned early, and the watcher didn't move them either because the item already had its new parent id.

When the watcher assimilated an item that was moved to another directory
on disk, updateFile only propagated the change of the item's own size.
The old parents kept the item's size and the new parents never got it,
so folder sizes and quota drifted apart while the space total stayed
right.

updateFile now treats a stored parent id that differs from the parent on
disk as a move. Like Tree.Move, it moves the item's size from the old
parents to the new ones. It adds the current size to the new parents
first, so a file that changed during the move is counted with its new
size.

A full scan, like the one at startup, calculates the tree sizes from
disk but kept the stale parent id of an item moved while nothing
watched. The next assimilation of that item would then move its size a
second time, so the scan now stores the new parent id when it also
covers the old parent.

Tree.Move renamed first and set the parent id last. A watcher
assimilation in between would now also move the size, and a cancelled
request left the move half done. So Tree.Move sets the parent id and
name right after the rename and finishes its bookkeeping without the
request's cancellation.
When the watcher could not propagate the size diff of an item, the diff
was lost. blobsize and the parent id already had their new values, so a
later assimilation of the item saw no diff and no move, and the parents
stayed wrong until a full scan. The same happened to the old parents of a
moved item when taking its size off them failed.

Such a failure now marks the affected parents dirty and recalculates
their tree sizes from disk with the dirty-only warmup. For a moved item
these are the new parent and, if it still holds the size, the old parent.
blobsize stays the only baseline, so the API paths need no change.
Tree.Move renamed an item and only then wrote its new parent id, without
holding the item's lock. A watcher that assimilated the item in between
took it for a move on disk and moved its size a second time. Move now
holds the item's lock from the rename until the parent id is written.

A full walk only updated the parent id of an item moved on disk when the
id cache already knew the old parent. With a cold cache the walk can visit
the new parent first, so the item kept its old parent id and its next
assimilation moved the size again. The walk now tries those items again
once it is done.

Two recalculations after failed propagations could walk the same folder
at the same time, both read the old tree size and propagate the same
diff. They now run one at a time.

Two specs checked a common ancestor with Consistently before they waited
for its size to settle. They wait for it first now.
…e deleted

Move writes the new parent id right after the rename. If deleting the old
id cache entry then failed, Move returned before it moved the tree sizes,
and the watcher didn't move them either, because the item already had its
new parent id. Log the error and go on, like a failed CacheID right after
it.
@NickWalters
NickWalters force-pushed the fix/posix-on-disk-move-treesize branch from 523ec16 to c73c996 Compare October 19, 2026 02:27
@NickWalters

NickWalters commented Oct 19, 2026 •

Copy link
Copy Markdown
Author

Rebased onto main now that #849, #808 and #809 are in. The only changes are the conflict fixes. go test -race ./pkg/storage/fs/posix/..., golangci-lint and GOOS=darwin/GOOS=freebsd vet pass.

From my side this is ready to merge. If you go with #860 instead, tell me and I'll drop the recalculation commit (now 7fbd8a8).

Tree.Move read the tree size of a moved folder after it released the
folder's lock, and through the node the request loaded before the move.
A size change that propagated through the folder after the unlock went
to the new parent and was also part of the size that Move carried, so
the new parent counted it twice. A size read through an older node
missed changes since the request loaded it.

Move now reads the tree size, or the blobsize of a file, from the
metadata backend while it still holds the lock.

Fixes opencloud-eu#886
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

2 participants