inode.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. autofs_hash_nuke(sbi);
  36. for ( n = 0 ; n < AUTOFS_MAX_SYMLINKS ; n++ ) {
  37. if ( test_bit(n, sbi->symlink_bitmap) )
  38. kfree(sbi->symlink[n].data);
  39. }
  40. kfree(sb->s_fs_info);
  41. out_kill_sb:
  42. DPRINTK(("autofs: shutting down\n"));
  43. kill_anon_super(sb);
  44. }
  45. static void autofs_read_inode(struct inode *inode);
  46. static const struct super_operations autofs_sops = {
  47. .read_inode = autofs_read_inode,
  48. .statfs = simple_statfs,
  49. };
  50. enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
  51. static 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, 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 = process_group(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. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  123. if ( !sbi )
  124. goto fail_unlock;
  125. DPRINTK(("autofs: starting up, sbi = %p\n",sbi));
  126. s->s_fs_info = sbi;
  127. sbi->magic = AUTOFS_SBI_MAGIC;
  128. sbi->pipe = NULL;
  129. sbi->catatonic = 1;
  130. sbi->exp_timeout = 0;
  131. sbi->oz_pgrp = process_group(current);
  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 = iget(s, AUTOFS_ROOT_INO);
  143. root = d_alloc_root(root_inode);
  144. pipe = NULL;
  145. if (!root)
  146. goto fail_iput;
  147. /* Can this call block? - WTF cares? s is locked. */
  148. if ( parse_options(data,&pipefd,&root_inode->i_uid,&root_inode->i_gid,&sbi->oz_pgrp,&minproto,&maxproto) ) {
  149. printk("autofs: called with bogus options\n");
  150. goto fail_dput;
  151. }
  152. /* Couldn't this be tested earlier? */
  153. if ( minproto > AUTOFS_PROTO_VERSION ||
  154. maxproto < AUTOFS_PROTO_VERSION ) {
  155. printk("autofs: kernel does not match daemon version\n");
  156. goto fail_dput;
  157. }
  158. DPRINTK(("autofs: pipe fd = %d, pgrp = %u\n", pipefd, sbi->oz_pgrp));
  159. pipe = fget(pipefd);
  160. if ( !pipe ) {
  161. printk("autofs: could not open pipe file descriptor\n");
  162. goto fail_dput;
  163. }
  164. if ( !pipe->f_op || !pipe->f_op->write )
  165. goto fail_fput;
  166. sbi->pipe = pipe;
  167. sbi->catatonic = 0;
  168. /*
  169. * Success! Install the root dentry now to indicate completion.
  170. */
  171. s->s_root = root;
  172. return 0;
  173. fail_fput:
  174. printk("autofs: pipe file descriptor does not contain proper ops\n");
  175. fput(pipe);
  176. fail_dput:
  177. dput(root);
  178. goto fail_free;
  179. fail_iput:
  180. printk("autofs: get root dentry failed\n");
  181. iput(root_inode);
  182. fail_free:
  183. kfree(sbi);
  184. s->s_fs_info = NULL;
  185. fail_unlock:
  186. return -EINVAL;
  187. }
  188. static void autofs_read_inode(struct inode *inode)
  189. {
  190. ino_t ino = inode->i_ino;
  191. unsigned int n;
  192. struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
  193. /* Initialize to the default case (stub directory) */
  194. inode->i_op = &simple_dir_inode_operations;
  195. inode->i_fop = &simple_dir_operations;
  196. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  197. inode->i_nlink = 2;
  198. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  199. inode->i_blocks = 0;
  200. if ( ino == AUTOFS_ROOT_INO ) {
  201. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
  202. inode->i_op = &autofs_root_inode_operations;
  203. inode->i_fop = &autofs_root_operations;
  204. inode->i_uid = inode->i_gid = 0; /* Changed in read_super */
  205. return;
  206. }
  207. inode->i_uid = inode->i_sb->s_root->d_inode->i_uid;
  208. inode->i_gid = inode->i_sb->s_root->d_inode->i_gid;
  209. if ( ino >= AUTOFS_FIRST_SYMLINK && ino < AUTOFS_FIRST_DIR_INO ) {
  210. /* Symlink inode - should be in symlink list */
  211. struct autofs_symlink *sl;
  212. n = ino - AUTOFS_FIRST_SYMLINK;
  213. if ( n >= AUTOFS_MAX_SYMLINKS || !test_bit(n,sbi->symlink_bitmap)) {
  214. printk("autofs: Looking for bad symlink inode %u\n", (unsigned int) ino);
  215. return;
  216. }
  217. inode->i_op = &autofs_symlink_inode_operations;
  218. sl = &sbi->symlink[n];
  219. inode->i_private = sl;
  220. inode->i_mode = S_IFLNK | S_IRWXUGO;
  221. inode->i_mtime.tv_sec = inode->i_ctime.tv_sec = sl->mtime;
  222. inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  223. inode->i_size = sl->len;
  224. inode->i_nlink = 1;
  225. }
  226. }