root.c 21 KB

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