dir.c 50.5 KB
Newer Older
1
2
/*
  FUSE: Filesystem in Userspace
Miklos Szeredi's avatar
Miklos Szeredi committed
3
  Copyright (C) 2001-2008  Miklos Szeredi <miklos@szeredi.hu>
4
5
6
7
8
9
10
11
12
13
14

  This program can be distributed under the terms of the GNU GPL.
  See the file COPYING.
*/

#include "fuse_i.h"

#include <linux/pagemap.h>
#include <linux/file.h>
#include <linux/sched.h>
#include <linux/namei.h>
15
#include <linux/slab.h>
16

Al Viro's avatar
Al Viro committed
17
static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
18
19
20
21
22
23
{
	struct fuse_conn *fc = get_fuse_conn(dir);
	struct fuse_inode *fi = get_fuse_inode(dir);

	if (!fc->do_readdirplus)
		return false;
24
25
	if (!fc->readdirplus_auto)
		return true;
26
27
	if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state))
		return true;
Al Viro's avatar
Al Viro committed
28
	if (ctx->pos == 0)
29
30
31
32
33
34
35
36
37
38
39
		return true;
	return false;
}

static void fuse_advise_use_readdirplus(struct inode *dir)
{
	struct fuse_inode *fi = get_fuse_inode(dir);

	set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
}

Miklos Szeredi's avatar
Miklos Szeredi committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#if BITS_PER_LONG >= 64
static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
{
	entry->d_time = time;
}

static inline u64 fuse_dentry_time(struct dentry *entry)
{
	return entry->d_time;
}
#else
/*
 * On 32 bit archs store the high 32 bits of time in d_fsdata
 */
static void fuse_dentry_settime(struct dentry *entry, u64 time)
{
	entry->d_time = time;
	entry->d_fsdata = (void *) (unsigned long) (time >> 32);
}

static u64 fuse_dentry_time(struct dentry *entry)
{
	return (u64) entry->d_time +
		((u64) (unsigned long) entry->d_fsdata << 32);
}
#endif

67
68
69
70
71
72
73
74
75
/*
 * FUSE caches dentries and attributes with separate timeout.  The
 * time in jiffies until the dentry/attributes are valid is stored in
 * dentry->d_time and fuse_inode->i_time respectively.
 */

/*
 * Calculate the time in jiffies until a dentry/attributes are valid
 */
Miklos Szeredi's avatar
Miklos Szeredi committed
76
static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
77
{
78
79
	if (sec || nsec) {
		struct timespec ts = {sec, nsec};
Miklos Szeredi's avatar
Miklos Szeredi committed
80
		return get_jiffies_64() + timespec_to_jiffies(&ts);
81
	} else
Miklos Szeredi's avatar
Miklos Szeredi committed
82
		return 0;
83
84
}

85
86
87
88
/*
 * Set dentry and possibly attribute timeouts from the lookup/mk*
 * replies
 */
89
90
static void fuse_change_entry_timeout(struct dentry *entry,
				      struct fuse_entry_out *o)
91
{
Miklos Szeredi's avatar
Miklos Szeredi committed
92
93
	fuse_dentry_settime(entry,
		time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
94
95
96
97
98
99
100
101
102
103
}

static u64 attr_timeout(struct fuse_attr_out *o)
{
	return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
}

static u64 entry_attr_timeout(struct fuse_entry_out *o)
{
	return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
104
105
}

106
107
108
109
/*
 * Mark the attributes as stale, so that at the next call to
 * ->getattr() they will be fetched from userspace
 */
110
111
void fuse_invalidate_attr(struct inode *inode)
{
Miklos Szeredi's avatar
Miklos Szeredi committed
112
	get_fuse_inode(inode)->i_time = 0;
113
114
}

115
116
117
118
119
120
121
122
123
124
/**
 * Mark the attributes as stale due to an atime change.  Avoid the invalidate if
 * atime is not used.
 */
void fuse_invalidate_atime(struct inode *inode)
{
	if (!IS_RDONLY(inode))
		fuse_invalidate_attr(inode);
}

125
126
127
128
129
130
131
132
/*
 * Just mark the entry as stale, so that a next attempt to look it up
 * will result in a new lookup call to userspace
 *
 * This is called when a dentry is about to become negative and the
 * timeout is unknown (unlink, rmdir, rename and in some cases
 * lookup)
 */
Miklos Szeredi's avatar
Miklos Szeredi committed
133
void fuse_invalidate_entry_cache(struct dentry *entry)
134
{
Miklos Szeredi's avatar
Miklos Szeredi committed
135
	fuse_dentry_settime(entry, 0);
136
137
}

138
139
140
141
/*
 * Same as fuse_invalidate_entry_cache(), but also try to remove the
 * dentry from the hash
 */
142
143
144
145
static void fuse_invalidate_entry(struct dentry *entry)
{
	d_invalidate(entry);
	fuse_invalidate_entry_cache(entry);
146
147
}

148
149
static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_req *req,
			     u64 nodeid, struct qstr *name,
150
151
			     struct fuse_entry_out *outarg)
{
152
	memset(outarg, 0, sizeof(struct fuse_entry_out));
153
	req->in.h.opcode = FUSE_LOOKUP;
154
	req->in.h.nodeid = nodeid;
155
	req->in.numargs = 1;
156
157
	req->in.args[0].size = name->len + 1;
	req->in.args[0].value = name->name;
158
	req->out.numargs = 1;
159
160
161
162
	if (fc->minor < 9)
		req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
	else
		req->out.args[0].size = sizeof(struct fuse_entry_out);
163
164
165
	req->out.args[0].value = outarg;
}

