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. /* 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. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  275. int oz_mode = autofs4_oz_mode(sbi);
  276. unsigned int lookup_type;
  277. int status;
  278. DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
  279. dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
  280. nd->flags);
  281. /* If it's our master or we shouldn't trigger a mount we're done */
  282. lookup_type = nd->flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY);
  283. if (oz_mode || !lookup_type)
  284. goto done;
  285. /* If an expire request is pending wait for it. */
  286. if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
  287. DPRINTK("waiting for active request %p name=%.*s",
  288. dentry, dentry->d_name.len, dentry->d_name.name);
  289. status = autofs4_wait(sbi, dentry, NFY_NONE);
  290. DPRINTK("request done status=%d", status);
  291. }
  292. /*
  293. * If the dentry contains directories then it is an
  294. * autofs multi-mount with no root mount offset. So
  295. * don't try to mount it again.
  296. */
  297. spin_lock(&dcache_lock);
  298. if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
  299. spin_unlock(&dcache_lock);
  300. status = try_to_fill_dentry(dentry, 0);
  301. if (status)
  302. goto out_error;
  303. /*
  304. * The mount succeeded but if there is no root mount
  305. * it must be an autofs multi-mount with no root offset
  306. * so we don't need to follow the mount.
  307. */
  308. if (d_mountpoint(dentry)) {
  309. if (!autofs4_follow_mount(&nd->mnt, &nd->dentry)) {
  310. status = -ENOENT;
  311. goto out_error;
  312. }
  313. }
  314. goto done;
  315. }
  316. spin_unlock(&dcache_lock);
  317. done:
  318. return NULL;
  319. out_error:
  320. path_release(nd);
  321. return ERR_PTR(status);
  322. }
  323. /*
  324. * Revalidate is called on every cache lookup. Some of those
  325. * cache lookups may actually happen while the dentry is not
  326. * yet completely filled in, and revalidate has to delay such
  327. * lookups..
  328. */
  329. static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
  330. {
  331. struct inode *dir = dentry->d_parent->d_inode;
  332. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  333. int oz_mode = autofs4_oz_mode(sbi);
  334. int flags = nd ? nd->flags : 0;
  335. int status = 1;
  336. /* Pending dentry */
  337. if (autofs4_ispending(dentry)) {
  338. /* The daemon never causes a mount to trigger */
  339. if (oz_mode)
  340. return 1;
  341. /*
  342. * A zero status is success otherwise we have a
  343. * negative error code.
  344. */
  345. status = try_to_fill_dentry(dentry, flags);
  346. if (status == 0)
  347. return 1;
  348. return status;
  349. }
  350. /* Negative dentry.. invalidate if "old" */
  351. if (dentry->d_inode == NULL)
  352. return 0;
  353. /* Check for a non-mountpoint directory with no contents */
  354. spin_lock(&dcache_lock);
  355. if (S_ISDIR(dentry->d_inode->i_mode) &&
  356. !d_mountpoint(dentry) &&
  357. __simple_empty(dentry)) {
  358. DPRINTK("dentry=%p %.*s, emptydir",
  359. dentry, dentry->d_name.len, dentry->d_name.name);
  360. spin_unlock(&dcache_lock);
  361. /* The daemon never causes a mount to trigger */
  362. if (oz_mode)
  363. return 1;
  364. /*
  365. * A zero status is success otherwise we have a
  366. * negative error code.
  367. */
  368. status = try_to_fill_dentry(dentry, flags);
  369. if (status == 0)
  370. return 1;
  371. return status;
  372. }
  373. spin_unlock(&dcache_lock);
  374. return 1;
  375. }
  376. void autofs4_dentry_release(struct dentry *de)
  377. {
  378. struct autofs_info *inf;
  379. DPRINTK("releasing %p", de);
  380. inf = autofs4_dentry_ino(de);
  381. de->d_fsdata = NULL;
  382. if (inf) {
  383. inf->dentry = NULL;
  384. inf->inode = NULL;
  385. autofs4_free_ino(inf);
  386. }
  387. }
  388. /* For dentries of directories in the root dir */
  389. static struct dentry_operations autofs4_root_dentry_operations = {
  390. .d_revalidate = autofs4_revalidate,
  391. .d_release = autofs4_dentry_release,
  392. };
  393. /* For other dentries */
  394. static struct dentry_operations autofs4_dentry_operations = {
  395. .d_revalidate = autofs4_revalidate,
  396. .d_release = autofs4_dentry_release,
  397. };
  398. /* Lookups in the root directory */
  399. static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  400. {
  401. struct autofs_sb_info *sbi;
  402. int oz_mode;
  403. DPRINTK("name = %.*s",
  404. dentry->d_name.len, dentry->d_name.name);
  405. /* File name too long to exist */
  406. if (dentry->d_name.len > NAME_MAX)
  407. return ERR_PTR(-ENAMETOOLONG);
  408. sbi = autofs4_sbi(dir->i_sb);
  409. oz_mode = autofs4_oz_mode(sbi);
  410. DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
  411. current->pid, process_group(current), sbi->catatonic, oz_mode);
  412. /*
  413. * Mark the dentry incomplete, but add it. This is needed so
  414. * that the VFS layer knows about the dentry, and we can count
  415. * on catching any lookups through the revalidate.
  416. *
  417. * Let all the hard work be done by the revalidate function that
  418. * needs to be able to do this anyway..
  419. *
  420. * We need to do this before we release the directory semaphore.
  421. */
  422. dentry->d_op = &autofs4_root_dentry_operations;
  423. if (!oz_mode) {
  424. spin_lock(&dentry->d_lock);
  425. dentry->d_flags |= DCACHE_AUTOFS_PENDING;
  426. spin_unlock(&dentry->d_lock);
  427. }
  428. dentry->d_fsdata = NULL;
  429. d_add(dentry, NULL);
  430. if (dentry->d_op && dentry->d_op->d_revalidate) {
  431. mutex_unlock(&dir->i_mutex);
  432. (dentry->d_op->d_revalidate)(dentry, nd);
  433. mutex_lock(&dir->i_mutex);
  434. }
  435. /*
  436. * If we are still pending, check if we had to handle
  437. * a signal. If so we can force a restart..
  438. */
  439. if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
  440. /* See if we were interrupted */
  441. if (signal_pending(current)) {
  442. sigset_t *sigset = &current->pending.signal;
  443. if (sigismember (sigset, SIGKILL) ||
  444. sigismember (sigset, SIGQUIT) ||
  445. sigismember (sigset, SIGINT)) {
  446. return ERR_PTR(-ERESTARTNOINTR);
  447. }
  448. }
  449. spin_lock(&dentry->d_lock);
  450. dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
  451. spin_unlock(&dentry->d_lock);
  452. }
  453. /*
  454. * If this dentry is unhashed, then we shouldn't honour this
  455. * lookup even if the dentry is positive. Returning ENOENT here
  456. * doesn't do the right thing for all system calls, but it should
  457. * be OK for the operations we permit from an autofs.
  458. */
  459. if (dentry->d_inode && d_unhashed(dentry))
  460. return ERR_PTR(-ENOENT);
  461. return NULL;
  462. }
  463. static int autofs4_dir_symlink(struct inode *dir,
  464. struct dentry *dentry,
  465. const char *symname)
  466. {
  467. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  468. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  469. struct autofs_info *p_ino;
  470. struct inode *inode;
  471. char *cp;
  472. DPRINTK("%s <- %.*s", symname,
  473. dentry->d_name.len, dentry->d_name.name);
  474. if (!autofs4_oz_mode(sbi))
  475. return -EACCES;
  476. ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
  477. if (ino == NULL)
  478. return -ENOSPC;
  479. ino->size = strlen(symname);
  480. ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
  481. if (cp == NULL) {
  482. kfree(ino);
  483. return -ENOSPC;
  484. }
  485. strcpy(cp, symname);
  486. inode = autofs4_get_inode(dir->i_sb, ino);
  487. d_instantiate(dentry, inode);
  488. if (dir == dir->i_sb->s_root->d_inode)
  489. dentry->d_op = &autofs4_root_dentry_operations;
  490. else
  491. dentry->d_op = &autofs4_dentry_operations;
  492. dentry->d_fsdata = ino;
  493. ino->dentry = dget(dentry);
  494. atomic_inc(&ino->count);
  495. p_ino = autofs4_dentry_ino(dentry->d_parent);
  496. if (p_ino && dentry->d_parent != dentry)
  497. atomic_inc(&p_ino->count);
  498. ino->inode = inode;
  499. dir->i_mtime = CURRENT_TIME;
  500. return 0;
  501. }
  502. /*
  503. * NOTE!
  504. *
  505. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  506. * that the file no longer exists. However, doing that means that the
  507. * VFS layer can turn the dentry into a negative dentry. We don't want
  508. * this, because since the unlink is probably the result of an expire.
  509. * We simply d_drop it, which allows the dentry lookup to remount it
  510. * if necessary.
  511. *
  512. * If a process is blocked on the dentry waiting for the expire to finish,
  513. * it will invalidate the dentry and try to mount with a new one.
  514. *
  515. * Also see autofs4_dir_rmdir()..
  516. */
  517. static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
  518. {
  519. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  520. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  521. struct autofs_info *p_ino;
  522. /* This allows root to remove symlinks */
  523. if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
  524. return -EACCES;
  525. if (atomic_dec_and_test(&ino->count)) {
  526. p_ino = autofs4_dentry_ino(dentry->d_parent);
  527. if (p_ino && dentry->d_parent != dentry)
  528. atomic_dec(&p_ino->count);
  529. }
  530. dput(ino->dentry);
  531. dentry->d_inode->i_size = 0;
  532. clear_nlink(dentry->d_inode);
  533. dir->i_mtime = CURRENT_TIME;
  534. d_drop(dentry);
  535. return 0;
  536. }
  537. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  538. {
  539. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  540. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  541. struct autofs_info *p_ino;
  542. if (!autofs4_oz_mode(sbi))
  543. return -EACCES;
  544. spin_lock(&dcache_lock);
  545. if (!list_empty(&dentry->d_subdirs)) {
  546. spin_unlock(&dcache_lock);
  547. return -ENOTEMPTY;
  548. }
  549. spin_lock(&dentry->d_lock);
  550. __d_drop(dentry);
  551. spin_unlock(&dentry->d_lock);
  552. spin_unlock(&dcache_lock);
  553. if (atomic_dec_and_test(&ino->count)) {
  554. p_ino = autofs4_dentry_ino(dentry->d_parent);
  555. if (p_ino && dentry->d_parent != dentry)
  556. atomic_dec(&p_ino->count);
  557. }
  558. dput(ino->dentry);
  559. dentry->d_inode->i_size = 0;
  560. clear_nlink(dentry->d_inode);
  561. if (dir->i_nlink)
  562. drop_nlink(dir);
  563. return 0;
  564. }
  565. static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  566. {
  567. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  568. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  569. struct autofs_info *p_ino;
  570. struct inode *inode;
  571. if ( !autofs4_oz_mode(sbi) )
  572. return -EACCES;
  573. DPRINTK("dentry %p, creating %.*s",
  574. dentry, dentry->d_name.len, dentry->d_name.name);
  575. ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
  576. if (ino == NULL)
  577. return -ENOSPC;
  578. inode = autofs4_get_inode(dir->i_sb, ino);
  579. d_instantiate(dentry, inode);
  580. if (dir == dir->i_sb->s_root->d_inode)
  581. dentry->d_op = &autofs4_root_dentry_operations;
  582. else
  583. dentry->d_op = &autofs4_dentry_operations;
  584. dentry->d_fsdata = ino;
  585. ino->dentry = dget(dentry);
  586. atomic_inc(&ino->count);
  587. p_ino = autofs4_dentry_ino(dentry->d_parent);
  588. if (p_ino && dentry->d_parent != dentry)
  589. atomic_inc(&p_ino->count);
  590. ino->inode = inode;
  591. inc_nlink(dir);
  592. dir->i_mtime = CURRENT_TIME;
  593. return 0;
  594. }
  595. /* Get/set timeout ioctl() operation */
  596. static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
  597. unsigned long __user *p)
  598. {
  599. int rv;
  600. unsigned long ntimeout;
  601. if ( (rv = get_user(ntimeout, p)) ||
  602. (rv = put_user(sbi->exp_timeout/HZ, p)) )
  603. return rv;
  604. if ( ntimeout > ULONG_MAX/HZ )
  605. sbi->exp_timeout = 0;
  606. else
  607. sbi->exp_timeout = ntimeout * HZ;
  608. return 0;
  609. }
  610. /* Return protocol version */
  611. static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
  612. {
  613. return put_user(sbi->version, p);
  614. }
  615. /* Return protocol sub version */
  616. static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
  617. {
  618. return put_user(sbi->sub_version, p);
  619. }
  620. /*
  621. * Tells the daemon whether we need to reghost or not. Also, clears
  622. * the reghost_needed flag.
  623. */
  624. static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
  625. {
  626. int status;
  627. DPRINTK("returning %d", sbi->needs_reghost);
  628. status = put_user(sbi->needs_reghost, p);
  629. if ( status )
  630. return status;
  631. sbi->needs_reghost = 0;
  632. return 0;
  633. }
  634. /*
  635. * Enable / Disable reghosting ioctl() operation
  636. */
  637. static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
  638. {
  639. int status;
  640. int val;
  641. status = get_user(val, p);
  642. DPRINTK("reghost = %d", val);
  643. if (status)
  644. return status;
  645. /* turn on/off reghosting, with the val */
  646. sbi->reghost_enabled = val;
  647. return 0;
  648. }
  649. /*
  650. * Tells the daemon whether it can umount the autofs mount.
  651. */
  652. static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
  653. {
  654. int status = 0;
  655. if (may_umount(mnt))
  656. status = 1;
  657. DPRINTK("returning %d", status);
  658. status = put_user(status, p);
  659. return status;
  660. }
  661. /* Identify autofs4_dentries - this is so we can tell if there's
  662. an extra dentry refcount or not. We only hold a refcount on the
  663. dentry if its non-negative (ie, d_inode != NULL)
  664. */
  665. int is_autofs4_dentry(struct dentry *dentry)
  666. {
  667. return dentry && dentry->d_inode &&
  668. (dentry->d_op == &autofs4_root_dentry_operations ||
  669. dentry->d_op == &autofs4_dentry_operations) &&
  670. dentry->d_fsdata != NULL;
  671. }
  672. /*
  673. * ioctl()'s on the root directory is the chief method for the daemon to
  674. * generate kernel reactions
  675. */
  676. static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
  677. unsigned int cmd, unsigned long arg)
  678. {
  679. struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
  680. void __user *p = (void __user *)arg;
  681. DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
  682. cmd,arg,sbi,process_group(current));
  683. if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  684. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
  685. return -ENOTTY;
  686. if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
  687. return -EPERM;
  688. switch(cmd) {
  689. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  690. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
  691. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  692. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  693. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  694. autofs4_catatonic_mode(sbi);
  695. return 0;
  696. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  697. return autofs4_get_protover(sbi, p);
  698. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  699. return autofs4_get_protosubver(sbi, p);
  700. case AUTOFS_IOC_SETTIMEOUT:
  701. return autofs4_get_set_timeout(sbi, p);
  702. case AUTOFS_IOC_TOGGLEREGHOST:
  703. return autofs4_toggle_reghost(sbi, p);
  704. case AUTOFS_IOC_ASKREGHOST:
  705. return autofs4_ask_reghost(sbi, p);
  706. case AUTOFS_IOC_ASKUMOUNT:
  707. return autofs4_ask_umount(filp->f_vfsmnt, p);
  708. /* return a single thing to expire */
  709. case AUTOFS_IOC_EXPIRE:
  710. return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
  711. /* same as above, but can send multiple expires through pipe */
  712. case AUTOFS_IOC_EXPIRE_MULTI:
  713. return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);
  714. default:
  715. return -ENOSYS;
  716. }
  717. }