ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / fs / autofs / root.c
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                 if (signal_pending(current)) {
242                         unlock_kernel();
243                         return ERR_PTR(-ERESTARTNOINTR);
244                 }
245         }
246         unlock_kernel();
247
248         /*
249          * If this dentry is unhashed, then we shouldn't honour this
250          * lookup even if the dentry is positive.  Returning ENOENT here
251          * doesn't do the right thing for all system calls, but it should
252          * be OK for the operations we permit from an autofs.
253          */
254         if ( dentry->d_inode && d_unhashed(dentry) )
255                 return ERR_PTR(-ENOENT);
256
257         return NULL;
258 }
259
260 static int autofs_root_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
261 {
262         struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
263         struct autofs_dirhash *dh = &sbi->dirhash;
264         struct autofs_dir_ent *ent;
265         unsigned int n;
266         int slsize;
267         struct autofs_symlink *sl;
268
269         DPRINTK(("autofs_root_symlink: %s <- ", symname));
270         autofs_say(dentry->d_name.name,dentry->d_name.len);
271
272         lock_kernel();
273         if ( !autofs_oz_mode(sbi) ) {
274                 unlock_kernel();
275                 return -EACCES;
276         }
277
278         if ( autofs_hash_lookup(dh, &dentry->d_name) ) {
279                 unlock_kernel();
280                 return -EEXIST;
281         }
282
283         n = find_first_zero_bit(sbi->symlink_bitmap,AUTOFS_MAX_SYMLINKS);
284         if ( n >= AUTOFS_MAX_SYMLINKS ) {
285                 unlock_kernel();
286                 return -ENOSPC;
287         }
288
289         set_bit(n,sbi->symlink_bitmap);
290         sl = &sbi->symlink[n];
291         sl->len = strlen(symname);
292         sl->data = kmalloc(slsize = sl->len+1, GFP_KERNEL);
293         if ( !sl->data ) {
294                 clear_bit(n,sbi->symlink_bitmap);
295                 unlock_kernel();
296                 return -ENOSPC;
297         }
298
299         ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
300         if ( !ent ) {
301                 kfree(sl->data);
302                 clear_bit(n,sbi->symlink_bitmap);
303                 unlock_kernel();
304                 return -ENOSPC;
305         }
306
307         ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
308         if ( !ent->name ) {
309                 kfree(sl->data);
310                 kfree(ent);
311                 clear_bit(n,sbi->symlink_bitmap);
312                 unlock_kernel();
313                 return -ENOSPC;
314         }
315
316         memcpy(sl->data,symname,slsize);
317         sl->mtime = get_seconds();
318
319         ent->ino = AUTOFS_FIRST_SYMLINK + n;
320         ent->hash = dentry->d_name.hash;
321         memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
322         ent->dentry = NULL;     /* We don't keep the dentry for symlinks */
323
324         autofs_hash_insert(dh,ent);
325         d_instantiate(dentry, iget(dir->i_sb,ent->ino));
326         unlock_kernel();
327         return 0;
328 }
329
330 /*
331  * NOTE!
332  *
333  * Normal filesystems would do a "d_delete()" to tell the VFS dcache
334  * that the file no longer exists. However, doing that means that the
335  * VFS layer can turn the dentry into a negative dentry, which we
336  * obviously do not want (we're dropping the entry not because it
337  * doesn't exist, but because it has timed out).
338  *
339  * Also see autofs_root_rmdir()..
340  */
341 static int autofs_root_unlink(struct inode *dir, struct dentry *dentry)
342 {
343         struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
344         struct autofs_dirhash *dh = &sbi->dirhash;
345         struct autofs_dir_ent *ent;
346         unsigned int n;
347
348         /* This allows root to remove symlinks */
349         lock_kernel();
350         if ( !autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) ) {
351                 unlock_kernel();
352                 return -EACCES;
353         }
354
355         ent = autofs_hash_lookup(dh, &dentry->d_name);
356         if ( !ent ) {
357                 unlock_kernel();
358                 return -ENOENT;
359         }
360
361         n = ent->ino - AUTOFS_FIRST_SYMLINK;
362         if ( n >= AUTOFS_MAX_SYMLINKS ) {
363                 unlock_kernel();
364                 return -EISDIR; /* It's a directory, dummy */
365         }
366         if ( !test_bit(n,sbi->symlink_bitmap) ) {
367                 unlock_kernel();
368                 return -EINVAL; /* Nonexistent symlink?  Shouldn't happen */
369         }
370         
371         dentry->d_time = (unsigned long)(struct autofs_dirhash *)NULL;
372         autofs_hash_delete(ent);
373         clear_bit(n,sbi->symlink_bitmap);
374         kfree(sbi->symlink[n].data);
375         d_drop(dentry);
376         
377         unlock_kernel();
378         return 0;
379 }
380
381 static int autofs_root_rmdir(struct inode *dir, struct dentry *dentry)
382 {
383         struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
384         struct autofs_dirhash *dh = &sbi->dirhash;
385         struct autofs_dir_ent *ent;
386
387         lock_kernel();
388         if ( !autofs_oz_mode(sbi) ) {
389                 unlock_kernel();
390                 return -EACCES;
391         }
392
393         ent = autofs_hash_lookup(dh, &dentry->d_name);
394         if ( !ent ) {
395                 unlock_kernel();
396                 return -ENOENT;
397         }
398
399         if ( (unsigned int)ent->ino < AUTOFS_FIRST_DIR_INO ) {
400                 unlock_kernel();
401                 return -ENOTDIR; /* Not a directory */
402         }
403
404         if ( ent->dentry != dentry ) {
405                 printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry->d_name.name);
406         }
407
408         dentry->d_time = (unsigned long)(struct autofs_dir_ent *)NULL;
409         autofs_hash_delete(ent);
410         dir->i_nlink--;
411         d_drop(dentry);
412         unlock_kernel();
413
414         return 0;
415 }
416
417 static int autofs_root_mkdir(struct inode *dir, struct dentry *dentry, int mode)
418 {
419         struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
420         struct autofs_dirhash *dh = &sbi->dirhash;
421         struct autofs_dir_ent *ent;
422         ino_t ino;
423
424         lock_kernel();
425         if ( !autofs_oz_mode(sbi) ) {
426                 unlock_kernel();
427                 return -EACCES;
428         }
429
430         ent = autofs_hash_lookup(dh, &dentry->d_name);
431         if ( ent ) {
432                 unlock_kernel();
433                 return -EEXIST;
434         }
435
436         if ( sbi->next_dir_ino < AUTOFS_FIRST_DIR_INO ) {
437                 printk("autofs: Out of inode numbers -- what the heck did you do??\n");
438                 unlock_kernel();
439                 return -ENOSPC;
440         }
441         ino = sbi->next_dir_ino++;
442
443         ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
444         if ( !ent ) {
445                 unlock_kernel();
446                 return -ENOSPC;
447         }
448
449         ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
450         if ( !ent->name ) {
451                 kfree(ent);
452                 unlock_kernel();
453                 return -ENOSPC;
454         }
455
456         ent->hash = dentry->d_name.hash;
457         memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
458         ent->ino = ino;
459         ent->dentry = dentry;
460         autofs_hash_insert(dh,ent);
461
462         dir->i_nlink++;
463         d_instantiate(dentry, iget(dir->i_sb,ino));
464         unlock_kernel();
465
466         return 0;
467 }
468
469 /* Get/set timeout ioctl() operation */
470 static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
471                                          unsigned long *p)
472 {
473         unsigned long ntimeout;
474
475         if (get_user(ntimeout, p) ||
476             put_user(sbi->exp_timeout / HZ, p))
477                 return -EFAULT;
478
479         if ( ntimeout > ULONG_MAX/HZ )
480                 sbi->exp_timeout = 0;
481         else
482                 sbi->exp_timeout = ntimeout * HZ;
483
484         return 0;
485 }
486
487 /* Return protocol version */
488 static inline int autofs_get_protover(int *p)
489 {
490         return put_user(AUTOFS_PROTO_VERSION, p);
491 }
492
493 /* Perform an expiry operation */
494 static inline int autofs_expire_run(struct super_block *sb,
495                                     struct autofs_sb_info *sbi,
496                                     struct vfsmount *mnt,
497                                     struct autofs_packet_expire *pkt_p)
498 {
499         struct autofs_dir_ent *ent;
500         struct autofs_packet_expire pkt;
501
502         memset(&pkt,0,sizeof pkt);
503
504         pkt.hdr.proto_version = AUTOFS_PROTO_VERSION;
505         pkt.hdr.type = autofs_ptype_expire;
506
507         if ( !sbi->exp_timeout ||
508              !(ent = autofs_expire(sb,sbi,mnt)) )
509                 return -EAGAIN;
510
511         pkt.len = ent->len;
512         memcpy(pkt.name, ent->name, pkt.len);
513         pkt.name[pkt.len] = '\0';
514
515         if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
516                 return -EFAULT;
517
518         return 0;
519 }
520
521 /*
522  * ioctl()'s on the root directory is the chief method for the daemon to
523  * generate kernel reactions
524  */
525 static int autofs_root_ioctl(struct inode *inode, struct file *filp,
526                              unsigned int cmd, unsigned long arg)
527 {
528         struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
529
530         DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd,arg,sbi,process_group(current)));
531
532         if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
533              _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
534                 return -ENOTTY;
535         
536         if ( !autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
537                 return -EPERM;
538         
539         switch(cmd) {
540         case AUTOFS_IOC_READY:  /* Wait queue: go ahead and retry */
541                 return autofs_wait_release(sbi,(autofs_wqt_t)arg,0);
542         case AUTOFS_IOC_FAIL:   /* Wait queue: fail with ENOENT */
543                 return autofs_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
544         case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
545                 autofs_catatonic_mode(sbi);
546                 return 0;
547         case AUTOFS_IOC_PROTOVER: /* Get protocol version */
548                 return autofs_get_protover((int *)arg);
549         case AUTOFS_IOC_SETTIMEOUT:
550                 return autofs_get_set_timeout(sbi,(unsigned long *)arg);
551         case AUTOFS_IOC_EXPIRE:
552                 return autofs_expire_run(inode->i_sb, sbi, filp->f_vfsmnt,
553                                          (struct autofs_packet_expire *)arg);
554         default:
555                 return -ENOSYS;
556         }
557 }