File: mounted.c

package info (click to toggle)
ocfs2-tools 1.8.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 6,232 kB
  • sloc: ansic: 86,865; sh: 5,781; python: 2,380; makefile: 1,305
file content (588 lines) | stat: -rw-r--r-- 13,460 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
 * mounted.c
 *
 * ocfs2 mount detect utility
 *
 * Copyright (C) 2004, 2011 Oracle.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA  02110-1301 USA.
 *
 */

#define _LARGEFILE64_SOURCE
#define _GNU_SOURCE /* Because libc really doesn't want us using O_DIRECT? */

#include <sys/sysmacros.h>
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/fd.h>
#include <linux/major.h>
#include <string.h>
#include <sys/stat.h>
#include <dirent.h>
#include <ctype.h>

#include <uuid/uuid.h>

#include "ocfs2-kernel/kernel-list.h"
#include "ocfs2/ocfs2.h"
#include "ocfs2/byteorder.h"
#include "tools-internal/verbose.h"

#undef max
#define max(a,b)	((a) > (b) ? (a) : (b))
#undef min
#define min(a,b)	((a) < (b) ? (a) : (b))

static int quick_detect = 1; /* default */
static char *device = NULL;
static char *progname = NULL;

static char *usage_string =
"usage: %s [-dfv] [device]\n"
"	-d quick detect\n"
"	-f full detect\n"
"	-v verbose\n";

static void get_max_widths(struct list_head *dev_list, int *dev_width,
			   int *cluster_width)
{
	ocfs2_devices *dev;
	struct list_head *pos;

	list_for_each(pos, dev_list) {
		dev = list_entry(pos, ocfs2_devices, list);
		if (dev->fs_type != 2)
			continue;
		*dev_width = max(*dev_width, strlen(dev->dev_name));
		*cluster_width = max(*cluster_width, strlen(dev->cluster));
	}
}

static void print_nodes(ocfs2_devices *dev, char **names,
			unsigned int length_of_names)
{
	int i, start = 1;
	unsigned int node_num;
        struct ocfs2_slot_map_data *map = dev->map;

	for (i = 0; i < map->md_num_slots; i++) {
		if (!map->md_slots[i].sd_valid)
			continue;

		if (start)
			start = 0;
		else
			printf(", ");

		node_num = map->md_slots[i].sd_node_num;

		if (node_num >= length_of_names)
			printf("Unknown");
		else if (names && names[node_num] && *(names[node_num]))
			printf("%s", names[node_num]);
		else
			printf("%d", node_num);
	}
}


static void print_full_detect(struct list_head *dev_list)
{
	ocfs2_devices *dev;
	struct list_head *pos;
	char **node_names = NULL;
	char **cluster_names = NULL;
	char *nodes[O2NM_MAX_NODES];
	char flag;
	int i = 0, dev_width = 7, cluster_width = 7;
	uint16_t num;

	get_max_widths(dev_list, &dev_width, &cluster_width);

	memset(nodes, 0, sizeof(nodes));

	o2cb_list_clusters(&cluster_names);

	if (cluster_names && *cluster_names) {
		o2cb_list_nodes(*cluster_names, &node_names);

		/* sort the names according to the node number */
		while(node_names && node_names[i] && *(node_names[i])) {
			if (o2cb_get_node_num(*cluster_names, node_names[i],
					      &num))
				break;
			if (num >= O2NM_MAX_NODES)
				break;
			nodes[num] = node_names[i];
			++i;
		}
	}

	printf("%-*s  %-5s  %-*s  %c  %-s\n", dev_width, "Device", "Stack",
	       cluster_width, "Cluster", 'F', "Nodes");

	list_for_each(pos, dev_list) {
		dev = list_entry(pos, ocfs2_devices, list);
		if (dev->fs_type != 2)
			continue;

		flag = ' ';
		if (!strcmp(dev->stack, OCFS2_CLASSIC_CLUSTER_STACK)) {
			if (dev->stackflags &
			    OCFS2_CLUSTER_O2CB_GLOBAL_HEARTBEAT)
				flag = 'G';
		}

		printf("%-*s  %-5s  %-*s  %c  ", dev_width, dev->dev_name,
		       dev->stack, cluster_width, dev->cluster, flag);

		if (dev->errcode) {
			fflush(stdout);
			com_err("Unknown", dev->errcode, " ");
		} else {
			if (dev->hb_dev)
				printf("Heartbeat device");
			else if (dev->mount_flags & OCFS2_MF_MOUNTED_CLUSTER)
				print_nodes(dev, nodes, O2NM_MAX_NODES);
			else
				printf("Not mounted");
			printf("\n");
		}
	}

	if (node_names)
		o2cb_free_nodes_list(node_names);

	if (cluster_names)
		o2cb_free_cluster_list(cluster_names);
}

