inode.c 8.5 KB

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