Whamcloud - gitweb
LU-14398 llapi: simplify llapi_fid2path()
[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(const char *path_or_device, const char *fidstr, char *path,
164                    int pathlen, long long *recno, int *linkno)
165 {
166         struct lu_fid fid;
167         struct getinfo_fid2path *gf = NULL;
168         int mnt_fd = -1;
169         int rc;
170
171         if (path_or_device == NULL || *path_or_device == '\0') {
172                 rc = -EINVAL;
173                 goto out;
174         }
175
176         if (*path_or_device == '/')
177                 rc = get_root_path(WANT_FD, NULL, &mnt_fd,
178                                    (char *)path_or_device, -1);
179         else
180                 rc = get_root_path(WANT_FD, (char *)path_or_device,
181                                    &mnt_fd, NULL, -1);
182
183         if (rc < 0)
184                 goto out;
185
186         rc = llapi_fid_parse(fidstr, &fid, NULL);
187         if (rc < 0) {
188                 llapi_err_noerrno(LLAPI_MSG_ERROR,
189                                   "bad FID format '%s', should be [seq:oid:ver] (e.g. "DFID")\n",
190                                   fidstr,
191                                   (unsigned long long)FID_SEQ_NORMAL, 2, 0);
192                 goto out;
193         }
194
195         gf = calloc(1, sizeof(*gf) + pathlen);
196         if (gf == NULL) {
197                 rc = -ENOMEM;
198                 goto out;
199         }
200
201         gf->gf_fid = fid;
202         if (recno)
203                 gf->gf_recno = *recno;
204         if (linkno)
205                 gf->gf_linkno = *linkno;
206         gf->gf_pathlen = pathlen;
207
208         rc = ioctl(mnt_fd, OBD_IOC_FID2PATH, gf);
209         if (rc < 0) {
210                 rc = -errno;
211                 goto out;
212         }
213
214         rc = copy_strip_dne_path(gf->gf_u.gf_path, path, pathlen);
215
216         if (recno)
217                 *recno = gf->gf_recno;
218
219         if (linkno)
220                 *linkno = gf->gf_linkno;
221
222 out:
223         if (!(mnt_fd < 0))
224                 close(mnt_fd);
225
226         free(gf);
227
228         errno = -rc;
229
230         return rc;
231 }
232
233 int llapi_get_mdt_index_by_fid(int fd, const struct lu_fid *fid,
234                                int *mdt_index)
235 {
236         int rc;
237
238         rc = ioctl(fd, LL_IOC_FID2MDTIDX, fid);
239         if (rc < 0)
240                 return -errno;
241
242         if (mdt_index)
243                 *mdt_index = rc;
244
245         return rc;
246 }
247
248 static int fid_from_lma(const char *path, int fd, struct lu_fid *fid)
249 {
250         struct lustre_mdt_attrs *lma;
251         char buf[512];
252         int rc = -1;
253
254         if (path == NULL)
255                 rc = fgetxattr(fd, XATTR_NAME_LMA, buf, sizeof(buf));
256         else
257                 rc = lgetxattr(path, XATTR_NAME_LMA, buf, sizeof(buf));
258         if (rc < 0)
259                 return -errno;
260
261         lma = (struct lustre_mdt_attrs *)buf;
262         memcpy(fid, &lma->lma_self_fid, sizeof(lma->lma_self_fid));
263         return 0;
264 }
265
266 int llapi_fd2fid(int fd, struct lu_fid *fid)
267 {
268         const struct lustre_file_handle *data;
269         struct file_handle *handle;
270         char buffer[sizeof(*handle) + MAX_HANDLE_SZ];
271         int mount_id;
272
273         memset(fid, 0, sizeof(*fid));
274
275         /* A lustre file handle should always fit in a 128 bytes long buffer
276          * (which is the value of MAX_HANDLE_SZ at the time this is written)
277          */
278         handle = (struct file_handle *)buffer;
279         handle->handle_bytes = MAX_HANDLE_SZ;
280
281         if (name_to_handle_at(fd, "", handle, &mount_id, AT_EMPTY_PATH)) {
282                 if (errno == EOVERFLOW)
283                         /* A Lustre file_handle would have fit */
284                         return -ENOTTY;
285                 return -errno;
286         }
287
288         if (handle->handle_type != FILEID_LUSTRE)
289                 /* Might be a locally mounted Lustre target */
290                 return fid_from_lma(NULL, fd, fid);
291         if (handle->handle_bytes < sizeof(*fid))
292                 /* Unexpected error try and recover */
293                 return fid_from_lma(NULL, fd, fid);
294
295         /* Parse the FID out of the handle */
296         data = (const struct lustre_file_handle *)handle->f_handle;
297         memcpy(fid, &data->lfh_child, sizeof(data->lfh_child));
298
299         return 0;
300 }
301
302 int llapi_path2fid(const char *path, struct lu_fid *fid)
303 {
304         int fd, rc;
305
306         fd = open(path, O_RDONLY | O_PATH | O_CLOEXEC | O_NOFOLLOW);
307         if (fd < 0)
308                 return -errno;
309
310         rc = llapi_fd2fid(fd, fid);
311         close(fd);
312
313         if (rc == -EBADF)
314                 /* Might be a locally mounted Lustre target
315                  *
316                  * Cannot use `fd' as fgetxattr() does not work on file
317                  * descriptor opened with O_PATH
318                  */
319                 rc = fid_from_lma(path, -1, fid);
320
321         return rc;
322 }
323
324 int llapi_fd2parent(int fd, unsigned int linkno, struct lu_fid *parent_fid,
325                     char *name, size_t name_size)
326 {
327         struct getparent *gp;
328         int rc;
329
330         if (name && name_size <= 1) {
331                 errno = EOVERFLOW;
332                 return -errno;
333         }
334
335         gp = malloc(sizeof(*gp) + name_size);
336         if (gp == NULL) {
337                 errno = ENOMEM;
338                 return -errno;
339         }
340
341         gp->gp_linkno = linkno;
342         gp->gp_name_size = name_size;
343
344         rc = ioctl(fd, LL_IOC_GETPARENT, gp);
345         if (rc < 0) {
346                 rc = -errno;
347                 goto err_free;
348         }
349
350         if (parent_fid)
351                 *parent_fid = gp->gp_fid;
352
353         if (name)
354                 rc = copy_strip_dne_path(gp->gp_name, name, name_size);
355
356 err_free:
357         free(gp);
358         return rc;
359 }
360
361 int llapi_path2parent(const char *path, unsigned int linkno,
362                       struct lu_fid *parent_fid, char *name, size_t name_size)
363 {
364         int fd;
365         int rc;
366
367         fd = open(path, O_RDONLY | O_NONBLOCK | O_NOFOLLOW);
368         if (fd < 0)
369                 return -errno;
370
371         rc = llapi_fd2parent(fd, linkno, parent_fid, name, name_size);
372         close(fd);
373
374         return rc;
375 }
376
377 /**
378  * Attempt to open a file with Lustre file identifier \a fid
379  * and return an open file descriptor.
380  *
381  * \param[in] lustre_dir        path within Lustre filesystem containing \a fid
382  * \param[in] fid               Lustre file identifier of file to open
383  * \param[in] flags             open() flags
384  *
385  * \retval                      non-negative file descriptor on successful open
386  * \retval                      negative errno if an error occurred
387  */
388 int llapi_open_by_fid(const char *lustre_dir, const struct lu_fid *fid,
389                       int flags)
390 {
391         char mntdir[PATH_MAX];
392         char path[PATH_MAX + 64];
393         int rc;
394
395         rc = llapi_search_mounts(lustre_dir, 0, mntdir, NULL);
396         if (rc)
397                 return rc;
398
399         snprintf(path, sizeof(path), "%s/.lustre/fid/"DFID, mntdir, PFID(fid));
400         rc = open(path, flags);
401         if (rc < 0)
402                 rc = -errno;
403
404         return rc;
405 }