vfsub.c 18.5 KB
Newer Older
1
2
// SPDX-License-Identifier: GPL-2.0
/*
3
 * Copyright (C) 2005-2020 Junjiro R. Okajima
4
5
6
7
8
9
 */

/*
 * sub-routines for VFS
 */

10
#include <linux/mnt_namespace.h>
11
#include <linux/namei.h>
12
#include <linux/nsproxy.h>
J. R. Okajima's avatar
J. R. Okajima committed
13
#include <linux/security.h>
J. R. Okajima's avatar
J. R. Okajima committed
14
#include <linux/splice.h>
15
16
#include "aufs.h"

17
18
19
20
21
22
23
24
25
26
#ifdef CONFIG_AUFS_BR_FUSE
int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
{
	if (!au_test_fuse(h_sb) || !au_userns)
		return 0;

	return is_current_mnt_ns(mnt) ? 0 : -EACCES;
}
#endif

J. R. Okajima's avatar
J. R. Okajima committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
{
	int err;

	lockdep_off();
	down_read(&h_sb->s_umount);
	err = __sync_filesystem(h_sb, wait);
	up_read(&h_sb->s_umount);
	lockdep_on();

	return err;
}

/* ---------------------------------------------------------------------- */

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
int vfsub_update_h_iattr(struct path *h_path, int *did)
{
	int err;
	struct kstat st;
	struct super_block *h_sb;

	/* for remote fs, leave work for its getattr or d_revalidate */
	/* for bad i_attr fs, handle them in aufs_getattr() */
	/* still some fs may acquire i_mutex. we need to skip them */
	err = 0;
	if (!did)
		did = &err;
	h_sb = h_path->dentry->d_sb;
	*did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
	if (*did)
		err = vfsub_getattr(h_path, &st);

	return err;
}

/* ---------------------------------------------------------------------- */

J. R. Okajima's avatar
J. R. Okajima committed
64
65
struct file *vfsub_dentry_open(struct path *path, int flags)
{
66
	return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
J. R. Okajima's avatar
J. R. Okajima committed
67
68
69
70
71
72
73
74
75
76
77
78
			   current_cred());
}

struct file *vfsub_filp_open(const char *path, int oflags, int mode)
{
	struct file *file;

	lockdep_off();
	file = filp_open(path,
			 oflags /* | __FMODE_NONOTIFY */,
			 mode);
	lockdep_on();
79
80
81
	if (IS_ERR(file))
		goto out;
	vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
J. R. Okajima's avatar
J. R. Okajima committed
82

83
out:
J. R. Okajima's avatar
J. R. Okajima committed
84
85
86
	return file;
}

87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Ideally this function should call VFS:do_last() in order to keep all its
 * checkings. But it is very hard for aufs to regenerate several VFS internal
 * structure such as nameidata. This is a second (or third) best approach.
 * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
 */
int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
		      struct vfsub_aopen_args *args)
{
	int err;
	struct au_branch *br = args->br;
	struct file *file = args->file;
	/* copied from linux/fs/namei.c:atomic_open() */
	struct dentry *const DENTRY_NOT_SET = (void *)-1UL;

	IMustLock(dir);
	AuDebugOn(!dir->i_op->atomic_open);

	err = au_br_test_oflag(args->open_flag, br);
	if (unlikely(err))
		goto out;

	au_lcnt_inc(&br->br_nfiles);
	file->f_path.dentry = DENTRY_NOT_SET;
	file->f_path.mnt = au_br_mnt(br);
	AuDbg("%ps\n", dir->i_op->atomic_open);
	err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
				     args->create_mode);
	if (unlikely(err < 0)) {
		au_lcnt_dec(&br->br_nfiles);
		goto out;
	}

	/* temporary workaround for nfsv4 branch */
	if (au_test_nfs(dir->i_sb))
		nfs_mark_for_revalidate(dir);

	if (file->f_mode & FMODE_CREATED)
		fsnotify_create(dir, dentry);
	if (!(file->f_mode & FMODE_OPENED)) {
		au_lcnt_dec(&br->br_nfiles);
		goto out;
	}

	/* todo: call VFS:may_open() here */
	/* todo: ima_file_check() too? */
	if (!err && (args->open_flag & __FMODE_EXEC))
		err = deny_write_access(file);
	if (!err)
		fsnotify_open(file);
	else
		au_lcnt_dec(&br->br_nfiles);
	/* note that the file is created and still opened */

out:
	return err;
}

