File: fs.c

package info (click to toggle)
xfsdump 3.2.0-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,012 kB
  • sloc: ansic: 45,797; sh: 3,449; makefile: 512
file content (370 lines) | stat: -rw-r--r-- 7,989 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (c) 2000-2002 Silicon Graphics, Inc.
 * 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.
 *
 * This program is distributed in the hope that it would 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 the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"

#include <unistd.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <mntent.h>
#include <sys/ioctl.h>
#include <assert.h>
#include <string.h>
#include <uuid/uuid.h>
#include <xfs/xfs.h>

#include "config.h"

#include "types.h"
#include "mlog.h"
#include "fs.h"


/* fs_info - a magic, highly heuristic function to convert a user-supplied
 * string into a file system type, character special device pathname,
 * a mount point, and file system ID (uuid). The primary source of information
 * is the getmntent(3) routines. Correspondence between "standard" disk
 * block and character device pathnames is used. The fstyp(1) utility
 * may be invoked as well.
 *
 * returns BOOL_TRUE on success, BOOL_FALSE on failure.
 *
 * coding rules: to make this very complex and lengthy decision tree
 * more graspable, some very strict coding rules were followed:
 *
 *	1) function arguments are ordered reference returns first, input
 *	parameters second.
 *
 *	2) all buffer-like refence return arguments are followed by a
 *	caller-supplied buffer size.
 *
 *	3) wherever possible functions return the result buffer pointer.
 *
 *	4) boolean functions return TRUE on success, FALSE on failure.
 *
 * all variables, parameters, and structure members are named as follows:
 *	object types:
 *		usr - user-specified mystery
 *		typ - file system type
 *		mnt - mount point pathname
 *		blk - block device pathname
 *		chr - character device pathname
 *		id - file system ID
 *		stat - stat buffer
 *		te - file system table entry
 *	object modifiers: appended to object type:
 *		b - buffer
 *		s - string
 *		d - default string
 *		p - pointer
 *	object size indication modifiers: appended to a modified object type:
 *		z - buffer size
 *		l - string length
 */

/* declarations of externally defined global variables
 */


/* definitions used locally
 */
struct fs_tab_ent {
	char *fte_blks;
	char *fte_mnts;
	char *fte_typs;
	struct fs_tab_ent *fte_nextp;
};

typedef struct fs_tab_ent fs_tab_ent_t;

static	fs_tab_ent_t *fs_tabp;

/* forward declarations
 */
static void fs_tab_build(void);
static void fs_tab_free(void);
static fs_tab_ent_t *fs_tab_ent_build(struct mntent *);
static void fs_tab_ent_free(fs_tab_ent_t *);
static fs_tab_ent_t *fs_tab_lookup_blk(char *);
static fs_tab_ent_t *fs_tab_lookup_mnt(char *);

/* ARGSUSED */
bool_t
fs_info(char *typb,		/* out */
	 int typbz,
	 char *typd,
	 char *blkb,		/* out */
	 int blkbz,
	 char *mntb,		/* out */
	 int mntbz,
	 uuid_t *idb,		/* out */
	 char *usrs)		/* in */
{
	struct stat64 statb;
	fs_tab_ent_t *tep;
	char *blks;
	char *mnts;
	char *typs;
	bool_t canstat;
	bool_t ok = BOOL_UNKNOWN;

	fs_tab_build();

	canstat = (stat64(usrs, &statb) == 0);
	if (canstat && (statb.st_mode & S_IFMT) == S_IFBLK) {
		if ((tep = fs_tab_lookup_blk(usrs)) != 0) {
			blks = tep->fte_blks;
			assert(strlen(blks) < (size_t)blkbz);
			strcpy(blkb, blks);
			mnts = tep->fte_mnts;
			if (mnts) {
				assert(strlen(mnts) < (size_t)mntbz);
				strcpy(mntb, mnts);
			} else {
				mntb[0] = 0;
			}
			if ((typs = tep->fte_typs) == 0) {
				typs = typd;
			}
			assert(strlen(typs) < (size_t)typbz);
			strcpy(typb, typs);
			ok = BOOL_TRUE;
		} else {
			ok = BOOL_FALSE;
		}
	} else if ((tep = fs_tab_lookup_mnt(usrs)) != 0) {
		blks = tep->fte_blks;
		assert(strlen(blks) < (size_t)blkbz);
		strcpy(blkb, blks);
		mnts = tep->fte_mnts;
		assert(strlen(mnts) < (size_t)mntbz);
		strcpy(mntb, mnts);
		typs = tep->fte_typs;
		assert(strlen(typs) < (size_t)typbz);
		strcpy(typb, typs);
		ok = BOOL_TRUE;
	} else {
		ok = BOOL_FALSE;
	}

	fs_tab_free();
	assert(ok != BOOL_UNKNOWN);

	if (ok == BOOL_TRUE) {
		int rval = fs_getid(mntb, idb);
		if (rval) {
			mlog(MLOG_NORMAL,
			      _("unable to determine uuid of fs mounted at %s: "
			      "%s\n"),
			      mntb,
			      strerror(errno));
		}
		{
			char string_uuid[37];
			uuid_unparse(*idb, string_uuid);
			mlog(MLOG_DEBUG,
			      "fs %s uuid [%s]\n",
			      mntb,
			      string_uuid);
		}
	}

	return ok;
}

