summaryrefslogtreecommitdiff
path: root/fs/backing-file.c
blob: c601b27f0fd35ca531fd835e5f7d6e0def34eace (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Common helpers for stackable filesystems and backing files.
 *
 * Forked from fs/overlayfs/file.c.
 *
 * Copyright (C) 2017 Red Hat, Inc.
 * Copyright (C) 2023 CTERA Networks.
 */

#include <linux/fs.h>
#include <linux/backing-file.h>
#include <linux/splice.h>
#include <linux/mm.h>
#include <linux/security.h>

#include "internal.h"

/**
 * backing_file_open - open a backing file for kernel internal use
 * @user_file:  file the user requested to open
 * @flags:	open flags
 * @real_path:	path of the backing file
 * @cred:	credentials for open
 *
 * Open a backing file for a stackable filesystem (e.g., overlayfs).
 * @user_file->f_path may be on the stackable filesystem and @real_path
 * on the underlying filesystem. In this case, we want to be able to
 * return the path of the stackable filesystem. This is done by
 * embedding the returned file into a container structure that also
 * stores the stacked file's path, which can be retrieved using
 * backing_file_user_path().
 */
struct file *backing_file_open(const struct file *user_file, int flags,
			       const struct path *real_path,
			       const struct cred *cred)
{
	const struct path *user_path = file_user_path(user_file);
	struct inode *real_inode = d_inode(real_path->dentry);
	struct file *f;
	int error;

	/*
	 * may_open() checked the inode of the user file, which need not carry
	 * S_APPEND and S_IMMUTABLE of the backing inode.
	 */
	if ((flags & O_ACCMODE) != O_RDONLY) {
		if (IS_IMMUTABLE(real_inode))
			return ERR_PTR(-EPERM);
		if (IS_APPEND(real_inode) && !(flags & O_APPEND))
			return ERR_PTR(-EPERM);
	}

	f = alloc_empty_backing_file(flags, cred, user_file);
	if (IS_ERR(f))
		return f;

	/*
	 * A user file that must not generate fsnotify events (e.g. the fd
	 * that fanotify creates for a permission event with
	 * dentry_open_nonotify()) must not generate them through its backing
	 * file either. Otherwise a listener watching the backing filesystem
	 * gets a nested permission event from its own read(2), which only
	 * another reader of the same group can answer.
	 */
	if (FMODE_FSNOTIFY_NONE(user_file->f_mode))
		file_set_fsnotify_mode(f, FMODE_NONOTIFY);

	path_get(user_path);
	backing_file_set_user_path(f, user_path);
	error = vfs_open(real_path, f);
	if (error) {
		fput(f);
		f = ERR_PTR(error);
	}

	return f;
}
EXPORT_SYMBOL_GPL(backing_file_open);

struct file *backing_tmpfile_open(const struct file *user_file, int flags,
				  const struct path *real_parentpath,
				  umode_t mode, const struct cred *cred)
{
	const struct mnt_idmap *real_idmap = mnt_idmap(real_parentpath->mnt);
	const struct path *user_path = &user_file->f_path;
	struct file *f;
	int error;

	f = alloc_empty_backing_file(flags, cred, user_file);
	if (IS_ERR(f))
		return f;

	path_get(user_path);
	backing_file_set_user_path(f, user_path);
	error = vfs_tmpfile(real_idmap, real_parentpath, f, mode);
	if (error) {
		fput(f);
		f = ERR_PTR(error);
	}
	return f;
}
EXPORT_SYMBOL(backing_tmpfile_open);

struct backing_aio {
	struct kiocb iocb;
	refcount_t ref;
	struct kiocb *orig_iocb;
	/* used for aio completion */
	bool is_write;
	void (*end_write)(struct kiocb *iocb, ssize_t);
	struct work_struct work;
	long res;
};

static struct kmem_cache *backing_aio_cachep;

/*
 * RWF_NOAPPEND stays set in ki_flags.  Pass it on, because the backing file is
 * in append mode as well and a new kiocb for it starts with IOCB_APPEND.
 */
#define BACKING_IOCB_MASK \
	(IOCB_NOWAIT | IOCB_HIPRI | IOCB_DSYNC | IOCB_SYNC | IOCB_APPEND | \
	 (__force int) RWF_NOAPPEND)

static rwf_t iocb_to_rw_flags(int flags)
{
	return (__force rwf_t)(flags & BACKING_IOCB_MASK);
}

static void backing_aio_put(struct backing_aio *aio)
{
	if (refcount_dec_and_test(&aio->ref)) {
		fput(aio->iocb.ki_filp);
		kmem_cache_free(backing_aio_cachep, aio);
	}
}

static void backing_aio_cleanup(struct backing_aio *aio, long res)
{
	struct kiocb *iocb = &aio->iocb;
	struct kiocb *orig_iocb = aio->orig_iocb;

	orig_iocb->ki_pos = iocb->ki_pos;
	if (aio->end_write)
		aio->end_write(orig_iocb, res);

	backing_aio_put(aio);
}

static void backing_aio_rw_complete(struct kiocb *iocb, long res)
{
	struct backing_aio *aio = container_of(iocb, struct backing_aio, iocb);
	struct kiocb *orig_iocb = aio->orig_iocb;

	/* Pairs with kiocb_start_write() in vfs_iocb_iter_write() */
	if (aio->is_write)
		kiocb_end_write(iocb);

	backing_aio_cleanup(aio, res);
	orig_iocb->ki_complete(orig_iocb, res);
}

static void backing_aio_complete_work(struct work_struct *work)
{
	struct backing_aio *aio = container_of(work, struct backing_aio, work);

	backing_aio_rw_complete(&aio->iocb, aio->res);
}

static void backing_aio_queue_completion(struct kiocb *iocb, long res)
{
	struct backing_aio *aio = container_of(iocb, struct backing_aio, iocb);

	/*
	 * Punt to a work queue to serialize updates of mtime/size.
	 */
	aio->res = res;
	INIT_WORK(&aio->work, backing_aio_complete_work);
	queue_work(file_inode(aio->orig_iocb->ki_filp)->i_sb->s_dio_done_wq,
		   &aio->work);
}

static int backing_aio_init_wq(struct kiocb *iocb)
{
	struct super_block *sb = file_inode(iocb->ki_filp)->i_sb;

	if (sb->s_dio_done_wq)
		return 0;

	return sb_init_dio_done_wq(sb);
}

static int do_backing_file_read_iter(struct file *file, struct iov_iter *iter,
				     struct kiocb *iocb, int flags)
{
	struct backing_aio *aio = NULL;
	int ret;

	if (is_sync_kiocb(iocb)) {
		rwf_t rwf = iocb_to_rw_flags(flags);

		return vfs_iter_read(file, iter, &iocb->ki_pos, rwf);
	}

	aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
	if (!aio)
		return -ENOMEM;

	aio->orig_iocb = iocb;
	kiocb_clone(&aio->iocb, iocb, get_file(file));
	aio->iocb.ki_complete = backing_aio_rw_complete;
	refcount_set(&aio->ref, 2);
	ret = vfs_iocb_iter_read(file, &aio->iocb, iter);
	backing_aio_put(aio);
	if (ret != -EIOCBQUEUED)
		backing_aio_cleanup(aio, ret);
	return ret;
}

ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter,
			       struct kiocb *iocb, int flags,
			       struct backing_file_ctx *ctx)
{
	ssize_t ret;

