root.c 21 KB

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