145
146
147
148
149
int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
{
	int err;

	err = kern_path(name, flags, path);
150
151
	if (!err && d_is_positive(path->dentry))
		vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
152
153
	return err;
}
J. R. Okajima's avatar
J. R. Okajima committed
154

J. R. Okajima's avatar
J. R. Okajima committed
155
156
157
158
159
160
161
162
struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
					     struct dentry *parent, int len)
{
	struct path path = {
		.mnt = NULL
	};

	path.dentry = lookup_one_len_unlocked(name, parent, len);
163
164
165
166
167
168
	if (IS_ERR(path.dentry))
		goto out;
	if (d_is_positive(path.dentry))
		vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/

out:
J. R. Okajima's avatar
J. R. Okajima committed
169
170
171
172
	AuTraceErrPtr(path.dentry);
	return path.dentry;
}

J. R. Okajima's avatar
J. R. Okajima committed
173
174
175
176
177
178
179
180
181
182
183
184
185
struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
				    int len)
{
	struct path path = {
		.mnt = NULL
	};

	/* VFS checks it too, but by WARN_ON_ONCE() */
	IMustLock(d_inode(parent));

	path.dentry = lookup_one_len(name, parent, len);
	if (IS_ERR(path.dentry))
		goto out;
186
187
	if (d_is_positive(path.dentry))
		vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
J. R. Okajima's avatar
J. R. Okajima committed
188
189
190
191
192
193

out:
	AuTraceErrPtr(path.dentry);
	return path.dentry;
}

194
195
196
197
198
199
void vfsub_call_lkup_one(void *args)
{
	struct vfsub_lkup_one_args *a = args;
	*a->errp = vfsub_lkup_one(a->name, a->parent);
}

J. R. Okajima's avatar
J. R. Okajima committed
200
201
/* ---------------------------------------------------------------------- */

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
				 struct dentry *d2, struct au_hinode *hdir2)
{
	struct dentry *d;

	lockdep_off();
	d = lock_rename(d1, d2);
	lockdep_on();
	au_hn_suspend(hdir1);
	if (hdir1 != hdir2)
		au_hn_suspend(hdir2);

	return d;
}

void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
			 struct dentry *d2, struct au_hinode *hdir2)
{
	au_hn_resume(hdir1);
	if (hdir1 != hdir2)
		au_hn_resume(hdir2);
	lockdep_off();
	unlock_rename(d1, d2);
	lockdep_on();
}

/* ---------------------------------------------------------------------- */

230
231
232
233
int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
{
	int err;
	struct dentry *d;
234
	struct user_namespace *userns;
235
236
237
238
239
240
241
242
243

	IMustLock(dir);

	d = path->dentry;
	path->dentry = d->d_parent;
	err = security_path_mknod(path, d, mode, 0);
	path->dentry = d;
	if (unlikely(err))
		goto out;
244
	userns = mnt_user_ns(path->mnt);
245
246

	lockdep_off();
247
	err = vfs_create(userns, dir, path->dentry, mode, want_excl);
248
	lockdep_on();
249
250
251
252
253
254
255
256
257
258
259
	if (!err) {
		struct path tmp = *path;
		int did;

		vfsub_update_h_iattr(&tmp, &did);
		if (did) {
			tmp.dentry = path->dentry->d_parent;
			vfsub_update_h_iattr(&tmp, /*did*/NULL);
		}
		/*ignore*/
	}
260
261
262
263
264

out:
	return err;
}

J. R. Okajima's avatar
J. R. Okajima committed
265
266
267
268
int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
{
	int err;
	struct dentry *d;
269
	struct user_namespace *userns;
J. R. Okajima's avatar
J. R. Okajima committed
270
271
272
273
274
275
276
277
278

	IMustLock(dir);

	d = path->dentry;
	path->dentry = d->d_parent;
	err = security_path_symlink(path, d, symname);
	path->dentry = d;
	if (unlikely(err))
		goto out;
279
	userns = mnt_user_ns(path->mnt);
J. R. Okajima's avatar
J. R. Okajima committed
280
281

	lockdep_off();
282
	err = vfs_symlink(userns, dir, path->dentry, symname);
J. R. Okajima's avatar
J. R. Okajima committed
283
	lockdep_on();
284
285
286
287
288
289
290
291
292
293
294
	if (!err) {
		struct path tmp = *path;
		int did;

		vfsub_update_h_iattr(&tmp, &did);
		if (did) {
			tmp.dentry = path->dentry->d_parent;
			vfsub_update_h_iattr(&tmp, /*did*/NULL);
		}
		/*ignore*/
	}
J. R. Okajima's avatar
J. R. Okajima committed
295
296
297
298
299
300
301
302
303

out:
	return err;
}

