summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/fs/smbsrv/smb_fem.c
blob: 458a9e35f6f1fd9727b875096d08b432040f0eb5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2020 Tintri by DDN, Inc.  All rights reserved.
 * Copyright 2015 Joyent, Inc.
 * Copyright 2022 RackTop Systems, Inc.
 */

#include <smbsrv/smb_kproto.h>
#include <smbsrv/smb_fsops.h>
#include <sys/sdt.h>
#include <sys/fcntl.h>
#include <sys/vfs.h>
#include <sys/vfs_opreg.h>
#include <sys/vnode.h>
#include <sys/fem.h>

extern caller_context_t	smb_ct;

static boolean_t	smb_fem_initialized = B_FALSE;
static fem_t		*smb_fcn_ops = NULL;
static fem_t		*smb_oplock_ops = NULL;

/*
 * Declarations for FCN (file change notification) FEM monitors
 */

static int smb_fem_fcn_create(femarg_t *, char *, vattr_t *, vcexcl_t, int,
    vnode_t **, cred_t *, int, caller_context_t *, vsecattr_t *);
static int smb_fem_fcn_remove(femarg_t *, char *, cred_t *,
    caller_context_t *, int);
static int smb_fem_fcn_rename(femarg_t *, char *, vnode_t *, char *,
    cred_t *, caller_context_t *, int);
static int smb_fem_fcn_mkdir(femarg_t *, char *, vattr_t *, vnode_t **,
    cred_t *, caller_context_t *, int, vsecattr_t *);
static int smb_fem_fcn_rmdir(femarg_t *, char *, vnode_t *, cred_t *,
    caller_context_t *, int);
static int smb_fem_fcn_link(femarg_t *, vnode_t *, char *, cred_t *,
    caller_context_t *, int);
static int smb_fem_fcn_symlink(femarg_t *, char *, vattr_t *,
    char *, cred_t *, caller_context_t *, int);

static const fs_operation_def_t smb_fcn_tmpl[] = {
	VOPNAME_CREATE, { .femop_create = smb_fem_fcn_create },
	VOPNAME_REMOVE, {.femop_remove = smb_fem_fcn_remove},
	VOPNAME_RENAME, {.femop_rename = smb_fem_fcn_rename},
	VOPNAME_MKDIR, {.femop_mkdir = smb_fem_fcn_mkdir},
	VOPNAME_RMDIR, {.femop_rmdir = smb_fem_fcn_rmdir},
	VOPNAME_LINK, {.femop_link = smb_fem_fcn_link},
	VOPNAME_SYMLINK, {.femop_symlink = smb_fem_fcn_symlink},
	NULL, NULL
};

/*
 * Declarations for oplock FEM monitors
 */

static int smb_fem_oplock_open(femarg_t *, int, cred_t *,
    struct caller_context *);
static int smb_fem_oplock_read(femarg_t *, uio_t *, int, cred_t *,
    struct caller_context *);
static int smb_fem_oplock_write(femarg_t *, uio_t *, int, cred_t *,
    struct caller_context *);
static int smb_fem_oplock_setattr(femarg_t *, vattr_t *, int, cred_t *,
    caller_context_t *);
static int smb_fem_oplock_space(femarg_t *, int, flock64_t *, int,
    offset_t, cred_t *, caller_context_t *);
static int smb_fem_oplock_vnevent(femarg_t *, vnevent_t, vnode_t *, char *,
    caller_context_t *);

static const fs_operation_def_t smb_oplock_tmpl[] = {
	VOPNAME_OPEN,	{ .femop_open = smb_fem_oplock_open },
	VOPNAME_READ,	{ .femop_read = smb_fem_oplock_read },
	VOPNAME_WRITE,	{ .femop_write = smb_fem_oplock_write },
	VOPNAME_SETATTR, { .femop_setattr = smb_fem_oplock_setattr },
	VOPNAME_SPACE,	{ .femop_space = smb_fem_oplock_space },
	VOPNAME_VNEVENT, { .femop_vnevent = smb_fem_oplock_vnevent },
	NULL, NULL
};

