root.c 20 KB

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