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
;
189 get_quota_command
= lp_get_quota_command();
190 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
)) {
231 dp
->curblocks
= STR_TO_SMB_BIG_UINT(p
, &p
);
236 while (p
&& *p
&& isspace(*p
)) {
241 dp
->softlimit
= STR_TO_SMB_BIG_UINT(p
, &p
);
246 while (p
&& *p
&& isspace(*p
)) {
251 dp
->hardlimit
= STR_TO_SMB_BIG_UINT(p
, &p
);
256 while (p
&& *p
&& isspace(*p
)) {
261 dp
->curinodes
= STR_TO_SMB_BIG_UINT(p
, &p
);
266 while (p
&& *p
&& isspace(*p
)) {
271 dp
->isoftlimit
= STR_TO_SMB_BIG_UINT(p
, &p
);
276 while (p
&& *p
&& isspace(*p
)) {
281 dp
->ihardlimit
= STR_TO_SMB_BIG_UINT(p
, &p
);
286 while (p
&& *p
&& isspace(*p
)) {
291 dp
->bsize
= STR_TO_SMB_BIG_UINT(p
, NULL
);
296 file_lines_free(lines
);
299 DEBUG (3, ("Parsed output of get_quota, ...\n"));
301 #ifdef LARGE_SMB_OFF_T
303 "qflags:%u curblocks:%llu softlimit:%llu hardlimit:%llu\n"
304 "curinodes:%llu isoftlimit:%llu ihardlimit:%llu bsize:%llu\n",
305 dp
->qflags
,(long long unsigned)dp
->curblocks
,
306 (long long unsigned)dp
->softlimit
,(long long unsigned)dp
->hardlimit
,
307 (long long unsigned)dp
->curinodes
,
308 (long long unsigned)dp
->isoftlimit
,(long long unsigned)dp
->ihardlimit
,
309 (long long unsigned)dp
->bsize
));
310 #else /* LARGE_SMB_OFF_T */
312 "qflags:%u curblocks:%lu softlimit:%lu hardlimit:%lu\n"
313 "curinodes:%lu isoftlimit:%lu ihardlimit:%lu bsize:%lu\n",
314 dp
->qflags
,(long unsigned)dp
->curblocks
,
315 (long unsigned)dp
->softlimit
,(long unsigned)dp
->hardlimit
,
316 (long unsigned)dp
->curinodes
,
317 (long unsigned)dp
->isoftlimit
,(long unsigned)dp
->ihardlimit
,
318 (long unsigned)dp
->bsize
));
319 #endif /* LARGE_SMB_OFF_T */
323 DEBUG (0, ("get_quota_command failed!\n"));
332 file_lines_free(lines
);
333 DEBUG(0,("The output of get_quota_command is invalid!\n"));
337 static int command_set_quota(const char *path
, enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*dp
)
339 const char *set_quota_command
;
341 set_quota_command
= lp_set_quota_command();
342 if (set_quota_command
&& *set_quota_command
) {
348 case SMB_USER_QUOTA_TYPE
:
349 case SMB_USER_FS_QUOTA_TYPE
:
352 case SMB_GROUP_QUOTA_TYPE
:
353 case SMB_GROUP_FS_QUOTA_TYPE
:
360 #ifdef LARGE_SMB_OFF_T
361 slprintf(syscmd
, sizeof(syscmd
)-1,
365 set_quota_command
, path
, qtype
, _id
, dp
->qflags
,
366 (long long unsigned)dp
->softlimit
,(long long unsigned)dp
->hardlimit
,
367 (long long unsigned)dp
->isoftlimit
,(long long unsigned)dp
->ihardlimit
,
368 (long long unsigned)dp
->bsize
);
369 #else /* LARGE_SMB_OFF_T */
370 slprintf(syscmd
, sizeof(syscmd
)-1,
374 set_quota_command
, path
, qtype
, _id
, dp
->qflags
,
375 (long unsigned)dp
->softlimit
,(long unsigned)dp
->hardlimit
,
376 (long unsigned)dp
->isoftlimit
,(long unsigned)dp
->ihardlimit
,
377 (long unsigned)dp
->bsize
);
378 #endif /* LARGE_SMB_OFF_T */
382 DEBUG (3, ("get_quota: Running command %s\n", syscmd
));
384 lines
= file_lines_pload(syscmd
, NULL
);
386 char *line
= lines
[0];
388 DEBUG (3, ("Read output from set_quota, \"%s\"\n", line
));
390 file_lines_free(lines
);
394 DEBUG (0, ("set_quota_command failed!\n"));
402 int sys_get_quota(const char *path
, enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*dp
)
407 char *mntpath
= NULL
;
412 smb_panic("sys_get_quota: called with NULL pointer");
414 if (command_get_quota(path
, qtype
, id
, dp
)==0) {
416 } else if (errno
!= ENOSYS
) {
420 if ((ret
=sys_path_to_bdev(path
,&mntpath
,&bdev
,&fs
))!=0) {
421 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path
));
426 DEBUG(10,("sys_get_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
428 for (i
=0;(fs
&& sys_quota_backends
[i
].name
&& sys_quota_backends
[i
].get_quota
);i
++) {
429 if (strcmp(fs
,sys_quota_backends
[i
].name
)==0) {
430 ret
= sys_quota_backends
[i
].get_quota(mntpath
, bdev
, qtype
, id
, dp
);
432 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
433 fs
,mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
),strerror(errno
)));
435 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
436 fs
,mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
)));
444 /* use the default vfs quota functions */
445 ret
=sys_get_vfs_quota(mntpath
, bdev
, qtype
, id
, dp
);
447 DEBUG(3,("sys_get_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s\n",
448 "vfs",mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
),strerror(errno
)));
450 DEBUG(10,("sys_get_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
451 "vfs",mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
)));
459 if ((ret
!=0)&& (errno
== EDQUOT
)) {
460 DEBUG(10,("sys_get_quota() warning over quota!\n"));
467 int sys_set_quota(const char *path
, enum SMB_QUOTA_TYPE qtype
, unid_t id
, SMB_DISK_QUOTA
*dp
)
472 char *mntpath
= NULL
;
476 /* find the block device file */
479 smb_panic("get_smb_quota: called with NULL pointer");
481 if (command_set_quota(path
, qtype
, id
, dp
)==0) {
483 } else if (errno
!= ENOSYS
) {
487 if ((ret
=sys_path_to_bdev(path
,&mntpath
,&bdev
,&fs
))!=0) {
488 DEBUG(0,("sys_path_to_bdev() failed for path [%s]!\n",path
));
493 DEBUG(10,("sys_set_quota() uid(%u, %u)\n", (unsigned)getuid(), (unsigned)geteuid()));
495 for (i
=0;(fs
&& sys_quota_backends
[i
].name
&& sys_quota_backends
[i
].set_quota
);i
++) {
496 if (strcmp(fs
,sys_quota_backends
[i
].name
)==0) {
497 ret
= sys_quota_backends
[i
].set_quota(mntpath
, bdev
, qtype
, id
, dp
);
499 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
500 fs
,mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
),strerror(errno
)));
502 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
503 fs
,mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
)));
511 /* use the default vfs quota functions */
512 ret
=sys_set_vfs_quota(mntpath
, bdev
, qtype
, id
, dp
);
514 DEBUG(3,("sys_set_%s_quota() failed for mntpath[%s] bdev[%s] qtype[%d] id[%d]: %s.\n",
515 "vfs",mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
),strerror(errno
)));
517 DEBUG(10,("sys_set_%s_quota() called for mntpath[%s] bdev[%s] qtype[%d] id[%d].\n",
518 "vfs",mntpath
,bdev
,qtype
,(qtype
==SMB_GROUP_QUOTA_TYPE
?id
.gid
:id
.uid
)));
526 if ((ret
!=0)&& (errno
== EDQUOT
)) {
527 DEBUG(10,("sys_set_quota() warning over quota!\n"));
534 #else /* HAVE_SYS_QUOTAS */
535 void dummy_sysquotas_c(void);
537 void dummy_sysquotas_c(void)
541 #endif /* HAVE_SYS_QUOTAS */