	if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
		return -EIO;

	if (!iov_iter_count(iter))
		return 0;

	if (iocb->ki_flags & IOCB_DIRECT &&
	    !(file->f_mode & FMODE_CAN_ODIRECT))
		return -EINVAL;

	scoped_with_creds(ctx->cred)
		ret = do_backing_file_read_iter(file, iter, iocb, flags);

	if (ctx->accessed)
		ctx->accessed(iocb->ki_filp);

	return ret;
}
EXPORT_SYMBOL_GPL(backing_file_read_iter);

static int do_backing_file_write_iter(struct file *file, struct iov_iter *iter,
				      struct kiocb *iocb, int flags,
				      void (*end_write)(struct kiocb *, ssize_t))
{
	struct backing_aio *aio;
	int ret;

	if (is_sync_kiocb(iocb)) {
		rwf_t rwf = iocb_to_rw_flags(flags);

		ret = vfs_iter_write(file, iter, &iocb->ki_pos, rwf);
		if (end_write)
			end_write(iocb, ret);
		return ret;
	}

	ret = backing_aio_init_wq(iocb);
	if (ret)
		return ret;

	aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
	if (!aio)
		return -ENOMEM;

	aio->orig_iocb = iocb;
	aio->is_write = true;
	aio->end_write = end_write;
	kiocb_clone(&aio->iocb, iocb, get_file(file));
	aio->iocb.ki_flags = flags;
	aio->iocb.ki_complete = backing_aio_queue_completion;
	refcount_set(&aio->ref, 2);
	ret = vfs_iocb_iter_write(file, &aio->iocb, iter);
	backing_aio_put(aio);
	if (ret != -EIOCBQUEUED)
		backing_aio_cleanup(aio, ret);
	return ret;
}

ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
				struct kiocb *iocb, int flags,
				struct backing_file_ctx *ctx)
{
	ssize_t ret;

