Fix PReg write support.
[Samba/eduardoll.git] / source4 / ntvfs / sysdep / sys_lease_linux.c
blob0727eed212105fc4bbb632a91d147fe5a8c89cd4
1 /*
2 Unix SMB/CIFS implementation.
4 Copyright (C) Stefan Metzmacher 2008
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 lease (oplock) implementation using fcntl F_SETLEASE on linux
24 #include "includes.h"
25 #include "system/filesys.h"
26 #include "lib/events/events.h"
27 #include "ntvfs/sysdep/sys_lease.h"
28 #include "ntvfs/ntvfs.h"
29 #include "librpc/gen_ndr/ndr_opendb.h"
30 #include "lib/util/dlinklist.h"
31 #include "cluster/cluster.h"
33 #define LINUX_LEASE_RT_SIGNAL (SIGRTMIN+1)
35 struct linux_lease_pending {
36 struct linux_lease_pending *prev, *next;
37 struct sys_lease_context *ctx;
38 struct opendb_entry e;
41 /* the global linked list of pending leases */
42 static struct linux_lease_pending *leases;
44 static void linux_lease_signal_handler(struct event_context *ev_ctx,
45 struct signal_event *se,
46 int signum, int count,
47 void *_info, void *private_data)
49 struct sys_lease_context *ctx = talloc_get_type(private_data,
50 struct sys_lease_context);
51 siginfo_t *info = (siginfo_t *)_info;
52 struct linux_lease_pending *c;
53 int got_fd = info->si_fd;
55 for (c = leases; c; c = c->next) {
56 int *fd = (int *)c->e.fd;
58 if (got_fd == *fd) {
59 break;
63 if (!c) {
64 return;
67 ctx->break_send(ctx->msg_ctx, &c->e, OPLOCK_BREAK_TO_NONE);
70 static int linux_lease_pending_destructor(struct linux_lease_pending *p)
72 int ret;
73 int *fd = (int *)p->e.fd;
75 DLIST_REMOVE(leases, p);
77 if (*fd == -1) {
78 return 0;
81 ret = fcntl(*fd, F_SETLEASE, F_UNLCK);
82 if (ret == -1) {
83 DEBUG(0,("%s: failed to remove oplock: %s\n",
84 __FUNCTION__, strerror(errno)));
87 return 0;
90 static NTSTATUS linux_lease_init(struct sys_lease_context *ctx)
92 struct signal_event *se;
94 se = event_add_signal(ctx->event_ctx, ctx,
95 LINUX_LEASE_RT_SIGNAL, SA_SIGINFO,
96 linux_lease_signal_handler, ctx);
97 NT_STATUS_HAVE_NO_MEMORY(se);
99 return NT_STATUS_OK;
102 static NTSTATUS linux_lease_setup(struct sys_lease_context *ctx,
103 struct opendb_entry *e)
105 int ret;
106 int *fd = (int *)e->fd;
107 struct linux_lease_pending *p;
109 if (e->oplock_level == OPLOCK_NONE) {
110 e->fd = NULL;
111 return NT_STATUS_OK;
112 } else if (e->oplock_level == OPLOCK_LEVEL_II) {
114 * the linux kernel doesn't support level2 oplocks
115 * so fix up the granted oplock level
117 e->oplock_level = OPLOCK_NONE;
118 e->allow_level_II_oplock = false;
119 e->fd = NULL;
120 return NT_STATUS_OK;
123 p = talloc(ctx, struct linux_lease_pending);
124 NT_STATUS_HAVE_NO_MEMORY(p);
126 p->ctx = ctx;
127 p->e = *e;
129 ret = fcntl(*fd, F_SETSIG, LINUX_LEASE_RT_SIGNAL);
130 if (ret == -1) {
131 talloc_free(p);
132 return map_nt_error_from_unix(errno);
135 ret = fcntl(*fd, F_SETLEASE, F_WRLCK);
136 if (ret == -1) {
137 talloc_free(p);
138 return map_nt_error_from_unix(errno);
141 DLIST_ADD(leases, p);
143 talloc_set_destructor(p, linux_lease_pending_destructor);
145 return NT_STATUS_OK;
148 static NTSTATUS linux_lease_remove(struct sys_lease_context *ctx,
149 struct opendb_entry *e);
151 static NTSTATUS linux_lease_update(struct sys_lease_context *ctx,
152 struct opendb_entry *e)
154 struct linux_lease_pending *c;
156 for (c = leases; c; c = c->next) {
157 if (c->e.fd == e->fd) {
158 break;
162 if (!c) {
163 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
167 * set the fd pointer to NULL so that the caller
168 * will not call the remove function as the oplock
169 * is already removed
171 e->fd = NULL;
173 talloc_free(c);
175 return NT_STATUS_OK;
178 static NTSTATUS linux_lease_remove(struct sys_lease_context *ctx,
179 struct opendb_entry *e)
181 struct linux_lease_pending *c;
183 for (c = leases; c; c = c->next) {
184 if (c->e.fd == e->fd) {
185 break;
189 if (!c) {
190 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
193 talloc_free(c);
195 return NT_STATUS_OK;
198 static struct sys_lease_ops linux_lease_ops = {
199 .name = "linux",
200 .init = linux_lease_init,
201 .setup = linux_lease_setup,
202 .update = linux_lease_update,
203 .remove = linux_lease_remove
207 initialialise the linux lease module
209 NTSTATUS sys_lease_linux_init(void)
211 /* register ourselves as a system lease module */
212 return sys_lease_register(&linux_lease_ops);