All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Misono, Tomohiro" <misono.tomohiro@jp.fujitsu.com>
To: Omar Sandoval <osandov@osandov.com>, <linux-btrfs@vger.kernel.org>
Cc: <kernel-team@fb.com>
Subject: Re: [PATCH v2 07/27] libbtrfsutil: add btrfs_util_subvolume_path()
Date: Fri, 23 Feb 2018 15:27:52 +0900	[thread overview]
Message-ID: <0452f2f6-02c5-7cb8-6a20-e1a65de096db@jp.fujitsu.com> (raw)
In-Reply-To: <af89178dc6f35fe6a807aec6fecc0611ae99b3fb.1518720598.git.osandov@fb.com>

On 2018/02/16 4:04, Omar Sandoval wrote:
> From: Omar Sandoval <osandov@fb.com>

> +PUBLIC enum btrfs_util_error btrfs_util_subvolume_path_fd(int fd, uint64_t id,
> +							  char **path_ret)
> +{
> +	char *path, *p;
> +	size_t capacity = 4096;
> +
> +	path = malloc(capacity);
> +	if (!path)
> +		return BTRFS_UTIL_ERROR_NO_MEMORY;
> +	p = path + capacity - 1;
> +	p[0] = '\0';
> +
> +	if (id == 0) {
> +		enum btrfs_util_error err;
> +
> +		err = btrfs_util_is_subvolume_fd(fd);
> +		if (err)
> +			return err;

'path' should be freed here and below.

> +
> +		err = btrfs_util_subvolume_id_fd(fd, &id);
> +		if (err)
> +			return err;
> +	}
> +
> +	while (id != BTRFS_FS_TREE_OBJECTID) {
> +		struct btrfs_ioctl_search_args search = {
> +			.key = {
> +				.tree_id = BTRFS_ROOT_TREE_OBJECTID,
> +				.min_objectid = id,
> +				.max_objectid = id,
> +				.min_type = BTRFS_ROOT_BACKREF_KEY,
> +				.max_type = BTRFS_ROOT_BACKREF_KEY,
> +				.min_offset = 0,
> +				.max_offset = UINT64_MAX,
> +				.min_transid = 0,
> +				.max_transid = UINT64_MAX,
> +				.nr_items = 1,
> +			},
> +		};
> +		struct btrfs_ioctl_ino_lookup_args lookup;
> +		const struct btrfs_ioctl_search_header *header;
> +		const struct btrfs_root_ref *ref;
> +		const char *name;
> +		uint16_t name_len;
> +		size_t lookup_len;
> +		size_t total_len;
> +		int ret;
> +
> +		ret = ioctl(fd, BTRFS_IOC_TREE_SEARCH, &search);
> +		if (ret == -1) {
> +			free(path);
> +			return BTRFS_UTIL_ERROR_SEARCH_FAILED;
> +		}
> +
> +		if (search.key.nr_items == 0) {
> +			free(path);
> +			errno = ENOENT;
> +			return BTRFS_UTIL_ERROR_SUBVOLUME_NOT_FOUND;
> +		}
> +
> +		header = (struct btrfs_ioctl_search_header *)search.buf;
> +		ref = (struct btrfs_root_ref *)(header + 1);
> +		name = (char *)(ref + 1);
> +		name_len = le16_to_cpu(ref->name_len);
> +
> +		id = header->offset;
> +
> +		lookup.treeid = id;
> +		lookup.objectid = le64_to_cpu(ref->dirid);
> +		ret = ioctl(fd, BTRFS_IOC_INO_LOOKUP, &lookup);
> +		if (ret == -1) {
> +			free(path);
> +			return BTRFS_UTIL_ERROR_SEARCH_FAILED;
> +		}
> +		lookup_len = strlen(lookup.name);
> +
> +		total_len = name_len + lookup_len + (id != BTRFS_FS_TREE_OBJECTID);
> +		if (p - total_len < path) {
> +			char *new_path, *new_p;
> +			size_t new_capacity = capacity * 2;
> +
> +			new_path = malloc(new_capacity);
> +			if (!new_path) {
> +				free(path);
> +				return BTRFS_UTIL_ERROR_NO_MEMORY;
> +			}
> +			new_p = new_path + new_capacity - (path + capacity - p);
> +			memcpy(new_p, p, path + capacity - p);
> +			free(path);
> +			path = new_path;
> +			p = new_p;
> +			capacity = new_capacity;
> +		}
> +		p -= name_len;
> +		memcpy(p, name, name_len);
> +		p -= lookup_len;
> +		memcpy(p, lookup.name, lookup_len);
> +		if (id != BTRFS_FS_TREE_OBJECTID)
> +			*--p = '/';
> +	}
> +
> +	if (p != path)
> +		memmove(path, p, path + capacity - p);
> +
> +	*path_ret = path;
> +
> +	return BTRFS_UTIL_OK;
> +}
> +
>  static enum btrfs_util_error openat_parent_and_name(int dirfd, const char *path,
>  						    char *name, size_t name_len,
>  						    int *fd)
> 


  reply	other threads:[~2018-02-23  6:28 UTC|newest]

