blob: 9a0520b50663b52f9508eb962bd3fbd92f55baa4 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- 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
Randy Dunlap16f7e0f2006-01-11 12:17:46 -080013#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/errno.h>
15#include <linux/stat.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/param.h>
18#include <linux/time.h>
19#include <linux/smp_lock.h>
20#include "autofs_i.h"
21
22static int autofs_root_readdir(struct file *,void *,filldir_t);
23static struct dentry *autofs_root_lookup(struct inode *,struct dentry *, struct nameidata *);
24static int autofs_root_symlink(struct inode *,struct dentry *,const char *);
25static int autofs_root_unlink(struct inode *,struct dentry *);
26static int autofs_root_rmdir(struct inode *,struct dentry *);
27static int autofs_root_mkdir(struct inode *,struct dentry *,int);
28static int autofs_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
29
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -080030const struct file_operations autofs_root_operations = {
jan Blunckca572722010-05-26 14:44:53 -070031 .llseek = generic_file_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 .read = generic_read_dir,
33 .readdir = autofs_root_readdir,
34 .ioctl = autofs_root_ioctl,
35};
36
Arjan van de Ven754661f2007-02-12 00:55:38 -080037const struct inode_operations autofs_root_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 .lookup = autofs_root_lookup,
39 .unlink = autofs_root_unlink,
40 .symlink = autofs_root_symlink,
41 .mkdir = autofs_root_mkdir,
42 .rmdir = autofs_root_rmdir,
43};
44
45static int autofs_root_readdir(struct file *filp, void *dirent, filldir_t filldir)
46{
47 struct autofs_dir_ent *ent = NULL;
48 struct autofs_dirhash *dirhash;
49 struct autofs_sb_info *sbi;
Josef "Jeff" Sipek81ed19b2006-12-08 02:36:46 -080050 struct inode * inode = filp->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 off_t onr, nr;
52
53 lock_kernel();
54
55 sbi = autofs_sbi(inode->i_sb);
56 dirhash = &sbi->dirhash;
57 nr = filp->f_pos;
58
59 switch(nr)
60 {
61 case 0:
62 if (filldir(dirent, ".", 1, nr, inode->i_ino, DT_DIR) < 0)
63 goto out;
64 filp->f_pos = ++nr;
65 /* fall through */
66 case 1:
67 if (filldir(dirent, "..", 2, nr, inode->i_ino, DT_DIR) < 0)
68 goto out;
69 filp->f_pos = ++nr;
70 /* fall through */
71 default:
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -070072 while (onr = nr, ent = autofs_hash_enum(dirhash,&nr,ent)) {
73 if (!ent->dentry || d_mountpoint(ent->dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (filldir(dirent,ent->name,ent->len,onr,ent->ino,DT_UNKNOWN) < 0)
75 goto out;
76 filp->f_pos = nr;
77 }
78 }
79 break;
80 }
81
82out:
83 unlock_kernel();
84 return 0;
85}
86
87static int try_to_fill_dentry(struct dentry *dentry, struct super_block *sb, struct autofs_sb_info *sbi)
88{
89 struct inode * inode;
90 struct autofs_dir_ent *ent;
91 int status = 0;
92
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -070093 if (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 do {
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -070095 if (status && dentry->d_inode) {
96 if (status != -ENOENT)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 printk("autofs warning: lookup failure on positive dentry, status = %d, name = %s\n", status, dentry->d_name.name);
98 return 0; /* Try to get the kernel to invalidate this dentry */
99 }
100
101 /* Turn this into a real negative dentry? */
102 if (status == -ENOENT) {
103 dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
104 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
105 return 1;
106 } else if (status) {
107 /* Return a negative dentry, but leave it "pending" */
108 return 1;
109 }
110 status = autofs_wait(sbi, &dentry->d_name);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700111 } while (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113
114 /* Abuse this field as a pointer to the directory entry, used to
115 find the expire list pointers */
116 dentry->d_time = (unsigned long) ent;
117
118 if (!dentry->d_inode) {
David Howells62328a02008-02-07 00:15:30 -0800119 inode = autofs_iget(sb, ent->ino);
120 if (IS_ERR(inode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 /* Failed, but leave pending for next time */
122 return 1;
123 }
124 dentry->d_inode = inode;
125 }
126
127 /* If this is a directory that isn't a mount point, bitch at the
128 daemon and fix it in user space */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700129 if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return !autofs_wait(sbi, &dentry->d_name);
131 }
132
133 /* We don't update the usages for the autofs daemon itself, this
134 is necessary for recursive autofs mounts */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700135 if (!autofs_oz_mode(sbi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 autofs_update_usage(&sbi->dirhash,ent);
137 }
138
139 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
140 return 1;
141}
142
143
144/*
145 * Revalidate is called on every cache lookup. Some of those
146 * cache lookups may actually happen while the dentry is not
147 * yet completely filled in, and revalidate has to delay such
148 * lookups..
149 */
150static int autofs_revalidate(struct dentry * dentry, struct nameidata *nd)
151{
152 struct inode * dir;
153 struct autofs_sb_info *sbi;
154 struct autofs_dir_ent *ent;
155 int res;
156
157 lock_kernel();
158 dir = dentry->d_parent->d_inode;
159 sbi = autofs_sbi(dir->i_sb);
160
161 /* Pending dentry */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700162 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (autofs_oz_mode(sbi))
164 res = 1;
165 else
166 res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
167 unlock_kernel();
168 return res;
169 }
170
171 /* Negative dentry.. invalidate if "old" */
172 if (!dentry->d_inode) {
173 unlock_kernel();
174 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
175 }
176
177 /* Check for a non-mountpoint directory */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700178 if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 if (autofs_oz_mode(sbi))
180 res = 1;
181 else
182 res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
183 unlock_kernel();
184 return res;
185 }
186
187 /* Update the usage list */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700188 if (!autofs_oz_mode(sbi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 ent = (struct autofs_dir_ent *) dentry->d_time;
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700190 if (ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 autofs_update_usage(&sbi->dirhash,ent);
192 }
193 unlock_kernel();
194 return 1;
195}
196
Al Viro08f11512009-02-20 05:56:19 +0000197static const struct dentry_operations autofs_dentry_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 .d_revalidate = autofs_revalidate,
199};
200
201static struct dentry *autofs_root_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
202{
203 struct autofs_sb_info *sbi;
204 int oz_mode;
205
206 DPRINTK(("autofs_root_lookup: name = "));
207 lock_kernel();
208 autofs_say(dentry->d_name.name,dentry->d_name.len);
209
210 if (dentry->d_name.len > NAME_MAX) {
211 unlock_kernel();
212 return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
213 }
214
215 sbi = autofs_sbi(dir->i_sb);
216
217 oz_mode = autofs_oz_mode(sbi);
Sukadev Bhattiprolufa0334f2007-05-10 22:23:08 -0700218 DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, "
Pavel Emelyanov69cccb82007-10-18 23:40:44 -0700219 "oz_mode = %d\n", task_pid_nr(current),
Pavel Emelianova47afb02007-10-18 23:39:46 -0700220 task_pgrp_nr(current), sbi->catatonic,
Sukadev Bhattiprolufa0334f2007-05-10 22:23:08 -0700221 oz_mode));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 /*
224 * Mark the dentry incomplete, but add it. This is needed so
225 * that the VFS layer knows about the dentry, and we can count
226 * on catching any lookups through the revalidate.
227 *
228 * Let all the hard work be done by the revalidate function that
229 * needs to be able to do this anyway..
230 *
231 * We need to do this before we release the directory semaphore.
232 */
233 dentry->d_op = &autofs_dentry_operations;
234 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
235 d_add(dentry, NULL);
236
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800237 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 autofs_revalidate(dentry, nd);
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800239 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241 /*
242 * If we are still pending, check if we had to handle
243 * a signal. If so we can force a restart..
244 */
245 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
246 /* See if we were interrupted */
247 if (signal_pending(current)) {
248 sigset_t *sigset = &current->pending.signal;
249 if (sigismember (sigset, SIGKILL) ||
250 sigismember (sigset, SIGQUIT) ||
251 sigismember (sigset, SIGINT)) {
252 unlock_kernel();
253 return ERR_PTR(-ERESTARTNOINTR);
254 }
255 }
256 }
257 unlock_kernel();
258
259 /*
260 * If this dentry is unhashed, then we shouldn't honour this
261 * lookup even if the dentry is positive. Returning ENOENT here
262 * doesn't do the right thing for all system calls, but it should
263 * be OK for the operations we permit from an autofs.
264 */
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700265 if (dentry->d_inode && d_unhashed(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return ERR_PTR(-ENOENT);
267
268 return NULL;
269}
270
271static int autofs_root_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
272{
273 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
274 struct autofs_dirhash *dh = &sbi->dirhash;
275 struct autofs_dir_ent *ent;
276 unsigned int n;
277 int slsize;
278 struct autofs_symlink *sl;
David Howells62328a02008-02-07 00:15:30 -0800279 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281 DPRINTK(("autofs_root_symlink: %s <- ", symname));
282 autofs_say(dentry->d_name.name,dentry->d_name.len);
283
284 lock_kernel();
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700285 if (!autofs_oz_mode(sbi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 unlock_kernel();
287 return -EACCES;
288 }
289
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700290 if (autofs_hash_lookup(dh, &dentry->d_name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 unlock_kernel();
292 return -EEXIST;
293 }
294
295 n = find_first_zero_bit(sbi->symlink_bitmap,AUTOFS_MAX_SYMLINKS);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700296 if (n >= AUTOFS_MAX_SYMLINKS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 unlock_kernel();
298 return -ENOSPC;
299 }
300
301 set_bit(n,sbi->symlink_bitmap);
302 sl = &sbi->symlink[n];
303 sl->len = strlen(symname);
304 sl->data = kmalloc(slsize = sl->len+1, GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700305 if (!sl->data) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 clear_bit(n,sbi->symlink_bitmap);
307 unlock_kernel();
308 return -ENOSPC;
309 }
310
311 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700312 if (!ent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 kfree(sl->data);
314 clear_bit(n,sbi->symlink_bitmap);
315 unlock_kernel();
316 return -ENOSPC;
317 }
318
319 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700320 if (!ent->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 kfree(sl->data);
322 kfree(ent);
323 clear_bit(n,sbi->symlink_bitmap);
324 unlock_kernel();
325 return -ENOSPC;
326 }
327
328 memcpy(sl->data,symname,slsize);
329 sl->mtime = get_seconds();
330
331 ent->ino = AUTOFS_FIRST_SYMLINK + n;
332 ent->hash = dentry->d_name.hash;
333 memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
334 ent->dentry = NULL; /* We don't keep the dentry for symlinks */
335
336 autofs_hash_insert(dh,ent);
David Howells62328a02008-02-07 00:15:30 -0800337
338 inode = autofs_iget(dir->i_sb, ent->ino);
339 if (IS_ERR(inode))
340 return PTR_ERR(inode);
341
342 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 unlock_kernel();
344 return 0;
345}
346
347/*
348 * NOTE!
349 *
350 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
351 * that the file no longer exists. However, doing that means that the
352 * VFS layer can turn the dentry into a negative dentry, which we
353 * obviously do not want (we're dropping the entry not because it
354 * doesn't exist, but because it has timed out).
355 *
356 * Also see autofs_root_rmdir()..
357 */
358static int autofs_root_unlink(struct inode *dir, struct dentry *dentry)
359{
360 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
361 struct autofs_dirhash *dh = &sbi->dirhash;
362 struct autofs_dir_ent *ent;
363 unsigned int n;
364
365 /* This allows root to remove symlinks */
366 lock_kernel();
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700367 if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 unlock_kernel();
369 return -EACCES;
370 }
371
372 ent = autofs_hash_lookup(dh, &dentry->d_name);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700373 if (!ent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 unlock_kernel();
375 return -ENOENT;
376 }
377
378 n = ent->ino - AUTOFS_FIRST_SYMLINK;
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700379 if (n >= AUTOFS_MAX_SYMLINKS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 unlock_kernel();
381 return -EISDIR; /* It's a directory, dummy */
382 }
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700383 if (!test_bit(n,sbi->symlink_bitmap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 unlock_kernel();
385 return -EINVAL; /* Nonexistent symlink? Shouldn't happen */
386 }
387
388 dentry->d_time = (unsigned long)(struct autofs_dirhash *)NULL;
389 autofs_hash_delete(ent);
390 clear_bit(n,sbi->symlink_bitmap);
391 kfree(sbi->symlink[n].data);
392 d_drop(dentry);
393
394 unlock_kernel();
395 return 0;
396}
397
398static int autofs_root_rmdir(struct inode *dir, struct dentry *dentry)
399{
400 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
401 struct autofs_dirhash *dh = &sbi->dirhash;
402 struct autofs_dir_ent *ent;
403
404 lock_kernel();
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700405 if (!autofs_oz_mode(sbi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 unlock_kernel();
407 return -EACCES;
408 }
409
410 ent = autofs_hash_lookup(dh, &dentry->d_name);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700411 if (!ent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 unlock_kernel();
413 return -ENOENT;
414 }
415
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700416 if ((unsigned int)ent->ino < AUTOFS_FIRST_DIR_INO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 unlock_kernel();
418 return -ENOTDIR; /* Not a directory */
419 }
420
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700421 if (ent->dentry != dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry->d_name.name);
423 }
424
425 dentry->d_time = (unsigned long)(struct autofs_dir_ent *)NULL;
426 autofs_hash_delete(ent);
Dave Hansen9a53c3a2006-09-30 23:29:03 -0700427 drop_nlink(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 d_drop(dentry);
429 unlock_kernel();
430
431 return 0;
432}
433
434static int autofs_root_mkdir(struct inode *dir, struct dentry *dentry, int mode)
435{
436 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
437 struct autofs_dirhash *dh = &sbi->dirhash;
438 struct autofs_dir_ent *ent;
David Howells62328a02008-02-07 00:15:30 -0800439 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 ino_t ino;
441
442 lock_kernel();
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700443 if (!autofs_oz_mode(sbi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 unlock_kernel();
445 return -EACCES;
446 }
447
448 ent = autofs_hash_lookup(dh, &dentry->d_name);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700449 if (ent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 unlock_kernel();
451 return -EEXIST;
452 }
453
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700454 if (sbi->next_dir_ino < AUTOFS_FIRST_DIR_INO) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 printk("autofs: Out of inode numbers -- what the heck did you do??\n");
456 unlock_kernel();
457 return -ENOSPC;
458 }
459 ino = sbi->next_dir_ino++;
460
461 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700462 if (!ent) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 unlock_kernel();
464 return -ENOSPC;
465 }
466
467 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700468 if (!ent->name) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 kfree(ent);
470 unlock_kernel();
471 return -ENOSPC;
472 }
473
474 ent->hash = dentry->d_name.hash;
475 memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
476 ent->ino = ino;
477 ent->dentry = dentry;
478 autofs_hash_insert(dh,ent);
479
Dave Hansend8c76e62006-09-30 23:29:04 -0700480 inc_nlink(dir);
David Howells62328a02008-02-07 00:15:30 -0800481
482 inode = autofs_iget(dir->i_sb, ino);
483 if (IS_ERR(inode)) {
484 drop_nlink(dir);
485 return PTR_ERR(inode);
486 }
487
488 d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 unlock_kernel();
490
491 return 0;
492}
493
494/* Get/set timeout ioctl() operation */
495static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
496 unsigned long __user *p)
497{
498 unsigned long ntimeout;
499
500 if (get_user(ntimeout, p) ||
501 put_user(sbi->exp_timeout / HZ, p))
502 return -EFAULT;
503
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700504 if (ntimeout > ULONG_MAX/HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 sbi->exp_timeout = 0;
506 else
507 sbi->exp_timeout = ntimeout * HZ;
508
509 return 0;
510}
511
512/* Return protocol version */
513static inline int autofs_get_protover(int __user *p)
514{
515 return put_user(AUTOFS_PROTO_VERSION, p);
516}
517
518/* Perform an expiry operation */
519static inline int autofs_expire_run(struct super_block *sb,
520 struct autofs_sb_info *sbi,
521 struct vfsmount *mnt,
522 struct autofs_packet_expire __user *pkt_p)
523{
524 struct autofs_dir_ent *ent;
525 struct autofs_packet_expire pkt;
526
527 memset(&pkt,0,sizeof pkt);
528
529 pkt.hdr.proto_version = AUTOFS_PROTO_VERSION;
530 pkt.hdr.type = autofs_ptype_expire;
531
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700532 if (!sbi->exp_timeout || !(ent = autofs_expire(sb,sbi,mnt)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return -EAGAIN;
534
535 pkt.len = ent->len;
536 memcpy(pkt.name, ent->name, pkt.len);
537 pkt.name[pkt.len] = '\0';
538
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700539 if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return -EFAULT;
541
542 return 0;
543}
544
545/*
546 * ioctl()'s on the root directory is the chief method for the daemon to
547 * generate kernel reactions
548 */
549static int autofs_root_ioctl(struct inode *inode, struct file *filp,
550 unsigned int cmd, unsigned long arg)
551{
552 struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
553 void __user *argp = (void __user *)arg;
554
Pavel Emelianova47afb02007-10-18 23:39:46 -0700555 DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd,arg,sbi,task_pgrp_nr(current)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700557 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
558 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return -ENOTTY;
560
Sukadev Bhattiprolud78e53c2007-05-10 22:23:06 -0700561 if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return -EPERM;
563
564 switch(cmd) {
565 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
566 return autofs_wait_release(sbi,(autofs_wqt_t)arg,0);
567 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
568 return autofs_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
569 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
570 autofs_catatonic_mode(sbi);
571 return 0;
572 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
573 return autofs_get_protover(argp);
574 case AUTOFS_IOC_SETTIMEOUT:
575 return autofs_get_set_timeout(sbi, argp);
576 case AUTOFS_IOC_EXPIRE:
Josef "Jeff" Sipek81ed19b2006-12-08 02:36:46 -0800577 return autofs_expire_run(inode->i_sb, sbi, filp->f_path.mnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 argp);
579 default:
580 return -ENOSYS;
581 }
582}