inode.c 8.4 KB

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