root.c 21 KB

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