166
u64 fuse_get_attr_version(struct fuse_conn *fc)
167
168
169
170
171
172
173
174
175
176
177
178
179
180
{
	u64 curr_version;

	/*
	 * The spin lock isn't actually needed on 64bit archs, but we
	 * don't yet care too much about such optimizations.
	 */
	spin_lock(&fc->lock);
	curr_version = fc->attr_version;
	spin_unlock(&fc->lock);

	return curr_version;
}

181
182
183
184
185
186
187
188
189
/*
 * Check whether the dentry is still valid
 *
 * If the entry validity timeout has expired and the dentry is
 * positive, try to redo the lookup.  If the lookup results in a
 * different inode, then let the VFS invalidate the dentry and redo
 * the lookup once more.  If the lookup results in the same inode,
 * then refresh the attributes, timeouts and mark the dentry valid.
 */
190
static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
191
{
192
	struct inode *inode;
193
194
	struct dentry *parent;
	struct fuse_conn *fc;
195
	struct fuse_inode *fi;
196
	int ret;
197

198
	inode = ACCESS_ONCE(entry->d_inode);
199
	if (inode && is_bad_inode(inode))
200
		goto invalid;
201
202
	else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
		 (flags & LOOKUP_REVAL)) {
203
204
		int err;
		struct fuse_entry_out outarg;
205
		struct fuse_req *req;
206
		struct fuse_forget_link *forget;
207
		u64 attr_version;
208

209
		/* For negative dentries, always do a fresh lookup */
210
		if (!inode)
211
			goto invalid;
212

213
		ret = -ECHILD;
214
		if (flags & LOOKUP_RCU)
215
			goto out;
216

217
		fc = get_fuse_conn(inode);
218
		req = fuse_get_req_nopages(fc);
219
		ret = PTR_ERR(req);
220
		if (IS_ERR(req))
221
			goto out;
222

223
224
		forget = fuse_alloc_forget();
		if (!forget) {
225
			fuse_put_request(fc, req);
226
227
			ret = -ENOMEM;
			goto out;
228
229
		}

230
		attr_version = fuse_get_attr_version(fc);
231

232
		parent = dget_parent(entry);
233
234
		fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
				 &entry->d_name, &outarg);
235
		fuse_request_send(fc, req);
236
		dput(parent);
237
		err = req->out.h.error;
238
		fuse_put_request(fc, req);
239
240
241
		/* Zero nodeid is same as -ENOENT */
		if (!err && !outarg.nodeid)
			err = -ENOENT;
242
		if (!err) {
243
			fi = get_fuse_inode(inode);
244
			if (outarg.nodeid != get_node_id(inode)) {
245
				fuse_queue_forget(fc, forget, outarg.nodeid, 1);
246
				goto invalid;
247
			}
248
			spin_lock(&fc->lock);
Miklos Szeredi's avatar
Miklos Szeredi committed
249
			fi->nlookup++;
250
			spin_unlock(&fc->lock);
251
		}
252
		kfree(forget);
253
		if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
254
			goto invalid;
255

256
257
258
259
		fuse_change_attributes(inode, &outarg.attr,
				       entry_attr_timeout(&outarg),
				       attr_version);
		fuse_change_entry_timeout(entry, &outarg);
260
	} else if (inode) {
261
262
263
264
265
		fi = get_fuse_inode(inode);
		if (flags & LOOKUP_RCU) {
			if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
				return -ECHILD;
		} else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
266
267
268
269
			parent = dget_parent(entry);
			fuse_advise_use_readdirplus(parent->d_inode);
			dput(parent);
		}
270
	}
271
272
273
274
275
276
	ret = 1;
out:
	return ret;

invalid:
	ret = 0;
277
278

	if (!(flags & LOOKUP_RCU) && check_submounts_and_drop(entry) != 0)
