root.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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 long autofs4_root_ioctl(struct file *filp,
  827. unsigned int cmd, unsigned long arg)
  828. {
  829. struct inode *inode = filp->f_dentry->d_inode;
  830. return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  831. }
  832. #ifdef CONFIG_COMPAT
  833. static long autofs4_root_compat_ioctl(struct file *filp,
  834. unsigned int cmd, unsigned long arg)
  835. {
  836. struct inode *inode = filp->f_path.dentry->d_inode;
  837. int ret;
  838. if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
  839. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
  840. else
  841. ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
  842. (unsigned long)compat_ptr(arg));
  843. return ret;
  844. }
  845. #endif