~ubuntu-branches/ubuntu/trusty/glusterfs/trusty

« back to all changes in this revision

Viewing changes to xlators/features/locks/src/inodelk.c

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Matthäi
  • Date: 2010-02-09 18:53:10 UTC
  • mfrom: (1.2.4 upstream) (4.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20100209185310-ww8p82lsbosorg2u
* New upstream release.
* Uploading to unstable.
* Bump Standards-Version to 3.8.4 (no changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (c) 2006, 2007, 2008 Gluster, Inc. <http://www.gluster.com>
 
3
  This file is part of GlusterFS.
 
4
 
 
5
  GlusterFS is free software; you can redistribute it and/or modify
 
6
  it under the terms of the GNU General Public License as published
 
7
  by the Free Software Foundation; either version 3 of the License,
 
8
  or (at your option) any later version.
 
9
 
 
10
  GlusterFS is distributed in the hope that it will be useful, but
 
11
  WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
  General Public License for more details.
 
14
 
 
15
  You should have received a copy of the GNU General Public License
 
16
  along with this program.  If not, see
 
17
  <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
#ifndef _CONFIG_H
 
21
#define _CONFIG_H
 
22
#include "config.h"
 
23
#endif
 
24
 
 
25
#include "glusterfs.h"
 
26
#include "compat.h"
 
27
#include "xlator.h"
 
28
#include "inode.h"
 
29
#include "logging.h"
 
30
#include "common-utils.h"
 
31
#include "list.h"
 
32
 
 
33
#include "locks.h"
 
34
#include "common.h"
 
35
 
 
36
void
 
37
__delete_inode_lock (pl_inode_lock_t *lock)
 
38
{
 
39
        list_del (&lock->list);
 
40
}
 
41
 
 
42
void
 
43
__destroy_inode_lock (pl_inode_lock_t *lock)
 
44
{
 
45
        FREE (lock);
 
46
}
 
47
 
 
48
/* Check if 2 inodelks are conflicting on type. Only 2 shared locks don't conflict */
 
49
static int
 
50
inodelk_type_conflict (pl_inode_lock_t *l1, pl_inode_lock_t *l2)
 
51
{
 
52
        if (l2->fl_type == F_WRLCK || l1->fl_type == F_WRLCK)
 
53
                return 1;
 
54
 
 
55
        return 0;
 
56
}
 
57
 
 
58
void
 
59
pl_print_inodelk (char *str, int size, int cmd, struct flock *flock, const char *domain)
 
60
{
 
61
        char *cmd_str = NULL;
 
62
        char *type_str = NULL;
 
63
 
 
64
        switch (cmd) {
 
65
#if F_GETLK != F_GETLK64
 
66
        case F_GETLK64:
 
67
#endif
 
68
        case F_GETLK:
 
69
                cmd_str = "GETLK";
 
70
                break;
 
71
 
 
72
#if F_SETLK != F_SETLK64
 
73
        case F_SETLK64:
 
74
#endif
 
75
        case F_SETLK:
 
76
                cmd_str = "SETLK";
 
77
                break;
 
78
 
 
79
#if F_SETLKW != F_SETLKW64
 
80
        case F_SETLKW64:
 
81
#endif
 
82
        case F_SETLKW:
 
83
                cmd_str = "SETLKW";
 
84
                break;
 
85
 
 
86
        default:
 
87
                cmd_str = "UNKNOWN";
 
88
                break;
 
89
        }
 
90
 
 
91
        switch (flock->l_type) {
 
92
        case F_RDLCK:
 
93
                type_str = "READ";
 
94
                break;
 
95
        case F_WRLCK:
 
96
                type_str = "WRITE";
 
97
                break;
 
98
        case F_UNLCK:
 
99
                type_str = "UNLOCK";
 
100
                break;
 
101
        default:
 
102
                type_str = "UNKNOWN";
 
103
                break;
 
104
        }
 
105
 
 
106
        snprintf (str, size, "lock=INODELK, cmd=%s, type=%s, "
 
107
                  "domain: %s, start=%llu, len=%llu, pid=%llu",
 
108
                  cmd_str, type_str, domain,
 
109
                  (unsigned long long) flock->l_start,
 
110
                  (unsigned long long) flock->l_len,
 
111
                  (unsigned long long) flock->l_pid);
 
112
}
 
113
 
 
114
/* Determine if the two inodelks overlap reach other's lock regions */
 
115
static int
 
116
inodelk_overlap (pl_inode_lock_t *l1, pl_inode_lock_t *l2)
 
117
{
 
118
        return ((l1->fl_end >= l2->fl_start) &&
 
119
                (l2->fl_end >= l1->fl_start));
 
120
}
 
121
 
 
122
/* Returns true if the 2 inodelks have the same owner */
 
123
static int same_inodelk_owner (pl_inode_lock_t *l1, pl_inode_lock_t *l2)
 
124
{
 
125
        return ((l1->owner == l2->owner) &&
 
126
                (l1->transport  == l2->transport));
 
127
}
 
128
 
 
129
/* Returns true if the 2 inodelks conflict with each other */
 
130
static int
 
131
inodelk_conflict (pl_inode_lock_t *l1, pl_inode_lock_t *l2)
 
132
{
 
133
        if (same_inodelk_owner (l1, l2))
 
134
                return 0;
 
135
 
 
136
        if (!inodelk_overlap (l1, l2))
 
137
                return 0;
 
138
 
 
139
        return (inodelk_type_conflict(l1, l2));
 
140
}
 
141
 
 
142
/* Determine if lock is grantable or not */
 
143
static pl_inode_lock_t *
 
144
__inodelk_grantable (pl_dom_list_t *dom, pl_inode_lock_t *lock)
 
145
{
 
146
        pl_inode_lock_t *l = NULL;
 
147
        pl_inode_lock_t *ret = NULL;
 
148
        if (list_empty (&dom->inodelk_list))
 
149
                goto out;
 
150
        list_for_each_entry (l, &dom->inodelk_list, list){
 
151
                if (inodelk_conflict (lock, l)) {
 
152
                        ret = l;
 
153
                        goto out;
 
154
                }
 
155
        }
 
156
out:
 
157
        return ret;
 
158
}
 
159
 
 
160
static pl_inode_lock_t *
 
161
__blocked_lock_conflict (pl_dom_list_t *dom, pl_inode_lock_t *lock)
 
162
{
 
163
        pl_inode_lock_t *l   = NULL;
 
164
        pl_inode_lock_t *ret = NULL;
 
165
 
 
166
        if (list_empty (&dom->blocked_entrylks))
 
167
                return NULL;
 
168
 
 
169
        list_for_each_entry (l, &dom->blocked_inodelks, blocked_locks) {
 
170
                if (inodelk_conflict (lock, l)) {
 
171
                        ret = l;
 
172
                        goto out;
 
173
                }
 
174
        }
 
175
 
 
176
out:
 
177
        return ret;
 
178
}
 
179
 
 
180
static int
 
181
__owner_has_lock (pl_dom_list_t *dom, pl_inode_lock_t *newlock)
 
182
{
 
183
        pl_inode_lock_t *lock = NULL;
 
184
 
 
185
        list_for_each_entry (lock, &dom->entrylk_list, list) {
 
186
                if (same_inodelk_owner (lock, newlock))
 
187
                        return 1;
 
188
        }
 
189
 
 
190
        list_for_each_entry (lock, &dom->blocked_entrylks, blocked_locks) {
 
191
                if (same_inodelk_owner (lock, newlock))
 
192
                        return 1;
 
193
        }
 
194
 
 
195
        return 0;
 
196
}
 
197
 
 
198
 
 
199
/* Determines if lock can be granted and adds the lock. If the lock
 
200
 * is blocking, adds it to the blocked_inodelks list of the domain.
 
201
 */
 
202
static int
 
203
__lock_inodelk (xlator_t *this, pl_inode_t *pl_inode, pl_inode_lock_t *lock,
 
204
                int can_block,  pl_dom_list_t *dom)
 
205
{
 
206
        pl_inode_lock_t *conf = NULL;
 
207
        int ret = -EINVAL;
 
208
 
 
209
        conf = __inodelk_grantable (dom, lock);
 
210
        if (conf){
 
211
                ret = -EAGAIN;
 
212
                if (can_block == 0)
 
213
                        goto out;
 
214
 
 
215
                list_add_tail (&lock->blocked_locks, &dom->blocked_inodelks);
 
216
 
 
217
                gf_log (this->name, GF_LOG_TRACE,
 
218
                        "%s (pid=%d) lk-owner:%"PRIu64" %"PRId64" - %"PRId64" => Blocked",
 
219
                        lock->fl_type == F_UNLCK ? "Unlock" : "Lock",
 
220
                        lock->client_pid,
 
221
                        lock->owner,
 
222
                        lock->user_flock.l_start,
 
223
                        lock->user_flock.l_len);
 
224
 
 
225
 
 
226
                goto out;
 
227
        }
 
228
 
 
229
        if (__blocked_lock_conflict (dom, lock) && !(__owner_has_lock (dom, lock))) {
 
230
                ret = -EAGAIN;
 
231
                if (can_block == 0)
 
232
                        goto out;
 
233
 
 
234
                list_add_tail (&lock->blocked_locks, &dom->blocked_inodelks);
 
235
 
 
236
                gf_log (this->name, GF_LOG_TRACE,
 
237
                        "Lock is grantable, but blocking to prevent starvation");
 
238
                gf_log (this->name, GF_LOG_TRACE,
 
239
                        "%s (pid=%d) (lk-owner=%"PRIu64") %"PRId64" - %"PRId64" => Blocked",
 
240
                        lock->fl_type == F_UNLCK ? "Unlock" : "Lock",
 
241
                        lock->client_pid,
 
242
                        lock->owner,
 
243
                        lock->user_flock.l_start,
 
244
                        lock->user_flock.l_len);
 
245
 
 
246
 
 
247
                goto out;
 
248
        }
 
249
        list_add (&lock->list, &dom->inodelk_list);
 
250
 
 
251
        ret = 0;
 
252
 
 
253
out:
 
254
        return ret;
 
255
}
 
256
 
 
257
/* Return true if the two inodelks have exactly same lock boundaries */
 
258
static int
 
259
inodelks_equal (pl_inode_lock_t *l1, pl_inode_lock_t *l2)
 
260
{
 
261
        if ((l1->fl_start == l2->fl_start) &&
 
262
            (l1->fl_end == l2->fl_end))
 
263
                return 1;
 
264
 
 
265
        return 0;
 
266
}
 
267
 
 
268
 
 
269
static pl_inode_lock_t *
 
270
find_matching_inodelk (pl_inode_lock_t *lock, pl_dom_list_t *dom)
 
271
{
 
272
        pl_inode_lock_t *l = NULL;
 
273
        list_for_each_entry (l, &dom->inodelk_list, list) {
 
274
                if (inodelks_equal (l, lock))
 
275
                        return l;
 
276
        }
 
277
        return NULL;
 
278
}
 
279
 
 
280
/* Set F_UNLCK removes a lock which has the exact same lock boundaries
 
281
 * as the UNLCK lock specifies. If such a lock is not found, returns invalid
 
282
 */
 
283
static pl_inode_lock_t *
 
284
__inode_unlock_lock (xlator_t *this, pl_inode_lock_t *lock, pl_dom_list_t *dom)
 
285
{
 
286
 
 
287
        pl_inode_lock_t *conf = NULL;
 
288
 
 
289
        conf = find_matching_inodelk (lock, dom);
 
290
        if (!conf) {
 
291
                gf_log (this->name, GF_LOG_DEBUG,
 
292
                        " Matching lock not found for unlock");
 
293
                goto out;
 
294
        }
 
295
        __delete_inode_lock (conf);
 
296
        gf_log (this->name, GF_LOG_DEBUG,
 
297
                " Matching lock found for unlock");
 
298
        __destroy_inode_lock (lock);
 
299
 
 
300
 
 
301
out:
 
302
        return conf;
 
303
 
 
304
 
 
305
}
 
306
static void
 
307
__grant_blocked_inode_locks (xlator_t *this, pl_inode_t *pl_inode, 
 
308
                             struct list_head *granted, pl_dom_list_t *dom)
 
309
{
 
310
        int           bl_ret = 0;
 
311
        pl_inode_lock_t *bl = NULL;
 
312
        pl_inode_lock_t *tmp = NULL;
 
313
 
 
314
        struct list_head blocked_list;
 
315
 
 
316
        INIT_LIST_HEAD (&blocked_list);
 
317
        list_splice_init (&dom->blocked_inodelks, &blocked_list);
 
318
 
 
319
        list_for_each_entry_safe (bl, tmp, &blocked_list, blocked_locks) {
 
320
 
 
321
                list_del_init (&bl->blocked_locks);
 
322
 
 
323
                bl_ret = __lock_inodelk (this, pl_inode, bl, 1, dom);
 
324
 
 
325
                if (bl_ret == 0) {
 
326
                        list_add (&bl->blocked_locks, granted);
 
327
                }
 
328
        }
 
329
        return;
 
330
}
 
331
 
 
332
/* Grant all inodelks blocked on a lock */
 
333
void
 
334
grant_blocked_inode_locks (xlator_t *this, pl_inode_t *pl_inode, pl_dom_list_t *dom)
 
335
{
 
336
        struct list_head granted;
 
337
        pl_inode_lock_t *lock;
 
338
        pl_inode_lock_t *tmp;
 
339
 
 
340
        INIT_LIST_HEAD (&granted);
 
341
 
 
342
        if (list_empty (&dom->blocked_inodelks)) {
 
343
                gf_log (this->name, GF_LOG_TRACE,
 
344
                        "No blocked locks to be granted for domain: %s", dom->domain);
 
345
        }
 
346
 
 
347
        pthread_mutex_lock (&pl_inode->mutex);
 
348
        {
 
349
                __grant_blocked_inode_locks (this, pl_inode, &granted, dom);
 
350
        }
 
351
        pthread_mutex_unlock (&pl_inode->mutex);
 
352
 
 
353
        list_for_each_entry_safe (lock, tmp, &granted, blocked_locks) {
 
354
                gf_log (this->name, GF_LOG_TRACE,
 
355
                        "%s (pid=%d) (lk-owner=%"PRIu64") %"PRId64" - %"PRId64" => Granted",
 
356
                        lock->fl_type == F_UNLCK ? "Unlock" : "Lock",
 
357
                        lock->client_pid,
 
358
                        lock->owner,
 
359
                        lock->user_flock.l_start,
 
360
                        lock->user_flock.l_len);
 
361
 
 
362
                pl_trace_out (this, lock->frame, NULL, NULL, F_SETLKW,
 
363
                              &lock->user_flock, 0, 0, lock->volume);
 
364
 
 
365
                STACK_UNWIND_STRICT (inodelk, lock->frame, 0, 0);
 
366
        }
 
367
 
 
368
}
 
369
 
 
370
/* Release all inodelks from this transport */
 
371
static int
 
372
release_inode_locks_of_transport (xlator_t *this, pl_dom_list_t *dom,
 
373
                                  inode_t *inode, transport_t *trans)
 
374
{
 
375
        pl_inode_lock_t *tmp = NULL;
 
376
        pl_inode_lock_t *l = NULL;
 
377
 
 
378
        pl_inode_t * pinode = NULL;
 
379
 
 
380
        struct list_head granted;
 
381
        struct list_head released;
 
382
 
 
383
        char *path = NULL;
 
384
 
 
385
        INIT_LIST_HEAD (&granted);
 
386
        INIT_LIST_HEAD (&released);
 
387
 
 
388
        pinode = pl_inode_get (this, inode);
 
389
 
 
390
        pthread_mutex_lock (&pinode->mutex);
 
391
        {
 
392
 
 
393
                list_for_each_entry_safe (l, tmp, &dom->blocked_inodelks, blocked_locks) {
 
394
                        if (l->transport != trans)
 
395
                                continue;
 
396
 
 
397
                        list_del_init (&l->blocked_locks);
 
398
 
 
399
                        if (inode_path (inode, NULL, &path) < 0) {
 
400
                                gf_log (this->name, GF_LOG_TRACE,
 
401
                                        "inode_path failed");
 
402
                                goto unlock;
 
403
                        }
 
404
 
 
405
                        gf_log (this->name, GF_LOG_TRACE,
 
406
                                "releasing lock on %s held by "
 
407
                                "{transport=%p, pid=%"PRId64" lk-owner=%"PRIu64"}",
 
408
                                path, trans,
 
409
                                (uint64_t) l->client_pid,
 
410
                                l->owner);
 
411
 
 
412
                        list_add (&l->blocked_locks, &released);
 
413
 
 
414
                }
 
415
 
 
416
                list_for_each_entry_safe (l, tmp, &dom->inodelk_list, list) {
 
417
                        if (l->transport != trans)
 
418
                                continue;
 
419
 
 
420
                        __delete_inode_lock (l);
 
421
                        __destroy_inode_lock (l);
 
422
 
 
423
 
 
424
                        if (inode_path (inode, NULL, &path) < 0) {
 
425
                                gf_log (this->name, GF_LOG_TRACE,
 
426
                                        "inode_path failed");
 
427
                                goto unlock;
 
428
                        }
 
429
 
 
430
                        gf_log (this->name, GF_LOG_TRACE,
 
431
                                "releasing lock on %s held by "
 
432
                                "{transport=%p, pid=%"PRId64" lk-owner=%"PRIu64"}",
 
433
                                path, trans,
 
434
                                (uint64_t) l->client_pid,
 
435
                                l->owner);
 
436
 
 
437
 
 
438
                }
 
439
        }
 
440
unlock:
 
441
        if (path)
 
442
                FREE (path);
 
443
 
 
444
        pthread_mutex_unlock (&pinode->mutex);
 
445
 
 
446
        list_for_each_entry_safe (l, tmp, &released, blocked_locks) {
 
447
                list_del_init (&l->blocked_locks);
 
448
 
 
449
                STACK_UNWIND_STRICT (inodelk, l->frame, -1, EAGAIN);
 
450
                FREE (l);
 
451
        }
 
452
 
 
453
        grant_blocked_inode_locks (this, pinode, dom);
 
454
        return 0;
 
455
}
 
456
 
 
457
 
 
458
static int
 
459
pl_inode_setlk (xlator_t *this, pl_inode_t *pl_inode, pl_inode_lock_t *lock,
 
460
                int can_block,  pl_dom_list_t *dom)
 
461
{
 
462
        int ret = -EINVAL;
 
463
        pl_inode_lock_t *retlock = NULL;
 
464
 
 
465
        pthread_mutex_lock (&pl_inode->mutex);
 
466
        {
 
467
                if (lock->fl_type != F_UNLCK) {
 
468
                        ret = __lock_inodelk (this, pl_inode, lock, can_block, dom);
 
469
                        if (ret == 0)
 
470
                                gf_log (this->name, GF_LOG_TRACE,
 
471
                                        "%s (pid=%d) (lk-owner=%"PRIu64") %"PRId64" - %"PRId64" => OK",
 
472
                                        lock->fl_type == F_UNLCK ? "Unlock" : "Lock",
 
473
                                        lock->client_pid,
 
474
                                        lock->owner,
 
475
                                        lock->fl_start,
 
476
                                        lock->fl_end);
 
477
 
 
478
                        if (ret == -EAGAIN)
 
479
                                gf_log (this->name, GF_LOG_TRACE,
 
480
                                        "%s (pid=%d) (lk-owner=%"PRIu64") %"PRId64" - %"PRId64" => NOK",
 
481
                                        lock->fl_type == F_UNLCK ? "Unlock" : "Lock",
 
482
                                        lock->client_pid,
 
483
                                        lock->owner,
 
484
                                        lock->user_flock.l_start,
 
485
                                        lock->user_flock.l_len);
 
486
 
 
487
                        goto out;
 
488
                }
 
489
 
 
490
 
 
491
                retlock = __inode_unlock_lock (this, lock, dom);
 
492
                if (!retlock) {
 
493
                        gf_log (this->name, GF_LOG_DEBUG,
 
494
                                "Bad Unlock issued on Inode lock");
 
495
                        ret = -EINVAL;
 
496
                        goto out;
 
497
                }
 
498
                __destroy_inode_lock (retlock);
 
499
 
 
500
                ret = 0;
 
501
 
 
502
 
 
503
        }
 
504
out:
 
505
        pthread_mutex_unlock (&pl_inode->mutex);
 
506
        grant_blocked_inode_locks (this, pl_inode, dom);
 
507
        return ret;
 
508
}
 
509
 
 
510
/* Create a new inode_lock_t */
 
511
pl_inode_lock_t *
 
512
new_inode_lock (struct flock *flock, transport_t *transport, pid_t client_pid,
 
513
                uint64_t owner, const char *volume)
 
514
 
 
515
{
 
516
        pl_inode_lock_t *lock = NULL;
 
517
 
 
518
        lock = CALLOC (1, sizeof (*lock));
 
519
        if (!lock) {
 
520
                return NULL;
 
521
        }
 
522
 
 
523
        lock->fl_start = flock->l_start;
 
524
        lock->fl_type  = flock->l_type;
 
525
 
 
526
        if (flock->l_len == 0)
 
527
                lock->fl_end = LLONG_MAX;
 
528
        else
 
529
                lock->fl_end = flock->l_start + flock->l_len - 1;
 
530
 
 
531
        lock->transport  = transport;
 
532
        lock->client_pid = client_pid;
 
533
        lock->owner      = owner;
 
534
        lock->volume     = volume;
 
535
 
 
536
        INIT_LIST_HEAD (&lock->list);
 
537
        INIT_LIST_HEAD (&lock->blocked_locks);
 
538
 
 
539
        return lock;
 
540
}
 
541
 
 
542
/* Common inodelk code called from pl_inodelk and pl_finodelk */
 
543
int
 
544
pl_common_inodelk (call_frame_t *frame, xlator_t *this,
 
545
                   const char *volume, inode_t *inode, int32_t cmd,
 
546
                   struct flock *flock, loc_t *loc, fd_t *fd)
 
547
{
 
548
        int32_t op_ret   = -1;
 
549
        int32_t op_errno = 0;
 
550
        int     ret      = -1;
 
551
        int     can_block = 0;
 
552
        transport_t *           transport  = NULL;
 
553
        pid_t                   client_pid = -1;
 
554
        uint64_t                owner      = -1;
 
555
        pl_inode_t *            pinode     = NULL;
 
556
        pl_inode_lock_t *       reqlock    = NULL;
 
557
        pl_dom_list_t *         dom        = NULL;
 
558
 
 
559
        VALIDATE_OR_GOTO (frame, out);
 
560
        VALIDATE_OR_GOTO (inode, unwind);
 
561
        VALIDATE_OR_GOTO (flock, unwind);
 
562
 
 
563
        if ((flock->l_start < 0) || (flock->l_len < 0)) {
 
564
                op_errno = EINVAL;
 
565
                goto unwind;
 
566
        }
 
567
 
 
568
        pl_trace_in (this, frame, fd, loc, cmd, flock, volume);
 
569
 
 
570
        transport  = frame->root->trans;
 
571
        client_pid = frame->root->pid;
 
572
        owner      = (uint64_t) (long)frame->root;
 
573
 
 
574
        pinode = pl_inode_get (this, inode);
 
575
        if (!pinode) {
 
576
                gf_log (this->name, GF_LOG_ERROR,
 
577
                        "Out of memory.");
 
578
                op_errno = ENOMEM;
 
579
                goto unwind;
 
580
        }
 
581
 
 
582
        dom = get_domain (pinode, volume);
 
583
 
 
584
        if (client_pid == 0) {
 
585
                /*
 
586
                  special case: this means release all locks
 
587
                  from this transport
 
588
                */
 
589
                gf_log (this->name, GF_LOG_TRACE,
 
590
                        "Releasing all locks from transport %p", transport);
 
591
 
 
592
                release_inode_locks_of_transport (this, dom, inode, transport);
 
593
                goto unwind;
 
594
        }
 
595
 
 
596
        reqlock = new_inode_lock (flock, transport, client_pid, owner, volume);
 
597
 
 
598
        if (!reqlock) {
 
599
                gf_log (this->name, GF_LOG_ERROR,
 
600
                        "Out of memory.");
 
601
                op_ret = -1;
 
602
                op_errno = ENOMEM;
 
603
                goto unwind;
 
604
        }
 
605
 
 
606
        switch (cmd) {
 
607
        case F_SETLKW:
 
608
                can_block = 1;
 
609
                reqlock->frame = frame;
 
610
                reqlock->this  = this;
 
611
 
 
612
                /* fall through */
 
613
 
 
614
        case F_SETLK:
 
615
                memcpy (&reqlock->user_flock, flock, sizeof (struct flock));
 
616
                ret = pl_inode_setlk (this, pinode, reqlock,
 
617
                                      can_block, dom);
 
618
 
 
619
                if (ret < 0) {
 
620
                        if (can_block) {
 
621
                                pl_trace_block (this, frame, fd, loc,
 
622
                                                cmd, flock, volume);
 
623
                                goto out;
 
624
                        }
 
625
                        gf_log (this->name, GF_LOG_TRACE, "returning EAGAIN");
 
626
                        op_errno = -ret;
 
627
                        __destroy_inode_lock (reqlock);
 
628
                        goto unwind;
 
629
                }
 
630
                break;
 
631
 
 
632
        default:
 
633
                op_errno = ENOTSUP;
 
634
                gf_log (this->name, GF_LOG_DEBUG,
 
635
                        "Lock command F_GETLK not supported for [f]inodelk "
 
636
                        "(cmd=%d)",
 
637
                        cmd);
 
638
                goto unwind;
 
639
        }
 
640
 
 
641
        op_ret = 0;
 
642
 
 
643
unwind:
 
644
        if ((inode != NULL) && (flock !=NULL)) {
 
645
                pl_update_refkeeper (this, inode);
 
646
                pl_trace_out (this, frame, fd, loc, cmd, flock, op_ret, op_errno, volume);
 
647
        }
 
648
 
 
649
        STACK_UNWIND_STRICT (inodelk, frame, op_ret, op_errno);
 
650
out:
 
651
        return 0;
 
652
}
 
653
 
 
654
int
 
655
pl_inodelk (call_frame_t *frame, xlator_t *this,
 
656
            const char *volume, loc_t *loc, int32_t cmd, struct flock *flock)
 
657
{
 
658
 
 
659
        pl_common_inodelk (frame, this, volume, loc->inode, cmd, flock, loc, NULL);
 
660
 
 
661
        return 0;
 
662
}
 
663
 
 
664
int
 
665
pl_finodelk (call_frame_t *frame, xlator_t *this,
 
666
             const char *volume, fd_t *fd, int32_t cmd, struct flock *flock)
 
667
{
 
668
 
 
669
        pl_common_inodelk (frame, this, volume, fd->inode, cmd, flock, NULL, fd);
 
670
 
 
671
        return 0;
 
672
 
 
673
}
 
674
 
 
675
 
 
676
static int32_t
 
677
__get_inodelk_count (xlator_t *this, pl_inode_t *pl_inode)
 
678
{
 
679
        int32_t            count  = 0;
 
680
        pl_inode_lock_t   *lock   = NULL;
 
681
        pl_dom_list_t     *dom    = NULL;
 
682
 
 
683
        list_for_each_entry (dom, &pl_inode->dom_list, inode_list) {
 
684
                list_for_each_entry (lock, &dom->inodelk_list, list) {
 
685
 
 
686
                        gf_log (this->name, GF_LOG_DEBUG,
 
687
                                " XATTR DEBUG"
 
688
                                " domain: %s %s (pid=%d) (lk-owner=%"PRIu64") %"PRId64" - %"PRId64" "
 
689
                                "state = Active",
 
690
                                dom->domain,
 
691
                                lock->fl_type == F_UNLCK ? "Unlock" : "Lock",
 
692
                                lock->client_pid,
 
693
                                lock->owner,
 
694
                                lock->user_flock.l_start,
 
695
                                lock->user_flock.l_len);
 
696
 
 
697
                        count++;
 
698
                }
 
699
 
 
700
                list_for_each_entry (lock, &dom->blocked_inodelks, blocked_locks) {
 
701
 
 
702
                        gf_log (this->name, GF_LOG_DEBUG,
 
703
                                " XATTR DEBUG"
 
704
                                " domain: %s %s (pid=%d) (lk-owner=%"PRIu64") %"PRId64" - %"PRId64" "
 
705
                                "state = Blocked",
 
706
                                dom->domain,
 
707
                                lock->fl_type == F_UNLCK ? "Unlock" : "Lock",
 
708
                                lock->client_pid,
 
709
                                lock->owner,
 
710
                                lock->user_flock.l_start,
 
711
                                lock->user_flock.l_len);
 
712
 
 
713
                        count++;
 
714
                }
 
715
 
 
716
        }
 
717
 
 
718
        return count;
 
719
}
 
720
 
 
721
int32_t
 
722
get_inodelk_count (xlator_t *this, inode_t *inode)
 
723
{
 
724
        pl_inode_t   *pl_inode = NULL;
 
725
        uint64_t      tmp_pl_inode = 0;
 
726
        int           ret      = 0;
 
727
        int32_t       count    = 0;
 
728
 
 
729
        ret = inode_ctx_get (inode, this, &tmp_pl_inode);
 
730
        if (ret != 0) {
 
731
                goto out;
 
732
        }
 
733
 
 
734
        pl_inode = (pl_inode_t *)(long) tmp_pl_inode;
 
735
 
 
736
        pthread_mutex_lock (&pl_inode->mutex);
 
737
        {
 
738
                count = __get_inodelk_count (this, pl_inode);
 
739
        }
 
740
        pthread_mutex_unlock (&pl_inode->mutex);
 
741
 
 
742
out:
 
743
        return count;
 
744
}