Deleted Added
full compact
quotaon.c (201387) quotaon.c (207736)
1/*
2 * Copyright (c) 1980, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 28 unchanged lines hidden (view full) ---

37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)quotaon.c 8.1 (Berkeley) 6/6/93";
42#endif /* not lint */
43#endif
44#include <sys/cdefs.h>
1/*
2 * Copyright (c) 1980, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Robert Elz at The University of Melbourne.
7 *
8 * Redistribution and use in source and binary forms, with or without

--- 28 unchanged lines hidden (view full) ---

37 The Regents of the University of California. All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41static char sccsid[] = "@(#)quotaon.c 8.1 (Berkeley) 6/6/93";
42#endif /* not lint */
43#endif
44#include <sys/cdefs.h>
45__FBSDID("$FreeBSD: head/usr.sbin/quotaon/quotaon.c 201387 2010-01-02 11:05:34Z ed $");
45__FBSDID("$FreeBSD: head/usr.sbin/quotaon/quotaon.c 207736 2010-05-07 00:41:12Z mckusick $");
46
47/*
48 * Turn quota on/off for a filesystem.
49 */
50#include <sys/param.h>
51#include <sys/file.h>
52#include <sys/mount.h>
53#include <ufs/ufs/quota.h>
54#include <err.h>
55#include <fstab.h>
46
47/*
48 * Turn quota on/off for a filesystem.
49 */
50#include <sys/param.h>
51#include <sys/file.h>
52#include <sys/mount.h>
53#include <ufs/ufs/quota.h>
54#include <err.h>
55#include <fstab.h>
56#include <libutil.h>
56#include <stdio.h>
57#include <stdlib.h>
58#include <string.h>
59#include <unistd.h>
60
61const char *qfname = QUOTAFILENAME;
62const char *qfextension[] = INITQFNAMES;
63
64int aflag; /* all filesystems */
65int gflag; /* operate on group quotas */
66int uflag; /* operate on user quotas */
67int vflag; /* verbose */
68
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <unistd.h>
61
62const char *qfname = QUOTAFILENAME;
63const char *qfextension[] = INITQFNAMES;
64
65int aflag; /* all filesystems */
66int gflag; /* operate on group quotas */
67int uflag; /* operate on user quotas */
68int vflag; /* verbose */
69
69int hasquota(struct fstab *, int, char **);
70int oneof(char *, char *[], int);
70int oneof(char *, char *[], int);
71int quotaonoff(struct fstab *fs, int, int, char *);
72int readonly(struct fstab *);
71int quotaonoff(struct fstab *fs, int, int);
73static void usage(void);
74
75int
76main(int argc, char **argv)
77{
78 struct fstab *fs;
72static void usage(void);
73
74int
75main(int argc, char **argv)
76{
77 struct fstab *fs;
79 char *qfnp, *whoami;
78 char *whoami;
80 long argnum, done = 0;
81 int ch, i, offmode = 0, errs = 0;
82
83 whoami = rindex(*argv, '/') + 1;
84 if (whoami == (char *)1)
85 whoami = *argv;
86 if (strcmp(whoami, "quotaoff") == 0)
87 offmode++;

--- 26 unchanged lines hidden (view full) ---

114 uflag++;
115 }
116 setfsent();
117 while ((fs = getfsent()) != NULL) {
118 if (strcmp(fs->fs_vfstype, "ufs") ||
119 strcmp(fs->fs_type, FSTAB_RW))
120 continue;
121 if (aflag) {
79 long argnum, done = 0;
80 int ch, i, offmode = 0, errs = 0;
81
82 whoami = rindex(*argv, '/') + 1;
83 if (whoami == (char *)1)
84 whoami = *argv;
85 if (strcmp(whoami, "quotaoff") == 0)
86 offmode++;

--- 26 unchanged lines hidden (view full) ---

113 uflag++;
114 }
115 setfsent();
116 while ((fs = getfsent()) != NULL) {
117 if (strcmp(fs->fs_vfstype, "ufs") ||
118 strcmp(fs->fs_type, FSTAB_RW))
119 continue;
120 if (aflag) {
122 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
123 errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
124 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
125 errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
121 if (gflag)
122 errs += quotaonoff(fs, offmode, GRPQUOTA);
123 if (uflag)
124 errs += quotaonoff(fs, offmode, USRQUOTA);
126 continue;
127 }
128 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
129 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
130 done |= 1 << argnum;
125 continue;
126 }
127 if ((argnum = oneof(fs->fs_file, argv, argc)) >= 0 ||
128 (argnum = oneof(fs->fs_spec, argv, argc)) >= 0) {
129 done |= 1 << argnum;
131 if (gflag && hasquota(fs, GRPQUOTA, &qfnp))
132 errs += quotaonoff(fs, offmode, GRPQUOTA, qfnp);
133 if (uflag && hasquota(fs, USRQUOTA, &qfnp))
134 errs += quotaonoff(fs, offmode, USRQUOTA, qfnp);
130 if (gflag)
131 errs += quotaonoff(fs, offmode, GRPQUOTA);
132 if (uflag)
133 errs += quotaonoff(fs, offmode, USRQUOTA);
135 }
136 }
137 endfsent();
138 for (i = 0; i < argc; i++)
139 if ((done & (1 << i)) == 0)
140 warnx("%s not found in fstab", argv[i]);
141 exit(errs);
142}

--- 6 unchanged lines hidden (view full) ---

