Commit | Line | Data |
---|---|---|
e5e5558e MS |
1 | /* |
2 | FUSE: Filesystem in Userspace | |
51eb01e7 | 3 | Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu> |
e5e5558e MS |
4 | |
5 | This program can be distributed under the terms of the GNU GPL. | |
6 | See the file COPYING. | |
7 | */ | |
8 | ||
9 | #include "fuse_i.h" | |
10 | ||
11 | #include <linux/pagemap.h> | |
12 | #include <linux/file.h> | |
13 | #include <linux/gfp.h> | |
14 | #include <linux/sched.h> | |
15 | #include <linux/namei.h> | |
16 | ||
0a0898cf MS |
17 | #if BITS_PER_LONG >= 64 |
18 | static inline void fuse_dentry_settime(struct dentry *entry, u64 time) | |
19 | { | |
20 | entry->d_time = time; | |
21 | } | |
22 | ||
23 | static inline u64 fuse_dentry_time(struct dentry *entry) | |
24 | { | |
25 | return entry->d_time; | |
26 | } | |
27 | #else | |
28 | /* | |
29 | * On 32 bit archs store the high 32 bits of time in d_fsdata | |
30 | */ | |
31 | static void fuse_dentry_settime(struct dentry *entry, u64 time) | |
32 | { | |
33 | entry->d_time = time; | |
34 | entry->d_fsdata = (void *) (unsigned long) (time >> 32); | |
35 | } | |
36 | ||
37 | static u64 fuse_dentry_time(struct dentry *entry) | |
38 | { | |
39 | return (u64) entry->d_time + | |
40 | ((u64) (unsigned long) entry->d_fsdata << 32); | |
41 | } | |
42 | #endif | |
43 | ||
6f9f1180 MS |
44 | /* |
45 | * FUSE caches dentries and attributes with separate timeout. The | |
46 | * time in jiffies until the dentry/attributes are valid is stored in | |
47 | * dentry->d_time and fuse_inode->i_time respectively. | |
48 | */ | |
49 | ||
50 | /* | |
51 | * Calculate the time in jiffies until a dentry/attributes are valid | |
52 | */ | |
0a0898cf | 53 | static u64 time_to_jiffies(unsigned long sec, unsigned long nsec) |
e5e5558e | 54 | { |
685d16dd MS |
55 | if (sec || nsec) { |
56 | struct timespec ts = {sec, nsec}; | |
0a0898cf | 57 | return get_jiffies_64() + timespec_to_jiffies(&ts); |
685d16dd | 58 | } else |
0a0898cf | 59 | return 0; |
e5e5558e MS |
60 | } |
61 | ||
6f9f1180 MS |
62 | /* |
63 | * Set dentry and possibly attribute timeouts from the lookup/mk* | |
64 | * replies | |
65 | */ | |
1fb69e78 MS |
66 | static void fuse_change_entry_timeout(struct dentry *entry, |
67 | struct fuse_entry_out *o) | |
0aa7c699 | 68 | { |
0a0898cf MS |
69 | fuse_dentry_settime(entry, |
70 | time_to_jiffies(o->entry_valid, o->entry_valid_nsec)); | |
1fb69e78 MS |
71 | } |
72 | ||
73 | static u64 attr_timeout(struct fuse_attr_out *o) | |
74 | { | |
75 | return time_to_jiffies(o->attr_valid, o->attr_valid_nsec); | |
76 | } | |
77 | ||
78 | static u64 entry_attr_timeout(struct fuse_entry_out *o) | |
79 | { | |
80 | return time_to_jiffies(o->attr_valid, o->attr_valid_nsec); | |
8cbdf1e6 MS |
81 | } |
82 | ||
6f9f1180 MS |
83 | /* |
84 | * Mark the attributes as stale, so that at the next call to | |
85 | * ->getattr() they will be fetched from userspace | |
86 | */ | |
8cbdf1e6 MS |
87 | void fuse_invalidate_attr(struct inode *inode) |
88 | { | |
0a0898cf | 89 | get_fuse_inode(inode)->i_time = 0; |
8cbdf1e6 MS |
90 | } |
91 | ||
6f9f1180 MS |
92 | /* |
93 | * Just mark the entry as stale, so that a next attempt to look it up | |
94 | * will result in a new lookup call to userspace | |
95 | * | |
96 | * This is called when a dentry is about to become negative and the | |
97 | * timeout is unknown (unlink, rmdir, rename and in some cases | |
98 | * lookup) | |
99 | */ | |
8cbdf1e6 MS |
100 | static void fuse_invalidate_entry_cache(struct dentry *entry) |
101 | { | |
0a0898cf | 102 | fuse_dentry_settime(entry, 0); |
8cbdf1e6 MS |
103 | } |
104 | ||
6f9f1180 MS |
105 | /* |
106 | * Same as fuse_invalidate_entry_cache(), but also try to remove the | |
107 | * dentry from the hash | |
108 | */ | |
8cbdf1e6 MS |
109 | static void fuse_invalidate_entry(struct dentry *entry) |
110 | { | |
111 | d_invalidate(entry); | |
112 | fuse_invalidate_entry_cache(entry); | |
0aa7c699 MS |
113 | } |
114 | ||
e5e5558e MS |
115 | static void fuse_lookup_init(struct fuse_req *req, struct inode *dir, |
116 | struct dentry *entry, | |
117 | struct fuse_entry_out *outarg) | |
118 | { | |
0e9663ee MS |
119 | struct fuse_conn *fc = get_fuse_conn(dir); |
120 | ||
121 | memset(outarg, 0, sizeof(struct fuse_entry_out)); | |
e5e5558e MS |
122 | req->in.h.opcode = FUSE_LOOKUP; |
123 | req->in.h.nodeid = get_node_id(dir); | |
e5e5558e MS |
124 | req->in.numargs = 1; |
125 | req->in.args[0].size = entry->d_name.len + 1; | |
126 | req->in.args[0].value = entry->d_name.name; | |
127 | req->out.numargs = 1; | |
0e9663ee MS |
128 | if (fc->minor < 9) |
129 | req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; | |
130 | else | |
131 | req->out.args[0].size = sizeof(struct fuse_entry_out); | |
e5e5558e MS |
132 | req->out.args[0].value = outarg; |
133 | } | |
134 | ||
7dca9fd3 MS |
135 | static u64 fuse_get_attr_version(struct fuse_conn *fc) |
136 | { | |
137 | u64 curr_version; | |
138 | ||
139 | /* | |
140 | * The spin lock isn't actually needed on 64bit archs, but we | |
141 | * don't yet care too much about such optimizations. | |
142 | */ | |
143 | spin_lock(&fc->lock); | |
144 | curr_version = fc->attr_version; | |
145 | spin_unlock(&fc->lock); | |
146 | ||
147 | return curr_version; | |
148 | } | |
149 | ||
6f9f1180 MS |
150 | /* |
151 | * Check whether the dentry is still valid | |
152 | * | |
153 | * If the entry validity timeout has expired and the dentry is | |
154 | * positive, try to redo the lookup. If the lookup results in a | |
155 | * different inode, then let the VFS invalidate the dentry and redo | |
156 | * the lookup once more. If the lookup results in the same inode, | |
157 | * then refresh the attributes, timeouts and mark the dentry valid. | |
158 | */ | |
e5e5558e MS |
159 | static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd) |
160 | { | |
8cbdf1e6 MS |
161 | struct inode *inode = entry->d_inode; |
162 | ||
163 | if (inode && is_bad_inode(inode)) | |
e5e5558e | 164 | return 0; |
0a0898cf | 165 | else if (fuse_dentry_time(entry) < get_jiffies_64()) { |
e5e5558e | 166 | int err; |
e5e5558e | 167 | struct fuse_entry_out outarg; |
8cbdf1e6 MS |
168 | struct fuse_conn *fc; |
169 | struct fuse_req *req; | |
2d51013e | 170 | struct fuse_req *forget_req; |
e956edd0 | 171 | struct dentry *parent; |
1fb69e78 | 172 | u64 attr_version; |
8cbdf1e6 | 173 | |
50322fe7 | 174 | /* For negative dentries, always do a fresh lookup */ |
8cbdf1e6 MS |
175 | if (!inode) |
176 | return 0; | |
177 | ||
178 | fc = get_fuse_conn(inode); | |
ce1d5a49 MS |
179 | req = fuse_get_req(fc); |
180 | if (IS_ERR(req)) | |
e5e5558e MS |
181 | return 0; |
182 | ||
2d51013e MS |
183 | forget_req = fuse_get_req(fc); |
184 | if (IS_ERR(forget_req)) { | |
185 | fuse_put_request(fc, req); | |
186 | return 0; | |
187 | } | |
188 | ||
7dca9fd3 | 189 | attr_version = fuse_get_attr_version(fc); |
1fb69e78 | 190 | |
e956edd0 MS |
191 | parent = dget_parent(entry); |
192 | fuse_lookup_init(req, parent->d_inode, entry, &outarg); | |
7c352bdf | 193 | request_send(fc, req); |
e956edd0 | 194 | dput(parent); |
e5e5558e | 195 | err = req->out.h.error; |
2d51013e | 196 | fuse_put_request(fc, req); |
50322fe7 MS |
197 | /* Zero nodeid is same as -ENOENT */ |
198 | if (!err && !outarg.nodeid) | |
199 | err = -ENOENT; | |
9e6268db | 200 | if (!err) { |
8cbdf1e6 | 201 | struct fuse_inode *fi = get_fuse_inode(inode); |
9e6268db | 202 | if (outarg.nodeid != get_node_id(inode)) { |
2d51013e MS |
203 | fuse_send_forget(fc, forget_req, |
204 | outarg.nodeid, 1); | |
9e6268db MS |
205 | return 0; |
206 | } | |
8da5ff23 | 207 | spin_lock(&fc->lock); |
9e6268db | 208 | fi->nlookup ++; |
8da5ff23 | 209 | spin_unlock(&fc->lock); |
9e6268db | 210 | } |
2d51013e | 211 | fuse_put_request(fc, forget_req); |
9e6268db | 212 | if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT) |
e5e5558e MS |
213 | return 0; |
214 | ||
1fb69e78 MS |
215 | fuse_change_attributes(inode, &outarg.attr, |
216 | entry_attr_timeout(&outarg), | |
217 | attr_version); | |
218 | fuse_change_entry_timeout(entry, &outarg); | |
e5e5558e MS |
219 | } |
220 | return 1; | |
221 | } | |
222 | ||
8bfc016d | 223 | static int invalid_nodeid(u64 nodeid) |
2827d0b2 MS |
224 | { |
225 | return !nodeid || nodeid == FUSE_ROOT_ID; | |
226 | } | |
227 | ||
e5e5558e MS |
228 | static struct dentry_operations fuse_dentry_operations = { |
229 | .d_revalidate = fuse_dentry_revalidate, | |
230 | }; | |
231 | ||
a5bfffac | 232 | int fuse_valid_type(int m) |
39ee059a MS |
233 | { |
234 | return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) || | |
235 | S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m); | |
236 | } | |
237 | ||
d2a85164 MS |
238 | /* |
239 | * Add a directory inode to a dentry, ensuring that no other dentry | |
240 | * refers to this inode. Called with fc->inst_mutex. | |
241 | */ | |
242 | static int fuse_d_add_directory(struct dentry *entry, struct inode *inode) | |
243 | { | |
244 | struct dentry *alias = d_find_alias(inode); | |
245 | if (alias) { | |
246 | /* This tries to shrink the subtree below alias */ | |
247 | fuse_invalidate_entry(alias); | |
248 | dput(alias); | |
249 | if (!list_empty(&inode->i_dentry)) | |
250 | return -EBUSY; | |
251 | } | |
252 | d_add(entry, inode); | |
253 | return 0; | |
254 | } | |
255 | ||
0aa7c699 MS |
256 | static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry, |
257 | struct nameidata *nd) | |
e5e5558e MS |
258 | { |
259 | int err; | |
e5e5558e MS |
260 | struct fuse_entry_out outarg; |
261 | struct inode *inode = NULL; | |
262 | struct fuse_conn *fc = get_fuse_conn(dir); | |
263 | struct fuse_req *req; | |
2d51013e | 264 | struct fuse_req *forget_req; |
1fb69e78 | 265 | u64 attr_version; |
e5e5558e MS |
266 | |
267 | if (entry->d_name.len > FUSE_NAME_MAX) | |
0aa7c699 | 268 | return ERR_PTR(-ENAMETOOLONG); |
e5e5558e | 269 | |
ce1d5a49 MS |
270 | req = fuse_get_req(fc); |
271 | if (IS_ERR(req)) | |
272 | return ERR_PTR(PTR_ERR(req)); | |
e5e5558e | 273 | |
2d51013e MS |
274 | forget_req = fuse_get_req(fc); |
275 | if (IS_ERR(forget_req)) { | |
276 | fuse_put_request(fc, req); | |
277 | return ERR_PTR(PTR_ERR(forget_req)); | |
278 | } | |
279 | ||
7dca9fd3 | 280 | attr_version = fuse_get_attr_version(fc); |
1fb69e78 | 281 | |
e5e5558e MS |
282 | fuse_lookup_init(req, dir, entry, &outarg); |
283 | request_send(fc, req); | |
e5e5558e | 284 | err = req->out.h.error; |
2d51013e | 285 | fuse_put_request(fc, req); |
50322fe7 MS |
286 | /* Zero nodeid is same as -ENOENT, but with valid timeout */ |
287 | if (!err && outarg.nodeid && | |
a5bfffac TS |
288 | (invalid_nodeid(outarg.nodeid) || |
289 | !fuse_valid_type(outarg.attr.mode))) | |
ee4e5271 | 290 | err = -EIO; |
8cbdf1e6 | 291 | if (!err && outarg.nodeid) { |
e5e5558e | 292 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, |
1fb69e78 MS |
293 | &outarg.attr, entry_attr_timeout(&outarg), |
294 | attr_version); | |
e5e5558e | 295 | if (!inode) { |
2d51013e | 296 | fuse_send_forget(fc, forget_req, outarg.nodeid, 1); |
0aa7c699 | 297 | return ERR_PTR(-ENOMEM); |
e5e5558e MS |
298 | } |
299 | } | |
2d51013e | 300 | fuse_put_request(fc, forget_req); |
e5e5558e | 301 | if (err && err != -ENOENT) |
0aa7c699 | 302 | return ERR_PTR(err); |
e5e5558e | 303 | |
d2a85164 MS |
304 | if (inode && S_ISDIR(inode->i_mode)) { |
305 | mutex_lock(&fc->inst_mutex); | |
306 | err = fuse_d_add_directory(entry, inode); | |
307 | mutex_unlock(&fc->inst_mutex); | |
308 | if (err) { | |
309 | iput(inode); | |
310 | return ERR_PTR(err); | |
311 | } | |
312 | } else | |
313 | d_add(entry, inode); | |
314 | ||
e5e5558e | 315 | entry->d_op = &fuse_dentry_operations; |
8cbdf1e6 | 316 | if (!err) |
1fb69e78 | 317 | fuse_change_entry_timeout(entry, &outarg); |
8cbdf1e6 MS |
318 | else |
319 | fuse_invalidate_entry_cache(entry); | |
0aa7c699 | 320 | return NULL; |
e5e5558e MS |
321 | } |
322 | ||
51eb01e7 MS |
323 | /* |
324 | * Synchronous release for the case when something goes wrong in CREATE_OPEN | |
325 | */ | |
326 | static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff, | |
327 | u64 nodeid, int flags) | |
328 | { | |
c756e0a4 MS |
329 | fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE); |
330 | ff->reserved_req->force = 1; | |
331 | request_send(fc, ff->reserved_req); | |
332 | fuse_put_request(fc, ff->reserved_req); | |
333 | kfree(ff); | |
51eb01e7 MS |
334 | } |
335 | ||
6f9f1180 MS |
336 | /* |
337 | * Atomic create+open operation | |
338 | * | |
339 | * If the filesystem doesn't support this, then fall back to separate | |
340 | * 'mknod' + 'open' requests. | |
341 | */ | |
fd72faac MS |
342 | static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode, |
343 | struct nameidata *nd) | |
344 | { | |
345 | int err; | |
346 | struct inode *inode; | |
347 | struct fuse_conn *fc = get_fuse_conn(dir); | |
348 | struct fuse_req *req; | |
51eb01e7 | 349 | struct fuse_req *forget_req; |
fd72faac MS |
350 | struct fuse_open_in inarg; |
351 | struct fuse_open_out outopen; | |
352 | struct fuse_entry_out outentry; | |
fd72faac MS |
353 | struct fuse_file *ff; |
354 | struct file *file; | |
355 | int flags = nd->intent.open.flags - 1; | |
356 | ||
fd72faac | 357 | if (fc->no_create) |
ce1d5a49 | 358 | return -ENOSYS; |
fd72faac | 359 | |
51eb01e7 MS |
360 | forget_req = fuse_get_req(fc); |
361 | if (IS_ERR(forget_req)) | |
362 | return PTR_ERR(forget_req); | |
363 | ||
ce1d5a49 | 364 | req = fuse_get_req(fc); |
51eb01e7 | 365 | err = PTR_ERR(req); |
ce1d5a49 | 366 | if (IS_ERR(req)) |
51eb01e7 | 367 | goto out_put_forget_req; |
fd72faac | 368 | |
ce1d5a49 | 369 | err = -ENOMEM; |
fd72faac MS |
370 | ff = fuse_file_alloc(); |
371 | if (!ff) | |
372 | goto out_put_request; | |
373 | ||
374 | flags &= ~O_NOCTTY; | |
375 | memset(&inarg, 0, sizeof(inarg)); | |
0e9663ee | 376 | memset(&outentry, 0, sizeof(outentry)); |
fd72faac MS |
377 | inarg.flags = flags; |
378 | inarg.mode = mode; | |
379 | req->in.h.opcode = FUSE_CREATE; | |
380 | req->in.h.nodeid = get_node_id(dir); | |
fd72faac MS |
381 | req->in.numargs = 2; |
382 | req->in.args[0].size = sizeof(inarg); | |
383 | req->in.args[0].value = &inarg; | |
384 | req->in.args[1].size = entry->d_name.len + 1; | |
385 | req->in.args[1].value = entry->d_name.name; | |
386 | req->out.numargs = 2; | |
0e9663ee MS |
387 | if (fc->minor < 9) |
388 | req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; | |
389 | else | |
390 | req->out.args[0].size = sizeof(outentry); | |
fd72faac MS |
391 | req->out.args[0].value = &outentry; |
392 | req->out.args[1].size = sizeof(outopen); | |
393 | req->out.args[1].value = &outopen; | |
394 | request_send(fc, req); | |
395 | err = req->out.h.error; | |
396 | if (err) { | |
397 | if (err == -ENOSYS) | |
398 | fc->no_create = 1; | |
399 | goto out_free_ff; | |
400 | } | |
401 | ||
402 | err = -EIO; | |
2827d0b2 | 403 | if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid)) |
fd72faac MS |
404 | goto out_free_ff; |
405 | ||
51eb01e7 | 406 | fuse_put_request(fc, req); |
fd72faac | 407 | inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation, |
1fb69e78 | 408 | &outentry.attr, entry_attr_timeout(&outentry), 0); |
fd72faac MS |
409 | if (!inode) { |
410 | flags &= ~(O_CREAT | O_EXCL | O_TRUNC); | |
411 | ff->fh = outopen.fh; | |
51eb01e7 MS |
412 | fuse_sync_release(fc, ff, outentry.nodeid, flags); |
413 | fuse_send_forget(fc, forget_req, outentry.nodeid, 1); | |
414 | return -ENOMEM; | |
fd72faac | 415 | } |
51eb01e7 | 416 | fuse_put_request(fc, forget_req); |
fd72faac | 417 | d_instantiate(entry, inode); |
1fb69e78 | 418 | fuse_change_entry_timeout(entry, &outentry); |
fd72faac MS |
419 | file = lookup_instantiate_filp(nd, entry, generic_file_open); |
420 | if (IS_ERR(file)) { | |
421 | ff->fh = outopen.fh; | |
51eb01e7 | 422 | fuse_sync_release(fc, ff, outentry.nodeid, flags); |
fd72faac MS |
423 | return PTR_ERR(file); |
424 | } | |
425 | fuse_finish_open(inode, file, ff, &outopen); | |
426 | return 0; | |
427 | ||
428 | out_free_ff: | |
429 | fuse_file_free(ff); | |
430 | out_put_request: | |
431 | fuse_put_request(fc, req); | |
51eb01e7 MS |
432 | out_put_forget_req: |
433 | fuse_put_request(fc, forget_req); | |
fd72faac MS |
434 | return err; |
435 | } | |
436 | ||
6f9f1180 MS |
437 | /* |
438 | * Code shared between mknod, mkdir, symlink and link | |
439 | */ | |
9e6268db MS |
440 | static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req, |
441 | struct inode *dir, struct dentry *entry, | |
442 | int mode) | |
443 | { | |
444 | struct fuse_entry_out outarg; | |
445 | struct inode *inode; | |
9e6268db | 446 | int err; |
2d51013e MS |
447 | struct fuse_req *forget_req; |
448 | ||
449 | forget_req = fuse_get_req(fc); | |
450 | if (IS_ERR(forget_req)) { | |
451 | fuse_put_request(fc, req); | |
452 | return PTR_ERR(forget_req); | |
453 | } | |
9e6268db | 454 | |
0e9663ee | 455 | memset(&outarg, 0, sizeof(outarg)); |
9e6268db | 456 | req->in.h.nodeid = get_node_id(dir); |
9e6268db | 457 | req->out.numargs = 1; |
0e9663ee MS |
458 | if (fc->minor < 9) |
459 | req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; | |
460 | else | |
461 | req->out.args[0].size = sizeof(outarg); | |
9e6268db MS |
462 | req->out.args[0].value = &outarg; |
463 | request_send(fc, req); | |
464 | err = req->out.h.error; | |
2d51013e MS |
465 | fuse_put_request(fc, req); |
466 | if (err) | |
467 | goto out_put_forget_req; | |
468 | ||
39ee059a MS |
469 | err = -EIO; |
470 | if (invalid_nodeid(outarg.nodeid)) | |
2d51013e | 471 | goto out_put_forget_req; |
39ee059a MS |
472 | |
473 | if ((outarg.attr.mode ^ mode) & S_IFMT) | |
2d51013e | 474 | goto out_put_forget_req; |
39ee059a | 475 | |
9e6268db | 476 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, |
1fb69e78 | 477 | &outarg.attr, entry_attr_timeout(&outarg), 0); |
9e6268db | 478 | if (!inode) { |
2d51013e | 479 | fuse_send_forget(fc, forget_req, outarg.nodeid, 1); |
9e6268db MS |
480 | return -ENOMEM; |
481 | } | |
2d51013e | 482 | fuse_put_request(fc, forget_req); |
9e6268db | 483 | |
d2a85164 MS |
484 | if (S_ISDIR(inode->i_mode)) { |
485 | struct dentry *alias; | |
486 | mutex_lock(&fc->inst_mutex); | |
487 | alias = d_find_alias(inode); | |
488 | if (alias) { | |
489 | /* New directory must have moved since mkdir */ | |
490 | mutex_unlock(&fc->inst_mutex); | |
491 | dput(alias); | |
492 | iput(inode); | |
493 | return -EBUSY; | |
494 | } | |
495 | d_instantiate(entry, inode); | |
496 | mutex_unlock(&fc->inst_mutex); | |
497 | } else | |
498 | d_instantiate(entry, inode); | |
9e6268db | 499 | |
1fb69e78 | 500 | fuse_change_entry_timeout(entry, &outarg); |
9e6268db MS |
501 | fuse_invalidate_attr(dir); |
502 | return 0; | |
39ee059a | 503 | |
2d51013e MS |
504 | out_put_forget_req: |
505 | fuse_put_request(fc, forget_req); | |
39ee059a | 506 | return err; |
9e6268db MS |
507 | } |
508 | ||
509 | static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode, | |
510 | dev_t rdev) | |
511 | { | |
512 | struct fuse_mknod_in inarg; | |
513 | struct fuse_conn *fc = get_fuse_conn(dir); | |
ce1d5a49 MS |
514 | struct fuse_req *req = fuse_get_req(fc); |
515 | if (IS_ERR(req)) | |
516 | return PTR_ERR(req); | |
9e6268db MS |
517 | |
518 | memset(&inarg, 0, sizeof(inarg)); | |
519 | inarg.mode = mode; | |
520 | inarg.rdev = new_encode_dev(rdev); | |
521 | req->in.h.opcode = FUSE_MKNOD; | |
522 | req->in.numargs = 2; | |
523 | req->in.args[0].size = sizeof(inarg); | |
524 | req->in.args[0].value = &inarg; | |
525 | req->in.args[1].size = entry->d_name.len + 1; | |
526 | req->in.args[1].value = entry->d_name.name; | |
527 | return create_new_entry(fc, req, dir, entry, mode); | |
528 | } | |
529 | ||
530 | static int fuse_create(struct inode *dir, struct dentry *entry, int mode, | |
531 | struct nameidata *nd) | |
532 | { | |
b9ba347f | 533 | if (nd && (nd->flags & LOOKUP_OPEN)) { |
fd72faac MS |
534 | int err = fuse_create_open(dir, entry, mode, nd); |
535 | if (err != -ENOSYS) | |
536 | return err; | |
537 | /* Fall back on mknod */ | |
538 | } | |
9e6268db MS |
539 | return fuse_mknod(dir, entry, mode, 0); |
540 | } | |
541 | ||
542 | static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode) | |
543 | { | |
544 | struct fuse_mkdir_in inarg; | |
545 | struct fuse_conn *fc = get_fuse_conn(dir); | |
ce1d5a49 MS |
546 | struct fuse_req *req = fuse_get_req(fc); |
547 | if (IS_ERR(req)) | |
548 | return PTR_ERR(req); | |
9e6268db MS |
549 | |
550 | memset(&inarg, 0, sizeof(inarg)); | |
551 | inarg.mode = mode; | |
552 | req->in.h.opcode = FUSE_MKDIR; | |
553 | req->in.numargs = 2; | |
554 | req->in.args[0].size = sizeof(inarg); | |
555 | req->in.args[0].value = &inarg; | |
556 | req->in.args[1].size = entry->d_name.len + 1; | |
557 | req->in.args[1].value = entry->d_name.name; | |
558 | return create_new_entry(fc, req, dir, entry, S_IFDIR); | |
559 | } | |
560 | ||
561 | static int fuse_symlink(struct inode *dir, struct dentry *entry, | |
562 | const char *link) | |
563 | { | |
564 | struct fuse_conn *fc = get_fuse_conn(dir); | |
565 | unsigned len = strlen(link) + 1; | |
ce1d5a49 MS |
566 | struct fuse_req *req = fuse_get_req(fc); |
567 | if (IS_ERR(req)) | |
568 | return PTR_ERR(req); | |
9e6268db MS |
569 | |
570 | req->in.h.opcode = FUSE_SYMLINK; | |
571 | req->in.numargs = 2; | |
572 | req->in.args[0].size = entry->d_name.len + 1; | |
573 | req->in.args[0].value = entry->d_name.name; | |
574 | req->in.args[1].size = len; | |
575 | req->in.args[1].value = link; | |
576 | return create_new_entry(fc, req, dir, entry, S_IFLNK); | |
577 | } | |
578 | ||
579 | static int fuse_unlink(struct inode *dir, struct dentry *entry) | |
580 | { | |
581 | int err; | |
582 | struct fuse_conn *fc = get_fuse_conn(dir); | |
ce1d5a49 MS |
583 | struct fuse_req *req = fuse_get_req(fc); |
584 | if (IS_ERR(req)) | |
585 | return PTR_ERR(req); | |
9e6268db MS |
586 | |
587 | req->in.h.opcode = FUSE_UNLINK; | |
588 | req->in.h.nodeid = get_node_id(dir); | |
9e6268db MS |
589 | req->in.numargs = 1; |
590 | req->in.args[0].size = entry->d_name.len + 1; | |
591 | req->in.args[0].value = entry->d_name.name; | |
592 | request_send(fc, req); | |
593 | err = req->out.h.error; | |
594 | fuse_put_request(fc, req); | |
595 | if (!err) { | |
596 | struct inode *inode = entry->d_inode; | |
597 | ||
598 | /* Set nlink to zero so the inode can be cleared, if | |
599 | the inode does have more links this will be | |
600 | discovered at the next lookup/getattr */ | |
ce71ec36 | 601 | clear_nlink(inode); |
9e6268db MS |
602 | fuse_invalidate_attr(inode); |
603 | fuse_invalidate_attr(dir); | |
8cbdf1e6 | 604 | fuse_invalidate_entry_cache(entry); |
9e6268db MS |
605 | } else if (err == -EINTR) |
606 | fuse_invalidate_entry(entry); | |
607 | return err; | |
608 | } | |
609 | ||
610 | static int fuse_rmdir(struct inode *dir, struct dentry *entry) | |
611 | { | |
612 | int err; | |
613 | struct fuse_conn *fc = get_fuse_conn(dir); | |
ce1d5a49 MS |
614 | struct fuse_req *req = fuse_get_req(fc); |
615 | if (IS_ERR(req)) | |
616 | return PTR_ERR(req); | |
9e6268db MS |
617 | |
618 | req->in.h.opcode = FUSE_RMDIR; | |
619 | req->in.h.nodeid = get_node_id(dir); | |
9e6268db MS |
620 | req->in.numargs = 1; |
621 | req->in.args[0].size = entry->d_name.len + 1; | |
622 | req->in.args[0].value = entry->d_name.name; | |
623 | request_send(fc, req); | |
624 | err = req->out.h.error; | |
625 | fuse_put_request(fc, req); | |
626 | if (!err) { | |
ce71ec36 | 627 | clear_nlink(entry->d_inode); |
9e6268db | 628 | fuse_invalidate_attr(dir); |
8cbdf1e6 | 629 | fuse_invalidate_entry_cache(entry); |
9e6268db MS |
630 | } else if (err == -EINTR) |
631 | fuse_invalidate_entry(entry); | |
632 | return err; | |
633 | } | |
634 | ||
635 | static int fuse_rename(struct inode *olddir, struct dentry *oldent, | |
636 | struct inode *newdir, struct dentry *newent) | |
637 | { | |
638 | int err; | |
639 | struct fuse_rename_in inarg; | |
640 | struct fuse_conn *fc = get_fuse_conn(olddir); | |
ce1d5a49 MS |
641 | struct fuse_req *req = fuse_get_req(fc); |
642 | if (IS_ERR(req)) | |
643 | return PTR_ERR(req); | |
9e6268db MS |
644 | |
645 | memset(&inarg, 0, sizeof(inarg)); | |
646 | inarg.newdir = get_node_id(newdir); | |
647 | req->in.h.opcode = FUSE_RENAME; | |
648 | req->in.h.nodeid = get_node_id(olddir); | |
9e6268db MS |
649 | req->in.numargs = 3; |
650 | req->in.args[0].size = sizeof(inarg); | |
651 | req->in.args[0].value = &inarg; | |
652 | req->in.args[1].size = oldent->d_name.len + 1; | |
653 | req->in.args[1].value = oldent->d_name.name; | |
654 | req->in.args[2].size = newent->d_name.len + 1; | |
655 | req->in.args[2].value = newent->d_name.name; | |
656 | request_send(fc, req); | |
657 | err = req->out.h.error; | |
658 | fuse_put_request(fc, req); | |
659 | if (!err) { | |
660 | fuse_invalidate_attr(olddir); | |
661 | if (olddir != newdir) | |
662 | fuse_invalidate_attr(newdir); | |
8cbdf1e6 MS |
663 | |
664 | /* newent will end up negative */ | |
665 | if (newent->d_inode) | |
666 | fuse_invalidate_entry_cache(newent); | |
9e6268db MS |
667 | } else if (err == -EINTR) { |
668 | /* If request was interrupted, DEITY only knows if the | |
669 | rename actually took place. If the invalidation | |
670 | fails (e.g. some process has CWD under the renamed | |
671 | directory), then there can be inconsistency between | |
672 | the dcache and the real filesystem. Tough luck. */ | |
673 | fuse_invalidate_entry(oldent); | |
674 | if (newent->d_inode) | |
675 | fuse_invalidate_entry(newent); | |
676 | } | |
677 | ||
678 | return err; | |
679 | } | |
680 | ||
681 | static int fuse_link(struct dentry *entry, struct inode *newdir, | |
682 | struct dentry *newent) | |
683 | { | |
684 | int err; | |
685 | struct fuse_link_in inarg; | |
686 | struct inode *inode = entry->d_inode; | |
687 | struct fuse_conn *fc = get_fuse_conn(inode); | |
ce1d5a49 MS |
688 | struct fuse_req *req = fuse_get_req(fc); |
689 | if (IS_ERR(req)) | |
690 | return PTR_ERR(req); | |
9e6268db MS |
691 | |
692 | memset(&inarg, 0, sizeof(inarg)); | |
693 | inarg.oldnodeid = get_node_id(inode); | |
694 | req->in.h.opcode = FUSE_LINK; | |
9e6268db MS |
695 | req->in.numargs = 2; |
696 | req->in.args[0].size = sizeof(inarg); | |
697 | req->in.args[0].value = &inarg; | |
698 | req->in.args[1].size = newent->d_name.len + 1; | |
699 | req->in.args[1].value = newent->d_name.name; | |
700 | err = create_new_entry(fc, req, newdir, newent, inode->i_mode); | |
701 | /* Contrary to "normal" filesystems it can happen that link | |
702 | makes two "logical" inodes point to the same "physical" | |
703 | inode. We invalidate the attributes of the old one, so it | |
704 | will reflect changes in the backing inode (link count, | |
705 | etc.) | |
706 | */ | |
707 | if (!err || err == -EINTR) | |
708 | fuse_invalidate_attr(inode); | |
709 | return err; | |
710 | } | |
711 | ||
1fb69e78 MS |
712 | static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr, |
713 | struct kstat *stat) | |
714 | { | |
715 | stat->dev = inode->i_sb->s_dev; | |
716 | stat->ino = attr->ino; | |
717 | stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777); | |
718 | stat->nlink = attr->nlink; | |
719 | stat->uid = attr->uid; | |
720 | stat->gid = attr->gid; | |
721 | stat->rdev = inode->i_rdev; | |
722 | stat->atime.tv_sec = attr->atime; | |
723 | stat->atime.tv_nsec = attr->atimensec; | |
724 | stat->mtime.tv_sec = attr->mtime; | |
725 | stat->mtime.tv_nsec = attr->mtimensec; | |
726 | stat->ctime.tv_sec = attr->ctime; | |
727 | stat->ctime.tv_nsec = attr->ctimensec; | |
728 | stat->size = attr->size; | |
729 | stat->blocks = attr->blocks; | |
730 | stat->blksize = (1 << inode->i_blkbits); | |
731 | } | |
732 | ||
c79e322f MS |
733 | static int fuse_do_getattr(struct inode *inode, struct kstat *stat, |
734 | struct file *file) | |
e5e5558e MS |
735 | { |
736 | int err; | |
c79e322f MS |
737 | struct fuse_getattr_in inarg; |
738 | struct fuse_attr_out outarg; | |
e5e5558e | 739 | struct fuse_conn *fc = get_fuse_conn(inode); |
1fb69e78 MS |
740 | struct fuse_req *req; |
741 | u64 attr_version; | |
742 | ||
743 | req = fuse_get_req(fc); | |
ce1d5a49 MS |
744 | if (IS_ERR(req)) |
745 | return PTR_ERR(req); | |
e5e5558e | 746 | |
7dca9fd3 | 747 | attr_version = fuse_get_attr_version(fc); |
1fb69e78 | 748 | |
c79e322f | 749 | memset(&inarg, 0, sizeof(inarg)); |
0e9663ee | 750 | memset(&outarg, 0, sizeof(outarg)); |
c79e322f MS |
751 | /* Directories have separate file-handle space */ |
752 | if (file && S_ISREG(inode->i_mode)) { | |
753 | struct fuse_file *ff = file->private_data; | |
754 | ||
755 | inarg.getattr_flags |= FUSE_GETATTR_FH; | |
756 | inarg.fh = ff->fh; | |
757 | } | |
e5e5558e MS |
758 | req->in.h.opcode = FUSE_GETATTR; |
759 | req->in.h.nodeid = get_node_id(inode); | |
c79e322f MS |
760 | req->in.numargs = 1; |
761 | req->in.args[0].size = sizeof(inarg); | |
762 | req->in.args[0].value = &inarg; | |
e5e5558e | 763 | req->out.numargs = 1; |
0e9663ee MS |
764 | if (fc->minor < 9) |
765 | req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE; | |
766 | else | |
767 | req->out.args[0].size = sizeof(outarg); | |
c79e322f | 768 | req->out.args[0].value = &outarg; |
e5e5558e MS |
769 | request_send(fc, req); |
770 | err = req->out.h.error; | |
771 | fuse_put_request(fc, req); | |
772 | if (!err) { | |
c79e322f | 773 | if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { |
e5e5558e MS |
774 | make_bad_inode(inode); |
775 | err = -EIO; | |
776 | } else { | |
c79e322f MS |
777 | fuse_change_attributes(inode, &outarg.attr, |
778 | attr_timeout(&outarg), | |
1fb69e78 MS |
779 | attr_version); |
780 | if (stat) | |
c79e322f | 781 | fuse_fillattr(inode, &outarg.attr, stat); |
e5e5558e MS |
782 | } |
783 | } | |
784 | return err; | |
785 | } | |
786 | ||
bcb4be80 MS |
787 | int fuse_update_attributes(struct inode *inode, struct kstat *stat, |
788 | struct file *file, bool *refreshed) | |
789 | { | |
790 | struct fuse_inode *fi = get_fuse_inode(inode); | |
791 | int err; | |
792 | bool r; | |
793 | ||
794 | if (fi->i_time < get_jiffies_64()) { | |
795 | r = true; | |
796 | err = fuse_do_getattr(inode, stat, file); | |
797 | } else { | |
798 | r = false; | |
799 | err = 0; | |
800 | if (stat) { | |
801 | generic_fillattr(inode, stat); | |
802 | stat->mode = fi->orig_i_mode; | |
803 | } | |
804 | } | |
805 | ||
806 | if (refreshed != NULL) | |
807 | *refreshed = r; | |
808 | ||
809 | return err; | |
810 | } | |
811 | ||
87729a55 MS |
812 | /* |
813 | * Calling into a user-controlled filesystem gives the filesystem | |
814 | * daemon ptrace-like capabilities over the requester process. This | |
815 | * means, that the filesystem daemon is able to record the exact | |
816 | * filesystem operations performed, and can also control the behavior | |
817 | * of the requester process in otherwise impossible ways. For example | |
818 | * it can delay the operation for arbitrary length of time allowing | |
819 | * DoS against the requester. | |
820 | * | |
821 | * For this reason only those processes can call into the filesystem, | |
822 | * for which the owner of the mount has ptrace privilege. This | |
823 | * excludes processes started by other users, suid or sgid processes. | |
824 | */ | |
e57ac683 | 825 | int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task) |
87729a55 MS |
826 | { |
827 | if (fc->flags & FUSE_ALLOW_OTHER) | |
828 | return 1; | |
829 | ||
830 | if (task->euid == fc->user_id && | |
831 | task->suid == fc->user_id && | |
832 | task->uid == fc->user_id && | |
833 | task->egid == fc->group_id && | |
834 | task->sgid == fc->group_id && | |
835 | task->gid == fc->group_id) | |
836 | return 1; | |
837 | ||
838 | return 0; | |
839 | } | |
840 | ||
31d40d74 MS |
841 | static int fuse_access(struct inode *inode, int mask) |
842 | { | |
843 | struct fuse_conn *fc = get_fuse_conn(inode); | |
844 | struct fuse_req *req; | |
845 | struct fuse_access_in inarg; | |
846 | int err; | |
847 | ||
848 | if (fc->no_access) | |
849 | return 0; | |
850 | ||
ce1d5a49 MS |
851 | req = fuse_get_req(fc); |
852 | if (IS_ERR(req)) | |
853 | return PTR_ERR(req); | |
31d40d74 MS |
854 | |
855 | memset(&inarg, 0, sizeof(inarg)); | |
856 | inarg.mask = mask; | |
857 | req->in.h.opcode = FUSE_ACCESS; | |
858 | req->in.h.nodeid = get_node_id(inode); | |
31d40d74 MS |
859 | req->in.numargs = 1; |
860 | req->in.args[0].size = sizeof(inarg); | |
861 | req->in.args[0].value = &inarg; | |
862 | request_send(fc, req); | |
863 | err = req->out.h.error; | |
864 | fuse_put_request(fc, req); | |
865 | if (err == -ENOSYS) { | |
866 | fc->no_access = 1; | |
867 | err = 0; | |
868 | } | |
869 | return err; | |
870 | } | |
871 | ||
6f9f1180 MS |
872 | /* |
873 | * Check permission. The two basic access models of FUSE are: | |
874 | * | |
875 | * 1) Local access checking ('default_permissions' mount option) based | |
876 | * on file mode. This is the plain old disk filesystem permission | |
877 | * modell. | |
878 | * | |
879 | * 2) "Remote" access checking, where server is responsible for | |
880 | * checking permission in each inode operation. An exception to this | |
881 | * is if ->permission() was invoked from sys_access() in which case an | |
882 | * access request is sent. Execute permission is still checked | |
883 | * locally based on file mode. | |
884 | */ | |
e5e5558e MS |
885 | static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd) |
886 | { | |
887 | struct fuse_conn *fc = get_fuse_conn(inode); | |
244f6385 MS |
888 | bool refreshed = false; |
889 | int err = 0; | |
e5e5558e | 890 | |
87729a55 | 891 | if (!fuse_allow_task(fc, current)) |
e5e5558e | 892 | return -EACCES; |
244f6385 MS |
893 | |
894 | /* | |
e8e96157 | 895 | * If attributes are needed, refresh them before proceeding |
244f6385 | 896 | */ |
e8e96157 MS |
897 | if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) || |
898 | ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) { | |
bcb4be80 MS |
899 | err = fuse_update_attributes(inode, NULL, NULL, &refreshed); |
900 | if (err) | |
901 | return err; | |
244f6385 MS |
902 | } |
903 | ||
904 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { | |
1e9a4ed9 MS |
905 | int err = generic_permission(inode, mask, NULL); |
906 | ||
907 | /* If permission is denied, try to refresh file | |
908 | attributes. This is also needed, because the root | |
909 | node will at first have no permissions */ | |
244f6385 | 910 | if (err == -EACCES && !refreshed) { |
c79e322f | 911 | err = fuse_do_getattr(inode, NULL, NULL); |
1e9a4ed9 MS |
912 | if (!err) |
913 | err = generic_permission(inode, mask, NULL); | |
914 | } | |
915 | ||
6f9f1180 MS |
916 | /* Note: the opposite of the above test does not |
917 | exist. So if permissions are revoked this won't be | |
918 | noticed immediately, only after the attribute | |
919 | timeout has expired */ | |
e8e96157 MS |
920 | } else if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR))) { |
921 | err = fuse_access(inode, mask); | |
922 | } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) { | |
923 | if (!(inode->i_mode & S_IXUGO)) { | |
924 | if (refreshed) | |
925 | return -EACCES; | |
926 | ||
c79e322f | 927 | err = fuse_do_getattr(inode, NULL, NULL); |
e8e96157 MS |
928 | if (!err && !(inode->i_mode & S_IXUGO)) |
929 | return -EACCES; | |
930 | } | |
e5e5558e | 931 | } |
244f6385 | 932 | return err; |
e5e5558e MS |
933 | } |
934 | ||
935 | static int parse_dirfile(char *buf, size_t nbytes, struct file *file, | |
936 | void *dstbuf, filldir_t filldir) | |
937 | { | |
938 | while (nbytes >= FUSE_NAME_OFFSET) { | |
939 | struct fuse_dirent *dirent = (struct fuse_dirent *) buf; | |
940 | size_t reclen = FUSE_DIRENT_SIZE(dirent); | |
941 | int over; | |
942 | if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX) | |
943 | return -EIO; | |
944 | if (reclen > nbytes) | |
945 | break; | |
946 | ||
947 | over = filldir(dstbuf, dirent->name, dirent->namelen, | |
948 | file->f_pos, dirent->ino, dirent->type); | |
949 | if (over) | |
950 | break; | |
951 | ||
952 | buf += reclen; | |
953 | nbytes -= reclen; | |
954 | file->f_pos = dirent->off; | |
955 | } | |
956 | ||
957 | return 0; | |
958 | } | |
959 | ||
04730fef | 960 | static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir) |
e5e5558e | 961 | { |
04730fef MS |
962 | int err; |
963 | size_t nbytes; | |
964 | struct page *page; | |
7706a9d6 | 965 | struct inode *inode = file->f_path.dentry->d_inode; |
e5e5558e | 966 | struct fuse_conn *fc = get_fuse_conn(inode); |
c756e0a4 | 967 | struct fuse_file *ff = file->private_data; |
248d86e8 MS |
968 | struct fuse_req *req; |
969 | ||
970 | if (is_bad_inode(inode)) | |
971 | return -EIO; | |
972 | ||
ce1d5a49 MS |
973 | req = fuse_get_req(fc); |
974 | if (IS_ERR(req)) | |
975 | return PTR_ERR(req); | |
e5e5558e | 976 | |
04730fef MS |
977 | page = alloc_page(GFP_KERNEL); |
978 | if (!page) { | |
979 | fuse_put_request(fc, req); | |
980 | return -ENOMEM; | |
981 | } | |
982 | req->num_pages = 1; | |
983 | req->pages[0] = page; | |
c756e0a4 | 984 | fuse_read_fill(req, ff, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR); |
361b1eb5 MS |
985 | request_send(fc, req); |
986 | nbytes = req->out.args[0].size; | |
e5e5558e MS |
987 | err = req->out.h.error; |
988 | fuse_put_request(fc, req); | |
989 | if (!err) | |
04730fef MS |
990 | err = parse_dirfile(page_address(page), nbytes, file, dstbuf, |
991 | filldir); | |
e5e5558e | 992 | |
04730fef | 993 | __free_page(page); |
b36c31ba | 994 | fuse_invalidate_attr(inode); /* atime changed */ |
04730fef | 995 | return err; |
e5e5558e MS |
996 | } |
997 | ||
998 | static char *read_link(struct dentry *dentry) | |
999 | { | |
1000 | struct inode *inode = dentry->d_inode; | |
1001 | struct fuse_conn *fc = get_fuse_conn(inode); | |
ce1d5a49 | 1002 | struct fuse_req *req = fuse_get_req(fc); |
e5e5558e MS |
1003 | char *link; |
1004 | ||
ce1d5a49 MS |
1005 | if (IS_ERR(req)) |
1006 | return ERR_PTR(PTR_ERR(req)); | |
e5e5558e MS |
1007 | |
1008 | link = (char *) __get_free_page(GFP_KERNEL); | |
1009 | if (!link) { | |
1010 | link = ERR_PTR(-ENOMEM); | |
1011 | goto out; | |
1012 | } | |
1013 | req->in.h.opcode = FUSE_READLINK; | |
1014 | req->in.h.nodeid = get_node_id(inode); | |
e5e5558e MS |
1015 | req->out.argvar = 1; |
1016 | req->out.numargs = 1; | |
1017 | req->out.args[0].size = PAGE_SIZE - 1; | |
1018 | req->out.args[0].value = link; | |
1019 | request_send(fc, req); | |
1020 | if (req->out.h.error) { | |
1021 | free_page((unsigned long) link); | |
1022 | link = ERR_PTR(req->out.h.error); | |
1023 | } else | |
1024 | link[req->out.args[0].size] = '\0'; | |
1025 | out: | |
1026 | fuse_put_request(fc, req); | |
b36c31ba | 1027 | fuse_invalidate_attr(inode); /* atime changed */ |
e5e5558e MS |
1028 | return link; |
1029 | } | |
1030 | ||
1031 | static void free_link(char *link) | |
1032 | { | |
1033 | if (!IS_ERR(link)) | |
1034 | free_page((unsigned long) link); | |
1035 | } | |
1036 | ||
1037 | static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd) | |
1038 | { | |
1039 | nd_set_link(nd, read_link(dentry)); | |
1040 | return NULL; | |
1041 | } | |
1042 | ||
1043 | static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c) | |
1044 | { | |
1045 | free_link(nd_get_link(nd)); | |
1046 | } | |
1047 | ||
1048 | static int fuse_dir_open(struct inode *inode, struct file *file) | |
1049 | { | |
04730fef | 1050 | return fuse_open_common(inode, file, 1); |
e5e5558e MS |
1051 | } |
1052 | ||
1053 | static int fuse_dir_release(struct inode *inode, struct file *file) | |
1054 | { | |
04730fef | 1055 | return fuse_release_common(inode, file, 1); |
e5e5558e MS |
1056 | } |
1057 | ||
82547981 MS |
1058 | static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync) |
1059 | { | |
1060 | /* nfsd can call this with no file */ | |
1061 | return file ? fuse_fsync_common(file, de, datasync, 1) : 0; | |
1062 | } | |
1063 | ||
17637cba MS |
1064 | static bool update_mtime(unsigned ivalid) |
1065 | { | |
1066 | /* Always update if mtime is explicitly set */ | |
1067 | if (ivalid & ATTR_MTIME_SET) | |
1068 | return true; | |
1069 | ||
1070 | /* If it's an open(O_TRUNC) or an ftruncate(), don't update */ | |
1071 | if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE))) | |
1072 | return false; | |
1073 | ||
1074 | /* In all other cases update */ | |
1075 | return true; | |
1076 | } | |
1077 | ||
befc649c | 1078 | static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg) |
9e6268db MS |
1079 | { |
1080 | unsigned ivalid = iattr->ia_valid; | |
9e6268db MS |
1081 | |
1082 | if (ivalid & ATTR_MODE) | |
befc649c | 1083 | arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode; |
9e6268db | 1084 | if (ivalid & ATTR_UID) |
befc649c | 1085 | arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid; |
9e6268db | 1086 | if (ivalid & ATTR_GID) |
befc649c | 1087 | arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid; |
9e6268db | 1088 | if (ivalid & ATTR_SIZE) |
befc649c | 1089 | arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size; |
17637cba MS |
1090 | if (ivalid & ATTR_ATIME) { |
1091 | arg->valid |= FATTR_ATIME; | |
befc649c | 1092 | arg->atime = iattr->ia_atime.tv_sec; |
17637cba MS |
1093 | arg->atimensec = iattr->ia_atime.tv_nsec; |
1094 | if (!(ivalid & ATTR_ATIME_SET)) | |
1095 | arg->valid |= FATTR_ATIME_NOW; | |
1096 | } | |
1097 | if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) { | |
1098 | arg->valid |= FATTR_MTIME; | |
befc649c | 1099 | arg->mtime = iattr->ia_mtime.tv_sec; |
17637cba MS |
1100 | arg->mtimensec = iattr->ia_mtime.tv_nsec; |
1101 | if (!(ivalid & ATTR_MTIME_SET)) | |
1102 | arg->valid |= FATTR_MTIME_NOW; | |
befc649c | 1103 | } |
9e6268db MS |
1104 | } |
1105 | ||
6f9f1180 MS |
1106 | /* |
1107 | * Set attributes, and at the same time refresh them. | |
1108 | * | |
1109 | * Truncation is slightly complicated, because the 'truncate' request | |
1110 | * may fail, in which case we don't want to touch the mapping. | |
9ffbb916 MS |
1111 | * vmtruncate() doesn't allow for this case, so do the rlimit checking |
1112 | * and the actual truncation by hand. | |
6f9f1180 | 1113 | */ |
49d4914f MS |
1114 | static int fuse_do_setattr(struct dentry *entry, struct iattr *attr, |
1115 | struct file *file) | |
9e6268db MS |
1116 | { |
1117 | struct inode *inode = entry->d_inode; | |
1118 | struct fuse_conn *fc = get_fuse_conn(inode); | |
9e6268db MS |
1119 | struct fuse_req *req; |
1120 | struct fuse_setattr_in inarg; | |
1121 | struct fuse_attr_out outarg; | |
1122 | int err; | |
9e6268db | 1123 | |
e57ac683 MS |
1124 | if (!fuse_allow_task(fc, current)) |
1125 | return -EACCES; | |
1126 | ||
1e9a4ed9 MS |
1127 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { |
1128 | err = inode_change_ok(inode, attr); | |
1129 | if (err) | |
1130 | return err; | |
1131 | } | |
1132 | ||
6ff958ed MS |
1133 | if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc) |
1134 | return 0; | |
1135 | ||
9e6268db MS |
1136 | if (attr->ia_valid & ATTR_SIZE) { |
1137 | unsigned long limit; | |
b2d2272f MS |
1138 | if (IS_SWAPFILE(inode)) |
1139 | return -ETXTBSY; | |
9e6268db MS |
1140 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; |
1141 | if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) { | |
1142 | send_sig(SIGXFSZ, current, 0); | |
1143 | return -EFBIG; | |
1144 | } | |
1145 | } | |
1146 | ||
ce1d5a49 MS |
1147 | req = fuse_get_req(fc); |
1148 | if (IS_ERR(req)) | |
1149 | return PTR_ERR(req); | |
9e6268db MS |
1150 | |
1151 | memset(&inarg, 0, sizeof(inarg)); | |
0e9663ee | 1152 | memset(&outarg, 0, sizeof(outarg)); |
befc649c | 1153 | iattr_to_fattr(attr, &inarg); |
49d4914f MS |
1154 | if (file) { |
1155 | struct fuse_file *ff = file->private_data; | |
1156 | inarg.valid |= FATTR_FH; | |
1157 | inarg.fh = ff->fh; | |
1158 | } | |
f3332114 MS |
1159 | if (attr->ia_valid & ATTR_SIZE) { |
1160 | /* For mandatory locking in truncate */ | |
1161 | inarg.valid |= FATTR_LOCKOWNER; | |
1162 | inarg.lock_owner = fuse_lock_owner_id(fc, current->files); | |
1163 | } | |
9e6268db MS |
1164 | req->in.h.opcode = FUSE_SETATTR; |
1165 | req->in.h.nodeid = get_node_id(inode); | |
9e6268db MS |
1166 | req->in.numargs = 1; |
1167 | req->in.args[0].size = sizeof(inarg); | |
1168 | req->in.args[0].value = &inarg; | |
1169 | req->out.numargs = 1; | |
0e9663ee MS |
1170 | if (fc->minor < 9) |
1171 | req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE; | |
1172 | else | |
1173 | req->out.args[0].size = sizeof(outarg); | |
9e6268db MS |
1174 | req->out.args[0].value = &outarg; |
1175 | request_send(fc, req); | |
1176 | err = req->out.h.error; | |
1177 | fuse_put_request(fc, req); | |
e00d2c2d MS |
1178 | if (err) { |
1179 | if (err == -EINTR) | |
1180 | fuse_invalidate_attr(inode); | |
1181 | return err; | |
1182 | } | |
9e6268db | 1183 | |
e00d2c2d MS |
1184 | if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { |
1185 | make_bad_inode(inode); | |
1186 | return -EIO; | |
1187 | } | |
1188 | ||
1fb69e78 | 1189 | fuse_change_attributes(inode, &outarg.attr, attr_timeout(&outarg), 0); |
e00d2c2d | 1190 | return 0; |
9e6268db MS |
1191 | } |
1192 | ||
49d4914f MS |
1193 | static int fuse_setattr(struct dentry *entry, struct iattr *attr) |
1194 | { | |
1195 | if (attr->ia_valid & ATTR_FILE) | |
1196 | return fuse_do_setattr(entry, attr, attr->ia_file); | |
1197 | else | |
1198 | return fuse_do_setattr(entry, attr, NULL); | |
1199 | } | |
1200 | ||
e5e5558e MS |
1201 | static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry, |
1202 | struct kstat *stat) | |
1203 | { | |
1204 | struct inode *inode = entry->d_inode; | |
244f6385 | 1205 | struct fuse_conn *fc = get_fuse_conn(inode); |
244f6385 MS |
1206 | |
1207 | if (!fuse_allow_task(fc, current)) | |
1208 | return -EACCES; | |
1209 | ||
bcb4be80 | 1210 | return fuse_update_attributes(inode, stat, NULL, NULL); |
e5e5558e MS |
1211 | } |
1212 | ||
92a8780e MS |
1213 | static int fuse_setxattr(struct dentry *entry, const char *name, |
1214 | const void *value, size_t size, int flags) | |
1215 | { | |
1216 | struct inode *inode = entry->d_inode; | |
1217 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1218 | struct fuse_req *req; | |
1219 | struct fuse_setxattr_in inarg; | |
1220 | int err; | |
1221 | ||
92a8780e MS |
1222 | if (fc->no_setxattr) |
1223 | return -EOPNOTSUPP; | |
1224 | ||
ce1d5a49 MS |
1225 | req = fuse_get_req(fc); |
1226 | if (IS_ERR(req)) | |
1227 | return PTR_ERR(req); | |
92a8780e MS |
1228 | |
1229 | memset(&inarg, 0, sizeof(inarg)); | |
1230 | inarg.size = size; | |
1231 | inarg.flags = flags; | |
1232 | req->in.h.opcode = FUSE_SETXATTR; | |
1233 | req->in.h.nodeid = get_node_id(inode); | |
92a8780e MS |
1234 | req->in.numargs = 3; |
1235 | req->in.args[0].size = sizeof(inarg); | |
1236 | req->in.args[0].value = &inarg; | |
1237 | req->in.args[1].size = strlen(name) + 1; | |
1238 | req->in.args[1].value = name; | |
1239 | req->in.args[2].size = size; | |
1240 | req->in.args[2].value = value; | |
1241 | request_send(fc, req); | |
1242 | err = req->out.h.error; | |
1243 | fuse_put_request(fc, req); | |
1244 | if (err == -ENOSYS) { | |
1245 | fc->no_setxattr = 1; | |
1246 | err = -EOPNOTSUPP; | |
1247 | } | |
1248 | return err; | |
1249 | } | |
1250 | ||
1251 | static ssize_t fuse_getxattr(struct dentry *entry, const char *name, | |
1252 | void *value, size_t size) | |
1253 | { | |
1254 | struct inode *inode = entry->d_inode; | |
1255 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1256 | struct fuse_req *req; | |
1257 | struct fuse_getxattr_in inarg; | |
1258 | struct fuse_getxattr_out outarg; | |
1259 | ssize_t ret; | |
1260 | ||
1261 | if (fc->no_getxattr) | |
1262 | return -EOPNOTSUPP; | |
1263 | ||
ce1d5a49 MS |
1264 | req = fuse_get_req(fc); |
1265 | if (IS_ERR(req)) | |
1266 | return PTR_ERR(req); | |
92a8780e MS |
1267 | |
1268 | memset(&inarg, 0, sizeof(inarg)); | |
1269 | inarg.size = size; | |
1270 | req->in.h.opcode = FUSE_GETXATTR; | |
1271 | req->in.h.nodeid = get_node_id(inode); | |
92a8780e MS |
1272 | req->in.numargs = 2; |
1273 | req->in.args[0].size = sizeof(inarg); | |
1274 | req->in.args[0].value = &inarg; | |
1275 | req->in.args[1].size = strlen(name) + 1; | |
1276 | req->in.args[1].value = name; | |
1277 | /* This is really two different operations rolled into one */ | |
1278 | req->out.numargs = 1; | |
1279 | if (size) { | |
1280 | req->out.argvar = 1; | |
1281 | req->out.args[0].size = size; | |
1282 | req->out.args[0].value = value; | |
1283 | } else { | |
1284 | req->out.args[0].size = sizeof(outarg); | |
1285 | req->out.args[0].value = &outarg; | |
1286 | } | |
1287 | request_send(fc, req); | |
1288 | ret = req->out.h.error; | |
1289 | if (!ret) | |
1290 | ret = size ? req->out.args[0].size : outarg.size; | |
1291 | else { | |
1292 | if (ret == -ENOSYS) { | |
1293 | fc->no_getxattr = 1; | |
1294 | ret = -EOPNOTSUPP; | |
1295 | } | |
1296 | } | |
1297 | fuse_put_request(fc, req); | |
1298 | return ret; | |
1299 | } | |
1300 | ||
1301 | static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size) | |
1302 | { | |
1303 | struct inode *inode = entry->d_inode; | |
1304 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1305 | struct fuse_req *req; | |
1306 | struct fuse_getxattr_in inarg; | |
1307 | struct fuse_getxattr_out outarg; | |
1308 | ssize_t ret; | |
1309 | ||
e57ac683 MS |
1310 | if (!fuse_allow_task(fc, current)) |
1311 | return -EACCES; | |
1312 | ||
92a8780e MS |
1313 | if (fc->no_listxattr) |
1314 | return -EOPNOTSUPP; | |
1315 | ||
ce1d5a49 MS |
1316 | req = fuse_get_req(fc); |
1317 | if (IS_ERR(req)) | |
1318 | return PTR_ERR(req); | |
92a8780e MS |
1319 | |
1320 | memset(&inarg, 0, sizeof(inarg)); | |
1321 | inarg.size = size; | |
1322 | req->in.h.opcode = FUSE_LISTXATTR; | |
1323 | req->in.h.nodeid = get_node_id(inode); | |
92a8780e MS |
1324 | req->in.numargs = 1; |
1325 | req->in.args[0].size = sizeof(inarg); | |
1326 | req->in.args[0].value = &inarg; | |
1327 | /* This is really two different operations rolled into one */ | |
1328 | req->out.numargs = 1; | |
1329 | if (size) { | |
1330 | req->out.argvar = 1; | |
1331 | req->out.args[0].size = size; | |
1332 | req->out.args[0].value = list; | |
1333 | } else { | |
1334 | req->out.args[0].size = sizeof(outarg); | |
1335 | req->out.args[0].value = &outarg; | |
1336 | } | |
1337 | request_send(fc, req); | |
1338 | ret = req->out.h.error; | |
1339 | if (!ret) | |
1340 | ret = size ? req->out.args[0].size : outarg.size; | |
1341 | else { | |
1342 | if (ret == -ENOSYS) { | |
1343 | fc->no_listxattr = 1; | |
1344 | ret = -EOPNOTSUPP; | |
1345 | } | |
1346 | } | |
1347 | fuse_put_request(fc, req); | |
1348 | return ret; | |
1349 | } | |
1350 | ||
1351 | static int fuse_removexattr(struct dentry *entry, const char *name) | |
1352 | { | |
1353 | struct inode *inode = entry->d_inode; | |
1354 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1355 | struct fuse_req *req; | |
1356 | int err; | |
1357 | ||
1358 | if (fc->no_removexattr) | |
1359 | return -EOPNOTSUPP; | |
1360 | ||
ce1d5a49 MS |
1361 | req = fuse_get_req(fc); |
1362 | if (IS_ERR(req)) | |
1363 | return PTR_ERR(req); | |
92a8780e MS |
1364 | |
1365 | req->in.h.opcode = FUSE_REMOVEXATTR; | |
1366 | req->in.h.nodeid = get_node_id(inode); | |
92a8780e MS |
1367 | req->in.numargs = 1; |
1368 | req->in.args[0].size = strlen(name) + 1; | |
1369 | req->in.args[0].value = name; | |
1370 | request_send(fc, req); | |
1371 | err = req->out.h.error; | |
1372 | fuse_put_request(fc, req); | |
1373 | if (err == -ENOSYS) { | |
1374 | fc->no_removexattr = 1; | |
1375 | err = -EOPNOTSUPP; | |
1376 | } | |
1377 | return err; | |
1378 | } | |
1379 | ||
754661f1 | 1380 | static const struct inode_operations fuse_dir_inode_operations = { |
e5e5558e | 1381 | .lookup = fuse_lookup, |
9e6268db MS |
1382 | .mkdir = fuse_mkdir, |
1383 | .symlink = fuse_symlink, | |
1384 | .unlink = fuse_unlink, | |
1385 | .rmdir = fuse_rmdir, | |
1386 | .rename = fuse_rename, | |
1387 | .link = fuse_link, | |
1388 | .setattr = fuse_setattr, | |
1389 | .create = fuse_create, | |
1390 | .mknod = fuse_mknod, | |
e5e5558e MS |
1391 | .permission = fuse_permission, |
1392 | .getattr = fuse_getattr, | |
92a8780e MS |
1393 | .setxattr = fuse_setxattr, |
1394 | .getxattr = fuse_getxattr, | |
1395 | .listxattr = fuse_listxattr, | |
1396 | .removexattr = fuse_removexattr, | |
e5e5558e MS |
1397 | }; |
1398 | ||
4b6f5d20 | 1399 | static const struct file_operations fuse_dir_operations = { |
b6aeaded | 1400 | .llseek = generic_file_llseek, |
e5e5558e MS |
1401 | .read = generic_read_dir, |
1402 | .readdir = fuse_readdir, | |
1403 | .open = fuse_dir_open, | |
1404 | .release = fuse_dir_release, | |
82547981 | 1405 | .fsync = fuse_dir_fsync, |
e5e5558e MS |
1406 | }; |
1407 | ||
754661f1 | 1408 | static const struct inode_operations fuse_common_inode_operations = { |
9e6268db | 1409 | .setattr = fuse_setattr, |
e5e5558e MS |
1410 | .permission = fuse_permission, |
1411 | .getattr = fuse_getattr, | |
92a8780e MS |
1412 | .setxattr = fuse_setxattr, |
1413 | .getxattr = fuse_getxattr, | |
1414 | .listxattr = fuse_listxattr, | |
1415 | .removexattr = fuse_removexattr, | |
e5e5558e MS |
1416 | }; |
1417 | ||
754661f1 | 1418 | static const struct inode_operations fuse_symlink_inode_operations = { |
9e6268db | 1419 | .setattr = fuse_setattr, |
e5e5558e MS |
1420 | .follow_link = fuse_follow_link, |
1421 | .put_link = fuse_put_link, | |
1422 | .readlink = generic_readlink, | |
1423 | .getattr = fuse_getattr, | |
92a8780e MS |
1424 | .setxattr = fuse_setxattr, |
1425 | .getxattr = fuse_getxattr, | |
1426 | .listxattr = fuse_listxattr, | |
1427 | .removexattr = fuse_removexattr, | |
e5e5558e MS |
1428 | }; |
1429 | ||
1430 | void fuse_init_common(struct inode *inode) | |
1431 | { | |
1432 | inode->i_op = &fuse_common_inode_operations; | |
1433 | } | |
1434 | ||
1435 | void fuse_init_dir(struct inode *inode) | |
1436 | { | |
1437 | inode->i_op = &fuse_dir_inode_operations; | |
1438 | inode->i_fop = &fuse_dir_operations; | |
1439 | } | |
1440 | ||
1441 | void fuse_init_symlink(struct inode *inode) | |
1442 | { | |
1443 | inode->i_op = &fuse_symlink_inode_operations; | |
1444 | } |