root.c 22 KB

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