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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "lib/util_file.h"
23 #include "lib/util/smb_strtox.h"
26 #define DBGC_CLASS DBGC_QUOTA
28 #ifdef HAVE_SYS_QUOTAS
30 #if defined(HAVE_QUOTACTL_4A)
32 /*#endif HAVE_QUOTACTL_4A */
33 #elif defined(HAVE_QUOTACTL_4B)
35 /*#endif HAVE_QUOTACTL_4B */
36 #elif defined(HAVE_QUOTACTL_3)
38 #error HAVE_QUOTACTL_3 not implemented
40 /* #endif HAVE_QUOTACTL_3 */
41 #else /* NO_QUOTACTL_USED */
43 #endif /* NO_QUOTACTL_USED */
45 #if defined(HAVE_MNTENT) && defined(HAVE_REALPATH)
46 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
51 struct mntent *mnt = NULL;
53 char *stat_mntpath = NULL;
56 /* find the block device file */
61 if (sys_stat(path, &S, false) != 0) {
67 stat_mntpath = sys_realpath(path);
68 if (stat_mntpath == NULL) {
69 DBG_WARNING("realpath(%s) failed - %s\n", path,
74 if (sys_stat(stat_mntpath, &S, false) != 0) {
75 DBG_WARNING("cannot stat real path %s - %s\n", stat_mntpath,
80 if (S.st_ex_dev != devno) {
81 DBG_WARNING("device on real path has changed\n");
88 p = strrchr(stat_mntpath, '/');
90 DBG_ERR("realpath for %s does not begin with a '/'\n",
95 if (p == stat_mntpath) {
101 if (sys_stat(stat_mntpath, &S, false) != 0) {
102 DBG_WARNING("cannot stat real path component %s - %s\n",
103 stat_mntpath, strerror(errno));
106 if (S.st_ex_dev != devno) {
111 if (p <= stat_mntpath + 1) {
116 fp = setmntent(MOUNTED,"r");
121 while ((mnt = getmntent(fp))) {
122 if (!strequal(mnt->mnt_dir, stat_mntpath)) {
126 if ( sys_stat(mnt->mnt_dir, &S, false) == -1 )
129 if (S.st_ex_dev == devno) {
130 (*mntpath) = SMB_STRDUP(mnt->mnt_dir);
131 (*bdev) = SMB_STRDUP(mnt->mnt_fsname);
132 (*fs) = SMB_STRDUP(mnt->mnt_type);
133 if ((*mntpath)&&(*bdev)&&(*fs)) {
149 SAFE_FREE(stat_mntpath);
152 /* #endif HAVE_MNTENT */
153 #elif defined(HAVE_DEVNM)
155 /* we have this on HPUX, ... */
156 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
162 if (!path||!mntpath||!bdev||!fs)
163 smb_panic("sys_path_to_bdev: called with NULL pointer");
169 /* find the block device file */
171 if ((ret=sys_stat(path, &S, false))!=0) {
175 if ((ret=devnm(S_IFBLK, S.st_ex_dev, dev_disk, 256, 1))!=0) {
179 /* we should get the mntpath right...
180 * but I don't know how
183 (*mntpath) = SMB_STRDUP(path);
184 (*bdev) = SMB_STRDUP(dev_disk);
185 if ((*mntpath)&&(*bdev)) {
197 /* #endif HAVE_DEVNM */
199 /* we should fake this up...*/
200 static int sys_path_to_bdev(const char *path, char **mntpath, char **bdev, char **fs)
204 if (!path||!mntpath||!bdev||!fs)
205 smb_panic("sys_path_to_bdev: called with NULL pointer");
211 (*mntpath) = SMB_STRDUP(path);
223 /*********************************************************************
224 Now the list of all filesystem specific quota systems we have found
225 **********************************************************************/
228 int (*get_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
229 int (*set_quota)(const char *path, const char *bdev, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp);
230 } sys_quota_backends[] = {
231 #ifdef HAVE_JFS_QUOTA_H
232 {"jfs2", sys_get_jfs2_quota, sys_set_jfs2_quota},
234 #if defined HAVE_XFS_QUOTAS
235 {"xfs", sys_get_xfs_quota, sys_set_xfs_quota},
236 {"gfs", sys_get_xfs_quota, sys_set_xfs_quota},
237 {"gfs2", sys_get_xfs_quota, sys_set_xfs_quota},
238 #endif /* HAVE_XFS_QUOTAS */
239 #ifdef HAVE_NFS_QUOTAS
240 {"nfs", sys_get_nfs_quota, sys_set_nfs_quota},
241 {"nfs4", sys_get_nfs_quota, sys_set_nfs_quota},
242 #endif /* HAVE_NFS_QUOTAS */
246 static int command_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
248 const struct loadparm_substitution *lp_sub =
249 loadparm_s3_global_substitution();
250 const char *get_quota_command;
253 get_quota_command = lp_get_quota_command(talloc_tos(), lp_sub);
254 if (get_quota_command && *get_quota_command) {
262 case SMB_USER_QUOTA_TYPE:
263 case SMB_USER_FS_QUOTA_TYPE:
266 case SMB_GROUP_QUOTA_TYPE:
267 case SMB_GROUP_FS_QUOTA_TYPE:
271 DEBUG(0,("invalid quota type.\n"));
275 argl = str_list_make_empty(talloc_tos());
276 str_list_add_printf(&argl, "%s", get_quota_command);
277 str_list_add_printf(&argl, "%s", path);
278 str_list_add_printf(&argl, "%d", qtype);
279 str_list_add_printf(&argl, "%d", _id);
284 DBG_NOTICE("Running command %s %s %d %d\n",
290 lines = file_lines_ploadv(talloc_tos(), argl, NULL);
294 char *line = lines[0];
296 DEBUG (3, ("Read output from get_quota, \"%s\"\n", line));
298 /* we need to deal with long long unsigned here, if supported */
300 dp->qflags = smb_strtoul(line,
310 while (p && *p && isspace(*p)) {
315 dp->curblocks = STR_TO_SMB_BIG_UINT(p, &p);
320 while (p && *p && isspace(*p)) {
325 dp->softlimit = STR_TO_SMB_BIG_UINT(p, &p);
330 while (p && *p && isspace(*p)) {
335 dp->hardlimit = STR_TO_SMB_BIG_UINT(p, &p);
340 while (p && *p && isspace(*p)) {
345 dp->curinodes = STR_TO_SMB_BIG_UINT(p, &p);
350 while (p && *p && isspace(*p)) {
355 dp->isoftlimit = STR_TO_SMB_BIG_UINT(p, &p);
360 while (p && *p && isspace(*p)) {
365 dp->ihardlimit = STR_TO_SMB_BIG_UINT(p, &p);
370 while (p && *p && isspace(*p)) {
375 dp->bsize = STR_TO_SMB_BIG_UINT(p, NULL);
383 DEBUG (3, ("Parsed output of get_quota, ...\n"));
386 "qflags:%"PRIu32" curblocks:%"PRIu64" softlimit:%"PRIu64" hardlimit:%"PRIu64"\n"
387 "curinodes:%"PRIu64" isoftlimit:%"PRIu64" ihardlimit:%"PRIu64" bsize:%"PRIu64"\n",
388 dp->qflags,dp->curblocks,
389 dp->softlimit,dp->hardlimit,
391 dp->isoftlimit,dp->ihardlimit,
396 DEBUG (0, ("get_quota_command failed!\n"));
406 DEBUG(0,("The output of get_quota_command is invalid!\n"));
410 static int command_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
412 const struct loadparm_substitution *lp_sub =
413 loadparm_s3_global_substitution();
414 const char *set_quota_command;
416 set_quota_command = lp_set_quota_command(talloc_tos(), lp_sub);
417 if (set_quota_command && *set_quota_command) {
423 case SMB_USER_QUOTA_TYPE:
424 case SMB_USER_FS_QUOTA_TYPE:
427 case SMB_GROUP_QUOTA_TYPE:
428 case SMB_GROUP_FS_QUOTA_TYPE:
435 argl = str_list_make_empty(talloc_tos());
436 str_list_add_printf(&argl, "%s", set_quota_command);
437 str_list_add_printf(&argl, "%s", path);
438 str_list_add_printf(&argl, "%d", qtype);
439 str_list_add_printf(&argl, "%d", _id);
440 str_list_add_printf(&argl, "%u", dp->qflags);
442 &argl, "%"PRIu64, dp->softlimit);
444 &argl, "%"PRIu64, dp->hardlimit);
446 &argl, "%"PRIu64, dp->isoftlimit);
448 &argl, "%"PRIu64, dp->ihardlimit);
450 &argl, "%"PRIu64, dp->bsize);
455 DBG_NOTICE("Running command "
457 "%"PRIu32" %"PRIu64" %"PRIu64" "
458 "%"PRIu64" %"PRIu64" %"PRIu64"\n",
470 lines = file_lines_ploadv(talloc_tos(), argl, NULL);
473 char *line = lines[0];
475 DEBUG (3, ("Read output from set_quota, \"%s\"\n", line));
481 DEBUG (0, ("set_quota_command failed!\n"));
489 int sys_get_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
494 char *mntpath = NULL;
499 smb_panic("sys_get_quota: called with NULL pointer");
501 if (command_get_quota(path, qtype, id, dp)==0) {
503 } else if (errno != ENOSYS) {
507 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
508 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
513 DEBUG(10,("sys_get_quota() uid(%u, %u), fs(%s)\n", (unsigned)getuid(), (unsigned)geteuid(), fs));
515 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].get_quota);i++) {
516 if (strcmp(fs,sys_quota_backends[i].name)==0) {
517 ret = sys_quota_backends[i].get_quota(mntpath, bdev, qtype, id, dp);
519 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
520 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
522 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
523 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
531 /* use the default vfs quota functions */
532 ret=sys_get_vfs_quota(mntpath, bdev, qtype, id, dp);
534 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s\n",
535 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
537 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
538 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
549 int sys_set_quota(const char *path, enum SMB_QUOTA_TYPE qtype, unid_t id, SMB_DISK_QUOTA *dp)
554 char *mntpath = NULL;
558 /* find the block device file */
561 smb_panic("get_smb_quota: called with NULL pointer");
563 if (command_set_quota(path, qtype, id, dp)==0) {
565 } else if (errno != ENOSYS) {
569 if ((ret=sys_path_to_bdev(path,&mntpath,&bdev,&fs))!=0) {
570 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path));
575 DEBUG(10,("sys_set_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
577 for (i=0;(fs && sys_quota_backends[i].name && sys_quota_backends[i].set_quota);i++) {
578 if (strcmp(fs,sys_quota_backends[i].name)==0) {
579 ret = sys_quota_backends[i].set_quota(mntpath, bdev, qtype, id, dp);
581 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
582 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
584 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
585 fs,mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
593 /* use the default vfs quota functions */
594 ret=sys_set_vfs_quota(mntpath, bdev, qtype, id, dp);
596 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
597 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid),strerror(errno)));
599 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
600 "vfs",mntpath,bdev,qtype,(qtype==SMB_GROUP_QUOTA_TYPE?id.gid:id.uid)));
611 #else /* HAVE_SYS_QUOTAS */
612 void dummy_sysquotas_c(void);
614 void dummy_sysquotas_c(void)
618 #endif /* HAVE_SYS_QUOTAS */