static int smb_fem_oplock_wait(smb_node_t *, caller_context_t *);

/*
 * smb_fem_init
 *
 * This function is not multi-thread safe. The caller must make sure only one
 * thread makes the call.
 */
int
smb_fem_init(void)
{
	int	rc = 0;

	if (smb_fem_initialized)
		return (0);

	rc = fem_create("smb_fcn_ops", smb_fcn_tmpl, &smb_fcn_ops);
	if (rc)
		return (rc);

	rc = fem_create("smb_oplock_ops", smb_oplock_tmpl,
	    &smb_oplock_ops);

	if (rc) {
		fem_free(smb_fcn_ops);
		smb_fcn_ops = NULL;
		return (rc);
	}

	smb_fem_initialized = B_TRUE;

	return (0);
}

/*
 * smb_fem_fini
 *
 * This function is not multi-thread safe. The caller must make sure only one
 * thread makes the call.
 */
void
smb_fem_fini(void)
{
	if (!smb_fem_initialized)
		return;

	if (smb_fcn_ops != NULL) {
		fem_free(smb_fcn_ops);
		smb_fcn_ops = NULL;
	}
	if (smb_oplock_ops != NULL) {
		fem_free(smb_oplock_ops);
		smb_oplock_ops = NULL;
	}
	smb_fem_initialized = B_FALSE;
}

/*
 * Install our fem hooks for change notify.
 * Not using hold/rele function here because we
 * remove the fem hooks before node destroy.
 */
int
smb_fem_fcn_install(smb_node_t *node)
{
	int rc;

	if (smb_fcn_ops == NULL)
		return (ENOSYS);
	rc = fem_install(node->vp, smb_fcn_ops, (void *)node, OPARGUNIQ,
	    NULL, NULL);
	return (rc);
}

int
smb_fem_fcn_uninstall(smb_node_t *node)
{
	int rc;

	if (smb_fcn_ops == NULL)
		return (ENOSYS);
	rc = fem_uninstall(node->vp, smb_fcn_ops, (void *)node);
	return (rc);
}

int
smb_fem_oplock_install(smb_node_t *node)
{
	int rc;

	if (smb_oplock_ops == NULL)
		return (ENOSYS);
	rc = fem_install(node->vp, smb_oplock_ops, (void *)node, OPARGUNIQ,
	    (fem_func_t)smb_node_ref, (fem_func_t)smb_node_release);
	return (rc);
}

void
smb_fem_oplock_uninstall(smb_node_t *node)
{
	if (smb_oplock_ops == NULL)
		return;
	VERIFY0(fem_uninstall(node->vp, smb_oplock_ops, (void *)node));
}

/*
 * FEM FCN monitors
 *
 * The FCN monitors intercept the respective VOP_* call regardless
 * of whether the call originates from CIFS, NFS, or a local process.
 *
 * Here we're only interested in operations that change the list of
 * names contained in the directory.  SMB clients can also ask to be
 * notified about events where a file in this directory has had its
 * meta-data changed (size, times, etc) but that's outside of the
 * design intent for these FEM hooks.  Those meta-data events DO
 * happen when caused by SMB clients (via smb_node_notify_modified)
 * but not by other FS activity because we don't have a good way to
 * place all the FEM hooks that would be required for that, and if
 * we did, the performance cost could be severe.
 */

/*
 * smb_fem_fcn_create()
 *
 * This monitor will catch only changes to VREG files and not to extended
 * attribute files.  This is fine because, for CIFS files, stream creates
 * should not trigger any file change notification on the VDIR directory
 * being monitored.  Creates of any other kind of extended attribute in
 * the directory will also not trigger any file change notification on the
 * VDIR directory being monitored.
 */

