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
);
106 call_rcu(&inode
->rcu_head
, lttng_inode_delete
);
110 void lttng_inode_release(struct urcu_ref
*ref
)
112 lttng_inode_destroy(caa_container_of(ref
, struct lttng_inode
, ref
));
116 void lttng_inode_get(struct lttng_inode
*inode
)
118 urcu_ref_get(&inode
->ref
);
121 void lttng_inode_put(struct lttng_inode
*inode
)
123 urcu_ref_put(&inode
->ref
, lttng_inode_release
);
126 const char *lttng_inode_get_path(const struct lttng_inode
*inode
)
131 int lttng_inode_rename(struct lttng_inode
*inode
, const char *new_path
,
135 char *new_path_copy
= NULL
;
137 if (inode
->unlink_pending
) {
138 WARN("An attempt to rename an unlinked file, %s to %s, has been performed",
139 inode
->path
, new_path
);
147 ret
= stat(new_path
, &statbuf
);
151 } else if (ret
< 0 && errno
!= ENOENT
) {
152 PERROR("Failed to stat() %s", new_path
);
158 new_path_copy
= strdup(new_path
);
159 if (!new_path_copy
) {
160 ERR("Failed to allocate storage for path %s", new_path
);
165 ret
= rename(inode
->path
, new_path
);
167 PERROR("Failed to rename %s to %s", inode
->path
, new_path
);
173 inode
->path
= new_path_copy
;
174 new_path_copy
= NULL
;
180 int lttng_inode_defer_unlink(struct lttng_inode
*inode
)
184 char suffix
[sizeof("-deleted-65535")] = "-deleted";
185 char new_path
[LTTNG_PATH_MAX
];
186 size_t original_path_len
= strlen(inode
->path
);
188 if (inode
->unlink_pending
) {
189 WARN("An attempt to re-unlink %s has been performed, ignoring.",
195 ret
= lttng_strncpy(new_path
, inode
->path
, sizeof(new_path
));
201 for (i
= 0; i
< UINT16_MAX
; i
++) {
205 p_ret
= snprintf(suffix
, sizeof(suffix
), "-deleted-%" PRIu16
, i
);
208 PERROR("Failed to form suffix to rename file %s",
213 assert(p_ret
!= sizeof(suffix
));
215 /* suffix is initialy set to '-deleted'. */
216 p_ret
= strlen(suffix
);
219 if (original_path_len
+ p_ret
+ 1 >= sizeof(new_path
)) {
224 strcat(&new_path
[original_path_len
], suffix
);
225 ret
= lttng_inode_rename(inode
, new_path
, false);
226 if (ret
!= -EEXIST
) {
229 new_path
[original_path_len
] = '\0';
232 inode
->unlink_pending
= true;
239 struct lttng_inode
*lttng_inode_create(const struct inode_id
*id
,
240 const char *path
, struct cds_lfht
*ht
)
242 struct lttng_inode
*inode
= zmalloc(sizeof(*inode
));
248 urcu_ref_init(&inode
->ref
);
249 cds_lfht_node_init(&inode
->registry_node
);
251 inode
->path
= strdup(path
);
258 lttng_inode_destroy(inode
);
262 struct lttng_inode_registry
*lttng_inode_registry_create(void)
264 struct lttng_inode_registry
*registry
= zmalloc(sizeof(*registry
));
270 pthread_mutex_lock(&seed
.lock
);
271 if (!seed
.initialized
) {
272 seed
.value
= (unsigned long) time(NULL
);
273 seed
.initialized
= true;
275 pthread_mutex_unlock(&seed
.lock
);
277 registry
->inodes
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
278 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
279 if (!registry
->inodes
) {
285 lttng_inode_registry_destroy(registry
);
289 void lttng_inode_registry_destroy(struct lttng_inode_registry
*registry
)
294 if (registry
->inodes
) {
295 int ret
= cds_lfht_destroy(registry
->inodes
, NULL
);
302 struct lttng_inode
*lttng_inode_registry_get_inode(
303 struct lttng_inode_registry
*registry
,
304 int fd
, const char *path
)
309 struct cds_lfht_iter iter
;
310 struct cds_lfht_node
*node
;
311 struct lttng_inode
*inode
= NULL
;
313 ret
= fstat(fd
, &statbuf
);
315 PERROR("stat() failed on file %s, fd = %i", path
, fd
);
319 id
.device
= statbuf
.st_dev
;
320 id
.inode
= statbuf
.st_ino
;
323 cds_lfht_lookup(registry
->inodes
,
324 lttng_inode_id_hash(&id
),
328 node
= cds_lfht_iter_get_node(&iter
);
330 inode
= caa_container_of(node
, struct lttng_inode
, registry_node
);
331 /* Renames should happen through the fs-handle interface. */
332 assert(!strcmp(path
, inode
->path
));
333 lttng_inode_get(inode
);
337 inode
= lttng_inode_create(&id
, path
, registry
->inodes
);
338 node
= cds_lfht_add_unique(registry
->inodes
,
339 lttng_inode_id_hash(&inode
->id
),
342 &inode
->registry_node
);
343 assert(node
== &inode
->registry_node
);