inode.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. };
  49. enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
  50. static match_table_t autofs_tokens = {
  51. {Opt_fd, "fd=%u"},
  52. {Opt_uid, "uid=%u"},
  53. {Opt_gid, "gid=%u"},
  54. {Opt_pgrp, "pgrp=%u"},
  55. {Opt_minproto, "minproto=%u"},
  56. {Opt_maxproto, "maxproto=%u"},
  57. {Opt_err, NULL}
  58. };
  59. static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
  60. pid_t *pgrp, int *minproto, int *maxproto)
  61. {
  62. char *p;
  63. substring_t args[MAX_OPT_ARGS];
  64. int option;
  65. *uid = current->uid;
  66. *gid = current->gid;
  67. *pgrp = task_pgrp_nr(current);
  68. *minproto = *maxproto = AUTOFS_PROTO_VERSION;
  69. *pipefd = -1;
  70. if (!options)
  71. return 1;
  72. while ((p = strsep(&options, ",")) != NULL) {
  73. int token;
  74. if (!*p)
  75. continue;
  76. token = match_token(p, autofs_tokens, args);
  77. switch (token) {
  78. case Opt_fd:
  79. if (match_int(&args[0], &option))
  80. return 1;
  81. *pipefd = option;
  82. break;
  83. case Opt_uid:
  84. if (match_int(&args[0], &option))
  85. return 1;
  86. *uid = option;
  87. break;
  88. case Opt_gid:
  89. if (match_int(&args[0], &option))
  90. return 1;
  91. *gid = option;
  92. break;
  93. case Opt_pgrp:
  94. if (match_int(&args[0], &option))
  95. return 1;
  96. *pgrp = option;
  97. break;
  98. case Opt_minproto:
  99. if (match_int(&args[0], &option))
  100. return 1;
  101. *minproto = option;
  102. break;
  103. case Opt_maxproto:
  104. if (match_int(&args[0], &option))
  105. return 1;
  106. *maxproto = option;
  107. break;
  108. default:
  109. return 1;
  110. }
  111. }
  112. return (*pipefd < 0);
  113. }
  114. int autofs_fill_super(struct super_block *s, void *data, int silent)
  115. {
  116. struct inode * root_inode;
  117. struct dentry * root;
  118. struct file * pipe;
  119. int pipefd;
  120. struct autofs_sb_info *sbi;
  121. int minproto, maxproto;
  122. pid_t pgid;
  123. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  124. if (!sbi)
  125. goto fail_unlock;
  126. DPRINTK(("autofs: starting up, sbi = %p\n",sbi));
  127. s->s_fs_info = sbi;
  128. sbi->magic = AUTOFS_SBI_MAGIC;
  129. sbi->pipe = NULL;
  130. sbi->catatonic = 1;
  131. sbi->exp_timeout = 0;
  132. autofs_initialize_hash(&sbi->dirhash);
  133. sbi->queues = NULL;
  134. memset(sbi->symlink_bitmap, 0, sizeof(long)*AUTOFS_SYMLINK_BITMAP_LEN);
  135. sbi->next_dir_ino = AUTOFS_FIRST_DIR_INO;
  136. s->s_blocksize = 1024;
  137. s->s_blocksize_bits = 10;
  138. s->s_magic = AUTOFS_SUPER_MAGIC;
  139. s->s_op = &autofs_sops;
  140. s->s_time_gran = 1;
  141. sbi->sb = s;
  142. root_inode = autofs_iget(s, AUTOFS_ROOT_INO);
  143. if (IS_ERR(root_inode))
  144. goto fail_free;
  145. root = d_alloc_root(root_inode);
  146. pipe = NULL;
  147. if (!root)
  148. goto fail_iput;
  149. /* Can this call block? - WTF cares? s is locked. */
  150. if (parse_options(data, &pipefd, &root_inode->i_uid,
  151. &root_inode->i_gid, &pgid, &minproto,
  152. &maxproto)) {
  153. printk("autofs: called with bogus options\n");
  154. goto fail_dput;
  155. }
  156. /* Couldn't this be tested earlier? */
  157. if (minproto > AUTOFS_PROTO_VERSION ||
  158. maxproto < AUTOFS_PROTO_VERSION) {
  159. printk("autofs: kernel does not match daemon version\n");
  160. goto fail_dput;
  161. }
  162. DPRINTK(("autofs: pipe fd = %d, pgrp = %u\n", pipefd, pgid));
  163. sbi->oz_pgrp = find_get_pid(pgid);
  164. if (!sbi->oz_pgrp) {
  165. printk("autofs: could not find process group %d\n", pgid);
  166. goto fail_dput;
  167. }
  168. pipe = fget(pipefd);
  169. if (!pipe) {
  170. printk("autofs: could not open pipe file descriptor\n");
  171. goto fail_put_pid;
  172. }
  173. if (!pipe->f_op || !pipe->f_op->write)
  174. goto fail_fput;
  175. sbi->pipe = pipe;
  176. sbi->catatonic = 0;
  177. /*
  178. * Success! Install the root dentry now to indicate completion.
  179. */
  180. s->s_root = root;
  181. return 0;
  182. fail_fput:
  183. printk("autofs: pipe file descriptor does not contain proper ops\n");
  184. fput(pipe);
  185. fail_put_pid:
  186. put_pid(sbi->oz_pgrp);
  187. fail_dput:
  188. dput(root);
  189. goto fail_free;
  190. fail_iput:
  191. printk("autofs: get root dentry failed\n");
  192. iput(root_inode);
  193. fail_free:
  194. kfree(sbi);
  195. s->s_fs_info = NULL;
  196. fail_unlock:
  197. return -EINVAL;
  198. }
  199. struct inode *autofs_iget(struct super_block *sb, unsigned long ino)
  200. {
  201. unsigned int n;
  202. struct autofs_sb_info *sbi = autofs_sbi(sb);
  203. struct inode *inode;
  204. inode = iget_locked(sb, ino);
  205. if (!inode)
  206. return ERR_PTR(-ENOMEM);
  207. if (!(inode->i_state & I_NEW))
  208. return inode;
  209. /* Initialize to the default case (stub directory) */
  210. inode->i_op = &simple_dir_inode_operations;
  211. inode->i_fop = &simple_dir_operations;
  212. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  213. inode->i_nlink = 2;
  214. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  215. inode->i_blocks = 0;
  216. if (ino == AUTOFS_ROOT_INO) {
  217. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
  218. inode->i_op = &autofs_root_inode_operations;
  219. inode->i_fop = &autofs_root_operations;
  220. inode->i_uid = inode->i_gid = 0; /* Changed in read_super */
  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. }