2
Copyright (c) 2007-2009 Gluster, Inc. <http://www.gluster.com>
3
This file is part of GlusterFS.
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.
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.
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/>.
2
Copyright (c) 2007-2009 Gluster, Inc. <http://www.gluster.com>
3
This file is part of GlusterFS.
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.
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.
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/>.
235
275
__inode_destroy (inode_t *inode)
238
data_pair_t *pair = NULL;
239
278
xlator_t *xl = NULL;
279
xlator_t *old_THIS = NULL;
244
for (pair = inode->ctx->members_list; pair; pair = pair->next) {
245
/* notify all xlators which have a context */
246
xl = xlator_search_by_name (inode->table->xl, pair->key);
249
gf_log (inode->table->name, GF_LOG_DEBUG,
250
"inode(%"PRId64")->ctx has invalid key(%s)",
251
inode->ino, pair->key);
284
for (index = 0; index < inode->table->xl->ctx->xl_count; index++) {
285
if (inode->_ctx[index].key) {
286
xl = (xlator_t *)(long)inode->_ctx[index].key;
289
if (xl->cbks->forget)
290
xl->cbks->forget (xl, inode);
255
if (xl->cbks->forget)
256
xl->cbks->forget (xl, inode);
258
gf_log (inode->table->name, GF_LOG_DEBUG,
259
"xlator(%s) in inode(%"PRId64") no FORGET fop",
260
xl->name, inode->ino);
262
dict_destroy (inode->ctx);
267
for (index = 0; index < inode->table->xl->ctx->xl_count; index++) {
268
if (inode->_ctx[index].key) {
269
xl = (xlator_t *)(long)inode->_ctx[index].key;
270
if (xl->cbks->forget)
271
xl->cbks->forget (xl, inode);
277
297
LOCK_DESTROY (&inode->lock);
278
298
// memset (inode, 0xb, sizeof (*inode));
527
__copy_dentries (inode_t *oldi, inode_t *newi)
561
__dentry_grep (inode_table_t *table, inode_t *parent, const char *name)
529
564
dentry_t *dentry = NULL;
530
dentry_t *newd = NULL;
531
565
dentry_t *tmp = NULL;
533
list_for_each_entry (dentry, &oldi->dentry_list, inode_list) {
534
tmp = __dentry_search_for_inode (newi, dentry->parent->ino,
538
newd = __dentry_create (newi, dentry->parent,
544
if (__is_dentry_hashed (dentry)) {
545
__dentry_unhash (dentry);
546
__dentry_hash (newd);
553
__adopt_children (inode_t *oldi, inode_t *newi)
555
dentry_t *dentry = NULL;
557
list_for_each_entry (dentry, &oldi->child_list, parent_list) {
558
assert (dentry->parent == oldi);
559
__inode_unref (dentry->parent);
560
dentry->parent = __inode_ref (newi);
563
list_splice_init (&oldi->child_list, &newi->child_list);
568
__inode_replace (inode_t *oldi, inode_t *newi)
570
gf_log (oldi->table->name, GF_LOG_DEBUG,
571
"inode(%"PRId64") replaced (%"PRId64"",
572
oldi->ino, newi->ino);
574
__copy_dentries (oldi, newi);
575
__adopt_children (oldi, newi);
577
newi->nlookup = oldi->nlookup;
578
newi->generation = oldi->generation;
581
oldi->generation = 0;
583
__inode_unhash (oldi);
586
newi->table->root = newi;
567
hash = hash_dentry (parent, name, table->hashsize);
569
list_for_each_entry (tmp, &table->name_hash[hash], hash) {
570
if (tmp->parent == parent && !strcmp (tmp->name, name)) {
581
inode_grep (inode_table_t *table, inode_t *parent, const char *name)
583
inode_t *inode = NULL;
584
dentry_t *dentry = NULL;
586
pthread_mutex_lock (&table->lock);
588
dentry = __dentry_grep (table, parent, name);
591
inode = dentry->inode;
596
pthread_mutex_unlock (&table->lock);
603
__inode_get (inode_table_t *table, ino_t ino, uint64_t gen)
605
inode_t *inode = NULL;
612
inode = __inode_search (table, ino);
615
if (!inode || inode->generation != gen) {
616
inode = __inode_search_attic (table, ino, gen);
626
inode_get (inode_table_t *table, ino_t ino, uint64_t gen)
628
inode_t *inode = NULL;
630
pthread_mutex_lock (&table->lock);
632
inode = __inode_get (table, ino, gen);
636
pthread_mutex_unlock (&table->lock);
643
__inode_atticize (inode_t *inode)
645
inode_table_t *table = NULL;
647
table = inode->table;
649
__inode_unhash (inode);
651
list_add (&inode->hash, &table->attic);
660
inode_gen_from_stat (struct stat *stbuf)
662
return (uint64_t) stbuf->st_dev;
591
__inode_link (inode_t *inode,
667
__inode_link (inode_t *inode, inode_t *parent, const char *name,
594
668
struct stat *stbuf)
596
670
dentry_t *dentry = NULL;
597
671
dentry_t *old_dentry = NULL;
598
672
inode_t *old_inode = NULL;
599
673
inode_table_t *table = NULL;
674
inode_t *link_inode = NULL;
601
676
table = inode->table;
604
assert (inode->ino == stbuf->st_ino);
606
inode->ino = stbuf->st_ino;
607
inode->st_mode = stbuf->st_mode;
609
old_inode = __inode_search (table, stbuf->st_ino);
611
if (old_inode && old_inode != inode) {
612
__inode_ref (old_inode);
613
__inode_replace (old_inode, inode);
614
__inode_unref (old_inode);
680
if (!__is_inode_hashed (inode)) {
681
inode->ino = stbuf->st_ino;
682
inode->st_mode = stbuf->st_mode;
683
inode->generation = inode_gen_from_stat (stbuf);
685
old_inode = __inode_search (table, inode->ino);
688
if (old_inode->generation < inode->generation) {
689
__inode_atticize (old_inode);
690
__inode_hash (inode);
692
link_inode = old_inode;
695
__inode_hash (inode);
616
__inode_hash (inode);
699
/* use only link_inode beyond this point */
619
dentry = __dentry_search_for_inode (inode, parent->ino, name);
621
dentry = __dentry_create (inode, parent, name);
624
old_dentry = __dentry_search (table, parent->ino, name);
626
__dentry_unhash (old_dentry);
629
__dentry_hash (dentry);
630
} else if (inode->ino != 1) {
631
gf_log (table->name, GF_LOG_DEBUG,
632
"child (%"PRId64") without a parent!", inode->ino);
701
old_dentry = __dentry_grep (table, parent, name);
703
if (!old_dentry || old_dentry->inode != link_inode) {
704
dentry = __dentry_create (link_inode, parent, name);
705
__dentry_hash (dentry);
708
__dentry_unset (old_dentry);
640
inode_link (inode_t *inode,
717
inode_link (inode_t *inode, inode_t *parent, const char *name,
643
718
struct stat *stbuf)
645
720
inode_table_t *table = NULL;
721
inode_t *linked_inode = NULL;
647
723
table = inode->table;
649
725
pthread_mutex_lock (&table->lock);
651
inode = __inode_link (inode, parent, name, stbuf);
727
linked_inode = __inode_link (inode, parent, name, stbuf);
730
__inode_ref (linked_inode);
653
732
pthread_mutex_unlock (&table->lock);
655
734
inode_table_prune (table);
714
__inode_unlink (inode_t *inode,
777
__inode_unlink (inode_t *inode, inode_t *parent, const char *name)
718
779
dentry_t *dentry = NULL;
720
781
dentry = __dentry_search_for_inode (inode, parent->ino, name);
722
/* dentry NULL for corrupted backend */
724
__dentry_unset (dentry);
783
/* dentry NULL for corrupted backend */
785
__dentry_unset (dentry);
729
inode_unlink (inode_t *inode,
790
inode_unlink (inode_t *inode, inode_t *parent, const char *name)
733
792
inode_table_t *table = NULL;
734
inode_t *unlink_inode = NULL;
736
797
table = inode->table;
737
unlink_inode = inode;
739
799
pthread_mutex_lock (&table->lock);
741
if (!__is_inode_hashed (inode)) {
742
unlink_inode = __inode_search (table, inode->ino);
743
if (unlink_inode == NULL)
744
unlink_inode = inode;
747
__inode_unlink (unlink_inode, parent, name);
801
__inode_unlink (inode, parent, name);
749
803
pthread_mutex_unlock (&table->lock);
756
inode_rename (inode_table_t *table,
810
inode_rename (inode_table_t *table, inode_t *srcdir, const char *srcname,
811
inode_t *dstdir, const char *dstname, inode_t *inode,
762
812
struct stat *stbuf)
764
dentry_t *old_dst = NULL;
765
inode_t *rename_inode = NULL;
767
rename_inode = inode;
814
table = inode->table;
769
816
pthread_mutex_lock (&table->lock);
771
if (!__is_inode_hashed (inode)) {
772
rename_inode = __inode_search (table, inode->ino);
773
if (rename_inode == NULL)
774
rename_inode = inode;
777
old_dst = __dentry_search (table, dstdir->ino, dstname);
779
__dentry_unset (old_dst);
781
__inode_unlink (rename_inode, srcdir, srcname);
782
__inode_link (rename_inode, dstdir, dstname, stbuf);
818
__inode_link (inode, dstdir, dstname, stbuf);
819
__inode_unlink (inode, srcdir, srcname);
784
821
pthread_mutex_unlock (&table->lock);
1052
1095
inode_from_path (inode_table_t *itable, const char *path)
1054
inode_t *inode = NULL;
1055
inode_t *parent = NULL;
1056
inode_t *root = NULL;
1057
inode_t *curr = NULL;
1058
char *pathname = NULL;
1059
char *component = NULL, *next_component = NULL;
1060
char *strtokptr = NULL;
1062
/* top-down approach */
1063
root = itable->root;
1064
parent = inode_ref (root);
1065
pathname = strdup (path);
1066
component = strtok_r (pathname, "/", &strtokptr);
1068
if (component == NULL)
1070
inode = inode_ref (parent);
1073
curr = inode_search (itable, parent->ino, component);
1076
component = strtok_r (NULL, "/", &strtokptr);
1080
next_component = strtok_r (NULL, "/", &strtokptr);
1082
if (next_component) {
1083
inode_unref (parent);
1090
component = next_component;
1094
inode_unref (parent);
1097
inode_t *inode = NULL;
1098
inode_t *parent = NULL;
1099
inode_t *root = NULL;
1100
inode_t *curr = NULL;
1101
char *pathname = NULL;
1102
char *component = NULL, *next_component = NULL;
1103
char *strtokptr = NULL;
1105
/* top-down approach */
1106
pathname = strdup (path);
1107
if (pathname == NULL) {
1108
gf_log ("inode", GF_LOG_ERROR, "out of memory");
1112
root = itable->root;
1113
parent = inode_ref (root);
1114
component = strtok_r (pathname, "/", &strtokptr);
1116
if (component == NULL)
1118
inode = inode_ref (parent);
1121
curr = inode_grep (itable, parent, component);
1124
component = strtok_r (NULL, "/", &strtokptr);
1128
next_component = strtok_r (NULL, "/", &strtokptr);
1130
if (next_component) {
1131
inode_unref (parent);
1138
component = next_component;
1142
inode_unref (parent);
1103
__inode_ctx_put (inode_t *inode, xlator_t *xlator, uint64_t value)
1153
__inode_ctx_put2 (inode_t *inode, xlator_t *xlator, uint64_t value1,
1113
1164
/* dont break, to check if key already exists
1116
if (inode->_ctx[index].key == (uint64_t)(long) xlator) {
1167
if (inode->_ctx[index].xl_key == xlator) {
1117
1168
put_idx = index;
1122
1173
if (put_idx == -1) {
1127
inode->_ctx[put_idx].key = (uint64_t)(long) xlator;
1128
inode->_ctx[put_idx].value = value;
1178
inode->_ctx[put_idx].xl_key = xlator;
1179
inode->_ctx[put_idx].value1 = value1;
1180
inode->_ctx[put_idx].value2 = value2;
1134
inode_ctx_put (inode_t *inode, xlator_t *xlator, uint64_t value)
1187
inode_ctx_put2 (inode_t *inode, xlator_t *xlator, uint64_t value1,
1138
if (!inode || !xlator)
1192
if (!inode || !xlator)
1141
1195
LOCK (&inode->lock);
1143
ret = __inode_ctx_put (inode, xlator, value);
1197
ret = __inode_ctx_put2 (inode, xlator, value1, value2);
1145
1199
UNLOCK (&inode->lock);
1151
__inode_ctx_get (inode_t *inode, xlator_t *xlator, uint64_t *value)
1206
__inode_ctx_get2 (inode_t *inode, xlator_t *xlator, uint64_t *value1,
1155
1212
for (index = 0; index < xlator->ctx->xl_count; index++) {
1156
if (inode->_ctx[index].key == (uint64_t)(long)xlator)
1213
if (inode->_ctx[index].xl_key == xlator)
1212
*value = inode->_ctx[index].value;
1275
*value1 = inode->_ctx[index].value1;
1277
*value2 = inode->_ctx[index].value2;
1214
inode->_ctx[index].key = 0;
1215
inode->_ctx[index].value = 0;
1279
inode->_ctx[index].key = 0;
1280
inode->_ctx[index].value1 = 0;
1281
inode->_ctx[index].value2 = 0;
1218
1284
UNLOCK (&inode->lock);
1291
__inode_ctx_put (inode_t *inode, xlator_t *key, uint64_t value)
1293
return __inode_ctx_put2 (inode, key, value, 0);
1298
inode_ctx_put (inode_t *inode, xlator_t *key, uint64_t value)
1300
return inode_ctx_put2 (inode, key, value, 0);
1305
__inode_ctx_get (inode_t *inode, xlator_t *key, uint64_t *value)
1307
return __inode_ctx_get2 (inode, key, value, 0);
1312
inode_ctx_get (inode_t *inode, xlator_t *key, uint64_t *value)
1314
return inode_ctx_get2 (inode, key, value, 0);
1319
inode_ctx_del (inode_t *inode, xlator_t *key, uint64_t *value)
1321
return inode_ctx_del2 (inode, key, value, 0);
1326
inode_dump (inode_t *inode, char *prefix)
1328
char key[GF_DUMP_MAX_BUF_LEN];
1330
xlator_t *xl = NULL;
1336
ret = TRY_LOCK(&inode->lock);
1339
gf_log("", GF_LOG_WARNING, "Unable to dump inode"
1340
" errno: %d", errno);
1344
gf_proc_dump_build_key(key, prefix, "nlookup");
1345
gf_proc_dump_write(key, "%ld", inode->nlookup);
1346
gf_proc_dump_build_key(key, prefix, "generation");
1347
gf_proc_dump_write(key, "%ld", inode->generation);
1348
gf_proc_dump_build_key(key, prefix, "ref");
1349
gf_proc_dump_write(key, "%u", inode->ref);
1350
gf_proc_dump_build_key(key, prefix, "ino");
1351
gf_proc_dump_write(key, "%ld", inode->ino);
1352
gf_proc_dump_build_key(key, prefix, "st_mode");
1353
gf_proc_dump_write(key, "%d", inode->st_mode);
1354
UNLOCK(&inode->lock);
1358
for (i = 0; i < inode->table->xl->ctx->xl_count; i++) {
1359
if (inode->_ctx[i].key) {
1360
xl = (xlator_t *)(long)inode->_ctx[i].key;
1361
if (xl->dumpops && xl->dumpops->inodectx)
1362
xl->dumpops->inodectx (xl, inode);
1371
inode_table_dump (inode_table_t *itable, char *prefix)
1374
char key[GF_DUMP_MAX_BUF_LEN];
1380
memset(key, 0, sizeof(key));
1381
ret = pthread_mutex_trylock(&itable->lock);
1384
gf_log("", GF_LOG_WARNING, "Unable to dump inode table"
1385
" errno: %d", errno);
1389
gf_proc_dump_build_key(key, prefix, "hashsize");
1390
gf_proc_dump_write(key, "%d", itable->hashsize);
1391
gf_proc_dump_build_key(key, prefix, "name");
1392
gf_proc_dump_write(key, "%s", itable->name);
1394
gf_proc_dump_build_key(key, prefix, "lru_limit");
1395
gf_proc_dump_write(key, "%d", itable->lru_limit);
1396
gf_proc_dump_build_key(key, prefix, "active_size");
1397
gf_proc_dump_write(key, "%d", itable->active_size);
1398
gf_proc_dump_build_key(key, prefix, "lru_size");
1399
gf_proc_dump_write(key, "%d", itable->lru_size);
1400
gf_proc_dump_build_key(key, prefix, "purge_size");
1401
gf_proc_dump_write(key, "%d", itable->purge_size);
1403
INODE_DUMP_LIST(&itable->active, key, prefix, "active");
1404
INODE_DUMP_LIST(&itable->lru, key, prefix, "lru");
1405
INODE_DUMP_LIST(&itable->purge, key, prefix, "purge");
1407
pthread_mutex_unlock(&itable->lock);