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