fd-tracker: add the lttng-inode interface
[lttng-tools.git] / src / common / fd-tracker / inode.c
1 /*
2 * Copyright (C) 2018 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <unistd.h>
21 #include <inttypes.h>
22 #include <urcu.h>
23 #include <urcu/ref.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>
30
31 #include "inode.h"
32
33 struct inode_id {
34 dev_t device;
35 ino_t inode;
36 };
37
38 struct lttng_inode_registry {
39 /* Hashtable of inode_id to lttng_inode. */
40 struct cds_lfht *inodes;
41 };
42
43 struct lttng_inode {
44 struct inode_id id;
45 char *path;
46 bool unlink_pending;
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;
51 struct urcu_ref ref;
52 struct rcu_head rcu_head;
53 };
54
55 static struct {
56 pthread_mutex_t lock;
57 bool initialized;
58 unsigned long value;
59 } seed = {
60 .lock = PTHREAD_MUTEX_INITIALIZER,
61 };
62
63 static
64 unsigned long lttng_inode_id_hash(struct inode_id *id)
65 {
66 uint64_t device = id->device, inode_no = id->inode;
67
68 return hash_key_u64(&device, seed.value) ^
69 hash_key_u64(&inode_no, seed.value);
70 }
71
72 static
73 int lttng_inode_match(struct cds_lfht_node *node, const void *key)
74 {
75 const struct inode_id *id = key;
76 struct lttng_inode *inode = caa_container_of(node, struct lttng_inode,
77 registry_node);
78
79 return inode->id.device == id->device && inode->id.inode == id->inode;
80 }
81
82 static
83 void lttng_inode_delete(struct rcu_head *head)
84 {
85 struct lttng_inode *inode = caa_container_of(head,
86 struct lttng_inode, rcu_head);
87
88 free(inode->path);
89 free(inode);
90 }
91
92 static
93 void lttng_inode_destroy(struct lttng_inode *inode)
94 {
95 if (!inode) {
96 return;
97 }
98 if (inode->unlink_pending) {
99 int ret = unlink(inode->path);
100
101 DBG("Unlinking %s during lttng_inode destruction", inode->path);
102 if (ret) {
103 PERROR("Failed to unlink %s", inode->path);
104 }
105 }
106 call_rcu(&inode->rcu_head, lttng_inode_delete);
107 }
108
109 static
110 void lttng_inode_release(struct urcu_ref *ref)
111 {
112 lttng_inode_destroy(caa_container_of(ref, struct lttng_inode, ref));
113 }
114
115 static
116 void lttng_inode_get(struct lttng_inode *inode)
117 {
118 urcu_ref_get(&inode->ref);
119 }
120
121 void lttng_inode_put(struct lttng_inode *inode)
122 {
123 urcu_ref_put(&inode->ref, lttng_inode_release);
124 }
125
126 const char *lttng_inode_get_path(const struct lttng_inode *inode)
127 {
128 return inode->path;
129 }
130
131 int lttng_inode_rename(struct lttng_inode *inode, const char *new_path,
132 bool overwrite)
133 {
134 int ret = 0;
135 char *new_path_copy = NULL;
136
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);
140 ret = -ENOENT;
141 goto end;
142 }
143
144 if (!overwrite) {
145 struct stat statbuf;
146
147 ret = stat(new_path, &statbuf);
148 if (ret == 0) {
149 ret = -EEXIST;
150 goto end;
151 } else if (ret < 0 && errno != ENOENT) {
152 PERROR("Failed to stat() %s", new_path);
153 ret = -errno;
154 goto end;
155 }
156 }
157
158 new_path_copy = strdup(new_path);
159 if (!new_path_copy) {
160 ERR("Failed to allocate storage for path %s", new_path);
161 ret = -ENOMEM;
162 goto end;
163 }
164
165 ret = rename(inode->path, new_path);
166 if (ret) {
167 PERROR("Failed to rename %s to %s", inode->path, new_path);
168 ret = -errno;
169 goto end;
170 }
171
172 free(inode->path);
173 inode->path = new_path_copy;
174 new_path_copy = NULL;
175 end:
176 free(new_path_copy);
177 return ret;
178 }
179
180 int lttng_inode_defer_unlink(struct lttng_inode *inode)
181 {
182 int ret = 0;
183 uint16_t i = 0;
184 char suffix[sizeof("-deleted-65535")] = "-deleted";
185 char new_path[LTTNG_PATH_MAX];
186 size_t original_path_len = strlen(inode->path);
187
188 if (inode->unlink_pending) {
189 WARN("An attempt to re-unlink %s has been performed, ignoring.",
190 inode->path);
191 ret = -ENOENT;
192 goto end;
193 }
194
195 ret = lttng_strncpy(new_path, inode->path, sizeof(new_path));
196 if (ret < 0) {
197 ret = -ENAMETOOLONG;
198 goto end;
199 }
200
201 for (i = 0; i < UINT16_MAX; i++) {
202 int p_ret;
203
204 if (i != 0) {
205 p_ret = snprintf(suffix, sizeof(suffix), "-deleted-%" PRIu16, i);
206
207 if (p_ret < 0) {
208 PERROR("Failed to form suffix to rename file %s",
209 inode->path);
210 ret = -errno;
211 goto end;
212 }
213 assert(p_ret != sizeof(suffix));
214 } else {
215 /* suffix is initialy set to '-deleted'. */
216 p_ret = strlen(suffix);
217 }
218
219 if (original_path_len + p_ret + 1 >= sizeof(new_path)) {
220 ret = -ENAMETOOLONG;
221 goto end;
222 }
223
224 strcat(&new_path[original_path_len], suffix);
225 ret = lttng_inode_rename(inode, new_path, false);
226 if (ret != -EEXIST) {
227 break;
228 }
229 new_path[original_path_len] = '\0';
230 }
231 if (!ret) {
232 inode->unlink_pending = true;
233 }
234 end:
235 return ret;
236 }
237
238 static
239 struct lttng_inode *lttng_inode_create(const struct inode_id *id,
240 const char *path, struct cds_lfht *ht)
241 {
242 struct lttng_inode *inode = zmalloc(sizeof(*inode));
243
244 if (!inode) {
245 goto end;
246 }
247
248 urcu_ref_init(&inode->ref);
249 cds_lfht_node_init(&inode->registry_node);
250 inode->id = *id;
251 inode->path = strdup(path);
252 if (!inode->path) {
253 goto error;
254 }
255 end:
256 return inode;
257 error:
258 lttng_inode_destroy(inode);
259 return NULL;
260 }
261
262 struct lttng_inode_registry *lttng_inode_registry_create(void)
263 {
264 struct lttng_inode_registry *registry = zmalloc(sizeof(*registry));
265
266 if (!registry) {
267 goto end;
268 }
269
270 pthread_mutex_lock(&seed.lock);
271 if (!seed.initialized) {
272 seed.value = (unsigned long) time(NULL);
273 seed.initialized = true;
274 }
275 pthread_mutex_unlock(&seed.lock);
276
277 registry->inodes = cds_lfht_new(DEFAULT_HT_SIZE, 1, 0,
278 CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, NULL);
279 if (!registry->inodes) {
280 goto error;
281 }
282 end:
283 return registry;
284 error:
285 lttng_inode_registry_destroy(registry);
286 return NULL;
287 }
288
289 void lttng_inode_registry_destroy(struct lttng_inode_registry *registry)
290 {
291 if (!registry) {
292 return;
293 }
294 if (registry->inodes) {
295 int ret = cds_lfht_destroy(registry->inodes, NULL);
296
297 assert(!ret);
298 }
299 free(registry);
300 }
301
302 struct lttng_inode *lttng_inode_registry_get_inode(
303 struct lttng_inode_registry *registry,
304 int fd, const char *path)
305 {
306 int ret;
307 struct stat statbuf;
308 struct inode_id id;
309 struct cds_lfht_iter iter;
310 struct cds_lfht_node *node;
311 struct lttng_inode *inode = NULL;
312
313 ret = fstat(fd, &statbuf);
314 if (ret < 0) {
315 PERROR("stat() failed on file %s, fd = %i", path, fd);
316 goto end;
317 }
318
319 id.device = statbuf.st_dev;
320 id.inode = statbuf.st_ino;
321
322 rcu_read_lock();
323 cds_lfht_lookup(registry->inodes,
324 lttng_inode_id_hash(&id),
325 lttng_inode_match,
326 &id,
327 &iter);
328 node = cds_lfht_iter_get_node(&iter);
329 if (node) {
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);
334 goto end_unlock;
335 }
336
337 inode = lttng_inode_create(&id, path, registry->inodes);
338 node = cds_lfht_add_unique(registry->inodes,
339 lttng_inode_id_hash(&inode->id),
340 lttng_inode_match,
341 &inode->id,
342 &inode->registry_node);
343 assert(node == &inode->registry_node);
344 end_unlock:
345 rcu_read_unlock();
346 end:
347 return inode;
348 }
This page took 0.03822 seconds and 5 git commands to generate.