Viewing: liblustreapi_pool.c

// SPDX-License-Identifier: LGPL-2.1+
/*
 * This file is part of Lustre, http://www.lustre.org/
 *
 * Pool pinning helpers for liblustreapi.
 */

#include <sys/types.h>
#include <sys/xattr.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include <lnetconfig/cyaml.h>
#include <linux/lustre/lustre_user.h>

#include "lustreapi_internal.h"

#define PIN_YAML_POOL_STR		"pool"
/* Optional constraint: if present, only this pool may be used for pinning. */
#define PIN_YAML_ONLY_POOL_STR		"only_pool"
/* Pools that must never be used for pinning. */
#define PIN_YAML_DENY_POOL_STR		"deny_pool"

/*
 * Enforce pinning constraints encoded in the lustre.pin YAML object.
 *
 * - If one or more "deny_pool: <name>" entries are present, @pool_name must
 *   not match any of them.
 * - If an "only_pool: <name>" entry is present, the file is pinned
 *   exclusively to the set of pools already named by "pool: <name>"
 *   entries; @pool_name is only allowed if it is already one of them. The
 *   value stored in "only_pool" itself is just one representative member
 *   of that set (however many pools were listed alongside the original
 *   "^any" marker) and is not the full constraint -- callers must check
 *   membership in the "pool" entries, not equality with "only_pool".
 *
 * Return: 0 if allowed; on violation, -EPERM is returned and errno is set to
 *         EPERM.
 */
static int pool_pin_check_constraints(struct cYAML *yaml,
				      const char *pool_name)
{
	struct cYAML *node;
	bool have_only = false;
	bool already_pinned = false;

	for (node = yaml->cy_child; node != NULL; node = node->cy_next) {
		if (node->cy_type != CYAML_TYPE_STRING ||
		    node->cy_valuestring == NULL)
			continue;

		if (strcmp(node->cy_string, PIN_YAML_DENY_POOL_STR) == 0 &&
		    strcmp(node->cy_valuestring, pool_name) == 0) {
			errno = EPERM;
			return -EPERM;
		}

		if (strcmp(node->cy_string, PIN_YAML_ONLY_POOL_STR) == 0)
			have_only = true;

		if (strcmp(node->cy_string, PIN_YAML_POOL_STR) == 0 &&
		    strcmp(node->cy_valuestring, pool_name) == 0)
			already_pinned = true;
	}

	if (have_only && !already_pinned) {
		errno = EPERM;
		return -EPERM;
	}

	return 0;
}

/* check validity of the supplied pool name, stripping off the
 * leading fsname portion, if supplied.
 *
 * Also sanity checked in the kernel by mgs_extract_fs_pool().
 *
 * Return 0 on success, or negative errno on error
 */
int llapi_pool_name_validate(const char **pool_name)
{
	const char *ptr;

	if (!pool_name || *pool_name == NULL) {
		errno = ENXIO;
		return -errno;
	}

	/* Strip off any 'fsname.' portion. */
	ptr = strchr(*pool_name, '.');
	if (ptr != NULL)
		*pool_name = ptr + 1;

	return llapi_name_validate(*pool_name, "-_", LOV_MAXPOOLNAME);
}

static int pool_pin_ensure_entry(struct cYAML *yaml, const char *pool_name,
				 char *buff, size_t bufflen)
{
	struct cYAML *node;

	/* Search for an existing entry. */
	for (node = yaml->cy_child; node != NULL; node = node->cy_next) {
		if (strcmp(node->cy_string, PIN_YAML_POOL_STR) == 0 &&
		    node->cy_type == CYAML_TYPE_STRING &&
		    node->cy_valuestring != NULL &&
		    strcmp(node->cy_valuestring, pool_name) == 0)
			break;
	}

	/* If not found, create a new entry. */
	if (node == NULL) {
		node = cYAML_create_string(yaml, (char *)PIN_YAML_POOL_STR,
					   (char *)pool_name);
		if (node == NULL)
			return -errno ? -errno : -ENOMEM;
	}

	/* Serialise the (possibly updated) YAML object into buff. */
	errno = -llapi_dump_pin_object(yaml, buff, bufflen);
	if (errno)
		return -errno;

	/* buff now contains the new serialised lustre.pin value. */
	return 0;
}

