S3: Add in profile counters for new vfs and syscall entries.
[samba.git] / source3 / modules / onefs_cbrl.c
1 /*
2  * Unix SMB/CIFS implementation.
3  * Support for OneFS system interfaces.
4  *
5  * Copyright (C) Zack Kirsch, 2009
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include "onefs.h"
22
23 #include <ifs/ifs_syscalls.h>
24 #include <sys/isi_cifs_brl.h>
25 #include <isi_ecs/isi_ecs_cbrl.h>
26
27 #undef DBGC_CLASS
28 #define DBGC_CLASS DBGC_LOCKING
29
30 extern struct blocking_lock_record *blocking_lock_queue;
31
32 static uint64_t onefs_get_new_id(void) {
33         static uint64_t id = 0;
34
35         id++;
36
37         return id;
38 }
39
40 enum onefs_cbrl_lock_state {ONEFS_CBRL_NONE, ONEFS_CBRL_ASYNC, ONEFS_CBRL_DONE,
41         ONEFS_CBRL_ERROR};
42
43 struct onefs_cbrl_blr_state {
44         uint64_t id;
45         enum onefs_cbrl_lock_state state;
46 };
47
48 static char *onefs_cbrl_blr_state_str(const struct blocking_lock_record *blr)
49 {
50         static fstring result;
51         struct onefs_cbrl_blr_state *bs;
52
53         SMB_ASSERT(blr);
54
55         bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
56
57         if (bs == NULL) {
58                 fstrcpy(result, "NULL CBRL BLR state - Posix lock?");
59                 return result;
60         }
61
62         switch (bs->state) {
63         case ONEFS_CBRL_NONE:
64                 fstr_sprintf(result, "CBRL BLR id=%llu: state=NONE", bs->id);
65                 break;
66         case ONEFS_CBRL_ASYNC:
67                 fstr_sprintf(result, "CBRL BLR id=%llu: state=ASYNC", bs->id);
68                 break;
69         case ONEFS_CBRL_DONE:
70                 fstr_sprintf(result, "CBRL BLR id=%llu: state=DONE", bs->id);
71                 break;
72         case ONEFS_CBRL_ERROR:
73                 fstr_sprintf(result, "CBRL BLR id=%llu: state=ERROR", bs->id);
74                 break;
75         default:
76                 fstr_sprintf(result, "CBRL BLR id=%llu: unknown state %d",
77                     bs->id, bs->state);
78                 break;
79         }
80
81         return result;
82 }
83
84 static void onefs_cbrl_enumerate_blq(const char *fn)
85 {
86         struct blocking_lock_record *blr;
87
88         if (DEBUGLVL(10))
89                 return;
90
91         DEBUG(10, ("CBRL BLR records (%s):\n", fn));
92
93         for (blr = blocking_lock_queue; blr; blr = blr->next)
94                 DEBUGADD(10, ("%s\n", onefs_cbrl_blr_state_str(blr)));
95 }
96
97 static struct blocking_lock_record *onefs_cbrl_find_blr(uint64_t id)
98 {
99         struct blocking_lock_record *blr;
100         struct onefs_cbrl_blr_state *bs;
101
102         onefs_cbrl_enumerate_blq("onefs_cbrl_find_blr");
103
104         for (blr = blocking_lock_queue; blr; blr = blr->next) {
105                 bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
106
107                 /* We don't control all of the BLRs on the BLQ. */
108                 if (bs == NULL)
109                         continue;
110
111                 if (bs->id == id) {
112                         DEBUG(10, ("found %s\n",
113                             onefs_cbrl_blr_state_str(blr)));
114                         break;
115                 }
116         }
117
118         if (blr == NULL) {
119                 DEBUG(5, ("Could not find CBRL BLR for id %llu\n", id));
120                 return NULL;
121         }
122
123         return blr;
124 }
125
126 static void onefs_cbrl_async_success(uint64_t id)
127 {
128         struct blocking_lock_record *blr;
129         struct onefs_cbrl_blr_state *bs;
130         uint16 num_locks;
131
132         DEBUG(10, ("CBRL async success!\n"));
133
134         /* Find BLR with id. Its okay not to find one (race with cancel) */
135         blr = onefs_cbrl_find_blr(id);
136         if (blr == NULL)
137                 return;
138
139         bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
140         SMB_ASSERT(bs);
141         SMB_ASSERT(bs->state == ONEFS_CBRL_ASYNC);
142
143         blr->lock_num++;
144
145         num_locks = SVAL(blr->req->vwv+7, 0);
146
147         if (blr->lock_num == num_locks)
148                 bs->state = ONEFS_CBRL_DONE;
149         else
150                 bs->state = ONEFS_CBRL_NONE;
151
152         /* Process the queue, to try the next lock or finish up. */
153         process_blocking_lock_queue();
154 }
155
156 static void onefs_cbrl_async_failure(uint64_t id)
157 {
158         struct blocking_lock_record *blr;
159         struct onefs_cbrl_blr_state *bs;
160
161         DEBUG(10, ("CBRL async failure!\n"));
162
163         /* Find BLR with id. Its okay not to find one (race with cancel) */
164         blr = onefs_cbrl_find_blr(id);
165         if (blr == NULL)
166                 return;
167
168         bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
169         SMB_ASSERT(bs);
170
171         SMB_ASSERT(bs->state == ONEFS_CBRL_ASYNC);
172         bs->state = ONEFS_CBRL_ERROR;
173
174         /* Process the queue. It will end up trying to retake the same lock,
175          * see the error in onefs_cbrl_lock_windows() and fail. */
176         process_blocking_lock_queue();
177 }
178
179 static struct cbrl_event_ops cbrl_ops =
180     {.cbrl_async_success = onefs_cbrl_async_success,
181      .cbrl_async_failure = onefs_cbrl_async_failure};
182  
183 static void onefs_cbrl_events_handler(struct event_context *ev,
184                                         struct fd_event *fde,
185                                         uint16_t flags,
186                                         void *private_data)
187 {
188         DEBUG(10, ("onefs_cbrl_events_handler\n"));
189
190         if (cbrl_event_dispatcher(&cbrl_ops)) {
191                 DEBUG(0, ("cbrl_event_dispatcher failed: %s\n",
192                         strerror(errno)));
193         }
194 }
195
196 static void onefs_init_cbrl(void)
197 {
198         static bool init_done = false;
199         static int cbrl_event_fd;
200         static struct fd_event *cbrl_fde;
201
202         if (init_done)
203                 return;
204
205         DEBUG(10, ("onefs_init_cbrl\n"));
206
207         /* Register the event channel for CBRL. */
208         cbrl_event_fd = cbrl_event_register();
209         if (cbrl_event_fd == -1) {
210                 DEBUG(0, ("cbrl_event_register failed: %s\n",
211                         strerror(errno)));
212                 return;
213         }
214
215         DEBUG(10, ("cbrl_event_fd = %d\n", cbrl_event_fd));
216
217         /* Register the oplock event_fd with samba's event system */
218         cbrl_fde = event_add_fd(smbd_event_context(),
219                                      NULL,
220                                      cbrl_event_fd,
221                                      EVENT_FD_READ,
222                                      onefs_cbrl_events_handler,
223                                      NULL);
224
225         init_done = true;
226         return;
227 }
228
229 /**
230  * Blocking PID. As far as I can tell, the blocking_pid is only used to tell
231  * whether a posix lock or a CIFS lock blocked us. If it was a posix lock,
232  * Samba polls every 10 seconds, which we don't want. -zkirsch
233  */
234 #define ONEFS_BLOCKING_PID 0xABCDABCD
235
236 /**
237  * @param[in]     br_lck        Contains the fsp.
238  * @param[in]     plock         Lock request.
239  * @param[in]     blocking_lock Only used for figuring out the error.
240  * @param[in,out] blr           The BLR for the already-deferred operation.
241  */
242 NTSTATUS onefs_brl_lock_windows(vfs_handle_struct *handle,
243                                 struct byte_range_lock *br_lck,
244                                 struct lock_struct *plock,
245                                 bool blocking_lock,
246                                 struct blocking_lock_record *blr)
247 {
248         int fd = br_lck->fsp->fh->fd;
249         uint64_t id = 0;
250         bool exclusive = false;
251         bool async = false;
252         bool pending = false;
253         bool pending_async = false;
254         int error;
255         struct onefs_cbrl_blr_state *bs;
256         NTSTATUS status;
257
258         START_PROFILE(syscall_brl_lock);
259
260         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
261         SMB_ASSERT(plock->lock_type != UNLOCK_LOCK);
262
263         onefs_cbrl_enumerate_blq("onefs_brl_lock_windows");
264
265         /* Will only initialize the first time its called. */
266         onefs_init_cbrl();
267
268         switch (plock->lock_type) {
269                 case WRITE_LOCK:
270                         exclusive = true;
271                         break;
272                 case READ_LOCK:
273                         break;
274                 case PENDING_WRITE_LOCK:
275                         /* Called when a blocking lock request is added - do an
276                          * async lock. */
277                         pending = true;
278                         async = true;
279                         exclusive = true;
280                         break;
281                 case PENDING_READ_LOCK:
282                         /* Called when a blocking lock request is added - do an
283                          * async lock. */
284                         pending = true;
285                         async = true;
286                         break;
287                 default:
288                         /* UNLOCK_LOCK: should only be used for a POSIX_LOCK */
289                         smb_panic("Invalid plock->lock_type passed in to "
290                             "onefs_brl_lock_windows");
291         }
292
293         /* Figure out if we're actually doing the lock or a no-op. We need to
294          * do a no-op when process_blocking_lock_queue calls back into us.
295          *
296          * We know process_* is calling into us if a blr is passed in and
297          * pending is false. */
298         if (!pending && blr) {
299                 /* Check the BLR state. */
300                 bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
301                 SMB_ASSERT(bs);
302
303                 /* ASYNC still in progress: The process_* calls will keep
304                  * calling even if we haven't gotten the lock. Keep erroring
305                  * without calling ifs_cbrl, or getting/setting an id. */
306                 if (bs->state == ONEFS_CBRL_ASYNC) {
307                         goto failure;
308                 }
309                 else if (bs->state == ONEFS_CBRL_ERROR) {
310                         END_PROFILE(syscall_brl_lock);
311                         return NT_STATUS_NO_MEMORY;
312                 }
313
314                 SMB_ASSERT(bs->state == ONEFS_CBRL_NONE);
315                 async = true;
316         }
317
318         if (async) {
319                 SMB_ASSERT(blocking_lock);
320                 SMB_ASSERT(blr);
321                 id = onefs_get_new_id();
322         }
323
324         DEBUG(10, ("Calling ifs_cbrl(LOCK)..."));
325         error = ifs_cbrl(fd, CBRL_OP_LOCK, exclusive, plock->start,
326             plock->size, async, id, plock->context.smbpid, plock->context.tid,
327             plock->fnum);
328         if (!error) {
329                 goto success;
330         } else if (errno == EWOULDBLOCK) {
331                 SMB_ASSERT(!async);
332         } else if (errno == EINPROGRESS) {
333                 SMB_ASSERT(async);
334
335                 if (pending) {
336                         /* Talloc a new BLR private state. */
337                         blr->blr_private = talloc(blr, struct onefs_cbrl_blr_state);
338                         pending_async = true;
339                 }
340
341                 /* Store the new id in the BLR private state. */
342                 bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
343                 bs->id = id;
344                 bs->state = ONEFS_CBRL_ASYNC;
345         } else {
346                 DEBUG(0, ("onefs_brl_lock_windows failure: error=%d (%s).\n",
347                         errno, strerror(errno)));
348         }
349
350 failure:
351
352         END_PROFILE(syscall_brl_lock);
353
354         /* Failure - error or async. */
355         plock->context.smbpid = (uint32) ONEFS_BLOCKING_PID;
356
357         if (pending_async)
358                 status = NT_STATUS_OK;
359         else
360                 status = brl_lock_failed(br_lck->fsp, plock, blocking_lock);
361
362         DEBUG(10, ("returning %s.\n", nt_errstr(status)));
363         return status;
364
365 success:
366
367         END_PROFILE(syscall_brl_lock);
368
369         /* Success. */
370         onefs_cbrl_enumerate_blq("onefs_brl_unlock_windows");
371         DEBUG(10, ("returning NT_STATUS_OK.\n"));
372         return NT_STATUS_OK;
373 }
374
375 #define CBRL_NOTYPE true
376
377 bool onefs_brl_unlock_windows(vfs_handle_struct *handle,
378                               struct messaging_context *msg_ctx,
379                               struct byte_range_lock *br_lck,
380                               const struct lock_struct *plock)
381 {
382         int error;
383         int fd = br_lck->fsp->fh->fd;
384
385         START_PROFILE(syscall_brl_unlock);
386
387         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
388         SMB_ASSERT(plock->lock_type == UNLOCK_LOCK);
389
390         DEBUG(10, ("Calling ifs_cbrl(UNLOCK)..."));
391         error = ifs_cbrl(fd, CBRL_OP_UNLOCK, CBRL_NOTYPE,
392             plock->start, plock->size, CBRL_NOTYPE, 0, plock->context.smbpid,
393             plock->context.tid, plock->fnum);
394
395         END_PROFILE(syscall_brl_unlock);
396
397         if (error) {
398                 DEBUG(10, ("returning false.\n"));
399                 return false;
400         }
401
402         DEBUG(10, ("returning true.\n"));
403         return true;
404
405         /* Problem with storing things in TDB: I won't know what BRL to unlock in the TDB.
406          *  - I could fake it?
407          *  - I could send Samba a message with which lock is being unlocked?
408          *  - I could *easily* make the "id" something you always pass in to
409          *  lock, unlock or cancel -- it identifies a lock. Makes sense!
410          */
411 }
412
413 /* Default implementation only calls this on PENDING locks. */
414 bool onefs_brl_cancel_windows(vfs_handle_struct *handle,
415                               struct byte_range_lock *br_lck,
416                               struct lock_struct *plock,
417                               struct blocking_lock_record *blr)
418 {
419         int error;
420         int fd = br_lck->fsp->fh->fd;
421         struct onefs_cbrl_blr_state *bs;
422
423         START_PROFILE(syscall_brl_cancel);
424
425         SMB_ASSERT(plock);
426         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
427         SMB_ASSERT(blr);
428
429         onefs_cbrl_enumerate_blq("onefs_brl_cancel_windows");
430
431         bs = ((struct onefs_cbrl_blr_state *)blr->blr_private);
432         SMB_ASSERT(bs);
433
434         if (bs->state == ONEFS_CBRL_DONE) {
435                 /* No-op. */
436                 DEBUG(10, ("State=DONE, returning true\n"));
437                 END_PROFILE(syscall_brl_cancel);
438                 return true;
439         }
440
441         SMB_ASSERT(bs->state == ONEFS_CBRL_NONE ||
442             bs->state == ONEFS_CBRL_ASYNC);
443
444         /* A real cancel. */
445         DEBUG(10, ("Calling ifs_cbrl(CANCEL)..."));
446         error = ifs_cbrl(fd, CBRL_OP_CANCEL, CBRL_NOTYPE, plock->start,
447             plock->size, CBRL_NOTYPE, bs->id, plock->context.smbpid,
448             plock->context.tid, plock->fnum);
449
450         END_PROFILE(syscall_brl_cancel);
451
452         if (error) {
453                 DEBUG(10, ("returning false\n"));
454                 bs->state = ONEFS_CBRL_ERROR;
455                 return false;
456         }
457
458         bs->state = ONEFS_CBRL_DONE;
459         onefs_cbrl_enumerate_blq("onefs_brl_cancel_windows");
460         DEBUG(10, ("returning true\n"));
461         return true;
462 }
463
464 /* TODO Optimization: Abstract out brl_get_locks() in the Windows case.
465  * We'll malloc some memory or whatever (can't return NULL), but not actually
466  * touch the TDB. */
467
468 /* XXX brl_locktest: CBRL does not support calling this, but its only for
469  * strict locking. Add empty VOP? */
470
471 /* XXX brl_lockquery: CBRL does not support calling this for WINDOWS LOCKS, but
472  * its only called for POSIX LOCKS. Add empty VOP? */
473
474 /* XXX brl_close_fnum: CBRL will do this automatically. I think this is a NO-OP
475  * for us, we could add an empty VOP. */
476