xref: /titanic_50/usr/src/uts/common/fs/smbsrv/smb_fem.c (revision 3e5de5d019b8b1edc6c40c035fa29f3a41341841)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright 2012 Nexenta Systems, Inc.  All rights reserved.
24  */
25 
26 #include <smbsrv/smb_kproto.h>
27 #include <smbsrv/smb_fsops.h>
28 #include <sys/sdt.h>
29 #include <sys/fcntl.h>
30 #include <sys/vfs.h>
31 #include <sys/vfs_opreg.h>
32 #include <sys/vnode.h>
33 #include <sys/fem.h>
34 
35 extern caller_context_t	smb_ct;
36 
37 static boolean_t	smb_fem_initialized = B_FALSE;
38 static fem_t		*smb_fcn_ops = NULL;
39 static fem_t		*smb_oplock_ops = NULL;
40 
41 /*
42  * Declarations for FCN (file change notification) FEM monitors
43  */
44 
45 static int smb_fem_fcn_create(femarg_t *, char *, vattr_t *, vcexcl_t, int,
46     vnode_t **, cred_t *, int, caller_context_t *, vsecattr_t *);
47 static int smb_fem_fcn_remove(femarg_t *, char *, cred_t *,
48     caller_context_t *, int);
49 static int smb_fem_fcn_rename(femarg_t *, char *, vnode_t *, char *,
50     cred_t *, caller_context_t *, int);
51 static int smb_fem_fcn_mkdir(femarg_t *, char *, vattr_t *, vnode_t **,
52     cred_t *, caller_context_t *, int, vsecattr_t *);
53 static int smb_fem_fcn_rmdir(femarg_t *, char *, vnode_t *, cred_t *,
54     caller_context_t *, int);
55 static int smb_fem_fcn_link(femarg_t *, vnode_t *, char *, cred_t *,
56     caller_context_t *, int);
57 static int smb_fem_fcn_symlink(femarg_t *, char *, vattr_t *,
58     char *, cred_t *, caller_context_t *, int);
59 
60 static const fs_operation_def_t smb_fcn_tmpl[] = {
61 	VOPNAME_CREATE, { .femop_create = smb_fem_fcn_create },
62 	VOPNAME_REMOVE, {.femop_remove = smb_fem_fcn_remove},
63 	VOPNAME_RENAME, {.femop_rename = smb_fem_fcn_rename},
64 	VOPNAME_MKDIR, {.femop_mkdir = smb_fem_fcn_mkdir},
65 	VOPNAME_RMDIR, {.femop_rmdir = smb_fem_fcn_rmdir},
66 	VOPNAME_LINK, {.femop_link = smb_fem_fcn_link},
67 	VOPNAME_SYMLINK, {.femop_symlink = smb_fem_fcn_symlink},
68 	NULL, NULL
69 };
70 
71 /*
72  * Declarations for oplock FEM monitors
73  */
74 
75 static int smb_fem_oplock_open(femarg_t *, int, cred_t *,
76     struct caller_context *);
77 static int smb_fem_oplock_read(femarg_t *, uio_t *, int, cred_t *,
78     struct caller_context *);
79 static int smb_fem_oplock_write(femarg_t *, uio_t *, int, cred_t *,
80     struct caller_context *);
81 static int smb_fem_oplock_setattr(femarg_t *, vattr_t *, int, cred_t *,
82     caller_context_t *);
83 static int smb_fem_oplock_rwlock(femarg_t *, int, caller_context_t *);
84 static int smb_fem_oplock_space(femarg_t *, int, flock64_t *, int,
85     offset_t, cred_t *, caller_context_t *);
86 static int smb_fem_oplock_vnevent(femarg_t *, vnevent_t, vnode_t *, char *,
87     caller_context_t *);
88 
89 static const fs_operation_def_t smb_oplock_tmpl[] = {
90 	VOPNAME_OPEN,	{ .femop_open = smb_fem_oplock_open },
91 	VOPNAME_READ,	{ .femop_read = smb_fem_oplock_read },
92 	VOPNAME_WRITE,	{ .femop_write = smb_fem_oplock_write },
93 	VOPNAME_SETATTR, { .femop_setattr = smb_fem_oplock_setattr },
94 	VOPNAME_RWLOCK, { .femop_rwlock = smb_fem_oplock_rwlock },
95 	VOPNAME_SPACE,	{ .femop_space = smb_fem_oplock_space },
96 	VOPNAME_VNEVENT, { .femop_vnevent = smb_fem_oplock_vnevent },
97 	NULL, NULL
98 };
99 
100 static int smb_fem_oplock_break(femarg_t *, caller_context_t *, uint32_t);
101 
102 /*
103  * smb_fem_init
104  *
105  * This function is not multi-thread safe. The caller must make sure only one
106  * thread makes the call.
107  */
108 int
109 smb_fem_init(void)
110 {
111 	int	rc = 0;
112 
113 	if (smb_fem_initialized)
114 		return (0);
115 
116 	rc = fem_create("smb_fcn_ops", smb_fcn_tmpl, &smb_fcn_ops);
117 	if (rc)
118 		return (rc);
119 
120 	rc = fem_create("smb_oplock_ops", smb_oplock_tmpl,
121 	    &smb_oplock_ops);
122 
123 	if (rc) {
124 		fem_free(smb_fcn_ops);
125 		smb_fcn_ops = NULL;
126 		return (rc);
127 	}
128 
129 	smb_fem_initialized = B_TRUE;
130 
131 	return (0);
132 }
133 
134 /*
135  * smb_fem_fini
136  *
137  * This function is not multi-thread safe. The caller must make sure only one
138  * thread makes the call.
139  */
140 void
141 smb_fem_fini(void)
142 {
143 	if (!smb_fem_initialized)
144 		return;
145 
146 	if (smb_fcn_ops != NULL) {
147 		fem_free(smb_fcn_ops);
148 		smb_fcn_ops = NULL;
149 	}
150 	if (smb_oplock_ops != NULL) {
151 		fem_free(smb_oplock_ops);
152 		smb_oplock_ops = NULL;
153 	}
154 	smb_fem_initialized = B_FALSE;
155 }
156 
157 int
158 smb_fem_fcn_install(smb_node_t *node)
159 {
160 	int rc;
161 
162 	if (smb_fcn_ops == NULL)
163 		return (ENOSYS);
164 	rc = fem_install(node->vp, smb_fcn_ops, (void *)node, OPARGUNIQ,
165 	    (fem_func_t)smb_node_ref, (fem_func_t)smb_node_release);
166 	return (rc);
167 }
168 
169 void
170 smb_fem_fcn_uninstall(smb_node_t *node)
171 {
172 	if (smb_fcn_ops == NULL)
173 		return;
174 	VERIFY0(fem_uninstall(node->vp, smb_fcn_ops, (void *)node));
175 }
176 
177 int
178 smb_fem_oplock_install(smb_node_t *node)
179 {
180 	int rc;
181 
182 	if (smb_oplock_ops == NULL)
183 		return (ENOSYS);
184 	rc = fem_install(node->vp, smb_oplock_ops, (void *)node, OPARGUNIQ,
185 	    (fem_func_t)smb_node_ref, (fem_func_t)smb_node_release);
186 	return (rc);
187 }
188 
189 void
190 smb_fem_oplock_uninstall(smb_node_t *node)
191 {
192 	if (smb_oplock_ops == NULL)
193 		return;
194 	VERIFY0(fem_uninstall(node->vp, smb_oplock_ops, (void *)node));
195 }
196 
197 /*
198  * FEM FCN monitors
199  *
200  * The FCN monitors intercept the respective VOP_* call regardless
201  * of whether the call originates from CIFS, NFS, or a local process.
202  */
203 
204 /*
205  * smb_fem_fcn_create()
206  *
207  * This monitor will catch only changes to VREG files and not to extended
208  * attribute files.  This is fine because, for CIFS files, stream creates
209  * should not trigger any file change notification on the VDIR directory
210  * being monitored.  Creates of any other kind of extended attribute in
211  * the directory will also not trigger any file change notification on the
212  * VDIR directory being monitored.
213  */
214 
215 static int
216 smb_fem_fcn_create(
217     femarg_t *arg,
218     char *name,
219     vattr_t *vap,
220     vcexcl_t excl,
221     int mode,
222     vnode_t **vpp,
223     cred_t *cr,
224     int flag,
225     caller_context_t *ct,
226     vsecattr_t *vsecp)
227 {
228 	smb_node_t *dnode;
229 	int error;
230 
231 	dnode = (smb_node_t *)arg->fa_fnode->fn_available;
232 
233 	ASSERT(dnode);
234 
235 	error = vnext_create(arg, name, vap, excl, mode, vpp, cr, flag,
236 	    ct, vsecp);
237 
238 	if (error == 0)
239 		smb_node_notify_change(dnode, FILE_ACTION_ADDED, name);
240 
241 	return (error);
242 }
243 
244 /*
245  * smb_fem_fcn_remove()
246  *
247  * This monitor will catch only changes to VREG files and to not extended
248  * attribute files.  This is fine because, for CIFS files, stream deletes
249  * should not trigger any file change notification on the VDIR directory
250  * being monitored.  Deletes of any other kind of extended attribute in
251  * the directory will also not trigger any file change notification on the
252  * VDIR directory being monitored.
253  */
254 
255 static int
256 smb_fem_fcn_remove(
257     femarg_t *arg,
258     char *name,
259     cred_t *cr,
260     caller_context_t *ct,
261     int flags)
262 {
263 	smb_node_t *dnode;
264 	int error;
265 
266 	dnode = (smb_node_t *)arg->fa_fnode->fn_available;
267 
268 	ASSERT(dnode);
269 
270 	error = vnext_remove(arg, name, cr, ct, flags);
271 
272 	if (error == 0)
273 		smb_node_notify_change(dnode, FILE_ACTION_REMOVED, name);
274 
275 	return (error);
276 }
277 
278 static int
279 smb_fem_fcn_rename(
280     femarg_t *arg,
281     char *snm,
282     vnode_t *tdvp,
283     char *tnm,
284     cred_t *cr,
285     caller_context_t *ct,
286     int flags)
287 {
288 	smb_node_t *dnode;
289 	int error;
290 
291 	dnode = (smb_node_t *)arg->fa_fnode->fn_available;
292 
293 	ASSERT(dnode);
294 
295 	error = vnext_rename(arg, snm, tdvp, tnm, cr, ct, flags);
296 
297 	if (error != 0)
298 		return (error);
299 
300 	/*
301 	 * Note that renames in the same directory are normally
302 	 * delivered in {old,new} pairs, and clients expect them
303 	 * in that order, if both events are delivered.
304 	 */
305 	smb_node_notify_change(dnode, FILE_ACTION_RENAMED_OLD_NAME, snm);
306 	smb_node_notify_change(dnode, FILE_ACTION_RENAMED_NEW_NAME, tnm);
307 
308 	return (0);
309 }
310 
311 static int
312 smb_fem_fcn_mkdir(
313     femarg_t *arg,
314     char *name,
315     vattr_t *vap,
316     vnode_t **vpp,
317     cred_t *cr,
318     caller_context_t *ct,
319     int flags,
320     vsecattr_t *vsecp)
321 {
322 	smb_node_t *dnode;
323 	int error;
324 
325 	dnode = (smb_node_t *)arg->fa_fnode->fn_available;
326 
327 	ASSERT(dnode);
328 
329 	error = vnext_mkdir(arg, name, vap, vpp, cr, ct, flags, vsecp);
330 
331 	if (error == 0)
332 		smb_node_notify_change(dnode, FILE_ACTION_ADDED, name);
333 
334 	return (error);
335 }
336 
337 static int
338 smb_fem_fcn_rmdir(
339     femarg_t *arg,
340     char *name,
341     vnode_t *cdir,
342     cred_t *cr,
343     caller_context_t *ct,
344     int flags)
345 {
346 	smb_node_t *dnode;
347 	int error;
348 
349 	dnode = (smb_node_t *)arg->fa_fnode->fn_available;
350 
351 	ASSERT(dnode);
352 
353 	error = vnext_rmdir(arg, name, cdir, cr, ct, flags);
354 
355 	if (error == 0)
356 		smb_node_notify_change(dnode, FILE_ACTION_REMOVED, name);
357 
358 	return (error);
359 }
360 
361 static int
362 smb_fem_fcn_link(
363     femarg_t *arg,
364     vnode_t *svp,
365     char *tnm,
366     cred_t *cr,
367     caller_context_t *ct,
368     int flags)
369 {
370 	smb_node_t *dnode;
371 	int error;
372 
373 	dnode = (smb_node_t *)arg->fa_fnode->fn_available;
374 
375 	ASSERT(dnode);
376 
377 	error = vnext_link(arg, svp, tnm, cr, ct, flags);
378 
379 	if (error == 0)
380 		smb_node_notify_change(dnode, FILE_ACTION_ADDED, tnm);
381 
382 	return (error);
383 }
384 
385 static int
386 smb_fem_fcn_symlink(
387     femarg_t *arg,
388     char *linkname,
389     vattr_t *vap,
390     char *target,
391     cred_t *cr,
392     caller_context_t *ct,
393     int flags)
394 {
395 	smb_node_t *dnode;
396 	int error;
397 
398 	dnode = (smb_node_t *)arg->fa_fnode->fn_available;
399 
400 	ASSERT(dnode);
401 
402 	error = vnext_symlink(arg, linkname, vap, target, cr, ct, flags);
403 
404 	if (error == 0)
405 		smb_node_notify_change(dnode, FILE_ACTION_ADDED, linkname);
406 
407 	return (error);
408 }
409 
410 /*
411  * FEM oplock monitors
412  *
413  * The monitors below are not intended to intercept CIFS calls.
414  * CIFS higher-level routines will break oplocks as needed prior
415  * to getting to the VFS layer.
416  */
417 static int
418 smb_fem_oplock_open(
419     femarg_t		*arg,
420     int			mode,
421     cred_t		*cr,
422     caller_context_t	*ct)
423 {
424 	int		rc;
425 	uint32_t	flags;
426 
427 	if (mode & (FWRITE|FTRUNC))
428 		flags = SMB_OPLOCK_BREAK_TO_NONE;
429 	else
430 		flags = SMB_OPLOCK_BREAK_TO_LEVEL_II;
431 
432 	rc = smb_fem_oplock_break(arg, ct, flags);
433 	if (rc == 0)
434 		rc = vnext_open(arg, mode, cr, ct);
435 	return (rc);
436 }
437 
438 /*
439  * Should normally be hit only via NFSv2/v3.  All other accesses
440  * (CIFS/NFS/local) should call VOP_OPEN first.
441  */
442 
443 static int
444 smb_fem_oplock_read(
445     femarg_t		*arg,
446     uio_t		*uiop,
447     int			ioflag,
448     cred_t		*cr,
449     caller_context_t	*ct)
450 {
451 	int	rc;
452 
453 	rc = smb_fem_oplock_break(arg, ct, SMB_OPLOCK_BREAK_TO_LEVEL_II);
454 	if (rc == 0)
455 		rc = vnext_read(arg, uiop, ioflag, cr, ct);
456 	return (rc);
457 }
458 
459 /*
460  * Should normally be hit only via NFSv2/v3.  All other accesses
461  * (CIFS/NFS/local) should call VOP_OPEN first.
462  */
463 
464 static int
465 smb_fem_oplock_write(
466     femarg_t		*arg,
467     uio_t		*uiop,
468     int			ioflag,
469     cred_t		*cr,
470     caller_context_t	*ct)
471 {
472 	int	rc;
473 
474 	rc = smb_fem_oplock_break(arg, ct, SMB_OPLOCK_BREAK_TO_NONE);
475 	if (rc == 0)
476 		rc = vnext_write(arg, uiop, ioflag, cr, ct);
477 	return (rc);
478 }
479 
480 static int
481 smb_fem_oplock_setattr(
482     femarg_t		*arg,
483     vattr_t		*vap,
484     int			flags,
485     cred_t		*cr,
486     caller_context_t	*ct)
487 {
488 	int	rc = 0;
489 
490 	if (vap->va_mask & AT_SIZE)
491 		rc = smb_fem_oplock_break(arg, ct, SMB_OPLOCK_BREAK_TO_NONE);
492 	if (rc == 0)
493 		rc = vnext_setattr(arg, vap, flags, cr, ct);
494 	return (rc);
495 }
496 
497 static int
498 smb_fem_oplock_rwlock(
499     femarg_t		*arg,
500     int			write_lock,
501     caller_context_t	*ct)
502 {
503 	int		rc;
504 	uint32_t	flags;
505 
506 	if (write_lock)
507 		flags = SMB_OPLOCK_BREAK_TO_NONE;
508 	else
509 		flags = SMB_OPLOCK_BREAK_TO_LEVEL_II;
510 
511 	rc = smb_fem_oplock_break(arg, ct, flags);
512 	if (rc == 0)
513 		rc = vnext_rwlock(arg, write_lock, ct);
514 
515 	return (rc);
516 }
517 
518 static int
519 smb_fem_oplock_space(
520     femarg_t		*arg,
521     int			cmd,
522     flock64_t		*bfp,
523     int			flag,
524     offset_t		offset,
525     cred_t		*cr,
526     caller_context_t	*ct)
527 {
528 	int	rc;
529 
530 	rc = smb_fem_oplock_break(arg, ct, SMB_OPLOCK_BREAK_TO_NONE);
531 	if (rc == 0)
532 		rc = vnext_space(arg, cmd, bfp, flag, offset, cr, ct);
533 	return (rc);
534 }
535 
536 /*
537  * smb_fem_oplock_vnevent()
538  *
539  * To intercept NFS and local renames and removes in order to break any
540  * existing oplock prior to the operation.
541  *
542  * Note: Currently, this monitor is traversed only when an FS is mounted
543  * non-nbmand.  (When the FS is mounted nbmand, share reservation checking
544  * will detect a share violation and return an error prior to the VOP layer
545  * being reached.)  Thus, for nbmand NFS and local renames and removes,
546  * an existing oplock is never broken prior to share checking (contrary to
547  * how it is with intra-CIFS remove and rename requests).
548  */
549 
550 static int
551 smb_fem_oplock_vnevent(
552     femarg_t		*arg,
553     vnevent_t		vnevent,
554     vnode_t		*dvp,
555     char		*name,
556     caller_context_t	*ct)
557 {
558 	int		rc;
559 	uint32_t	flags;
560 
561 	switch (vnevent) {
562 	case VE_REMOVE:
563 	case VE_RENAME_DEST:
564 		flags = SMB_OPLOCK_BREAK_TO_NONE | SMB_OPLOCK_BREAK_BATCH;
565 		rc = smb_fem_oplock_break(arg, ct, flags);
566 		break;
567 	case VE_RENAME_SRC:
568 		flags = SMB_OPLOCK_BREAK_TO_LEVEL_II | SMB_OPLOCK_BREAK_BATCH;
569 		rc = smb_fem_oplock_break(arg, ct, flags);
570 		break;
571 	default:
572 		rc = 0;
573 		break;
574 	}
575 
576 	if (rc != 0)
577 		return (rc);
578 
579 	return (vnext_vnevent(arg, vnevent, dvp, name, ct));
580 }
581 
582 static int
583 smb_fem_oplock_break(femarg_t *arg, caller_context_t *ct, uint32_t flags)
584 {
585 	smb_node_t	*node;
586 	int		rc;
587 
588 	node = (smb_node_t *)((arg)->fa_fnode->fn_available);
589 	SMB_NODE_VALID(node);
590 
591 	if (ct && (ct->cc_caller_id == smb_ct.cc_caller_id))
592 		return (0);
593 
594 	if (ct && (ct->cc_flags & CC_DONTBLOCK)) {
595 		flags |= SMB_OPLOCK_BREAK_NOWAIT;
596 		rc = smb_oplock_break(NULL, node, flags);
597 		if (rc == EAGAIN)
598 			ct->cc_flags |= CC_WOULDBLOCK;
599 	} else {
600 		rc = smb_oplock_break(NULL, node, flags);
601 	}
602 
603 	return (rc);
604 }
605