279
		ret = 1;
280
	goto out;
281
282
}

283
static int invalid_nodeid(u64 nodeid)
284
285
286
287
{
	return !nodeid || nodeid == FUSE_ROOT_ID;
}

Al Viro's avatar
Al Viro committed
288
const struct dentry_operations fuse_dentry_operations = {
289
290
291
	.d_revalidate	= fuse_dentry_revalidate,
};

292
int fuse_valid_type(int m)
293
294
295
296
297
{
	return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
		S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
}

298
299
int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
		     struct fuse_entry_out *outarg, struct inode **inode)
300
{
301
	struct fuse_conn *fc = get_fuse_conn_super(sb);
302
	struct fuse_req *req;
303
	struct fuse_forget_link *forget;
304
	u64 attr_version;
305
	int err;
306

307
308
309
310
	*inode = NULL;
	err = -ENAMETOOLONG;
	if (name->len > FUSE_NAME_MAX)
		goto out;
311

312
	req = fuse_get_req_nopages(fc);
313
	err = PTR_ERR(req);
314
	if (IS_ERR(req))
315
		goto out;
316

317
318
319
	forget = fuse_alloc_forget();
	err = -ENOMEM;
	if (!forget) {
320
		fuse_put_request(fc, req);
321
		goto out;
322
323
	}

324
	attr_version = fuse_get_attr_version(fc);
325

326
	fuse_lookup_init(fc, req, nodeid, name, outarg);
327
	fuse_request_send(fc, req);
328
	err = req->out.h.error;
329
	fuse_put_request(fc, req);
330
	/* Zero nodeid is same as -ENOENT, but with valid timeout */
331
332
333
334
335
336
337
338
339
340
341
342
343
344
	if (err || !outarg->nodeid)
		goto out_put_forget;

	err = -EIO;
	if (!outarg->nodeid)
		goto out_put_forget;
	if (!fuse_valid_type(outarg->attr.mode))
		goto out_put_forget;

	*inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
			   &outarg->attr, entry_attr_timeout(outarg),
			   attr_version);
	err = -ENOMEM;
	if (!*inode) {
345
		fuse_queue_forget(fc, forget, outarg->nodeid, 1);
346
		goto out;
347
	}
348
349
350
	err = 0;

 out_put_forget:
351
	kfree(forget);
352
353
354
355
356
 out:
	return err;
}

static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
Al Viro's avatar
Al Viro committed
357
				  unsigned int flags)
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
{
	int err;
	struct fuse_entry_out outarg;
	struct inode *inode;
	struct dentry *newent;
	bool outarg_valid = true;

	err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
			       &outarg, &inode);
	if (err == -ENOENT) {
		outarg_valid = false;
		err = 0;
	}
	if (err)
		goto out_err;

	err = -EIO;
	if (inode && get_node_id(inode) == FUSE_ROOT_ID)
		goto out_iput;
377

378
	newent = d_materialise_unique(entry, inode);
379
380
381
	err = PTR_ERR(newent);
	if (IS_ERR(newent))
		goto out_err;
382

383
	entry = newent ? newent : entry;
384
	if (outarg_valid)
385
		fuse_change_entry_timeout(entry, &outarg);
386
387
	else
		fuse_invalidate_entry_cache(entry);
388

389
	fuse_advise_use_readdirplus(dir);
390
	return newent;
391
392
393
394
395

 out_iput:
	iput(inode);
 out_err:
	return ERR_PTR(err);
396
397
}

398
399
400
401
402
403
/*
 * Atomic create+open operation
 *
 * If the filesystem doesn't support this, then fall back to separate
 * 'mknod' + 'open' requests.
 */
Al Viro's avatar
Al Viro committed
404
static int fuse_create_open(struct inode *dir, struct dentry *entry,
Al Viro's avatar
Al Viro committed
405
			    struct file *file, unsigned flags,
Al Viro's avatar
Al Viro committed
406
			    umode_t mode, int *opened)
