inode.c 8.5 KB

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