File: quotaon.c

package info (click to toggle)
quota 3.04-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 948 kB
  • ctags: 932
  • sloc: ansic: 7,622; sh: 486; makefile: 173
file content (295 lines) | stat: -rw-r--r-- 8,704 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
/*
 * Copyright (c) 1980, 1990 Regents of the University of California. All
 * rights reserved.
 * 
 * This code is derived from software contributed to Berkeley by Robert Elz at
 * The University of Melbourne.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer. 2.
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution. 3. All advertising
 * materials mentioning features or use of this software must display the
 * following acknowledgement: This product includes software developed by the
 * University of California, Berkeley and its contributors. 4. Neither the
 * name of the University nor the names of its contributors may be used to
 * endorse or promote products derived from this software without specific
 * prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#ident "$Copyright: (c) 1980, 1990 Regents of the University of California $"
#ident "$Copyright: All rights reserved. $"
#ident "$Id: quotaon.c,v 1.13 2002/02/27 21:23:55 jkar8572 Exp $"

/*
 * Turn quota on/off for a filesystem.
 */
#include <stdio.h>
#include <errno.h>
#include <getopt.h>
#include <string.h>
#include <stdlib.h>

#include "quotaon.h"

int aflag;			/* all file systems */
int gflag;			/* operate on group quotas */
int uflag;			/* operate on user quotas */
int vflag;			/* verbose */
int pflag;			/* just print status */
int kqf;			/* kernel quota format */
char *progname;

static void usage(void)
{
	errstr(_("Usage:\n\t%s [-guvp] [-x state] -a\n\t%s [-guvp] [-x state] filesys ...\n"), progname, progname);
	exit(1);
}

/*
 *	For both VFS quota formats, need to pass in the quota file;
 *	for XFS quota manager, pass on the -x command line option.
 */
static int newstate(struct mntent *mnt, int offmode, int type, char *extra)
{
	int flags, ret = 0;
	newstate_t *statefunc;

	flags = offmode ? STATEFLAG_OFF : STATEFLAG_ON;
	if (vflag > 1)
		flags |= STATEFLAG_VERYVERBOSE;
	else if (vflag)
		flags |= STATEFLAG_VERBOSE;
	if (aflag)
		flags |= STATEFLAG_ALL;

	if (!strcmp(mnt->mnt_type, MNTTYPE_XFS)) {	/* XFS filesystem has special handling... */
		if (!(kqf & (1 << QF_XFS))) {
			errstr("Can't change state of XFS quota. It's not compiled in kernel.\n");
			return 1;
		}
		if (kqf & (1 << QF_XFS) &&
		    ((offmode && (kern_quota_on(mnt->mnt_fsname, USRQUOTA, 1 << QF_XFS)
		    || kern_quota_on(mnt->mnt_fsname, GRPQUOTA, 1 << QF_XFS)))
		    || (!offmode && kern_quota_on(mnt->mnt_fsname, type, 1 << QF_XFS))))
			ret = xfs_newstate(mnt, type, extra, flags);
	}
	else {
		extra = get_qf_name(mnt, type, (kqf & (1 << QF_VFSV0)) ? QF_VFSV0 : QF_VFSOLD);
		statefunc = (kqf & (1 << QF_VFSV0)) ? v2_newstate : v1_newstate;
		ret = statefunc(mnt, type, extra, flags);
		free(extra);
	}
	return ret;
}

/* Print state of quota (on/off) */
static int print_state(struct mntent *mnt, int type)
{
	int on = 0;

	if (!strcmp(mnt->mnt_type, MNTTYPE_XFS)) {
		if (kqf & (1 << QF_XFS))
			on = kern_quota_on(mnt->mnt_fsname, type, 1 << QF_XFS) != -1;
	}
	else if (kqf & (1 << QF_VFSV0))
		on = kern_quota_on(mnt->mnt_fsname, type, 1 << QF_VFSV0) != -1;
	else if (kqf & (1 << QF_VFSOLD))
		on = kern_quota_on(mnt->mnt_fsname, type, 1 << QF_VFSOLD) != -1;

	printf("%s quota on %s (%s) is %s\n", type2name(type), mnt->mnt_dir, mnt->mnt_fsname,
	  on ? "on" : "off");
	
	return on;
}

