Whamcloud - gitweb
ext2fs: check if Lustre filesystem is mounted
[tools/e2fsprogs.git] / lib / ext2fs / ismounted.c
1 /*
2  * ismounted.c --- Check to see if the filesystem was mounted
3  *
4  * Copyright (C) 1995,1996,1997,1998,1999,2000 Theodore Ts'o.
5  *
6  * %Begin-Header%
7  * This file may be redistributed under the terms of the GNU Library
8  * General Public License, version 2.
9  * %End-Header%
10  */
11
12 /* define BSD_SOURCE to make sure we get the major() macro */
13 #ifndef _BSD_SOURCE
14 #define _BSD_SOURCE
15 #endif
16 #ifndef _DEFAULT_SOURCE
17 #define _DEFAULT_SOURCE /* since glibc 2.20 _SVID_SOURCE is deprecated */
18 #endif
19
20 #include "config.h"
21 #include <stdio.h>
22 #if HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25 #if HAVE_ERRNO_H
26 #include <errno.h>
27 #endif
28 #include <fcntl.h>
29 #ifdef HAVE_LINUX_FD_H
30 #include <linux/fd.h>
31 #endif
32 #ifdef HAVE_LINUX_LOOP_H
33 #include <linux/loop.h>
34 #include <sys/ioctl.h>
35 #ifdef HAVE_LINUX_MAJOR_H
36 #include <linux/major.h>
37 #endif /* HAVE_LINUX_MAJOR_H */
38 #endif /* HAVE_LINUX_LOOP_H */
39 #ifdef HAVE_MNTENT_H
40 #include <mntent.h>
41 #endif
42 #ifdef HAVE_GETMNTINFO
43 #include <paths.h>
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #endif /* HAVE_GETMNTINFO */
47 #include <string.h>
48 #include <sys/stat.h>
49 #if HAVE_SYS_TYPES_H
50 #include <sys/types.h>
51 #endif
52 #ifdef HAVE_SYS_SYSMACROS_H
53 #include <sys/sysmacros.h>
54 #endif
55 #include <dirent.h>
56
57 #include "ext2_fs.h"
58 #include "ext2fs.h"
59 #include "ext2fsP.h"
60
61 #ifdef HAVE_SETMNTENT
62 /*
63  * Check to see if a regular file is mounted.
64  * If /etc/mtab/ is a symlink of /proc/mounts, you will need the following check
65  * because the name in /proc/mounts is a loopback device not a regular file.
66  */
67 static int check_loop_mounted(const char *mnt_fsname, dev_t mnt_rdev,
68                                 dev_t file_dev, ino_t file_ino)
69 {
70 #if defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H)
71         struct loop_info64 loopinfo = {0, };
72         int loop_fd, ret;
73
74         if (major(mnt_rdev) == LOOP_MAJOR) {
75                 loop_fd = open(mnt_fsname, O_RDONLY);
76                 if (loop_fd < 0)
77                         return -1;
78
79                 ret = ioctl(loop_fd, LOOP_GET_STATUS64, &loopinfo);
80                 close(loop_fd);
81                 if (ret < 0)
82                         return -1;
83
84                 if (file_dev == loopinfo.lo_device &&
85                                 file_ino == loopinfo.lo_inode)
86                         return 1;
87         }
88 #endif /* defined(HAVE_LINUX_LOOP_H) && defined(HAVE_LINUX_MAJOR_H) */
89         return 0;
90 }
91
92 /*
93  * Helper function which checks a file in /etc/mtab format to see if a
94  * filesystem is mounted.  Returns an error if the file doesn't exist
95  * or can't be opened.
96  */
97 static errcode_t check_mntent_file(const char *mtab_file, const char *file,
98                                    int *mount_flags, char *mtpt, int mtlen)
99 {
100         struct mntent   *mnt;
101         struct stat     st_buf;
102         errcode_t       retval = 0;
103         dev_t           file_dev=0, file_rdev=0;
104         ino_t           file_ino=0;
105         FILE            *f;
106         int             fd;
107
108         *mount_flags = 0;
109
110         if ((f = setmntent (mtab_file, "r")) == NULL) {
111                 if (errno == ENOENT) {
112                         if (getenv("EXT2FS_NO_MTAB_OK"))
113                                 return 0;
114                         else
115                                 return EXT2_ET_NO_MTAB_FILE;
116                 }
117                 return errno;
118         }
119         if (stat(file, &st_buf) == 0) {
120                 if (ext2fsP_is_disk_device(st_buf.st_mode)) {
121 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
122                         file_rdev = st_buf.st_rdev;
123 #endif  /* __GNU__ */
124                 } else {
125                         file_dev = st_buf.st_dev;
126                         file_ino = st_buf.st_ino;
127                 }
128         }
129         while ((mnt = getmntent (f)) != NULL) {
130                 if (mnt->mnt_fsname[0] != '/')
131                         continue;
132                 if (stat(mnt->mnt_dir, &st_buf) != 0)
133                         continue;
134                 if (strcmp(file, mnt->mnt_fsname) == 0) {
135                         if (file_rdev && (file_rdev != st_buf.st_dev)) {
136 #ifdef DEBUG
137                                 printf("Bogus entry in %s!  "
138                                        "(%s does not exist)\n",
139                                        mtab_file, mnt->mnt_dir);
140 #endif /* DEBUG */
141                                 continue;
142                         }
143                         break;
144                 }
145                 if (stat(mnt->mnt_fsname, &st_buf) == 0) {
146                         if (ext2fsP_is_disk_device(st_buf.st_mode)) {
147 #ifndef __GNU__
148                                 if (file_rdev && (file_rdev == st_buf.st_rdev))
149                                         break;
150                                 if (check_loop_mounted(mnt->mnt_fsname,
151                                                 st_buf.st_rdev, file_dev,
152                                                 file_ino) == 1)
153                                         break;
154 #endif  /* __GNU__ */
155                         } else {
156                                 if (file_dev && ((file_dev == st_buf.st_dev) &&
157                                                  (file_ino == st_buf.st_ino)))
158                                         break;
159                         }
160                 }
161         }
162
163         if (mnt == 0) {
164 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
165                 /*
166                  * Do an extra check to see if this is the root device.  We
167                  * can't trust /etc/mtab, and /proc/mounts will only list
168                  * /dev/root for the root filesystem.  Argh.  Instead we
169                  * check if the given device has the same major/minor number
170                  * as the device that the root directory is on.
171                  */
172                 if (file_rdev && stat("/", &st_buf) == 0) {
173                         if (st_buf.st_dev == file_rdev) {
174                                 *mount_flags = EXT2_MF_MOUNTED;
175                                 if (mtpt)
176                                         strncpy(mtpt, "/", mtlen);
177                                 goto is_root;
178                         }
179                 }
180 #endif  /* __GNU__ */
181                 goto errout;
182         }
183         *mount_flags = EXT2_MF_MOUNTED;
184
185 #ifdef MNTOPT_RO
186         /* Check to see if the ro option is set */
187         if (hasmntopt(mnt, MNTOPT_RO))
188                 *mount_flags |= EXT2_MF_READONLY;
189 #endif
190
191         if (mtpt)
192                 strncpy(mtpt, mnt->mnt_dir, mtlen);
193         /*
194          * Check to see if we're referring to the root filesystem.
195          * If so, do a manual check to see if we can open /etc/mtab
196          * read/write, since if the root is mounted read/only, the
197          * contents of /etc/mtab may not be accurate.
198          */
199         if (!strcmp(mnt->mnt_dir, "/")) {
200 is_root:
201 #define TEST_FILE "/.ismount-test-file"
202                 *mount_flags |= EXT2_MF_ISROOT;
203                 fd = open(TEST_FILE, O_RDWR|O_CREAT, 0600);
204                 if (fd < 0) {
205                         if (errno == EROFS)
206                                 *mount_flags |= EXT2_MF_READONLY;
207                 } else
208                         close(fd);
209                 (void) unlink(TEST_FILE);
210         }
211         retval = 0;
212 errout:
213         endmntent (f);
214         return retval;
215 }
216
217 static errcode_t check_mntent(const char *file, int *mount_flags,
218                               char *mtpt, int mtlen)
219 {
220         errcode_t       retval;
221
222 #ifdef DEBUG
223         retval = check_mntent_file("/tmp/mtab", file, mount_flags,
224                                    mtpt, mtlen);
225         if (retval == 0)
226                 return 0;
227 #endif /* DEBUG */
228 #ifdef __linux__
229         retval = check_mntent_file("/proc/mounts", file, mount_flags,
230                                    mtpt, mtlen);
231         if (retval == 0)
232                 return 0;
233 #endif /* __linux__ */
234 #if defined(MOUNTED) || defined(_PATH_MOUNTED)
235 #ifndef MOUNTED
236 #define MOUNTED _PATH_MOUNTED
237 #endif /* MOUNTED */
238         retval = check_mntent_file(MOUNTED, file, mount_flags, mtpt, mtlen);
239         return retval;
240 #else
241         *mount_flags = 0;
242         return 0;
243 #endif /* defined(MOUNTED) || defined(_PATH_MOUNTED) */
244 }
245
246 #else
247 #if defined(HAVE_GETMNTINFO)
248
249 static errcode_t check_getmntinfo(const char *file, int *mount_flags,
250                                   char *mtpt, int mtlen)
251 {
252         struct statfs *mp;
253         int    len, n;
254         const  char   *s1;
255         char    *s2;
256
257         n = getmntinfo(&mp, MNT_NOWAIT);
258         if (n == 0)
259                 return errno;
260
261         len = sizeof(_PATH_DEV) - 1;
262         s1 = file;
263         if (strncmp(_PATH_DEV, s1, len) == 0)
264                 s1 += len;
265
266         *mount_flags = 0;
267         while (--n >= 0) {
268                 s2 = mp->f_mntfromname;
269                 if (strncmp(_PATH_DEV, s2, len) == 0) {
270                         s2 += len - 1;
271                         *s2 = 'r';
272                 }
273                 if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0) {
274                         *mount_flags = EXT2_MF_MOUNTED;
275                         break;
276                 }
277                 ++mp;
278         }
279         if (mtpt)
280                 strncpy(mtpt, mp->f_mntonname, mtlen);
281         return 0;
282 }
283 #endif /* HAVE_GETMNTINFO */
284 #endif /* HAVE_SETMNTENT */
285
286 /*
287  * Check to see if we're dealing with the swap device.
288  */
289 static int is_swap_device(const char *file)
290 {
291         FILE            *f;
292         char            buf[1024], *cp;
293         dev_t           file_dev;
294         struct stat     st_buf;
295         int             ret = 0;
296
297         file_dev = 0;
298 #ifndef __GNU__ /* The GNU hurd is broken with respect to stat devices */
299         if ((stat(file, &st_buf) == 0) &&
300             ext2fsP_is_disk_device(st_buf.st_mode))
301                 file_dev = st_buf.st_rdev;
302 #endif  /* __GNU__ */
303
304         if (!(f = fopen("/proc/swaps", "r")))
305                 return 0;
306         /* Skip the first line */
307         if (!fgets(buf, sizeof(buf), f))
308                 goto leave;
309         if (*buf && strncmp(buf, "Filename\t", 9))
310                 /* Linux <=2.6.19 contained a bug in the /proc/swaps
311                  * code where the header would not be displayed
312                  */
313                 goto valid_first_line;
314
315         while (fgets(buf, sizeof(buf), f)) {
316 valid_first_line:
317                 if ((cp = strchr(buf, ' ')) != NULL)
318                         *cp = 0;
319                 if ((cp = strchr(buf, '\t')) != NULL)
320                         *cp = 0;
321                 if (strcmp(buf, file) == 0) {
322                         ret++;
323                         break;
324                 }
325 #ifndef __GNU__
326                 if (file_dev && (stat(buf, &st_buf) == 0) &&
327                     ext2fsP_is_disk_device(st_buf.st_mode) &&
328                     file_dev == st_buf.st_rdev) {
329                         ret++;
330                         break;
331                 }
332 #endif  /* __GNU__ */
333         }
334
335 leave:
336         fclose(f);
337         return ret;
338 }
339
340 static int check_lustre_zfs(char *mnt_device, const char *real_devname)
341 {
342         char *real_mnt_device, *buf, *ptr;
343         FILE *fp;
344         int rc = 0;
345
346         real_mnt_device = malloc(PATH_MAX);
347         if (real_mnt_device == NULL) {
348                 fprintf(stderr, "Cannot allocate memory to store path\n");
349                 return 1;
350         }
351
352         buf = malloc(PATH_MAX);
353         if (buf == NULL) {
354                 fprintf(stderr, "Cannot allocate memory to store command\n");
355                 free(real_mnt_device);
356                 return 1;
357         }
358
359         /* Get the pool name from mnt_device */
360         ptr = strchr(mnt_device, '/');
361         if (ptr)
362                 *ptr = '\0';
363
364         memset(buf, 0, PATH_MAX);
365         sprintf(buf, "zpool list -Hv %s", mnt_device);
366
367         fp = popen(buf, "r");
368         if (fp) {
369                 int line = 0;
370                 while (!feof(fp)) {
371                         memset(buf, 0, PATH_MAX);
372                         fscanf(fp, "%s%*[^\n]", buf);
373                         /* skip the first line */
374                         if (line++ == 0)
375                                 continue;
376                         /* skip empty line */
377                         if (strlen(buf) == 0)
378                                 continue;
379
380                         if (realpath(buf, real_mnt_device) == NULL)
381                                 continue;
382                         if (strcmp(real_devname, real_mnt_device) == 0) {
383                                 fprintf(stderr, "device %s mounted by Lustre\n",
384                                         real_devname);
385                                 rc = -1;
386                                 break;
387                         }
388                 }
389                 pclose(fp);
390         }
391
392         return rc;
393 }
394
395 static int check_lustre_ldiskfs(const char *mnt_device,
396                                 const char *real_devname)
397 {
398         char *real_mnt_device;
399
400         real_mnt_device = malloc(PATH_MAX);
401         if (real_mnt_device == NULL) {
402                 fprintf(stderr, "Cannot allocate memory to store path\n");
403                 return 1;
404         }
405
406         memset(real_mnt_device, 0, PATH_MAX);
407         if (realpath(mnt_device, real_mnt_device) == NULL) {
408                 fprintf(stderr, "Cannot resolve mntdev %s\n", mnt_device);
409                 free(real_mnt_device);
410                 return 1;
411         }
412
413         if (strcmp(real_devname, real_mnt_device) == 0) {
414                 fprintf(stderr, "device %s mounted by lustre\n", real_devname);
415                 free(real_mnt_device);
416                 return -1;
417         }
418
419         free(real_mnt_device);
420         return 0;
421 }
422
423 static int check_lustre_proc_vals(const char *procname,const char *real_devname)
424 {
425         struct dirent *direntp;
426         DIR *dirp;
427         int rc = 0;
428         char  *mnt_device, *proc_val;
429
430         mnt_device = malloc(PATH_MAX);
431         if (mnt_device == NULL) {
432                 fprintf(stderr, "Cannot allocate memory to store device\n");
433                 return 1;
434         }
435         proc_val = malloc(PATH_MAX);
436         if (proc_val == NULL) {
437                 fprintf(stderr, "Cannot allocate memory to store proc\n");
438                 free(mnt_device);
439                 return 1;
440         }
441
442         dirp = opendir(procname);
443         if (dirp) {
444                 do {
445                         int fd, numr;
446                         char *ptr;
447
448                         direntp = readdir(dirp);
449                         if (direntp == NULL)
450                                 break;
451                         if ((strncmp(direntp->d_name, ".", 1) == 0) ||
452                             (strncmp(direntp->d_name, "..", 2) == 0)) {
453                                 continue;
454                         }
455
456                         memset(proc_val, 0, PATH_MAX);
457                         snprintf(proc_val, PATH_MAX, "%s/%s/mntdev", procname,
458                                  direntp->d_name);
459                         fd = open(proc_val, O_RDONLY);
460                         if (fd < 0)  {
461                                 if (errno == ENOENT || errno == ENOTDIR)
462                                         continue;
463                                 fprintf(stderr, "Cannot open %s: %s\n",
464                                         proc_val, strerror(errno));
465                                 rc = 1;
466                                 break;
467                         }
468
469                         memset(mnt_device, 0, PATH_MAX);
470                         numr = read(fd, mnt_device, PATH_MAX);
471                         if (numr < 0) {
472                                 fprintf(stderr, "Failure to get mntdev info\n");
473                                 rc = 1;
474                                 close(fd);
475                                 break;
476                         }
477                         close(fd);
478
479                         ptr = strchr(mnt_device, '\n');
480                         if (ptr)
481                                 *ptr = '\0';
482
483                         if (strstr(procname, "osd-zfs") != NULL)
484                                 rc = check_lustre_zfs(mnt_device, real_devname);
485                         else
486                                 rc = check_lustre_ldiskfs(mnt_device,
487                                                           real_devname);
488
489                 } while (direntp != NULL && rc == 0);
490
491                 closedir(dirp);
492         }
493
494         free(proc_val);
495         free(mnt_device);
496
497         return rc;
498 }
499
500 static errcode_t check_if_lustre_mounted(const char *device, int *mount_flags)
501 {
502         char *real_device;
503         int rc = 0;
504
505         real_device = malloc(PATH_MAX);
506         if (real_device == NULL) {
507                 fprintf(stderr, "Cannot allocate memory to store path\n");
508                 return EXT2_ET_NO_MEMORY;
509         }
510
511         if (realpath(device, real_device) == NULL) {
512                 fprintf(stderr, "Cannot resolve path %s\n", device);
513                 rc = EXT2_ET_BAD_DEVICE_NAME;
514                 goto out_free;
515         }
516
517         rc = check_lustre_proc_vals("/proc/fs/lustre/osd-ldiskfs", real_device);
518         if (!rc)
519                 rc = check_lustre_proc_vals("/proc/fs/lustre/osd-zfs",
520                                             real_device);
521         if (rc)
522                 *mount_flags |= EXT2_MF_MOUNTED;
523
524 out_free:
525         free(real_device);
526
527         return rc;
528 }
529
530
531 /*
532  * ext2fs_check_mount_point() fills determines if the device is
533  * mounted or otherwise busy, and fills in mount_flags with one or
534  * more of the following flags: EXT2_MF_MOUNTED, EXT2_MF_ISROOT,
535  * EXT2_MF_READONLY, EXT2_MF_SWAP, and EXT2_MF_BUSY.  If mtpt is
536  * non-NULL, the directory where the device is mounted is copied to
537  * where mtpt is pointing, up to mtlen characters.
538  */
539 #ifdef __TURBOC__
540  #pragma argsused
541 #endif
542 errcode_t ext2fs_check_mount_point(const char *device, int *mount_flags,
543                                   char *mtpt, int mtlen)
544 {
545         errcode_t       retval = 0;
546         int             busy = 0;
547
548         if (getenv("EXT2FS_PRETEND_RO_MOUNT")) {
549                 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_READONLY;
550                 if (getenv("EXT2FS_PRETEND_ROOTFS"))
551                         *mount_flags = EXT2_MF_ISROOT;
552                 return 0;
553         }
554         if (getenv("EXT2FS_PRETEND_RW_MOUNT")) {
555                 *mount_flags = EXT2_MF_MOUNTED;
556                 if (getenv("EXT2FS_PRETEND_ROOTFS"))
557                         *mount_flags = EXT2_MF_ISROOT;
558                 return 0;
559         }
560
561 #ifdef __linux__ /* This only works on Linux 2.6+ systems */
562         {
563                 struct stat st_buf;
564
565                 if (stat(device, &st_buf) == 0 &&
566                     ext2fsP_is_disk_device(st_buf.st_mode)) {
567                         int fd = open(device, O_RDONLY | O_EXCL);
568
569                         if (fd >= 0) {
570                                 /*
571                                  * The device is not busy so it's
572                                  * definitelly not mounted. No need to
573                                  * to perform any more checks.
574                                  */
575                                 close(fd);
576                                 *mount_flags = 0;
577                                 return 0;
578                         } else if (errno == EBUSY) {
579                                 busy = 1;
580                         }
581                 }
582         }
583 #endif
584
585         if (is_swap_device(device)) {
586                 *mount_flags = EXT2_MF_MOUNTED | EXT2_MF_SWAP;
587                 strncpy(mtpt, "<swap>", mtlen);
588         } else {
589 #ifdef HAVE_SETMNTENT
590                 retval = check_mntent(device, mount_flags, mtpt, mtlen);
591 #else
592 #ifdef HAVE_GETMNTINFO
593                 retval = check_getmntinfo(device, mount_flags, mtpt, mtlen);
594 #else
595 #ifdef __GNUC__
596  #warning "Can't use getmntent or getmntinfo to check for mounted filesystems!"
597 #endif
598                 *mount_flags = 0;
599 #endif /* HAVE_GETMNTINFO */
600 #endif /* HAVE_SETMNTENT */
601         }
602         if (retval)
603                 return retval;
604
605         if (busy)
606                 *mount_flags |= EXT2_MF_BUSY;
607
608         retval = check_if_lustre_mounted(device, mount_flags);
609
610         return 0;
611 }
612
613 /*
614  * ext2fs_check_if_mounted() sets the mount_flags EXT2_MF_MOUNTED,
615  * EXT2_MF_READONLY, and EXT2_MF_ROOT
616  *
617  */
618 errcode_t ext2fs_check_if_mounted(const char *file, int *mount_flags)
619 {
620         return ext2fs_check_mount_point(file, mount_flags, NULL, 0);
621 }
622
623 #ifdef DEBUG
624 int main(int argc, char **argv)
625 {
626         int     retval, mount_flags;
627         char    mntpt[80];
628
629         if (argc < 2) {
630                 fprintf(stderr, "Usage: %s device\n", argv[0]);
631                 exit(1);
632         }
633
634         add_error_table(&et_ext2_error_table);
635         mntpt[0] = 0;
636         retval = ext2fs_check_mount_point(argv[1], &mount_flags,
637                                           mntpt, sizeof(mntpt));
638         if (retval) {
639                 com_err(argv[0], retval,
640                         "while calling ext2fs_check_if_mounted");
641                 exit(1);
642         }
643         printf("Device %s reports flags %02x\n", argv[1], mount_flags);
644         if (mount_flags & EXT2_MF_BUSY)
645                 printf("\t%s is apparently in use.\n", argv[1]);
646         if (mount_flags & EXT2_MF_MOUNTED)
647                 printf("\t%s is mounted.\n", argv[1]);
648         if (mount_flags & EXT2_MF_SWAP)
649                 printf("\t%s is a swap device.\n", argv[1]);
650         if (mount_flags & EXT2_MF_READONLY)
651                 printf("\t%s is read-only.\n", argv[1]);
652         if (mount_flags & EXT2_MF_ISROOT)
653                 printf("\t%s is the root filesystem.\n", argv[1]);
654         if (mntpt[0])
655                 printf("\t%s is mounted on %s.\n", argv[1], mntpt);
656         exit(0);
657 }
658 #endif /* DEBUG */