1 // SPDX-License-Identifier: GPL-2.0-only
3 * Common helpers for stackable filesystems and backing files.
5 * Forked from fs/overlayfs/file.c.
7 * Copyright (C) 2017 Red Hat, Inc.
8 * Copyright (C) 2023 CTERA Networks.
12 #include <linux/backing-file.h>
13 #include <linux/splice.h>
18 * backing_file_open - open a backing file for kernel internal use
19 * @user_path: path that the user reuqested to open
21 * @real_path: path of the backing file
22 * @cred: credentials for open
24 * Open a backing file for a stackable filesystem (e.g., overlayfs).
25 * @user_path may be on the stackable filesystem and @real_path on the
26 * underlying filesystem. In this case, we want to be able to return the
27 * @user_path of the stackable filesystem. This is done by embedding the
28 * returned file into a container structure that also stores the stacked
29 * file's path, which can be retrieved using backing_file_user_path().
31 struct file
*backing_file_open(const struct path
*user_path
, int flags
,
32 const struct path
*real_path
,
33 const struct cred
*cred
)
38 f
= alloc_empty_backing_file(flags
, cred
);
43 *backing_file_user_path(f
) = *user_path
;
44 error
= vfs_open(real_path
, f
);
52 EXPORT_SYMBOL_GPL(backing_file_open
);
57 struct kiocb
*orig_iocb
;
58 /* used for aio completion */
59 void (*end_write
)(struct file
*);
60 struct work_struct work
;
64 static struct kmem_cache
*backing_aio_cachep
;
66 #define BACKING_IOCB_MASK \
67 (IOCB_NOWAIT | IOCB_HIPRI | IOCB_DSYNC | IOCB_SYNC | IOCB_APPEND)
69 static rwf_t
iocb_to_rw_flags(int flags
)
71 return (__force rwf_t
)(flags
& BACKING_IOCB_MASK
);
74 static void backing_aio_put(struct backing_aio
*aio
)
76 if (refcount_dec_and_test(&aio
->ref
)) {
77 fput(aio
->iocb
.ki_filp
);
78 kmem_cache_free(backing_aio_cachep
, aio
);
82 static void backing_aio_cleanup(struct backing_aio
*aio
, long res
)
84 struct kiocb
*iocb
= &aio
->iocb
;
85 struct kiocb
*orig_iocb
= aio
->orig_iocb
;
88 aio
->end_write(orig_iocb
->ki_filp
);
90 orig_iocb
->ki_pos
= iocb
->ki_pos
;
94 static void backing_aio_rw_complete(struct kiocb
*iocb
, long res
)
96 struct backing_aio
*aio
= container_of(iocb
, struct backing_aio
, iocb
);
97 struct kiocb
*orig_iocb
= aio
->orig_iocb
;
99 if (iocb
->ki_flags
& IOCB_WRITE
)
100 kiocb_end_write(iocb
);
102 backing_aio_cleanup(aio
, res
);
103 orig_iocb
->ki_complete(orig_iocb
, res
);
106 static void backing_aio_complete_work(struct work_struct
*work
)
108 struct backing_aio
*aio
= container_of(work
, struct backing_aio
, work
);
110 backing_aio_rw_complete(&aio
->iocb
, aio
->res
);
113 static void backing_aio_queue_completion(struct kiocb
*iocb
, long res
)
115 struct backing_aio
*aio
= container_of(iocb
, struct backing_aio
, iocb
);
118 * Punt to a work queue to serialize updates of mtime/size.
121 INIT_WORK(&aio
->work
, backing_aio_complete_work
);
122 queue_work(file_inode(aio
->orig_iocb
->ki_filp
)->i_sb
->s_dio_done_wq
,
126 static int backing_aio_init_wq(struct kiocb
*iocb
)
128 struct super_block
*sb
= file_inode(iocb
->ki_filp
)->i_sb
;
130 if (sb
->s_dio_done_wq
)
133 return sb_init_dio_done_wq(sb
);
137 ssize_t
backing_file_read_iter(struct file
*file
, struct iov_iter
*iter
,
138 struct kiocb
*iocb
, int flags
,
139 struct backing_file_ctx
*ctx
)
141 struct backing_aio
*aio
= NULL
;
142 const struct cred
*old_cred
;
145 if (WARN_ON_ONCE(!(file
->f_mode
& FMODE_BACKING
)))
148 if (!iov_iter_count(iter
))
151 if (iocb
->ki_flags
& IOCB_DIRECT
&&
152 !(file
->f_mode
& FMODE_CAN_ODIRECT
))
155 old_cred
= override_creds(ctx
->cred
);
156 if (is_sync_kiocb(iocb
)) {
157 rwf_t rwf
= iocb_to_rw_flags(flags
);
159 ret
= vfs_iter_read(file
, iter
, &iocb
->ki_pos
, rwf
);
162 aio
= kmem_cache_zalloc(backing_aio_cachep
, GFP_KERNEL
);
166 aio
->orig_iocb
= iocb
;
167 kiocb_clone(&aio
->iocb
, iocb
, get_file(file
));
168 aio
->iocb
.ki_complete
= backing_aio_rw_complete
;
169 refcount_set(&aio
->ref
, 2);
170 ret
= vfs_iocb_iter_read(file
, &aio
->iocb
, iter
);
171 backing_aio_put(aio
);
172 if (ret
!= -EIOCBQUEUED
)
173 backing_aio_cleanup(aio
, ret
);
176 revert_creds(old_cred
);
179 ctx
->accessed(ctx
->user_file
);
183 EXPORT_SYMBOL_GPL(backing_file_read_iter
);
185 ssize_t
backing_file_write_iter(struct file
*file
, struct iov_iter
*iter
,
186 struct kiocb
*iocb
, int flags
,
187 struct backing_file_ctx
*ctx
)
189 const struct cred
*old_cred
;
192 if (WARN_ON_ONCE(!(file
->f_mode
& FMODE_BACKING
)))
195 if (!iov_iter_count(iter
))
198 ret
= file_remove_privs(ctx
->user_file
);
202 if (iocb
->ki_flags
& IOCB_DIRECT
&&
203 !(file
->f_mode
& FMODE_CAN_ODIRECT
))
207 * Stacked filesystems don't support deferred completions, don't copy
208 * this property in case it is set by the issuer.
210 flags
&= ~IOCB_DIO_CALLER_COMP
;
212 old_cred
= override_creds(ctx
->cred
);
213 if (is_sync_kiocb(iocb
)) {
214 rwf_t rwf
= iocb_to_rw_flags(flags
);
216 ret
= vfs_iter_write(file
, iter
, &iocb
->ki_pos
, rwf
);
218 ctx
->end_write(ctx
->user_file
);
220 struct backing_aio
*aio
;
222 ret
= backing_aio_init_wq(iocb
);
227 aio
= kmem_cache_zalloc(backing_aio_cachep
, GFP_KERNEL
);
231 aio
->orig_iocb
= iocb
;
232 aio
->end_write
= ctx
->end_write
;
233 kiocb_clone(&aio
->iocb
, iocb
, get_file(file
));
234 aio
->iocb
.ki_flags
= flags
;
235 aio
->iocb
.ki_complete
= backing_aio_queue_completion
;
236 refcount_set(&aio
->ref
, 2);
237 ret
= vfs_iocb_iter_write(file
, &aio
->iocb
, iter
);
238 backing_aio_put(aio
);
239 if (ret
!= -EIOCBQUEUED
)
240 backing_aio_cleanup(aio
, ret
);
243 revert_creds(old_cred
);
247 EXPORT_SYMBOL_GPL(backing_file_write_iter
);
249 ssize_t
backing_file_splice_read(struct file
*in
, loff_t
*ppos
,
250 struct pipe_inode_info
*pipe
, size_t len
,
252 struct backing_file_ctx
*ctx
)
254 const struct cred
*old_cred
;
257 if (WARN_ON_ONCE(!(in
->f_mode
& FMODE_BACKING
)))
260 old_cred
= override_creds(ctx
->cred
);
261 ret
= vfs_splice_read(in
, ppos
, pipe
, len
, flags
);
262 revert_creds(old_cred
);
265 ctx
->accessed(ctx
->user_file
);
269 EXPORT_SYMBOL_GPL(backing_file_splice_read
);
271 ssize_t
backing_file_splice_write(struct pipe_inode_info
*pipe
,
272 struct file
*out
, loff_t
*ppos
, size_t len
,
274 struct backing_file_ctx
*ctx
)
276 const struct cred
*old_cred
;
279 if (WARN_ON_ONCE(!(out
->f_mode
& FMODE_BACKING
)))
282 ret
= file_remove_privs(ctx
->user_file
);
286 old_cred
= override_creds(ctx
->cred
);
287 file_start_write(out
);
288 ret
= iter_file_splice_write(pipe
, out
, ppos
, len
, flags
);
290 revert_creds(old_cred
);
293 ctx
->end_write(ctx
->user_file
);
297 EXPORT_SYMBOL_GPL(backing_file_splice_write
);
299 static int __init
backing_aio_init(void)
301 backing_aio_cachep
= kmem_cache_create("backing_aio",
302 sizeof(struct backing_aio
),
303 0, SLAB_HWCACHE_ALIGN
, NULL
);
304 if (!backing_aio_cachep
)
309 fs_initcall(backing_aio_init
);