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.
6 * This module is intended for testing purposes.
8 * Copyright (C) Uri Simchoni, 2016
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.
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.
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/>.
25 #include "smbd/smbd.h"
28 #define DBGC_CLASS DBGC_VFS
30 static int dfq_get_quota(struct vfs_handle_struct
*handle
,
31 const struct smb_filename
*smb_fname
,
32 enum SMB_QUOTA_TYPE qtype
,
36 static uint64_t dfq_load_param(int snum
, const char *path
, const char *section
,
37 const char *param
, uint64_t def_val
)
42 talloc_asprintf(talloc_tos(), "%s/%s/%s", section
, param
, path
);
47 ret
= (uint64_t)lp_parm_ulonglong(snum
, "fake_dfq", option
,
48 (unsigned long long)def_val
);
55 static uint64_t dfq_disk_free(vfs_handle_struct
*handle
,
56 const struct smb_filename
*smb_fname
,
62 int snum
= SNUM(handle
->conn
);
63 uint64_t dfq_bsize
= 0;
64 struct smb_filename
*rpath_fname
= NULL
;
66 /* look up the params based on real path to be resilient
67 * to refactoring of path<->realpath
69 rpath_fname
= SMB_VFS_NEXT_REALPATH(handle
, talloc_tos(), smb_fname
);
70 if (rpath_fname
!= NULL
) {
71 dfq_bsize
= dfq_load_param(snum
, rpath_fname
->base_name
,
72 "df", "block size", 0);
75 TALLOC_FREE(rpath_fname
);
76 return SMB_VFS_NEXT_DISK_FREE(handle
, smb_fname
, bsize
, dfree
,
81 *dfree
= dfq_load_param(snum
, rpath_fname
->base_name
,
82 "df", "disk free", 0);
83 *dsize
= dfq_load_param(snum
, rpath_fname
->base_name
,
84 "df", "disk size", 0);
86 if ((*bsize
) < 1024) {
87 free_1k
= (*dfree
) / (1024 / (*bsize
));
89 free_1k
= ((*bsize
) / 1024) * (*dfree
);
92 TALLOC_FREE(rpath_fname
);
96 static int dfq_get_quota(struct vfs_handle_struct
*handle
,
97 const struct smb_filename
*smb_fname
,
98 enum SMB_QUOTA_TYPE qtype
,
104 char *section
= NULL
;
105 int snum
= SNUM(handle
->conn
);
107 struct smb_filename
*rpath_fname
= NULL
;
109 rpath_fname
= SMB_VFS_NEXT_REALPATH(handle
, talloc_tos(), smb_fname
);
110 if (rpath_fname
== NULL
) {
115 case SMB_USER_QUOTA_TYPE
:
116 section
= talloc_asprintf(talloc_tos(), "u%llu",
117 (unsigned long long)id
.uid
);
119 case SMB_GROUP_QUOTA_TYPE
:
120 section
= talloc_asprintf(talloc_tos(), "g%llu",
121 (unsigned long long)id
.gid
);
123 case SMB_USER_FS_QUOTA_TYPE
:
124 section
= talloc_strdup(talloc_tos(), "udflt");
126 case SMB_GROUP_FS_QUOTA_TYPE
:
127 section
= talloc_strdup(talloc_tos(), "gdflt");
133 if (section
== NULL
) {
137 bsize
= dfq_load_param(snum
, rpath_fname
->base_name
,
138 section
, "block size", 4096);
143 if (dfq_load_param(snum
, rpath_fname
->base_name
,
144 section
, "err", 0) != 0) {
150 if (dfq_load_param(snum
, rpath_fname
->base_name
,
151 section
, "nosys", 0) != 0) {
160 qt
->hardlimit
= dfq_load_param(snum
, rpath_fname
->base_name
,
161 section
, "hard limit", 0);
162 qt
->softlimit
= dfq_load_param(snum
, rpath_fname
->base_name
,
163 section
, "soft limit", 0);
164 qt
->curblocks
= dfq_load_param(snum
, rpath_fname
->base_name
,
165 section
, "cur blocks", 0);
167 dfq_load_param(snum
, rpath_fname
->base_name
,
168 section
, "inode hard limit", 0);
170 dfq_load_param(snum
, rpath_fname
->base_name
,
171 section
, "inode soft limit", 0);
172 qt
->curinodes
= dfq_load_param(snum
, rpath_fname
->base_name
,
173 section
, "cur inodes", 0);
174 qt
->qflags
= dfq_load_param(snum
, rpath_fname
->base_name
,
175 section
, "qflags", QUOTAS_DENY_DISK
);
180 rc
= SMB_VFS_NEXT_GET_QUOTA(handle
, smb_fname
, qtype
, id
, qt
);
184 TALLOC_FREE(section
);
185 TALLOC_FREE(rpath_fname
);
190 static void dfq_fake_stat(struct vfs_handle_struct
*handle
,
191 struct smb_filename
*smb_fname
,
192 SMB_STRUCT_STAT
*sbuf
)
194 int snum
= SNUM(handle
->conn
);
195 char *full_path
= NULL
;
196 char *to_free
= NULL
;
197 char path
[PATH_MAX
+ 1];
201 len
= full_path_tos(handle
->conn
->cwd_fsp
->fsp_name
->base_name
,
202 smb_fname
->base_name
,
204 &full_path
, &to_free
);
206 DBG_ERR("Could not allocate memory in full_path_tos.\n");
210 gid
= dfq_load_param(snum
, full_path
, "stat", "sgid", 0);
212 sbuf
->st_ex_gid
= gid
;
213 sbuf
->st_ex_mode
|= S_ISGID
;
216 TALLOC_FREE(to_free
);
219 static int dfq_stat(vfs_handle_struct
*handle
,
220 struct smb_filename
*smb_fname
)
224 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
229 dfq_fake_stat(handle
, smb_fname
, &smb_fname
->st
);
234 static int dfq_fstat(vfs_handle_struct
*handle
,
236 SMB_STRUCT_STAT
*sbuf
)
240 ret
= SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
245 dfq_fake_stat(handle
, fsp
->fsp_name
, sbuf
);
250 static int dfq_lstat(vfs_handle_struct
*handle
,
251 struct smb_filename
*smb_fname
)
255 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
260 dfq_fake_stat(handle
, smb_fname
, &smb_fname
->st
);
265 struct vfs_fn_pointers vfs_fake_dfq_fns
= {
266 /* Disk operations */
268 .disk_free_fn
= dfq_disk_free
,
269 .get_quota_fn
= dfq_get_quota
,
272 .fstat_fn
= dfq_fstat
,
273 .lstat_fn
= dfq_lstat
,
277 NTSTATUS
vfs_fake_dfq_init(TALLOC_CTX
*ctx
)
279 return smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "fake_dfq",