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"
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 /*#endif HAVE_QUOTACTL_4B */
35 #elif defined(HAVE_QUOTACTL_3)
37 #error HAVE_QUOTACTL_3 not implemented
39 /* #endif HAVE_QUOTACTL_3 */
40 #else /* NO_QUOTACTL_USED */
42 #endif /* NO_QUOTACTL_USED */
44 #if defined(HAVE_MNTENT) && defined(HAVE_REALPATH)
45 static int sys_path_to_bdev(const char *path
, char **mntpath
, char **bdev
, char **fs
)
50 struct mntent
*mnt
= NULL
;
52 char *stat_mntpath
= NULL
;
55 /* find the block device file */
60 if (sys_stat(path
, &S
, false) != 0) {
66 stat_mntpath
= sys_realpath(path
);
67 if (stat_mntpath
== NULL
) {
68 DBG_WARNING("realpath(%s) failed - %s\n", path
,
73 if (sys_stat(stat_mntpath
, &S
, false) != 0) {
74 DBG_WARNING("cannot stat real path %s - %s\n", stat_mntpath
,
79 if (S
.st_ex_dev
!= devno
) {
80 DBG_WARNING("device on real path has changed\n");
87 p
= strrchr(stat_mntpath
, '/');
89 DBG_ERR("realpath for %s does not begin with a '/'\n",
94 if (p
== stat_mntpath
) {
100 if (sys_stat(stat_mntpath
, &S
, false) != 0) {
101 DBG_WARNING("cannot stat real path component %s - %s\n",
102 stat_mntpath
, strerror(errno
));
105 if (S
.st_ex_dev
!= devno
) {
110 if (p
<= stat_mntpath
+ 1) {
115 fp
= setmntent(MOUNTED
,"r");
120 while ((mnt
= getmntent(fp
))) {
121 if (!strequal(mnt
->mnt_dir
, stat_mntpath
)) {
125 if ( sys_stat(mnt
->mnt_dir
, &S
, false) == -1 )
128 if (S
.st_ex_dev
== devno
) {
129 (*mntpath
) = SMB_STRDUP(mnt
->mnt_dir
);
130 (*bdev
) = SMB_STRDUP(mnt
->mnt_fsname
);
131 (*fs
) = SMB_STRDUP(mnt
->mnt_type
);
132 if ((*mntpath
)&&(*bdev
)&&(*fs
)) {
148 SAFE_FREE(stat_mntpath
);
151 /* #endif HAVE_MNTENT */
152 #elif defined(HAVE_DEVNM)
154 /* we have this on HPUX, ... */
155 static int sys_path_to_bdev(const char *path
, char **mntpath
, char **bdev
, char **fs
)
161 if (!path
||!mntpath
||!bdev
||!fs
)
162 smb_panic("sys_path_to_bdev: called with NULL pointer");
168 /* find the block device file */
170 if ((ret
=sys_stat(path
, &S
, false))!=0) {
174 if ((ret
=devnm(S_IFBLK
, S
.st_ex_dev
, dev_disk
, 256, 1))!=0) {
178 /* we should get the mntpath right...
179 * but I don't know how
182 (*mntpath
) = SMB_STRDUP(path
);
183 (*bdev
) = SMB_STRDUP(dev_disk
);
184 if ((*mntpath
)&&(*bdev
)) {
196 /* #endif HAVE_DEVNM */
198 /* we should fake this up...*/
199 static int sys_path_to_bdev(const char *path
, char **mntpath
, char **bdev
, char **fs
)
203 if (!path
||!mntpath
||!bdev
||!fs
)
204 smb_panic("sys_path_to_bdev: called with NULL pointer");
210 (*mntpath
) = SMB_STRDUP(path
);
222 /*********************************************************************
223 Now the list of all filesystem specific quota systems we have found
224 **********************************************************************/
227 int (*get_quota
)(const char *path
, const char *bdev
, enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*dp
);
228 int (*set_quota
)(const char *path
, const char *bdev
, enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*dp
);
229 } sys_quota_backends
[] = {
230 #ifdef HAVE_JFS_QUOTA_H
231 {"jfs2", sys_get_jfs2_quota
, sys_set_jfs2_quota
},
233 #if defined HAVE_XFS_QUOTAS
234 {"xfs", sys_get_xfs_quota
, sys_set_xfs_quota
},
235 {"gfs", sys_get_xfs_quota
, sys_set_xfs_quota
},
236 {"gfs2", sys_get_xfs_quota
, sys_set_xfs_quota
},
237 #endif /* HAVE_XFS_QUOTAS */
238 #ifdef HAVE_NFS_QUOTAS
239 {"nfs", sys_get_nfs_quota
, sys_set_nfs_quota
},
240 {"nfs4", sys_get_nfs_quota
, sys_set_nfs_quota
},
241 #endif /* HAVE_NFS_QUOTAS */
245 static int command_get_quota(const char *path
, enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*dp
)
247 const struct loadparm_substitution
*lp_sub
=
248 loadparm_s3_global_substitution();
249 const char *get_quota_command
;
252 get_quota_command
= lp_get_quota_command(talloc_tos(), lp_sub
);
253 if (get_quota_command
&& *get_quota_command
) {
261 case SMB_USER_QUOTA_TYPE
:
262 case SMB_USER_FS_QUOTA_TYPE
:
265 case SMB_GROUP_QUOTA_TYPE
:
266 case SMB_GROUP_FS_QUOTA_TYPE
:
270 DEBUG(0,("invalid quota type.\n"));
274 argl
= talloc_zero_array(talloc_tos(), char *, 5);
278 argl
[0] = talloc_strdup(argl
, get_quota_command
);
279 if (argl
[0] == NULL
) {
283 argl
[1] = talloc_strdup(argl
, path
);
284 if (argl
[1] == NULL
) {
288 argl
[2] = talloc_asprintf(argl
, "%d", qtype
);
289 if (argl
[2] == NULL
) {
293 argl
[3] = talloc_asprintf(argl
, "%d", _id
);
294 if (argl
[3] == NULL
) {
300 DBG_NOTICE("Running command %s %s %d %d\n",
306 lines
= file_lines_ploadv(talloc_tos(), argl
, NULL
);
310 char *line
= lines
[0];
312 DEBUG (3, ("Read output from get_quota, \"%s\"\n", line
));
314 /* we need to deal with long long unsigned here, if supported */
316 dp
->qflags
= smb_strtoul(line
,
326 while (p
&& *p
&& isspace(*p
)) {
331 dp
->curblocks
= STR_TO_SMB_BIG_UINT(p
, &p
);
336 while (p
&& *p
&& isspace(*p
)) {
341 dp
->softlimit
= STR_TO_SMB_BIG_UINT(p
, &p
);
346 while (p
&& *p
&& isspace(*p
)) {
351 dp
->hardlimit
= STR_TO_SMB_BIG_UINT(p
, &p
);
356 while (p
&& *p
&& isspace(*p
)) {
361 dp
->curinodes
= STR_TO_SMB_BIG_UINT(p
, &p
);
366 while (p
&& *p
&& isspace(*p
)) {
371 dp
->isoftlimit
= STR_TO_SMB_BIG_UINT(p
, &p
);
376 while (p
&& *p
&& isspace(*p
)) {
381 dp
->ihardlimit
= STR_TO_SMB_BIG_UINT(p
, &p
);
386 while (p
&& *p
&& isspace(*p
)) {
391 dp
->bsize
= STR_TO_SMB_BIG_UINT(p
, NULL
);
399 DEBUG (3, ("Parsed output of get_quota, ...\n"));
402 "qflags:%u curblocks:%llu softlimit:%llu hardlimit:%llu\n"
403 "curinodes:%llu isoftlimit:%llu ihardlimit:%llu bsize:%llu\n",
404 dp
->qflags
,(long long unsigned)dp
->curblocks
,
405 (long long unsigned)dp
->softlimit
,(long long unsigned)dp
->hardlimit
,
406 (long long unsigned)dp
->curinodes
,
407 (long long unsigned)dp
->isoftlimit
,(long long unsigned)dp
->ihardlimit
,
408 (long long unsigned)dp
->bsize
));
412 DEBUG (0, ("get_quota_command failed!\n"));
422 DEBUG(0,("The output of get_quota_command is invalid!\n"));
426 static int command_set_quota(const char *path
, enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*dp
)
428 const struct loadparm_substitution
*lp_sub
=
429 loadparm_s3_global_substitution();
430 const char *set_quota_command
;
432 set_quota_command
= lp_set_quota_command(talloc_tos(), lp_sub
);
433 if (set_quota_command
&& *set_quota_command
) {
439 case SMB_USER_QUOTA_TYPE
:
440 case SMB_USER_FS_QUOTA_TYPE
:
443 case SMB_GROUP_QUOTA_TYPE
:
444 case SMB_GROUP_FS_QUOTA_TYPE
:
451 argl
= talloc_zero_array(talloc_tos(), char *, 11);
455 argl
[0] = talloc_strdup(argl
, set_quota_command
);
456 if (argl
[0] == NULL
) {
460 argl
[1] = talloc_strdup(argl
, path
);
461 if (argl
[1] == NULL
) {
465 argl
[2] = talloc_asprintf(argl
, "%d", qtype
);
466 if (argl
[2] == NULL
) {
470 argl
[3] = talloc_asprintf(argl
, "%d", _id
);
471 if (argl
[3] == NULL
) {
475 argl
[4] = talloc_asprintf(argl
, "%u", dp
->qflags
);
476 if (argl
[4] == NULL
) {
480 argl
[5] = talloc_asprintf(argl
, "%llu",
481 (long long unsigned)dp
->softlimit
);
482 if (argl
[5] == NULL
) {
486 argl
[6] = talloc_asprintf(argl
, "%llu",
487 (long long unsigned)dp
->hardlimit
);
488 if (argl
[6] == NULL
) {
492 argl
[7] = talloc_asprintf(argl
, "%llu",
493 (long long unsigned)dp
->isoftlimit
);
494 if (argl
[7] == NULL
) {
498 argl
[8] = talloc_asprintf(argl
, "%llu",
499 (long long unsigned)dp
->ihardlimit
);
500 if (argl
[8] == NULL
) {
504 argl
[9] = talloc_asprintf(argl
, "%llu",
505 (long long unsigned)dp
->bsize
);
506 if (argl
[9] == NULL
) {
512 DBG_NOTICE("Running command "
521 (long long unsigned)dp
->softlimit
,
522 (long long unsigned)dp
->hardlimit
,
523 (long long unsigned)dp
->isoftlimit
,
524 (long long unsigned)dp
->ihardlimit
,
525 (long long unsigned)dp
->bsize
);
527 lines
= file_lines_ploadv(talloc_tos(), argl
, NULL
);
530 char *line
= lines
[0];
532 DEBUG (3, ("Read output from set_quota, \"%s\"\n", line
));
538 DEBUG (0, ("set_quota_command failed!\n"));
546 int sys_get_quota(const char *path
, enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*dp
)
551 char *mntpath
= NULL
;
556 smb_panic("sys_get_quota: called with NULL pointer");
558 if (command_get_quota(path
, qtype
, id
, dp
)==0) {
560 } else if (errno
!= ENOSYS
) {
564 if ((ret
=sys_path_to_bdev(path
,&mntpath
,&bdev
,&fs
))!=0) {
565 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path
));
570 DEBUG(10,("sys_get_quota() uid(%u, %u), fs(%s)\n", (unsigned)getuid(), (unsigned)geteuid(), fs
));
572 for (i
=0;(fs
&& sys_quota_backends
[i
].name
&& sys_quota_backends
[i
].get_quota
);i
++) {
573 if (strcmp(fs
,sys_quota_backends
[i
].name
)==0) {
574 ret
= sys_quota_backends
[i
].get_quota(mntpath
, bdev
, qtype
, id
, dp
);
576 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
577 fs
,mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
),strerror(errno
)));
579 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
580 fs
,mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
)));
588 /* use the default vfs quota functions */
589 ret
=sys_get_vfs_quota(mntpath
, bdev
, qtype
, id
, dp
);
591 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s\n",
592 "vfs",mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
),strerror(errno
)));
594 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
595 "vfs",mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
)));
606 int sys_set_quota(const char *path
, enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*dp
)
611 char *mntpath
= NULL
;
615 /* find the block device file */
618 smb_panic("get_smb_quota: called with NULL pointer");
620 if (command_set_quota(path
, qtype
, id
, dp
)==0) {
622 } else if (errno
!= ENOSYS
) {
626 if ((ret
=sys_path_to_bdev(path
,&mntpath
,&bdev
,&fs
))!=0) {
627 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path
));
632 DEBUG(10,("sys_set_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
634 for (i
=0;(fs
&& sys_quota_backends
[i
].name
&& sys_quota_backends
[i
].set_quota
);i
++) {
635 if (strcmp(fs
,sys_quota_backends
[i
].name
)==0) {
636 ret
= sys_quota_backends
[i
].set_quota(mntpath
, bdev
, qtype
, id
, dp
);
638 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
639 fs
,mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
),strerror(errno
)));
641 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
642 fs
,mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
)));
650 /* use the default vfs quota functions */
651 ret
=sys_set_vfs_quota(mntpath
, bdev
, qtype
, id
, dp
);
653 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
654 "vfs",mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
),strerror(errno
)));
656 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
657 "vfs",mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
)));
668 #else /* HAVE_SYS_QUOTAS */
669 void dummy_sysquotas_c(void);
671 void dummy_sysquotas_c(void)
675 #endif /* HAVE_SYS_QUOTAS */