static int
smb_fem_fcn_create(
    femarg_t *arg,
    char *name,
    vattr_t *vap,
    vcexcl_t excl,
    int mode,
    vnode_t **vpp,
    cred_t *cr,
    int flag,
    caller_context_t *ct,
    vsecattr_t *vsecp)
{
	smb_node_t *dnode;
	int error;

	dnode = (smb_node_t *)arg->fa_fnode->fn_available;

	ASSERT(dnode);

	error = vnext_create(arg, name, vap, excl, mode, vpp, cr, flag,
	    ct, vsecp);

	if (error == 0 && ct != &smb_ct)
		smb_node_notify_change(dnode, FILE_ACTION_ADDED, name);

	return (error);
}

/*
 * smb_fem_fcn_remove()
 *
 * This monitor will catch only changes to VREG files and to not extended
 * attribute files.  This is fine because, for CIFS files, stream deletes
 * should not trigger any file change notification on the VDIR directory
 * being monitored.  Deletes of any other kind of extended attribute in
 * the directory will also not trigger any file change notification on the
 * VDIR directory being monitored.
 */

static int
smb_fem_fcn_remove(
    femarg_t *arg,
    char *name,
    cred_t *cr,
    caller_context_t *ct,
    int flags)
{
	smb_node_t *dnode;
	int error;

	dnode = (smb_node_t *)arg->fa_fnode->fn_available;

	ASSERT(dnode);

	error = vnext_remove(arg, name, cr, ct, flags);

	if (error == 0 && ct != &smb_ct)
		smb_node_notify_change(dnode, FILE_ACTION_REMOVED, name);

	return (error);
}

static int
smb_fem_fcn_rename(
    femarg_t *arg,
    char *snm,
    vnode_t *tdvp,
    char *tnm,
    cred_t *cr,
    caller_context_t *ct,
    int flags)
{
	smb_node_t *dnode;
	int error;

	dnode = (smb_node_t *)arg->fa_fnode->fn_available;

	ASSERT(dnode);

	error = vnext_rename(arg, snm, tdvp, tnm, cr, ct, flags);

	if (error == 0 && ct != &smb_ct) {
		/*
		 * Note that renames in the same directory are normally
		 * delivered in {old,new} pairs, and clients expect them
		 * in that order, if both events are delivered.
		 */
		smb_node_notify_change(dnode,
		    FILE_ACTION_RENAMED_OLD_NAME, snm);
		smb_node_notify_change(dnode,
		    FILE_ACTION_RENAMED_NEW_NAME, tnm);
	}

	return (error);
}

static int
smb_fem_fcn_mkdir(
    femarg_t *arg,
    char *name,
    vattr_t *vap,
    vnode_t **vpp,
    cred_t *cr,
    caller_context_t *ct,
    int flags,
    vsecattr_t *vsecp)
{
	smb_node_t *dnode;
	int error;

	dnode = (smb_node_t *)arg->fa_fnode->fn_available;

	ASSERT(dnode);

	error = vnext_mkdir(arg, name, vap, vpp, cr, ct, flags, vsecp);

	if (error == 0 && ct != &smb_ct)
		smb_node_notify_change(dnode, FILE_ACTION_ADDED, name);

	return (error);
}

static int
smb_fem_fcn_rmdir(
    femarg_t *arg,
    char *name,
    vnode_t *cdir,
    cred_t *cr,
    caller_context_t *ct,
    int flags)
{
	smb_node_t *dnode;
	int error;

	dnode = (smb_node_t *)arg->fa_fnode->fn_available;

	ASSERT(dnode);

	error = vnext_rmdir(arg, name, cdir, cr, ct, flags);

	if (error == 0 && ct != &smb_ct)
		smb_node_notify_change(dnode, FILE_ACTION_REMOVED, name);

	return (error);
}

static int
smb_fem_fcn_link(
    femarg_t *arg,
    vnode_t *svp,
    char *tnm,
    cred_t *cr,
    caller_context_t *ct,
    int flags)
{
	smb_node_t *dnode;
	int error;

	dnode = (smb_node_t *)arg->fa_fnode->fn_available;

	ASSERT(dnode);

	error = vnext_link(arg, svp, tnm, cr, ct, flags);

	if (error == 0 && ct != &smb_ct)
		smb_node_notify_change(dnode, FILE_ACTION_ADDED, tnm);

	return (error);
}

