]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libfrog/paths.c
Merge branch 'libxfs-4.15-sync' into for-next
[thirdparty/xfsprogs-dev.git] / libfrog / paths.c
1 /*
2 * Copyright (c) 2005-2006 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 #include <paths.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include "path.h"
28 #include "input.h"
29 #include "project.h"
30 #include <limits.h>
31
32 extern char *progname;
33
34 int fs_count;
35 int xfs_fs_count;
36 struct fs_path *fs_table;
37 struct fs_path *fs_path;
38
39 char *mtab_file;
40 #define PROC_MOUNTS "/proc/self/mounts"
41
42 static int
43 fs_device_number(
44 const char *name,
45 dev_t *devnum)
46 {
47 struct stat sbuf;
48
49 if (stat(name, &sbuf) < 0)
50 return errno;
51 /*
52 * We want to match st_rdev if the path provided is a device
53 * special file. Otherwise we are looking for the the
54 * device id for the containing filesystem, in st_dev.
55 */
56 if (S_ISBLK(sbuf.st_mode) || S_ISCHR(sbuf.st_mode))
57 *devnum = sbuf.st_rdev;
58 else
59 *devnum = sbuf.st_dev;
60
61 return 0;
62 }
63
64 /*
65 * Find the FS table entry for the given path. The "flags" argument
66 * is a mask containing FS_MOUNT_POINT or FS_PROJECT_PATH (or both)
67 * to indicate the type of table entry sought.
68 * fs_table_lookup() finds the fs table entry for the filesystem hosting
69 * the file represented in the "dir" argument. To compare against actual
70 * mount point entries, use fs_table_lookup_mount() instead.
71 */
72 struct fs_path *
73 fs_table_lookup(
74 const char *dir,
75 uint flags)
76 {
77 uint i;
78 dev_t dev = 0;
79
80 if (fs_device_number(dir, &dev))
81 return NULL;
82
83 for (i = 0; i < fs_count; i++) {
84 if (flags && !(flags & fs_table[i].fs_flags))
85 continue;
86 if (fs_table[i].fs_datadev == dev)
87 return &fs_table[i];
88 }
89 return NULL;
90 }
91
92 /*
93 * Find the FS table entry describing an actual mount for the given path.
94 * Unlike fs_table_lookup(), fs_table_lookup_mount() compares the "dir"
95 * argument to actual mount point entries in the table. Accordingly, it
96 * will find matches only if the "dir" argument is indeed mounted.
97 */
98 struct fs_path *
99 fs_table_lookup_mount(
100 const char *dir)
101 {
102 uint i;
103 dev_t dev = 0;
104 char rpath[PATH_MAX];
105
106 if (fs_device_number(dir, &dev))
107 return NULL;
108
109 for (i = 0; i < fs_count; i++) {
110 if (fs_table[i].fs_flags != FS_MOUNT_POINT)
111 continue;
112 if (!realpath(fs_table[i].fs_dir, rpath))
113 continue;
114 if (strcmp(rpath, dir) == 0)
115 return &fs_table[i];
116 }
117 return NULL;
118 }
119
120 static int
121 fs_table_insert(
122 char *dir,
123 uint prid,
124 uint flags,
125 char *fsname,
126 char *fslog,
127 char *fsrt)
128 {
129 dev_t datadev, logdev, rtdev;
130 struct fs_path *tmp_fs_table;
131 int error;
132
133 datadev = logdev = rtdev = 0;
134 error = fs_device_number(dir, &datadev);
135 if (error)
136 goto out_nodev;
137 if (fslog) {
138 error = fs_device_number(fslog, &logdev);
139 if (error)
140 goto out_nodev;
141 }
142 if (fsrt) {
143 error = fs_device_number(fsrt, &rtdev);
144 if (error)
145 goto out_nodev;
146 }
147
148 if (!platform_test_xfs_path(dir))
149 flags |= FS_FOREIGN;
150
151 /*
152 * Make copies of the directory and data device path.
153 * The log device and real-time device, if non-null,
154 * are already the result of strdup() calls so we
155 * don't need to duplicate those. In fact, this
156 * function is assumed to "consume" both of those
157 * pointers, meaning if an error occurs they will
158 * both get freed.
159 */
160 error = ENOMEM;
161 dir = strdup(dir);
162 if (!dir)
163 goto out_nodev;
164 fsname = strdup(fsname);
165 if (!fsname)
166 goto out_noname;
167
168 tmp_fs_table = realloc(fs_table, sizeof(fs_path_t) * (fs_count + 1));
169 if (!tmp_fs_table)
170 goto out_norealloc;
171 fs_table = tmp_fs_table;
172
173 /* Put foreign filesystems at the end, xfs filesystems at the front */
174 if (flags & FS_FOREIGN || fs_count == 0) {
175 fs_path = &fs_table[fs_count];
176 } else {
177 /* move foreign fs entries down, insert new one just before */
178 memmove(&fs_table[xfs_fs_count + 1], &fs_table[xfs_fs_count],
179 sizeof(fs_path_t)*(fs_count - xfs_fs_count));
180 fs_path = &fs_table[xfs_fs_count];
181 }
182 fs_path->fs_dir = dir;
183 fs_path->fs_prid = prid;
184 fs_path->fs_flags = flags;
185 fs_path->fs_name = fsname;
186 fs_path->fs_log = fslog;
187 fs_path->fs_rt = fsrt;
188 fs_path->fs_datadev = datadev;
189 fs_path->fs_logdev = logdev;
190 fs_path->fs_rtdev = rtdev;
191 fs_count++;
192 if (!(flags & FS_FOREIGN))
193 xfs_fs_count++;
194
195 return 0;
196
197 out_norealloc:
198 free(fsname);
199 out_noname:
200 free(dir);
201 out_nodev:
202 /* "Consume" fslog and fsrt even if there's an error */
203 free(fslog);
204 free(fsrt);
205
206 return error;
207 }
208
209 /* Remove all the cached entries in the fs table. */
210 void
211 fs_table_destroy(void)
212 {
213 int i;
214 struct fs_path *fsp;
215
216 for (i = 0, fsp = fs_table; i < fs_count; i++, fsp++) {
217 free(fsp->fs_name);
218 free(fsp->fs_dir);
219 free(fsp->fs_log);
220 free(fsp->fs_rt);
221 }
222
223 fs_count = 0;
224 xfs_fs_count = 0;
225 free(fs_table);
226 fs_table = NULL;
227 }
228
229 /*
230 * Table iteration (cursor-based) interfaces
231 */
232
233 /*
234 * Initialize an fs_table cursor. If a directory path is supplied,
235 * the cursor is set up to appear as though the table contains only
236 * a single entry which represents the directory specified.
237 * Otherwise it is set up to prepare for visiting all entries in the
238 * global table, starting with the first. "flags" can be either
239 * FS_MOUNT_POINT or FS_PROJECT_PATH to limit what type of entries
240 * will be selected by fs_cursor_next_entry(). 0 can be used as a
241 * wild card (selecting either type).
242 */
243 void
244 fs_cursor_initialise(
245 char *dir,
246 uint flags,
247 fs_cursor_t *cur)
248 {
249 fs_path_t *path;
250
251 memset(cur, 0, sizeof(*cur));
252 if (dir) {
253 if ((path = fs_table_lookup(dir, flags)) == NULL)
254 return;
255 cur->local = *path;
256 cur->count = 1;
257 cur->table = &cur->local;
258 } else {
259 cur->count = fs_count;
260 cur->table = fs_table;
261 }
262 cur->flags = flags;
263 }
264
265 /*
266 * Use the cursor to find the next entry in the table having the
267 * type specified by the cursor's "flags" field.
268 */
269 struct fs_path *
270 fs_cursor_next_entry(
271 fs_cursor_t *cur)
272 {
273 while (cur->index < cur->count) {
274 fs_path_t *next = &cur->table[cur->index++];
275
276 if (!cur->flags || (cur->flags & next->fs_flags))
277 return next;
278 }
279 return NULL;
280 }
281
282
283 #if defined(HAVE_GETMNTENT)
284 #include <mntent.h>
285
286 /*
287 * Determines whether the "logdev" or "rtdev" mount options are
288 * present for the given mount point. If so, the value for each (a
289 * device path) is returned in the pointers whose addresses are
290 * provided. The pointers are assigned NULL for an option not
291 * present. Note that the path buffers returned are allocated
292 * dynamically and it is the caller's responsibility to free them.
293 */
294 static int
295 fs_extract_mount_options(
296 struct mntent *mnt,
297 char **logp,
298 char **rtp)
299 {
300 char *fslog, *fsrt;
301
302 /*
303 * Extract log device and realtime device from mount options.
304 *
305 * Note: the glibc hasmntopt implementation requires that the
306 * character in mnt_opts immediately after the search string
307 * must be a NULL ('\0'), a comma (','), or an equals ('=').
308 * Therefore we cannot search for 'logdev=' directly.
309 */
310 if ((fslog = hasmntopt(mnt, "logdev")) && fslog[6] == '=')
311 fslog += 7;
312 if ((fsrt = hasmntopt(mnt, "rtdev")) && fsrt[5] == '=')
313 fsrt += 6;
314
315 /* Do this only after we've finished processing mount options */
316 if (fslog) {
317 fslog = strndup(fslog, strcspn(fslog, " ,"));
318 if (!fslog)
319 goto out_nomem;
320 }
321 if (fsrt) {
322 fsrt = strndup(fsrt, strcspn(fsrt, " ,"));
323 if (!fsrt) {
324 free(fslog);
325 goto out_nomem;
326 }
327 }
328 *logp = fslog;
329 *rtp = fsrt;
330
331 return 0;
332
333 out_nomem:
334 *logp = NULL;
335 *rtp = NULL;
336 fprintf(stderr, _("%s: unable to extract mount options for \"%s\"\n"),
337 progname, mnt->mnt_dir);
338 return ENOMEM;
339 }
340
341 /*
342 * If *path is NULL, initialize the fs table with all xfs mount points in mtab
343 * If *path is specified, search for that path in mtab
344 *
345 * Everything - path, devices, and mountpoints - are boiled down to realpath()
346 * for comparison, but fs_table is populated with what comes from getmntent.
347 */
348 static int
349 fs_table_initialise_mounts(
350 char *path)
351 {
352 struct mntent *mnt;
353 FILE *mtp;
354 char *fslog, *fsrt;
355 int error, found;
356 char rpath[PATH_MAX], rmnt_fsname[PATH_MAX], rmnt_dir[PATH_MAX];
357
358 error = found = 0;
359 fslog = fsrt = NULL;
360
361 if (!mtab_file) {
362 mtab_file = PROC_MOUNTS;
363 if (access(mtab_file, R_OK) != 0)
364 mtab_file = MOUNTED;
365 }
366
367 if ((mtp = setmntent(mtab_file, "r")) == NULL)
368 return ENOENT;
369
370 /* Use realpath to resolve symlinks, relative paths, etc */
371 if (path)
372 if (!realpath(path, rpath))
373 return errno;
374
375 while ((mnt = getmntent(mtp)) != NULL) {
376 if (!realpath(mnt->mnt_dir, rmnt_dir))
377 continue;
378 if (!realpath(mnt->mnt_fsname, rmnt_fsname))
379 continue;
380
381 if (path &&
382 ((strcmp(rpath, rmnt_dir) != 0) &&
383 (strcmp(rpath, rmnt_fsname) != 0)))
384 continue;
385 if (fs_extract_mount_options(mnt, &fslog, &fsrt))
386 continue;
387 (void) fs_table_insert(mnt->mnt_dir, 0, FS_MOUNT_POINT,
388 mnt->mnt_fsname, fslog, fsrt);
389 if (path) {
390 found = 1;
391 break;
392 }
393 }
394 endmntent(mtp);
395
396 if (path && !found)
397 error = ENXIO;
398
399 return error;
400 }
401
402 #elif defined(HAVE_GETMNTINFO)
403 #include <sys/mount.h>
404
405 /*
406 * If *path is NULL, initialize the fs table with all xfs mount points in mtab
407 * If *path is specified, search for that path in mtab
408 *
409 * Everything - path, devices, and mountpoints - are boiled down to realpath()
410 * for comparison, but fs_table is populated with what comes from getmntinfo.
411 */
412 static int
413 fs_table_initialise_mounts(
414 char *path)
415 {
416 struct statfs *stats;
417 int i, count, error, found;
418 char rpath[PATH_MAX], rmntfromname[PATH_MAX], rmntonname[PATH_MAX];
419
420 error = found = 0;
421 if ((count = getmntinfo(&stats, 0)) < 0) {
422 fprintf(stderr, _("%s: getmntinfo() failed: %s\n"),
423 progname, strerror(errno));
424 return 0;
425 }
426
427 /* Use realpath to resolve symlinks, relative paths, etc */
428 if (path)
429 if (!realpath(path, rpath))
430 return errno;
431
432 for (i = 0; i < count; i++) {
433 if (!realpath(stats[i].f_mntfromname, rmntfromname))
434 continue;
435 if (!realpath(stats[i].f_mntonname, rmntonname))
436 continue;
437
438 if (path &&
439 ((strcmp(rpath, rmntonname) != 0) &&
440 (strcmp(rpath, rmntfromname) != 0)))
441 continue;
442 /* TODO: external log and realtime device? */
443 (void) fs_table_insert(stats[i].f_mntonname, 0,
444 FS_MOUNT_POINT, stats[i].f_mntfromname,
445 NULL, NULL);
446 if (path) {
447 found = 1;
448 break;
449 }
450 }
451 if (path && !found)
452 error = ENXIO;
453
454 return error;
455 }
456
457 #else
458 # error "How do I extract info about mounted filesystems on this platform?"
459 #endif
460
461 /*
462 * Given a directory, match it up to a filesystem mount point.
463 */
464 static struct fs_path *
465 fs_mount_point_from_path(
466 const char *dir)
467 {
468 fs_cursor_t cursor;
469 fs_path_t *fs;
470 dev_t dev = 0;
471
472 if (fs_device_number(dir, &dev))
473 return NULL;
474
475 fs_cursor_initialise(NULL, FS_MOUNT_POINT, &cursor);
476 while ((fs = fs_cursor_next_entry(&cursor))) {
477 if (fs->fs_datadev == dev)
478 break;
479 }
480 return fs;
481 }
482
483 static void
484 fs_table_insert_mount(
485 char *mount)
486 {
487 int error;
488
489 error = fs_table_initialise_mounts(mount);
490 if (error)
491 fprintf(stderr, _("%s: cannot setup path for mount %s: %s\n"),
492 progname, mount, strerror(error));
493 }
494
495 static int
496 fs_table_initialise_projects(
497 char *project)
498 {
499 fs_project_path_t *path;
500 fs_path_t *fs;
501 prid_t prid = 0;
502 int error = 0, found = 0;
503
504 if (project)
505 prid = prid_from_string(project);
506
507 setprpathent();
508 while ((path = getprpathent()) != NULL) {
509 if (project && prid != path->pp_prid)
510 continue;
511 fs = fs_mount_point_from_path(path->pp_pathname);
512 if (!fs) {
513 fprintf(stderr, _("%s: cannot find mount point for path `%s': %s\n"),
514 progname, path->pp_pathname, strerror(errno));
515 continue;
516 }
517 (void) fs_table_insert(path->pp_pathname, path->pp_prid,
518 FS_PROJECT_PATH, fs->fs_name,
519 NULL, NULL);
520 if (project) {
521 found = 1;
522 break;
523 }
524 }
525 endprpathent();
526
527 if (project && !found)
528 error = ENOENT;
529
530 return error;
531 }
532
533 static void
534 fs_table_insert_project(
535 char *project)
536 {
537 int error;
538
539 error = fs_table_initialise_projects(project);
540 if (error)
541 fprintf(stderr, _("%s: cannot setup path for project %s: %s\n"),
542 progname, project, strerror(error));
543 }
544
545 /*
546 * Initialize fs_table to contain the given set of mount points and
547 * projects. If mount_count is zero, mounts is ignored and the
548 * table is populated with mounted filesystems. If project_count is
549 * zero, projects is ignored and the table is populated with all
550 * projects defined in the projects file.
551 */
552 void
553 fs_table_initialise(
554 int mount_count,
555 char *mounts[],
556 int project_count,
557 char *projects[])
558 {
559 int error;
560 int i;
561
562 if (mount_count) {
563 for (i = 0; i < mount_count; i++)
564 fs_table_insert_mount(mounts[i]);
565 } else {
566 error = fs_table_initialise_mounts(NULL);
567 if (error)
568 goto out_error;
569 }
570 if (project_count) {
571 for (i = 0; i < project_count; i++)
572 fs_table_insert_project(projects[i]);
573 } else {
574 error = fs_table_initialise_projects(NULL);
575 if (error)
576 goto out_error;
577 }
578
579 return;
580
581 out_error:
582 fprintf(stderr, _("%s: cannot initialise path table: %s\n"),
583 progname, strerror(error));
584 }
585
586 void
587 fs_table_insert_project_path(
588 char *dir,
589 prid_t prid)
590 {
591 fs_path_t *fs;
592 int error = 0;
593
594 fs = fs_mount_point_from_path(dir);
595 if (fs)
596 error = fs_table_insert(dir, prid, FS_PROJECT_PATH,
597 fs->fs_name, NULL, NULL);
598 else
599 error = ENOENT;
600
601 if (error) {
602 fprintf(stderr, _("%s: cannot setup path for project dir %s: %s\n"),
603 progname, dir, strerror(error));
604 exit(1);
605 }
606 }