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
|
/*
Copyright (c) 2007-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/>.
*/
#include "fd.h"
#include "glusterfs.h"
#include "inode.h"
#include "dict.h"
#include "statedump.h"
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif
static uint32_t
gf_fd_fdtable_expand (fdtable_t *fdtable, uint32_t nr);
static fd_t *
_fd_ref (fd_t *fd);
/*
Allocate in memory chunks of power of 2 starting from 1024B
Assumes fdtable->lock is held
*/
static inline uint32_t
gf_roundup_power_of_two (uint32_t nr)
{
uint32_t result = 1;
if (nr < 0) {
gf_log ("server-protocol/fd",
GF_LOG_ERROR,
"Negative number passed");
return -1;
}
while (result <= nr)
result *= 2;
return result;
}
static int
gf_fd_chain_fd_entries (fdentry_t *entries, uint32_t startidx,
uint32_t endcount)
{
uint32_t i = 0;
if (!entries)
return -1;
/* Chain only till the second to last entry because we want to
* ensure that the last entry has GF_FDTABLE_END.
*/
for (i = startidx; i < (endcount - 1); i++)
entries[i].next_free = i + 1;
/* i has already been incremented upto the last entry. */
entries[i].next_free = GF_FDTABLE_END;
return 0;
}
static uint32_t
gf_fd_fdtable_expand (fdtable_t *fdtable, uint32_t nr)
{
fdentry_t *oldfds = NULL;
uint32_t oldmax_fds = -1;
if (fdtable == NULL || nr < 0)
{
gf_log ("fd", GF_LOG_ERROR, "invalid argument");
return EINVAL;
}
nr /= (1024 / sizeof (fdentry_t));
nr = gf_roundup_power_of_two (nr + 1);
nr *= (1024 / sizeof (fdentry_t));
oldfds = fdtable->fdentries;
oldmax_fds = fdtable->max_fds;
fdtable->fdentries = CALLOC (nr, sizeof (fdentry_t));
ERR_ABORT (fdtable->fdentries);
fdtable->max_fds = nr;
if (oldfds) {
uint32_t cpy = oldmax_fds * sizeof (fdentry_t);
memcpy (fdtable->fdentries, oldfds, cpy);
}
gf_fd_chain_fd_entries (fdtable->fdentries, oldmax_fds,
fdtable->max_fds);
/* Now that expansion is done, we must update the fd list
* head pointer so that the fd allocation functions can continue
* using the expanded table.
*/
fdtable->first_free = oldmax_fds;
FREE (oldfds);
return 0;
}
fdtable_t *
gf_fd_fdtable_alloc (void)
{
fdtable_t *fdtable = NULL;
fdtable = CALLOC (1, sizeof (*fdtable));
if (!fdtable)
return NULL;
pthread_mutex_init (&fdtable->lock, NULL);
pthread_mutex_lock (&fdtable->lock);
{
gf_fd_fdtable_expand (fdtable, 0);
}
pthread_mutex_unlock (&fdtable->lock);
return fdtable;
}
fdentry_t *
__gf_fd_fdtable_get_all_fds (fdtable_t *fdtable, uint32_t *count)
{
fdentry_t *fdentries = NULL;
if (count == NULL) {
goto out;
}
fdentries = fdtable->fdentries;
fdtable->fdentries = calloc (fdtable->max_fds, sizeof (fdentry_t));
gf_fd_chain_fd_entries (fdtable->fdentries, 0, fdtable->max_fds);
*count = fdtable->max_fds;
out:
return fdentries;
}
fdentry_t *
gf_fd_fdtable_get_all_fds (fdtable_t *fdtable, uint32_t *count)
{
fdentry_t *entries = NULL;
if (fdtable) {
pthread_mutex_lock (&fdtable->lock);
{
entries = __gf_fd_fdtable_get_all_fds (fdtable, count);
}
pthread_mutex_unlock (&fdtable->lock);
}
return entries;
}
void
gf_fd_fdtable_destroy (fdtable_t *fdtable)
{
struct list_head list = {0, };
fd_t *fd = NULL;
fdentry_t *fdentries = NULL;
uint32_t fd_count = 0;
int32_t i = 0;
INIT_LIST_HEAD (&list);
if (!fdtable)
return;
pthread_mutex_lock (&fdtable->lock);
{
fdentries = __gf_fd_fdtable_get_all_fds (fdtable, &fd_count);
FREE (fdtable->fdentries);
}
pthread_mutex_unlock (&fdtable->lock);
if (fdentries != NULL) {
for (i = 0; i < fd_count; i++) {
fd = fdentries[i].fd;
if (fd != NULL) {
fd_unref (fd);
}
}
FREE (fdentries);
pthread_mutex_destroy (&fdtable->lock);
FREE (fdtable);
}
}
int32_t
gf_fd_unused_get (fdtable_t *fdtable, fd_t *fdptr)
{
int32_t fd = -1;
fdentry_t *fde = NULL;
int error;
int alloc_attempts = 0;
if (fdtable == NULL || fdptr == NULL)
{
gf_log ("fd", GF_LOG_ERROR, "invalid argument");
return EINVAL;
}
pthread_mutex_lock (&fdtable->lock);
{
fd_alloc_try_again:
if (fdtable->first_free != GF_FDTABLE_END) {
fde = &fdtable->fdentries[fdtable->first_free];
fd = fdtable->first_free;
fdtable->first_free = fde->next_free;
fde->next_free = GF_FDENTRY_ALLOCATED;
fde->fd = fdptr;
} else {
/* If this is true, there is something
* seriously wrong with our data structures.
*/
if (alloc_attempts >= 2) {
gf_log ("server-protocol.c", GF_LOG_ERROR,
"Multiple attempts to expand fd table"
" have failed.");
goto out;
}
error = gf_fd_fdtable_expand (fdtable,
fdtable->max_fds + 1);
if (error) {
gf_log ("server-protocol.c",
GF_LOG_ERROR,
"Cannot expand fdtable:%s", strerror (error));
goto out;
}
++alloc_attempts;
/* At this point, the table stands expanded
* with the first_free referring to the first
* free entry in the new set of fdentries that
* have just been allocated. That means, the
* above logic should just work.
*/
goto fd_alloc_try_again;
}
}
out:
pthread_mutex_unlock (&fdtable->lock);
return fd;
}
inline void
gf_fd_put (fdtable_t *fdtable, int32_t fd)
{
fd_t *fdptr = NULL;
fdentry_t *fde = NULL;
if (fdtable == NULL || fd < 0)
{
gf_log ("fd", GF_LOG_ERROR, "invalid argument");
return;
}
if (!(fd < fdtable->max_fds))
{
gf_log ("fd", GF_LOG_ERROR, "invalid argument");
return;
}
pthread_mutex_lock (&fdtable->lock);
{
fde = &fdtable->fdentries[fd];
/* If the entry is not allocated, put operation must return
* without doing anything.
* This has the potential of masking out any bugs in a user of
* fd that ends up calling gf_fd_put twice for the same fd or
* for an unallocated fd, but thats a price we have to pay for
* ensuring sanity of our fd-table.
*/
if (fde->next_free != GF_FDENTRY_ALLOCATED)
goto unlock_out;
fdptr = fde->fd;
fde->fd = NULL;
fde->next_free = fdtable->first_free;
fdtable->first_free = fd;
}
unlock_out:
pthread_mutex_unlock (&fdtable->lock);
if (fdptr) {
fd_unref (fdptr);
}
}
fd_t *
gf_fd_fdptr_get (fdtable_t *fdtable, int64_t fd)
{
fd_t *fdptr = NULL;
if (fdtable == NULL || fd < 0)
{
gf_log ("fd", GF_LOG_ERROR, "invalid argument");
errno = EINVAL;
return NULL;
}
if (!(fd < fdtable->max_fds))
{
gf_log ("fd", GF_LOG_ERROR, "invalid argument");
errno = EINVAL;
return NULL;
}
pthread_mutex_lock (&fdtable->lock);
{
fdptr = fdtable->fdentries[fd].fd;
if (fdptr) {
fd_ref (fdptr);
}
}
pthread_mutex_unlock (&fdtable->lock);
return fdptr;
}
fd_t *
_fd_ref (fd_t *fd)
{
++fd->refcount;
return fd;
}
fd_t *
fd_ref (fd_t *fd)
{
fd_t *refed_fd = NULL;
if (!fd) {
gf_log ("fd", GF_LOG_ERROR, "@fd=%p", fd);
return NULL;
}
LOCK (&fd->inode->lock);
refed_fd = _fd_ref (fd);
UNLOCK (&fd->inode->lock);
return refed_fd;
}
fd_t *
_fd_unref (fd_t *fd)
{
assert (fd->refcount);
--fd->refcount;
if (fd->refcount == 0){
list_del_init (&fd->inode_list);
}
return fd;
}
static void
fd_destroy (fd_t *fd)
{
xlator_t *xl = NULL;
int i = 0;
if (fd == NULL){
gf_log ("xlator", GF_LOG_ERROR, "invalid arugument");
goto out;
}
if (fd->inode == NULL){
gf_log ("xlator", GF_LOG_ERROR, "fd->inode is NULL");
goto out;
}
if (!fd->_ctx)
goto out;
if (S_ISDIR (fd->inode->st_mode)) {
for (i = 0; i < fd->inode->table->xl->ctx->xl_count; i++) {
if (fd->_ctx[i].key) {
xl = (xlator_t *)(long)fd->_ctx[i].key;
if (xl->cbks->releasedir)
xl->cbks->releasedir (xl, fd);
}
}
} else {
for (i = 0; i < fd->inode->table->xl->ctx->xl_count; i++) {
if (fd->_ctx[i].key) {
xl = (xlator_t *)(long)fd->_ctx[i].key;
if (xl->cbks->release)
xl->cbks->release (xl, fd);
}
}
}
LOCK_DESTROY (&fd->lock);
FREE (fd->_ctx);
inode_unref (fd->inode);
fd->inode = (inode_t *)0xaaaaaaaa;
FREE (fd);
out:
return;
}
void
fd_unref (fd_t *fd)
{
int32_t refcount = 0;
if (!fd) {
gf_log ("fd.c", GF_LOG_ERROR, "fd is NULL");
return;
}
LOCK (&fd->inode->lock);
{
_fd_unref (fd);
refcount = fd->refcount;
}
UNLOCK (&fd->inode->lock);
if (refcount == 0) {
fd_destroy (fd);
}
return ;
}
fd_t *
fd_bind (fd_t *fd)
{
inode_t *inode = fd->inode;
if (!fd) {
gf_log ("fd.c", GF_LOG_ERROR, "fd is NULL");
return NULL;
}
LOCK (&inode->lock);
{
list_add (&fd->inode_list, &inode->fd_list);
}
UNLOCK (&inode->lock);
return fd;
}
fd_t *
fd_create (inode_t *inode, pid_t pid)
{
fd_t *fd = NULL;
if (inode == NULL) {
gf_log ("fd", GF_LOG_ERROR, "invalid argument");
return NULL;
}
fd = CALLOC (1, sizeof (fd_t));
ERR_ABORT (fd);
fd->_ctx = CALLOC (1, (sizeof (struct _fd_ctx) *
inode->table->xl->ctx->xl_count));
fd->inode = inode_ref (inode);
fd->pid = pid;
INIT_LIST_HEAD (&fd->inode_list);
LOCK_INIT (&fd->lock);
LOCK (&inode->lock);
fd = _fd_ref (fd);
UNLOCK (&inode->lock);
return fd;
}
fd_t *
fd_lookup (inode_t *inode, pid_t pid)
{
fd_t *fd = NULL;
fd_t *iter_fd = NULL;
LOCK (&inode->lock);
{
if (list_empty (&inode->fd_list)) {
fd = NULL;
} else {
list_for_each_entry (iter_fd, &inode->fd_list, inode_list) {
if (pid) {
if (iter_fd->pid == pid) {
fd = _fd_ref (iter_fd);
break;
}
} else {
fd = _fd_ref (iter_fd);
break;
}
}
}
}
UNLOCK (&inode->lock);
return fd;
}
uint8_t
fd_list_empty (inode_t *inode)
{
uint8_t empty = 0;
LOCK (&inode->lock);
{
empty = list_empty (&inode->fd_list);
}
UNLOCK (&inode->lock);
return empty;
}
int
__fd_ctx_set (fd_t *fd, xlator_t *xlator, uint64_t value)
{
int index = 0;
int ret = 0;
int set_idx = -1;
if (!fd || !xlator)
return -1;
for (index = 0; index < xlator->ctx->xl_count; index++) {
if (!fd->_ctx[index].key) {
if (set_idx == -1)
set_idx = index;
/* dont break, to check if key already exists
further on */
}
if (fd->_ctx[index].key == (uint64_t)(long) xlator) {
set_idx = index;
break;
}
}
if (set_idx == -1) {
ret = -1;
goto out;
}
fd->_ctx[set_idx].key = (uint64_t)(long) xlator;
fd->_ctx[set_idx].value = value;
out:
return ret;
}
int
fd_ctx_set (fd_t *fd, xlator_t *xlator, uint64_t value)
{
int ret = 0;
if (!fd || !xlator)
return -1;
LOCK (&fd->lock);
{
ret = __fd_ctx_set (fd, xlator, value);
}
UNLOCK (&fd->lock);
return ret;
}
int
__fd_ctx_get (fd_t *fd, xlator_t *xlator, uint64_t *value)
{
int index = 0;
int ret = 0;
if (!fd || !xlator)
return -1;
for (index = 0; index < xlator->ctx->xl_count; index++) {
if (fd->_ctx[index].key == (uint64_t)(long)xlator)
break;
}
if (index == xlator->ctx->xl_count) {
ret = -1;
goto out;
}
if (value)
*value = fd->_ctx[index].value;
out:
return ret;
}
int
fd_ctx_get (fd_t *fd, xlator_t *xlator, uint64_t *value)
{
int ret = 0;
if (!fd || !xlator)
return -1;
LOCK (&fd->lock);
{
ret = __fd_ctx_get (fd, xlator, value);
}
UNLOCK (&fd->lock);
return ret;
}
int
__fd_ctx_del (fd_t *fd, xlator_t *xlator, uint64_t *value)
{
int index = 0;
int ret = 0;
if (!fd || !xlator)
return -1;
for (index = 0; index < xlator->ctx->xl_count; index++) {
if (fd->_ctx[index].key == (uint64_t)(long)xlator)
break;
}
if (index == xlator->ctx->xl_count) {
ret = -1;
goto out;
}
if (value)
*value = fd->_ctx[index].value;
fd->_ctx[index].key = 0;
fd->_ctx[index].value = 0;
out:
return ret;
}
int
fd_ctx_del (fd_t *fd, xlator_t *xlator, uint64_t *value)
{
int ret = 0;
if (!fd || !xlator)
return -1;
LOCK (&fd->lock);
{
ret = __fd_ctx_del (fd, xlator, value);
}
UNLOCK (&fd->lock);
return ret;
}
void
fd_dump (fd_t *fd, char *prefix)
{
char key[GF_DUMP_MAX_BUF_LEN];
if (!fd)
return;
memset(key, 0, sizeof(key));
gf_proc_dump_build_key(key, prefix, "pid");
gf_proc_dump_write(key, "%d", fd->pid);
gf_proc_dump_build_key(key, prefix, "refcount");
gf_proc_dump_write(key, "%d", fd->refcount);
gf_proc_dump_build_key(key, prefix, "flags");
gf_proc_dump_write(key, "%d", fd->flags);
if (fd->inode) {
gf_proc_dump_build_key(key, prefix, "inode");
gf_proc_dump_write(key, "%ld", fd->inode->ino);
}
}
void
fdentry_dump (fdentry_t *fdentry, char *prefix)
{
if (!fdentry)
return;
if (GF_FDENTRY_ALLOCATED != fdentry->next_free)
return;
if (fdentry->fd)
fd_dump(fdentry->fd, prefix);
}
void
fdtable_dump (fdtable_t *fdtable, char *prefix)
{
char key[GF_DUMP_MAX_BUF_LEN];
int i = 0;
int ret = -1;
if (!fdtable)
return;
ret = pthread_mutex_trylock (&fdtable->lock);
if (ret) {
gf_log ("fd", GF_LOG_WARNING, "Unable to acquire lock");
return;
}
memset(key, 0, sizeof(key));
gf_proc_dump_build_key(key, prefix, "refcount");
gf_proc_dump_write(key, "%d", fdtable->refcount);
gf_proc_dump_build_key(key, prefix, "maxfds");
gf_proc_dump_write(key, "%d", fdtable->max_fds);
gf_proc_dump_build_key(key, prefix, "first_free");
gf_proc_dump_write(key, "%d", fdtable->first_free);
for ( i = 0 ; i < fdtable->max_fds; i++) {
if (GF_FDENTRY_ALLOCATED ==
fdtable->fdentries[i].next_free) {
gf_proc_dump_build_key(key, prefix, "fdentry[%d]", i);
gf_proc_dump_add_section(key);
fdentry_dump(&fdtable->fdentries[i], key);
}
}
pthread_mutex_unlock(&fdtable->lock);
}
|