fs: rcu-walk aware d_revalidate method
[deliverable/linux.git] / drivers / staging / autofs / root.c
1 /* -*- linux-c -*- --------------------------------------------------------- *
2 *
3 * drivers/staging/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 if (nd->flags & LOOKUP_RCU)
165 return -ECHILD;
166
167 lock_kernel();
168 dir = dentry->d_parent->d_inode;
169 sbi = autofs_sbi(dir->i_sb);
170
171 /* Pending dentry */
172 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
173 if (autofs_oz_mode(sbi))
174 res = 1;
175 else
176 res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
177 unlock_kernel();
178 return res;
179 }
180
181 /* Negative dentry.. invalidate if "old" */
182 if (!dentry->d_inode) {
183 unlock_kernel();
184 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
185 }
186
187 /* Check for a non-mountpoint directory */
188 if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry)) {
189 if (autofs_oz_mode(sbi))
190 res = 1;
191 else
192 res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
193 unlock_kernel();
194 return res;
195 }
196
197 /* Update the usage list */
198 if (!autofs_oz_mode(sbi)) {
199 ent = (struct autofs_dir_ent *) dentry->d_time;
200 if (ent)
201 autofs_update_usage(&sbi->dirhash,ent);
202 }
203 unlock_kernel();
204 return 1;
205 }
206
207 static const struct dentry_operations autofs_dentry_operations = {
208 .d_revalidate = autofs_revalidate,
209 };
210
211 static struct dentry *autofs_root_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
212 {
213 struct autofs_sb_info *sbi;
214 int oz_mode;
215
216 DPRINTK(("autofs_root_lookup: name = "));
217 lock_kernel();
218 autofs_say(dentry->d_name.name,dentry->d_name.len);
219
220 if (dentry->d_name.len > NAME_MAX) {
221 unlock_kernel();
222 return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
223 }
224
225 sbi = autofs_sbi(dir->i_sb);
226
227 oz_mode = autofs_oz_mode(sbi);
228 DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, "
229 "oz_mode = %d\n", task_pid_nr(current),
230 task_pgrp_nr(current), sbi->catatonic,
231 oz_mode));
232
233 /*
234 * Mark the dentry incomplete, but add it. This is needed so
235 * that the VFS layer knows about the dentry, and we can count
236 * on catching any lookups through the revalidate.
237 *
238 * Let all the hard work be done by the revalidate function that
239 * needs to be able to do this anyway..
240 *
241 * We need to do this before we release the directory semaphore.
242 */
243 d_set_d_op(dentry, &autofs_dentry_operations);
244 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
245 d_add(dentry, NULL);
246
247 mutex_unlock(&dir->i_mutex);
248 autofs_revalidate(dentry, nd);
249 mutex_lock(&dir->i_mutex);
250
251 /*
252 * If we are still pending, check if we had to handle
253 * a signal. If so we can force a restart..
254 */
255 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
256 /* See if we were interrupted */
257 if (signal_pending(current)) {
258 sigset_t *sigset = &current->pending.signal;
259 if (sigismember (sigset, SIGKILL) ||
260 sigismember (sigset, SIGQUIT) ||
261 sigismember (sigset, SIGINT)) {
262 unlock_kernel();
263 return ERR_PTR(-ERESTARTNOINTR);
264 }
265 }
266 }
267 unlock_kernel();
268
269 /*
270 * If this dentry is unhashed, then we shouldn't honour this
271 * lookup even if the dentry is positive. Returning ENOENT here
272 * doesn't do the right thing for all system calls, but it should
273 * be OK for the operations we permit from an autofs.
274 */
275 if (dentry->d_inode && d_unhashed(dentry))
276 return ERR_PTR(-ENOENT);
277
278 return NULL;
279 }
280
281 static int autofs_root_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
282 {
283 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
284 struct autofs_dirhash *dh = &sbi->dirhash;
285 struct autofs_dir_ent *ent;
286 unsigned int n;
287 int slsize;
288 struct autofs_symlink *sl;
289 struct inode *inode;
290
291 DPRINTK(("autofs_root_symlink: %s <- ", symname));
292 autofs_say(dentry->d_name.name,dentry->d_name.len);
293
294 lock_kernel();
295 if (!autofs_oz_mode(sbi)) {
296 unlock_kernel();
297 return -EACCES;
298 }
299
300 if (autofs_hash_lookup(dh, &dentry->d_name)) {
301 unlock_kernel();
302 return -EEXIST;
303 }
304
305 n = find_first_zero_bit(sbi->symlink_bitmap,AUTOFS_MAX_SYMLINKS);
306 if (n >= AUTOFS_MAX_SYMLINKS) {
307 unlock_kernel();
308 return -ENOSPC;
309 }
310
311 set_bit(n,sbi->symlink_bitmap);
312 sl = &sbi->symlink[n];
313 sl->len = strlen(symname);
314 sl->data = kmalloc(slsize = sl->len+1, GFP_KERNEL);
315 if (!sl->data) {
316 clear_bit(n,sbi->symlink_bitmap);
317 unlock_kernel();
318 return -ENOSPC;
319 }
320
321 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
322 if (!ent) {
323 kfree(sl->data);
324 clear_bit(n,sbi->symlink_bitmap);
325 unlock_kernel();
326 return -ENOSPC;
327 }
328
329 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
330 if (!ent->name) {
331 kfree(sl->data);
332 kfree(ent);
333 clear_bit(n,sbi->symlink_bitmap);
334 unlock_kernel();
335 return -ENOSPC;
336 }
337
338 memcpy(sl->data,symname,slsize);
339 sl->mtime = get_seconds();
340
341 ent->ino = AUTOFS_FIRST_SYMLINK + n;
342 ent->hash = dentry->d_name.hash;
343 memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
344 ent->dentry = NULL; /* We don't keep the dentry for symlinks */
345
346 autofs_hash_insert(dh,ent);
347
348 inode = autofs_iget(dir->i_sb, ent->ino);
349 if (IS_ERR(inode))
350 return PTR_ERR(inode);
351
352 d_instantiate(dentry, inode);
353 unlock_kernel();
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, which we
363 * obviously do not want (we're dropping the entry not because it
364 * doesn't exist, but because it has timed out).
365 *
366 * Also see autofs_root_rmdir()..
367 */
368 static int autofs_root_unlink(struct inode *dir, struct dentry *dentry)
369 {
370 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
371 struct autofs_dirhash *dh = &sbi->dirhash;
372 struct autofs_dir_ent *ent;
373 unsigned int n;
374
375 /* This allows root to remove symlinks */
376 lock_kernel();
377 if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) {
378 unlock_kernel();
379 return -EACCES;
380 }
381
382 ent = autofs_hash_lookup(dh, &dentry->d_name);
383 if (!ent) {
384 unlock_kernel();
385 return -ENOENT;
386 }
387
388 n = ent->ino - AUTOFS_FIRST_SYMLINK;
389 if (n >= AUTOFS_MAX_SYMLINKS) {
390 unlock_kernel();
391 return -EISDIR; /* It's a directory, dummy */
392 }
393 if (!test_bit(n,sbi->symlink_bitmap)) {
394 unlock_kernel();
395 return -EINVAL; /* Nonexistent symlink? Shouldn't happen */
396 }
397
398 dentry->d_time = (unsigned long)(struct autofs_dirhash *)NULL;
399 autofs_hash_delete(ent);
400 clear_bit(n,sbi->symlink_bitmap);
401 kfree(sbi->symlink[n].data);
402 d_drop(dentry);
403
404 unlock_kernel();
405 return 0;
406 }
407
408 static int autofs_root_rmdir(struct inode *dir, struct dentry *dentry)
409 {
410 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
411 struct autofs_dirhash *dh = &sbi->dirhash;
412 struct autofs_dir_ent *ent;
413
414 lock_kernel();
415 if (!autofs_oz_mode(sbi)) {
416 unlock_kernel();
417 return -EACCES;
418 }
419
420 ent = autofs_hash_lookup(dh, &dentry->d_name);
421 if (!ent) {
422 unlock_kernel();
423 return -ENOENT;
424 }
425
426 if ((unsigned int)ent->ino < AUTOFS_FIRST_DIR_INO) {
427 unlock_kernel();
428 return -ENOTDIR; /* Not a directory */
429 }
430
431 if (ent->dentry != dentry) {
432 printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry->d_name.name);
433 }
434
435 dentry->d_time = (unsigned long)(struct autofs_dir_ent *)NULL;
436 autofs_hash_delete(ent);
437 drop_nlink(dir);
438 d_drop(dentry);
439 unlock_kernel();
440
441 return 0;
442 }
443
444 static int autofs_root_mkdir(struct inode *dir, struct dentry *dentry, int mode)
445 {
446 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
447 struct autofs_dirhash *dh = &sbi->dirhash;
448 struct autofs_dir_ent *ent;
449 struct inode *inode;
450 ino_t ino;
451
452 lock_kernel();
453 if (!autofs_oz_mode(sbi)) {
454 unlock_kernel();
455 return -EACCES;
456 }
457
458 ent = autofs_hash_lookup(dh, &dentry->d_name);
459 if (ent) {
460 unlock_kernel();
461 return -EEXIST;
462 }
463
464 if (sbi->next_dir_ino < AUTOFS_FIRST_DIR_INO) {
465 printk("autofs: Out of inode numbers -- what the heck did you do??\n");
466 unlock_kernel();
467 return -ENOSPC;
468 }
469 ino = sbi->next_dir_ino++;
470
471 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
472 if (!ent) {
473 unlock_kernel();
474 return -ENOSPC;
475 }
476
477 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
478 if (!ent->name) {
479 kfree(ent);
480 unlock_kernel();
481 return -ENOSPC;
482 }
483
484 ent->hash = dentry->d_name.hash;
485 memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
486 ent->ino = ino;
487 ent->dentry = dentry;
488 autofs_hash_insert(dh,ent);
489
490 inc_nlink(dir);
491
492 inode = autofs_iget(dir->i_sb, ino);
493 if (IS_ERR(inode)) {
494 drop_nlink(dir);
495 return PTR_ERR(inode);
496 }
497
498 d_instantiate(dentry, inode);
499 unlock_kernel();
500
501 return 0;
502 }
503
504 /* Get/set timeout ioctl() operation */
505 #ifdef CONFIG_COMPAT
506 static inline int autofs_compat_get_set_timeout(struct autofs_sb_info *sbi,
507 unsigned int __user *p)
508 {
509 unsigned long ntimeout;
510
511 if (get_user(ntimeout, p) ||
512 put_user(sbi->exp_timeout / HZ, p))
513 return -EFAULT;
514
515 if (ntimeout > UINT_MAX/HZ)
516 sbi->exp_timeout = 0;
517 else
518 sbi->exp_timeout = ntimeout * HZ;
519
520 return 0;
521 }
522 #endif
523
524 static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
525 unsigned long __user *p)
526 {
527 unsigned long ntimeout;
528
529 if (get_user(ntimeout, p) ||
530 put_user(sbi->exp_timeout / HZ, p))
531 return -EFAULT;
532
533 if (ntimeout > ULONG_MAX/HZ)
534 sbi->exp_timeout = 0;
535 else
536 sbi->exp_timeout = ntimeout * HZ;
537
538 return 0;
539 }
540
541 /* Return protocol version */
542 static inline int autofs_get_protover(int __user *p)
543 {
544 return put_user(AUTOFS_PROTO_VERSION, p);
545 }
546
547 /* Perform an expiry operation */
548 static inline int autofs_expire_run(struct super_block *sb,
549 struct autofs_sb_info *sbi,
550 struct vfsmount *mnt,
551 struct autofs_packet_expire __user *pkt_p)
552 {
553 struct autofs_dir_ent *ent;
554 struct autofs_packet_expire pkt;
555
556 memset(&pkt,0,sizeof pkt);
557
558 pkt.hdr.proto_version = AUTOFS_PROTO_VERSION;
559 pkt.hdr.type = autofs_ptype_expire;
560
561 if (!sbi->exp_timeout || !(ent = autofs_expire(sb,sbi,mnt)))
562 return -EAGAIN;
563
564 pkt.len = ent->len;
565 memcpy(pkt.name, ent->name, pkt.len);
566 pkt.name[pkt.len] = '\0';
567
568 if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
569 return -EFAULT;
570
571 return 0;
572 }
573
574 /*
575 * ioctl()'s on the root directory is the chief method for the daemon to
576 * generate kernel reactions
577 */
578 static int autofs_do_root_ioctl(struct inode *inode, struct file *filp,
579 unsigned int cmd, unsigned long arg)
580 {
581 struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
582 void __user *argp = (void __user *)arg;
583
584 DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd,arg,sbi,task_pgrp_nr(current)));
585
586 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
587 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
588 return -ENOTTY;
589
590 if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
591 return -EPERM;
592
593 switch(cmd) {
594 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
595 return autofs_wait_release(sbi,(autofs_wqt_t)arg,0);
596 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
597 return autofs_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
598 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
599 autofs_catatonic_mode(sbi);
600 return 0;
601 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
602 return autofs_get_protover(argp);
603 #ifdef CONFIG_COMPAT
604 case AUTOFS_IOC_SETTIMEOUT32:
605 return autofs_compat_get_set_timeout(sbi, argp);
606 #endif
607 case AUTOFS_IOC_SETTIMEOUT:
608 return autofs_get_set_timeout(sbi, argp);
609 case AUTOFS_IOC_EXPIRE:
610 return autofs_expire_run(inode->i_sb, sbi, filp->f_path.mnt,
611 argp);
612 default:
613 return -ENOSYS;
614 }
615
616 }
617
618 static long autofs_root_ioctl(struct file *filp,
619 unsigned int cmd, unsigned long arg)
620 {
621 int ret;
622
623 lock_kernel();
624 ret = autofs_do_root_ioctl(filp->f_path.dentry->d_inode,
625 filp, cmd, arg);
626 unlock_kernel();
627
628 return ret;
629 }
630
631 #ifdef CONFIG_COMPAT
632 static long autofs_root_compat_ioctl(struct file *filp,
633 unsigned int cmd, unsigned long arg)
634 {
635 struct inode *inode = filp->f_path.dentry->d_inode;
636 int ret;
637
638 lock_kernel();
639 if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
640 ret = autofs_do_root_ioctl(inode, filp, cmd, arg);
641 else
642 ret = autofs_do_root_ioctl(inode, filp, cmd,
643 (unsigned long)compat_ptr(arg));
644 unlock_kernel();
645
646 return ret;
647 }
648 #endif
This page took 0.046179 seconds and 5 git commands to generate.