int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
{
	int err;
	struct dentry *d;
304
	struct user_namespace *userns;
J. R. Okajima's avatar
J. R. Okajima committed
305
306
307
308
309
310
311
312
313

	IMustLock(dir);

	d = path->dentry;
	path->dentry = d->d_parent;
	err = security_path_mknod(path, d, mode, new_encode_dev(dev));
	path->dentry = d;
	if (unlikely(err))
		goto out;
314
	userns = mnt_user_ns(path->mnt);
J. R. Okajima's avatar
J. R. Okajima committed
315
316

	lockdep_off();
317
	err = vfs_mknod(userns, dir, path->dentry, mode, dev);
J. R. Okajima's avatar
J. R. Okajima committed
318
	lockdep_on();
319
320
321
322
323
324
325
326
327
328
329
	if (!err) {
		struct path tmp = *path;
		int did;

		vfsub_update_h_iattr(&tmp, &did);
		if (did) {
			tmp.dentry = path->dentry->d_parent;
			vfsub_update_h_iattr(&tmp, /*did*/NULL);
		}
		/*ignore*/
	}
J. R. Okajima's avatar
J. R. Okajima committed
330
331
332
333
334

out:
	return err;
}

335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
static int au_test_nlink(struct inode *inode)
{
	const unsigned int link_max = UINT_MAX >> 1; /* rough margin */

	if (!au_test_fs_no_limit_nlink(inode->i_sb)
	    || inode->i_nlink < link_max)
		return 0;
	return -EMLINK;
}

int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
	       struct inode **delegated_inode)
{
	int err;
	struct dentry *d;
350
	struct user_namespace *userns;
351
352
353
354
355
356
357
358
359
360
361
362
363
364

	IMustLock(dir);

	err = au_test_nlink(d_inode(src_dentry));
	if (unlikely(err))
		return err;

	/* we don't call may_linkat() */
	d = path->dentry;
	path->dentry = d->d_parent;
	err = security_path_link(src_dentry, path, d);
	path->dentry = d;
	if (unlikely(err))
		goto out;
365
	userns = mnt_user_ns(path->mnt);
366
367

	lockdep_off();
368
	err = vfs_link(src_dentry, userns, dir, path->dentry, delegated_inode);
369
	lockdep_on();
370
371
372
373
374
375
376
377
378
379
380
381
382
383
	if (!err) {
		struct path tmp = *path;
		int did;

		/* fuse has different memory inode for the same inumber */
		vfsub_update_h_iattr(&tmp, &did);
		if (did) {
			tmp.dentry = path->dentry->d_parent;
			vfsub_update_h_iattr(&tmp, /*did*/NULL);
			tmp.dentry = src_dentry;
			vfsub_update_h_iattr(&tmp, /*did*/NULL);
		}
		/*ignore*/
	}
384
385
386
387
388

out:
	return err;
}

J. R. Okajima's avatar
J. R. Okajima committed
389
390
391
392
393
int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
		 struct inode *dir, struct path *path,
		 struct inode **delegated_inode, unsigned int flags)
{
	int err;
394
	struct renamedata rd;
J. R. Okajima's avatar
J. R. Okajima committed
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
	struct path tmp = {
		.mnt	= path->mnt
	};
	struct dentry *d;

	IMustLock(dir);
	IMustLock(src_dir);

	d = path->dentry;
	path->dentry = d->d_parent;
	tmp.dentry = src_dentry->d_parent;
	err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
	path->dentry = d;
	if (unlikely(err))
		goto out;

411
412
413
414
415
416
417
418
	rd.old_mnt_userns = mnt_user_ns(path->mnt);
	rd.old_dir = src_dir;
	rd.old_dentry = src_dentry;
	rd.new_mnt_userns = rd.old_mnt_userns;
	rd.new_dir = dir;
	rd.new_dentry = path->dentry;
	rd.delegated_inode = delegated_inode;
	rd.flags = flags;
J. R. Okajima's avatar
J. R. Okajima committed
419
	lockdep_off();
420
	err = vfs_rename(&rd);
J. R. Okajima's avatar
J. R. Okajima committed
421
	lockdep_on();
422
423
424
425
426
427
428
429
430
431
432
433
434
	if (!err) {
		int did;

		tmp.dentry = d->d_parent;
		vfsub_update_h_iattr(&tmp, &did);
		if (did) {
			tmp.dentry = src_dentry;
			vfsub_update_h_iattr(&tmp, /*did*/NULL);
			tmp.dentry = src_dentry->d_parent;
			vfsub_update_h_iattr(&tmp, /*did*/NULL);
		}
		/*ignore*/
	}
J. R. Okajima's avatar
J. R. Okajima committed
435
436
437
438
439

out:
	return err;
}