static int
smb_fem_fcn_symlink(
    femarg_t *arg,
    char *linkname,
    vattr_t *vap,
    char *target,
    cred_t *cr,
    caller_context_t *ct,
    int flags)
{
	smb_node_t *dnode;
	int error;

	dnode = (smb_node_t *)arg->fa_fnode->fn_available;

	ASSERT(dnode);

	error = vnext_symlink(arg, linkname, vap, target, cr, ct, flags);

	if (error == 0 && ct != &smb_ct)
		smb_node_notify_change(dnode, FILE_ACTION_ADDED, linkname);

	return (error);
}

/*
 * FEM oplock monitors
 *
 * The monitors below are not intended to intercept CIFS calls.
 * CIFS higher-level routines will break oplocks as needed prior
 * to getting to the VFS layer.
 */
static int
smb_fem_oplock_open(
    femarg_t		*arg,
    int			mode,
    cred_t		*cr,
    caller_context_t	*ct)
{
	smb_node_t	*node;
	uint32_t	status;
	int		rc = 0;

	if (ct != &smb_ct) {
		uint32_t req_acc = FILE_READ_DATA;
		uint32_t cr_disp = FILE_OPEN_IF;

		node = (smb_node_t *)(arg->fa_fnode->fn_available);
		SMB_NODE_VALID(node);

		/*
		 * Get req_acc, cr_disp just accurate enough so
		 * the oplock break call does the right thing.
		 */
		if (mode & FWRITE) {
			req_acc = FILE_READ_DATA | FILE_WRITE_DATA;
			cr_disp = (mode & FTRUNC) ?
			    FILE_OVERWRITE_IF : FILE_OPEN_IF;
		} else {
			req_acc = FILE_READ_DATA;
			cr_disp = FILE_OPEN_IF;
		}

		status = smb_oplock_break_OPEN(node, NULL,
		    req_acc, cr_disp);
		if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS)
			rc = smb_fem_oplock_wait(node, ct);
		else if (status != 0)
			rc = EIO;
	}
	if (rc == 0)
		rc = vnext_open(arg, mode, cr, ct);

	return (rc);
}

/*
 * Should normally be hit only via NFSv2/v3.  All other accesses
 * (CIFS/NFS/local) should call VOP_OPEN first.
 */

static int
smb_fem_oplock_read(
    femarg_t		*arg,
    uio_t		*uiop,
    int			ioflag,
    cred_t		*cr,
    caller_context_t	*ct)
{
	smb_node_t	*node;
	uint32_t	status;
	int	rc = 0;

	if (ct != &smb_ct) {
		node = (smb_node_t *)(arg->fa_fnode->fn_available);
		SMB_NODE_VALID(node);

		status = smb_oplock_break_READ(node, NULL);
		if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS)
			rc = smb_fem_oplock_wait(node, ct);
		else if (status != 0)
			rc = EIO;
	}
	if (rc == 0)
		rc = vnext_read(arg, uiop, ioflag, cr, ct);

	return (rc);
}

/*
 * Should normally be hit only via NFSv2/v3.  All other accesses
 * (CIFS/NFS/local) should call VOP_OPEN first.
 */

static int
smb_fem_oplock_write(
    femarg_t		*arg,
    uio_t		*uiop,
    int			ioflag,
    cred_t		*cr,
    caller_context_t	*ct)
{
	smb_node_t	*node;
	uint32_t	status;
	int	rc = 0;

	if (ct != &smb_ct) {
		node = (smb_node_t *)(arg->fa_fnode->fn_available);
		SMB_NODE_VALID(node);

		status = smb_oplock_break_WRITE(node, NULL);
		if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS)
			rc = smb_fem_oplock_wait(node, ct);
		else if (status != 0)
			rc = EIO;
	}
	if (rc == 0)
		rc = vnext_write(arg, uiop, ioflag, cr, ct);

	return (rc);
}