int main(int argc, char **argv)
{
	struct mntent *mnt;
	char *xarg = NULL;
	int c, offmode = 0, errs = 0;

	gettexton();

	progname = basename(argv[0]);
	if (strcmp(progname, "quotaoff") == 0)
		offmode++;
	else if (strcmp(progname, "quotaon") != 0)
		die(1, _("Name must be quotaon or quotaoff not %s\n"), progname);

	while ((c = getopt(argc, argv, "afvugpx:V")) != -1) {
		switch (c) {
		  case 'a':
			  aflag++;
			  break;
		  case 'f':
			  offmode++;
			  break;
		  case 'g':
			  gflag++;
			  break;
		  case 'u':
			  uflag++;
			  break;
		  case 'v':
			  vflag++;
			  break;
		  case 'x':
			  xarg = optarg;
			  break;
		  case 'p':
			  pflag++;
			  break;
		  case 'V':
			  version();
			  exit(0);
		  default:
			  usage();
		}
	}
	argc -= optind;
	argv += optind;

	if (argc <= 0 && !aflag)
		usage();
	if (!gflag && !uflag) {
		gflag++;
		uflag++;
	}

	kqf = kern_quota_format();
	if (kqf == QF_TOONEW) {
		errstr(_("WARNING - Kernel quota newer than supported! Quota need not work properly.\n"));
		kqf = 1 << QF_VFSV0;
	}

	if (init_mounts_scan(aflag ? 0 : argc, argv) < 0)
		return 1;
	while ((mnt = get_next_mount(0))) {
		if (!strcmp(mnt->mnt_type, MNTTYPE_NFS)) {
			if (!aflag)
				fprintf(stderr, "%s: Quota can't be turned on on NFS filesystem\n", mnt->mnt_fsname);
			continue;
		}

		if (!pflag) {
			if (gflag)
				errs += newstate(mnt, offmode, GRPQUOTA, xarg);
			if (uflag)
				errs += newstate(mnt, offmode, USRQUOTA, xarg);
		}
		else {
			if (gflag)
				errs += print_state(mnt, GRPQUOTA);
			if (uflag)
				errs += print_state(mnt, USRQUOTA);
		}
	}
	end_mounts_scan();

	return errs;
}

/*
 *	Enable/disable VFS quota on given filesystem
 */
static int quotaonoff(char *quotadev, char *quotadir, char *quotafile, int type, int flags)
{
	int qcmd;

	if (flags & STATEFLAG_OFF) {
		qcmd = QCMD(Q_QUOTAOFF, type);
		if (quotactl(qcmd, quotadev, 0, NULL) < 0) {
			errstr(_("quotactl on %s [%s]: %s\n"), quotadev, quotadir, strerror(errno));
			return 1;
		}
		if (flags & STATEFLAG_VERBOSE)
			printf(_("%s [%s]: %s quotas turned off\n"), quotadev, quotadir, type2name(type));
		return 0;
	}
	qcmd = QCMD(Q_QUOTAON, type);
	if (quotactl(qcmd, quotadev, 0, (void *)quotafile) < 0) {
		if (errno == ENOENT)
			errstr(_("can't find %s on %s [%s]\n"), quotafile, quotadev, quotadir);
		else
			errstr(_("using %s on %s [%s]: %s\n"), quotafile, quotadev, quotadir, strerror(errno));
		if (errno == EINVAL)
			errstr(_("Maybe create new quota files with quotacheck(8)?\n"));
		return 1;
	}
	if (flags & STATEFLAG_VERBOSE)
		printf(_("%s [%s]: %s quotas turned on\n"), quotadev, quotadir, type2name(type));
	return 0;
}

/*
 *	Enable/disable rsquash on given filesystem
 */
static int quotarsquashonoff(const char *quotadev, int type, int flags)
{
#if defined(MNTOPT_RSQUASH)
	int mode = (flags & STATEFLAG_OFF) ? 0 : 1;
	int qcmd = QCMD(Q_V1_RSQUASH, type);

	if (quotactl(qcmd, quotadev, 0, (void *)&mode) < 0) {
		errstr(_("set root_squash on %s: %s\n"), quotadev, strerror(errno));
		return 1;
	}
	if ((flags & STATEFLAG_VERBOSE) && (flags & STATEFLAG_OFF))
		printf(_("%s: %s root_squash turned off\n"), quotadev, type2name(type));
	else if ((flags & STATEFLAG_VERBOSE) && (flags & STATEFLAG_ON))
		printf(_("%s: %s root_squash turned on\n"), quotadev, type2name(type));
#endif
	return 0;
}

/*
 *	Enable/disable quota/rootsquash on given filesystem (version 1)
 */
int v1_newstate(struct mntent *mnt, int type, char *file, int flags)
{
	int errs = 0;
	const char *dev = get_device_name(mnt->mnt_fsname);

	if (!dev)
		return 1;
	if ((flags & STATEFLAG_OFF) && hasmntopt(mnt, MNTOPT_RSQUASH))
		errs += quotarsquashonoff(dev, type, flags);
	if (hasquota(mnt, type))
		errs += quotaonoff((char *)dev, mnt->mnt_dir, file, type, flags);
	if ((flags & STATEFLAG_ON) && hasmntopt(mnt, MNTOPT_RSQUASH))
		errs += quotarsquashonoff(dev, type, flags);
	free((char *)dev);
	return errs;
}

/*
 *	Enable/disable quota on given filesystem (version 2 quota)
 */
int v2_newstate(struct mntent *mnt, int type, char *file, int flags)
{
	const char *dev = get_device_name(mnt->mnt_fsname);
	int errs = 0;

	if (!dev)
		return 1;
	if (hasquota(mnt, type))
		errs = quotaonoff((char *)dev, mnt->mnt_dir, file, type, flags);
	free((char *)dev);
	return errs;
}