]>
Commit | Line | Data |
---|---|---|
e5e5558e MS |
1 | /* |
2 | FUSE: Filesystem in Userspace | |
3 | Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu> | |
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 | ||
6f9f1180 MS |
17 | /* |
18 | * FUSE caches dentries and attributes with separate timeout. The | |
19 | * time in jiffies until the dentry/attributes are valid is stored in | |
20 | * dentry->d_time and fuse_inode->i_time respectively. | |
21 | */ | |
22 | ||
23 | /* | |
24 | * Calculate the time in jiffies until a dentry/attributes are valid | |
25 | */ | |
e5e5558e MS |
26 | static inline unsigned long time_to_jiffies(unsigned long sec, |
27 | unsigned long nsec) | |
28 | { | |
29 | struct timespec ts = {sec, nsec}; | |
30 | return jiffies + timespec_to_jiffies(&ts); | |
31 | } | |
32 | ||
6f9f1180 MS |
33 | /* |
34 | * Set dentry and possibly attribute timeouts from the lookup/mk* | |
35 | * replies | |
36 | */ | |
0aa7c699 MS |
37 | static void fuse_change_timeout(struct dentry *entry, struct fuse_entry_out *o) |
38 | { | |
0aa7c699 | 39 | entry->d_time = time_to_jiffies(o->entry_valid, o->entry_valid_nsec); |
8cbdf1e6 MS |
40 | if (entry->d_inode) |
41 | get_fuse_inode(entry->d_inode)->i_time = | |
42 | time_to_jiffies(o->attr_valid, o->attr_valid_nsec); | |
43 | } | |
44 | ||
6f9f1180 MS |
45 | /* |
46 | * Mark the attributes as stale, so that at the next call to | |
47 | * ->getattr() they will be fetched from userspace | |
48 | */ | |
8cbdf1e6 MS |
49 | void fuse_invalidate_attr(struct inode *inode) |
50 | { | |
51 | get_fuse_inode(inode)->i_time = jiffies - 1; | |
52 | } | |
53 | ||
6f9f1180 MS |
54 | /* |
55 | * Just mark the entry as stale, so that a next attempt to look it up | |
56 | * will result in a new lookup call to userspace | |
57 | * | |
58 | * This is called when a dentry is about to become negative and the | |
59 | * timeout is unknown (unlink, rmdir, rename and in some cases | |
60 | * lookup) | |
61 | */ | |
8cbdf1e6 MS |
62 | static void fuse_invalidate_entry_cache(struct dentry *entry) |
63 | { | |
64 | entry->d_time = jiffies - 1; | |
65 | } | |
66 | ||
6f9f1180 MS |
67 | /* |
68 | * Same as fuse_invalidate_entry_cache(), but also try to remove the | |
69 | * dentry from the hash | |
70 | */ | |
8cbdf1e6 MS |
71 | static void fuse_invalidate_entry(struct dentry *entry) |
72 | { | |
73 | d_invalidate(entry); | |
74 | fuse_invalidate_entry_cache(entry); | |
0aa7c699 MS |
75 | } |
76 | ||
e5e5558e MS |
77 | static void fuse_lookup_init(struct fuse_req *req, struct inode *dir, |
78 | struct dentry *entry, | |
79 | struct fuse_entry_out *outarg) | |
80 | { | |
81 | req->in.h.opcode = FUSE_LOOKUP; | |
82 | req->in.h.nodeid = get_node_id(dir); | |
83 | req->inode = dir; | |
84 | req->in.numargs = 1; | |
85 | req->in.args[0].size = entry->d_name.len + 1; | |
86 | req->in.args[0].value = entry->d_name.name; | |
87 | req->out.numargs = 1; | |
88 | req->out.args[0].size = sizeof(struct fuse_entry_out); | |
89 | req->out.args[0].value = outarg; | |
90 | } | |
91 | ||
6f9f1180 MS |
92 | /* |
93 | * Check whether the dentry is still valid | |
94 | * | |
95 | * If the entry validity timeout has expired and the dentry is | |
96 | * positive, try to redo the lookup. If the lookup results in a | |
97 | * different inode, then let the VFS invalidate the dentry and redo | |
98 | * the lookup once more. If the lookup results in the same inode, | |
99 | * then refresh the attributes, timeouts and mark the dentry valid. | |
100 | */ | |
e5e5558e MS |
101 | static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd) |
102 | { | |
8cbdf1e6 MS |
103 | struct inode *inode = entry->d_inode; |
104 | ||
105 | if (inode && is_bad_inode(inode)) | |
e5e5558e MS |
106 | return 0; |
107 | else if (time_after(jiffies, entry->d_time)) { | |
108 | int err; | |
e5e5558e | 109 | struct fuse_entry_out outarg; |
8cbdf1e6 MS |
110 | struct fuse_conn *fc; |
111 | struct fuse_req *req; | |
112 | ||
6f9f1180 | 113 | /* Doesn't hurt to "reset" the validity timeout */ |
8cbdf1e6 MS |
114 | fuse_invalidate_entry_cache(entry); |
115 | if (!inode) | |
116 | return 0; | |
117 | ||
118 | fc = get_fuse_conn(inode); | |
119 | req = fuse_get_request(fc); | |
e5e5558e MS |
120 | if (!req) |
121 | return 0; | |
122 | ||
123 | fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg); | |
7c352bdf | 124 | request_send(fc, req); |
e5e5558e | 125 | err = req->out.h.error; |
9e6268db | 126 | if (!err) { |
8cbdf1e6 | 127 | struct fuse_inode *fi = get_fuse_inode(inode); |
9e6268db MS |
128 | if (outarg.nodeid != get_node_id(inode)) { |
129 | fuse_send_forget(fc, req, outarg.nodeid, 1); | |
130 | return 0; | |
131 | } | |
132 | fi->nlookup ++; | |
133 | } | |
e5e5558e | 134 | fuse_put_request(fc, req); |
9e6268db | 135 | if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT) |
e5e5558e MS |
136 | return 0; |
137 | ||
138 | fuse_change_attributes(inode, &outarg.attr); | |
0aa7c699 | 139 | fuse_change_timeout(entry, &outarg); |
e5e5558e MS |
140 | } |
141 | return 1; | |
142 | } | |
143 | ||
6f9f1180 MS |
144 | /* |
145 | * Check if there's already a hashed alias of this directory inode. | |
146 | * If yes, then lookup and mkdir must not create a new alias. | |
147 | */ | |
f007d5c9 MS |
148 | static int dir_alias(struct inode *inode) |
149 | { | |
150 | if (S_ISDIR(inode->i_mode)) { | |
f007d5c9 MS |
151 | struct dentry *alias = d_find_alias(inode); |
152 | if (alias) { | |
153 | dput(alias); | |
154 | return 1; | |
155 | } | |
156 | } | |
157 | return 0; | |
158 | } | |
159 | ||
2827d0b2 MS |
160 | static inline int invalid_nodeid(u64 nodeid) |
161 | { | |
162 | return !nodeid || nodeid == FUSE_ROOT_ID; | |
163 | } | |
164 | ||
e5e5558e MS |
165 | static struct dentry_operations fuse_dentry_operations = { |
166 | .d_revalidate = fuse_dentry_revalidate, | |
167 | }; | |
168 | ||
0aa7c699 MS |
169 | static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry, |
170 | struct nameidata *nd) | |
e5e5558e MS |
171 | { |
172 | int err; | |
e5e5558e MS |
173 | struct fuse_entry_out outarg; |
174 | struct inode *inode = NULL; | |
175 | struct fuse_conn *fc = get_fuse_conn(dir); | |
176 | struct fuse_req *req; | |
177 | ||
178 | if (entry->d_name.len > FUSE_NAME_MAX) | |
0aa7c699 | 179 | return ERR_PTR(-ENAMETOOLONG); |
e5e5558e MS |
180 | |
181 | req = fuse_get_request(fc); | |
182 | if (!req) | |
0aa7c699 | 183 | return ERR_PTR(-EINTR); |
e5e5558e MS |
184 | |
185 | fuse_lookup_init(req, dir, entry, &outarg); | |
186 | request_send(fc, req); | |
e5e5558e | 187 | err = req->out.h.error; |
8cbdf1e6 | 188 | if (!err && outarg.nodeid && invalid_nodeid(outarg.nodeid)) |
ee4e5271 | 189 | err = -EIO; |
8cbdf1e6 | 190 | if (!err && outarg.nodeid) { |
e5e5558e | 191 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, |
9e6268db | 192 | &outarg.attr); |
e5e5558e | 193 | if (!inode) { |
9e6268db | 194 | fuse_send_forget(fc, req, outarg.nodeid, 1); |
0aa7c699 | 195 | return ERR_PTR(-ENOMEM); |
e5e5558e MS |
196 | } |
197 | } | |
198 | fuse_put_request(fc, req); | |
199 | if (err && err != -ENOENT) | |
0aa7c699 | 200 | return ERR_PTR(err); |
e5e5558e | 201 | |
0aa7c699 MS |
202 | if (inode && dir_alias(inode)) { |
203 | iput(inode); | |
204 | return ERR_PTR(-EIO); | |
e5e5558e | 205 | } |
0aa7c699 | 206 | d_add(entry, inode); |
e5e5558e | 207 | entry->d_op = &fuse_dentry_operations; |
8cbdf1e6 | 208 | if (!err) |
0aa7c699 | 209 | fuse_change_timeout(entry, &outarg); |
8cbdf1e6 MS |
210 | else |
211 | fuse_invalidate_entry_cache(entry); | |
0aa7c699 | 212 | return NULL; |
e5e5558e MS |
213 | } |
214 | ||
6f9f1180 MS |
215 | /* |
216 | * Atomic create+open operation | |
217 | * | |
218 | * If the filesystem doesn't support this, then fall back to separate | |
219 | * 'mknod' + 'open' requests. | |
220 | */ | |
fd72faac MS |
221 | static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode, |
222 | struct nameidata *nd) | |
223 | { | |
224 | int err; | |
225 | struct inode *inode; | |
226 | struct fuse_conn *fc = get_fuse_conn(dir); | |
227 | struct fuse_req *req; | |
228 | struct fuse_open_in inarg; | |
229 | struct fuse_open_out outopen; | |
230 | struct fuse_entry_out outentry; | |
fd72faac MS |
231 | struct fuse_file *ff; |
232 | struct file *file; | |
233 | int flags = nd->intent.open.flags - 1; | |
234 | ||
235 | err = -ENOSYS; | |
236 | if (fc->no_create) | |
237 | goto out; | |
238 | ||
239 | err = -ENAMETOOLONG; | |
240 | if (entry->d_name.len > FUSE_NAME_MAX) | |
241 | goto out; | |
242 | ||
243 | err = -EINTR; | |
244 | req = fuse_get_request(fc); | |
245 | if (!req) | |
246 | goto out; | |
247 | ||
248 | ff = fuse_file_alloc(); | |
249 | if (!ff) | |
250 | goto out_put_request; | |
251 | ||
252 | flags &= ~O_NOCTTY; | |
253 | memset(&inarg, 0, sizeof(inarg)); | |
254 | inarg.flags = flags; | |
255 | inarg.mode = mode; | |
256 | req->in.h.opcode = FUSE_CREATE; | |
257 | req->in.h.nodeid = get_node_id(dir); | |
258 | req->inode = dir; | |
259 | req->in.numargs = 2; | |
260 | req->in.args[0].size = sizeof(inarg); | |
261 | req->in.args[0].value = &inarg; | |
262 | req->in.args[1].size = entry->d_name.len + 1; | |
263 | req->in.args[1].value = entry->d_name.name; | |
264 | req->out.numargs = 2; | |
265 | req->out.args[0].size = sizeof(outentry); | |
266 | req->out.args[0].value = &outentry; | |
267 | req->out.args[1].size = sizeof(outopen); | |
268 | req->out.args[1].value = &outopen; | |
269 | request_send(fc, req); | |
270 | err = req->out.h.error; | |
271 | if (err) { | |
272 | if (err == -ENOSYS) | |
273 | fc->no_create = 1; | |
274 | goto out_free_ff; | |
275 | } | |
276 | ||
277 | err = -EIO; | |
2827d0b2 | 278 | if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid)) |
fd72faac MS |
279 | goto out_free_ff; |
280 | ||
281 | inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation, | |
282 | &outentry.attr); | |
283 | err = -ENOMEM; | |
284 | if (!inode) { | |
285 | flags &= ~(O_CREAT | O_EXCL | O_TRUNC); | |
286 | ff->fh = outopen.fh; | |
6f9f1180 MS |
287 | /* Special release, with inode = NULL, this will |
288 | trigger a 'forget' request when the release is | |
289 | complete */ | |
fd72faac MS |
290 | fuse_send_release(fc, ff, outentry.nodeid, NULL, flags, 0); |
291 | goto out_put_request; | |
292 | } | |
293 | fuse_put_request(fc, req); | |
fd72faac | 294 | d_instantiate(entry, inode); |
0aa7c699 | 295 | fuse_change_timeout(entry, &outentry); |
fd72faac MS |
296 | file = lookup_instantiate_filp(nd, entry, generic_file_open); |
297 | if (IS_ERR(file)) { | |
298 | ff->fh = outopen.fh; | |
299 | fuse_send_release(fc, ff, outentry.nodeid, inode, flags, 0); | |
300 | return PTR_ERR(file); | |
301 | } | |
302 | fuse_finish_open(inode, file, ff, &outopen); | |
303 | return 0; | |
304 | ||
305 | out_free_ff: | |
306 | fuse_file_free(ff); | |
307 | out_put_request: | |
308 | fuse_put_request(fc, req); | |
309 | out: | |
310 | return err; | |
311 | } | |
312 | ||
6f9f1180 MS |
313 | /* |
314 | * Code shared between mknod, mkdir, symlink and link | |
315 | */ | |
9e6268db MS |
316 | static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req, |
317 | struct inode *dir, struct dentry *entry, | |
318 | int mode) | |
319 | { | |
320 | struct fuse_entry_out outarg; | |
321 | struct inode *inode; | |
9e6268db MS |
322 | int err; |
323 | ||
324 | req->in.h.nodeid = get_node_id(dir); | |
325 | req->inode = dir; | |
326 | req->out.numargs = 1; | |
327 | req->out.args[0].size = sizeof(outarg); | |
328 | req->out.args[0].value = &outarg; | |
329 | request_send(fc, req); | |
330 | err = req->out.h.error; | |
331 | if (err) { | |
332 | fuse_put_request(fc, req); | |
333 | return err; | |
334 | } | |
2827d0b2 | 335 | if (invalid_nodeid(outarg.nodeid)) { |
ee4e5271 MS |
336 | fuse_put_request(fc, req); |
337 | return -EIO; | |
338 | } | |
9e6268db MS |
339 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, |
340 | &outarg.attr); | |
341 | if (!inode) { | |
342 | fuse_send_forget(fc, req, outarg.nodeid, 1); | |
343 | return -ENOMEM; | |
344 | } | |
345 | fuse_put_request(fc, req); | |
346 | ||
347 | /* Don't allow userspace to do really stupid things... */ | |
f007d5c9 | 348 | if (((inode->i_mode ^ mode) & S_IFMT) || dir_alias(inode)) { |
9e6268db MS |
349 | iput(inode); |
350 | return -EIO; | |
351 | } | |
352 | ||
9e6268db | 353 | d_instantiate(entry, inode); |
0aa7c699 | 354 | fuse_change_timeout(entry, &outarg); |
9e6268db MS |
355 | fuse_invalidate_attr(dir); |
356 | return 0; | |
357 | } | |
358 | ||
359 | static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode, | |
360 | dev_t rdev) | |
361 | { | |
362 | struct fuse_mknod_in inarg; | |
363 | struct fuse_conn *fc = get_fuse_conn(dir); | |
364 | struct fuse_req *req = fuse_get_request(fc); | |
365 | if (!req) | |
7c352bdf | 366 | return -EINTR; |
9e6268db MS |
367 | |
368 | memset(&inarg, 0, sizeof(inarg)); | |
369 | inarg.mode = mode; | |
370 | inarg.rdev = new_encode_dev(rdev); | |
371 | req->in.h.opcode = FUSE_MKNOD; | |
372 | req->in.numargs = 2; | |
373 | req->in.args[0].size = sizeof(inarg); | |
374 | req->in.args[0].value = &inarg; | |
375 | req->in.args[1].size = entry->d_name.len + 1; | |
376 | req->in.args[1].value = entry->d_name.name; | |
377 | return create_new_entry(fc, req, dir, entry, mode); | |
378 | } | |
379 | ||
380 | static int fuse_create(struct inode *dir, struct dentry *entry, int mode, | |
381 | struct nameidata *nd) | |
382 | { | |
fd72faac MS |
383 | if (nd && (nd->flags & LOOKUP_CREATE)) { |
384 | int err = fuse_create_open(dir, entry, mode, nd); | |
385 | if (err != -ENOSYS) | |
386 | return err; | |
387 | /* Fall back on mknod */ | |
388 | } | |
9e6268db MS |
389 | return fuse_mknod(dir, entry, mode, 0); |
390 | } | |
391 | ||
392 | static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode) | |
393 | { | |
394 | struct fuse_mkdir_in inarg; | |
395 | struct fuse_conn *fc = get_fuse_conn(dir); | |
396 | struct fuse_req *req = fuse_get_request(fc); | |
397 | if (!req) | |
7c352bdf | 398 | return -EINTR; |
9e6268db MS |
399 | |
400 | memset(&inarg, 0, sizeof(inarg)); | |
401 | inarg.mode = mode; | |
402 | req->in.h.opcode = FUSE_MKDIR; | |
403 | req->in.numargs = 2; | |
404 | req->in.args[0].size = sizeof(inarg); | |
405 | req->in.args[0].value = &inarg; | |
406 | req->in.args[1].size = entry->d_name.len + 1; | |
407 | req->in.args[1].value = entry->d_name.name; | |
408 | return create_new_entry(fc, req, dir, entry, S_IFDIR); | |
409 | } | |
410 | ||
411 | static int fuse_symlink(struct inode *dir, struct dentry *entry, | |
412 | const char *link) | |
413 | { | |
414 | struct fuse_conn *fc = get_fuse_conn(dir); | |
415 | unsigned len = strlen(link) + 1; | |
416 | struct fuse_req *req; | |
417 | ||
418 | if (len > FUSE_SYMLINK_MAX) | |
419 | return -ENAMETOOLONG; | |
420 | ||
421 | req = fuse_get_request(fc); | |
422 | if (!req) | |
7c352bdf | 423 | return -EINTR; |
9e6268db MS |
424 | |
425 | req->in.h.opcode = FUSE_SYMLINK; | |
426 | req->in.numargs = 2; | |
427 | req->in.args[0].size = entry->d_name.len + 1; | |
428 | req->in.args[0].value = entry->d_name.name; | |
429 | req->in.args[1].size = len; | |
430 | req->in.args[1].value = link; | |
431 | return create_new_entry(fc, req, dir, entry, S_IFLNK); | |
432 | } | |
433 | ||
434 | static int fuse_unlink(struct inode *dir, struct dentry *entry) | |
435 | { | |
436 | int err; | |
437 | struct fuse_conn *fc = get_fuse_conn(dir); | |
438 | struct fuse_req *req = fuse_get_request(fc); | |
439 | if (!req) | |
7c352bdf | 440 | return -EINTR; |
9e6268db MS |
441 | |
442 | req->in.h.opcode = FUSE_UNLINK; | |
443 | req->in.h.nodeid = get_node_id(dir); | |
444 | req->inode = dir; | |
445 | req->in.numargs = 1; | |
446 | req->in.args[0].size = entry->d_name.len + 1; | |
447 | req->in.args[0].value = entry->d_name.name; | |
448 | request_send(fc, req); | |
449 | err = req->out.h.error; | |
450 | fuse_put_request(fc, req); | |
451 | if (!err) { | |
452 | struct inode *inode = entry->d_inode; | |
453 | ||
454 | /* Set nlink to zero so the inode can be cleared, if | |
455 | the inode does have more links this will be | |
456 | discovered at the next lookup/getattr */ | |
457 | inode->i_nlink = 0; | |
458 | fuse_invalidate_attr(inode); | |
459 | fuse_invalidate_attr(dir); | |
8cbdf1e6 | 460 | fuse_invalidate_entry_cache(entry); |
9e6268db MS |
461 | } else if (err == -EINTR) |
462 | fuse_invalidate_entry(entry); | |
463 | return err; | |
464 | } | |
465 | ||
466 | static int fuse_rmdir(struct inode *dir, struct dentry *entry) | |
467 | { | |
468 | int err; | |
469 | struct fuse_conn *fc = get_fuse_conn(dir); | |
470 | struct fuse_req *req = fuse_get_request(fc); | |
471 | if (!req) | |
7c352bdf | 472 | return -EINTR; |
9e6268db MS |
473 | |
474 | req->in.h.opcode = FUSE_RMDIR; | |
475 | req->in.h.nodeid = get_node_id(dir); | |
476 | req->inode = dir; | |
477 | req->in.numargs = 1; | |
478 | req->in.args[0].size = entry->d_name.len + 1; | |
479 | req->in.args[0].value = entry->d_name.name; | |
480 | request_send(fc, req); | |
481 | err = req->out.h.error; | |
482 | fuse_put_request(fc, req); | |
483 | if (!err) { | |
484 | entry->d_inode->i_nlink = 0; | |
485 | fuse_invalidate_attr(dir); | |
8cbdf1e6 | 486 | fuse_invalidate_entry_cache(entry); |
9e6268db MS |
487 | } else if (err == -EINTR) |
488 | fuse_invalidate_entry(entry); | |
489 | return err; | |
490 | } | |
491 | ||
492 | static int fuse_rename(struct inode *olddir, struct dentry *oldent, | |
493 | struct inode *newdir, struct dentry *newent) | |
494 | { | |
495 | int err; | |
496 | struct fuse_rename_in inarg; | |
497 | struct fuse_conn *fc = get_fuse_conn(olddir); | |
498 | struct fuse_req *req = fuse_get_request(fc); | |
499 | if (!req) | |
7c352bdf | 500 | return -EINTR; |
9e6268db MS |
501 | |
502 | memset(&inarg, 0, sizeof(inarg)); | |
503 | inarg.newdir = get_node_id(newdir); | |
504 | req->in.h.opcode = FUSE_RENAME; | |
505 | req->in.h.nodeid = get_node_id(olddir); | |
506 | req->inode = olddir; | |
507 | req->inode2 = newdir; | |
508 | req->in.numargs = 3; | |
509 | req->in.args[0].size = sizeof(inarg); | |
510 | req->in.args[0].value = &inarg; | |
511 | req->in.args[1].size = oldent->d_name.len + 1; | |
512 | req->in.args[1].value = oldent->d_name.name; | |
513 | req->in.args[2].size = newent->d_name.len + 1; | |
514 | req->in.args[2].value = newent->d_name.name; | |
515 | request_send(fc, req); | |
516 | err = req->out.h.error; | |
517 | fuse_put_request(fc, req); | |
518 | if (!err) { | |
519 | fuse_invalidate_attr(olddir); | |
520 | if (olddir != newdir) | |
521 | fuse_invalidate_attr(newdir); | |
8cbdf1e6 MS |
522 | |
523 | /* newent will end up negative */ | |
524 | if (newent->d_inode) | |
525 | fuse_invalidate_entry_cache(newent); | |
9e6268db MS |
526 | } else if (err == -EINTR) { |
527 | /* If request was interrupted, DEITY only knows if the | |
528 | rename actually took place. If the invalidation | |
529 | fails (e.g. some process has CWD under the renamed | |
530 | directory), then there can be inconsistency between | |
531 | the dcache and the real filesystem. Tough luck. */ | |
532 | fuse_invalidate_entry(oldent); | |
533 | if (newent->d_inode) | |
534 | fuse_invalidate_entry(newent); | |
535 | } | |
536 | ||
537 | return err; | |
538 | } | |
539 | ||
540 | static int fuse_link(struct dentry *entry, struct inode *newdir, | |
541 | struct dentry *newent) | |
542 | { | |
543 | int err; | |
544 | struct fuse_link_in inarg; | |
545 | struct inode *inode = entry->d_inode; | |
546 | struct fuse_conn *fc = get_fuse_conn(inode); | |
547 | struct fuse_req *req = fuse_get_request(fc); | |
548 | if (!req) | |
7c352bdf | 549 | return -EINTR; |
9e6268db MS |
550 | |
551 | memset(&inarg, 0, sizeof(inarg)); | |
552 | inarg.oldnodeid = get_node_id(inode); | |
553 | req->in.h.opcode = FUSE_LINK; | |
554 | req->inode2 = inode; | |
555 | req->in.numargs = 2; | |
556 | req->in.args[0].size = sizeof(inarg); | |
557 | req->in.args[0].value = &inarg; | |
558 | req->in.args[1].size = newent->d_name.len + 1; | |
559 | req->in.args[1].value = newent->d_name.name; | |
560 | err = create_new_entry(fc, req, newdir, newent, inode->i_mode); | |
561 | /* Contrary to "normal" filesystems it can happen that link | |
562 | makes two "logical" inodes point to the same "physical" | |
563 | inode. We invalidate the attributes of the old one, so it | |
564 | will reflect changes in the backing inode (link count, | |
565 | etc.) | |
566 | */ | |
567 | if (!err || err == -EINTR) | |
568 | fuse_invalidate_attr(inode); | |
569 | return err; | |
570 | } | |
571 | ||
e5e5558e MS |
572 | int fuse_do_getattr(struct inode *inode) |
573 | { | |
574 | int err; | |
575 | struct fuse_attr_out arg; | |
576 | struct fuse_conn *fc = get_fuse_conn(inode); | |
577 | struct fuse_req *req = fuse_get_request(fc); | |
578 | if (!req) | |
7c352bdf | 579 | return -EINTR; |
e5e5558e MS |
580 | |
581 | req->in.h.opcode = FUSE_GETATTR; | |
582 | req->in.h.nodeid = get_node_id(inode); | |
583 | req->inode = inode; | |
584 | req->out.numargs = 1; | |
585 | req->out.args[0].size = sizeof(arg); | |
586 | req->out.args[0].value = &arg; | |
587 | request_send(fc, req); | |
588 | err = req->out.h.error; | |
589 | fuse_put_request(fc, req); | |
590 | if (!err) { | |
591 | if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) { | |
592 | make_bad_inode(inode); | |
593 | err = -EIO; | |
594 | } else { | |
595 | struct fuse_inode *fi = get_fuse_inode(inode); | |
596 | fuse_change_attributes(inode, &arg.attr); | |
597 | fi->i_time = time_to_jiffies(arg.attr_valid, | |
598 | arg.attr_valid_nsec); | |
599 | } | |
600 | } | |
601 | return err; | |
602 | } | |
603 | ||
87729a55 MS |
604 | /* |
605 | * Calling into a user-controlled filesystem gives the filesystem | |
606 | * daemon ptrace-like capabilities over the requester process. This | |
607 | * means, that the filesystem daemon is able to record the exact | |
608 | * filesystem operations performed, and can also control the behavior | |
609 | * of the requester process in otherwise impossible ways. For example | |
610 | * it can delay the operation for arbitrary length of time allowing | |
611 | * DoS against the requester. | |
612 | * | |
613 | * For this reason only those processes can call into the filesystem, | |
614 | * for which the owner of the mount has ptrace privilege. This | |
615 | * excludes processes started by other users, suid or sgid processes. | |
616 | */ | |
617 | static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task) | |
618 | { | |
619 | if (fc->flags & FUSE_ALLOW_OTHER) | |
620 | return 1; | |
621 | ||
622 | if (task->euid == fc->user_id && | |
623 | task->suid == fc->user_id && | |
624 | task->uid == fc->user_id && | |
625 | task->egid == fc->group_id && | |
626 | task->sgid == fc->group_id && | |
627 | task->gid == fc->group_id) | |
628 | return 1; | |
629 | ||
630 | return 0; | |
631 | } | |
632 | ||
6f9f1180 MS |
633 | /* |
634 | * Check whether the inode attributes are still valid | |
635 | * | |
636 | * If the attribute validity timeout has expired, then fetch the fresh | |
637 | * attributes with a 'getattr' request | |
638 | * | |
639 | * I'm not sure why cached attributes are never returned for the root | |
640 | * inode, this is probably being too cautious. | |
641 | */ | |
e5e5558e MS |
642 | static int fuse_revalidate(struct dentry *entry) |
643 | { | |
644 | struct inode *inode = entry->d_inode; | |
645 | struct fuse_inode *fi = get_fuse_inode(inode); | |
646 | struct fuse_conn *fc = get_fuse_conn(inode); | |
647 | ||
87729a55 MS |
648 | if (!fuse_allow_task(fc, current)) |
649 | return -EACCES; | |
650 | if (get_node_id(inode) != FUSE_ROOT_ID && | |
651 | time_before_eq(jiffies, fi->i_time)) | |
e5e5558e MS |
652 | return 0; |
653 | ||
654 | return fuse_do_getattr(inode); | |
655 | } | |
656 | ||
31d40d74 MS |
657 | static int fuse_access(struct inode *inode, int mask) |
658 | { | |
659 | struct fuse_conn *fc = get_fuse_conn(inode); | |
660 | struct fuse_req *req; | |
661 | struct fuse_access_in inarg; | |
662 | int err; | |
663 | ||
664 | if (fc->no_access) | |
665 | return 0; | |
666 | ||
667 | req = fuse_get_request(fc); | |
668 | if (!req) | |
669 | return -EINTR; | |
670 | ||
671 | memset(&inarg, 0, sizeof(inarg)); | |
672 | inarg.mask = mask; | |
673 | req->in.h.opcode = FUSE_ACCESS; | |
674 | req->in.h.nodeid = get_node_id(inode); | |
675 | req->inode = inode; | |
676 | req->in.numargs = 1; | |
677 | req->in.args[0].size = sizeof(inarg); | |
678 | req->in.args[0].value = &inarg; | |
679 | request_send(fc, req); | |
680 | err = req->out.h.error; | |
681 | fuse_put_request(fc, req); | |
682 | if (err == -ENOSYS) { | |
683 | fc->no_access = 1; | |
684 | err = 0; | |
685 | } | |
686 | return err; | |
687 | } | |
688 | ||
6f9f1180 MS |
689 | /* |
690 | * Check permission. The two basic access models of FUSE are: | |
691 | * | |
692 | * 1) Local access checking ('default_permissions' mount option) based | |
693 | * on file mode. This is the plain old disk filesystem permission | |
694 | * modell. | |
695 | * | |
696 | * 2) "Remote" access checking, where server is responsible for | |
697 | * checking permission in each inode operation. An exception to this | |
698 | * is if ->permission() was invoked from sys_access() in which case an | |
699 | * access request is sent. Execute permission is still checked | |
700 | * locally based on file mode. | |
701 | */ | |
e5e5558e MS |
702 | static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd) |
703 | { | |
704 | struct fuse_conn *fc = get_fuse_conn(inode); | |
705 | ||
87729a55 | 706 | if (!fuse_allow_task(fc, current)) |
e5e5558e | 707 | return -EACCES; |
1e9a4ed9 MS |
708 | else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { |
709 | int err = generic_permission(inode, mask, NULL); | |
710 | ||
711 | /* If permission is denied, try to refresh file | |
712 | attributes. This is also needed, because the root | |
713 | node will at first have no permissions */ | |
714 | if (err == -EACCES) { | |
715 | err = fuse_do_getattr(inode); | |
716 | if (!err) | |
717 | err = generic_permission(inode, mask, NULL); | |
718 | } | |
719 | ||
6f9f1180 MS |
720 | /* Note: the opposite of the above test does not |
721 | exist. So if permissions are revoked this won't be | |
722 | noticed immediately, only after the attribute | |
723 | timeout has expired */ | |
1e9a4ed9 MS |
724 | |
725 | return err; | |
726 | } else { | |
e5e5558e | 727 | int mode = inode->i_mode; |
e5e5558e MS |
728 | if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO)) |
729 | return -EACCES; | |
31d40d74 MS |
730 | |
731 | if (nd && (nd->flags & LOOKUP_ACCESS)) | |
732 | return fuse_access(inode, mask); | |
e5e5558e MS |
733 | return 0; |
734 | } | |
735 | } | |
736 | ||
737 | static int parse_dirfile(char *buf, size_t nbytes, struct file *file, | |
738 | void *dstbuf, filldir_t filldir) | |
739 | { | |
740 | while (nbytes >= FUSE_NAME_OFFSET) { | |
741 | struct fuse_dirent *dirent = (struct fuse_dirent *) buf; | |
742 | size_t reclen = FUSE_DIRENT_SIZE(dirent); | |
743 | int over; | |
744 | if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX) | |
745 | return -EIO; | |
746 | if (reclen > nbytes) | |
747 | break; | |
748 | ||
749 | over = filldir(dstbuf, dirent->name, dirent->namelen, | |
750 | file->f_pos, dirent->ino, dirent->type); | |
751 | if (over) | |
752 | break; | |
753 | ||
754 | buf += reclen; | |
755 | nbytes -= reclen; | |
756 | file->f_pos = dirent->off; | |
757 | } | |
758 | ||
759 | return 0; | |
760 | } | |
761 | ||
04730fef MS |
762 | static inline size_t fuse_send_readdir(struct fuse_req *req, struct file *file, |
763 | struct inode *inode, loff_t pos, | |
764 | size_t count) | |
e5e5558e | 765 | { |
04730fef | 766 | return fuse_send_read_common(req, file, inode, pos, count, 1); |
e5e5558e MS |
767 | } |
768 | ||
04730fef | 769 | static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir) |
e5e5558e | 770 | { |
04730fef MS |
771 | int err; |
772 | size_t nbytes; | |
773 | struct page *page; | |
e5e5558e MS |
774 | struct inode *inode = file->f_dentry->d_inode; |
775 | struct fuse_conn *fc = get_fuse_conn(inode); | |
7c352bdf | 776 | struct fuse_req *req = fuse_get_request(fc); |
e5e5558e | 777 | if (!req) |
04730fef | 778 | return -EINTR; |
e5e5558e | 779 | |
04730fef MS |
780 | page = alloc_page(GFP_KERNEL); |
781 | if (!page) { | |
782 | fuse_put_request(fc, req); | |
783 | return -ENOMEM; | |
784 | } | |
785 | req->num_pages = 1; | |
786 | req->pages[0] = page; | |
787 | nbytes = fuse_send_readdir(req, file, inode, file->f_pos, PAGE_SIZE); | |
e5e5558e MS |
788 | err = req->out.h.error; |
789 | fuse_put_request(fc, req); | |
790 | if (!err) | |
04730fef MS |
791 | err = parse_dirfile(page_address(page), nbytes, file, dstbuf, |
792 | filldir); | |
e5e5558e | 793 | |
04730fef | 794 | __free_page(page); |
b36c31ba | 795 | fuse_invalidate_attr(inode); /* atime changed */ |
04730fef | 796 | return err; |
e5e5558e MS |
797 | } |
798 | ||
799 | static char *read_link(struct dentry *dentry) | |
800 | { | |
801 | struct inode *inode = dentry->d_inode; | |
802 | struct fuse_conn *fc = get_fuse_conn(inode); | |
803 | struct fuse_req *req = fuse_get_request(fc); | |
804 | char *link; | |
805 | ||
806 | if (!req) | |
7c352bdf | 807 | return ERR_PTR(-EINTR); |
e5e5558e MS |
808 | |
809 | link = (char *) __get_free_page(GFP_KERNEL); | |
810 | if (!link) { | |
811 | link = ERR_PTR(-ENOMEM); | |
812 | goto out; | |
813 | } | |
814 | req->in.h.opcode = FUSE_READLINK; | |
815 | req->in.h.nodeid = get_node_id(inode); | |
816 | req->inode = inode; | |
817 | req->out.argvar = 1; | |
818 | req->out.numargs = 1; | |
819 | req->out.args[0].size = PAGE_SIZE - 1; | |
820 | req->out.args[0].value = link; | |
821 | request_send(fc, req); | |
822 | if (req->out.h.error) { | |
823 | free_page((unsigned long) link); | |
824 | link = ERR_PTR(req->out.h.error); | |
825 | } else | |
826 | link[req->out.args[0].size] = '\0'; | |
827 | out: | |
828 | fuse_put_request(fc, req); | |
b36c31ba | 829 | fuse_invalidate_attr(inode); /* atime changed */ |
e5e5558e MS |
830 | return link; |
831 | } | |
832 | ||
833 | static void free_link(char *link) | |
834 | { | |
835 | if (!IS_ERR(link)) | |
836 | free_page((unsigned long) link); | |
837 | } | |
838 | ||
839 | static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd) | |
840 | { | |
841 | nd_set_link(nd, read_link(dentry)); | |
842 | return NULL; | |
843 | } | |
844 | ||
845 | static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c) | |
846 | { | |
847 | free_link(nd_get_link(nd)); | |
848 | } | |
849 | ||
850 | static int fuse_dir_open(struct inode *inode, struct file *file) | |
851 | { | |
04730fef | 852 | return fuse_open_common(inode, file, 1); |
e5e5558e MS |
853 | } |
854 | ||
855 | static int fuse_dir_release(struct inode *inode, struct file *file) | |
856 | { | |
04730fef | 857 | return fuse_release_common(inode, file, 1); |
e5e5558e MS |
858 | } |
859 | ||
82547981 MS |
860 | static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync) |
861 | { | |
862 | /* nfsd can call this with no file */ | |
863 | return file ? fuse_fsync_common(file, de, datasync, 1) : 0; | |
864 | } | |
865 | ||
befc649c | 866 | static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg) |
9e6268db MS |
867 | { |
868 | unsigned ivalid = iattr->ia_valid; | |
9e6268db MS |
869 | |
870 | if (ivalid & ATTR_MODE) | |
befc649c | 871 | arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode; |
9e6268db | 872 | if (ivalid & ATTR_UID) |
befc649c | 873 | arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid; |
9e6268db | 874 | if (ivalid & ATTR_GID) |
befc649c | 875 | arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid; |
9e6268db | 876 | if (ivalid & ATTR_SIZE) |
befc649c | 877 | arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size; |
9e6268db MS |
878 | /* You can only _set_ these together (they may change by themselves) */ |
879 | if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) { | |
befc649c MS |
880 | arg->valid |= FATTR_ATIME | FATTR_MTIME; |
881 | arg->atime = iattr->ia_atime.tv_sec; | |
882 | arg->mtime = iattr->ia_mtime.tv_sec; | |
883 | } | |
884 | if (ivalid & ATTR_FILE) { | |
885 | struct fuse_file *ff = iattr->ia_file->private_data; | |
886 | arg->valid |= FATTR_FH; | |
887 | arg->fh = ff->fh; | |
9e6268db | 888 | } |
9e6268db MS |
889 | } |
890 | ||
6f9f1180 MS |
891 | /* |
892 | * Set attributes, and at the same time refresh them. | |
893 | * | |
894 | * Truncation is slightly complicated, because the 'truncate' request | |
895 | * may fail, in which case we don't want to touch the mapping. | |
896 | * vmtruncate() doesn't allow for this case. So do the rlimit | |
897 | * checking by hand and call vmtruncate() only after the file has | |
898 | * actually been truncated. | |
899 | */ | |
9e6268db MS |
900 | static int fuse_setattr(struct dentry *entry, struct iattr *attr) |
901 | { | |
902 | struct inode *inode = entry->d_inode; | |
903 | struct fuse_conn *fc = get_fuse_conn(inode); | |
904 | struct fuse_inode *fi = get_fuse_inode(inode); | |
905 | struct fuse_req *req; | |
906 | struct fuse_setattr_in inarg; | |
907 | struct fuse_attr_out outarg; | |
908 | int err; | |
909 | int is_truncate = 0; | |
910 | ||
1e9a4ed9 MS |
911 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { |
912 | err = inode_change_ok(inode, attr); | |
913 | if (err) | |
914 | return err; | |
915 | } | |
916 | ||
9e6268db MS |
917 | if (attr->ia_valid & ATTR_SIZE) { |
918 | unsigned long limit; | |
919 | is_truncate = 1; | |
920 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; | |
921 | if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) { | |
922 | send_sig(SIGXFSZ, current, 0); | |
923 | return -EFBIG; | |
924 | } | |
925 | } | |
926 | ||
927 | req = fuse_get_request(fc); | |
928 | if (!req) | |
7c352bdf | 929 | return -EINTR; |
9e6268db MS |
930 | |
931 | memset(&inarg, 0, sizeof(inarg)); | |
befc649c | 932 | iattr_to_fattr(attr, &inarg); |
9e6268db MS |
933 | req->in.h.opcode = FUSE_SETATTR; |
934 | req->in.h.nodeid = get_node_id(inode); | |
935 | req->inode = inode; | |
936 | req->in.numargs = 1; | |
937 | req->in.args[0].size = sizeof(inarg); | |
938 | req->in.args[0].value = &inarg; | |
939 | req->out.numargs = 1; | |
940 | req->out.args[0].size = sizeof(outarg); | |
941 | req->out.args[0].value = &outarg; | |
942 | request_send(fc, req); | |
943 | err = req->out.h.error; | |
944 | fuse_put_request(fc, req); | |
945 | if (!err) { | |
946 | if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { | |
947 | make_bad_inode(inode); | |
948 | err = -EIO; | |
949 | } else { | |
950 | if (is_truncate) { | |
951 | loff_t origsize = i_size_read(inode); | |
952 | i_size_write(inode, outarg.attr.size); | |
953 | if (origsize > outarg.attr.size) | |
954 | vmtruncate(inode, outarg.attr.size); | |
955 | } | |
956 | fuse_change_attributes(inode, &outarg.attr); | |
957 | fi->i_time = time_to_jiffies(outarg.attr_valid, | |
958 | outarg.attr_valid_nsec); | |
959 | } | |
960 | } else if (err == -EINTR) | |
961 | fuse_invalidate_attr(inode); | |
962 | ||
963 | return err; | |
964 | } | |
965 | ||
e5e5558e MS |
966 | static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry, |
967 | struct kstat *stat) | |
968 | { | |
969 | struct inode *inode = entry->d_inode; | |
970 | int err = fuse_revalidate(entry); | |
971 | if (!err) | |
972 | generic_fillattr(inode, stat); | |
973 | ||
974 | return err; | |
975 | } | |
976 | ||
92a8780e MS |
977 | static int fuse_setxattr(struct dentry *entry, const char *name, |
978 | const void *value, size_t size, int flags) | |
979 | { | |
980 | struct inode *inode = entry->d_inode; | |
981 | struct fuse_conn *fc = get_fuse_conn(inode); | |
982 | struct fuse_req *req; | |
983 | struct fuse_setxattr_in inarg; | |
984 | int err; | |
985 | ||
986 | if (size > FUSE_XATTR_SIZE_MAX) | |
987 | return -E2BIG; | |
988 | ||
989 | if (fc->no_setxattr) | |
990 | return -EOPNOTSUPP; | |
991 | ||
992 | req = fuse_get_request(fc); | |
993 | if (!req) | |
7c352bdf | 994 | return -EINTR; |
92a8780e MS |
995 | |
996 | memset(&inarg, 0, sizeof(inarg)); | |
997 | inarg.size = size; | |
998 | inarg.flags = flags; | |
999 | req->in.h.opcode = FUSE_SETXATTR; | |
1000 | req->in.h.nodeid = get_node_id(inode); | |
1001 | req->inode = inode; | |
1002 | req->in.numargs = 3; | |
1003 | req->in.args[0].size = sizeof(inarg); | |
1004 | req->in.args[0].value = &inarg; | |
1005 | req->in.args[1].size = strlen(name) + 1; | |
1006 | req->in.args[1].value = name; | |
1007 | req->in.args[2].size = size; | |
1008 | req->in.args[2].value = value; | |
1009 | request_send(fc, req); | |
1010 | err = req->out.h.error; | |
1011 | fuse_put_request(fc, req); | |
1012 | if (err == -ENOSYS) { | |
1013 | fc->no_setxattr = 1; | |
1014 | err = -EOPNOTSUPP; | |
1015 | } | |
1016 | return err; | |
1017 | } | |
1018 | ||
1019 | static ssize_t fuse_getxattr(struct dentry *entry, const char *name, | |
1020 | void *value, size_t size) | |
1021 | { | |
1022 | struct inode *inode = entry->d_inode; | |
1023 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1024 | struct fuse_req *req; | |
1025 | struct fuse_getxattr_in inarg; | |
1026 | struct fuse_getxattr_out outarg; | |
1027 | ssize_t ret; | |
1028 | ||
1029 | if (fc->no_getxattr) | |
1030 | return -EOPNOTSUPP; | |
1031 | ||
1032 | req = fuse_get_request(fc); | |
1033 | if (!req) | |
7c352bdf | 1034 | return -EINTR; |
92a8780e MS |
1035 | |
1036 | memset(&inarg, 0, sizeof(inarg)); | |
1037 | inarg.size = size; | |
1038 | req->in.h.opcode = FUSE_GETXATTR; | |
1039 | req->in.h.nodeid = get_node_id(inode); | |
1040 | req->inode = inode; | |
1041 | req->in.numargs = 2; | |
1042 | req->in.args[0].size = sizeof(inarg); | |
1043 | req->in.args[0].value = &inarg; | |
1044 | req->in.args[1].size = strlen(name) + 1; | |
1045 | req->in.args[1].value = name; | |
1046 | /* This is really two different operations rolled into one */ | |
1047 | req->out.numargs = 1; | |
1048 | if (size) { | |
1049 | req->out.argvar = 1; | |
1050 | req->out.args[0].size = size; | |
1051 | req->out.args[0].value = value; | |
1052 | } else { | |
1053 | req->out.args[0].size = sizeof(outarg); | |
1054 | req->out.args[0].value = &outarg; | |
1055 | } | |
1056 | request_send(fc, req); | |
1057 | ret = req->out.h.error; | |
1058 | if (!ret) | |
1059 | ret = size ? req->out.args[0].size : outarg.size; | |
1060 | else { | |
1061 | if (ret == -ENOSYS) { | |
1062 | fc->no_getxattr = 1; | |
1063 | ret = -EOPNOTSUPP; | |
1064 | } | |
1065 | } | |
1066 | fuse_put_request(fc, req); | |
1067 | return ret; | |
1068 | } | |
1069 | ||
1070 | static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size) | |
1071 | { | |
1072 | struct inode *inode = entry->d_inode; | |
1073 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1074 | struct fuse_req *req; | |
1075 | struct fuse_getxattr_in inarg; | |
1076 | struct fuse_getxattr_out outarg; | |
1077 | ssize_t ret; | |
1078 | ||
1079 | if (fc->no_listxattr) | |
1080 | return -EOPNOTSUPP; | |
1081 | ||
1082 | req = fuse_get_request(fc); | |
1083 | if (!req) | |
7c352bdf | 1084 | return -EINTR; |
92a8780e MS |
1085 | |
1086 | memset(&inarg, 0, sizeof(inarg)); | |
1087 | inarg.size = size; | |
1088 | req->in.h.opcode = FUSE_LISTXATTR; | |
1089 | req->in.h.nodeid = get_node_id(inode); | |
1090 | req->inode = inode; | |
1091 | req->in.numargs = 1; | |
1092 | req->in.args[0].size = sizeof(inarg); | |
1093 | req->in.args[0].value = &inarg; | |
1094 | /* This is really two different operations rolled into one */ | |
1095 | req->out.numargs = 1; | |
1096 | if (size) { | |
1097 | req->out.argvar = 1; | |
1098 | req->out.args[0].size = size; | |
1099 | req->out.args[0].value = list; | |
1100 | } else { | |
1101 | req->out.args[0].size = sizeof(outarg); | |
1102 | req->out.args[0].value = &outarg; | |
1103 | } | |
1104 | request_send(fc, req); | |
1105 | ret = req->out.h.error; | |
1106 | if (!ret) | |
1107 | ret = size ? req->out.args[0].size : outarg.size; | |
1108 | else { | |
1109 | if (ret == -ENOSYS) { | |
1110 | fc->no_listxattr = 1; | |
1111 | ret = -EOPNOTSUPP; | |
1112 | } | |
1113 | } | |
1114 | fuse_put_request(fc, req); | |
1115 | return ret; | |
1116 | } | |
1117 | ||
1118 | static int fuse_removexattr(struct dentry *entry, const char *name) | |
1119 | { | |
1120 | struct inode *inode = entry->d_inode; | |
1121 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1122 | struct fuse_req *req; | |
1123 | int err; | |
1124 | ||
1125 | if (fc->no_removexattr) | |
1126 | return -EOPNOTSUPP; | |
1127 | ||
1128 | req = fuse_get_request(fc); | |
1129 | if (!req) | |
7c352bdf | 1130 | return -EINTR; |
92a8780e MS |
1131 | |
1132 | req->in.h.opcode = FUSE_REMOVEXATTR; | |
1133 | req->in.h.nodeid = get_node_id(inode); | |
1134 | req->inode = inode; | |
1135 | req->in.numargs = 1; | |
1136 | req->in.args[0].size = strlen(name) + 1; | |
1137 | req->in.args[0].value = name; | |
1138 | request_send(fc, req); | |
1139 | err = req->out.h.error; | |
1140 | fuse_put_request(fc, req); | |
1141 | if (err == -ENOSYS) { | |
1142 | fc->no_removexattr = 1; | |
1143 | err = -EOPNOTSUPP; | |
1144 | } | |
1145 | return err; | |
1146 | } | |
1147 | ||
e5e5558e MS |
1148 | static struct inode_operations fuse_dir_inode_operations = { |
1149 | .lookup = fuse_lookup, | |
9e6268db MS |
1150 | .mkdir = fuse_mkdir, |
1151 | .symlink = fuse_symlink, | |
1152 | .unlink = fuse_unlink, | |
1153 | .rmdir = fuse_rmdir, | |
1154 | .rename = fuse_rename, | |
1155 | .link = fuse_link, | |
1156 | .setattr = fuse_setattr, | |
1157 | .create = fuse_create, | |
1158 | .mknod = fuse_mknod, | |
e5e5558e MS |
1159 | .permission = fuse_permission, |
1160 | .getattr = fuse_getattr, | |
92a8780e MS |
1161 | .setxattr = fuse_setxattr, |
1162 | .getxattr = fuse_getxattr, | |
1163 | .listxattr = fuse_listxattr, | |
1164 | .removexattr = fuse_removexattr, | |
e5e5558e MS |
1165 | }; |
1166 | ||
1167 | static struct file_operations fuse_dir_operations = { | |
b6aeaded | 1168 | .llseek = generic_file_llseek, |
e5e5558e MS |
1169 | .read = generic_read_dir, |
1170 | .readdir = fuse_readdir, | |
1171 | .open = fuse_dir_open, | |
1172 | .release = fuse_dir_release, | |
82547981 | 1173 | .fsync = fuse_dir_fsync, |
e5e5558e MS |
1174 | }; |
1175 | ||
1176 | static struct inode_operations fuse_common_inode_operations = { | |
9e6268db | 1177 | .setattr = fuse_setattr, |
e5e5558e MS |
1178 | .permission = fuse_permission, |
1179 | .getattr = fuse_getattr, | |
92a8780e MS |
1180 | .setxattr = fuse_setxattr, |
1181 | .getxattr = fuse_getxattr, | |
1182 | .listxattr = fuse_listxattr, | |
1183 | .removexattr = fuse_removexattr, | |
e5e5558e MS |
1184 | }; |
1185 | ||
1186 | static struct inode_operations fuse_symlink_inode_operations = { | |
9e6268db | 1187 | .setattr = fuse_setattr, |
e5e5558e MS |
1188 | .follow_link = fuse_follow_link, |
1189 | .put_link = fuse_put_link, | |
1190 | .readlink = generic_readlink, | |
1191 | .getattr = fuse_getattr, | |
92a8780e MS |
1192 | .setxattr = fuse_setxattr, |
1193 | .getxattr = fuse_getxattr, | |
1194 | .listxattr = fuse_listxattr, | |
1195 | .removexattr = fuse_removexattr, | |
e5e5558e MS |
1196 | }; |
1197 | ||
1198 | void fuse_init_common(struct inode *inode) | |
1199 | { | |
1200 | inode->i_op = &fuse_common_inode_operations; | |
1201 | } | |
1202 | ||
1203 | void fuse_init_dir(struct inode *inode) | |
1204 | { | |
1205 | inode->i_op = &fuse_dir_inode_operations; | |
1206 | inode->i_fop = &fuse_dir_operations; | |
1207 | } | |
1208 | ||
1209 | void fuse_init_symlink(struct inode *inode) | |
1210 | { | |
1211 | inode->i_op = &fuse_symlink_inode_operations; | |
1212 | } |