inode.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 "autofs_i.h"
  19. #include <linux/module.h>
  20. static void autofs_put_super(struct super_block *sb)
  21. {
  22. struct autofs_sb_info *sbi = autofs_sbi(sb);
  23. unsigned int n;
  24. if ( !sbi->catatonic )
  25. autofs_catatonic_mode(sbi); /* Free wait queues, close pipe */
  26. autofs_hash_nuke(sbi);
  27. for ( n = 0 ; n < AUTOFS_MAX_SYMLINKS ; n++ ) {
  28. if ( test_bit(n, sbi->symlink_bitmap) )
  29. kfree(sbi->symlink[n].data);
  30. }
  31. kfree(sb->s_fs_info);
  32. DPRINTK(("autofs: shutting down\n"));
  33. }
  34. static void autofs_read_inode(struct inode *inode);
  35. static struct super_operations autofs_sops = {
  36. .read_inode = autofs_read_inode,
  37. .put_super = autofs_put_super,
  38. .statfs = simple_statfs,
  39. };
  40. enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
  41. static match_table_t autofs_tokens = {
  42. {Opt_fd, "fd=%u"},
  43. {Opt_uid, "uid=%u"},
  44. {Opt_gid, "gid=%u"},
  45. {Opt_pgrp, "pgrp=%u"},
  46. {Opt_minproto, "minproto=%u"},
  47. {Opt_maxproto, "maxproto=%u"},
  48. {Opt_err, NULL}
  49. };
  50. static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid, pid_t *pgrp, int *minproto, int *maxproto)
  51. {
  52. char *p;
  53. substring_t args[MAX_OPT_ARGS];
  54. int option;
  55. *uid = current->uid;
  56. *gid = current->gid;
  57. *pgrp = process_group(current);
  58. *minproto = *maxproto = AUTOFS_PROTO_VERSION;
  59. *pipefd = -1;
  60. if (!options)
  61. return 1;
  62. while ((p = strsep(&options, ",")) != NULL) {
  63. int token;
  64. if (!*p)
  65. continue;
  66. token = match_token(p, autofs_tokens, args);
  67. switch (token) {
  68. case Opt_fd:
  69. if (match_int(&args[0], &option))
  70. return 1;
  71. *pipefd = option;
  72. break;
  73. case Opt_uid:
  74. if (match_int(&args[0], &option))
  75. return 1;
  76. *uid = option;
  77. break;
  78. case Opt_gid:
  79. if (match_int(&args[0], &option))
  80. return 1;
  81. *gid = option;
  82. break;
  83. case Opt_pgrp:
  84. if (match_int(&args[0], &option))
  85. return 1;
  86. *pgrp = option;
  87. break;
  88. case Opt_minproto:
  89. if (match_int(&args[0], &option))
  90. return 1;
  91. *minproto = option;
  92. break;
  93. case Opt_maxproto:
  94. if (match_int(&args[0], &option))
  95. return 1;
  96. *maxproto = option;
  97. break;
  98. default:
  99. return 1;
  100. }
  101. }
  102. return (*pipefd < 0);
  103. }
  104. int autofs_fill_super(struct super_block *s, void *data, int silent)
  105. {
  106. struct inode * root_inode;
  107. struct dentry * root;
  108. struct file * pipe;
  109. int pipefd;
  110. struct autofs_sb_info *sbi;
  111. int minproto, maxproto;
  112. sbi = kmalloc(sizeof(*sbi), GFP_KERNEL);
  113. if ( !sbi )
  114. goto fail_unlock;
  115. memset(sbi, 0, sizeof(*sbi));
  116. DPRINTK(("autofs: starting up, sbi = %p\n",sbi));
  117. s->s_fs_info = sbi;
  118. sbi->magic = AUTOFS_SBI_MAGIC;
  119. sbi->catatonic = 0;
  120. sbi->exp_timeout = 0;
  121. sbi->oz_pgrp = process_group(current);
  122. autofs_initialize_hash(&sbi->dirhash);
  123. sbi->queues = NULL;
  124. memset(sbi->symlink_bitmap, 0, sizeof(long)*AUTOFS_SYMLINK_BITMAP_LEN);
  125. sbi->next_dir_ino = AUTOFS_FIRST_DIR_INO;
  126. s->s_blocksize = 1024;
  127. s->s_blocksize_bits = 10;
  128. s->s_magic = AUTOFS_SUPER_MAGIC;
  129. s->s_op = &autofs_sops;
  130. s->s_time_gran = 1;
  131. sbi->sb = s;
  132. root_inode = iget(s, AUTOFS_ROOT_INO);
  133. root = d_alloc_root(root_inode);
  134. pipe = NULL;
  135. if (!root)
  136. goto fail_iput;
  137. /* Can this call block? - WTF cares? s is locked. */
  138. if ( parse_options(data,&pipefd,&root_inode->i_uid,&root_inode->i_gid,&sbi->oz_pgrp,&minproto,&maxproto) ) {
  139. printk("autofs: called with bogus options\n");
  140. goto fail_dput;
  141. }
  142. /* Couldn't this be tested earlier? */
  143. if ( minproto > AUTOFS_PROTO_VERSION ||
  144. maxproto < AUTOFS_PROTO_VERSION ) {
  145. printk("autofs: kernel does not match daemon version\n");
  146. goto fail_dput;
  147. }
  148. DPRINTK(("autofs: pipe fd = %d, pgrp = %u\n", pipefd, sbi->oz_pgrp));
  149. pipe = fget(pipefd);
  150. if ( !pipe ) {
  151. printk("autofs: could not open pipe file descriptor\n");
  152. goto fail_dput;
  153. }
  154. if ( !pipe->f_op || !pipe->f_op->write )
  155. goto fail_fput;
  156. sbi->pipe = pipe;
  157. /*
  158. * Success! Install the root dentry now to indicate completion.
  159. */
  160. s->s_root = root;
  161. return 0;
  162. fail_fput:
  163. printk("autofs: pipe file descriptor does not contain proper ops\n");
  164. fput(pipe);
  165. fail_dput:
  166. dput(root);
  167. goto fail_free;
  168. fail_iput:
  169. printk("autofs: get root dentry failed\n");
  170. iput(root_inode);
  171. fail_free:
  172. kfree(sbi);
  173. fail_unlock:
  174. return -EINVAL;
  175. }
  176. static void autofs_read_inode(struct inode *inode)
  177. {
  178. ino_t ino = inode->i_ino;
  179. unsigned int n;
  180. struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
  181. /* Initialize to the default case (stub directory) */
  182. inode->i_op = &simple_dir_inode_operations;
  183. inode->i_fop = &simple_dir_operations;
  184. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  185. inode->i_nlink = 2;
  186. inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
  187. inode->i_blocks = 0;
  188. inode->i_blksize = 1024;
  189. if ( ino == AUTOFS_ROOT_INO ) {
  190. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO | S_IWUSR;
  191. inode->i_op = &autofs_root_inode_operations;
  192. inode->i_fop = &autofs_root_operations;
  193. inode->i_uid = inode->i_gid = 0; /* Changed in read_super */
  194. return;
  195. }
  196. inode->i_uid = inode->i_sb->s_root->d_inode->i_uid;
  197. inode->i_gid = inode->i_sb->s_root->d_inode->i_gid;
  198. if ( ino >= AUTOFS_FIRST_SYMLINK && ino < AUTOFS_FIRST_DIR_INO ) {
  199. /* Symlink inode - should be in symlink list */
  200. struct autofs_symlink *sl;
  201. n = ino - AUTOFS_FIRST_SYMLINK;
  202. if ( n >= AUTOFS_MAX_SYMLINKS || !test_bit(n,sbi->symlink_bitmap)) {
  203. printk("autofs: Looking for bad symlink inode %u\n", (unsigned int) ino);
  204. return;
  205. }
  206. inode->i_op = &autofs_symlink_inode_operations;
  207. sl = &sbi->symlink[n];
  208. inode->u.generic_ip = sl;
  209. inode->i_mode = S_IFLNK | S_IRWXUGO;
  210. inode->i_mtime.tv_sec = inode->i_ctime.tv_sec = sl->mtime;
  211. inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec = 0;
  212. inode->i_size = sl->len;
  213. inode->i_nlink = 1;
  214. }
  215. }