blob: 72a74cde6de8c288793293d4e4ca4bb4ea58b793 [file] [log] [blame]
Miklos Szeredie5e55582005-09-09 13:10:28 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07003 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredie5e55582005-09-09 13:10:28 -07004
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
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080017/*
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 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080026static unsigned long time_to_jiffies(unsigned long sec, unsigned long nsec)
Miklos Szeredie5e55582005-09-09 13:10:28 -070027{
28 struct timespec ts = {sec, nsec};
29 return jiffies + timespec_to_jiffies(&ts);
30}
31
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080032/*
33 * Set dentry and possibly attribute timeouts from the lookup/mk*
34 * replies
35 */
Miklos Szeredi0aa7c692006-01-06 00:19:34 -080036static void fuse_change_timeout(struct dentry *entry, struct fuse_entry_out *o)
37{
Miklos Szeredi0aa7c692006-01-06 00:19:34 -080038 entry->d_time = time_to_jiffies(o->entry_valid, o->entry_valid_nsec);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -080039 if (entry->d_inode)
40 get_fuse_inode(entry->d_inode)->i_time =
41 time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
42}
43
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080044/*
45 * Mark the attributes as stale, so that at the next call to
46 * ->getattr() they will be fetched from userspace
47 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -080048void fuse_invalidate_attr(struct inode *inode)
49{
50 get_fuse_inode(inode)->i_time = jiffies - 1;
51}
52
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080053/*
54 * Just mark the entry as stale, so that a next attempt to look it up
55 * will result in a new lookup call to userspace
56 *
57 * This is called when a dentry is about to become negative and the
58 * timeout is unknown (unlink, rmdir, rename and in some cases
59 * lookup)
60 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -080061static void fuse_invalidate_entry_cache(struct dentry *entry)
62{
63 entry->d_time = jiffies - 1;
64}
65
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080066/*
67 * Same as fuse_invalidate_entry_cache(), but also try to remove the
68 * dentry from the hash
69 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -080070static void fuse_invalidate_entry(struct dentry *entry)
71{
72 d_invalidate(entry);
73 fuse_invalidate_entry_cache(entry);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -080074}
75
Miklos Szeredie5e55582005-09-09 13:10:28 -070076static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
77 struct dentry *entry,
78 struct fuse_entry_out *outarg)
79{
80 req->in.h.opcode = FUSE_LOOKUP;
81 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredie5e55582005-09-09 13:10:28 -070082 req->in.numargs = 1;
83 req->in.args[0].size = entry->d_name.len + 1;
84 req->in.args[0].value = entry->d_name.name;
85 req->out.numargs = 1;
86 req->out.args[0].size = sizeof(struct fuse_entry_out);
87 req->out.args[0].value = outarg;
88}
89
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080090/*
91 * Check whether the dentry is still valid
92 *
93 * If the entry validity timeout has expired and the dentry is
94 * positive, try to redo the lookup. If the lookup results in a
95 * different inode, then let the VFS invalidate the dentry and redo
96 * the lookup once more. If the lookup results in the same inode,
97 * then refresh the attributes, timeouts and mark the dentry valid.
98 */
Miklos Szeredie5e55582005-09-09 13:10:28 -070099static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
100{
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800101 struct inode *inode = entry->d_inode;
102
103 if (inode && is_bad_inode(inode))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700104 return 0;
105 else if (time_after(jiffies, entry->d_time)) {
106 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700107 struct fuse_entry_out outarg;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800108 struct fuse_conn *fc;
109 struct fuse_req *req;
110
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800111 /* Doesn't hurt to "reset" the validity timeout */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800112 fuse_invalidate_entry_cache(entry);
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800113
114 /* For negative dentries, always do a fresh lookup */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800115 if (!inode)
116 return 0;
117
118 fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700119 req = fuse_get_req(fc);
120 if (IS_ERR(req))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700121 return 0;
122
123 fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700124 request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700125 err = req->out.h.error;
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800126 /* Zero nodeid is same as -ENOENT */
127 if (!err && !outarg.nodeid)
128 err = -ENOENT;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700129 if (!err) {
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800130 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700131 if (outarg.nodeid != get_node_id(inode)) {
132 fuse_send_forget(fc, req, outarg.nodeid, 1);
133 return 0;
134 }
135 fi->nlookup ++;
136 }
Miklos Szeredie5e55582005-09-09 13:10:28 -0700137 fuse_put_request(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700138 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700139 return 0;
140
141 fuse_change_attributes(inode, &outarg.attr);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800142 fuse_change_timeout(entry, &outarg);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700143 }
144 return 1;
145}
146
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800147/*
148 * Check if there's already a hashed alias of this directory inode.
149 * If yes, then lookup and mkdir must not create a new alias.
150 */
Miklos Szeredif007d5c2005-11-28 13:44:16 -0800151static int dir_alias(struct inode *inode)
152{
153 if (S_ISDIR(inode->i_mode)) {
Miklos Szeredif007d5c2005-11-28 13:44:16 -0800154 struct dentry *alias = d_find_alias(inode);
155 if (alias) {
156 dput(alias);
157 return 1;
158 }
159 }
160 return 0;
161}
162
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800163static int invalid_nodeid(u64 nodeid)
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800164{
165 return !nodeid || nodeid == FUSE_ROOT_ID;
166}
167
Miklos Szeredie5e55582005-09-09 13:10:28 -0700168static struct dentry_operations fuse_dentry_operations = {
169 .d_revalidate = fuse_dentry_revalidate,
170};
171
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800172static int valid_mode(int m)
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800173{
174 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
175 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
176}
177
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800178static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
179 struct nameidata *nd)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700180{
181 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700182 struct fuse_entry_out outarg;
183 struct inode *inode = NULL;
184 struct fuse_conn *fc = get_fuse_conn(dir);
185 struct fuse_req *req;
186
187 if (entry->d_name.len > FUSE_NAME_MAX)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800188 return ERR_PTR(-ENAMETOOLONG);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700189
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700190 req = fuse_get_req(fc);
191 if (IS_ERR(req))
192 return ERR_PTR(PTR_ERR(req));
Miklos Szeredie5e55582005-09-09 13:10:28 -0700193
194 fuse_lookup_init(req, dir, entry, &outarg);
195 request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700196 err = req->out.h.error;
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800197 /* Zero nodeid is same as -ENOENT, but with valid timeout */
198 if (!err && outarg.nodeid &&
199 (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
Miklos Szerediee4e5272005-09-27 21:45:21 -0700200 err = -EIO;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800201 if (!err && outarg.nodeid) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700202 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700203 &outarg.attr);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700204 if (!inode) {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700205 fuse_send_forget(fc, req, outarg.nodeid, 1);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800206 return ERR_PTR(-ENOMEM);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700207 }
208 }
209 fuse_put_request(fc, req);
210 if (err && err != -ENOENT)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800211 return ERR_PTR(err);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700212
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800213 if (inode && dir_alias(inode)) {
214 iput(inode);
215 return ERR_PTR(-EIO);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700216 }
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800217 d_add(entry, inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700218 entry->d_op = &fuse_dentry_operations;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800219 if (!err)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800220 fuse_change_timeout(entry, &outarg);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800221 else
222 fuse_invalidate_entry_cache(entry);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800223 return NULL;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700224}
225
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800226/*
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700227 * Synchronous release for the case when something goes wrong in CREATE_OPEN
228 */
229static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
230 u64 nodeid, int flags)
231{
232 struct fuse_req *req;
233
234 req = fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
235 req->force = 1;
236 request_send(fc, req);
237 fuse_put_request(fc, req);
238}
239
240/*
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800241 * Atomic create+open operation
242 *
243 * If the filesystem doesn't support this, then fall back to separate
244 * 'mknod' + 'open' requests.
245 */
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800246static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
247 struct nameidata *nd)
248{
249 int err;
250 struct inode *inode;
251 struct fuse_conn *fc = get_fuse_conn(dir);
252 struct fuse_req *req;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700253 struct fuse_req *forget_req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800254 struct fuse_open_in inarg;
255 struct fuse_open_out outopen;
256 struct fuse_entry_out outentry;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800257 struct fuse_file *ff;
258 struct file *file;
259 int flags = nd->intent.open.flags - 1;
260
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800261 if (fc->no_create)
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700262 return -ENOSYS;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800263
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700264 forget_req = fuse_get_req(fc);
265 if (IS_ERR(forget_req))
266 return PTR_ERR(forget_req);
267
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700268 req = fuse_get_req(fc);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700269 err = PTR_ERR(req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700270 if (IS_ERR(req))
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700271 goto out_put_forget_req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800272
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700273 err = -ENOMEM;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800274 ff = fuse_file_alloc();
275 if (!ff)
276 goto out_put_request;
277
278 flags &= ~O_NOCTTY;
279 memset(&inarg, 0, sizeof(inarg));
280 inarg.flags = flags;
281 inarg.mode = mode;
282 req->in.h.opcode = FUSE_CREATE;
283 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800284 req->in.numargs = 2;
285 req->in.args[0].size = sizeof(inarg);
286 req->in.args[0].value = &inarg;
287 req->in.args[1].size = entry->d_name.len + 1;
288 req->in.args[1].value = entry->d_name.name;
289 req->out.numargs = 2;
290 req->out.args[0].size = sizeof(outentry);
291 req->out.args[0].value = &outentry;
292 req->out.args[1].size = sizeof(outopen);
293 req->out.args[1].value = &outopen;
294 request_send(fc, req);
295 err = req->out.h.error;
296 if (err) {
297 if (err == -ENOSYS)
298 fc->no_create = 1;
299 goto out_free_ff;
300 }
301
302 err = -EIO;
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800303 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800304 goto out_free_ff;
305
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700306 fuse_put_request(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800307 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
308 &outentry.attr);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800309 if (!inode) {
310 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
311 ff->fh = outopen.fh;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700312 fuse_sync_release(fc, ff, outentry.nodeid, flags);
313 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
314 return -ENOMEM;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800315 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700316 fuse_put_request(fc, forget_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800317 d_instantiate(entry, inode);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800318 fuse_change_timeout(entry, &outentry);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800319 file = lookup_instantiate_filp(nd, entry, generic_file_open);
320 if (IS_ERR(file)) {
321 ff->fh = outopen.fh;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700322 fuse_sync_release(fc, ff, outentry.nodeid, flags);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800323 return PTR_ERR(file);
324 }
325 fuse_finish_open(inode, file, ff, &outopen);
326 return 0;
327
328 out_free_ff:
329 fuse_file_free(ff);
330 out_put_request:
331 fuse_put_request(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700332 out_put_forget_req:
333 fuse_put_request(fc, forget_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800334 return err;
335}
336
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800337/*
338 * Code shared between mknod, mkdir, symlink and link
339 */
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700340static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
341 struct inode *dir, struct dentry *entry,
342 int mode)
343{
344 struct fuse_entry_out outarg;
345 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700346 int err;
347
348 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700349 req->out.numargs = 1;
350 req->out.args[0].size = sizeof(outarg);
351 req->out.args[0].value = &outarg;
352 request_send(fc, req);
353 err = req->out.h.error;
354 if (err) {
355 fuse_put_request(fc, req);
356 return err;
357 }
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800358 err = -EIO;
359 if (invalid_nodeid(outarg.nodeid))
360 goto out_put_request;
361
362 if ((outarg.attr.mode ^ mode) & S_IFMT)
363 goto out_put_request;
364
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700365 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
366 &outarg.attr);
367 if (!inode) {
368 fuse_send_forget(fc, req, outarg.nodeid, 1);
369 return -ENOMEM;
370 }
371 fuse_put_request(fc, req);
372
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800373 if (dir_alias(inode)) {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700374 iput(inode);
375 return -EIO;
376 }
377
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700378 d_instantiate(entry, inode);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800379 fuse_change_timeout(entry, &outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700380 fuse_invalidate_attr(dir);
381 return 0;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800382
383 out_put_request:
384 fuse_put_request(fc, req);
385 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700386}
387
388static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
389 dev_t rdev)
390{
391 struct fuse_mknod_in inarg;
392 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700393 struct fuse_req *req = fuse_get_req(fc);
394 if (IS_ERR(req))
395 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700396
397 memset(&inarg, 0, sizeof(inarg));
398 inarg.mode = mode;
399 inarg.rdev = new_encode_dev(rdev);
400 req->in.h.opcode = FUSE_MKNOD;
401 req->in.numargs = 2;
402 req->in.args[0].size = sizeof(inarg);
403 req->in.args[0].value = &inarg;
404 req->in.args[1].size = entry->d_name.len + 1;
405 req->in.args[1].value = entry->d_name.name;
406 return create_new_entry(fc, req, dir, entry, mode);
407}
408
409static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
410 struct nameidata *nd)
411{
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800412 if (nd && (nd->flags & LOOKUP_CREATE)) {
413 int err = fuse_create_open(dir, entry, mode, nd);
414 if (err != -ENOSYS)
415 return err;
416 /* Fall back on mknod */
417 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700418 return fuse_mknod(dir, entry, mode, 0);
419}
420
421static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
422{
423 struct fuse_mkdir_in inarg;
424 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700425 struct fuse_req *req = fuse_get_req(fc);
426 if (IS_ERR(req))
427 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700428
429 memset(&inarg, 0, sizeof(inarg));
430 inarg.mode = mode;
431 req->in.h.opcode = FUSE_MKDIR;
432 req->in.numargs = 2;
433 req->in.args[0].size = sizeof(inarg);
434 req->in.args[0].value = &inarg;
435 req->in.args[1].size = entry->d_name.len + 1;
436 req->in.args[1].value = entry->d_name.name;
437 return create_new_entry(fc, req, dir, entry, S_IFDIR);
438}
439
440static int fuse_symlink(struct inode *dir, struct dentry *entry,
441 const char *link)
442{
443 struct fuse_conn *fc = get_fuse_conn(dir);
444 unsigned len = strlen(link) + 1;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700445 struct fuse_req *req = fuse_get_req(fc);
446 if (IS_ERR(req))
447 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700448
449 req->in.h.opcode = FUSE_SYMLINK;
450 req->in.numargs = 2;
451 req->in.args[0].size = entry->d_name.len + 1;
452 req->in.args[0].value = entry->d_name.name;
453 req->in.args[1].size = len;
454 req->in.args[1].value = link;
455 return create_new_entry(fc, req, dir, entry, S_IFLNK);
456}
457
458static int fuse_unlink(struct inode *dir, struct dentry *entry)
459{
460 int err;
461 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700462 struct fuse_req *req = fuse_get_req(fc);
463 if (IS_ERR(req))
464 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700465
466 req->in.h.opcode = FUSE_UNLINK;
467 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700468 req->in.numargs = 1;
469 req->in.args[0].size = entry->d_name.len + 1;
470 req->in.args[0].value = entry->d_name.name;
471 request_send(fc, req);
472 err = req->out.h.error;
473 fuse_put_request(fc, req);
474 if (!err) {
475 struct inode *inode = entry->d_inode;
476
477 /* Set nlink to zero so the inode can be cleared, if
478 the inode does have more links this will be
479 discovered at the next lookup/getattr */
480 inode->i_nlink = 0;
481 fuse_invalidate_attr(inode);
482 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800483 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700484 } else if (err == -EINTR)
485 fuse_invalidate_entry(entry);
486 return err;
487}
488
489static int fuse_rmdir(struct inode *dir, struct dentry *entry)
490{
491 int err;
492 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700493 struct fuse_req *req = fuse_get_req(fc);
494 if (IS_ERR(req))
495 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700496
497 req->in.h.opcode = FUSE_RMDIR;
498 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700499 req->in.numargs = 1;
500 req->in.args[0].size = entry->d_name.len + 1;
501 req->in.args[0].value = entry->d_name.name;
502 request_send(fc, req);
503 err = req->out.h.error;
504 fuse_put_request(fc, req);
505 if (!err) {
506 entry->d_inode->i_nlink = 0;
507 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800508 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700509 } else if (err == -EINTR)
510 fuse_invalidate_entry(entry);
511 return err;
512}
513
514static int fuse_rename(struct inode *olddir, struct dentry *oldent,
515 struct inode *newdir, struct dentry *newent)
516{
517 int err;
518 struct fuse_rename_in inarg;
519 struct fuse_conn *fc = get_fuse_conn(olddir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700520 struct fuse_req *req = fuse_get_req(fc);
521 if (IS_ERR(req))
522 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700523
524 memset(&inarg, 0, sizeof(inarg));
525 inarg.newdir = get_node_id(newdir);
526 req->in.h.opcode = FUSE_RENAME;
527 req->in.h.nodeid = get_node_id(olddir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700528 req->in.numargs = 3;
529 req->in.args[0].size = sizeof(inarg);
530 req->in.args[0].value = &inarg;
531 req->in.args[1].size = oldent->d_name.len + 1;
532 req->in.args[1].value = oldent->d_name.name;
533 req->in.args[2].size = newent->d_name.len + 1;
534 req->in.args[2].value = newent->d_name.name;
535 request_send(fc, req);
536 err = req->out.h.error;
537 fuse_put_request(fc, req);
538 if (!err) {
539 fuse_invalidate_attr(olddir);
540 if (olddir != newdir)
541 fuse_invalidate_attr(newdir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800542
543 /* newent will end up negative */
544 if (newent->d_inode)
545 fuse_invalidate_entry_cache(newent);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700546 } else if (err == -EINTR) {
547 /* If request was interrupted, DEITY only knows if the
548 rename actually took place. If the invalidation
549 fails (e.g. some process has CWD under the renamed
550 directory), then there can be inconsistency between
551 the dcache and the real filesystem. Tough luck. */
552 fuse_invalidate_entry(oldent);
553 if (newent->d_inode)
554 fuse_invalidate_entry(newent);
555 }
556
557 return err;
558}
559
560static int fuse_link(struct dentry *entry, struct inode *newdir,
561 struct dentry *newent)
562{
563 int err;
564 struct fuse_link_in inarg;
565 struct inode *inode = entry->d_inode;
566 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700567 struct fuse_req *req = fuse_get_req(fc);
568 if (IS_ERR(req))
569 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700570
571 memset(&inarg, 0, sizeof(inarg));
572 inarg.oldnodeid = get_node_id(inode);
573 req->in.h.opcode = FUSE_LINK;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700574 req->in.numargs = 2;
575 req->in.args[0].size = sizeof(inarg);
576 req->in.args[0].value = &inarg;
577 req->in.args[1].size = newent->d_name.len + 1;
578 req->in.args[1].value = newent->d_name.name;
579 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
580 /* Contrary to "normal" filesystems it can happen that link
581 makes two "logical" inodes point to the same "physical"
582 inode. We invalidate the attributes of the old one, so it
583 will reflect changes in the backing inode (link count,
584 etc.)
585 */
586 if (!err || err == -EINTR)
587 fuse_invalidate_attr(inode);
588 return err;
589}
590
Miklos Szeredie5e55582005-09-09 13:10:28 -0700591int fuse_do_getattr(struct inode *inode)
592{
593 int err;
594 struct fuse_attr_out arg;
595 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700596 struct fuse_req *req = fuse_get_req(fc);
597 if (IS_ERR(req))
598 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700599
600 req->in.h.opcode = FUSE_GETATTR;
601 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700602 req->out.numargs = 1;
603 req->out.args[0].size = sizeof(arg);
604 req->out.args[0].value = &arg;
605 request_send(fc, req);
606 err = req->out.h.error;
607 fuse_put_request(fc, req);
608 if (!err) {
609 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
610 make_bad_inode(inode);
611 err = -EIO;
612 } else {
613 struct fuse_inode *fi = get_fuse_inode(inode);
614 fuse_change_attributes(inode, &arg.attr);
615 fi->i_time = time_to_jiffies(arg.attr_valid,
616 arg.attr_valid_nsec);
617 }
618 }
619 return err;
620}
621
Miklos Szeredi87729a52005-09-09 13:10:34 -0700622/*
623 * Calling into a user-controlled filesystem gives the filesystem
624 * daemon ptrace-like capabilities over the requester process. This
625 * means, that the filesystem daemon is able to record the exact
626 * filesystem operations performed, and can also control the behavior
627 * of the requester process in otherwise impossible ways. For example
628 * it can delay the operation for arbitrary length of time allowing
629 * DoS against the requester.
630 *
631 * For this reason only those processes can call into the filesystem,
632 * for which the owner of the mount has ptrace privilege. This
633 * excludes processes started by other users, suid or sgid processes.
634 */
635static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
636{
637 if (fc->flags & FUSE_ALLOW_OTHER)
638 return 1;
639
640 if (task->euid == fc->user_id &&
641 task->suid == fc->user_id &&
642 task->uid == fc->user_id &&
643 task->egid == fc->group_id &&
644 task->sgid == fc->group_id &&
645 task->gid == fc->group_id)
646 return 1;
647
648 return 0;
649}
650
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800651/*
652 * Check whether the inode attributes are still valid
653 *
654 * If the attribute validity timeout has expired, then fetch the fresh
655 * attributes with a 'getattr' request
656 *
657 * I'm not sure why cached attributes are never returned for the root
658 * inode, this is probably being too cautious.
659 */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700660static int fuse_revalidate(struct dentry *entry)
661{
662 struct inode *inode = entry->d_inode;
663 struct fuse_inode *fi = get_fuse_inode(inode);
664 struct fuse_conn *fc = get_fuse_conn(inode);
665
Miklos Szeredi87729a52005-09-09 13:10:34 -0700666 if (!fuse_allow_task(fc, current))
667 return -EACCES;
668 if (get_node_id(inode) != FUSE_ROOT_ID &&
669 time_before_eq(jiffies, fi->i_time))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700670 return 0;
671
672 return fuse_do_getattr(inode);
673}
674
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800675static int fuse_access(struct inode *inode, int mask)
676{
677 struct fuse_conn *fc = get_fuse_conn(inode);
678 struct fuse_req *req;
679 struct fuse_access_in inarg;
680 int err;
681
682 if (fc->no_access)
683 return 0;
684
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700685 req = fuse_get_req(fc);
686 if (IS_ERR(req))
687 return PTR_ERR(req);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800688
689 memset(&inarg, 0, sizeof(inarg));
690 inarg.mask = mask;
691 req->in.h.opcode = FUSE_ACCESS;
692 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800693 req->in.numargs = 1;
694 req->in.args[0].size = sizeof(inarg);
695 req->in.args[0].value = &inarg;
696 request_send(fc, req);
697 err = req->out.h.error;
698 fuse_put_request(fc, req);
699 if (err == -ENOSYS) {
700 fc->no_access = 1;
701 err = 0;
702 }
703 return err;
704}
705
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800706/*
707 * Check permission. The two basic access models of FUSE are:
708 *
709 * 1) Local access checking ('default_permissions' mount option) based
710 * on file mode. This is the plain old disk filesystem permission
711 * modell.
712 *
713 * 2) "Remote" access checking, where server is responsible for
714 * checking permission in each inode operation. An exception to this
715 * is if ->permission() was invoked from sys_access() in which case an
716 * access request is sent. Execute permission is still checked
717 * locally based on file mode.
718 */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700719static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
720{
721 struct fuse_conn *fc = get_fuse_conn(inode);
722
Miklos Szeredi87729a52005-09-09 13:10:34 -0700723 if (!fuse_allow_task(fc, current))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700724 return -EACCES;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700725 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
726 int err = generic_permission(inode, mask, NULL);
727
728 /* If permission is denied, try to refresh file
729 attributes. This is also needed, because the root
730 node will at first have no permissions */
731 if (err == -EACCES) {
732 err = fuse_do_getattr(inode);
733 if (!err)
734 err = generic_permission(inode, mask, NULL);
735 }
736
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800737 /* Note: the opposite of the above test does not
738 exist. So if permissions are revoked this won't be
739 noticed immediately, only after the attribute
740 timeout has expired */
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700741
742 return err;
743 } else {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700744 int mode = inode->i_mode;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700745 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
746 return -EACCES;
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800747
748 if (nd && (nd->flags & LOOKUP_ACCESS))
749 return fuse_access(inode, mask);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700750 return 0;
751 }
752}
753
754static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
755 void *dstbuf, filldir_t filldir)
756{
757 while (nbytes >= FUSE_NAME_OFFSET) {
758 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
759 size_t reclen = FUSE_DIRENT_SIZE(dirent);
760 int over;
761 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
762 return -EIO;
763 if (reclen > nbytes)
764 break;
765
766 over = filldir(dstbuf, dirent->name, dirent->namelen,
767 file->f_pos, dirent->ino, dirent->type);
768 if (over)
769 break;
770
771 buf += reclen;
772 nbytes -= reclen;
773 file->f_pos = dirent->off;
774 }
775
776 return 0;
777}
778
Miklos Szeredie5e55582005-09-09 13:10:28 -0700779static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
780{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700781 int err;
782 size_t nbytes;
783 struct page *page;
784 struct inode *inode = file->f_dentry->d_inode;
785 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800786 struct fuse_req *req;
787
788 if (is_bad_inode(inode))
789 return -EIO;
790
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700791 req = fuse_get_req(fc);
792 if (IS_ERR(req))
793 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700794
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700795 page = alloc_page(GFP_KERNEL);
796 if (!page) {
797 fuse_put_request(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700798 return -ENOMEM;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700799 }
800 req->num_pages = 1;
801 req->pages[0] = page;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800802 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
803 request_send(fc, req);
804 nbytes = req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700805 err = req->out.h.error;
806 fuse_put_request(fc, req);
807 if (!err)
808 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
809 filldir);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700810
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700811 __free_page(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700812 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700813 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700814}
815
816static char *read_link(struct dentry *dentry)
817{
818 struct inode *inode = dentry->d_inode;
819 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700820 struct fuse_req *req = fuse_get_req(fc);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700821 char *link;
822
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700823 if (IS_ERR(req))
824 return ERR_PTR(PTR_ERR(req));
Miklos Szeredie5e55582005-09-09 13:10:28 -0700825
826 link = (char *) __get_free_page(GFP_KERNEL);
827 if (!link) {
828 link = ERR_PTR(-ENOMEM);
829 goto out;
830 }
831 req->in.h.opcode = FUSE_READLINK;
832 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700833 req->out.argvar = 1;
834 req->out.numargs = 1;
835 req->out.args[0].size = PAGE_SIZE - 1;
836 req->out.args[0].value = link;
837 request_send(fc, req);
838 if (req->out.h.error) {
839 free_page((unsigned long) link);
840 link = ERR_PTR(req->out.h.error);
841 } else
842 link[req->out.args[0].size] = '\0';
843 out:
844 fuse_put_request(fc, req);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700845 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700846 return link;
847}
848
849static void free_link(char *link)
850{
851 if (!IS_ERR(link))
852 free_page((unsigned long) link);
853}
854
855static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
856{
857 nd_set_link(nd, read_link(dentry));
858 return NULL;
859}
860
861static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
862{
863 free_link(nd_get_link(nd));
864}
865
866static int fuse_dir_open(struct inode *inode, struct file *file)
867{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700868 return fuse_open_common(inode, file, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700869}
870
871static int fuse_dir_release(struct inode *inode, struct file *file)
872{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700873 return fuse_release_common(inode, file, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700874}
875
Miklos Szeredi82547982005-09-09 13:10:38 -0700876static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
877{
878 /* nfsd can call this with no file */
879 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
880}
881
Miklos Szeredibefc6492005-11-07 00:59:52 -0800882static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700883{
884 unsigned ivalid = iattr->ia_valid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700885
886 if (ivalid & ATTR_MODE)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800887 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700888 if (ivalid & ATTR_UID)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800889 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700890 if (ivalid & ATTR_GID)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800891 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700892 if (ivalid & ATTR_SIZE)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800893 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700894 /* You can only _set_ these together (they may change by themselves) */
895 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
Miklos Szeredibefc6492005-11-07 00:59:52 -0800896 arg->valid |= FATTR_ATIME | FATTR_MTIME;
897 arg->atime = iattr->ia_atime.tv_sec;
898 arg->mtime = iattr->ia_mtime.tv_sec;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700899 }
Miklos Szeredibefc6492005-11-07 00:59:52 -0800900 if (ivalid & ATTR_FILE) {
901 struct fuse_file *ff = iattr->ia_file->private_data;
902 arg->valid |= FATTR_FH;
903 arg->fh = ff->fh;
904 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700905}
906
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800907/*
908 * Set attributes, and at the same time refresh them.
909 *
910 * Truncation is slightly complicated, because the 'truncate' request
911 * may fail, in which case we don't want to touch the mapping.
912 * vmtruncate() doesn't allow for this case. So do the rlimit
913 * checking by hand and call vmtruncate() only after the file has
914 * actually been truncated.
915 */
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700916static int fuse_setattr(struct dentry *entry, struct iattr *attr)
917{
918 struct inode *inode = entry->d_inode;
919 struct fuse_conn *fc = get_fuse_conn(inode);
920 struct fuse_inode *fi = get_fuse_inode(inode);
921 struct fuse_req *req;
922 struct fuse_setattr_in inarg;
923 struct fuse_attr_out outarg;
924 int err;
925 int is_truncate = 0;
926
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700927 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
928 err = inode_change_ok(inode, attr);
929 if (err)
930 return err;
931 }
932
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700933 if (attr->ia_valid & ATTR_SIZE) {
934 unsigned long limit;
935 is_truncate = 1;
936 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
937 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
938 send_sig(SIGXFSZ, current, 0);
939 return -EFBIG;
940 }
941 }
942
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700943 req = fuse_get_req(fc);
944 if (IS_ERR(req))
945 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700946
947 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredibefc6492005-11-07 00:59:52 -0800948 iattr_to_fattr(attr, &inarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700949 req->in.h.opcode = FUSE_SETATTR;
950 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700951 req->in.numargs = 1;
952 req->in.args[0].size = sizeof(inarg);
953 req->in.args[0].value = &inarg;
954 req->out.numargs = 1;
955 req->out.args[0].size = sizeof(outarg);
956 req->out.args[0].value = &outarg;
957 request_send(fc, req);
958 err = req->out.h.error;
959 fuse_put_request(fc, req);
960 if (!err) {
961 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
962 make_bad_inode(inode);
963 err = -EIO;
964 } else {
965 if (is_truncate) {
966 loff_t origsize = i_size_read(inode);
967 i_size_write(inode, outarg.attr.size);
968 if (origsize > outarg.attr.size)
969 vmtruncate(inode, outarg.attr.size);
970 }
971 fuse_change_attributes(inode, &outarg.attr);
972 fi->i_time = time_to_jiffies(outarg.attr_valid,
973 outarg.attr_valid_nsec);
974 }
975 } else if (err == -EINTR)
976 fuse_invalidate_attr(inode);
977
978 return err;
979}
980
Miklos Szeredie5e55582005-09-09 13:10:28 -0700981static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
982 struct kstat *stat)
983{
984 struct inode *inode = entry->d_inode;
985 int err = fuse_revalidate(entry);
986 if (!err)
987 generic_fillattr(inode, stat);
988
989 return err;
990}
991
Miklos Szeredi92a87802005-09-09 13:10:31 -0700992static int fuse_setxattr(struct dentry *entry, const char *name,
993 const void *value, size_t size, int flags)
994{
995 struct inode *inode = entry->d_inode;
996 struct fuse_conn *fc = get_fuse_conn(inode);
997 struct fuse_req *req;
998 struct fuse_setxattr_in inarg;
999 int err;
1000
Miklos Szeredi92a87802005-09-09 13:10:31 -07001001 if (fc->no_setxattr)
1002 return -EOPNOTSUPP;
1003
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001004 req = fuse_get_req(fc);
1005 if (IS_ERR(req))
1006 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001007
1008 memset(&inarg, 0, sizeof(inarg));
1009 inarg.size = size;
1010 inarg.flags = flags;
1011 req->in.h.opcode = FUSE_SETXATTR;
1012 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001013 req->in.numargs = 3;
1014 req->in.args[0].size = sizeof(inarg);
1015 req->in.args[0].value = &inarg;
1016 req->in.args[1].size = strlen(name) + 1;
1017 req->in.args[1].value = name;
1018 req->in.args[2].size = size;
1019 req->in.args[2].value = value;
1020 request_send(fc, req);
1021 err = req->out.h.error;
1022 fuse_put_request(fc, req);
1023 if (err == -ENOSYS) {
1024 fc->no_setxattr = 1;
1025 err = -EOPNOTSUPP;
1026 }
1027 return err;
1028}
1029
1030static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1031 void *value, size_t size)
1032{
1033 struct inode *inode = entry->d_inode;
1034 struct fuse_conn *fc = get_fuse_conn(inode);
1035 struct fuse_req *req;
1036 struct fuse_getxattr_in inarg;
1037 struct fuse_getxattr_out outarg;
1038 ssize_t ret;
1039
1040 if (fc->no_getxattr)
1041 return -EOPNOTSUPP;
1042
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001043 req = fuse_get_req(fc);
1044 if (IS_ERR(req))
1045 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001046
1047 memset(&inarg, 0, sizeof(inarg));
1048 inarg.size = size;
1049 req->in.h.opcode = FUSE_GETXATTR;
1050 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001051 req->in.numargs = 2;
1052 req->in.args[0].size = sizeof(inarg);
1053 req->in.args[0].value = &inarg;
1054 req->in.args[1].size = strlen(name) + 1;
1055 req->in.args[1].value = name;
1056 /* This is really two different operations rolled into one */
1057 req->out.numargs = 1;
1058 if (size) {
1059 req->out.argvar = 1;
1060 req->out.args[0].size = size;
1061 req->out.args[0].value = value;
1062 } else {
1063 req->out.args[0].size = sizeof(outarg);
1064 req->out.args[0].value = &outarg;
1065 }
1066 request_send(fc, req);
1067 ret = req->out.h.error;
1068 if (!ret)
1069 ret = size ? req->out.args[0].size : outarg.size;
1070 else {
1071 if (ret == -ENOSYS) {
1072 fc->no_getxattr = 1;
1073 ret = -EOPNOTSUPP;
1074 }
1075 }
1076 fuse_put_request(fc, req);
1077 return ret;
1078}
1079
1080static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1081{
1082 struct inode *inode = entry->d_inode;
1083 struct fuse_conn *fc = get_fuse_conn(inode);
1084 struct fuse_req *req;
1085 struct fuse_getxattr_in inarg;
1086 struct fuse_getxattr_out outarg;
1087 ssize_t ret;
1088
1089 if (fc->no_listxattr)
1090 return -EOPNOTSUPP;
1091
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001092 req = fuse_get_req(fc);
1093 if (IS_ERR(req))
1094 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001095
1096 memset(&inarg, 0, sizeof(inarg));
1097 inarg.size = size;
1098 req->in.h.opcode = FUSE_LISTXATTR;
1099 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001100 req->in.numargs = 1;
1101 req->in.args[0].size = sizeof(inarg);
1102 req->in.args[0].value = &inarg;
1103 /* This is really two different operations rolled into one */
1104 req->out.numargs = 1;
1105 if (size) {
1106 req->out.argvar = 1;
1107 req->out.args[0].size = size;
1108 req->out.args[0].value = list;
1109 } else {
1110 req->out.args[0].size = sizeof(outarg);
1111 req->out.args[0].value = &outarg;
1112 }
1113 request_send(fc, req);
1114 ret = req->out.h.error;
1115 if (!ret)
1116 ret = size ? req->out.args[0].size : outarg.size;
1117 else {
1118 if (ret == -ENOSYS) {
1119 fc->no_listxattr = 1;
1120 ret = -EOPNOTSUPP;
1121 }
1122 }
1123 fuse_put_request(fc, req);
1124 return ret;
1125}
1126
1127static int fuse_removexattr(struct dentry *entry, const char *name)
1128{
1129 struct inode *inode = entry->d_inode;
1130 struct fuse_conn *fc = get_fuse_conn(inode);
1131 struct fuse_req *req;
1132 int err;
1133
1134 if (fc->no_removexattr)
1135 return -EOPNOTSUPP;
1136
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001137 req = fuse_get_req(fc);
1138 if (IS_ERR(req))
1139 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001140
1141 req->in.h.opcode = FUSE_REMOVEXATTR;
1142 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001143 req->in.numargs = 1;
1144 req->in.args[0].size = strlen(name) + 1;
1145 req->in.args[0].value = name;
1146 request_send(fc, req);
1147 err = req->out.h.error;
1148 fuse_put_request(fc, req);
1149 if (err == -ENOSYS) {
1150 fc->no_removexattr = 1;
1151 err = -EOPNOTSUPP;
1152 }
1153 return err;
1154}
1155
Miklos Szeredie5e55582005-09-09 13:10:28 -07001156static struct inode_operations fuse_dir_inode_operations = {
1157 .lookup = fuse_lookup,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001158 .mkdir = fuse_mkdir,
1159 .symlink = fuse_symlink,
1160 .unlink = fuse_unlink,
1161 .rmdir = fuse_rmdir,
1162 .rename = fuse_rename,
1163 .link = fuse_link,
1164 .setattr = fuse_setattr,
1165 .create = fuse_create,
1166 .mknod = fuse_mknod,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001167 .permission = fuse_permission,
1168 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001169 .setxattr = fuse_setxattr,
1170 .getxattr = fuse_getxattr,
1171 .listxattr = fuse_listxattr,
1172 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001173};
1174
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001175static const struct file_operations fuse_dir_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001176 .llseek = generic_file_llseek,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001177 .read = generic_read_dir,
1178 .readdir = fuse_readdir,
1179 .open = fuse_dir_open,
1180 .release = fuse_dir_release,
Miklos Szeredi82547982005-09-09 13:10:38 -07001181 .fsync = fuse_dir_fsync,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001182};
1183
1184static struct inode_operations fuse_common_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001185 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001186 .permission = fuse_permission,
1187 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001188 .setxattr = fuse_setxattr,
1189 .getxattr = fuse_getxattr,
1190 .listxattr = fuse_listxattr,
1191 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001192};
1193
1194static struct inode_operations fuse_symlink_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001195 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001196 .follow_link = fuse_follow_link,
1197 .put_link = fuse_put_link,
1198 .readlink = generic_readlink,
1199 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001200 .setxattr = fuse_setxattr,
1201 .getxattr = fuse_getxattr,
1202 .listxattr = fuse_listxattr,
1203 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001204};
1205
1206void fuse_init_common(struct inode *inode)
1207{
1208 inode->i_op = &fuse_common_inode_operations;
1209}
1210
1211void fuse_init_dir(struct inode *inode)
1212{
1213 inode->i_op = &fuse_dir_inode_operations;
1214 inode->i_fop = &fuse_dir_operations;
1215}
1216
1217void fuse_init_symlink(struct inode *inode)
1218{
1219 inode->i_op = &fuse_symlink_inode_operations;
1220}