2 * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 #include <sys/types.h>
24 #include <urcu/rculfhash.h>
25 #include <common/hashtable/utils.h>
26 #include <common/macros.h>
27 #include <common/defaults.h>
28 #include <common/error.h>
29 #include <lttng/constant.h>
38 struct lttng_inode_registry
{
39 /* Hashtable of inode_id to lttng_inode. */
40 struct cds_lfht
*inodes
;
47 /* Node in the lttng_inode_registry's ht. */
48 struct cds_lfht_node registry_node
;
49 /* Weak reference to ht containing the node. */
50 struct cds_lfht
*registry_ht
;
52 struct rcu_head rcu_head
;
60 .lock
= PTHREAD_MUTEX_INITIALIZER
,
64 unsigned long lttng_inode_id_hash(struct inode_id
*id
)
66 uint64_t device
= id
->device
, inode_no
= id
->inode
;
68 return hash_key_u64(&device
, seed
.value
) ^
69 hash_key_u64(&inode_no
, seed
.value
);
73 int lttng_inode_match(struct cds_lfht_node
*node
, const void *key
)
75 const struct inode_id
*id
= key
;
76 struct lttng_inode
*inode
= caa_container_of(node
, struct lttng_inode
,
79 return inode
->id
.device
== id
->device
&& inode
->id
.inode
== id
->inode
;
83 void lttng_inode_delete(struct rcu_head
*head
)
85 struct lttng_inode
*inode
= caa_container_of(head
,
86 struct lttng_inode
, rcu_head
);
93 void lttng_inode_destroy(struct lttng_inode
*inode
)
98 if (inode
->unlink_pending
) {
99 int ret
= unlink(inode
->path
);
101 DBG("Unlinking %s during lttng_inode destruction", inode
->path
);
103 PERROR("Failed to unlink %s", inode
->path
);
107 cds_lfht_del(inode
->registry_ht
, &inode
->registry_node
);
109 call_rcu(&inode
->rcu_head
, lttng_inode_delete
);
113 void lttng_inode_release(struct urcu_ref
*ref
)
115 lttng_inode_destroy(caa_container_of(ref
, struct lttng_inode
, ref
));
119 void lttng_inode_get(struct lttng_inode
*inode
)
121 urcu_ref_get(&inode
->ref
);
124 void lttng_inode_put(struct lttng_inode
*inode
)
126 urcu_ref_put(&inode
->ref
, lttng_inode_release
);
129 const char *lttng_inode_get_path(const struct lttng_inode
*inode
)
134 int lttng_inode_rename(struct lttng_inode
*inode
, const char *new_path
,
138 char *new_path_copy
= NULL
;
140 if (inode
->unlink_pending
) {
141 WARN("An attempt to rename an unlinked file, %s to %s, has been performed",
142 inode
->path
, new_path
);
150 ret
= stat(new_path
, &statbuf
);
154 } else if (ret
< 0 && errno
!= ENOENT
) {
155 PERROR("Failed to stat() %s", new_path
);
161 new_path_copy
= strdup(new_path
);
162 if (!new_path_copy
) {
163 ERR("Failed to allocate storage for path %s", new_path
);
168 ret
= rename(inode
->path
, new_path
);
170 PERROR("Failed to rename %s to %s", inode
->path
, new_path
);
176 inode
->path
= new_path_copy
;
177 new_path_copy
= NULL
;
183 int lttng_inode_defer_unlink(struct lttng_inode
*inode
)
187 char suffix
[sizeof("-deleted-65535")] = "-deleted";
188 char new_path
[LTTNG_PATH_MAX
];
189 size_t original_path_len
= strlen(inode
->path
);
191 if (inode
->unlink_pending
) {
192 WARN("An attempt to re-unlink %s has been performed, ignoring.",
198 ret
= lttng_strncpy(new_path
, inode
->path
, sizeof(new_path
));
204 for (i
= 0; i
< UINT16_MAX
; i
++) {
208 p_ret
= snprintf(suffix
, sizeof(suffix
), "-deleted-%" PRIu16
, i
);
211 PERROR("Failed to form suffix to rename file %s",
216 assert(p_ret
!= sizeof(suffix
));
218 /* suffix is initialy set to '-deleted'. */
219 p_ret
= strlen(suffix
);
222 if (original_path_len
+ p_ret
+ 1 >= sizeof(new_path
)) {
227 strcat(&new_path
[original_path_len
], suffix
);
228 ret
= lttng_inode_rename(inode
, new_path
, false);
229 if (ret
!= -EEXIST
) {
232 new_path
[original_path_len
] = '\0';
235 inode
->unlink_pending
= true;
242 struct lttng_inode
*lttng_inode_create(const struct inode_id
*id
,
243 const char *path
, struct cds_lfht
*ht
)
245 struct lttng_inode
*inode
= zmalloc(sizeof(*inode
));
251 urcu_ref_init(&inode
->ref
);
252 cds_lfht_node_init(&inode
->registry_node
);
254 inode
->path
= strdup(path
);
255 inode
->registry_ht
= ht
;
262 lttng_inode_destroy(inode
);
266 struct lttng_inode_registry
*lttng_inode_registry_create(void)
268 struct lttng_inode_registry
*registry
= zmalloc(sizeof(*registry
));
274 pthread_mutex_lock(&seed
.lock
);
275 if (!seed
.initialized
) {
276 seed
.value
= (unsigned long) time(NULL
);
277 seed
.initialized
= true;
279 pthread_mutex_unlock(&seed
.lock
);
281 registry
->inodes
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
282 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
283 if (!registry
->inodes
) {
289 lttng_inode_registry_destroy(registry
);
293 void lttng_inode_registry_destroy(struct lttng_inode_registry
*registry
)
298 if (registry
->inodes
) {
299 int ret
= cds_lfht_destroy(registry
->inodes
, NULL
);
306 struct lttng_inode
*lttng_inode_registry_get_inode(
307 struct lttng_inode_registry
*registry
,
308 int fd
, const char *path
)
313 struct cds_lfht_iter iter
;
314 struct cds_lfht_node
*node
;
315 struct lttng_inode
*inode
= NULL
;
317 ret
= fstat(fd
, &statbuf
);
319 PERROR("stat() failed on file %s, fd = %i", path
, fd
);
323 id
.device
= statbuf
.st_dev
;
324 id
.inode
= statbuf
.st_ino
;
327 cds_lfht_lookup(registry
->inodes
,
328 lttng_inode_id_hash(&id
),
332 node
= cds_lfht_iter_get_node(&iter
);
334 inode
= caa_container_of(node
, struct lttng_inode
, registry_node
);
335 /* Renames should happen through the fs-handle interface. */
336 assert(!strcmp(path
, inode
->path
));
337 lttng_inode_get(inode
);
341 inode
= lttng_inode_create(&id
, path
, registry
->inodes
);
342 node
= cds_lfht_add_unique(registry
->inodes
,
343 lttng_inode_id_hash(&inode
->id
),
346 &inode
->registry_node
);
347 assert(node
== &inode
->registry_node
);