]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/fs_context.c
fs_context flavour for submounts
[mirror_ubuntu-jammy-kernel.git] / fs / fs_context.c
CommitLineData
9bc61ab1
DH
1/* 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;
e1a91586
AV
72 case FS_CONTEXT_FOR_SUBMOUNT:
73 fc->user_ns = get_user_ns(reference->d_sb->s_user_ns);
74 break;
8d0347f6
DH
75 case FS_CONTEXT_FOR_RECONFIGURE:
76 /* We don't pin any namespaces as the superblock's
77 * subscriptions cannot be changed at this point.
78 */
79 atomic_inc(&reference->d_sb->s_active);
80 fc->root = dget(reference);
81 break;
9bc61ab1
DH
82 }
83
84 ret = legacy_init_fs_context(fc);
85 if (ret < 0)
86 goto err_fc;
87 fc->need_free = true;
88 return fc;
89
90err_fc:
91 put_fs_context(fc);
92 return ERR_PTR(ret);
93}
94
95struct fs_context *fs_context_for_mount(struct file_system_type *fs_type,
96 unsigned int sb_flags)
97{
98 return alloc_fs_context(fs_type, NULL, sb_flags, 0,
99 FS_CONTEXT_FOR_MOUNT);
100}
101EXPORT_SYMBOL(fs_context_for_mount);
102
8d0347f6
DH
103struct fs_context *fs_context_for_reconfigure(struct dentry *dentry,
104 unsigned int sb_flags,
105 unsigned int sb_flags_mask)
106{
107 return alloc_fs_context(dentry->d_sb->s_type, dentry, sb_flags,
108 sb_flags_mask, FS_CONTEXT_FOR_RECONFIGURE);
109}
110EXPORT_SYMBOL(fs_context_for_reconfigure);
111
e1a91586
AV
112struct fs_context *fs_context_for_submount(struct file_system_type *type,
113 struct dentry *reference)
114{
115 return alloc_fs_context(type, reference, 0, 0, FS_CONTEXT_FOR_SUBMOUNT);
116}
117EXPORT_SYMBOL(fs_context_for_submount);
118
c9ce29ed
AV
119void fc_drop_locked(struct fs_context *fc)
120{
121 struct super_block *sb = fc->root->d_sb;
122 dput(fc->root);
123 fc->root = NULL;
124 deactivate_locked_super(sb);
125}
126
9bc61ab1 127static void legacy_fs_context_free(struct fs_context *fc);
8d0347f6 128
9bc61ab1
DH
129/**
130 * put_fs_context - Dispose of a superblock configuration context.
131 * @fc: The context to dispose of.
132 */
133void put_fs_context(struct fs_context *fc)
134{
135 struct super_block *sb;
136
137 if (fc->root) {
138 sb = fc->root->d_sb;
139 dput(fc->root);
140 fc->root = NULL;
141 deactivate_super(sb);
142 }
143
144 if (fc->need_free)
145 legacy_fs_context_free(fc);
146
147 security_free_mnt_opts(&fc->security);
8d0347f6 148 put_net(fc->net_ns);
9bc61ab1
DH
149 put_user_ns(fc->user_ns);
150 put_cred(fc->cred);
151 kfree(fc->subtype);
152 put_filesystem(fc->fs_type);
153 kfree(fc->source);
154 kfree(fc);
155}
156EXPORT_SYMBOL(put_fs_context);
157
158/*
159 * Free the config for a filesystem that doesn't support fs_context.
160 */
161static void legacy_fs_context_free(struct fs_context *fc)
162{
163 kfree(fc->fs_private);
164}
165
166/*
167 * Add monolithic mount data.
168 */
169static int legacy_parse_monolithic(struct fs_context *fc, void *data)
170{
171 struct legacy_fs_context *ctx = fc->fs_private;
172 ctx->legacy_data = data;
173 if (!ctx->legacy_data)
174 return 0;
175 if (fc->fs_type->fs_flags & FS_BINARY_MOUNTDATA)
176 return 0;
177 return security_sb_eat_lsm_opts(ctx->legacy_data, &fc->security);
178}
179
180/*
181 * Get a mountable root with the legacy mount command.
182 */
183int legacy_get_tree(struct fs_context *fc)
184{
185 struct legacy_fs_context *ctx = fc->fs_private;
186 struct super_block *sb;
187 struct dentry *root;
188
189 root = fc->fs_type->mount(fc->fs_type, fc->sb_flags,
190 fc->source, ctx->legacy_data);
191 if (IS_ERR(root))
192 return PTR_ERR(root);
193
194 sb = root->d_sb;
195 BUG_ON(!sb);
196
197 fc->root = root;
198 return 0;
199}
200
8d0347f6
DH
201/*
202 * Handle remount.
203 */
204int legacy_reconfigure(struct fs_context *fc)
205{
206 struct legacy_fs_context *ctx = fc->fs_private;
207 struct super_block *sb = fc->root->d_sb;
208
209 if (!sb->s_op->remount_fs)
210 return 0;
211
212 return sb->s_op->remount_fs(sb, &fc->sb_flags,
213 ctx ? ctx->legacy_data : NULL);
214}
215
9bc61ab1
DH
216/*
217 * Initialise a legacy context for a filesystem that doesn't support
218 * fs_context.
219 */
220static int legacy_init_fs_context(struct fs_context *fc)
221{
222 fc->fs_private = kzalloc(sizeof(struct legacy_fs_context), GFP_KERNEL);
223 if (!fc->fs_private)
224 return -ENOMEM;
225 return 0;
226}
227
228int parse_monolithic_mount_data(struct fs_context *fc, void *data)
229{
230 return legacy_parse_monolithic(fc, data);
231}