407
408
409
410
411
{
	int err;
	struct inode *inode;
	struct fuse_conn *fc = get_fuse_conn(dir);
	struct fuse_req *req;
412
	struct fuse_forget_link *forget;
413
	struct fuse_create_in inarg;
414
415
416
417
	struct fuse_open_out outopen;
	struct fuse_entry_out outentry;
	struct fuse_file *ff;

418
419
420
	/* Userspace expects S_IFREG in create mode */
	BUG_ON((mode & S_IFMT) != S_IFREG);

421
	forget = fuse_alloc_forget();
422
	err = -ENOMEM;
423
	if (!forget)
424
		goto out_err;
425

426
	req = fuse_get_req_nopages(fc);
427
	err = PTR_ERR(req);
428
	if (IS_ERR(req))
429
		goto out_put_forget_req;
430

431
	err = -ENOMEM;
Tejun Heo's avatar
Tejun Heo committed
432
	ff = fuse_file_alloc(fc);
433
434
435
	if (!ff)
		goto out_put_request;

436
437
438
	if (!fc->dont_mask)
		mode &= ~current_umask();

439
440
	flags &= ~O_NOCTTY;
	memset(&inarg, 0, sizeof(inarg));
441
	memset(&outentry, 0, sizeof(outentry));
442
443
	inarg.flags = flags;
	inarg.mode = mode;
444
	inarg.umask = current_umask();
445
446
447
	req->in.h.opcode = FUSE_CREATE;
	req->in.h.nodeid = get_node_id(dir);
	req->in.numargs = 2;
448
449
	req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
						sizeof(inarg);
450
451
452
453
	req->in.args[0].value = &inarg;
	req->in.args[1].size = entry->d_name.len + 1;
	req->in.args[1].value = entry->d_name.name;
	req->out.numargs = 2;
454
455
456
457
	if (fc->minor < 9)
		req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
	else
		req->out.args[0].size = sizeof(outentry);
458
459
460
	req->out.args[0].value = &outentry;
	req->out.args[1].size = sizeof(outopen);
	req->out.args[1].value = &outopen;
461
	fuse_request_send(fc, req);
462
	err = req->out.h.error;
463
	if (err)
464
465
466
		goto out_free_ff;

	err = -EIO;
467
	if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
468
469
		goto out_free_ff;

470
	fuse_put_request(fc, req);
471
472
473
	ff->fh = outopen.fh;
	ff->nodeid = outentry.nodeid;
	ff->open_flags = outopen.open_flags;
474
	inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
475
			  &outentry.attr, entry_attr_timeout(&outentry), 0);
476
477
	if (!inode) {
		flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
478
		fuse_sync_release(ff, flags);
479
		fuse_queue_forget(fc, forget, outentry.nodeid, 1);
480
481
		err = -ENOMEM;
		goto out_err;
482
	}
483
	kfree(forget);
484
	d_instantiate(entry, inode);
485
	fuse_change_entry_timeout(entry, &outentry);
486
	fuse_invalidate_attr(dir);
Al Viro's avatar
Al Viro committed
487
488
	err = finish_open(file, entry, generic_file_open, opened);
	if (err) {
489
		fuse_sync_release(ff, flags);
490
491
492
	} else {
		file->private_data = fuse_file_get(ff);
		fuse_finish_open(inode, file);
493
	}
Al Viro's avatar
Al Viro committed
494
	return err;
495

496
out_free_ff:
497
	fuse_file_free(ff);
498
out_put_request:
499
	fuse_put_request(fc, req);
500
out_put_forget_req:
501
	kfree(forget);
502
out_err:
Al Viro's avatar
Al Viro committed
503
	return err;
504
505
506
}

static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
Al Viro's avatar
Al Viro committed
507
static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
Al Viro's avatar
Al Viro committed
508
			    struct file *file, unsigned flags,
Al Viro's avatar
Al Viro committed
509
			    umode_t mode, int *opened)
510
511
512
513
514
515
{
	int err;
	struct fuse_conn *fc = get_fuse_conn(dir);
	struct dentry *res = NULL;

	if (d_unhashed(entry)) {
Al Viro's avatar
Al Viro committed
516
		res = fuse_lookup(dir, entry, 0);
517
		if (IS_ERR(res))
Al Viro's avatar
Al Viro committed
518
			return PTR_ERR(res);
519
520
521
522
523
524
525
526
527

		if (res)
			entry = res;
	}

	if (!(flags & O_CREAT) || entry->d_inode)
		goto no_open;

	/* Only creates */
528
	*opened |= FILE_CREATED;
529
530
531
532

	if (fc->no_create)
		goto mknod;

Al Viro's avatar
Al Viro committed
533
	err = fuse_create_open(dir, entry, file, flags, mode, opened);
Al Viro's avatar
Al Viro committed
534
	if (err == -ENOSYS) {
535
536
537
538
539
		fc->no_create = 1;
		goto mknod;
	}
out_dput:
	dput(res);
Al Viro's avatar
Al Viro committed
540
	return err;
541
542
543

mknod:
	err = fuse_mknod(dir, entry, mode, 0);
Al Viro's avatar
Al Viro committed
544
	if (err)
545
546
		goto out_dput;
no_open:
Al Viro's avatar
Al Viro committed
547
	return finish_no_open(file, res);
548
549
}

550
551
552
/*
 * Code shared between mknod, mkdir, symlink and link
 */
553
554
static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
			    struct inode *dir, struct dentry *entry,
