File: devtab.c

package info (click to toggle)
nfs-user-server 2.2beta47-23
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,016 kB
  • ctags: 1,435
  • sloc: ansic: 12,067; sh: 652; makefile: 265
file content (353 lines) | stat: -rw-r--r-- 8,321 bytes parent folder | download | duplicates (5)
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
/*
 * devtab.c
 *
 * Mapping of disk device numbers to ids used in pseudo inode numbers.
 *
 * Pseudo inode numbers (psi's) are made up of a combination of an index,
 * representing a device number, and the inode. Generation of psi's
 * happens like this:
 *
 *   bit prefix		index		inode #		inode max
 *   0			3bits		28bits		268435456
 *   10			3bits		27bits		134217728
 *   110		3bits		26bits		67108864
 *   1110		3bits		25bits		33554432
 *   ...
 *
 * Even for people exporting 24 different partitions, the smallest
 * range of inode numbers we can represent is 25bits.
 *
 * For mapping device numbers to indices, we use a file
 * (/var/state/nfs/devtab), containing the names of the device files.
 * The reason to make this a human-readable file is that this allows
 * the administrator to reorder entries so that the biggest partitions
 * are named first, and thus receive a 28bit inode range.
 *
 * Beware that when modifying the device table, you must first kill
 * mountd and nfsd. Also make sure that no client has mounted a file
 * system from your server, otherwise they might be accessing random
 * files, with the possibility of hosing your entire system.
 *
 * Copyright (C) 1998, Olaf Kirch <okir@monad.swb.de>
 */

#include "system.h"
#include "logging.h"
#include "auth.h"

#ifdef ENABLE_DEVTAB

#ifndef PATH_DEVTAB
# define PATH_DEVTAB		"/var/state/nfs/devtab"
#endif
#define PATH_DEVTAB_LOCK	PATH_DEVTAB ".lock"


static void			devtab_read(void);
static void			devtab_lock(void);
static void			devtab_unlock(void);
static FILE *			devtab_open(const char *mode, struct stat *sbp);
static unsigned int		devtab_add(dev_t dev);
static const char *		devtab_getname(dev_t dev);

static dev_t *			devtab;
static unsigned int		nrdevs;
static time_t			devtab_mtime;
static int			devtab_locked = 0;

/*
 * Locate the index associated with the given device number
 */
unsigned int
devtab_index(dev_t dev)
{
	unsigned int	index;
	struct stat	stb;
	FILE		*fp;
	int		oldmask;

	/* First, try to find entry in device table */
	for (index = 0; index < nrdevs; index++) {
		if (devtab[index] == dev)
			return index;
	}

	if (logging_enabled(D_DEVTAB)) {
		Dprintf(D_DEVTAB, "Can't find device 0x%x (%s) in devtab.",
			dev, devtab_getname(dev));
	}

	/* Entry not found. We need to create a new entry. */

	/* Set proper credentials and umask */
	auth_override_uid(ROOT_UID);
	oldmask = umask(0);

	/* First, lock devtab file */
	devtab_lock();

	fp = devtab_open("a", &stb);
	if (stb.st_mtime != devtab_mtime) {
		Dprintf(D_DEVTAB, "%sreading devtab file.\n",
			devtab_mtime? "re" : "");
		fclose(fp);
		devtab_read();
		for (index = 0; index < nrdevs; index++) {
			if (devtab[index] == dev)
				goto done;
		}
		/* re-open devtab */
		fp = devtab_open("a", &stb);
	}

	/* Add the device to the in-core table */
	index = devtab_add(dev);

	/* Find the device name and append to devtab file */
	fprintf(fp, "%s\n", devtab_getname(dev));
	fclose(fp);

done:
	devtab_unlock();
	auth_override_uid(auth_uid);
	umask(oldmask);
	return index;
}

static void
devtab_lock(void)
{
	char		tempname[sizeof(PATH_DEVTAB)+16], buffer[64];
	unsigned int	retry = 0, maxretry = 10;
	int		fd, n;
	int		pid = getpid();

	if (devtab_locked)
		return;

	/* Create temporary lock file, and write our PID to it. */
	sprintf(tempname, "%s.%d", PATH_DEVTAB, pid);
	if ((fd = open(tempname, O_WRONLY|O_EXCL|O_CREAT, 0600)) < 0) {
		Dprintf(L_FATAL, "Unable to create %s: %s",
			tempname, strerror(errno));
		/* notreached */
	}
	sprintf(buffer, "%d", pid);
	write(fd, buffer, strlen(buffer));
	close(fd);

	while (1) {
		Dprintf(D_DEVTAB, "Trying to lock %s", PATH_DEVTAB);
		if (link(tempname, PATH_DEVTAB_LOCK) >= 0) {
			devtab_locked = 1;
			break;
		}
		if (errno != EEXIST) {
			Dprintf(L_ERROR, "Unable to lock %s: %s",
				PATH_DEVTAB, strerror(errno));
			break;
		}
		if ((fd = open(PATH_DEVTAB_LOCK, O_RDONLY)) < 0) {
			Dprintf(L_ERROR, "Unable to open %s: %s",
				PATH_DEVTAB_LOCK, strerror(errno));
			break;
		}
		if ((n = read(fd, buffer, sizeof(buffer))) < 0) {
			Dprintf(L_ERROR,
				"unable to read pid from %s: %s",
				PATH_DEVTAB_LOCK, strerror(errno));
			break;
		}
		close(fd);

		buffer[n] = '\0';
		pid = atoi(buffer);
		if (kill(pid, 0) < 0 && errno == ESRCH) {
			Dprintf(L_WARNING,
				"removing stale lock for %s by pid=%d",
				PATH_DEVTAB, pid);
			unlink(PATH_DEVTAB_LOCK);
			continue;
		}

		if (retry == 1)
			Dprintf(L_ERROR, "%s already locked by pid=%d.",
				PATH_DEVTAB, pid);
		if (retry >= maxretry) {
			if ((maxretry <<= 1) >= 10 * 60)
				maxretry = 10 * 60;
			retry = 0;
		}
		retry++;
		sleep(1);
	}

	unlink(tempname);
	if (!devtab_locked)
		Dprintf(L_FATAL, "Aborting.");

	Dprintf(D_DEVTAB, "Successfully locked %s", PATH_DEVTAB);
}