static void print_quick_detect(struct list_head *dev_list)
{
	ocfs2_devices *dev;
	struct list_head *pos;
	char uuid[OCFS2_VOL_UUID_LEN * 2 + 1];
	int i, dev_width = 7, cluster_width = 7;
	char *p, flag;

	get_max_widths(dev_list, &dev_width, &cluster_width);

	printf("%-*s  %-5s  %-*s  %c  %-32s  %-s\n", dev_width, "Device",
	       "Stack", cluster_width, "Cluster", 'F', "UUID", "Label");

	list_for_each(pos, dev_list) {
		dev = list_entry(pos, ocfs2_devices, list);
		if (dev->fs_type != 2)
			continue;

		for (i = 0, p = uuid; i < OCFS2_VOL_UUID_LEN; i++) {
			snprintf(p, 3, "%02X", dev->uuid[i]);
			p += 2;
		}

		flag = ' ';
		if (!strcmp(dev->stack, OCFS2_CLASSIC_CLUSTER_STACK)) {
			if (dev->stackflags &
			    OCFS2_CLUSTER_O2CB_GLOBAL_HEARTBEAT)
				flag = 'G';
		}

		printf("%-*s  %-5s  %-*s  %c  %-32s  %-s\n", dev_width,
		       dev->dev_name, dev->stack, cluster_width, dev->cluster,
		       flag, uuid, dev->label);
	}
}

static void scan_dir_for_dev(char *dirname, dev_t devno, char **devname)
{
	DIR *dir;
	struct dirent *dp;
	char path[PATH_MAX];
	int dirlen;
	struct stat st;

	dir = opendir(dirname);
	if (dir == NULL)
		return;
	dirlen = strlen(dirname) + 2;
	while ((dp = readdir(dir)) != 0) {
		if (dirlen + strlen(dp->d_name) >= sizeof(path))
			continue;

		if (dp->d_name[0] == '.' &&
		    ((dp->d_name[1] == 0) ||
		     ((dp->d_name[1] == '.') && (dp->d_name[2] == 0))))
			continue;

		sprintf(path, "%s/%s", dirname, dp->d_name);
		if (stat(path, &st) < 0)
			continue;

		if (S_ISBLK(st.st_mode) && st.st_rdev == devno) {
			*devname = strdup(path);
			break;
		}
	}
	closedir(dir);
	return;
}

static void free_partition_list(struct list_head *dev_list)
{
	struct list_head *pos1, *pos2;
	ocfs2_devices *dev;

	list_for_each_safe(pos1, pos2, dev_list) {
		dev = list_entry(pos1, ocfs2_devices, list);
		if (dev->map)
			ocfs2_free(&dev->map);
		list_del(&(dev->list));
		ocfs2_free(&dev);
	}
}

static void list_rm_device(struct list_head *dev_list, int major, int minor)
{
	struct list_head *pos1, *pos2;
	ocfs2_devices *dev;

	list_for_each_safe(pos1, pos2, dev_list) {
		dev = list_entry(pos1, ocfs2_devices, list);
		if ((dev->maj_num == major) && (dev->min_num == minor)) {
			if (dev->map)
				ocfs2_free(&dev->map);
			list_del(&(dev->list));
			ocfs2_free(&dev);
		}
	}
}

static int is_partition(int major, int minor)
{
	char path[PATH_MAX + 1];
	struct stat info;

	snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/partition",
			major, minor);

	return !stat(path, &info);
}

static int find_whole_disk_minor(int major, int minor) {
#ifndef SCSI_BLK_MAJOR
#ifdef SCSI_DISK0_MAJOR
#ifdef SCSI_DISK8_MAJOR
#define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
		  ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR) || \
		  ((M) >= SCSI_DISK8_MAJOR && (M) <= SCSI_DISK15_MAJOR))
