smbd: Remove unused [push_pull]_file_id_24
[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 "includes.h"
22 #include "onefs.h"
23
24 #include <ifs/ifs_syscalls.h>
25 #include <sys/isi_cifs_brl.h>
26 #include <isi_ecs/isi_ecs_cbrl.h>
27
28 #undef DBGC_CLASS
29 #define DBGC_CLASS DBGC_LOCKING
30
31 static uint64_t onefs_get_new_id(void) {
32         static uint64_t id = 0;
33
34         id++;
35
36         return id;
37 }
38
39 enum onefs_cbrl_lock_state {ONEFS_CBRL_NONE, ONEFS_CBRL_ASYNC, ONEFS_CBRL_DONE,
40         ONEFS_CBRL_ERROR};
41
42 struct onefs_cbrl_blr_state {
43         uint64_t id;
44         enum onefs_cbrl_lock_state state;
45 };
46
47 static char *onefs_cbrl_blr_state_str(const struct blocking_lock_record *blr)
48 {
49         static fstring result;
50         struct onefs_cbrl_blr_state *bs;
51
52         SMB_ASSERT(blr);
53
54         bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
55
56         if (bs == NULL) {
57                 fstrcpy(result, "NULL CBRL BLR state - Posix lock?");
58                 return result;
59         }
60
61         switch (bs->state) {
62         case ONEFS_CBRL_NONE:
63                 fstr_sprintf(result, "CBRL BLR id=%llu: state=NONE", bs->id);
64                 break;
65         case ONEFS_CBRL_ASYNC:
66                 fstr_sprintf(result, "CBRL BLR id=%llu: state=ASYNC", bs->id);
67                 break;
68         case ONEFS_CBRL_DONE:
69                 fstr_sprintf(result, "CBRL BLR id=%llu: state=DONE", bs->id);
70                 break;
71         case ONEFS_CBRL_ERROR:
72                 fstr_sprintf(result, "CBRL BLR id=%llu: state=ERROR", bs->id);
73                 break;
74         default:
75                 fstr_sprintf(result, "CBRL BLR id=%llu: unknown state %d",
76                     bs->id, bs->state);
77                 break;
78         }
79
80         return result;
81 }
82
83 static void onefs_cbrl_enumerate_blq(const char *fn)
84 {
85         struct smbd_server_connection *sconn = smbd_server_conn;
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         if (sconn->using_smb2) {
94                 struct smbd_smb2_request *smb2req;
95                 for (smb2req = sconn->smb2.requests; smb2req; smb2req = nextreq) {
96                         blr = get_pending_smb2req_blr(smb2req);
97                         if (blr) {
98                                 DEBUGADD(10, ("%s\n", onefs_cbrl_blr_state_str(blr)));
99                         }
100                 }
101         } else {
102                 for (blr = sconn->smb1.locks.blocking_lock_queue; blr; blr = blr->next)
103                         DEBUGADD(10, ("%s\n", onefs_cbrl_blr_state_str(blr)));
104         }
105 }
106
107 static struct blocking_lock_record *onefs_cbrl_find_blr(uint64_t id)
108 {
109         struct smbd_server_connection *sconn = smbd_server_conn;
110         struct blocking_lock_record *blr;
111         struct onefs_cbrl_blr_state *bs;
112
113         onefs_cbrl_enumerate_blq("onefs_cbrl_find_blr");
114
115         if (sconn->using_smb2) {
116                 struct smbd_smb2_request *smb2req;
117                 for (smb2req = sconn->smb2.requests; smb2req; smb2req = nextreq) {
118                         blr = get_pending_smb2req_blr(smb2req);
119                         if (!blr) {
120                                 continue;
121                         }
122                         bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
123                         if (bs == NULL) {
124                                 continue;
125                         }
126                         if (bs->id == id) {
127                                 DEBUG(10, ("found %s\n",
128                                     onefs_cbrl_blr_state_str(blr)));
129                                 break;
130                         }
131                 }
132         } else {
133                 for (blr = sconn->smb1.locks.blocking_lock_queue; blr; blr = blr->next) {
134                         bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
135
136                         /* We don't control all of the BLRs on the BLQ. */
137                         if (bs == NULL)
138                                 continue;
139
140                         if (bs->id == id) {
141                                 DEBUG(10, ("found %s\n",
142                                     onefs_cbrl_blr_state_str(blr)));
143                                 break;
144                         }
145                 }
146         }
147
148         if (blr == NULL) {
149                 DEBUG(5, ("Could not find CBRL BLR for id %llu\n", id));
150                 return NULL;
151         }
152
153         return blr;
154 }
155
156 static void onefs_cbrl_async_success(uint64_t id)
157 {
158         struct blocking_lock_record *blr;
159         struct onefs_cbrl_blr_state *bs;
160         uint16 num_locks;
161
162         DEBUG(10, ("CBRL async success!\n"));
163
164         /* Find BLR with id. Its okay not to find one (race with cancel) */
165         blr = onefs_cbrl_find_blr(id);
166         if (blr == NULL)
167                 return;
168
169         bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
170         SMB_ASSERT(bs);
171         SMB_ASSERT(bs->state == ONEFS_CBRL_ASYNC);
172
173         blr->lock_num++;
174
175         num_locks = SVAL(blr->req->vwv+7, 0);
176
177         if (blr->lock_num == num_locks)
178                 bs->state = ONEFS_CBRL_DONE;
179         else
180                 bs->state = ONEFS_CBRL_NONE;
181
182         /* Self contend our own level 2 oplock. The kernel handles
183          * contention of other opener's level 2 oplocks. */
184         contend_level2_oplocks_begin(blr->fsp,
185             LEVEL2_CONTEND_WINDOWS_BRL);
186
187         /* Process the queue, to try the next lock or finish up. */
188         process_blocking_lock_queue(smbd_server_conn);
189 }
190
191 static void onefs_cbrl_async_failure(uint64_t id)
192 {
193         struct blocking_lock_record *blr;
194         struct onefs_cbrl_blr_state *bs;
195
196         DEBUG(10, ("CBRL async failure!\n"));
197
198         /* Find BLR with id. Its okay not to find one (race with cancel) */
199         blr = onefs_cbrl_find_blr(id);
200         if (blr == NULL)
201                 return;
202
203         bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
204         SMB_ASSERT(bs);
205
206         SMB_ASSERT(bs->state == ONEFS_CBRL_ASYNC);
207         bs->state = ONEFS_CBRL_ERROR;
208
209         /* Process the queue. It will end up trying to retake the same lock,
210          * see the error in onefs_cbrl_lock_windows() and fail. */
211         process_blocking_lock_queue(smbd_server_conn);
212 }
213
214 static struct cbrl_event_ops cbrl_ops =
215     {.cbrl_async_success = onefs_cbrl_async_success,
216      .cbrl_async_failure = onefs_cbrl_async_failure};
217  
218 static void onefs_cbrl_events_handler(struct event_context *ev,
219                                         struct fd_event *fde,
220                                         uint16_t flags,
221                                         void *private_data)
222 {
223         DEBUG(10, ("onefs_cbrl_events_handler\n"));
224
225         if (cbrl_event_dispatcher(&cbrl_ops)) {
226                 DEBUG(0, ("cbrl_event_dispatcher failed: %s\n",
227                         strerror(errno)));
228         }
229 }
230
231 static void onefs_init_cbrl(void)
232 {
233         static bool init_done = false;
234         static int cbrl_event_fd;
235         static struct fd_event *cbrl_fde;
236
237         if (init_done)
238                 return;
239
240         DEBUG(10, ("onefs_init_cbrl\n"));
241
242         /* Register the event channel for CBRL. */
243         cbrl_event_fd = cbrl_event_register();
244         if (cbrl_event_fd == -1) {
245                 DEBUG(0, ("cbrl_event_register failed: %s\n",
246                         strerror(errno)));
247                 return;
248         }
249
250         DEBUG(10, ("cbrl_event_fd = %d\n", cbrl_event_fd));
251
252         /* Register the CBRL event_fd with samba's event system */
253         cbrl_fde = event_add_fd(smbd_event_context(),
254                                      NULL,
255                                      cbrl_event_fd,
256                                      EVENT_FD_READ,
257                                      onefs_cbrl_events_handler,
258                                      NULL);
259
260         init_done = true;
261         return;
262 }
263
264 /**
265  * Blocking PID. As far as I can tell, the blocking_pid is only used to tell
266  * whether a posix lock or a CIFS lock blocked us. If it was a posix lock,
267  * Samba polls every 10 seconds, which we don't want. -zkirsch
268  */
269 #define ONEFS_BLOCKING_PID 0xABCDABCD
270
271 /**
272  * @param[in]     br_lck        Contains the fsp.
273  * @param[in]     plock         Lock request.
274  * @param[in]     blocking_lock Only used for figuring out the error.
275  * @param[in,out] blr           The BLR for the already-deferred operation.
276  */
277 NTSTATUS onefs_brl_lock_windows(vfs_handle_struct *handle,
278                                 struct byte_range_lock *br_lck,
279                                 struct lock_struct *plock,
280                                 bool blocking_lock,
281                                 struct blocking_lock_record *blr)
282 {
283         int fd = br_lck->fsp->fh->fd;
284         uint64_t id = 0;
285         enum cbrl_lock_type type;
286         bool async = false;
287         bool pending = false;
288         bool pending_async = false;
289         int error;
290         struct onefs_cbrl_blr_state *bs;
291         NTSTATUS status;
292
293         START_PROFILE(syscall_brl_lock);
294
295         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
296         SMB_ASSERT(plock->lock_type != UNLOCK_LOCK);
297
298         onefs_cbrl_enumerate_blq("onefs_brl_lock_windows");
299
300         /* Will only initialize the first time its called. */
301         onefs_init_cbrl();
302
303         switch (plock->lock_type) {
304                 case WRITE_LOCK:
305                         type = CBRL_LK_EX;
306                         break;
307                 case READ_LOCK:
308                         type = CBRL_LK_SH;
309                         break;
310                 case PENDING_WRITE_LOCK:
311                         /* Called when a blocking lock request is added - do an
312                          * async lock. */
313                         type = CBRL_LK_EX;
314                         pending = true;
315                         async = true;
316                         break;
317                 case PENDING_READ_LOCK:
318                         /* Called when a blocking lock request is added - do an
319                          * async lock. */
320                         type = CBRL_LK_SH;
321                         pending = true;
322                         async = true;
323                         break;
324                 default:
325                         /* UNLOCK_LOCK: should only be used for a POSIX_LOCK */
326                         smb_panic("Invalid plock->lock_type passed in to "
327                             "onefs_brl_lock_windows");
328         }
329
330         /* Figure out if we're actually doing the lock or a no-op. We need to
331          * do a no-op when process_blocking_lock_queue calls back into us.
332          *
333          * We know process_* is calling into us if a blr is passed in and
334          * pending is false. */
335         if (!pending && blr) {
336                 /* Check the BLR state. */
337                 bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
338                 SMB_ASSERT(bs);
339
340                 /* ASYNC still in progress: The process_* calls will keep
341                  * calling even if we haven't gotten the lock. Keep erroring
342                  * without calling ifs_cbrl, or getting/setting an id. */
343                 if (bs->state == ONEFS_CBRL_ASYNC) {
344                         goto failure;
345                 }
346                 else if (bs->state == ONEFS_CBRL_ERROR) {
347                         END_PROFILE(syscall_brl_lock);
348                         return NT_STATUS_NO_MEMORY;
349                 }
350
351                 SMB_ASSERT(bs->state == ONEFS_CBRL_NONE);
352                 async = true;
353         }
354
355         if (async) {
356                 SMB_ASSERT(blocking_lock);
357                 SMB_ASSERT(blr);
358                 id = onefs_get_new_id();
359         }
360
361         DEBUG(10, ("Calling ifs_cbrl(LOCK)...\n"));
362         error = ifs_cbrl(fd, CBRL_OP_LOCK, type, plock->start,
363             plock->size, async, id, plock->context.smbpid, plock->context.tid,
364             plock->fnum);
365         if (!error) {
366                 goto success;
367         } else if (errno == EWOULDBLOCK) {
368                 SMB_ASSERT(!async);
369         } else if (errno == EINPROGRESS) {
370                 SMB_ASSERT(async);
371
372                 if (pending) {
373                         /* Talloc a new BLR private state. */
374                         blr->blr_private = talloc(blr, struct onefs_cbrl_blr_state);
375                         pending_async = true;
376                 }
377
378                 /* Store the new id in the BLR private state. */
379                 bs = (struct onefs_cbrl_blr_state *)blr->blr_private;
380                 bs->id = id;
381                 bs->state = ONEFS_CBRL_ASYNC;
382         } else {
383                 DEBUG(0, ("onefs_brl_lock_windows failure: error=%d (%s).\n",
384                         errno, strerror(errno)));
385         }
386
387 failure:
388
389         END_PROFILE(syscall_brl_lock);
390
391         /* Failure - error or async. */
392         plock->context.smbpid = (uint32) ONEFS_BLOCKING_PID;
393
394         if (pending_async)
395                 status = NT_STATUS_OK;
396         else
397                 status = brl_lock_failed(br_lck->fsp, plock, blocking_lock);
398
399         DEBUG(10, ("returning %s.\n", nt_errstr(status)));
400         return status;
401
402 success:
403         /* Self contend our own level 2 oplock. The kernel handles
404          * contention of other opener's level 2 oplocks. */
405         contend_level2_oplocks_begin(br_lck->fsp,
406             LEVEL2_CONTEND_WINDOWS_BRL);
407
408         END_PROFILE(syscall_brl_lock);
409
410         /* Success. */
411         onefs_cbrl_enumerate_blq("onefs_brl_unlock_windows");
412         DEBUG(10, ("returning NT_STATUS_OK.\n"));
413         return NT_STATUS_OK;
414 }
415
416 bool onefs_brl_unlock_windows(vfs_handle_struct *handle,
417                               struct messaging_context *msg_ctx,
418                               struct byte_range_lock *br_lck,
419                               const struct lock_struct *plock)
420 {
421         int error;
422         int fd = br_lck->fsp->fh->fd;
423
424         START_PROFILE(syscall_brl_unlock);
425
426         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
427         SMB_ASSERT(plock->lock_type == UNLOCK_LOCK);
428
429         DEBUG(10, ("Calling ifs_cbrl(UNLOCK)...\n"));
430         error = ifs_cbrl(fd, CBRL_OP_UNLOCK, CBRL_LK_SH,
431             plock->start, plock->size, false, 0, plock->context.smbpid,
432             plock->context.tid, plock->fnum);
433
434         END_PROFILE(syscall_brl_unlock);
435
436         if (error) {
437                 DEBUG(10, ("returning false.\n"));
438                 return false;
439         }
440
441         /* For symmetry purposes, end our oplock contention even though its
442          * currently a no-op. */
443         contend_level2_oplocks_end(br_lck->fsp, LEVEL2_CONTEND_WINDOWS_BRL);
444
445         DEBUG(10, ("returning true.\n"));
446         return true;
447
448         /* Problem with storing things in TDB: I won't know what BRL to unlock in the TDB.
449          *  - I could fake it?
450          *  - I could send Samba a message with which lock is being unlocked?
451          *  - I could *easily* make the "id" something you always pass in to
452          *  lock, unlock or cancel -- it identifies a lock. Makes sense!
453          */
454 }
455
456 /* Default implementation only calls this on PENDING locks. */
457 bool onefs_brl_cancel_windows(vfs_handle_struct *handle,
458                               struct byte_range_lock *br_lck,
459                               struct lock_struct *plock,
460                               struct blocking_lock_record *blr)
461 {
462         int error;
463         int fd = br_lck->fsp->fh->fd;
464         struct onefs_cbrl_blr_state *bs;
465
466         START_PROFILE(syscall_brl_cancel);
467
468         SMB_ASSERT(plock);
469         SMB_ASSERT(plock->lock_flav == WINDOWS_LOCK);
470         SMB_ASSERT(blr);
471
472         onefs_cbrl_enumerate_blq("onefs_brl_cancel_windows");
473
474         bs = ((struct onefs_cbrl_blr_state *)blr->blr_private);
475         SMB_ASSERT(bs);
476
477         if (bs->state == ONEFS_CBRL_DONE || bs->state == ONEFS_CBRL_ERROR) {
478                 /* No-op. */
479                 DEBUG(10, ("State=%d, returning true\n", bs->state));
480                 END_PROFILE(syscall_brl_cancel);
481                 return true;
482         }
483
484         SMB_ASSERT(bs->state == ONEFS_CBRL_NONE ||
485             bs->state == ONEFS_CBRL_ASYNC);
486
487         /* A real cancel. */
488         DEBUG(10, ("Calling ifs_cbrl(CANCEL)...\n"));
489         error = ifs_cbrl(fd, CBRL_OP_CANCEL, CBRL_LK_UNSPEC, plock->start,
490             plock->size, false, bs->id, plock->context.smbpid,
491             plock->context.tid, plock->fnum);
492
493         END_PROFILE(syscall_brl_cancel);
494
495         if (error) {
496                 DEBUG(10, ("returning false\n"));
497                 bs->state = ONEFS_CBRL_ERROR;
498                 return false;
499         }
500
501         bs->state = ONEFS_CBRL_DONE;
502         onefs_cbrl_enumerate_blq("onefs_brl_cancel_windows");
503         DEBUG(10, ("returning true\n"));
504         return true;
505 }
506
507 bool onefs_strict_lock(vfs_handle_struct *handle,
508                         files_struct *fsp,
509                         struct lock_struct *plock)
510 {
511         int error;
512
513         START_PROFILE(syscall_strict_lock);
514
515         SMB_ASSERT(plock->lock_type == READ_LOCK ||
516             plock->lock_type == WRITE_LOCK);
517
518         if (!lp_locking(handle->conn->params) ||
519             !lp_strict_locking(handle->conn->params)) {
520                 END_PROFILE(syscall_strict_lock);
521                 return True;
522         }
523
524         if (plock->lock_flav == POSIX_LOCK) {
525                 END_PROFILE(syscall_strict_lock);
526                 return SMB_VFS_NEXT_STRICT_LOCK(handle, fsp, plock);
527         }
528
529         if (plock->size == 0) {
530                 END_PROFILE(syscall_strict_lock);
531                 return True;
532         }
533
534         error = ifs_cbrl(fsp->fh->fd, CBRL_OP_LOCK,
535             plock->lock_type == READ_LOCK ? CBRL_LK_RD : CBRL_LK_WR,
536             plock->start, plock->size, 0, 0, plock->context.smbpid,
537             plock->context.tid, plock->fnum);
538
539         END_PROFILE(syscall_strict_lock);
540
541         return (error == 0);
542 }
543
544 void onefs_strict_unlock(vfs_handle_struct *handle,
545                         files_struct *fsp,
546                         struct lock_struct *plock)
547 {
548         START_PROFILE(syscall_strict_unlock);
549
550         SMB_ASSERT(plock->lock_type == READ_LOCK ||
551             plock->lock_type == WRITE_LOCK);
552
553         if (!lp_locking(handle->conn->params) ||
554             !lp_strict_locking(handle->conn->params)) {
555                 END_PROFILE(syscall_strict_unlock);
556                 return;
557         }
558
559         if (plock->lock_flav == POSIX_LOCK) {
560                 SMB_VFS_NEXT_STRICT_UNLOCK(handle, fsp, plock);
561                 END_PROFILE(syscall_strict_unlock);
562                 return;
563         }
564
565         if (plock->size == 0) {
566                 END_PROFILE(syscall_strict_unlock);
567                 return;
568         }
569
570         if (fsp->fh) {
571                 ifs_cbrl(fsp->fh->fd, CBRL_OP_UNLOCK,
572                     plock->lock_type == READ_LOCK ? CBRL_LK_RD : CBRL_LK_WR,
573                     plock->start, plock->size, 0, 0, plock->context.smbpid,
574                     plock->context.tid, plock->fnum);
575         }
576
577         END_PROFILE(syscall_strict_unlock);
578 }
579
580 /* TODO Optimization: Abstract out brl_get_locks() in the Windows case.
581  * We'll malloc some memory or whatever (can't return NULL), but not actually
582  * touch the TDB. */
583
584 /* XXX brl_locktest: CBRL does not support calling this, but its only for
585  * strict locking. Add empty VOP? */
586
587 /* XXX brl_lockquery: CBRL does not support calling this for WINDOWS LOCKS, but
588  * its only called for POSIX LOCKS. Add empty VOP? */
589
590 /* XXX brl_close_fnum: CBRL will do this automatically. I think this is a NO-OP
591  * for us, we could add an empty VOP. */
592