root.c 26 KB

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