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
|
/*
Unix SMB/CIFS implementation.
POSIX NTVFS backend - notify
Copyright (C) Andrew Tridgell 2006
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 2 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, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
#include "vfs_posix.h"
#include "lib/messaging/irpc.h"
#include "messaging/messaging.h"
/* pending notifies buffer, hung off struct pvfs_file for open directories
that have used change notify */
struct pvfs_notify_buffer {
struct pvfs_file *f;
uint32_t num_changes;
struct notify_changes *changes;
uint32_t max_buffer_size;
uint32_t current_buffer_size;
/* these last two are only present when a notify request is
pending */
struct ntvfs_request *req;
struct smb_notify *info;
};
/*
send a reply to a pending notify request
*/
static void pvfs_notify_send(struct pvfs_notify_buffer *notify_buffer, NTSTATUS status)
{
struct ntvfs_request *req = notify_buffer->req;
struct smb_notify *info = notify_buffer->info;
info->out.num_changes = notify_buffer->num_changes;
info->out.changes = talloc_steal(req, notify_buffer->changes);
notify_buffer->num_changes = 0;
notify_buffer->changes = NULL;
notify_buffer->current_buffer_size = 0;
notify_buffer->req = NULL;
notify_buffer->info = NULL;
DEBUG(0,("sending %d changes\n", info->out.num_changes));
if (info->out.num_changes != 0) {
status = NT_STATUS_OK;
}
req->async_states->status = status;
req->async_states->send_fn(req);
}
/*
destroy a notify buffer. Called when the handle is closed
*/
static int pvfs_notify_destructor(void *ptr)
{
struct pvfs_notify_buffer *n = talloc_get_type(ptr, struct pvfs_notify_buffer);
notify_remove(n->f->pvfs->notify_context, n);
n->f->notify_buffer = NULL;
if (n->req) {
pvfs_notify_send(n, NT_STATUS_OK);
}
return 0;
}
/*
called when a async notify event comes in
*/
static void pvfs_notify_callback(void *private, const struct notify_event *ev)
{
struct pvfs_notify_buffer *n = talloc_get_type(private, struct pvfs_notify_buffer);
n->changes = talloc_realloc(n, n->changes, struct notify_changes, n->num_changes+1);
n->changes[n->num_changes].action = ev->action;
n->changes[n->num_changes].name.s = talloc_strdup(n->changes, ev->path);
n->num_changes++;
DEBUG(0,("got notify for '%s' action=%d\n", ev->path, ev->action));
if (n->req != NULL) {
pvfs_notify_send(n, NT_STATUS_OK);
}
}
/*
setup a notify buffer on a directory handle
*/
static NTSTATUS pvfs_notify_setup(struct pvfs_state *pvfs, struct pvfs_file *f,
uint32_t buffer_size, uint32_t filter, BOOL recursive)
{
NTSTATUS status;
struct notify_entry e;
f->notify_buffer = talloc_zero(f, struct pvfs_notify_buffer);
NT_STATUS_HAVE_NO_MEMORY(f->notify_buffer);
f->notify_buffer->max_buffer_size = buffer_size;
f->notify_buffer->f = f;
e.filter = filter;
e.path = f->handle->name->full_name;
e.recursive = recursive;
status = notify_add(pvfs->notify_context, &e,
pvfs_notify_callback, f->notify_buffer);
NT_STATUS_NOT_OK_RETURN(status);
talloc_set_destructor(f->notify_buffer, pvfs_notify_destructor);
return NT_STATUS_OK;
}
/*
called from the pvfs_wait code when either an event has come in, or
the notify request has been cancelled
*/
static void pvfs_notify_end(void *private, enum pvfs_wait_notice reason)
{
struct pvfs_notify_buffer *notify_buffer = talloc_get_type(private, struct pvfs_notify_buffer);
struct ntvfs_request *req = notify_buffer->req;
if (req == NULL) {
/* nothing to do, nobody is waiting */
return;
}
if (reason == PVFS_WAIT_CANCEL) {
pvfs_notify_send(notify_buffer, NT_STATUS_CANCELLED);
} else {
pvfs_notify_send(notify_buffer, NT_STATUS_OK);
}
}
/* change notify request - always async. This request blocks until the
event buffer is non-empty */
NTSTATUS pvfs_notify(struct ntvfs_module_context *ntvfs,
struct ntvfs_request *req,
struct smb_notify *info)
{
struct pvfs_state *pvfs = talloc_get_type(ntvfs->private_data,
struct pvfs_state);
struct pvfs_file *f;
NTSTATUS status;
f = pvfs_find_fd(pvfs, req, info->in.file.fnum);
if (!f) {
return NT_STATUS_INVALID_HANDLE;
}
/* this request doesn't make sense unless its async */
if (!(req->async_states->state & NTVFS_ASYNC_STATE_MAY_ASYNC)) {
return NT_STATUS_INVALID_PARAMETER;
}
/* its only valid for directories */
if (f->handle->fd != -1) {
return NT_STATUS_INVALID_PARAMETER;
}
/* if the handle doesn't currently have a notify buffer then
create one */
if (f->notify_buffer == NULL) {
status = pvfs_notify_setup(pvfs, f,
info->in.buffer_size,
info->in.completion_filter,
info->in.recursive);
NT_STATUS_NOT_OK_RETURN(status);
}
req->async_states->status = NT_STATUS_OK;
if (f->notify_buffer->req != NULL) {
DEBUG(0,("Notify already setup\n"));
pvfs_notify_send(f->notify_buffer, NT_STATUS_CANCELLED);
}
f->notify_buffer->req = talloc_reference(f->notify_buffer, req);
f->notify_buffer->info = info;
/* if the buffer is empty then start waiting */
if (f->notify_buffer->num_changes == 0) {
void *wait_handle =
pvfs_wait_message(pvfs, req, -1, timeval_zero(),
pvfs_notify_end, f->notify_buffer);
NT_STATUS_HAVE_NO_MEMORY(wait_handle);
talloc_steal(req, wait_handle);
return NT_STATUS_OK;
}
pvfs_notify_send(f->notify_buffer, NT_STATUS_OK);
return NT_STATUS_OK;
}
|