diff options
Diffstat (limited to 'fs/autofs')
-rw-r--r-- | fs/autofs/Makefile | 7 | ||||
-rw-r--r-- | fs/autofs/autofs_i.h | 164 | ||||
-rw-r--r-- | fs/autofs/dirhash.c | 249 | ||||
-rw-r--r-- | fs/autofs/init.c | 52 | ||||
-rw-r--r-- | fs/autofs/inode.c | 250 | ||||
-rw-r--r-- | fs/autofs/root.c | 564 | ||||
-rw-r--r-- | fs/autofs/symlink.c | 25 | ||||
-rw-r--r-- | fs/autofs/waitq.c | 206 |
8 files changed, 1517 insertions, 0 deletions
diff --git a/fs/autofs/Makefile b/fs/autofs/Makefile new file mode 100644 index 000000000000..453a60f46d05 --- /dev/null +++ b/fs/autofs/Makefile | |||
@@ -0,0 +1,7 @@ | |||
1 | # | ||
2 | # Makefile for the linux autofs-filesystem routines. | ||
3 | # | ||
4 | |||
5 | obj-$(CONFIG_AUTOFS_FS) += autofs.o | ||
6 | |||
7 | autofs-objs := dirhash.o init.o inode.o root.o symlink.o waitq.o | ||
diff --git a/fs/autofs/autofs_i.h b/fs/autofs/autofs_i.h new file mode 100644 index 000000000000..6171431272dc --- /dev/null +++ b/fs/autofs/autofs_i.h | |||
@@ -0,0 +1,164 @@ | |||
1 | /* -*- linux-c -*- ------------------------------------------------------- * | ||
2 | * | ||
3 | * linux/fs/autofs/autofs_i.h | ||
4 | * | ||
5 | * Copyright 1997-1998 Transmeta Corporation - All Rights Reserved | ||
6 | * | ||
7 | * This file is part of the Linux kernel and is made available under | ||
8 | * the terms of the GNU General Public License, version 2, or at your | ||
9 | * option, any later version, incorporated herein by reference. | ||
10 | * | ||
11 | * ----------------------------------------------------------------------- */ | ||
12 | |||
13 | /* Internal header file for autofs */ | ||
14 | |||
15 | #include <linux/auto_fs.h> | ||
16 | |||
17 | /* This is the range of ioctl() numbers we claim as ours */ | ||
18 | #define AUTOFS_IOC_FIRST AUTOFS_IOC_READY | ||
19 | #define AUTOFS_IOC_COUNT 32 | ||
20 | |||
21 | #include <linux/kernel.h> | ||
22 | #include <linux/slab.h> | ||
23 | #include <linux/time.h> | ||
24 | #include <linux/string.h> | ||
25 | #include <linux/wait.h> | ||
26 | #include <linux/dcache.h> | ||
27 | #include <linux/namei.h> | ||
28 | #include <linux/mount.h> | ||
29 | #include <linux/sched.h> | ||
30 | |||
31 | #include <asm/current.h> | ||
32 | #include <asm/uaccess.h> | ||
33 | |||
34 | #ifdef DEBUG | ||
35 | #define DPRINTK(D) (printk D) | ||
36 | #else | ||
37 | #define DPRINTK(D) ((void)0) | ||
38 | #endif | ||
39 | |||
40 | #define AUTOFS_SUPER_MAGIC 0x0187 | ||
41 | |||
42 | /* | ||
43 | * If the daemon returns a negative response (AUTOFS_IOC_FAIL) then the | ||
44 | * kernel will keep the negative response cached for up to the time given | ||
45 | * here, although the time can be shorter if the kernel throws the dcache | ||
46 | * entry away. This probably should be settable from user space. | ||
47 | */ | ||
48 | #define AUTOFS_NEGATIVE_TIMEOUT (60*HZ) /* 1 minute */ | ||
49 | |||
50 | /* Structures associated with the root directory hash table */ | ||
51 | |||
52 | #define AUTOFS_HASH_SIZE 67 | ||
53 | |||
54 | struct autofs_dir_ent { | ||
55 | int hash; | ||
56 | char *name; | ||
57 | int len; | ||
58 | ino_t ino; | ||
59 | struct dentry *dentry; | ||
60 | /* Linked list of entries */ | ||
61 | struct autofs_dir_ent *next; | ||
62 | struct autofs_dir_ent **back; | ||
63 | /* The following entries are for the expiry system */ | ||
64 | unsigned long last_usage; | ||
65 | struct list_head exp; | ||
66 | }; | ||
67 | |||
68 | struct autofs_dirhash { | ||
69 | struct autofs_dir_ent *h[AUTOFS_HASH_SIZE]; | ||
70 | struct list_head expiry_head; | ||
71 | }; | ||
72 | |||
73 | struct autofs_wait_queue { | ||
74 | wait_queue_head_t queue; | ||
75 | struct autofs_wait_queue *next; | ||
76 | autofs_wqt_t wait_queue_token; | ||
77 | /* We use the following to see what we are waiting for */ | ||
78 | int hash; | ||
79 | int len; | ||
80 | char *name; | ||
81 | /* This is for status reporting upon return */ | ||
82 | int status; | ||
83 | int wait_ctr; | ||
84 | }; | ||
85 | |||
86 | struct autofs_symlink { | ||
87 | char *data; | ||
88 | int len; | ||
89 | time_t mtime; | ||
90 | }; | ||
91 | |||
92 | #define AUTOFS_MAX_SYMLINKS 256 | ||
93 | |||
94 | #define AUTOFS_ROOT_INO 1 | ||
95 | #define AUTOFS_FIRST_SYMLINK 2 | ||
96 | #define AUTOFS_FIRST_DIR_INO (AUTOFS_FIRST_SYMLINK+AUTOFS_MAX_SYMLINKS) | ||
97 | |||
98 | #define AUTOFS_SYMLINK_BITMAP_LEN \ | ||
99 | ((AUTOFS_MAX_SYMLINKS+((sizeof(long)*1)-1))/(sizeof(long)*8)) | ||
100 | |||
101 | #define AUTOFS_SBI_MAGIC 0x6d4a556d | ||
102 | |||
103 | struct autofs_sb_info { | ||
104 | u32 magic; | ||
105 | struct file *pipe; | ||
106 | pid_t oz_pgrp; | ||
107 | int catatonic; | ||
108 | unsigned long exp_timeout; | ||
109 | ino_t next_dir_ino; | ||
110 | struct autofs_wait_queue *queues; /* Wait queue pointer */ | ||
111 | struct autofs_dirhash dirhash; /* Root directory hash */ | ||
112 | struct autofs_symlink symlink[AUTOFS_MAX_SYMLINKS]; | ||
113 | unsigned long symlink_bitmap[AUTOFS_SYMLINK_BITMAP_LEN]; | ||
114 | }; | ||
115 | |||
116 | static inline struct autofs_sb_info *autofs_sbi(struct super_block *sb) | ||
117 | { | ||
118 | return (struct autofs_sb_info *)(sb->s_fs_info); | ||
119 | } | ||
120 | |||
121 | /* autofs_oz_mode(): do we see the man behind the curtain? (The | ||
122 | processes which do manipulations for us in user space sees the raw | ||
123 | filesystem without "magic".) */ | ||
124 | |||
125 | static inline int autofs_oz_mode(struct autofs_sb_info *sbi) { | ||
126 | return sbi->catatonic || process_group(current) == sbi->oz_pgrp; | ||
127 | } | ||
128 | |||
129 | /* Hash operations */ | ||
130 | |||
131 | void autofs_initialize_hash(struct autofs_dirhash *); | ||
132 | struct autofs_dir_ent *autofs_hash_lookup(const struct autofs_dirhash *,struct qstr *); | ||
133 | void autofs_hash_insert(struct autofs_dirhash *,struct autofs_dir_ent *); | ||
134 | void autofs_hash_delete(struct autofs_dir_ent *); | ||
135 | struct autofs_dir_ent *autofs_hash_enum(const struct autofs_dirhash *,off_t *,struct autofs_dir_ent *); | ||
136 | void autofs_hash_dputall(struct autofs_dirhash *); | ||
137 | void autofs_hash_nuke(struct autofs_dirhash *); | ||
138 | |||
139 | /* Expiration-handling functions */ | ||
140 | |||
141 | void autofs_update_usage(struct autofs_dirhash *,struct autofs_dir_ent *); | ||
142 | struct autofs_dir_ent *autofs_expire(struct super_block *,struct autofs_sb_info *, struct vfsmount *mnt); | ||
143 | |||
144 | /* Operations structures */ | ||
145 | |||
146 | extern struct inode_operations autofs_root_inode_operations; | ||
147 | extern struct inode_operations autofs_symlink_inode_operations; | ||
148 | extern struct file_operations autofs_root_operations; | ||
149 | |||
150 | /* Initializing function */ | ||
151 | |||
152 | int autofs_fill_super(struct super_block *, void *, int); | ||
153 | |||
154 | /* Queue management functions */ | ||
155 | |||
156 | int autofs_wait(struct autofs_sb_info *,struct qstr *); | ||
157 | int autofs_wait_release(struct autofs_sb_info *,autofs_wqt_t,int); | ||
158 | void autofs_catatonic_mode(struct autofs_sb_info *); | ||
159 | |||
160 | #ifdef DEBUG | ||
161 | void autofs_say(const char *name, int len); | ||
162 | #else | ||
163 | #define autofs_say(n,l) ((void)0) | ||
164 | #endif | ||
diff --git a/fs/autofs/dirhash.c b/fs/autofs/dirhash.c new file mode 100644 index 000000000000..448143fd0796 --- /dev/null +++ b/fs/autofs/dirhash.c | |||
@@ -0,0 +1,249 @@ | |||
1 | /* -*- linux-c -*- --------------------------------------------------------- * | ||
2 | * | ||
3 | * linux/fs/autofs/dirhash.c | ||
4 | * | ||
5 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved | ||
6 | * | ||
7 | * This file is part of the Linux kernel and is made available under | ||
8 | * the terms of the GNU General Public License, version 2, or at your | ||
9 | * option, any later version, incorporated herein by reference. | ||
10 | * | ||
11 | * ------------------------------------------------------------------------- */ | ||
12 | |||
13 | #include "autofs_i.h" | ||
14 | |||
15 | /* Functions for maintenance of expiry queue */ | ||
16 | |||
17 | static void autofs_init_usage(struct autofs_dirhash *dh, | ||
18 | struct autofs_dir_ent *ent) | ||
19 | { | ||
20 | list_add_tail(&ent->exp, &dh->expiry_head); | ||
21 | ent->last_usage = jiffies; | ||
22 | } | ||
23 | |||
24 | static void autofs_delete_usage(struct autofs_dir_ent *ent) | ||
25 | { | ||
26 | list_del(&ent->exp); | ||
27 | } | ||
28 | |||
29 | void autofs_update_usage(struct autofs_dirhash *dh, | ||
30 | struct autofs_dir_ent *ent) | ||
31 | { | ||
32 | autofs_delete_usage(ent); /* Unlink from current position */ | ||
33 | autofs_init_usage(dh,ent); /* Relink at queue tail */ | ||
34 | } | ||
35 | |||
36 | struct autofs_dir_ent *autofs_expire(struct super_block *sb, | ||
37 | struct autofs_sb_info *sbi, | ||
38 | struct vfsmount *mnt) | ||
39 | { | ||
40 | struct autofs_dirhash *dh = &sbi->dirhash; | ||
41 | struct autofs_dir_ent *ent; | ||
42 | struct dentry *dentry; | ||
43 | unsigned long timeout = sbi->exp_timeout; | ||
44 | |||
45 | while (1) { | ||
46 | if ( list_empty(&dh->expiry_head) || sbi->catatonic ) | ||
47 | return NULL; /* No entries */ | ||
48 | /* We keep the list sorted by last_usage and want old stuff */ | ||
49 | ent = list_entry(dh->expiry_head.next, struct autofs_dir_ent, exp); | ||
50 | if (jiffies - ent->last_usage < timeout) | ||
51 | break; | ||
52 | /* Move to end of list in case expiry isn't desirable */ | ||
53 | autofs_update_usage(dh, ent); | ||
54 | |||
55 | /* Check to see that entry is expirable */ | ||
56 | if ( ent->ino < AUTOFS_FIRST_DIR_INO ) | ||
57 | return ent; /* Symlinks are always expirable */ | ||
58 | |||
59 | /* Get the dentry for the autofs subdirectory */ | ||
60 | dentry = ent->dentry; | ||
61 | |||
62 | if ( !dentry ) { | ||
63 | /* Should only happen in catatonic mode */ | ||
64 | printk("autofs: dentry == NULL but inode range is directory, entry %s\n", ent->name); | ||
65 | autofs_delete_usage(ent); | ||
66 | continue; | ||
67 | } | ||
68 | |||
69 | if ( !dentry->d_inode ) { | ||
70 | dput(dentry); | ||
71 | printk("autofs: negative dentry on expiry queue: %s\n", | ||
72 | ent->name); | ||
73 | autofs_delete_usage(ent); | ||
74 | continue; | ||
75 | } | ||
76 | |||
77 | /* Make sure entry is mounted and unused; note that dentry will | ||
78 | point to the mounted-on-top root. */ | ||
79 | if (!S_ISDIR(dentry->d_inode->i_mode)||!d_mountpoint(dentry)) { | ||
80 | DPRINTK(("autofs: not expirable (not a mounted directory): %s\n", ent->name)); | ||
81 | continue; | ||
82 | } | ||
83 | mntget(mnt); | ||
84 | dget(dentry); | ||
85 | if (!follow_down(&mnt, &dentry)) { | ||
86 | dput(dentry); | ||
87 | mntput(mnt); | ||
88 | DPRINTK(("autofs: not expirable (not a mounted directory): %s\n", ent->name)); | ||
89 | continue; | ||
90 | } | ||
91 | while (d_mountpoint(dentry) && follow_down(&mnt, &dentry)) | ||
92 | ; | ||
93 | dput(dentry); | ||
94 | |||
95 | if ( may_umount(mnt) == 0 ) { | ||
96 | mntput(mnt); | ||
97 | DPRINTK(("autofs: signaling expire on %s\n", ent->name)); | ||
98 | return ent; /* Expirable! */ | ||
99 | } | ||
100 | DPRINTK(("autofs: didn't expire due to may_umount: %s\n", ent->name)); | ||
101 | mntput(mnt); | ||
102 | } | ||
103 | return NULL; /* No expirable entries */ | ||
104 | } | ||
105 | |||
106 | void autofs_initialize_hash(struct autofs_dirhash *dh) { | ||
107 | memset(&dh->h, 0, AUTOFS_HASH_SIZE*sizeof(struct autofs_dir_ent *)); | ||
108 | INIT_LIST_HEAD(&dh->expiry_head); | ||
109 | } | ||
110 | |||
111 | struct autofs_dir_ent *autofs_hash_lookup(const struct autofs_dirhash *dh, struct qstr *name) | ||
112 | { | ||
113 | struct autofs_dir_ent *dhn; | ||
114 | |||
115 | DPRINTK(("autofs_hash_lookup: hash = 0x%08x, name = ", name->hash)); | ||
116 | autofs_say(name->name,name->len); | ||
117 | |||
118 | for ( dhn = dh->h[(unsigned) name->hash % AUTOFS_HASH_SIZE] ; dhn ; dhn = dhn->next ) { | ||
119 | if ( name->hash == dhn->hash && | ||
120 | name->len == dhn->len && | ||
121 | !memcmp(name->name, dhn->name, name->len) ) | ||
122 | break; | ||
123 | } | ||
124 | |||
125 | return dhn; | ||
126 | } | ||
127 | |||
128 | void autofs_hash_insert(struct autofs_dirhash *dh, struct autofs_dir_ent *ent) | ||
129 | { | ||
130 | struct autofs_dir_ent **dhnp; | ||
131 | |||
132 | DPRINTK(("autofs_hash_insert: hash = 0x%08x, name = ", ent->hash)); | ||
133 | autofs_say(ent->name,ent->len); | ||
134 | |||
135 | autofs_init_usage(dh,ent); | ||
136 | if (ent->dentry) | ||
137 | dget(ent->dentry); | ||
138 | |||
139 | dhnp = &dh->h[(unsigned) ent->hash % AUTOFS_HASH_SIZE]; | ||
140 | ent->next = *dhnp; | ||
141 | ent->back = dhnp; | ||
142 | *dhnp = ent; | ||
143 | if ( ent->next ) | ||
144 | ent->next->back = &(ent->next); | ||
145 | } | ||
146 | |||
147 | void autofs_hash_delete(struct autofs_dir_ent *ent) | ||
148 | { | ||
149 | *(ent->back) = ent->next; | ||
150 | if ( ent->next ) | ||
151 | ent->next->back = ent->back; | ||
152 | |||
153 | autofs_delete_usage(ent); | ||
154 | |||
155 | if ( ent->dentry ) | ||
156 | dput(ent->dentry); | ||
157 | kfree(ent->name); | ||
158 | kfree(ent); | ||
159 | } | ||
160 | |||
161 | /* | ||
162 | * Used by readdir(). We must validate "ptr", so we can't simply make it | ||
163 | * a pointer. Values below 0xffff are reserved; calling with any value | ||
164 | * <= 0x10000 will return the first entry found. | ||
165 | * | ||
166 | * "last" can be NULL or the value returned by the last search *if* we | ||
167 | * want the next sequential entry. | ||
168 | */ | ||
169 | struct autofs_dir_ent *autofs_hash_enum(const struct autofs_dirhash *dh, | ||
170 | off_t *ptr, struct autofs_dir_ent *last) | ||
171 | { | ||
172 | int bucket, ecount, i; | ||
173 | struct autofs_dir_ent *ent; | ||
174 | |||
175 | bucket = (*ptr >> 16) - 1; | ||
176 | ecount = *ptr & 0xffff; | ||
177 | |||
178 | if ( bucket < 0 ) { | ||
179 | bucket = ecount = 0; | ||
180 | } | ||
181 | |||
182 | DPRINTK(("autofs_hash_enum: bucket %d, entry %d\n", bucket, ecount)); | ||
183 | |||
184 | ent = last ? last->next : NULL; | ||
185 | |||
186 | if ( ent ) { | ||
187 | ecount++; | ||
188 | } else { | ||
189 | while ( bucket < AUTOFS_HASH_SIZE ) { | ||
190 | ent = dh->h[bucket]; | ||
191 | for ( i = ecount ; ent && i ; i-- ) | ||
192 | ent = ent->next; | ||
193 | |||
194 | if (ent) { | ||
195 | ecount++; /* Point to *next* entry */ | ||
196 | break; | ||
197 | } | ||
198 | |||
199 | bucket++; ecount = 0; | ||
200 | } | ||
201 | } | ||
202 | |||
203 | #ifdef DEBUG | ||
204 | if ( !ent ) | ||
205 | printk("autofs_hash_enum: nothing found\n"); | ||
206 | else { | ||
207 | printk("autofs_hash_enum: found hash %08x, name", ent->hash); | ||
208 | autofs_say(ent->name,ent->len); | ||
209 | } | ||
210 | #endif | ||
211 | |||
212 | *ptr = ((bucket+1) << 16) + ecount; | ||
213 | return ent; | ||
214 | } | ||
215 | |||
216 | /* Iterate over all the ents, and remove all dentry pointers. Used on | ||
217 | entering catatonic mode, in order to make the filesystem unmountable. */ | ||
218 | void autofs_hash_dputall(struct autofs_dirhash *dh) | ||
219 | { | ||
220 | int i; | ||
221 | struct autofs_dir_ent *ent; | ||
222 | |||
223 | for ( i = 0 ; i < AUTOFS_HASH_SIZE ; i++ ) { | ||
224 | for ( ent = dh->h[i] ; ent ; ent = ent->next ) { | ||
225 | if ( ent->dentry ) { | ||
226 | dput(ent->dentry); | ||
227 | ent->dentry = NULL; | ||
228 | } | ||
229 | } | ||
230 | } | ||
231 | } | ||
232 | |||
233 | /* Delete everything. This is used on filesystem destruction, so we | ||
234 | make no attempt to keep the pointers valid */ | ||
235 | void autofs_hash_nuke(struct autofs_dirhash *dh) | ||
236 | { | ||
237 | int i; | ||
238 | struct autofs_dir_ent *ent, *nent; | ||
239 | |||
240 | for ( i = 0 ; i < AUTOFS_HASH_SIZE ; i++ ) { | ||
241 | for ( ent = dh->h[i] ; ent ; ent = nent ) { | ||
242 | nent = ent->next; | ||
243 | if ( ent->dentry ) | ||
244 | dput(ent->dentry); | ||
245 | kfree(ent->name); | ||
246 | kfree(ent); | ||
247 | } | ||
248 | } | ||
249 | } | ||
diff --git a/fs/autofs/init.c b/fs/autofs/init.c new file mode 100644 index 000000000000..b977ece69f0c --- /dev/null +++ b/fs/autofs/init.c | |||
@@ -0,0 +1,52 @@ | |||
1 | /* -*- linux-c -*- --------------------------------------------------------- * | ||
2 | * | ||
3 | * linux/fs/autofs/init.c | ||
4 | * | ||
5 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved | ||
6 | * | ||
7 | * This file is part of the Linux kernel and is made available under | ||
8 | * the terms of the GNU General Public License, version 2, or at your | ||
9 | * option, any later version, incorporated herein by reference. | ||
10 | * | ||
11 | * ------------------------------------------------------------------------- */ | ||
12 | |||
13 | #include <linux/module.h> | ||
14 | #include <linux/init.h> | ||
15 | #include "autofs_i.h" | ||
16 | |||
17 | static struct super_block *autofs_get_sb(struct file_system_type *fs_type, | ||
18 | int flags, const char *dev_name, void *data) | ||
19 | { | ||
20 | return get_sb_nodev(fs_type, flags, data, autofs_fill_super); | ||
21 | } | ||
22 | |||
23 | static struct file_system_type autofs_fs_type = { | ||
24 | .owner = THIS_MODULE, | ||
25 | .name = "autofs", | ||
26 | .get_sb = autofs_get_sb, | ||
27 | .kill_sb = kill_anon_super, | ||
28 | }; | ||
29 | |||
30 | static int __init init_autofs_fs(void) | ||
31 | { | ||
32 | return register_filesystem(&autofs_fs_type); | ||
33 | } | ||
34 | |||
35 | static void __exit exit_autofs_fs(void) | ||
36 | { | ||
37 | unregister_filesystem(&autofs_fs_type); | ||
38 | } | ||
39 | |||
40 | module_init(init_autofs_fs); | ||
41 | module_exit(exit_autofs_fs); | ||
42 | |||
43 | #ifdef DEBUG | ||
44 | void autofs_say(const char *name, int len) | ||
45 | { | ||
46 | printk("(%d: ", len); | ||
47 | while ( len-- ) | ||
48 | printk("%c", *name++); | ||
49 | printk(")\n"); | ||
50 | } | ||
51 | #endif | ||
52 | MODULE_LICENSE("GPL"); | ||
diff --git a/fs/autofs/inode.c b/fs/autofs/inode.c new file mode 100644 index 000000000000..4888c1fabbf7 --- /dev/null +++ b/fs/autofs/inode.c | |||
@@ -0,0 +1,250 @@ | |||
1 | /* -*- linux-c -*- --------------------------------------------------------- * | ||
2 | * | ||
3 | * linux/fs/autofs/inode.c | ||
4 | * | ||
5 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved | ||
6 | * | ||
7 | * This file is part of the Linux kernel and is made available under | ||
8 | * the terms of the GNU General Public License, version 2, or at your | ||
9 | * option, any later version, incorporated herein by reference. | ||
10 | * | ||
11 | * ------------------------------------------------------------------------- */ | ||
12 | |||
13 | #include <linux/kernel.h> | ||
14 | #include <linux/mm.h> | ||
15 | #include <linux/slab.h> | ||
16 | #include <linux/file.h> | ||
17 | #include <linux/parser.h> | ||
18 | #include <linux/bitops.h> | ||
19 | #include "autofs_i.h" | ||
20 | #include <linux/module.h> | ||
21 | |||
22 | static void autofs_put_super(struct super_block *sb) | ||
23 | { | ||
24 | struct autofs_sb_info *sbi = autofs_sbi(sb); | ||
25 | unsigned int n; | ||
26 | |||
27 | if ( !sbi->catatonic ) | ||
28 | autofs_catatonic_mode(sbi); /* Free wait queues, close pipe */ | ||
29 | |||
30 | autofs_hash_nuke(&sbi->dirhash); | ||
31 | for ( n = 0 ; n < AUTOFS_MAX_SYMLINKS ; n++ ) { | ||
32 | if ( test_bit(n, sbi->symlink_bitmap) ) | ||
33 | kfree(sbi->symlink[n].data); | ||
34 | } | ||
35 | |||
36 | kfree(sb->s_fs_info); | ||
37 | |||
38 | DPRINTK(("autofs: shutting down\n")); | ||
39 | } | ||
40 | |||
41 | static void autofs_read_inode(struct inode *inode); | ||
42 | |||
43 | static struct super_operations autofs_sops = { | ||
44 | .read_inode = autofs_read_inode, | ||
45 | .put_super = autofs_put_super, | ||
46 | .statfs = simple_statfs, | ||
47 | }; | ||
48 | |||
49 | enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto}; | ||
50 | |||
51 | static match_table_t autofs_tokens = { | ||
52 | {Opt_fd, "fd=%u"}, | ||
53 | {Opt_uid, "uid=%u"}, | ||
54 | {Opt_gid, "gid=%u"}, | ||
55 | {Opt_pgrp, "pgrp=%u"}, | ||
56 | {Opt_minproto, "minproto=%u"}, | ||
57 | {Opt_maxproto, "maxproto=%u"}, | ||
58 | {Opt_err, NULL} | ||
59 | }; | ||
60 | |||
61 | static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid, pid_t *pgrp, int *minproto, int *maxproto) | ||
62 | { | ||
63 | char *p; | ||
64 | substring_t args[MAX_OPT_ARGS]; | ||
65 | int option; | ||
66 | |||
67 | *uid = current->uid; | ||
68 | *gid = current->gid; | ||
69 | *pgrp = process_group(current); | ||
70 | |||
71 | *minproto = *maxproto = AUTOFS_PROTO_VERSION; | ||
72 | |||
73 | *pipefd = -1; | ||
74 | |||
75 | if (!options) | ||
76 | return 1; | ||
77 | |||
78 | while ((p = strsep(&options, ",")) != NULL) { | ||
79 | int token; | ||
80 | if (!*p) | ||
81 | continue; | ||
82 | |||
83 | token = match_token(p, autofs_tokens, args); | ||
84 | switch (token) { | ||
85 | case Opt_fd: | ||
86 | if (match_int(&args[0], &option)) | ||
87 | return 1; | ||
88 | *pipefd = option; | ||
89 | break; | ||
90 | case Opt_uid: | ||
91 | if (match_int(&args[0], &option)) | ||
92 | return 1; | ||
93 | *uid = option; | ||
94 | break; | ||
95 | case Opt_gid: | ||
96 | if (match_int(&args[0], &option)) | ||
97 | return 1; | ||
98 | *gid = option; | ||
99 | break; | ||
100 | case Opt_pgrp: | ||
101 | if (match_int(&args[0], &option)) | ||
102 | return 1; | ||
103 | *pgrp = option; | ||
104 | break; | ||
105 | case Opt_minproto: | ||
106 | if (match_int(&args[0], &option)) | ||
107 | return 1; | ||
108 | *minproto = option; | ||
109 | break; | ||
110 | case Opt_maxproto: | ||
111 | if (match_int(&args[0], &option)) | ||
112 | return 1; | ||
113 | *maxproto = option; | ||
114 | break; | ||
115 | default: | ||
116 | return 1; | ||
117 | } | ||
118 | } | ||
119 | return (*pipefd < 0); | ||
120 | } | ||
121 | |||
122 | int autofs_fill_super(struct super_block *s, void *data, int silent) | ||
123 | { | ||
124 | struct inode * root_inode; | ||
125 | struct dentry * root; | ||
126 | struct file * pipe; | ||
127 | int pipefd; | ||
128 | struct autofs_sb_info *sbi; | ||
129 | int minproto, maxproto; | ||
130 | |||
131 | sbi = kmalloc(sizeof(*sbi), GFP_KERNEL); | ||
132 | if ( !sbi ) | ||
133 | goto fail_unlock; | ||
134 | memset(sbi, 0, sizeof(*sbi)); | ||
135 | DPRINTK(("autofs: starting up, sbi = %p\n",sbi)); | ||
136 | |||
137 | s->s_fs_info = sbi; | ||
138 | sbi->magic = AUTOFS_SBI_MAGIC; | ||
139 | sbi->catatonic = 0; | ||
140 | sbi->exp_timeout = 0; | ||
141 | sbi->oz_pgrp = process_group(current); | ||
142 | autofs_initialize_hash(&sbi->dirhash); | ||
143 | sbi->queues = NULL; | ||
144 | memset(sbi->symlink_bitmap, 0, sizeof(long)*AUTOFS_SYMLINK_BITMAP_LEN); | ||
145 | sbi->next_dir_ino = AUTOFS_FIRST_DIR_INO; | ||
146 | s->s_blocksize = 1024; | ||
147 | s->s_blocksize_bits = 10; | ||
148 | s->s_magic = AUTOFS_SUPER_MAGIC; | ||
149 | s->s_op = &autofs_sops; | ||
150 | s->s_time_gran = 1; | ||
151 | |||
152 | root_inode = iget(s, AUTOFS_ROOT_INO); | ||
153 | root = d_alloc_root(root_inode); | ||
154 | pipe = NULL; | ||
155 | |||
156 | if (!root) | ||
157 | goto fail_iput; | ||
158 | |||
159 | /* Can this call block? - WTF cares? s is locked. */ | ||
160 | if ( parse_options(data,&pipefd,&root_inode->i_uid,&root_inode->i_gid,&sbi->oz_pgrp,&minproto,&maxproto) ) { | ||
161 | printk("autofs: called with bogus options\n"); | ||
162 | goto fail_dput; | ||
163 | } | ||
164 | |||
165 | /* Couldn't this be tested earlier? */ | ||
166 | if ( minproto > AUTOFS_PROTO_VERSION || | ||
167 | maxproto < AUTOFS_PROTO_VERSION ) { | ||
168 | printk("autofs: kernel does not match daemon version\n"); | ||
169 | goto fail_dput; | ||
170 | } | ||
171 | |||
172 | DPRINTK(("autofs: pipe fd = %d, pgrp = %u\n", pipefd, sbi->oz_pgrp)); | ||
173 | pipe = fget(pipefd); | ||
174 | |||
175 | if ( !pipe ) { | ||
176 | printk("autofs: could not open pipe file descriptor\n"); | ||
177 | goto fail_dput; | ||
178 | } | ||
179 | if ( !pipe->f_op || !pipe->f_op->write ) | ||
180 | goto fail_fput; | ||
181 | sbi->pipe = pipe; | ||
182 | |||
183 | /* | ||
184 | * Success! Install the root dentry now to indicate completion. | ||
185 | */ | ||
186 | s->s_root = root; | ||
187 | return 0; | ||
188 | |||
189 | fail_fput: | ||
190 | printk("autofs: pipe file descriptor does not contain proper ops\n"); | ||
191 | fput(pipe); | ||
192 | fail_dput: | ||
193 | dput(root); | ||
194 | goto fail_free; | ||
195 | fail_iput: | ||
196 | printk("autofs: get root dentry failed\n"); | ||
197 | iput(root_inode); | ||
198 | fail_free: | ||
199 | kfree(sbi); | ||
200 | fail_unlock: | ||
201 | return -EINVAL; | ||
202 | } | ||
203 | |||
204 | static void autofs_read_inode(struct inode *inode) | ||
205 | { | ||
206 | ino_t ino = inode->i_ino; | ||
207 | unsigned int n; | ||
208 | struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb); | ||
209 | |||
210 | /* Initialize to the default case (stub directory) */ | ||
211 | |||
212 | inode->i_op = &simple_dir_inode_operations; | ||
213 | inode->i_fop = &simple_dir_operations; | ||
214 | inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO; | ||
215 | inode->i_nlink = 2; | ||
216 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; | ||
217 | inode->i_blocks = 0; | ||
218 | inode->i_blksize = 1024; | ||
219 | |||
220 | if ( ino == AUTOFS_ROOT_INO ) { | ||
221 | inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR; | ||
222 | inode->i_op = &autofs_root_inode_operations; | ||
223 | inode->i_fop = &autofs_root_operations; | ||
224 | inode->i_uid = inode->i_gid = 0; /* Changed in read_super */ | ||
225 | return; | ||
226 | } | ||
227 | |||
228 | inode->i_uid = inode->i_sb->s_root->d_inode->i_uid; | ||
229 | inode->i_gid = inode->i_sb->s_root->d_inode->i_gid; | ||
230 | |||
231 | if ( ino >= AUTOFS_FIRST_SYMLINK && ino < AUTOFS_FIRST_DIR_INO ) { | ||
232 | /* Symlink inode - should be in symlink list */ | ||
233 | struct autofs_symlink *sl; | ||
234 | |||
235 | n = ino - AUTOFS_FIRST_SYMLINK; | ||
236 | if ( n >= AUTOFS_MAX_SYMLINKS || !test_bit(n,sbi->symlink_bitmap)) { | ||
237 | printk("autofs: Looking for bad symlink inode %u\n", (unsigned int) ino); | ||
238 | return; | ||
239 | } | ||
240 | |||
241 | inode->i_op = &autofs_symlink_inode_operations; | ||
242 | sl = &sbi->symlink[n]; | ||
243 | inode->u.generic_ip = sl; | ||
244 | inode->i_mode = S_IFLNK | S_IRWXUGO; | ||
245 | inode->i_mtime.tv_sec = inode->i_ctime.tv_sec = sl->mtime; | ||
246 | inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0; | ||
247 | inode->i_size = sl->len; | ||
248 | inode->i_nlink = 1; | ||
249 | } | ||
250 | } | ||
diff --git a/fs/autofs/root.c b/fs/autofs/root.c new file mode 100644 index 000000000000..a1ab1c0ed215 --- /dev/null +++ b/fs/autofs/root.c | |||
@@ -0,0 +1,564 @@ | |||
1 | /* -*- linux-c -*- --------------------------------------------------------- * | ||
2 | * | ||
3 | * linux/fs/autofs/root.c | ||
4 | * | ||
5 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved | ||
6 | * | ||
7 | * This file is part of the Linux kernel and is made available under | ||
8 | * the terms of the GNU General Public License, version 2, or at your | ||
9 | * option, any later version, incorporated herein by reference. | ||
10 | * | ||
11 | * ------------------------------------------------------------------------- */ | ||
12 | |||
13 | #include <linux/errno.h> | ||
14 | #include <linux/stat.h> | ||
15 | #include <linux/param.h> | ||
16 | #include <linux/time.h> | ||
17 | #include <linux/smp_lock.h> | ||
18 | #include "autofs_i.h" | ||
19 | |||
20 | static int autofs_root_readdir(struct file *,void *,filldir_t); | ||
21 | static struct dentry *autofs_root_lookup(struct inode *,struct dentry *, struct nameidata *); | ||
22 | static int autofs_root_symlink(struct inode *,struct dentry *,const char *); | ||
23 | static int autofs_root_unlink(struct inode *,struct dentry *); | ||
24 | static int autofs_root_rmdir(struct inode *,struct dentry *); | ||
25 | static int autofs_root_mkdir(struct inode *,struct dentry *,int); | ||
26 | static int autofs_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long); | ||
27 | |||
28 | struct file_operations autofs_root_operations = { | ||
29 | .read = generic_read_dir, | ||
30 | .readdir = autofs_root_readdir, | ||
31 | .ioctl = autofs_root_ioctl, | ||
32 | }; | ||
33 | |||
34 | struct inode_operations autofs_root_inode_operations = { | ||
35 | .lookup = autofs_root_lookup, | ||
36 | .unlink = autofs_root_unlink, | ||
37 | .symlink = autofs_root_symlink, | ||
38 | .mkdir = autofs_root_mkdir, | ||
39 | .rmdir = autofs_root_rmdir, | ||
40 | }; | ||
41 | |||
42 | static int autofs_root_readdir(struct file *filp, void *dirent, filldir_t filldir) | ||
43 | { | ||
44 | struct autofs_dir_ent *ent = NULL; | ||
45 | struct autofs_dirhash *dirhash; | ||
46 | struct autofs_sb_info *sbi; | ||
47 | struct inode * inode = filp->f_dentry->d_inode; | ||
48 | off_t onr, nr; | ||
49 | |||
50 | lock_kernel(); | ||
51 | |||
52 | sbi = autofs_sbi(inode->i_sb); | ||
53 | dirhash = &sbi->dirhash; | ||
54 | nr = filp->f_pos; | ||
55 | |||
56 | switch(nr) | ||
57 | { | ||
58 | case 0: | ||
59 | if (filldir(dirent, ".", 1, nr, inode->i_ino, DT_DIR) < 0) | ||
60 | goto out; | ||
61 | filp->f_pos = ++nr; | ||
62 | /* fall through */ | ||
63 | case 1: | ||
64 | if (filldir(dirent, "..", 2, nr, inode->i_ino, DT_DIR) < 0) | ||
65 | goto out; | ||
66 | filp->f_pos = ++nr; | ||
67 | /* fall through */ | ||
68 | default: | ||
69 | while ( onr = nr, ent = autofs_hash_enum(dirhash,&nr,ent) ) { | ||
70 | if ( !ent->dentry || d_mountpoint(ent->dentry) ) { | ||
71 | if (filldir(dirent,ent->name,ent->len,onr,ent->ino,DT_UNKNOWN) < 0) | ||
72 | goto out; | ||
73 | filp->f_pos = nr; | ||
74 | } | ||
75 | } | ||
76 | break; | ||
77 | } | ||
78 | |||
79 | out: | ||
80 | unlock_kernel(); | ||
81 | return 0; | ||
82 | } | ||
83 | |||
84 | static int try_to_fill_dentry(struct dentry *dentry, struct super_block *sb, struct autofs_sb_info *sbi) | ||
85 | { | ||
86 | struct inode * inode; | ||
87 | struct autofs_dir_ent *ent; | ||
88 | int status = 0; | ||
89 | |||
90 | if ( !(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name)) ) { | ||
91 | do { | ||
92 | if ( status && dentry->d_inode ) { | ||
93 | if ( status != -ENOENT ) | ||
94 | printk("autofs warning: lookup failure on positive dentry, status = %d, name = %s\n", status, dentry->d_name.name); | ||
95 | return 0; /* Try to get the kernel to invalidate this dentry */ | ||
96 | } | ||
97 | |||
98 | /* Turn this into a real negative dentry? */ | ||
99 | if (status == -ENOENT) { | ||
100 | dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT; | ||
101 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; | ||
102 | return 1; | ||
103 | } else if (status) { | ||
104 | /* Return a negative dentry, but leave it "pending" */ | ||
105 | return 1; | ||
106 | } | ||
107 | status = autofs_wait(sbi, &dentry->d_name); | ||
108 | } while (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name)) ); | ||
109 | } | ||
110 | |||
111 | /* Abuse this field as a pointer to the directory entry, used to | ||
112 | find the expire list pointers */ | ||
113 | dentry->d_time = (unsigned long) ent; | ||
114 | |||
115 | if (!dentry->d_inode) { | ||
116 | inode = iget(sb, ent->ino); | ||
117 | if (!inode) { | ||
118 | /* Failed, but leave pending for next time */ | ||
119 | return 1; | ||
120 | } | ||
121 | dentry->d_inode = inode; | ||
122 | } | ||
123 | |||
124 | /* If this is a directory that isn't a mount point, bitch at the | ||
125 | daemon and fix it in user space */ | ||
126 | if ( S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry) ) { | ||
127 | return !autofs_wait(sbi, &dentry->d_name); | ||
128 | } | ||
129 | |||
130 | /* We don't update the usages for the autofs daemon itself, this | ||
131 | is necessary for recursive autofs mounts */ | ||
132 | if ( !autofs_oz_mode(sbi) ) { | ||
133 | autofs_update_usage(&sbi->dirhash,ent); | ||
134 | } | ||
135 | |||
136 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; | ||
137 | return 1; | ||
138 | } | ||
139 | |||
140 | |||
141 | /* | ||
142 | * Revalidate is called on every cache lookup. Some of those | ||
143 | * cache lookups may actually happen while the dentry is not | ||
144 | * yet completely filled in, and revalidate has to delay such | ||
145 | * lookups.. | ||
146 | */ | ||
147 | static int autofs_revalidate(struct dentry * dentry, struct nameidata *nd) | ||
148 | { | ||
149 | struct inode * dir; | ||
150 | struct autofs_sb_info *sbi; | ||
151 | struct autofs_dir_ent *ent; | ||
152 | int res; | ||
153 | |||
154 | lock_kernel(); | ||
155 | dir = dentry->d_parent->d_inode; | ||
156 | sbi = autofs_sbi(dir->i_sb); | ||
157 | |||
158 | /* Pending dentry */ | ||
159 | if ( dentry->d_flags & DCACHE_AUTOFS_PENDING ) { | ||
160 | if (autofs_oz_mode(sbi)) | ||
161 | res = 1; | ||
162 | else | ||
163 | res = try_to_fill_dentry(dentry, dir->i_sb, sbi); | ||
164 | unlock_kernel(); | ||
165 | return res; | ||
166 | } | ||
167 | |||
168 | /* Negative dentry.. invalidate if "old" */ | ||
169 | if (!dentry->d_inode) { | ||
170 | unlock_kernel(); | ||
171 | return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT); | ||
172 | } | ||
173 | |||
174 | /* Check for a non-mountpoint directory */ | ||
175 | if ( S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry) ) { | ||
176 | if (autofs_oz_mode(sbi)) | ||
177 | res = 1; | ||
178 | else | ||
179 | res = try_to_fill_dentry(dentry, dir->i_sb, sbi); | ||
180 | unlock_kernel(); | ||
181 | return res; | ||
182 | } | ||
183 | |||
184 | /* Update the usage list */ | ||
185 | if ( !autofs_oz_mode(sbi) ) { | ||
186 | ent = (struct autofs_dir_ent *) dentry->d_time; | ||
187 | if ( ent ) | ||
188 | autofs_update_usage(&sbi->dirhash,ent); | ||
189 | } | ||
190 | unlock_kernel(); | ||
191 | return 1; | ||
192 | } | ||
193 | |||
194 | static struct dentry_operations autofs_dentry_operations = { | ||
195 | .d_revalidate = autofs_revalidate, | ||
196 | }; | ||
197 | |||
198 | static struct dentry *autofs_root_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) | ||
199 | { | ||
200 | struct autofs_sb_info *sbi; | ||
201 | int oz_mode; | ||
202 | |||
203 | DPRINTK(("autofs_root_lookup: name = ")); | ||
204 | lock_kernel(); | ||
205 | autofs_say(dentry->d_name.name,dentry->d_name.len); | ||
206 | |||
207 | if (dentry->d_name.len > NAME_MAX) { | ||
208 | unlock_kernel(); | ||
209 | return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */ | ||
210 | } | ||
211 | |||
212 | sbi = autofs_sbi(dir->i_sb); | ||
213 | |||
214 | oz_mode = autofs_oz_mode(sbi); | ||
215 | DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d\n", | ||
216 | current->pid, process_group(current), sbi->catatonic, oz_mode)); | ||
217 | |||
218 | /* | ||
219 | * Mark the dentry incomplete, but add it. This is needed so | ||
220 | * that the VFS layer knows about the dentry, and we can count | ||
221 | * on catching any lookups through the revalidate. | ||
222 | * | ||
223 | * Let all the hard work be done by the revalidate function that | ||
224 | * needs to be able to do this anyway.. | ||
225 | * | ||
226 | * We need to do this before we release the directory semaphore. | ||
227 | */ | ||
228 | dentry->d_op = &autofs_dentry_operations; | ||
229 | dentry->d_flags |= DCACHE_AUTOFS_PENDING; | ||
230 | d_add(dentry, NULL); | ||
231 | |||
232 | up(&dir->i_sem); | ||
233 | autofs_revalidate(dentry, nd); | ||
234 | down(&dir->i_sem); | ||
235 | |||
236 | /* | ||
237 | * If we are still pending, check if we had to handle | ||
238 | * a signal. If so we can force a restart.. | ||
239 | */ | ||
240 | if (dentry->d_flags & DCACHE_AUTOFS_PENDING) { | ||
241 | /* See if we were interrupted */ | ||
242 | if (signal_pending(current)) { | ||
243 | sigset_t *sigset = ¤t->pending.signal; | ||
244 | if (sigismember (sigset, SIGKILL) || | ||
245 | sigismember (sigset, SIGQUIT) || | ||
246 | sigismember (sigset, SIGINT)) { | ||
247 | unlock_kernel(); | ||
248 | return ERR_PTR(-ERESTARTNOINTR); | ||
249 | } | ||
250 | } | ||
251 | } | ||
252 | unlock_kernel(); | ||
253 | |||
254 | /* | ||
255 | * If this dentry is unhashed, then we shouldn't honour this | ||
256 | * lookup even if the dentry is positive. Returning ENOENT here | ||
257 | * doesn't do the right thing for all system calls, but it should | ||
258 | * be OK for the operations we permit from an autofs. | ||
259 | */ | ||
260 | if ( dentry->d_inode && d_unhashed(dentry) ) | ||
261 | return ERR_PTR(-ENOENT); | ||
262 | |||
263 | return NULL; | ||
264 | } | ||
265 | |||
266 | static int autofs_root_symlink(struct inode *dir, struct dentry *dentry, const char *symname) | ||
267 | { | ||
268 | struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb); | ||
269 | struct autofs_dirhash *dh = &sbi->dirhash; | ||
270 | struct autofs_dir_ent *ent; | ||
271 | unsigned int n; | ||
272 | int slsize; | ||
273 | struct autofs_symlink *sl; | ||
274 | |||
275 | DPRINTK(("autofs_root_symlink: %s <- ", symname)); | ||
276 | autofs_say(dentry->d_name.name,dentry->d_name.len); | ||
277 | |||
278 | lock_kernel(); | ||
279 | if ( !autofs_oz_mode(sbi) ) { | ||
280 | unlock_kernel(); | ||
281 | return -EACCES; | ||
282 | } | ||
283 | |||
284 | if ( autofs_hash_lookup(dh, &dentry->d_name) ) { | ||
285 | unlock_kernel(); | ||
286 | return -EEXIST; | ||
287 | } | ||
288 | |||
289 | n = find_first_zero_bit(sbi->symlink_bitmap,AUTOFS_MAX_SYMLINKS); | ||
290 | if ( n >= AUTOFS_MAX_SYMLINKS ) { | ||
291 | unlock_kernel(); | ||
292 | return -ENOSPC; | ||
293 | } | ||
294 | |||
295 | set_bit(n,sbi->symlink_bitmap); | ||
296 | sl = &sbi->symlink[n]; | ||
297 | sl->len = strlen(symname); | ||
298 | sl->data = kmalloc(slsize = sl->len+1, GFP_KERNEL); | ||
299 | if ( !sl->data ) { | ||
300 | clear_bit(n,sbi->symlink_bitmap); | ||
301 | unlock_kernel(); | ||
302 | return -ENOSPC; | ||
303 | } | ||
304 | |||
305 | ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL); | ||
306 | if ( !ent ) { | ||
307 | kfree(sl->data); | ||
308 | clear_bit(n,sbi->symlink_bitmap); | ||
309 | unlock_kernel(); | ||
310 | return -ENOSPC; | ||
311 | } | ||
312 | |||
313 | ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL); | ||
314 | if ( !ent->name ) { | ||
315 | kfree(sl->data); | ||
316 | kfree(ent); | ||
317 | clear_bit(n,sbi->symlink_bitmap); | ||
318 | unlock_kernel(); | ||
319 | return -ENOSPC; | ||
320 | } | ||
321 | |||
322 | memcpy(sl->data,symname,slsize); | ||
323 | sl->mtime = get_seconds(); | ||
324 | |||
325 | ent->ino = AUTOFS_FIRST_SYMLINK + n; | ||
326 | ent->hash = dentry->d_name.hash; | ||
327 | memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len)); | ||
328 | ent->dentry = NULL; /* We don't keep the dentry for symlinks */ | ||
329 | |||
330 | autofs_hash_insert(dh,ent); | ||
331 | d_instantiate(dentry, iget(dir->i_sb,ent->ino)); | ||
332 | unlock_kernel(); | ||
333 | return 0; | ||
334 | } | ||
335 | |||
336 | /* | ||
337 | * NOTE! | ||
338 | * | ||
339 | * Normal filesystems would do a "d_delete()" to tell the VFS dcache | ||
340 | * that the file no longer exists. However, doing that means that the | ||
341 | * VFS layer can turn the dentry into a negative dentry, which we | ||
342 | * obviously do not want (we're dropping the entry not because it | ||
343 | * doesn't exist, but because it has timed out). | ||
344 | * | ||
345 | * Also see autofs_root_rmdir().. | ||
346 | */ | ||
347 | static int autofs_root_unlink(struct inode *dir, struct dentry *dentry) | ||
348 | { | ||
349 | struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb); | ||
350 | struct autofs_dirhash *dh = &sbi->dirhash; | ||
351 | struct autofs_dir_ent *ent; | ||
352 | unsigned int n; | ||
353 | |||
354 | /* This allows root to remove symlinks */ | ||
355 | lock_kernel(); | ||
356 | if ( !autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) ) { | ||
357 | unlock_kernel(); | ||
358 | return -EACCES; | ||
359 | } | ||
360 | |||
361 | ent = autofs_hash_lookup(dh, &dentry->d_name); | ||
362 | if ( !ent ) { | ||
363 | unlock_kernel(); | ||
364 | return -ENOENT; | ||
365 | } | ||
366 | |||
367 | n = ent->ino - AUTOFS_FIRST_SYMLINK; | ||
368 | if ( n >= AUTOFS_MAX_SYMLINKS ) { | ||
369 | unlock_kernel(); | ||
370 | return -EISDIR; /* It's a directory, dummy */ | ||
371 | } | ||
372 | if ( !test_bit(n,sbi->symlink_bitmap) ) { | ||
373 | unlock_kernel(); | ||
374 | return -EINVAL; /* Nonexistent symlink? Shouldn't happen */ | ||
375 | } | ||
376 | |||
377 | dentry->d_time = (unsigned long)(struct autofs_dirhash *)NULL; | ||
378 | autofs_hash_delete(ent); | ||
379 | clear_bit(n,sbi->symlink_bitmap); | ||
380 | kfree(sbi->symlink[n].data); | ||
381 | d_drop(dentry); | ||
382 | |||
383 | unlock_kernel(); | ||
384 | return 0; | ||
385 | } | ||
386 | |||
387 | static int autofs_root_rmdir(struct inode *dir, struct dentry *dentry) | ||
388 | { | ||
389 | struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb); | ||
390 | struct autofs_dirhash *dh = &sbi->dirhash; | ||
391 | struct autofs_dir_ent *ent; | ||
392 | |||
393 | lock_kernel(); | ||
394 | if ( !autofs_oz_mode(sbi) ) { | ||
395 | unlock_kernel(); | ||
396 | return -EACCES; | ||
397 | } | ||
398 | |||
399 | ent = autofs_hash_lookup(dh, &dentry->d_name); | ||
400 | if ( !ent ) { | ||
401 | unlock_kernel(); | ||
402 | return -ENOENT; | ||
403 | } | ||
404 | |||
405 | if ( (unsigned int)ent->ino < AUTOFS_FIRST_DIR_INO ) { | ||
406 | unlock_kernel(); | ||
407 | return -ENOTDIR; /* Not a directory */ | ||
408 | } | ||
409 | |||
410 | if ( ent->dentry != dentry ) { | ||
411 | printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry->d_name.name); | ||
412 | } | ||
413 | |||
414 | dentry->d_time = (unsigned long)(struct autofs_dir_ent *)NULL; | ||
415 | autofs_hash_delete(ent); | ||
416 | dir->i_nlink--; | ||
417 | d_drop(dentry); | ||
418 | unlock_kernel(); | ||
419 | |||
420 | return 0; | ||
421 | } | ||
422 | |||
423 | static int autofs_root_mkdir(struct inode *dir, struct dentry *dentry, int mode) | ||
424 | { | ||
425 | struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb); | ||
426 | struct autofs_dirhash *dh = &sbi->dirhash; | ||
427 | struct autofs_dir_ent *ent; | ||
428 | ino_t ino; | ||
429 | |||
430 | lock_kernel(); | ||
431 | if ( !autofs_oz_mode(sbi) ) { | ||
432 | unlock_kernel(); | ||
433 | return -EACCES; | ||
434 | } | ||
435 | |||
436 | ent = autofs_hash_lookup(dh, &dentry->d_name); | ||
437 | if ( ent ) { | ||
438 | unlock_kernel(); | ||
439 | return -EEXIST; | ||
440 | } | ||
441 | |||
442 | if ( sbi->next_dir_ino < AUTOFS_FIRST_DIR_INO ) { | ||
443 | printk("autofs: Out of inode numbers -- what the heck did you do??\n"); | ||
444 | unlock_kernel(); | ||
445 | return -ENOSPC; | ||
446 | } | ||
447 | ino = sbi->next_dir_ino++; | ||
448 | |||
449 | ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL); | ||
450 | if ( !ent ) { | ||
451 | unlock_kernel(); | ||
452 | return -ENOSPC; | ||
453 | } | ||
454 | |||
455 | ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL); | ||
456 | if ( !ent->name ) { | ||
457 | kfree(ent); | ||
458 | unlock_kernel(); | ||
459 | return -ENOSPC; | ||
460 | } | ||
461 | |||
462 | ent->hash = dentry->d_name.hash; | ||
463 | memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len)); | ||
464 | ent->ino = ino; | ||
465 | ent->dentry = dentry; | ||
466 | autofs_hash_insert(dh,ent); | ||
467 | |||
468 | dir->i_nlink++; | ||
469 | d_instantiate(dentry, iget(dir->i_sb,ino)); | ||
470 | unlock_kernel(); | ||
471 | |||
472 | return 0; | ||
473 | } | ||
474 | |||
475 | /* Get/set timeout ioctl() operation */ | ||
476 | static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi, | ||
477 | unsigned long __user *p) | ||
478 | { | ||
479 | unsigned long ntimeout; | ||
480 | |||
481 | if (get_user(ntimeout, p) || | ||
482 | put_user(sbi->exp_timeout / HZ, p)) | ||
483 | return -EFAULT; | ||
484 | |||
485 | if ( ntimeout > ULONG_MAX/HZ ) | ||
486 | sbi->exp_timeout = 0; | ||
487 | else | ||
488 | sbi->exp_timeout = ntimeout * HZ; | ||
489 | |||
490 | return 0; | ||
491 | } | ||
492 | |||
493 | /* Return protocol version */ | ||
494 | static inline int autofs_get_protover(int __user *p) | ||
495 | { | ||
496 | return put_user(AUTOFS_PROTO_VERSION, p); | ||
497 | } | ||
498 | |||
499 | /* Perform an expiry operation */ | ||
500 | static inline int autofs_expire_run(struct super_block *sb, | ||
501 | struct autofs_sb_info *sbi, | ||
502 | struct vfsmount *mnt, | ||
503 | struct autofs_packet_expire __user *pkt_p) | ||
504 | { | ||
505 | struct autofs_dir_ent *ent; | ||
506 | struct autofs_packet_expire pkt; | ||
507 | |||
508 | memset(&pkt,0,sizeof pkt); | ||
509 | |||
510 | pkt.hdr.proto_version = AUTOFS_PROTO_VERSION; | ||
511 | pkt.hdr.type = autofs_ptype_expire; | ||
512 | |||
513 | if ( !sbi->exp_timeout || | ||
514 | !(ent = autofs_expire(sb,sbi,mnt)) ) | ||
515 | return -EAGAIN; | ||
516 | |||
517 | pkt.len = ent->len; | ||
518 | memcpy(pkt.name, ent->name, pkt.len); | ||
519 | pkt.name[pkt.len] = '\0'; | ||
520 | |||
521 | if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) ) | ||
522 | return -EFAULT; | ||
523 | |||
524 | return 0; | ||
525 | } | ||
526 | |||
527 | /* | ||
528 | * ioctl()'s on the root directory is the chief method for the daemon to | ||
529 | * generate kernel reactions | ||
530 | */ | ||
531 | static int autofs_root_ioctl(struct inode *inode, struct file *filp, | ||
532 | unsigned int cmd, unsigned long arg) | ||
533 | { | ||
534 | struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb); | ||
535 | void __user *argp = (void __user *)arg; | ||
536 | |||
537 | DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd,arg,sbi,process_group(current))); | ||
538 | |||
539 | if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) || | ||
540 | _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT ) | ||
541 | return -ENOTTY; | ||
542 | |||
543 | if ( !autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) ) | ||
544 | return -EPERM; | ||
545 | |||
546 | switch(cmd) { | ||
547 | case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */ | ||
548 | return autofs_wait_release(sbi,(autofs_wqt_t)arg,0); | ||
549 | case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */ | ||
550 | return autofs_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT); | ||
551 | case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */ | ||
552 | autofs_catatonic_mode(sbi); | ||
553 | return 0; | ||
554 | case AUTOFS_IOC_PROTOVER: /* Get protocol version */ | ||
555 | return autofs_get_protover(argp); | ||
556 | case AUTOFS_IOC_SETTIMEOUT: | ||
557 | return autofs_get_set_timeout(sbi, argp); | ||
558 | case AUTOFS_IOC_EXPIRE: | ||
559 | return autofs_expire_run(inode->i_sb, sbi, filp->f_vfsmnt, | ||
560 | argp); | ||
561 | default: | ||
562 | return -ENOSYS; | ||
563 | } | ||
564 | } | ||
diff --git a/fs/autofs/symlink.c b/fs/autofs/symlink.c new file mode 100644 index 000000000000..f028396f1383 --- /dev/null +++ b/fs/autofs/symlink.c | |||
@@ -0,0 +1,25 @@ | |||
1 | /* -*- linux-c -*- --------------------------------------------------------- * | ||
2 | * | ||
3 | * linux/fs/autofs/symlink.c | ||
4 | * | ||
5 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved | ||
6 | * | ||
7 | * This file is part of the Linux kernel and is made available under | ||
8 | * the terms of the GNU General Public License, version 2, or at your | ||
9 | * option, any later version, incorporated herein by reference. | ||
10 | * | ||
11 | * ------------------------------------------------------------------------- */ | ||
12 | |||
13 | #include "autofs_i.h" | ||
14 | |||
15 | static int autofs_follow_link(struct dentry *dentry, struct nameidata *nd) | ||
16 | { | ||
17 | char *s=((struct autofs_symlink *)dentry->d_inode->u.generic_ip)->data; | ||
18 | nd_set_link(nd, s); | ||
19 | return 0; | ||
20 | } | ||
21 | |||
22 | struct inode_operations autofs_symlink_inode_operations = { | ||
23 | .readlink = generic_readlink, | ||
24 | .follow_link = autofs_follow_link | ||
25 | }; | ||
diff --git a/fs/autofs/waitq.c b/fs/autofs/waitq.c new file mode 100644 index 000000000000..1fcaa1568541 --- /dev/null +++ b/fs/autofs/waitq.c | |||
@@ -0,0 +1,206 @@ | |||
1 | /* -*- linux-c -*- --------------------------------------------------------- * | ||
2 | * | ||
3 | * linux/fs/autofs/waitq.c | ||
4 | * | ||
5 | * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved | ||
6 | * | ||
7 | * This file is part of the Linux kernel and is made available under | ||
8 | * the terms of the GNU General Public License, version 2, or at your | ||
9 | * option, any later version, incorporated herein by reference. | ||
10 | * | ||
11 | * ------------------------------------------------------------------------- */ | ||
12 | |||
13 | #include <linux/slab.h> | ||
14 | #include <linux/time.h> | ||
15 | #include <linux/signal.h> | ||
16 | #include <linux/file.h> | ||
17 | #include "autofs_i.h" | ||
18 | |||
19 | /* We make this a static variable rather than a part of the superblock; it | ||
20 | is better if we don't reassign numbers easily even across filesystems */ | ||
21 | static autofs_wqt_t autofs_next_wait_queue = 1; | ||
22 | |||
23 | /* These are the signals we allow interrupting a pending mount */ | ||
24 | #define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT)) | ||
25 | |||
26 | void autofs_catatonic_mode(struct autofs_sb_info *sbi) | ||
27 | { | ||
28 | struct autofs_wait_queue *wq, *nwq; | ||
29 | |||
30 | DPRINTK(("autofs: entering catatonic mode\n")); | ||
31 | |||
32 | sbi->catatonic = 1; | ||
33 | wq = sbi->queues; | ||
34 | sbi->queues = NULL; /* Erase all wait queues */ | ||
35 | while ( wq ) { | ||
36 | nwq = wq->next; | ||
37 | wq->status = -ENOENT; /* Magic is gone - report failure */ | ||
38 | kfree(wq->name); | ||
39 | wq->name = NULL; | ||
40 | wake_up(&wq->queue); | ||
41 | wq = nwq; | ||
42 | } | ||
43 | fput(sbi->pipe); /* Close the pipe */ | ||
44 | autofs_hash_dputall(&sbi->dirhash); /* Remove all dentry pointers */ | ||
45 | } | ||
46 | |||
47 | static int autofs_write(struct file *file, const void *addr, int bytes) | ||
48 | { | ||
49 | unsigned long sigpipe, flags; | ||
50 | mm_segment_t fs; | ||
51 | const char *data = (const char *)addr; | ||
52 | ssize_t wr = 0; | ||
53 | |||
54 | /** WARNING: this is not safe for writing more than PIPE_BUF bytes! **/ | ||
55 | |||
56 | sigpipe = sigismember(¤t->pending.signal, SIGPIPE); | ||
57 | |||
58 | /* Save pointer to user space and point back to kernel space */ | ||
59 | fs = get_fs(); | ||
60 | set_fs(KERNEL_DS); | ||
61 | |||
62 | while (bytes && | ||
63 | (wr = file->f_op->write(file,data,bytes,&file->f_pos)) > 0) { | ||
64 | data += wr; | ||
65 | bytes -= wr; | ||
66 | } | ||
67 | |||
68 | set_fs(fs); | ||
69 | |||
70 | /* Keep the currently executing process from receiving a | ||
71 | SIGPIPE unless it was already supposed to get one */ | ||
72 | if (wr == -EPIPE && !sigpipe) { | ||
73 | spin_lock_irqsave(¤t->sighand->siglock, flags); | ||
74 | sigdelset(¤t->pending.signal, SIGPIPE); | ||
75 | recalc_sigpending(); | ||
76 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); | ||
77 | } | ||
78 | |||
79 | return (bytes > 0); | ||
80 | } | ||
81 | |||
82 | static void autofs_notify_daemon(struct autofs_sb_info *sbi, struct autofs_wait_queue *wq) | ||
83 | { | ||
84 | struct autofs_packet_missing pkt; | ||
85 | |||
86 | DPRINTK(("autofs_wait: wait id = 0x%08lx, name = ", wq->wait_queue_token)); | ||
87 | autofs_say(wq->name,wq->len); | ||
88 | |||
89 | memset(&pkt,0,sizeof pkt); /* For security reasons */ | ||
90 | |||
91 | pkt.hdr.proto_version = AUTOFS_PROTO_VERSION; | ||
92 | pkt.hdr.type = autofs_ptype_missing; | ||
93 | pkt.wait_queue_token = wq->wait_queue_token; | ||
94 | pkt.len = wq->len; | ||
95 | memcpy(pkt.name, wq->name, pkt.len); | ||
96 | pkt.name[pkt.len] = '\0'; | ||
97 | |||
98 | if ( autofs_write(sbi->pipe,&pkt,sizeof(struct autofs_packet_missing)) ) | ||
99 | autofs_catatonic_mode(sbi); | ||
100 | } | ||
101 | |||
102 | int autofs_wait(struct autofs_sb_info *sbi, struct qstr *name) | ||
103 | { | ||
104 | struct autofs_wait_queue *wq; | ||
105 | int status; | ||
106 | |||
107 | /* In catatonic mode, we don't wait for nobody */ | ||
108 | if ( sbi->catatonic ) | ||
109 | return -ENOENT; | ||
110 | |||
111 | /* We shouldn't be able to get here, but just in case */ | ||
112 | if ( name->len > NAME_MAX ) | ||
113 | return -ENOENT; | ||
114 | |||
115 | for ( wq = sbi->queues ; wq ; wq = wq->next ) { | ||
116 | if ( wq->hash == name->hash && | ||
117 | wq->len == name->len && | ||
118 | wq->name && !memcmp(wq->name,name->name,name->len) ) | ||
119 | break; | ||
120 | } | ||
121 | |||
122 | if ( !wq ) { | ||
123 | /* Create a new wait queue */ | ||
124 | wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL); | ||
125 | if ( !wq ) | ||
126 | return -ENOMEM; | ||
127 | |||
128 | wq->name = kmalloc(name->len,GFP_KERNEL); | ||
129 | if ( !wq->name ) { | ||
130 | kfree(wq); | ||
131 | return -ENOMEM; | ||
132 | } | ||
133 | wq->wait_queue_token = autofs_next_wait_queue++; | ||
134 | init_waitqueue_head(&wq->queue); | ||
135 | wq->hash = name->hash; | ||
136 | wq->len = name->len; | ||
137 | wq->status = -EINTR; /* Status return if interrupted */ | ||
138 | memcpy(wq->name, name->name, name->len); | ||
139 | wq->next = sbi->queues; | ||
140 | sbi->queues = wq; | ||
141 | |||
142 | /* autofs_notify_daemon() may block */ | ||
143 | wq->wait_ctr = 2; | ||
144 | autofs_notify_daemon(sbi,wq); | ||
145 | } else | ||
146 | wq->wait_ctr++; | ||
147 | |||
148 | /* wq->name is NULL if and only if the lock is already released */ | ||
149 | |||
150 | if ( sbi->catatonic ) { | ||
151 | /* We might have slept, so check again for catatonic mode */ | ||
152 | wq->status = -ENOENT; | ||
153 | if ( wq->name ) { | ||
154 | kfree(wq->name); | ||
155 | wq->name = NULL; | ||
156 | } | ||
157 | } | ||
158 | |||
159 | if ( wq->name ) { | ||
160 | /* Block all but "shutdown" signals while waiting */ | ||
161 | sigset_t sigmask; | ||
162 | |||
163 | siginitsetinv(&sigmask, SHUTDOWN_SIGS); | ||
164 | sigprocmask(SIG_BLOCK, &sigmask, &sigmask); | ||
165 | |||
166 | interruptible_sleep_on(&wq->queue); | ||
167 | |||
168 | sigprocmask(SIG_SETMASK, &sigmask, NULL); | ||
169 | } else { | ||
170 | DPRINTK(("autofs_wait: skipped sleeping\n")); | ||
171 | } | ||
172 | |||
173 | status = wq->status; | ||
174 | |||
175 | if ( ! --wq->wait_ctr ) /* Are we the last process to need status? */ | ||
176 | kfree(wq); | ||
177 | |||
178 | return status; | ||
179 | } | ||
180 | |||
181 | |||
182 | int autofs_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status) | ||
183 | { | ||
184 | struct autofs_wait_queue *wq, **wql; | ||
185 | |||
186 | for ( wql = &sbi->queues ; (wq = *wql) != 0 ; wql = &wq->next ) { | ||
187 | if ( wq->wait_queue_token == wait_queue_token ) | ||
188 | break; | ||
189 | } | ||
190 | if ( !wq ) | ||
191 | return -EINVAL; | ||
192 | |||
193 | *wql = wq->next; /* Unlink from chain */ | ||
194 | kfree(wq->name); | ||
195 | wq->name = NULL; /* Do not wait on this queue */ | ||
196 | |||
197 | wq->status = status; | ||
198 | |||
199 | if ( ! --wq->wait_ctr ) /* Is anyone still waiting for this guy? */ | ||
200 | kfree(wq); | ||
201 | else | ||
202 | wake_up(&wq->queue); | ||
203 | |||
204 | return 0; | ||
205 | } | ||
206 | |||