blob: 29f1e9f6e85c3648c8a3deeb7847d078625e9844 [file] [log] [blame]
Miklos Szeredie5e55582005-09-09 13:10:28 -07001/*
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
17static inline unsigned long time_to_jiffies(unsigned long sec,
18 unsigned long nsec)
19{
20 struct timespec ts = {sec, nsec};
21 return jiffies + timespec_to_jiffies(&ts);
22}
23
24static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
25 struct dentry *entry,
26 struct fuse_entry_out *outarg)
27{
28 req->in.h.opcode = FUSE_LOOKUP;
29 req->in.h.nodeid = get_node_id(dir);
30 req->inode = dir;
31 req->in.numargs = 1;
32 req->in.args[0].size = entry->d_name.len + 1;
33 req->in.args[0].value = entry->d_name.name;
34 req->out.numargs = 1;
35 req->out.args[0].size = sizeof(struct fuse_entry_out);
36 req->out.args[0].value = outarg;
37}
38
39static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
40{
41 if (!entry->d_inode || is_bad_inode(entry->d_inode))
42 return 0;
43 else if (time_after(jiffies, entry->d_time)) {
44 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -070045 struct fuse_entry_out outarg;
46 struct inode *inode = entry->d_inode;
47 struct fuse_inode *fi = get_fuse_inode(inode);
48 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -070049 struct fuse_req *req = fuse_get_request(fc);
Miklos Szeredie5e55582005-09-09 13:10:28 -070050 if (!req)
51 return 0;
52
53 fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -070054 request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -070055 err = req->out.h.error;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070056 if (!err) {
57 if (outarg.nodeid != get_node_id(inode)) {
58 fuse_send_forget(fc, req, outarg.nodeid, 1);
59 return 0;
60 }
61 fi->nlookup ++;
62 }
Miklos Szeredie5e55582005-09-09 13:10:28 -070063 fuse_put_request(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070064 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
Miklos Szeredie5e55582005-09-09 13:10:28 -070065 return 0;
66
67 fuse_change_attributes(inode, &outarg.attr);
Miklos Szeredie5e55582005-09-09 13:10:28 -070068 entry->d_time = time_to_jiffies(outarg.entry_valid,
69 outarg.entry_valid_nsec);
70 fi->i_time = time_to_jiffies(outarg.attr_valid,
71 outarg.attr_valid_nsec);
72 }
73 return 1;
74}
75
76static struct dentry_operations fuse_dentry_operations = {
77 .d_revalidate = fuse_dentry_revalidate,
78};
79
80static int fuse_lookup_iget(struct inode *dir, struct dentry *entry,
81 struct inode **inodep)
82{
83 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -070084 struct fuse_entry_out outarg;
85 struct inode *inode = NULL;
86 struct fuse_conn *fc = get_fuse_conn(dir);
87 struct fuse_req *req;
88
89 if (entry->d_name.len > FUSE_NAME_MAX)
90 return -ENAMETOOLONG;
91
92 req = fuse_get_request(fc);
93 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -070094 return -EINTR;
Miklos Szeredie5e55582005-09-09 13:10:28 -070095
96 fuse_lookup_init(req, dir, entry, &outarg);
97 request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -070098 err = req->out.h.error;
Miklos Szerediee4e5272005-09-27 21:45:21 -070099 if (!err && (!outarg.nodeid || outarg.nodeid == FUSE_ROOT_ID))
100 err = -EIO;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700101 if (!err) {
102 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700103 &outarg.attr);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700104 if (!inode) {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700105 fuse_send_forget(fc, req, outarg.nodeid, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700106 return -ENOMEM;
107 }
108 }
109 fuse_put_request(fc, req);
110 if (err && err != -ENOENT)
111 return err;
112
113 if (inode) {
114 struct fuse_inode *fi = get_fuse_inode(inode);
115 entry->d_time = time_to_jiffies(outarg.entry_valid,
116 outarg.entry_valid_nsec);
117 fi->i_time = time_to_jiffies(outarg.attr_valid,
118 outarg.attr_valid_nsec);
119 }
120
121 entry->d_op = &fuse_dentry_operations;
122 *inodep = inode;
123 return 0;
124}
125
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700126void fuse_invalidate_attr(struct inode *inode)
127{
128 get_fuse_inode(inode)->i_time = jiffies - 1;
129}
130
131static void fuse_invalidate_entry(struct dentry *entry)
132{
133 d_invalidate(entry);
134 entry->d_time = jiffies - 1;
135}
136
137static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
138 struct inode *dir, struct dentry *entry,
139 int mode)
140{
141 struct fuse_entry_out outarg;
142 struct inode *inode;
143 struct fuse_inode *fi;
144 int err;
145
146 req->in.h.nodeid = get_node_id(dir);
147 req->inode = dir;
148 req->out.numargs = 1;
149 req->out.args[0].size = sizeof(outarg);
150 req->out.args[0].value = &outarg;
151 request_send(fc, req);
152 err = req->out.h.error;
153 if (err) {
154 fuse_put_request(fc, req);
155 return err;
156 }
Miklos Szerediee4e5272005-09-27 21:45:21 -0700157 if (!outarg.nodeid || outarg.nodeid == FUSE_ROOT_ID) {
158 fuse_put_request(fc, req);
159 return -EIO;
160 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700161 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
162 &outarg.attr);
163 if (!inode) {
164 fuse_send_forget(fc, req, outarg.nodeid, 1);
165 return -ENOMEM;
166 }
167 fuse_put_request(fc, req);
168
169 /* Don't allow userspace to do really stupid things... */
170 if ((inode->i_mode ^ mode) & S_IFMT) {
171 iput(inode);
172 return -EIO;
173 }
174
175 entry->d_time = time_to_jiffies(outarg.entry_valid,
176 outarg.entry_valid_nsec);
177
178 fi = get_fuse_inode(inode);
179 fi->i_time = time_to_jiffies(outarg.attr_valid,
180 outarg.attr_valid_nsec);
181
182 d_instantiate(entry, inode);
183 fuse_invalidate_attr(dir);
184 return 0;
185}
186
187static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
188 dev_t rdev)
189{
190 struct fuse_mknod_in inarg;
191 struct fuse_conn *fc = get_fuse_conn(dir);
192 struct fuse_req *req = fuse_get_request(fc);
193 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700194 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700195
196 memset(&inarg, 0, sizeof(inarg));
197 inarg.mode = mode;
198 inarg.rdev = new_encode_dev(rdev);
199 req->in.h.opcode = FUSE_MKNOD;
200 req->in.numargs = 2;
201 req->in.args[0].size = sizeof(inarg);
202 req->in.args[0].value = &inarg;
203 req->in.args[1].size = entry->d_name.len + 1;
204 req->in.args[1].value = entry->d_name.name;
205 return create_new_entry(fc, req, dir, entry, mode);
206}
207
208static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
209 struct nameidata *nd)
210{
211 return fuse_mknod(dir, entry, mode, 0);
212}
213
214static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
215{
216 struct fuse_mkdir_in inarg;
217 struct fuse_conn *fc = get_fuse_conn(dir);
218 struct fuse_req *req = fuse_get_request(fc);
219 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700220 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700221
222 memset(&inarg, 0, sizeof(inarg));
223 inarg.mode = mode;
224 req->in.h.opcode = FUSE_MKDIR;
225 req->in.numargs = 2;
226 req->in.args[0].size = sizeof(inarg);
227 req->in.args[0].value = &inarg;
228 req->in.args[1].size = entry->d_name.len + 1;
229 req->in.args[1].value = entry->d_name.name;
230 return create_new_entry(fc, req, dir, entry, S_IFDIR);
231}
232
233static int fuse_symlink(struct inode *dir, struct dentry *entry,
234 const char *link)
235{
236 struct fuse_conn *fc = get_fuse_conn(dir);
237 unsigned len = strlen(link) + 1;
238 struct fuse_req *req;
239
240 if (len > FUSE_SYMLINK_MAX)
241 return -ENAMETOOLONG;
242
243 req = fuse_get_request(fc);
244 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700245 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700246
247 req->in.h.opcode = FUSE_SYMLINK;
248 req->in.numargs = 2;
249 req->in.args[0].size = entry->d_name.len + 1;
250 req->in.args[0].value = entry->d_name.name;
251 req->in.args[1].size = len;
252 req->in.args[1].value = link;
253 return create_new_entry(fc, req, dir, entry, S_IFLNK);
254}
255
256static int fuse_unlink(struct inode *dir, struct dentry *entry)
257{
258 int err;
259 struct fuse_conn *fc = get_fuse_conn(dir);
260 struct fuse_req *req = fuse_get_request(fc);
261 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700262 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700263
264 req->in.h.opcode = FUSE_UNLINK;
265 req->in.h.nodeid = get_node_id(dir);
266 req->inode = dir;
267 req->in.numargs = 1;
268 req->in.args[0].size = entry->d_name.len + 1;
269 req->in.args[0].value = entry->d_name.name;
270 request_send(fc, req);
271 err = req->out.h.error;
272 fuse_put_request(fc, req);
273 if (!err) {
274 struct inode *inode = entry->d_inode;
275
276 /* Set nlink to zero so the inode can be cleared, if
277 the inode does have more links this will be
278 discovered at the next lookup/getattr */
279 inode->i_nlink = 0;
280 fuse_invalidate_attr(inode);
281 fuse_invalidate_attr(dir);
282 } else if (err == -EINTR)
283 fuse_invalidate_entry(entry);
284 return err;
285}
286
287static int fuse_rmdir(struct inode *dir, struct dentry *entry)
288{
289 int err;
290 struct fuse_conn *fc = get_fuse_conn(dir);
291 struct fuse_req *req = fuse_get_request(fc);
292 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700293 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700294
295 req->in.h.opcode = FUSE_RMDIR;
296 req->in.h.nodeid = get_node_id(dir);
297 req->inode = dir;
298 req->in.numargs = 1;
299 req->in.args[0].size = entry->d_name.len + 1;
300 req->in.args[0].value = entry->d_name.name;
301 request_send(fc, req);
302 err = req->out.h.error;
303 fuse_put_request(fc, req);
304 if (!err) {
305 entry->d_inode->i_nlink = 0;
306 fuse_invalidate_attr(dir);
307 } else if (err == -EINTR)
308 fuse_invalidate_entry(entry);
309 return err;
310}
311
312static int fuse_rename(struct inode *olddir, struct dentry *oldent,
313 struct inode *newdir, struct dentry *newent)
314{
315 int err;
316 struct fuse_rename_in inarg;
317 struct fuse_conn *fc = get_fuse_conn(olddir);
318 struct fuse_req *req = fuse_get_request(fc);
319 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700320 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700321
322 memset(&inarg, 0, sizeof(inarg));
323 inarg.newdir = get_node_id(newdir);
324 req->in.h.opcode = FUSE_RENAME;
325 req->in.h.nodeid = get_node_id(olddir);
326 req->inode = olddir;
327 req->inode2 = newdir;
328 req->in.numargs = 3;
329 req->in.args[0].size = sizeof(inarg);
330 req->in.args[0].value = &inarg;
331 req->in.args[1].size = oldent->d_name.len + 1;
332 req->in.args[1].value = oldent->d_name.name;
333 req->in.args[2].size = newent->d_name.len + 1;
334 req->in.args[2].value = newent->d_name.name;
335 request_send(fc, req);
336 err = req->out.h.error;
337 fuse_put_request(fc, req);
338 if (!err) {
339 fuse_invalidate_attr(olddir);
340 if (olddir != newdir)
341 fuse_invalidate_attr(newdir);
342 } else if (err == -EINTR) {
343 /* If request was interrupted, DEITY only knows if the
344 rename actually took place. If the invalidation
345 fails (e.g. some process has CWD under the renamed
346 directory), then there can be inconsistency between
347 the dcache and the real filesystem. Tough luck. */
348 fuse_invalidate_entry(oldent);
349 if (newent->d_inode)
350 fuse_invalidate_entry(newent);
351 }
352
353 return err;
354}
355
356static int fuse_link(struct dentry *entry, struct inode *newdir,
357 struct dentry *newent)
358{
359 int err;
360 struct fuse_link_in inarg;
361 struct inode *inode = entry->d_inode;
362 struct fuse_conn *fc = get_fuse_conn(inode);
363 struct fuse_req *req = fuse_get_request(fc);
364 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700365 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700366
367 memset(&inarg, 0, sizeof(inarg));
368 inarg.oldnodeid = get_node_id(inode);
369 req->in.h.opcode = FUSE_LINK;
370 req->inode2 = inode;
371 req->in.numargs = 2;
372 req->in.args[0].size = sizeof(inarg);
373 req->in.args[0].value = &inarg;
374 req->in.args[1].size = newent->d_name.len + 1;
375 req->in.args[1].value = newent->d_name.name;
376 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
377 /* Contrary to "normal" filesystems it can happen that link
378 makes two "logical" inodes point to the same "physical"
379 inode. We invalidate the attributes of the old one, so it
380 will reflect changes in the backing inode (link count,
381 etc.)
382 */
383 if (!err || err == -EINTR)
384 fuse_invalidate_attr(inode);
385 return err;
386}
387
Miklos Szeredie5e55582005-09-09 13:10:28 -0700388int fuse_do_getattr(struct inode *inode)
389{
390 int err;
391 struct fuse_attr_out arg;
392 struct fuse_conn *fc = get_fuse_conn(inode);
393 struct fuse_req *req = fuse_get_request(fc);
394 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700395 return -EINTR;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700396
397 req->in.h.opcode = FUSE_GETATTR;
398 req->in.h.nodeid = get_node_id(inode);
399 req->inode = inode;
400 req->out.numargs = 1;
401 req->out.args[0].size = sizeof(arg);
402 req->out.args[0].value = &arg;
403 request_send(fc, req);
404 err = req->out.h.error;
405 fuse_put_request(fc, req);
406 if (!err) {
407 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
408 make_bad_inode(inode);
409 err = -EIO;
410 } else {
411 struct fuse_inode *fi = get_fuse_inode(inode);
412 fuse_change_attributes(inode, &arg.attr);
413 fi->i_time = time_to_jiffies(arg.attr_valid,
414 arg.attr_valid_nsec);
415 }
416 }
417 return err;
418}
419
Miklos Szeredi87729a52005-09-09 13:10:34 -0700420/*
421 * Calling into a user-controlled filesystem gives the filesystem
422 * daemon ptrace-like capabilities over the requester process. This
423 * means, that the filesystem daemon is able to record the exact
424 * filesystem operations performed, and can also control the behavior
425 * of the requester process in otherwise impossible ways. For example
426 * it can delay the operation for arbitrary length of time allowing
427 * DoS against the requester.
428 *
429 * For this reason only those processes can call into the filesystem,
430 * for which the owner of the mount has ptrace privilege. This
431 * excludes processes started by other users, suid or sgid processes.
432 */
433static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
434{
435 if (fc->flags & FUSE_ALLOW_OTHER)
436 return 1;
437
438 if (task->euid == fc->user_id &&
439 task->suid == fc->user_id &&
440 task->uid == fc->user_id &&
441 task->egid == fc->group_id &&
442 task->sgid == fc->group_id &&
443 task->gid == fc->group_id)
444 return 1;
445
446 return 0;
447}
448
Miklos Szeredie5e55582005-09-09 13:10:28 -0700449static int fuse_revalidate(struct dentry *entry)
450{
451 struct inode *inode = entry->d_inode;
452 struct fuse_inode *fi = get_fuse_inode(inode);
453 struct fuse_conn *fc = get_fuse_conn(inode);
454
Miklos Szeredi87729a52005-09-09 13:10:34 -0700455 if (!fuse_allow_task(fc, current))
456 return -EACCES;
457 if (get_node_id(inode) != FUSE_ROOT_ID &&
458 time_before_eq(jiffies, fi->i_time))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700459 return 0;
460
461 return fuse_do_getattr(inode);
462}
463
464static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
465{
466 struct fuse_conn *fc = get_fuse_conn(inode);
467
Miklos Szeredi87729a52005-09-09 13:10:34 -0700468 if (!fuse_allow_task(fc, current))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700469 return -EACCES;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700470 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
471 int err = generic_permission(inode, mask, NULL);
472
473 /* If permission is denied, try to refresh file
474 attributes. This is also needed, because the root
475 node will at first have no permissions */
476 if (err == -EACCES) {
477 err = fuse_do_getattr(inode);
478 if (!err)
479 err = generic_permission(inode, mask, NULL);
480 }
481
482 /* FIXME: Need some mechanism to revoke permissions:
483 currently if the filesystem suddenly changes the
484 file mode, we will not be informed about it, and
485 continue to allow access to the file/directory.
486
487 This is actually not so grave, since the user can
488 simply keep access to the file/directory anyway by
489 keeping it open... */
490
491 return err;
492 } else {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700493 int mode = inode->i_mode;
494 if ((mask & MAY_WRITE) && IS_RDONLY(inode) &&
495 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
496 return -EROFS;
497 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
498 return -EACCES;
499 return 0;
500 }
501}
502
503static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
504 void *dstbuf, filldir_t filldir)
505{
506 while (nbytes >= FUSE_NAME_OFFSET) {
507 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
508 size_t reclen = FUSE_DIRENT_SIZE(dirent);
509 int over;
510 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
511 return -EIO;
512 if (reclen > nbytes)
513 break;
514
515 over = filldir(dstbuf, dirent->name, dirent->namelen,
516 file->f_pos, dirent->ino, dirent->type);
517 if (over)
518 break;
519
520 buf += reclen;
521 nbytes -= reclen;
522 file->f_pos = dirent->off;
523 }
524
525 return 0;
526}
527
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700528static inline size_t fuse_send_readdir(struct fuse_req *req, struct file *file,
529 struct inode *inode, loff_t pos,
530 size_t count)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700531{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700532 return fuse_send_read_common(req, file, inode, pos, count, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700533}
534
535static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
536{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700537 int err;
538 size_t nbytes;
539 struct page *page;
540 struct inode *inode = file->f_dentry->d_inode;
541 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700542 struct fuse_req *req = fuse_get_request(fc);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700543 if (!req)
544 return -EINTR;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700545
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700546 page = alloc_page(GFP_KERNEL);
547 if (!page) {
548 fuse_put_request(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700549 return -ENOMEM;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700550 }
551 req->num_pages = 1;
552 req->pages[0] = page;
553 nbytes = fuse_send_readdir(req, file, inode, file->f_pos, PAGE_SIZE);
554 err = req->out.h.error;
555 fuse_put_request(fc, req);
556 if (!err)
557 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
558 filldir);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700559
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700560 __free_page(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700561 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700562 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700563}
564
565static char *read_link(struct dentry *dentry)
566{
567 struct inode *inode = dentry->d_inode;
568 struct fuse_conn *fc = get_fuse_conn(inode);
569 struct fuse_req *req = fuse_get_request(fc);
570 char *link;
571
572 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700573 return ERR_PTR(-EINTR);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700574
575 link = (char *) __get_free_page(GFP_KERNEL);
576 if (!link) {
577 link = ERR_PTR(-ENOMEM);
578 goto out;
579 }
580 req->in.h.opcode = FUSE_READLINK;
581 req->in.h.nodeid = get_node_id(inode);
582 req->inode = inode;
583 req->out.argvar = 1;
584 req->out.numargs = 1;
585 req->out.args[0].size = PAGE_SIZE - 1;
586 req->out.args[0].value = link;
587 request_send(fc, req);
588 if (req->out.h.error) {
589 free_page((unsigned long) link);
590 link = ERR_PTR(req->out.h.error);
591 } else
592 link[req->out.args[0].size] = '\0';
593 out:
594 fuse_put_request(fc, req);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700595 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700596 return link;
597}
598
599static void free_link(char *link)
600{
601 if (!IS_ERR(link))
602 free_page((unsigned long) link);
603}
604
605static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
606{
607 nd_set_link(nd, read_link(dentry));
608 return NULL;
609}
610
611static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
612{
613 free_link(nd_get_link(nd));
614}
615
616static int fuse_dir_open(struct inode *inode, struct file *file)
617{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700618 return fuse_open_common(inode, file, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700619}
620
621static int fuse_dir_release(struct inode *inode, struct file *file)
622{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700623 return fuse_release_common(inode, file, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700624}
625
Miklos Szeredi82547982005-09-09 13:10:38 -0700626static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
627{
628 /* nfsd can call this with no file */
629 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
630}
631
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700632static unsigned iattr_to_fattr(struct iattr *iattr, struct fuse_attr *fattr)
633{
634 unsigned ivalid = iattr->ia_valid;
635 unsigned fvalid = 0;
636
637 memset(fattr, 0, sizeof(*fattr));
638
639 if (ivalid & ATTR_MODE)
640 fvalid |= FATTR_MODE, fattr->mode = iattr->ia_mode;
641 if (ivalid & ATTR_UID)
642 fvalid |= FATTR_UID, fattr->uid = iattr->ia_uid;
643 if (ivalid & ATTR_GID)
644 fvalid |= FATTR_GID, fattr->gid = iattr->ia_gid;
645 if (ivalid & ATTR_SIZE)
646 fvalid |= FATTR_SIZE, fattr->size = iattr->ia_size;
647 /* You can only _set_ these together (they may change by themselves) */
648 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
649 fvalid |= FATTR_ATIME | FATTR_MTIME;
650 fattr->atime = iattr->ia_atime.tv_sec;
651 fattr->mtime = iattr->ia_mtime.tv_sec;
652 }
653
654 return fvalid;
655}
656
657static int fuse_setattr(struct dentry *entry, struct iattr *attr)
658{
659 struct inode *inode = entry->d_inode;
660 struct fuse_conn *fc = get_fuse_conn(inode);
661 struct fuse_inode *fi = get_fuse_inode(inode);
662 struct fuse_req *req;
663 struct fuse_setattr_in inarg;
664 struct fuse_attr_out outarg;
665 int err;
666 int is_truncate = 0;
667
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700668 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
669 err = inode_change_ok(inode, attr);
670 if (err)
671 return err;
672 }
673
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700674 if (attr->ia_valid & ATTR_SIZE) {
675 unsigned long limit;
676 is_truncate = 1;
677 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
678 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
679 send_sig(SIGXFSZ, current, 0);
680 return -EFBIG;
681 }
682 }
683
684 req = fuse_get_request(fc);
685 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700686 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700687
688 memset(&inarg, 0, sizeof(inarg));
689 inarg.valid = iattr_to_fattr(attr, &inarg.attr);
690 req->in.h.opcode = FUSE_SETATTR;
691 req->in.h.nodeid = get_node_id(inode);
692 req->inode = inode;
693 req->in.numargs = 1;
694 req->in.args[0].size = sizeof(inarg);
695 req->in.args[0].value = &inarg;
696 req->out.numargs = 1;
697 req->out.args[0].size = sizeof(outarg);
698 req->out.args[0].value = &outarg;
699 request_send(fc, req);
700 err = req->out.h.error;
701 fuse_put_request(fc, req);
702 if (!err) {
703 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
704 make_bad_inode(inode);
705 err = -EIO;
706 } else {
707 if (is_truncate) {
708 loff_t origsize = i_size_read(inode);
709 i_size_write(inode, outarg.attr.size);
710 if (origsize > outarg.attr.size)
711 vmtruncate(inode, outarg.attr.size);
712 }
713 fuse_change_attributes(inode, &outarg.attr);
714 fi->i_time = time_to_jiffies(outarg.attr_valid,
715 outarg.attr_valid_nsec);
716 }
717 } else if (err == -EINTR)
718 fuse_invalidate_attr(inode);
719
720 return err;
721}
722
Miklos Szeredie5e55582005-09-09 13:10:28 -0700723static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
724 struct kstat *stat)
725{
726 struct inode *inode = entry->d_inode;
727 int err = fuse_revalidate(entry);
728 if (!err)
729 generic_fillattr(inode, stat);
730
731 return err;
732}
733
734static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
735 struct nameidata *nd)
736{
737 struct inode *inode;
738 int err = fuse_lookup_iget(dir, entry, &inode);
739 if (err)
740 return ERR_PTR(err);
741 if (inode && S_ISDIR(inode->i_mode)) {
742 /* Don't allow creating an alias to a directory */
743 struct dentry *alias = d_find_alias(inode);
744 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
745 dput(alias);
746 iput(inode);
747 return ERR_PTR(-EIO);
748 }
749 }
750 return d_splice_alias(inode, entry);
751}
752
Miklos Szeredi92a87802005-09-09 13:10:31 -0700753static int fuse_setxattr(struct dentry *entry, const char *name,
754 const void *value, size_t size, int flags)
755{
756 struct inode *inode = entry->d_inode;
757 struct fuse_conn *fc = get_fuse_conn(inode);
758 struct fuse_req *req;
759 struct fuse_setxattr_in inarg;
760 int err;
761
762 if (size > FUSE_XATTR_SIZE_MAX)
763 return -E2BIG;
764
765 if (fc->no_setxattr)
766 return -EOPNOTSUPP;
767
768 req = fuse_get_request(fc);
769 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700770 return -EINTR;
Miklos Szeredi92a87802005-09-09 13:10:31 -0700771
772 memset(&inarg, 0, sizeof(inarg));
773 inarg.size = size;
774 inarg.flags = flags;
775 req->in.h.opcode = FUSE_SETXATTR;
776 req->in.h.nodeid = get_node_id(inode);
777 req->inode = inode;
778 req->in.numargs = 3;
779 req->in.args[0].size = sizeof(inarg);
780 req->in.args[0].value = &inarg;
781 req->in.args[1].size = strlen(name) + 1;
782 req->in.args[1].value = name;
783 req->in.args[2].size = size;
784 req->in.args[2].value = value;
785 request_send(fc, req);
786 err = req->out.h.error;
787 fuse_put_request(fc, req);
788 if (err == -ENOSYS) {
789 fc->no_setxattr = 1;
790 err = -EOPNOTSUPP;
791 }
792 return err;
793}
794
795static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
796 void *value, size_t size)
797{
798 struct inode *inode = entry->d_inode;
799 struct fuse_conn *fc = get_fuse_conn(inode);
800 struct fuse_req *req;
801 struct fuse_getxattr_in inarg;
802 struct fuse_getxattr_out outarg;
803 ssize_t ret;
804
805 if (fc->no_getxattr)
806 return -EOPNOTSUPP;
807
808 req = fuse_get_request(fc);
809 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700810 return -EINTR;
Miklos Szeredi92a87802005-09-09 13:10:31 -0700811
812 memset(&inarg, 0, sizeof(inarg));
813 inarg.size = size;
814 req->in.h.opcode = FUSE_GETXATTR;
815 req->in.h.nodeid = get_node_id(inode);
816 req->inode = inode;
817 req->in.numargs = 2;
818 req->in.args[0].size = sizeof(inarg);
819 req->in.args[0].value = &inarg;
820 req->in.args[1].size = strlen(name) + 1;
821 req->in.args[1].value = name;
822 /* This is really two different operations rolled into one */
823 req->out.numargs = 1;
824 if (size) {
825 req->out.argvar = 1;
826 req->out.args[0].size = size;
827 req->out.args[0].value = value;
828 } else {
829 req->out.args[0].size = sizeof(outarg);
830 req->out.args[0].value = &outarg;
831 }
832 request_send(fc, req);
833 ret = req->out.h.error;
834 if (!ret)
835 ret = size ? req->out.args[0].size : outarg.size;
836 else {
837 if (ret == -ENOSYS) {
838 fc->no_getxattr = 1;
839 ret = -EOPNOTSUPP;
840 }
841 }
842 fuse_put_request(fc, req);
843 return ret;
844}
845
846static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
847{
848 struct inode *inode = entry->d_inode;
849 struct fuse_conn *fc = get_fuse_conn(inode);
850 struct fuse_req *req;
851 struct fuse_getxattr_in inarg;
852 struct fuse_getxattr_out outarg;
853 ssize_t ret;
854
855 if (fc->no_listxattr)
856 return -EOPNOTSUPP;
857
858 req = fuse_get_request(fc);
859 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700860 return -EINTR;
Miklos Szeredi92a87802005-09-09 13:10:31 -0700861
862 memset(&inarg, 0, sizeof(inarg));
863 inarg.size = size;
864 req->in.h.opcode = FUSE_LISTXATTR;
865 req->in.h.nodeid = get_node_id(inode);
866 req->inode = inode;
867 req->in.numargs = 1;
868 req->in.args[0].size = sizeof(inarg);
869 req->in.args[0].value = &inarg;
870 /* This is really two different operations rolled into one */
871 req->out.numargs = 1;
872 if (size) {
873 req->out.argvar = 1;
874 req->out.args[0].size = size;
875 req->out.args[0].value = list;
876 } else {
877 req->out.args[0].size = sizeof(outarg);
878 req->out.args[0].value = &outarg;
879 }
880 request_send(fc, req);
881 ret = req->out.h.error;
882 if (!ret)
883 ret = size ? req->out.args[0].size : outarg.size;
884 else {
885 if (ret == -ENOSYS) {
886 fc->no_listxattr = 1;
887 ret = -EOPNOTSUPP;
888 }
889 }
890 fuse_put_request(fc, req);
891 return ret;
892}
893
894static int fuse_removexattr(struct dentry *entry, const char *name)
895{
896 struct inode *inode = entry->d_inode;
897 struct fuse_conn *fc = get_fuse_conn(inode);
898 struct fuse_req *req;
899 int err;
900
901 if (fc->no_removexattr)
902 return -EOPNOTSUPP;
903
904 req = fuse_get_request(fc);
905 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700906 return -EINTR;
Miklos Szeredi92a87802005-09-09 13:10:31 -0700907
908 req->in.h.opcode = FUSE_REMOVEXATTR;
909 req->in.h.nodeid = get_node_id(inode);
910 req->inode = inode;
911 req->in.numargs = 1;
912 req->in.args[0].size = strlen(name) + 1;
913 req->in.args[0].value = name;
914 request_send(fc, req);
915 err = req->out.h.error;
916 fuse_put_request(fc, req);
917 if (err == -ENOSYS) {
918 fc->no_removexattr = 1;
919 err = -EOPNOTSUPP;
920 }
921 return err;
922}
923
Miklos Szeredie5e55582005-09-09 13:10:28 -0700924static struct inode_operations fuse_dir_inode_operations = {
925 .lookup = fuse_lookup,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700926 .mkdir = fuse_mkdir,
927 .symlink = fuse_symlink,
928 .unlink = fuse_unlink,
929 .rmdir = fuse_rmdir,
930 .rename = fuse_rename,
931 .link = fuse_link,
932 .setattr = fuse_setattr,
933 .create = fuse_create,
934 .mknod = fuse_mknod,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700935 .permission = fuse_permission,
936 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -0700937 .setxattr = fuse_setxattr,
938 .getxattr = fuse_getxattr,
939 .listxattr = fuse_listxattr,
940 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700941};
942
943static struct file_operations fuse_dir_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700944 .llseek = generic_file_llseek,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700945 .read = generic_read_dir,
946 .readdir = fuse_readdir,
947 .open = fuse_dir_open,
948 .release = fuse_dir_release,
Miklos Szeredi82547982005-09-09 13:10:38 -0700949 .fsync = fuse_dir_fsync,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700950};
951
952static struct inode_operations fuse_common_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700953 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700954 .permission = fuse_permission,
955 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -0700956 .setxattr = fuse_setxattr,
957 .getxattr = fuse_getxattr,
958 .listxattr = fuse_listxattr,
959 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700960};
961
962static struct inode_operations fuse_symlink_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700963 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700964 .follow_link = fuse_follow_link,
965 .put_link = fuse_put_link,
966 .readlink = generic_readlink,
967 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -0700968 .setxattr = fuse_setxattr,
969 .getxattr = fuse_getxattr,
970 .listxattr = fuse_listxattr,
971 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700972};
973
974void fuse_init_common(struct inode *inode)
975{
976 inode->i_op = &fuse_common_inode_operations;
977}
978
979void fuse_init_dir(struct inode *inode)
980{
981 inode->i_op = &fuse_dir_inode_operations;
982 inode->i_fop = &fuse_dir_operations;
983}
984
985void fuse_init_symlink(struct inode *inode)
986{
987 inode->i_op = &fuse_symlink_inode_operations;
988}