blob: c72a8a97935c9733af8522eeb2533bd83c930ba1 [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
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);
82 req->inode = dir;
83 req->in.numargs = 1;
84 req->in.args[0].size = entry->d_name.len + 1;
85 req->in.args[0].value = entry->d_name.name;
86 req->out.numargs = 1;
87 req->out.args[0].size = sizeof(struct fuse_entry_out);
88 req->out.args[0].value = outarg;
89}
90
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080091/*
92 * Check whether the dentry is still valid
93 *
94 * If the entry validity timeout has expired and the dentry is
95 * positive, try to redo the lookup. If the lookup results in a
96 * different inode, then let the VFS invalidate the dentry and redo
97 * the lookup once more. If the lookup results in the same inode,
98 * then refresh the attributes, timeouts and mark the dentry valid.
99 */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700100static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
101{
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800102 struct inode *inode = entry->d_inode;
103
104 if (inode && is_bad_inode(inode))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700105 return 0;
106 else if (time_after(jiffies, entry->d_time)) {
107 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700108 struct fuse_entry_out outarg;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800109 struct fuse_conn *fc;
110 struct fuse_req *req;
111
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800112 /* Doesn't hurt to "reset" the validity timeout */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800113 fuse_invalidate_entry_cache(entry);
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800114
115 /* For negative dentries, always do a fresh lookup */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800116 if (!inode)
117 return 0;
118
119 fc = get_fuse_conn(inode);
120 req = fuse_get_request(fc);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700121 if (!req)
122 return 0;
123
124 fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700125 request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700126 err = req->out.h.error;
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800127 /* Zero nodeid is same as -ENOENT */
128 if (!err && !outarg.nodeid)
129 err = -ENOENT;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700130 if (!err) {
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800131 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700132 if (outarg.nodeid != get_node_id(inode)) {
133 fuse_send_forget(fc, req, outarg.nodeid, 1);
134 return 0;
135 }
136 fi->nlookup ++;
137 }
Miklos Szeredie5e55582005-09-09 13:10:28 -0700138 fuse_put_request(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700139 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700140 return 0;
141
142 fuse_change_attributes(inode, &outarg.attr);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800143 fuse_change_timeout(entry, &outarg);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700144 }
145 return 1;
146}
147
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800148/*
149 * Check if there's already a hashed alias of this directory inode.
150 * If yes, then lookup and mkdir must not create a new alias.
151 */
Miklos Szeredif007d5c2005-11-28 13:44:16 -0800152static int dir_alias(struct inode *inode)
153{
154 if (S_ISDIR(inode->i_mode)) {
Miklos Szeredif007d5c2005-11-28 13:44:16 -0800155 struct dentry *alias = d_find_alias(inode);
156 if (alias) {
157 dput(alias);
158 return 1;
159 }
160 }
161 return 0;
162}
163
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800164static int invalid_nodeid(u64 nodeid)
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800165{
166 return !nodeid || nodeid == FUSE_ROOT_ID;
167}
168
Miklos Szeredie5e55582005-09-09 13:10:28 -0700169static struct dentry_operations fuse_dentry_operations = {
170 .d_revalidate = fuse_dentry_revalidate,
171};
172
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800173static int valid_mode(int m)
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800174{
175 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
176 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
177}
178
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800179static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
180 struct nameidata *nd)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700181{
182 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700183 struct fuse_entry_out outarg;
184 struct inode *inode = NULL;
185 struct fuse_conn *fc = get_fuse_conn(dir);
186 struct fuse_req *req;
187
188 if (entry->d_name.len > FUSE_NAME_MAX)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800189 return ERR_PTR(-ENAMETOOLONG);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700190
191 req = fuse_get_request(fc);
192 if (!req)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800193 return ERR_PTR(-EINTR);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700194
195 fuse_lookup_init(req, dir, entry, &outarg);
196 request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700197 err = req->out.h.error;
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800198 /* Zero nodeid is same as -ENOENT, but with valid timeout */
199 if (!err && outarg.nodeid &&
200 (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
Miklos Szerediee4e5272005-09-27 21:45:21 -0700201 err = -EIO;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800202 if (!err && outarg.nodeid) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700203 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700204 &outarg.attr);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700205 if (!inode) {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700206 fuse_send_forget(fc, req, outarg.nodeid, 1);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800207 return ERR_PTR(-ENOMEM);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700208 }
209 }
210 fuse_put_request(fc, req);
211 if (err && err != -ENOENT)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800212 return ERR_PTR(err);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700213
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800214 if (inode && dir_alias(inode)) {
215 iput(inode);
216 return ERR_PTR(-EIO);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700217 }
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800218 d_add(entry, inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700219 entry->d_op = &fuse_dentry_operations;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800220 if (!err)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800221 fuse_change_timeout(entry, &outarg);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800222 else
223 fuse_invalidate_entry_cache(entry);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800224 return NULL;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700225}
226
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800227/*
228 * Atomic create+open operation
229 *
230 * If the filesystem doesn't support this, then fall back to separate
231 * 'mknod' + 'open' requests.
232 */
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800233static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
234 struct nameidata *nd)
235{
236 int err;
237 struct inode *inode;
238 struct fuse_conn *fc = get_fuse_conn(dir);
239 struct fuse_req *req;
240 struct fuse_open_in inarg;
241 struct fuse_open_out outopen;
242 struct fuse_entry_out outentry;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800243 struct fuse_file *ff;
244 struct file *file;
245 int flags = nd->intent.open.flags - 1;
246
247 err = -ENOSYS;
248 if (fc->no_create)
249 goto out;
250
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800251 err = -EINTR;
252 req = fuse_get_request(fc);
253 if (!req)
254 goto out;
255
256 ff = fuse_file_alloc();
257 if (!ff)
258 goto out_put_request;
259
260 flags &= ~O_NOCTTY;
261 memset(&inarg, 0, sizeof(inarg));
262 inarg.flags = flags;
263 inarg.mode = mode;
264 req->in.h.opcode = FUSE_CREATE;
265 req->in.h.nodeid = get_node_id(dir);
266 req->inode = dir;
267 req->in.numargs = 2;
268 req->in.args[0].size = sizeof(inarg);
269 req->in.args[0].value = &inarg;
270 req->in.args[1].size = entry->d_name.len + 1;
271 req->in.args[1].value = entry->d_name.name;
272 req->out.numargs = 2;
273 req->out.args[0].size = sizeof(outentry);
274 req->out.args[0].value = &outentry;
275 req->out.args[1].size = sizeof(outopen);
276 req->out.args[1].value = &outopen;
277 request_send(fc, req);
278 err = req->out.h.error;
279 if (err) {
280 if (err == -ENOSYS)
281 fc->no_create = 1;
282 goto out_free_ff;
283 }
284
285 err = -EIO;
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800286 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800287 goto out_free_ff;
288
289 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
290 &outentry.attr);
291 err = -ENOMEM;
292 if (!inode) {
293 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
294 ff->fh = outopen.fh;
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800295 /* Special release, with inode = NULL, this will
296 trigger a 'forget' request when the release is
297 complete */
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800298 fuse_send_release(fc, ff, outentry.nodeid, NULL, flags, 0);
299 goto out_put_request;
300 }
301 fuse_put_request(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800302 d_instantiate(entry, inode);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800303 fuse_change_timeout(entry, &outentry);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800304 file = lookup_instantiate_filp(nd, entry, generic_file_open);
305 if (IS_ERR(file)) {
306 ff->fh = outopen.fh;
307 fuse_send_release(fc, ff, outentry.nodeid, inode, flags, 0);
308 return PTR_ERR(file);
309 }
310 fuse_finish_open(inode, file, ff, &outopen);
311 return 0;
312
313 out_free_ff:
314 fuse_file_free(ff);
315 out_put_request:
316 fuse_put_request(fc, req);
317 out:
318 return err;
319}
320
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800321/*
322 * Code shared between mknod, mkdir, symlink and link
323 */
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700324static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
325 struct inode *dir, struct dentry *entry,
326 int mode)
327{
328 struct fuse_entry_out outarg;
329 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700330 int err;
331
332 req->in.h.nodeid = get_node_id(dir);
333 req->inode = dir;
334 req->out.numargs = 1;
335 req->out.args[0].size = sizeof(outarg);
336 req->out.args[0].value = &outarg;
337 request_send(fc, req);
338 err = req->out.h.error;
339 if (err) {
340 fuse_put_request(fc, req);
341 return err;
342 }
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800343 err = -EIO;
344 if (invalid_nodeid(outarg.nodeid))
345 goto out_put_request;
346
347 if ((outarg.attr.mode ^ mode) & S_IFMT)
348 goto out_put_request;
349
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700350 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
351 &outarg.attr);
352 if (!inode) {
353 fuse_send_forget(fc, req, outarg.nodeid, 1);
354 return -ENOMEM;
355 }
356 fuse_put_request(fc, req);
357
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800358 if (dir_alias(inode)) {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700359 iput(inode);
360 return -EIO;
361 }
362
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700363 d_instantiate(entry, inode);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800364 fuse_change_timeout(entry, &outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700365 fuse_invalidate_attr(dir);
366 return 0;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800367
368 out_put_request:
369 fuse_put_request(fc, req);
370 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700371}
372
373static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
374 dev_t rdev)
375{
376 struct fuse_mknod_in inarg;
377 struct fuse_conn *fc = get_fuse_conn(dir);
378 struct fuse_req *req = fuse_get_request(fc);
379 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700380 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700381
382 memset(&inarg, 0, sizeof(inarg));
383 inarg.mode = mode;
384 inarg.rdev = new_encode_dev(rdev);
385 req->in.h.opcode = FUSE_MKNOD;
386 req->in.numargs = 2;
387 req->in.args[0].size = sizeof(inarg);
388 req->in.args[0].value = &inarg;
389 req->in.args[1].size = entry->d_name.len + 1;
390 req->in.args[1].value = entry->d_name.name;
391 return create_new_entry(fc, req, dir, entry, mode);
392}
393
394static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
395 struct nameidata *nd)
396{
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800397 if (nd && (nd->flags & LOOKUP_CREATE)) {
398 int err = fuse_create_open(dir, entry, mode, nd);
399 if (err != -ENOSYS)
400 return err;
401 /* Fall back on mknod */
402 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700403 return fuse_mknod(dir, entry, mode, 0);
404}
405
406static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
407{
408 struct fuse_mkdir_in inarg;
409 struct fuse_conn *fc = get_fuse_conn(dir);
410 struct fuse_req *req = fuse_get_request(fc);
411 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700412 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700413
414 memset(&inarg, 0, sizeof(inarg));
415 inarg.mode = mode;
416 req->in.h.opcode = FUSE_MKDIR;
417 req->in.numargs = 2;
418 req->in.args[0].size = sizeof(inarg);
419 req->in.args[0].value = &inarg;
420 req->in.args[1].size = entry->d_name.len + 1;
421 req->in.args[1].value = entry->d_name.name;
422 return create_new_entry(fc, req, dir, entry, S_IFDIR);
423}
424
425static int fuse_symlink(struct inode *dir, struct dentry *entry,
426 const char *link)
427{
428 struct fuse_conn *fc = get_fuse_conn(dir);
429 unsigned len = strlen(link) + 1;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800430 struct fuse_req *req = fuse_get_request(fc);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700431 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700432 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700433
434 req->in.h.opcode = FUSE_SYMLINK;
435 req->in.numargs = 2;
436 req->in.args[0].size = entry->d_name.len + 1;
437 req->in.args[0].value = entry->d_name.name;
438 req->in.args[1].size = len;
439 req->in.args[1].value = link;
440 return create_new_entry(fc, req, dir, entry, S_IFLNK);
441}
442
443static int fuse_unlink(struct inode *dir, struct dentry *entry)
444{
445 int err;
446 struct fuse_conn *fc = get_fuse_conn(dir);
447 struct fuse_req *req = fuse_get_request(fc);
448 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700449 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700450
451 req->in.h.opcode = FUSE_UNLINK;
452 req->in.h.nodeid = get_node_id(dir);
453 req->inode = dir;
454 req->in.numargs = 1;
455 req->in.args[0].size = entry->d_name.len + 1;
456 req->in.args[0].value = entry->d_name.name;
457 request_send(fc, req);
458 err = req->out.h.error;
459 fuse_put_request(fc, req);
460 if (!err) {
461 struct inode *inode = entry->d_inode;
462
463 /* Set nlink to zero so the inode can be cleared, if
464 the inode does have more links this will be
465 discovered at the next lookup/getattr */
466 inode->i_nlink = 0;
467 fuse_invalidate_attr(inode);
468 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800469 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700470 } else if (err == -EINTR)
471 fuse_invalidate_entry(entry);
472 return err;
473}
474
475static int fuse_rmdir(struct inode *dir, struct dentry *entry)
476{
477 int err;
478 struct fuse_conn *fc = get_fuse_conn(dir);
479 struct fuse_req *req = fuse_get_request(fc);
480 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700481 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700482
483 req->in.h.opcode = FUSE_RMDIR;
484 req->in.h.nodeid = get_node_id(dir);
485 req->inode = dir;
486 req->in.numargs = 1;
487 req->in.args[0].size = entry->d_name.len + 1;
488 req->in.args[0].value = entry->d_name.name;
489 request_send(fc, req);
490 err = req->out.h.error;
491 fuse_put_request(fc, req);
492 if (!err) {
493 entry->d_inode->i_nlink = 0;
494 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800495 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700496 } else if (err == -EINTR)
497 fuse_invalidate_entry(entry);
498 return err;
499}
500
501static int fuse_rename(struct inode *olddir, struct dentry *oldent,
502 struct inode *newdir, struct dentry *newent)
503{
504 int err;
505 struct fuse_rename_in inarg;
506 struct fuse_conn *fc = get_fuse_conn(olddir);
507 struct fuse_req *req = fuse_get_request(fc);
508 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700509 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700510
511 memset(&inarg, 0, sizeof(inarg));
512 inarg.newdir = get_node_id(newdir);
513 req->in.h.opcode = FUSE_RENAME;
514 req->in.h.nodeid = get_node_id(olddir);
515 req->inode = olddir;
516 req->inode2 = newdir;
517 req->in.numargs = 3;
518 req->in.args[0].size = sizeof(inarg);
519 req->in.args[0].value = &inarg;
520 req->in.args[1].size = oldent->d_name.len + 1;
521 req->in.args[1].value = oldent->d_name.name;
522 req->in.args[2].size = newent->d_name.len + 1;
523 req->in.args[2].value = newent->d_name.name;
524 request_send(fc, req);
525 err = req->out.h.error;
526 fuse_put_request(fc, req);
527 if (!err) {
528 fuse_invalidate_attr(olddir);
529 if (olddir != newdir)
530 fuse_invalidate_attr(newdir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800531
532 /* newent will end up negative */
533 if (newent->d_inode)
534 fuse_invalidate_entry_cache(newent);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700535 } else if (err == -EINTR) {
536 /* If request was interrupted, DEITY only knows if the
537 rename actually took place. If the invalidation
538 fails (e.g. some process has CWD under the renamed
539 directory), then there can be inconsistency between
540 the dcache and the real filesystem. Tough luck. */
541 fuse_invalidate_entry(oldent);
542 if (newent->d_inode)
543 fuse_invalidate_entry(newent);
544 }
545
546 return err;
547}
548
549static int fuse_link(struct dentry *entry, struct inode *newdir,
550 struct dentry *newent)
551{
552 int err;
553 struct fuse_link_in inarg;
554 struct inode *inode = entry->d_inode;
555 struct fuse_conn *fc = get_fuse_conn(inode);
556 struct fuse_req *req = fuse_get_request(fc);
557 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700558 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700559
560 memset(&inarg, 0, sizeof(inarg));
561 inarg.oldnodeid = get_node_id(inode);
562 req->in.h.opcode = FUSE_LINK;
563 req->inode2 = inode;
564 req->in.numargs = 2;
565 req->in.args[0].size = sizeof(inarg);
566 req->in.args[0].value = &inarg;
567 req->in.args[1].size = newent->d_name.len + 1;
568 req->in.args[1].value = newent->d_name.name;
569 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
570 /* Contrary to "normal" filesystems it can happen that link
571 makes two "logical" inodes point to the same "physical"
572 inode. We invalidate the attributes of the old one, so it
573 will reflect changes in the backing inode (link count,
574 etc.)
575 */
576 if (!err || err == -EINTR)
577 fuse_invalidate_attr(inode);
578 return err;
579}
580
Miklos Szeredie5e55582005-09-09 13:10:28 -0700581int fuse_do_getattr(struct inode *inode)
582{
583 int err;
584 struct fuse_attr_out arg;
585 struct fuse_conn *fc = get_fuse_conn(inode);
586 struct fuse_req *req = fuse_get_request(fc);
587 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700588 return -EINTR;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700589
590 req->in.h.opcode = FUSE_GETATTR;
591 req->in.h.nodeid = get_node_id(inode);
592 req->inode = inode;
593 req->out.numargs = 1;
594 req->out.args[0].size = sizeof(arg);
595 req->out.args[0].value = &arg;
596 request_send(fc, req);
597 err = req->out.h.error;
598 fuse_put_request(fc, req);
599 if (!err) {
600 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
601 make_bad_inode(inode);
602 err = -EIO;
603 } else {
604 struct fuse_inode *fi = get_fuse_inode(inode);
605 fuse_change_attributes(inode, &arg.attr);
606 fi->i_time = time_to_jiffies(arg.attr_valid,
607 arg.attr_valid_nsec);
608 }
609 }
610 return err;
611}
612
Miklos Szeredi87729a52005-09-09 13:10:34 -0700613/*
614 * Calling into a user-controlled filesystem gives the filesystem
615 * daemon ptrace-like capabilities over the requester process. This
616 * means, that the filesystem daemon is able to record the exact
617 * filesystem operations performed, and can also control the behavior
618 * of the requester process in otherwise impossible ways. For example
619 * it can delay the operation for arbitrary length of time allowing
620 * DoS against the requester.
621 *
622 * For this reason only those processes can call into the filesystem,
623 * for which the owner of the mount has ptrace privilege. This
624 * excludes processes started by other users, suid or sgid processes.
625 */
626static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
627{
628 if (fc->flags & FUSE_ALLOW_OTHER)
629 return 1;
630
631 if (task->euid == fc->user_id &&
632 task->suid == fc->user_id &&
633 task->uid == fc->user_id &&
634 task->egid == fc->group_id &&
635 task->sgid == fc->group_id &&
636 task->gid == fc->group_id)
637 return 1;
638
639 return 0;
640}
641
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800642/*
643 * Check whether the inode attributes are still valid
644 *
645 * If the attribute validity timeout has expired, then fetch the fresh
646 * attributes with a 'getattr' request
647 *
648 * I'm not sure why cached attributes are never returned for the root
649 * inode, this is probably being too cautious.
650 */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700651static int fuse_revalidate(struct dentry *entry)
652{
653 struct inode *inode = entry->d_inode;
654 struct fuse_inode *fi = get_fuse_inode(inode);
655 struct fuse_conn *fc = get_fuse_conn(inode);
656
Miklos Szeredi87729a52005-09-09 13:10:34 -0700657 if (!fuse_allow_task(fc, current))
658 return -EACCES;
659 if (get_node_id(inode) != FUSE_ROOT_ID &&
660 time_before_eq(jiffies, fi->i_time))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700661 return 0;
662
663 return fuse_do_getattr(inode);
664}
665
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800666static int fuse_access(struct inode *inode, int mask)
667{
668 struct fuse_conn *fc = get_fuse_conn(inode);
669 struct fuse_req *req;
670 struct fuse_access_in inarg;
671 int err;
672
673 if (fc->no_access)
674 return 0;
675
676 req = fuse_get_request(fc);
677 if (!req)
678 return -EINTR;
679
680 memset(&inarg, 0, sizeof(inarg));
681 inarg.mask = mask;
682 req->in.h.opcode = FUSE_ACCESS;
683 req->in.h.nodeid = get_node_id(inode);
684 req->inode = inode;
685 req->in.numargs = 1;
686 req->in.args[0].size = sizeof(inarg);
687 req->in.args[0].value = &inarg;
688 request_send(fc, req);
689 err = req->out.h.error;
690 fuse_put_request(fc, req);
691 if (err == -ENOSYS) {
692 fc->no_access = 1;
693 err = 0;
694 }
695 return err;
696}
697
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800698/*
699 * Check permission. The two basic access models of FUSE are:
700 *
701 * 1) Local access checking ('default_permissions' mount option) based
702 * on file mode. This is the plain old disk filesystem permission
703 * modell.
704 *
705 * 2) "Remote" access checking, where server is responsible for
706 * checking permission in each inode operation. An exception to this
707 * is if ->permission() was invoked from sys_access() in which case an
708 * access request is sent. Execute permission is still checked
709 * locally based on file mode.
710 */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700711static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
712{
713 struct fuse_conn *fc = get_fuse_conn(inode);
714
Miklos Szeredi87729a52005-09-09 13:10:34 -0700715 if (!fuse_allow_task(fc, current))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700716 return -EACCES;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700717 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
718 int err = generic_permission(inode, mask, NULL);
719
720 /* If permission is denied, try to refresh file
721 attributes. This is also needed, because the root
722 node will at first have no permissions */
723 if (err == -EACCES) {
724 err = fuse_do_getattr(inode);
725 if (!err)
726 err = generic_permission(inode, mask, NULL);
727 }
728
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800729 /* Note: the opposite of the above test does not
730 exist. So if permissions are revoked this won't be
731 noticed immediately, only after the attribute
732 timeout has expired */
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700733
734 return err;
735 } else {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700736 int mode = inode->i_mode;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700737 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
738 return -EACCES;
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800739
740 if (nd && (nd->flags & LOOKUP_ACCESS))
741 return fuse_access(inode, mask);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700742 return 0;
743 }
744}
745
746static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
747 void *dstbuf, filldir_t filldir)
748{
749 while (nbytes >= FUSE_NAME_OFFSET) {
750 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
751 size_t reclen = FUSE_DIRENT_SIZE(dirent);
752 int over;
753 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
754 return -EIO;
755 if (reclen > nbytes)
756 break;
757
758 over = filldir(dstbuf, dirent->name, dirent->namelen,
759 file->f_pos, dirent->ino, dirent->type);
760 if (over)
761 break;
762
763 buf += reclen;
764 nbytes -= reclen;
765 file->f_pos = dirent->off;
766 }
767
768 return 0;
769}
770
Miklos Szeredie5e55582005-09-09 13:10:28 -0700771static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
772{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700773 int err;
774 size_t nbytes;
775 struct page *page;
776 struct inode *inode = file->f_dentry->d_inode;
777 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800778 struct fuse_req *req;
779
780 if (is_bad_inode(inode))
781 return -EIO;
782
783 req = fuse_get_request(fc);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700784 if (!req)
785 return -EINTR;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700786
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700787 page = alloc_page(GFP_KERNEL);
788 if (!page) {
789 fuse_put_request(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700790 return -ENOMEM;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700791 }
792 req->num_pages = 1;
793 req->pages[0] = page;
Miklos Szeredi361b1eb2006-01-16 22:14:45 -0800794 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
795 request_send(fc, req);
796 nbytes = req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700797 err = req->out.h.error;
798 fuse_put_request(fc, req);
799 if (!err)
800 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
801 filldir);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700802
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700803 __free_page(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700804 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700805 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700806}
807
808static char *read_link(struct dentry *dentry)
809{
810 struct inode *inode = dentry->d_inode;
811 struct fuse_conn *fc = get_fuse_conn(inode);
812 struct fuse_req *req = fuse_get_request(fc);
813 char *link;
814
815 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700816 return ERR_PTR(-EINTR);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700817
818 link = (char *) __get_free_page(GFP_KERNEL);
819 if (!link) {
820 link = ERR_PTR(-ENOMEM);
821 goto out;
822 }
823 req->in.h.opcode = FUSE_READLINK;
824 req->in.h.nodeid = get_node_id(inode);
825 req->inode = inode;
826 req->out.argvar = 1;
827 req->out.numargs = 1;
828 req->out.args[0].size = PAGE_SIZE - 1;
829 req->out.args[0].value = link;
830 request_send(fc, req);
831 if (req->out.h.error) {
832 free_page((unsigned long) link);
833 link = ERR_PTR(req->out.h.error);
834 } else
835 link[req->out.args[0].size] = '\0';
836 out:
837 fuse_put_request(fc, req);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700838 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700839 return link;
840}
841
842static void free_link(char *link)
843{
844 if (!IS_ERR(link))
845 free_page((unsigned long) link);
846}
847
848static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
849{
850 nd_set_link(nd, read_link(dentry));
851 return NULL;
852}
853
854static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
855{
856 free_link(nd_get_link(nd));
857}
858
859static int fuse_dir_open(struct inode *inode, struct file *file)
860{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700861 return fuse_open_common(inode, file, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700862}
863
864static int fuse_dir_release(struct inode *inode, struct file *file)
865{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700866 return fuse_release_common(inode, file, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700867}
868
Miklos Szeredi82547982005-09-09 13:10:38 -0700869static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
870{
871 /* nfsd can call this with no file */
872 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
873}
874
Miklos Szeredibefc6492005-11-07 00:59:52 -0800875static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700876{
877 unsigned ivalid = iattr->ia_valid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700878
879 if (ivalid & ATTR_MODE)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800880 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700881 if (ivalid & ATTR_UID)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800882 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700883 if (ivalid & ATTR_GID)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800884 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700885 if (ivalid & ATTR_SIZE)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800886 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700887 /* You can only _set_ these together (they may change by themselves) */
888 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
Miklos Szeredibefc6492005-11-07 00:59:52 -0800889 arg->valid |= FATTR_ATIME | FATTR_MTIME;
890 arg->atime = iattr->ia_atime.tv_sec;
891 arg->mtime = iattr->ia_mtime.tv_sec;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700892 }
Miklos Szeredibefc6492005-11-07 00:59:52 -0800893 if (ivalid & ATTR_FILE) {
894 struct fuse_file *ff = iattr->ia_file->private_data;
895 arg->valid |= FATTR_FH;
896 arg->fh = ff->fh;
897 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700898}
899
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800900/*
901 * Set attributes, and at the same time refresh them.
902 *
903 * Truncation is slightly complicated, because the 'truncate' request
904 * may fail, in which case we don't want to touch the mapping.
905 * vmtruncate() doesn't allow for this case. So do the rlimit
906 * checking by hand and call vmtruncate() only after the file has
907 * actually been truncated.
908 */
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700909static int fuse_setattr(struct dentry *entry, struct iattr *attr)
910{
911 struct inode *inode = entry->d_inode;
912 struct fuse_conn *fc = get_fuse_conn(inode);
913 struct fuse_inode *fi = get_fuse_inode(inode);
914 struct fuse_req *req;
915 struct fuse_setattr_in inarg;
916 struct fuse_attr_out outarg;
917 int err;
918 int is_truncate = 0;
919
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700920 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
921 err = inode_change_ok(inode, attr);
922 if (err)
923 return err;
924 }
925
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700926 if (attr->ia_valid & ATTR_SIZE) {
927 unsigned long limit;
928 is_truncate = 1;
929 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
930 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
931 send_sig(SIGXFSZ, current, 0);
932 return -EFBIG;
933 }
934 }
935
936 req = fuse_get_request(fc);
937 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700938 return -EINTR;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700939
940 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredibefc6492005-11-07 00:59:52 -0800941 iattr_to_fattr(attr, &inarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700942 req->in.h.opcode = FUSE_SETATTR;
943 req->in.h.nodeid = get_node_id(inode);
944 req->inode = inode;
945 req->in.numargs = 1;
946 req->in.args[0].size = sizeof(inarg);
947 req->in.args[0].value = &inarg;
948 req->out.numargs = 1;
949 req->out.args[0].size = sizeof(outarg);
950 req->out.args[0].value = &outarg;
951 request_send(fc, req);
952 err = req->out.h.error;
953 fuse_put_request(fc, req);
954 if (!err) {
955 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
956 make_bad_inode(inode);
957 err = -EIO;
958 } else {
959 if (is_truncate) {
960 loff_t origsize = i_size_read(inode);
961 i_size_write(inode, outarg.attr.size);
962 if (origsize > outarg.attr.size)
963 vmtruncate(inode, outarg.attr.size);
964 }
965 fuse_change_attributes(inode, &outarg.attr);
966 fi->i_time = time_to_jiffies(outarg.attr_valid,
967 outarg.attr_valid_nsec);
968 }
969 } else if (err == -EINTR)
970 fuse_invalidate_attr(inode);
971
972 return err;
973}
974
Miklos Szeredie5e55582005-09-09 13:10:28 -0700975static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
976 struct kstat *stat)
977{
978 struct inode *inode = entry->d_inode;
979 int err = fuse_revalidate(entry);
980 if (!err)
981 generic_fillattr(inode, stat);
982
983 return err;
984}
985
Miklos Szeredi92a87802005-09-09 13:10:31 -0700986static int fuse_setxattr(struct dentry *entry, const char *name,
987 const void *value, size_t size, int flags)
988{
989 struct inode *inode = entry->d_inode;
990 struct fuse_conn *fc = get_fuse_conn(inode);
991 struct fuse_req *req;
992 struct fuse_setxattr_in inarg;
993 int err;
994
Miklos Szeredi92a87802005-09-09 13:10:31 -0700995 if (fc->no_setxattr)
996 return -EOPNOTSUPP;
997
998 req = fuse_get_request(fc);
999 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -07001000 return -EINTR;
Miklos Szeredi92a87802005-09-09 13:10:31 -07001001
1002 memset(&inarg, 0, sizeof(inarg));
1003 inarg.size = size;
1004 inarg.flags = flags;
1005 req->in.h.opcode = FUSE_SETXATTR;
1006 req->in.h.nodeid = get_node_id(inode);
1007 req->inode = inode;
1008 req->in.numargs = 3;
1009 req->in.args[0].size = sizeof(inarg);
1010 req->in.args[0].value = &inarg;
1011 req->in.args[1].size = strlen(name) + 1;
1012 req->in.args[1].value = name;
1013 req->in.args[2].size = size;
1014 req->in.args[2].value = value;
1015 request_send(fc, req);
1016 err = req->out.h.error;
1017 fuse_put_request(fc, req);
1018 if (err == -ENOSYS) {
1019 fc->no_setxattr = 1;
1020 err = -EOPNOTSUPP;
1021 }
1022 return err;
1023}
1024
1025static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1026 void *value, size_t size)
1027{
1028 struct inode *inode = entry->d_inode;
1029 struct fuse_conn *fc = get_fuse_conn(inode);
1030 struct fuse_req *req;
1031 struct fuse_getxattr_in inarg;
1032 struct fuse_getxattr_out outarg;
1033 ssize_t ret;
1034
1035 if (fc->no_getxattr)
1036 return -EOPNOTSUPP;
1037
1038 req = fuse_get_request(fc);
1039 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -07001040 return -EINTR;
Miklos Szeredi92a87802005-09-09 13:10:31 -07001041
1042 memset(&inarg, 0, sizeof(inarg));
1043 inarg.size = size;
1044 req->in.h.opcode = FUSE_GETXATTR;
1045 req->in.h.nodeid = get_node_id(inode);
1046 req->inode = inode;
1047 req->in.numargs = 2;
1048 req->in.args[0].size = sizeof(inarg);
1049 req->in.args[0].value = &inarg;
1050 req->in.args[1].size = strlen(name) + 1;
1051 req->in.args[1].value = name;
1052 /* This is really two different operations rolled into one */
1053 req->out.numargs = 1;
1054 if (size) {
1055 req->out.argvar = 1;
1056 req->out.args[0].size = size;
1057 req->out.args[0].value = value;
1058 } else {
1059 req->out.args[0].size = sizeof(outarg);
1060 req->out.args[0].value = &outarg;
1061 }
1062 request_send(fc, req);
1063 ret = req->out.h.error;
1064 if (!ret)
1065 ret = size ? req->out.args[0].size : outarg.size;
1066 else {
1067 if (ret == -ENOSYS) {
1068 fc->no_getxattr = 1;
1069 ret = -EOPNOTSUPP;
1070 }
1071 }
1072 fuse_put_request(fc, req);
1073 return ret;
1074}
1075
1076static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1077{
1078 struct inode *inode = entry->d_inode;
1079 struct fuse_conn *fc = get_fuse_conn(inode);
1080 struct fuse_req *req;
1081 struct fuse_getxattr_in inarg;
1082 struct fuse_getxattr_out outarg;
1083 ssize_t ret;
1084
1085 if (fc->no_listxattr)
1086 return -EOPNOTSUPP;
1087
1088 req = fuse_get_request(fc);
1089 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -07001090 return -EINTR;
Miklos Szeredi92a87802005-09-09 13:10:31 -07001091
1092 memset(&inarg, 0, sizeof(inarg));
1093 inarg.size = size;
1094 req->in.h.opcode = FUSE_LISTXATTR;
1095 req->in.h.nodeid = get_node_id(inode);
1096 req->inode = inode;
1097 req->in.numargs = 1;
1098 req->in.args[0].size = sizeof(inarg);
1099 req->in.args[0].value = &inarg;
1100 /* This is really two different operations rolled into one */
1101 req->out.numargs = 1;
1102 if (size) {
1103 req->out.argvar = 1;
1104 req->out.args[0].size = size;
1105 req->out.args[0].value = list;
1106 } else {
1107 req->out.args[0].size = sizeof(outarg);
1108 req->out.args[0].value = &outarg;
1109 }
1110 request_send(fc, req);
1111 ret = req->out.h.error;
1112 if (!ret)
1113 ret = size ? req->out.args[0].size : outarg.size;
1114 else {
1115 if (ret == -ENOSYS) {
1116 fc->no_listxattr = 1;
1117 ret = -EOPNOTSUPP;
1118 }
1119 }
1120 fuse_put_request(fc, req);
1121 return ret;
1122}
1123
1124static int fuse_removexattr(struct dentry *entry, const char *name)
1125{
1126 struct inode *inode = entry->d_inode;
1127 struct fuse_conn *fc = get_fuse_conn(inode);
1128 struct fuse_req *req;
1129 int err;
1130
1131 if (fc->no_removexattr)
1132 return -EOPNOTSUPP;
1133
1134 req = fuse_get_request(fc);
1135 if (!req)
Miklos Szeredi7c352bd2005-09-09 13:10:39 -07001136 return -EINTR;
Miklos Szeredi92a87802005-09-09 13:10:31 -07001137
1138 req->in.h.opcode = FUSE_REMOVEXATTR;
1139 req->in.h.nodeid = get_node_id(inode);
1140 req->inode = inode;
1141 req->in.numargs = 1;
1142 req->in.args[0].size = strlen(name) + 1;
1143 req->in.args[0].value = name;
1144 request_send(fc, req);
1145 err = req->out.h.error;
1146 fuse_put_request(fc, req);
1147 if (err == -ENOSYS) {
1148 fc->no_removexattr = 1;
1149 err = -EOPNOTSUPP;
1150 }
1151 return err;
1152}
1153
Miklos Szeredie5e55582005-09-09 13:10:28 -07001154static struct inode_operations fuse_dir_inode_operations = {
1155 .lookup = fuse_lookup,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001156 .mkdir = fuse_mkdir,
1157 .symlink = fuse_symlink,
1158 .unlink = fuse_unlink,
1159 .rmdir = fuse_rmdir,
1160 .rename = fuse_rename,
1161 .link = fuse_link,
1162 .setattr = fuse_setattr,
1163 .create = fuse_create,
1164 .mknod = fuse_mknod,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001165 .permission = fuse_permission,
1166 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001167 .setxattr = fuse_setxattr,
1168 .getxattr = fuse_getxattr,
1169 .listxattr = fuse_listxattr,
1170 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001171};
1172
1173static struct file_operations fuse_dir_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001174 .llseek = generic_file_llseek,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001175 .read = generic_read_dir,
1176 .readdir = fuse_readdir,
1177 .open = fuse_dir_open,
1178 .release = fuse_dir_release,
Miklos Szeredi82547982005-09-09 13:10:38 -07001179 .fsync = fuse_dir_fsync,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001180};
1181
1182static struct inode_operations fuse_common_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001183 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001184 .permission = fuse_permission,
1185 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001186 .setxattr = fuse_setxattr,
1187 .getxattr = fuse_getxattr,
1188 .listxattr = fuse_listxattr,
1189 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001190};
1191
1192static struct inode_operations fuse_symlink_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001193 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001194 .follow_link = fuse_follow_link,
1195 .put_link = fuse_put_link,
1196 .readlink = generic_readlink,
1197 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001198 .setxattr = fuse_setxattr,
1199 .getxattr = fuse_getxattr,
1200 .listxattr = fuse_listxattr,
1201 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001202};
1203
1204void fuse_init_common(struct inode *inode)
1205{
1206 inode->i_op = &fuse_common_inode_operations;
1207}
1208
1209void fuse_init_dir(struct inode *inode)
1210{
1211 inode->i_op = &fuse_dir_inode_operations;
1212 inode->i_fop = &fuse_dir_operations;
1213}
1214
1215void fuse_init_symlink(struct inode *inode)
1216{
1217 inode->i_op = &fuse_symlink_inode_operations;
1218}