inode.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
  4. * Copyright 2005-2006 Ian Kent <raven@themaw.net>
  5. */
  6. #include <linux/seq_file.h>
  7. #include <linux/pagemap.h>
  8. #include "autofs_i.h"
  9. struct autofs_info *autofs_new_ino(struct autofs_sb_info *sbi)
  10. {
  11. struct autofs_info *ino;
  12. ino = kzalloc_obj(*ino);
  13. if (ino) {
  14. INIT_LIST_HEAD(&ino->active);
  15. INIT_LIST_HEAD(&ino->expiring);
  16. ino->last_used = jiffies;
  17. ino->sbi = sbi;
  18. ino->exp_timeout = -1;
  19. ino->count = 1;
  20. }
  21. return ino;
  22. }
  23. void autofs_clean_ino(struct autofs_info *ino)
  24. {
  25. ino->uid = GLOBAL_ROOT_UID;
  26. ino->gid = GLOBAL_ROOT_GID;
  27. ino->exp_timeout = -1;
  28. ino->last_used = jiffies;
  29. }
  30. void autofs_free_ino(struct autofs_info *ino)
  31. {
  32. kfree_rcu(ino, rcu);
  33. }
  34. void autofs_kill_sb(struct super_block *sb)
  35. {
  36. struct autofs_sb_info *sbi = autofs_sbi(sb);
  37. /*
  38. * In the event of a failure in get_sb_nodev the superblock
  39. * info is not present so nothing else has been setup, so
  40. * just call kill_anon_super when we are called from
  41. * deactivate_super.
  42. */
  43. if (sbi) {
  44. /* Free wait queues, close pipe */
  45. autofs_catatonic_mode(sbi);
  46. put_pid(sbi->oz_pgrp);
  47. }
  48. pr_debug("shutting down\n");
  49. kill_anon_super(sb);
  50. if (sbi)
  51. kfree_rcu(sbi, rcu);
  52. }
  53. static int autofs_show_options(struct seq_file *m, struct dentry *root)
  54. {
  55. struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
  56. struct inode *root_inode = d_inode(root->d_sb->s_root);
  57. if (!sbi)
  58. return 0;
  59. seq_printf(m, ",fd=%d", sbi->pipefd);
  60. if (!uid_eq(root_inode->i_uid, GLOBAL_ROOT_UID))
  61. seq_printf(m, ",uid=%u",
  62. from_kuid_munged(&init_user_ns, root_inode->i_uid));
  63. if (!gid_eq(root_inode->i_gid, GLOBAL_ROOT_GID))
  64. seq_printf(m, ",gid=%u",
  65. from_kgid_munged(&init_user_ns, root_inode->i_gid));
  66. seq_printf(m, ",pgrp=%d", pid_vnr(sbi->oz_pgrp));
  67. seq_printf(m, ",timeout=%lu", sbi->exp_timeout/HZ);
  68. seq_printf(m, ",minproto=%d", sbi->min_proto);
  69. seq_printf(m, ",maxproto=%d", sbi->max_proto);
  70. if (autofs_type_offset(sbi->type))
  71. seq_puts(m, ",offset");
  72. else if (autofs_type_direct(sbi->type))
  73. seq_puts(m, ",direct");
  74. else
  75. seq_puts(m, ",indirect");
  76. if (sbi->flags & AUTOFS_SBI_STRICTEXPIRE)
  77. seq_puts(m, ",strictexpire");
  78. if (sbi->flags & AUTOFS_SBI_IGNORE)
  79. seq_puts(m, ",ignore");
  80. #ifdef CONFIG_CHECKPOINT_RESTORE
  81. if (sbi->pipe)
  82. seq_printf(m, ",pipe_ino=%ld", file_inode(sbi->pipe)->i_ino);
  83. else
  84. seq_puts(m, ",pipe_ino=-1");
  85. #endif
  86. return 0;
  87. }
  88. static void autofs_evict_inode(struct inode *inode)
  89. {
  90. clear_inode(inode);
  91. kfree(inode->i_private);
  92. }
  93. static const struct super_operations autofs_sops = {
  94. .statfs = simple_statfs,
  95. .show_options = autofs_show_options,
  96. .evict_inode = autofs_evict_inode,
  97. };
  98. enum {
  99. Opt_direct,
  100. Opt_fd,
  101. Opt_gid,
  102. Opt_ignore,
  103. Opt_indirect,
  104. Opt_maxproto,
  105. Opt_minproto,
  106. Opt_offset,
  107. Opt_pgrp,
  108. Opt_strictexpire,
  109. Opt_uid,
  110. };
  111. const struct fs_parameter_spec autofs_param_specs[] = {
  112. fsparam_flag ("direct", Opt_direct),
  113. fsparam_fd ("fd", Opt_fd),
  114. fsparam_gid ("gid", Opt_gid),
  115. fsparam_flag ("ignore", Opt_ignore),
  116. fsparam_flag ("indirect", Opt_indirect),
  117. fsparam_u32 ("maxproto", Opt_maxproto),
  118. fsparam_u32 ("minproto", Opt_minproto),
  119. fsparam_flag ("offset", Opt_offset),
  120. fsparam_u32 ("pgrp", Opt_pgrp),
  121. fsparam_flag ("strictexpire", Opt_strictexpire),
  122. fsparam_uid ("uid", Opt_uid),
  123. {}
  124. };
  125. struct autofs_fs_context {
  126. kuid_t uid;
  127. kgid_t gid;
  128. int pgrp;
  129. bool pgrp_set;
  130. };
  131. /*
  132. * Open the fd. We do it here rather than in get_tree so that it's done in the
  133. * context of the system call that passed the data and not the one that
  134. * triggered the superblock creation, lest the fd gets reassigned.
  135. */
  136. static int autofs_parse_fd(struct fs_context *fc, struct autofs_sb_info *sbi,
  137. struct fs_parameter *param,
  138. struct fs_parse_result *result)
  139. {
  140. struct file *pipe;
  141. int ret;
  142. if (param->type == fs_value_is_file) {
  143. /* came through the new api */
  144. pipe = param->file;
  145. param->file = NULL;
  146. } else {
  147. pipe = fget(result->uint_32);
  148. }
  149. if (!pipe) {
  150. errorf(fc, "could not open pipe file descriptor");
  151. return -EBADF;
  152. }
  153. ret = autofs_check_pipe(pipe);
  154. if (ret < 0) {
  155. errorf(fc, "Invalid/unusable pipe");
  156. fput(pipe);
  157. return -EBADF;
  158. }
  159. autofs_set_packet_pipe_flags(pipe);
  160. if (sbi->pipe)
  161. fput(sbi->pipe);
  162. sbi->pipefd = result->uint_32;
  163. sbi->pipe = pipe;
  164. return 0;
  165. }
  166. static int autofs_parse_param(struct fs_context *fc, struct fs_parameter *param)
  167. {
  168. struct autofs_fs_context *ctx = fc->fs_private;
  169. struct autofs_sb_info *sbi = fc->s_fs_info;
  170. struct fs_parse_result result;
  171. int opt;
  172. opt = fs_parse(fc, autofs_param_specs, param, &result);
  173. if (opt < 0)
  174. return opt;
  175. switch (opt) {
  176. case Opt_fd:
  177. return autofs_parse_fd(fc, sbi, param, &result);
  178. case Opt_uid:
  179. ctx->uid = result.uid;
  180. break;
  181. case Opt_gid:
  182. ctx->gid = result.gid;
  183. break;
  184. case Opt_pgrp:
  185. ctx->pgrp = result.uint_32;
  186. ctx->pgrp_set = true;
  187. break;
  188. case Opt_minproto:
  189. sbi->min_proto = result.uint_32;
  190. break;
  191. case Opt_maxproto:
  192. sbi->max_proto = result.uint_32;
  193. break;
  194. case Opt_indirect:
  195. set_autofs_type_indirect(&sbi->type);
  196. break;
  197. case Opt_direct:
  198. set_autofs_type_direct(&sbi->type);
  199. break;
  200. case Opt_offset:
  201. set_autofs_type_offset(&sbi->type);
  202. break;
  203. case Opt_strictexpire:
  204. sbi->flags |= AUTOFS_SBI_STRICTEXPIRE;
  205. break;
  206. case Opt_ignore:
  207. sbi->flags |= AUTOFS_SBI_IGNORE;
  208. }
  209. return 0;
  210. }
  211. static struct autofs_sb_info *autofs_alloc_sbi(void)
  212. {
  213. struct autofs_sb_info *sbi;
  214. sbi = kzalloc_obj(*sbi);
  215. if (!sbi)
  216. return NULL;
  217. sbi->magic = AUTOFS_SBI_MAGIC;
  218. sbi->flags = AUTOFS_SBI_CATATONIC;
  219. sbi->min_proto = AUTOFS_MIN_PROTO_VERSION;
  220. sbi->max_proto = AUTOFS_MAX_PROTO_VERSION;
  221. sbi->pipefd = -1;
  222. sbi->mnt_ns_id = to_ns_common(current->nsproxy->mnt_ns)->ns_id;
  223. set_autofs_type_indirect(&sbi->type);
  224. mutex_init(&sbi->wq_mutex);
  225. mutex_init(&sbi->pipe_mutex);
  226. spin_lock_init(&sbi->fs_lock);
  227. spin_lock_init(&sbi->lookup_lock);
  228. INIT_LIST_HEAD(&sbi->active_list);
  229. INIT_LIST_HEAD(&sbi->expiring_list);
  230. return sbi;
  231. }
  232. static int autofs_validate_protocol(struct fs_context *fc)
  233. {
  234. struct autofs_sb_info *sbi = fc->s_fs_info;
  235. /* Test versions first */
  236. if (sbi->max_proto < AUTOFS_MIN_PROTO_VERSION ||
  237. sbi->min_proto > AUTOFS_MAX_PROTO_VERSION) {
  238. errorf(fc, "kernel does not match daemon version "
  239. "daemon (%d, %d) kernel (%d, %d)\n",
  240. sbi->min_proto, sbi->max_proto,
  241. AUTOFS_MIN_PROTO_VERSION, AUTOFS_MAX_PROTO_VERSION);
  242. return -EINVAL;
  243. }
  244. /* Establish highest kernel protocol version */
  245. if (sbi->max_proto > AUTOFS_MAX_PROTO_VERSION)
  246. sbi->version = AUTOFS_MAX_PROTO_VERSION;
  247. else
  248. sbi->version = sbi->max_proto;
  249. switch (sbi->version) {
  250. case 4:
  251. sbi->sub_version = 7;
  252. break;
  253. case 5:
  254. sbi->sub_version = AUTOFS_PROTO_SUBVERSION;
  255. break;
  256. default:
  257. sbi->sub_version = 0;
  258. }
  259. return 0;
  260. }
  261. static int autofs_fill_super(struct super_block *s, struct fs_context *fc)
  262. {
  263. struct autofs_fs_context *ctx = fc->fs_private;
  264. struct autofs_sb_info *sbi = s->s_fs_info;
  265. struct inode *root_inode;
  266. struct autofs_info *ino;
  267. pr_debug("starting up, sbi = %p\n", sbi);
  268. sbi->sb = s;
  269. s->s_blocksize = 1024;
  270. s->s_blocksize_bits = 10;
  271. s->s_magic = AUTOFS_SUPER_MAGIC;
  272. s->s_op = &autofs_sops;
  273. set_default_d_op(s, &autofs_dentry_operations);
  274. s->s_time_gran = 1;
  275. /*
  276. * Get the root inode and dentry, but defer checking for errors.
  277. */
  278. ino = autofs_new_ino(sbi);
  279. if (!ino)
  280. return -ENOMEM;
  281. root_inode = autofs_get_inode(s, S_IFDIR | 0755);
  282. if (!root_inode)
  283. return -ENOMEM;
  284. root_inode->i_uid = ctx->uid;
  285. root_inode->i_gid = ctx->gid;
  286. root_inode->i_fop = &autofs_root_operations;
  287. root_inode->i_op = &autofs_dir_inode_operations;
  288. s->s_root = d_make_root(root_inode);
  289. if (unlikely(!s->s_root)) {
  290. autofs_free_ino(ino);
  291. return -ENOMEM;
  292. }
  293. s->s_root->d_fsdata = ino;
  294. if (ctx->pgrp_set) {
  295. sbi->oz_pgrp = find_get_pid(ctx->pgrp);
  296. if (!sbi->oz_pgrp)
  297. return invalf(fc, "Could not find process group %d",
  298. ctx->pgrp);
  299. } else
  300. sbi->oz_pgrp = get_task_pid(current, PIDTYPE_PGID);
  301. if (autofs_type_trigger(sbi->type))
  302. /* s->s_root won't be contended so there's little to
  303. * be gained by not taking the d_lock when setting
  304. * d_flags, even when a lot mounts are being done.
  305. */
  306. managed_dentry_set_managed(s->s_root);
  307. pr_debug("pipe fd = %d, pgrp = %u\n",
  308. sbi->pipefd, pid_nr(sbi->oz_pgrp));
  309. sbi->flags &= ~AUTOFS_SBI_CATATONIC;
  310. return 0;
  311. }
  312. /*
  313. * Validate the parameters and then request a superblock.
  314. */
  315. static int autofs_get_tree(struct fs_context *fc)
  316. {
  317. struct autofs_sb_info *sbi = fc->s_fs_info;
  318. int ret;
  319. ret = autofs_validate_protocol(fc);
  320. if (ret)
  321. return ret;
  322. if (sbi->pipefd < 0)
  323. return invalf(fc, "No control pipe specified");
  324. return get_tree_nodev(fc, autofs_fill_super);
  325. }
  326. static void autofs_free_fc(struct fs_context *fc)
  327. {
  328. struct autofs_fs_context *ctx = fc->fs_private;
  329. struct autofs_sb_info *sbi = fc->s_fs_info;
  330. if (sbi) {
  331. if (sbi->pipe)
  332. fput(sbi->pipe);
  333. kfree(sbi);
  334. }
  335. kfree(ctx);
  336. }
  337. static const struct fs_context_operations autofs_context_ops = {
  338. .free = autofs_free_fc,
  339. .parse_param = autofs_parse_param,
  340. .get_tree = autofs_get_tree,
  341. };
  342. /*
  343. * Set up the filesystem mount context.
  344. */
  345. int autofs_init_fs_context(struct fs_context *fc)
  346. {
  347. struct autofs_fs_context *ctx;
  348. struct autofs_sb_info *sbi;
  349. ctx = kzalloc_obj(struct autofs_fs_context);
  350. if (!ctx)
  351. goto nomem;
  352. ctx->uid = current_uid();
  353. ctx->gid = current_gid();
  354. sbi = autofs_alloc_sbi();
  355. if (!sbi)
  356. goto nomem_ctx;
  357. fc->fs_private = ctx;
  358. fc->s_fs_info = sbi;
  359. fc->ops = &autofs_context_ops;
  360. return 0;
  361. nomem_ctx:
  362. kfree(ctx);
  363. nomem:
  364. return -ENOMEM;
  365. }
  366. struct inode *autofs_get_inode(struct super_block *sb, umode_t mode)
  367. {
  368. struct inode *inode = new_inode(sb);
  369. if (inode == NULL)
  370. return NULL;
  371. inode->i_mode = mode;
  372. if (sb->s_root) {
  373. inode->i_uid = d_inode(sb->s_root)->i_uid;
  374. inode->i_gid = d_inode(sb->s_root)->i_gid;
  375. }
  376. simple_inode_init_ts(inode);
  377. inode->i_ino = get_next_ino();
  378. if (S_ISDIR(mode)) {
  379. set_nlink(inode, 2);
  380. inode->i_op = &autofs_dir_inode_operations;
  381. inode->i_fop = &autofs_dir_operations;
  382. } else if (S_ISLNK(mode)) {
  383. inode->i_op = &autofs_symlink_inode_operations;
  384. } else
  385. WARN_ON(1);
  386. return inode;
  387. }