inode.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 = GLOBAL_ROOT_UID;
  37. ino->gid = GLOBAL_ROOT_GID;
  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 (!uid_eq(root_inode->i_uid, GLOBAL_ROOT_UID))
  71. seq_printf(m, ",uid=%u",
  72. from_kuid_munged(&init_user_ns, root_inode->i_uid));
  73. if (!gid_eq(root_inode->i_gid, GLOBAL_ROOT_GID))
  74. seq_printf(m, ",gid=%u",
  75. from_kgid_munged(&init_user_ns, root_inode->i_gid));
  76. seq_printf(m, ",pgrp=%d", sbi->oz_pgrp);
  77. seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
  78. seq_printf(m, ",minproto=%d", sbi->min_proto);
  79. seq_printf(m, ",maxproto=%d", sbi->max_proto);
  80. if (autofs_type_offset(sbi->type))
  81. seq_printf(m, ",offset");
  82. else if (autofs_type_direct(sbi->type))
  83. seq_printf(m, ",direct");
  84. else
  85. seq_printf(m, ",indirect");
  86. return 0;
  87. }
  88. static void autofs4_evict_inode(struct inode *inode)
  89. {
  90. clear_inode(inode);
  91. kfree(inode->i_private);
  92. }
  93. static const struct super_operations autofs4_sops = {
  94. .statfs = simple_statfs,
  95. .show_options = autofs4_show_options,
  96. .evict_inode = autofs4_evict_inode,
  97. };
  98. enum {Opt_err, Opt_fd, Opt_uid, Opt_gid, Opt_pgrp, Opt_minproto, Opt_maxproto,
  99. Opt_indirect, Opt_direct, Opt_offset};
  100. static const match_table_t tokens = {
  101. {Opt_fd, "fd=%u"},
  102. {Opt_uid, "uid=%u"},
  103. {Opt_gid, "gid=%u"},
  104. {Opt_pgrp, "pgrp=%u"},
  105. {Opt_minproto, "minproto=%u"},
  106. {Opt_maxproto, "maxproto=%u"},
  107. {Opt_indirect, "indirect"},
  108. {Opt_direct, "direct"},
  109. {Opt_offset, "offset"},
  110. {Opt_err, NULL}
  111. };
  112. static int parse_options(char *options, int *pipefd, kuid_t *uid, kgid_t *gid,
  113. pid_t *pgrp, unsigned int *type, int *minproto, int *maxproto)
  114. {
  115. char *p;
  116. substring_t args[MAX_OPT_ARGS];
  117. int option;
  118. *uid = current_uid();
  119. *gid = current_gid();
  120. *pgrp = task_pgrp_nr(current);
  121. *minproto = AUTOFS_MIN_PROTO_VERSION;
  122. *maxproto = AUTOFS_MAX_PROTO_VERSION;
  123. *pipefd = -1;
  124. if (!options)
  125. return 1;
  126. while ((p = strsep(&options, ",")) != NULL) {
  127. int token;
  128. if (!*p)
  129. continue;
  130. token = match_token(p, tokens, args);
  131. switch (token) {
  132. case Opt_fd:
  133. if (match_int(args, pipefd))
  134. return 1;
  135. break;
  136. case Opt_uid:
  137. if (match_int(args, &option))
  138. return 1;
  139. *uid = make_kuid(current_user_ns(), option);
  140. if (!uid_valid(*uid))
  141. return 1;
  142. break;
  143. case Opt_gid:
  144. if (match_int(args, &option))
  145. return 1;
  146. *gid = make_kgid(current_user_ns(), option);
  147. if (!gid_valid(*gid))
  148. return 1;
  149. break;
  150. case Opt_pgrp:
  151. if (match_int(args, &option))
  152. return 1;
  153. *pgrp = option;
  154. break;
  155. case Opt_minproto:
  156. if (match_int(args, &option))
  157. return 1;
  158. *minproto = option;
  159. break;
  160. case Opt_maxproto:
  161. if (match_int(args, &option))
  162. return 1;
  163. *maxproto = option;
  164. break;
  165. case Opt_indirect:
  166. set_autofs_type_indirect(type);
  167. break;
  168. case Opt_direct:
  169. set_autofs_type_direct(type);
  170. break;
  171. case Opt_offset:
  172. set_autofs_type_offset(type);
  173. break;
  174. default:
  175. return 1;
  176. }
  177. }
  178. return (*pipefd < 0);
  179. }
  180. int autofs4_fill_super(struct super_block *s, void *data, int silent)
  181. {
  182. struct inode * root_inode;
  183. struct dentry * root;
  184. struct file * pipe;
  185. int pipefd;
  186. struct autofs_sb_info *sbi;
  187. struct autofs_info *ino;
  188. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  189. if (!sbi)
  190. goto fail_unlock;
  191. DPRINTK("starting up, sbi = %p",sbi);
  192. s->s_fs_info = sbi;
  193. sbi->magic = AUTOFS_SBI_MAGIC;
  194. sbi->pipefd = -1;
  195. sbi->pipe = NULL;
  196. sbi->catatonic = 1;
  197. sbi->exp_timeout = 0;
  198. sbi->oz_pgrp = task_pgrp_nr(current);
  199. sbi->sb = s;
  200. sbi->version = 0;
  201. sbi->sub_version = 0;
  202. set_autofs_type_indirect(&sbi->type);
  203. sbi->min_proto = 0;
  204. sbi->max_proto = 0;
  205. mutex_init(&sbi->wq_mutex);
  206. mutex_init(&sbi->pipe_mutex);
  207. spin_lock_init(&sbi->fs_lock);
  208. sbi->queues = NULL;
  209. spin_lock_init(&sbi->lookup_lock);
  210. INIT_LIST_HEAD(&sbi->active_list);
  211. INIT_LIST_HEAD(&sbi->expiring_list);
  212. s->s_blocksize = 1024;
  213. s->s_blocksize_bits = 10;
  214. s->s_magic = AUTOFS_SUPER_MAGIC;
  215. s->s_op = &autofs4_sops;
  216. s->s_d_op = &autofs4_dentry_operations;
  217. s->s_time_gran = 1;
  218. /*
  219. * Get the root inode and dentry, but defer checking for errors.
  220. */
  221. ino = autofs4_new_ino(sbi);
  222. if (!ino)
  223. goto fail_free;
  224. root_inode = autofs4_get_inode(s, S_IFDIR | 0755);
  225. root = d_make_root(root_inode);
  226. if (!root)
  227. goto fail_ino;
  228. pipe = NULL;
  229. root->d_fsdata = ino;
  230. /* Can this call block? */
  231. if (parse_options(data, &pipefd, &root_inode->i_uid, &root_inode->i_gid,
  232. &sbi->oz_pgrp, &sbi->type, &sbi->min_proto,
  233. &sbi->max_proto)) {
  234. printk("autofs: called with bogus options\n");
  235. goto fail_dput;
  236. }
  237. if (autofs_type_trigger(sbi->type))
  238. __managed_dentry_set_managed(root);
  239. root_inode->i_fop = &autofs4_root_operations;
  240. root_inode->i_op = &autofs4_dir_inode_operations;
  241. /* Couldn't this be tested earlier? */
  242. if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
  243. sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
  244. printk("autofs: kernel does not match daemon version "
  245. "daemon (%d, %d) kernel (%d, %d)\n",
  246. sbi->min_proto, sbi->max_proto,
  247. AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
  248. goto fail_dput;
  249. }
  250. /* Establish highest kernel protocol version */
  251. if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
  252. sbi->version = AUTOFS_MAX_PROTO_VERSION;
  253. else
  254. sbi->version = sbi->max_proto;
  255. sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
  256. DPRINTK("pipe fd = %d, pgrp = %u", pipefd, sbi->oz_pgrp);
  257. pipe = fget(pipefd);
  258. if (!pipe) {
  259. printk("autofs: could not open pipe file descriptor\n");
  260. goto fail_dput;
  261. }
  262. if (autofs_prepare_pipe(pipe) < 0)
  263. goto fail_fput;
  264. sbi->pipe = pipe;
  265. sbi->pipefd = pipefd;
  266. sbi->catatonic = 0;
  267. /*
  268. * Success! Install the root dentry now to indicate completion.
  269. */
  270. s->s_root = root;
  271. return 0;
  272. /*
  273. * Failure ... clean up.
  274. */
  275. fail_fput:
  276. printk("autofs: pipe file descriptor does not contain proper ops\n");
  277. fput(pipe);
  278. /* fall through */
  279. fail_dput:
  280. dput(root);
  281. goto fail_free;
  282. fail_ino:
  283. kfree(ino);
  284. fail_free:
  285. kfree(sbi);
  286. s->s_fs_info = NULL;
  287. fail_unlock:
  288. return -EINVAL;
  289. }
  290. struct inode *autofs4_get_inode(struct super_block *sb, umode_t mode)
  291. {
  292. struct inode *inode = new_inode(sb);
  293. if (inode == NULL)
  294. return NULL;
  295. inode->i_mode = mode;
  296. if (sb->s_root) {
  297. inode->i_uid = sb->s_root->d_inode->i_uid;
  298. inode->i_gid = sb->s_root->d_inode->i_gid;
  299. }
  300. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  301. inode->i_ino = get_next_ino();
  302. if (S_ISDIR(mode)) {
  303. set_nlink(inode, 2);
  304. inode->i_op = &autofs4_dir_inode_operations;
  305. inode->i_fop = &autofs4_dir_operations;
  306. } else if (S_ISLNK(mode)) {
  307. inode->i_op = &autofs4_symlink_inode_operations;
  308. }
  309. return inode;
  310. }