static void
devtab_unlock(void)
{
	if (devtab_locked) {
		Dprintf(D_DEVTAB, "Unlocking %s", PATH_DEVTAB);
		unlink(PATH_DEVTAB_LOCK);
	}
	devtab_locked = 0;
}

static FILE *
devtab_open(const char *mode, struct stat *sbp)
{
	FILE	*fp;

	if ((fp = fopen(PATH_DEVTAB, mode)) == NULL) {
		devtab_unlock();
		Dprintf(L_FATAL,
			"unable to open %s for %s: %s", PATH_DEVTAB,
			(mode[0] == 'r')? "reading" : "writing",
			strerror(errno));
	}
	if (sbp && fstat(fileno(fp), sbp) < 0) {
		devtab_unlock();
		Dprintf(L_FATAL,
			"unable to stat %s: %s", PATH_DEVTAB,
			strerror(errno));
	}

	return fp;
}

static unsigned int
devtab_add(dev_t dev)
{
	Dprintf(D_DEVTAB, "Mapping dev 0x%x to index %u\n", dev, nrdevs);

	/* Grow device table if needed */
	if ((nrdevs % 8) == 0)
		devtab = (dev_t *) xrealloc(devtab,
				(nrdevs + 8) * sizeof(dev_t));
	devtab[nrdevs++] = dev;
	return nrdevs-1;
}

static void
devtab_read(void)
{
	struct stat	stb;
	char		buffer[1024], *sp;
	FILE		*fp;

	if (devtab)
		free(devtab);
	devtab = NULL;
	nrdevs = 0;

	fp = devtab_open("r", &stb);
	devtab_mtime = stb.st_mtime;

	while (fgets(buffer, sizeof(buffer), fp) != NULL) {
		/* shouldn't be there, but try to be nice to
		 * admins */
		if (buffer[0] == '#')
			continue;
		if ((sp = strchr(buffer, '\n')) != NULL)
			*sp = '\0';
		if (!strncmp(buffer, "devnum-", 7)) {
			stb.st_rdev = strtoul(buffer+7, &sp, 16);
			if (*sp) {
				Dprintf(L_FATAL,
					"invalid device name %s in %s",
					buffer, PATH_DEVTAB);
			}
		} else if (stat(buffer, &stb) < 0) {
			Dprintf(L_FATAL,
				"can't stat device name %s in %s: %s",
				buffer, PATH_DEVTAB,
				strerror(errno));
		}
		devtab_add(stb.st_rdev);
	}

	fclose(fp);
	return;
}

/*
 * Search all of /dev for a device file matching the given device number
 *
 * This routine works recursively in order to accomodate systems
 * that keep their device names in something like /dev/dsk
 */
static const char *
devtab_find(const char *dirname, dev_t dev, unsigned int depth)
{
	static char	fullname[1024];
	const char	*result = NULL;
	unsigned int	dirlen;
	struct dirent	*dp;
	struct stat	stb;
	DIR		*dir;

	/* Safeguard against infinite recursion (/dev may contain
	 * strange things like /dev/fd) */
	if (depth > 4)
		return NULL;

	Dprintf(D_DEVTAB, "Looking for dev 0x%x in %s\n", dev, dirname);
	if (dirname != fullname)
		strcpy(fullname, dirname);
	dirlen = strlen(fullname);
	fullname[dirlen++] = '/';
	fullname[dirlen] = '\0';

	if ((dir = opendir(fullname)) == NULL) {
		Dprintf(L_WARNING,
			"can't open %s for reading: %s",
			fullname, strerror(errno));
		return NULL;
	}

	while (!result && (dp = readdir(dir)) != NULL) {
		/* skip . and .. */
		if (!strcmp(dp->d_name, "..") || !strcmp(dp->d_name, "."))
			continue;
		/* skip long file names */
		if (strlen(dp->d_name) + dirlen + 1 >= sizeof(fullname))
			continue;
		strcpy(fullname + dirlen, dp->d_name);
		if (lstat(fullname, &stb) < 0) {
			Dprintf(L_WARNING,
				"unable to stat %s: %s (huh?!)",
				fullname, strerror(errno));
			continue;
		}
		if (S_ISDIR(stb.st_mode)) {
			result = devtab_find(fullname, dev, depth + 1);
		} else if (S_ISBLK(stb.st_mode) && stb.st_rdev == dev) {
			result = fullname;
		}
	}
	closedir(dir);
	return result;
}

static const char *
devtab_getname(dev_t dev)
{
	static char	fakename[64];
	const char	*result;

	if ((result = devtab_find("/dev", dev, 0)) == NULL) {
		/* Uh-oh... fake name */
		sprintf(fakename, "devnum-0x%lx", (unsigned long) dev);
		result = fakename;
	}

	return result;
}

#endif /* ENABLE_DEVTAB */