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