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
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
|
/*
Copyright (c) 2006-2009 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 "server-protocol.h"
#include "server-helpers.h"
/* server_loc_fill - derive a loc_t for a given inode number
*
* NOTE: make sure that @loc is empty, because any pointers it holds with reference will
* be leaked after returning from here.
*/
int
server_loc_fill (loc_t *loc, server_state_t *state,
ino_t ino, ino_t par,
const char *name, const char *path)
{
inode_t *inode = NULL;
inode_t *parent = NULL;
int32_t ret = -1;
char *dentry_path = NULL;
GF_VALIDATE_OR_GOTO ("server", loc, out);
GF_VALIDATE_OR_GOTO ("server", state, out);
GF_VALIDATE_OR_GOTO ("server", path, out);
/* anything beyond this point is success */
ret = 0;
loc->ino = ino;
inode = loc->inode;
if (inode == NULL) {
if (ino)
inode = inode_search (state->itable, ino, NULL);
if ((inode == NULL) &&
(par && name))
inode = inode_search (state->itable, par, name);
loc->inode = inode;
if (inode)
loc->ino = inode->ino;
}
parent = loc->parent;
if (parent == NULL) {
if (inode)
parent = inode_parent (inode, par, name);
else
parent = inode_search (state->itable, par, NULL);
loc->parent = parent;
}
if (name && parent) {
ret = inode_path (parent, name, &dentry_path);
if (ret < 0) {
gf_log (state->bound_xl->name, GF_LOG_DEBUG,
"failed to build path for %"PRId64"/%s: %s",
parent->ino, name, strerror (-ret));
}
} else if (inode) {
ret = inode_path (inode, NULL, &dentry_path);
if (ret < 0) {
gf_log (state->bound_xl->name, GF_LOG_DEBUG,
"failed to build path for %"PRId64": %s",
inode->ino, strerror (-ret));
}
}
if (dentry_path) {
if (strcmp (dentry_path, path)) {
gf_log (state->bound_xl->name, GF_LOG_DEBUG,
"paths differ for inode(%"PRId64"): "
"client path = %s. dentry path = %s",
ino, path, dentry_path);
}
loc->path = dentry_path;
loc->name = strrchr (loc->path, '/');
if (loc->name)
loc->name++;
} else {
loc->path = strdup (path);
loc->name = strrchr (loc->path, '/');
if (loc->name)
loc->name++;
}
out:
return ret;
}
/*
* stat_to_str - convert struct stat to a ASCII string
* @stbuf: struct stat pointer
*
* not for external reference
*/
char *
stat_to_str (struct stat *stbuf)
{
int ret = 0;
char *tmp_buf = NULL;
uint64_t dev = stbuf->st_dev;
uint64_t ino = stbuf->st_ino;
uint32_t mode = stbuf->st_mode;
uint32_t nlink = stbuf->st_nlink;
uint32_t uid = stbuf->st_uid;
uint32_t gid = stbuf->st_gid;
uint64_t rdev = stbuf->st_rdev;
uint64_t size = stbuf->st_size;
uint32_t blksize = stbuf->st_blksize;
uint64_t blocks = stbuf->st_blocks;
uint32_t atime = stbuf->st_atime;
uint32_t mtime = stbuf->st_mtime;
uint32_t ctime = stbuf->st_ctime;
uint32_t atime_nsec = ST_ATIM_NSEC(stbuf);
uint32_t mtime_nsec = ST_MTIM_NSEC(stbuf);
uint32_t ctime_nsec = ST_CTIM_NSEC(stbuf);
ret = asprintf (&tmp_buf,
GF_STAT_PRINT_FMT_STR,
dev,
ino,
mode,
nlink,
uid,
gid,
rdev,
size,
blksize,
blocks,
atime,
atime_nsec,
mtime,
mtime_nsec,
ctime,
ctime_nsec);
if (-1 == ret) {
gf_log ("protocol/server", GF_LOG_DEBUG,
"asprintf failed while setting up stat buffer string");
return NULL;
}
return tmp_buf;
}
void
server_loc_wipe (loc_t *loc)
{
if (loc->parent) {
inode_unref (loc->parent);
loc->parent = NULL;
}
if (loc->inode) {
inode_unref (loc->inode);
loc->inode = NULL;
}
if (loc->path)
FREE (loc->path);
}
void
server_resolve_wipe (server_resolve_t *resolve)
{
struct resolve_comp *comp = NULL;
int i = 0;
if (resolve->path)
FREE (resolve->path);
if (resolve->bname)
FREE (resolve->bname);
if (resolve->resolved)
FREE (resolve->resolved);
loc_wipe (&resolve->deep_loc);
comp = resolve->components;
if (comp) {
for (i = 0; comp[i].basename; i++) {
if (comp[i].inode)
inode_unref (comp[i].inode);
}
FREE (resolve->components);
}
}
void
free_state (server_state_t *state)
{
if (state->trans) {
transport_unref (state->trans);
state->trans = NULL;
}
if (state->fd) {
fd_unref (state->fd);
state->fd = NULL;
}
if (state->iobref) {
iobref_unref (state->iobref);
state->iobref = NULL;
}
if (state->iobuf) {
iobuf_unref (state->iobuf);
state->iobuf = NULL;
}
if (state->dict) {
dict_unref (state->dict);
state->dict = NULL;
}
if (state->volume)
FREE (state->volume);
if (state->name)
FREE (state->name);
server_loc_wipe (&state->loc);
server_loc_wipe (&state->loc2);
server_resolve_wipe (&state->resolve);
server_resolve_wipe (&state->resolve2);
FREE (state);
}
call_frame_t *
server_copy_frame (call_frame_t *frame)
{
call_frame_t *new_frame = NULL;
server_state_t *state = NULL, *new_state = NULL;
state = frame->root->state;
new_frame = copy_frame (frame);
new_state = CALLOC (1, sizeof (server_state_t));
new_frame->root->op = frame->root->op;
new_frame->root->type = frame->root->type;
new_frame->root->trans = state->trans;
new_frame->root->state = new_state;
new_state->bound_xl = state->bound_xl;
new_state->trans = transport_ref (state->trans);
new_state->itable = state->itable;
new_state->resolve.fd_no = -1;
new_state->resolve2.fd_no = -1;
return new_frame;
}
int
gf_add_locker (struct _lock_table *table, const char *volume,
loc_t *loc, fd_t *fd, pid_t pid)
{
int32_t ret = -1;
struct _locker *new = NULL;
uint8_t dir = 0;
new = CALLOC (1, sizeof (struct _locker));
if (new == NULL) {
gf_log ("server", GF_LOG_ERROR,
"failed to allocate memory for \'struct _locker\'");
goto out;
}
INIT_LIST_HEAD (&new->lockers);
new->volume = strdup (volume);
if (fd == NULL) {
loc_copy (&new->loc, loc);
dir = S_ISDIR (new->loc.inode->st_mode);
} else {
new->fd = fd_ref (fd);
dir = S_ISDIR (fd->inode->st_mode);
}
new->pid = pid;
LOCK (&table->lock);
{
if (dir)
list_add_tail (&new->lockers, &table->dir_lockers);
else
list_add_tail (&new->lockers, &table->file_lockers);
}
UNLOCK (&table->lock);
out:
return ret;
}
int
gf_del_locker (struct _lock_table *table, const char *volume,
loc_t *loc, fd_t *fd, pid_t pid)
{
struct _locker *locker = NULL;
struct _locker *tmp = NULL;
int32_t ret = 0;
uint8_t dir = 0;
struct list_head *head = NULL;
struct list_head del;
INIT_LIST_HEAD (&del);
if (fd) {
dir = S_ISDIR (fd->inode->st_mode);
} else {
dir = S_ISDIR (loc->inode->st_mode);
}
LOCK (&table->lock);
{
if (dir) {
head = &table->dir_lockers;
} else {
head = &table->file_lockers;
}
list_for_each_entry_safe (locker, tmp, head, lockers) {
if (locker->fd && fd &&
(locker->fd == fd) && (locker->pid == pid)
&& !strcmp (locker->volume, volume)) {
list_move_tail (&locker->lockers, &del);
} else if (locker->loc.inode &&
loc &&
(locker->loc.inode == loc->inode) &&
(locker->pid == pid)
&& !strcmp (locker->volume, volume)) {
list_move_tail (&locker->lockers, &del);
}
}
}
UNLOCK (&table->lock);
tmp = NULL;
locker = NULL;
list_for_each_entry_safe (locker, tmp, &del, lockers) {
list_del_init (&locker->lockers);
if (locker->fd)
fd_unref (locker->fd);
else
loc_wipe (&locker->loc);
free (locker->volume);
free (locker);
}
return ret;
}
int
gf_direntry_to_bin (dir_entry_t *head, char *buffer)
{
dir_entry_t *trav = NULL;
uint32_t len = 0;
uint32_t this_len = 0;
size_t buflen = -1;
char *ptr = NULL;
char *tmp_buf = NULL;
trav = head->next;
while (trav) {
len += strlen (trav->name);
len += 1;
len += strlen (trav->link);
len += 1; /* for '\n' */
len += 256; // max possible for statbuf;
trav = trav->next;
}
ptr = buffer;
trav = head->next;
while (trav) {
tmp_buf = stat_to_str (&trav->buf);
/* tmp_buf will have \n before \0 */
this_len = sprintf (ptr, "%s/%s%s\n",
trav->name, tmp_buf,
trav->link);
FREE (tmp_buf);
trav = trav->next;
ptr += this_len;
}
buflen = strlen (buffer);
return buflen;
}
static struct _lock_table *
gf_lock_table_new (void)
{
struct _lock_table *new = NULL;
new = CALLOC (1, sizeof (struct _lock_table));
if (new == NULL) {
gf_log ("server-protocol", GF_LOG_CRITICAL,
"failed to allocate memory for new lock table");
goto out;
}
INIT_LIST_HEAD (&new->dir_lockers);
INIT_LIST_HEAD (&new->file_lockers);
LOCK_INIT (&new->lock);
out:
return new;
}
int
do_lock_table_cleanup (xlator_t *this, server_connection_t *conn,
call_frame_t *frame, struct _lock_table *ltable)
{
struct list_head file_lockers, dir_lockers;
call_frame_t *tmp_frame = NULL;
struct flock flock = {0, };
xlator_t *bound_xl = NULL;
struct _locker *locker = NULL, *tmp = NULL;
int ret = -1;
bound_xl = conn->bound_xl;
INIT_LIST_HEAD (&file_lockers);
INIT_LIST_HEAD (&dir_lockers);
LOCK (<able->lock);
{
list_splice_init (<able->file_lockers,
&file_lockers);
list_splice_init (<able->dir_lockers, &dir_lockers);
}
UNLOCK (<able->lock);
free (ltable);
flock.l_type = F_UNLCK;
flock.l_start = 0;
flock.l_len = 0;
list_for_each_entry_safe (locker,
tmp, &file_lockers, lockers) {
tmp_frame = copy_frame (frame);
if (tmp_frame == NULL) {
gf_log (this->name, GF_LOG_ERROR,
"out of memory");
goto out;
}
/*
pid = 0 is a special case that tells posix-locks
to release all locks from this transport
*/
tmp_frame->root->pid = 0;
tmp_frame->root->trans = conn;
if (locker->fd) {
STACK_WIND (tmp_frame, server_nop_cbk,
bound_xl,
bound_xl->fops->finodelk,
locker->volume,
locker->fd, F_SETLK, &flock);
fd_unref (locker->fd);
} else {
STACK_WIND (tmp_frame, server_nop_cbk,
bound_xl,
bound_xl->fops->inodelk,
locker->volume,
&(locker->loc), F_SETLK, &flock);
loc_wipe (&locker->loc);
}
free (locker->volume);
list_del_init (&locker->lockers);
free (locker);
}
tmp = NULL;
locker = NULL;
list_for_each_entry_safe (locker, tmp, &dir_lockers, lockers) {
tmp_frame = copy_frame (frame);
tmp_frame->root->pid = 0;
tmp_frame->root->trans = conn;
if (locker->fd) {
STACK_WIND (tmp_frame, server_nop_cbk,
bound_xl,
bound_xl->fops->fentrylk,
locker->volume,
locker->fd, NULL,
ENTRYLK_UNLOCK, ENTRYLK_WRLCK);
fd_unref (locker->fd);
} else {
STACK_WIND (tmp_frame, server_nop_cbk,
bound_xl,
bound_xl->fops->entrylk,
locker->volume,
&(locker->loc), NULL,
ENTRYLK_UNLOCK, ENTRYLK_WRLCK);
loc_wipe (&locker->loc);
}
free (locker->volume);
list_del_init (&locker->lockers);
free (locker);
}
ret = 0;
out:
return ret;
}
static int
server_connection_cleanup_flush_cbk (call_frame_t *frame, void *cookie,
xlator_t *this, int32_t op_ret,
int32_t op_errno)
{
fd_t *fd = NULL;
fd = frame->local;
fd_unref (fd);
frame->local = NULL;
STACK_DESTROY (frame->root);
return 0;
}
int
do_fd_cleanup (xlator_t *this, server_connection_t *conn, call_frame_t *frame,
fdentry_t *fdentries, int fd_count)
{
fd_t *fd = NULL;
int i = 0, ret = -1;
call_frame_t *tmp_frame = NULL;
xlator_t *bound_xl = NULL;
bound_xl = conn->bound_xl;
for (i = 0;i < fd_count; i++) {
fd = fdentries[i].fd;
if (fd != NULL) {
tmp_frame = copy_frame (frame);
if (tmp_frame == NULL) {
gf_log (this->name, GF_LOG_ERROR,
"out of memory");
goto out;
}
tmp_frame->local = fd;
tmp_frame->root->pid = 0;
tmp_frame->root->trans = conn;
tmp_frame->root->lk_owner = 0;
STACK_WIND (tmp_frame,
server_connection_cleanup_flush_cbk,
bound_xl, bound_xl->fops->flush, fd);
}
}
FREE (fdentries);
ret = 0;
out:
return ret;
}
int
do_connection_cleanup (xlator_t *this, server_connection_t *conn,
struct _lock_table *ltable, fdentry_t *fdentries, int fd_count)
{
int ret = 0;
int saved_ret = 0;
call_frame_t *frame = NULL;
server_state_t *state = NULL;
frame = create_frame (this, this->ctx->pool);
if (frame == NULL) {
gf_log (this->name, GF_LOG_ERROR, "out of memory");
goto out;
}
saved_ret = do_lock_table_cleanup (this, conn, frame, ltable);
if (fdentries != NULL) {
ret = do_fd_cleanup (this, conn, frame, fdentries, fd_count);
}
state = CALL_STATE (frame);
if (state)
free (state);
STACK_DESTROY (frame->root);
if (saved_ret || ret) {
ret = -1;
}
out:
return ret;
}
int
server_connection_cleanup (xlator_t *this, server_connection_t *conn)
{
char do_cleanup = 0;
struct _lock_table *ltable = NULL;
fdentry_t *fdentries = NULL;
uint32_t fd_count = 0;
int ret = 0;
if (conn == NULL) {
goto out;
}
pthread_mutex_lock (&conn->lock);
{
conn->active_transports--;
if (conn->active_transports == 0) {
if (conn->ltable) {
ltable = conn->ltable;
conn->ltable = gf_lock_table_new ();
}
if (conn->fdtable) {
fdentries = gf_fd_fdtable_get_all_fds (conn->fdtable,
&fd_count);
}
do_cleanup = 1;
}
}
pthread_mutex_unlock (&conn->lock);
if (do_cleanup && conn->bound_xl)
ret = do_connection_cleanup (this, conn, ltable, fdentries, fd_count);
out:
return ret;
}
int
server_connection_destroy (xlator_t *this, server_connection_t *conn)
{
call_frame_t *frame = NULL, *tmp_frame = NULL;
xlator_t *bound_xl = NULL;
int32_t ret = -1;
server_state_t *state = NULL;
struct list_head file_lockers;
struct list_head dir_lockers;
struct _lock_table *ltable = NULL;
struct _locker *locker = NULL, *tmp = NULL;
struct flock flock = {0,};
fd_t *fd = NULL;
int32_t i = 0;
fdentry_t *fdentries = NULL;
uint32_t fd_count = 0;
if (conn == NULL) {
ret = 0;
goto out;
}
bound_xl = (xlator_t *) (conn->bound_xl);
if (bound_xl) {
/* trans will have ref_count = 1 after this call, but its
ok since this function is called in
GF_EVENT_TRANSPORT_CLEANUP */
frame = create_frame (this, this->ctx->pool);
pthread_mutex_lock (&(conn->lock));
{
if (conn->ltable) {
ltable = conn->ltable;
conn->ltable = NULL;
}
}
pthread_mutex_unlock (&conn->lock);
INIT_LIST_HEAD (&file_lockers);
INIT_LIST_HEAD (&dir_lockers);
LOCK (<able->lock);
{
list_splice_init (<able->file_lockers,
&file_lockers);
list_splice_init (<able->dir_lockers, &dir_lockers);
}
UNLOCK (<able->lock);
free (ltable);
flock.l_type = F_UNLCK;
flock.l_start = 0;
flock.l_len = 0;
list_for_each_entry_safe (locker,
tmp, &file_lockers, lockers) {
tmp_frame = copy_frame (frame);
/*
pid = 0 is a special case that tells posix-locks
to release all locks from this transport
*/
tmp_frame->root->pid = 0;
tmp_frame->root->trans = conn;
if (locker->fd) {
STACK_WIND (tmp_frame, server_nop_cbk,
bound_xl,
bound_xl->fops->finodelk,
locker->volume,
locker->fd, F_SETLK, &flock);
fd_unref (locker->fd);
} else {
STACK_WIND (tmp_frame, server_nop_cbk,
bound_xl,
bound_xl->fops->inodelk,
locker->volume,
&(locker->loc), F_SETLK, &flock);
loc_wipe (&locker->loc);
}
free (locker->volume);
list_del_init (&locker->lockers);
free (locker);
}
tmp = NULL;
locker = NULL;
list_for_each_entry_safe (locker, tmp, &dir_lockers, lockers) {
tmp_frame = copy_frame (frame);
tmp_frame->root->pid = 0;
tmp_frame->root->trans = conn;
if (locker->fd) {
STACK_WIND (tmp_frame, server_nop_cbk,
bound_xl,
bound_xl->fops->fentrylk,
locker->volume,
locker->fd, NULL,
ENTRYLK_UNLOCK, ENTRYLK_WRLCK);
fd_unref (locker->fd);
} else {
STACK_WIND (tmp_frame, server_nop_cbk,
bound_xl,
bound_xl->fops->entrylk,
locker->volume,
&(locker->loc), NULL,
ENTRYLK_UNLOCK, ENTRYLK_WRLCK);
loc_wipe (&locker->loc);
}
free (locker->volume);
list_del_init (&locker->lockers);
free (locker);
}
pthread_mutex_lock (&(conn->lock));
{
if (conn->fdtable) {
fdentries = gf_fd_fdtable_get_all_fds (conn->fdtable,
&fd_count);
gf_fd_fdtable_destroy (conn->fdtable);
conn->fdtable = NULL;
}
}
pthread_mutex_unlock (&conn->lock);
if (fdentries != NULL) {
for (i = 0; i < fd_count; i++) {
fd = fdentries[i].fd;
if (fd != NULL) {
tmp_frame = copy_frame (frame);
tmp_frame->local = fd;
STACK_WIND (tmp_frame,
server_connection_cleanup_flush_cbk,
bound_xl,
bound_xl->fops->flush,
fd);
}
}
FREE (fdentries);
}
}
state = CALL_STATE (frame);
if (state)
free (state);
STACK_DESTROY (frame->root);
gf_log (this->name, GF_LOG_INFO, "destroyed connection of %s",
conn->id);
FREE (conn->id);
FREE (conn);
out:
return ret;
}
server_connection_t *
server_connection_get (xlator_t *this, const char *id)
{
server_connection_t *conn = NULL;
server_connection_t *trav = NULL;
server_conf_t *conf = NULL;
conf = this->private;
pthread_mutex_lock (&conf->mutex);
{
list_for_each_entry (trav, &conf->conns, list) {
if (!strcmp (id, trav->id)) {
conn = trav;
break;
}
}
if (!conn) {
conn = (void *) CALLOC (1, sizeof (*conn));
conn->id = strdup (id);
conn->fdtable = gf_fd_fdtable_alloc ();
conn->ltable = gf_lock_table_new ();
pthread_mutex_init (&conn->lock, NULL);
list_add (&conn->list, &conf->conns);
}
conn->ref++;
conn->active_transports++;
}
pthread_mutex_unlock (&conf->mutex);
return conn;
}
void
server_connection_put (xlator_t *this, server_connection_t *conn)
{
server_conf_t *conf = NULL;
server_connection_t *todel = NULL;
if (conn == NULL) {
goto out;
}
conf = this->private;
pthread_mutex_lock (&conf->mutex);
{
conn->ref--;
if (!conn->ref) {
list_del_init (&conn->list);
todel = conn;
}
}
pthread_mutex_unlock (&conf->mutex);
if (todel) {
server_connection_destroy (this, todel);
}
out:
return;
}
|