]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | * Copyright 2001-2003 Ian Kent <raven@themaw.net> | |
8 | * | |
9 | * This file is part of the Linux kernel and is made available under | |
10 | * the terms of the GNU General Public License, version 2, or at your | |
11 | * option, any later version, incorporated herein by reference. | |
12 | * | |
13 | * ------------------------------------------------------------------------- */ | |
14 | ||
16f7e0fe | 15 | #include <linux/capability.h> |
1da177e4 LT |
16 | #include <linux/errno.h> |
17 | #include <linux/stat.h> | |
18 | #include <linux/param.h> | |
19 | #include <linux/time.h> | |
20 | #include <linux/smp_lock.h> | |
21 | #include "autofs_i.h" | |
22 | ||
23 | static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *); | |
24 | static int autofs4_dir_unlink(struct inode *,struct dentry *); | |
25 | static int autofs4_dir_rmdir(struct inode *,struct dentry *); | |
26 | static int autofs4_dir_mkdir(struct inode *,struct dentry *,int); | |
27 | static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long); | |
28 | static int autofs4_dir_open(struct inode *inode, struct file *file); | |
29 | static int autofs4_dir_close(struct inode *inode, struct file *file); | |
30 | static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir); | |
31 | static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir); | |
32 | static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *); | |
1da177e4 LT |
33 | |
34 | struct file_operations autofs4_root_operations = { | |
35 | .open = dcache_dir_open, | |
36 | .release = dcache_dir_close, | |
37 | .read = generic_read_dir, | |
38 | .readdir = autofs4_root_readdir, | |
39 | .ioctl = autofs4_root_ioctl, | |
40 | }; | |
41 | ||
42 | struct file_operations autofs4_dir_operations = { | |
43 | .open = autofs4_dir_open, | |
44 | .release = autofs4_dir_close, | |
45 | .read = generic_read_dir, | |
46 | .readdir = autofs4_dir_readdir, | |
47 | }; | |
48 | ||
49 | struct inode_operations autofs4_root_inode_operations = { | |
50 | .lookup = autofs4_lookup, | |
51 | .unlink = autofs4_dir_unlink, | |
52 | .symlink = autofs4_dir_symlink, | |
53 | .mkdir = autofs4_dir_mkdir, | |
54 | .rmdir = autofs4_dir_rmdir, | |
55 | }; | |
56 | ||
57 | struct inode_operations autofs4_dir_inode_operations = { | |
58 | .lookup = autofs4_lookup, | |
59 | .unlink = autofs4_dir_unlink, | |
60 | .symlink = autofs4_dir_symlink, | |
61 | .mkdir = autofs4_dir_mkdir, | |
62 | .rmdir = autofs4_dir_rmdir, | |
63 | }; | |
64 | ||
65 | static int autofs4_root_readdir(struct file *file, void *dirent, | |
66 | filldir_t filldir) | |
67 | { | |
68 | struct autofs_sb_info *sbi = autofs4_sbi(file->f_dentry->d_sb); | |
69 | int oz_mode = autofs4_oz_mode(sbi); | |
70 | ||
71 | DPRINTK("called, filp->f_pos = %lld", file->f_pos); | |
72 | ||
73 | /* | |
74 | * Don't set reghost flag if: | |
75 | * 1) f_pos is larger than zero -- we've already been here. | |
76 | * 2) we haven't even enabled reghosting in the 1st place. | |
77 | * 3) this is the daemon doing a readdir | |
78 | */ | |
79 | if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled) | |
80 | sbi->needs_reghost = 1; | |
81 | ||
82 | DPRINTK("needs_reghost = %d", sbi->needs_reghost); | |
83 | ||
f360ce3b | 84 | return dcache_readdir(file, dirent, filldir); |
1da177e4 LT |
85 | } |
86 | ||
87 | /* Update usage from here to top of tree, so that scan of | |
88 | top-level directories will give a useful result */ | |
9cf6f4b3 | 89 | static void autofs4_update_usage(struct vfsmount *mnt, struct dentry *dentry) |
1da177e4 LT |
90 | { |
91 | struct dentry *top = dentry->d_sb->s_root; | |
92 | ||
93 | spin_lock(&dcache_lock); | |
94 | for(; dentry != top; dentry = dentry->d_parent) { | |
95 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
96 | ||
97 | if (ino) { | |
9cf6f4b3 | 98 | touch_atime(mnt, dentry); |
1da177e4 LT |
99 | ino->last_used = jiffies; |
100 | } | |
101 | } | |
102 | spin_unlock(&dcache_lock); | |
103 | } | |
104 | ||
1da177e4 LT |
105 | static int autofs4_dir_open(struct inode *inode, struct file *file) |
106 | { | |
107 | struct dentry *dentry = file->f_dentry; | |
108 | struct vfsmount *mnt = file->f_vfsmnt; | |
109 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | |
f360ce3b | 110 | struct dentry *cursor; |
1da177e4 LT |
111 | int status; |
112 | ||
f360ce3b IK |
113 | status = dcache_dir_open(inode, file); |
114 | if (status) | |
115 | goto out; | |
116 | ||
117 | cursor = file->private_data; | |
118 | cursor->d_fsdata = NULL; | |
119 | ||
1da177e4 LT |
120 | DPRINTK("file=%p dentry=%p %.*s", |
121 | file, dentry, dentry->d_name.len, dentry->d_name.name); | |
122 | ||
123 | if (autofs4_oz_mode(sbi)) | |
124 | goto out; | |
125 | ||
126 | if (autofs4_ispending(dentry)) { | |
127 | DPRINTK("dentry busy"); | |
f360ce3b IK |
128 | dcache_dir_close(inode, file); |
129 | status = -EBUSY; | |
130 | goto out; | |
1da177e4 LT |
131 | } |
132 | ||
f360ce3b | 133 | status = -ENOENT; |
1da177e4 LT |
134 | if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) { |
135 | struct nameidata nd; | |
f360ce3b | 136 | int empty, ret; |
1da177e4 LT |
137 | |
138 | /* In case there are stale directory dentrys from a failed mount */ | |
139 | spin_lock(&dcache_lock); | |
140 | empty = list_empty(&dentry->d_subdirs); | |
141 | spin_unlock(&dcache_lock); | |
142 | ||
143 | if (!empty) | |
144 | d_invalidate(dentry); | |
145 | ||
146 | nd.flags = LOOKUP_DIRECTORY; | |
f360ce3b | 147 | ret = (dentry->d_op->d_revalidate)(dentry, &nd); |
1da177e4 | 148 | |
f360ce3b IK |
149 | if (!ret) { |
150 | dcache_dir_close(inode, file); | |
151 | goto out; | |
152 | } | |
1da177e4 LT |
153 | } |
154 | ||
155 | if (d_mountpoint(dentry)) { | |
156 | struct file *fp = NULL; | |
157 | struct vfsmount *fp_mnt = mntget(mnt); | |
158 | struct dentry *fp_dentry = dget(dentry); | |
159 | ||
9b1e3afd IK |
160 | if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) { |
161 | dput(fp_dentry); | |
162 | mntput(fp_mnt); | |
f360ce3b IK |
163 | dcache_dir_close(inode, file); |
164 | goto out; | |
9b1e3afd | 165 | } |
1da177e4 LT |
166 | |
167 | fp = dentry_open(fp_dentry, fp_mnt, file->f_flags); | |
168 | status = PTR_ERR(fp); | |
169 | if (IS_ERR(fp)) { | |
f360ce3b IK |
170 | dcache_dir_close(inode, file); |
171 | goto out; | |
1da177e4 | 172 | } |
f360ce3b | 173 | cursor->d_fsdata = fp; |
1da177e4 | 174 | } |
1da177e4 | 175 | return 0; |
f360ce3b IK |
176 | out: |
177 | return status; | |
1da177e4 LT |
178 | } |
179 | ||
180 | static int autofs4_dir_close(struct inode *inode, struct file *file) | |
181 | { | |
182 | struct dentry *dentry = file->f_dentry; | |
183 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | |
f360ce3b IK |
184 | struct dentry *cursor = file->private_data; |
185 | int status = 0; | |
1da177e4 LT |
186 | |
187 | DPRINTK("file=%p dentry=%p %.*s", | |
188 | file, dentry, dentry->d_name.len, dentry->d_name.name); | |
189 | ||
190 | if (autofs4_oz_mode(sbi)) | |
191 | goto out; | |
192 | ||
193 | if (autofs4_ispending(dentry)) { | |
194 | DPRINTK("dentry busy"); | |
f360ce3b IK |
195 | status = -EBUSY; |
196 | goto out; | |
1da177e4 LT |
197 | } |
198 | ||
199 | if (d_mountpoint(dentry)) { | |
f360ce3b IK |
200 | struct file *fp = cursor->d_fsdata; |
201 | if (!fp) { | |
202 | status = -ENOENT; | |
203 | goto out; | |
204 | } | |
1da177e4 | 205 | filp_close(fp, current->files); |
1da177e4 LT |
206 | } |
207 | out: | |
f360ce3b IK |
208 | dcache_dir_close(inode, file); |
209 | return status; | |
1da177e4 LT |
210 | } |
211 | ||
212 | static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir) | |
213 | { | |
214 | struct dentry *dentry = file->f_dentry; | |
215 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | |
f360ce3b | 216 | struct dentry *cursor = file->private_data; |
1da177e4 LT |
217 | int status; |
218 | ||
219 | DPRINTK("file=%p dentry=%p %.*s", | |
220 | file, dentry, dentry->d_name.len, dentry->d_name.name); | |
221 | ||
222 | if (autofs4_oz_mode(sbi)) | |
223 | goto out; | |
224 | ||
225 | if (autofs4_ispending(dentry)) { | |
226 | DPRINTK("dentry busy"); | |
227 | return -EBUSY; | |
228 | } | |
229 | ||
230 | if (d_mountpoint(dentry)) { | |
f360ce3b | 231 | struct file *fp = cursor->d_fsdata; |
1da177e4 LT |
232 | |
233 | if (!fp) | |
234 | return -ENOENT; | |
235 | ||
236 | if (!fp->f_op || !fp->f_op->readdir) | |
237 | goto out; | |
238 | ||
239 | status = vfs_readdir(fp, filldir, dirent); | |
240 | file->f_pos = fp->f_pos; | |
241 | if (status) | |
242 | autofs4_copy_atime(file, fp); | |
243 | return status; | |
244 | } | |
245 | out: | |
f360ce3b | 246 | return dcache_readdir(file, dirent, filldir); |
1da177e4 LT |
247 | } |
248 | ||
9cf6f4b3 | 249 | static int try_to_fill_dentry(struct vfsmount *mnt, struct dentry *dentry, int flags) |
1da177e4 | 250 | { |
9cf6f4b3 CH |
251 | struct super_block *sb = mnt->mnt_sb; |
252 | struct autofs_sb_info *sbi = autofs4_sbi(sb); | |
718c604a | 253 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
1da177e4 LT |
254 | int status = 0; |
255 | ||
256 | /* Block on any pending expiry here; invalidate the dentry | |
257 | when expiration is done to trigger mount request with a new | |
258 | dentry */ | |
718c604a | 259 | if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) { |
1da177e4 LT |
260 | DPRINTK("waiting for expire %p name=%.*s", |
261 | dentry, dentry->d_name.len, dentry->d_name.name); | |
262 | ||
263 | status = autofs4_wait(sbi, dentry, NFY_NONE); | |
718c604a | 264 | |
1da177e4 | 265 | DPRINTK("expire done status=%d", status); |
718c604a | 266 | |
1684b2bb IK |
267 | /* |
268 | * If the directory still exists the mount request must | |
269 | * continue otherwise it can't be followed at the right | |
270 | * time during the walk. | |
271 | */ | |
272 | status = d_invalidate(dentry); | |
273 | if (status != -EBUSY) | |
274 | return 0; | |
1da177e4 LT |
275 | } |
276 | ||
277 | DPRINTK("dentry=%p %.*s ino=%p", | |
278 | dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode); | |
279 | ||
718c604a IK |
280 | /* |
281 | * Wait for a pending mount, triggering one if there | |
282 | * isn't one already | |
283 | */ | |
1da177e4 LT |
284 | if (dentry->d_inode == NULL) { |
285 | DPRINTK("waiting for mount name=%.*s", | |
286 | dentry->d_name.len, dentry->d_name.name); | |
287 | ||
288 | status = autofs4_wait(sbi, dentry, NFY_MOUNT); | |
718c604a | 289 | |
1da177e4 LT |
290 | DPRINTK("mount done status=%d", status); |
291 | ||
292 | if (status && dentry->d_inode) | |
293 | return 0; /* Try to get the kernel to invalidate this dentry */ | |
718c604a | 294 | |
1da177e4 LT |
295 | /* Turn this into a real negative dentry? */ |
296 | if (status == -ENOENT) { | |
1da177e4 LT |
297 | spin_lock(&dentry->d_lock); |
298 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; | |
299 | spin_unlock(&dentry->d_lock); | |
2d753e62 | 300 | return 0; |
1da177e4 LT |
301 | } else if (status) { |
302 | /* Return a negative dentry, but leave it "pending" */ | |
2d753e62 | 303 | return 0; |
1da177e4 LT |
304 | } |
305 | /* Trigger mount for path component or follow link */ | |
306 | } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) || | |
307 | current->link_count) { | |
308 | DPRINTK("waiting for mount name=%.*s", | |
309 | dentry->d_name.len, dentry->d_name.name); | |
310 | ||
311 | spin_lock(&dentry->d_lock); | |
312 | dentry->d_flags |= DCACHE_AUTOFS_PENDING; | |
313 | spin_unlock(&dentry->d_lock); | |
314 | status = autofs4_wait(sbi, dentry, NFY_MOUNT); | |
315 | ||
316 | DPRINTK("mount done status=%d", status); | |
317 | ||
318 | if (status) { | |
319 | spin_lock(&dentry->d_lock); | |
320 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; | |
321 | spin_unlock(&dentry->d_lock); | |
322 | return 0; | |
323 | } | |
324 | } | |
325 | ||
718c604a IK |
326 | /* |
327 | * We don't update the usages for the autofs daemon itself, this | |
328 | * is necessary for recursive autofs mounts | |
329 | */ | |
1da177e4 | 330 | if (!autofs4_oz_mode(sbi)) |
9cf6f4b3 | 331 | autofs4_update_usage(mnt, dentry); |
1da177e4 | 332 | |
e0a7aae9 IK |
333 | /* Initialize expiry counter after successful mount */ |
334 | if (ino) | |
335 | ino->last_used = jiffies; | |
336 | ||
1da177e4 LT |
337 | spin_lock(&dentry->d_lock); |
338 | dentry->d_flags &= ~DCACHE_AUTOFS_PENDING; | |
339 | spin_unlock(&dentry->d_lock); | |
340 | return 1; | |
341 | } | |
342 | ||
343 | /* | |
344 | * Revalidate is called on every cache lookup. Some of those | |
345 | * cache lookups may actually happen while the dentry is not | |
346 | * yet completely filled in, and revalidate has to delay such | |
347 | * lookups.. | |
348 | */ | |
718c604a | 349 | static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd) |
1da177e4 | 350 | { |
718c604a | 351 | struct inode *dir = dentry->d_parent->d_inode; |
1da177e4 LT |
352 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); |
353 | int oz_mode = autofs4_oz_mode(sbi); | |
354 | int flags = nd ? nd->flags : 0; | |
355 | int status = 1; | |
356 | ||
357 | /* Pending dentry */ | |
358 | if (autofs4_ispending(dentry)) { | |
359 | if (!oz_mode) | |
9cf6f4b3 | 360 | status = try_to_fill_dentry(nd->mnt, dentry, flags); |
1da177e4 LT |
361 | return status; |
362 | } | |
363 | ||
364 | /* Negative dentry.. invalidate if "old" */ | |
365 | if (dentry->d_inode == NULL) | |
2d753e62 | 366 | return 0; |
1da177e4 LT |
367 | |
368 | /* Check for a non-mountpoint directory with no contents */ | |
369 | spin_lock(&dcache_lock); | |
370 | if (S_ISDIR(dentry->d_inode->i_mode) && | |
371 | !d_mountpoint(dentry) && | |
2d753e62 | 372 | simple_empty_nolock(dentry)) { |
1da177e4 LT |
373 | DPRINTK("dentry=%p %.*s, emptydir", |
374 | dentry, dentry->d_name.len, dentry->d_name.name); | |
375 | spin_unlock(&dcache_lock); | |
376 | if (!oz_mode) | |
9cf6f4b3 | 377 | status = try_to_fill_dentry(nd->mnt, dentry, flags); |
1da177e4 LT |
378 | return status; |
379 | } | |
380 | spin_unlock(&dcache_lock); | |
381 | ||
382 | /* Update the usage list */ | |
383 | if (!oz_mode) | |
9cf6f4b3 | 384 | autofs4_update_usage(nd->mnt, dentry); |
1da177e4 LT |
385 | |
386 | return 1; | |
387 | } | |
388 | ||
389 | static void autofs4_dentry_release(struct dentry *de) | |
390 | { | |
391 | struct autofs_info *inf; | |
392 | ||
393 | DPRINTK("releasing %p", de); | |
394 | ||
395 | inf = autofs4_dentry_ino(de); | |
396 | de->d_fsdata = NULL; | |
397 | ||
398 | if (inf) { | |
399 | inf->dentry = NULL; | |
400 | inf->inode = NULL; | |
401 | ||
402 | autofs4_free_ino(inf); | |
403 | } | |
404 | } | |
405 | ||
406 | /* For dentries of directories in the root dir */ | |
407 | static struct dentry_operations autofs4_root_dentry_operations = { | |
408 | .d_revalidate = autofs4_revalidate, | |
409 | .d_release = autofs4_dentry_release, | |
410 | }; | |
411 | ||
412 | /* For other dentries */ | |
413 | static struct dentry_operations autofs4_dentry_operations = { | |
414 | .d_revalidate = autofs4_revalidate, | |
415 | .d_release = autofs4_dentry_release, | |
416 | }; | |
417 | ||
418 | /* Lookups in the root directory */ | |
419 | static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) | |
420 | { | |
421 | struct autofs_sb_info *sbi; | |
422 | int oz_mode; | |
423 | ||
424 | DPRINTK("name = %.*s", | |
425 | dentry->d_name.len, dentry->d_name.name); | |
426 | ||
718c604a | 427 | /* File name too long to exist */ |
1da177e4 | 428 | if (dentry->d_name.len > NAME_MAX) |
718c604a | 429 | return ERR_PTR(-ENAMETOOLONG); |
1da177e4 LT |
430 | |
431 | sbi = autofs4_sbi(dir->i_sb); | |
1da177e4 | 432 | oz_mode = autofs4_oz_mode(sbi); |
718c604a | 433 | |
1da177e4 LT |
434 | DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d", |
435 | current->pid, process_group(current), sbi->catatonic, oz_mode); | |
436 | ||
437 | /* | |
438 | * Mark the dentry incomplete, but add it. This is needed so | |
439 | * that the VFS layer knows about the dentry, and we can count | |
440 | * on catching any lookups through the revalidate. | |
441 | * | |
442 | * Let all the hard work be done by the revalidate function that | |
443 | * needs to be able to do this anyway.. | |
444 | * | |
445 | * We need to do this before we release the directory semaphore. | |
446 | */ | |
447 | dentry->d_op = &autofs4_root_dentry_operations; | |
448 | ||
449 | if (!oz_mode) { | |
450 | spin_lock(&dentry->d_lock); | |
451 | dentry->d_flags |= DCACHE_AUTOFS_PENDING; | |
452 | spin_unlock(&dentry->d_lock); | |
453 | } | |
454 | dentry->d_fsdata = NULL; | |
455 | d_add(dentry, NULL); | |
456 | ||
457 | if (dentry->d_op && dentry->d_op->d_revalidate) { | |
1b1dcc1b | 458 | mutex_unlock(&dir->i_mutex); |
1da177e4 | 459 | (dentry->d_op->d_revalidate)(dentry, nd); |
1b1dcc1b | 460 | mutex_lock(&dir->i_mutex); |
1da177e4 LT |
461 | } |
462 | ||
463 | /* | |
464 | * If we are still pending, check if we had to handle | |
465 | * a signal. If so we can force a restart.. | |
466 | */ | |
467 | if (dentry->d_flags & DCACHE_AUTOFS_PENDING) { | |
468 | /* See if we were interrupted */ | |
469 | if (signal_pending(current)) { | |
470 | sigset_t *sigset = ¤t->pending.signal; | |
471 | if (sigismember (sigset, SIGKILL) || | |
472 | sigismember (sigset, SIGQUIT) || | |
473 | sigismember (sigset, SIGINT)) { | |
474 | return ERR_PTR(-ERESTARTNOINTR); | |
475 | } | |
476 | } | |
477 | } | |
478 | ||
479 | /* | |
480 | * If this dentry is unhashed, then we shouldn't honour this | |
481 | * lookup even if the dentry is positive. Returning ENOENT here | |
482 | * doesn't do the right thing for all system calls, but it should | |
483 | * be OK for the operations we permit from an autofs. | |
484 | */ | |
718c604a | 485 | if (dentry->d_inode && d_unhashed(dentry)) |
1da177e4 LT |
486 | return ERR_PTR(-ENOENT); |
487 | ||
488 | return NULL; | |
489 | } | |
490 | ||
491 | static int autofs4_dir_symlink(struct inode *dir, | |
492 | struct dentry *dentry, | |
493 | const char *symname) | |
494 | { | |
495 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); | |
496 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
1aff3c8b | 497 | struct autofs_info *p_ino; |
1da177e4 LT |
498 | struct inode *inode; |
499 | char *cp; | |
500 | ||
501 | DPRINTK("%s <- %.*s", symname, | |
502 | dentry->d_name.len, dentry->d_name.name); | |
503 | ||
504 | if (!autofs4_oz_mode(sbi)) | |
505 | return -EACCES; | |
506 | ||
507 | ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555); | |
508 | if (ino == NULL) | |
509 | return -ENOSPC; | |
510 | ||
511 | ino->size = strlen(symname); | |
512 | ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL); | |
513 | ||
514 | if (cp == NULL) { | |
515 | kfree(ino); | |
516 | return -ENOSPC; | |
517 | } | |
518 | ||
519 | strcpy(cp, symname); | |
520 | ||
521 | inode = autofs4_get_inode(dir->i_sb, ino); | |
522 | d_instantiate(dentry, inode); | |
523 | ||
524 | if (dir == dir->i_sb->s_root->d_inode) | |
525 | dentry->d_op = &autofs4_root_dentry_operations; | |
526 | else | |
527 | dentry->d_op = &autofs4_dentry_operations; | |
528 | ||
529 | dentry->d_fsdata = ino; | |
530 | ino->dentry = dget(dentry); | |
1aff3c8b IK |
531 | atomic_inc(&ino->count); |
532 | p_ino = autofs4_dentry_ino(dentry->d_parent); | |
533 | if (p_ino && dentry->d_parent != dentry) | |
534 | atomic_inc(&p_ino->count); | |
1da177e4 LT |
535 | ino->inode = inode; |
536 | ||
537 | dir->i_mtime = CURRENT_TIME; | |
538 | ||
539 | return 0; | |
540 | } | |
541 | ||
542 | /* | |
543 | * NOTE! | |
544 | * | |
545 | * Normal filesystems would do a "d_delete()" to tell the VFS dcache | |
546 | * that the file no longer exists. However, doing that means that the | |
547 | * VFS layer can turn the dentry into a negative dentry. We don't want | |
548 | * this, because since the unlink is probably the result of an expire. | |
549 | * We simply d_drop it, which allows the dentry lookup to remount it | |
550 | * if necessary. | |
551 | * | |
552 | * If a process is blocked on the dentry waiting for the expire to finish, | |
553 | * it will invalidate the dentry and try to mount with a new one. | |
554 | * | |
555 | * Also see autofs4_dir_rmdir().. | |
556 | */ | |
557 | static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry) | |
558 | { | |
559 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); | |
560 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
1aff3c8b | 561 | struct autofs_info *p_ino; |
1da177e4 LT |
562 | |
563 | /* This allows root to remove symlinks */ | |
564 | if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) ) | |
565 | return -EACCES; | |
566 | ||
1aff3c8b IK |
567 | if (atomic_dec_and_test(&ino->count)) { |
568 | p_ino = autofs4_dentry_ino(dentry->d_parent); | |
569 | if (p_ino && dentry->d_parent != dentry) | |
570 | atomic_dec(&p_ino->count); | |
571 | } | |
1da177e4 LT |
572 | dput(ino->dentry); |
573 | ||
574 | dentry->d_inode->i_size = 0; | |
575 | dentry->d_inode->i_nlink = 0; | |
576 | ||
577 | dir->i_mtime = CURRENT_TIME; | |
578 | ||
579 | d_drop(dentry); | |
580 | ||
581 | return 0; | |
582 | } | |
583 | ||
584 | static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry) | |
585 | { | |
586 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); | |
587 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
1aff3c8b | 588 | struct autofs_info *p_ino; |
1da177e4 LT |
589 | |
590 | if (!autofs4_oz_mode(sbi)) | |
591 | return -EACCES; | |
592 | ||
593 | spin_lock(&dcache_lock); | |
594 | if (!list_empty(&dentry->d_subdirs)) { | |
595 | spin_unlock(&dcache_lock); | |
596 | return -ENOTEMPTY; | |
597 | } | |
598 | spin_lock(&dentry->d_lock); | |
599 | __d_drop(dentry); | |
600 | spin_unlock(&dentry->d_lock); | |
601 | spin_unlock(&dcache_lock); | |
602 | ||
1aff3c8b IK |
603 | if (atomic_dec_and_test(&ino->count)) { |
604 | p_ino = autofs4_dentry_ino(dentry->d_parent); | |
605 | if (p_ino && dentry->d_parent != dentry) | |
606 | atomic_dec(&p_ino->count); | |
607 | } | |
1da177e4 | 608 | dput(ino->dentry); |
1da177e4 LT |
609 | dentry->d_inode->i_size = 0; |
610 | dentry->d_inode->i_nlink = 0; | |
611 | ||
612 | if (dir->i_nlink) | |
613 | dir->i_nlink--; | |
614 | ||
615 | return 0; | |
616 | } | |
617 | ||
618 | static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
619 | { | |
620 | struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb); | |
621 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
1aff3c8b | 622 | struct autofs_info *p_ino; |
1da177e4 LT |
623 | struct inode *inode; |
624 | ||
625 | if ( !autofs4_oz_mode(sbi) ) | |
626 | return -EACCES; | |
627 | ||
628 | DPRINTK("dentry %p, creating %.*s", | |
629 | dentry, dentry->d_name.len, dentry->d_name.name); | |
630 | ||
631 | ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555); | |
632 | if (ino == NULL) | |
633 | return -ENOSPC; | |
634 | ||
635 | inode = autofs4_get_inode(dir->i_sb, ino); | |
636 | d_instantiate(dentry, inode); | |
637 | ||
638 | if (dir == dir->i_sb->s_root->d_inode) | |
639 | dentry->d_op = &autofs4_root_dentry_operations; | |
640 | else | |
641 | dentry->d_op = &autofs4_dentry_operations; | |
642 | ||
643 | dentry->d_fsdata = ino; | |
644 | ino->dentry = dget(dentry); | |
1aff3c8b IK |
645 | atomic_inc(&ino->count); |
646 | p_ino = autofs4_dentry_ino(dentry->d_parent); | |
647 | if (p_ino && dentry->d_parent != dentry) | |
648 | atomic_inc(&p_ino->count); | |
1da177e4 LT |
649 | ino->inode = inode; |
650 | dir->i_nlink++; | |
651 | dir->i_mtime = CURRENT_TIME; | |
652 | ||
653 | return 0; | |
654 | } | |
655 | ||
656 | /* Get/set timeout ioctl() operation */ | |
657 | static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi, | |
658 | unsigned long __user *p) | |
659 | { | |
660 | int rv; | |
661 | unsigned long ntimeout; | |
662 | ||
663 | if ( (rv = get_user(ntimeout, p)) || | |
664 | (rv = put_user(sbi->exp_timeout/HZ, p)) ) | |
665 | return rv; | |
666 | ||
667 | if ( ntimeout > ULONG_MAX/HZ ) | |
668 | sbi->exp_timeout = 0; | |
669 | else | |
670 | sbi->exp_timeout = ntimeout * HZ; | |
671 | ||
672 | return 0; | |
673 | } | |
674 | ||
675 | /* Return protocol version */ | |
676 | static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p) | |
677 | { | |
678 | return put_user(sbi->version, p); | |
679 | } | |
680 | ||
681 | /* Return protocol sub version */ | |
682 | static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p) | |
683 | { | |
684 | return put_user(sbi->sub_version, p); | |
685 | } | |
686 | ||
687 | /* | |
688 | * Tells the daemon whether we need to reghost or not. Also, clears | |
689 | * the reghost_needed flag. | |
690 | */ | |
691 | static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p) | |
692 | { | |
693 | int status; | |
694 | ||
695 | DPRINTK("returning %d", sbi->needs_reghost); | |
696 | ||
697 | status = put_user(sbi->needs_reghost, p); | |
698 | if ( status ) | |
699 | return status; | |
700 | ||
701 | sbi->needs_reghost = 0; | |
702 | return 0; | |
703 | } | |
704 | ||
705 | /* | |
706 | * Enable / Disable reghosting ioctl() operation | |
707 | */ | |
708 | static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p) | |
709 | { | |
710 | int status; | |
711 | int val; | |
712 | ||
713 | status = get_user(val, p); | |
714 | ||
715 | DPRINTK("reghost = %d", val); | |
716 | ||
717 | if (status) | |
718 | return status; | |
719 | ||
720 | /* turn on/off reghosting, with the val */ | |
721 | sbi->reghost_enabled = val; | |
722 | return 0; | |
723 | } | |
724 | ||
725 | /* | |
726 | * Tells the daemon whether it can umount the autofs mount. | |
727 | */ | |
728 | static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p) | |
729 | { | |
730 | int status = 0; | |
731 | ||
732 | if (may_umount(mnt) == 0) | |
733 | status = 1; | |
734 | ||
735 | DPRINTK("returning %d", status); | |
736 | ||
737 | status = put_user(status, p); | |
738 | ||
739 | return status; | |
740 | } | |
741 | ||
742 | /* Identify autofs4_dentries - this is so we can tell if there's | |
743 | an extra dentry refcount or not. We only hold a refcount on the | |
744 | dentry if its non-negative (ie, d_inode != NULL) | |
745 | */ | |
746 | int is_autofs4_dentry(struct dentry *dentry) | |
747 | { | |
748 | return dentry && dentry->d_inode && | |
749 | (dentry->d_op == &autofs4_root_dentry_operations || | |
750 | dentry->d_op == &autofs4_dentry_operations) && | |
751 | dentry->d_fsdata != NULL; | |
752 | } | |
753 | ||
754 | /* | |
755 | * ioctl()'s on the root directory is the chief method for the daemon to | |
756 | * generate kernel reactions | |
757 | */ | |
758 | static int autofs4_root_ioctl(struct inode *inode, struct file *filp, | |
759 | unsigned int cmd, unsigned long arg) | |
760 | { | |
761 | struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb); | |
762 | void __user *p = (void __user *)arg; | |
763 | ||
764 | DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u", | |
765 | cmd,arg,sbi,process_group(current)); | |
766 | ||
767 | if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) || | |
768 | _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT ) | |
769 | return -ENOTTY; | |
770 | ||
771 | if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) ) | |
772 | return -EPERM; | |
773 | ||
774 | switch(cmd) { | |
775 | case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */ | |
776 | return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0); | |
777 | case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */ | |
778 | return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT); | |
779 | case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */ | |
780 | autofs4_catatonic_mode(sbi); | |
781 | return 0; | |
782 | case AUTOFS_IOC_PROTOVER: /* Get protocol version */ | |
783 | return autofs4_get_protover(sbi, p); | |
784 | case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */ | |
785 | return autofs4_get_protosubver(sbi, p); | |
786 | case AUTOFS_IOC_SETTIMEOUT: | |
787 | return autofs4_get_set_timeout(sbi, p); | |
788 | ||
789 | case AUTOFS_IOC_TOGGLEREGHOST: | |
790 | return autofs4_toggle_reghost(sbi, p); | |
791 | case AUTOFS_IOC_ASKREGHOST: | |
792 | return autofs4_ask_reghost(sbi, p); | |
793 | ||
794 | case AUTOFS_IOC_ASKUMOUNT: | |
795 | return autofs4_ask_umount(filp->f_vfsmnt, p); | |
796 | ||
797 | /* return a single thing to expire */ | |
798 | case AUTOFS_IOC_EXPIRE: | |
799 | return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p); | |
800 | /* same as above, but can send multiple expires through pipe */ | |
801 | case AUTOFS_IOC_EXPIRE_MULTI: | |
802 | return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p); | |
803 | ||
804 | default: | |
805 | return -ENOSYS; | |
806 | } | |
807 | } |