static int pool_unpin_update_entry(struct cYAML *yaml, const char *pool_name,
				   char *buff, size_t bufflen, bool deny_only)
{
	struct cYAML *node = NULL;
	struct cYAML *only_node = NULL;
	bool was_positive_pool = false;

	/*
	 * Search for the entry to be deleted. @deny_only restricts the
	 * search to a "deny_pool" entry, for callers that specifically want
	 * to clear a deny rule (lfs pool unpin --not-pool) without touching
	 * a same-named positive pin -- though a pool can never be both, since
	 * llapi_pool_deny_fd() refuses to deny a pool that is already
	 * positively pinned.
	 *
	 * Otherwise, prefer a positive "pool" entry, but fall back to a
	 * "deny_pool" entry for the same name so that a plain unpin can also
	 * clear a deny rule.
	 */
	if (!deny_only) {
		for (node = yaml->cy_child; node != NULL; node = node->cy_next) {
			if (strcmp(node->cy_string, PIN_YAML_POOL_STR) == 0 &&
			    node->cy_type == CYAML_TYPE_STRING &&
			    node->cy_valuestring != NULL &&
			    strcmp(node->cy_valuestring, pool_name) == 0)
				break;
		}
		was_positive_pool = node != NULL;
	}
	if (node == NULL) {
		for (node = yaml->cy_child; node != NULL; node = node->cy_next) {
			if (strcmp(node->cy_string, PIN_YAML_DENY_POOL_STR) == 0 &&
			    node->cy_type == CYAML_TYPE_STRING &&
			    node->cy_valuestring != NULL &&
			    strcmp(node->cy_valuestring, pool_name) == 0)
				break;
		}
	}
	if (node == NULL) {
		/* No matching pool or deny_pool entry in lustre.pin. */
		errno = ENOENT;
		return -ENOENT;
	}

	/* Remove the node from the YAML tree. */
	if (node == yaml->cy_child) {
		/* the first child */
		if (node->cy_next)
			node->cy_next->cy_prev = NULL;
		yaml->cy_child = node->cy_next;
	} else {
		/* not the first child */
		node->cy_prev->cy_next = node->cy_next;
		if (node->cy_next)
			node->cy_next->cy_prev = node->cy_prev;
	}
	node->cy_prev = node->cy_next = NULL;
	cYAML_free_tree(node);

	/*
	 * If an only_pool entry names the pool that was just unpinned,
	 * either repoint it at another surviving "pool" entry (so the
	 * remaining pools stay exclusively pinned) or drop it if none
	 * remain. Simply deleting it here would silently turn off
	 * exclusivity for the other pools that are still pinned.
	 */
	if (was_positive_pool) {
		for (only_node = yaml->cy_child; only_node != NULL;
		     only_node = only_node->cy_next) {
			if (strcmp(only_node->cy_string,
				  PIN_YAML_ONLY_POOL_STR) == 0 &&
			    only_node->cy_type == CYAML_TYPE_STRING &&
			    only_node->cy_valuestring != NULL &&
			    strcmp(only_node->cy_valuestring, pool_name) == 0)
				break;
		}
	}
	if (only_node != NULL) {
		struct cYAML *remaining = NULL;

		for (node = yaml->cy_child; node != NULL; node = node->cy_next) {
			if (strcmp(node->cy_string, PIN_YAML_POOL_STR) == 0 &&
			    node->cy_type == CYAML_TYPE_STRING &&
			    node->cy_valuestring != NULL) {
				remaining = node;
				break;
			}
		}

		if (remaining != NULL) {
			if (cYAML_create_string(yaml, PIN_YAML_ONLY_POOL_STR,
						remaining->cy_valuestring) == NULL) {
				errno = ENOMEM;
				return -ENOMEM;
			}
		}

		if (only_node == yaml->cy_child) {
			if (only_node->cy_next)
				only_node->cy_next->cy_prev = NULL;
			yaml->cy_child = only_node->cy_next;
		} else {
			only_node->cy_prev->cy_next = only_node->cy_next;
			if (only_node->cy_next)
				only_node->cy_next->cy_prev =
					only_node->cy_prev;
		}
		only_node->cy_prev = only_node->cy_next = NULL;
		cYAML_free_tree(only_node);
	}

	errno = -llapi_dump_pin_object(yaml, buff, bufflen);
	if (errno)
		return -errno;

	return 0;
}

static int pool_is_pinned_from_yaml(struct cYAML *yaml, const char *pool_name)
{
	struct cYAML *node;
	int pinned = 0;

	for (node = yaml->cy_child; node != NULL; node = node->cy_next) {
		if (strcmp(node->cy_string, PIN_YAML_POOL_STR) == 0 &&
		    node->cy_type == CYAML_TYPE_STRING &&
		    node->cy_valuestring != NULL &&
		    strcmp(node->cy_valuestring, pool_name) == 0) {
			pinned = 1;
			break;
		}
	}

	cYAML_free_tree(yaml);
	return pinned;
}

/**
 * llapi_pool_pin_fd() - Pin a file for a given pool name using an open fd.
 * @fd: Open file descriptor for a Lustre file.
 * @pool_name: Name of the pool to pin for (optionally fsname-qualified).
 *
 * Ensure that a "pool: pool_name" entry exists in the lustre.pin xattr of the
 * file referenced by @fd. If such an entry already exists, this function
 * succeeds without modifying the xattr.
 *
 * Return: 0 on success; on failure, a negative errno value is returned and
 *         errno is set to the corresponding error code.
 */
int llapi_pool_pin_fd(int fd, const char *pool_name)
{
	struct cYAML *yaml = NULL;
	char buff[XATTR_SIZE_MAX];
	int rc;

	rc = llapi_pool_name_validate(&pool_name);
	if (rc)
		return rc;

	yaml = llapi_read_pin_xattr_object_fd(fd);
	if (yaml == NULL && errno == ENODATA) {
		snprintf(buff, sizeof(buff), "[%s: %s]", PIN_YAML_POOL_STR,
			 pool_name);
	} else if (yaml == NULL) {
		return -errno;
	} else {
		/* Enforce any existing only_pool constraint, if present. */
		rc = pool_pin_check_constraints(yaml, pool_name);
		if (rc < 0)
			goto out;

		rc = pool_pin_ensure_entry(yaml, pool_name, buff, sizeof(buff));
		if (rc < 0)
			goto out;
	}

	if (fsetxattr(fd, XATTR_LUSTRE_PIN, buff, strlen(buff), 0) < 0)
		rc = -errno;
	else
		rc = 0;

out:
	if (yaml)
		cYAML_free_tree(yaml);
	return rc;
}

/**
 * llapi_pool_pin_file() - Pin a file for a given pool name using a path.
 * @path: Path to a Lustre file.
 * @pool_name: Name of the pool to pin for (optionally fsname-qualified).
 *
 * Ensure that a "pool: pool_name" entry exists in the lustre.pin xattr of the
 * file at @path. If such an entry already exists, this function succeeds
 * without modifying the xattr.
 *
 * Return: 0 on success; on failure, a negative errno value is returned and
 *         errno is set to the corresponding error code.
 */
int llapi_pool_pin_file(const char *path, const char *pool_name)
{
	struct cYAML *yaml = NULL;
	char buff[XATTR_SIZE_MAX];
	int rc;

	rc = llapi_pool_name_validate(&pool_name);
	if (rc)
		return rc;

	yaml = llapi_read_pin_xattr_object(path);
	if (yaml == NULL && errno == ENODATA) {
		snprintf(buff, sizeof(buff), "[%s: %s]", PIN_YAML_POOL_STR,
			 pool_name);
	} else if (yaml == NULL) {
		llapi_error(LLAPI_MSG_ERROR, errno,
			    "cannot read or parse pin xattr of file '%s'.",
			    path);
		rc = -errno;
		goto out;
	} else {
		/* Enforce any existing only_pool constraint, if
		 * present.
		 */
		rc = pool_pin_check_constraints(yaml, pool_name);
		if (rc < 0)
			goto out;

		rc = pool_pin_ensure_entry(yaml, pool_name, buff,
					   sizeof(buff));
		if (rc < 0)
			goto out;
	}

	if (setxattr(path, XATTR_LUSTRE_PIN, buff, strlen(buff), 0) < 0)
		rc = -errno;
	else
		rc = 0;

out:
	if (yaml)
		cYAML_free_tree(yaml);
	return rc;
}

/**
 * llapi_pool_pin_fid() - Pin a file for a given pool name using its FID.
 * @lustre_dir: Path within the target Lustre filesystem (usually the mount).
 * @fid:        FID of the file to be pinned.
 * @pool_name:  Name of the pool to pin for (optionally fsname-qualified).
 *
 * Open the object referenced by @fid in @lustre_dir, then ensure a
 * corresponding "pool: pool_name" entry exists in its lustre.pin xattr.
 *
 * Return: 0 on success; on failure, a negative errno value is returned and
 *         errno is set to the corresponding error code.
 */
int llapi_pool_pin_fid(const char *lustre_dir, const struct lu_fid *fid,
		       const char *pool_name)
{
	int fd;
	int rc;

	fd = llapi_open_by_fid(lustre_dir, fid, O_RDONLY | O_NONBLOCK);
	if (fd < 0)
		return fd; /* negative errno, errno set by helper */

	rc = llapi_pool_pin_fd(fd, pool_name);
	if (rc < 0) {
		int saved_errno = errno;

		close(fd);
		errno = saved_errno;
		return rc;
	}

	close(fd);
	return 0;
}

static int pool_unpin_or_undeny_fd(int fd, const char *pool_name,
				   bool deny_only)
{
	int rc = 0;
	struct cYAML *yaml = NULL;
	char buff[XATTR_SIZE_MAX];

	rc = llapi_pool_name_validate(&pool_name);
	if (rc)
		return rc;

	yaml = llapi_read_pin_xattr_object_fd(fd);

	if (yaml == NULL && errno == ENODATA) {
		errno = ENOENT;
		return -errno;
	}
	if (yaml == NULL)
		return -errno;

	rc = pool_unpin_update_entry(yaml, pool_name, buff, sizeof(buff),
				     deny_only);
	if (rc < 0)
		goto out;

	if (strlen(buff) == 0) {
		if (fremovexattr(fd, XATTR_LUSTRE_PIN) < 0)
			rc = -errno;
		else
			rc = 0;
	} else {
		if (fsetxattr(fd, XATTR_LUSTRE_PIN, buff, strlen(buff), 0) < 0)
			rc = -errno;
		else
			rc = 0;
	}

out:
	if (yaml)
		cYAML_free_tree(yaml);
	return rc;
}

/**
 * llapi_pool_unpin_fd() - Remove a pool pin from a file (by fd).
 * @fd: Open file descriptor for a Lustre file.
 * @pool_name: Name of the pool to unpin (optionally fsname-qualified).
 *
 * Remove the "pool: pool_name" entry from the lustre.pin xattr of the file
 * referenced by @fd, or if there is none, a deny rule for @pool_name. If
 * neither exists (or the attribute is missing), this is treated as an error
 * and ENOENT is reported. When the last entry is removed, the lustre.pin
 * xattr itself is deleted.
 *
 * Return: 0 on success; on failure, a negative errno value is returned and
 *         errno is set to the corresponding error code.
 */
int llapi_pool_unpin_fd(int fd, const char *pool_name)
{
	return pool_unpin_or_undeny_fd(fd, pool_name, false);
}

/**
 * llapi_pool_undeny_fd() - Remove a pool deny rule from a file (by fd).
 * @fd: Open file descriptor for a Lustre file.
 * @pool_name: Name of the denied pool to clear (optionally fsname-qualified).
 *
 * Remove the "deny_pool: pool_name" entry from the lustre.pin xattr of the
 * file referenced by @fd, without touching a same-named positive pin (a pool
 * can never be both positively pinned and denied at once). If no matching
 * deny entry exists (or the attribute is missing), this is treated as an
 * error and ENOENT is reported. When the last entry is removed, the
 * lustre.pin xattr itself is deleted.
 *
 * Return: 0 on success; on failure, a negative errno value is returned and
 *         errno is set to the corresponding error code.
 */
int llapi_pool_undeny_fd(int fd, const char *pool_name)
{
	return pool_unpin_or_undeny_fd(fd, pool_name, true);
}

static int pool_pin_mark_exclusive_entry(struct cYAML *yaml,
					 const char *pool_name,
					 char *buff, size_t bufflen)
{
	struct cYAML *node;

	for (node = yaml->cy_child; node != NULL; node = node->cy_next) {
		if (strcmp(node->cy_string, PIN_YAML_ONLY_POOL_STR) == 0 &&
		    node->cy_type == CYAML_TYPE_STRING &&
		    node->cy_valuestring != NULL)
			break;
	}

	if (node == NULL) {
		node = cYAML_create_string(yaml, (char *)PIN_YAML_ONLY_POOL_STR,
					   (char *)pool_name);
		if (node == NULL)
			return -errno ? -errno : -ENOMEM;
	}

	errno = -llapi_dump_pin_object(yaml, buff, bufflen);
	if (errno)
		return -errno;

	return 0;
}

/**
 * llapi_pool_pin_only_fd() - Pin a file exclusively to a given pool name.
 * @fd: Open file descriptor for a Lustre file.
 * @pool_name: Name of the pool to pin for (optionally fsname-qualified).
 *
 * Ensure that a "pool: pool_name" entry exists in the lustre.pin xattr of the
 * file referenced by @fd, and mark the pin exclusive by adding an
 * "only_pool" entry if one is not already present. Both steps happen in a
 * single read-modify-write of the xattr, so the file is never left
 * observably pinned-but-not-yet-exclusive the way calling llapi_pool_pin_fd()
 * followed by a separate exclusivity update would.
 *
 * llapi_dump_pin_object() encodes an "only_pool" entry as a "^any" element
 * in the "pool" YAML list, e.g. [pool: [<name>,^any]]. lustre.pin supports
 * exclusivity to a whole *set* of pools (e.g. "[pool: [p1,p2,^any]]" pins
 * exclusively to {p1,p2}), so only_pool's stored value is not "the"
 * exclusive pool -- it is reduced to a single representative "pool" entry
 * by the round trip through the on-disk string form, and which one survives
 * that round trip can change across re-reads. Only its presence is
 * meaningful to this function: @pool_name is added to the "pool" entries
 * regardless, so there is nothing else to enforce once an only_pool entry
 * exists.
 *
 * Return: 0 on success; on failure, a negative errno value is returned and
 *         errno is set to the corresponding error code.
 */
int llapi_pool_pin_only_fd(int fd, const char *pool_name)
{
	int rc = 0;
	struct cYAML *yaml = NULL;
	char buff[XATTR_SIZE_MAX];

	rc = llapi_pool_name_validate(&pool_name);
	if (rc)
		return rc;

	yaml = llapi_read_pin_xattr_object_fd(fd);
	if (yaml == NULL && errno == ENODATA) {
		yaml = cYAML_create_object(NULL, NULL);
		if (yaml == NULL) {
			if (!errno)
				errno = ENOMEM;
			return -errno;
		}
	} else if (yaml == NULL) {
		return -errno;
	} else {
		/* Enforce any existing only_pool constraint, if present. */
		rc = pool_pin_check_constraints(yaml, pool_name);
		if (rc < 0)
			goto out;
	}

	rc = pool_pin_ensure_entry(yaml, pool_name, buff, sizeof(buff));
	if (rc < 0)
		goto out;

	rc = pool_pin_mark_exclusive_entry(yaml, pool_name, buff, sizeof(buff));
	if (rc < 0)
		goto out;

	if (fsetxattr(fd, XATTR_LUSTRE_PIN, buff, strlen(buff), 0) < 0)
		rc = -errno;
	else
		rc = 0;

out:
	if (yaml)
		cYAML_free_tree(yaml);
	return rc;
}

/**
 * llapi_pool_deny_fd() - Add a pool deny rule to a file (by fd).
 * @fd: Open file descriptor for a Lustre file.
 * @pool_name: Name of the pool to deny (optionally fsname-qualified).
 *
 * Ensure that a deny constraint for @pool_name exists in the lustre.pin
 * xattr of the file referenced by @fd (serialised as a "^pool_name" element
 * of the "pool" list). If such a constraint already exists, this function
 * succeeds without modifying the xattr.
 *
 * Return: 0 on success; -EEXIST if @pool_name is already positively pinned
 *         (plain or exclusive) for this file, since the same target cannot
 *         be both pinned to and denied from the same pool; on any other
 *         failure, a negative errno value is returned and errno is set to
 *         the corresponding error code.
 */
int llapi_pool_deny_fd(int fd, const char *pool_name)
{
	int rc = 0;
	struct cYAML *yaml = NULL;
	struct cYAML *node;
	char buff[XATTR_SIZE_MAX];
	bool has_deny = false;

	rc = llapi_pool_name_validate(&pool_name);
	if (rc)
		return rc;

	yaml = llapi_read_pin_xattr_object_fd(fd);
	if (yaml == NULL && errno == ENODATA) {
		yaml = cYAML_create_object(NULL, NULL);
		if (yaml == NULL) {
			if (!errno)
				errno = ENOMEM;
			return -errno;
		}
	} else if (yaml == NULL) {
		return -errno;
	} else {
		/*
		 * Reject denying a pool that is currently pinned (positively
		 * or exclusively) for this file; otherwise the file could end
		 * up both pinned to and denied from the same pool.
		 */
		for (node = yaml->cy_child; node != NULL;
		     node = node->cy_next) {
			if (node->cy_type != CYAML_TYPE_STRING ||
			    node->cy_valuestring == NULL)
				continue;

			if ((strcmp(node->cy_string, PIN_YAML_POOL_STR) == 0 ||
			     strcmp(node->cy_string,
				   PIN_YAML_ONLY_POOL_STR) == 0) &&
			    strcmp(node->cy_valuestring, pool_name) == 0) {
				errno = EEXIST;
				rc = -EEXIST;
				goto out;
			}

			if (strcmp(node->cy_string,
				  PIN_YAML_DENY_POOL_STR) == 0 &&
			    strcmp(node->cy_valuestring, pool_name) == 0)
				has_deny = true;
		}
	}

	/*
	 * Always go through cYAML + llapi_dump_pin_object() rather than
	 * hand-writing the string: the "deny_pool" key is an in-memory
	 * cYAML node name only, and must be serialised as a "^pool_name"
	 * element of the "pool" list, not as a literal "deny_pool: ..."
	 * entry, which the pin string parser rejects.
	 */
	if (has_deny) {
		/*
		 * Already denied: nothing to change, so skip the
		 * serialise+fsetxattr() below entirely rather than issuing
		 * a no-op MDT setxattr RPC, which could needlessly fail with
		 * EPERM under mdt.enable_pin_gid(4) or bump the file's ctime
		 * for no reason.
		 */
		rc = 0;
		goto out;
	}

	if (cYAML_create_string(yaml, (char *)PIN_YAML_DENY_POOL_STR,
				(char *)pool_name) == NULL) {
		if (!errno)
			errno = ENOMEM;
		rc = -errno;
		goto out;
	}

	rc = llapi_dump_pin_object(yaml, buff, sizeof(buff));
	if (rc < 0) {
		errno = -rc;
		rc = -errno;
		goto out;
	}

	if (fsetxattr(fd, XATTR_LUSTRE_PIN, buff, strlen(buff), 0) < 0)
		rc = -errno;
	else
		rc = 0;

out:
	if (yaml)
		cYAML_free_tree(yaml);
	return rc;
}

/**
 * llapi_pool_unpin_file() - Remove a pool pin from a file.
 * @path: Path to a Lustre file.
 * @pool_name: Name of the pool to unpin (optionally fsname-qualified).
 *
 * Remove the "pool: pool_name" entry from the lustre.pin xattr of @path, or
 * if there is none, a deny rule for @pool_name. If neither exists (or the
 * attribute is missing), this is treated as an error and ENOENT is reported.
 * When the last entry is removed, the lustre.pin xattr itself is deleted.
 *
 * Return: 0 on success; on failure, a negative errno value is returned and
 *         errno is set to the corresponding error code.
 */
int llapi_pool_unpin_file(const char *path, const char *pool_name)
{
	struct cYAML *yaml = NULL;
	char buff[XATTR_SIZE_MAX];
	int rc;

	rc = llapi_pool_name_validate(&pool_name);
	if (rc)
		return rc;

	yaml = llapi_read_pin_xattr_object(path);

	if (yaml == NULL && errno == ENODATA) {
		errno = ENOENT;
		rc = -errno;
		goto out;
	}
	if (yaml == NULL) {
		llapi_error(LLAPI_MSG_ERROR, errno,
			    "cannot read or parse pin xattr of file '%s'.",
			    path);
		rc = -errno;
		goto out;
	}

	rc = pool_unpin_update_entry(yaml, pool_name, buff, sizeof(buff), false);
	if (rc < 0)
		goto out;

	if (strlen(buff) == 0) {
		if (removexattr(path, XATTR_LUSTRE_PIN) < 0)
			rc = -errno;
		else
			rc = 0;
	} else {
		if (setxattr(path, XATTR_LUSTRE_PIN, buff, strlen(buff), 0) < 0)
			rc = -errno;
		else
			rc = 0;
	}

out:
	if (yaml)
		cYAML_free_tree(yaml);
	return rc;
}

/**
 * llapi_pool_unpin_fid() - Remove a pool pin from a FID-referenced file.
 * @lustre_dir: Path within the target Lustre filesystem (usually the mount).
 * @fid:        FID of the file to be unpinned.
 * @pool_name:  Name of the pool to unpin (optionally fsname-qualified).
 *
 * Open the object referenced by @fid in @lustre_dir and remove the
 * corresponding "pool: pool_name" entry from its lustre.pin xattr.
 *
 * Return: 0 on success; on failure, a negative errno value is returned and
 *         errno is set to the corresponding error code.
 */
int llapi_pool_unpin_fid(const char *lustre_dir, const struct lu_fid *fid,
			 const char *pool_name)
{
	int fd;
	int rc;

	fd = llapi_open_by_fid(lustre_dir, fid, O_RDONLY | O_NONBLOCK);
	if (fd < 0)
		return fd;

	rc = llapi_pool_unpin_fd(fd, pool_name);
	if (rc < 0) {
		int saved_errno = errno;

		close(fd);
		errno = saved_errno;
		return rc;
	}

	close(fd);
	return 0;
}

/**
 * llapi_pool_is_pinned_fd() - Test if a file is pinned for a pool name (fd).
 * @fd: Open file descriptor for a Lustre file.
 * @pool_name: Pool name to test (optionally fsname-qualified).
 *
 * Check whether the lustre.pin xattr of the file referenced by @fd contains
 * a "pool: pool_name" entry.
 *
 * Return: 1 if such an entry exists, 0 if not. On error, a negative errno
 *         value is returned.
 */
int llapi_pool_is_pinned_fd(int fd, const char *pool_name)
{
	struct cYAML *yaml;
	int rc;

	rc = llapi_pool_name_validate(&pool_name);
	if (rc)
		return rc;

	yaml = llapi_read_pin_xattr_object_fd(fd);
	if (yaml == NULL) {
		/*
		 * No lustre.pin attribute: treat as "not pinned".
		 * Any other error is propagated as -errno.
		 */
		if (errno == ENODATA)
			return 0;
		return -errno;
	}

	return pool_is_pinned_from_yaml(yaml, pool_name);
}

/**
 * llapi_pool_is_pinned_file() - Test if a file is pinned for a pool name.
 * @path: Path to a Lustre file.
 * @pool_name: Pool name to test (optionally fsname-qualified).
 *
 * Check whether the lustre.pin xattr of the file at @path contains a
 * "pool: pool_name" entry.
 *
 * Return: 1 if such an entry exists, 0 if not. On error, a negative errno
 *         value is returned.
 */
int llapi_pool_is_pinned_file(const char *path, const char *pool_name)
{
	struct cYAML *yaml;
	int rc;

	rc = llapi_pool_name_validate(&pool_name);
	if (rc)
		return rc;

	yaml = llapi_read_pin_xattr_object(path);
	if (yaml == NULL) {
		if (errno == ENODATA)
			return 0;
		return -errno;
	}

	return pool_is_pinned_from_yaml(yaml, pool_name);
}

/**
 * llapi_pool_is_pinned_fid() - Test if a FID-referenced file is pool-pinned.
 * @lustre_dir: Path within the target Lustre filesystem (usually the mount).
 * @fid:        FID of the file to be checked.
 * @pool_name:  Pool name to test (optionally fsname-qualified).
 *
 * Open the object referenced by @fid in @lustre_dir and check whether its
 * lustre.pin xattr contains a "pool: pool_name" entry.
 *
 * Return: 1 if such an entry exists, 0 if not. On error, a negative errno
 *         value is returned, including failures from llapi_open_by_fid().
 */
int llapi_pool_is_pinned_fid(const char *lustre_dir, const struct lu_fid *fid,
			     const char *pool_name)
{
	int fd;
	int pinned;

	fd = llapi_open_by_fid(lustre_dir, fid, O_RDONLY | O_NONBLOCK);
	if (fd < 0)
		return fd;

	pinned = llapi_pool_is_pinned_fd(fd, pool_name);
	if (pinned < 0) {
		int saved_errno = errno;

		close(fd);
		errno = saved_errno;
		return pinned;
	}

	close(fd);
	return pinned;
}