root.c 21 KB

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