inode.c 8.4 KB

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