Al Viro's avatar
Al Viro committed
555
			    umode_t mode)
556
557
558
559
{
	struct fuse_entry_out outarg;
	struct inode *inode;
	int err;
560
	struct fuse_forget_link *forget;
561

562
563
	forget = fuse_alloc_forget();
	if (!forget) {
564
		fuse_put_request(fc, req);
565
		return -ENOMEM;
566
	}
567

568
	memset(&outarg, 0, sizeof(outarg));
569
570
	req->in.h.nodeid = get_node_id(dir);
	req->out.numargs = 1;
571
572
573
574
	if (fc->minor < 9)
		req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
	else
		req->out.args[0].size = sizeof(outarg);
575
	req->out.args[0].value = &outarg;
576
	fuse_request_send(fc, req);
577
	err = req->out.h.error;
578
579
580
581
	fuse_put_request(fc, req);
	if (err)
		goto out_put_forget_req;

582
583
	err = -EIO;
	if (invalid_nodeid(outarg.nodeid))
584
		goto out_put_forget_req;
585
586

	if ((outarg.attr.mode ^ mode) & S_IFMT)
587
		goto out_put_forget_req;
588

589
	inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
590
			  &outarg.attr, entry_attr_timeout(&outarg), 0);
591
	if (!inode) {
592
		fuse_queue_forget(fc, forget, outarg.nodeid, 1);
593
594
		return -ENOMEM;
	}
595
	kfree(forget);
596

597
598
599
	err = d_instantiate_no_diralias(entry, inode);
	if (err)
		return err;
600

601
	fuse_change_entry_timeout(entry, &outarg);
602
603
	fuse_invalidate_attr(dir);
	return 0;
604

605
 out_put_forget_req:
606
	kfree(forget);
607
	return err;
608
609
}

Al Viro's avatar
Al Viro committed
610
static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
611
612
613
614
		      dev_t rdev)
{
	struct fuse_mknod_in inarg;
	struct fuse_conn *fc = get_fuse_conn(dir);
615
	struct fuse_req *req = fuse_get_req_nopages(fc);
616
617
	if (IS_ERR(req))
		return PTR_ERR(req);
618

619
620
621
	if (!fc->dont_mask)
		mode &= ~current_umask();

622
623
624
	memset(&inarg, 0, sizeof(inarg));
	inarg.mode = mode;
	inarg.rdev = new_encode_dev(rdev);
625
	inarg.umask = current_umask();
626
627
	req->in.h.opcode = FUSE_MKNOD;
	req->in.numargs = 2;
628
629
	req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
						sizeof(inarg);
630
631
632
633
634
635
	req->in.args[0].value = &inarg;
	req->in.args[1].size = entry->d_name.len + 1;
	req->in.args[1].value = entry->d_name.name;
	return create_new_entry(fc, req, dir, entry, mode);
}

Al Viro's avatar
Al Viro committed
636
static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
Al Viro's avatar
Al Viro committed
637
		       bool excl)
638
639
640
641
{
	return fuse_mknod(dir, entry, mode, 0);
}

642
static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
643
644
645
{
	struct fuse_mkdir_in inarg;
	struct fuse_conn *fc = get_fuse_conn(dir);
646
	struct fuse_req *req = fuse_get_req_nopages(fc);
647
648
	if (IS_ERR(req))
		return PTR_ERR(req);
649

650
651
652
	if (!fc->dont_mask)
		mode &= ~current_umask();

653
654
	memset(&inarg, 0, sizeof(inarg));
	inarg.mode = mode;
655
	inarg.umask = current_umask();
656
657
658
659
660
661
662
663
664
665
666
667
668
669
	req->in.h.opcode = FUSE_MKDIR;
	req->in.numargs = 2;
	req->in.args[0].size = sizeof(inarg);
	req->in.args[0].value = &inarg;
	req->in.args[1].size = entry->d_name.len + 1;
	req->in.args[1].value = entry->d_name.name;
	return create_new_entry(fc, req, dir, entry, S_IFDIR);
}

static int fuse_symlink(struct inode *dir, struct dentry *entry,
			const char *link)
{
	struct fuse_conn *fc = get_fuse_conn(dir);
	unsigned len = strlen(link) + 1;
670
	struct fuse_req *req = fuse_get_req_nopages(fc);
671
672
	if (IS_ERR(req))
		return PTR_ERR(req);
673
674
675
676
677
678
679
680
681
682

	req->in.h.opcode = FUSE_SYMLINK;
	req->in.numargs = 2;
	req->in.args[0].size = entry->d_name.len + 1;
	req->in.args[0].value = entry->d_name.name;
	req->in.args[1].size = len;
	req->in.args[1].value = link;
	return create_new_entry(fc, req, dir, entry, S_IFLNK);
}

