root.c 21 KB

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