]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - fs/shiftfs.c
UBUNTU: SAUCE: shiftfs: fix build error with 5.11
[mirror_ubuntu-hirsute-kernel.git] / fs / shiftfs.c
1 #include <linux/btrfs.h>
2 #include <linux/capability.h>
3 #include <linux/cred.h>
4 #include <linux/mount.h>
5 #include <linux/fdtable.h>
6 #include <linux/file.h>
7 #include <linux/fs.h>
8 #include <linux/namei.h>
9 #include <linux/module.h>
10 #include <linux/kernel.h>
11 #include <linux/magic.h>
12 #include <linux/parser.h>
13 #include <linux/security.h>
14 #include <linux/seq_file.h>
15 #include <linux/statfs.h>
16 #include <linux/slab.h>
17 #include <linux/user_namespace.h>
18 #include <linux/uidgid.h>
19 #include <linux/xattr.h>
20 #include <linux/posix_acl.h>
21 #include <linux/posix_acl_xattr.h>
22 #include <linux/uio.h>
23 #include <linux/fiemap.h>
24
25 struct shiftfs_super_info {
26 struct vfsmount *mnt;
27 struct user_namespace *userns;
28 /* creds of process who created the super block */
29 const struct cred *creator_cred;
30 bool mark;
31 unsigned int passthrough;
32 unsigned int passthrough_mark;
33 };
34
35 static void shiftfs_fill_inode(struct inode *inode, unsigned long ino,
36 umode_t mode, dev_t dev, struct dentry *dentry);
37
38 #define SHIFTFS_PASSTHROUGH_NONE 0
39 #define SHIFTFS_PASSTHROUGH_STAT 1
40 #define SHIFTFS_PASSTHROUGH_IOCTL 2
41 #define SHIFTFS_PASSTHROUGH_ALL \
42 (SHIFTFS_PASSTHROUGH_STAT | SHIFTFS_PASSTHROUGH_IOCTL)
43
44 static inline bool shiftfs_passthrough_ioctls(struct shiftfs_super_info *info)
45 {
46 if (!(info->passthrough & SHIFTFS_PASSTHROUGH_IOCTL))
47 return false;
48
49 return true;
50 }
51
52 static inline bool shiftfs_passthrough_statfs(struct shiftfs_super_info *info)
53 {
54 if (!(info->passthrough & SHIFTFS_PASSTHROUGH_STAT))
55 return false;
56
57 return true;
58 }
59
60 enum {
61 OPT_MARK,
62 OPT_PASSTHROUGH,
63 OPT_LAST,
64 };
65
66 /* global filesystem options */
67 static const match_table_t tokens = {
68 { OPT_MARK, "mark" },
69 { OPT_PASSTHROUGH, "passthrough=%u" },
70 { OPT_LAST, NULL }
71 };
72
73 static const struct cred *shiftfs_override_creds(const struct super_block *sb)
74 {
75 struct shiftfs_super_info *sbinfo = sb->s_fs_info;
76
77 return override_creds(sbinfo->creator_cred);
78 }
79
80 static inline void shiftfs_revert_object_creds(const struct cred *oldcred,
81 struct cred *newcred)
82 {
83 revert_creds(oldcred);
84 put_cred(newcred);
85 }
86
87 static kuid_t shift_kuid(struct user_namespace *from, struct user_namespace *to,
88 kuid_t kuid)
89 {
90 uid_t uid = from_kuid(from, kuid);
91 return make_kuid(to, uid);
92 }
93
94 static kgid_t shift_kgid(struct user_namespace *from, struct user_namespace *to,
95 kgid_t kgid)
96 {
97 gid_t gid = from_kgid(from, kgid);
98 return make_kgid(to, gid);
99 }
100
101 static int shiftfs_override_object_creds(const struct super_block *sb,
102 const struct cred **oldcred,
103 struct cred **newcred,
104 struct dentry *dentry, umode_t mode,
105 bool hardlink)
106 {
107 struct shiftfs_super_info *sbinfo = sb->s_fs_info;
108 kuid_t fsuid = current_fsuid();
109 kgid_t fsgid = current_fsgid();
110
111 *oldcred = shiftfs_override_creds(sb);
112
113 *newcred = prepare_creds();
114 if (!*newcred) {
115 revert_creds(*oldcred);
116 return -ENOMEM;
117 }
118
119 (*newcred)->fsuid = shift_kuid(sb->s_user_ns, sbinfo->userns, fsuid);
120 (*newcred)->fsgid = shift_kgid(sb->s_user_ns, sbinfo->userns, fsgid);
121
122 if (!hardlink) {
123 int err = security_dentry_create_files_as(dentry, mode,
124 &dentry->d_name,
125 *oldcred, *newcred);
126 if (err) {
127 shiftfs_revert_object_creds(*oldcred, *newcred);
128 return err;
129 }
130 }
131
132 put_cred(override_creds(*newcred));
133 return 0;
134 }
135
136 static void shiftfs_copyattr(struct inode *from, struct inode *to)
137 {
138 struct user_namespace *from_ns = from->i_sb->s_user_ns;
139 struct user_namespace *to_ns = to->i_sb->s_user_ns;
140
141 to->i_uid = shift_kuid(from_ns, to_ns, from->i_uid);
142 to->i_gid = shift_kgid(from_ns, to_ns, from->i_gid);
143 to->i_mode = from->i_mode;
144 to->i_atime = from->i_atime;
145 to->i_mtime = from->i_mtime;
146 to->i_ctime = from->i_ctime;
147 i_size_write(to, i_size_read(from));
148 }
149
150 static void shiftfs_copyflags(struct inode *from, struct inode *to)
151 {
152 unsigned int mask = S_SYNC | S_IMMUTABLE | S_APPEND | S_NOATIME;
153
154 inode_set_flags(to, from->i_flags & mask, mask);
155 }
156
157 static void shiftfs_file_accessed(struct file *file)
158 {
159 struct inode *upperi, *loweri;
160
161 if (file->f_flags & O_NOATIME)
162 return;
163
164 upperi = file_inode(file);
165 loweri = upperi->i_private;
166
167 if (!loweri)
168 return;
169
170 upperi->i_mtime = loweri->i_mtime;
171 upperi->i_ctime = loweri->i_ctime;
172
173 touch_atime(&file->f_path);
174 }
175
176 static int shiftfs_parse_mount_options(struct shiftfs_super_info *sbinfo,
177 char *options)
178 {
179 char *p;
180 substring_t args[MAX_OPT_ARGS];
181
182 sbinfo->mark = false;
183 sbinfo->passthrough = 0;
184
185 while ((p = strsep(&options, ",")) != NULL) {
186 int err, intarg, token;
187
188 if (!*p)
189 continue;
190
191 token = match_token(p, tokens, args);
192 switch (token) {
193 case OPT_MARK:
194 sbinfo->mark = true;
195 break;
196 case OPT_PASSTHROUGH:
197 err = match_int(&args[0], &intarg);
198 if (err)
199 return err;
200
201 if (intarg & ~SHIFTFS_PASSTHROUGH_ALL)
202 return -EINVAL;
203
204 sbinfo->passthrough = intarg;
205 break;
206 default:
207 return -EINVAL;
208 }
209 }
210
211 return 0;
212 }
213
214 static void shiftfs_d_release(struct dentry *dentry)
215 {
216 struct dentry *lowerd = dentry->d_fsdata;
217
218 if (lowerd)
219 dput(lowerd);
220 }
221
222 static struct dentry *shiftfs_d_real(struct dentry *dentry,
223 const struct inode *inode)
224 {
225 struct dentry *lowerd = dentry->d_fsdata;
226
227 if (inode && d_inode(dentry) == inode)
228 return dentry;
229
230 lowerd = d_real(lowerd, inode);
231 if (lowerd && (!inode || inode == d_inode(lowerd)))
232 return lowerd;
233
234 WARN(1, "shiftfs_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
235 inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
236 return dentry;
237 }
238
239 static int shiftfs_d_weak_revalidate(struct dentry *dentry, unsigned int flags)
240 {
241 int err = 1;
242 struct dentry *lowerd = dentry->d_fsdata;
243
244 if (d_is_negative(lowerd) != d_is_negative(dentry))
245 return 0;
246
247 if ((lowerd->d_flags & DCACHE_OP_WEAK_REVALIDATE))
248 err = lowerd->d_op->d_weak_revalidate(lowerd, flags);
249
250 if (d_really_is_positive(dentry)) {
251 struct inode *inode = d_inode(dentry);
252 struct inode *loweri = d_inode(lowerd);
253
254 shiftfs_copyattr(loweri, inode);
255 }
256
257 return err;
258 }
259
260 static int shiftfs_d_revalidate(struct dentry *dentry, unsigned int flags)
261 {
262 int err = 1;
263 struct dentry *lowerd = dentry->d_fsdata;
264
265 if (d_unhashed(lowerd) ||
266 ((d_is_negative(lowerd) != d_is_negative(dentry))))
267 return 0;
268
269 if (flags & LOOKUP_RCU)
270 return -ECHILD;
271
272 if ((lowerd->d_flags & DCACHE_OP_REVALIDATE))
273 err = lowerd->d_op->d_revalidate(lowerd, flags);
274
275 if (d_really_is_positive(dentry)) {
276 struct inode *inode = d_inode(dentry);
277 struct inode *loweri = d_inode(lowerd);
278
279 shiftfs_copyattr(loweri, inode);
280 }
281
282 return err;
283 }
284
285 static const struct dentry_operations shiftfs_dentry_ops = {
286 .d_release = shiftfs_d_release,
287 .d_real = shiftfs_d_real,
288 .d_revalidate = shiftfs_d_revalidate,
289 .d_weak_revalidate = shiftfs_d_weak_revalidate,
290 };
291
292 static const char *shiftfs_get_link(struct dentry *dentry, struct inode *inode,
293 struct delayed_call *done)
294 {
295 const char *p;
296 const struct cred *oldcred;
297 struct dentry *lowerd;
298
299 /* RCU lookup not supported */
300 if (!dentry)
301 return ERR_PTR(-ECHILD);
302
303 lowerd = dentry->d_fsdata;
304 oldcred = shiftfs_override_creds(dentry->d_sb);
305 p = vfs_get_link(lowerd, done);
306 revert_creds(oldcred);
307
308 return p;
309 }
310
311 static int shiftfs_setxattr(struct dentry *dentry, struct inode *inode,
312 const char *name, const void *value,
313 size_t size, int flags)
314 {
315 struct dentry *lowerd = dentry->d_fsdata;
316 int err;
317 const struct cred *oldcred;
318
319 oldcred = shiftfs_override_creds(dentry->d_sb);
320 err = vfs_setxattr(lowerd, name, value, size, flags);
321 revert_creds(oldcred);
322
323 shiftfs_copyattr(lowerd->d_inode, inode);
324
325 return err;
326 }
327
328 static int shiftfs_xattr_get(const struct xattr_handler *handler,
329 struct dentry *dentry, struct inode *inode,
330 const char *name, void *value, size_t size)
331 {
332 struct dentry *lowerd = dentry->d_fsdata;
333 int err;
334 const struct cred *oldcred;
335
336 oldcred = shiftfs_override_creds(dentry->d_sb);
337 err = vfs_getxattr(lowerd, name, value, size);
338 revert_creds(oldcred);
339
340 return err;
341 }
342
343 static ssize_t shiftfs_listxattr(struct dentry *dentry, char *list,
344 size_t size)
345 {
346 struct dentry *lowerd = dentry->d_fsdata;
347 int err;
348 const struct cred *oldcred;
349
350 oldcred = shiftfs_override_creds(dentry->d_sb);
351 err = vfs_listxattr(lowerd, list, size);
352 revert_creds(oldcred);
353
354 return err;
355 }
356
357 static int shiftfs_removexattr(struct dentry *dentry, const char *name)
358 {
359 struct dentry *lowerd = dentry->d_fsdata;
360 int err;
361 const struct cred *oldcred;
362
363 oldcred = shiftfs_override_creds(dentry->d_sb);
364 err = vfs_removexattr(lowerd, name);
365 revert_creds(oldcred);
366
367 /* update c/mtime */
368 shiftfs_copyattr(lowerd->d_inode, d_inode(dentry));
369
370 return err;
371 }
372
373 static int shiftfs_xattr_set(const struct xattr_handler *handler,
374 struct dentry *dentry, struct inode *inode,
375 const char *name, const void *value, size_t size,
376 int flags)
377 {
378 if (!value)
379 return shiftfs_removexattr(dentry, name);
380 return shiftfs_setxattr(dentry, inode, name, value, size, flags);
381 }
382
383 static int shiftfs_inode_test(struct inode *inode, void *data)
384 {
385 return inode->i_private == data;
386 }
387
388 static int shiftfs_inode_set(struct inode *inode, void *data)
389 {
390 inode->i_private = data;
391 return 0;
392 }
393
394 static int shiftfs_create_object(struct inode *diri, struct dentry *dentry,
395 umode_t mode, const char *symlink,
396 struct dentry *hardlink, bool excl)
397 {
398 int err;
399 const struct cred *oldcred;
400 struct cred *newcred;
401 void *loweri_iop_ptr = NULL;
402 umode_t modei = mode;
403 struct super_block *dir_sb = diri->i_sb;
404 struct dentry *lowerd_new = dentry->d_fsdata;
405 struct inode *inode = NULL, *loweri_dir = diri->i_private;
406 const struct inode_operations *loweri_dir_iop = loweri_dir->i_op;
407 struct dentry *lowerd_link = NULL;
408
409 if (hardlink) {
410 loweri_iop_ptr = loweri_dir_iop->link;
411 } else {
412 switch (mode & S_IFMT) {
413 case S_IFDIR:
414 loweri_iop_ptr = loweri_dir_iop->mkdir;
415 break;
416 case S_IFREG:
417 loweri_iop_ptr = loweri_dir_iop->create;
418 break;
419 case S_IFLNK:
420 loweri_iop_ptr = loweri_dir_iop->symlink;
421 break;
422 case S_IFSOCK:
423 /* fall through */
424 case S_IFIFO:
425 loweri_iop_ptr = loweri_dir_iop->mknod;
426 break;
427 }
428 }
429 if (!loweri_iop_ptr) {
430 err = -EINVAL;
431 goto out_iput;
432 }
433
434 inode_lock_nested(loweri_dir, I_MUTEX_PARENT);
435
436 if (!hardlink) {
437 inode = new_inode(dir_sb);
438 if (!inode) {
439 err = -ENOMEM;
440 goto out_iput;
441 }
442
443 /*
444 * new_inode() will have added the new inode to the super
445 * block's list of inodes. Further below we will call
446 * inode_insert5() Which would perform the same operation again
447 * thereby corrupting the list. To avoid this raise I_CREATING
448 * in i_state which will cause inode_insert5() to skip this
449 * step. I_CREATING will be cleared by d_instantiate_new()
450 * below.
451 */
452 spin_lock(&inode->i_lock);
453 inode->i_state |= I_CREATING;
454 spin_unlock(&inode->i_lock);
455
456 inode_init_owner(inode, diri, mode);
457 modei = inode->i_mode;
458 }
459
460 err = shiftfs_override_object_creds(dentry->d_sb, &oldcred, &newcred,
461 dentry, modei, hardlink != NULL);
462 if (err)
463 goto out_iput;
464
465 if (hardlink) {
466 lowerd_link = hardlink->d_fsdata;
467 err = vfs_link(lowerd_link, loweri_dir, lowerd_new, NULL);
468 } else {
469 switch (modei & S_IFMT) {
470 case S_IFDIR:
471 err = vfs_mkdir(loweri_dir, lowerd_new, modei);
472 break;
473 case S_IFREG:
474 err = vfs_create(loweri_dir, lowerd_new, modei, excl);
475 break;
476 case S_IFLNK:
477 err = vfs_symlink(loweri_dir, lowerd_new, symlink);
478 break;
479 case S_IFSOCK:
480 /* fall through */
481 case S_IFIFO:
482 err = vfs_mknod(loweri_dir, lowerd_new, modei, 0);
483 break;
484 default:
485 err = -EINVAL;
486 break;
487 }
488 }
489
490 shiftfs_revert_object_creds(oldcred, newcred);
491
492 if (!err && WARN_ON(!lowerd_new->d_inode))
493 err = -EIO;
494 if (err)
495 goto out_iput;
496
497 if (hardlink) {
498 inode = d_inode(hardlink);
499 ihold(inode);
500
501 /* copy up times from lower inode */
502 shiftfs_copyattr(d_inode(lowerd_link), inode);
503 set_nlink(d_inode(hardlink), d_inode(lowerd_link)->i_nlink);
504 d_instantiate(dentry, inode);
505 } else {
506 struct inode *inode_tmp;
507 struct inode *loweri_new = d_inode(lowerd_new);
508
509 inode_tmp = inode_insert5(inode, (unsigned long)loweri_new,
510 shiftfs_inode_test, shiftfs_inode_set,
511 loweri_new);
512 if (unlikely(inode_tmp != inode)) {
513 pr_err_ratelimited("shiftfs: newly created inode found in cache\n");
514 iput(inode_tmp);
515 err = -EINVAL;
516 goto out_iput;
517 }
518
519 ihold(loweri_new);
520 shiftfs_fill_inode(inode, loweri_new->i_ino, loweri_new->i_mode,
521 0, lowerd_new);
522 d_instantiate_new(dentry, inode);
523 }
524
525 shiftfs_copyattr(loweri_dir, diri);
526 if (loweri_iop_ptr == loweri_dir_iop->mkdir)
527 set_nlink(diri, loweri_dir->i_nlink);
528
529 inode = NULL;
530
531 out_iput:
532 iput(inode);
533 inode_unlock(loweri_dir);
534
535 return err;
536 }
537
538 static int shiftfs_create(struct inode *dir, struct dentry *dentry,
539 umode_t mode, bool excl)
540 {
541 mode |= S_IFREG;
542
543 return shiftfs_create_object(dir, dentry, mode, NULL, NULL, excl);
544 }
545
546 static int shiftfs_mkdir(struct inode *dir, struct dentry *dentry,
547 umode_t mode)
548 {
549 mode |= S_IFDIR;
550
551 return shiftfs_create_object(dir, dentry, mode, NULL, NULL, false);
552 }
553
554 static int shiftfs_link(struct dentry *hardlink, struct inode *dir,
555 struct dentry *dentry)
556 {
557 return shiftfs_create_object(dir, dentry, 0, NULL, hardlink, false);
558 }
559
560 static int shiftfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
561 dev_t rdev)
562 {
563 if (!S_ISFIFO(mode) && !S_ISSOCK(mode))
564 return -EPERM;
565
566 return shiftfs_create_object(dir, dentry, mode, NULL, NULL, false);
567 }
568
569 static int shiftfs_symlink(struct inode *dir, struct dentry *dentry,
570 const char *symlink)
571 {
572 return shiftfs_create_object(dir, dentry, S_IFLNK, symlink, NULL, false);
573 }
574
575 static int shiftfs_rm(struct inode *dir, struct dentry *dentry, bool rmdir)
576 {
577 struct dentry *lowerd = dentry->d_fsdata;
578 struct inode *loweri = dir->i_private;
579 struct inode *inode = d_inode(dentry);
580 int err;
581 const struct cred *oldcred;
582
583 dget(lowerd);
584 oldcred = shiftfs_override_creds(dentry->d_sb);
585 inode_lock_nested(loweri, I_MUTEX_PARENT);
586 if (rmdir)
587 err = vfs_rmdir(loweri, lowerd);
588 else
589 err = vfs_unlink(loweri, lowerd, NULL);
590 revert_creds(oldcred);
591
592 if (!err) {
593 d_drop(dentry);
594
595 if (rmdir)
596 clear_nlink(inode);
597 else
598 drop_nlink(inode);
599 }
600 inode_unlock(loweri);
601
602 shiftfs_copyattr(loweri, dir);
603 dput(lowerd);
604
605 return err;
606 }
607
608 static int shiftfs_unlink(struct inode *dir, struct dentry *dentry)
609 {
610 return shiftfs_rm(dir, dentry, false);
611 }
612
613 static int shiftfs_rmdir(struct inode *dir, struct dentry *dentry)
614 {
615 return shiftfs_rm(dir, dentry, true);
616 }
617
618 static int shiftfs_rename(struct inode *olddir, struct dentry *old,
619 struct inode *newdir, struct dentry *new,
620 unsigned int flags)
621 {
622 struct dentry *lowerd_dir_old = old->d_parent->d_fsdata,
623 *lowerd_dir_new = new->d_parent->d_fsdata,
624 *lowerd_old = old->d_fsdata, *lowerd_new = new->d_fsdata,
625 *trapd;
626 struct inode *loweri_dir_old = lowerd_dir_old->d_inode,
627 *loweri_dir_new = lowerd_dir_new->d_inode;
628 int err = -EINVAL;
629 const struct cred *oldcred;
630
631 trapd = lock_rename(lowerd_dir_new, lowerd_dir_old);
632
633 if (trapd == lowerd_old || trapd == lowerd_new)
634 goto out_unlock;
635
636 oldcred = shiftfs_override_creds(old->d_sb);
637 err = vfs_rename(loweri_dir_old, lowerd_old, loweri_dir_new, lowerd_new,
638 NULL, flags);
639 revert_creds(oldcred);
640
641 shiftfs_copyattr(loweri_dir_old, olddir);
642 shiftfs_copyattr(loweri_dir_new, newdir);
643
644 out_unlock:
645 unlock_rename(lowerd_dir_new, lowerd_dir_old);
646
647 return err;
648 }
649
650 static struct dentry *shiftfs_lookup(struct inode *dir, struct dentry *dentry,
651 unsigned int flags)
652 {
653 struct dentry *new;
654 struct inode *newi;
655 const struct cred *oldcred;
656 struct dentry *lowerd = dentry->d_parent->d_fsdata;
657 struct inode *inode = NULL, *loweri = lowerd->d_inode;
658
659 inode_lock(loweri);
660 oldcred = shiftfs_override_creds(dentry->d_sb);
661 new = lookup_one_len(dentry->d_name.name, lowerd, dentry->d_name.len);
662 revert_creds(oldcred);
663 inode_unlock(loweri);
664
665 if (IS_ERR(new))
666 return new;
667
668 dentry->d_fsdata = new;
669
670 newi = new->d_inode;
671 if (!newi)
672 goto out;
673
674 inode = iget5_locked(dentry->d_sb, (unsigned long)newi,
675 shiftfs_inode_test, shiftfs_inode_set, newi);
676 if (!inode) {
677 dput(new);
678 return ERR_PTR(-ENOMEM);
679 }
680 if (inode->i_state & I_NEW) {
681 /*
682 * inode->i_private set by shiftfs_inode_set(), but we still
683 * need to take a reference
684 */
685 ihold(newi);
686 shiftfs_fill_inode(inode, newi->i_ino, newi->i_mode, 0, new);
687 unlock_new_inode(inode);
688 }
689
690 out:
691 return d_splice_alias(inode, dentry);
692 }
693
694 static int shiftfs_permission(struct inode *inode, int mask)
695 {
696 int err;
697 const struct cred *oldcred;
698 struct inode *loweri = inode->i_private;
699
700 if (!loweri) {
701 WARN_ON(!(mask & MAY_NOT_BLOCK));
702 return -ECHILD;
703 }
704
705 err = generic_permission(inode, mask);
706 if (err)
707 return err;
708
709 oldcred = shiftfs_override_creds(inode->i_sb);
710 err = inode_permission(loweri, mask);
711 revert_creds(oldcred);
712
713 return err;
714 }
715
716 static int shiftfs_fiemap(struct inode *inode,
717 struct fiemap_extent_info *fieinfo, u64 start,
718 u64 len)
719 {
720 int err;
721 const struct cred *oldcred;
722 struct inode *loweri = inode->i_private;
723
724 if (!loweri->i_op->fiemap)
725 return -EOPNOTSUPP;
726
727 oldcred = shiftfs_override_creds(inode->i_sb);
728 if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC)
729 filemap_write_and_wait(loweri->i_mapping);
730 err = loweri->i_op->fiemap(loweri, fieinfo, start, len);
731 revert_creds(oldcred);
732
733 return err;
734 }
735
736 static int shiftfs_tmpfile(struct inode *dir, struct dentry *dentry,
737 umode_t mode)
738 {
739 int err;
740 const struct cred *oldcred;
741 struct dentry *lowerd = dentry->d_fsdata;
742 struct inode *loweri = dir->i_private;
743
744 if (!loweri->i_op->tmpfile)
745 return -EOPNOTSUPP;
746
747 oldcred = shiftfs_override_creds(dir->i_sb);
748 err = loweri->i_op->tmpfile(loweri, lowerd, mode);
749 revert_creds(oldcred);
750
751 return err;
752 }
753
754 static int shiftfs_setattr(struct dentry *dentry, struct iattr *attr)
755 {
756 struct dentry *lowerd = dentry->d_fsdata;
757 struct inode *loweri = lowerd->d_inode;
758 struct iattr newattr;
759 const struct cred *oldcred;
760 struct super_block *sb = dentry->d_sb;
761 struct shiftfs_super_info *sbinfo = sb->s_fs_info;
762 int err;
763
764 err = setattr_prepare(dentry, attr);
765 if (err)
766 return err;
767
768 newattr = *attr;
769 newattr.ia_uid = shift_kuid(sb->s_user_ns, sbinfo->userns, attr->ia_uid);
770 newattr.ia_gid = shift_kgid(sb->s_user_ns, sbinfo->userns, attr->ia_gid);
771
772 /*
773 * mode change is for clearing setuid/setgid bits. Allow lower fs
774 * to interpret this in its own way.
775 */
776 if (newattr.ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
777 newattr.ia_valid &= ~ATTR_MODE;
778
779 inode_lock(loweri);
780 oldcred = shiftfs_override_creds(dentry->d_sb);
781 err = notify_change(lowerd, &newattr, NULL);
782 revert_creds(oldcred);
783 inode_unlock(loweri);
784
785 shiftfs_copyattr(loweri, d_inode(dentry));
786
787 return err;
788 }
789
790 static int shiftfs_getattr(const struct path *path, struct kstat *stat,
791 u32 request_mask, unsigned int query_flags)
792 {
793 struct inode *inode = path->dentry->d_inode;
794 struct dentry *lowerd = path->dentry->d_fsdata;
795 struct inode *loweri = lowerd->d_inode;
796 struct shiftfs_super_info *info = path->dentry->d_sb->s_fs_info;
797 struct path newpath = { .mnt = info->mnt, .dentry = lowerd };
798 struct user_namespace *from_ns = loweri->i_sb->s_user_ns;
799 struct user_namespace *to_ns = inode->i_sb->s_user_ns;
800 const struct cred *oldcred;
801 int err;
802
803 oldcred = shiftfs_override_creds(inode->i_sb);
804 err = vfs_getattr(&newpath, stat, request_mask, query_flags);
805 revert_creds(oldcred);
806
807 if (err)
808 return err;
809
810 /* transform the underlying id */
811 stat->uid = shift_kuid(from_ns, to_ns, stat->uid);
812 stat->gid = shift_kgid(from_ns, to_ns, stat->gid);
813 return 0;
814 }
815
816 #ifdef CONFIG_SHIFT_FS_POSIX_ACL
817
818 static int
819 shift_acl_ids(struct user_namespace *from, struct user_namespace *to,
820 struct posix_acl *acl)
821 {
822 int i;
823
824 for (i = 0; i < acl->a_count; i++) {
825 struct posix_acl_entry *e = &acl->a_entries[i];
826 switch(e->e_tag) {
827 case ACL_USER:
828 e->e_uid = shift_kuid(from, to, e->e_uid);
829 if (!uid_valid(e->e_uid))
830 return -EOVERFLOW;
831 break;
832 case ACL_GROUP:
833 e->e_gid = shift_kgid(from, to, e->e_gid);
834 if (!gid_valid(e->e_gid))
835 return -EOVERFLOW;
836 break;
837 }
838 }
839 return 0;
840 }
841
842 static void
843 shift_acl_xattr_ids(struct user_namespace *from, struct user_namespace *to,
844 void *value, size_t size)
845 {
846 struct posix_acl_xattr_header *header = value;
847 struct posix_acl_xattr_entry *entry = (void *)(header + 1), *end;
848 int count;
849 kuid_t kuid;
850 kgid_t kgid;
851
852 if (!value)
853 return;
854 if (size < sizeof(struct posix_acl_xattr_header))
855 return;
856 if (header->a_version != cpu_to_le32(POSIX_ACL_XATTR_VERSION))
857 return;
858
859 count = posix_acl_xattr_count(size);
860 if (count < 0)
861 return;
862 if (count == 0)
863 return;
864
865 for (end = entry + count; entry != end; entry++) {
866 switch(le16_to_cpu(entry->e_tag)) {
867 case ACL_USER:
868 kuid = make_kuid(&init_user_ns, le32_to_cpu(entry->e_id));
869 kuid = shift_kuid(from, to, kuid);
870 entry->e_id = cpu_to_le32(from_kuid(&init_user_ns, kuid));
871 break;
872 case ACL_GROUP:
873 kgid = make_kgid(&init_user_ns, le32_to_cpu(entry->e_id));
874 kgid = shift_kgid(from, to, kgid);
875 entry->e_id = cpu_to_le32(from_kgid(&init_user_ns, kgid));
876 break;
877 default:
878 break;
879 }
880 }
881 }
882
883 static struct posix_acl *shiftfs_get_acl(struct inode *inode, int type)
884 {
885 struct inode *loweri = inode->i_private;
886 const struct cred *oldcred;
887 struct posix_acl *lower_acl, *acl = NULL;
888 struct user_namespace *from_ns = loweri->i_sb->s_user_ns;
889 struct user_namespace *to_ns = inode->i_sb->s_user_ns;
890 int size;
891 int err;
892
893 if (!IS_POSIXACL(loweri))
894 return NULL;
895
896 oldcred = shiftfs_override_creds(inode->i_sb);
897 lower_acl = get_acl(loweri, type);
898 revert_creds(oldcred);
899
900 if (lower_acl && !IS_ERR(lower_acl)) {
901 /* XXX: export posix_acl_clone? */
902 size = sizeof(struct posix_acl) +
903 lower_acl->a_count * sizeof(struct posix_acl_entry);
904 acl = kmemdup(lower_acl, size, GFP_KERNEL);
905 posix_acl_release(lower_acl);
906
907 if (!acl)
908 return ERR_PTR(-ENOMEM);
909
910 refcount_set(&acl->a_refcount, 1);
911
912 err = shift_acl_ids(from_ns, to_ns, acl);
913 if (err) {
914 kfree(acl);
915 return ERR_PTR(err);
916 }
917 }
918
919 return acl;
920 }
921
922 static int
923 shiftfs_posix_acl_xattr_get(const struct xattr_handler *handler,
924 struct dentry *dentry, struct inode *inode,
925 const char *name, void *buffer, size_t size)
926 {
927 struct inode *loweri = inode->i_private;
928 int ret;
929
930 ret = shiftfs_xattr_get(NULL, dentry, inode, handler->name,
931 buffer, size);
932 if (ret < 0)
933 return ret;
934
935 inode_lock(loweri);
936 shift_acl_xattr_ids(loweri->i_sb->s_user_ns, inode->i_sb->s_user_ns,
937 buffer, size);
938 inode_unlock(loweri);
939 return ret;
940 }
941
942 static int
943 shiftfs_posix_acl_xattr_set(const struct xattr_handler *handler,
944 struct dentry *dentry, struct inode *inode,
945 const char *name, const void *value,
946 size_t size, int flags)
947 {
948 struct inode *loweri = inode->i_private;
949 int err;
950
951 if (!IS_POSIXACL(loweri) || !loweri->i_op->set_acl)
952 return -EOPNOTSUPP;
953 if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
954 return value ? -EACCES : 0;
955 if (!inode_owner_or_capable(inode))
956 return -EPERM;
957
958 if (value) {
959 shift_acl_xattr_ids(inode->i_sb->s_user_ns,
960 loweri->i_sb->s_user_ns,
961 (void *)value, size);
962 err = shiftfs_setxattr(dentry, inode, handler->name, value,
963 size, flags);
964 } else {
965 err = shiftfs_removexattr(dentry, handler->name);
966 }
967
968 if (!err)
969 shiftfs_copyattr(loweri, inode);
970
971 return err;
972 }
973
974 static const struct xattr_handler
975 shiftfs_posix_acl_access_xattr_handler = {
976 .name = XATTR_NAME_POSIX_ACL_ACCESS,
977 .flags = ACL_TYPE_ACCESS,
978 .get = shiftfs_posix_acl_xattr_get,
979 .set = shiftfs_posix_acl_xattr_set,
980 };
981
982 static const struct xattr_handler
983 shiftfs_posix_acl_default_xattr_handler = {
984 .name = XATTR_NAME_POSIX_ACL_DEFAULT,
985 .flags = ACL_TYPE_DEFAULT,
986 .get = shiftfs_posix_acl_xattr_get,
987 .set = shiftfs_posix_acl_xattr_set,
988 };
989
990 #else /* !CONFIG_SHIFT_FS_POSIX_ACL */
991
992 #define shiftfs_get_acl NULL
993
994 #endif /* CONFIG_SHIFT_FS_POSIX_ACL */
995
996 static const struct inode_operations shiftfs_dir_inode_operations = {
997 .lookup = shiftfs_lookup,
998 .mkdir = shiftfs_mkdir,
999 .symlink = shiftfs_symlink,
1000 .unlink = shiftfs_unlink,
1001 .rmdir = shiftfs_rmdir,
1002 .rename = shiftfs_rename,
1003 .link = shiftfs_link,
1004 .setattr = shiftfs_setattr,
1005 .create = shiftfs_create,
1006 .mknod = shiftfs_mknod,
1007 .permission = shiftfs_permission,
1008 .getattr = shiftfs_getattr,
1009 .listxattr = shiftfs_listxattr,
1010 .get_acl = shiftfs_get_acl,
1011 };
1012
1013 static const struct inode_operations shiftfs_file_inode_operations = {
1014 .fiemap = shiftfs_fiemap,
1015 .getattr = shiftfs_getattr,
1016 .get_acl = shiftfs_get_acl,
1017 .listxattr = shiftfs_listxattr,
1018 .permission = shiftfs_permission,
1019 .setattr = shiftfs_setattr,
1020 .tmpfile = shiftfs_tmpfile,
1021 };
1022
1023 static const struct inode_operations shiftfs_special_inode_operations = {
1024 .getattr = shiftfs_getattr,
1025 .get_acl = shiftfs_get_acl,
1026 .listxattr = shiftfs_listxattr,
1027 .permission = shiftfs_permission,
1028 .setattr = shiftfs_setattr,
1029 };
1030
1031 static const struct inode_operations shiftfs_symlink_inode_operations = {
1032 .getattr = shiftfs_getattr,
1033 .get_link = shiftfs_get_link,
1034 .listxattr = shiftfs_listxattr,
1035 .setattr = shiftfs_setattr,
1036 };
1037
1038 static struct file *shiftfs_open_realfile(const struct file *file,
1039 struct inode *realinode)
1040 {
1041 struct file *realfile;
1042 const struct cred *old_cred;
1043 struct inode *inode = file_inode(file);
1044 struct dentry *lowerd = file->f_path.dentry->d_fsdata;
1045 struct shiftfs_super_info *info = inode->i_sb->s_fs_info;
1046 struct path realpath = { .mnt = info->mnt, .dentry = lowerd };
1047
1048 old_cred = shiftfs_override_creds(inode->i_sb);
1049 realfile = open_with_fake_path(&realpath, file->f_flags, realinode,
1050 info->creator_cred);
1051 revert_creds(old_cred);
1052
1053 return realfile;
1054 }
1055
1056 #define SHIFTFS_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
1057
1058 static int shiftfs_change_flags(struct file *file, unsigned int flags)
1059 {
1060 struct inode *inode = file_inode(file);
1061 int err;
1062
1063 /* if some flag changed that cannot be changed then something's amiss */
1064 if (WARN_ON((file->f_flags ^ flags) & ~SHIFTFS_SETFL_MASK))
1065 return -EIO;
1066
1067 flags &= SHIFTFS_SETFL_MASK;
1068
1069 if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
1070 return -EPERM;
1071
1072 if (flags & O_DIRECT) {
1073 if (!file->f_mapping->a_ops ||
1074 !file->f_mapping->a_ops->direct_IO)
1075 return -EINVAL;
1076 }
1077
1078 if (file->f_op->check_flags) {
1079 err = file->f_op->check_flags(flags);
1080 if (err)
1081 return err;
1082 }
1083
1084 spin_lock(&file->f_lock);
1085 file->f_flags = (file->f_flags & ~SHIFTFS_SETFL_MASK) | flags;
1086 spin_unlock(&file->f_lock);
1087
1088 return 0;
1089 }
1090
1091 static int shiftfs_open(struct inode *inode, struct file *file)
1092 {
1093 struct file *realfile;
1094
1095 realfile = shiftfs_open_realfile(file, inode->i_private);
1096 if (IS_ERR(realfile))
1097 return PTR_ERR(realfile);
1098
1099 file->private_data = realfile;
1100 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO. */
1101 file->f_mapping = realfile->f_mapping;
1102
1103 return 0;
1104 }
1105
1106 static int shiftfs_dir_open(struct inode *inode, struct file *file)
1107 {
1108 struct file *realfile;
1109 const struct cred *oldcred;
1110 struct dentry *lowerd = file->f_path.dentry->d_fsdata;
1111 struct shiftfs_super_info *info = inode->i_sb->s_fs_info;
1112 struct path realpath = { .mnt = info->mnt, .dentry = lowerd };
1113
1114 oldcred = shiftfs_override_creds(file->f_path.dentry->d_sb);
1115 realfile = dentry_open(&realpath, file->f_flags | O_NOATIME,
1116 info->creator_cred);
1117 revert_creds(oldcred);
1118 if (IS_ERR(realfile))
1119 return PTR_ERR(realfile);
1120
1121 file->private_data = realfile;
1122
1123 return 0;
1124 }
1125
1126 static int shiftfs_release(struct inode *inode, struct file *file)
1127 {
1128 struct file *realfile = file->private_data;
1129
1130 if (realfile)
1131 fput(realfile);
1132
1133 return 0;
1134 }
1135
1136 static int shiftfs_dir_release(struct inode *inode, struct file *file)
1137 {
1138 return shiftfs_release(inode, file);
1139 }
1140
1141 static loff_t shiftfs_dir_llseek(struct file *file, loff_t offset, int whence)
1142 {
1143 struct file *realfile = file->private_data;
1144
1145 return vfs_llseek(realfile, offset, whence);
1146 }
1147
1148 static loff_t shiftfs_file_llseek(struct file *file, loff_t offset, int whence)
1149 {
1150 struct inode *realinode = file_inode(file)->i_private;
1151
1152 return generic_file_llseek_size(file, offset, whence,
1153 realinode->i_sb->s_maxbytes,
1154 i_size_read(realinode));
1155 }
1156
1157 /* XXX: Need to figure out what to to about atime updates, maybe other
1158 * timestamps too ... ref. ovl_file_accessed() */
1159
1160 static rwf_t shiftfs_iocb_to_rwf(struct kiocb *iocb)
1161 {
1162 int ifl = iocb->ki_flags;
1163 rwf_t flags = 0;
1164
1165 if (ifl & IOCB_NOWAIT)
1166 flags |= RWF_NOWAIT;
1167 if (ifl & IOCB_HIPRI)
1168 flags |= RWF_HIPRI;
1169 if (ifl & IOCB_DSYNC)
1170 flags |= RWF_DSYNC;
1171 if (ifl & IOCB_SYNC)
1172 flags |= RWF_SYNC;
1173
1174 return flags;
1175 }
1176
1177 static int shiftfs_real_fdget(const struct file *file, struct fd *lowerfd)
1178 {
1179 struct file *realfile;
1180
1181 if (file->f_op->open != shiftfs_open &&
1182 file->f_op->open != shiftfs_dir_open)
1183 return -EINVAL;
1184
1185 realfile = file->private_data;
1186 lowerfd->flags = 0;
1187 lowerfd->file = realfile;
1188
1189 /* Did the flags change since open? */
1190 if (unlikely(file->f_flags & ~lowerfd->file->f_flags))
1191 return shiftfs_change_flags(lowerfd->file, file->f_flags);
1192
1193 return 0;
1194 }
1195
1196 static ssize_t shiftfs_read_iter(struct kiocb *iocb, struct iov_iter *iter)
1197 {
1198 struct file *file = iocb->ki_filp;
1199 struct fd lowerfd;
1200 const struct cred *oldcred;
1201 ssize_t ret;
1202
1203 if (!iov_iter_count(iter))
1204 return 0;
1205
1206 ret = shiftfs_real_fdget(file, &lowerfd);
1207 if (ret)
1208 return ret;
1209
1210 oldcred = shiftfs_override_creds(file->f_path.dentry->d_sb);
1211 ret = vfs_iter_read(lowerfd.file, iter, &iocb->ki_pos,
1212 shiftfs_iocb_to_rwf(iocb));
1213 revert_creds(oldcred);
1214
1215 shiftfs_file_accessed(file);
1216
1217 fdput(lowerfd);
1218 return ret;
1219 }
1220
1221 static ssize_t shiftfs_write_iter(struct kiocb *iocb, struct iov_iter *iter)
1222 {
1223 struct file *file = iocb->ki_filp;
1224 struct inode *inode = file_inode(file);
1225 struct fd lowerfd;
1226 const struct cred *oldcred;
1227 ssize_t ret;
1228
1229 if (!iov_iter_count(iter))
1230 return 0;
1231
1232 inode_lock(inode);
1233 /* Update mode */
1234 shiftfs_copyattr(inode->i_private, inode);
1235 ret = file_remove_privs(file);
1236 if (ret)
1237 goto out_unlock;
1238
1239 ret = shiftfs_real_fdget(file, &lowerfd);
1240 if (ret)
1241 goto out_unlock;
1242
1243 oldcred = shiftfs_override_creds(file->f_path.dentry->d_sb);
1244 file_start_write(lowerfd.file);
1245 ret = vfs_iter_write(lowerfd.file, iter, &iocb->ki_pos,
1246 shiftfs_iocb_to_rwf(iocb));
1247 file_end_write(lowerfd.file);
1248 revert_creds(oldcred);
1249
1250 /* Update size */
1251 shiftfs_copyattr(inode->i_private, inode);
1252
1253 fdput(lowerfd);
1254
1255 out_unlock:
1256 inode_unlock(inode);
1257 return ret;
1258 }
1259
1260 static int shiftfs_fsync(struct file *file, loff_t start, loff_t end,
1261 int datasync)
1262 {
1263 struct fd lowerfd;
1264 const struct cred *oldcred;
1265 int ret;
1266
1267 ret = shiftfs_real_fdget(file, &lowerfd);
1268 if (ret)
1269 return ret;
1270
1271 oldcred = shiftfs_override_creds(file->f_path.dentry->d_sb);
1272 ret = vfs_fsync_range(lowerfd.file, start, end, datasync);
1273 revert_creds(oldcred);
1274
1275 fdput(lowerfd);
1276 return ret;
1277 }
1278
1279 static int shiftfs_mmap(struct file *file, struct vm_area_struct *vma)
1280 {
1281 struct file *realfile = file->private_data;
1282 const struct cred *oldcred;
1283 int ret;
1284
1285 if (!realfile->f_op->mmap)
1286 return -ENODEV;
1287
1288 if (WARN_ON(file != vma->vm_file))
1289 return -EIO;
1290
1291 oldcred = shiftfs_override_creds(file->f_path.dentry->d_sb);
1292 vma->vm_file = get_file(realfile);
1293 ret = call_mmap(vma->vm_file, vma);
1294 revert_creds(oldcred);
1295
1296 shiftfs_file_accessed(file);
1297
1298 if (ret) {
1299 /*
1300 * Drop refcount from new vm_file value and restore original
1301 * vm_file value
1302 */
1303 vma->vm_file = file;
1304 fput(realfile);
1305 } else {
1306 /* Drop refcount from previous vm_file value */
1307 fput(file);
1308 }
1309
1310 return ret;
1311 }
1312
1313 static long shiftfs_fallocate(struct file *file, int mode, loff_t offset,
1314 loff_t len)
1315 {
1316 struct inode *inode = file_inode(file);
1317 struct inode *loweri = inode->i_private;
1318 struct fd lowerfd;
1319 const struct cred *oldcred;
1320 int ret;
1321
1322 ret = shiftfs_real_fdget(file, &lowerfd);
1323 if (ret)
1324 return ret;
1325
1326 oldcred = shiftfs_override_creds(file->f_path.dentry->d_sb);
1327 ret = vfs_fallocate(lowerfd.file, mode, offset, len);
1328 revert_creds(oldcred);
1329
1330 /* Update size */
1331 shiftfs_copyattr(loweri, inode);
1332
1333 fdput(lowerfd);
1334 return ret;
1335 }
1336
1337 static int shiftfs_fadvise(struct file *file, loff_t offset, loff_t len,
1338 int advice)
1339 {
1340 struct fd lowerfd;
1341 const struct cred *oldcred;
1342 int ret;
1343
1344 ret = shiftfs_real_fdget(file, &lowerfd);
1345 if (ret)
1346 return ret;
1347
1348 oldcred = shiftfs_override_creds(file->f_path.dentry->d_sb);
1349 ret = vfs_fadvise(lowerfd.file, offset, len, advice);
1350 revert_creds(oldcred);
1351
1352 fdput(lowerfd);
1353 return ret;
1354 }
1355
1356 static int shiftfs_override_ioctl_creds(int cmd, const struct super_block *sb,
1357 const struct cred **oldcred,
1358 struct cred **newcred)
1359 {
1360 struct shiftfs_super_info *sbinfo = sb->s_fs_info;
1361 kuid_t fsuid = current_fsuid();
1362 kgid_t fsgid = current_fsgid();
1363
1364 *oldcred = shiftfs_override_creds(sb);
1365
1366 *newcred = prepare_creds();
1367 if (!*newcred) {
1368 revert_creds(*oldcred);
1369 return -ENOMEM;
1370 }
1371
1372 (*newcred)->fsuid = shift_kuid(sb->s_user_ns, sbinfo->userns, fsuid);
1373 (*newcred)->fsgid = shift_kgid(sb->s_user_ns, sbinfo->userns, fsgid);
1374
1375 /* clear all caps to prevent bypassing capable() checks */
1376 cap_clear((*newcred)->cap_bset);
1377 cap_clear((*newcred)->cap_effective);
1378 cap_clear((*newcred)->cap_inheritable);
1379 cap_clear((*newcred)->cap_permitted);
1380
1381 if (cmd == BTRFS_IOC_SNAP_DESTROY) {
1382 kuid_t kuid_root = make_kuid(sb->s_user_ns, 0);
1383 /*
1384 * Allow the root user in the container to remove subvolumes
1385 * from other users.
1386 */
1387 if (uid_valid(kuid_root) && uid_eq(fsuid, kuid_root))
1388 cap_raise((*newcred)->cap_effective, CAP_DAC_OVERRIDE);
1389 }
1390
1391 put_cred(override_creds(*newcred));
1392 return 0;
1393 }
1394
1395 static inline void shiftfs_revert_ioctl_creds(const struct cred *oldcred,
1396 struct cred *newcred)
1397 {
1398 return shiftfs_revert_object_creds(oldcred, newcred);
1399 }
1400
1401 static inline bool is_btrfs_snap_ioctl(int cmd)
1402 {
1403 if ((cmd == BTRFS_IOC_SNAP_CREATE) || (cmd == BTRFS_IOC_SNAP_CREATE_V2))
1404 return true;
1405
1406 return false;
1407 }
1408
1409 static int shiftfs_btrfs_ioctl_fd_restore(int cmd, int fd, void __user *arg,
1410 struct btrfs_ioctl_vol_args *v1,
1411 struct btrfs_ioctl_vol_args_v2 *v2)
1412 {
1413 int ret;
1414
1415 if (!is_btrfs_snap_ioctl(cmd))
1416 return 0;
1417
1418 if (cmd == BTRFS_IOC_SNAP_CREATE)
1419 ret = copy_to_user(arg, v1, sizeof(*v1));
1420 else
1421 ret = copy_to_user(arg, v2, sizeof(*v2));
1422
1423 close_fd(fd);
1424 kfree(v1);
1425 kfree(v2);
1426
1427 return ret;
1428 }
1429
1430 static int shiftfs_btrfs_ioctl_fd_replace(int cmd, void __user *arg,
1431 struct btrfs_ioctl_vol_args **b1,
1432 struct btrfs_ioctl_vol_args_v2 **b2,
1433 int *newfd)
1434 {
1435 int oldfd, ret;
1436 struct fd src;
1437 struct fd lfd = {};
1438 struct btrfs_ioctl_vol_args *v1 = NULL;
1439 struct btrfs_ioctl_vol_args_v2 *v2 = NULL;
1440
1441 if (!is_btrfs_snap_ioctl(cmd))
1442 return 0;
1443
1444 if (cmd == BTRFS_IOC_SNAP_CREATE) {
1445 v1 = memdup_user(arg, sizeof(*v1));
1446 if (IS_ERR(v1))
1447 return PTR_ERR(v1);
1448 oldfd = v1->fd;
1449 *b1 = v1;
1450 } else {
1451 v2 = memdup_user(arg, sizeof(*v2));
1452 if (IS_ERR(v2))
1453 return PTR_ERR(v2);
1454 oldfd = v2->fd;
1455 *b2 = v2;
1456 }
1457
1458 src = fdget(oldfd);
1459 if (!src.file)
1460 return -EINVAL;
1461
1462 ret = shiftfs_real_fdget(src.file, &lfd);
1463 if (ret) {
1464 fdput(src);
1465 return ret;
1466 }
1467
1468 /*
1469 * shiftfs_real_fdget() does not take a reference to lfd.file, so
1470 * take a reference here to offset the one which will be put by
1471 * close_fd(), and make sure that reference is put on fdput(lfd).
1472 */
1473 get_file(lfd.file);
1474 lfd.flags |= FDPUT_FPUT;
1475 fdput(src);
1476
1477 *newfd = get_unused_fd_flags(lfd.file->f_flags);
1478 if (*newfd < 0) {
1479 fdput(lfd);
1480 return *newfd;
1481 }
1482
1483 fd_install(*newfd, lfd.file);
1484
1485 if (cmd == BTRFS_IOC_SNAP_CREATE) {
1486 v1->fd = *newfd;
1487 ret = copy_to_user(arg, v1, sizeof(*v1));
1488 v1->fd = oldfd;
1489 } else {
1490 v2->fd = *newfd;
1491 ret = copy_to_user(arg, v2, sizeof(*v2));
1492 v2->fd = oldfd;
1493 }
1494
1495 if (ret)
1496 shiftfs_btrfs_ioctl_fd_restore(cmd, *newfd, arg, v1, v2);
1497
1498 return ret;
1499 }
1500
1501 static long shiftfs_real_ioctl(struct file *file, unsigned int cmd,
1502 unsigned long arg)
1503 {
1504 struct fd lowerfd;
1505 struct cred *newcred;
1506 const struct cred *oldcred;
1507 int newfd = -EBADF;
1508 long err = 0, ret = 0;
1509 void __user *argp = (void __user *)arg;
1510 struct super_block *sb = file->f_path.dentry->d_sb;
1511 struct btrfs_ioctl_vol_args *btrfs_v1 = NULL;
1512 struct btrfs_ioctl_vol_args_v2 *btrfs_v2 = NULL;
1513
1514 ret = shiftfs_btrfs_ioctl_fd_replace(cmd, argp, &btrfs_v1, &btrfs_v2,
1515 &newfd);
1516 if (ret < 0)
1517 return ret;
1518
1519 ret = shiftfs_real_fdget(file, &lowerfd);
1520 if (ret)
1521 goto out_restore;
1522
1523 ret = shiftfs_override_ioctl_creds(cmd, sb, &oldcred, &newcred);
1524 if (ret)
1525 goto out_fdput;
1526
1527 ret = vfs_ioctl(lowerfd.file, cmd, arg);
1528
1529 shiftfs_revert_ioctl_creds(oldcred, newcred);
1530
1531 shiftfs_copyattr(file_inode(lowerfd.file), file_inode(file));
1532 shiftfs_copyflags(file_inode(lowerfd.file), file_inode(file));
1533
1534 out_fdput:
1535 fdput(lowerfd);
1536
1537 out_restore:
1538 err = shiftfs_btrfs_ioctl_fd_restore(cmd, newfd, argp,
1539 btrfs_v1, btrfs_v2);
1540 if (!ret)
1541 ret = err;
1542
1543 return ret;
1544 }
1545
1546 static bool in_ioctl_whitelist(int flag, unsigned long arg)
1547 {
1548 void __user *argp = (void __user *)arg;
1549 u64 flags = 0;
1550
1551 switch (flag) {
1552 case BTRFS_IOC_FS_INFO:
1553 return true;
1554 case BTRFS_IOC_SNAP_CREATE:
1555 return true;
1556 case BTRFS_IOC_SNAP_CREATE_V2:
1557 return true;
1558 case BTRFS_IOC_SUBVOL_CREATE:
1559 return true;
1560 case BTRFS_IOC_SUBVOL_CREATE_V2:
1561 return true;
1562 case BTRFS_IOC_SUBVOL_GETFLAGS:
1563 return true;
1564 case BTRFS_IOC_SUBVOL_SETFLAGS:
1565 if (copy_from_user(&flags, argp, sizeof(flags)))
1566 return false;
1567
1568 if (flags & ~BTRFS_SUBVOL_RDONLY)
1569 return false;
1570
1571 return true;
1572 case BTRFS_IOC_SNAP_DESTROY:
1573 return true;
1574 }
1575
1576 return false;
1577 }
1578
1579 static long shiftfs_ioctl(struct file *file, unsigned int cmd,
1580 unsigned long arg)
1581 {
1582 switch (cmd) {
1583 case FS_IOC_GETVERSION:
1584 /* fall through */
1585 case FS_IOC_GETFLAGS:
1586 /* fall through */
1587 case FS_IOC_SETFLAGS:
1588 break;
1589 default:
1590 if (!in_ioctl_whitelist(cmd, arg) ||
1591 !shiftfs_passthrough_ioctls(file->f_path.dentry->d_sb->s_fs_info))
1592 return -ENOTTY;
1593 }
1594
1595 return shiftfs_real_ioctl(file, cmd, arg);
1596 }
1597
1598 static long shiftfs_compat_ioctl(struct file *file, unsigned int cmd,
1599 unsigned long arg)
1600 {
1601 switch (cmd) {
1602 case FS_IOC32_GETVERSION:
1603 /* fall through */
1604 case FS_IOC32_GETFLAGS:
1605 /* fall through */
1606 case FS_IOC32_SETFLAGS:
1607 break;
1608 default:
1609 if (!in_ioctl_whitelist(cmd, arg) ||
1610 !shiftfs_passthrough_ioctls(file->f_path.dentry->d_sb->s_fs_info))
1611 return -ENOIOCTLCMD;
1612 }
1613
1614 return shiftfs_real_ioctl(file, cmd, arg);
1615 }
1616
1617 enum shiftfs_copyop {
1618 SHIFTFS_COPY,
1619 SHIFTFS_CLONE,
1620 SHIFTFS_DEDUPE,
1621 };
1622
1623 static ssize_t shiftfs_copyfile(struct file *file_in, loff_t pos_in,
1624 struct file *file_out, loff_t pos_out, u64 len,
1625 unsigned int flags, enum shiftfs_copyop op)
1626 {
1627 ssize_t ret;
1628 struct fd real_in, real_out;
1629 const struct cred *oldcred;
1630 struct inode *inode_out = file_inode(file_out);
1631 struct inode *loweri = inode_out->i_private;
1632
1633 ret = shiftfs_real_fdget(file_out, &real_out);
1634 if (ret)
1635 return ret;
1636
1637 ret = shiftfs_real_fdget(file_in, &real_in);
1638 if (ret) {
1639 fdput(real_out);
1640 return ret;
1641 }
1642
1643 oldcred = shiftfs_override_creds(inode_out->i_sb);
1644 switch (op) {
1645 case SHIFTFS_COPY:
1646 ret = vfs_copy_file_range(real_in.file, pos_in, real_out.file,
1647 pos_out, len, flags);
1648 break;
1649
1650 case SHIFTFS_CLONE:
1651 ret = vfs_clone_file_range(real_in.file, pos_in, real_out.file,
1652 pos_out, len, flags);
1653 break;
1654
1655 case SHIFTFS_DEDUPE:
1656 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
1657 real_out.file, pos_out, len,
1658 flags);
1659 break;
1660 }
1661 revert_creds(oldcred);
1662
1663 /* Update size */
1664 shiftfs_copyattr(loweri, inode_out);
1665
1666 fdput(real_in);
1667 fdput(real_out);
1668
1669 return ret;
1670 }
1671
1672 static ssize_t shiftfs_copy_file_range(struct file *file_in, loff_t pos_in,
1673 struct file *file_out, loff_t pos_out,
1674 size_t len, unsigned int flags)
1675 {
1676 return shiftfs_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
1677 SHIFTFS_COPY);
1678 }
1679
1680 static loff_t shiftfs_remap_file_range(struct file *file_in, loff_t pos_in,
1681 struct file *file_out, loff_t pos_out,
1682 loff_t len, unsigned int remap_flags)
1683 {
1684 enum shiftfs_copyop op;
1685
1686 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
1687 return -EINVAL;
1688
1689 if (remap_flags & REMAP_FILE_DEDUP)
1690 op = SHIFTFS_DEDUPE;
1691 else
1692 op = SHIFTFS_CLONE;
1693
1694 return shiftfs_copyfile(file_in, pos_in, file_out, pos_out, len,
1695 remap_flags, op);
1696 }
1697
1698 static int shiftfs_iterate_shared(struct file *file, struct dir_context *ctx)
1699 {
1700 const struct cred *oldcred;
1701 int err = -ENOTDIR;
1702 struct file *realfile = file->private_data;
1703
1704 oldcred = shiftfs_override_creds(file->f_path.dentry->d_sb);
1705 err = iterate_dir(realfile, ctx);
1706 revert_creds(oldcred);
1707
1708 return err;
1709 }
1710
1711 const struct file_operations shiftfs_file_operations = {
1712 .open = shiftfs_open,
1713 .release = shiftfs_release,
1714 .llseek = shiftfs_file_llseek,
1715 .read_iter = shiftfs_read_iter,
1716 .write_iter = shiftfs_write_iter,
1717 .fsync = shiftfs_fsync,
1718 .mmap = shiftfs_mmap,
1719 .fallocate = shiftfs_fallocate,
1720 .fadvise = shiftfs_fadvise,
1721 .unlocked_ioctl = shiftfs_ioctl,
1722 .compat_ioctl = shiftfs_compat_ioctl,
1723 .copy_file_range = shiftfs_copy_file_range,
1724 .remap_file_range = shiftfs_remap_file_range,
1725 };
1726
1727 const struct file_operations shiftfs_dir_operations = {
1728 .open = shiftfs_dir_open,
1729 .release = shiftfs_dir_release,
1730 .compat_ioctl = shiftfs_compat_ioctl,
1731 .fsync = shiftfs_fsync,
1732 .iterate_shared = shiftfs_iterate_shared,
1733 .llseek = shiftfs_dir_llseek,
1734 .read = generic_read_dir,
1735 .unlocked_ioctl = shiftfs_ioctl,
1736 };
1737
1738 static const struct address_space_operations shiftfs_aops = {
1739 /* For O_DIRECT dentry_open() checks f_mapping->a_ops->direct_IO */
1740 .direct_IO = noop_direct_IO,
1741 };
1742
1743 static void shiftfs_fill_inode(struct inode *inode, unsigned long ino,
1744 umode_t mode, dev_t dev, struct dentry *dentry)
1745 {
1746 struct inode *loweri;
1747
1748 inode->i_ino = ino;
1749 inode->i_flags |= S_NOCMTIME;
1750
1751 mode &= S_IFMT;
1752 inode->i_mode = mode;
1753 switch (mode & S_IFMT) {
1754 case S_IFDIR:
1755 inode->i_op = &shiftfs_dir_inode_operations;
1756 inode->i_fop = &shiftfs_dir_operations;
1757 break;
1758 case S_IFLNK:
1759 inode->i_op = &shiftfs_symlink_inode_operations;
1760 break;
1761 case S_IFREG:
1762 inode->i_op = &shiftfs_file_inode_operations;
1763 inode->i_fop = &shiftfs_file_operations;
1764 inode->i_mapping->a_ops = &shiftfs_aops;
1765 break;
1766 default:
1767 inode->i_op = &shiftfs_special_inode_operations;
1768 init_special_inode(inode, mode, dev);
1769 break;
1770 }
1771
1772 if (!dentry)
1773 return;
1774
1775 loweri = dentry->d_inode;
1776 if (!loweri->i_op->get_link)
1777 inode->i_opflags |= IOP_NOFOLLOW;
1778
1779 shiftfs_copyattr(loweri, inode);
1780 shiftfs_copyflags(loweri, inode);
1781 set_nlink(inode, loweri->i_nlink);
1782 }
1783
1784 static int shiftfs_show_options(struct seq_file *m, struct dentry *dentry)
1785 {
1786 struct super_block *sb = dentry->d_sb;
1787 struct shiftfs_super_info *sbinfo = sb->s_fs_info;
1788
1789 if (sbinfo->mark)
1790 seq_show_option(m, "mark", NULL);
1791
1792 if (sbinfo->passthrough)
1793 seq_printf(m, ",passthrough=%u", sbinfo->passthrough);
1794
1795 return 0;
1796 }
1797
1798 static int shiftfs_statfs(struct dentry *dentry, struct kstatfs *buf)
1799 {
1800 struct super_block *sb = dentry->d_sb;
1801 struct shiftfs_super_info *sbinfo = sb->s_fs_info;
1802 struct dentry *root = sb->s_root;
1803 struct dentry *realroot = root->d_fsdata;
1804 struct path realpath = { .mnt = sbinfo->mnt, .dentry = realroot };
1805 int err;
1806
1807 err = vfs_statfs(&realpath, buf);
1808 if (err)
1809 return err;
1810
1811 if (!shiftfs_passthrough_statfs(sbinfo))
1812 buf->f_type = sb->s_magic;
1813
1814 return 0;
1815 }
1816
1817 static void shiftfs_evict_inode(struct inode *inode)
1818 {
1819 struct inode *loweri = inode->i_private;
1820
1821 clear_inode(inode);
1822
1823 if (loweri)
1824 iput(loweri);
1825 }
1826
1827 static void shiftfs_put_super(struct super_block *sb)
1828 {
1829 struct shiftfs_super_info *sbinfo = sb->s_fs_info;
1830
1831 if (sbinfo) {
1832 mntput(sbinfo->mnt);
1833 put_cred(sbinfo->creator_cred);
1834 kfree(sbinfo);
1835 }
1836 }
1837
1838 static const struct xattr_handler shiftfs_xattr_handler = {
1839 .prefix = "",
1840 .get = shiftfs_xattr_get,
1841 .set = shiftfs_xattr_set,
1842 };
1843
1844 const struct xattr_handler *shiftfs_xattr_handlers[] = {
1845 #ifdef CONFIG_SHIFT_FS_POSIX_ACL
1846 &shiftfs_posix_acl_access_xattr_handler,
1847 &shiftfs_posix_acl_default_xattr_handler,
1848 #endif
1849 &shiftfs_xattr_handler,
1850 NULL
1851 };
1852
1853 static inline bool passthrough_is_subset(int old_flags, int new_flags)
1854 {
1855 if ((new_flags & old_flags) != new_flags)
1856 return false;
1857
1858 return true;
1859 }
1860
1861 static int shiftfs_super_check_flags(unsigned long old_flags,
1862 unsigned long new_flags)
1863 {
1864 if ((old_flags & SB_RDONLY) && !(new_flags & SB_RDONLY))
1865 return -EPERM;
1866
1867 if ((old_flags & SB_NOSUID) && !(new_flags & SB_NOSUID))
1868 return -EPERM;
1869
1870 if ((old_flags & SB_NODEV) && !(new_flags & SB_NODEV))
1871 return -EPERM;
1872
1873 if ((old_flags & SB_NOEXEC) && !(new_flags & SB_NOEXEC))
1874 return -EPERM;
1875
1876 if ((old_flags & SB_NOATIME) && !(new_flags & SB_NOATIME))
1877 return -EPERM;
1878
1879 if ((old_flags & SB_NODIRATIME) && !(new_flags & SB_NODIRATIME))
1880 return -EPERM;
1881
1882 if (!(old_flags & SB_POSIXACL) && (new_flags & SB_POSIXACL))
1883 return -EPERM;
1884
1885 return 0;
1886 }
1887
1888 static int shiftfs_remount(struct super_block *sb, int *flags, char *data)
1889 {
1890 int err;
1891 struct shiftfs_super_info new = {};
1892 struct shiftfs_super_info *info = sb->s_fs_info;
1893
1894 err = shiftfs_parse_mount_options(&new, data);
1895 if (err)
1896 return err;
1897
1898 err = shiftfs_super_check_flags(sb->s_flags, *flags);
1899 if (err)
1900 return err;
1901
1902 /* Mark mount option cannot be changed. */
1903 if (info->mark || (info->mark != new.mark))
1904 return -EPERM;
1905
1906 if (info->passthrough != new.passthrough) {
1907 /* Don't allow exceeding passthrough options of mark mount. */
1908 if (!passthrough_is_subset(info->passthrough_mark,
1909 info->passthrough))
1910 return -EPERM;
1911
1912 info->passthrough = new.passthrough;
1913 }
1914
1915 return 0;
1916 }
1917
1918 static const struct super_operations shiftfs_super_ops = {
1919 .put_super = shiftfs_put_super,
1920 .show_options = shiftfs_show_options,
1921 .statfs = shiftfs_statfs,
1922 .remount_fs = shiftfs_remount,
1923 .evict_inode = shiftfs_evict_inode,
1924 };
1925
1926 struct shiftfs_data {
1927 void *data;
1928 const char *path;
1929 };
1930
1931 static void shiftfs_super_force_flags(struct super_block *sb,
1932 unsigned long lower_flags)
1933 {
1934 sb->s_flags |= lower_flags & (SB_RDONLY | SB_NOSUID | SB_NODEV |
1935 SB_NOEXEC | SB_NOATIME | SB_NODIRATIME);
1936
1937 if (!(lower_flags & SB_POSIXACL))
1938 sb->s_flags &= ~SB_POSIXACL;
1939 }
1940
1941 static int shiftfs_fill_super(struct super_block *sb, void *raw_data,
1942 int silent)
1943 {
1944 int err;
1945 struct path path = {};
1946 struct shiftfs_super_info *sbinfo_mp;
1947 char *name = NULL;
1948 struct inode *inode = NULL;
1949 struct dentry *dentry = NULL;
1950 struct shiftfs_data *data = raw_data;
1951 struct shiftfs_super_info *sbinfo = NULL;
1952
1953 if (!data->path)
1954 return -EINVAL;
1955
1956 sb->s_fs_info = kzalloc(sizeof(*sbinfo), GFP_KERNEL);
1957 if (!sb->s_fs_info)
1958 return -ENOMEM;
1959 sbinfo = sb->s_fs_info;
1960
1961 err = shiftfs_parse_mount_options(sbinfo, data->data);
1962 if (err)
1963 return err;
1964
1965 /* to mount a mark, must be userns admin */
1966 if (!sbinfo->mark && !ns_capable(current_user_ns(), CAP_SYS_ADMIN))
1967 return -EPERM;
1968
1969 name = kstrdup(data->path, GFP_KERNEL);
1970 if (!name)
1971 return -ENOMEM;
1972
1973 err = kern_path(name, LOOKUP_FOLLOW, &path);
1974 if (err)
1975 goto out_free_name;
1976
1977 if (!S_ISDIR(path.dentry->d_inode->i_mode)) {
1978 err = -ENOTDIR;
1979 goto out_put_path;
1980 }
1981
1982 sb->s_flags |= SB_POSIXACL;
1983
1984 if (sbinfo->mark) {
1985 struct cred *cred_tmp;
1986 struct super_block *lower_sb = path.mnt->mnt_sb;
1987
1988 /* to mark a mount point, must root wrt lower s_user_ns */
1989 if (!ns_capable(lower_sb->s_user_ns, CAP_SYS_ADMIN)) {
1990 err = -EPERM;
1991 goto out_put_path;
1992 }
1993
1994 /*
1995 * this part is visible unshifted, so make sure no
1996 * executables that could be used to give suid
1997 * privileges
1998 */
1999 sb->s_iflags = SB_I_NOEXEC;
2000
2001 shiftfs_super_force_flags(sb, lower_sb->s_flags);
2002
2003 /*
2004 * Handle nesting of shiftfs mounts by referring this mark
2005 * mount back to the original mark mount. This is more
2006 * efficient and alleviates concerns about stack depth.
2007 */
2008 if (lower_sb->s_magic == SHIFTFS_MAGIC) {
2009 sbinfo_mp = lower_sb->s_fs_info;
2010
2011 /* Doesn't make sense to mark a mark mount */
2012 if (sbinfo_mp->mark) {
2013 err = -EINVAL;
2014 goto out_put_path;
2015 }
2016
2017 if (!passthrough_is_subset(sbinfo_mp->passthrough,
2018 sbinfo->passthrough)) {
2019 err = -EPERM;
2020 goto out_put_path;
2021 }
2022
2023 sbinfo->mnt = mntget(sbinfo_mp->mnt);
2024 dentry = dget(path.dentry->d_fsdata);
2025 /*
2026 * Copy up the passthrough mount options from the
2027 * parent mark mountpoint.
2028 */
2029 sbinfo->passthrough_mark = sbinfo_mp->passthrough_mark;
2030 sbinfo->creator_cred = get_cred(sbinfo_mp->creator_cred);
2031 } else {
2032 sbinfo->mnt = mntget(path.mnt);
2033 dentry = dget(path.dentry);
2034 /*
2035 * For a new mark passthrough_mark and passthrough
2036 * are identical.
2037 */
2038 sbinfo->passthrough_mark = sbinfo->passthrough;
2039
2040 cred_tmp = prepare_creds();
2041 if (!cred_tmp) {
2042 err = -ENOMEM;
2043 goto out_put_path;
2044 }
2045 /* Don't override disk quota limits or use reserved space. */
2046 cap_lower(cred_tmp->cap_effective, CAP_SYS_RESOURCE);
2047 sbinfo->creator_cred = cred_tmp;
2048 }
2049 } else {
2050 /*
2051 * This leg executes if we're admin capable in the namespace,
2052 * so be very careful.
2053 */
2054 err = -EPERM;
2055 if (path.dentry->d_sb->s_magic != SHIFTFS_MAGIC)
2056 goto out_put_path;
2057
2058 sbinfo_mp = path.dentry->d_sb->s_fs_info;
2059 if (!sbinfo_mp->mark)
2060 goto out_put_path;
2061
2062 if (!passthrough_is_subset(sbinfo_mp->passthrough,
2063 sbinfo->passthrough))
2064 goto out_put_path;
2065
2066 sbinfo->mnt = mntget(sbinfo_mp->mnt);
2067 sbinfo->creator_cred = get_cred(sbinfo_mp->creator_cred);
2068 dentry = dget(path.dentry->d_fsdata);
2069 /*
2070 * Copy up passthrough settings from mark mountpoint so we can
2071 * verify when the overlay wants to remount with different
2072 * passthrough settings.
2073 */
2074 sbinfo->passthrough_mark = sbinfo_mp->passthrough;
2075 shiftfs_super_force_flags(sb, path.mnt->mnt_sb->s_flags);
2076 }
2077
2078 sb->s_stack_depth = dentry->d_sb->s_stack_depth + 1;
2079 if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
2080 printk(KERN_ERR "shiftfs: maximum stacking depth exceeded\n");
2081 err = -EINVAL;
2082 goto out_put_path;
2083 }
2084
2085 inode = new_inode(sb);
2086 if (!inode) {
2087 err = -ENOMEM;
2088 goto out_put_path;
2089 }
2090 shiftfs_fill_inode(inode, dentry->d_inode->i_ino, S_IFDIR, 0, dentry);
2091
2092 ihold(dentry->d_inode);
2093 inode->i_private = dentry->d_inode;
2094
2095 sb->s_magic = SHIFTFS_MAGIC;
2096 sb->s_maxbytes = MAX_LFS_FILESIZE;
2097 sb->s_op = &shiftfs_super_ops;
2098 sb->s_xattr = shiftfs_xattr_handlers;
2099 sb->s_d_op = &shiftfs_dentry_ops;
2100 sb->s_root = d_make_root(inode);
2101 if (!sb->s_root) {
2102 err = -ENOMEM;
2103 goto out_put_path;
2104 }
2105
2106 sb->s_root->d_fsdata = dentry;
2107 sbinfo->userns = get_user_ns(dentry->d_sb->s_user_ns);
2108 shiftfs_copyattr(dentry->d_inode, sb->s_root->d_inode);
2109
2110 dentry = NULL;
2111 err = 0;
2112
2113 out_put_path:
2114 path_put(&path);
2115
2116 out_free_name:
2117 kfree(name);
2118
2119 dput(dentry);
2120
2121 return err;
2122 }
2123
2124 static struct dentry *shiftfs_mount(struct file_system_type *fs_type,
2125 int flags, const char *dev_name, void *data)
2126 {
2127 struct shiftfs_data d = { data, dev_name };
2128
2129 return mount_nodev(fs_type, flags, &d, shiftfs_fill_super);
2130 }
2131
2132 static struct file_system_type shiftfs_type = {
2133 .owner = THIS_MODULE,
2134 .name = "shiftfs",
2135 .mount = shiftfs_mount,
2136 .kill_sb = kill_anon_super,
2137 .fs_flags = FS_USERNS_MOUNT,
2138 };
2139
2140 static int __init shiftfs_init(void)
2141 {
2142 return register_filesystem(&shiftfs_type);
2143 }
2144
2145 static void __exit shiftfs_exit(void)
2146 {
2147 unregister_filesystem(&shiftfs_type);
2148 }
2149
2150 MODULE_ALIAS_FS("shiftfs");
2151 MODULE_AUTHOR("James Bottomley");
2152 MODULE_AUTHOR("Seth Forshee <seth.forshee@canonical.com>");
2153 MODULE_AUTHOR("Christian Brauner <christian.brauner@ubuntu.com>");
2154 MODULE_DESCRIPTION("id shifting filesystem");
2155 MODULE_LICENSE("GPL v2");
2156 module_init(shiftfs_init)
2157 module_exit(shiftfs_exit)