root.c 21 KB

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