aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAl Viro <viro@zeniv.linux.org.uk>2009-03-29 19:00:13 -0400
committerAl Viro <viro@zeniv.linux.org.uk>2009-03-31 23:00:26 -0400
commit3e93cd671813e204c258f1e6c797959920cf7772 (patch)
treea1e0b2d2da7d296cc1c53be9cd6efa5b94b01b5c
parentf8ef3ed2bebd2c4cb9ece92efa185d7aead8831a (diff)
Take fs_struct handling to new file (fs/fs_struct.c)
Pure code move; two new helper functions for nfsd and daemonize (unshare_fs_struct() and daemonize_fs_struct() resp.; for now - the same code as used to be in callers). unshare_fs_struct() exported (for nfsd, as copy_fs_struct()/exit_fs() used to be), copy_fs_struct() and exit_fs() don't need exports anymore. Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>
-rw-r--r--fs/Makefile2
-rw-r--r--fs/fs_struct.c141
-rw-r--r--fs/internal.h6
-rw-r--r--fs/namei.c7
-rw-r--r--fs/namespace.c68
-rw-r--r--fs/nfsd/nfssvc.c7
-rw-r--r--include/linux/fs_struct.h2
-rw-r--r--kernel/exit.c31
-rw-r--r--kernel/fork.c29
9 files changed, 155 insertions, 138 deletions
diff --git a/fs/Makefile b/fs/Makefile
index 6e82a307bcd4..b5cd8e18dd9f 100644
--- a/fs/Makefile
+++ b/fs/Makefile
@@ -11,7 +11,7 @@ obj-y := open.o read_write.o file_table.o super.o \
11 attr.o bad_inode.o file.o filesystems.o namespace.o \ 11 attr.o bad_inode.o file.o filesystems.o namespace.o \
12 seq_file.o xattr.o libfs.o fs-writeback.o \ 12 seq_file.o xattr.o libfs.o fs-writeback.o \
13 pnode.o drop_caches.o splice.o sync.o utimes.o \ 13 pnode.o drop_caches.o splice.o sync.o utimes.o \
14 stack.o 14 stack.o fs_struct.o
15 15
16ifeq ($(CONFIG_BLOCK),y) 16ifeq ($(CONFIG_BLOCK),y)
17obj-y += buffer.o bio.o block_dev.o direct-io.o mpage.o ioprio.o 17obj-y += buffer.o bio.o block_dev.o direct-io.o mpage.o ioprio.o
diff --git a/fs/fs_struct.c b/fs/fs_struct.c
new file mode 100644
index 000000000000..36e0a123bbf3
--- /dev/null
+++ b/fs/fs_struct.c
@@ -0,0 +1,141 @@
1#include <linux/module.h>
2#include <linux/sched.h>
3#include <linux/fs.h>
4#include <linux/path.h>
5#include <linux/slab.h>
6
7/*
8 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
9 * It can block.
10 */
11void set_fs_root(struct fs_struct *fs, struct path *path)
12{
13 struct path old_root;
14
15 write_lock(&fs->lock);
16 old_root = fs->root;
17 fs->root = *path;
18 path_get(path);
19 write_unlock(&fs->lock);
20 if (old_root.dentry)
21 path_put(&old_root);
22}
23
24/*
25 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
26 * It can block.
27 */
28void set_fs_pwd(struct fs_struct *fs, struct path *path)
29{
30 struct path old_pwd;
31
32 write_lock(&fs->lock);
33 old_pwd = fs->pwd;
34 fs->pwd = *path;
35 path_get(path);
36 write_unlock(&fs->lock);
37
38 if (old_pwd.dentry)
39 path_put(&old_pwd);
40}
41
42void chroot_fs_refs(struct path *old_root, struct path *new_root)
43{
44 struct task_struct *g, *p;
45 struct fs_struct *fs;
46 int count = 0;
47
48 read_lock(&tasklist_lock);
49 do_each_thread(g, p) {
50 task_lock(p);
51 fs = p->fs;
52 if (fs) {
53 write_lock(&fs->lock);
54 if (fs->root.dentry == old_root->dentry
55 && fs->root.mnt == old_root->mnt) {
56 path_get(new_root);
57 fs->root = *new_root;
58 count++;
59 }
60 if (fs->pwd.dentry == old_root->dentry
61 && fs->pwd.mnt == old_root->mnt) {
62 path_get(new_root);
63 fs->pwd = *new_root;
64 count++;
65 }
66 write_unlock(&fs->lock);
67 }
68 task_unlock(p);
69 } while_each_thread(g, p);
70 read_unlock(&tasklist_lock);
71 while (count--)
72 path_put(old_root);
73}
74
75void put_fs_struct(struct fs_struct *fs)
76{
77 /* No need to hold fs->lock if we are killing it */
78 if (atomic_dec_and_test(&fs->count)) {
79 path_put(&fs->root);
80 path_put(&fs->pwd);
81 kmem_cache_free(fs_cachep, fs);
82 }
83}
84
85void exit_fs(struct task_struct *tsk)
86{
87 struct fs_struct * fs = tsk->fs;
88
89 if (fs) {
90 task_lock(tsk);
91 tsk->fs = NULL;
92 task_unlock(tsk);
93 put_fs_struct(fs);
94 }
95}
96
97struct fs_struct *copy_fs_struct(struct fs_struct *old)
98{
99 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
100 /* We don't need to lock fs - think why ;-) */
101 if (fs) {
102 atomic_set(&fs->count, 1);
103 rwlock_init(&fs->lock);
104 fs->umask = old->umask;
105 read_lock(&old->lock);
106 fs->root = old->root;
107 path_get(&old->root);
108 fs->pwd = old->pwd;
109 path_get(&old->pwd);
110 read_unlock(&old->lock);
111 }
112 return fs;
113}
114
115int unshare_fs_struct(void)
116{
117 struct fs_struct *fsp = copy_fs_struct(current->fs);
118 if (!fsp)
119 return -ENOMEM;
120 exit_fs(current);
121 current->fs = fsp;
122 return 0;
123}
124EXPORT_SYMBOL_GPL(unshare_fs_struct);
125
126/* to be mentioned only in INIT_TASK */
127struct fs_struct init_fs = {
128 .count = ATOMIC_INIT(1),
129 .lock = __RW_LOCK_UNLOCKED(init_fs.lock),
130 .umask = 0022,
131};
132
133void daemonize_fs_struct(void)
134{
135 struct fs_struct *fs;
136
137 exit_fs(current); /* current->fs->count--; */
138 fs = &init_fs;
139 current->fs = fs;
140 atomic_inc(&fs->count);
141}
diff --git a/fs/internal.h b/fs/internal.h
index 53af885f1732..477a105f8df3 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -11,6 +11,7 @@
11 11
12struct super_block; 12struct super_block;
13struct linux_binprm; 13struct linux_binprm;
14struct path;
14 15
15/* 16/*
16 * block_dev.c 17 * block_dev.c
@@ -60,3 +61,8 @@ extern void umount_tree(struct vfsmount *, int, struct list_head *);
60extern struct vfsmount *copy_tree(struct vfsmount *, struct dentry *, int); 61extern struct vfsmount *copy_tree(struct vfsmount *, struct dentry *, int);
61 62
62extern void __init mnt_init(void); 63extern void __init mnt_init(void);
64
65/*
66 * fs_struct.c
67 */
68extern void chroot_fs_refs(struct path *, struct path *);
diff --git a/fs/namei.c b/fs/namei.c
index d040ce11785d..4c65a6460138 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -2897,10 +2897,3 @@ EXPORT_SYMBOL(vfs_symlink);
2897EXPORT_SYMBOL(vfs_unlink); 2897EXPORT_SYMBOL(vfs_unlink);
2898EXPORT_SYMBOL(dentry_unhash); 2898EXPORT_SYMBOL(dentry_unhash);
2899EXPORT_SYMBOL(generic_readlink); 2899EXPORT_SYMBOL(generic_readlink);
2900
2901/* to be mentioned only in INIT_TASK */
2902struct fs_struct init_fs = {
2903 .count = ATOMIC_INIT(1),
2904 .lock = __RW_LOCK_UNLOCKED(init_fs.lock),
2905 .umask = 0022,
2906};
diff --git a/fs/namespace.c b/fs/namespace.c
index f7ec283ccfbb..1e56303c718e 100644
--- a/fs/namespace.c
+++ b/fs/namespace.c
@@ -2093,74 +2093,6 @@ out1:
2093} 2093}
2094 2094
2095/* 2095/*
2096 * Replace the fs->{rootmnt,root} with {mnt,dentry}. Put the old values.
2097 * It can block. Requires the big lock held.
2098 */
2099void set_fs_root(struct fs_struct *fs, struct path *path)
2100{
2101 struct path old_root;
2102
2103 write_lock(&fs->lock);
2104 old_root = fs->root;
2105 fs->root = *path;
2106 path_get(path);
2107 write_unlock(&fs->lock);
2108 if (old_root.dentry)
2109 path_put(&old_root);
2110}
2111
2112/*
2113 * Replace the fs->{pwdmnt,pwd} with {mnt,dentry}. Put the old values.
2114 * It can block. Requires the big lock held.
2115 */
2116void set_fs_pwd(struct fs_struct *fs, struct path *path)
2117{
2118 struct path old_pwd;
2119
2120 write_lock(&fs->lock);
2121 old_pwd = fs->pwd;
2122 fs->pwd = *path;
2123 path_get(path);
2124 write_unlock(&fs->lock);
2125
2126 if (old_pwd.dentry)
2127 path_put(&old_pwd);
2128}
2129
2130static void chroot_fs_refs(struct path *old_root, struct path *new_root)
2131{
2132 struct task_struct *g, *p;
2133 struct fs_struct *fs;
2134 int count = 0;
2135
2136 read_lock(&tasklist_lock);
2137 do_each_thread(g, p) {
2138 task_lock(p);
2139 fs = p->fs;
2140 if (fs) {
2141 write_lock(&fs->lock);
2142 if (fs->root.dentry == old_root->dentry
2143 && fs->root.mnt == old_root->mnt) {
2144 path_get(new_root);
2145 fs->root = *new_root;
2146 count++;
2147 }
2148 if (fs->pwd.dentry == old_root->dentry
2149 && fs->pwd.mnt == old_root->mnt) {
2150 path_get(new_root);
2151 fs->pwd = *new_root;
2152 count++;
2153 }
2154 write_unlock(&fs->lock);
2155 }
2156 task_unlock(p);
2157 } while_each_thread(g, p);
2158 read_unlock(&tasklist_lock);
2159 while (count--)
2160 path_put(old_root);
2161}
2162
2163/*
2164 * pivot_root Semantics: 2096 * pivot_root Semantics:
2165 * Moves the root file system of the current process to the directory put_old, 2097 * Moves the root file system of the current process to the directory put_old,
2166 * makes new_root as the new root file system of the current process, and sets 2098 * makes new_root as the new root file system of the current process, and sets
diff --git a/fs/nfsd/nfssvc.c b/fs/nfsd/nfssvc.c
index 07e4f5d7baa8..144d69918614 100644
--- a/fs/nfsd/nfssvc.c
+++ b/fs/nfsd/nfssvc.c
@@ -404,7 +404,6 @@ static int
404nfsd(void *vrqstp) 404nfsd(void *vrqstp)
405{ 405{
406 struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp; 406 struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
407 struct fs_struct *fsp;
408 int err, preverr = 0; 407 int err, preverr = 0;
409 408
410 /* Lock module and set up kernel thread */ 409 /* Lock module and set up kernel thread */
@@ -413,13 +412,11 @@ nfsd(void *vrqstp)
413 /* At this point, the thread shares current->fs 412 /* At this point, the thread shares current->fs
414 * with the init process. We need to create files with a 413 * with the init process. We need to create files with a
415 * umask of 0 instead of init's umask. */ 414 * umask of 0 instead of init's umask. */
416 fsp = copy_fs_struct(current->fs); 415 if (unshare_fs_struct() < 0) {
417 if (!fsp) {
418 printk("Unable to start nfsd thread: out of memory\n"); 416 printk("Unable to start nfsd thread: out of memory\n");
419 goto out; 417 goto out;
420 } 418 }
421 exit_fs(current); 419
422 current->fs = fsp;
423 current->fs->umask = 0; 420 current->fs->umask = 0;
424 421
425 /* 422 /*
diff --git a/include/linux/fs_struct.h b/include/linux/fs_struct.h
index 18b467dbe278..298cef1c0793 100644
--- a/include/linux/fs_struct.h
+++ b/include/linux/fs_struct.h
@@ -20,5 +20,7 @@ extern void set_fs_root(struct fs_struct *, struct path *);
20extern void set_fs_pwd(struct fs_struct *, struct path *); 20extern void set_fs_pwd(struct fs_struct *, struct path *);
21extern struct fs_struct *copy_fs_struct(struct fs_struct *); 21extern struct fs_struct *copy_fs_struct(struct fs_struct *);
22extern void put_fs_struct(struct fs_struct *); 22extern void put_fs_struct(struct fs_struct *);
23extern void daemonize_fs_struct(void);
24extern int unshare_fs_struct(void);
23 25
24#endif /* _LINUX_FS_STRUCT_H */ 26#endif /* _LINUX_FS_STRUCT_H */
diff --git a/kernel/exit.c b/kernel/exit.c
index 167e1e3ad7c6..ad8375758a79 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -429,7 +429,6 @@ EXPORT_SYMBOL(disallow_signal);
429void daemonize(const char *name, ...) 429void daemonize(const char *name, ...)
430{ 430{
431 va_list args; 431 va_list args;
432 struct fs_struct *fs;
433 sigset_t blocked; 432 sigset_t blocked;
434 433
435 va_start(args, name); 434 va_start(args, name);
@@ -462,11 +461,7 @@ void daemonize(const char *name, ...)
462 461
463 /* Become as one with the init task */ 462 /* Become as one with the init task */
464 463
465 exit_fs(current); /* current->fs->count--; */ 464 daemonize_fs_struct();
466 fs = init_task.fs;
467 current->fs = fs;
468 atomic_inc(&fs->count);
469
470 exit_files(current); 465 exit_files(current);
471 current->files = init_task.files; 466 current->files = init_task.files;
472 atomic_inc(&current->files->count); 467 atomic_inc(&current->files->count);
@@ -565,30 +560,6 @@ void exit_files(struct task_struct *tsk)
565 } 560 }
566} 561}
567 562
568void put_fs_struct(struct fs_struct *fs)
569{
570 /* No need to hold fs->lock if we are killing it */
571 if (atomic_dec_and_test(&fs->count)) {
572 path_put(&fs->root);
573 path_put(&fs->pwd);
574 kmem_cache_free(fs_cachep, fs);
575 }
576}
577
578void exit_fs(struct task_struct *tsk)
579{
580 struct fs_struct * fs = tsk->fs;
581
582 if (fs) {
583 task_lock(tsk);
584 tsk->fs = NULL;
585 task_unlock(tsk);
586 put_fs_struct(fs);
587 }
588}
589
590EXPORT_SYMBOL_GPL(exit_fs);
591
592#ifdef CONFIG_MM_OWNER 563#ifdef CONFIG_MM_OWNER
593/* 564/*
594 * Task p is exiting and it owned mm, lets find a new owner for it 565 * Task p is exiting and it owned mm, lets find a new owner for it
diff --git a/kernel/fork.c b/kernel/fork.c
index 47c15840a381..05c02dc586b1 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -681,38 +681,13 @@ fail_nomem:
681 return retval; 681 return retval;
682} 682}
683 683
684static struct fs_struct *__copy_fs_struct(struct fs_struct *old)
685{
686 struct fs_struct *fs = kmem_cache_alloc(fs_cachep, GFP_KERNEL);
687 /* We don't need to lock fs - think why ;-) */
688 if (fs) {
689 atomic_set(&fs->count, 1);
690 rwlock_init(&fs->lock);
691 fs->umask = old->umask;
692 read_lock(&old->lock);
693 fs->root = old->root;
694 path_get(&old->root);
695 fs->pwd = old->pwd;
696 path_get(&old->pwd);
697 read_unlock(&old->lock);
698 }
699 return fs;
700}
701
702struct fs_struct *copy_fs_struct(struct fs_struct *old)
703{
704 return __copy_fs_struct(old);
705}
706
707EXPORT_SYMBOL_GPL(copy_fs_struct);
708
709static int copy_fs(unsigned long clone_flags, struct task_struct *tsk) 684static int copy_fs(unsigned long clone_flags, struct task_struct *tsk)
710{ 685{
711 if (clone_flags & CLONE_FS) { 686 if (clone_flags & CLONE_FS) {
712 atomic_inc(&current->fs->count); 687 atomic_inc(&current->fs->count);
713 return 0; 688 return 0;
714 } 689 }
715 tsk->fs = __copy_fs_struct(current->fs); 690 tsk->fs = copy_fs_struct(current->fs);
716 if (!tsk->fs) 691 if (!tsk->fs)
717 return -ENOMEM; 692 return -ENOMEM;
718 return 0; 693 return 0;
@@ -1545,7 +1520,7 @@ static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
1545 1520
1546 if ((unshare_flags & CLONE_FS) && 1521 if ((unshare_flags & CLONE_FS) &&
1547 (fs && atomic_read(&fs->count) > 1)) { 1522 (fs && atomic_read(&fs->count) > 1)) {
1548 *new_fsp = __copy_fs_struct(current->fs); 1523 *new_fsp = copy_fs_struct(current->fs);
1549 if (!*new_fsp) 1524 if (!*new_fsp)
1550 return -ENOMEM; 1525 return -ENOMEM;
1551 } 1526 }