root.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  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/slab.h>
  18. #include <linux/param.h>
  19. #include <linux/time.h>
  20. #include <linux/compat.h>
  21. #include <linux/smp_lock.h>
  22. #include "autofs_i.h"
  23. static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
  24. static int autofs4_dir_unlink(struct inode *,struct dentry *);
  25. static int autofs4_dir_rmdir(struct inode *,struct dentry *);
  26. static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
  27. static long autofs4_root_ioctl(struct file *,unsigned int,unsigned long);
  28. static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long);
  29. static int autofs4_dir_open(struct inode *inode, struct file *file);
  30. static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
  31. static void *autofs4_follow_link(struct dentry *, struct nameidata *);
  32. #define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
  33. #define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
  34. const struct file_operations autofs4_root_operations = {
  35. .open = dcache_dir_open,
  36. .release = dcache_dir_close,
  37. .read = generic_read_dir,
  38. .readdir = dcache_readdir,
  39. .llseek = dcache_dir_lseek,
  40. .unlocked_ioctl = autofs4_root_ioctl,
  41. #ifdef CONFIG_COMPAT
  42. .compat_ioctl = autofs4_root_compat_ioctl,
  43. #endif
  44. };
  45. const struct file_operations autofs4_dir_operations = {
  46. .open = autofs4_dir_open,
  47. .release = dcache_dir_close,
  48. .read = generic_read_dir,
  49. .readdir = dcache_readdir,
  50. .llseek = dcache_dir_lseek,
  51. };
  52. const struct inode_operations autofs4_indirect_root_inode_operations = {
  53. .lookup = autofs4_lookup,
  54. .unlink = autofs4_dir_unlink,
  55. .symlink = autofs4_dir_symlink,
  56. .mkdir = autofs4_dir_mkdir,
  57. .rmdir = autofs4_dir_rmdir,
  58. };
  59. const struct inode_operations autofs4_direct_root_inode_operations = {
  60. .lookup = autofs4_lookup,
  61. .unlink = autofs4_dir_unlink,
  62. .mkdir = autofs4_dir_mkdir,
  63. .rmdir = autofs4_dir_rmdir,
  64. .follow_link = autofs4_follow_link,
  65. };
  66. const struct inode_operations autofs4_dir_inode_operations = {
  67. .lookup = autofs4_lookup,
  68. .unlink = autofs4_dir_unlink,
  69. .symlink = autofs4_dir_symlink,
  70. .mkdir = autofs4_dir_mkdir,
  71. .rmdir = autofs4_dir_rmdir,
  72. };
  73. static void autofs4_add_active(struct dentry *dentry)
  74. {
  75. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  76. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  77. if (ino) {
  78. spin_lock(&sbi->lookup_lock);
  79. if (!ino->active_count) {
  80. if (list_empty(&ino->active))
  81. list_add(&ino->active, &sbi->active_list);
  82. }
  83. ino->active_count++;
  84. spin_unlock(&sbi->lookup_lock);
  85. }
  86. return;
  87. }
  88. static void autofs4_del_active(struct dentry *dentry)
  89. {
  90. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  91. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  92. if (ino) {
  93. spin_lock(&sbi->lookup_lock);
  94. ino->active_count--;
  95. if (!ino->active_count) {
  96. if (!list_empty(&ino->active))
  97. list_del_init(&ino->active);
  98. }
  99. spin_unlock(&sbi->lookup_lock);
  100. }
  101. return;
  102. }
  103. static unsigned int autofs4_need_mount(unsigned int flags)
  104. {
  105. unsigned int res = 0;
  106. if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS))
  107. res = 1;
  108. return res;
  109. }
  110. static int autofs4_dir_open(struct inode *inode, struct file *file)
  111. {
  112. struct dentry *dentry = file->f_path.dentry;
  113. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  114. DPRINTK("file=%p dentry=%p %.*s",
  115. file, dentry, dentry->d_name.len, dentry->d_name.name);
  116. if (autofs4_oz_mode(sbi))
  117. goto out;
  118. /*
  119. * An empty directory in an autofs file system is always a
  120. * mount point. The daemon must have failed to mount this
  121. * during lookup so it doesn't exist. This can happen, for
  122. * example, if user space returns an incorrect status for a
  123. * mount request. Otherwise we're doing a readdir on the
  124. * autofs file system so just let the libfs routines handle
  125. * it.
  126. */
  127. spin_lock(&dcache_lock);
  128. if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
  129. spin_unlock(&dcache_lock);
  130. return -ENOENT;
  131. }
  132. spin_unlock(&dcache_lock);
  133. out:
  134. return dcache_dir_open(inode, file);
  135. }
  136. static int try_to_fill_dentry(struct dentry *dentry, int flags)
  137. {
  138. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  139. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  140. int status;
  141. DPRINTK("dentry=%p %.*s ino=%p",
  142. dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
  143. /*
  144. * Wait for a pending mount, triggering one if there
  145. * isn't one already
  146. */
  147. if (dentry->d_inode == NULL) {
  148. DPRINTK("waiting for mount name=%.*s",
  149. dentry->d_name.len, dentry->d_name.name);
  150. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  151. DPRINTK("mount done status=%d", status);
  152. /* Turn this into a real negative dentry? */
  153. if (status == -ENOENT) {
  154. spin_lock(&sbi->fs_lock);
  155. ino->flags &= ~AUTOFS_INF_PENDING;
  156. spin_unlock(&sbi->fs_lock);
  157. return status;
  158. } else if (status) {
  159. /* Return a negative dentry, but leave it "pending" */
  160. return status;
  161. }
  162. /* Trigger mount for path component or follow link */
  163. } else if (ino->flags & AUTOFS_INF_PENDING ||
  164. autofs4_need_mount(flags)) {
  165. DPRINTK("waiting for mount name=%.*s",
  166. dentry->d_name.len, dentry->d_name.name);
  167. spin_lock(&sbi->fs_lock);
  168. ino->flags |= AUTOFS_INF_PENDING;
  169. spin_unlock(&sbi->fs_lock);
  170. status = autofs4_wait(sbi, dentry, NFY_MOUNT);
  171. DPRINTK("mount done status=%d", status);
  172. if (status) {
  173. spin_lock(&sbi->fs_lock);
  174. ino->flags &= ~AUTOFS_INF_PENDING;
  175. spin_unlock(&sbi->fs_lock);
  176. return status;
  177. }
  178. }
  179. /* Initialize expiry counter after successful mount */
  180. if (ino)
  181. ino->last_used = jiffies;
  182. spin_lock(&sbi->fs_lock);
  183. ino->flags &= ~AUTOFS_INF_PENDING;
  184. spin_unlock(&sbi->fs_lock);
  185. return 0;
  186. }
  187. /* For autofs direct mounts the follow link triggers the mount */
  188. static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
  189. {
  190. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  191. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  192. int oz_mode = autofs4_oz_mode(sbi);
  193. unsigned int lookup_type;
  194. int status;
  195. DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
  196. dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
  197. nd->flags);
  198. /*
  199. * For an expire of a covered direct or offset mount we need
  200. * to break out of follow_down() at the autofs mount trigger
  201. * (d_mounted--), so we can see the expiring flag, and manage
  202. * the blocking and following here until the expire is completed.
  203. */
  204. if (oz_mode) {
  205. spin_lock(&sbi->fs_lock);
  206. if (ino->flags & AUTOFS_INF_EXPIRING) {
  207. spin_unlock(&sbi->fs_lock);
  208. /* Follow down to our covering mount. */
  209. if (!follow_down(&nd->path))
  210. goto done;
  211. goto follow;
  212. }
  213. spin_unlock(&sbi->fs_lock);
  214. goto done;
  215. }
  216. /* If an expire request is pending everyone must wait. */
  217. autofs4_expire_wait(dentry);
  218. /* We trigger a mount for almost all flags */
  219. lookup_type = autofs4_need_mount(nd->flags);
  220. spin_lock(&sbi->fs_lock);
  221. spin_lock(&dcache_lock);
  222. if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) {
  223. spin_unlock(&dcache_lock);
  224. spin_unlock(&sbi->fs_lock);
  225. goto follow;
  226. }
  227. /*
  228. * If the dentry contains directories then it is an autofs
  229. * multi-mount with no root mount offset. So don't try to
  230. * mount it again.
  231. */
  232. if (ino->flags & AUTOFS_INF_PENDING ||
  233. (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) {
  234. spin_unlock(&dcache_lock);
  235. spin_unlock(&sbi->fs_lock);
  236. status = try_to_fill_dentry(dentry, nd->flags);
  237. if (status)
  238. goto out_error;
  239. goto follow;
  240. }
  241. spin_unlock(&dcache_lock);
  242. spin_unlock(&sbi->fs_lock);
  243. follow:
  244. /*
  245. * If there is no root mount it must be an autofs
  246. * multi-mount with no root offset so we don't need
  247. * to follow it.
  248. */
  249. if (d_mountpoint(dentry)) {
  250. if (!autofs4_follow_mount(&nd->path)) {
  251. status = -ENOENT;
  252. goto out_error;
  253. }
  254. }
  255. done:
  256. return NULL;
  257. out_error:
  258. path_put(&nd->path);
  259. return ERR_PTR(status);
  260. }
  261. /*
  262. * Revalidate is called on every cache lookup. Some of those
  263. * cache lookups may actually happen while the dentry is not
  264. * yet completely filled in, and revalidate has to delay such
  265. * lookups..
  266. */
  267. static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
  268. {
  269. struct inode *dir = dentry->d_parent->d_inode;
  270. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  271. int oz_mode = autofs4_oz_mode(sbi);
  272. int flags = nd ? nd->flags : 0;
  273. int status = 1;
  274. /* Pending dentry */
  275. spin_lock(&sbi->fs_lock);
  276. if (autofs4_ispending(dentry)) {
  277. /* The daemon never causes a mount to trigger */
  278. spin_unlock(&sbi->fs_lock);
  279. if (oz_mode)
  280. return 1;
  281. /*
  282. * If the directory has gone away due to an expire
  283. * we have been called as ->d_revalidate() and so
  284. * we need to return false and proceed to ->lookup().
  285. */
  286. if (autofs4_expire_wait(dentry) == -EAGAIN)
  287. return 0;
  288. /*
  289. * A zero status is success otherwise we have a
  290. * negative error code.
  291. */
  292. status = try_to_fill_dentry(dentry, flags);
  293. if (status == 0)
  294. return 1;
  295. return status;
  296. }
  297. spin_unlock(&sbi->fs_lock);
  298. /* Negative dentry.. invalidate if "old" */
  299. if (dentry->d_inode == NULL)
  300. return 0;
  301. /* Check for a non-mountpoint directory with no contents */
  302. spin_lock(&dcache_lock);
  303. if (S_ISDIR(dentry->d_inode->i_mode) &&
  304. !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
  305. DPRINTK("dentry=%p %.*s, emptydir",
  306. dentry, dentry->d_name.len, dentry->d_name.name);
  307. spin_unlock(&dcache_lock);
  308. /* The daemon never causes a mount to trigger */
  309. if (oz_mode)
  310. return 1;
  311. /*
  312. * A zero status is success otherwise we have a
  313. * negative error code.
  314. */
  315. status = try_to_fill_dentry(dentry, flags);
  316. if (status == 0)
  317. return 1;
  318. return status;
  319. }
  320. spin_unlock(&dcache_lock);
  321. return 1;
  322. }
  323. void autofs4_dentry_release(struct dentry *de)
  324. {
  325. struct autofs_info *inf;
  326. DPRINTK("releasing %p", de);
  327. inf = autofs4_dentry_ino(de);
  328. de->d_fsdata = NULL;
  329. if (inf) {
  330. struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
  331. if (sbi) {
  332. spin_lock(&sbi->lookup_lock);
  333. if (!list_empty(&inf->active))
  334. list_del(&inf->active);
  335. if (!list_empty(&inf->expiring))
  336. list_del(&inf->expiring);
  337. spin_unlock(&sbi->lookup_lock);
  338. }
  339. inf->dentry = NULL;
  340. inf->inode = NULL;
  341. autofs4_free_ino(inf);
  342. }
  343. }
  344. /* For dentries of directories in the root dir */
  345. static const struct dentry_operations autofs4_root_dentry_operations = {
  346. .d_revalidate = autofs4_revalidate,
  347. .d_release = autofs4_dentry_release,
  348. };
  349. /* For other dentries */
  350. static const struct dentry_operations autofs4_dentry_operations = {
  351. .d_revalidate = autofs4_revalidate,
  352. .d_release = autofs4_dentry_release,
  353. };
  354. static struct dentry *autofs4_lookup_active(struct dentry *dentry)
  355. {
  356. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  357. struct dentry *parent = dentry->d_parent;
  358. struct qstr *name = &dentry->d_name;
  359. unsigned int len = name->len;
  360. unsigned int hash = name->hash;
  361. const unsigned char *str = name->name;
  362. struct list_head *p, *head;
  363. spin_lock(&dcache_lock);
  364. spin_lock(&sbi->lookup_lock);
  365. head = &sbi->active_list;
  366. list_for_each(p, head) {
  367. struct autofs_info *ino;
  368. struct dentry *active;
  369. struct qstr *qstr;
  370. ino = list_entry(p, struct autofs_info, active);
  371. active = ino->dentry;
  372. spin_lock(&active->d_lock);
  373. /* Already gone? */
  374. if (atomic_read(&active->d_count) == 0)
  375. goto next;
  376. qstr = &active->d_name;
  377. if (active->d_name.hash != hash)
  378. goto next;
  379. if (active->d_parent != parent)
  380. goto next;
  381. if (qstr->len != len)
  382. goto next;
  383. if (memcmp(qstr->name, str, len))
  384. goto next;
  385. if (d_unhashed(active)) {
  386. dget(active);
  387. spin_unlock(&active->d_lock);
  388. spin_unlock(&sbi->lookup_lock);
  389. spin_unlock(&dcache_lock);
  390. return active;
  391. }
  392. next:
  393. spin_unlock(&active->d_lock);
  394. }
  395. spin_unlock(&sbi->lookup_lock);
  396. spin_unlock(&dcache_lock);
  397. return NULL;
  398. }
  399. static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
  400. {
  401. struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
  402. struct dentry *parent = dentry->d_parent;
  403. struct qstr *name = &dentry->d_name;
  404. unsigned int len = name->len;
  405. unsigned int hash = name->hash;
  406. const unsigned char *str = name->name;
  407. struct list_head *p, *head;
  408. spin_lock(&dcache_lock);
  409. spin_lock(&sbi->lookup_lock);
  410. head = &sbi->expiring_list;
  411. list_for_each(p, head) {
  412. struct autofs_info *ino;
  413. struct dentry *expiring;
  414. struct qstr *qstr;
  415. ino = list_entry(p, struct autofs_info, expiring);
  416. expiring = ino->dentry;
  417. spin_lock(&expiring->d_lock);
  418. /* Bad luck, we've already been dentry_iput */
  419. if (!expiring->d_inode)
  420. goto next;
  421. qstr = &expiring->d_name;
  422. if (expiring->d_name.hash != hash)
  423. goto next;
  424. if (expiring->d_parent != parent)
  425. goto next;
  426. if (qstr->len != len)
  427. goto next;
  428. if (memcmp(qstr->name, str, len))
  429. goto next;
  430. if (d_unhashed(expiring)) {
  431. dget(expiring);
  432. spin_unlock(&expiring->d_lock);
  433. spin_unlock(&sbi->lookup_lock);
  434. spin_unlock(&dcache_lock);
  435. return expiring;
  436. }
  437. next:
  438. spin_unlock(&expiring->d_lock);
  439. }
  440. spin_unlock(&sbi->lookup_lock);
  441. spin_unlock(&dcache_lock);
  442. return NULL;
  443. }
  444. /* Lookups in the root directory */
  445. static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
  446. {
  447. struct autofs_sb_info *sbi;
  448. struct autofs_info *ino;
  449. struct dentry *expiring, *active;
  450. int oz_mode;
  451. DPRINTK("name = %.*s",
  452. dentry->d_name.len, dentry->d_name.name);
  453. /* File name too long to exist */
  454. if (dentry->d_name.len > NAME_MAX)
  455. return ERR_PTR(-ENAMETOOLONG);
  456. sbi = autofs4_sbi(dir->i_sb);
  457. oz_mode = autofs4_oz_mode(sbi);
  458. DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
  459. current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
  460. active = autofs4_lookup_active(dentry);
  461. if (active) {
  462. dentry = active;
  463. ino = autofs4_dentry_ino(dentry);
  464. } else {
  465. /*
  466. * Mark the dentry incomplete but don't hash it. We do this
  467. * to serialize our inode creation operations (symlink and
  468. * mkdir) which prevents deadlock during the callback to
  469. * the daemon. Subsequent user space lookups for the same
  470. * dentry are placed on the wait queue while the daemon
  471. * itself is allowed passage unresticted so the create
  472. * operation itself can then hash the dentry. Finally,
  473. * we check for the hashed dentry and return the newly
  474. * hashed dentry.
  475. */
  476. dentry->d_op = &autofs4_root_dentry_operations;
  477. /*
  478. * And we need to ensure that the same dentry is used for
  479. * all following lookup calls until it is hashed so that
  480. * the dentry flags are persistent throughout the request.
  481. */
  482. ino = autofs4_init_ino(NULL, sbi, 0555);
  483. if (!ino)
  484. return ERR_PTR(-ENOMEM);
  485. dentry->d_fsdata = ino;
  486. ino->dentry = dentry;
  487. autofs4_add_active(dentry);
  488. d_instantiate(dentry, NULL);
  489. }
  490. if (!oz_mode) {
  491. mutex_unlock(&dir->i_mutex);
  492. expiring = autofs4_lookup_expiring(dentry);
  493. if (expiring) {
  494. /*
  495. * If we are racing with expire the request might not
  496. * be quite complete but the directory has been removed
  497. * so it must have been successful, so just wait for it.
  498. */
  499. autofs4_expire_wait(expiring);
  500. autofs4_del_expiring(expiring);
  501. dput(expiring);
  502. }
  503. spin_lock(&sbi->fs_lock);
  504. ino->flags |= AUTOFS_INF_PENDING;
  505. spin_unlock(&sbi->fs_lock);
  506. if (dentry->d_op && dentry->d_op->d_revalidate)
  507. (dentry->d_op->d_revalidate)(dentry, nd);
  508. mutex_lock(&dir->i_mutex);
  509. }
  510. /*
  511. * If we are still pending, check if we had to handle
  512. * a signal. If so we can force a restart..
  513. */
  514. if (ino->flags & AUTOFS_INF_PENDING) {
  515. /* See if we were interrupted */
  516. if (signal_pending(current)) {
  517. sigset_t *sigset = &current->pending.signal;
  518. if (sigismember (sigset, SIGKILL) ||
  519. sigismember (sigset, SIGQUIT) ||
  520. sigismember (sigset, SIGINT)) {
  521. if (active)
  522. dput(active);
  523. return ERR_PTR(-ERESTARTNOINTR);
  524. }
  525. }
  526. if (!oz_mode) {
  527. spin_lock(&sbi->fs_lock);
  528. ino->flags &= ~AUTOFS_INF_PENDING;
  529. spin_unlock(&sbi->fs_lock);
  530. }
  531. }
  532. /*
  533. * If this dentry is unhashed, then we shouldn't honour this
  534. * lookup. Returning ENOENT here doesn't do the right thing
  535. * for all system calls, but it should be OK for the operations
  536. * we permit from an autofs.
  537. */
  538. if (!oz_mode && d_unhashed(dentry)) {
  539. /*
  540. * A user space application can (and has done in the past)
  541. * remove and re-create this directory during the callback.
  542. * This can leave us with an unhashed dentry, but a
  543. * successful mount! So we need to perform another
  544. * cached lookup in case the dentry now exists.
  545. */
  546. struct dentry *parent = dentry->d_parent;
  547. struct dentry *new = d_lookup(parent, &dentry->d_name);
  548. if (new != NULL)
  549. dentry = new;
  550. else
  551. dentry = ERR_PTR(-ENOENT);
  552. if (active)
  553. dput(active);
  554. return dentry;
  555. }
  556. if (active)
  557. return active;
  558. return NULL;
  559. }
  560. static int autofs4_dir_symlink(struct inode *dir,
  561. struct dentry *dentry,
  562. const char *symname)
  563. {
  564. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  565. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  566. struct autofs_info *p_ino;
  567. struct inode *inode;
  568. char *cp;
  569. DPRINTK("%s <- %.*s", symname,
  570. dentry->d_name.len, dentry->d_name.name);
  571. if (!autofs4_oz_mode(sbi))
  572. return -EACCES;
  573. ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
  574. if (!ino)
  575. return -ENOMEM;
  576. autofs4_del_active(dentry);
  577. ino->size = strlen(symname);
  578. cp = kmalloc(ino->size + 1, GFP_KERNEL);
  579. if (!cp) {
  580. if (!dentry->d_fsdata)
  581. kfree(ino);
  582. return -ENOMEM;
  583. }
  584. strcpy(cp, symname);
  585. inode = autofs4_get_inode(dir->i_sb, ino);
  586. if (!inode) {
  587. kfree(cp);
  588. if (!dentry->d_fsdata)
  589. kfree(ino);
  590. return -ENOMEM;
  591. }
  592. d_add(dentry, inode);
  593. if (dir == dir->i_sb->s_root->d_inode)
  594. dentry->d_op = &autofs4_root_dentry_operations;
  595. else
  596. dentry->d_op = &autofs4_dentry_operations;
  597. dentry->d_fsdata = ino;
  598. ino->dentry = dget(dentry);
  599. atomic_inc(&ino->count);
  600. p_ino = autofs4_dentry_ino(dentry->d_parent);
  601. if (p_ino && dentry->d_parent != dentry)
  602. atomic_inc(&p_ino->count);
  603. ino->inode = inode;
  604. ino->u.symlink = cp;
  605. dir->i_mtime = CURRENT_TIME;
  606. return 0;
  607. }
  608. /*
  609. * NOTE!
  610. *
  611. * Normal filesystems would do a "d_delete()" to tell the VFS dcache
  612. * that the file no longer exists. However, doing that means that the
  613. * VFS layer can turn the dentry into a negative dentry. We don't want
  614. * this, because the unlink is probably the result of an expire.
  615. * We simply d_drop it and add it to a expiring list in the super block,
  616. * which allows the dentry lookup to check for an incomplete expire.
  617. *
  618. * If a process is blocked on the dentry waiting for the expire to finish,
  619. * it will invalidate the dentry and try to mount with a new one.
  620. *
  621. * Also see autofs4_dir_rmdir()..
  622. */
  623. static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
  624. {
  625. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  626. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  627. struct autofs_info *p_ino;
  628. /* This allows root to remove symlinks */
  629. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  630. return -EACCES;
  631. if (atomic_dec_and_test(&ino->count)) {
  632. p_ino = autofs4_dentry_ino(dentry->d_parent);
  633. if (p_ino && dentry->d_parent != dentry)
  634. atomic_dec(&p_ino->count);
  635. }
  636. dput(ino->dentry);
  637. dentry->d_inode->i_size = 0;
  638. clear_nlink(dentry->d_inode);
  639. dir->i_mtime = CURRENT_TIME;
  640. spin_lock(&dcache_lock);
  641. autofs4_add_expiring(dentry);
  642. spin_lock(&dentry->d_lock);
  643. __d_drop(dentry);
  644. spin_unlock(&dentry->d_lock);
  645. spin_unlock(&dcache_lock);
  646. return 0;
  647. }
  648. static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
  649. {
  650. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  651. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  652. struct autofs_info *p_ino;
  653. DPRINTK("dentry %p, removing %.*s",
  654. dentry, dentry->d_name.len, dentry->d_name.name);
  655. if (!autofs4_oz_mode(sbi))
  656. return -EACCES;
  657. spin_lock(&dcache_lock);
  658. if (!list_empty(&dentry->d_subdirs)) {
  659. spin_unlock(&dcache_lock);
  660. return -ENOTEMPTY;
  661. }
  662. autofs4_add_expiring(dentry);
  663. spin_lock(&dentry->d_lock);
  664. __d_drop(dentry);
  665. spin_unlock(&dentry->d_lock);
  666. spin_unlock(&dcache_lock);
  667. if (atomic_dec_and_test(&ino->count)) {
  668. p_ino = autofs4_dentry_ino(dentry->d_parent);
  669. if (p_ino && dentry->d_parent != dentry)
  670. atomic_dec(&p_ino->count);
  671. }
  672. dput(ino->dentry);
  673. dentry->d_inode->i_size = 0;
  674. clear_nlink(dentry->d_inode);
  675. if (dir->i_nlink)
  676. drop_nlink(dir);
  677. return 0;
  678. }
  679. static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  680. {
  681. struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
  682. struct autofs_info *ino = autofs4_dentry_ino(dentry);
  683. struct autofs_info *p_ino;
  684. struct inode *inode;
  685. if (!autofs4_oz_mode(sbi))
  686. return -EACCES;
  687. DPRINTK("dentry %p, creating %.*s",
  688. dentry, dentry->d_name.len, dentry->d_name.name);
  689. ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
  690. if (!ino)
  691. return -ENOMEM;
  692. autofs4_del_active(dentry);
  693. inode = autofs4_get_inode(dir->i_sb, ino);
  694. if (!inode) {
  695. if (!dentry->d_fsdata)
  696. kfree(ino);
  697. return -ENOMEM;
  698. }
  699. d_add(dentry, inode);
  700. if (dir == dir->i_sb->s_root->d_inode)
  701. dentry->d_op = &autofs4_root_dentry_operations;
  702. else
  703. dentry->d_op = &autofs4_dentry_operations;
  704. dentry->d_fsdata = ino;
  705. ino->dentry = dget(dentry);
  706. atomic_inc(&ino->count);
  707. p_ino = autofs4_dentry_ino(dentry->d_parent);
  708. if (p_ino && dentry->d_parent != dentry)
  709. atomic_inc(&p_ino->count);
  710. ino->inode = inode;
  711. inc_nlink(dir);
  712. dir->i_mtime = CURRENT_TIME;
  713. return 0;
  714. }
  715. /* Get/set timeout ioctl() operation */
  716. #ifdef CONFIG_COMPAT
  717. static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
  718. compat_ulong_t __user *p)
  719. {
  720. int rv;
  721. unsigned long ntimeout;
  722. if ((rv = get_user(ntimeout, p)) ||
  723. (rv = put_user(sbi->exp_timeout/HZ, p)))
  724. return rv;
  725. if (ntimeout > UINT_MAX/HZ)
  726. sbi->exp_timeout = 0;
  727. else
  728. sbi->exp_timeout = ntimeout * HZ;
  729. return 0;
  730. }
  731. #endif
  732. static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
  733. unsigned long __user *p)
  734. {
  735. int rv;
  736. unsigned long ntimeout;
  737. if ((rv = get_user(ntimeout, p)) ||
  738. (rv = put_user(sbi->exp_timeout/HZ, p)))
  739. return rv;
  740. if (ntimeout > ULONG_MAX/HZ)
  741. sbi->exp_timeout = 0;
  742. else
  743. sbi->exp_timeout = ntimeout * HZ;
  744. return 0;
  745. }
  746. /* Return protocol version */
  747. static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
  748. {
  749. return put_user(sbi->version, p);
  750. }
  751. /* Return protocol sub version */
  752. static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
  753. {
  754. return put_user(sbi->sub_version, p);
  755. }
  756. /*
  757. * Tells the daemon whether it can umount the autofs mount.
  758. */
  759. static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
  760. {
  761. int status = 0;
  762. if (may_umount(mnt))
  763. status = 1;
  764. DPRINTK("returning %d", status);
  765. status = put_user(status, p);
  766. return status;
  767. }
  768. /* Identify autofs4_dentries - this is so we can tell if there's
  769. an extra dentry refcount or not. We only hold a refcount on the
  770. dentry if its non-negative (ie, d_inode != NULL)
  771. */
  772. int is_autofs4_dentry(struct dentry *dentry)
  773. {
  774. return dentry && dentry->d_inode &&
  775. (dentry->d_op == &autofs4_root_dentry_operations ||
  776. dentry->d_op == &autofs4_dentry_operations) &&
  777. dentry->d_fsdata != NULL;
  778. }
  779. /*
  780. * ioctl()'s on the root directory is the chief method for the daemon to
  781. * generate kernel reactions
  782. */
  783. static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
  784. unsigned int cmd, unsigned long arg)
  785. {
  786. struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
  787. void __user *p = (void __user *)arg;
  788. DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
  789. cmd,arg,sbi,task_pgrp_nr(current));
  790. if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
  791. _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
  792. return -ENOTTY;
  793. if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
  794. return -EPERM;
  795. switch(cmd) {
  796. case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
  797. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
  798. case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
  799. return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
  800. case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
  801. autofs4_catatonic_mode(sbi);
  802. return 0;
  803. case AUTOFS_IOC_PROTOVER: /* Get protocol version */
  804. return autofs4_get_protover(sbi, p);
  805. case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
  806. return autofs4_get_protosubver(sbi, p);
  807. case AUTOFS_IOC_SETTIMEOUT:
  808. return autofs4_get_set_timeout(sbi, p);
  809. #ifdef CONFIG_COMPAT
  810. case AUTOFS_IOC_SETTIMEOUT32:
  811. return autofs4_compat_get_set_timeout(sbi, p);
  812. #endif
  813. case AUTOFS_IOC_ASKUMOUNT:
  814. return autofs4_ask_umount(filp->f_path.mnt, p);
  815. /* return a single thing to expire */
  816. case AUTOFS_IOC_EXPIRE:
  817. return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
  818. /* same as above, but can send multiple expires through pipe */
  819. case AUTOFS_IOC_EXPIRE_MULTI:
  820. return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
  821. default:
  822. return -ENOSYS;
  823. }
  824. }
  825. static long autofs4_root_ioctl(struct file *filp,
  826. unsigned int cmd, unsigned long arg)
  827. {
  828. long ret;
  829. struct inode *inode = filp->f_dentry->d_inode;
  830. lock_kernel();
  831. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  832. unlock_kernel();
  833. return ret;
  834. }
  835. #ifdef CONFIG_COMPAT
  836. static long autofs4_root_compat_ioctl(struct file *filp,
  837. unsigned int cmd, unsigned long arg)
  838. {
  839. struct inode *inode = filp->f_path.dentry->d_inode;
  840. int ret;
  841. lock_kernel();
  842. if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
  843. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  844. else
  845. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
  846. (unsigned long)compat_ptr(arg));
  847. unlock_kernel();
  848. return ret;
  849. }
  850. #endif