inode.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* -*- c -*- --------------------------------------------------------------- *
  2. *
  3. * linux/fs/autofs/inode.c
  4. *
  5. * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
  6. * Copyright 2005-2006 Ian Kent <raven@themaw.net>
  7. *
  8. * This file is part of the Linux kernel and is made available under
  9. * the terms of the GNU General Public License, version 2, or at your
  10. * option, any later version, incorporated herein by reference.
  11. *
  12. * ------------------------------------------------------------------------- */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/file.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/pagemap.h>
  18. #include <linux/parser.h>
  19. #include <linux/bitops.h>
  20. #include <linux/magic.h>
  21. #include "autofs_i.h"
  22. #include <linux/module.h>
  23. struct autofs_info *autofs4_new_ino(struct autofs_sb_info *sbi)
  24. {
  25. struct autofs_info *ino = kzalloc(sizeof(*ino), GFP_KERNEL);
  26. if (ino) {
  27. INIT_LIST_HEAD(&ino->active);
  28. INIT_LIST_HEAD(&ino->expiring);
  29. ino->last_used = jiffies;
  30. ino->sbi = sbi;
  31. }
  32. return ino;
  33. }
  34. void autofs4_clean_ino(struct autofs_info *ino)
  35. {
  36. ino->uid = 0;
  37. ino->gid = 0;
  38. ino->last_used = jiffies;
  39. }
  40. void autofs4_free_ino(struct autofs_info *ino)
  41. {
  42. kfree(ino);
  43. }
  44. void autofs4_kill_sb(struct super_block *sb)
  45. {
  46. struct autofs_sb_info *sbi = autofs4_sbi(sb);
  47. /*
  48. * In the event of a failure in get_sb_nodev the superblock
  49. * info is not present so nothing else has been setup, so
  50. * just call kill_anon_super when we are called from
  51. * deactivate_super.
  52. */
  53. if (!sbi)
  54. goto out_kill_sb;
  55. /* Free wait queues, close pipe */
  56. autofs4_catatonic_mode(sbi);
  57. sb->s_fs_info = NULL;
  58. kfree(sbi);
  59. out_kill_sb:
  60. DPRINTK("shutting down");
  61. kill_litter_super(sb);
  62. }
  63. static int autofs4_show_options(struct seq_file *m, struct dentry *root)
  64. {
  65. struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
  66. struct inode *root_inode = root->d_sb->s_root->d_inode;
  67. if (!sbi)
  68. return 0;
  69. seq_printf(m, ",fd=%d", sbi->pipefd);
  70. if (root_inode->i_uid != 0)
  71. seq_printf(m, ",uid=%u", root_inode->i_uid);
  72. if (root_inode->i_gid != 0)
  73. seq_printf(m, ",gid=%u", root_inode->i_gid);
  74. seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
  75. seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
  76. seq_printf(m, ",minproto=%d", sbi->min_proto);
  77. seq_printf(m, ",maxproto=%d", sbi->max_proto);
  78. if (autofs_type_offset(sbi->type))
  79. seq_printf(m, ",offset");
  80. else if (autofs_type_direct(sbi->type))
  81. seq_printf(m, ",direct");
  82. else
  83. seq_printf(m, ",indirect");
  84. return 0;
  85. }
  86. static void autofs4_evict_inode(struct inode *inode)
  87. {
  88. end_writeback(inode);
  89. kfree(inode->i_private);
  90. }
  91. static const struct super_operations autofs4_sops = {
  92. .statfs = simple_statfs,
  93. .show_options = autofs4_show_options,
  94. .evict_inode = autofs4_evict_inode,
  95. };
  96. enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
  97. Opt_indirect, Opt_direct, Opt_offset};
  98. static const match_table_t tokens = {
  99. {Opt_fd, "fd=%u"},
  100. {Opt_uid, "uid=%u"},
  101. {Opt_gid, "gid=%u"},
  102. {Opt_pgrp, "pgrp=%u"},
  103. {Opt_minproto, "minproto=%u"},
  104. {Opt_maxproto, "maxproto=%u"},
  105. {Opt_indirect, "indirect"},
  106. {Opt_direct, "direct"},
  107. {Opt_offset, "offset"},
  108. {Opt_err, NULL}
  109. };
  110. static int parse_options(char *options, int *pipefd, uid_t *uid, gid_t *gid,
  111. pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
  112. {
  113. char *p;
  114. substring_t args[MAX_OPT_ARGS];
  115. int option;
  116. *uid = current_uid();
  117. *gid = current_gid();
  118. *pgrp = task_pgrp_nr(current);
  119. *minproto = AUTOFS_MIN_PROTO_VERSION;
  120. *maxproto = AUTOFS_MAX_PROTO_VERSION;
  121. *pipefd = -1;
  122. if (!options)
  123. return 1;
  124. while ((p = strsep(&options, ",")) != NULL) {
  125. int token;
  126. if (!*p)
  127. continue;
  128. token = match_token(p, tokens, args);
  129. switch (token) {
  130. case Opt_fd:
  131. if (match_int(args, pipefd))
  132. return 1;
  133. break;
  134. case Opt_uid:
  135. if (match_int(args, &option))
  136. return 1;
  137. *uid = option;
  138. break;
  139. case Opt_gid:
  140. if (match_int(args, &option))
  141. return 1;
  142. *gid = option;
  143. break;
  144. case Opt_pgrp:
  145. if (match_int(args, &option))
  146. return 1;
  147. *pgrp = option;
  148. break;
  149. case Opt_minproto:
  150. if (match_int(args, &option))
  151. return 1;
  152. *minproto = option;
  153. break;
  154. case Opt_maxproto:
  155. if (match_int(args, &option))
  156. return 1;
  157. *maxproto = option;
  158. break;
  159. case Opt_indirect:
  160. set_autofs_type_indirect(type);
  161. break;
  162. case Opt_direct:
  163. set_autofs_type_direct(type);
  164. break;
  165. case Opt_offset:
  166. set_autofs_type_offset(type);
  167. break;
  168. default:
  169. return 1;
  170. }
  171. }
  172. return (*pipefd < 0);
  173. }
  174. int autofs4_fill_super(struct super_block *s, void *data, int silent)
  175. {
  176. struct inode * root_inode;
  177. struct dentry * root;
  178. struct file * pipe;
  179. int pipefd;
  180. struct autofs_sb_info *sbi;
  181. struct autofs_info *ino;
  182. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  183. if (!sbi)
  184. goto fail_unlock;
  185. DPRINTK("starting up, sbi = %p",sbi);
  186. s->s_fs_info = sbi;
  187. sbi->magic = AUTOFS_SBI_MAGIC;
  188. sbi->pipefd = -1;
  189. sbi->pipe = NULL;
  190. sbi->catatonic = 1;
  191. sbi->exp_timeout = 0;
  192. sbi->oz_pgrp = task_pgrp_nr(current);
  193. sbi->sb = s;
  194. sbi->version = 0;
  195. sbi->sub_version = 0;
  196. set_autofs_type_indirect(&sbi->type);
  197. sbi->min_proto = 0;
  198. sbi->max_proto = 0;
  199. mutex_init(&sbi->wq_mutex);
  200. mutex_init(&sbi->pipe_mutex);
  201. spin_lock_init(&sbi->fs_lock);
  202. sbi->queues = NULL;
  203. spin_lock_init(&sbi->lookup_lock);
  204. INIT_LIST_HEAD(&sbi->active_list);
  205. INIT_LIST_HEAD(&sbi->expiring_list);
  206. s->s_blocksize = 1024;
  207. s->s_blocksize_bits = 10;
  208. s->s_magic = AUTOFS_SUPER_MAGIC;
  209. s->s_op = &autofs4_sops;
  210. s->s_d_op = &autofs4_dentry_operations;
  211. s->s_time_gran = 1;
  212. /*
  213. * Get the root inode and dentry, but defer checking for errors.
  214. */
  215. ino = autofs4_new_ino(sbi);
  216. if (!ino)
  217. goto fail_free;
  218. root_inode = autofs4_get_inode(s, S_IFDIR | 0755);
  219. root = d_make_root(root_inode);
  220. if (!root)
  221. goto fail_ino;
  222. pipe = NULL;
  223. root->d_fsdata = ino;
  224. /* Can this call block? */
  225. if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
  226. &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
  227. &sbi->max_proto)) {
  228. printk("autofs: called with bogus options\n");
  229. goto fail_dput;
  230. }
  231. if (autofs_type_trigger(sbi->type))
  232. __managed_dentry_set_managed(root);
  233. root_inode->i_fop = &autofs4_root_operations;
  234. root_inode->i_op = &autofs4_dir_inode_operations;
  235. /* Couldn't this be tested earlier? */
  236. if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
  237. sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
  238. printk("autofs: kernel does not match daemon version "
  239. "daemon (%d, %d) kernel (%d, %d)\n",
  240. sbi->min_proto, sbi->max_proto,
  241. AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
  242. goto fail_dput;
  243. }
  244. /* Establish highest kernel protocol version */
  245. if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
  246. sbi->version = AUTOFS_MAX_PROTO_VERSION;
  247. else
  248. sbi->version = sbi->max_proto;
  249. sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
  250. DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
  251. pipe = fget(pipefd);
  252. if (!pipe) {
  253. printk("autofs: could not open pipe file descriptor\n");
  254. goto fail_dput;
  255. }
  256. if (autofs_prepare_pipe(pipe) < 0)
  257. goto fail_fput;
  258. sbi->pipe = pipe;
  259. sbi->pipefd = pipefd;
  260. sbi->catatonic = 0;
  261. /*
  262. * Success! Install the root dentry now to indicate completion.
  263. */
  264. s->s_root = root;
  265. return 0;
  266. /*
  267. * Failure ... clean up.
  268. */
  269. fail_fput:
  270. printk("autofs: pipe file descriptor does not contain proper ops\n");
  271. fput(pipe);
  272. /* fall through */
  273. fail_dput:
  274. dput(root);
  275. goto fail_free;
  276. fail_ino:
  277. kfree(ino);
  278. fail_free:
  279. kfree(sbi);
  280. s->s_fs_info = NULL;
  281. fail_unlock:
  282. return -EINVAL;
  283. }
  284. struct inode *autofs4_get_inode(struct super_block *sb, umode_t mode)
  285. {
  286. struct inode *inode = new_inode(sb);
  287. if (inode == NULL)
  288. return NULL;
  289. inode->i_mode = mode;
  290. if (sb->s_root) {
  291. inode->i_uid = sb->s_root->d_inode->i_uid;
  292. inode->i_gid = sb->s_root->d_inode->i_gid;
  293. }
  294. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  295. inode->i_ino = get_next_ino();
  296. if (S_ISDIR(mode)) {
  297. set_nlink(inode, 2);
  298. inode->i_op = &autofs4_dir_inode_operations;
  299. inode->i_fop = &autofs4_dir_operations;
  300. } else if (S_ISLNK(mode)) {
  301. inode->i_op = &autofs4_symlink_inode_operations;
  302. }
  303. return inode;
  304. }