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