683
684
685
686
687
688
689
690
static inline void fuse_update_ctime(struct inode *inode)
{
	if (!IS_NOCMTIME(inode)) {
		inode->i_ctime = current_fs_time(inode->i_sb);
		mark_inode_dirty_sync(inode);
	}
}

691
692
693
694
static int fuse_unlink(struct inode *dir, struct dentry *entry)
{
	int err;
	struct fuse_conn *fc = get_fuse_conn(dir);
695
	struct fuse_req *req = fuse_get_req_nopages(fc);
696
697
	if (IS_ERR(req))
		return PTR_ERR(req);
698
699
700
701
702
703

	req->in.h.opcode = FUSE_UNLINK;
	req->in.h.nodeid = get_node_id(dir);
	req->in.numargs = 1;
	req->in.args[0].size = entry->d_name.len + 1;
	req->in.args[0].value = entry->d_name.name;
704
	fuse_request_send(fc, req);
705
706
707
708
	err = req->out.h.error;
	fuse_put_request(fc, req);
	if (!err) {
		struct inode *inode = entry->d_inode;
Miklos Szeredi's avatar
Miklos Szeredi committed
709
		struct fuse_inode *fi = get_fuse_inode(inode);
710

Miklos Szeredi's avatar
Miklos Szeredi committed
711
712
		spin_lock(&fc->lock);
		fi->attr_version = ++fc->attr_version;
713
714
715
716
717
718
719
720
		/*
		 * If i_nlink == 0 then unlink doesn't make sense, yet this can
		 * happen if userspace filesystem is careless.  It would be
		 * difficult to enforce correct nlink usage so just ignore this
		 * condition here
		 */
		if (inode->i_nlink > 0)
			drop_nlink(inode);
Miklos Szeredi's avatar
Miklos Szeredi committed
721
		spin_unlock(&fc->lock);
722
723
		fuse_invalidate_attr(inode);
		fuse_invalidate_attr(dir);
724
		fuse_invalidate_entry_cache(entry);
725
		fuse_update_ctime(inode);
726
727
728
729
730
731
732
733
734
	} else if (err == -EINTR)
		fuse_invalidate_entry(entry);
	return err;
}

static int fuse_rmdir(struct inode *dir, struct dentry *entry)
{
	int err;
	struct fuse_conn *fc = get_fuse_conn(dir);
735
	struct fuse_req *req = fuse_get_req_nopages(fc);
736
737
	if (IS_ERR(req))
		return PTR_ERR(req);
738
739
740
741
742
743

	req->in.h.opcode = FUSE_RMDIR;
	req->in.h.nodeid = get_node_id(dir);
	req->in.numargs = 1;
	req->in.args[0].size = entry->d_name.len + 1;
	req->in.args[0].value = entry->d_name.name;
744
	fuse_request_send(fc, req);
745
746
747
	err = req->out.h.error;
	fuse_put_request(fc, req);
	if (!err) {
748
		clear_nlink(entry->d_inode);
749
		fuse_invalidate_attr(dir);
750
		fuse_invalidate_entry_cache(entry);
751
752
753
754
755
	} else if (err == -EINTR)
		fuse_invalidate_entry(entry);
	return err;
}

Miklos Szeredi's avatar
Miklos Szeredi committed
756
757
758
static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
			      struct inode *newdir, struct dentry *newent,
			      unsigned int flags, int opcode, size_t argsize)
