xref: /linux/fs/autofs/root.c (revision 092e0e7e520a1fca03e13c9f2d157432a8657ff2)
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/capability.h>
14 #include <linux/errno.h>
15 #include <linux/stat.h>
16 #include <linux/slab.h>
17 #include <linux/param.h>
18 #include <linux/time.h>
19 #include <linux/compat.h>
20 #include <linux/smp_lock.h>
21 #include "autofs_i.h"
22 
23 static int autofs_root_readdir(struct file *,void *,filldir_t);
24 static struct dentry *autofs_root_lookup(struct inode *,struct dentry *, struct nameidata *);
25 static int autofs_root_symlink(struct inode *,struct dentry *,const char *);
26 static int autofs_root_unlink(struct inode *,struct dentry *);
27 static int autofs_root_rmdir(struct inode *,struct dentry *);
28 static int autofs_root_mkdir(struct inode *,struct dentry *,int);
29 static long autofs_root_ioctl(struct file *,unsigned int,unsigned long);
30 #ifdef CONFIG_COMPAT
31 static long autofs_root_compat_ioctl(struct file *,unsigned int,unsigned long);
32 #endif
33 
34 const struct file_operations autofs_root_operations = {
35 	.llseek		= generic_file_llseek,
36 	.read		= generic_read_dir,
37 	.readdir	= autofs_root_readdir,
38 	.unlocked_ioctl	= autofs_root_ioctl,
39 #ifdef CONFIG_COMPAT
40 	.compat_ioctl	= autofs_root_compat_ioctl,
41 #endif
42 };
43 
44 const struct inode_operations autofs_root_inode_operations = {
45         .lookup		= autofs_root_lookup,
46         .unlink		= autofs_root_unlink,
47         .symlink	= autofs_root_symlink,
48         .mkdir		= autofs_root_mkdir,
49         .rmdir		= autofs_root_rmdir,
50 };
51 
52 static int autofs_root_readdir(struct file *filp, void *dirent, filldir_t filldir)
53 {
54 	struct autofs_dir_ent *ent = NULL;
55 	struct autofs_dirhash *dirhash;
56 	struct autofs_sb_info *sbi;
57 	struct inode * inode = filp->f_path.dentry->d_inode;
58 	off_t onr, nr;
59 
60 	lock_kernel();
61 
62 	sbi = autofs_sbi(inode->i_sb);
63 	dirhash = &sbi->dirhash;
64 	nr = filp->f_pos;
65 
66 	switch(nr)
67 	{
68 	case 0:
69 		if (filldir(dirent, ".", 1, nr, inode->i_ino, DT_DIR) < 0)
70 			goto out;
71 		filp->f_pos = ++nr;
72 		/* fall through */
73 	case 1:
74 		if (filldir(dirent, "..", 2, nr, inode->i_ino, DT_DIR) < 0)
75 			goto out;
76 		filp->f_pos = ++nr;
77 		/* fall through */
78 	default:
79 		while (onr = nr, ent = autofs_hash_enum(dirhash,&nr,ent)) {
80 			if (!ent->dentry || d_mountpoint(ent->dentry)) {
81 				if (filldir(dirent,ent->name,ent->len,onr,ent->ino,DT_UNKNOWN) < 0)
82 					goto out;
83 				filp->f_pos = nr;
84 			}
85 		}
86 		break;
87 	}
88 
89 out:
90 	unlock_kernel();
91 	return 0;
92 }
93 
94 static int try_to_fill_dentry(struct dentry *dentry, struct super_block *sb, struct autofs_sb_info *sbi)
95 {
96 	struct inode * inode;
97 	struct autofs_dir_ent *ent;
98 	int status = 0;
99 
100 	if (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name))) {
101 		do {
102 			if (status && dentry->d_inode) {
103 				if (status != -ENOENT)
104 					printk("autofs warning: lookup failure on positive dentry, status = %d, name = %s\n", status, dentry->d_name.name);
105 				return 0; /* Try to get the kernel to invalidate this dentry */
106 			}
107 
108 			/* Turn this into a real negative dentry? */
109 			if (status == -ENOENT) {
110 				dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
111 				dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
112 				return 1;
113 			} else if (status) {
114 				/* Return a negative dentry, but leave it "pending" */
115 				return 1;
116 			}
117 			status = autofs_wait(sbi, &dentry->d_name);
118 		} while (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name)));
119 	}
120 
121 	/* Abuse this field as a pointer to the directory entry, used to
122 	   find the expire list pointers */
123 	dentry->d_time = (unsigned long) ent;
124 
125 	if (!dentry->d_inode) {
126 		inode = autofs_iget(sb, ent->ino);
127 		if (IS_ERR(inode)) {
128 			/* Failed, but leave pending for next time */
129 			return 1;
130 		}
131 		dentry->d_inode = inode;
132 	}
133 
134 	/* If this is a directory that isn't a mount point, bitch at the
135 	   daemon and fix it in user space */
136 	if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry)) {
137 		return !autofs_wait(sbi, &dentry->d_name);
138 	}
139 
140 	/* We don't update the usages for the autofs daemon itself, this
141 	   is necessary for recursive autofs mounts */
142 	if (!autofs_oz_mode(sbi)) {
143 		autofs_update_usage(&sbi->dirhash,ent);
144 	}
145 
146 	dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
147 	return 1;
148 }
149 
150 
151 /*
152  * Revalidate is called on every cache lookup.  Some of those
153  * cache lookups may actually happen while the dentry is not
154  * yet completely filled in, and revalidate has to delay such
155  * lookups..
156  */
157 static int autofs_revalidate(struct dentry * dentry, struct nameidata *nd)
158 {
159 	struct inode * dir;
160 	struct autofs_sb_info *sbi;
161 	struct autofs_dir_ent *ent;
162 	int res;
163 
164 	lock_kernel();
165 	dir = dentry->d_parent->d_inode;
166 	sbi = autofs_sbi(dir->i_sb);
167 
168 	/* Pending dentry */
169 	if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
170 		if (autofs_oz_mode(sbi))
171 			res = 1;
172 		else
173 			res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
174 		unlock_kernel();
175 		return res;
176 	}
177 
178 	/* Negative dentry.. invalidate if "old" */
179 	if (!dentry->d_inode) {
180 		unlock_kernel();
181 		return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
182 	}
183 
184 	/* Check for a non-mountpoint directory */
185 	if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry)) {
186 		if (autofs_oz_mode(sbi))
187 			res = 1;
188 		else
189 			res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
190 		unlock_kernel();
191 		return res;
192 	}
193 
194 	/* Update the usage list */
195 	if (!autofs_oz_mode(sbi)) {
196 		ent = (struct autofs_dir_ent *) dentry->d_time;
197 		if (ent)
198 			autofs_update_usage(&sbi->dirhash,ent);
199 	}
200 	unlock_kernel();
201 	return 1;
202 }
203 
204 static const struct dentry_operations autofs_dentry_operations = {
205 	.d_revalidate	= autofs_revalidate,
206 };
207 
208 static struct dentry *autofs_root_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
209 {
210 	struct autofs_sb_info *sbi;
211 	int oz_mode;
212 
213 	DPRINTK(("autofs_root_lookup: name = "));
214 	lock_kernel();
215 	autofs_say(dentry->d_name.name,dentry->d_name.len);
216 
217 	if (dentry->d_name.len > NAME_MAX) {
218 		unlock_kernel();
219 		return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
220 	}
221 
222 	sbi = autofs_sbi(dir->i_sb);
223 
224 	oz_mode = autofs_oz_mode(sbi);
225 	DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, "
226 				"oz_mode = %d\n", task_pid_nr(current),
227 				task_pgrp_nr(current), sbi->catatonic,
228 				oz_mode));
229 
230 	/*
231 	 * Mark the dentry incomplete, but add it. This is needed so
232 	 * that the VFS layer knows about the dentry, and we can count
233 	 * on catching any lookups through the revalidate.
234 	 *
235 	 * Let all the hard work be done by the revalidate function that
236 	 * needs to be able to do this anyway..
237 	 *
238 	 * We need to do this before we release the directory semaphore.
239 	 */
240 	dentry->d_op = &autofs_dentry_operations;
241 	dentry->d_flags |= DCACHE_AUTOFS_PENDING;
242 	d_add(dentry, NULL);
243 
244 	mutex_unlock(&dir->i_mutex);
245 	autofs_revalidate(dentry, nd);
246 	mutex_lock(&dir->i_mutex);
247 
248 	/*
249 	 * If we are still pending, check if we had to handle
250 	 * a signal. If so we can force a restart..
251 	 */
252 	if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
253 		/* See if we were interrupted */
254 		if (signal_pending(current)) {
255 			sigset_t *sigset = &current->pending.signal;
256 			if (sigismember (sigset, SIGKILL) ||
257 			    sigismember (sigset, SIGQUIT) ||
258 			    sigismember (sigset, SIGINT)) {
259 				unlock_kernel();
260 				return ERR_PTR(-ERESTARTNOINTR);
261 			}
262 		}
263 	}
264 	unlock_kernel();
265 
266 	/*
267 	 * If this dentry is unhashed, then we shouldn't honour this
268 	 * lookup even if the dentry is positive.  Returning ENOENT here
269 	 * doesn't do the right thing for all system calls, but it should
270 	 * be OK for the operations we permit from an autofs.
271 	 */
272 	if (dentry->d_inode && d_unhashed(dentry))
273 		return ERR_PTR(-ENOENT);
274 
275 	return NULL;
276 }
277 
278 static int autofs_root_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
279 {
280 	struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
281 	struct autofs_dirhash *dh = &sbi->dirhash;
282 	struct autofs_dir_ent *ent;
283 	unsigned int n;
284 	int slsize;
285 	struct autofs_symlink *sl;
286 	struct inode *inode;
287 
288 	DPRINTK(("autofs_root_symlink: %s <- ", symname));
289 	autofs_say(dentry->d_name.name,dentry->d_name.len);
290 
291 	lock_kernel();
292 	if (!autofs_oz_mode(sbi)) {
293 		unlock_kernel();
294 		return -EACCES;
295 	}
296 
297 	if (autofs_hash_lookup(dh, &dentry->d_name)) {
298 		unlock_kernel();
299 		return -EEXIST;
300 	}
301 
302 	n = find_first_zero_bit(sbi->symlink_bitmap,AUTOFS_MAX_SYMLINKS);
303 	if (n >= AUTOFS_MAX_SYMLINKS) {
304 		unlock_kernel();
305 		return -ENOSPC;
306 	}
307 
308 	set_bit(n,sbi->symlink_bitmap);
309 	sl = &sbi->symlink[n];
310 	sl->len = strlen(symname);
311 	sl->data = kmalloc(slsize = sl->len+1, GFP_KERNEL);
312 	if (!sl->data) {
313 		clear_bit(n,sbi->symlink_bitmap);
314 		unlock_kernel();
315 		return -ENOSPC;
316 	}
317 
318 	ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
319 	if (!ent) {
320 		kfree(sl->data);
321 		clear_bit(n,sbi->symlink_bitmap);
322 		unlock_kernel();
323 		return -ENOSPC;
324 	}
325 
326 	ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
327 	if (!ent->name) {
328 		kfree(sl->data);
329 		kfree(ent);
330 		clear_bit(n,sbi->symlink_bitmap);
331 		unlock_kernel();
332 		return -ENOSPC;
333 	}
334 
335 	memcpy(sl->data,symname,slsize);
336 	sl->mtime = get_seconds();
337 
338 	ent->ino = AUTOFS_FIRST_SYMLINK + n;
339 	ent->hash = dentry->d_name.hash;
340 	memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
341 	ent->dentry = NULL;	/* We don't keep the dentry for symlinks */
342 
343 	autofs_hash_insert(dh,ent);
344 
345 	inode = autofs_iget(dir->i_sb, ent->ino);
346 	if (IS_ERR(inode))
347 		return PTR_ERR(inode);
348 
349 	d_instantiate(dentry, inode);
350 	unlock_kernel();
351 	return 0;
352 }
353 
354 /*
355  * NOTE!
356  *
357  * Normal filesystems would do a "d_delete()" to tell the VFS dcache
358  * that the file no longer exists. However, doing that means that the
359  * VFS layer can turn the dentry into a negative dentry, which we
360  * obviously do not want (we're dropping the entry not because it
361  * doesn't exist, but because it has timed out).
362  *
363  * Also see autofs_root_rmdir()..
364  */
365 static int autofs_root_unlink(struct inode *dir, struct dentry *dentry)
366 {
367 	struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
368 	struct autofs_dirhash *dh = &sbi->dirhash;
369 	struct autofs_dir_ent *ent;
370 	unsigned int n;
371 
372 	/* This allows root to remove symlinks */
373 	lock_kernel();
374 	if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) {
375 		unlock_kernel();
376 		return -EACCES;
377 	}
378 
379 	ent = autofs_hash_lookup(dh, &dentry->d_name);
380 	if (!ent) {
381 		unlock_kernel();
382 		return -ENOENT;
383 	}
384 
385 	n = ent->ino - AUTOFS_FIRST_SYMLINK;
386 	if (n >= AUTOFS_MAX_SYMLINKS) {
387 		unlock_kernel();
388 		return -EISDIR;	/* It's a directory, dummy */
389 	}
390 	if (!test_bit(n,sbi->symlink_bitmap)) {
391 		unlock_kernel();
392 		return -EINVAL;	/* Nonexistent symlink?  Shouldn't happen */
393 	}
394 
395 	dentry->d_time = (unsigned long)(struct autofs_dirhash *)NULL;
396 	autofs_hash_delete(ent);
397 	clear_bit(n,sbi->symlink_bitmap);
398 	kfree(sbi->symlink[n].data);
399 	d_drop(dentry);
400 
401 	unlock_kernel();
402 	return 0;
403 }
404 
405 static int autofs_root_rmdir(struct inode *dir, struct dentry *dentry)
406 {
407 	struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
408 	struct autofs_dirhash *dh = &sbi->dirhash;
409 	struct autofs_dir_ent *ent;
410 
411 	lock_kernel();
412 	if (!autofs_oz_mode(sbi)) {
413 		unlock_kernel();
414 		return -EACCES;
415 	}
416 
417 	ent = autofs_hash_lookup(dh, &dentry->d_name);
418 	if (!ent) {
419 		unlock_kernel();
420 		return -ENOENT;
421 	}
422 
423 	if ((unsigned int)ent->ino < AUTOFS_FIRST_DIR_INO) {
424 		unlock_kernel();
425 		return -ENOTDIR; /* Not a directory */
426 	}
427 
428 	if (ent->dentry != dentry) {
429 		printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry->d_name.name);
430 	}
431 
432 	dentry->d_time = (unsigned long)(struct autofs_dir_ent *)NULL;
433 	autofs_hash_delete(ent);
434 	drop_nlink(dir);
435 	d_drop(dentry);
436 	unlock_kernel();
437 
438 	return 0;
439 }
440 
441 static int autofs_root_mkdir(struct inode *dir, struct dentry *dentry, int mode)
442 {
443 	struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
444 	struct autofs_dirhash *dh = &sbi->dirhash;
445 	struct autofs_dir_ent *ent;
446 	struct inode *inode;
447 	ino_t ino;
448 
449 	lock_kernel();
450 	if (!autofs_oz_mode(sbi)) {
451 		unlock_kernel();
452 		return -EACCES;
453 	}
454 
455 	ent = autofs_hash_lookup(dh, &dentry->d_name);
456 	if (ent) {
457 		unlock_kernel();
458 		return -EEXIST;
459 	}
460 
461 	if (sbi->next_dir_ino < AUTOFS_FIRST_DIR_INO) {
462 		printk("autofs: Out of inode numbers -- what the heck did you do??\n");
463 		unlock_kernel();
464 		return -ENOSPC;
465 	}
466 	ino = sbi->next_dir_ino++;
467 
468 	ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
469 	if (!ent) {
470 		unlock_kernel();
471 		return -ENOSPC;
472 	}
473 
474 	ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
475 	if (!ent->name) {
476 		kfree(ent);
477 		unlock_kernel();
478 		return -ENOSPC;
479 	}
480 
481 	ent->hash = dentry->d_name.hash;
482 	memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
483 	ent->ino = ino;
484 	ent->dentry = dentry;
485 	autofs_hash_insert(dh,ent);
486 
487 	inc_nlink(dir);
488 
489 	inode = autofs_iget(dir->i_sb, ino);
490 	if (IS_ERR(inode)) {
491 		drop_nlink(dir);
492 		return PTR_ERR(inode);
493 	}
494 
495 	d_instantiate(dentry, inode);
496 	unlock_kernel();
497 
498 	return 0;
499 }
500 
501 /* Get/set timeout ioctl() operation */
502 #ifdef CONFIG_COMPAT
503 static inline int autofs_compat_get_set_timeout(struct autofs_sb_info *sbi,
504 					 unsigned int __user *p)
505 {
506 	unsigned long ntimeout;
507 
508 	if (get_user(ntimeout, p) ||
509 	    put_user(sbi->exp_timeout / HZ, p))
510 		return -EFAULT;
511 
512 	if (ntimeout > UINT_MAX/HZ)
513 		sbi->exp_timeout = 0;
514 	else
515 		sbi->exp_timeout = ntimeout * HZ;
516 
517 	return 0;
518 }
519 #endif
520 
521 static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
522 					 unsigned long __user *p)
523 {
524 	unsigned long ntimeout;
525 
526 	if (get_user(ntimeout, p) ||
527 	    put_user(sbi->exp_timeout / HZ, p))
528 		return -EFAULT;
529 
530 	if (ntimeout > ULONG_MAX/HZ)
531 		sbi->exp_timeout = 0;
532 	else
533 		sbi->exp_timeout = ntimeout * HZ;
534 
535 	return 0;
536 }
537 
538 /* Return protocol version */
539 static inline int autofs_get_protover(int __user *p)
540 {
541 	return put_user(AUTOFS_PROTO_VERSION, p);
542 }
543 
544 /* Perform an expiry operation */
545 static inline int autofs_expire_run(struct super_block *sb,
546 				    struct autofs_sb_info *sbi,
547 				    struct vfsmount *mnt,
548 				    struct autofs_packet_expire __user *pkt_p)
549 {
550 	struct autofs_dir_ent *ent;
551 	struct autofs_packet_expire pkt;
552 
553 	memset(&pkt,0,sizeof pkt);
554 
555 	pkt.hdr.proto_version = AUTOFS_PROTO_VERSION;
556 	pkt.hdr.type = autofs_ptype_expire;
557 
558 	if (!sbi->exp_timeout || !(ent = autofs_expire(sb,sbi,mnt)))
559 		return -EAGAIN;
560 
561 	pkt.len = ent->len;
562 	memcpy(pkt.name, ent->name, pkt.len);
563 	pkt.name[pkt.len] = '\0';
564 
565 	if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
566 		return -EFAULT;
567 
568 	return 0;
569 }
570 
571 /*
572  * ioctl()'s on the root directory is the chief method for the daemon to
573  * generate kernel reactions
574  */
575 static int autofs_do_root_ioctl(struct inode *inode, struct file *filp,
576 			     unsigned int cmd, unsigned long arg)
577 {
578 	struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
579 	void __user *argp = (void __user *)arg;
580 
581 	DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd,arg,sbi,task_pgrp_nr(current)));
582 
583 	if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
584 	     _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
585 		return -ENOTTY;
586 
587 	if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
588 		return -EPERM;
589 
590 	switch(cmd) {
591 	case AUTOFS_IOC_READY:	/* Wait queue: go ahead and retry */
592 		return autofs_wait_release(sbi,(autofs_wqt_t)arg,0);
593 	case AUTOFS_IOC_FAIL:	/* Wait queue: fail with ENOENT */
594 		return autofs_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
595 	case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
596 		autofs_catatonic_mode(sbi);
597 		return 0;
598 	case AUTOFS_IOC_PROTOVER: /* Get protocol version */
599 		return autofs_get_protover(argp);
600 #ifdef CONFIG_COMPAT
601 	case AUTOFS_IOC_SETTIMEOUT32:
602 		return autofs_compat_get_set_timeout(sbi, argp);
603 #endif
604 	case AUTOFS_IOC_SETTIMEOUT:
605 		return autofs_get_set_timeout(sbi, argp);
606 	case AUTOFS_IOC_EXPIRE:
607 		return autofs_expire_run(inode->i_sb, sbi, filp->f_path.mnt,
608 					 argp);
609 	default:
610 		return -ENOSYS;
611 	}
612 
613 }
614 
615 static long autofs_root_ioctl(struct file *filp,
616 			     unsigned int cmd, unsigned long arg)
617 {
618 	int ret;
619 
620 	lock_kernel();
621 	ret = autofs_do_root_ioctl(filp->f_path.dentry->d_inode,
622 				   filp, cmd, arg);
623 	unlock_kernel();
624 
625 	return ret;
626 }
627 
628 #ifdef CONFIG_COMPAT
629 static long autofs_root_compat_ioctl(struct file *filp,
630 			     unsigned int cmd, unsigned long arg)
631 {
632 	struct inode *inode = filp->f_path.dentry->d_inode;
633 	int ret;
634 
635 	lock_kernel();
636 	if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
637 		ret = autofs_do_root_ioctl(inode, filp, cmd, arg);
638 	else
639 		ret = autofs_do_root_ioctl(inode, filp, cmd,
640 			(unsigned long)compat_ptr(arg));
641 	unlock_kernel();
642 
643 	return ret;
644 }
645 #endif
646