Commit | Line | Data |
---|---|---|
e0e72660 | 1 | /* |
ab5be9fa | 2 | * Copyright (C) 2020 Jérémie Galarneau <jeremie.galarneau@efficios.com> |
e0e72660 | 3 | * |
ab5be9fa | 4 | * SPDX-License-Identifier: GPL-2.0-only |
e0e72660 | 5 | * |
e0e72660 JG |
6 | */ |
7 | ||
5c1f54d1 JG |
8 | #include <common/defaults.h> |
9 | #include <common/error.h> | |
10 | #include <common/hashtable/utils.h> | |
11 | #include <common/macros.h> | |
f7c3ffd7 JG |
12 | #include <common/optional.h> |
13 | #include <common/string-utils/format.h> | |
14 | #include <common/utils.h> | |
5c1f54d1 JG |
15 | #include <inttypes.h> |
16 | #include <lttng/constant.h> | |
e0e72660 | 17 | #include <sys/stat.h> |
5c1f54d1 | 18 | #include <sys/types.h> |
e0e72660 | 19 | #include <unistd.h> |
e0e72660 | 20 | #include <urcu.h> |
e0e72660 | 21 | #include <urcu/rculfhash.h> |
5c1f54d1 | 22 | #include <urcu/ref.h> |
e0e72660 JG |
23 | |
24 | #include "inode.h" | |
25 | ||
26 | struct inode_id { | |
27 | dev_t device; | |
28 | ino_t inode; | |
29 | }; | |
30 | ||
31 | struct lttng_inode_registry { | |
32 | /* Hashtable of inode_id to lttng_inode. */ | |
33 | struct cds_lfht *inodes; | |
34 | }; | |
35 | ||
36 | struct lttng_inode { | |
37 | struct inode_id id; | |
e0e72660 JG |
38 | /* Node in the lttng_inode_registry's ht. */ |
39 | struct cds_lfht_node registry_node; | |
40 | /* Weak reference to ht containing the node. */ | |
41 | struct cds_lfht *registry_ht; | |
42 | struct urcu_ref ref; | |
43 | struct rcu_head rcu_head; | |
f7c3ffd7 JG |
44 | /* Location from which this file can be opened. */ |
45 | struct { | |
46 | struct lttng_directory_handle *directory_handle; | |
47 | char *path; | |
48 | } location; | |
49 | /* Unlink the underlying file at the release of the inode. */ | |
50 | bool unlink_pending; | |
51 | LTTNG_OPTIONAL(unsigned int) unlinked_id; | |
52 | /* Weak reference. */ | |
53 | struct lttng_unlinked_file_pool *unlinked_file_pool; | |
54 | }; | |
55 | ||
56 | struct lttng_unlinked_file_pool { | |
57 | struct lttng_directory_handle *unlink_directory_handle; | |
58 | char *unlink_directory_path; | |
59 | unsigned int file_count; | |
60 | unsigned int next_id; | |
e0e72660 JG |
61 | }; |
62 | ||
63 | static struct { | |
64 | pthread_mutex_t lock; | |
65 | bool initialized; | |
66 | unsigned long value; | |
67 | } seed = { | |
5c1f54d1 | 68 | .lock = PTHREAD_MUTEX_INITIALIZER, |
e0e72660 JG |
69 | }; |
70 | ||
f7c3ffd7 | 71 | static unsigned long lttng_inode_id_hash(const struct inode_id *id) |
e0e72660 JG |
72 | { |
73 | uint64_t device = id->device, inode_no = id->inode; | |
74 | ||
5c1f54d1 JG |
75 | return hash_key_u64(&device, seed.value) ^ |
76 | hash_key_u64(&inode_no, seed.value); | |
e0e72660 JG |
77 | } |
78 | ||
5c1f54d1 | 79 | static int lttng_inode_match(struct cds_lfht_node *node, const void *key) |
e0e72660 JG |
80 | { |
81 | const struct inode_id *id = key; | |
f7c3ffd7 | 82 | const struct lttng_inode *inode = caa_container_of( |
5c1f54d1 | 83 | node, struct lttng_inode, registry_node); |
e0e72660 JG |
84 | |
85 | return inode->id.device == id->device && inode->id.inode == id->inode; | |
86 | } | |
87 | ||
f7c3ffd7 | 88 | static void lttng_inode_free(struct rcu_head *head) |
e0e72660 | 89 | { |
5c1f54d1 JG |
90 | struct lttng_inode *inode = |
91 | caa_container_of(head, struct lttng_inode, rcu_head); | |
e0e72660 | 92 | |
e0e72660 JG |
93 | free(inode); |
94 | } | |
95 | ||
f7c3ffd7 JG |
96 | static int lttng_unlinked_file_pool_add_inode( |
97 | struct lttng_unlinked_file_pool *pool, | |
98 | struct lttng_inode *inode) | |
99 | { | |
100 | int ret; | |
101 | const unsigned int unlinked_id = pool->next_id++; | |
102 | char *inode_unlinked_name; | |
103 | bool reference_acquired; | |
104 | ||
105 | DBG("Adding inode of %s to unlinked file pool as id %u", | |
106 | inode->location.path, unlinked_id); | |
107 | ret = asprintf(&inode_unlinked_name, "%u", unlinked_id); | |
108 | if (ret < 0) { | |
109 | ERR("Failed to format unlinked inode name"); | |
110 | ret = -1; | |
111 | goto end; | |
112 | } | |
113 | ||
114 | if (pool->file_count == 0) { | |
115 | DBG("Creating unlinked files directory at %s", | |
116 | pool->unlink_directory_path); | |
117 | assert(!pool->unlink_directory_handle); | |
118 | ret = utils_mkdir(pool->unlink_directory_path, | |
119 | S_IRWXU | S_IRWXG, -1, -1); | |
120 | if (ret) { | |
121 | if (errno == EEXIST) { | |
122 | /* | |
123 | * Unexpected (previous crash?), but not an | |
124 | * error. | |
125 | */ | |
126 | DBG("Unlinked file directory \"%s\" already exists", | |
127 | pool->unlink_directory_path); | |
128 | } else { | |
129 | PERROR("Failed to create unlinked files directory at %s", | |
130 | pool->unlink_directory_path); | |
131 | goto end; | |
132 | } | |
133 | } | |
134 | pool->unlink_directory_handle = lttng_directory_handle_create( | |
135 | pool->unlink_directory_path); | |
d05a3d93 JG |
136 | if (!pool->unlink_directory_handle) { |
137 | ERR("Failed to create directory handle to unlinked file pool at %s", | |
138 | pool->unlink_directory_path); | |
139 | ret = -1; | |
140 | goto end; | |
141 | } | |
f7c3ffd7 JG |
142 | } |
143 | ||
144 | ret = lttng_directory_handle_rename(inode->location.directory_handle, | |
145 | inode->location.path, pool->unlink_directory_handle, | |
146 | inode_unlinked_name); | |
147 | if (ret) { | |
148 | goto end; | |
149 | } | |
150 | ||
151 | lttng_directory_handle_put(inode->location.directory_handle); | |
152 | inode->location.directory_handle = NULL; | |
153 | reference_acquired = lttng_directory_handle_get( | |
154 | pool->unlink_directory_handle); | |
155 | assert(reference_acquired); | |
156 | inode->location.directory_handle = pool->unlink_directory_handle; | |
157 | ||
158 | free(inode->location.path); | |
159 | inode->location.path = inode_unlinked_name; | |
160 | inode_unlinked_name = NULL; | |
161 | LTTNG_OPTIONAL_SET(&inode->unlinked_id, unlinked_id); | |
162 | pool->file_count++; | |
163 | end: | |
164 | free(inode_unlinked_name); | |
165 | return ret; | |
166 | } | |
167 | ||
168 | static int lttng_unlinked_file_pool_remove_inode( | |
169 | struct lttng_unlinked_file_pool *pool, | |
170 | struct lttng_inode *inode) | |
171 | { | |
172 | int ret; | |
173 | ||
174 | DBG("Removing inode with unlinked id %u from unlinked file pool", | |
175 | LTTNG_OPTIONAL_GET(inode->unlinked_id)); | |
176 | ||
177 | ret = lttng_directory_handle_unlink_file( | |
178 | inode->location.directory_handle, inode->location.path); | |
179 | if (ret) { | |
180 | PERROR("Failed to unlink file %s from unlinked file directory", | |
181 | inode->location.path); | |
182 | goto end; | |
183 | } | |
184 | free(inode->location.path); | |
185 | inode->location.path = NULL; | |
186 | lttng_directory_handle_put(inode->location.directory_handle); | |
187 | inode->location.directory_handle = NULL; | |
188 | ||
189 | pool->file_count--; | |
190 | if (pool->file_count == 0) { | |
191 | ret = utils_recursive_rmdir(pool->unlink_directory_path); | |
192 | if (ret) { | |
193 | /* | |
194 | * There is nothing the caller can do, don't report an | |
195 | * error except through logging. | |
196 | */ | |
197 | PERROR("Failed to remove unlinked files directory at %s", | |
198 | pool->unlink_directory_path); | |
199 | } | |
200 | lttng_directory_handle_put(pool->unlink_directory_handle); | |
201 | pool->unlink_directory_handle = NULL; | |
202 | } | |
203 | end: | |
204 | return ret; | |
205 | } | |
206 | ||
5c1f54d1 | 207 | static void lttng_inode_destroy(struct lttng_inode *inode) |
e0e72660 JG |
208 | { |
209 | if (!inode) { | |
210 | return; | |
211 | } | |
f7c3ffd7 JG |
212 | |
213 | rcu_read_lock(); | |
214 | cds_lfht_del(inode->registry_ht, &inode->registry_node); | |
215 | rcu_read_unlock(); | |
216 | ||
e0e72660 | 217 | if (inode->unlink_pending) { |
f7c3ffd7 | 218 | int ret; |
e0e72660 | 219 | |
f7c3ffd7 JG |
220 | assert(inode->location.directory_handle); |
221 | assert(inode->location.path); | |
222 | DBG("Removing %s from unlinked file pool", | |
223 | inode->location.path); | |
224 | ret = lttng_unlinked_file_pool_remove_inode(inode->unlinked_file_pool, inode); | |
e0e72660 | 225 | if (ret) { |
f7c3ffd7 | 226 | PERROR("Failed to unlink %s", inode->location.path); |
e0e72660 JG |
227 | } |
228 | } | |
f7c3ffd7 JG |
229 | |
230 | lttng_directory_handle_put( | |
231 | inode->location.directory_handle); | |
232 | inode->location.directory_handle = NULL; | |
233 | free(inode->location.path); | |
234 | inode->location.path = NULL; | |
235 | call_rcu(&inode->rcu_head, lttng_inode_free); | |
e0e72660 JG |
236 | } |
237 | ||
5c1f54d1 | 238 | static void lttng_inode_release(struct urcu_ref *ref) |
e0e72660 | 239 | { |
5c1f54d1 | 240 | lttng_inode_destroy(caa_container_of(ref, struct lttng_inode, ref)); |
e0e72660 JG |
241 | } |
242 | ||
5c1f54d1 | 243 | static void lttng_inode_get(struct lttng_inode *inode) |
e0e72660 JG |
244 | { |
245 | urcu_ref_get(&inode->ref); | |
246 | } | |
247 | ||
f7c3ffd7 JG |
248 | struct lttng_unlinked_file_pool *lttng_unlinked_file_pool_create( |
249 | const char *path) | |
250 | { | |
251 | struct lttng_unlinked_file_pool *pool = zmalloc(sizeof(*pool)); | |
252 | ||
253 | if (!path || *path != '/') { | |
254 | ERR("Unlinked file pool must be created with an absolute path, path = \"%s\"", | |
255 | path ? path : "NULL"); | |
256 | goto error; | |
257 | } | |
258 | ||
259 | pool->unlink_directory_path = strdup(path); | |
260 | if (!pool->unlink_directory_path) { | |
261 | PERROR("Failed to allocation unlinked file pool path"); | |
262 | goto error; | |
263 | } | |
264 | DBG("Unlinked file pool created at: %s", path); | |
265 | return pool; | |
266 | error: | |
267 | lttng_unlinked_file_pool_destroy(pool); | |
268 | return NULL; | |
269 | } | |
270 | ||
271 | void lttng_unlinked_file_pool_destroy( | |
272 | struct lttng_unlinked_file_pool *pool) | |
273 | { | |
274 | if (!pool) { | |
275 | return; | |
276 | } | |
277 | ||
278 | assert(pool->file_count == 0); | |
279 | lttng_directory_handle_put(pool->unlink_directory_handle); | |
280 | free(pool->unlink_directory_path); | |
281 | free(pool); | |
282 | } | |
283 | ||
e0e72660 JG |
284 | void lttng_inode_put(struct lttng_inode *inode) |
285 | { | |
286 | urcu_ref_put(&inode->ref, lttng_inode_release); | |
287 | } | |
288 | ||
dd95933f JG |
289 | struct lttng_directory_handle *lttng_inode_get_location_directory_handle( |
290 | struct lttng_inode *inode) | |
291 | { | |
292 | if (inode->location.directory_handle) { | |
293 | const bool reference_acquired = lttng_directory_handle_get( | |
294 | inode->location.directory_handle); | |
295 | ||
296 | assert(reference_acquired); | |
297 | } | |
298 | return inode->location.directory_handle; | |
299 | } | |
300 | ||
301 | void lttng_inode_borrow_location(struct lttng_inode *inode, | |
f7c3ffd7 JG |
302 | const struct lttng_directory_handle **out_directory_handle, |
303 | const char **out_path) | |
e0e72660 | 304 | { |
f7c3ffd7 JG |
305 | if (out_directory_handle) { |
306 | *out_directory_handle = inode->location.directory_handle; | |
307 | } | |
308 | if (out_path) { | |
309 | *out_path = inode->location.path; | |
310 | } | |
e0e72660 JG |
311 | } |
312 | ||
5c1f54d1 | 313 | int lttng_inode_rename( |
f7c3ffd7 JG |
314 | struct lttng_inode *inode, |
315 | struct lttng_directory_handle *old_directory_handle, | |
316 | const char *old_path, | |
317 | struct lttng_directory_handle *new_directory_handle, | |
318 | const char *new_path, | |
319 | bool overwrite) | |
e0e72660 JG |
320 | { |
321 | int ret = 0; | |
f7c3ffd7 JG |
322 | char *new_path_copy = strdup(new_path); |
323 | bool reference_acquired; | |
324 | ||
325 | DBG("Performing rename of inode from %s to %s with %s directory handles", | |
326 | old_path, new_path, | |
327 | lttng_directory_handle_equals(old_directory_handle, | |
328 | new_directory_handle) ? | |
329 | "identical" : | |
330 | "different"); | |
331 | ||
332 | if (!new_path_copy) { | |
333 | ret = -ENOMEM; | |
334 | goto end; | |
335 | } | |
e0e72660 JG |
336 | |
337 | if (inode->unlink_pending) { | |
f7c3ffd7 JG |
338 | WARN("An attempt to rename an unlinked file from %s to %s has been performed", |
339 | old_path, new_path); | |
e0e72660 JG |
340 | ret = -ENOENT; |
341 | goto end; | |
342 | } | |
343 | ||
344 | if (!overwrite) { | |
f7c3ffd7 | 345 | /* Verify that file doesn't exist. */ |
e0e72660 JG |
346 | struct stat statbuf; |
347 | ||
f7c3ffd7 JG |
348 | ret = lttng_directory_handle_stat( |
349 | new_directory_handle, new_path, &statbuf); | |
e0e72660 | 350 | if (ret == 0) { |
f7c3ffd7 JG |
351 | ERR("Refusing to rename %s as the destination already exists", |
352 | old_path); | |
e0e72660 JG |
353 | ret = -EEXIST; |
354 | goto end; | |
355 | } else if (ret < 0 && errno != ENOENT) { | |
356 | PERROR("Failed to stat() %s", new_path); | |
357 | ret = -errno; | |
358 | goto end; | |
359 | } | |
360 | } | |
361 | ||
f7c3ffd7 JG |
362 | ret = lttng_directory_handle_rename(old_directory_handle, old_path, |
363 | new_directory_handle, new_path); | |
e0e72660 | 364 | if (ret) { |
f7c3ffd7 | 365 | PERROR("Failed to rename file %s to %s", old_path, new_path); |
e0e72660 JG |
366 | ret = -errno; |
367 | goto end; | |
368 | } | |
369 | ||
f7c3ffd7 JG |
370 | reference_acquired = lttng_directory_handle_get(new_directory_handle); |
371 | assert(reference_acquired); | |
372 | lttng_directory_handle_put(inode->location.directory_handle); | |
373 | free(inode->location.path); | |
374 | inode->location.directory_handle = new_directory_handle; | |
375 | /* Ownership transferred. */ | |
376 | inode->location.path = new_path_copy; | |
e0e72660 JG |
377 | new_path_copy = NULL; |
378 | end: | |
379 | free(new_path_copy); | |
380 | return ret; | |
381 | } | |
382 | ||
f7c3ffd7 | 383 | int lttng_inode_unlink(struct lttng_inode *inode) |
e0e72660 JG |
384 | { |
385 | int ret = 0; | |
f7c3ffd7 JG |
386 | |
387 | DBG("Attempting unlink of inode %s", inode->location.path); | |
e0e72660 JG |
388 | |
389 | if (inode->unlink_pending) { | |
390 | WARN("An attempt to re-unlink %s has been performed, ignoring.", | |
f7c3ffd7 | 391 | inode->location.path); |
e0e72660 JG |
392 | ret = -ENOENT; |
393 | goto end; | |
394 | } | |
395 | ||
f7c3ffd7 JG |
396 | /* |
397 | * Move to the temporary "deleted" directory until all | |
398 | * references are released. | |
399 | */ | |
400 | ret = lttng_unlinked_file_pool_add_inode( | |
401 | inode->unlinked_file_pool, inode); | |
402 | if (ret) { | |
403 | PERROR("Failed to add inode \"%s\" to the unlinked file pool", | |
404 | inode->location.path); | |
e0e72660 JG |
405 | goto end; |
406 | } | |
f7c3ffd7 | 407 | inode->unlink_pending = true; |
e0e72660 JG |
408 | end: |
409 | return ret; | |
410 | } | |
411 | ||
5c1f54d1 | 412 | static struct lttng_inode *lttng_inode_create(const struct inode_id *id, |
f7c3ffd7 JG |
413 | struct cds_lfht *ht, |
414 | struct lttng_unlinked_file_pool *unlinked_file_pool, | |
415 | struct lttng_directory_handle *directory_handle, | |
416 | const char *path) | |
e0e72660 | 417 | { |
f7c3ffd7 JG |
418 | struct lttng_inode *inode = NULL; |
419 | char *path_copy; | |
420 | bool reference_acquired; | |
421 | ||
422 | path_copy = strdup(path); | |
423 | if (!path_copy) { | |
424 | goto end; | |
425 | } | |
e0e72660 | 426 | |
f7c3ffd7 JG |
427 | reference_acquired = lttng_directory_handle_get(directory_handle); |
428 | assert(reference_acquired); | |
429 | ||
430 | inode = zmalloc(sizeof(*inode)); | |
e0e72660 JG |
431 | if (!inode) { |
432 | goto end; | |
433 | } | |
434 | ||
435 | urcu_ref_init(&inode->ref); | |
436 | cds_lfht_node_init(&inode->registry_node); | |
437 | inode->id = *id; | |
e0e72660 | 438 | inode->registry_ht = ht; |
f7c3ffd7 JG |
439 | inode->unlinked_file_pool = unlinked_file_pool; |
440 | /* Ownership of path copy is transferred to inode. */ | |
441 | inode->location.path = path_copy; | |
442 | path_copy = NULL; | |
443 | inode->location.directory_handle = directory_handle; | |
e0e72660 | 444 | end: |
f7c3ffd7 | 445 | free(path_copy); |
e0e72660 | 446 | return inode; |
e0e72660 JG |
447 | } |
448 | ||
449 | struct lttng_inode_registry *lttng_inode_registry_create(void) | |
450 | { | |
451 | struct lttng_inode_registry *registry = zmalloc(sizeof(*registry)); | |
452 | ||
453 | if (!registry) { | |
454 | goto end; | |
455 | } | |
456 | ||
457 | pthread_mutex_lock(&seed.lock); | |
458 | if (!seed.initialized) { | |
459 | seed.value = (unsigned long) time(NULL); | |
460 | seed.initialized = true; | |
461 | } | |
462 | pthread_mutex_unlock(&seed.lock); | |
463 | ||
464 | registry->inodes = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0, | |
465 | CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL); | |
466 | if (!registry->inodes) { | |
467 | goto error; | |
468 | } | |
469 | end: | |
470 | return registry; | |
471 | error: | |
472 | lttng_inode_registry_destroy(registry); | |
473 | return NULL; | |
474 | } | |
475 | ||
476 | void lttng_inode_registry_destroy(struct lttng_inode_registry *registry) | |
477 | { | |
478 | if (!registry) { | |
479 | return; | |
480 | } | |
481 | if (registry->inodes) { | |
482 | int ret = cds_lfht_destroy(registry->inodes, NULL); | |
483 | ||
484 | assert(!ret); | |
485 | } | |
486 | free(registry); | |
487 | } | |
488 | ||
489 | struct lttng_inode *lttng_inode_registry_get_inode( | |
f7c3ffd7 JG |
490 | struct lttng_inode_registry *registry, |
491 | struct lttng_directory_handle *handle, | |
492 | const char *path, | |
493 | int fd, | |
494 | struct lttng_unlinked_file_pool *unlinked_file_pool) | |
e0e72660 JG |
495 | { |
496 | int ret; | |
497 | struct stat statbuf; | |
498 | struct inode_id id; | |
499 | struct cds_lfht_iter iter; | |
500 | struct cds_lfht_node *node; | |
501 | struct lttng_inode *inode = NULL; | |
502 | ||
503 | ret = fstat(fd, &statbuf); | |
504 | if (ret < 0) { | |
f7c3ffd7 | 505 | PERROR("stat() failed on fd %i", fd); |
e0e72660 JG |
506 | goto end; |
507 | } | |
508 | ||
509 | id.device = statbuf.st_dev; | |
510 | id.inode = statbuf.st_ino; | |
511 | ||
512 | rcu_read_lock(); | |
5c1f54d1 JG |
513 | cds_lfht_lookup(registry->inodes, lttng_inode_id_hash(&id), |
514 | lttng_inode_match, &id, &iter); | |
e0e72660 JG |
515 | node = cds_lfht_iter_get_node(&iter); |
516 | if (node) { | |
5c1f54d1 JG |
517 | inode = caa_container_of( |
518 | node, struct lttng_inode, registry_node); | |
e0e72660 JG |
519 | lttng_inode_get(inode); |
520 | goto end_unlock; | |
521 | } | |
522 | ||
f7c3ffd7 JG |
523 | inode = lttng_inode_create(&id, registry->inodes, unlinked_file_pool, |
524 | handle, path); | |
e0e72660 | 525 | node = cds_lfht_add_unique(registry->inodes, |
5c1f54d1 JG |
526 | lttng_inode_id_hash(&inode->id), lttng_inode_match, |
527 | &inode->id, &inode->registry_node); | |
e0e72660 JG |
528 | assert(node == &inode->registry_node); |
529 | end_unlock: | |
530 | rcu_read_unlock(); | |
531 | end: | |
532 | return inode; | |
533 | } |