759
760
{
	int err;
Miklos Szeredi's avatar
Miklos Szeredi committed
761
	struct fuse_rename2_in inarg;
762
	struct fuse_conn *fc = get_fuse_conn(olddir);
Miklos Szeredi's avatar
Miklos Szeredi committed
763
	struct fuse_req *req;
764

Miklos Szeredi's avatar
Miklos Szeredi committed
765
	req = fuse_get_req_nopages(fc);
766
767
	if (IS_ERR(req))
		return PTR_ERR(req);
768

Miklos Szeredi's avatar
Miklos Szeredi committed
769
	memset(&inarg, 0, argsize);
770
	inarg.newdir = get_node_id(newdir);
Miklos Szeredi's avatar
Miklos Szeredi committed
771
772
	inarg.flags = flags;
	req->in.h.opcode = opcode;
773
774
	req->in.h.nodeid = get_node_id(olddir);
	req->in.numargs = 3;
Miklos Szeredi's avatar
Miklos Szeredi committed
775
	req->in.args[0].size = argsize;
776
777
778
779
780
	req->in.args[0].value = &inarg;
	req->in.args[1].size = oldent->d_name.len + 1;
	req->in.args[1].value = oldent->d_name.name;
	req->in.args[2].size = newent->d_name.len + 1;
	req->in.args[2].value = newent->d_name.name;
781
	fuse_request_send(fc, req);
782
783
784
	err = req->out.h.error;
	fuse_put_request(fc, req);
	if (!err) {
785
786
		/* ctime changes */
		fuse_invalidate_attr(oldent->d_inode);
787
		fuse_update_ctime(oldent->d_inode);
788

Miklos Szeredi's avatar
Miklos Szeredi committed
789
790
791
792
793
		if (flags & RENAME_EXCHANGE) {
			fuse_invalidate_attr(newent->d_inode);
			fuse_update_ctime(newent->d_inode);
		}

794
795
796
		fuse_invalidate_attr(olddir);
		if (olddir != newdir)
			fuse_invalidate_attr(newdir);
797
798

		/* newent will end up negative */
Miklos Szeredi's avatar
Miklos Szeredi committed
799
		if (!(flags & RENAME_EXCHANGE) && newent->d_inode) {
800
			fuse_invalidate_attr(newent->d_inode);
801
			fuse_invalidate_entry_cache(newent);
802
			fuse_update_ctime(newent->d_inode);
803
		}
804
805
806
807
808
809
810
811
812
813
814
815
816
817
	} else if (err == -EINTR) {
		/* If request was interrupted, DEITY only knows if the
		   rename actually took place.  If the invalidation
		   fails (e.g. some process has CWD under the renamed
		   directory), then there can be inconsistency between
		   the dcache and the real filesystem.  Tough luck. */
		fuse_invalidate_entry(oldent);
		if (newent->d_inode)
			fuse_invalidate_entry(newent);
	}

	return err;
}

Miklos Szeredi's avatar
Miklos Szeredi committed
818
819
820
821
822
823
824
825
826
827
static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
			struct inode *newdir, struct dentry *newent,
			unsigned int flags)
{
	struct fuse_conn *fc = get_fuse_conn(olddir);
	int err;

	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
		return -EINVAL;

Miklos Szeredi's avatar
Miklos Szeredi committed
828
829
830
	if (flags) {
		if (fc->no_rename2 || fc->minor < 23)
			return -EINVAL;
Miklos Szeredi's avatar
Miklos Szeredi committed
831

Miklos Szeredi's avatar
Miklos Szeredi committed
832
833
834
835
836
837
838
839
840
841
842
		err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
					 FUSE_RENAME2,
					 sizeof(struct fuse_rename2_in));
		if (err == -ENOSYS) {
			fc->no_rename2 = 1;
			err = -EINVAL;
		}
	} else {
		err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
					 FUSE_RENAME,
					 sizeof(struct fuse_rename_in));
Miklos Szeredi's avatar
Miklos Szeredi committed
843
	}
Miklos Szeredi's avatar
Miklos Szeredi committed
844

Miklos Szeredi's avatar
Miklos Szeredi committed
845
	return err;
Miklos Szeredi's avatar
Miklos Szeredi committed
846
}
Miklos Szeredi's avatar
Miklos Szeredi committed
847

848
849
850
851
852
853
854
static int fuse_link(struct dentry *entry, struct inode *newdir,
		     struct dentry *newent)
{
	int err;
	struct fuse_link_in inarg;
	struct inode *inode = entry->d_inode;
	struct fuse_conn *fc = get_fuse_conn(inode);
855
	struct fuse_req *req = fuse_get_req_nopages(fc);
856
857
	if (IS_ERR(req))
		return PTR_ERR(req);
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873

	memset(&inarg, 0, sizeof(inarg));
	inarg.oldnodeid = get_node_id(inode);
	req->in.h.opcode = FUSE_LINK;
	req->in.numargs = 2;
	req->in.args[0].size = sizeof(inarg);
	req->in.args[0].value = &inarg;
	req->in.args[1].size = newent->d_name.len + 1;
	req->in.args[1].value = newent->d_name.name;
	err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
	/* Contrary to "normal" filesystems it can happen that link
	   makes two "logical" inodes point to the same "physical"
	   inode.  We invalidate the attributes of the old one, so it
	   will reflect changes in the backing inode (link count,
	   etc.)
	*/
Miklos Szeredi's avatar
Miklos Szeredi committed
874
875
876
877
878
879
880
	if (!err) {
		struct fuse_inode *fi = get_fuse_inode(inode);

		spin_lock(&fc->lock);
		fi->attr_version = ++fc->attr_version;
		inc_nlink(inode);
		spin_unlock(&fc->lock);
881
		fuse_invalidate_attr(inode);
882
		fuse_update_ctime(inode);
Miklos Szeredi's avatar
Miklos Szeredi committed
883
884
885
	} else if (err == -EINTR) {
		fuse_invalidate_attr(inode);
	}
886
887
888
	return err;
}

