~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/fs/autofs4/root.c

Version: ~ [ 2.4.0 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.