blob: 857cd46a687b786b57ee3a025fe198f4a4929fe8 [file] [log] [blame]
David Howells9bc61ab2018-11-04 03:19:03 -05001/* Provide a way to create a superblock configuration context within the kernel
2 * that allows a superblock to be set up prior to mounting.
3 *
4 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public Licence
9 * as published by the Free Software Foundation; either version
10 * 2 of the Licence, or (at your option) any later version.
11 */
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14#include <linux/fs_context.h>
15#include <linux/fs.h>
16#include <linux/mount.h>
17#include <linux/nsproxy.h>
18#include <linux/slab.h>
19#include <linux/magic.h>
20#include <linux/security.h>
21#include <linux/mnt_namespace.h>
22#include <linux/pid_namespace.h>
23#include <linux/user_namespace.h>
24#include <net/net_namespace.h>
25#include "mount.h"
26#include "internal.h"
27
28struct legacy_fs_context {
29 char *legacy_data; /* Data page for legacy filesystems */
30 size_t data_size;
31};
32
33static int legacy_init_fs_context(struct fs_context *fc);
34
35/**
36 * alloc_fs_context - Create a filesystem context.
37 * @fs_type: The filesystem type.
38 * @reference: The dentry from which this one derives (or NULL)
39 * @sb_flags: Filesystem/superblock flags (SB_*)
40 * @sb_flags_mask: Applicable members of @sb_flags
41 * @purpose: The purpose that this configuration shall be used for.
42 *
43 * Open a filesystem and create a mount context. The mount context is
44 * initialised with the supplied flags and, if a submount/automount from
45 * another superblock (referred to by @reference) is supplied, may have
46 * parameters such as namespaces copied across from that superblock.
47 */
48static struct fs_context *alloc_fs_context(struct file_system_type *fs_type,
49 struct dentry *reference,
50 unsigned int sb_flags,
51 unsigned int sb_flags_mask,
52 enum fs_context_purpose purpose)
53{
54 struct fs_context *fc;
55 int ret = -ENOMEM;
56
57 fc = kzalloc(sizeof(struct fs_context), GFP_KERNEL);
58 if (!fc)
59 return ERR_PTR(-ENOMEM);
60
61 fc->purpose = purpose;
62 fc->sb_flags = sb_flags;
63 fc->sb_flags_mask = sb_flags_mask;
64 fc->fs_type = get_filesystem(fs_type);
65 fc->cred = get_current_cred();
66 fc->net_ns = get_net(current->nsproxy->net_ns);
67
68 switch (purpose) {
69 case FS_CONTEXT_FOR_MOUNT:
70 fc->user_ns = get_user_ns(fc->cred->user_ns);
71 break;
72 }
73
74 ret = legacy_init_fs_context(fc);
75 if (ret < 0)
76 goto err_fc;
77 fc->need_free = true;
78 return fc;
79
80err_fc:
81 put_fs_context(fc);
82 return ERR_PTR(ret);
83}
84
85struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
86 unsigned int sb_flags)
87{
88 return alloc_fs_context(fs_type, NULL, sb_flags, 0,
89 FS_CONTEXT_FOR_MOUNT);
90}
91EXPORT_SYMBOL(fs_context_for_mount);
92
Al Viroc9ce29e2018-12-20 15:04:50 -050093void fc_drop_locked(struct fs_context *fc)
94{
95 struct super_block *sb = fc->root->d_sb;
96 dput(fc->root);
97 fc->root = NULL;
98 deactivate_locked_super(sb);
99}
100
David Howells9bc61ab2018-11-04 03:19:03 -0500101static void legacy_fs_context_free(struct fs_context *fc);
102/**
103 * put_fs_context - Dispose of a superblock configuration context.
104 * @fc: The context to dispose of.
105 */
106void put_fs_context(struct fs_context *fc)
107{
108 struct super_block *sb;
109
110 if (fc->root) {
111 sb = fc->root->d_sb;
112 dput(fc->root);
113 fc->root = NULL;
114 deactivate_super(sb);
115 }
116
117 if (fc->need_free)
118 legacy_fs_context_free(fc);
119
120 security_free_mnt_opts(&fc->security);
121 if (fc->net_ns)
122 put_net(fc->net_ns);
123 put_user_ns(fc->user_ns);
124 put_cred(fc->cred);
125 kfree(fc->subtype);
126 put_filesystem(fc->fs_type);
127 kfree(fc->source);
128 kfree(fc);
129}
130EXPORT_SYMBOL(put_fs_context);
131
132/*
133 * Free the config for a filesystem that doesn't support fs_context.
134 */
135static void legacy_fs_context_free(struct fs_context *fc)
136{
137 kfree(fc->fs_private);
138}
139
140/*
141 * Add monolithic mount data.
142 */
143static int legacy_parse_monolithic(struct fs_context *fc, void *data)
144{
145 struct legacy_fs_context *ctx = fc->fs_private;
146 ctx->legacy_data = data;
147 if (!ctx->legacy_data)
148 return 0;
149 if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
150 return 0;
151 return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);
152}
153
154/*
155 * Get a mountable root with the legacy mount command.
156 */
157int legacy_get_tree(struct fs_context *fc)
158{
159 struct legacy_fs_context *ctx = fc->fs_private;
160 struct super_block *sb;
161 struct dentry *root;
162
163 root = fc->fs_type->mount(fc->fs_type, fc->sb_flags,
164 fc->source, ctx->legacy_data);
165 if (IS_ERR(root))
166 return PTR_ERR(root);
167
168 sb = root->d_sb;
169 BUG_ON(!sb);
170
171 fc->root = root;
172 return 0;
173}
174
175/*
176 * Initialise a legacy context for a filesystem that doesn't support
177 * fs_context.
178 */
179static int legacy_init_fs_context(struct fs_context *fc)
180{
181 fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL);
182 if (!fc->fs_private)
183 return -ENOMEM;
184 return 0;
185}
186
187int parse_monolithic_mount_data(struct fs_context *fc, void *data)
188{
189 return legacy_parse_monolithic(fc, data);
190}