Whamcloud - gitweb
LU-14398 llapi: add llapi_fid2path_at()
[fs/lustre-release.git] / lustre / utils / liblustreapi_fid.c
1 /*
2  * LGPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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
11  *
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.
16  *
17  * LGPL HEADER END
18  */
19 /*
20  * lustre/utils/liblustreapi_fid.c
21  *
22  * lustreapi library for FID mapping calls for determining the pathname
23  * of Lustre files from the File IDentifier.
24  *
25  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
26  * Use is subject to license terms.
27  *
28  * Copyright (c) 2011, 2017, Intel Corporation.
29  *
30  * Copyright (c) 2018, 2019, Data Direct Networks
31  */
32
33 /* for O_DIRECTORY */
34 #ifndef _GNU_SOURCE
35 #define _GNU_SOURCE
36 #endif
37
38 #include <ctype.h>
39 #include <errno.h>
40 #include <fcntl.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <sys/ioctl.h>
44 #include <sys/xattr.h>
45 #include <unistd.h>
46
47 #include <libcfs/util/ioctl.h>
48 #include <lustre/lustreapi.h>
49 #include <linux/lustre/lustre_fid.h>
50 #include "lustreapi_internal.h"
51
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)
54 {
55         const char *a;
56         char *b;
57
58         for (a = src, b = tgt; *a != '\0' && b - tgt < tgtlen; a++) {
59                 if (*a == '/' && *(a + 1) == '/')
60                         continue;
61                 *b = *a;
62                 b++;
63         }
64         if (b - tgt >= tgtlen) {
65                 errno = ERANGE;
66                 return -errno;
67         }
68
69         *b = '\0';
70
71         if (tgt[0] == '\0') { /* ROOT path */
72                 tgt[0] = '/';
73                 tgt[1] = '\0';
74         }
75
76         return 0;
77 }
78
79 /**
80  * parse a FID from a string into a binary lu_fid
81  *
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.
85  *
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
89  *
90  * \retval      0 on success
91  * \retval      -errno on failure
92  */
93 int llapi_fid_parse(const char *fidstr, struct lu_fid *fid, char **endptr)
94 {
95         unsigned long long val;
96         bool bracket = false;
97         char *end = (char *)fidstr;
98         int rc = 0;
99
100         if (!fidstr || !fid) {
101                 rc = -EINVAL;
102                 goto out;
103         }
104
105         while (isspace(*fidstr))
106                 fidstr++;
107         while (*fidstr == '[') {
108                 bracket = true;
109                 fidstr++;
110         }
111
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.
117          */
118         errno = 0;
119         val = strtoull(fidstr, &end, 0);
120         if ((val == 0 && errno == EINVAL) || *end != ':') {
121                 rc = -EINVAL;
122                 goto out;
123         }
124         if (val >= UINT64_MAX)
125                 rc = -ERANGE;
126         else
127                 fid->f_seq = val;
128
129         fidstr = end + 1; /* skip first ':', checked above */
130         errno = 0;
131         val = strtoull(fidstr, &end, 0);
132         if ((val == 0 && errno == EINVAL) || *end != ':') {
133                 rc = -EINVAL;
134                 goto out;
135         }
136         if (val > UINT32_MAX)
137                 rc = -ERANGE;
138         else
139                 fid->f_oid = val;
140
141         fidstr = end + 1; /* skip second ':', checked above */
142         errno = 0;
143         val = strtoull(fidstr, &end, 0);
144         if (val == 0 && errno == EINVAL) {
145                 rc = -EINVAL;
146                 goto out;
147         }
148         if (val > UINT32_MAX)
149                 rc = -ERANGE;
150         else
151                 fid->f_ver = val;
152
153         if (bracket && *end == ']')
154                 end++;
155 out:
156         if (endptr)
157                 *endptr = end;
158
159         errno = -rc;
160         return rc;
161 }
162
163 int llapi_fid2path_at(int mnt_fd, const struct lu_fid *fid,
164                       char *path_buf, int path_buf_size,
165                       long long *recno, int *linkno)
166 {
167         struct getinfo_fid2path *gf = NULL;
168         int rc;
169
170         gf = calloc(1, sizeof(*gf) + path_buf_size);
171         if (gf == NULL) {
172                 rc = -ENOMEM;
173                 goto out;
174         }
175
176         gf->gf_fid = *fid;
177         if (recno != NULL)
178                 gf->gf_recno = *recno;
179
180         if (linkno != NULL)
181                 gf->gf_linkno = *linkno;
182
183         gf->gf_pathlen = path_buf_size;
184
185         rc = ioctl(mnt_fd, OBD_IOC_FID2PATH, gf);
186         if (rc) {
187                 rc = -errno;
188                 goto out;
189         }
190
191         rc = copy_strip_dne_path(gf->gf_u.gf_path, path_buf, path_buf_size);
192
193         if (recno != NULL)
194                 *recno = gf->gf_recno;
195
196         if (linkno != NULL)
197                 *linkno = gf->gf_linkno;
198 out:
199         free(gf);
200
201         return rc;
202 }
203
204 int llapi_fid2path(const char *path_or_device, const char *fidstr, char *path,
205                    int pathlen, long long *recno, int *linkno)
206 {
207         struct lu_fid fid;
208         int mnt_fd = -1;
209         int rc;
210
211         if (path_or_device == NULL || *path_or_device == '\0') {
212                 rc = -EINVAL;
213                 goto out;
214         }
215
216         if (*path_or_device == '/')
217                 rc = get_root_path(WANT_FD, NULL, &mnt_fd,
218                                    (char *)path_or_device, -1);
219         else
220                 rc = get_root_path(WANT_FD, (char *)path_or_device,
221                                    &mnt_fd, NULL, -1);
222
223         if (rc < 0)
224                 goto out;
225
226         rc = llapi_fid_parse(fidstr, &fid, NULL);
227         if (rc < 0) {
228                 llapi_err_noerrno(LLAPI_MSG_ERROR,
229                                   "bad FID format '%s', should be [seq:oid:ver] (e.g. "DFID")\n",
230                                   fidstr,
231                                   (unsigned long long)FID_SEQ_NORMAL, 2, 0);
232                 goto out;
233         }
234
235         rc = llapi_fid2path_at(mnt_fd, &fid, path, pathlen, recno, linkno);
236 out:
237         if (!(mnt_fd < 0))
238                 close(mnt_fd);
239
240         return rc;
241 }
242
243 int llapi_get_mdt_index_by_fid(int fd, const struct lu_fid *fid,
244                                int *mdt_index)
245 {
246         int rc;
247
248         rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
249         if (rc < 0)
250                 return -errno;
251
252         if (mdt_index)
253                 *mdt_index = rc;
254
255         return rc;
256 }
257
258 static int fid_from_lma(const char *path, int fd, struct lu_fid *fid)
259 {
260         struct lustre_mdt_attrs *lma;
261         char buf[512];
262         int rc = -1;
263
264         if (path == NULL)
265                 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
266         else
267                 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
268         if (rc < 0)
269                 return -errno;
270
271         lma = (struct lustre_mdt_attrs *)buf;
272         memcpy(fid, &lma->lma_self_fid, sizeof(lma->lma_self_fid));
273         return 0;
274 }
275
276 int llapi_fd2fid(int fd, struct lu_fid *fid)
277 {
278         const struct lustre_file_handle *data;
279         struct file_handle *handle;
280         char buffer[sizeof(*handle) + MAX_HANDLE_SZ];
281         int mount_id;
282
283         memset(fid, 0, sizeof(*fid));
284
285         /* A lustre file handle should always fit in a 128 bytes long buffer
286          * (which is the value of MAX_HANDLE_SZ at the time this is written)
287          */
288         handle = (struct file_handle *)buffer;
289         handle->handle_bytes = MAX_HANDLE_SZ;
290
291         if (name_to_handle_at(fd, "", handle, &mount_id, AT_EMPTY_PATH)) {
292                 if (errno == EOVERFLOW)
293                         /* A Lustre file_handle would have fit */
294                         return -ENOTTY;
295                 return -errno;
296         }
297
298         if (handle->handle_type != FILEID_LUSTRE)
299                 /* Might be a locally mounted Lustre target */
300                 return fid_from_lma(NULL, fd, fid);
301         if (handle->handle_bytes < sizeof(*fid))
302                 /* Unexpected error try and recover */
303                 return fid_from_lma(NULL, fd, fid);
304
305         /* Parse the FID out of the handle */
306         data = (const struct lustre_file_handle *)handle->f_handle;
307         memcpy(fid, &data->lfh_child, sizeof(data->lfh_child));
308
309         return 0;
310 }
311
312 int llapi_path2fid(const char *path, struct lu_fid *fid)
313 {
314         int fd, rc;
315
316         fd = open(path, O_RDONLY | O_PATH | O_CLOEXEC | O_NOFOLLOW);
317         if (fd < 0)
318                 return -errno;
319
320         rc = llapi_fd2fid(fd, fid);
321         close(fd);
322
323         if (rc == -EBADF)
324                 /* Might be a locally mounted Lustre target
325                  *
326                  * Cannot use `fd' as fgetxattr() does not work on file
327                  * descriptor opened with O_PATH
328                  */
329                 rc = fid_from_lma(path, -1, fid);
330
331         return rc;
332 }
333
334 int llapi_fd2parent(int fd, unsigned int linkno, struct lu_fid *parent_fid,
335                     char *name, size_t name_size)
336 {
337         struct getparent *gp;
338         int rc;
339
340         if (name && name_size <= 1) {
341                 errno = EOVERFLOW;
342                 return -errno;
343         }
344
345         gp = malloc(sizeof(*gp) + name_size);
346         if (gp == NULL) {
347                 errno = ENOMEM;
348                 return -errno;
349         }
350
351         gp->gp_linkno = linkno;
352         gp->gp_name_size = name_size;
353
354         rc = ioctl(fd, LL_IOC_GETPARENT, gp);
355         if (rc < 0) {
356                 rc = -errno;
357                 goto err_free;
358         }
359
360         if (parent_fid)
361                 *parent_fid = gp->gp_fid;
362
363         if (name)
364                 rc = copy_strip_dne_path(gp->gp_name, name, name_size);
365
366 err_free:
367         free(gp);
368         return rc;
369 }
370
371 int llapi_path2parent(const char *path, unsigned int linkno,
372                       struct lu_fid *parent_fid, char *name, size_t name_size)
373 {
374         int fd;
375         int rc;
376
377         fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
378         if (fd < 0)
379                 return -errno;
380
381         rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
382         close(fd);
383
384         return rc;
385 }
386
387 /**
388  * Attempt to open a file with Lustre file identifier \a fid
389  * and return an open file descriptor.
390  *
391  * \param[in] lustre_dir        path within Lustre filesystem containing \a fid
392  * \param[in] fid               Lustre file identifier of file to open
393  * \param[in] flags             open() flags
394  *
395  * \retval                      non-negative file descriptor on successful open
396  * \retval                      negative errno if an error occurred
397  */
398 int llapi_open_by_fid(const char *lustre_dir, const struct lu_fid *fid,
399                       int flags)
400 {
401         char mntdir[PATH_MAX];
402         char path[PATH_MAX + 64];
403         int rc;
404
405         rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
406         if (rc)
407                 return rc;
408
409         snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
410         rc = open(path, flags);
411         if (rc < 0)
412                 rc = -errno;
413
414         return rc;
415 }