inode.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 void autofs_read_inode(struct inode *inode);
  47. static const struct super_operations autofs_sops = {
  48. .read_inode = autofs_read_inode,
  49. .statfs = simple_statfs,
  50. };
  51. enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
  52. static match_table_t autofs_tokens = {
  53. {Opt_fd, "fd=%u"},
  54. {Opt_uid, "uid=%u"},
  55. {Opt_gid, "gid=%u"},
  56. {Opt_pgrp, "pgrp=%u"},
  57. {Opt_minproto, "minproto=%u"},
  58. {Opt_maxproto, "maxproto=%u"},
  59. {Opt_err, NULL}
  60. };
  61. static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
  62. pid_t *pgrp, int *minproto, int *maxproto)
  63. {
  64. char *p;
  65. substring_t args[MAX_OPT_ARGS];
  66. int option;
  67. *uid = current->uid;
  68. *gid = current->gid;
  69. *pgrp = task_pgrp_nr(current);
  70. *minproto = *maxproto = AUTOFS_PROTO_VERSION;
  71. *pipefd = -1;
  72. if (!options)
  73. return 1;
  74. while ((p = strsep(&options, ",")) != NULL) {
  75. int token;
  76. if (!*p)
  77. continue;
  78. token = match_token(p, autofs_tokens, args);
  79. switch (token) {
  80. case Opt_fd:
  81. if (match_int(&args[0], &option))
  82. return 1;
  83. *pipefd = option;
  84. break;
  85. case Opt_uid:
  86. if (match_int(&args[0], &option))
  87. return 1;
  88. *uid = option;
  89. break;
  90. case Opt_gid:
  91. if (match_int(&args[0], &option))
  92. return 1;
  93. *gid = option;
  94. break;
  95. case Opt_pgrp:
  96. if (match_int(&args[0], &option))
  97. return 1;
  98. *pgrp = option;
  99. break;
  100. case Opt_minproto:
  101. if (match_int(&args[0], &option))
  102. return 1;
  103. *minproto = option;
  104. break;
  105. case Opt_maxproto:
  106. if (match_int(&args[0], &option))
  107. return 1;
  108. *maxproto = option;
  109. break;
  110. default:
  111. return 1;
  112. }
  113. }
  114. return (*pipefd < 0);
  115. }
  116. int autofs_fill_super(struct super_block *s, void *data, int silent)
  117. {
  118. struct inode * root_inode;
  119. struct dentry * root;
  120. struct file * pipe;
  121. int pipefd;
  122. struct autofs_sb_info *sbi;
  123. int minproto, maxproto;
  124. pid_t pgid;
  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 = iget(s, AUTOFS_ROOT_INO);
  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. static void autofs_read_inode(struct inode *inode)
  200. {
  201. ino_t ino = inode->i_ino;
  202. unsigned int n;
  203. struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
  204. /* Initialize to the default case (stub directory) */
  205. inode->i_op = &simple_dir_inode_operations;
  206. inode->i_fop = &simple_dir_operations;
  207. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  208. inode->i_nlink = 2;
  209. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  210. inode->i_blocks = 0;
  211. if (ino == AUTOFS_ROOT_INO) {
  212. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
  213. inode->i_op = &autofs_root_inode_operations;
  214. inode->i_fop = &autofs_root_operations;
  215. inode->i_uid = inode->i_gid = 0; /* Changed in read_super */
  216. return;
  217. }
  218. inode->i_uid = inode->i_sb->s_root->d_inode->i_uid;
  219. inode->i_gid = inode->i_sb->s_root->d_inode->i_gid;
  220. if (ino >= AUTOFS_FIRST_SYMLINK && ino < AUTOFS_FIRST_DIR_INO) {
  221. /* Symlink inode - should be in symlink list */
  222. struct autofs_symlink *sl;
  223. n = ino - AUTOFS_FIRST_SYMLINK;
  224. if (n >= AUTOFS_MAX_SYMLINKS || !test_bit(n,sbi->symlink_bitmap)) {
  225. printk("autofs: Looking for bad symlink inode %u\n", (unsigned int) ino);
  226. return;
  227. }
  228. inode->i_op = &autofs_symlink_inode_operations;
  229. sl = &sbi->symlink[n];
  230. inode->i_private = sl;
  231. inode->i_mode = S_IFLNK | S_IRWXUGO;
  232. inode->i_mtime.tv_sec = inode->i_ctime.tv_sec = sl->mtime;
  233. inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  234. inode->i_size = sl->len;
  235. inode->i_nlink = 1;
  236. }
  237. }