summaryrefslogtreecommitdiffstats
path: root/xlators/features/locks/src/reservelk.c
blob: 11abd26d85f15df7470a1f814a2633e168a7724c (plain)
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
   Copyright (c) 2006-2012 Red Hat, Inc. <http://www.redhat.com>
   This file is part of GlusterFS.

   This file is licensed to you under your choice of the GNU Lesser
   General Public License, version 3 or any later version (LGPLv3 or
   later), or the GNU General Public License, version 2 (GPLv2), in all
   cases as published by the Free Software Foundation.
*/
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include "glusterfs.h"
#include "compat.h"
#include "xlator.h"
#include "inode.h"
#include "logging.h"
#include "common-utils.h"
#include "list.h"

#include "locks.h"
#include "common.h"

void
__delete_reserve_lock (posix_lock_t *lock)
{
        list_del (&lock->list);
}

void
__destroy_reserve_lock (posix_lock_t *lock)
{
        GF_FREE (lock);
}

/* Return true if the two reservelks have exactly same lock boundaries */
int
reservelks_equal (posix_lock_t *l1, posix_lock_t *l2)
{
        if ((l1->fl_start == l2->fl_start) &&
            (l1->fl_end == l2->fl_end))
                return 1;

        return 0;
}

/* Determine if lock is grantable or not */
static posix_lock_t *
__reservelk_grantable (pl_inode_t *pl_inode, posix_lock_t *lock)
{
        xlator_t     *this     = NULL;
        posix_lock_t *l        = NULL;
        posix_lock_t *ret_lock = NULL;

        this = THIS;

        if (list_empty (&pl_inode->reservelk_list)) {
                gf_log (this->name, GF_LOG_TRACE,
                        "No reservelks in list");
                goto out;
        }
        list_for_each_entry (l, &pl_inode->reservelk_list, list){
                if (reservelks_equal (lock, l)) {
                        ret_lock = l;
                        break;
                }
        }
out:
        return ret_lock;
}

static inline int
__same_owner_reservelk (posix_lock_t *l1, posix_lock_t *l2)
{
        return (is_same_lkowner (&l1->owner, &l2->owner));

}

static posix_lock_t *
__matching_reservelk (pl_inode_t *pl_inode, posix_lock_t *lock)
{
        posix_lock_t *l = NULL;

        if (list_empty (&pl_inode->reservelk_list)) {
            gf_log ("posix-locks", GF_LOG_TRACE,
                    "reservelk list empty");
            return NULL;
        }

        list_for_each_entry (l, &pl_inode->reservelk_list, list) {
                if (reservelks_equal (l, lock)) {
                        gf_log ("posix-locks", GF_LOG_TRACE,
                                "equal reservelk found");
                        break;
                }
        }

        return l;
}

static int
__reservelk_conflict (xlator_t *this, pl_inode_t *pl_inode,
                      posix_lock_t *lock)
{
        posix_lock_t *conf = NULL;
        int ret = 0;

        conf = __matching_reservelk (pl_inode, lock);
        if (conf) {
                gf_log (this->name, GF_LOG_TRACE,
                        "Matching reservelk found");
                if (__same_owner_reservelk (lock, conf)) {
                        list_del_init (&conf->list);
                        gf_log (this->name, GF_LOG_TRACE,
                                "Removing the matching reservelk for setlk to progress");
                        GF_FREE (conf);
                        ret = 0;
                } else {
                        gf_log (this->name, GF_LOG_TRACE,
                                "Conflicting reservelk found");
                        ret = 1;
                }

        }
        return ret;

}

int
pl_verify_reservelk (xlator_t *this, pl_inode_t *pl_inode,
                     posix_lock_t *lock, int can_block)
{
        int ret = 0;

        pthread_mutex_lock (&pl_inode->mutex);
        {
                if (__reservelk_conflict (this, pl_inode, lock)) {
                        gf_log (this->name, GF_LOG_TRACE,
                                "Found conflicting reservelk. Blocking until reservelk is unlocked.");
                        lock->blocked = can_block;
                        list_add_tail (&lock->list, &pl_inode->blocked_calls);
                        ret = -1;
                        goto unlock;
                }

                gf_log (this->name, GF_LOG_TRACE,
                        "no conflicting reservelk found. Call continuing");
                ret = 0;

        }
unlock:
        pthread_mutex_unlock (&pl_inode->mutex);

        return ret;

}


