2 Unix SMB/CIFS implementation.
3 System QUOTA function wrappers
4 Copyright (C) Stefan (metze) Metzmacher 2003
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define DBGC_CLASS DBGC_QUOTA
27 #ifdef HAVE_SYS_QUOTAS
29 #if defined(HAVE_QUOTACTL_4A)
31 /*#endif HAVE_QUOTACTL_4A */
32 #elif defined(HAVE_QUOTACTL_4B)
34 #error HAVE_QUOTACTL_4B not implemeted
36 /*#endif HAVE_QUOTACTL_4B */
37 #elif defined(HAVE_QUOTACTL_3)
39 #error HAVE_QUOTACTL_3 not implemented
41 /* #endif HAVE_QUOTACTL_3 */
42 #else /* NO_QUOTACTL_USED */
44 #endif /* NO_QUOTACTL_USED */
47 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
55 /* find the block device file */
57 if (!path||!mntpath||!bdev||!fs)
58 smb_panic("sys_path_to_bdev: called with NULL pointer");
64 if ( sys_stat(path, &S) == -1 )
69 fp = setmntent(MOUNTED,"r");
74 while ((mnt = getmntent(fp))) {
75 if ( sys_stat(mnt->mnt_dir,&S) == -1 )
78 if (S.st_dev == devno) {
79 (*mntpath) = SMB_STRDUP(mnt->mnt_dir);
80 (*bdev) = SMB_STRDUP(mnt->mnt_fsname);
81 (*fs) = SMB_STRDUP(mnt->mnt_type);
82 if ((*mntpath)&&(*bdev)&&(*fs)) {
99 /* #endif HAVE_MNTENT */
100 #elif defined(HAVE_DEVNM)
102 /* we have this on HPUX, ... */
103 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
109 if (!path||!mntpath||!bdev||!fs)
110 smb_panic("sys_path_to_bdev: called with NULL pointer");
116 /* find the block device file */
118 if ((ret=sys_stat(path, &S))!=0) {
122 if ((ret=devnm(S_IFBLK, S.st_dev, dev_disk, 256, 1))!=0) {
126 /* we should get the mntpath right...
127 * but I don't know how
130 (*mntpath) = SMB_STRDUP(path);
131 (*bdev) = SMB_STRDUP(dev_disk);
132 if ((*mntpath)&&(*bdev)) {
144 /* #endif HAVE_DEVNM */
146 /* we should fake this up...*/
147 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
151 if (!path||!mntpath||!bdev||!fs)
152 smb_panic("sys_path_to_bdev: called with NULL pointer");
158 (*mntpath) = SMB_STRDUP(path);
170 /*********************************************************************
171 Now the list of all filesystem specific quota systems we have found
172 **********************************************************************/
175 int (*get_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
176 int (*set_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
177 } sys_quota_backends[] = {
178 #ifdef HAVE_XFS_QUOTAS
179 {"xfs", sys_get_xfs_quota, sys_set_xfs_quota},
180 #endif /* HAVE_XFS_QUOTAS */
184 static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
186 const char *get_quota_command;
188 get_quota_command = lp_get_quota_command();
189 if (get_quota_command && *get_quota_command) {
197 case SMB_USER_QUOTA_TYPE:
198 case SMB_USER_FS_QUOTA_TYPE:
201 case SMB_GROUP_QUOTA_TYPE:
202 case SMB_GROUP_FS_QUOTA_TYPE:
206 DEBUG(0,("invalid quota type.\n"));
210 slprintf(syscmd, sizeof(syscmd)-1,
212 get_quota_command, path, qtype, _id);
214 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
216 lines = file_lines_pload(syscmd, NULL);
218 char *line = lines[0];
220 DEBUG (3, ("Read output from get_quota, \"%s\"\n", line));
222 /* we need to deal with long long unsigned here, if supported */
224 dp->qflags = (enum SMB_QUOTA_TYPE)strtoul(line, &p2, 10);
226 while (p && *p && isspace(*p))
229 dp->curblocks = STR_TO_SMB_BIG_UINT(p, &p);
232 while (p && *p && isspace(*p))
235 dp->softlimit = STR_TO_SMB_BIG_UINT(p, &p);
238 while (p && *p && isspace(*p))
241 dp->hardlimit = STR_TO_SMB_BIG_UINT(p, &p);
244 while (p && *p && isspace(*p))
247 dp->curinodes = STR_TO_SMB_BIG_UINT(p, &p);
250 while (p && *p && isspace(*p))
253 dp->isoftlimit = STR_TO_SMB_BIG_UINT(p, &p);
256 while (p && *p && isspace(*p))
259 dp->ihardlimit = STR_TO_SMB_BIG_UINT(p, &p);
262 while (p && *p && isspace(*p))
265 dp->bsize = STR_TO_SMB_BIG_UINT(p, NULL);
268 file_lines_free(lines);
269 DEBUG (3, ("Parsed output of get_quota, ...\n"));
271 #ifdef LARGE_SMB_OFF_T
273 "qflags:%u curblocks:%llu softlimit:%llu hardlimit:%llu\n"
274 "curinodes:%llu isoftlimit:%llu ihardlimit:%llu bsize:%llu\n",
275 dp->qflags,(long long unsigned)dp->curblocks,
276 (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit,
277 (long long unsigned)dp->curinodes,
278 (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit,
279 (long long unsigned)dp->bsize));
280 #else /* LARGE_SMB_OFF_T */
282 "qflags:%u curblocks:%lu softlimit:%lu hardlimit:%lu\n"
283 "curinodes:%lu isoftlimit:%lu ihardlimit:%lu bsize:%lu\n",
284 dp->qflags,(long unsigned)dp->curblocks,
285 (long unsigned)dp->softlimit,(long unsigned)dp->hardlimit,
286 (long unsigned)dp->curinodes,
287 (long unsigned)dp->isoftlimit,(long unsigned)dp->ihardlimit,
288 (long unsigned)dp->bsize));
289 #endif /* LARGE_SMB_OFF_T */
293 DEBUG (0, ("get_quota_command failed!\n"));
301 DEBUG(0,("The output of get_quota_command is invalid!\n"));
305 static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
307 const char *set_quota_command;
309 set_quota_command = lp_set_quota_command();
310 if (set_quota_command && *set_quota_command) {
316 case SMB_USER_QUOTA_TYPE:
317 case SMB_USER_FS_QUOTA_TYPE:
320 case SMB_GROUP_QUOTA_TYPE:
321 case SMB_GROUP_FS_QUOTA_TYPE:
328 #ifdef LARGE_SMB_OFF_T
329 slprintf(syscmd, sizeof(syscmd)-1,
333 set_quota_command, path, qtype, _id, dp->qflags,
334 (long long unsigned)dp->softlimit,(long long unsigned)dp->hardlimit,
335 (long long unsigned)dp->isoftlimit,(long long unsigned)dp->ihardlimit,
336 (long long unsigned)dp->bsize);
337 #else /* LARGE_SMB_OFF_T */
338 slprintf(syscmd, sizeof(syscmd)-1,
342 set_quota_command, path, qtype, _id, dp->qflags,
343 (long unsigned)dp->softlimit,(long unsigned)dp->hardlimit,
344 (long unsigned)dp->isoftlimit,(long unsigned)dp->ihardlimit,
345 (long unsigned)dp->bsize);
346 #endif /* LARGE_SMB_OFF_T */
350 DEBUG (3, ("get_quota: Running command %s\n", syscmd));
352 lines = file_lines_pload(syscmd, NULL);
354 char *line = lines[0];
356 DEBUG (3, ("Read output from set_quota, \"%s\"\n", line));
358 file_lines_free(lines);
362 DEBUG (0, ("set_quota_command failed!\n"));
370 int sys_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
375 char *mntpath = NULL;
380 smb_panic("sys_get_quota: called with NULL pointer");
382 if (command_get_quota(path, qtype, id, dp)==0) {
384 } else if (errno != ENOSYS) {
388 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
389 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
394 DEBUG(10,("sys_get_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
396 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].get_quota);i++) {
397 if (strcmp(fs,sys_quota_backends[i].name)==0) {
398 ret = sys_quota_backends[i].get_quota(mntpath, bdev, qtype, id, dp);
400 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
401 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
403 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
404 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
412 /* use the default vfs quota functions */
413 ret=sys_get_vfs_quota(mntpath, bdev, qtype, id, dp);
415 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s\n",
416 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
418 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
419 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
427 if ((ret!=0)&& (errno == EDQUOT)) {
428 DEBUG(10,("sys_get_quota() warning over quota!\n"));
435 int sys_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
440 char *mntpath = NULL;
444 /* find the block device file */
447 smb_panic("get_smb_quota: called with NULL pointer");
449 if (command_set_quota(path, qtype, id, dp)==0) {
451 } else if (errno != ENOSYS) {
455 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
456 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
461 DEBUG(10,("sys_set_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
463 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].set_quota);i++) {
464 if (strcmp(fs,sys_quota_backends[i].name)==0) {
465 ret = sys_quota_backends[i].set_quota(mntpath, bdev, qtype, id, dp);
467 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
468 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
470 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
471 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
479 /* use the default vfs quota functions */
480 ret=sys_set_vfs_quota(mntpath, bdev, qtype, id, dp);
482 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
483 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
485 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
486 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
494 if ((ret!=0)&& (errno == EDQUOT)) {
495 DEBUG(10,("sys_set_quota() warning over quota!\n"));
502 #else /* HAVE_SYS_QUOTAS */
503 void dummy_sysquotas_c(void);
505 void dummy_sysquotas_c(void)
509 #endif /* HAVE_SYS_QUOTAS */