Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow zdb -r to understand object ids as well #16307

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

allanjude
Copy link
Contributor

Sponsored-by: Klara, Inc.
Sponsored-By: Wasabi Technology, Inc.

Motivation and Context

Previously I created: zdb -r pool/dataset path/to/file /tmp/recover.dat

But it turns out, if there is a problem with a directory, you might want to recover a file by its inode number instead

Description

If the argument is a number, thread it as the object-id instead of a filename

How Has This Been Tested?

Manually

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Performance enhancement (non-breaking change which improves efficiency)
  • Code cleanup (non-breaking change which makes code smaller or more readable)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Library ABI change (libzfs, libzfs_core, libnvpair, libuutil and libzfsbootenv)
  • Documentation (a change to man pages or other documentation)

Checklist:

Sponsored-by: Klara, Inc.
Sponsored-By: Wasabi Technology, Inc.
Signed-off-by: Allan Jude <[email protected]>
Comment on lines +9375 to +9376
if (zdb_numeric(argv[1])) {
object = strtoull(argv[1], NULL, 0);
Copy link
Member

@robn robn Jul 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a couple of problems here. zdb_numeric() and strtoull(base=0) permit different things, for example, zdb_numeric allows abc, but strtoull() does not, and will return 0.

The other more obvious problem is that this prevents a file name that zdb_numeric() returns true for, eg, there's no way to actually dump a file called abc.

Probably it needs a switch or magic value to say "interpret as number, not name". Maybe it's -N to match the "dataset is an objset id" case? Or maybe it can be a leading / on the filename, since that can't appear relative to the dataset? This seems to work reasonably well:

diff --git cmd/zdb/zdb.c cmd/zdb/zdb.c
index 3b8b67bce..9018eea58 100644
--- cmd/zdb/zdb.c
+++ cmd/zdb/zdb.c
@@ -9372,8 +9372,12 @@ main(int argc, char **argv)
 		if (argc != 3)
 			usage();
 		dump_opt['v'] = verbose;
-		if (zdb_numeric(argv[1])) {
-			object = strtoull(argv[1], NULL, 0);
+		if (argv[1][0] == '/') {
+			errno = 0;
+			object = strtoull(&(argv[1])[1], NULL, 0);
+			error = errno;
+			if (error == 0 && object == 0)
+				error = EINVAL;
 		} else {
 			error = dump_path(argv[0], argv[1], &object);
 		}

Absent that, maybe trying a number if dump_path() returns ENOENT? It has the reverse problem in that you can't dump by object number if you have a file named that, so maybe it's not useful.

@behlendorf behlendorf added the Status: Revision Needed Changes are required for the PR to be accepted label Aug 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Revision Needed Changes are required for the PR to be accepted
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants