4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the GNU Lesser General Public License
8 * (LGPL) version 2.1 or (at your discretion) any later version.
9 * (LGPL) version 2.1 accompanies this distribution, and is available at
10 * http://www.gnu.org/licenses/lgpl-2.1.html
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
20 * lustre/utils/liblustreapi_fid.c
22 * lustreapi library for FID mapping calls for determining the pathname
23 * of Lustre files from the File IDentifier.
25 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
26 * Use is subject to license terms.
28 * Copyright (c) 2011, 2017, Intel Corporation.
30 * Copyright (c) 2018, 2019, Data Direct Networks
43 #include <sys/ioctl.h>
44 #include <sys/xattr.h>
47 #include <libcfs/util/ioctl.h>
48 #include <lustre/lustreapi.h>
49 #include <linux/lustre/lustre_fid.h>
50 #include "lustreapi_internal.h"
52 /* strip instances of // (DNE striped directory) when copying to reply buffer */
53 static int copy_strip_dne_path(const char *src, char *tgt, size_t tgtlen)
58 for (a = src, b = tgt; *a != '\0' && b - tgt < tgtlen; a++) {
59 if (*a == '/' && *(a + 1) == '/')
64 if (b - tgt >= tgtlen) {
71 if (tgt[0] == '\0') { /* ROOT path */
80 * parse a FID from a string into a binary lu_fid
82 * Only the format of the FID is checked, not whether the numeric value
83 * contains a valid FID sequence or object ID or version. Optional leading
84 * whitespace and '[' from the standard FID format are skipped.
86 * \param[in] fidstr string to be parsed
87 * \param[out] fid Lustre File IDentifier
88 * \param[out] endptr pointer to first invalid/unused character in @fidstr
90 * \retval 0 on success
91 * \retval -errno on failure
93 int llapi_fid_parse(const char *fidstr, struct lu_fid *fid, char **endptr)
95 unsigned long long val;
97 char *end = (char *)fidstr;
100 if (!fidstr || !fid) {
105 while (isspace(*fidstr))
107 while (*fidstr == '[') {
112 /* Parse the FID fields individually with strtoull() instead of a
113 * single call to sscanf() so that the character after the FID can
114 * be returned in @endptr, in case the string has more to parse.
115 * If values are present, but too large for the field, continue
116 * parsing to consume the whole FID and return -ERANGE at the end.
119 val = strtoull(fidstr, &end, 0);
120 if ((val == 0 && errno == EINVAL) || *end != ':') {
124 if (val >= UINT64_MAX)
129 fidstr = end + 1; /* skip first ':', checked above */
131 val = strtoull(fidstr, &end, 0);
132 if ((val == 0 && errno == EINVAL) || *end != ':') {
136 if (val > UINT32_MAX)
141 fidstr = end + 1; /* skip second ':', checked above */
143 val = strtoull(fidstr, &end, 0);
144 if (val == 0 && errno == EINVAL) {
148 if (val > UINT32_MAX)
153 if (bracket && *end == ']')
163 static inline char *get_gf_path(struct getinfo_fid2path *gf)
165 #ifndef HAVE_FID2PATH_ANON_UNIONS
166 return gf->gf_u.gf_path;
172 int llapi_fid2path_at(int mnt_fd, const struct lu_fid *fid,
173 char *path_buf, int path_buf_size,
174 long long *recno, int *linkno)
176 struct getinfo_fid2path *gf = NULL;
179 gf = calloc(1, sizeof(*gf) + path_buf_size);
187 gf->gf_recno = *recno;
190 gf->gf_linkno = *linkno;
192 gf->gf_pathlen = path_buf_size;
194 rc = ioctl(mnt_fd, OBD_IOC_FID2PATH, gf);
200 rc = copy_strip_dne_path(get_gf_path(gf), path_buf, path_buf_size);
203 *recno = gf->gf_recno;
206 *linkno = gf->gf_linkno;
213 int llapi_fid2path(const char *path_or_device, const char *fidstr, char *path,
214 int pathlen, long long *recno, int *linkno)
220 if (path_or_device == NULL || *path_or_device == '\0') {
225 if (*path_or_device == '/')
226 rc = get_root_path(WANT_FD, NULL, &mnt_fd,
227 (char *)path_or_device, -1);
229 rc = get_root_path(WANT_FD, (char *)path_or_device,
235 rc = llapi_fid_parse(fidstr, &fid, NULL);
237 llapi_err_noerrno(LLAPI_MSG_ERROR,
238 "bad FID format '%s', should be [seq:oid:ver] (e.g. "DFID")\n",
240 (unsigned long long)FID_SEQ_NORMAL, 2, 0);
244 rc = llapi_fid2path_at(mnt_fd, &fid, path, pathlen, recno, linkno);
252 int llapi_get_mdt_index_by_fid(int fd, const struct lu_fid *fid,
257 rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
267 static int fid_from_lma(const char *path, int fd, struct lu_fid *fid)
269 #ifdef HAVE_SERVER_SUPPORT
270 struct lustre_mdt_attrs *lma;
275 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
277 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
281 lma = (struct lustre_mdt_attrs *)buf;
282 memcpy(fid, &lma->lma_self_fid, sizeof(lma->lma_self_fid));
289 int llapi_fd2fid(int fd, struct lu_fid *fid)
291 const struct lustre_file_handle *data;
292 struct file_handle *handle;
293 char buffer[sizeof(*handle) + MAX_HANDLE_SZ];
296 memset(fid, 0, sizeof(*fid));
298 /* A lustre file handle should always fit in a 128 bytes long buffer
299 * (which is the value of MAX_HANDLE_SZ at the time this is written)
301 handle = (struct file_handle *)buffer;
302 handle->handle_bytes = MAX_HANDLE_SZ;
304 if (name_to_handle_at(fd, "", handle, &mount_id, AT_EMPTY_PATH)) {
305 if (errno == EOVERFLOW)
306 /* A Lustre file_handle would have fit */
311 if (handle->handle_type != FILEID_LUSTRE)
312 /* Might be a locally mounted Lustre target */
313 return fid_from_lma(NULL, fd, fid);
314 if (handle->handle_bytes < sizeof(*fid))
315 /* Unexpected error try and recover */
316 return fid_from_lma(NULL, fd, fid);
318 /* Parse the FID out of the handle */
319 data = (const struct lustre_file_handle *)handle->f_handle;
320 memcpy(fid, &data->lfh_child, sizeof(data->lfh_child));
325 int llapi_path2fid(const char *path, struct lu_fid *fid)
329 fd = open(path, O_RDONLY | O_PATH | O_CLOEXEC | O_NOFOLLOW);
333 rc = llapi_fd2fid(fd, fid);
337 /* Might be a locally mounted Lustre target
339 * Cannot use `fd' as fgetxattr() does not work on file
340 * descriptor opened with O_PATH
342 rc = fid_from_lma(path, -1, fid);
347 int llapi_fd2parent(int fd, unsigned int linkno, struct lu_fid *parent_fid,
348 char *name, size_t name_size)
350 struct getparent *gp;
353 if (name && name_size <= 1) {
358 gp = malloc(sizeof(*gp) + name_size);
364 gp->gp_linkno = linkno;
365 gp->gp_name_size = name_size;
367 rc = ioctl(fd, LL_IOC_GETPARENT, gp);
374 *parent_fid = gp->gp_fid;
377 rc = copy_strip_dne_path(gp->gp_name, name, name_size);
384 int llapi_path2parent(const char *path, unsigned int linkno,
385 struct lu_fid *parent_fid, char *name, size_t name_size)
390 fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
394 rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
401 * Attempt to open a file with Lustre file identifier \a fid
402 * and return an open file descriptor.
404 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
405 * \param[in] fid Lustre file identifier of file to open
406 * \param[in] flags open() flags
408 * \retval non-negative file descriptor on successful open
409 * \retval negative errno if an error occurred
411 int llapi_open_by_fid(const char *lustre_dir, const struct lu_fid *fid,
414 char mntdir[PATH_MAX];
415 char path[PATH_MAX + 64];
418 rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
422 snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
423 rc = open(path, flags);