s3:vfs_fileid: maintain an array of nolock inodes
[samba.git] / source3 / modules / vfs_fileid.c
1 /*
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.
5  *
6  * Copyright (C) 2007, Stefan Metzmacher
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "system/filesys.h"
25
26 static int vfs_fileid_debug_level = DBGC_VFS;
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS vfs_fileid_debug_level
30
31 struct fileid_mount_entry {
32         SMB_DEV_T device;
33         const char *mnt_fsname;
34         fsid_t fsid;
35         uint64_t devid;
36 };
37
38 struct fileid_nolock_inode {
39         dev_t dev;
40         ino_t ino;
41 };
42
43 struct fileid_handle_data {
44         struct file_id (*mapping_fn)(struct fileid_handle_data *data,
45                                      const SMB_STRUCT_STAT *sbuf);
46         char **fstype_deny_list;
47         char **fstype_allow_list;
48         char **mntdir_deny_list;
49         char **mntdir_allow_list;
50         unsigned num_mount_entries;
51         struct fileid_mount_entry *mount_entries;
52         struct {
53                 size_t num_inodes;
54                 struct fileid_nolock_inode *inodes;
55         } nolock;
56 };
57
58 /* check if a mount entry is allowed based on fstype and mount directory */
59 static bool fileid_mount_entry_allowed(struct fileid_handle_data *data,
60                                        struct mntent *m)
61 {
62         int i;
63         char **fstype_deny = data->fstype_deny_list;
64         char **fstype_allow = data->fstype_allow_list;
65         char **mntdir_deny = data->mntdir_deny_list;
66         char **mntdir_allow = data->mntdir_allow_list;
67
68         if (fstype_deny != NULL) {
69                 for (i = 0; fstype_deny[i] != NULL; i++) {
70                         if (strcmp(m->mnt_type, fstype_deny[i]) == 0) {
71                                 return false;
72                         }
73                 }
74         }
75         if (fstype_allow != NULL) {
76                 for (i = 0; fstype_allow[i] != NULL; i++) {
77                         if (strcmp(m->mnt_type, fstype_allow[i]) == 0) {
78                                 break;
79                         }
80                 }
81                 if (fstype_allow[i] == NULL) {
82                         return false;
83                 }
84         }
85         if (mntdir_deny != NULL) {
86                 for (i=0; mntdir_deny[i] != NULL; i++) {
87                         if (strcmp(m->mnt_dir, mntdir_deny[i]) == 0) {
88                                 return false;
89                         }
90                 }
91         }
92         if (mntdir_allow != NULL) {
93                 for (i=0; mntdir_allow[i] != NULL; i++) {
94                         if (strcmp(m->mnt_dir, mntdir_allow[i]) == 0) {
95                                 break;
96                         }
97                 }
98                 if (mntdir_allow[i] == NULL) {
99                         return false;
100                 }
101         }
102         return true;
103 }
104
105
106 /* load all the mount entries from the mtab */
107 static void fileid_load_mount_entries(struct fileid_handle_data *data)
108 {
109         FILE *f;
110         struct mntent *m;
111
112         data->num_mount_entries = 0;
113         TALLOC_FREE(data->mount_entries);
114
115         f = setmntent("/etc/mtab", "r");
116         if (!f) return;
117
118         while ((m = getmntent(f))) {
119                 struct stat st;
120                 struct statfs sfs;
121                 struct fileid_mount_entry *cur;
122                 bool allowed;
123
124                 allowed = fileid_mount_entry_allowed(data, m);
125                 if (!allowed) {
126                         DBG_DEBUG("skipping mount entry %s\n", m->mnt_dir);
127                         continue;
128                 }
129                 if (stat(m->mnt_dir, &st) != 0) continue;
130                 if (statfs(m->mnt_dir, &sfs) != 0) continue;
131
132                 if (strncmp(m->mnt_fsname, "/dev/", 5) == 0) {
133                         m->mnt_fsname += 5;
134                 }
135
136                 data->mount_entries = talloc_realloc(data,
137                                                            data->mount_entries,
138                                                            struct fileid_mount_entry,
139                                                            data->num_mount_entries+1);
140                 if (data->mount_entries == NULL) {
141                         goto nomem;
142                 }
143
144                 cur = &data->mount_entries[data->num_mount_entries];
145                 cur->device     = st.st_dev;
146                 cur->mnt_fsname = talloc_strdup(data->mount_entries,
147                                                 m->mnt_fsname);
148                 if (!cur->mnt_fsname) goto nomem;
149                 cur->fsid       = sfs.f_fsid;
150                 cur->devid      = (uint64_t)-1;
151
152                 data->num_mount_entries++;
153         }
154         endmntent(f);
155         return;
156         
157 nomem:
158         if (f) endmntent(f);
159
160         data->num_mount_entries = 0;
161         TALLOC_FREE(data->mount_entries);
162
163         return;
164 }
165
166 /* find a mount entry given a dev_t */
167 static struct fileid_mount_entry *fileid_find_mount_entry(struct fileid_handle_data *data,
168                                                           SMB_DEV_T dev)
169 {
170         unsigned i;
171
172         if (data->num_mount_entries == 0) {
173                 fileid_load_mount_entries(data);
174         }
175         for (i=0;i<data->num_mount_entries;i++) {
176                 if (data->mount_entries[i].device == dev) {
177                         return &data->mount_entries[i];
178                 }
179         }
180         /* 2nd pass after reloading */
181         fileid_load_mount_entries(data);
182         for (i=0;i<data->num_mount_entries;i++) {
183                 if (data->mount_entries[i].device == dev) {
184                         return &data->mount_entries[i];
185                 }
186         }       
187         return NULL;
188 }
189
190
191 /* a 64 bit hash, based on the one in tdb */
192 static uint64_t fileid_uint64_hash(const uint8_t *s, size_t len)
193 {
194         uint64_t value; /* Used to compute the hash value.  */
195         uint32_t i;     /* Used to cycle through random values. */
196
197         /* Set the initial value from the key size. */
198         for (value = 0x238F13AFLL * len, i=0; i < len; i++)
199                 value = (value + (((uint64_t)s[i]) << (i*5 % 24)));
200
201         return (1103515243LL * value + 12345LL);
202 }
203
204 /* a device mapping using a fsname */
205 static uint64_t fileid_device_mapping_fsname(struct fileid_handle_data *data,
206                                              const SMB_STRUCT_STAT *sbuf)
207 {
208         struct fileid_mount_entry *m;
209
210         m = fileid_find_mount_entry(data, sbuf->st_ex_dev);
211         if (!m) return sbuf->st_ex_dev;
212
213         if (m->devid == (uint64_t)-1) {
214                 m->devid = fileid_uint64_hash((const uint8_t *)m->mnt_fsname,
215                                               strlen(m->mnt_fsname));
216         }
217
218         return m->devid;
219 }
220
221 static struct file_id fileid_mapping_fsname(struct fileid_handle_data *data,
222                                             const SMB_STRUCT_STAT *sbuf)
223 {
224         struct file_id id = { .inode = sbuf->st_ex_ino, };
225
226         id.devid = fileid_device_mapping_fsname(data, sbuf);
227
228         return id;
229 }
230
231 /* a device mapping using a hostname */
232 static uint64_t fileid_device_mapping_hostname(struct fileid_handle_data *data,
233                                                const SMB_STRUCT_STAT *sbuf)
234 {
235         char hostname[HOST_NAME_MAX+1];
236         char *devname = NULL;
237         uint64_t id;
238         size_t devname_len;
239         int rc;
240
241         rc = gethostname(hostname, HOST_NAME_MAX+1);
242         if (rc != 0) {
243                 DBG_ERR("gethostname failed\n");
244                 return UINT64_MAX;
245         }
246
247         devname = talloc_asprintf(talloc_tos(), "%s%ju",
248                                   hostname, (uintmax_t)sbuf->st_ex_dev);
249         if (devname == NULL) {
250                 DBG_ERR("talloc_asprintf failed\n");
251                 return UINT64_MAX;
252         }
253         devname_len = talloc_array_length(devname) - 1;
254
255         id = fileid_uint64_hash((uint8_t *)devname, devname_len);
256
257         TALLOC_FREE(devname);
258
259         return id;
260 }
261
262 static struct file_id fileid_mapping_hostname(struct fileid_handle_data *data,
263                                               const SMB_STRUCT_STAT *sbuf)
264 {
265         struct file_id id = { .inode = sbuf->st_ex_ino, };
266
267         id.devid = fileid_device_mapping_hostname(data, sbuf);
268
269         return id;
270 }
271
272 static bool fileid_is_nolock_inode(struct fileid_handle_data *data,
273                                    const SMB_STRUCT_STAT *sbuf)
274 {
275         size_t i;
276
277         /*
278          * We could make this a binary search over an sorted array,
279          * but for now we keep things simple.
280          */
281
282         for (i=0; i < data->nolock.num_inodes; i++) {
283                 if (data->nolock.inodes[i].ino != sbuf->st_ex_ino) {
284                         continue;
285                 }
286
287                 if (data->nolock.inodes[i].dev == 0) {
288                         /*
289                          * legacy "fileid:nolockinode"
290                          * handling ignoring dev
291                          */
292                         return true;
293                 }
294
295                 if (data->nolock.inodes[i].dev != sbuf->st_ex_dev) {
296                         continue;
297                 }
298
299                 return true;
300         }
301
302         return false;
303 }
304
305 static int fileid_add_nolock_inode(struct fileid_handle_data *data,
306                                    const SMB_STRUCT_STAT *sbuf)
307 {
308         bool exists = fileid_is_nolock_inode(data, sbuf);
309         struct fileid_nolock_inode *inodes = NULL;
310
311         if (exists) {
312                 return 0;
313         }
314
315         inodes = talloc_realloc(data, data->nolock.inodes,
316                                 struct fileid_nolock_inode,
317                                 data->nolock.num_inodes + 1);
318         if (inodes == NULL) {
319                 return -1;
320         }
321
322         inodes[data->nolock.num_inodes] = (struct fileid_nolock_inode) {
323                 .dev = sbuf->st_ex_dev,
324                 .ino = sbuf->st_ex_ino,
325         };
326         data->nolock.inodes = inodes;
327         data->nolock.num_inodes += 1;
328
329         return 0;
330 }
331
332 /* a device mapping using a fsname for files and hostname for dirs */
333 static struct file_id fileid_mapping_fsname_nodirs(
334         struct fileid_handle_data *data,
335         const SMB_STRUCT_STAT *sbuf)
336 {
337         if (S_ISDIR(sbuf->st_ex_mode)) {
338                 return fileid_mapping_hostname(data, sbuf);
339         }
340
341         return fileid_mapping_fsname(data, sbuf);
342 }
343
344 static struct file_id fileid_mapping_fsname_norootdir(
345         struct fileid_handle_data *data,
346         const SMB_STRUCT_STAT *sbuf)
347 {
348         if (fileid_is_nolock_inode(data, sbuf)) {
349                 return fileid_mapping_hostname(data, sbuf);
350         }
351
352         return fileid_mapping_fsname(data, sbuf);
353 }
354
355 static struct file_id fileid_mapping_fsname_norootdir_ext(
356         struct fileid_handle_data *data,
357         const SMB_STRUCT_STAT *sbuf)
358 {
359         if (fileid_is_nolock_inode(data, sbuf)) {
360                 struct file_id id = fileid_mapping_hostname(data, sbuf);
361                 id.extid = getpid();
362                 return id;
363         }
364
365         return fileid_mapping_fsname(data, sbuf);
366 }
367
368 /* device mapping functions using a fsid */
369 static uint64_t fileid_device_mapping_fsid(struct fileid_handle_data *data,
370                                            const SMB_STRUCT_STAT *sbuf)
371 {
372         struct fileid_mount_entry *m;
373
374         m = fileid_find_mount_entry(data, sbuf->st_ex_dev);
375         if (!m) return sbuf->st_ex_dev;
376
377         if (m->devid == (uint64_t)-1) {
378                 if (sizeof(fsid_t) > sizeof(uint64_t)) {
379                         m->devid = fileid_uint64_hash((uint8_t *)&m->fsid,
380                                                       sizeof(m->fsid));
381                 } else {
382                         union {
383                                 uint64_t ret;
384                                 fsid_t fsid;
385                         } u;
386                         ZERO_STRUCT(u);
387                         u.fsid = m->fsid;
388                         m->devid = u.ret;
389                 }
390         }
391
392         return m->devid;
393 }
394
395 static struct file_id fileid_mapping_fsid(struct fileid_handle_data *data,
396                                           const SMB_STRUCT_STAT *sbuf)
397 {
398         struct file_id id = { .inode = sbuf->st_ex_ino, };
399
400         id.devid = fileid_device_mapping_fsid(data, sbuf);
401
402         return id;
403 }
404
405 static int get_connectpath_ino(struct vfs_handle_struct *handle,
406                                const char *path,
407                                SMB_STRUCT_STAT *psbuf)
408 {
409         TALLOC_CTX *frame = talloc_stackframe();
410         struct smb_filename *fname = NULL;
411         const char *fullpath = NULL;
412         int ret;
413
414         if (path[0] == '/') {
415                 fullpath = path;
416         } else {
417                 fullpath = talloc_asprintf(frame,
418                                            "%s/%s",
419                                            handle->conn->connectpath,
420                                            path);
421                 if (fullpath == NULL) {
422                         DBG_ERR("talloc_asprintf() failed\n");
423                         TALLOC_FREE(frame);
424                         return -1;
425                 }
426         }
427
428         fname = synthetic_smb_fname(frame,
429                                     fullpath,
430                                     NULL,
431                                     NULL,
432                                     0,
433                                     0);
434         if (fname == NULL) {
435                 DBG_ERR("synthetic_smb_fname(%s) failed - %s\n",
436                         fullpath, strerror(errno));
437                 TALLOC_FREE(frame);
438                 return -1;
439         }
440
441         ret = SMB_VFS_NEXT_STAT(handle, fname);
442         if (ret != 0) {
443                 DBG_ERR("stat failed for %s with %s\n",
444                         fullpath, strerror(errno));
445                 TALLOC_FREE(frame);
446                 return -1;
447         }
448         *psbuf = fname->st;
449
450         TALLOC_FREE(frame);
451
452         return 0;
453 }
454
455 static int fileid_connect(struct vfs_handle_struct *handle,
456                           const char *service, const char *user)
457 {
458         struct fileid_handle_data *data;
459         const char *algorithm;
460         const char **fstype_deny_list = NULL;
461         const char **fstype_allow_list = NULL;
462         const char **mntdir_deny_list = NULL;
463         const char **mntdir_allow_list = NULL;
464         ino_t nolockinode;
465         bool rootdir_nolock = false;
466         int saved_errno;
467         int ret = SMB_VFS_NEXT_CONNECT(handle, service, user);
468
469         if (ret < 0) {
470                 return ret;
471         }
472
473         data = talloc_zero(handle->conn, struct fileid_handle_data);
474         if (!data) {
475                 saved_errno = errno;
476                 SMB_VFS_NEXT_DISCONNECT(handle);
477                 DEBUG(0, ("talloc_zero() failed\n"));
478                 errno = saved_errno;
479                 return -1;
480         }
481
482         /*
483          * "fileid:mapping" is only here as fallback for old setups
484          * "fileid:algorithm" is the option new setups should use
485          */
486         algorithm = lp_parm_const_string(SNUM(handle->conn),
487                                          "fileid", "mapping",
488                                          "fsname");
489         algorithm = lp_parm_const_string(SNUM(handle->conn),
490                                          "fileid", "algorithm",
491                                          algorithm);
492         if (strcmp("fsname", algorithm) == 0) {
493                 data->mapping_fn = fileid_mapping_fsname;
494         } else if (strcmp("fsname_nodirs", algorithm) == 0) {
495                 data->mapping_fn = fileid_mapping_fsname_nodirs;
496         } else if (strcmp("fsid", algorithm) == 0) {
497                 data->mapping_fn = fileid_mapping_fsid;
498         } else if (strcmp("hostname", algorithm) == 0) {
499                 data->mapping_fn = fileid_mapping_hostname;
500         } else if (strcmp("fsname_norootdir", algorithm) == 0) {
501                 data->mapping_fn = fileid_mapping_fsname_norootdir;
502                 rootdir_nolock = true;
503         } else if (strcmp("fsname_norootdir_ext", algorithm) == 0) {
504                 data->mapping_fn = fileid_mapping_fsname_norootdir_ext;
505                 rootdir_nolock = true;
506         } else {
507                 SMB_VFS_NEXT_DISCONNECT(handle);
508                 DEBUG(0,("fileid_connect(): unknown algorithm[%s]\n", algorithm));
509                 return -1;
510         }
511
512         fstype_deny_list = lp_parm_string_list(SNUM(handle->conn), "fileid",
513                                                "fstype deny", NULL);
514         if (fstype_deny_list != NULL) {
515                 data->fstype_deny_list = str_list_copy(data, fstype_deny_list);
516                 if (data->fstype_deny_list == NULL) {
517                         saved_errno = errno;
518                         DBG_ERR("str_list_copy failed\n");
519                         SMB_VFS_NEXT_DISCONNECT(handle);
520                         errno = saved_errno;
521                         return -1;
522                 }
523         }
524
525         fstype_allow_list = lp_parm_string_list(SNUM(handle->conn), "fileid",
526                                                 "fstype allow", NULL);
527         if (fstype_allow_list != NULL) {
528                 data->fstype_allow_list = str_list_copy(data, fstype_allow_list);
529                 if (data->fstype_allow_list == NULL) {
530                         saved_errno = errno;
531                         DBG_ERR("str_list_copy failed\n");
532                         SMB_VFS_NEXT_DISCONNECT(handle);
533                         errno = saved_errno;
534                         return -1;
535                 }
536         }
537
538         mntdir_deny_list = lp_parm_string_list(SNUM(handle->conn), "fileid",
539                                                "mntdir deny", NULL);
540         if (mntdir_deny_list != NULL) {
541                 data->mntdir_deny_list = str_list_copy(data, mntdir_deny_list);
542                 if (data->mntdir_deny_list == NULL) {
543                         saved_errno = errno;
544                         DBG_ERR("str_list_copy failed\n");
545                         SMB_VFS_NEXT_DISCONNECT(handle);
546                         errno = saved_errno;
547                         return -1;
548                 }
549         }
550
551         mntdir_allow_list = lp_parm_string_list(SNUM(handle->conn), "fileid",
552                                                 "mntdir allow", NULL);
553         if (mntdir_allow_list != NULL) {
554                 data->mntdir_allow_list = str_list_copy(data, mntdir_allow_list);
555                 if (data->mntdir_allow_list == NULL) {
556                         saved_errno = errno;
557                         DBG_ERR("str_list_copy failed\n");
558                         SMB_VFS_NEXT_DISCONNECT(handle);
559                         errno = saved_errno;
560                         return -1;
561                 }
562         }
563
564         nolockinode = lp_parm_ulong(SNUM(handle->conn), "fileid", "nolockinode", 0);
565         if (nolockinode != 0) {
566                 SMB_STRUCT_STAT tmpsbuf = { .st_ex_ino = nolockinode, };
567
568                 ret = fileid_add_nolock_inode(data, &tmpsbuf);
569                 if (ret != 0) {
570                         saved_errno = errno;
571                         SMB_VFS_NEXT_DISCONNECT(handle);
572                         errno = saved_errno;
573                         return -1;
574                 }
575         }
576
577         if (rootdir_nolock) {
578                 SMB_STRUCT_STAT rootdirsbuf;
579
580                 ret = get_connectpath_ino(handle, ".", &rootdirsbuf);
581                 if (ret != 0) {
582                         saved_errno = errno;
583                         SMB_VFS_NEXT_DISCONNECT(handle);
584                         errno = saved_errno;
585                         return -1;
586                 }
587
588                 ret = fileid_add_nolock_inode(data, &rootdirsbuf);
589                 if (ret != 0) {
590                         saved_errno = errno;
591                         SMB_VFS_NEXT_DISCONNECT(handle);
592                         errno = saved_errno;
593                         return -1;
594                 }
595         }
596
597         SMB_VFS_HANDLE_SET_DATA(handle, data, NULL,
598                                 struct fileid_handle_data,
599                                 return -1);
600
601         DBG_DEBUG("connect to service[%s] with algorithm[%s] nolock.inodes %zu\n",
602                   service, algorithm, data->nolock.num_inodes);
603
604         return 0;
605 }
606
607 static void fileid_disconnect(struct vfs_handle_struct *handle)
608 {
609         const struct loadparm_substitution *lp_sub =
610                 loadparm_s3_global_substitution();
611
612         DEBUG(10,("fileid_disconnect() connect to service[%s].\n",
613                   lp_servicename(talloc_tos(), lp_sub, SNUM(handle->conn))));
614
615         SMB_VFS_NEXT_DISCONNECT(handle);
616 }
617
618 static struct file_id fileid_file_id_create(struct vfs_handle_struct *handle,
619                                             const SMB_STRUCT_STAT *sbuf)
620 {
621         struct fileid_handle_data *data;
622         struct file_id id = { .inode = 0, };
623
624         SMB_VFS_HANDLE_GET_DATA(handle, data,
625                                 struct fileid_handle_data,
626                                 return id);
627
628         id = data->mapping_fn(data, sbuf);
629
630         DBG_DEBUG("Returning dev [%jx] inode [%jx] extid [%jx]\n",
631                   (uintmax_t)id.devid, (uintmax_t)id.inode, (uintmax_t)id.extid);
632
633         return id;
634 }
635
636 static struct vfs_fn_pointers vfs_fileid_fns = {
637         .connect_fn = fileid_connect,
638         .disconnect_fn = fileid_disconnect,
639         .file_id_create_fn = fileid_file_id_create
640 };
641
642 static_decl_vfs;
643 NTSTATUS vfs_fileid_init(TALLOC_CTX *ctx)
644 {
645         NTSTATUS ret;
646
647         ret = smb_register_vfs(SMB_VFS_INTERFACE_VERSION, "fileid",
648                                &vfs_fileid_fns);
649         if (!NT_STATUS_IS_OK(ret)) {
650                 return ret;
651         }
652
653         vfs_fileid_debug_level = debug_add_class("fileid");
654         if (vfs_fileid_debug_level == -1) {
655                 vfs_fileid_debug_level = DBGC_VFS;
656                 DEBUG(0, ("vfs_fileid: Couldn't register custom debugging class!\n"));
657         } else {
658                 DEBUG(10, ("vfs_fileid: Debug class number of 'fileid': %d\n", vfs_fileid_debug_level));
659         }
660
661         return ret;
662 }