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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
|
/*
Copyright (c) 2006-2011 Gluster, Inc. <http://www.gluster.com>
This file is part of GlusterFS.
GlusterFS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
GlusterFS is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#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_inode_lock (pl_inode_lock_t *lock)
{
list_del (&lock->list);
}
void
__destroy_inode_lock (pl_inode_lock_t *lock)
{
GF_FREE (lock);
}
/* Check if 2 inodelks are conflicting on type. Only 2 shared locks don't conflict */
static int
inodelk_type_conflict (pl_inode_lock_t *l1, pl_inode_lock_t *l2)
{
if (l2->fl_type == F_WRLCK || l1->fl_type == F_WRLCK)
return 1;
return 0;
}
void
pl_print_inodelk (char *str, int size, int cmd, struct gf_flock *flock, const char *domain)
{
char *cmd_str = NULL;
char *type_str = NULL;
switch (cmd) {
#if F_GETLK != F_GETLK64
case F_GETLK64:
#endif
case F_GETLK:
cmd_str = "GETLK";
break;
#if F_SETLK != F_SETLK64
case F_SETLK64:
#endif
case F_SETLK:
cmd_str = "SETLK";
break;
#if F_SETLKW != F_SETLKW64
case F_SETLKW64:
#endif
case F_SETLKW:
cmd_str = "SETLKW";
break;
default:
cmd_str = "UNKNOWN";
break;
}
switch (flock->l_type) {
case F_RDLCK:
type_str = "READ";
break;
case F_WRLCK:
type_str = "WRITE";
break;
case F_UNLCK:
type_str = "UNLOCK";
break;
default:
type_str = "UNKNOWN";
break;
}
snprintf (str, size, "lock=INODELK, cmd=%s, type=%s, "
"domain: %s, start=%llu, len=%llu, pid=%llu",
cmd_str, type_str, domain,
(unsigned long long) flock->l_start,
(unsigned long long) flock->l_len,
(unsigned long long) flock->l_pid);
}
/* Determine if the two inodelks overlap reach other's lock regions */
static int
inodelk_overlap (pl_inode_lock_t *l1, pl_inode_lock_t *l2)
{
return ((l1->fl_end >= l2->fl_start) &&
(l2->fl_end >= l1->fl_start));
}
/* Returns true if the 2 inodelks have the same owner */
static inline int
same_inodelk_owner (pl_inode_lock_t *l1, pl_inode_lock_t *l2)
{
return (is_same_lkowner (&l1->owner, &l2->owner) &&
(l1->transport == l2->transport));
}
/* Returns true if the 2 inodelks conflict with each other */
static int
inodelk_conflict (pl_inode_lock_t *l1, pl_inode_lock_t *l2)
{
return (inodelk_overlap (l1, l2) &&
inodelk_type_conflict (l1, l2));
}
/* Determine if lock is grantable or not */
static pl_inode_lock_t *
__inodelk_grantable (pl_dom_list_t *dom, pl_inode_lock_t *lock)
{
pl_inode_lock_t *l = NULL;
pl_inode_lock_t *ret = NULL;
if (list_empty (&dom->inodelk_list))
goto out;
list_for_each_entry (l, &dom->inodelk_list, list){
if (inodelk_conflict (lock, l) &&
!same_inodelk_owner (lock, l)) {
ret = l;
goto out;
}
}
out:
return ret;
}
static pl_inode_lock_t *
__blocked_lock_conflict (pl_dom_list_t *dom, pl_inode_lock_t *lock)
{
pl_inode_lock_t *l = NULL;
pl_inode_lock_t *ret = NULL;
if (list_empty (&dom->blocked_inodelks))
return NULL;
list_for_each_entry (l, &dom->blocked_inodelks, blocked_locks) {
if (inodelk_conflict (lock, l)) {
ret = l;
goto out;
}
}
out:
return ret;
}
static int
__owner_has_lock (pl_dom_list_t *dom, pl_inode_lock_t *newlock)
{
pl_inode_lock_t *lock = NULL;
list_for_each_entry (lock, &dom->inodelk_list, list) {
if (same_inodelk_owner (lock, newlock))
return 1;
}
list_for_each_entry (lock, &dom->blocked_inodelks, blocked_locks) {
if (same_inodelk_owner (lock, newlock))
return 1;
}
return 0;
}
/* Determines if lock can be granted and adds the lock. If the lock
* is blocking, adds it to the blocked_inodelks list of the domain.
*/
static int
__lock_inodelk (xlator_t *this, pl_inode_t *pl_inode, pl_inode_lock_t *lock,
int can_block, pl_dom_list_t *dom)
{
pl_inode_lock_t *conf = NULL;
int ret = -EINVAL;
conf = __inodelk_grantable (dom, lock);
if (conf){
ret = -EAGAIN;
if (can_block == 0)
goto out;
gettimeofday (&lock->blkd_time, NULL);
list_add_tail (&lock->blocked_locks, &dom->blocked_inodelks);
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;
}
if (__blocked_lock_conflict (dom, lock) && !(__owner_has_lock (dom, lock))) {
ret = -EAGAIN;
if (can_block == 0)
goto out;
gettimeofday (&lock->blkd_time, NULL);
list_add_tail (&lock->blocked_locks, &dom->blocked_inodelks);
gf_log (this->name, GF_LOG_TRACE,
"Lock is grantable, but blocking to prevent starvation");
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;
}
gettimeofday (&lock->granted_time, NULL);
list_add (&lock->list, &dom->inodelk_list);
ret = 0;
out:
return ret;
}
/* Return true if the two inodelks have exactly same lock boundaries */
static int
inodelks_equal (pl_inode_lock_t *l1, pl_inode_lock_t *l2)
{
if ((l1->fl_start == l2->fl_start) &&
(l1->fl_end == l2->fl_end))
return 1;
return 0;
}
static pl_inode_lock_t *
find_matching_inodelk (pl_inode_lock_t *lock, pl_dom_list_t *dom)
{
pl_inode_lock_t *l = NULL;
list_for_each_entry (l, &dom->inodelk_list, list) {
if (inodelks_equal (l, lock) &&
same_inodelk_owner (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 pl_inode_lock_t *
__inode_unlock_lock (xlator_t *this, pl_inode_lock_t *lock, pl_dom_list_t *dom)
{
pl_inode_lock_t *conf = NULL;
conf = find_matching_inodelk (lock, dom);
if (!conf) {
gf_log (this->name, GF_LOG_DEBUG,
" Matching lock not found for unlock");
goto out;
}
__delete_inode_lock (conf);
gf_log (this->name, GF_LOG_DEBUG,
" Matching lock found for unlock");
__destroy_inode_lock (lock);
out:
return conf;
}
static void
__grant_blocked_inode_locks (xlator_t *this, pl_inode_t *pl_inode,
struct list_head *granted, pl_dom_list_t *dom)
{
int bl_ret = 0;
pl_inode_lock_t *bl = NULL;
pl_inode_lock_t *tmp = NULL;
struct list_head blocked_list;
INIT_LIST_HEAD (&blocked_list);
list_splice_init (&dom->blocked_inodelks, &blocked_list);
list_for_each_entry_safe (bl, tmp, &blocked_list, blocked_locks) {
list_del_init (&bl->blocked_locks);
bl_ret = __lock_inodelk (this, pl_inode, bl, 1, dom);
if (bl_ret == 0) {
list_add (&bl->blocked_locks, granted);
}
}
return;
}
/* Grant all inodelks blocked on a lock */
void
grant_blocked_inode_locks (xlator_t *this, pl_inode_t *pl_inode, pl_dom_list_t *dom)
{
struct list_head granted;
pl_inode_lock_t *lock;
pl_inode_lock_t *tmp;
INIT_LIST_HEAD (&granted);
if (list_empty (&dom->blocked_inodelks)) {
gf_log (this->name, GF_LOG_TRACE,
"No blocked locks to be granted for domain: %s", dom->domain);
}
pthread_mutex_lock (&pl_inode->mutex);
{
__grant_blocked_inode_locks (this, pl_inode, &granted, dom);
}
pthread_mutex_unlock (&pl_inode->mutex);
list_for_each_entry_safe (lock, tmp, &granted, blocked_locks) {
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);
pl_trace_out (this, lock->frame, NULL, NULL, F_SETLKW,
&lock->user_flock, 0, 0, lock->volume);
STACK_UNWIND_STRICT (inodelk, lock->frame, 0, 0);
}
}
/* Release all inodelks from this transport */
static int
release_inode_locks_of_transport (xlator_t *this, pl_dom_list_t *dom,
inode_t *inode, void *trans)
{
pl_inode_lock_t *tmp = NULL;
pl_inode_lock_t *l = NULL;
pl_inode_t * pinode = NULL;
struct list_head granted;
struct list_head released;
char *path = NULL;
char *file = NULL;
INIT_LIST_HEAD (&granted);
INIT_LIST_HEAD (&released);
pinode = pl_inode_get (this, inode);
pthread_mutex_lock (&pinode->mutex);
{
list_for_each_entry_safe (l, tmp, &dom->blocked_inodelks, blocked_locks) {
if (l->transport != trans)
continue;
list_del_init (&l->blocked_locks);
inode_path (inode, NULL, &path);
if (path)
file = path;
else
file = uuid_utoa (inode->gfid);
gf_log (this->name, GF_LOG_DEBUG,
"releasing blocking lock on %s held by "
"{transport=%p, pid=%"PRId64" lk-owner=%s}",
file, trans, (uint64_t) l->client_pid,
lkowner_utoa (&l->owner));
list_add (&l->blocked_locks, &released);
if (path) {
GF_FREE (path);
path = NULL;
}
}
list_for_each_entry_safe (l, tmp, &dom->inodelk_list, list) {
if (l->transport != trans)
continue;
__delete_inode_lock (l);
__destroy_inode_lock (l);
inode_path (inode, NULL, &path);
if (path)
file = path;
else
file = uuid_utoa (inode->gfid);
gf_log (this->name, GF_LOG_DEBUG,
"releasing granted lock on %s held by "
"{transport=%p, pid=%"PRId64" lk-owner=%s}",
file, trans, (uint64_t) l->client_pid,
lkowner_utoa (&l->owner));
if (path) {
GF_FREE (path);
path = NULL;
}
}
}
if (path)
GF_FREE (path);
pthread_mutex_unlock (&pinode->mutex);
list_for_each_entry_safe (l, tmp, &released, blocked_locks) {
list_del_init (&l->blocked_locks);
STACK_UNWIND_STRICT (inodelk, l->frame, -1, EAGAIN);
GF_FREE (l);
}
grant_blocked_inode_locks (this, pinode, dom);
return 0;
}
static int
pl_inode_setlk (xlator_t *this, pl_inode_t *pl_inode, pl_inode_lock_t *lock,
int can_block, pl_dom_list_t *dom)
{
int ret = -EINVAL;
pl_inode_lock_t *retlock = NULL;
pthread_mutex_lock (&pl_inode->mutex);
{
if (lock->fl_type != F_UNLCK) {
ret = __lock_inodelk (this, pl_inode, lock, can_block, dom);
if (ret == 0)
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);
if (ret == -EAGAIN)
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);
goto out;
}
retlock = __inode_unlock_lock (this, lock, dom);
if (!retlock) {
gf_log (this->name, GF_LOG_DEBUG,
"Bad Unlock issued on Inode lock");
ret = -EINVAL;
goto out;
}
__destroy_inode_lock (retlock);
ret = 0;
}
out:
pthread_mutex_unlock (&pl_inode->mutex);
grant_blocked_inode_locks (this, pl_inode, dom);
return ret;
}
/* Create a new inode_lock_t */
pl_inode_lock_t *
new_inode_lock (struct gf_flock *flock, void *transport, pid_t client_pid,
gf_lkowner_t *owner, const char *volume)
{
pl_inode_lock_t *lock = NULL;
lock = GF_CALLOC (1, sizeof (*lock),
gf_locks_mt_pl_inode_lock_t);
if (!lock) {
return NULL;
}
lock->fl_start = flock->l_start;
lock->fl_type = flock->l_type;
if (flock->l_len == 0)
lock->fl_end = LLONG_MAX;
else
lock->fl_end = flock->l_start + flock->l_len - 1;
lock->transport = transport;
lock->client_pid = client_pid;
lock->volume = volume;
lock->owner = *owner;
INIT_LIST_HEAD (&lock->list);
INIT_LIST_HEAD (&lock->blocked_locks);
return lock;
}
/* Common inodelk code called from pl_inodelk and pl_finodelk */
int
pl_common_inodelk (call_frame_t *frame, xlator_t *this,
const char *volume, inode_t *inode, int32_t cmd,
struct gf_flock *flock, loc_t *loc, fd_t *fd)
{
int32_t op_ret = -1;
int32_t op_errno = 0;
int ret = -1;
int can_block = 0;
pid_t client_pid = -1;
void * transport = NULL;
pl_inode_t * pinode = NULL;
pl_inode_lock_t * reqlock = NULL;
pl_dom_list_t * dom = NULL;
VALIDATE_OR_GOTO (frame, out);
VALIDATE_OR_GOTO (inode, unwind);
VALIDATE_OR_GOTO (flock, unwind);
if ((flock->l_start < 0) || (flock->l_len < 0)) {
op_errno = EINVAL;
goto unwind;
}
pl_trace_in (this, frame, fd, loc, cmd, flock, volume);
transport = frame->root->trans;
client_pid = frame->root->pid;
pinode = pl_inode_get (this, inode);
if (!pinode) {
op_errno = ENOMEM;
goto unwind;
}
dom = get_domain (pinode, volume);
if (frame->root->lk_owner.len == 0) {
/*
special case: this means release all locks
from this transport
*/
gf_log (this->name, GF_LOG_TRACE,
"Releasing all locks from transport %p", transport);
release_inode_locks_of_transport (this, dom, inode, transport);
op_ret = 0;
goto unwind;
}
reqlock = new_inode_lock (flock, transport, client_pid,
&frame->root->lk_owner, volume);
if (!reqlock) {
op_ret = -1;
op_errno = ENOMEM;
goto unwind;
}
reqlock->frame = frame;
reqlock->this = this;
switch (cmd) {
case F_SETLKW:
can_block = 1;
reqlock->frame = frame;
reqlock->this = this;
/* fall through */
case F_SETLK:
memcpy (&reqlock->user_flock, flock, sizeof (struct gf_flock));
ret = pl_inode_setlk (this, pinode, reqlock,
can_block, dom);
if (ret < 0) {
if ((can_block) && (F_UNLCK != flock->l_type)) {
pl_trace_block (this, frame, fd, loc,
cmd, flock, volume);
goto out;
}
gf_log (this->name, GF_LOG_TRACE, "returning EAGAIN");
op_errno = -ret;
__destroy_inode_lock (reqlock);
goto unwind;
}
break;
default:
op_errno = ENOTSUP;
gf_log (this->name, GF_LOG_DEBUG,
"Lock command F_GETLK not supported for [f]inodelk "
"(cmd=%d)",
cmd);
goto unwind;
}
op_ret = 0;
unwind:
if ((inode != NULL) && (flock !=NULL)) {
pl_update_refkeeper (this, inode);
pl_trace_out (this, frame, fd, loc, cmd, flock, op_ret, op_errno, volume);
}
STACK_UNWIND_STRICT (inodelk, frame, op_ret, op_errno);
out:
return 0;
}
int
pl_inodelk (call_frame_t *frame, xlator_t *this,
const char *volume, loc_t *loc, int32_t cmd, struct gf_flock *flock)
{
pl_common_inodelk (frame, this, volume, loc->inode, cmd, flock, loc, NULL);
return 0;
}
int
pl_finodelk (call_frame_t *frame, xlator_t *this,
const char *volume, fd_t *fd, int32_t cmd, struct gf_flock *flock)
{
pl_common_inodelk (frame, this, volume, fd->inode, cmd, flock, NULL, fd);
return 0;
}
static int32_t
__get_inodelk_count (xlator_t *this, pl_inode_t *pl_inode)
{
int32_t count = 0;
pl_inode_lock_t *lock = NULL;
pl_dom_list_t *dom = NULL;
list_for_each_entry (dom, &pl_inode->dom_list, inode_list) {
list_for_each_entry (lock, &dom->inodelk_list, list) {
gf_log (this->name, GF_LOG_DEBUG,
" XATTR DEBUG"
" domain: %s %s (pid=%d) (lk-owner=%s) %"PRId64" - %"PRId64" "
"state = Active",
dom->domain,
lock->fl_type == F_UNLCK ? "Unlock" : "Lock",
lock->client_pid,
lkowner_utoa (&lock->owner),
lock->user_flock.l_start,
lock->user_flock.l_len);
count++;
}
list_for_each_entry (lock, &dom->blocked_inodelks, blocked_locks) {
gf_log (this->name, GF_LOG_DEBUG,
" XATTR DEBUG"
" domain: %s %s (pid=%d) (lk-owner=%s) %"PRId64" - %"PRId64" "
"state = Blocked",
dom->domain,
lock->fl_type == F_UNLCK ? "Unlock" : "Lock",
lock->client_pid,
lkowner_utoa (&lock->owner),
lock->user_flock.l_start,
lock->user_flock.l_len);
count++;
}
}
return count;
}
int32_t
get_inodelk_count (xlator_t *this, inode_t *inode)
{
pl_inode_t *pl_inode = NULL;
uint64_t tmp_pl_inode = 0;
int ret = 0;
int32_t count = 0;
ret = inode_ctx_get (inode, this, &tmp_pl_inode);
if (ret != 0) {
goto out;
}
pl_inode = (pl_inode_t *)(long) tmp_pl_inode;
pthread_mutex_lock (&pl_inode->mutex);
{
count = __get_inodelk_count (this, pl_inode);
}
pthread_mutex_unlock (&pl_inode->mutex);
out:
return count;
}
|