root.c 26 KB

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