root.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  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. static int autofs4_dir_open(struct inode *inode, struct file *file)
  76. {
  77. struct dentry *dentry = file->f_dentry;
  78. struct vfsmount *mnt = file->f_vfsmnt;
  79. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  80. struct dentry *cursor;
  81. int status;
  82. status = dcache_dir_open(inode, file);
  83. if (status)
  84. goto out;
  85. cursor = file->private_data;
  86. cursor->d_fsdata = NULL;
  87. DPRINTK("file=%p dentry=%p %.*s",
  88. file, dentry, dentry->d_name.len, dentry->d_name.name);
  89. if (autofs4_oz_mode(sbi))
  90. goto out;
  91. if (autofs4_ispending(dentry)) {
  92. DPRINTK("dentry busy");
  93. dcache_dir_close(inode, file);
  94. status = -EBUSY;
  95. goto out;
  96. }
  97. status = -ENOENT;
  98. if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
  99. struct nameidata nd;
  100. int empty, ret;
  101. /* In case there are stale directory dentrys from a failed mount */
  102. spin_lock(&dcache_lock);
  103. empty = list_empty(&dentry->d_subdirs);
  104. spin_unlock(&dcache_lock);
  105. if (!empty)
  106. d_invalidate(dentry);
  107. nd.flags = LOOKUP_DIRECTORY;
  108. ret = (dentry->d_op->d_revalidate)(dentry, &nd);
  109. if (!ret) {
  110. dcache_dir_close(inode, file);
  111. goto out;
  112. }
  113. }
  114. if (d_mountpoint(dentry)) {
  115. struct file *fp = NULL;
  116. struct vfsmount *fp_mnt = mntget(mnt);
  117. struct dentry *fp_dentry = dget(dentry);
  118. if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
  119. dput(fp_dentry);
  120. mntput(fp_mnt);
  121. dcache_dir_close(inode, file);
  122. goto out;
  123. }
  124. fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
  125. status = PTR_ERR(fp);
  126. if (IS_ERR(fp)) {
  127. dcache_dir_close(inode, file);
  128. goto out;
  129. }
  130. cursor->d_fsdata = fp;
  131. }
  132. return 0;
  133. out:
  134. return status;
  135. }
  136. static int autofs4_dir_close(struct inode *inode, struct file *file)
  137. {
  138. struct dentry *dentry = file->f_dentry;
  139. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  140. struct dentry *cursor = file->private_data;
  141. int status = 0;
  142. DPRINTK("file=%p dentry=%p %.*s",
  143. file, dentry, dentry->d_name.len, dentry->d_name.name);
  144. if (autofs4_oz_mode(sbi))
  145. goto out;
  146. if (autofs4_ispending(dentry)) {
  147. DPRINTK("dentry busy");
  148. status = -EBUSY;
  149. goto out;
  150. }
  151. if (d_mountpoint(dentry)) {
  152. struct file *fp = cursor->d_fsdata;
  153. if (!fp) {
  154. status = -ENOENT;
  155. goto out;
  156. }
  157. filp_close(fp, current->files);
  158. }
  159. out:
  160. dcache_dir_close(inode, file);
  161. return status;
  162. }
  163. static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
  164. {
  165. struct dentry *dentry = file->f_dentry;
  166. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  167. struct dentry *cursor = file->private_data;
  168. int status;
  169. DPRINTK("file=%p dentry=%p %.*s",
  170. file, dentry, dentry->d_name.len, dentry->d_name.name);
  171. if (autofs4_oz_mode(sbi))
  172. goto out;
  173. if (autofs4_ispending(dentry)) {
  174. DPRINTK("dentry busy");
  175. return -EBUSY;
  176. }
  177. if (d_mountpoint(dentry)) {
  178. struct file *fp = cursor->d_fsdata;
  179. if (!fp)
  180. return -ENOENT;
  181. if (!fp->f_op || !fp->f_op->readdir)
  182. goto out;
  183. status = vfs_readdir(fp, filldir, dirent);
  184. file->f_pos = fp->f_pos;
  185. if (status)
  186. autofs4_copy_atime(file, fp);
  187. return status;
  188. }
  189. out:
  190. return dcache_readdir(file, dirent, filldir);
  191. }
  192. static int try_to_fill_dentry(struct dentry *dentry, int flags)
  193. {
  194. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  195. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  196. int status = 0;
  197. /* Block on any pending expiry here; invalidate the dentry
  198. when expiration is done to trigger mount request with a new
  199. dentry */
  200. if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
  201. DPRINTK("waiting for expire %p name=%.*s",
  202. dentry, dentry->d_name.len, dentry->d_name.name);
  203. status = autofs4_wait(sbi, dentry, NFY_NONE);
  204. DPRINTK("expire done status=%d", status);
  205. /*
  206. * If the directory still exists the mount request must
  207. * continue otherwise it can't be followed at the right
  208. * time during the walk.
  209. */
  210. status = d_invalidate(dentry);
  211. if (status != -EBUSY)
  212. return 0;
  213. }
  214. DPRINTK("dentry=%p %.*s ino=%p",
  215. dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
  216. /*
  217. * Wait for a pending mount, triggering one if there
  218. * isn't one already
  219. */
  220. if (dentry->d_inode == NULL) {
  221. DPRINTK("waiting for mount name=%.*s",
  222. dentry->d_name.len, dentry->d_name.name);
  223. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  224. DPRINTK("mount done status=%d", status);
  225. if (status && dentry->d_inode)
  226. return 0; /* Try to get the kernel to invalidate this dentry */
  227. /* Turn this into a real negative dentry? */
  228. if (status == -ENOENT) {
  229. spin_lock(&dentry->d_lock);
  230. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  231. spin_unlock(&dentry->d_lock);
  232. return 0;
  233. } else if (status) {
  234. /* Return a negative dentry, but leave it "pending" */
  235. return 0;
  236. }
  237. /* Trigger mount for path component or follow link */
  238. } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
  239. current->link_count) {
  240. DPRINTK("waiting for mount name=%.*s",
  241. dentry->d_name.len, dentry->d_name.name);
  242. spin_lock(&dentry->d_lock);
  243. dentry->d_flags |= DCACHE_AUTOFS_PENDING;
  244. spin_unlock(&dentry->d_lock);
  245. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  246. DPRINTK("mount done status=%d", status);
  247. if (status) {
  248. spin_lock(&dentry->d_lock);
  249. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  250. spin_unlock(&dentry->d_lock);
  251. return 0;
  252. }
  253. }
  254. /* Initialize expiry counter after successful mount */
  255. if (ino)
  256. ino->last_used = jiffies;
  257. spin_lock(&dentry->d_lock);
  258. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  259. spin_unlock(&dentry->d_lock);
  260. return 1;
  261. }
  262. /*
  263. * Revalidate is called on every cache lookup. Some of those
  264. * cache lookups may actually happen while the dentry is not
  265. * yet completely filled in, and revalidate has to delay such
  266. * lookups..
  267. */
  268. static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
  269. {
  270. struct inode *dir = dentry->d_parent->d_inode;
  271. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  272. int oz_mode = autofs4_oz_mode(sbi);
  273. int flags = nd ? nd->flags : 0;
  274. int status = 1;
  275. /* Pending dentry */
  276. if (autofs4_ispending(dentry)) {
  277. if (!oz_mode)
  278. status = try_to_fill_dentry(dentry, flags);
  279. return status;
  280. }
  281. /* Negative dentry.. invalidate if "old" */
  282. if (dentry->d_inode == NULL)
  283. return 0;
  284. /* Check for a non-mountpoint directory with no contents */
  285. spin_lock(&dcache_lock);
  286. if (S_ISDIR(dentry->d_inode->i_mode) &&
  287. !d_mountpoint(dentry) &&
  288. __simple_empty(dentry)) {
  289. DPRINTK("dentry=%p %.*s, emptydir",
  290. dentry, dentry->d_name.len, dentry->d_name.name);
  291. spin_unlock(&dcache_lock);
  292. if (!oz_mode)
  293. status = try_to_fill_dentry(dentry, flags);
  294. return status;
  295. }
  296. spin_unlock(&dcache_lock);
  297. return 1;
  298. }
  299. static void autofs4_dentry_release(struct dentry *de)
  300. {
  301. struct autofs_info *inf;
  302. DPRINTK("releasing %p", de);
  303. inf = autofs4_dentry_ino(de);
  304. de->d_fsdata = NULL;
  305. if (inf) {
  306. inf->dentry = NULL;
  307. inf->inode = NULL;
  308. autofs4_free_ino(inf);
  309. }
  310. }
  311. /* For dentries of directories in the root dir */
  312. static struct dentry_operations autofs4_root_dentry_operations = {
  313. .d_revalidate = autofs4_revalidate,
  314. .d_release = autofs4_dentry_release,
  315. };
  316. /* For other dentries */
  317. static struct dentry_operations autofs4_dentry_operations = {
  318. .d_revalidate = autofs4_revalidate,
  319. .d_release = autofs4_dentry_release,
  320. };
  321. /* Lookups in the root directory */
  322. static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  323. {
  324. struct autofs_sb_info *sbi;
  325. int oz_mode;
  326. DPRINTK("name = %.*s",
  327. dentry->d_name.len, dentry->d_name.name);
  328. /* File name too long to exist */
  329. if (dentry->d_name.len > NAME_MAX)
  330. return ERR_PTR(-ENAMETOOLONG);
  331. sbi = autofs4_sbi(dir->i_sb);
  332. oz_mode = autofs4_oz_mode(sbi);
  333. DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
  334. current->pid, process_group(current), sbi->catatonic, oz_mode);
  335. /*
  336. * Mark the dentry incomplete, but add it. This is needed so
  337. * that the VFS layer knows about the dentry, and we can count
  338. * on catching any lookups through the revalidate.
  339. *
  340. * Let all the hard work be done by the revalidate function that
  341. * needs to be able to do this anyway..
  342. *
  343. * We need to do this before we release the directory semaphore.
  344. */
  345. dentry->d_op = &autofs4_root_dentry_operations;
  346. if (!oz_mode) {
  347. spin_lock(&dentry->d_lock);
  348. dentry->d_flags |= DCACHE_AUTOFS_PENDING;
  349. spin_unlock(&dentry->d_lock);
  350. }
  351. dentry->d_fsdata = NULL;
  352. d_add(dentry, NULL);
  353. if (dentry->d_op && dentry->d_op->d_revalidate) {
  354. mutex_unlock(&dir->i_mutex);
  355. (dentry->d_op->d_revalidate)(dentry, nd);
  356. mutex_lock(&dir->i_mutex);
  357. }
  358. /*
  359. * If we are still pending, check if we had to handle
  360. * a signal. If so we can force a restart..
  361. */
  362. if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
  363. /* See if we were interrupted */
  364. if (signal_pending(current)) {
  365. sigset_t *sigset = &current->pending.signal;
  366. if (sigismember (sigset, SIGKILL) ||
  367. sigismember (sigset, SIGQUIT) ||
  368. sigismember (sigset, SIGINT)) {
  369. return ERR_PTR(-ERESTARTNOINTR);
  370. }
  371. }
  372. }
  373. /*
  374. * If this dentry is unhashed, then we shouldn't honour this
  375. * lookup even if the dentry is positive. Returning ENOENT here
  376. * doesn't do the right thing for all system calls, but it should
  377. * be OK for the operations we permit from an autofs.
  378. */
  379. if (dentry->d_inode && d_unhashed(dentry))
  380. return ERR_PTR(-ENOENT);
  381. return NULL;
  382. }
  383. static int autofs4_dir_symlink(struct inode *dir,
  384. struct dentry *dentry,
  385. const char *symname)
  386. {
  387. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  388. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  389. struct autofs_info *p_ino;
  390. struct inode *inode;
  391. char *cp;
  392. DPRINTK("%s <- %.*s", symname,
  393. dentry->d_name.len, dentry->d_name.name);
  394. if (!autofs4_oz_mode(sbi))
  395. return -EACCES;
  396. ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
  397. if (ino == NULL)
  398. return -ENOSPC;
  399. ino->size = strlen(symname);
  400. ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
  401. if (cp == NULL) {
  402. kfree(ino);
  403. return -ENOSPC;
  404. }
  405. strcpy(cp, symname);
  406. inode = autofs4_get_inode(dir->i_sb, ino);
  407. d_instantiate(dentry, inode);
  408. if (dir == dir->i_sb->s_root->d_inode)
  409. dentry->d_op = &autofs4_root_dentry_operations;
  410. else
  411. dentry->d_op = &autofs4_dentry_operations;
  412. dentry->d_fsdata = ino;
  413. ino->dentry = dget(dentry);
  414. atomic_inc(&ino->count);
  415. p_ino = autofs4_dentry_ino(dentry->d_parent);
  416. if (p_ino && dentry->d_parent != dentry)
  417. atomic_inc(&p_ino->count);
  418. ino->inode = inode;
  419. dir->i_mtime = CURRENT_TIME;
  420. return 0;
  421. }
  422. /*
  423. * NOTE!
  424. *
  425. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  426. * that the file no longer exists. However, doing that means that the
  427. * VFS layer can turn the dentry into a negative dentry. We don't want
  428. * this, because since the unlink is probably the result of an expire.
  429. * We simply d_drop it, which allows the dentry lookup to remount it
  430. * if necessary.
  431. *
  432. * If a process is blocked on the dentry waiting for the expire to finish,
  433. * it will invalidate the dentry and try to mount with a new one.
  434. *
  435. * Also see autofs4_dir_rmdir()..
  436. */
  437. static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
  438. {
  439. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  440. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  441. struct autofs_info *p_ino;
  442. /* This allows root to remove symlinks */
  443. if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
  444. return -EACCES;
  445. if (atomic_dec_and_test(&ino->count)) {
  446. p_ino = autofs4_dentry_ino(dentry->d_parent);
  447. if (p_ino && dentry->d_parent != dentry)
  448. atomic_dec(&p_ino->count);
  449. }
  450. dput(ino->dentry);
  451. dentry->d_inode->i_size = 0;
  452. dentry->d_inode->i_nlink = 0;
  453. dir->i_mtime = CURRENT_TIME;
  454. d_drop(dentry);
  455. return 0;
  456. }
  457. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  458. {
  459. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  460. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  461. struct autofs_info *p_ino;
  462. if (!autofs4_oz_mode(sbi))
  463. return -EACCES;
  464. spin_lock(&dcache_lock);
  465. if (!list_empty(&dentry->d_subdirs)) {
  466. spin_unlock(&dcache_lock);
  467. return -ENOTEMPTY;
  468. }
  469. spin_lock(&dentry->d_lock);
  470. __d_drop(dentry);
  471. spin_unlock(&dentry->d_lock);
  472. spin_unlock(&dcache_lock);
  473. if (atomic_dec_and_test(&ino->count)) {
  474. p_ino = autofs4_dentry_ino(dentry->d_parent);
  475. if (p_ino && dentry->d_parent != dentry)
  476. atomic_dec(&p_ino->count);
  477. }
  478. dput(ino->dentry);
  479. dentry->d_inode->i_size = 0;
  480. dentry->d_inode->i_nlink = 0;
  481. if (dir->i_nlink)
  482. dir->i_nlink--;
  483. return 0;
  484. }
  485. static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  486. {
  487. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  488. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  489. struct autofs_info *p_ino;
  490. struct inode *inode;
  491. if ( !autofs4_oz_mode(sbi) )
  492. return -EACCES;
  493. DPRINTK("dentry %p, creating %.*s",
  494. dentry, dentry->d_name.len, dentry->d_name.name);
  495. ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
  496. if (ino == NULL)
  497. return -ENOSPC;
  498. inode = autofs4_get_inode(dir->i_sb, ino);
  499. d_instantiate(dentry, inode);
  500. if (dir == dir->i_sb->s_root->d_inode)
  501. dentry->d_op = &autofs4_root_dentry_operations;
  502. else
  503. dentry->d_op = &autofs4_dentry_operations;
  504. dentry->d_fsdata = ino;
  505. ino->dentry = dget(dentry);
  506. atomic_inc(&ino->count);
  507. p_ino = autofs4_dentry_ino(dentry->d_parent);
  508. if (p_ino && dentry->d_parent != dentry)
  509. atomic_inc(&p_ino->count);
  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))
  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. }