/* fs_mounted - a predicate determining if the specified file system
 * is actually currently mounted at its mount point.
 * will not take time to code this now - just check if mntpt is non-NULL.
 */
/* ARGSUSED */
bool_t
fs_mounted(char *typs, char *chrs, char *mnts, uuid_t *idp)
{
	return strlen(mnts) > 0 ? BOOL_TRUE : BOOL_FALSE;
}

int
fs_getid(char *mnts, uuid_t *idb)
{
	struct xfs_fsop_geom_v1 geo;
	int fd;

	fd = open(mnts, O_RDONLY);
	if (fd < 0) {
		uuid_clear(*idb);
		return -1;
	}
	if (ioctl(fd, XFS_IOC_FSGEOMETRY_V1, &geo)) {
		uuid_clear(*idb);
		close(fd);
		return -1;
	}
	close(fd);
	uuid_copy(*idb, geo.uuid);

	return 0;
}

size_t
fs_getinocnt(char *mnts)
{
	struct statvfs vfsstat;
	int rval;

	rval = statvfs(mnts, &vfsstat);
	if (rval) {
		return 0;
	}

	if (vfsstat.f_files < vfsstat.f_ffree) {
		return 0;
	}

	return (size_t)(vfsstat.f_files - vfsstat.f_ffree);
}

static void
fs_tab_build(void)
{
	register struct mntent *mntentp;
	fs_tab_ent_t *tep;
	FILE *fp;

	fs_tabp = 0;
	fp = setmntent(MOUNTED, "r");
        if (fp == NULL) {
		mlog(MLOG_NORMAL,
		      _("Can't open %s for mount information\n"),
		      MOUNTED);
		return;
	}
	while ((mntentp = getmntent(fp)) != 0) {
		tep = fs_tab_ent_build(mntentp);
		tep->fte_nextp = fs_tabp;
		fs_tabp = tep;
	}
	endmntent(fp);
}

static void
fs_tab_free(void)
{
	fs_tab_ent_t *tep;
	fs_tab_ent_t *otep;

	for (tep = fs_tabp
	      ;
	      tep
	      ;
	      otep = tep, tep = tep->fte_nextp, fs_tab_ent_free(otep))

		;
}

static fs_tab_ent_t *
fs_tab_ent_build(struct mntent *mntentp)
{
	fs_tab_ent_t *tep;
	char *cp;

	tep = (fs_tab_ent_t *)calloc(1, sizeof(fs_tab_ent_t));
	assert(tep);

	if (mntentp->mnt_dir) {
		cp = calloc(1, strlen(mntentp->mnt_dir) + 1);
		assert(cp);
		(void)strcpy(cp, mntentp->mnt_dir);
		tep->fte_mnts = cp;
	} else {
		tep->fte_mnts = 0;
	}

	if (mntentp->mnt_type) {
		cp = calloc(1, strlen(mntentp->mnt_type) + 1);
		assert(cp);
		(void)strcpy(cp, mntentp->mnt_type);
		tep->fte_typs = cp;
	} else {
		tep->fte_typs = 0;
	}

	if (mntentp->mnt_fsname) {
		cp = calloc(1, strlen(mntentp->mnt_fsname) + 1);
		assert(cp);
		(void)strcpy(cp, mntentp->mnt_fsname);
		tep->fte_blks = cp;
	} else {
		tep->fte_blks = 0;
	}

	return tep;
}

static void
fs_tab_ent_free(fs_tab_ent_t *tep)
{
	if (tep->fte_blks) free(tep->fte_blks);
	if (tep->fte_mnts) free(tep->fte_mnts);
	if (tep->fte_typs) free(tep->fte_typs);
	memset((void *)tep, 0, sizeof(*tep)); /* bug catcher */
	free(tep);
}

static fs_tab_ent_t *
fs_tab_lookup_blk(char *blks)
{
	fs_tab_ent_t *tep;

	for (tep = fs_tabp; tep; tep = tep->fte_nextp) {
		struct stat64 stata;
		bool_t aok;
		struct stat64 statb;
		bool_t bok;

		if (!tep->fte_blks) {
			continue;
		}

		if (!strcmp(tep->fte_blks, blks)) {
			return tep;
		}

		aok = !stat64(blks, &stata);
		bok = !stat64(tep->fte_blks, &statb);
		if (aok && bok && stata.st_rdev == statb.st_rdev) {
			return tep;
		}
	}
	return 0;
}

static fs_tab_ent_t *
fs_tab_lookup_mnt(char *mnts)
{
	fs_tab_ent_t *tep;

	for (tep = fs_tabp; tep; tep = tep->fte_nextp) {
		if (tep->fte_mnts && !strcmp(tep->fte_mnts, mnts)) {
			return tep;
		}
	}
	return 0;
}