]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libutil/quotafile.c
MFC r336017,r338799
[FreeBSD/FreeBSD.git] / lib / libutil / quotafile.c
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$
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
50 struct 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
60 static const char *qfextension[] = INITQFNAMES;
61
62 /*
63  * Check to see if a particular quota is to be enabled.
64  */
65 static int
66 hasquota(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 = strchr(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
112 struct quotafile *
113 quota_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 ((qf = calloc(1, sizeof(*qf))) == NULL)
122                 return (NULL);
123         qf->fd = -1;
124         qf->quotatype = quotatype;
125         strlcpy(qf->fsname, fs->fs_file, sizeof(qf->fsname));
126         if (stat(qf->fsname, &st) != 0)
127                 goto error;
128         qf->dev = st.st_dev;
129         qcmd = QCMD(Q_GETQUOTASIZE, quotatype);
130         if (quotactl(qf->fsname, qcmd, 0, &qf->wordsize) == 0)
131                 return (qf);
132         /* We only check the quota file for ufs */
133         if (strcmp(fs->fs_vfstype, "ufs")) {
134                 errno = 0;
135                 goto error;
136         }
137         serrno = hasquota(fs, quotatype, qf->qfname, sizeof(qf->qfname));
138         if (serrno == 0) {
139                 errno = EOPNOTSUPP;
140                 goto error;
141         }
142         qf->accmode = openflags & O_ACCMODE;
143         if ((qf->fd = open(qf->qfname, qf->accmode|O_CLOEXEC)) < 0 &&
144             (openflags & O_CREAT) != O_CREAT)
145                 goto error;
146         /* File open worked, so process it */
147         if (qf->fd != -1) {
148                 qf->wordsize = 32;
149                 switch (read(qf->fd, &dqh, sizeof(dqh))) {
150                 case -1:
151                         goto error;
152                 case sizeof(dqh):
153                         if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) != 0) {
154                                 /* no magic, assume 32 bits */
155                                 qf->wordsize = 32;
156                                 return (qf);
157                         }
158                         if (be32toh(dqh.dqh_version) != Q_DQHDR64_VERSION ||
159                             be32toh(dqh.dqh_hdrlen) != sizeof(struct dqhdr64) ||
160                             be32toh(dqh.dqh_reclen) != sizeof(struct dqblk64)) {
161                                 /* correct magic, wrong version / lengths */
162                                 errno = EINVAL;
163                                 goto error;
164                         }
165                         qf->wordsize = 64;
166                         return (qf);
167                 default:
168                         qf->wordsize = 32;
169                         return (qf);
170                 }
171                 /* not reached */
172         }
173         /* open failed, but O_CREAT was specified, so create a new file */
174         if ((qf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC, 0)) <
175             0)
176                 goto error;
177         qf->wordsize = 64;
178         memset(&dqh, 0, sizeof(dqh));
179         memcpy(dqh.dqh_magic, Q_DQHDR64_MAGIC, sizeof(dqh.dqh_magic));
180         dqh.dqh_version = htobe32(Q_DQHDR64_VERSION);
181         dqh.dqh_hdrlen = htobe32(sizeof(struct dqhdr64));
182         dqh.dqh_reclen = htobe32(sizeof(struct dqblk64));
183         if (write(qf->fd, &dqh, sizeof(dqh)) != sizeof(dqh)) {
184                 /* it was one we created ourselves */
185                 unlink(qf->qfname);
186                 goto error;
187         }
188         grp = getgrnam(QUOTAGROUP);
189         fchown(qf->fd, 0, grp ? grp->gr_gid : 0);
190         fchmod(qf->fd, 0640);
191         return (qf);
192 error:
193         serrno = errno;
194         /* did we have an open file? */
195         if (qf->fd != -1)
196                 close(qf->fd);
197         free(qf);
198         errno = serrno;
199         return (NULL);
200 }
201
202 void
203 quota_close(struct quotafile *qf)
204 {
205
206         if (qf->fd != -1)
207                 close(qf->fd);
208         free(qf);
209 }
210
211 int
212 quota_on(struct quotafile *qf)
213 {
214         int qcmd;
215
216         qcmd = QCMD(Q_QUOTAON, qf->quotatype);
217         return (quotactl(qf->fsname, qcmd, 0, qf->qfname));
218 }
219
220 int
221 quota_off(struct quotafile *qf)
222 {
223
224         return (quotactl(qf->fsname, QCMD(Q_QUOTAOFF, qf->quotatype), 0, 0));
225 }
226
227 const char *
228 quota_fsname(const struct quotafile *qf)
229 {
230
231         return (qf->fsname);
232 }
233
234 const char *
235 quota_qfname(const struct quotafile *qf)
236 {
237
238         return (qf->qfname);
239 }
240
241 int
242 quota_check_path(const struct quotafile *qf, const char *path)
243 {
244         struct stat st;
245
246         if (stat(path, &st) == -1)
247                 return (-1);
248         return (st.st_dev == qf->dev);
249 }
250
251 int
252 quota_maxid(struct quotafile *qf)
253 {
254         struct stat st;
255         int maxid;
256
257         if (stat(qf->qfname, &st) < 0)
258                 return (0);
259         switch (qf->wordsize) {
260         case 32:
261                 maxid = st.st_size / sizeof(struct dqblk32) - 1;
262                 break;
263         case 64:
264                 maxid = st.st_size / sizeof(struct dqblk64) - 2;
265                 break;
266         default:
267                 maxid = 0;
268                 break;
269         }
270         return (maxid > 0 ? maxid : 0);
271 }
272
273 static int
274 quota_read32(struct quotafile *qf, struct dqblk *dqb, int id)
275 {
276         struct dqblk32 dqb32;
277         off_t off;
278
279         off = id * sizeof(struct dqblk32);
280         if (lseek(qf->fd, off, SEEK_SET) == -1)
281                 return (-1);
282         switch (read(qf->fd, &dqb32, sizeof(dqb32))) {
283         case 0:
284                 memset(dqb, 0, sizeof(*dqb));
285                 return (0);
286         case sizeof(dqb32):
287                 dqb->dqb_bhardlimit = dqb32.dqb_bhardlimit;
288                 dqb->dqb_bsoftlimit = dqb32.dqb_bsoftlimit;
289                 dqb->dqb_curblocks = dqb32.dqb_curblocks;
290                 dqb->dqb_ihardlimit = dqb32.dqb_ihardlimit;
291                 dqb->dqb_isoftlimit = dqb32.dqb_isoftlimit;
292                 dqb->dqb_curinodes = dqb32.dqb_curinodes;
293                 dqb->dqb_btime = dqb32.dqb_btime;
294                 dqb->dqb_itime = dqb32.dqb_itime;
295                 return (0);
296         default:
297                 return (-1);
298         }
299 }
300
301 static int
302 quota_read64(struct quotafile *qf, struct dqblk *dqb, int id)
303 {
304         struct dqblk64 dqb64;
305         off_t off;
306
307         off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64);
308         if (lseek(qf->fd, off, SEEK_SET) == -1)
309                 return (-1);
310         switch (read(qf->fd, &dqb64, sizeof(dqb64))) {
311         case 0:
312                 memset(dqb, 0, sizeof(*dqb));
313                 return (0);
314         case sizeof(dqb64):
315                 dqb->dqb_bhardlimit = be64toh(dqb64.dqb_bhardlimit);
316                 dqb->dqb_bsoftlimit = be64toh(dqb64.dqb_bsoftlimit);
317                 dqb->dqb_curblocks = be64toh(dqb64.dqb_curblocks);
318                 dqb->dqb_ihardlimit = be64toh(dqb64.dqb_ihardlimit);
319                 dqb->dqb_isoftlimit = be64toh(dqb64.dqb_isoftlimit);
320                 dqb->dqb_curinodes = be64toh(dqb64.dqb_curinodes);
321                 dqb->dqb_btime = be64toh(dqb64.dqb_btime);
322                 dqb->dqb_itime = be64toh(dqb64.dqb_itime);
323                 return (0);
324         default:
325                 return (-1);
326         }
327 }
328
329 int
330 quota_read(struct quotafile *qf, struct dqblk *dqb, int id)
331 {
332         int qcmd;
333
334         if (qf->fd == -1) {
335                 qcmd = QCMD(Q_GETQUOTA, qf->quotatype);
336                 return (quotactl(qf->fsname, qcmd, id, dqb));
337         }
338         switch (qf->wordsize) {
339         case 32:
340                 return (quota_read32(qf, dqb, id));
341         case 64:
342                 return (quota_read64(qf, dqb, id));
343         default:
344                 errno = EINVAL;
345                 return (-1);
346         }
347         /* not reached */
348 }
349
350 #define CLIP32(u64) ((u64) > UINT32_MAX ? UINT32_MAX : (uint32_t)(u64))
351
352 static int
353 quota_write32(struct quotafile *qf, const struct dqblk *dqb, int id)
354 {
355         struct dqblk32 dqb32;
356         off_t off;
357
358         dqb32.dqb_bhardlimit = CLIP32(dqb->dqb_bhardlimit);
359         dqb32.dqb_bsoftlimit = CLIP32(dqb->dqb_bsoftlimit);
360         dqb32.dqb_curblocks = CLIP32(dqb->dqb_curblocks);
361         dqb32.dqb_ihardlimit = CLIP32(dqb->dqb_ihardlimit);
362         dqb32.dqb_isoftlimit = CLIP32(dqb->dqb_isoftlimit);
363         dqb32.dqb_curinodes = CLIP32(dqb->dqb_curinodes);
364         dqb32.dqb_btime = CLIP32(dqb->dqb_btime);
365         dqb32.dqb_itime = CLIP32(dqb->dqb_itime);
366
367         off = id * sizeof(struct dqblk32);
368         if (lseek(qf->fd, off, SEEK_SET) == -1)
369                 return (-1);
370         if (write(qf->fd, &dqb32, sizeof(dqb32)) == sizeof(dqb32))
371                 return (0);
372         return (-1);
373 }
374
375 static int
376 quota_write64(struct quotafile *qf, const struct dqblk *dqb, int id)
377 {
378         struct dqblk64 dqb64;
379         off_t off;
380
381         dqb64.dqb_bhardlimit = htobe64(dqb->dqb_bhardlimit);
382         dqb64.dqb_bsoftlimit = htobe64(dqb->dqb_bsoftlimit);
383         dqb64.dqb_curblocks = htobe64(dqb->dqb_curblocks);
384         dqb64.dqb_ihardlimit = htobe64(dqb->dqb_ihardlimit);
385         dqb64.dqb_isoftlimit = htobe64(dqb->dqb_isoftlimit);
386         dqb64.dqb_curinodes = htobe64(dqb->dqb_curinodes);
387         dqb64.dqb_btime = htobe64(dqb->dqb_btime);
388         dqb64.dqb_itime = htobe64(dqb->dqb_itime);
389
390         off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64);
391         if (lseek(qf->fd, off, SEEK_SET) == -1)
392                 return (-1);
393         if (write(qf->fd, &dqb64, sizeof(dqb64)) == sizeof(dqb64))
394                 return (0);
395         return (-1);
396 }
397
398 int
399 quota_write_usage(struct quotafile *qf, struct dqblk *dqb, int id)
400 {
401         struct dqblk dqbuf;
402         int qcmd;
403
404         if (qf->fd == -1) {
405                 qcmd = QCMD(Q_SETUSE, qf->quotatype);
406                 return (quotactl(qf->fsname, qcmd, id, dqb));
407         }
408         /*
409          * Have to do read-modify-write of quota in file.
410          */
411         if ((qf->accmode & O_RDWR) != O_RDWR) {
412                 errno = EBADF;
413                 return (-1);
414         }
415         if (quota_read(qf, &dqbuf, id) != 0)
416                 return (-1);
417         /*
418          * Reset time limit if have a soft limit and were
419          * previously under it, but are now over it.
420          */
421         if (dqbuf.dqb_bsoftlimit && id != 0 &&
422             dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
423             dqb->dqb_curblocks >= dqbuf.dqb_bsoftlimit)
424                 dqbuf.dqb_btime = 0;
425         if (dqbuf.dqb_isoftlimit && id != 0 &&
426             dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit &&
427             dqb->dqb_curinodes >= dqbuf.dqb_isoftlimit)
428                 dqbuf.dqb_itime = 0;
429         dqbuf.dqb_curinodes = dqb->dqb_curinodes;
430         dqbuf.dqb_curblocks = dqb->dqb_curblocks;
431         /*
432          * Write it back.
433          */
434         switch (qf->wordsize) {
435         case 32:
436                 return (quota_write32(qf, &dqbuf, id));
437         case 64:
438                 return (quota_write64(qf, &dqbuf, id));
439         default:
440                 errno = EINVAL;
441                 return (-1);
442         }
443         /* not reached */
444 }
445
446 int
447 quota_write_limits(struct quotafile *qf, struct dqblk *dqb, int id)
448 {
449         struct dqblk dqbuf;
450         int qcmd;
451
452         if (qf->fd == -1) {
453                 qcmd = QCMD(Q_SETQUOTA, qf->quotatype);
454                 return (quotactl(qf->fsname, qcmd, id, dqb));
455         }
456         /*
457          * Have to do read-modify-write of quota in file.
458          */
459         if ((qf->accmode & O_RDWR) != O_RDWR) {
460                 errno = EBADF;
461                 return (-1);
462         }
463         if (quota_read(qf, &dqbuf, id) != 0)
464                 return (-1);
465         /*
466          * Reset time limit if have a soft limit and were
467          * previously under it, but are now over it
468          * or if there previously was no soft limit, but
469          * now have one and are over it.
470          */
471         if (dqbuf.dqb_bsoftlimit && id != 0 &&
472             dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit &&
473             dqbuf.dqb_curblocks >= dqb->dqb_bsoftlimit)
474                 dqb->dqb_btime = 0;
475         if (dqbuf.dqb_bsoftlimit == 0 && id != 0 &&
476             dqb->dqb_bsoftlimit > 0 &&
477             dqbuf.dqb_curblocks >= dqb->dqb_bsoftlimit)
478                 dqb->dqb_btime = 0;
479         if (dqbuf.dqb_isoftlimit && id != 0 &&
480             dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit &&
481             dqbuf.dqb_curinodes >= dqb->dqb_isoftlimit)
482                 dqb->dqb_itime = 0;
483         if (dqbuf.dqb_isoftlimit == 0 && id !=0 &&
484             dqb->dqb_isoftlimit > 0 &&
485             dqbuf.dqb_curinodes >= dqb->dqb_isoftlimit)
486                 dqb->dqb_itime = 0;
487         dqb->dqb_curinodes = dqbuf.dqb_curinodes;
488         dqb->dqb_curblocks = dqbuf.dqb_curblocks;
489         /*
490          * Write it back.
491          */
492         switch (qf->wordsize) {
493         case 32:
494                 return (quota_write32(qf, dqb, id));
495         case 64:
496                 return (quota_write64(qf, dqb, id));
497         default:
498                 errno = EINVAL;
499                 return (-1);
500         }
501         /* not reached */
502 }
503
504 /*
505  * Convert a quota file from one format to another.
506  */
507 int
508 quota_convert(struct quotafile *qf, int wordsize)
509 {
510         struct quotafile *newqf;
511         struct dqhdr64 dqh;
512         struct dqblk dqblk;
513         struct group *grp;
514         int serrno, maxid, id, fd;
515
516         /*
517          * Quotas must not be active and quotafile must be open
518          * for reading and writing.
519          */
520         if ((qf->accmode & O_RDWR) != O_RDWR || qf->fd == -1) {
521                 errno = EBADF;
522                 return (-1);
523         }
524         if ((wordsize != 32 && wordsize != 64) ||
525              wordsize == qf->wordsize) {
526                 errno = EINVAL;
527                 return (-1);
528         }
529         maxid = quota_maxid(qf);
530         if ((newqf = calloc(1, sizeof(*qf))) == NULL) {
531                 errno = ENOMEM;
532                 return (-1);
533         }
534         *newqf = *qf;
535         snprintf(newqf->qfname, MAXPATHLEN + 1, "%s_%d.orig", qf->qfname,
536             qf->wordsize);
537         if (rename(qf->qfname, newqf->qfname) < 0) {
538                 free(newqf);
539                 return (-1);
540         }
541         if ((newqf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC|O_CLOEXEC,
542             0)) < 0) {
543                 serrno = errno;
544                 goto error;
545         }
546         newqf->wordsize = wordsize;
547         if (wordsize == 64) {
548                 memset(&dqh, 0, sizeof(dqh));
549                 memcpy(dqh.dqh_magic, Q_DQHDR64_MAGIC, sizeof(dqh.dqh_magic));
550                 dqh.dqh_version = htobe32(Q_DQHDR64_VERSION);
551                 dqh.dqh_hdrlen = htobe32(sizeof(struct dqhdr64));
552                 dqh.dqh_reclen = htobe32(sizeof(struct dqblk64));
553                 if (write(newqf->fd, &dqh, sizeof(dqh)) != sizeof(dqh)) {
554                         serrno = errno;
555                         goto error;
556                 }
557         }
558         grp = getgrnam(QUOTAGROUP);
559         fchown(newqf->fd, 0, grp ? grp->gr_gid : 0);
560         fchmod(newqf->fd, 0640);
561         for (id = 0; id <= maxid; id++) {
562                 if ((quota_read(qf, &dqblk, id)) < 0)
563                         break;
564                 switch (newqf->wordsize) {
565                 case 32:
566                         if ((quota_write32(newqf, &dqblk, id)) < 0)
567                                 break;
568                         continue;
569                 case 64:
570                         if ((quota_write64(newqf, &dqblk, id)) < 0)
571                                 break;
572                         continue;
573                 default:
574                         errno = EINVAL;
575                         break;
576                 }
577         }
578         if (id < maxid) {
579                 serrno = errno;
580                 goto error;
581         }
582         /*
583          * Update the passed in quotafile to reference the new file
584          * of the converted format size.
585          */
586         fd = qf->fd;
587         qf->fd = newqf->fd;
588         newqf->fd = fd;
589         qf->wordsize = newqf->wordsize;
590         quota_close(newqf);
591         return (0);
592 error:
593         /* put back the original file */
594         (void) rename(newqf->qfname, qf->qfname);
595         quota_close(newqf);
596         errno = serrno;
597         return (-1);
598 }