#else
#define SCSI_DISK_MAJOR(M) ((M) == SCSI_DISK0_MAJOR || \
		  ((M) >= SCSI_DISK1_MAJOR && (M) <= SCSI_DISK7_MAJOR))
#endif /* defined(SCSI_DISK8_MAJOR) */
#define SCSI_BLK_MAJOR(M) (SCSI_DISK_MAJOR((M)) || (M) == SCSI_CDROM_MAJOR)
#else
#define SCSI_BLK_MAJOR(M)  ((M) == SCSI_DISK_MAJOR || (M) == SCSI_CDROM_MAJOR)
#endif /* defined(SCSI_DISK0_MAJOR) */
#endif /* defined(SCSI_BLK_MAJOR) */
	if (major == HD_MAJOR)
		return (minor - (minor%64));

	if (SCSI_BLK_MAJOR(major))
	       return (minor - (minor%16));
	/* FIXME: Catch all */
	return 0;
}

static errcode_t build_partition_list(struct list_head *dev_list, char *device)
{
	errcode_t ret = 0;
	FILE *proc = NULL;
	char line[512];
	char name[512];
	char *devname = NULL;
	int major, minor;
	ocfs2_devices *dev;
	uint64_t numblocks;

	if (device) {
		ret = ocfs2_malloc0(sizeof(ocfs2_devices), &dev);
		if (ret)
			goto bail;
		strncpy(dev->dev_name, device, sizeof(dev->dev_name));
		list_add(&(dev->list), dev_list);
		return 0;
	}

	proc = fopen ("/proc/partitions", "r");
	if (proc == NULL) {
		ret = OCFS2_ET_IO;
		goto bail;
	}

	while (fgets (line, sizeof(line), proc) != NULL) {
		if (sscanf(line, "%d %d %*d %99[^ \t\n]",
			   &major, &minor, name) != 3)
			continue;

		ret = ocfs2_malloc0(sizeof(ocfs2_devices), &dev);
		if (ret)
			goto bail;

		/*
		 * Try to translate private device-mapper dm-<N> names
		 * to standard /dev/mapper/<name>.
		 */
		if (!strncmp(name, "dm-", 3) && isdigit(name[3])) {
			devname = NULL;
			scan_dir_for_dev("/dev/mapper",
					 makedev(major, minor), &devname);
			if (devname) {
				snprintf(dev->dev_name, sizeof(dev->dev_name),
					 "%s", devname);
				free(devname);
			} else
				snprintf(dev->dev_name, sizeof(dev->dev_name),
					 "/dev/%s", name);

		} else {
			snprintf(dev->dev_name, sizeof(dev->dev_name),
				 "/dev/%s", name);
		}

		/* skip devices smaller than 1M */

		if (ocfs2_get_device_size(dev->dev_name, 4096, &numblocks)) {
			verbosef(VL_DEBUG, "Unable to get size of %s\n",
				 dev->dev_name);
			ocfs2_free(&dev);
			continue;
		}


		if (numblocks <= (1024 * 1024 / 4096)) {
			verbosef(VL_DEBUG, "Skipping small device %s\n",
				 dev->dev_name);
			ocfs2_free(&dev);
			continue;
		}

		if (is_partition(major, minor)) {
			int whole_minor = find_whole_disk_minor(major, minor);
			list_rm_device(dev_list, major, whole_minor);
		}

		dev->maj_num = major;
		dev->min_num = minor;

		list_add_tail(&(dev->list), dev_list);
	}

bail:
	if (proc)
		fclose(proc);

	return ret;
}

static void usage(char *progname)
{
	printf(usage_string, progname);
}


static int read_options(int argc, char **argv)
{
	int ret = 0;
	int c;

	progname = argv[0];

	if (argc < 2) {
		usage(progname);
		ret = 1;					  
		goto bail;
	}

	while(1) {
		c = getopt(argc, argv, "dfv");
		if (c == -1)
			break;

		switch (c) {
		case 'd':	/* quick detect*/
			quick_detect = 1;
			break;

		case 'f':	/* full detect*/
			quick_detect = 0;
			break;

		case 'v':
			tools_verbose();
			break;

		default:
			break;
		}
	}

	if (!ret && optind < argc && argv[optind])
		device = argv[optind];

bail:
	return ret;
}

