root.c 22 KB

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