| 1 | |
| 2 | |
| 3 | * Common helpers for stackable filesystems and backing files.
|
| 4 | *
|
| 5 | * Forked from fs/overlayfs/file.c.
|
| 6 | *
|
| 7 | * Copyright (C) 2017 Red Hat, Inc.
|
| 8 | * Copyright (C) 2023 CTERA Networks.
|
| 9 |
|
| 10 |
|
| 11 | #include <linux/fs.h>
|
| 12 | #include <linux/backing-file.h>
|
| 13 | #include <linux/splice.h>
|
| 14 | #include <linux/mm.h>
|
| 15 | #include <linux/security.h>
|
| 16 |
|
| 17 | #include "internal.h"
|
| 18 |
|
| 19 | |
| 20 | * backing_file_open - open a backing file for kernel internal use
|
| 21 | * @user_file: file the user requested to open
|
| 22 | * @flags: open flags
|
| 23 | * @real_path: path of the backing file
|
| 24 | * @cred: credentials for open
|
| 25 | *
|
| 26 | * Open a backing file for a stackable filesystem (e.g., overlayfs).
|
| 27 | * @user_file->f_path may be on the stackable filesystem and @real_path
|
| 28 | * on the underlying filesystem. In this case, we want to be able to
|
| 29 | * return the path of the stackable filesystem. This is done by
|
| 30 | * embedding the returned file into a container structure that also
|
| 31 | * stores the stacked file's path, which can be retrieved using
|
| 32 | * backing_file_user_path().
|
| 33 |
|
| 34 | struct file *backing_file_open(const struct file *user_file, int flags,
|
| 35 | const struct path *real_path,
|
| 36 | const struct cred *cred)
|
| 37 | {
|
| 38 | const struct path *user_path = file_user_path(user_file);
|
| 39 | struct file *f;
|
| 40 | int error;
|
| 41 |
|
| 42 | f = alloc_empty_backing_file(flags, cred, user_file);
|
| 43 | if (IS_ERR(f))
|
| 44 | return f;
|
| 45 |
|
| 46 | path_get(user_path);
|
| 47 | backing_file_set_user_path(f, user_path);
|
| 48 | error = vfs_open(real_path, f);
|
| 49 | if (error) {
|
| 50 | fput(f);
|
| 51 | f = ERR_PTR(error);
|
| 52 | }
|
| 53 |
|
| 54 | return f;
|
| 55 | }
|
| 56 | EXPORT_SYMBOL_GPL(backing_file_open);
|
| 57 |
|
| 58 | struct file *backing_tmpfile_open(const struct file *user_file, int flags,
|
| 59 | const struct path *real_parentpath,
|
| 60 | umode_t mode, const struct cred *cred)
|
| 61 | {
|
| 62 | const struct mnt_idmap *real_idmap = mnt_idmap(real_parentpath->mnt);
|
| 63 | const struct path *user_path = &user_file->f_path;
|
| 64 | struct file *f;
|
| 65 | int error;
|
| 66 |
|
| 67 | f = alloc_empty_backing_file(flags, cred, user_file);
|
| 68 | if (IS_ERR(f))
|
| 69 | return f;
|
| 70 |
|
| 71 | path_get(user_path);
|
| 72 | backing_file_set_user_path(f, user_path);
|
| 73 | error = vfs_tmpfile(real_idmap, real_parentpath, f, mode);
|
| 74 | if (error) {
|
| 75 | fput(f);
|
| 76 | f = ERR_PTR(error);
|
| 77 | }
|
| 78 | return f;
|
| 79 | }
|
| 80 | EXPORT_SYMBOL(backing_tmpfile_open);
|
| 81 |
|
| 82 | struct backing_aio {
|
| 83 | struct kiocb iocb;
|
| 84 | refcount_t ref;
|
| 85 | struct kiocb *orig_iocb;
|
| 86 |
|
| 87 | void (*end_write)(struct kiocb *iocb, ssize_t);
|
| 88 | struct work_struct work;
|
| 89 | long res;
|
| 90 | };
|
| 91 |
|
| 92 | static struct kmem_cache *backing_aio_cachep;
|
| 93 |
|
| 94 | #define BACKING_IOCB_MASK \
|
| 95 | (IOCB_NOWAIT | IOCB_HIPRI | IOCB_DSYNC | IOCB_SYNC | IOCB_APPEND)
|
| 96 |
|
| 97 | static rwf_t iocb_to_rw_flags(int flags)
|
| 98 | {
|
| 99 | return (__force rwf_t)(flags & BACKING_IOCB_MASK);
|
| 100 | }
|
| 101 |
|
| 102 | static void backing_aio_put(struct backing_aio *aio)
|
| 103 | {
|
| 104 | if (refcount_dec_and_test(&aio->ref)) {
|
| 105 | fput(aio->iocb.ki_filp);
|
| 106 | kmem_cache_free(backing_aio_cachep, aio);
|
| 107 | }
|
| 108 | }
|
| 109 |
|
| 110 | static void backing_aio_cleanup(struct backing_aio *aio, long res)
|
| 111 | {
|
| 112 | struct kiocb *iocb = &aio->iocb;
|
| 113 | struct kiocb *orig_iocb = aio->orig_iocb;
|
| 114 |
|
| 115 | orig_iocb->ki_pos = iocb->ki_pos;
|
| 116 | if (aio->end_write)
|
| 117 | aio->end_write(orig_iocb, res);
|
| 118 |
|
| 119 | backing_aio_put(aio);
|
| 120 | }
|
| 121 |
|
| 122 | static void backing_aio_rw_complete(struct kiocb *iocb, long res)
|
| 123 | {
|
| 124 | struct backing_aio *aio = container_of(iocb, struct backing_aio, iocb);
|
| 125 | struct kiocb *orig_iocb = aio->orig_iocb;
|
| 126 |
|
| 127 | if (iocb->ki_flags & IOCB_WRITE)
|
| 128 | kiocb_end_write(iocb);
|
| 129 |
|
| 130 | backing_aio_cleanup(aio, res);
|
| 131 | orig_iocb->ki_complete(orig_iocb, res);
|
| 132 | }
|
| 133 |
|
| 134 | static void backing_aio_complete_work(struct work_struct *work)
|
| 135 | {
|
| 136 | struct backing_aio *aio = container_of(work, struct backing_aio, work);
|
| 137 |
|
| 138 | backing_aio_rw_complete(&aio->iocb, aio->res);
|
| 139 | }
|
| 140 |
|
| 141 | static void backing_aio_queue_completion(struct kiocb *iocb, long res)
|
| 142 | {
|
| 143 | struct backing_aio *aio = container_of(iocb, struct backing_aio, iocb);
|
| 144 |
|
| 145 | |
| 146 | * Punt to a work queue to serialize updates of mtime/size.
|
| 147 |
|
| 148 | aio->res = res;
|
| 149 | INIT_WORK(&aio->work, backing_aio_complete_work);
|
| 150 | queue_work(file_inode(aio->orig_iocb->ki_filp)->i_sb->s_dio_done_wq,
|
| 151 | &aio->work);
|
| 152 | }
|
| 153 |
|
| 154 | static int backing_aio_init_wq(struct kiocb *iocb)
|
| 155 | {
|
| 156 | struct super_block *sb = file_inode(iocb->ki_filp)->i_sb;
|
| 157 |
|
| 158 | if (sb->s_dio_done_wq)
|
| 159 | return 0;
|
| 160 |
|
| 161 | return sb_init_dio_done_wq(sb);
|
| 162 | }
|
| 163 |
|
| 164 | static int do_backing_file_read_iter(struct file *file, struct iov_iter *iter,
|
| 165 | struct kiocb *iocb, int flags)
|
| 166 | {
|
| 167 | struct backing_aio *aio = NULL;
|
| 168 | int ret;
|
| 169 |
|
| 170 | if (is_sync_kiocb(iocb)) {
|
| 171 | rwf_t rwf = iocb_to_rw_flags(flags);
|
| 172 |
|
| 173 | return vfs_iter_read(file, iter, &iocb->ki_pos, rwf);
|
| 174 | }
|
| 175 |
|
| 176 | aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
|
| 177 | if (!aio)
|
| 178 | return -ENOMEM;
|
| 179 |
|
| 180 | aio->orig_iocb = iocb;
|
| 181 | kiocb_clone(&aio->iocb, iocb, get_file(file));
|
| 182 | aio->iocb.ki_complete = backing_aio_rw_complete;
|
| 183 | refcount_set(&aio->ref, 2);
|
| 184 | ret = vfs_iocb_iter_read(file, &aio->iocb, iter);
|
| 185 | backing_aio_put(aio);
|
| 186 | if (ret != -EIOCBQUEUED)
|
| 187 | backing_aio_cleanup(aio, ret);
|
| 188 | return ret;
|
| 189 | }
|
| 190 |
|
| 191 | ssize_t backing_file_read_iter(struct file *file, struct iov_iter *iter,
|
| 192 | struct kiocb *iocb, int flags,
|
| 193 | struct backing_file_ctx *ctx)
|
| 194 | {
|
| 195 | ssize_t ret;
|
| 196 |
|
| 197 | if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
|
| 198 | return -EIO;
|
| 199 |
|
| 200 | if (!iov_iter_count(iter))
|
| 201 | return 0;
|
| 202 |
|
| 203 | if (iocb->ki_flags & IOCB_DIRECT &&
|
| 204 | !(file->f_mode & FMODE_CAN_ODIRECT))
|
| 205 | return -EINVAL;
|
| 206 |
|
| 207 | scoped_with_creds(ctx->cred)
|
| 208 | ret = do_backing_file_read_iter(file, iter, iocb, flags);
|
| 209 |
|
| 210 | if (ctx->accessed)
|
| 211 | ctx->accessed(iocb->ki_filp);
|
| 212 |
|
| 213 | return ret;
|
| 214 | }
|
| 215 | EXPORT_SYMBOL_GPL(backing_file_read_iter);
|
| 216 |
|
| 217 | static int do_backing_file_write_iter(struct file *file, struct iov_iter *iter,
|
| 218 | struct kiocb *iocb, int flags,
|
| 219 | void (*end_write)(struct kiocb *, ssize_t))
|
| 220 | {
|
| 221 | struct backing_aio *aio;
|
| 222 | int ret;
|
| 223 |
|
| 224 | if (is_sync_kiocb(iocb)) {
|
| 225 | rwf_t rwf = iocb_to_rw_flags(flags);
|
| 226 |
|
| 227 | ret = vfs_iter_write(file, iter, &iocb->ki_pos, rwf);
|
| 228 | if (end_write)
|
| 229 | end_write(iocb, ret);
|
| 230 | return ret;
|
| 231 | }
|
| 232 |
|
| 233 | ret = backing_aio_init_wq(iocb);
|
| 234 | if (ret)
|
| 235 | return ret;
|
| 236 |
|
| 237 | aio = kmem_cache_zalloc(backing_aio_cachep, GFP_KERNEL);
|
| 238 | if (!aio)
|
| 239 | return -ENOMEM;
|
| 240 |
|
| 241 | aio->orig_iocb = iocb;
|
| 242 | aio->end_write = end_write;
|
| 243 | kiocb_clone(&aio->iocb, iocb, get_file(file));
|
| 244 | aio->iocb.ki_flags = flags;
|
| 245 | aio->iocb.ki_complete = backing_aio_queue_completion;
|
| 246 | refcount_set(&aio->ref, 2);
|
| 247 | ret = vfs_iocb_iter_write(file, &aio->iocb, iter);
|
| 248 | backing_aio_put(aio);
|
| 249 | if (ret != -EIOCBQUEUED)
|
| 250 | backing_aio_cleanup(aio, ret);
|
| 251 | return ret;
|
| 252 | }
|
| 253 |
|
| 254 | ssize_t backing_file_write_iter(struct file *file, struct iov_iter *iter,
|
| 255 | struct kiocb *iocb, int flags,
|
| 256 | struct backing_file_ctx *ctx)
|
| 257 | {
|
| 258 | ssize_t ret;
|
| 259 |
|
| 260 | if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
|
| 261 | return -EIO;
|
| 262 |
|
| 263 | if (!iov_iter_count(iter))
|
| 264 | return 0;
|
| 265 |
|
| 266 | ret = file_remove_privs(iocb->ki_filp);
|
| 267 | if (ret)
|
| 268 | return ret;
|
| 269 |
|
| 270 | if (iocb->ki_flags & IOCB_DIRECT &&
|
| 271 | !(file->f_mode & FMODE_CAN_ODIRECT))
|
| 272 | return -EINVAL;
|
| 273 |
|
| 274 | scoped_with_creds(ctx->cred)
|
| 275 | return do_backing_file_write_iter(file, iter, iocb, flags, ctx->end_write);
|
| 276 | }
|
| 277 | EXPORT_SYMBOL_GPL(backing_file_write_iter);
|
| 278 |
|
| 279 | ssize_t backing_file_splice_read(struct file *in, struct kiocb *iocb,
|
| 280 | struct pipe_inode_info *pipe, size_t len,
|
| 281 | unsigned int flags,
|
| 282 | struct backing_file_ctx *ctx)
|
| 283 | {
|
| 284 | ssize_t ret;
|
| 285 |
|
| 286 | if (WARN_ON_ONCE(!(in->f_mode & FMODE_BACKING)))
|
| 287 | return -EIO;
|
| 288 |
|
| 289 | scoped_with_creds(ctx->cred)
|
| 290 | ret = vfs_splice_read(in, &iocb->ki_pos, pipe, len, flags);
|
| 291 |
|
| 292 | if (ctx->accessed)
|
| 293 | ctx->accessed(iocb->ki_filp);
|
| 294 |
|
| 295 | return ret;
|
| 296 | }
|
| 297 | EXPORT_SYMBOL_GPL(backing_file_splice_read);
|
| 298 |
|
| 299 | ssize_t backing_file_splice_write(struct pipe_inode_info *pipe,
|
| 300 | struct file *out, struct kiocb *iocb,
|
| 301 | size_t len, unsigned int flags,
|
| 302 | struct backing_file_ctx *ctx)
|
| 303 | {
|
| 304 | ssize_t ret;
|
| 305 |
|
| 306 | if (WARN_ON_ONCE(!(out->f_mode & FMODE_BACKING)))
|
| 307 | return -EIO;
|
| 308 |
|
| 309 | if (!out->f_op->splice_write)
|
| 310 | return -EINVAL;
|
| 311 |
|
| 312 | ret = file_remove_privs(iocb->ki_filp);
|
| 313 | if (ret)
|
| 314 | return ret;
|
| 315 |
|
| 316 | scoped_with_creds(ctx->cred) {
|
| 317 | file_start_write(out);
|
| 318 | ret = out->f_op->splice_write(pipe, out, &iocb->ki_pos, len, flags);
|
| 319 | file_end_write(out);
|
| 320 | }
|
| 321 |
|
| 322 | if (ctx->end_write)
|
| 323 | ctx->end_write(iocb, ret);
|
| 324 |
|
| 325 | return ret;
|
| 326 | }
|
| 327 | EXPORT_SYMBOL_GPL(backing_file_splice_write);
|
| 328 |
|
| 329 | int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
|
| 330 | struct backing_file_ctx *ctx)
|
| 331 | {
|
| 332 | struct file *user_file = vma->vm_file;
|
| 333 | int ret;
|
| 334 |
|
| 335 | if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)))
|
| 336 | return -EIO;
|
| 337 |
|
| 338 | if (!can_mmap_file(file))
|
| 339 | return -ENODEV;
|
| 340 |
|
| 341 | vma_set_file(vma, file);
|
| 342 |
|
| 343 | scoped_with_creds(ctx->cred) {
|
| 344 | ret = security_mmap_backing_file(vma, file, user_file);
|
| 345 | if (ret)
|
| 346 | return ret;
|
| 347 |
|
| 348 | ret = vfs_mmap(vma->vm_file, vma);
|
| 349 | }
|
| 350 |
|
| 351 | if (ctx->accessed)
|
| 352 | ctx->accessed(user_file);
|
| 353 |
|
| 354 | return ret;
|
| 355 | }
|
| 356 | EXPORT_SYMBOL_GPL(backing_file_mmap);
|
| 357 |
|
| 358 | static int __init backing_aio_init(void)
|
| 359 | {
|
| 360 | backing_aio_cachep = KMEM_CACHE(backing_aio, SLAB_HWCACHE_ALIGN);
|
| 361 | if (!backing_aio_cachep)
|
| 362 | return -ENOMEM;
|
| 363 |
|
| 364 | return 0;
|
| 365 | }
|
| 366 | fs_initcall(backing_aio_init);
|