inode.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* -*- linux-c -*- --------------------------------------------------------- *
  2. *
  3. * linux/fs/autofs/inode.c
  4. *
  5. * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
  6. *
  7. * This file is part of the Linux kernel and is made available under
  8. * the terms of the GNU General Public License, version 2, or at your
  9. * option, any later version, incorporated herein by reference.
  10. *
  11. * ------------------------------------------------------------------------- */
  12. #include <linux/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/slab.h>
  15. #include <linux/file.h>
  16. #include <linux/parser.h>
  17. #include <linux/bitops.h>
  18. #include <linux/magic.h>
  19. #include "autofs_i.h"
  20. #include <linux/module.h>
  21. void autofs_kill_sb(struct super_block *sb)
  22. {
  23. struct autofs_sb_info *sbi = autofs_sbi(sb);
  24. unsigned int n;
  25. /*
  26. * In the event of a failure in get_sb_nodev the superblock
  27. * info is not present so nothing else has been setup, so
  28. * just call kill_anon_super when we are called from
  29. * deactivate_super.
  30. */
  31. if (!sbi)
  32. goto out_kill_sb;
  33. if (!sbi->catatonic)
  34. autofs_catatonic_mode(sbi); /* Free wait queues, close pipe */
  35. put_pid(sbi->oz_pgrp);
  36. autofs_hash_nuke(sbi);
  37. for (n = 0; n < AUTOFS_MAX_SYMLINKS; n++) {
  38. if (test_bit(n, sbi->symlink_bitmap))
  39. kfree(sbi->symlink[n].data);
  40. }
  41. kfree(sb->s_fs_info);
  42. out_kill_sb:
  43. DPRINTK(("autofs: shutting down\n"));
  44. kill_anon_super(sb);
  45. }
  46. static const struct super_operations autofs_sops = {
  47. .statfs = simple_statfs,
  48. .show_options = generic_show_options,
  49. };
  50. enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
  51. static const match_table_t autofs_tokens = {
  52. {Opt_fd, "fd=%u"},
  53. {Opt_uid, "uid=%u"},
  54. {Opt_gid, "gid=%u"},
  55. {Opt_pgrp, "pgrp=%u"},
  56. {Opt_minproto, "minproto=%u"},
  57. {Opt_maxproto, "maxproto=%u"},
  58. {Opt_err, NULL}
  59. };
  60. static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
  61. pid_t *pgrp, int *minproto, int *maxproto)
  62. {
  63. char *p;
  64. substring_t args[MAX_OPT_ARGS];
  65. int option;
  66. *uid = current_uid();
  67. *gid = current_gid();
  68. *pgrp = task_pgrp_nr(current);
  69. *minproto = *maxproto = AUTOFS_PROTO_VERSION;
  70. *pipefd = -1;
  71. if (!options)
  72. return 1;
  73. while ((p = strsep(&options, ",")) != NULL) {
  74. int token;
  75. if (!*p)
  76. continue;
  77. token = match_token(p, autofs_tokens, args);
  78. switch (token) {
  79. case Opt_fd:
  80. if (match_int(&args[0], &option))
  81. return 1;
  82. *pipefd = option;
  83. break;
  84. case Opt_uid:
  85. if (match_int(&args[0], &option))
  86. return 1;
  87. *uid = option;
  88. break;
  89. case Opt_gid:
  90. if (match_int(&args[0], &option))
  91. return 1;
  92. *gid = option;
  93. break;
  94. case Opt_pgrp:
  95. if (match_int(&args[0], &option))
  96. return 1;
  97. *pgrp = option;
  98. break;
  99. case Opt_minproto:
  100. if (match_int(&args[0], &option))
  101. return 1;
  102. *minproto = option;
  103. break;
  104. case Opt_maxproto:
  105. if (match_int(&args[0], &option))
  106. return 1;
  107. *maxproto = option;
  108. break;
  109. default:
  110. return 1;
  111. }
  112. }
  113. return (*pipefd < 0);
  114. }
  115. int autofs_fill_super(struct super_block *s, void *data, int silent)
  116. {
  117. struct inode * root_inode;
  118. struct dentry * root;
  119. struct file * pipe;
  120. int pipefd;
  121. struct autofs_sb_info *sbi;
  122. int minproto, maxproto;
  123. pid_t pgid;
  124. save_mount_options(s, data);
  125. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  126. if (!sbi)
  127. goto fail_unlock;
  128. DPRINTK(("autofs: starting up, sbi = %p\n",sbi));
  129. s->s_fs_info = sbi;
  130. sbi->magic = AUTOFS_SBI_MAGIC;
  131. sbi->pipe = NULL;
  132. sbi->catatonic = 1;
  133. sbi->exp_timeout = 0;
  134. autofs_initialize_hash(&sbi->dirhash);
  135. sbi->queues = NULL;
  136. memset(sbi->symlink_bitmap, 0, sizeof(long)*AUTOFS_SYMLINK_BITMAP_LEN);
  137. sbi->next_dir_ino = AUTOFS_FIRST_DIR_INO;
  138. s->s_blocksize = 1024;
  139. s->s_blocksize_bits = 10;
  140. s->s_magic = AUTOFS_SUPER_MAGIC;
  141. s->s_op = &autofs_sops;
  142. s->s_time_gran = 1;
  143. sbi->sb = s;
  144. root_inode = autofs_iget(s, AUTOFS_ROOT_INO);
  145. if (IS_ERR(root_inode))
  146. goto fail_free;
  147. root = d_alloc_root(root_inode);
  148. pipe = NULL;
  149. if (!root)
  150. goto fail_iput;
  151. /* Can this call block? - WTF cares? s is locked. */
  152. if (parse_options(data, &pipefd, &root_inode->i_uid,
  153. &root_inode->i_gid, &pgid, &minproto,
  154. &maxproto)) {
  155. printk("autofs: called with bogus options\n");
  156. goto fail_dput;
  157. }
  158. /* Couldn't this be tested earlier? */
  159. if (minproto > AUTOFS_PROTO_VERSION ||
  160. maxproto < AUTOFS_PROTO_VERSION) {
  161. printk("autofs: kernel does not match daemon version\n");
  162. goto fail_dput;
  163. }
  164. DPRINTK(("autofs: pipe fd = %d, pgrp = %u\n", pipefd, pgid));
  165. sbi->oz_pgrp = find_get_pid(pgid);
  166. if (!sbi->oz_pgrp) {
  167. printk("autofs: could not find process group %d\n", pgid);
  168. goto fail_dput;
  169. }
  170. pipe = fget(pipefd);
  171. if (!pipe) {
  172. printk("autofs: could not open pipe file descriptor\n");
  173. goto fail_put_pid;
  174. }
  175. if (!pipe->f_op || !pipe->f_op->write)
  176. goto fail_fput;
  177. sbi->pipe = pipe;
  178. sbi->catatonic = 0;
  179. /*
  180. * Success! Install the root dentry now to indicate completion.
  181. */
  182. s->s_root = root;
  183. return 0;
  184. fail_fput:
  185. printk("autofs: pipe file descriptor does not contain proper ops\n");
  186. fput(pipe);
  187. fail_put_pid:
  188. put_pid(sbi->oz_pgrp);
  189. fail_dput:
  190. dput(root);
  191. goto fail_free;
  192. fail_iput:
  193. printk("autofs: get root dentry failed\n");
  194. iput(root_inode);
  195. fail_free:
  196. kfree(sbi);
  197. s->s_fs_info = NULL;
  198. fail_unlock:
  199. return -EINVAL;
  200. }
  201. struct inode *autofs_iget(struct super_block *sb, unsigned long ino)
  202. {
  203. unsigned int n;
  204. struct autofs_sb_info *sbi = autofs_sbi(sb);
  205. struct inode *inode;
  206. inode = iget_locked(sb, ino);
  207. if (!inode)
  208. return ERR_PTR(-ENOMEM);
  209. if (!(inode->i_state & I_NEW))
  210. return inode;
  211. /* Initialize to the default case (stub directory) */
  212. inode->i_op = &simple_dir_inode_operations;
  213. inode->i_fop = &simple_dir_operations;
  214. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  215. inode->i_nlink = 2;
  216. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  217. if (ino == AUTOFS_ROOT_INO) {
  218. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
  219. inode->i_op = &autofs_root_inode_operations;
  220. inode->i_fop = &autofs_root_operations;
  221. goto done;
  222. }
  223. inode->i_uid = inode->i_sb->s_root->d_inode->i_uid;
  224. inode->i_gid = inode->i_sb->s_root->d_inode->i_gid;
  225. if (ino >= AUTOFS_FIRST_SYMLINK && ino < AUTOFS_FIRST_DIR_INO) {
  226. /* Symlink inode - should be in symlink list */
  227. struct autofs_symlink *sl;
  228. n = ino - AUTOFS_FIRST_SYMLINK;
  229. if (n >= AUTOFS_MAX_SYMLINKS || !test_bit(n,sbi->symlink_bitmap)) {
  230. printk("autofs: Looking for bad symlink inode %u\n", (unsigned int) ino);
  231. goto done;
  232. }
  233. inode->i_op = &autofs_symlink_inode_operations;
  234. sl = &sbi->symlink[n];
  235. inode->i_private = sl;
  236. inode->i_mode = S_IFLNK | S_IRWXUGO;
  237. inode->i_mtime.tv_sec = inode->i_ctime.tv_sec = sl->mtime;
  238. inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  239. inode->i_size = sl->len;
  240. inode->i_nlink = 1;
  241. }
  242. done:
  243. unlock_new_inode(inode);
  244. return inode;
  245. }