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>
19 * backing_file_open - open a backing file for kernel internal use
20 * @user_path: path that the user reuqested to open
22 * @real_path: path of the backing file
23 * @cred: credentials for open
25 * Open a backing file for a stackable filesystem (e.g., overlayfs).
26 * @user_path may be on the stackable filesystem and @real_path on the
27 * underlying filesystem. In this case, we want to be able to return the
28 * @user_path of the stackable filesystem. This is done by embedding the
29 * returned file into a container structure that also stores the stacked
30 * file's path, which can be retrieved using backing_file_user_path().
32 struct file
*backing_file_open(const struct path
*user_path
, int flags
,
33 const struct path
*real_path
,
34 const struct cred
*cred
)
39 f
= alloc_empty_backing_file(flags
, cred
);
44 *backing_file_user_path(f
) = *user_path
;
45 error
= vfs_open(real_path
, f
);
53 EXPORT_SYMBOL_GPL(backing_file_open
);
55 struct file
*backing_tmpfile_open(const struct path
*user_path
, int flags
,
56 const struct path
*real_parentpath
,
57 umode_t mode
, const struct cred
*cred
)
59 struct mnt_idmap
*real_idmap
= mnt_idmap(real_parentpath
->mnt
);
63 f
= alloc_empty_backing_file(flags
, cred
);
68 *backing_file_user_path(f
) = *user_path
;
69 error
= vfs_tmpfile(real_idmap
, real_parentpath
, f
, mode
);
76 EXPORT_SYMBOL(backing_tmpfile_open
);
81 struct kiocb
*orig_iocb
;
82 /* used for aio completion */
83 void (*end_write
)(struct kiocb
*iocb
, ssize_t
);
84 struct work_struct work
;
88 static struct kmem_cache
*backing_aio_cachep
;
90 #define BACKING_IOCB_MASK \
91 (IOCB_NOWAIT | IOCB_HIPRI | IOCB_DSYNC | IOCB_SYNC | IOCB_APPEND)
93 static rwf_t
iocb_to_rw_flags(int flags
)
95 return (__force rwf_t
)(flags
& BACKING_IOCB_MASK
);
98 static void backing_aio_put(struct backing_aio
*aio
)
100 if (refcount_dec_and_test(&aio
->ref
)) {
101 fput(aio
->iocb
.ki_filp
);
102 kmem_cache_free(backing_aio_cachep
, aio
);
106 static void backing_aio_cleanup(struct backing_aio
*aio
, long res
)
108 struct kiocb
*iocb
= &aio
->iocb
;
109 struct kiocb
*orig_iocb
= aio
->orig_iocb
;
111 orig_iocb
->ki_pos
= iocb
->ki_pos
;
113 aio
->end_write(orig_iocb
, res
);
115 backing_aio_put(aio
);
118 static void backing_aio_rw_complete(struct kiocb
*iocb
, long res
)
120 struct backing_aio
*aio
= container_of(iocb
, struct backing_aio
, iocb
);
121 struct kiocb
*orig_iocb
= aio
->orig_iocb
;
123 if (iocb
->ki_flags
& IOCB_WRITE
)
124 kiocb_end_write(iocb
);
126 backing_aio_cleanup(aio
, res
);
127 orig_iocb
->ki_complete(orig_iocb
, res
);
130 static void backing_aio_complete_work(struct work_struct
*work
)
132 struct backing_aio
*aio
= container_of(work
, struct backing_aio
, work
);
134 backing_aio_rw_complete(&aio
->iocb
, aio
->res
);
137 static void backing_aio_queue_completion(struct kiocb
*iocb
, long res
)
139 struct backing_aio
*aio
= container_of(iocb
, struct backing_aio
, iocb
);
142 * Punt to a work queue to serialize updates of mtime/size.
145 INIT_WORK(&aio
->work
, backing_aio_complete_work
);
146 queue_work(file_inode(aio
->orig_iocb
->ki_filp
)->i_sb
->s_dio_done_wq
,
150 static int backing_aio_init_wq(struct kiocb
*iocb
)
152 struct super_block
*sb
= file_inode(iocb
->ki_filp
)->i_sb
;
154 if (sb
->s_dio_done_wq
)
157 return sb_init_dio_done_wq(sb
);
161 ssize_t
backing_file_read_iter(struct file
*file
, struct iov_iter
*iter
,
162 struct kiocb
*iocb
, int flags
,
163 struct backing_file_ctx
*ctx
)
165 struct backing_aio
*aio
= NULL
;
166 const struct cred
*old_cred
;
169 if (WARN_ON_ONCE(!(file
->f_mode
& FMODE_BACKING
)))
172 if (!iov_iter_count(iter
))
175 if (iocb
->ki_flags
& IOCB_DIRECT
&&
176 !(file
->f_mode
& FMODE_CAN_ODIRECT
))
179 old_cred
= override_creds_light(ctx
->cred
);
180 if (is_sync_kiocb(iocb
)) {
181 rwf_t rwf
= iocb_to_rw_flags(flags
);
183 ret
= vfs_iter_read(file
, iter
, &iocb
->ki_pos
, rwf
);
186 aio
= kmem_cache_zalloc(backing_aio_cachep
, GFP_KERNEL
);
190 aio
->orig_iocb
= iocb
;
191 kiocb_clone(&aio
->iocb
, iocb
, get_file(file
));
192 aio
->iocb
.ki_complete
= backing_aio_rw_complete
;
193 refcount_set(&aio
->ref
, 2);
194 ret
= vfs_iocb_iter_read(file
, &aio
->iocb
, iter
);
195 backing_aio_put(aio
);
196 if (ret
!= -EIOCBQUEUED
)
197 backing_aio_cleanup(aio
, ret
);
200 revert_creds_light(old_cred
);
203 ctx
->accessed(iocb
->ki_filp
);
207 EXPORT_SYMBOL_GPL(backing_file_read_iter
);
209 ssize_t
backing_file_write_iter(struct file
*file
, struct iov_iter
*iter
,
210 struct kiocb
*iocb
, int flags
,
211 struct backing_file_ctx
*ctx
)
213 const struct cred
*old_cred
;
216 if (WARN_ON_ONCE(!(file
->f_mode
& FMODE_BACKING
)))
219 if (!iov_iter_count(iter
))
222 ret
= file_remove_privs(iocb
->ki_filp
);
226 if (iocb
->ki_flags
& IOCB_DIRECT
&&
227 !(file
->f_mode
& FMODE_CAN_ODIRECT
))
231 * Stacked filesystems don't support deferred completions, don't copy
232 * this property in case it is set by the issuer.
234 flags
&= ~IOCB_DIO_CALLER_COMP
;
236 old_cred
= override_creds_light(ctx
->cred
);
237 if (is_sync_kiocb(iocb
)) {
238 rwf_t rwf
= iocb_to_rw_flags(flags
);
240 ret
= vfs_iter_write(file
, iter
, &iocb
->ki_pos
, rwf
);
242 ctx
->end_write(iocb
, ret
);
244 struct backing_aio
*aio
;
246 ret
= backing_aio_init_wq(iocb
);
251 aio
= kmem_cache_zalloc(backing_aio_cachep
, GFP_KERNEL
);
255 aio
->orig_iocb
= iocb
;
256 aio
->end_write
= ctx
->end_write
;
257 kiocb_clone(&aio
->iocb
, iocb
, get_file(file
));
258 aio
->iocb
.ki_flags
= flags
;
259 aio
->iocb
.ki_complete
= backing_aio_queue_completion
;
260 refcount_set(&aio
->ref
, 2);
261 ret
= vfs_iocb_iter_write(file
, &aio
->iocb
, iter
);
262 backing_aio_put(aio
);
263 if (ret
!= -EIOCBQUEUED
)
264 backing_aio_cleanup(aio
, ret
);
267 revert_creds_light(old_cred
);
271 EXPORT_SYMBOL_GPL(backing_file_write_iter
);
273 ssize_t
backing_file_splice_read(struct file
*in
, struct kiocb
*iocb
,
274 struct pipe_inode_info
*pipe
, size_t len
,
276 struct backing_file_ctx
*ctx
)
278 const struct cred
*old_cred
;
281 if (WARN_ON_ONCE(!(in
->f_mode
& FMODE_BACKING
)))
284 old_cred
= override_creds_light(ctx
->cred
);
285 ret
= vfs_splice_read(in
, &iocb
->ki_pos
, pipe
, len
, flags
);
286 revert_creds_light(old_cred
);
289 ctx
->accessed(iocb
->ki_filp
);
293 EXPORT_SYMBOL_GPL(backing_file_splice_read
);
295 ssize_t
backing_file_splice_write(struct pipe_inode_info
*pipe
,
296 struct file
*out
, struct kiocb
*iocb
,
297 size_t len
, unsigned int flags
,
298 struct backing_file_ctx
*ctx
)
300 const struct cred
*old_cred
;
303 if (WARN_ON_ONCE(!(out
->f_mode
& FMODE_BACKING
)))
306 if (!out
->f_op
->splice_write
)
309 ret
= file_remove_privs(iocb
->ki_filp
);
313 old_cred
= override_creds_light(ctx
->cred
);
314 file_start_write(out
);
315 ret
= out
->f_op
->splice_write(pipe
, out
, &iocb
->ki_pos
, len
, flags
);
317 revert_creds_light(old_cred
);
320 ctx
->end_write(iocb
, ret
);
324 EXPORT_SYMBOL_GPL(backing_file_splice_write
);
326 int backing_file_mmap(struct file
*file
, struct vm_area_struct
*vma
,
327 struct backing_file_ctx
*ctx
)
329 const struct cred
*old_cred
;
330 struct file
*user_file
= vma
->vm_file
;
333 if (WARN_ON_ONCE(!(file
->f_mode
& FMODE_BACKING
)))
336 if (!file
->f_op
->mmap
)
339 vma_set_file(vma
, file
);
341 old_cred
= override_creds_light(ctx
->cred
);
342 ret
= call_mmap(vma
->vm_file
, vma
);
343 revert_creds_light(old_cred
);
346 ctx
->accessed(user_file
);
350 EXPORT_SYMBOL_GPL(backing_file_mmap
);
352 static int __init
backing_aio_init(void)
354 backing_aio_cachep
= KMEM_CACHE(backing_aio
, SLAB_HWCACHE_ALIGN
);
355 if (!backing_aio_cachep
)
360 fs_initcall(backing_aio_init
);