440
441
442
443
int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
{
	int err;
	struct dentry *d;
444
	struct user_namespace *userns;
445
446
447
448
449
450
451
452
453

	IMustLock(dir);

	d = path->dentry;
	path->dentry = d->d_parent;
	err = security_path_mkdir(path, d, mode);
	path->dentry = d;
	if (unlikely(err))
		goto out;
454
	userns = mnt_user_ns(path->mnt);
455
456

	lockdep_off();
457
	err = vfs_mkdir(userns, dir, path->dentry, mode);
458
	lockdep_on();
459
460
461
462
463
464
465
466
467
468
469
	if (!err) {
		struct path tmp = *path;
		int did;

		vfsub_update_h_iattr(&tmp, &did);
		if (did) {
			tmp.dentry = path->dentry->d_parent;
			vfsub_update_h_iattr(&tmp, /*did*/NULL);
		}
		/*ignore*/
	}
470
471
472
473
474
475
476
477
478

out:
	return err;
}

int vfsub_rmdir(struct inode *dir, struct path *path)
{
	int err;
	struct dentry *d;
479
	struct user_namespace *userns;
480
481
482
483
484
485
486
487
488

	IMustLock(dir);

	d = path->dentry;
	path->dentry = d->d_parent;
	err = security_path_rmdir(path, d);
	path->dentry = d;
	if (unlikely(err))
		goto out;
489
	userns = mnt_user_ns(path->mnt);
490
491

	lockdep_off();
492
	err = vfs_rmdir(userns, dir, path->dentry);
493
	lockdep_on();
494
495
496
497
498
499
500
501
	if (!err) {
		struct path tmp = {
			.dentry	= path->dentry->d_parent,
			.mnt	= path->mnt
		};

		vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
	}
502
503
504
505
506

out:
	return err;
}

507
508
/* ---------------------------------------------------------------------- */

509
510
511
512
513
514
515
516
517
/* todo: support mmap_sem? */
ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
		     loff_t *ppos)
{
	ssize_t err;

	lockdep_off();
	err = vfs_read(file, ubuf, count, ppos);
	lockdep_on();
518
519
	if (err >= 0)
		vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
520
521
522
523
524
525
526
	return err;
}

ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
		     loff_t *ppos)
{
	ssize_t err;
527
528
529
530
531
532
533

	lockdep_off();
	err = kernel_read(file, kbuf, count, ppos);
	lockdep_on();
	AuTraceErr(err);
	if (err >= 0)
		vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
534
535
536
537
538
539
540
541
542
543
544
	return err;
}

ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
		      loff_t *ppos)
{
	ssize_t err;

	lockdep_off();
	err = vfs_write(file, ubuf, count, ppos);
	lockdep_on();
545
546
	if (err >= 0)
		vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
547
548
549
550
551
552
	return err;
}

ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
{
	ssize_t err;
553
554
555
556
557
558

	lockdep_off();
	err = kernel_write(file, kbuf, count, ppos);
	lockdep_on();
	if (err >= 0)
		vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
559
560
561
	return err;
}

J. R. Okajima's avatar
J. R. Okajima committed
562
563
564
565
566
567
568
569
570
571
572
573
574
int vfsub_flush(struct file *file, fl_owner_t id)
{
	int err;

	err = 0;
	if (file->f_op->flush) {
		if (!au_test_nfs(file->f_path.dentry->d_sb))
			err = file->f_op->flush(file, id);
		else {
			lockdep_off();
			err = file->f_op->flush(file, id);
			lockdep_on();
		}
575
576
577
		if (!err)
			vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
		/*ignore*/
J. R. Okajima's avatar
J. R. Okajima committed
578
579
580
581
	}
	return err;
}

J. R. Okajima's avatar
J. R. Okajima committed
582
583
584
585
586
587
588
589
590
int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
{
	int err;

	AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);

	lockdep_off();
	err = iterate_dir(file, ctx);
	lockdep_on();
