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 <common/defaults.h>
19 #include <common/error.h>
20 #include <common/hashtable/utils.h>
21 #include <common/macros.h>
23 #include <lttng/constant.h>
25 #include <sys/types.h>
28 #include <urcu/rculfhash.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
,
63 static unsigned long lttng_inode_id_hash(struct inode_id
*id
)
65 uint64_t device
= id
->device
, inode_no
= id
->inode
;
67 return hash_key_u64(&device
, seed
.value
) ^
68 hash_key_u64(&inode_no
, seed
.value
);
71 static int lttng_inode_match(struct cds_lfht_node
*node
, const void *key
)
73 const struct inode_id
*id
= key
;
74 struct lttng_inode
*inode
= caa_container_of(
75 node
, struct lttng_inode
, registry_node
);
77 return inode
->id
.device
== id
->device
&& inode
->id
.inode
== id
->inode
;
80 static void lttng_inode_delete(struct rcu_head
*head
)
82 struct lttng_inode
*inode
=
83 caa_container_of(head
, struct lttng_inode
, rcu_head
);
89 static void lttng_inode_destroy(struct lttng_inode
*inode
)
94 if (inode
->unlink_pending
) {
95 int ret
= unlink(inode
->path
);
97 DBG("Unlinking %s during lttng_inode destruction", inode
->path
);
99 PERROR("Failed to unlink %s", inode
->path
);
103 cds_lfht_del(inode
->registry_ht
, &inode
->registry_node
);
105 call_rcu(&inode
->rcu_head
, lttng_inode_delete
);
108 static void lttng_inode_release(struct urcu_ref
*ref
)
110 lttng_inode_destroy(caa_container_of(ref
, struct lttng_inode
, ref
));
113 static void lttng_inode_get(struct lttng_inode
*inode
)
115 urcu_ref_get(&inode
->ref
);
118 void lttng_inode_put(struct lttng_inode
*inode
)
120 urcu_ref_put(&inode
->ref
, lttng_inode_release
);
123 const char *lttng_inode_get_path(const struct lttng_inode
*inode
)
128 int lttng_inode_rename(
129 struct lttng_inode
*inode
, const char *new_path
, bool overwrite
)
132 char *new_path_copy
= NULL
;
134 if (inode
->unlink_pending
) {
135 WARN("An attempt to rename an unlinked file, %s to %s, has been performed",
136 inode
->path
, new_path
);
144 ret
= stat(new_path
, &statbuf
);
148 } else if (ret
< 0 && errno
!= ENOENT
) {
149 PERROR("Failed to stat() %s", new_path
);
155 new_path_copy
= strdup(new_path
);
156 if (!new_path_copy
) {
157 ERR("Failed to allocate storage for path %s", new_path
);
162 ret
= rename(inode
->path
, new_path
);
164 PERROR("Failed to rename %s to %s", inode
->path
, new_path
);
170 inode
->path
= new_path_copy
;
171 new_path_copy
= NULL
;
177 int lttng_inode_defer_unlink(struct lttng_inode
*inode
)
181 char suffix
[sizeof("-deleted-65535")] = "-deleted";
182 char new_path
[LTTNG_PATH_MAX
];
183 size_t original_path_len
= strlen(inode
->path
);
185 if (inode
->unlink_pending
) {
186 WARN("An attempt to re-unlink %s has been performed, ignoring.",
192 ret
= lttng_strncpy(new_path
, inode
->path
, sizeof(new_path
));
198 for (i
= 0; i
< UINT16_MAX
; i
++) {
202 p_ret
= snprintf(suffix
, sizeof(suffix
),
203 "-deleted-%" PRIu16
, i
);
206 PERROR("Failed to form suffix to rename file %s",
211 assert(p_ret
!= sizeof(suffix
));
213 /* suffix is initialy set to '-deleted'. */
214 p_ret
= strlen(suffix
);
217 if (original_path_len
+ p_ret
+ 1 >= sizeof(new_path
)) {
222 strcat(&new_path
[original_path_len
], suffix
);
223 ret
= lttng_inode_rename(inode
, new_path
, false);
224 if (ret
!= -EEXIST
) {
227 new_path
[original_path_len
] = '\0';
230 inode
->unlink_pending
= true;
236 static struct lttng_inode
*lttng_inode_create(const struct inode_id
*id
,
240 struct lttng_inode
*inode
= zmalloc(sizeof(*inode
));
246 urcu_ref_init(&inode
->ref
);
247 cds_lfht_node_init(&inode
->registry_node
);
249 inode
->path
= strdup(path
);
250 inode
->registry_ht
= ht
;
257 lttng_inode_destroy(inode
);
261 struct lttng_inode_registry
*lttng_inode_registry_create(void)
263 struct lttng_inode_registry
*registry
= zmalloc(sizeof(*registry
));
269 pthread_mutex_lock(&seed
.lock
);
270 if (!seed
.initialized
) {
271 seed
.value
= (unsigned long) time(NULL
);
272 seed
.initialized
= true;
274 pthread_mutex_unlock(&seed
.lock
);
276 registry
->inodes
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
277 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, NULL
);
278 if (!registry
->inodes
) {
284 lttng_inode_registry_destroy(registry
);
288 void lttng_inode_registry_destroy(struct lttng_inode_registry
*registry
)
293 if (registry
->inodes
) {
294 int ret
= cds_lfht_destroy(registry
->inodes
, NULL
);
301 struct lttng_inode
*lttng_inode_registry_get_inode(
302 struct lttng_inode_registry
*registry
, int fd
, const char *path
)
307 struct cds_lfht_iter iter
;
308 struct cds_lfht_node
*node
;
309 struct lttng_inode
*inode
= NULL
;
311 ret
= fstat(fd
, &statbuf
);
313 PERROR("stat() failed on file %s, fd = %i", path
, fd
);
317 id
.device
= statbuf
.st_dev
;
318 id
.inode
= statbuf
.st_ino
;
321 cds_lfht_lookup(registry
->inodes
, lttng_inode_id_hash(&id
),
322 lttng_inode_match
, &id
, &iter
);
323 node
= cds_lfht_iter_get_node(&iter
);
325 inode
= caa_container_of(
326 node
, struct lttng_inode
, registry_node
);
327 /* Renames should happen through the fs-handle interface. */
328 assert(!strcmp(path
, inode
->path
));
329 lttng_inode_get(inode
);
333 inode
= lttng_inode_create(&id
, path
, registry
->inodes
);
334 node
= cds_lfht_add_unique(registry
->inodes
,
335 lttng_inode_id_hash(&inode
->id
), lttng_inode_match
,
336 &inode
->id
, &inode
->registry_node
);
337 assert(node
== &inode
->registry_node
);