149 "usage: quotaon [-g] [-u] [-v] -a",
150 " quotaon [-g] [-u] [-v] filesystem ...",
151 " quotaoff [-g] [-u] [-v] -a",
152 " quotaoff [-g] [-u] [-v] filesystem ...");
153 exit(1);
154}
155
156int
134 }
135 }
136 endfsent();
137 for (i = 0; i < argc; i++)
138 if ((done & (1 << i)) == 0)
139 warnx("%s not found in fstab", argv[i]);
140 exit(errs);
141}

--- 6 unchanged lines hidden (view full) ---

148 "usage: quotaon [-g] [-u] [-v] -a",
149 " quotaon [-g] [-u] [-v] filesystem ...",
150 " quotaoff [-g] [-u] [-v] -a",
151 " quotaoff [-g] [-u] [-v] filesystem ...");
152 exit(1);
153}
154
155int
157quotaonoff(struct fstab *fs, int offmode, int type, char *qfpathname)
156quotaonoff(struct fstab *fs, int offmode, int type)
158{
157{
158 struct quotafile *qf;
159
159
160 if (strcmp(fs->fs_file, "/") && readonly(fs))
161 return (1);
160 if ((qf = quota_open(fs, type, O_RDONLY)) == NULL)
161 return (0);
162 if (offmode) {
162 if (offmode) {
163 if (quotactl(fs->fs_file, QCMD(Q_QUOTAOFF, type), 0, 0) < 0) {
164 warn("%s", fs->fs_file);
163 if (quota_off(qf) != 0) {
164 warn("%s", quota_fsname(qf));
165 return (1);
166 }
167 if (vflag)
165 return (1);
166 }
167 if (vflag)
168 printf("%s: quotas turned off\n", fs->fs_file);
169 return (0);
168 printf("%s: quotas turned off\n", quota_fsname(qf));
169 quota_close(qf);
170 return(0);
170 }
171 }
171 if (quotactl(fs->fs_file, QCMD(Q_QUOTAON, type), 0, qfpathname) < 0) {
172 warnx("using %s on", qfpathname);
173 warn("%s", fs->fs_file);
172 if (quota_on(qf) != 0) {
173 warn("using %s on %s", quota_qfname(qf), quota_fsname(qf));
174 return (1);
175 }
176 if (vflag)
177 printf("%s: %s quotas turned on with data file %s\n",
174 return (1);
175 }
176 if (vflag)
177 printf("%s: %s quotas turned on with data file %s\n",
178 fs->fs_file, qfextension[type], qfpathname);
179 return (0);
178 quota_fsname(qf), qfextension[type], quota_qfname(qf));
179 quota_close(qf);
180 return(0);
180}
181
182/*
183 * Check to see if target appears in list of size cnt.
184 */
185int
186oneof(char *target, char *list[], int cnt)
187{
188 int i;
189
190 for (i = 0; i < cnt; i++)
191 if (strcmp(target, list[i]) == 0)
192 return (i);
193 return (-1);
194}
181}
182
183/*
184 * Check to see if target appears in list of size cnt.
185 */
186int
187oneof(char *target, char *list[], int cnt)
188{
189 int i;
190
191 for (i = 0; i < cnt; i++)
192 if (strcmp(target, list[i]) == 0)
193 return (i);
194 return (-1);
195}
195
196/*
197 * Check to see if a particular quota is to be enabled.
198 */
199int
200hasquota(struct fstab *fs, int type, char **qfnamep)
201{
202 char *opt;
203 char *cp;
204 struct statfs sfb;
205 static char initname, usrname[100], grpname[100];
206 static char buf[BUFSIZ];
207
208 if (!initname) {
209 (void)snprintf(usrname, sizeof(usrname), "%s%s",
210 qfextension[USRQUOTA], qfname);
211 (void)snprintf(grpname, sizeof(grpname), "%s%s",
212 qfextension[GRPQUOTA], qfname);
213 initname = 1;
214 }
215 strcpy(buf, fs->fs_mntops);
216 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
217 if ((cp = index(opt, '=')))
218 *cp++ = '\0';
219 if (type == USRQUOTA && strcmp(opt, usrname) == 0)
220 break;
221 if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
222 break;
223 }
224 if (!opt)
225 return (0);
226 if (cp)
227 *qfnamep = cp;
228 else {
229 (void)snprintf(buf, sizeof(buf), "%s/%s.%s", fs->fs_file,
230 qfname, qfextension[type]);
231 *qfnamep = buf;
232 }
233 if (statfs(fs->fs_file, &sfb) != 0) {
234 warn("cannot statfs mount point %s", fs->fs_file);
235 return (0);
236 }
237 if (strcmp(fs->fs_file, sfb.f_mntonname)) {
238 warnx("%s not mounted for %s quotas", fs->fs_file,
239 type == USRQUOTA ? "user" : "group");
240 return (0);
241 }
242 return (1);
243}
244
245/*
246 * Verify filesystem is mounted and not readonly.
247 */
248int
249readonly(struct fstab *fs)
250{
251 struct statfs fsbuf;
252
253 if (statfs(fs->fs_file, &fsbuf) < 0 ||
254 strcmp(fsbuf.f_mntonname, fs->fs_file) ||
255 strcmp(fsbuf.f_mntfromname, fs->fs_spec)) {
256 printf("%s: not mounted\n", fs->fs_file);
257 return (1);
258 }
259 if (fsbuf.f_flags & MNT_RDONLY) {
260 printf("%s: mounted read-only\n", fs->fs_file);
261 return (1);
262 }
263 return (0);
264}