aboutsummaryrefslogtreecommitdiffstats
path: root/fs/autofs4
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /fs/autofs4
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'fs/autofs4')
-rw-r--r--fs/autofs4/Makefile7
-rw-r--r--fs/autofs4/autofs_i.h193
-rw-r--r--fs/autofs4/expire.c358
-rw-r--r--fs/autofs4/init.c42
-rw-r--r--fs/autofs4/inode.c324
-rw-r--r--fs/autofs4/root.c808
-rw-r--r--fs/autofs4/symlink.c25
-rw-r--r--fs/autofs4/waitq.c303
8 files changed, 2060 insertions, 0 deletions
diff --git a/fs/autofs4/Makefile b/fs/autofs4/Makefile
new file mode 100644
index 000000000000..f2c3b79e94d2
--- /dev/null
+++ b/fs/autofs4/Makefile
@@ -0,0 +1,7 @@
1#
2# Makefile for the linux autofs-filesystem routines.
3#
4
5obj-$(CONFIG_AUTOFS4_FS) += autofs4.o
6
7autofs4-objs := init.o inode.o root.o symlink.o waitq.o expire.o
diff --git a/fs/autofs4/autofs_i.h b/fs/autofs4/autofs_i.h
new file mode 100644
index 000000000000..f5a52c871726
--- /dev/null
+++ b/fs/autofs4/autofs_i.h
@@ -0,0 +1,193 @@
1/* -*- 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_fs4.h>
16#include <linux/list.h>
17
18/* This is the range of ioctl() numbers we claim as ours */
19#define AUTOFS_IOC_FIRST AUTOFS_IOC_READY
20#define AUTOFS_IOC_COUNT 32
21
22#include <linux/kernel.h>
23#include <linux/slab.h>
24#include <linux/time.h>
25#include <linux/string.h>
26#include <linux/wait.h>
27#include <linux/sched.h>
28#include <linux/mount.h>
29#include <linux/namei.h>
30#include <asm/current.h>
31#include <asm/uaccess.h>
32
33/* #define DEBUG */
34
35#ifdef DEBUG
36#define DPRINTK(fmt,args...) do { printk(KERN_DEBUG "pid %d: %s: " fmt "\n" , current->pid , __FUNCTION__ , ##args); } while(0)
37#else
38#define DPRINTK(fmt,args...) do {} while(0)
39#endif
40
41#define AUTOFS_SUPER_MAGIC 0x0187
42
43/*
44 * If the daemon returns a negative response (AUTOFS_IOC_FAIL) then the
45 * kernel will keep the negative response cached for up to the time given
46 * here, although the time can be shorter if the kernel throws the dcache
47 * entry away. This probably should be settable from user space.
48 */
49#define AUTOFS_NEGATIVE_TIMEOUT (60*HZ) /* 1 minute */
50
51/* Unified info structure. This is pointed to by both the dentry and
52 inode structures. Each file in the filesystem has an instance of this
53 structure. It holds a reference to the dentry, so dentries are never
54 flushed while the file exists. All name lookups are dealt with at the
55 dentry level, although the filesystem can interfere in the validation
56 process. Readdir is implemented by traversing the dentry lists. */
57struct autofs_info {
58 struct dentry *dentry;
59 struct inode *inode;
60
61 int flags;
62
63 struct autofs_sb_info *sbi;
64 unsigned long last_used;
65
66 mode_t mode;
67 size_t size;
68
69 void (*free)(struct autofs_info *);
70 union {
71 const char *symlink;
72 } u;
73};
74
75#define AUTOFS_INF_EXPIRING (1<<0) /* dentry is in the process of expiring */
76
77struct autofs_wait_queue {
78 wait_queue_head_t queue;
79 struct autofs_wait_queue *next;
80 autofs_wqt_t wait_queue_token;
81 /* We use the following to see what we are waiting for */
82 int hash;
83 int len;
84 char *name;
85 /* This is for status reporting upon return */
86 int status;
87 atomic_t wait_ctr;
88};
89
90#define AUTOFS_SBI_MAGIC 0x6d4a556d
91
92struct autofs_sb_info {
93 u32 magic;
94 struct file *pipe;
95 pid_t oz_pgrp;
96 int catatonic;
97 int version;
98 int sub_version;
99 unsigned long exp_timeout;
100 int reghost_enabled;
101 int needs_reghost;
102 struct super_block *sb;
103 struct semaphore wq_sem;
104 struct autofs_wait_queue *queues; /* Wait queue pointer */
105};
106
107static inline struct autofs_sb_info *autofs4_sbi(struct super_block *sb)
108{
109 return (struct autofs_sb_info *)(sb->s_fs_info);
110}
111
112static inline struct autofs_info *autofs4_dentry_ino(struct dentry *dentry)
113{
114 return (struct autofs_info *)(dentry->d_fsdata);
115}
116
117/* autofs4_oz_mode(): do we see the man behind the curtain? (The
118 processes which do manipulations for us in user space sees the raw
119 filesystem without "magic".) */
120
121static inline int autofs4_oz_mode(struct autofs_sb_info *sbi) {
122 return sbi->catatonic || process_group(current) == sbi->oz_pgrp;
123}
124
125/* Does a dentry have some pending activity? */
126static inline int autofs4_ispending(struct dentry *dentry)
127{
128 struct autofs_info *inf = autofs4_dentry_ino(dentry);
129
130 return (dentry->d_flags & DCACHE_AUTOFS_PENDING) ||
131 (inf != NULL && inf->flags & AUTOFS_INF_EXPIRING);
132}
133
134static inline void autofs4_copy_atime(struct file *src, struct file *dst)
135{
136 dst->f_dentry->d_inode->i_atime = src->f_dentry->d_inode->i_atime;
137 return;
138}
139
140struct inode *autofs4_get_inode(struct super_block *, struct autofs_info *);
141void autofs4_free_ino(struct autofs_info *);
142
143/* Expiration */
144int is_autofs4_dentry(struct dentry *);
145int autofs4_expire_run(struct super_block *, struct vfsmount *,
146 struct autofs_sb_info *,
147 struct autofs_packet_expire __user *);
148int autofs4_expire_multi(struct super_block *, struct vfsmount *,
149 struct autofs_sb_info *, int __user *);
150
151/* Operations structures */
152
153extern struct inode_operations autofs4_symlink_inode_operations;
154extern struct inode_operations autofs4_dir_inode_operations;
155extern struct inode_operations autofs4_root_inode_operations;
156extern struct file_operations autofs4_dir_operations;
157extern struct file_operations autofs4_root_operations;
158
159/* Initializing function */
160
161int autofs4_fill_super(struct super_block *, void *, int);
162struct autofs_info *autofs4_init_ino(struct autofs_info *, struct autofs_sb_info *sbi, mode_t mode);
163
164/* Queue management functions */
165
166enum autofs_notify
167{
168 NFY_NONE,
169 NFY_MOUNT,
170 NFY_EXPIRE
171};
172
173int autofs4_wait(struct autofs_sb_info *,struct dentry *, enum autofs_notify);
174int autofs4_wait_release(struct autofs_sb_info *,autofs_wqt_t,int);
175void autofs4_catatonic_mode(struct autofs_sb_info *);
176
177static inline int simple_positive(struct dentry *dentry)
178{
179 return dentry->d_inode && !d_unhashed(dentry);
180}
181
182static inline int simple_empty_nolock(struct dentry *dentry)
183{
184 struct dentry *child;
185 int ret = 0;
186
187 list_for_each_entry(child, &dentry->d_subdirs, d_child)
188 if (simple_positive(child))
189 goto out;
190 ret = 1;
191out:
192 return ret;
193}
diff --git a/fs/autofs4/expire.c b/fs/autofs4/expire.c
new file mode 100644
index 000000000000..31540a6404d9
--- /dev/null
+++ b/fs/autofs4/expire.c
@@ -0,0 +1,358 @@
1/* -*- c -*- --------------------------------------------------------------- *
2 *
3 * linux/fs/autofs/expire.c
4 *
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
7 * Copyright 2001-2003 Ian Kent <raven@themaw.net>
8 *
9 * This file is part of the Linux kernel and is made available under
10 * the terms of the GNU General Public License, version 2, or at your
11 * option, any later version, incorporated herein by reference.
12 *
13 * ------------------------------------------------------------------------- */
14
15#include "autofs_i.h"
16
17static unsigned long now;
18
19/* Check if a dentry can be expired return 1 if it can else return 0 */
20static inline int autofs4_can_expire(struct dentry *dentry,
21 unsigned long timeout, int do_now)
22{
23 struct autofs_info *ino = autofs4_dentry_ino(dentry);
24
25 /* dentry in the process of being deleted */
26 if (ino == NULL)
27 return 0;
28
29 /* No point expiring a pending mount */
30 if (dentry->d_flags & DCACHE_AUTOFS_PENDING)
31 return 0;
32
33 if (!do_now) {
34 /* Too young to die */
35 if (time_after(ino->last_used + timeout, now))
36 return 0;
37
38 /* update last_used here :-
39 - obviously makes sense if it is in use now
40 - less obviously, prevents rapid-fire expire
41 attempts if expire fails the first time */
42 ino->last_used = now;
43 }
44
45 return 1;
46}
47
48/* Check a mount point for busyness return 1 if not busy, otherwise */
49static int autofs4_check_mount(struct vfsmount *mnt, struct dentry *dentry)
50{
51 int status = 0;
52
53 DPRINTK("dentry %p %.*s",
54 dentry, (int)dentry->d_name.len, dentry->d_name.name);
55
56 mntget(mnt);
57 dget(dentry);
58
59 if (!follow_down(&mnt, &dentry))
60 goto done;
61
62 while (d_mountpoint(dentry) && follow_down(&mnt, &dentry))
63 ;
64
65 /* This is an autofs submount, we can't expire it */
66 if (is_autofs4_dentry(dentry))
67 goto done;
68
69 /* The big question */
70 if (may_umount_tree(mnt) == 0)
71 status = 1;
72done:
73 DPRINTK("returning = %d", status);
74 mntput(mnt);
75 dput(dentry);
76 return status;
77}
78
79/* Check a directory tree of mount points for busyness
80 * The tree is not busy iff no mountpoints are busy
81 * Return 1 if the tree is busy or 0 otherwise
82 */
83static int autofs4_check_tree(struct vfsmount *mnt,
84 struct dentry *top,
85 unsigned long timeout,
86 int do_now)
87{
88 struct dentry *this_parent = top;
89 struct list_head *next;
90
91 DPRINTK("parent %p %.*s",
92 top, (int)top->d_name.len, top->d_name.name);
93
94 /* Negative dentry - give up */
95 if (!simple_positive(top))
96 return 0;
97
98 /* Timeout of a tree mount is determined by its top dentry */
99 if (!autofs4_can_expire(top, timeout, do_now))
100 return 0;
101
102 spin_lock(&dcache_lock);
103repeat:
104 next = this_parent->d_subdirs.next;
105resume:
106 while (next != &this_parent->d_subdirs) {
107 struct dentry *dentry = list_entry(next, struct dentry, d_child);
108
109 /* Negative dentry - give up */
110 if (!simple_positive(dentry)) {
111 next = next->next;
112 continue;
113 }
114
115 DPRINTK("dentry %p %.*s",
116 dentry, (int)dentry->d_name.len, dentry->d_name.name);
117
118 if (!simple_empty_nolock(dentry)) {
119 this_parent = dentry;
120 goto repeat;
121 }
122
123 dentry = dget(dentry);
124 spin_unlock(&dcache_lock);
125
126 if (d_mountpoint(dentry)) {
127 /* First busy => tree busy */
128 if (!autofs4_check_mount(mnt, dentry)) {
129 dput(dentry);
130 return 0;
131 }
132 }
133
134 dput(dentry);
135 spin_lock(&dcache_lock);
136 next = next->next;
137 }
138
139 if (this_parent != top) {
140 next = this_parent->d_child.next;
141 this_parent = this_parent->d_parent;
142 goto resume;
143 }
144 spin_unlock(&dcache_lock);
145
146 return 1;
147}
148
149static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
150 struct dentry *parent,
151 unsigned long timeout,
152 int do_now)
153{
154 struct dentry *this_parent = parent;
155 struct list_head *next;
156
157 DPRINTK("parent %p %.*s",
158 parent, (int)parent->d_name.len, parent->d_name.name);
159
160 spin_lock(&dcache_lock);
161repeat:
162 next = this_parent->d_subdirs.next;
163resume:
164 while (next != &this_parent->d_subdirs) {
165 struct dentry *dentry = list_entry(next, struct dentry, d_child);
166
167 /* Negative dentry - give up */
168 if (!simple_positive(dentry)) {
169 next = next->next;
170 continue;
171 }
172
173 DPRINTK("dentry %p %.*s",
174 dentry, (int)dentry->d_name.len, dentry->d_name.name);
175
176 if (!list_empty(&dentry->d_subdirs)) {
177 this_parent = dentry;
178 goto repeat;
179 }
180
181 dentry = dget(dentry);
182 spin_unlock(&dcache_lock);
183
184 if (d_mountpoint(dentry)) {
185 /* Can we expire this guy */
186 if (!autofs4_can_expire(dentry, timeout, do_now))
187 goto cont;
188
189 /* Can we umount this guy */
190 if (autofs4_check_mount(mnt, dentry))
191 return dentry;
192
193 }
194cont:
195 dput(dentry);
196 spin_lock(&dcache_lock);
197 next = next->next;
198 }
199
200 if (this_parent != parent) {
201 next = this_parent->d_child.next;
202 this_parent = this_parent->d_parent;
203 goto resume;
204 }
205 spin_unlock(&dcache_lock);
206
207 return NULL;
208}
209
210/*
211 * Find an eligible tree to time-out
212 * A tree is eligible if :-
213 * - it is unused by any user process
214 * - it has been unused for exp_timeout time
215 */
216static struct dentry *autofs4_expire(struct super_block *sb,
217 struct vfsmount *mnt,
218 struct autofs_sb_info *sbi,
219 int how)
220{
221 unsigned long timeout;
222 struct dentry *root = sb->s_root;
223 struct dentry *expired = NULL;
224 struct list_head *next;
225 int do_now = how & AUTOFS_EXP_IMMEDIATE;
226 int exp_leaves = how & AUTOFS_EXP_LEAVES;
227
228 if ( !sbi->exp_timeout || !root )
229 return NULL;
230
231 now = jiffies;
232 timeout = sbi->exp_timeout;
233
234 spin_lock(&dcache_lock);
235 next = root->d_subdirs.next;
236
237 /* On exit from the loop expire is set to a dgot dentry
238 * to expire or it's NULL */
239 while ( next != &root->d_subdirs ) {
240 struct dentry *dentry = list_entry(next, struct dentry, d_child);
241
242 /* Negative dentry - give up */
243 if ( !simple_positive(dentry) ) {
244 next = next->next;
245 continue;
246 }
247
248 dentry = dget(dentry);
249 spin_unlock(&dcache_lock);
250
251 /* Case 1: indirect mount or top level direct mount */
252 if (d_mountpoint(dentry)) {
253 DPRINTK("checking mountpoint %p %.*s",
254 dentry, (int)dentry->d_name.len, dentry->d_name.name);
255
256 /* Can we expire this guy */
257 if (!autofs4_can_expire(dentry, timeout, do_now))
258 goto next;
259
260 /* Can we umount this guy */
261 if (autofs4_check_mount(mnt, dentry)) {
262 expired = dentry;
263 break;
264 }
265 goto next;
266 }
267
268 if ( simple_empty(dentry) )
269 goto next;
270
271 /* Case 2: tree mount, expire iff entire tree is not busy */
272 if (!exp_leaves) {
273 if (autofs4_check_tree(mnt, dentry, timeout, do_now)) {
274 expired = dentry;
275 break;
276 }
277 /* Case 3: direct mount, expire individual leaves */
278 } else {
279 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
280 if (expired) {
281 dput(dentry);
282 break;
283 }
284 }
285next:
286 dput(dentry);
287 spin_lock(&dcache_lock);
288 next = next->next;
289 }
290
291 if ( expired ) {
292 DPRINTK("returning %p %.*s",
293 expired, (int)expired->d_name.len, expired->d_name.name);
294 spin_lock(&dcache_lock);
295 list_del(&expired->d_parent->d_subdirs);
296 list_add(&expired->d_parent->d_subdirs, &expired->d_child);
297 spin_unlock(&dcache_lock);
298 return expired;
299 }
300 spin_unlock(&dcache_lock);
301
302 return NULL;
303}
304
305/* Perform an expiry operation */
306int autofs4_expire_run(struct super_block *sb,
307 struct vfsmount *mnt,
308 struct autofs_sb_info *sbi,
309 struct autofs_packet_expire __user *pkt_p)
310{
311 struct autofs_packet_expire pkt;
312 struct dentry *dentry;
313
314 memset(&pkt,0,sizeof pkt);
315
316 pkt.hdr.proto_version = sbi->version;
317 pkt.hdr.type = autofs_ptype_expire;
318
319 if ((dentry = autofs4_expire(sb, mnt, sbi, 0)) == NULL)
320 return -EAGAIN;
321
322 pkt.len = dentry->d_name.len;
323 memcpy(pkt.name, dentry->d_name.name, pkt.len);
324 pkt.name[pkt.len] = '\0';
325 dput(dentry);
326
327 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
328 return -EFAULT;
329
330 return 0;
331}
332
333/* Call repeatedly until it returns -EAGAIN, meaning there's nothing
334 more to be done */
335int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
336 struct autofs_sb_info *sbi, int __user *arg)
337{
338 struct dentry *dentry;
339 int ret = -EAGAIN;
340 int do_now = 0;
341
342 if (arg && get_user(do_now, arg))
343 return -EFAULT;
344
345 if ((dentry = autofs4_expire(sb, mnt, sbi, do_now)) != NULL) {
346 struct autofs_info *de_info = autofs4_dentry_ino(dentry);
347
348 /* This is synchronous because it makes the daemon a
349 little easier */
350 de_info->flags |= AUTOFS_INF_EXPIRING;
351 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
352 de_info->flags &= ~AUTOFS_INF_EXPIRING;
353 dput(dentry);
354 }
355
356 return ret;
357}
358
diff --git a/fs/autofs4/init.c b/fs/autofs4/init.c
new file mode 100644
index 000000000000..acecec8578ce
--- /dev/null
+++ b/fs/autofs4/init.c
@@ -0,0 +1,42 @@
1/* -*- 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
17static 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, autofs4_fill_super);
21}
22
23static 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
30static int __init init_autofs4_fs(void)
31{
32 return register_filesystem(&autofs_fs_type);
33}
34
35static void __exit exit_autofs4_fs(void)
36{
37 unregister_filesystem(&autofs_fs_type);
38}
39
40module_init(init_autofs4_fs)
41module_exit(exit_autofs4_fs)
42MODULE_LICENSE("GPL");
diff --git a/fs/autofs4/inode.c b/fs/autofs4/inode.c
new file mode 100644
index 000000000000..a52560746628
--- /dev/null
+++ b/fs/autofs4/inode.c
@@ -0,0 +1,324 @@
1/* -*- 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/slab.h>
15#include <linux/file.h>
16#include <linux/pagemap.h>
17#include <linux/parser.h>
18#include <linux/bitops.h>
19#include "autofs_i.h"
20#include <linux/module.h>
21
22static void ino_lnkfree(struct autofs_info *ino)
23{
24 if (ino->u.symlink) {
25 kfree(ino->u.symlink);
26 ino->u.symlink = NULL;
27 }
28}
29
30struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
31 struct autofs_sb_info *sbi, mode_t mode)
32{
33 int reinit = 1;
34
35 if (ino == NULL) {
36 reinit = 0;
37 ino = kmalloc(sizeof(*ino), GFP_KERNEL);
38 }
39
40 if (ino == NULL)
41 return NULL;
42
43 ino->flags = 0;
44 ino->mode = mode;
45 ino->inode = NULL;
46 ino->dentry = NULL;
47 ino->size = 0;
48
49 ino->last_used = jiffies;
50
51 ino->sbi = sbi;
52
53 if (reinit && ino->free)
54 (ino->free)(ino);
55
56 memset(&ino->u, 0, sizeof(ino->u));
57
58 ino->free = NULL;
59
60 if (S_ISLNK(mode))
61 ino->free = ino_lnkfree;
62
63 return ino;
64}
65
66void autofs4_free_ino(struct autofs_info *ino)
67{
68 if (ino->dentry) {
69 ino->dentry->d_fsdata = NULL;
70 if (ino->dentry->d_inode)
71 dput(ino->dentry);
72 ino->dentry = NULL;
73 }
74 if (ino->free)
75 (ino->free)(ino);
76 kfree(ino);
77}
78
79static void autofs4_put_super(struct super_block *sb)
80{
81 struct autofs_sb_info *sbi = autofs4_sbi(sb);
82
83 sb->s_fs_info = NULL;
84
85 if ( !sbi->catatonic )
86 autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
87
88 kfree(sbi);
89
90 DPRINTK("shutting down");
91}
92
93static struct super_operations autofs4_sops = {
94 .put_super = autofs4_put_super,
95 .statfs = simple_statfs,
96};
97
98enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
99
100static match_table_t tokens = {
101 {Opt_fd, "fd=%u"},
102 {Opt_uid, "uid=%u"},
103 {Opt_gid, "gid=%u"},
104 {Opt_pgrp, "pgrp=%u"},
105 {Opt_minproto, "minproto=%u"},
106 {Opt_maxproto, "maxproto=%u"},
107 {Opt_err, NULL}
108};
109
110static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
111 pid_t *pgrp, int *minproto, int *maxproto)
112{
113 char *p;
114 substring_t args[MAX_OPT_ARGS];
115 int option;
116
117 *uid = current->uid;
118 *gid = current->gid;
119 *pgrp = process_group(current);
120
121 *minproto = AUTOFS_MIN_PROTO_VERSION;
122 *maxproto = AUTOFS_MAX_PROTO_VERSION;
123
124 *pipefd = -1;
125
126 if (!options)
127 return 1;
128
129 while ((p = strsep(&options, ",")) != NULL) {
130 int token;
131 if (!*p)
132 continue;
133
134 token = match_token(p, tokens, args);
135 switch (token) {
136 case Opt_fd:
137 if (match_int(args, pipefd))
138 return 1;
139 break;
140 case Opt_uid:
141 if (match_int(args, &option))
142 return 1;
143 *uid = option;
144 break;
145 case Opt_gid:
146 if (match_int(args, &option))
147 return 1;
148 *gid = option;
149 break;
150 case Opt_pgrp:
151 if (match_int(args, &option))
152 return 1;
153 *pgrp = option;
154 break;
155 case Opt_minproto:
156 if (match_int(args, &option))
157 return 1;
158 *minproto = option;
159 break;
160 case Opt_maxproto:
161 if (match_int(args, &option))
162 return 1;
163 *maxproto = option;
164 break;
165 default:
166 return 1;
167 }
168 }
169 return (*pipefd < 0);
170}
171
172static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
173{
174 struct autofs_info *ino;
175
176 ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
177 if (!ino)
178 return NULL;
179
180 return ino;
181}
182
183int autofs4_fill_super(struct super_block *s, void *data, int silent)
184{
185 struct inode * root_inode;
186 struct dentry * root;
187 struct file * pipe;
188 int pipefd;
189 struct autofs_sb_info *sbi;
190 struct autofs_info *ino;
191 int minproto, maxproto;
192
193 sbi = (struct autofs_sb_info *) kmalloc(sizeof(*sbi), GFP_KERNEL);
194 if ( !sbi )
195 goto fail_unlock;
196 DPRINTK("starting up, sbi = %p",sbi);
197
198 memset(sbi, 0, sizeof(*sbi));
199
200 s->s_fs_info = sbi;
201 sbi->magic = AUTOFS_SBI_MAGIC;
202 sbi->catatonic = 0;
203 sbi->exp_timeout = 0;
204 sbi->oz_pgrp = process_group(current);
205 sbi->sb = s;
206 sbi->version = 0;
207 sbi->sub_version = 0;
208 init_MUTEX(&sbi->wq_sem);
209 sbi->queues = NULL;
210 s->s_blocksize = 1024;
211 s->s_blocksize_bits = 10;
212 s->s_magic = AUTOFS_SUPER_MAGIC;
213 s->s_op = &autofs4_sops;
214 s->s_time_gran = 1;
215
216 /*
217 * Get the root inode and dentry, but defer checking for errors.
218 */
219 ino = autofs4_mkroot(sbi);
220 if (!ino)
221 goto fail_free;
222 root_inode = autofs4_get_inode(s, ino);
223 kfree(ino);
224 if (!root_inode)
225 goto fail_free;
226
227 root_inode->i_op = &autofs4_root_inode_operations;
228 root_inode->i_fop = &autofs4_root_operations;
229 root = d_alloc_root(root_inode);
230 pipe = NULL;
231
232 if (!root)
233 goto fail_iput;
234
235 /* Can this call block? */
236 if (parse_options(data, &pipefd,
237 &root_inode->i_uid, &root_inode->i_gid,
238 &sbi->oz_pgrp,
239 &minproto, &maxproto)) {
240 printk("autofs: called with bogus options\n");
241 goto fail_dput;
242 }
243
244 /* Couldn't this be tested earlier? */
245 if (maxproto < AUTOFS_MIN_PROTO_VERSION ||
246 minproto > AUTOFS_MAX_PROTO_VERSION) {
247 printk("autofs: kernel does not match daemon version "
248 "daemon (%d, %d) kernel (%d, %d)\n",
249 minproto, maxproto,
250 AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
251 goto fail_dput;
252 }
253
254 sbi->version = maxproto > AUTOFS_MAX_PROTO_VERSION ? AUTOFS_MAX_PROTO_VERSION : maxproto;
255 sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
256
257 DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
258 pipe = fget(pipefd);
259
260 if ( !pipe ) {
261 printk("autofs: could not open pipe file descriptor\n");
262 goto fail_dput;
263 }
264 if ( !pipe->f_op || !pipe->f_op->write )
265 goto fail_fput;
266 sbi->pipe = pipe;
267
268 /*
269 * Success! Install the root dentry now to indicate completion.
270 */
271 s->s_root = root;
272 return 0;
273
274 /*
275 * Failure ... clean up.
276 */
277fail_fput:
278 printk("autofs: pipe file descriptor does not contain proper ops\n");
279 fput(pipe);
280 /* fall through */
281fail_dput:
282 dput(root);
283 goto fail_free;
284fail_iput:
285 printk("autofs: get root dentry failed\n");
286 iput(root_inode);
287fail_free:
288 kfree(sbi);
289fail_unlock:
290 return -EINVAL;
291}
292
293struct inode *autofs4_get_inode(struct super_block *sb,
294 struct autofs_info *inf)
295{
296 struct inode *inode = new_inode(sb);
297
298 if (inode == NULL)
299 return NULL;
300
301 inf->inode = inode;
302 inode->i_mode = inf->mode;
303 if (sb->s_root) {
304 inode->i_uid = sb->s_root->d_inode->i_uid;
305 inode->i_gid = sb->s_root->d_inode->i_gid;
306 } else {
307 inode->i_uid = 0;
308 inode->i_gid = 0;
309 }
310 inode->i_blksize = PAGE_CACHE_SIZE;
311 inode->i_blocks = 0;
312 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
313
314 if (S_ISDIR(inf->mode)) {
315 inode->i_nlink = 2;
316 inode->i_op = &autofs4_dir_inode_operations;
317 inode->i_fop = &autofs4_dir_operations;
318 } else if (S_ISLNK(inf->mode)) {
319 inode->i_size = inf->size;
320 inode->i_op = &autofs4_symlink_inode_operations;
321 }
322
323 return inode;
324}
diff --git a/fs/autofs4/root.c b/fs/autofs4/root.c
new file mode 100644
index 000000000000..3765c047f157
--- /dev/null
+++ b/fs/autofs4/root.c
@@ -0,0 +1,808 @@
1/* -*- c -*- --------------------------------------------------------------- *
2 *
3 * linux/fs/autofs/root.c
4 *
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
7 * Copyright 2001-2003 Ian Kent <raven@themaw.net>
8 *
9 * This file is part of the Linux kernel and is made available under
10 * the terms of the GNU General Public License, version 2, or at your
11 * option, any later version, incorporated herein by reference.
12 *
13 * ------------------------------------------------------------------------- */
14
15#include <linux/errno.h>
16#include <linux/stat.h>
17#include <linux/param.h>
18#include <linux/time.h>
19#include <linux/smp_lock.h>
20#include "autofs_i.h"
21
22static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
23static int autofs4_dir_unlink(struct inode *,struct dentry *);
24static int autofs4_dir_rmdir(struct inode *,struct dentry *);
25static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
26static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
27static int autofs4_dir_open(struct inode *inode, struct file *file);
28static int autofs4_dir_close(struct inode *inode, struct file *file);
29static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
30static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
31static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
32static int autofs4_dcache_readdir(struct file *, void *, filldir_t);
33
34struct file_operations autofs4_root_operations = {
35 .open = dcache_dir_open,
36 .release = dcache_dir_close,
37 .read = generic_read_dir,
38 .readdir = autofs4_root_readdir,
39 .ioctl = autofs4_root_ioctl,
40};
41
42struct file_operations autofs4_dir_operations = {
43 .open = autofs4_dir_open,
44 .release = autofs4_dir_close,
45 .read = generic_read_dir,
46 .readdir = autofs4_dir_readdir,
47};
48
49struct inode_operations autofs4_root_inode_operations = {
50 .lookup = autofs4_lookup,
51 .unlink = autofs4_dir_unlink,
52 .symlink = autofs4_dir_symlink,
53 .mkdir = autofs4_dir_mkdir,
54 .rmdir = autofs4_dir_rmdir,
55};
56
57struct inode_operations autofs4_dir_inode_operations = {
58 .lookup = autofs4_lookup,
59 .unlink = autofs4_dir_unlink,
60 .symlink = autofs4_dir_symlink,
61 .mkdir = autofs4_dir_mkdir,
62 .rmdir = autofs4_dir_rmdir,
63};
64
65static int autofs4_root_readdir(struct file *file, void *dirent,
66 filldir_t filldir)
67{
68 struct autofs_sb_info *sbi = autofs4_sbi(file->f_dentry->d_sb);
69 int oz_mode = autofs4_oz_mode(sbi);
70
71 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
72
73 /*
74 * Don't set reghost flag if:
75 * 1) f_pos is larger than zero -- we've already been here.
76 * 2) we haven't even enabled reghosting in the 1st place.
77 * 3) this is the daemon doing a readdir
78 */
79 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
80 sbi->needs_reghost = 1;
81
82 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
83
84 return autofs4_dcache_readdir(file, dirent, filldir);
85}
86
87/* Update usage from here to top of tree, so that scan of
88 top-level directories will give a useful result */
89static void autofs4_update_usage(struct dentry *dentry)
90{
91 struct dentry *top = dentry->d_sb->s_root;
92
93 spin_lock(&dcache_lock);
94 for(; dentry != top; dentry = dentry->d_parent) {
95 struct autofs_info *ino = autofs4_dentry_ino(dentry);
96
97 if (ino) {
98 update_atime(dentry->d_inode);
99 ino->last_used = jiffies;
100 }
101 }
102 spin_unlock(&dcache_lock);
103}
104
105/*
106 * From 2.4 kernel readdir.c
107 */
108static int autofs4_dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
109{
110 int i;
111 struct dentry *dentry = filp->f_dentry;
112
113 i = filp->f_pos;
114 switch (i) {
115 case 0:
116 if (filldir(dirent, ".", 1, i, dentry->d_inode->i_ino, DT_DIR) < 0)
117 break;
118 i++;
119 filp->f_pos++;
120 /* fallthrough */
121 case 1:
122 if (filldir(dirent, "..", 2, i, dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
123 break;
124 i++;
125 filp->f_pos++;
126 /* fallthrough */
127 default: {
128 struct list_head *list;
129 int j = i-2;
130
131 spin_lock(&dcache_lock);
132 list = dentry->d_subdirs.next;
133
134 for (;;) {
135 if (list == &dentry->d_subdirs) {
136 spin_unlock(&dcache_lock);
137 return 0;
138 }
139 if (!j)
140 break;
141 j--;
142 list = list->next;
143 }
144
145 while(1) {
146 struct dentry *de = list_entry(list, struct dentry, d_child);
147
148 if (!d_unhashed(de) && de->d_inode) {
149 spin_unlock(&dcache_lock);
150 if (filldir(dirent, de->d_name.name, de->d_name.len, filp->f_pos, de->d_inode->i_ino, DT_UNKNOWN) < 0)
151 break;
152 spin_lock(&dcache_lock);
153 }
154 filp->f_pos++;
155 list = list->next;
156 if (list != &dentry->d_subdirs)
157 continue;
158 spin_unlock(&dcache_lock);
159 break;
160 }
161 }
162 }
163 return 0;
164}
165
166static int autofs4_dir_open(struct inode *inode, struct file *file)
167{
168 struct dentry *dentry = file->f_dentry;
169 struct vfsmount *mnt = file->f_vfsmnt;
170 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
171 int status;
172
173 DPRINTK("file=%p dentry=%p %.*s",
174 file, dentry, dentry->d_name.len, dentry->d_name.name);
175
176 if (autofs4_oz_mode(sbi))
177 goto out;
178
179 if (autofs4_ispending(dentry)) {
180 DPRINTK("dentry busy");
181 return -EBUSY;
182 }
183
184 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
185 struct nameidata nd;
186 int empty;
187
188 /* In case there are stale directory dentrys from a failed mount */
189 spin_lock(&dcache_lock);
190 empty = list_empty(&dentry->d_subdirs);
191 spin_unlock(&dcache_lock);
192
193 if (!empty)
194 d_invalidate(dentry);
195
196 nd.flags = LOOKUP_DIRECTORY;
197 status = (dentry->d_op->d_revalidate)(dentry, &nd);
198
199 if (!status)
200 return -ENOENT;
201 }
202
203 if (d_mountpoint(dentry)) {
204 struct file *fp = NULL;
205 struct vfsmount *fp_mnt = mntget(mnt);
206 struct dentry *fp_dentry = dget(dentry);
207
208 while (follow_down(&fp_mnt, &fp_dentry) && d_mountpoint(fp_dentry));
209
210 fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
211 status = PTR_ERR(fp);
212 if (IS_ERR(fp)) {
213 file->private_data = NULL;
214 return status;
215 }
216 file->private_data = fp;
217 }
218out:
219 return 0;
220}
221
222static int autofs4_dir_close(struct inode *inode, struct file *file)
223{
224 struct dentry *dentry = file->f_dentry;
225 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
226
227 DPRINTK("file=%p dentry=%p %.*s",
228 file, dentry, dentry->d_name.len, dentry->d_name.name);
229
230 if (autofs4_oz_mode(sbi))
231 goto out;
232
233 if (autofs4_ispending(dentry)) {
234 DPRINTK("dentry busy");
235 return -EBUSY;
236 }
237
238 if (d_mountpoint(dentry)) {
239 struct file *fp = file->private_data;
240
241 if (!fp)
242 return -ENOENT;
243
244 filp_close(fp, current->files);
245 file->private_data = NULL;
246 }
247out:
248 return 0;
249}
250
251static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
252{
253 struct dentry *dentry = file->f_dentry;
254 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
255 int status;
256
257 DPRINTK("file=%p dentry=%p %.*s",
258 file, dentry, dentry->d_name.len, dentry->d_name.name);
259
260 if (autofs4_oz_mode(sbi))
261 goto out;
262
263 if (autofs4_ispending(dentry)) {
264 DPRINTK("dentry busy");
265 return -EBUSY;
266 }
267
268 if (d_mountpoint(dentry)) {
269 struct file *fp = file->private_data;
270
271 if (!fp)
272 return -ENOENT;
273
274 if (!fp->f_op || !fp->f_op->readdir)
275 goto out;
276
277 status = vfs_readdir(fp, filldir, dirent);
278 file->f_pos = fp->f_pos;
279 if (status)
280 autofs4_copy_atime(file, fp);
281 return status;
282 }
283out:
284 return autofs4_dcache_readdir(file, dirent, filldir);
285}
286
287static int try_to_fill_dentry(struct dentry *dentry,
288 struct super_block *sb,
289 struct autofs_sb_info *sbi, int flags)
290{
291 struct autofs_info *de_info = autofs4_dentry_ino(dentry);
292 int status = 0;
293
294 /* Block on any pending expiry here; invalidate the dentry
295 when expiration is done to trigger mount request with a new
296 dentry */
297 if (de_info && (de_info->flags & AUTOFS_INF_EXPIRING)) {
298 DPRINTK("waiting for expire %p name=%.*s",
299 dentry, dentry->d_name.len, dentry->d_name.name);
300
301 status = autofs4_wait(sbi, dentry, NFY_NONE);
302
303 DPRINTK("expire done status=%d", status);
304
305 return 0;
306 }
307
308 DPRINTK("dentry=%p %.*s ino=%p",
309 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
310
311 /* Wait for a pending mount, triggering one if there isn't one already */
312 if (dentry->d_inode == NULL) {
313 DPRINTK("waiting for mount name=%.*s",
314 dentry->d_name.len, dentry->d_name.name);
315
316 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
317
318 DPRINTK("mount done status=%d", status);
319
320 if (status && dentry->d_inode)
321 return 0; /* Try to get the kernel to invalidate this dentry */
322
323 /* Turn this into a real negative dentry? */
324 if (status == -ENOENT) {
325 dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
326 spin_lock(&dentry->d_lock);
327 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
328 spin_unlock(&dentry->d_lock);
329 return 1;
330 } else if (status) {
331 /* Return a negative dentry, but leave it "pending" */
332 return 1;
333 }
334 /* Trigger mount for path component or follow link */
335 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
336 current->link_count) {
337 DPRINTK("waiting for mount name=%.*s",
338 dentry->d_name.len, dentry->d_name.name);
339
340 spin_lock(&dentry->d_lock);
341 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
342 spin_unlock(&dentry->d_lock);
343 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
344
345 DPRINTK("mount done status=%d", status);
346
347 if (status) {
348 spin_lock(&dentry->d_lock);
349 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
350 spin_unlock(&dentry->d_lock);
351 return 0;
352 }
353 }
354
355 /* We don't update the usages for the autofs daemon itself, this
356 is necessary for recursive autofs mounts */
357 if (!autofs4_oz_mode(sbi))
358 autofs4_update_usage(dentry);
359
360 spin_lock(&dentry->d_lock);
361 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
362 spin_unlock(&dentry->d_lock);
363 return 1;
364}
365
366/*
367 * Revalidate is called on every cache lookup. Some of those
368 * cache lookups may actually happen while the dentry is not
369 * yet completely filled in, and revalidate has to delay such
370 * lookups..
371 */
372static int autofs4_revalidate(struct dentry * dentry, struct nameidata *nd)
373{
374 struct inode * dir = dentry->d_parent->d_inode;
375 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
376 int oz_mode = autofs4_oz_mode(sbi);
377 int flags = nd ? nd->flags : 0;
378 int status = 1;
379
380 /* Pending dentry */
381 if (autofs4_ispending(dentry)) {
382 if (!oz_mode)
383 status = try_to_fill_dentry(dentry, dir->i_sb, sbi, flags);
384 return status;
385 }
386
387 /* Negative dentry.. invalidate if "old" */
388 if (dentry->d_inode == NULL)
389 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
390
391 /* Check for a non-mountpoint directory with no contents */
392 spin_lock(&dcache_lock);
393 if (S_ISDIR(dentry->d_inode->i_mode) &&
394 !d_mountpoint(dentry) &&
395 list_empty(&dentry->d_subdirs)) {
396 DPRINTK("dentry=%p %.*s, emptydir",
397 dentry, dentry->d_name.len, dentry->d_name.name);
398 spin_unlock(&dcache_lock);
399 if (!oz_mode)
400 status = try_to_fill_dentry(dentry, dir->i_sb, sbi, flags);
401 return status;
402 }
403 spin_unlock(&dcache_lock);
404
405 /* Update the usage list */
406 if (!oz_mode)
407 autofs4_update_usage(dentry);
408
409 return 1;
410}
411
412static void autofs4_dentry_release(struct dentry *de)
413{
414 struct autofs_info *inf;
415
416 DPRINTK("releasing %p", de);
417
418 inf = autofs4_dentry_ino(de);
419 de->d_fsdata = NULL;
420
421 if (inf) {
422 inf->dentry = NULL;
423 inf->inode = NULL;
424
425 autofs4_free_ino(inf);
426 }
427}
428
429/* For dentries of directories in the root dir */
430static struct dentry_operations autofs4_root_dentry_operations = {
431 .d_revalidate = autofs4_revalidate,
432 .d_release = autofs4_dentry_release,
433};
434
435/* For other dentries */
436static struct dentry_operations autofs4_dentry_operations = {
437 .d_revalidate = autofs4_revalidate,
438 .d_release = autofs4_dentry_release,
439};
440
441/* Lookups in the root directory */
442static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
443{
444 struct autofs_sb_info *sbi;
445 int oz_mode;
446
447 DPRINTK("name = %.*s",
448 dentry->d_name.len, dentry->d_name.name);
449
450 if (dentry->d_name.len > NAME_MAX)
451 return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
452
453 sbi = autofs4_sbi(dir->i_sb);
454
455 oz_mode = autofs4_oz_mode(sbi);
456 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
457 current->pid, process_group(current), sbi->catatonic, oz_mode);
458
459 /*
460 * Mark the dentry incomplete, but add it. This is needed so
461 * that the VFS layer knows about the dentry, and we can count
462 * on catching any lookups through the revalidate.
463 *
464 * Let all the hard work be done by the revalidate function that
465 * needs to be able to do this anyway..
466 *
467 * We need to do this before we release the directory semaphore.
468 */
469 dentry->d_op = &autofs4_root_dentry_operations;
470
471 if (!oz_mode) {
472 spin_lock(&dentry->d_lock);
473 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
474 spin_unlock(&dentry->d_lock);
475 }
476 dentry->d_fsdata = NULL;
477 d_add(dentry, NULL);
478
479 if (dentry->d_op && dentry->d_op->d_revalidate) {
480 up(&dir->i_sem);
481 (dentry->d_op->d_revalidate)(dentry, nd);
482 down(&dir->i_sem);
483 }
484
485 /*
486 * If we are still pending, check if we had to handle
487 * a signal. If so we can force a restart..
488 */
489 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
490 /* See if we were interrupted */
491 if (signal_pending(current)) {
492 sigset_t *sigset = &current->pending.signal;
493 if (sigismember (sigset, SIGKILL) ||
494 sigismember (sigset, SIGQUIT) ||
495 sigismember (sigset, SIGINT)) {
496 return ERR_PTR(-ERESTARTNOINTR);
497 }
498 }
499 }
500
501 /*
502 * If this dentry is unhashed, then we shouldn't honour this
503 * lookup even if the dentry is positive. Returning ENOENT here
504 * doesn't do the right thing for all system calls, but it should
505 * be OK for the operations we permit from an autofs.
506 */
507 if ( dentry->d_inode && d_unhashed(dentry) )
508 return ERR_PTR(-ENOENT);
509
510 return NULL;
511}
512
513static int autofs4_dir_symlink(struct inode *dir,
514 struct dentry *dentry,
515 const char *symname)
516{
517 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
518 struct autofs_info *ino = autofs4_dentry_ino(dentry);
519 struct inode *inode;
520 char *cp;
521
522 DPRINTK("%s <- %.*s", symname,
523 dentry->d_name.len, dentry->d_name.name);
524
525 if (!autofs4_oz_mode(sbi))
526 return -EACCES;
527
528 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
529 if (ino == NULL)
530 return -ENOSPC;
531
532 ino->size = strlen(symname);
533 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
534
535 if (cp == NULL) {
536 kfree(ino);
537 return -ENOSPC;
538 }
539
540 strcpy(cp, symname);
541
542 inode = autofs4_get_inode(dir->i_sb, ino);
543 d_instantiate(dentry, inode);
544
545 if (dir == dir->i_sb->s_root->d_inode)
546 dentry->d_op = &autofs4_root_dentry_operations;
547 else
548 dentry->d_op = &autofs4_dentry_operations;
549
550 dentry->d_fsdata = ino;
551 ino->dentry = dget(dentry);
552 ino->inode = inode;
553
554 dir->i_mtime = CURRENT_TIME;
555
556 return 0;
557}
558
559/*
560 * NOTE!
561 *
562 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
563 * that the file no longer exists. However, doing that means that the
564 * VFS layer can turn the dentry into a negative dentry. We don't want
565 * this, because since the unlink is probably the result of an expire.
566 * We simply d_drop it, which allows the dentry lookup to remount it
567 * if necessary.
568 *
569 * If a process is blocked on the dentry waiting for the expire to finish,
570 * it will invalidate the dentry and try to mount with a new one.
571 *
572 * Also see autofs4_dir_rmdir()..
573 */
574static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
575{
576 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
577 struct autofs_info *ino = autofs4_dentry_ino(dentry);
578
579 /* This allows root to remove symlinks */
580 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
581 return -EACCES;
582
583 dput(ino->dentry);
584
585 dentry->d_inode->i_size = 0;
586 dentry->d_inode->i_nlink = 0;
587
588 dir->i_mtime = CURRENT_TIME;
589
590 d_drop(dentry);
591
592 return 0;
593}
594
595static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
596{
597 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
598 struct autofs_info *ino = autofs4_dentry_ino(dentry);
599
600 if (!autofs4_oz_mode(sbi))
601 return -EACCES;
602
603 spin_lock(&dcache_lock);
604 if (!list_empty(&dentry->d_subdirs)) {
605 spin_unlock(&dcache_lock);
606 return -ENOTEMPTY;
607 }
608 spin_lock(&dentry->d_lock);
609 __d_drop(dentry);
610 spin_unlock(&dentry->d_lock);
611 spin_unlock(&dcache_lock);
612
613 dput(ino->dentry);
614
615 dentry->d_inode->i_size = 0;
616 dentry->d_inode->i_nlink = 0;
617
618 if (dir->i_nlink)
619 dir->i_nlink--;
620
621 return 0;
622}
623
624static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
625{
626 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
627 struct autofs_info *ino = autofs4_dentry_ino(dentry);
628 struct inode *inode;
629
630 if ( !autofs4_oz_mode(sbi) )
631 return -EACCES;
632
633 DPRINTK("dentry %p, creating %.*s",
634 dentry, dentry->d_name.len, dentry->d_name.name);
635
636 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
637 if (ino == NULL)
638 return -ENOSPC;
639
640 inode = autofs4_get_inode(dir->i_sb, ino);
641 d_instantiate(dentry, inode);
642
643 if (dir == dir->i_sb->s_root->d_inode)
644 dentry->d_op = &autofs4_root_dentry_operations;
645 else
646 dentry->d_op = &autofs4_dentry_operations;
647
648 dentry->d_fsdata = ino;
649 ino->dentry = dget(dentry);
650 ino->inode = inode;
651 dir->i_nlink++;
652 dir->i_mtime = CURRENT_TIME;
653
654 return 0;
655}
656
657/* Get/set timeout ioctl() operation */
658static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
659 unsigned long __user *p)
660{
661 int rv;
662 unsigned long ntimeout;
663
664 if ( (rv = get_user(ntimeout, p)) ||
665 (rv = put_user(sbi->exp_timeout/HZ, p)) )
666 return rv;
667
668 if ( ntimeout > ULONG_MAX/HZ )
669 sbi->exp_timeout = 0;
670 else
671 sbi->exp_timeout = ntimeout * HZ;
672
673 return 0;
674}
675
676/* Return protocol version */
677static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
678{
679 return put_user(sbi->version, p);
680}
681
682/* Return protocol sub version */
683static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
684{
685 return put_user(sbi->sub_version, p);
686}
687
688/*
689 * Tells the daemon whether we need to reghost or not. Also, clears
690 * the reghost_needed flag.
691 */
692static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
693{
694 int status;
695
696 DPRINTK("returning %d", sbi->needs_reghost);
697
698 status = put_user(sbi->needs_reghost, p);
699 if ( status )
700 return status;
701
702 sbi->needs_reghost = 0;
703 return 0;
704}
705
706/*
707 * Enable / Disable reghosting ioctl() operation
708 */
709static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
710{
711 int status;
712 int val;
713
714 status = get_user(val, p);
715
716 DPRINTK("reghost = %d", val);
717
718 if (status)
719 return status;
720
721 /* turn on/off reghosting, with the val */
722 sbi->reghost_enabled = val;
723 return 0;
724}
725
726/*
727* Tells the daemon whether it can umount the autofs mount.
728*/
729static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
730{
731 int status = 0;
732
733 if (may_umount(mnt) == 0)
734 status = 1;
735
736 DPRINTK("returning %d", status);
737
738 status = put_user(status, p);
739
740 return status;
741}
742
743/* Identify autofs4_dentries - this is so we can tell if there's
744 an extra dentry refcount or not. We only hold a refcount on the
745 dentry if its non-negative (ie, d_inode != NULL)
746*/
747int is_autofs4_dentry(struct dentry *dentry)
748{
749 return dentry && dentry->d_inode &&
750 (dentry->d_op == &autofs4_root_dentry_operations ||
751 dentry->d_op == &autofs4_dentry_operations) &&
752 dentry->d_fsdata != NULL;
753}
754
755/*
756 * ioctl()'s on the root directory is the chief method for the daemon to
757 * generate kernel reactions
758 */
759static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
760 unsigned int cmd, unsigned long arg)
761{
762 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
763 void __user *p = (void __user *)arg;
764
765 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
766 cmd,arg,sbi,process_group(current));
767
768 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
769 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
770 return -ENOTTY;
771
772 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
773 return -EPERM;
774
775 switch(cmd) {
776 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
777 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
778 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
779 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
780 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
781 autofs4_catatonic_mode(sbi);
782 return 0;
783 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
784 return autofs4_get_protover(sbi, p);
785 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
786 return autofs4_get_protosubver(sbi, p);
787 case AUTOFS_IOC_SETTIMEOUT:
788 return autofs4_get_set_timeout(sbi, p);
789
790 case AUTOFS_IOC_TOGGLEREGHOST:
791 return autofs4_toggle_reghost(sbi, p);
792 case AUTOFS_IOC_ASKREGHOST:
793 return autofs4_ask_reghost(sbi, p);
794
795 case AUTOFS_IOC_ASKUMOUNT:
796 return autofs4_ask_umount(filp->f_vfsmnt, p);
797
798 /* return a single thing to expire */
799 case AUTOFS_IOC_EXPIRE:
800 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
801 /* same as above, but can send multiple expires through pipe */
802 case AUTOFS_IOC_EXPIRE_MULTI:
803 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);
804
805 default:
806 return -ENOSYS;
807 }
808}
diff --git a/fs/autofs4/symlink.c b/fs/autofs4/symlink.c
new file mode 100644
index 000000000000..c265a66edf0f
--- /dev/null
+++ b/fs/autofs4/symlink.c
@@ -0,0 +1,25 @@
1/* -*- 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
15static int autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
16{
17 struct autofs_info *ino = autofs4_dentry_ino(dentry);
18 nd_set_link(nd, (char *)ino->u.symlink);
19 return 0;
20}
21
22struct inode_operations autofs4_symlink_inode_operations = {
23 .readlink = generic_readlink,
24 .follow_link = autofs4_follow_link
25};
diff --git a/fs/autofs4/waitq.c b/fs/autofs4/waitq.c
new file mode 100644
index 000000000000..1ab24a662e09
--- /dev/null
+++ b/fs/autofs4/waitq.c
@@ -0,0 +1,303 @@
1/* -*- c -*- --------------------------------------------------------------- *
2 *
3 * linux/fs/autofs/waitq.c
4 *
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 2001-2003 Ian Kent <raven@themaw.net>
7 *
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
11 *
12 * ------------------------------------------------------------------------- */
13
14#include <linux/slab.h>
15#include <linux/time.h>
16#include <linux/signal.h>
17#include <linux/file.h>
18#include "autofs_i.h"
19
20/* We make this a static variable rather than a part of the superblock; it
21 is better if we don't reassign numbers easily even across filesystems */
22static autofs_wqt_t autofs4_next_wait_queue = 1;
23
24/* These are the signals we allow interrupting a pending mount */
25#define SHUTDOWN_SIGS (sigmask(SIGKILL) | sigmask(SIGINT) | sigmask(SIGQUIT))
26
27void autofs4_catatonic_mode(struct autofs_sb_info *sbi)
28{
29 struct autofs_wait_queue *wq, *nwq;
30
31 DPRINTK("entering catatonic mode");
32
33 sbi->catatonic = 1;
34 wq = sbi->queues;
35 sbi->queues = NULL; /* Erase all wait queues */
36 while ( wq ) {
37 nwq = wq->next;
38 wq->status = -ENOENT; /* Magic is gone - report failure */
39 kfree(wq->name);
40 wq->name = NULL;
41 wake_up_interruptible(&wq->queue);
42 wq = nwq;
43 }
44 if (sbi->pipe) {
45 fput(sbi->pipe); /* Close the pipe */
46 sbi->pipe = NULL;
47 }
48
49 shrink_dcache_sb(sbi->sb);
50}
51
52static int autofs4_write(struct file *file, const void *addr, int bytes)
53{
54 unsigned long sigpipe, flags;
55 mm_segment_t fs;
56 const char *data = (const char *)addr;
57 ssize_t wr = 0;
58
59 /** WARNING: this is not safe for writing more than PIPE_BUF bytes! **/
60
61 sigpipe = sigismember(&current->pending.signal, SIGPIPE);
62
63 /* Save pointer to user space and point back to kernel space */
64 fs = get_fs();
65 set_fs(KERNEL_DS);
66
67 while (bytes &&
68 (wr = file->f_op->write(file,data,bytes,&file->f_pos)) > 0) {
69 data += wr;
70 bytes -= wr;
71 }
72
73 set_fs(fs);
74
75 /* Keep the currently executing process from receiving a
76 SIGPIPE unless it was already supposed to get one */
77 if (wr == -EPIPE && !sigpipe) {
78 spin_lock_irqsave(&current->sighand->siglock, flags);
79 sigdelset(&current->pending.signal, SIGPIPE);
80 recalc_sigpending();
81 spin_unlock_irqrestore(&current->sighand->siglock, flags);
82 }
83
84 return (bytes > 0);
85}
86
87static void autofs4_notify_daemon(struct autofs_sb_info *sbi,
88 struct autofs_wait_queue *wq,
89 int type)
90{
91 union autofs_packet_union pkt;
92 size_t pktsz;
93
94 DPRINTK("wait id = 0x%08lx, name = %.*s, type=%d",
95 wq->wait_queue_token, wq->len, wq->name, type);
96
97 memset(&pkt,0,sizeof pkt); /* For security reasons */
98
99 pkt.hdr.proto_version = sbi->version;
100 pkt.hdr.type = type;
101 if (type == autofs_ptype_missing) {
102 struct autofs_packet_missing *mp = &pkt.missing;
103
104 pktsz = sizeof(*mp);
105
106 mp->wait_queue_token = wq->wait_queue_token;
107 mp->len = wq->len;
108 memcpy(mp->name, wq->name, wq->len);
109 mp->name[wq->len] = '\0';
110 } else if (type == autofs_ptype_expire_multi) {
111 struct autofs_packet_expire_multi *ep = &pkt.expire_multi;
112
113 pktsz = sizeof(*ep);
114
115 ep->wait_queue_token = wq->wait_queue_token;
116 ep->len = wq->len;
117 memcpy(ep->name, wq->name, wq->len);
118 ep->name[wq->len] = '\0';
119 } else {
120 printk("autofs4_notify_daemon: bad type %d!\n", type);
121 return;
122 }
123
124 if (autofs4_write(sbi->pipe, &pkt, pktsz))
125 autofs4_catatonic_mode(sbi);
126}
127
128static int autofs4_getpath(struct autofs_sb_info *sbi,
129 struct dentry *dentry, char **name)
130{
131 struct dentry *root = sbi->sb->s_root;
132 struct dentry *tmp;
133 char *buf = *name;
134 char *p;
135 int len = 0;
136
137 spin_lock(&dcache_lock);
138 for (tmp = dentry ; tmp != root ; tmp = tmp->d_parent)
139 len += tmp->d_name.len + 1;
140
141 if (--len > NAME_MAX) {
142 spin_unlock(&dcache_lock);
143 return 0;
144 }
145
146 *(buf + len) = '\0';
147 p = buf + len - dentry->d_name.len;
148 strncpy(p, dentry->d_name.name, dentry->d_name.len);
149
150 for (tmp = dentry->d_parent; tmp != root ; tmp = tmp->d_parent) {
151 *(--p) = '/';
152 p -= tmp->d_name.len;
153 strncpy(p, tmp->d_name.name, tmp->d_name.len);
154 }
155 spin_unlock(&dcache_lock);
156
157 return len;
158}
159
160int autofs4_wait(struct autofs_sb_info *sbi, struct dentry *dentry,
161 enum autofs_notify notify)
162{
163 struct autofs_wait_queue *wq;
164 char *name;
165 int len, status;
166
167 /* In catatonic mode, we don't wait for nobody */
168 if ( sbi->catatonic )
169 return -ENOENT;
170
171 name = kmalloc(NAME_MAX + 1, GFP_KERNEL);
172 if (!name)
173 return -ENOMEM;
174
175 len = autofs4_getpath(sbi, dentry, &name);
176 if (!len) {
177 kfree(name);
178 return -ENOENT;
179 }
180
181 if (down_interruptible(&sbi->wq_sem)) {
182 kfree(name);
183 return -EINTR;
184 }
185
186 for (wq = sbi->queues ; wq ; wq = wq->next) {
187 if (wq->hash == dentry->d_name.hash &&
188 wq->len == len &&
189 wq->name && !memcmp(wq->name, name, len))
190 break;
191 }
192
193 if ( !wq ) {
194 /* Create a new wait queue */
195 wq = kmalloc(sizeof(struct autofs_wait_queue),GFP_KERNEL);
196 if ( !wq ) {
197 kfree(name);
198 up(&sbi->wq_sem);
199 return -ENOMEM;
200 }
201
202 wq->wait_queue_token = autofs4_next_wait_queue;
203 if (++autofs4_next_wait_queue == 0)
204 autofs4_next_wait_queue = 1;
205 wq->next = sbi->queues;
206 sbi->queues = wq;
207 init_waitqueue_head(&wq->queue);
208 wq->hash = dentry->d_name.hash;
209 wq->name = name;
210 wq->len = len;
211 wq->status = -EINTR; /* Status return if interrupted */
212 atomic_set(&wq->wait_ctr, 2);
213 up(&sbi->wq_sem);
214
215 DPRINTK("new wait id = 0x%08lx, name = %.*s, nfy=%d",
216 (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
217 /* autofs4_notify_daemon() may block */
218 if (notify != NFY_NONE) {
219 autofs4_notify_daemon(sbi,wq,
220 notify == NFY_MOUNT ?
221 autofs_ptype_missing :
222 autofs_ptype_expire_multi);
223 }
224 } else {
225 atomic_inc(&wq->wait_ctr);
226 up(&sbi->wq_sem);
227 kfree(name);
228 DPRINTK("existing wait id = 0x%08lx, name = %.*s, nfy=%d",
229 (unsigned long) wq->wait_queue_token, wq->len, wq->name, notify);
230 }
231
232 /* wq->name is NULL if and only if the lock is already released */
233
234 if ( sbi->catatonic ) {
235 /* We might have slept, so check again for catatonic mode */
236 wq->status = -ENOENT;
237 if ( wq->name ) {
238 kfree(wq->name);
239 wq->name = NULL;
240 }
241 }
242
243 if ( wq->name ) {
244 /* Block all but "shutdown" signals while waiting */
245 sigset_t oldset;
246 unsigned long irqflags;
247
248 spin_lock_irqsave(&current->sighand->siglock, irqflags);
249 oldset = current->blocked;
250 siginitsetinv(&current->blocked, SHUTDOWN_SIGS & ~oldset.sig[0]);
251 recalc_sigpending();
252 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
253
254 wait_event_interruptible(wq->queue, wq->name == NULL);
255
256 spin_lock_irqsave(&current->sighand->siglock, irqflags);
257 current->blocked = oldset;
258 recalc_sigpending();
259 spin_unlock_irqrestore(&current->sighand->siglock, irqflags);
260 } else {
261 DPRINTK("skipped sleeping");
262 }
263
264 status = wq->status;
265
266 /* Are we the last process to need status? */
267 if (atomic_dec_and_test(&wq->wait_ctr))
268 kfree(wq);
269
270 return status;
271}
272
273
274int autofs4_wait_release(struct autofs_sb_info *sbi, autofs_wqt_t wait_queue_token, int status)
275{
276 struct autofs_wait_queue *wq, **wql;
277
278 down(&sbi->wq_sem);
279 for ( wql = &sbi->queues ; (wq = *wql) != 0 ; wql = &wq->next ) {
280 if ( wq->wait_queue_token == wait_queue_token )
281 break;
282 }
283
284 if ( !wq ) {
285 up(&sbi->wq_sem);
286 return -EINVAL;
287 }
288
289 *wql = wq->next; /* Unlink from chain */
290 up(&sbi->wq_sem);
291 kfree(wq->name);
292 wq->name = NULL; /* Do not wait on this queue */
293
294 wq->status = status;
295
296 if (atomic_dec_and_test(&wq->wait_ctr)) /* Is anyone still waiting for this guy? */
297 kfree(wq);
298 else
299 wake_up_interruptible(&wq->queue);
300
301 return 0;
302}
303