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
,
41 unsigned num_mount_entries
;
42 struct fileid_mount_entry
*mount_entries
;
45 /* load all the mount entries from the mtab */
46 static void fileid_load_mount_entries(struct fileid_handle_data
*data
)
51 data
->num_mount_entries
= 0;
52 TALLOC_FREE(data
->mount_entries
);
54 f
= setmntent("/etc/mtab", "r");
57 while ((m
= getmntent(f
))) {
60 struct fileid_mount_entry
*cur
;
62 if (stat(m
->mnt_dir
, &st
) != 0) continue;
63 if (statfs(m
->mnt_dir
, &sfs
) != 0) continue;
65 if (strncmp(m
->mnt_fsname
, "/dev/", 5) == 0) {
69 data
->mount_entries
= TALLOC_REALLOC_ARRAY(data
,
71 struct fileid_mount_entry
,
72 data
->num_mount_entries
+1);
73 if (data
->mount_entries
== NULL
) {
77 cur
= &data
->mount_entries
[data
->num_mount_entries
];
78 cur
->device
= st
.st_dev
;
79 cur
->mnt_fsname
= talloc_strdup(data
->mount_entries
,
81 if (!cur
->mnt_fsname
) goto nomem
;
82 cur
->fsid
= sfs
.f_fsid
;
83 cur
->devid
= (uint64_t)-1;
85 data
->num_mount_entries
++;
93 data
->num_mount_entries
= 0;
94 TALLOC_FREE(data
->mount_entries
);
99 /* find a mount entry given a dev_t */
100 static struct fileid_mount_entry
*fileid_find_mount_entry(struct fileid_handle_data
*data
,
105 if (data
->num_mount_entries
== 0) {
106 fileid_load_mount_entries(data
);
108 for (i
=0;i
<data
->num_mount_entries
;i
++) {
109 if (data
->mount_entries
[i
].device
== dev
) {
110 return &data
->mount_entries
[i
];
113 /* 2nd pass after reloading */
114 fileid_load_mount_entries(data
);
115 for (i
=0;i
<data
->num_mount_entries
;i
++) {
116 if (data
->mount_entries
[i
].device
== dev
) {
117 return &data
->mount_entries
[i
];
124 /* a 64 bit hash, based on the one in tdb */
125 static uint64_t fileid_uint64_hash(const uint8_t *s
, size_t len
)
127 uint64_t value
; /* Used to compute the hash value. */
128 uint32_t i
; /* Used to cycle through random values. */
130 /* Set the initial value from the key size. */
131 for (value
= 0x238F13AFLL
* len
, i
=0; i
< len
; i
++)
132 value
= (value
+ (s
[i
] << (i
*5 % 24)));
134 return (1103515243LL * value
+ 12345LL);
137 /* a device mapping using a fsname */
138 static uint64_t fileid_device_mapping_fsname(struct fileid_handle_data
*data
,
141 struct fileid_mount_entry
*m
;
143 m
= fileid_find_mount_entry(data
, dev
);
146 if (m
->devid
== (uint64_t)-1) {
147 m
->devid
= fileid_uint64_hash((uint8_t *)m
->mnt_fsname
,
148 strlen(m
->mnt_fsname
));
154 /* device mapping functions using a fsid */
155 static uint64_t fileid_device_mapping_fsid(struct fileid_handle_data
*data
,
158 struct fileid_mount_entry
*m
;
160 m
= fileid_find_mount_entry(data
, dev
);
163 if (m
->devid
== (uint64_t)-1) {
164 if (sizeof(fsid_t
) > sizeof(uint64_t)) {
165 m
->devid
= fileid_uint64_hash((uint8_t *)&m
->fsid
,
181 static int fileid_connect(struct vfs_handle_struct
*handle
,
182 const char *service
, const char *user
)
184 struct fileid_handle_data
*data
;
185 const char *algorithm
;
186 int ret
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
192 data
= talloc_zero(handle
->conn
, struct fileid_handle_data
);
194 SMB_VFS_NEXT_DISCONNECT(handle
);
195 DEBUG(0, ("talloc_zero() failed\n"));
200 * "fileid:mapping" is only here as fallback for old setups
201 * "fileid:algorithm" is the option new setups should use
203 algorithm
= lp_parm_const_string(SNUM(handle
->conn
),
206 algorithm
= lp_parm_const_string(SNUM(handle
->conn
),
207 "fileid", "algorithm",
209 if (strcmp("fsname", algorithm
) == 0) {
210 data
->device_mapping_fn
= fileid_device_mapping_fsname
;
211 } else if (strcmp("fsid", algorithm
) == 0) {
212 data
->device_mapping_fn
= fileid_device_mapping_fsid
;
214 SMB_VFS_NEXT_DISCONNECT(handle
);
215 DEBUG(0,("fileid_connect(): unknown algorithm[%s]\n", algorithm
));
219 SMB_VFS_HANDLE_SET_DATA(handle
, data
, NULL
,
220 struct fileid_handle_data
,
223 DEBUG(10, ("fileid_connect(): connect to service[%s] with algorithm[%s]\n",
224 service
, algorithm
));
229 static void fileid_disconnect(struct vfs_handle_struct
*handle
)
231 DEBUG(10,("fileid_disconnect() connect to service[%s].\n",
232 lp_servicename(SNUM(handle
->conn
))));
234 SMB_VFS_NEXT_DISCONNECT(handle
);
237 static struct file_id
fileid_file_id_create(struct vfs_handle_struct
*handle
,
238 const SMB_STRUCT_STAT
*sbuf
)
240 struct fileid_handle_data
*data
;
245 SMB_VFS_HANDLE_GET_DATA(handle
, data
,
246 struct fileid_handle_data
,
249 id
.devid
= data
->device_mapping_fn(data
, sbuf
->st_ex_dev
);
250 id
.inode
= sbuf
->st_ex_ino
;
255 static struct vfs_fn_pointers vfs_fileid_fns
= {
256 .connect_fn
= fileid_connect
,
257 .disconnect
= fileid_disconnect
,
258 .file_id_create
= fileid_file_id_create
261 NTSTATUS
vfs_fileid_init(void);
262 NTSTATUS
vfs_fileid_init(void)
266 ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "fileid",
268 if (!NT_STATUS_IS_OK(ret
)) {
272 vfs_fileid_debug_level
= debug_add_class("fileid");
273 if (vfs_fileid_debug_level
== -1) {
274 vfs_fileid_debug_level
= DBGC_VFS
;
275 DEBUG(0, ("vfs_fileid: Couldn't register custom debugging class!\n"));
277 DEBUG(10, ("vfs_fileid: Debug class number of 'fileid': %d\n", vfs_fileid_debug_level
));