static int
smb_fem_oplock_setattr(
    femarg_t		*arg,
    vattr_t		*vap,
    int			flags,
    cred_t		*cr,
    caller_context_t	*ct)
{
	smb_node_t	*node;
	uint32_t	status;
	int	rc = 0;

	if (ct != &smb_ct && (vap->va_mask & AT_SIZE) != 0) {
		node = (smb_node_t *)(arg->fa_fnode->fn_available);
		SMB_NODE_VALID(node);

		status = smb_oplock_break_SETINFO(node, NULL,
		    FileEndOfFileInformation);
		if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS)
			rc = smb_fem_oplock_wait(node, ct);
		else if (status != 0)
			rc = EIO;
	}
	if (rc == 0)
		rc = vnext_setattr(arg, vap, flags, cr, ct);
	return (rc);
}

static int
smb_fem_oplock_space(
    femarg_t		*arg,
    int			cmd,
    flock64_t		*bfp,
    int			flag,
    offset_t		offset,
    cred_t		*cr,
    caller_context_t	*ct)
{
	smb_node_t	*node;
	uint32_t	status;
	int	rc = 0;

	if (ct != &smb_ct) {
		node = (smb_node_t *)(arg->fa_fnode->fn_available);
		SMB_NODE_VALID(node);

		status = smb_oplock_break_SETINFO(node, NULL,
		    FileAllocationInformation);
		if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS)
			rc = smb_fem_oplock_wait(node, ct);
		else if (status != 0)
			rc = EIO;
	}
	if (rc == 0)
		rc = vnext_space(arg, cmd, bfp, flag, offset, cr, ct);
	return (rc);
}

/*
 * smb_fem_oplock_vnevent()
 *
 * To intercept NFS and local renames and removes in order to break any
 * existing oplock prior to the operation.
 *
 * Note: Currently, this monitor is traversed only when an FS is mounted
 * non-nbmand.  (When the FS is mounted nbmand, share reservation checking
 * will detect a share violation and return an error prior to the VOP layer
 * being reached.)  Thus, for nbmand NFS and local renames and removes,
 * an existing oplock is never broken prior to share checking (contrary to
 * how it is with intra-CIFS remove and rename requests).
 */

static int
smb_fem_oplock_vnevent(
    femarg_t		*arg,
    vnevent_t		vnevent,
    vnode_t		*dvp,
    char		*name,
    caller_context_t	*ct)
{
	smb_node_t	*node;
	uint32_t	status;
	int		rc = 0;

	if (ct != &smb_ct) {
		node = (smb_node_t *)(arg->fa_fnode->fn_available);
		SMB_NODE_VALID(node);

		switch (vnevent) {
		case VE_REMOVE:
		case VE_PRE_RENAME_DEST:
		case VE_RENAME_DEST:
			status = smb_oplock_break_HANDLE(node, NULL);
			break;
		case VE_PRE_RENAME_SRC:
		case VE_RENAME_SRC:
			status = smb_oplock_break_SETINFO(node, NULL,
			    FileRenameInformation);
			break;
		default:
			status = 0;
			break;
		}
		if (status == NT_STATUS_OPLOCK_BREAK_IN_PROGRESS)
			rc = smb_fem_oplock_wait(node, ct);
		else if (status != 0)
			rc = EIO;
	}
	if (rc == 0)
		rc = vnext_vnevent(arg, vnevent, dvp, name, ct);

	return (rc);
}

int smb_fem_oplock_timeout = 5000; /* mSec. */

static int
smb_fem_oplock_wait(smb_node_t *node, caller_context_t *ct)
{
	int		rc = 0;

	ASSERT(ct != &smb_ct);

	if (ct && (ct->cc_flags & CC_DONTBLOCK)) {
		ct->cc_flags |= CC_WOULDBLOCK;
		rc = EAGAIN;
	} else {
		(void) smb_oplock_wait_break_fem(node,
		    smb_fem_oplock_timeout);
		rc = 0;
	}

	return (rc);
}