]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - fs/backing-file.c
fs: factor out backing_file_{read,write}_iter() helpers
[thirdparty/kernel/linux.git] / fs / backing-file.c
CommitLineData
f91a704f
AG
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Common helpers for stackable filesystems and backing files.
4 *
a6293b3e
AG
5 * Forked from fs/overlayfs/file.c.
6 *
7 * Copyright (C) 2017 Red Hat, Inc.
f91a704f
AG
8 * Copyright (C) 2023 CTERA Networks.
9 */
10
11#include <linux/fs.h>
12#include <linux/backing-file.h>
13
14#include "internal.h"
15
16/**
17 * backing_file_open - open a backing file for kernel internal use
18 * @user_path: path that the user reuqested to open
19 * @flags: open flags
20 * @real_path: path of the backing file
21 * @cred: credentials for open
22 *
23 * Open a backing file for a stackable filesystem (e.g., overlayfs).
24 * @user_path may be on the stackable filesystem and @real_path on the
25 * underlying filesystem. In this case, we want to be able to return the
26 * @user_path of the stackable filesystem. This is done by embedding the
27 * returned file into a container structure that also stores the stacked
28 * file's path, which can be retrieved using backing_file_user_path().
29 */
30struct file *backing_file_open(const struct path *user_path, int flags,
31 const struct path *real_path,
32 const struct cred *cred)
33{
34 struct file *f;
35 int error;
36
37 f = alloc_empty_backing_file(flags, cred);
38 if (IS_ERR(f))
39 return f;
40
41 path_get(user_path);
42 *backing_file_user_path(f) = *user_path;
43 error = vfs_open(real_path, f);
44 if (error) {
45 fput(f);
46 f = ERR_PTR(error);
47 }
48
49 return f;
50}
51EXPORT_SYMBOL_GPL(backing_file_open);
a6293b3e
AG
52
53struct backing_aio {
54 struct kiocb iocb;
55 refcount_t ref;
56 struct kiocb *orig_iocb;
57 /* used for aio completion */
58 void (*end_write)(struct file *);
59 struct work_struct work;
60 long res;
61};
62
63static struct kmem_cache *backing_aio_cachep;
64
65#define BACKING_IOCB_MASK \
66 (IOCB_NOWAIT | IOCB_HIPRI | IOCB_DSYNC | IOCB_SYNC | IOCB_APPEND)
67
68static rwf_t iocb_to_rw_flags(int flags)
69{
70 return (__force rwf_t)(flags & BACKING_IOCB_MASK);
71}
72
73static void backing_aio_put(struct backing_aio *aio)
74{
75 if (refcount_dec_and_test(&aio->ref)) {
76 fput(aio->iocb.ki_filp);
77 kmem_cache_free(backing_aio_cachep, aio);
78 }
79}
80
81static void backing_aio_cleanup(struct backing_aio *aio, long res)
82{
83 struct kiocb *iocb = &aio->iocb;
84 struct kiocb *orig_iocb = aio->orig_iocb;
85
86 if (aio->end_write)
87 aio->end_write(orig_iocb->ki_filp);
88
89 orig_iocb->ki_pos = iocb->ki_pos;
90 backing_aio_put(aio);
91}
92
93static void backing_aio_rw_complete(struct kiocb *iocb, long res)
94{
95 struct backing_aio *aio = container_of(iocb, struct backing_aio, iocb);
96 struct kiocb *orig_iocb = aio->orig_iocb;
97
98 if (iocb->ki_flags & IOCB_WRITE)
99 kiocb_end_write(iocb);
100
101 backing_aio_cleanup(aio, res);
102 orig_iocb->ki_complete(orig_iocb, res);
103}
104
105static void backing_aio_complete_work(struct work_struct *work)
106{
107 struct backing_aio *aio = container_of(work, struct backing_aio, work);
108
109 backing_aio_rw_complete(&aio->iocb, aio->res);
110}
111
112static void backing_aio_queue_completion(struct kiocb *iocb, long res)
113{
114 struct backing_aio *aio = container_of(iocb, struct backing_aio, iocb);
115
116 /*
117 * Punt to a work queue to serialize updates of mtime/size.
118 */
119 aio->res = res;
120 INIT_WORK(&aio->work, backing_aio_complete_work);
121 queue_work(file_inode(aio->orig_iocb->ki_filp)->i_sb->s_dio_done_wq,
122 &aio->work);
123}
124
125static int backing_aio_init_wq(struct kiocb *iocb)
126{
127 struct super_block *sb = file_inode(iocb->ki_filp)->i_sb;
128
129 if (sb->s_dio_done_wq)
130 return 0;
131
132 return sb_init_dio_done_wq(sb);
133}
134
135
136ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter,
137 struct kiocb *iocb, int flags,
138 struct backing_file_ctx *ctx)
139{
140 struct backing_aio *aio = NULL;
141 const struct cred *old_cred;
142 ssize_t ret;
143
144 if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
145 return -EIO;
146
147 if (!iov_iter_count(iter))
148 return 0;
149
150 if (iocb->ki_flags & IOCB_DIRECT &&
151 !(file->f_mode & FMODE_CAN_ODIRECT))
152 return -EINVAL;
153
154 old_cred = override_creds(ctx->cred);
155 if (is_sync_kiocb(iocb)) {
156 rwf_t rwf = iocb_to_rw_flags(flags);
157
158 ret = vfs_iter_read(file, iter, &iocb->ki_pos, rwf);
159 } else {
160 ret = -ENOMEM;
161 aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
162 if (!aio)
163 goto out;
164
165 aio->orig_iocb = iocb;
166 kiocb_clone(&aio->iocb, iocb, get_file(file));
167 aio->iocb.ki_complete = backing_aio_rw_complete;
168 refcount_set(&aio->ref, 2);
169 ret = vfs_iocb_iter_read(file, &aio->iocb, iter);
170 backing_aio_put(aio);
171 if (ret != -EIOCBQUEUED)
172 backing_aio_cleanup(aio, ret);
173 }
174out:
175 revert_creds(old_cred);
176
177 if (ctx->accessed)
178 ctx->accessed(ctx->user_file);
179
180 return ret;
181}
182EXPORT_SYMBOL_GPL(backing_file_read_iter);
183
184ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
185 struct kiocb *iocb, int flags,
186 struct backing_file_ctx *ctx)
187{
188 const struct cred *old_cred;
189 ssize_t ret;
190
191 if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
192 return -EIO;
193
194 if (!iov_iter_count(iter))
195 return 0;
196
197 ret = file_remove_privs(ctx->user_file);
198 if (ret)
199 return ret;
200
201 if (iocb->ki_flags & IOCB_DIRECT &&
202 !(file->f_mode & FMODE_CAN_ODIRECT))
203 return -EINVAL;
204
205 /*
206 * Stacked filesystems don't support deferred completions, don't copy
207 * this property in case it is set by the issuer.
208 */
209 flags &= ~IOCB_DIO_CALLER_COMP;
210
211 old_cred = override_creds(ctx->cred);
212 if (is_sync_kiocb(iocb)) {
213 rwf_t rwf = iocb_to_rw_flags(flags);
214
215 ret = vfs_iter_write(file, iter, &iocb->ki_pos, rwf);
216 if (ctx->end_write)
217 ctx->end_write(ctx->user_file);
218 } else {
219 struct backing_aio *aio;
220
221 ret = backing_aio_init_wq(iocb);
222 if (ret)
223 goto out;
224
225 ret = -ENOMEM;
226 aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
227 if (!aio)
228 goto out;
229
230 aio->orig_iocb = iocb;
231 aio->end_write = ctx->end_write;
232 kiocb_clone(&aio->iocb, iocb, get_file(file));
233 aio->iocb.ki_flags = flags;
234 aio->iocb.ki_complete = backing_aio_queue_completion;
235 refcount_set(&aio->ref, 2);
236 ret = vfs_iocb_iter_write(file, &aio->iocb, iter);
237 backing_aio_put(aio);
238 if (ret != -EIOCBQUEUED)
239 backing_aio_cleanup(aio, ret);
240 }
241out:
242 revert_creds(old_cred);
243
244 return ret;
245}
246EXPORT_SYMBOL_GPL(backing_file_write_iter);
247
248static int __init backing_aio_init(void)
249{
250 backing_aio_cachep = kmem_cache_create("backing_aio",
251 sizeof(struct backing_aio),
252 0, SLAB_HWCACHE_ALIGN, NULL);
253 if (!backing_aio_cachep)
254 return -ENOMEM;
255
256 return 0;
257}
258fs_initcall(backing_aio_init);