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