6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
8 * Set quota on specified device for specified user to specified value.
10 * Uses the NFS style quota system/quotactl rather than the Melbourne
16 static char *rcsid_setquota_c = "$Header$";
24 #include <sys/param.h>
26 #include <ufs/quota.h>
28 #define kb(n) (howmany(dbtob(n), 1024))
30 static char device[20];
31 static char quotafilename[30];
32 static struct dqblk zblk = {0};
34 int main(int argc, char **argv)
36 int uid, uid_low, uid_high, soft_quota, qfd;
41 while (argc > 4 && *argv[1] == '-')
60 if ((argc != 4 && !range_mode) || (argc != 5 && range_mode))
63 fprintf(stderr, "usage: setquota [-u] special uid quota\n"
64 "setquota -r [-u] special uid_low uid_high quota\n"
65 "-u means set limit to <quota> + cur usage\n"
66 "special is a mounted filesystem special device\n"
67 "quota is in 1KB units\n");
71 if ((!range_mode && (!isdigit(*argv[2]) || !isdigit(*argv[3]))) ||
72 (range_mode && (!isdigit(*argv[2]) || !isdigit(*argv[3]) ||
75 fprintf(stderr, "setquota: uid and quota must be numeric\n");
81 uid_low = atoi(argv[2]);
82 uid_high = atoi(argv[3]);
83 soft_quota = atoi(argv[4]);
84 if (uid_low > uid_high)
86 fprintf(stderr, "setquota: range error\n");
92 uid_low = uid_high = atoi(argv[2]);
93 soft_quota = atoi(argv[3]);
98 for (uid = uid_low; uid <= uid_high; uid++)
100 if (quotactl(Q_GETQUOTA, device, uid, &odb))
102 if (!(qfd = open(quotafilename, O_RDWR)))
104 perror("No quota file");
108 lseek(qfd, 32767 * sizeof(struct dqblk), L_SET);
109 write(qfd, &zblk, sizeof(struct dqblk));
112 if (quotactl(Q_GETQUOTA, device, uid, &odb) != 0)
114 perror("Can't get current quota info");
119 db.dqb_bsoftlimit = soft_quota;
120 db.dqb_bhardlimit = (db.dqb_bsoftlimit * 6) / 5;
121 db.dqb_fsoftlimit = soft_quota / 2;
122 db.dqb_fhardlimit = (db.dqb_fsoftlimit * 6) / 5;
123 db.dqb_btimelimit = odb.dqb_btimelimit;
124 db.dqb_ftimelimit = odb.dqb_ftimelimit;
126 db.dqb_bsoftlimit *= btodb(1024);
127 db.dqb_bhardlimit *= btodb(1024);
131 db.dqb_bhardlimit += odb.dqb_curblocks;
132 db.dqb_bsoftlimit += odb.dqb_curblocks;
133 db.dqb_fhardlimit += odb.dqb_curfiles;
134 db.dqb_fsoftlimit += odb.dqb_curfiles;
137 if (quotactl(Q_SETQLIM, device, uid, &db) < 0)
139 fprintf(stderr, "quotactl: %d on ", uid);
145 if (quotactl(Q_SYNC, device, 0, 0) < 0)
147 perror("can't sync disk quota");
154 get_device(char *device_or_dir)
159 fstab = setmntent(MNTTAB, "r");
160 while (mntp = getmntent(fstab))
162 if (!strcmp(mntp->mnt_fsname, device_or_dir) ||
163 !strcmp(mntp->mnt_dir, device_or_dir))
165 strcpy(device, mntp->mnt_fsname);
166 sprintf(quotafilename, "%s/quotas", mntp->mnt_dir);
171 fprintf(stderr, "%s not mounted.\n", device_or_dir);