inode.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /* -*- 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/slab.h>
  14. #include <linux/file.h>
  15. #include <linux/pagemap.h>
  16. #include <linux/parser.h>
  17. #include <linux/bitops.h>
  18. #include "autofs_i.h"
  19. #include <linux/module.h>
  20. static void ino_lnkfree(struct autofs_info *ino)
  21. {
  22. if (ino->u.symlink) {
  23. kfree(ino->u.symlink);
  24. ino->u.symlink = NULL;
  25. }
  26. }
  27. struct autofs_info *autofs4_init_ino(struct autofs_info *ino,
  28. struct autofs_sb_info *sbi, mode_t mode)
  29. {
  30. int reinit = 1;
  31. if (ino == NULL) {
  32. reinit = 0;
  33. ino = kmalloc(sizeof(*ino), GFP_KERNEL);
  34. }
  35. if (ino == NULL)
  36. return NULL;
  37. ino->flags = 0;
  38. ino->mode = mode;
  39. ino->inode = NULL;
  40. ino->dentry = NULL;
  41. ino->size = 0;
  42. ino->last_used = jiffies;
  43. ino->sbi = sbi;
  44. if (reinit && ino->free)
  45. (ino->free)(ino);
  46. memset(&ino->u, 0, sizeof(ino->u));
  47. ino->free = NULL;
  48. if (S_ISLNK(mode))
  49. ino->free = ino_lnkfree;
  50. return ino;
  51. }
  52. void autofs4_free_ino(struct autofs_info *ino)
  53. {
  54. if (ino->dentry) {
  55. ino->dentry->d_fsdata = NULL;
  56. if (ino->dentry->d_inode)
  57. dput(ino->dentry);
  58. ino->dentry = NULL;
  59. }
  60. if (ino->free)
  61. (ino->free)(ino);
  62. kfree(ino);
  63. }
  64. static void autofs4_put_super(struct super_block *sb)
  65. {
  66. struct autofs_sb_info *sbi = autofs4_sbi(sb);
  67. sb->s_fs_info = NULL;
  68. if ( !sbi->catatonic )
  69. autofs4_catatonic_mode(sbi); /* Free wait queues, close pipe */
  70. kfree(sbi);
  71. DPRINTK("shutting down");
  72. }
  73. static struct super_operations autofs4_sops = {
  74. .put_super = autofs4_put_super,
  75. .statfs = simple_statfs,
  76. };
  77. enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto};
  78. static match_table_t tokens = {
  79. {Opt_fd, "fd=%u"},
  80. {Opt_uid, "uid=%u"},
  81. {Opt_gid, "gid=%u"},
  82. {Opt_pgrp, "pgrp=%u"},
  83. {Opt_minproto, "minproto=%u"},
  84. {Opt_maxproto, "maxproto=%u"},
  85. {Opt_err, NULL}
  86. };
  87. static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
  88. pid_t *pgrp, int *minproto, int *maxproto)
  89. {
  90. char *p;
  91. substring_t args[MAX_OPT_ARGS];
  92. int option;
  93. *uid = current->uid;
  94. *gid = current->gid;
  95. *pgrp = process_group(current);
  96. *minproto = AUTOFS_MIN_PROTO_VERSION;
  97. *maxproto = AUTOFS_MAX_PROTO_VERSION;
  98. *pipefd = -1;
  99. if (!options)
  100. return 1;
  101. while ((p = strsep(&options, ",")) != NULL) {
  102. int token;
  103. if (!*p)
  104. continue;
  105. token = match_token(p, tokens, args);
  106. switch (token) {
  107. case Opt_fd:
  108. if (match_int(args, pipefd))
  109. return 1;
  110. break;
  111. case Opt_uid:
  112. if (match_int(args, &option))
  113. return 1;
  114. *uid = option;
  115. break;
  116. case Opt_gid:
  117. if (match_int(args, &option))
  118. return 1;
  119. *gid = option;
  120. break;
  121. case Opt_pgrp:
  122. if (match_int(args, &option))
  123. return 1;
  124. *pgrp = option;
  125. break;
  126. case Opt_minproto:
  127. if (match_int(args, &option))
  128. return 1;
  129. *minproto = option;
  130. break;
  131. case Opt_maxproto:
  132. if (match_int(args, &option))
  133. return 1;
  134. *maxproto = option;
  135. break;
  136. default:
  137. return 1;
  138. }
  139. }
  140. return (*pipefd < 0);
  141. }
  142. static struct autofs_info *autofs4_mkroot(struct autofs_sb_info *sbi)
  143. {
  144. struct autofs_info *ino;
  145. ino = autofs4_init_ino(NULL, sbi, S_IFDIR | 0755);
  146. if (!ino)
  147. return NULL;
  148. return ino;
  149. }
  150. int autofs4_fill_super(struct super_block *s, void *data, int silent)
  151. {
  152. struct inode * root_inode;
  153. struct dentry * root;
  154. struct file * pipe;
  155. int pipefd;
  156. struct autofs_sb_info *sbi;
  157. struct autofs_info *ino;
  158. int minproto, maxproto;
  159. sbi = (struct autofs_sb_info *) kmalloc(sizeof(*sbi), GFP_KERNEL);
  160. if ( !sbi )
  161. goto fail_unlock;
  162. DPRINTK("starting up, sbi = %p",sbi);
  163. memset(sbi, 0, sizeof(*sbi));
  164. s->s_fs_info = sbi;
  165. sbi->magic = AUTOFS_SBI_MAGIC;
  166. sbi->catatonic = 0;
  167. sbi->exp_timeout = 0;
  168. sbi->oz_pgrp = process_group(current);
  169. sbi->sb = s;
  170. sbi->version = 0;
  171. sbi->sub_version = 0;
  172. init_MUTEX(&sbi->wq_sem);
  173. spin_lock_init(&sbi->fs_lock);
  174. sbi->queues = NULL;
  175. s->s_blocksize = 1024;
  176. s->s_blocksize_bits = 10;
  177. s->s_magic = AUTOFS_SUPER_MAGIC;
  178. s->s_op = &autofs4_sops;
  179. s->s_time_gran = 1;
  180. /*
  181. * Get the root inode and dentry, but defer checking for errors.
  182. */
  183. ino = autofs4_mkroot(sbi);
  184. if (!ino)
  185. goto fail_free;
  186. root_inode = autofs4_get_inode(s, ino);
  187. kfree(ino);
  188. if (!root_inode)
  189. goto fail_free;
  190. root_inode->i_op = &autofs4_root_inode_operations;
  191. root_inode->i_fop = &autofs4_root_operations;
  192. root = d_alloc_root(root_inode);
  193. pipe = NULL;
  194. if (!root)
  195. goto fail_iput;
  196. /* Can this call block? */
  197. if (parse_options(data, &pipefd,
  198. &root_inode->i_uid, &root_inode->i_gid,
  199. &sbi->oz_pgrp,
  200. &minproto, &maxproto)) {
  201. printk("autofs: called with bogus options\n");
  202. goto fail_dput;
  203. }
  204. /* Couldn't this be tested earlier? */
  205. if (maxproto < AUTOFS_MIN_PROTO_VERSION ||
  206. minproto > AUTOFS_MAX_PROTO_VERSION) {
  207. printk("autofs: kernel does not match daemon version "
  208. "daemon (%d, %d) kernel (%d, %d)\n",
  209. minproto, maxproto,
  210. AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
  211. goto fail_dput;
  212. }
  213. sbi->version = maxproto > AUTOFS_MAX_PROTO_VERSION ? AUTOFS_MAX_PROTO_VERSION : maxproto;
  214. sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
  215. DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
  216. pipe = fget(pipefd);
  217. if ( !pipe ) {
  218. printk("autofs: could not open pipe file descriptor\n");
  219. goto fail_dput;
  220. }
  221. if ( !pipe->f_op || !pipe->f_op->write )
  222. goto fail_fput;
  223. sbi->pipe = pipe;
  224. /*
  225. * Success! Install the root dentry now to indicate completion.
  226. */
  227. s->s_root = root;
  228. return 0;
  229. /*
  230. * Failure ... clean up.
  231. */
  232. fail_fput:
  233. printk("autofs: pipe file descriptor does not contain proper ops\n");
  234. fput(pipe);
  235. /* fall through */
  236. fail_dput:
  237. dput(root);
  238. goto fail_free;
  239. fail_iput:
  240. printk("autofs: get root dentry failed\n");
  241. iput(root_inode);
  242. fail_free:
  243. kfree(sbi);
  244. fail_unlock:
  245. return -EINVAL;
  246. }
  247. struct inode *autofs4_get_inode(struct super_block *sb,
  248. struct autofs_info *inf)
  249. {
  250. struct inode *inode = new_inode(sb);
  251. if (inode == NULL)
  252. return NULL;
  253. inf->inode = inode;
  254. inode->i_mode = inf->mode;
  255. if (sb->s_root) {
  256. inode->i_uid = sb->s_root->d_inode->i_uid;
  257. inode->i_gid = sb->s_root->d_inode->i_gid;
  258. } else {
  259. inode->i_uid = 0;
  260. inode->i_gid = 0;
  261. }
  262. inode->i_blksize = PAGE_CACHE_SIZE;
  263. inode->i_blocks = 0;
  264. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  265. if (S_ISDIR(inf->mode)) {
  266. inode->i_nlink = 2;
  267. inode->i_op = &autofs4_dir_inode_operations;
  268. inode->i_fop = &autofs4_dir_operations;
  269. } else if (S_ISLNK(inf->mode)) {
  270. inode->i_size = inf->size;
  271. inode->i_op = &autofs4_symlink_inode_operations;
  272. }
  273. return inode;
  274. }