quotafile.c revision 201144
1/*-
2 * Copyright (c) 2008 Dag-Erling Co��dan Sm��rgrav
3 * Copyright (c) 2008 Marshall Kirk McKusick
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer
11 *    in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: projects/quota64/lib/libutil/quotafile.c 201144 2009-12-28 22:44:19Z mckusick $
29 */
30
31#include <sys/types.h>
32#include <sys/endian.h>
33#include <sys/mount.h>
34#include <sys/stat.h>
35
36#include <ufs/ufs/quota.h>
37
38#include <errno.h>
39#include <fcntl.h>
40#include <fstab.h>
41#include <grp.h>
42#include <pwd.h>
43#include <libutil.h>
44#include <stdint.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include <unistd.h>
49
50struct quotafile {
51	int fd;				/* -1 means using quotactl for access */
52	int accmode;			/* access mode */
53	int wordsize;			/* 32-bit or 64-bit limits */
54	int quotatype;			/* USRQUOTA or GRPQUOTA */
55	dev_t dev;			/* device */
56	char fsname[MAXPATHLEN + 1];	/* mount point of filesystem */
57	char qfname[MAXPATHLEN + 1];	/* quota file if not using quotactl */
58};
59
60static const char *qfextension[] = INITQFNAMES;
61
62/*
63 * Check to see if a particular quota is to be enabled.
64 */
65static int
66hasquota(struct fstab *fs, int type, char *qfnamep, int qfbufsize)
67{
68	char *opt;
69	char *cp;
70	struct statfs sfb;
71	char buf[BUFSIZ];
72	static char initname, usrname[100], grpname[100];
73
74	/*
75	 * 1) we only need one of these
76	 * 2) fstab may specify a different filename
77	 */
78	if (!initname) {
79		(void)snprintf(usrname, sizeof(usrname), "%s%s",
80		    qfextension[USRQUOTA], QUOTAFILENAME);
81		(void)snprintf(grpname, sizeof(grpname), "%s%s",
82		    qfextension[GRPQUOTA], QUOTAFILENAME);
83		initname = 1;
84	}
85	strcpy(buf, fs->fs_mntops);
86	for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) {
87		if ((cp = index(opt, '=')))
88			*cp++ = '\0';
89		if (type == USRQUOTA && strcmp(opt, usrname) == 0)
90			break;
91		if (type == GRPQUOTA && strcmp(opt, grpname) == 0)
92			break;
93	}
94	if (!opt)
95		return (0);
96	/*
97	 * Ensure that the filesystem is mounted.
98	 */
99	if (statfs(fs->fs_file, &sfb) != 0 ||
100	    strcmp(fs->fs_file, sfb.f_mntonname)) {
101		return (0);
102	}
103	if (cp) {
104		strncpy(qfnamep, cp, qfbufsize);
105	} else {
106		(void)snprintf(qfnamep, qfbufsize, "%s/%s.%s", fs->fs_file,
107		    QUOTAFILENAME, qfextension[type]);
108	}
109	return (1);
110}
111
112struct quotafile *
113quota_open(struct fstab *fs, int quotatype, int openflags)
114{
115	struct quotafile *qf;
116	struct dqhdr64 dqh;
117	struct group *grp;
118	struct stat st;
119	int qcmd, serrno;
120
121	if (strcmp(fs->fs_vfstype, "ufs"))
122		return (NULL);
123	if ((qf = calloc(1, sizeof(*qf))) == NULL)
124		return (NULL);
125	qf->fd = -1;
126	qf->quotatype = quotatype;
127	strncpy(qf->fsname, fs->fs_file, sizeof(qf->fsname));
128	if (stat(qf->fsname, &st) != 0)
129		goto error;
130	qf->dev = st.st_dev;
131	serrno = hasquota(fs, quotatype, qf->qfname, sizeof(qf->qfname));
132	qcmd = QCMD(Q_GETQUOTA, quotatype);
133	if (quotactl(fs->fs_file, qcmd, 0, &dqh) == 0) {
134		qf->wordsize = 64;
135		return (qf);
136	}
137	if (serrno == 0) {
138		errno = EOPNOTSUPP;
139		goto error;
140	}
141	qf->accmode = openflags & O_ACCMODE;
142	if ((qf->fd = open(qf->qfname, qf->accmode)) < 0 &&
143	    (openflags & O_CREAT) != O_CREAT)
144		goto error;
145	/* File open worked, so process it */
146	if (qf->fd != -1) {
147		qf->wordsize = 32;
148		switch (read(qf->fd, &dqh, sizeof(dqh))) {
149		case -1:
150			goto error;
151		case sizeof(dqh):
152			if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) != 0) {
153				/* no magic, assume 32 bits */
154				qf->wordsize = 32;
155				return (qf);
156			}
157			if (be32toh(dqh.dqh_version) != Q_DQHDR64_VERSION ||
158			    be32toh(dqh.dqh_hdrlen) != sizeof(struct dqhdr64) ||
159			    be32toh(dqh.dqh_reclen) != sizeof(struct dqblk64)) {
160				/* correct magic, wrong version / lengths */
161				errno = EINVAL;
162				goto error;
163			}
164			qf->wordsize = 64;
165			return (qf);
166		default:
167			qf->wordsize = 32;
168			return (qf);
169		}
170		/* not reached */
171	}
172	/* open failed, but O_CREAT was specified, so create a new file */
173	if ((qf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC, 0)) < 0)
174		goto error;
175	qf->wordsize = 64;
176	memset(&dqh, 0, sizeof(dqh));
177	memcpy(dqh.dqh_magic, Q_DQHDR64_MAGIC, sizeof(dqh.dqh_magic));
178	dqh.dqh_version = htobe32(Q_DQHDR64_VERSION);
179	dqh.dqh_hdrlen = htobe32(sizeof(struct dqhdr64));
180	dqh.dqh_reclen = htobe32(sizeof(struct dqblk64));
181	if (write(qf->fd, &dqh, sizeof(dqh)) != sizeof(dqh)) {
182		/* it was one we created ourselves */
183		unlink(qf->qfname);
184		goto error;
185	}
186	grp = getgrnam(QUOTAGROUP);
187	fchown(qf->fd, 0, grp ? grp->gr_gid : 0);
188	fchmod(qf->fd, 0640);
189	return (qf);
190error:
191	serrno = errno;
192	/* did we have an open file? */
193	if (qf->fd != -1)
194		close(qf->fd);
195	free(qf);
196	errno = serrno;
197	return (NULL);
198}
199
200void
201quota_close(struct quotafile *qf)
202{
203
204	if (qf->fd != -1)
205		close(qf->fd);
206	free(qf);
207}
208
209int
210quota_on(struct quotafile *qf)
211{
212	int qcmd;
213
214	qcmd = QCMD(Q_QUOTAON, qf->quotatype);
215	return (quotactl(qf->fsname, qcmd, 0, qf->qfname));
216}
217
218int
219quota_off(struct quotafile *qf)
220{
221
222	return (quotactl(qf->fsname, QCMD(Q_QUOTAOFF, qf->quotatype), 0, 0));
223}
224
225const char *
226quota_fsname(const struct quotafile *qf)
227{
228
229	return (qf->fsname);
230}
231
232const char *
233quota_qfname(const struct quotafile *qf)
234{
235
236	return (qf->qfname);
237}
238
239int
240quota_check_path(const struct quotafile *qf, const char *path)
241{
242	struct stat st;
243
244	if (stat(path, &st) == -1)
245		return (-1);
246	return (st.st_dev == qf->dev);
247}
248
249int
250quota_maxid(struct quotafile *qf)
251{
252	struct stat st;
253
254	if (stat(qf->qfname, &st) < 0)
255		return (0);
256	switch (qf->wordsize) {
257	case 32:
258		return (st.st_size / sizeof(struct dqblk32) - 1);
259	case 64:
260		return (st.st_size / sizeof(struct dqblk64) - 2);
261	default:
262		return (0);
263	}
264	/* not reached */
265}
266
267static int
268quota_read32(struct quotafile *qf, struct dqblk *dqb, int id)
269{
270	struct dqblk32 dqb32;
271	off_t off;
272
273	off = id * sizeof(struct dqblk32);
274	if (lseek(qf->fd, off, SEEK_SET) == -1)
275		return (-1);
276	switch (read(qf->fd, &dqb32, sizeof(dqb32))) {
277	case 0:
278		memset(dqb, 0, sizeof(*dqb));
279		return (0);
280	case sizeof(dqb32):
281		dqb->dqb_bhardlimit = dqb32.dqb_bhardlimit;
282		dqb->dqb_bsoftlimit = dqb32.dqb_bsoftlimit;
283		dqb->dqb_curblocks = dqb32.dqb_curblocks;
284		dqb->dqb_ihardlimit = dqb32.dqb_ihardlimit;
285		dqb->dqb_isoftlimit = dqb32.dqb_isoftlimit;
286		dqb->dqb_curinodes = dqb32.dqb_curinodes;
287		dqb->dqb_btime = dqb32.dqb_btime;
288		dqb->dqb_itime = dqb32.dqb_itime;
289		return (0);
290	default:
291		return (-1);
292	}
293}
294
295static int
296quota_read64(struct quotafile *qf, struct dqblk *dqb, int id)
297{
298	struct dqblk64 dqb64;
299	off_t off;
300
301	off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64);
302	if (lseek(qf->fd, off, SEEK_SET) == -1)
303		return (-1);
304	switch (read(qf->fd, &dqb64, sizeof(dqb64))) {
305	case 0:
306		memset(dqb, 0, sizeof(*dqb));
307		return (0);
308	case sizeof(dqb64):
309		dqb->dqb_bhardlimit = be64toh(dqb64.dqb_bhardlimit);
310		dqb->dqb_bsoftlimit = be64toh(dqb64.dqb_bsoftlimit);
311		dqb->dqb_curblocks = be64toh(dqb64.dqb_curblocks);
312		dqb->dqb_ihardlimit = be64toh(dqb64.dqb_ihardlimit);
313		dqb->dqb_isoftlimit = be64toh(dqb64.dqb_isoftlimit);
314		dqb->dqb_curinodes = be64toh(dqb64.dqb_curinodes);
315		dqb->dqb_btime = be64toh(dqb64.dqb_btime);
316		dqb->dqb_itime = be64toh(dqb64.dqb_itime);
317		return (0);
318	default:
319		return (-1);
320	}
321}
322
323int
324quota_read(struct quotafile *qf, struct dqblk *dqb, int id)
325{
326	int qcmd;
327
328	if (qf->fd == -1) {
329		qcmd = QCMD(Q_GETQUOTA, qf->quotatype);
330		return (quotactl(qf->fsname, qcmd, id, dqb));
331	}
332	switch (qf->wordsize) {
333	case 32:
334		return (quota_read32(qf, dqb, id));
335	case 64:
336		return (quota_read64(qf, dqb, id));
337	default:
338		errno = EINVAL;
339		return (-1);
340	}
341	/* not reached */
342}
343
344#define CLIP32(u64) ((u64) > UINT32_MAX ? UINT32_MAX : (uint32_t)(u64))
345
346static int
347quota_write32(struct quotafile *qf, const struct dqblk *dqb, int id)
348{
349	struct dqblk32 dqb32;
350	off_t off;
351
352	dqb32.dqb_bhardlimit = CLIP32(dqb->dqb_bhardlimit);
353	dqb32.dqb_bsoftlimit = CLIP32(dqb->dqb_bsoftlimit);
354	dqb32.dqb_curblocks = CLIP32(dqb->dqb_curblocks);
355	dqb32.dqb_ihardlimit = CLIP32(dqb->dqb_ihardlimit);
356	dqb32.dqb_isoftlimit = CLIP32(dqb->dqb_isoftlimit);
357	dqb32.dqb_curinodes = CLIP32(dqb->dqb_curinodes);
358	dqb32.dqb_btime = CLIP32(dqb->dqb_btime);
359	dqb32.dqb_itime = CLIP32(dqb->dqb_itime);
360
361	off = id * sizeof(struct dqblk32);
362	if (lseek(qf->fd, off, SEEK_SET) == -1)
363		return (-1);
364	if (write(qf->fd, &dqb32, sizeof(dqb32)) == sizeof(dqb32))
365		return (0);
366	return (-1);
367}
368
369static int
370quota_write64(struct quotafile *qf, const struct dqblk *dqb, int id)
371{
372	struct dqblk64 dqb64;
373	off_t off;
374
375	dqb64.dqb_bhardlimit = htobe64(dqb->dqb_bhardlimit);
376	dqb64.dqb_bsoftlimit = htobe64(dqb->dqb_bsoftlimit);
377	dqb64.dqb_curblocks = htobe64(dqb->dqb_curblocks);
378	dqb64.dqb_ihardlimit = htobe64(dqb->dqb_ihardlimit);
379	dqb64.dqb_isoftlimit = htobe64(dqb->dqb_isoftlimit);
380	dqb64.dqb_curinodes = htobe64(dqb->dqb_curinodes);
381	dqb64.dqb_btime = htobe64(dqb->dqb_btime);
382	dqb64.dqb_itime = htobe64(dqb->dqb_itime);
383
384	off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64);
385	if (lseek(qf->fd, off, SEEK_SET) == -1)
386		return (-1);
387	if (write(qf->fd, &dqb64, sizeof(dqb64)) == sizeof(dqb64))
388		return (0);
389	return (-1);
390}
391
392int
393quota_write_usage(struct quotafile *qf, struct dqblk *dqb, int id)
394{
395	struct dqblk dqbuf;
396	int qcmd;
397
398	if ((qf->accmode & O_RDWR) != O_RDWR) {
399		errno = EBADF;
400		return (-1);
401	}
402	if (qf->fd == -1) {
403		qcmd = QCMD(Q_SETUSE, qf->quotatype);
404		return (quotactl(qf->fsname, qcmd, id, dqb));
405	}
406	/*
407	 * Have to do read-modify-write of quota in file.
408	 */
409	if (quota_read(qf, &dqbuf, id) != 0)
410		return (-1);
411	/*
412	 * Reset time limit if have a soft limit and were
413	 * previously under it, but are now over it.
414	 */
415	if (dqbuf.dqb_bsoftlimit && id != 0 &&
416	    dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
417	    dqb->dqb_curblocks >= dqbuf.dqb_bsoftlimit)
418		dqbuf.dqb_btime = 0;
419	if (dqbuf.dqb_isoftlimit && id != 0 &&
420	    dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit &&
421	    dqb->dqb_curinodes >= dqbuf.dqb_isoftlimit)
422		dqbuf.dqb_itime = 0;
423	dqbuf.dqb_curinodes = dqb->dqb_curinodes;
424	dqbuf.dqb_curblocks = dqb->dqb_curblocks;
425	/*
426	 * Write it back.
427	 */
428	switch (qf->wordsize) {
429	case 32:
430		return (quota_write32(qf, &dqbuf, id));
431	case 64:
432		return (quota_write64(qf, &dqbuf, id));
433	default:
434		errno = EINVAL;
435		return (-1);
436	}
437	/* not reached */
438}
439
440int
441quota_write_limits(struct quotafile *qf, struct dqblk *dqb, int id)
442{
443	struct dqblk dqbuf;
444	int qcmd;
445
446	if ((qf->accmode & O_RDWR) != O_RDWR) {
447		errno = EBADF;
448		return (-1);
449	}
450	if (qf->fd == -1) {
451		qcmd = QCMD(Q_SETQUOTA, qf->quotatype);
452		return (quotactl(qf->fsname, qcmd, id, dqb));
453	}
454	/*
455	 * Have to do read-modify-write of quota in file.
456	 */
457	if (quota_read(qf, &dqbuf, id) != 0)
458		return (-1);
459	/*
460	 * Reset time limit if have a soft limit and were
461	 * previously under it, but are now over it
462	 * or if there previously was no soft limit, but
463	 * now have one and are over it.
464	 */
465	if (dqbuf.dqb_bsoftlimit && id != 0 &&
466	    dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
467	    dqbuf.dqb_curblocks >= dqb->dqb_bsoftlimit)
468		dqb->dqb_btime = 0;
469	if (dqbuf.dqb_bsoftlimit == 0 && id != 0 &&
470	    dqb->dqb_bsoftlimit > 0 &&
471	    dqbuf.dqb_curblocks >= dqb->dqb_bsoftlimit)
472		dqb->dqb_btime = 0;
473	if (dqbuf.dqb_isoftlimit && id != 0 &&
474	    dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit &&
475	    dqbuf.dqb_curinodes >= dqb->dqb_isoftlimit)
476		dqb->dqb_itime = 0;
477	if (dqbuf.dqb_isoftlimit == 0 && id !=0 &&
478	    dqb->dqb_isoftlimit > 0 &&
479	    dqbuf.dqb_curinodes >= dqb->dqb_isoftlimit)
480		dqb->dqb_itime = 0;
481	dqb->dqb_curinodes = dqbuf.dqb_curinodes;
482	dqb->dqb_curblocks = dqbuf.dqb_curblocks;
483	/*
484	 * Write it back.
485	 */
486	switch (qf->wordsize) {
487	case 32:
488		return (quota_write32(qf, dqb, id));
489	case 64:
490		return (quota_write64(qf, dqb, id));
491	default:
492		errno = EINVAL;
493		return (-1);
494	}
495	/* not reached */
496}
497
498/*
499 * Convert a quota file from one format to another.
500 */
501int
502quota_convert(struct quotafile *qf, int wordsize)
503{
504	struct quotafile *newqf;
505	struct dqhdr64 dqh;
506	struct dqblk dqblk;
507	struct group *grp;
508	int serrno, maxid, id, fd;
509
510	/*
511	 * Quotas must not be active and quotafile must be open
512	 * for reading and writing.
513	 */
514	if ((qf->accmode & O_RDWR) != O_RDWR || qf->fd == -1) {
515		errno = EBADF;
516		return (-1);
517	}
518	if ((wordsize != 32 && wordsize != 64) ||
519	     wordsize == qf->wordsize) {
520		errno = EINVAL;
521		return (-1);
522	}
523	maxid = quota_maxid(qf);
524	if ((newqf = calloc(1, sizeof(*qf))) == NULL) {
525		errno = ENOMEM;
526		return (-1);
527	}
528	*newqf = *qf;
529	snprintf(newqf->qfname, MAXPATHLEN + 1, "%s_%d.orig", qf->qfname,
530	    qf->wordsize);
531	if (rename(qf->qfname, newqf->qfname) < 0) {
532		free(newqf);
533		return (-1);
534	}
535	if ((newqf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC, 0)) < 0) {
536		serrno = errno;
537		goto error;
538	}
539	newqf->wordsize = wordsize;
540	if (wordsize == 64) {
541		memset(&dqh, 0, sizeof(dqh));
542		memcpy(dqh.dqh_magic, Q_DQHDR64_MAGIC, sizeof(dqh.dqh_magic));
543		dqh.dqh_version = htobe32(Q_DQHDR64_VERSION);
544		dqh.dqh_hdrlen = htobe32(sizeof(struct dqhdr64));
545		dqh.dqh_reclen = htobe32(sizeof(struct dqblk64));
546		if (write(newqf->fd, &dqh, sizeof(dqh)) != sizeof(dqh)) {
547			serrno = errno;
548			goto error;
549		}
550	}
551	grp = getgrnam(QUOTAGROUP);
552	fchown(newqf->fd, 0, grp ? grp->gr_gid : 0);
553	fchmod(newqf->fd, 0640);
554	for (id = 0; id <= maxid; id++) {
555		if ((quota_read(qf, &dqblk, id)) < 0)
556			break;
557		switch (newqf->wordsize) {
558		case 32:
559			if ((quota_write32(newqf, &dqblk, id)) < 0)
560				break;
561			continue;
562		case 64:
563			if ((quota_write64(newqf, &dqblk, id)) < 0)
564				break;
565			continue;
566		default:
567			errno = EINVAL;
568			break;
569		}
570	}
571	if (id < maxid) {
572		serrno = errno;
573		goto error;
574	}
575	/*
576	 * Update the passed in quotafile to reference the new file
577	 * of the converted format size.
578	 */
579	fd = qf->fd;
580	qf->fd = newqf->fd;
581	newqf->fd = fd;
582	qf->wordsize = newqf->wordsize;
583	quota_close(newqf);
584	return (0);
585error:
586	/* put back the original file */
587	(void) rename(newqf->qfname, qf->qfname);
588	quota_close(newqf);
589	errno = serrno;
590	return (-1);
591}
592