Thread overview: 64+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-02-15 19:04 [PATCH v2 00/27] btrfs-progs: introduce libbtrfsutil, "btrfs-progs as a library" Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 01/27] btrfs-progs: get rid of undocumented qgroup inheritance options Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 02/27] Add libbtrfsutil Omar Sandoval
2018-02-20 17:32   ` Liu Bo
2018-02-20 18:34     ` David Sterba
2018-02-15 19:04 ` [PATCH v2 03/27] libbtrfsutil: add Python bindings Omar Sandoval
2018-02-21 13:47   ` David Sterba
2018-02-21 18:08     ` Omar Sandoval
2018-02-22  1:44   ` Misono, Tomohiro
2018-02-15 19:04 ` [PATCH v2 04/27] libbtrfsutil: add btrfs_util_is_subvolume() and btrfs_util_subvolume_id() Omar Sandoval
2018-02-21 11:43   ` David Sterba
2018-02-21 13:02     ` David Sterba
2018-02-21 18:13       ` Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 05/27] libbtrfsutil: add qgroup inheritance helpers Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 06/27] libbtrfsutil: add btrfs_util_create_subvolume() Omar Sandoval
2018-02-23  8:24   ` Misono, Tomohiro
2018-02-23 22:58     ` Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 07/27] libbtrfsutil: add btrfs_util_subvolume_path() Omar Sandoval
2018-02-23  6:27   ` Misono, Tomohiro [this message]
2018-02-23 22:44     ` Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 08/27] libbtrfsutil: add btrfs_util_subvolume_info() Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 09/27] libbtrfsutil: add btrfs_util_[gs]et_read_only() Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 10/27] libbtrfsutil: add btrfs_util_[gs]et_default_subvolume() Omar Sandoval
2018-02-22  1:55   ` Misono, Tomohiro
2018-02-23 22:40     ` Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 11/27] libbtrfsutil: add subvolume iterator helpers Omar Sandoval
2018-02-23  7:40   ` Misono, Tomohiro
2018-02-23 22:49     ` Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 12/27] libbtrfsutil: add btrfs_util_create_snapshot() Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 13/27] libbtrfsutil: add btrfs_util_delete_subvolume() Omar Sandoval
2018-02-15 19:04 ` [PATCH v2 14/27] libbtrfsutil: add btrfs_util_deleted_subvolumes() Omar Sandoval
2018-02-23  2:12   ` Misono, Tomohiro
2018-02-23 23:33     ` Omar Sandoval
2018-02-28  4:11       ` Misono, Tomohiro
2018-02-15 19:05 ` [PATCH v2 15/27] libbtrfsutil: add filesystem sync helpers Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 16/27] btrfs-progs: use libbtrfsutil for read-only property Omar Sandoval
2018-02-22  4:23   ` Misono, Tomohiro
2018-02-23 22:41     ` Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 17/27] btrfs-progs: use libbtrfsutil for sync ioctls Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 18/27] btrfs-progs: use libbtrfsutil for set-default Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 19/27] btrfs-progs: use libbtrfsutil for get-default Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 20/27] btrfs-progs: use libbtrfsutil for subvol create and snapshot Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 21/27] btrfs-progs: use libbtrfsutil for subvol delete Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 22/27] btrfs-progs: use libbtrfsutil for subvol show Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 23/27] btrfs-progs: use libbtrfsutil for subvol sync Omar Sandoval
2018-02-22  2:03   ` Misono, Tomohiro
2018-02-23 22:41     ` Omar Sandoval
2018-02-23 23:22       ` David Sterba
2018-02-22  2:09   ` Misono, Tomohiro
2018-02-15 19:05 ` [PATCH v2 24/27] btrfs-progs: replace test_issubvolume() with btrfs_util_is_subvolume() Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 25/27] btrfs-progs: add recursive snapshot/delete using libbtrfsutil Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 26/27] btrfs-progs: use libbtrfsutil for subvolume list Omar Sandoval
2018-02-23  2:26   ` Misono, Tomohiro
2018-02-23 23:05     ` Omar Sandoval
2018-02-15 19:05 ` [PATCH v2 27/27] btrfs-progs: deprecate libbtrfs helpers Omar Sandoval
2018-02-21 15:04   ` David Sterba
2018-02-21 18:19     ` Omar Sandoval
2018-02-20 18:50 ` [PATCH v2 00/27] btrfs-progs: introduce libbtrfsutil, "btrfs-progs as a library" David Sterba
2018-02-21 15:13   ` David Sterba
2018-02-21 18:50     ` Omar Sandoval
2018-02-23 20:28       ` David Sterba
2018-02-26 23:36         ` Omar Sandoval
2018-02-27 15:04           ` David Sterba
2018-02-27 20:48             ` Omar Sandoval

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0452f2f6-02c5-7cb8-6a20-e1a65de096db@jp.fujitsu.com \
    --to=misono.tomohiro@jp.fujitsu.com \
    --cc=kernel-team@fb.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=osandov@osandov.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.