static ssize_t do_pread(int fd, void *buf, size_t count, off_t offset)
{
	ssize_t rd = 0, ret;

	while (1) {
		ret = pread(fd, buf + rd, count - rd, offset + rd);
		if (ret > 0)
			rd += ret;
		if (ret <= 0 || rd == count)
			break;
	}

	if (rd)
		return rd;
	return ret;
}

static void populate_sb_info(ocfs2_devices *dev, struct ocfs2_super_block *sb)
{
	uint32_t incompat;

	if (!sb)
		return;

	dev->fs_type = 2;

	memcpy(dev->label, sb->s_label, sizeof(dev->label));
	memcpy(dev->uuid, sb->s_uuid, sizeof(dev->uuid));

	incompat = le32_to_cpu(sb->s_feature_incompat);

	memcpy(dev->label, sb->s_label, sizeof(dev->label));
	memcpy(dev->uuid, sb->s_uuid, sizeof(dev->uuid));

#define CLUSTERINFO_VALID	(OCFS2_FEATURE_INCOMPAT_USERSPACE_STACK | \
				 OCFS2_FEATURE_INCOMPAT_CLUSTERINFO)

	if (incompat & OCFS2_FEATURE_INCOMPAT_LOCAL_MOUNT)
		snprintf(dev->stack, sizeof(dev->stack), "%s", "None");
	else if (incompat & CLUSTERINFO_VALID) {
		snprintf(dev->stack, sizeof(dev->stack), "%.*s",
			 OCFS2_STACK_LABEL_LEN, sb->s_cluster_info.ci_stack);
		snprintf(dev->cluster, sizeof(dev->cluster), "%.*s",
			 OCFS2_CLUSTER_NAME_LEN, sb->s_cluster_info.ci_cluster);
		dev->stackflags = sb->s_cluster_info.ci_stackflags;
	} else
		snprintf(dev->stack, sizeof(dev->stack), "%s",
			 OCFS2_CLASSIC_CLUSTER_STACK);
}

static void do_quick_detect(struct list_head *dev_list)
{
	int fd = -1, ret;
	char buf[512];
	struct ocfs2_dinode *di;
	uint32_t offset;
	struct list_head *pos;
	ocfs2_devices *dev;

	list_for_each(pos, dev_list) {
		dev = list_entry(pos, ocfs2_devices, list);

		verbosef(VL_APP, "Probing device %s\n", dev->dev_name);

		fd = open(dev->dev_name, O_RDONLY);
		if (fd < 0) {
			verbosef(VL_DEBUG, "Device %s open failed with '%s'\n",
			 	dev->dev_name, strerror(errno));
			continue;
		}

		/* ignore error but log if in verbose */
		ret = posix_fadvise(fd, 0, (1024 * 1024), POSIX_FADV_DONTNEED);
		if (ret < 0) {
			verbosef(VL_DEBUG, "Buffer cache free for device %s "
				 "failed with '%s'\n", dev->dev_name,
				 strerror(errno));
		}

		for (offset = 1; offset <= 8; offset <<= 1) {
			ret = do_pread(fd, buf, sizeof(buf), (offset * 1024));
			if (ret < sizeof(buf))
				break;
			di = (struct ocfs2_dinode *)buf;
			if (!memcmp(di->i_signature,
				    OCFS2_SUPER_BLOCK_SIGNATURE,
				    strlen(OCFS2_SUPER_BLOCK_SIGNATURE))) {
				populate_sb_info(dev, &di->id2.i_super);
				break;
			}
		}

		close(fd);
	}
}

static void do_full_detect(struct list_head *dev_list)
{
	ocfs2_check_heartbeats(dev_list, 1);
}

int main(int argc, char **argv)
{
	errcode_t ret = 0;
	struct list_head dev_list;

	initialize_ocfs_error_table();
	initialize_o2dl_error_table();
	initialize_o2cb_error_table();

	INIT_LIST_HEAD(&(dev_list));

	ret = read_options(argc, argv);
	if (ret)
		goto bail;

	o2cb_init();

	ret = build_partition_list(&dev_list, device);
	if (ret) {
		com_err(progname, ret, "while building partition list");
		goto bail;
	}

	if (quick_detect) {
		do_quick_detect(&dev_list);
		print_quick_detect(&dev_list);
	} else {
		do_full_detect(&dev_list);
		print_full_detect(&dev_list);
	}

	free_partition_list(&dev_list);

bail:
	return ret;
}