ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / autofs4 / root.c
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  *
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/errno.h>
15 #include <linux/stat.h>
16 #include <linux/param.h>
17 #include <linux/time.h>
18 #include <linux/smp_lock.h>
19 #include "autofs_i.h"
20
21 static struct dentry *autofs4_dir_lookup(struct inode *,struct dentry *, struct nameidata *);
22 static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
23 static int autofs4_dir_unlink(struct inode *,struct dentry *);
24 static int autofs4_dir_rmdir(struct inode *,struct dentry *);
25 static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
26 static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
27 static struct dentry *autofs4_root_lookup(struct inode *,struct dentry *, struct nameidata *);
28
29 struct file_operations autofs4_root_operations = {
30         .open           = dcache_dir_open,
31         .release        = dcache_dir_close,
32         .llseek         = dcache_dir_lseek,
33         .read           = generic_read_dir,
34         .readdir        = dcache_readdir,
35         .ioctl          = autofs4_root_ioctl,
36 };
37
38 struct inode_operations autofs4_root_inode_operations = {
39         .lookup         = autofs4_root_lookup,
40         .unlink         = autofs4_dir_unlink,
41         .symlink        = autofs4_dir_symlink,
42         .mkdir          = autofs4_dir_mkdir,
43         .rmdir          = autofs4_dir_rmdir,
44 };
45
46 struct inode_operations autofs4_dir_inode_operations = {
47         .lookup         = autofs4_dir_lookup,
48         .unlink         = autofs4_dir_unlink,
49         .symlink        = autofs4_dir_symlink,
50         .mkdir          = autofs4_dir_mkdir,
51         .rmdir          = autofs4_dir_rmdir,
52 };
53
54 /* Update usage from here to top of tree, so that scan of
55    top-level directories will give a useful result */
56 static void autofs4_update_usage(struct dentry *dentry)
57 {
58         struct dentry *top = dentry->d_sb->s_root;
59
60         for(; dentry != top; dentry = dentry->d_parent) {
61                 struct autofs_info *ino = autofs4_dentry_ino(dentry);
62
63                 if (ino) {
64                         update_atime(dentry->d_inode);
65                         ino->last_used = jiffies;
66                 }
67         }
68 }
69
70 static int try_to_fill_dentry(struct dentry *dentry, 
71                               struct super_block *sb,
72                               struct autofs_sb_info *sbi)
73 {
74         struct autofs_info *de_info = autofs4_dentry_ino(dentry);
75         int status = 0;
76
77         /* Block on any pending expiry here; invalidate the dentry
78            when expiration is done to trigger mount request with a new
79            dentry */
80         if (de_info && (de_info->flags & AUTOFS_INF_EXPIRING)) {
81                 DPRINTK(("try_to_fill_entry: waiting for expire %p name=%.*s, flags&PENDING=%s de_info=%p de_info->flags=%x\n",
82                          dentry, dentry->d_name.len, dentry->d_name.name, 
83                          dentry->d_flags & DCACHE_AUTOFS_PENDING?"t":"f",
84                          de_info, de_info?de_info->flags:0));
85                 status = autofs4_wait(sbi, &dentry->d_name, NFY_NONE);
86                 
87                 DPRINTK(("try_to_fill_entry: expire done status=%d\n", status));
88                 
89                 return 0;
90         }
91
92         DPRINTK(("try_to_fill_entry: dentry=%p %.*s ino=%p\n", 
93                  dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode));
94
95         /* Wait for a pending mount, triggering one if there isn't one already */
96         while(dentry->d_inode == NULL) {
97                 DPRINTK(("try_to_fill_entry: waiting for mount name=%.*s, de_info=%p de_info->flags=%x\n",
98                          dentry->d_name.len, dentry->d_name.name, 
99                          de_info, de_info?de_info->flags:0));
100                 status = autofs4_wait(sbi, &dentry->d_name, NFY_MOUNT);
101                  
102                 DPRINTK(("try_to_fill_entry: mount done status=%d\n", status));
103
104                 if (status && dentry->d_inode)
105                         return 0; /* Try to get the kernel to invalidate this dentry */
106                 
107                 /* Turn this into a real negative dentry? */
108                 if (status == -ENOENT) {
109                         dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
110                         dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
111                         return 1;
112                 } else if (status) {
113                         /* Return a negative dentry, but leave it "pending" */
114                         return 1;
115                 }
116         }
117
118         /* If this is an unused directory that isn't a mount point,
119            bitch at the daemon and fix it in user space */
120         spin_lock(&dcache_lock);
121         if (S_ISDIR(dentry->d_inode->i_mode) &&
122             !d_mountpoint(dentry) && 
123             list_empty(&dentry->d_subdirs)) {
124                 DPRINTK(("try_to_fill_entry: mounting existing dir\n"));
125                 spin_unlock(&dcache_lock);
126                 return autofs4_wait(sbi, &dentry->d_name, NFY_MOUNT) == 0;
127         }
128         spin_unlock(&dcache_lock);
129
130         /* We don't update the usages for the autofs daemon itself, this
131            is necessary for recursive autofs mounts */
132         if (!autofs4_oz_mode(sbi))
133                 autofs4_update_usage(dentry);
134
135         dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
136         return 1;
137 }
138
139
140 /*
141  * Revalidate is called on every cache lookup.  Some of those
142  * cache lookups may actually happen while the dentry is not
143  * yet completely filled in, and revalidate has to delay such
144  * lookups..
145  */
146 static int autofs4_root_revalidate(struct dentry * dentry, struct nameidata *nd)
147 {
148         struct inode * dir = dentry->d_parent->d_inode;
149         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
150         int oz_mode = autofs4_oz_mode(sbi);
151
152         /* Pending dentry */
153         if (autofs4_ispending(dentry)) {
154                 if (autofs4_oz_mode(sbi))
155                         return 1;
156                 else
157                         return try_to_fill_dentry(dentry, dir->i_sb, sbi);
158         }
159
160         /* Negative dentry.. invalidate if "old" */
161         if (dentry->d_inode == NULL)
162                 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
163
164         /* Check for a non-mountpoint directory with no contents */
165         spin_lock(&dcache_lock);
166         if (S_ISDIR(dentry->d_inode->i_mode) &&
167             !d_mountpoint(dentry) && 
168             list_empty(&dentry->d_subdirs)) {
169                 DPRINTK(("autofs_root_revalidate: dentry=%p %.*s, emptydir\n",
170                          dentry, dentry->d_name.len, dentry->d_name.name));
171                 spin_unlock(&dcache_lock);
172                 if (oz_mode)
173                         return 1;
174                 else
175                         return try_to_fill_dentry(dentry, dir->i_sb, sbi);
176         }
177         spin_unlock(&dcache_lock);
178
179         /* Update the usage list */
180         if (!oz_mode)
181                 autofs4_update_usage(dentry);
182
183         return 1;
184 }
185
186 static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
187 {
188         struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
189
190         if (!autofs4_oz_mode(sbi))
191                 autofs4_update_usage(dentry);
192
193         return 1;
194 }
195
196 static void autofs4_dentry_release(struct dentry *de)
197 {
198         struct autofs_info *inf;
199
200         DPRINTK(("autofs4_dentry_release: releasing %p\n", de));
201
202         inf = autofs4_dentry_ino(de);
203         de->d_fsdata = NULL;
204
205         if (inf) {
206                 inf->dentry = NULL;
207                 inf->inode = NULL;
208
209                 autofs4_free_ino(inf);
210         }
211 }
212
213 /* For dentries of directories in the root dir */
214 static struct dentry_operations autofs4_root_dentry_operations = {
215         .d_revalidate   = autofs4_root_revalidate,
216         .d_release      = autofs4_dentry_release,
217 };
218
219 /* For other dentries */
220 static struct dentry_operations autofs4_dentry_operations = {
221         .d_revalidate   = autofs4_revalidate,
222         .d_release      = autofs4_dentry_release,
223 };
224
225 /* Lookups in non-root dirs never find anything - if it's there, it's
226    already in the dcache */
227 /* SMP-safe */
228 static struct dentry *autofs4_dir_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
229 {
230 #if 0
231         DPRINTK(("autofs_dir_lookup: ignoring lookup of %.*s/%.*s\n",
232                  dentry->d_parent->d_name.len, dentry->d_parent->d_name.name,
233                  dentry->d_name.len, dentry->d_name.name));
234 #endif
235
236         dentry->d_fsdata = NULL;
237         d_add(dentry, NULL);
238         return NULL;
239 }
240
241 /* Lookups in the root directory */
242 static struct dentry *autofs4_root_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
243 {
244         struct autofs_sb_info *sbi;
245         int oz_mode;
246
247         DPRINTK(("autofs_root_lookup: name = %.*s\n", 
248                  dentry->d_name.len, dentry->d_name.name));
249
250         if (dentry->d_name.len > NAME_MAX)
251                 return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
252
253         sbi = autofs4_sbi(dir->i_sb);
254
255         lock_kernel();
256         oz_mode = autofs4_oz_mode(sbi);
257         DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d\n",
258                  current->pid, process_group(current), sbi->catatonic, oz_mode));
259
260         /*
261          * Mark the dentry incomplete, but add it. This is needed so
262          * that the VFS layer knows about the dentry, and we can count
263          * on catching any lookups through the revalidate.
264          *
265          * Let all the hard work be done by the revalidate function that
266          * needs to be able to do this anyway..
267          *
268          * We need to do this before we release the directory semaphore.
269          */
270         dentry->d_op = &autofs4_root_dentry_operations;
271
272         if (!oz_mode)
273                 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
274         dentry->d_fsdata = NULL;
275         d_add(dentry, NULL);
276
277         if (dentry->d_op && dentry->d_op->d_revalidate) {
278                 up(&dir->i_sem);
279                 (dentry->d_op->d_revalidate)(dentry, nd);
280                 down(&dir->i_sem);
281         }
282
283         /*
284          * If we are still pending, check if we had to handle
285          * a signal. If so we can force a restart..
286          */
287         if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
288                 if (signal_pending(current)) {
289                         unlock_kernel();
290                         return ERR_PTR(-ERESTARTNOINTR);
291                 }
292         }
293         unlock_kernel();
294
295         /*
296          * If this dentry is unhashed, then we shouldn't honour this
297          * lookup even if the dentry is positive.  Returning ENOENT here
298          * doesn't do the right thing for all system calls, but it should
299          * be OK for the operations we permit from an autofs.
300          */
301         if ( dentry->d_inode && d_unhashed(dentry) )
302                 return ERR_PTR(-ENOENT);
303
304         return NULL;
305 }
306
307 static int autofs4_dir_symlink(struct inode *dir, 
308                                struct dentry *dentry,
309                                const char *symname)
310 {
311         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
312         struct autofs_info *ino = autofs4_dentry_ino(dentry);
313         struct inode *inode;
314         char *cp;
315
316         DPRINTK(("autofs_dir_symlink: %s <- %.*s\n", symname, 
317                  dentry->d_name.len, dentry->d_name.name));
318
319         lock_kernel();
320         if (!autofs4_oz_mode(sbi)) {
321                 unlock_kernel();
322                 return -EACCES;
323         }
324
325         ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
326         if (ino == NULL) {
327                 unlock_kernel();
328                 return -ENOSPC;
329         }
330
331         ino->size = strlen(symname);
332         ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
333
334         if (cp == NULL) {
335                 kfree(ino);
336                 unlock_kernel();
337                 return -ENOSPC;
338         }
339
340         strcpy(cp, symname);
341
342         inode = autofs4_get_inode(dir->i_sb, ino);
343         d_instantiate(dentry, inode);
344
345         if (dir == dir->i_sb->s_root->d_inode)
346                 dentry->d_op = &autofs4_root_dentry_operations;
347         else
348                 dentry->d_op = &autofs4_dentry_operations;
349
350         dentry->d_fsdata = ino;
351         ino->dentry = dget(dentry);
352         ino->inode = inode;
353
354         dir->i_mtime = CURRENT_TIME;
355
356         unlock_kernel();
357         return 0;
358 }
359
360 /*
361  * NOTE!
362  *
363  * Normal filesystems would do a "d_delete()" to tell the VFS dcache
364  * that the file no longer exists. However, doing that means that the
365  * VFS layer can turn the dentry into a negative dentry.  We don't want
366  * this, because since the unlink is probably the result of an expire.
367  * We simply d_drop it, which allows the dentry lookup to remount it
368  * if necessary.
369  *
370  * If a process is blocked on the dentry waiting for the expire to finish,
371  * it will invalidate the dentry and try to mount with a new one.
372  *
373  * Also see autofs_dir_rmdir().. 
374  */
375 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
376 {
377         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
378         struct autofs_info *ino = autofs4_dentry_ino(dentry);
379         
380         /* This allows root to remove symlinks */
381         lock_kernel();
382         if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) ) {
383                 unlock_kernel();
384                 return -EACCES;
385         }
386
387         dput(ino->dentry);
388
389         dentry->d_inode->i_size = 0;
390         dentry->d_inode->i_nlink = 0;
391
392         dir->i_mtime = CURRENT_TIME;
393
394         d_drop(dentry);
395
396         unlock_kernel();
397         
398         return 0;
399 }
400
401 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
402 {
403         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
404         struct autofs_info *ino = autofs4_dentry_ino(dentry);
405         
406         lock_kernel();
407         if (!autofs4_oz_mode(sbi)) {
408                 unlock_kernel();
409                 return -EACCES;
410         }
411
412         spin_lock(&dcache_lock);
413         if (!list_empty(&dentry->d_subdirs)) {
414                 spin_unlock(&dcache_lock);
415                 unlock_kernel();
416                 return -ENOTEMPTY;
417         }
418         __d_drop(dentry);
419         spin_unlock(&dcache_lock);
420
421         dput(ino->dentry);
422
423         dentry->d_inode->i_size = 0;
424         dentry->d_inode->i_nlink = 0;
425
426         if (dir->i_nlink)
427                 dir->i_nlink--;
428
429         unlock_kernel();
430         return 0;
431 }
432
433
434
435 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
436 {
437         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
438         struct autofs_info *ino = autofs4_dentry_ino(dentry);
439         struct inode *inode;
440
441         lock_kernel();
442         if ( !autofs4_oz_mode(sbi) ) {
443                 unlock_kernel();
444                 return -EACCES;
445         }
446
447         DPRINTK(("autofs_dir_mkdir: dentry %p, creating %.*s\n",
448                  dentry, dentry->d_name.len, dentry->d_name.name));
449
450         ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
451         if (ino == NULL) {
452                 unlock_kernel();
453                 return -ENOSPC;
454         }
455
456         inode = autofs4_get_inode(dir->i_sb, ino);
457         d_instantiate(dentry, inode);
458
459         if (dir == dir->i_sb->s_root->d_inode)
460                 dentry->d_op = &autofs4_root_dentry_operations;
461         else
462                 dentry->d_op = &autofs4_dentry_operations;
463
464         dentry->d_fsdata = ino;
465         ino->dentry = dget(dentry);
466         ino->inode = inode;
467         dir->i_nlink++;
468         dir->i_mtime = CURRENT_TIME;
469
470         unlock_kernel();
471         return 0;
472 }
473
474 /* Get/set timeout ioctl() operation */
475 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
476                                          unsigned long *p)
477 {
478         int rv;
479         unsigned long ntimeout;
480
481         if ( (rv = get_user(ntimeout, p)) ||
482              (rv = put_user(sbi->exp_timeout/HZ, p)) )
483                 return rv;
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 autofs4_get_protover(struct autofs_sb_info *sbi, int *p)
495 {
496         return put_user(sbi->version, p);
497 }
498
499 /* Identify autofs_dentries - this is so we can tell if there's
500    an extra dentry refcount or not.  We only hold a refcount on the
501    dentry if its non-negative (ie, d_inode != NULL)
502 */
503 int is_autofs4_dentry(struct dentry *dentry)
504 {
505         return dentry && dentry->d_inode &&
506                 (dentry->d_op == &autofs4_root_dentry_operations ||
507                  dentry->d_op == &autofs4_dentry_operations) &&
508                 dentry->d_fsdata != NULL;
509 }
510
511 /*
512  * ioctl()'s on the root directory is the chief method for the daemon to
513  * generate kernel reactions
514  */
515 static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
516                              unsigned int cmd, unsigned long arg)
517 {
518         struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
519
520         DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",
521                  cmd,arg,sbi,process_group(current)));
522
523         if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
524              _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
525                 return -ENOTTY;
526         
527         if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
528                 return -EPERM;
529         
530         switch(cmd) {
531         case AUTOFS_IOC_READY:  /* Wait queue: go ahead and retry */
532                 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
533         case AUTOFS_IOC_FAIL:   /* Wait queue: fail with ENOENT */
534                 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
535         case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
536                 autofs4_catatonic_mode(sbi);
537                 return 0;
538         case AUTOFS_IOC_PROTOVER: /* Get protocol version */
539                 return autofs4_get_protover(sbi, (int *)arg);
540         case AUTOFS_IOC_SETTIMEOUT:
541                 return autofs4_get_set_timeout(sbi,(unsigned long *)arg);
542
543         /* return a single thing to expire */
544         case AUTOFS_IOC_EXPIRE:
545                 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi,
546                                           (struct autofs_packet_expire *)arg);
547         /* same as above, but can send multiple expires through pipe */
548         case AUTOFS_IOC_EXPIRE_MULTI:
549                 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi,
550                                             (int *)arg);
551
552         default:
553                 return -ENOSYS;
554         }
555 }