591
592
	if (err >= 0)
		vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
J. R. Okajima's avatar
J. R. Okajima committed
593
594
595
596

	return err;
}

J. R. Okajima's avatar
J. R. Okajima committed
597
598
599
600
601
602
603
604
605
606
long vfsub_splice_to(struct file *in, loff_t *ppos,
		     struct pipe_inode_info *pipe, size_t len,
		     unsigned int flags)
{
	long err;

	lockdep_off();
	err = do_splice_to(in, ppos, pipe, len, flags);
	lockdep_on();
	file_accessed(in);
607
608
	if (err >= 0)
		vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
J. R. Okajima's avatar
J. R. Okajima committed
609
610
611
612
613
614
615
616
617
618
619
	return err;
}

long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
		       loff_t *ppos, size_t len, unsigned int flags)
{
	long err;

	lockdep_off();
	err = do_splice_from(pipe, out, ppos, len, flags);
	lockdep_on();
620
621
	if (err >= 0)
		vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
J. R. Okajima's avatar
J. R. Okajima committed
622
623
624
625
626
627
628
629
630
631
632
	return err;
}

int vfsub_fsync(struct file *file, struct path *path, int datasync)
{
	int err;

	/* file can be NULL */
	lockdep_off();
	err = vfs_fsync(file, datasync);
	lockdep_on();
633
634
635
636
637
638
639
	if (!err) {
		if (!path) {
			AuDebugOn(!file);
			path = &file->f_path;
		}
		vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
	}
J. R. Okajima's avatar
J. R. Okajima committed
640
641
642
643
644
645
646
647
648
649
	return err;
}

/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
		struct file *h_file)
{
	int err;
	struct inode *h_inode;
	struct super_block *h_sb;
650
	struct user_namespace *h_userns;
J. R. Okajima's avatar
J. R. Okajima committed
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665

	if (!h_file) {
		err = vfsub_truncate(h_path, length);
		goto out;
	}

	h_inode = d_inode(h_path->dentry);
	h_sb = h_inode->i_sb;
	lockdep_off();
	sb_start_write(h_sb);
	lockdep_on();
	err = locks_verify_truncate(h_inode, h_file, length);
	if (!err)
		err = security_path_truncate(h_path);
	if (!err) {
666
		h_userns = mnt_user_ns(h_path->mnt);
J. R. Okajima's avatar
J. R. Okajima committed
667
		lockdep_off();
668
669
		err = do_truncate(h_userns, h_path->dentry, length, attr,
				  h_file);
J. R. Okajima's avatar
J. R. Okajima committed
670
671
672
673
674
675
676
677
678
679
		lockdep_on();
	}
	lockdep_off();
	sb_end_write(h_sb);
	lockdep_on();

out:
	return err;
}

680
681
/* ---------------------------------------------------------------------- */

682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
struct au_vfsub_mkdir_args {
	int *errp;
	struct inode *dir;
	struct path *path;
	int mode;
};

static void au_call_vfsub_mkdir(void *args)
{
	struct au_vfsub_mkdir_args *a = args;
	*a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
}

int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
{
	int err, do_sio, wkq_err;
698
	struct user_namespace *userns;
699

700
701
	userns = mnt_user_ns(path->mnt);
	do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
	if (!do_sio) {
		lockdep_off();
		err = vfsub_mkdir(dir, path, mode);
		lockdep_on();
	} else {
		struct au_vfsub_mkdir_args args = {
			.errp	= &err,
			.dir	= dir,
			.path	= path,
			.mode	= mode
		};
		wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
		if (unlikely(wkq_err))
			err = wkq_err;
	}

	return err;
}

struct au_vfsub_rmdir_args {
	int *errp;
	struct inode *dir;
	struct path *path;
};

static void au_call_vfsub_rmdir(void *args)
{
	struct au_vfsub_rmdir_args *a = args;
	*a->errp = vfsub_rmdir(a->dir, a->path);
}

int vfsub_sio_rmdir(struct inode *dir, struct path *path)
{
	int err, do_sio, wkq_err;
736
	struct user_namespace *userns;
737

738
739
	userns = mnt_user_ns(path->mnt);
	do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
	if (!do_sio) {
		lockdep_off();
		err = vfsub_rmdir(dir, path);
		lockdep_on();
	} else {
		struct au_vfsub_rmdir_args args = {
			.errp	= &err,
			.dir	= dir,
			.path	= path
		};
		wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
		if (unlikely(wkq_err))
			err = wkq_err;
	}

	return err;
}