/* Determines if lock can be granted and adds the lock. If the lock
 * is blocking, adds it to the blocked_reservelks.
 */
static int
__lock_reservelk (xlator_t *this, pl_inode_t *pl_inode, posix_lock_t *lock,
                  int can_block)
{
        posix_lock_t *conf = NULL;
        int ret = -EINVAL;

        conf = __reservelk_grantable (pl_inode, lock);
        if (conf){
                ret = -EAGAIN;
                if (can_block == 0)
                        goto out;

                list_add_tail (&lock->list, &pl_inode->blocked_reservelks);

                gf_log (this->name, GF_LOG_TRACE,
                        "%s (pid=%d) lk-owner:%s %"PRId64" - %"PRId64" => Blocked",
                        lock->fl_type == F_UNLCK ? "Unlock" : "Lock",
                        lock->client_pid,
                        lkowner_utoa (&lock->owner),
                        lock->user_flock.l_start,
                        lock->user_flock.l_len);


                goto out;
        }

        list_add (&lock->list, &pl_inode->reservelk_list);

        ret = 0;

out:
        return ret;
}

static posix_lock_t *
find_matching_reservelk (posix_lock_t *lock, pl_inode_t *pl_inode)
{
        posix_lock_t *l = NULL;
        list_for_each_entry (l, &pl_inode->reservelk_list, list) {
                if (reservelks_equal (l, lock))
                        return l;
        }
        return NULL;
}

/* Set F_UNLCK removes a lock which has the exact same lock boundaries
 * as the UNLCK lock specifies. If such a lock is not found, returns invalid
 */
static posix_lock_t *
__reserve_unlock_lock (xlator_t *this, posix_lock_t *lock, pl_inode_t *pl_inode)
{

        posix_lock_t *conf = NULL;

        conf = find_matching_reservelk (lock, pl_inode);
        if (!conf) {
                gf_log (this->name, GF_LOG_DEBUG,
                        " Matching lock not found for unlock");
                goto out;
        }
        __delete_reserve_lock (conf);
        gf_log (this->name, GF_LOG_DEBUG,
                " Matching lock found for unlock");

out:
        return conf;


}

static void
__grant_blocked_reserve_locks (xlator_t *this, pl_inode_t *pl_inode,
                               struct list_head *granted)
{
        int              bl_ret = 0;
        posix_lock_t *bl = NULL;
        posix_lock_t *tmp = NULL;

        struct list_head blocked_list;

        INIT_LIST_HEAD (&blocked_list);
        list_splice_init (&pl_inode->blocked_reservelks, &blocked_list);

        list_for_each_entry_safe (bl, tmp, &blocked_list, list) {

                list_del_init (&bl->list);

                bl_ret = __lock_reservelk (this, pl_inode, bl, 1);

                if (bl_ret == 0) {
                        list_add (&bl->list, granted);
                }
        }
        return;
}

/* Grant all reservelks blocked on lock(s) */
void
grant_blocked_reserve_locks (xlator_t *this, pl_inode_t *pl_inode)
{
        struct list_head granted;
        posix_lock_t *lock = NULL;
        posix_lock_t *tmp = NULL;

        INIT_LIST_HEAD (&granted);

        if (list_empty (&pl_inode->blocked_reservelks)) {
                gf_log (this->name, GF_LOG_TRACE,
                        "No blocked locks to be granted");
                return;
        }

        pthread_mutex_lock (&pl_inode->mutex);
        {
                __grant_blocked_reserve_locks (this, pl_inode, &granted);
        }
        pthread_mutex_unlock (&pl_inode->mutex);

        list_for_each_entry_safe (lock, tmp, &granted, list) {
                gf_log (this->name, GF_LOG_TRACE,
                        "%s (pid=%d) (lk-owner=%s) %"PRId64" - %"PRId64" => Granted",
                        lock->fl_type == F_UNLCK ? "Unlock" : "Lock",
                        lock->client_pid,
                        lkowner_utoa (&lock->owner),
                        lock->user_flock.l_start,
                        lock->user_flock.l_len);

                STACK_UNWIND_STRICT (lk, lock->frame, 0, 0, &lock->user_flock,
                                     NULL);
        }

}

static void
__grant_blocked_lock_calls (xlator_t *this, pl_inode_t *pl_inode,
                            struct list_head *granted)
{
        int              bl_ret = 0;
        posix_lock_t *bl = NULL;
        posix_lock_t *tmp = NULL;

