quotafile.c revision 188604
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 188604 2009-02-14 08:08:08Z 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 wordsize; /* 32-bit or 64-bit limits */ 53 int quotatype; /* USRQUOTA or GRPQUOTA */ 54 char fsname[MAXPATHLEN + 1]; /* mount point of filesystem */ 55 char qfname[MAXPATHLEN + 1]; /* quota file if not using quotactl */ 56}; 57 58static const char *qfextension[] = INITQFNAMES; 59 60/* 61 * Check to see if a particular quota is to be enabled. 62 */ 63static int 64hasquota(struct fstab *fs, int type, char *qfnamep, int qfbufsize) 65{ 66 char *opt; 67 char *cp; 68 struct statfs sfb; 69 char buf[BUFSIZ]; 70 static char initname, usrname[100], grpname[100]; 71 72 if (!initname) { 73 (void)snprintf(usrname, sizeof(usrname), "%s%s", 74 qfextension[USRQUOTA], QUOTAFILENAME); 75 (void)snprintf(grpname, sizeof(grpname), "%s%s", 76 qfextension[GRPQUOTA], QUOTAFILENAME); 77 initname = 1; 78 } 79 strcpy(buf, fs->fs_mntops); 80 for (opt = strtok(buf, ","); opt; opt = strtok(NULL, ",")) { 81 if ((cp = index(opt, '='))) 82 *cp++ = '\0'; 83 if (type == USRQUOTA && strcmp(opt, usrname) == 0) 84 break; 85 if (type == GRPQUOTA && strcmp(opt, grpname) == 0) 86 break; 87 } 88 if (!opt) 89 return (0); 90 /* 91 * Ensure that the filesystem is mounted. 92 */ 93 if (statfs(fs->fs_file, &sfb) != 0 || 94 strcmp(fs->fs_file, sfb.f_mntonname)) { 95 return (0); 96 } 97 if (cp) { 98 strncpy(qfnamep, cp, qfbufsize); 99 } else { 100 (void)snprintf(qfnamep, qfbufsize, "%s/%s.%s", fs->fs_file, 101 QUOTAFILENAME, qfextension[type]); 102 } 103 return (1); 104} 105 106struct quotafile * 107quota_open(struct fstab *fs, int quotatype, int openflags) 108{ 109 struct quotafile *qf; 110 struct dqhdr64 dqh; 111 struct group *grp; 112 int qcmd, serrno; 113 114 if ((qf = calloc(1, sizeof(*qf))) == NULL) 115 return (NULL); 116 qf->quotatype = quotatype; 117 strncpy(qf->fsname, fs->fs_file, sizeof(qf->fsname)); 118 qcmd = QCMD(Q_GETQUOTA, quotatype); 119 if (quotactl(fs->fs_file, qcmd, 0, &dqh) == 0) { 120 qf->wordsize = 64; 121 qf->fd = -1; 122 return (qf); 123 } 124 if (!hasquota(fs, quotatype, qf->qfname, sizeof(qf->qfname))) { 125 free(qf); 126 errno = EOPNOTSUPP; 127 return (NULL); 128 } 129 if ((qf->fd = open(qf->qfname, openflags & O_ACCMODE)) < 0 && 130 (openflags & O_CREAT) == 0) { 131 serrno = errno; 132 free(qf); 133 errno = serrno; 134 return (NULL); 135 } 136 /* File open worked, so process it */ 137 if (qf->fd != -1) { 138 qf->wordsize = 32; 139 switch (read(qf->fd, &dqh, sizeof(dqh))) { 140 case -1: 141 serrno = errno; 142 close(qf->fd); 143 free(qf); 144 errno = serrno; 145 return (NULL); 146 case sizeof(dqh): 147 if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) != 0) { 148 /* no magic, assume 32 bits */ 149 qf->wordsize = 32; 150 return (qf); 151 } 152 if (be32toh(dqh.dqh_version) != Q_DQHDR64_VERSION || 153 be32toh(dqh.dqh_hdrlen) != sizeof(struct dqhdr64) || 154 be32toh(dqh.dqh_reclen) != sizeof(struct dqblk64)) { 155 /* correct magic, wrong version / lengths */ 156 close(qf->fd); 157 free(qf); 158 errno = EINVAL; 159 return (NULL); 160 } 161 qf->wordsize = 64; 162 return (qf); 163 default: 164 qf->wordsize = 32; 165 return (qf); 166 } 167 /* not reached */ 168 } 169 /* Open failed above, but O_CREAT specified, so create a new file */ 170 if ((qf->fd = open(qf->qfname, O_RDWR|O_CREAT|O_TRUNC, 0)) < 0) { 171 serrno = errno; 172 free(qf); 173 errno = serrno; 174 return (NULL); 175 } 176 qf->wordsize = 64; 177 memset(&dqh, 0, sizeof(dqh)); 178 memcpy(dqh.dqh_magic, Q_DQHDR64_MAGIC, sizeof(dqh.dqh_magic)); 179 dqh.dqh_version = htobe32(Q_DQHDR64_VERSION); 180 dqh.dqh_hdrlen = htobe32(sizeof(struct dqhdr64)); 181 dqh.dqh_reclen = htobe32(sizeof(struct dqblk64)); 182 if (write(qf->fd, &dqh, sizeof(dqh)) != sizeof(dqh)) { 183 serrno = errno; 184 unlink(qf->qfname); 185 close(qf->fd); 186 free(qf); 187 errno = serrno; 188 return (NULL); 189 } 190 grp = getgrnam(QUOTAGROUP); 191 fchown(qf->fd, 0, grp ? grp->gr_gid : 0); 192 fchmod(qf->fd, 0640); 193 return (qf); 194} 195 196void 197quota_close(struct quotafile *qf) 198{ 199 200 if (qf->fd != -1) 201 close(qf->fd); 202 free(qf); 203} 204 205static int 206quota_read32(struct quotafile *qf, struct dqblk *dqb, int id) 207{ 208 struct dqblk32 dqb32; 209 off_t off; 210 211 off = id * sizeof(struct dqblk32); 212 if (lseek(qf->fd, off, SEEK_SET) == -1) 213 return (-1); 214 switch (read(qf->fd, &dqb32, sizeof(dqb32))) { 215 case 0: 216 memset(&dqb, 0, sizeof(*dqb)); 217 return (0); 218 case sizeof(dqb32): 219 dqb->dqb_bhardlimit = dqb32.dqb_bhardlimit; 220 dqb->dqb_bsoftlimit = dqb32.dqb_bsoftlimit; 221 dqb->dqb_curblocks = dqb32.dqb_curblocks; 222 dqb->dqb_ihardlimit = dqb32.dqb_ihardlimit; 223 dqb->dqb_isoftlimit = dqb32.dqb_isoftlimit; 224 dqb->dqb_curinodes = dqb32.dqb_curinodes; 225 dqb->dqb_btime = dqb32.dqb_btime; 226 dqb->dqb_itime = dqb32.dqb_itime; 227 return (0); 228 default: 229 return (-1); 230 } 231} 232 233static int 234quota_read64(struct quotafile *qf, struct dqblk *dqb, int id) 235{ 236 struct dqblk64 dqb64; 237 off_t off; 238 239 off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64); 240 if (lseek(qf->fd, off, SEEK_SET) == -1) 241 return (-1); 242 switch (read(qf->fd, &dqb64, sizeof(dqb64))) { 243 case 0: 244 memset(&dqb, 0, sizeof(*dqb)); 245 return (0); 246 case sizeof(dqb64): 247 dqb->dqb_bhardlimit = be64toh(dqb64.dqb_bhardlimit); 248 dqb->dqb_bsoftlimit = be64toh(dqb64.dqb_bsoftlimit); 249 dqb->dqb_curblocks = be64toh(dqb64.dqb_curblocks); 250 dqb->dqb_ihardlimit = be64toh(dqb64.dqb_ihardlimit); 251 dqb->dqb_isoftlimit = be64toh(dqb64.dqb_isoftlimit); 252 dqb->dqb_curinodes = be64toh(dqb64.dqb_curinodes); 253 dqb->dqb_btime = be64toh(dqb64.dqb_btime); 254 dqb->dqb_itime = be64toh(dqb64.dqb_itime); 255 return (0); 256 default: 257 return (-1); 258 } 259} 260 261int 262quota_read(struct quotafile *qf, struct dqblk *dqb, int id) 263{ 264 int qcmd; 265 266 if (qf->fd == -1) { 267 qcmd = QCMD(Q_GETQUOTA, qf->quotatype); 268 return (quotactl(qf->fsname, qcmd, id, dqb)); 269 } 270 switch (qf->wordsize) { 271 case 32: 272 return quota_read32(qf, dqb, id); 273 case 64: 274 return quota_read64(qf, dqb, id); 275 default: 276 errno = EINVAL; 277 return (-1); 278 } 279 /* not reached */ 280} 281 282#define CLIP32(u64) ((u64) > UINT32_MAX ? UINT32_MAX : (uint32_t)(u64)) 283 284static int 285quota_write32(struct quotafile *qf, const struct dqblk *dqb, int id) 286{ 287 struct dqblk32 dqb32; 288 off_t off; 289 290 dqb32.dqb_bhardlimit = CLIP32(dqb->dqb_bhardlimit); 291 dqb32.dqb_bsoftlimit = CLIP32(dqb->dqb_bsoftlimit); 292 dqb32.dqb_curblocks = CLIP32(dqb->dqb_curblocks); 293 dqb32.dqb_ihardlimit = CLIP32(dqb->dqb_ihardlimit); 294 dqb32.dqb_isoftlimit = CLIP32(dqb->dqb_isoftlimit); 295 dqb32.dqb_curinodes = CLIP32(dqb->dqb_curinodes); 296 dqb32.dqb_btime = CLIP32(dqb->dqb_btime); 297 dqb32.dqb_itime = CLIP32(dqb->dqb_itime); 298 299 off = id * sizeof(struct dqblk32); 300 if (lseek(qf->fd, off, SEEK_SET) == -1) 301 return (-1); 302 if (write(qf->fd, &dqb32, sizeof(dqb32)) == sizeof(dqb32)) 303 return (0); 304 return (-1); 305} 306 307static int 308quota_write64(struct quotafile *qf, const struct dqblk *dqb, int id) 309{ 310 struct dqblk64 dqb64; 311 off_t off; 312 313 dqb64.dqb_bhardlimit = htobe64(dqb->dqb_bhardlimit); 314 dqb64.dqb_bsoftlimit = htobe64(dqb->dqb_bsoftlimit); 315 dqb64.dqb_curblocks = htobe64(dqb->dqb_curblocks); 316 dqb64.dqb_ihardlimit = htobe64(dqb->dqb_ihardlimit); 317 dqb64.dqb_isoftlimit = htobe64(dqb->dqb_isoftlimit); 318 dqb64.dqb_curinodes = htobe64(dqb->dqb_curinodes); 319 dqb64.dqb_btime = htobe64(dqb->dqb_btime); 320 dqb64.dqb_itime = htobe64(dqb->dqb_itime); 321 322 off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64); 323 if (lseek(qf->fd, off, SEEK_SET) == -1) 324 return (-1); 325 if (write(qf->fd, &dqb64, sizeof(dqb64)) == sizeof(dqb64)) 326 return (0); 327 return (-1); 328} 329 330int 331quota_write_usage(struct quotafile *qf, struct dqblk *dqb, int id) 332{ 333 struct dqblk dqbuf; 334 int qcmd; 335 336 if (qf->fd == -1) { 337 qcmd = QCMD(Q_SETUSE, qf->quotatype); 338 return (quotactl(qf->fsname, qcmd, id, dqb)); 339 } 340 /* 341 * Have to do read-modify-write of quota in file. 342 */ 343 if (quota_read(qf, &dqbuf, id) != 0) 344 return (-1); 345 /* 346 * Reset time limit if have a soft limit and were 347 * previously under it, but are now over it. 348 */ 349 if (dqbuf.dqb_bsoftlimit && id != 0 && 350 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit && 351 dqb->dqb_curblocks >= dqbuf.dqb_bsoftlimit) 352 dqbuf.dqb_btime = 0; 353 if (dqbuf.dqb_isoftlimit && id != 0 && 354 dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit && 355 dqb->dqb_curinodes >= dqbuf.dqb_isoftlimit) 356 dqbuf.dqb_itime = 0; 357 dqbuf.dqb_curinodes = dqb->dqb_curinodes; 358 dqbuf.dqb_curblocks = dqb->dqb_curblocks; 359 /* 360 * Write it back. 361 */ 362 switch (qf->wordsize) { 363 case 32: 364 return quota_write32(qf, &dqbuf, id); 365 case 64: 366 return quota_write64(qf, &dqbuf, id); 367 default: 368 errno = EINVAL; 369 return (-1); 370 } 371 /* not reached */ 372} 373 374int 375quota_write_limits(struct quotafile *qf, struct dqblk *dqb, int id) 376{ 377 struct dqblk dqbuf; 378 int qcmd; 379 380 if (qf->fd == -1) { 381 qcmd = QCMD(Q_SETQUOTA, qf->quotatype); 382 return (quotactl(qf->fsname, qcmd, id, dqb)); 383 } 384 /* 385 * Have to do read-modify-write of quota in file. 386 */ 387 if (quota_read(qf, &dqbuf, id) != 0) 388 return (-1); 389 /* 390 * Reset time limit if have a soft limit and were 391 * previously under it, but are now over it 392 * or if there previously was no soft limit, but 393 * now have one and are over it. 394 */ 395 if (dqbuf.dqb_bsoftlimit && id != 0 && 396 dqbuf.dqb_curblocks < dqbuf.dqb_bsoftlimit && 397 dqbuf.dqb_curblocks >= dqb->dqb_bsoftlimit) 398 dqb->dqb_btime = 0; 399 if (dqbuf.dqb_bsoftlimit == 0 && id != 0 && 400 dqb->dqb_bsoftlimit > 0 && 401 dqbuf.dqb_curblocks >= dqb->dqb_bsoftlimit) 402 dqb->dqb_btime = 0; 403 if (dqbuf.dqb_isoftlimit && id != 0 && 404 dqbuf.dqb_curinodes < dqbuf.dqb_isoftlimit && 405 dqbuf.dqb_curinodes >= dqb->dqb_isoftlimit) 406 dqb->dqb_itime = 0; 407 if (dqbuf.dqb_isoftlimit == 0 && id !=0 && 408 dqb->dqb_isoftlimit > 0 && 409 dqbuf.dqb_curinodes >= dqb->dqb_isoftlimit) 410 dqb->dqb_itime = 0; 411 dqb->dqb_curinodes = dqbuf.dqb_curinodes; 412 dqb->dqb_curblocks = dqbuf.dqb_curblocks; 413 /* 414 * Write it back. 415 */ 416 switch (qf->wordsize) { 417 case 32: 418 return quota_write32(qf, dqb, id); 419 case 64: 420 return quota_write64(qf, dqb, id); 421 default: 422 errno = EINVAL; 423 return (-1); 424 } 425 /* not reached */ 426} 427