/* ---------------------------------------------------------------------- */

J. R. Okajima's avatar
J. R. Okajima committed
760
761
762
763
764
765
766
767
768
769
770
struct notify_change_args {
	int *errp;
	struct path *path;
	struct iattr *ia;
	struct inode **delegated_inode;
};

static void call_notify_change(void *args)
{
	struct notify_change_args *a = args;
	struct inode *h_inode;
771
	struct user_namespace *userns;
J. R. Okajima's avatar
J. R. Okajima committed
772
773
774
775
776
777

	h_inode = d_inode(a->path->dentry);
	IMustLock(h_inode);

	*a->errp = -EPERM;
	if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
778
		userns = mnt_user_ns(a->path->mnt);
J. R. Okajima's avatar
J. R. Okajima committed
779
		lockdep_off();
780
		*a->errp = notify_change(userns, a->path->dentry, a->ia,
J. R. Okajima's avatar
J. R. Okajima committed
781
782
					 a->delegated_inode);
		lockdep_on();
783
784
		if (!*a->errp)
			vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
J. R. Okajima's avatar
J. R. Okajima committed
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
	}
	AuTraceErr(*a->errp);
}

int vfsub_notify_change(struct path *path, struct iattr *ia,
			struct inode **delegated_inode)
{
	int err;
	struct notify_change_args args = {
		.errp			= &err,
		.path			= path,
		.ia			= ia,
		.delegated_inode	= delegated_inode
	};

	call_notify_change(&args);

	return err;
}

int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
			    struct inode **delegated_inode)
{
	int err, wkq_err;
	struct notify_change_args args = {
		.errp			= &err,
		.path			= path,
		.ia			= ia,
		.delegated_inode	= delegated_inode
	};

	wkq_err = au_wkq_wait(call_notify_change, &args);
	if (unlikely(wkq_err))
		err = wkq_err;

	return err;
}

/* ---------------------------------------------------------------------- */

J. R. Okajima's avatar
J. R. Okajima committed
825
826
827
828
829
830
831
832
833
834
835
836
struct unlink_args {
	int *errp;
	struct inode *dir;
	struct path *path;
	struct inode **delegated_inode;
};

static void call_unlink(void *args)
{
	struct unlink_args *a = args;
	struct dentry *d = a->path->dentry;
	struct inode *h_inode;
837
	struct user_namespace *userns;
J. R. Okajima's avatar
J. R. Okajima committed
838
839
	const int stop_sillyrename = (au_test_nfs(d->d_sb)
				      && au_dcount(d) == 1);
J. R. Okajima's avatar
J. R. Okajima committed
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856

	IMustLock(a->dir);

	a->path->dentry = d->d_parent;
	*a->errp = security_path_unlink(a->path, d);
	a->path->dentry = d;
	if (unlikely(*a->errp))
		return;

	if (!stop_sillyrename)
		dget(d);
	h_inode = NULL;
	if (d_is_positive(d)) {
		h_inode = d_inode(d);
		ihold(h_inode);
	}

857
	userns = mnt_user_ns(a->path->mnt);
J. R. Okajima's avatar
J. R. Okajima committed
858
	lockdep_off();
859
	*a->errp = vfs_unlink(userns, a->dir, d, a->delegated_inode);
J. R. Okajima's avatar
J. R. Okajima committed
860
	lockdep_on();
861
862
863
864
865
866
867
	if (!*a->errp) {
		struct path tmp = {
			.dentry = d->d_parent,
			.mnt	= a->path->mnt
		};
		vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
	}
J. R. Okajima's avatar
J. R. Okajima committed
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903

	if (!stop_sillyrename)
		dput(d);
	if (h_inode)
		iput(h_inode);

	AuTraceErr(*a->errp);
}

/*
 * @dir: must be locked.
 * @dentry: target dentry.
 */
int vfsub_unlink(struct inode *dir, struct path *path,
		 struct inode **delegated_inode, int force)
{
	int err;
	struct unlink_args args = {
		.errp			= &err,
		.dir			= dir,
		.path			= path,
		.delegated_inode	= delegated_inode
	};

	if (!force)
		call_unlink(&args);
	else {
		int wkq_err;

		wkq_err = au_wkq_wait(call_unlink, &args);
		if (unlikely(wkq_err))
			err = wkq_err;
	}

	return err;
}