bf498600ef30cfcea0fdcb34449bef373677f44f
[amitay/samba.git] / source3 / modules / vfs_fake_dfq.c
1 /*
2  * Fake Disk-Free and Quota VFS module.  Implements passthrough operation
3  * of all VFS calls, except for "disk free" and "get quota" which
4  * are handled by reading a text file named ".dfq" in the current directory.
5  *
6  * This module is intended for testing purposes.
7  *
8  * Copyright (C) Uri Simchoni, 2016
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, see <http://www.gnu.org/licenses/>.
22  */
23
24 #include "includes.h"
25 #include "smbd/smbd.h"
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_VFS
29
30 static int dfq_get_quota(struct vfs_handle_struct *handle, const char *path,
31                          enum SMB_QUOTA_TYPE qtype, unid_t id,
32                          SMB_DISK_QUOTA *qt);
33
34 static uint64_t dfq_load_param(int snum, const char *path, const char *section,
35                                const char *param, uint64_t def_val)
36 {
37         uint64_t ret;
38
39         char *option =
40             talloc_asprintf(talloc_tos(), "%s/%s/%s", section, param, path);
41         if (option == NULL) {
42                 return def_val;
43         }
44
45         ret = (uint64_t)lp_parm_ulonglong(snum, "fake_dfq", option,
46                                           (unsigned long long)def_val);
47
48         TALLOC_FREE(option);
49
50         return ret;
51 }
52
53 static uint64_t dfq_disk_free(vfs_handle_struct *handle, const char *path,
54                               uint64_t *bsize, uint64_t *dfree, uint64_t *dsize)
55 {
56         uint64_t free_1k;
57         int snum = SNUM(handle->conn);
58         uint64_t dfq_bsize = 0;
59         char *rpath = NULL;
60
61         /* look up the params based on real path to be resilient
62          * to refactoring of path<->realpath
63          */
64         rpath = SMB_VFS_NEXT_REALPATH(handle, path);
65         if (rpath != NULL) {
66                 dfq_bsize = dfq_load_param(snum, rpath, "df", "block size", 0);
67         }
68         if (dfq_bsize == 0) {
69                 SAFE_FREE(rpath);
70                 return SMB_VFS_NEXT_DISK_FREE(handle, path, bsize, dfree,
71                                               dsize);
72         }
73
74         *bsize = dfq_bsize;
75         *dfree = dfq_load_param(snum, rpath, "df", "disk free", 0);
76         *dsize = dfq_load_param(snum, rpath, "df", "disk size", 0);
77
78         if ((*bsize) < 1024) {
79                 free_1k = (*dfree) / (1024 / (*bsize));
80         } else {
81                 free_1k = ((*bsize) / 1024) * (*dfree);
82         }
83
84         SAFE_FREE(rpath);
85         return free_1k;
86 }
87
88 static int dfq_get_quota(struct vfs_handle_struct *handle, const char *path,
89                          enum SMB_QUOTA_TYPE qtype, unid_t id,
90                          SMB_DISK_QUOTA *qt)
91 {
92         int rc = 0;
93         int save_errno;
94         char *section = NULL;
95         int snum = SNUM(handle->conn);
96         uint64_t bsize = 0;
97         char *rpath = NULL;
98
99         rpath = SMB_VFS_NEXT_REALPATH(handle, path);
100         if (rpath == NULL) {
101                 goto dflt;
102         }
103
104         switch (qtype) {
105         case SMB_USER_QUOTA_TYPE:
106                 section = talloc_asprintf(talloc_tos(), "u%llu",
107                                           (unsigned long long)id.uid);
108                 break;
109         case SMB_GROUP_QUOTA_TYPE:
110                 section = talloc_asprintf(talloc_tos(), "g%llu",
111                                           (unsigned long long)id.gid);
112                 break;
113         case SMB_USER_FS_QUOTA_TYPE:
114                 section = talloc_strdup(talloc_tos(), "udflt");
115                 break;
116         case SMB_GROUP_FS_QUOTA_TYPE:
117                 section = talloc_strdup(talloc_tos(), "gdflt");
118                 break;
119         default:
120                 break;
121         }
122
123         if (section == NULL) {
124                 goto dflt;
125         }
126
127         bsize = dfq_load_param(snum, rpath, section, "block size", 4096);
128         if (bsize == 0) {
129                 goto dflt;
130         }
131
132         if (dfq_load_param(snum, rpath, section, "err", 0) != 0) {
133                 errno = ENOTSUP;
134                 rc = -1;
135                 goto out;
136         }
137
138         if (dfq_load_param(snum, rpath, section, "nosys", 0) != 0) {
139                 errno = ENOSYS;
140                 rc = -1;
141                 goto out;
142         }
143
144         ZERO_STRUCTP(qt);
145
146         qt->bsize = bsize;
147         qt->hardlimit = dfq_load_param(snum, rpath, section, "hard limit", 0);
148         qt->softlimit = dfq_load_param(snum, rpath, section, "soft limit", 0);
149         qt->curblocks = dfq_load_param(snum, rpath, section, "cur blocks", 0);
150         qt->ihardlimit =
151             dfq_load_param(snum, rpath, section, "inode hard limit", 0);
152         qt->isoftlimit =
153             dfq_load_param(snum, rpath, section, "inode soft limit", 0);
154         qt->curinodes = dfq_load_param(snum, rpath, section, "cur inodes", 0);
155         qt->qflags = dfq_load_param(snum, rpath, section, "qflags", QUOTAS_DENY_DISK);
156
157         if (dfq_load_param(snum, rpath, section, "edquot", 0) != 0) {
158                 errno = EDQUOT;
159                 rc = -1;
160         }
161
162         goto out;
163
164 dflt:
165         rc = SMB_VFS_NEXT_GET_QUOTA(handle, path, qtype, id, qt);
166
167 out:
168         save_errno = errno;
169         TALLOC_FREE(section);
170         SAFE_FREE(rpath);
171         errno = save_errno;
172         return rc;
173 }
174
175 struct vfs_fn_pointers vfs_fake_dfq_fns = {
176     /* Disk operations */
177
178     .disk_free_fn = dfq_disk_free,
179     .get_quota_fn = dfq_get_quota,
180 };
181
182 static_decl_vfs;
183 NTSTATUS vfs_fake_dfq_init(void)
184 {
185         return smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fake_dfq",
186                                 &vfs_fake_dfq_fns);
187 }