889
890
891
static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
			  struct kstat *stat)
{
Miklos Szeredi's avatar
Miklos Szeredi committed
892
	unsigned int blkbits;
893
894
895
	struct fuse_conn *fc = get_fuse_conn(inode);

	/* see the comment in fuse_change_attributes() */
896
	if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
897
		attr->size = i_size_read(inode);
898
899
		attr->mtime = inode->i_mtime.tv_sec;
		attr->mtimensec = inode->i_mtime.tv_nsec;
900
901
		attr->ctime = inode->i_ctime.tv_sec;
		attr->ctimensec = inode->i_ctime.tv_nsec;
902
	}
Miklos Szeredi's avatar
Miklos Szeredi committed
903

904
905
906
907
	stat->dev = inode->i_sb->s_dev;
	stat->ino = attr->ino;
	stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
	stat->nlink = attr->nlink;
908
909
	stat->uid = make_kuid(&init_user_ns, attr->uid);
	stat->gid = make_kgid(&init_user_ns, attr->gid);
910
911
912
913
914
915
916
917
918
	stat->rdev = inode->i_rdev;
	stat->atime.tv_sec = attr->atime;
	stat->atime.tv_nsec = attr->atimensec;
	stat->mtime.tv_sec = attr->mtime;
	stat->mtime.tv_nsec = attr->mtimensec;
	stat->ctime.tv_sec = attr->ctime;
	stat->ctime.tv_nsec = attr->ctimensec;
	stat->size = attr->size;
	stat->blocks = attr->blocks;
Miklos Szeredi's avatar
Miklos Szeredi committed
919
920
921
922
923
924
925

	if (attr->blksize != 0)
		blkbits = ilog2(attr->blksize);
	else
		blkbits = inode->i_sb->s_blocksize_bits;

	stat->blksize = 1 << blkbits;
926
927
}

928
929
static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
			   struct file *file)
930
931
{
	int err;
932
933
	struct fuse_getattr_in inarg;
	struct fuse_attr_out outarg;
934
	struct fuse_conn *fc = get_fuse_conn(inode);
935
936
937
	struct fuse_req *req;
	u64 attr_version;

938
	req = fuse_get_req_nopages(fc);
939
940
	if (IS_ERR(req))
		return PTR_ERR(req);
941

942
	attr_version = fuse_get_attr_version(fc);
943

944
	memset(&inarg, 0, sizeof(inarg));
945
	memset(&outarg, 0, sizeof(outarg));
946
947
948
949
950
951
952
	/* Directories have separate file-handle space */
	if (file && S_ISREG(inode->i_mode)) {
		struct fuse_file *ff = file->private_data;

		inarg.getattr_flags |= FUSE_GETATTR_FH;
		inarg.fh = ff->fh;
	}
953
954
	req->in.h.opcode = FUSE_GETATTR;
	req->in.h.nodeid = get_node_id(inode);
955
956
957
	req->in.numargs = 1;
	req->in.args[0].size = sizeof(inarg);
	req->in.args[0].value = &inarg;
958
	req->out.numargs = 1;
959
960
961
962
	if (fc->minor < 9)
		req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
	else
		req->out.args[0].size = sizeof(outarg);
963
	req->out.args[0].value = &outarg;
964
	fuse_request_send(fc, req);
965
966
967
	err = req->out.h.error;
	fuse_put_request(fc, req);
	if (!err) {
968
		if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
969
970
971
			make_bad_inode(inode);
			err = -EIO;
		} else {
972
973
			fuse_change_attributes(inode, &outarg.attr,
					       attr_timeout(&outarg),
974
975
					       attr_version);
			if (stat)
976
				fuse_fillattr(inode, &outarg.attr, stat);
977
978
979
980
981
		}
	}
	return err;
}

Miklos Szeredi's avatar
Miklos Szeredi committed
982
983
984
985
986
987
988
int fuse_update_attributes(struct inode *inode, struct kstat *stat,
			   struct file *file, bool *refreshed)
{
	struct fuse_inode *fi = get_fuse_inode(inode);
	int err;
	bool r;

Miklos Szeredi's avatar
Miklos Szeredi committed
989
	if (time_before64(fi->i_time, get_jiffies_64())) {
Miklos Szeredi's avatar
Miklos Szeredi committed
990
991
992
993
994
995
996
997
		r = true;
		err = fuse_do_getattr(inode, stat, file);
	} else {
		r = false;
		err = 0;
		if (stat) {
			generic_fillattr(inode, stat);
			stat->mode = fi->orig_i_mode;
998
			stat->ino = fi->orig_ino;
Miklos Szeredi's avatar
Miklos Szeredi committed