        struct list_head blocked_list;

        INIT_LIST_HEAD (&blocked_list);
        list_splice_init (&pl_inode->blocked_reservelks, &blocked_list);

        list_for_each_entry_safe (bl, tmp, &blocked_list, list) {

                list_del_init (&bl->list);

                bl_ret = pl_verify_reservelk (this, pl_inode, bl, bl->blocked);

                if (bl_ret == 0) {
                        list_add_tail (&bl->list, granted);
                }
        }
        return;
}

void
grant_blocked_lock_calls (xlator_t *this, pl_inode_t *pl_inode)
{
        struct list_head granted;
        posix_lock_t *lock = NULL;
        posix_lock_t *tmp = NULL;
        fd_t         *fd  = NULL;

        int can_block = 0;
        int32_t cmd = 0;
        int ret = 0;

        if (list_empty (&pl_inode->blocked_calls)) {
                gf_log (this->name, GF_LOG_TRACE,
                        "No blocked lock calls to be granted");
                return;
        }

        pthread_mutex_lock (&pl_inode->mutex);
        {
                __grant_blocked_lock_calls (this, pl_inode, &granted);
        }
        pthread_mutex_unlock (&pl_inode->mutex);

        list_for_each_entry_safe (lock, tmp, &granted, list) {
                fd = fd_from_fdnum (lock);

                if (lock->blocked) {
                        can_block = 1;
                        cmd = F_SETLKW;
                }
                else
                        cmd = F_SETLK;

                lock->blocked = 0;
                ret = pl_setlk (this, pl_inode, lock, can_block);
                if (ret == -1) {
                        if (can_block) {
                                pl_trace_block (this, lock->frame, fd, NULL,
                                                cmd, &lock->user_flock, NULL);
                                continue;
                        } else {
                                gf_log (this->name, GF_LOG_DEBUG, "returning EAGAIN");
                                pl_trace_out (this, lock->frame, fd, NULL, cmd,
                                              &lock->user_flock, -1, EAGAIN, NULL);
                                pl_update_refkeeper (this, fd->inode);
                                STACK_UNWIND_STRICT (lk, lock->frame, -1,
                                                     EAGAIN, &lock->user_flock,
                                                     NULL);
                                __destroy_lock (lock);
                        }
                }

        }

}


int
pl_reserve_unlock (xlator_t *this, pl_inode_t *pl_inode, posix_lock_t *lock)
{
        posix_lock_t *retlock = NULL;
        int ret = -1;

        pthread_mutex_lock (&pl_inode->mutex);
        {
                retlock = __reserve_unlock_lock (this, lock, pl_inode);
                if (!retlock) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "Bad Unlock issued on Inode lock");
                        ret = -EINVAL;
                        goto out;
                }

                gf_log (this->name, GF_LOG_TRACE,
                        "Reservelk Unlock successful");
                __destroy_reserve_lock (retlock);
                ret = 0;
        }
out:
        pthread_mutex_unlock (&pl_inode->mutex);

        grant_blocked_reserve_locks (this, pl_inode);
        grant_blocked_lock_calls (this, pl_inode);

        return ret;

}

int
pl_reserve_setlk (xlator_t *this, pl_inode_t *pl_inode, posix_lock_t *lock,
                  int can_block)
{
        int ret = -EINVAL;

        pthread_mutex_lock (&pl_inode->mutex);
        {

                        ret = __lock_reservelk (this, pl_inode, lock, can_block);
                        if (ret < 0)
                                gf_log (this->name, GF_LOG_TRACE,
                                        "%s (pid=%d) (lk-owner=%s) %"PRId64" - %"PRId64" => NOK",
                                        lock->fl_type == F_UNLCK ? "Unlock" : "Lock",
                                        lock->client_pid,
                                        lkowner_utoa (&lock->owner),
                                        lock->user_flock.l_start,
                                        lock->user_flock.l_len);
                        else
                                gf_log (this->name, GF_LOG_TRACE,
                                        "%s (pid=%d) (lk-owner=%s) %"PRId64" - %"PRId64" => OK",
                                        lock->fl_type == F_UNLCK ? "Unlock" : "Lock",
                                        lock->client_pid,
                                        lkowner_utoa (&lock->owner),
                                        lock->fl_start,
                                        lock->fl_end);

        }
        pthread_mutex_unlock (&pl_inode->mutex);
        return ret;
}