summaryrefslogtreecommitdiff
path: root/source3/smbd/oplock_irix.c
blob: d7f45e67de492f0744f518c2173226097a8c4240 (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
/*
   Unix SMB/CIFS implementation.
   IRIX kernel oplock processing
   Copyright (C) Andrew Tridgell 1992-1998
   
   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/>.
*/

#define DBGC_CLASS DBGC_LOCKING
#include "includes.h"
#include "smbd/globals.h"

#if HAVE_KERNEL_OPLOCKS_IRIX

struct irix_oplocks_context {
	struct kernel_oplocks *ctx;
	int write_fd;
	int read_fd;
	struct fd_event *read_fde;
	bool pending;
};

/****************************************************************************
 Test to see if IRIX kernel oplocks work.
****************************************************************************/

static bool irix_oplocks_available(void)
{
	int fd;
	int pfd[2];
	TALLOC_CTX *ctx = talloc_stackframe();
	char *tmpname = NULL;

	set_effective_capability(KERNEL_OPLOCK_CAPABILITY);

	tmpname = talloc_asprintf(ctx,
				"%s/koplock.%d",
				lp_lockdir(),
				(int)sys_getpid());
	if (!tmpname) {
		TALLOC_FREE(ctx);
		return False;
	}

	if(pipe(pfd) != 0) {
		DEBUG(0,("check_kernel_oplocks: Unable to create pipe. Error "
			 "was %s\n",
			 strerror(errno) ));
		TALLOC_FREE(ctx);
		return False;
	}

	if((fd = sys_open(tmpname, O_RDWR|O_CREAT|O_EXCL|O_TRUNC, 0600)) < 0) {
		DEBUG(0,("check_kernel_oplocks: Unable to open temp test file "
			 "%s. Error was %s\n",
			 tmpname, strerror(errno) ));
		unlink( tmpname );
		close(pfd[0]);
		close(pfd[1]);
		TALLOC_FREE(ctx);
		return False;
	}

	unlink(tmpname);

	TALLOC_FREE(ctx);

	if(sys_fcntl_long(fd, F_OPLKREG, pfd[1]) == -1) {
		DEBUG(0,("check_kernel_oplocks: Kernel oplocks are not "
			 "available on this machine. Disabling kernel oplock "
			 "support.\n" ));
		close(pfd[0]);
		close(pfd[1]);
		close(fd);
		return False;
	}

	if(sys_fcntl_long(fd, F_OPLKACK, OP_REVOKE) < 0 ) {
		DEBUG(0,("check_kernel_oplocks: Error when removing kernel "
			 "oplock. Error was %s. Disabling kernel oplock "
			 "support.\n", strerror(errno) ));
		close(pfd[0]);
		close(pfd[1]);
		close(fd);
		return False;
	}

	close(pfd[0]);
	close(pfd[1]);
	close(fd);

	return True;
}

/****************************************************************************
 * Deal with the IRIX kernel <--> smbd
 * oplock break protocol.
****************************************************************************/

static files_struct *irix_oplock_receive_message(struct kernel_oplocks *_ctx)
{
	struct irix_oplocks_context *ctx = talloc_get_type(_ctx->private_data,
					   struct irix_oplocks_context);
	oplock_stat_t os;
	char dummy;
	struct file_id fileid;
	files_struct *fsp;

	/*
	 * TODO: is it correct to assume we only get one
	 * oplock break, for each byte we read from the pipe?
	 */
	ctx->pending = false;

	/*
	 * Read one byte of zero to clear the
	 * kernel break notify message.
	 */

	if(read(ctx->read_fd, &dummy, 1) != 1) {
		DEBUG(0,("irix_oplock_receive_message: read of kernel "
			 "notification failed. Error was %s.\n",
			 strerror(errno) ));
		return NULL;
	}

	/*
	 * Do a query to get the
	 * device and inode of the file that has the break
	 * request outstanding.
	 */

	if(sys_fcntl_ptr(ctx->read_fd, F_OPLKSTAT, &os) < 0) {
		DEBUG(0,("irix_oplock_receive_message: fcntl of kernel "
			 "notification failed. Error was %s.\n",
			 strerror(errno) ));
		if(errno == EAGAIN) {
			/*
			 * Duplicate kernel break message - ignore.
			 */
			return NULL;
		}
		return NULL;
	}

	/*
	 * We only have device and inode info here - we have to guess that this
	 * is the first fsp open with this dev,ino pair.
	 *
	 * NOTE: this doesn't work if any VFS modules overloads
	 *       the file_id_create() hook!
	 */

	fileid = file_id_create_dev((SMB_DEV_T)os.os_dev,
				    (SMB_INO_T)os.os_ino);
	if ((fsp = file_find_di_first(fileid)) == NULL) {
		DEBUG(0,("irix_oplock_receive_message: unable to find open "
			 "file with dev = %x, inode = %.0f\n",
			 (unsigned int)os.os_dev, (double)os.os_ino ));
		return NULL;
	}
     
	DEBUG(5,("irix_oplock_receive_message: kernel oplock break request "
		 "received for file_id %s gen_id = %ul",
		 file_id_string_tos(&fsp->file_id),
		 fsp->fh->gen_id ));

	return fsp;
}

/****************************************************************************
 Attempt to set an kernel oplock on a file.
****************************************************************************/

static bool irix_set_kernel_oplock(struct kernel_oplocks *_ctx,
				   files_struct *fsp, int oplock_type)
{
	struct irix_oplocks_context *ctx = talloc_get_type(_ctx->private_data,
					   struct irix_oplocks_context);

	if (sys_fcntl_long(fsp->fh->fd, F_OPLKREG, ctx->write_fd) == -1) {
		if(errno != EAGAIN) {
			DEBUG(0,("irix_set_kernel_oplock: Unable to get "
				 "kernel oplock on file %s, file_id %s "
				 "gen_id = %ul. Error was %s\n", 
				 fsp->fsp_name, file_id_string_tos(&fsp->file_id), 
				 fsp->fh->gen_id,
				 strerror(errno) ));
		} else {
			DEBUG(5,("irix_set_kernel_oplock: Refused oplock on "
				 "file %s, fd = %d, file_id = %s, "
				 "gen_id = %ul. Another process had the file "
				 "open.\n",
				 fsp->fsp_name, fsp->fh->fd,
				 file_id_string_tos(&fsp->file_id),
				 fsp->fh->gen_id ));
		}
		return False;
	}
	
	DEBUG(10,("irix_set_kernel_oplock: got kernel oplock on file %s, file_id = %s "
		  "gen_id = %ul\n",
		  fsp->fsp_name, file_id_string_tos(&fsp->file_id),
		  fsp->fh->gen_id));

	return True;
}

/****************************************************************************
 Release a kernel oplock on a file.
****************************************************************************/

static void irix_release_kernel_oplock(struct kernel_oplocks *_ctx,
				       files_struct *fsp)
{
	if (DEBUGLVL(10)) {
		/*
		 * Check and print out the current kernel
		 * oplock state of this file.
		 */
		int state = sys_fcntl_long(fsp->fh->fd, F_OPLKACK, -1);
		dbgtext("irix_release_kernel_oplock: file %s, file_id = %s"
			"gen_id = %ul, has kernel oplock state "
			"of %x.\n", fsp->fsp_name, file_id_string_tos(&fsp->file_id),
                        fsp->fh->gen_id, state );
	}

	/*
	 * Remove the kernel oplock on this file.
	 */
	if(sys_fcntl_long(fsp->fh->fd, F_OPLKACK, OP_REVOKE) < 0) {
		if( DEBUGLVL( 0 )) {
			dbgtext("irix_release_kernel_oplock: Error when "
				"removing kernel oplock on file " );
			dbgtext("%s, file_id = %s gen_id = %ul. "
				"Error was %s\n",
				fsp->fsp_name, file_id_string_tos(&fsp->file_id),
				fsp->fh->gen_id,
				strerror(errno) );
		}
	}
}

static void irix_oplocks_read_fde_handler(struct event_context *ev,
					  struct fd_event *fde,
					  uint16_t flags,
					  void *private_data)
{
	struct irix_oplocks_context *ctx = talloc_get_type(private_data,
					   struct irix_oplocks_context);
	files_struct *fsp;

	fsp = irix_oplock_receive_message(ctx->ctx);
	break_kernel_oplock(smbd_messaging_context(), fsp);
}

/****************************************************************************
 Setup kernel oplocks.
****************************************************************************/

static const struct kernel_oplocks_ops irix_koplocks = {
	.set_oplock		= irix_set_kernel_oplock,
	.release_oplock		= irix_release_kernel_oplock,
};

struct kernel_oplocks *irix_init_kernel_oplocks(TALLOC_CTX *mem_ctx)
{
	struct kernel_oplocks *_ctx;
	struct irix_oplocks_context *ctx;
	int pfd[2];

	if (!irix_oplocks_available())
		return NULL;

	_ctx = talloc_zero(mem_ctx, struct kernel_oplocks);
	if (!_ctx) {
		return NULL;
	}

	ctx = talloc_zero(_ctx, struct irix_oplocks_context);
	if (!ctx) {
		talloc_free(_ctx);
		return NULL;
	}
	_ctx->ops = &irix_koplocks;
	_ctx->private_data = ctx;
	ctx->ctx = _ctx;

	if(pipe(pfd) != 0) {
		talloc_free(_ctx);
		DEBUG(0,("setup_kernel_oplock_pipe: Unable to create pipe. "
			 "Error was %s\n", strerror(errno) ));
		return False;
	}

	ctx->read_fd = pfd[0];
	ctx->write_fd = pfd[1];

	ctx->read_fde = event_add_fd(smbd_event_context(),
				     ctx,
				     ctx->read_fd,
				     EVENT_FD_READ,
				     irix_oplocks_read_fde_handler,
				     ctx);
	return _ctx;
}
#else
 void oplock_irix_dummy(void);
 void oplock_irix_dummy(void) {}
#endif /* HAVE_KERNEL_OPLOCKS_IRIX */