summaryrefslogtreecommitdiff
path: root/source4/ntvfs/posix/pvfs_oplock.c
blob: 50b1478f13fb0c179043db2a0607c02bf3f21a0f (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
/*
   Unix SMB/CIFS implementation.

   POSIX NTVFS backend - oplock handling

   Copyright (C) Stefan Metzmacher 2008

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "includes.h"
#include "lib/messaging/messaging.h"
#include "lib/messaging/irpc.h"
#include "vfs_posix.h"


struct pvfs_oplock {
	struct pvfs_file_handle *handle;
	struct pvfs_file *file;
	uint32_t level;
	struct messaging_context *msg_ctx;
};

/*
  receive oplock breaks and forward them to the client
*/
static void pvfs_oplock_break(struct pvfs_oplock *opl, uint8_t level)
{
	NTSTATUS status;
	struct pvfs_file *f = opl->file;
	struct pvfs_file_handle *h = opl->handle;
	struct pvfs_state *pvfs = h->pvfs;

	DEBUG(10,("pvfs_oplock_break: sending oplock break level %d for '%s' %p\n",
		level, h->name->original_name, h));
	status = ntvfs_send_oplock_break(pvfs->ntvfs, f->ntvfs, level);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0,("pvfs_oplock_break: sending oplock break failed: %s\n",
			nt_errstr(status)));
	}
}

static void pvfs_oplock_break_dispatch(struct messaging_context *msg,
				       void *private_data, uint32_t msg_type,
				       struct server_id src, DATA_BLOB *data)
{
	struct pvfs_oplock *opl = talloc_get_type(private_data,
						  struct pvfs_oplock);
	struct opendb_oplock_break opb;

	ZERO_STRUCT(opb);

	/* we need to check that this one is for us. See
	   messaging_send_ptr() for the other side of this.
	 */
	if (data->length == sizeof(struct opendb_oplock_break)) {
		struct opendb_oplock_break *p;
		p = (struct opendb_oplock_break *)data->data;
		opb = *p;
	} else {
		DEBUG(0,("%s: ignore oplock break with length[%u]\n",
			__location__, data->length));
		return;
	}
	if (opb.file_handle != opl->handle) {
		return;
	}

	/*
	 * maybe we should use ntvfs_setup_async()
	 */
	pvfs_oplock_break(opl, opb.level);
}

static int pvfs_oplock_destructor(struct pvfs_oplock *opl)
{
	messaging_deregister(opl->msg_ctx, MSG_NTVFS_OPLOCK_BREAK, opl);
	return 0;
}

NTSTATUS pvfs_setup_oplock(struct pvfs_file *f, uint32_t oplock_granted)
{
	NTSTATUS status;
	struct pvfs_oplock *opl;
	uint32_t level = OPLOCK_NONE;

	f->handle->oplock = NULL;

	switch (oplock_granted) {
	case EXCLUSIVE_OPLOCK_RETURN:
		level = OPLOCK_EXCLUSIVE;
		break;
	case BATCH_OPLOCK_RETURN:
		level = OPLOCK_BATCH;
		break;
	case LEVEL_II_OPLOCK_RETURN:
		level = OPLOCK_LEVEL_II;
		break;
	}

	if (level == OPLOCK_NONE) {
		return NT_STATUS_OK;
	}

	opl = talloc(f->handle, struct pvfs_oplock);
	NT_STATUS_HAVE_NO_MEMORY(opl);

	opl->handle	= f->handle;
	opl->file	= f;
	opl->level	= level;
	opl->msg_ctx	= f->pvfs->ntvfs->ctx->msg_ctx;

	status = messaging_register(opl->msg_ctx,
				    opl,
				    MSG_NTVFS_OPLOCK_BREAK,
				    pvfs_oplock_break_dispatch);
	NT_STATUS_NOT_OK_RETURN(status);

	/* destructor */
	talloc_set_destructor(opl, pvfs_oplock_destructor);

	f->handle->oplock = opl;

	return NT_STATUS_OK;
}