2 * VFS module to alter the algorithm to calculate
3 * the struct file_id used as key for the share mode
4 * and byte range locking db's.
6 * Copyright (C) 2007, Stefan Metzmacher
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "smbd/smbd.h"
24 #include "system/filesys.h"
26 static int vfs_fileid_debug_level = DBGC_VFS;
29 #define DBGC_CLASS vfs_fileid_debug_level
31 struct fileid_mount_entry {
33 const char *mnt_fsname;
38 struct fileid_handle_data {
39 uint64_t (*device_mapping_fn)(struct fileid_handle_data *data,
40 const SMB_STRUCT_STAT *sbuf);
41 uint64_t (*extid_mapping_fn)(struct fileid_handle_data *data,
42 const SMB_STRUCT_STAT *sbuf);
43 char **fstype_deny_list;
44 char **fstype_allow_list;
45 char **mntdir_deny_list;
46 char **mntdir_allow_list;
47 unsigned num_mount_entries;
48 struct fileid_mount_entry *mount_entries;
52 /* check if a mount entry is allowed based on fstype and mount directory */
53 static bool fileid_mount_entry_allowed(struct fileid_handle_data *data,
57 char **fstype_deny = data->fstype_deny_list;
58 char **fstype_allow = data->fstype_allow_list;
59 char **mntdir_deny = data->mntdir_deny_list;
60 char **mntdir_allow = data->mntdir_allow_list;
62 if (fstype_deny != NULL) {
63 for (i = 0; fstype_deny[i] != NULL; i++) {
64 if (strcmp(m->mnt_type, fstype_deny[i]) == 0) {
69 if (fstype_allow != NULL) {
70 for (i = 0; fstype_allow[i] != NULL; i++) {
71 if (strcmp(m->mnt_type, fstype_allow[i]) == 0) {
75 if (fstype_allow[i] == NULL) {
79 if (mntdir_deny != NULL) {
80 for (i=0; mntdir_deny[i] != NULL; i++) {
81 if (strcmp(m->mnt_dir, mntdir_deny[i]) == 0) {
86 if (mntdir_allow != NULL) {
87 for (i=0; mntdir_allow[i] != NULL; i++) {
88 if (strcmp(m->mnt_dir, mntdir_allow[i]) == 0) {
92 if (mntdir_allow[i] == NULL) {
100 /* load all the mount entries from the mtab */
101 static void fileid_load_mount_entries(struct fileid_handle_data *data)
106 data->num_mount_entries = 0;
107 TALLOC_FREE(data->mount_entries);
109 f = setmntent("/etc/mtab", "r");
112 while ((m = getmntent(f))) {
115 struct fileid_mount_entry *cur;
118 allowed = fileid_mount_entry_allowed(data, m);
120 DBG_DEBUG("skipping mount entry %s\n", m->mnt_dir);
123 if (stat(m->mnt_dir, &st) != 0) continue;
124 if (statfs(m->mnt_dir, &sfs) != 0) continue;
126 if (strncmp(m->mnt_fsname, "/dev/", 5) == 0) {
130 data->mount_entries = talloc_realloc(data,
132 struct fileid_mount_entry,
133 data->num_mount_entries+1);
134 if (data->mount_entries == NULL) {
138 cur = &data->mount_entries[data->num_mount_entries];
139 cur->device = st.st_dev;
140 cur->mnt_fsname = talloc_strdup(data->mount_entries,
142 if (!cur->mnt_fsname) goto nomem;
143 cur->fsid = sfs.f_fsid;
144 cur->devid = (uint64_t)-1;
146 data->num_mount_entries++;
154 data->num_mount_entries = 0;
155 TALLOC_FREE(data->mount_entries);
160 /* find a mount entry given a dev_t */
161 static struct fileid_mount_entry *fileid_find_mount_entry(struct fileid_handle_data *data,
166 if (data->num_mount_entries == 0) {
167 fileid_load_mount_entries(data);
169 for (i=0;i<data->num_mount_entries;i++) {
170 if (data->mount_entries[i].device == dev) {
171 return &data->mount_entries[i];
174 /* 2nd pass after reloading */
175 fileid_load_mount_entries(data);
176 for (i=0;i<data->num_mount_entries;i++) {
177 if (data->mount_entries[i].device == dev) {
178 return &data->mount_entries[i];
185 /* a 64 bit hash, based on the one in tdb */
186 static uint64_t fileid_uint64_hash(const uint8_t *s, size_t len)
188 uint64_t value; /* Used to compute the hash value. */
189 uint32_t i; /* Used to cycle through random values. */
191 /* Set the initial value from the key size. */
192 for (value = 0x238F13AFLL * len, i=0; i < len; i++)
193 value = (value + (((uint64_t)s[i]) << (i*5 % 24)));
195 return (1103515243LL * value + 12345LL);
198 /* a device mapping using a fsname */
199 static uint64_t fileid_device_mapping_fsname(struct fileid_handle_data *data,
200 const SMB_STRUCT_STAT *sbuf)
202 struct fileid_mount_entry *m;
204 m = fileid_find_mount_entry(data, sbuf->st_ex_dev);
205 if (!m) return sbuf->st_ex_dev;
207 if (m->devid == (uint64_t)-1) {
208 m->devid = fileid_uint64_hash((const uint8_t *)m->mnt_fsname,
209 strlen(m->mnt_fsname));
215 /* a device mapping using a hostname */
216 static uint64_t fileid_device_mapping_hostname(struct fileid_handle_data *data,
217 const SMB_STRUCT_STAT *sbuf)
219 char hostname[HOST_NAME_MAX+1];
220 char *devname = NULL;
225 rc = gethostname(hostname, HOST_NAME_MAX+1);
227 DBG_ERR("gethostname failed\n");
231 devname = talloc_asprintf(talloc_tos(), "%s%ju",
232 hostname, (uintmax_t)sbuf->st_ex_dev);
233 if (devname == NULL) {
234 DBG_ERR("talloc_asprintf failed\n");
237 devname_len = talloc_array_length(devname) - 1;
239 id = fileid_uint64_hash((uint8_t *)devname, devname_len);
241 TALLOC_FREE(devname);
246 /* a device mapping using a fsname for files and hostname for dirs */
247 static uint64_t fileid_device_mapping_fsname_nodirs(
248 struct fileid_handle_data *data,
249 const SMB_STRUCT_STAT *sbuf)
251 if (S_ISDIR(sbuf->st_ex_mode)) {
252 return fileid_device_mapping_hostname(data, sbuf);
255 return fileid_device_mapping_fsname(data, sbuf);
258 /* device mapping functions using a fsid */
259 static uint64_t fileid_device_mapping_fsid(struct fileid_handle_data *data,
260 const SMB_STRUCT_STAT *sbuf)
262 struct fileid_mount_entry *m;
264 m = fileid_find_mount_entry(data, sbuf->st_ex_dev);
265 if (!m) return sbuf->st_ex_dev;
267 if (m->devid == (uint64_t)-1) {
268 if (sizeof(fsid_t) > sizeof(uint64_t)) {
269 m->devid = fileid_uint64_hash((uint8_t *)&m->fsid,
285 static uint64_t fileid_extid_mapping_zero(struct fileid_handle_data *data,
286 const SMB_STRUCT_STAT *sbuf)
291 static uint64_t fileid_extid_mapping_pid(struct fileid_handle_data *data,
292 const SMB_STRUCT_STAT *sbuf)
297 static int get_connectpath_ino(struct vfs_handle_struct *handle,
300 struct smb_filename *fname = NULL;
303 fname = synthetic_smb_fname(talloc_tos(),
304 handle->conn->connectpath,
309 DBG_ERR("synthetic_smb_fname failed\n");
313 ret = SMB_VFS_NEXT_STAT(handle, fname);
315 DBG_ERR("stat failed for %s with %s\n",
316 handle->conn->connectpath, strerror(errno));
320 *ino = fname->st.st_ex_ino;
326 static int fileid_connect(struct vfs_handle_struct *handle,
327 const char *service, const char *user)
329 struct fileid_handle_data *data;
330 const char *algorithm;
331 const char **fstype_deny_list = NULL;
332 const char **fstype_allow_list = NULL;
333 const char **mntdir_deny_list = NULL;
334 const char **mntdir_allow_list = NULL;
336 int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
342 data = talloc_zero(handle->conn, struct fileid_handle_data);
345 SMB_VFS_NEXT_DISCONNECT(handle);
346 DEBUG(0, ("talloc_zero() failed\n"));
351 data->nolockinode = 0;
354 * "fileid:mapping" is only here as fallback for old setups
355 * "fileid:algorithm" is the option new setups should use
357 algorithm = lp_parm_const_string(SNUM(handle->conn),
360 algorithm = lp_parm_const_string(SNUM(handle->conn),
361 "fileid", "algorithm",
363 if (strcmp("fsname", algorithm) == 0) {
364 data->device_mapping_fn = fileid_device_mapping_fsname;
365 data->extid_mapping_fn = fileid_extid_mapping_zero;
366 } else if (strcmp("fsname_nodirs", algorithm) == 0) {
367 data->device_mapping_fn = fileid_device_mapping_fsname_nodirs;
368 data->extid_mapping_fn = fileid_extid_mapping_zero;
369 } else if (strcmp("fsid", algorithm) == 0) {
370 data->device_mapping_fn = fileid_device_mapping_fsid;
371 data->extid_mapping_fn = fileid_extid_mapping_zero;
372 } else if (strcmp("hostname", algorithm) == 0) {
373 data->device_mapping_fn = fileid_device_mapping_hostname;
374 data->extid_mapping_fn = fileid_extid_mapping_zero;
375 } else if (strcmp("fsname_norootdir", algorithm) == 0) {
376 data->device_mapping_fn = fileid_device_mapping_fsname;
377 data->extid_mapping_fn = fileid_extid_mapping_zero;
379 ret = get_connectpath_ino(handle, &data->nolockinode);
382 SMB_VFS_NEXT_DISCONNECT(handle);
386 } else if (strcmp("fsname_norootdir_ext", algorithm) == 0) {
387 data->device_mapping_fn = fileid_device_mapping_fsname;
388 data->extid_mapping_fn = fileid_extid_mapping_pid;
390 ret = get_connectpath_ino(handle, &data->nolockinode);
393 SMB_VFS_NEXT_DISCONNECT(handle);
398 SMB_VFS_NEXT_DISCONNECT(handle);
399 DEBUG(0,("fileid_connect(): unknown algorithm[%s]\n", algorithm));
403 fstype_deny_list = lp_parm_string_list(SNUM(handle->conn), "fileid",
404 "fstype deny", NULL);
405 if (fstype_deny_list != NULL) {
406 data->fstype_deny_list = str_list_copy(data, fstype_deny_list);
407 if (data->fstype_deny_list == NULL) {
409 DBG_ERR("str_list_copy failed\n");
410 SMB_VFS_NEXT_DISCONNECT(handle);
416 fstype_allow_list = lp_parm_string_list(SNUM(handle->conn), "fileid",
417 "fstype allow", NULL);
418 if (fstype_allow_list != NULL) {
419 data->fstype_allow_list = str_list_copy(data, fstype_allow_list);
420 if (data->fstype_allow_list == NULL) {
422 DBG_ERR("str_list_copy failed\n");
423 SMB_VFS_NEXT_DISCONNECT(handle);
429 mntdir_deny_list = lp_parm_string_list(SNUM(handle->conn), "fileid",
430 "mntdir deny", NULL);
431 if (mntdir_deny_list != NULL) {
432 data->mntdir_deny_list = str_list_copy(data, mntdir_deny_list);
433 if (data->mntdir_deny_list == NULL) {
435 DBG_ERR("str_list_copy failed\n");
436 SMB_VFS_NEXT_DISCONNECT(handle);
442 mntdir_allow_list = lp_parm_string_list(SNUM(handle->conn), "fileid",
443 "mntdir allow", NULL);
444 if (mntdir_allow_list != NULL) {
445 data->mntdir_allow_list = str_list_copy(data, mntdir_allow_list);
446 if (data->mntdir_allow_list == NULL) {
448 DBG_ERR("str_list_copy failed\n");
449 SMB_VFS_NEXT_DISCONNECT(handle);
455 data->nolockinode = lp_parm_ulong(SNUM(handle->conn), "fileid",
456 "nolockinode", data->nolockinode);
458 SMB_VFS_HANDLE_SET_DATA(handle, data, NULL,
459 struct fileid_handle_data,
462 DBG_DEBUG("connect to service[%s] with algorithm[%s] nolockinode %lli\n",
463 service, algorithm, (long long) data->nolockinode);
468 static void fileid_disconnect(struct vfs_handle_struct *handle)
470 const struct loadparm_substitution *lp_sub =
471 loadparm_s3_global_substitution();
473 DEBUG(10,("fileid_disconnect() connect to service[%s].\n",
474 lp_servicename(talloc_tos(), lp_sub, SNUM(handle->conn))));
476 SMB_VFS_NEXT_DISCONNECT(handle);
479 static struct file_id fileid_file_id_create(struct vfs_handle_struct *handle,
480 const SMB_STRUCT_STAT *sbuf)
482 struct fileid_handle_data *data;
488 SMB_VFS_HANDLE_GET_DATA(handle, data,
489 struct fileid_handle_data,
492 if ((data->nolockinode != 0) &&
493 (sbuf->st_ex_ino == data->nolockinode)) {
494 devid = fileid_device_mapping_hostname(data, sbuf);
495 id.extid = data->extid_mapping_fn(data, sbuf);
497 devid = data->device_mapping_fn(data, sbuf);
500 id.inode = sbuf->st_ex_ino;
503 DBG_DEBUG("Returning dev [%jx] inode [%jx] extid [%jx]\n",
504 (uintmax_t)id.devid, (uintmax_t)id.inode, (uintmax_t)id.extid);
509 static struct vfs_fn_pointers vfs_fileid_fns = {
510 .connect_fn = fileid_connect,
511 .disconnect_fn = fileid_disconnect,
512 .file_id_create_fn = fileid_file_id_create
516 NTSTATUS vfs_fileid_init(TALLOC_CTX *ctx)
520 ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fileid",
522 if (!NT_STATUS_IS_OK(ret)) {
526 vfs_fileid_debug_level = debug_add_class("fileid");
527 if (vfs_fileid_debug_level == -1) {
528 vfs_fileid_debug_level = DBGC_VFS;
529 DEBUG(0, ("vfs_fileid: Couldn't register custom debugging class!\n"));
531 DEBUG(10, ("vfs_fileid: Debug class number of 'fileid': %d\n", vfs_fileid_debug_level));