	if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
		return -EIO;

	if (!iov_iter_count(iter))
		return 0;

	/*
	 * F_SETFL and RWF_NOAPPEND can take append mode away from the user
	 * file. They checked its inode, not the backing one.
	 */
	if ((file->f_flags & O_APPEND) && !(flags & IOCB_APPEND) &&
	    IS_APPEND(file_inode(file)))
		return -EPERM;

	ret = file_remove_privs(iocb->ki_filp);
	if (ret)
		return ret;

	if (iocb->ki_flags & IOCB_DIRECT &&
	    !(file->f_mode & FMODE_CAN_ODIRECT))
		return -EINVAL;

	scoped_with_creds(ctx->cred)
		return do_backing_file_write_iter(file, iter, iocb, flags, ctx->end_write);
}
EXPORT_SYMBOL_GPL(backing_file_write_iter);

ssize_t backing_file_splice_read(struct file *in, struct kiocb *iocb,
				 struct pipe_inode_info *pipe, size_t len,
				 unsigned int flags,
				 struct backing_file_ctx *ctx)
{
	ssize_t ret;

	if (WARN_ON_ONCE(!(in->f_mode & FMODE_BACKING)))
		return -EIO;

	scoped_with_creds(ctx->cred)
		ret = vfs_splice_read(in, &iocb->ki_pos, pipe, len, flags);

	if (ctx->accessed)
		ctx->accessed(iocb->ki_filp);

	return ret;
}
EXPORT_SYMBOL_GPL(backing_file_splice_read);

ssize_t backing_file_splice_write(struct pipe_inode_info *pipe,
				  struct file *out, struct kiocb *iocb,
				  size_t len, unsigned int flags,
				  struct backing_file_ctx *ctx)
{
	ssize_t ret;

	if (WARN_ON_ONCE(!(out->f_mode & FMODE_BACKING)))
		return -EIO;

	if (!out->f_op->splice_write)
		return -EINVAL;

	/* splice refuses a user file in append mode, so F_SETFL cleared it */
	if ((out->f_flags & O_APPEND) && IS_APPEND(file_inode(out)))
		return -EPERM;

	ret = file_remove_privs(iocb->ki_filp);
	if (ret)
		return ret;

	scoped_with_creds(ctx->cred) {
		file_start_write(out);
		ret = out->f_op->splice_write(pipe, out, &iocb->ki_pos, len, flags);
		file_end_write(out);
	}

	if (ctx->end_write)
		ctx->end_write(iocb, ret);

	return ret;
}
EXPORT_SYMBOL_GPL(backing_file_splice_write);

int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
		      struct backing_file_ctx *ctx)
{
	struct file *user_file = vma->vm_file;
	int ret;

	if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
		return -EIO;

	if (!can_mmap_file(file))
		return -ENODEV;

	/* do_mmap() checked the inode of the user file */
	if ((vma->vm_flags & VM_SHARED) && IS_APPEND(file_inode(file)))
		return -EACCES;

	vma_set_file(vma, file);

	scoped_with_creds(ctx->cred) {
		ret = security_mmap_backing_file(vma, file, user_file);
		if (ret)
			return ret;

		ret = vfs_mmap(vma->vm_file, vma);
	}

	if (ctx->accessed)
		ctx->accessed(user_file);

	return ret;
}
EXPORT_SYMBOL_GPL(backing_file_mmap);

static int __init backing_aio_init(void)
{
	backing_aio_cachep = KMEM_CACHE(backing_aio, SLAB_HWCACHE_ALIGN);
	if (!backing_aio_cachep)
		return -ENOMEM;

	return 0;
}
fs_initcall(backing_aio_init);