ALSA: pcm: Fix missing check of the new non-cached buffer type
[linux-2.6-block.git] / fs / iomap / apply.c
CommitLineData
73ce6aba 1// SPDX-License-Identifier: GPL-2.0
ae259a9c
CH
2/*
3 * Copyright (C) 2010 Red Hat, Inc.
72b4daa2 4 * Copyright (c) 2016-2018 Christoph Hellwig.
ae259a9c
CH
5 */
6#include <linux/module.h>
7#include <linux/compiler.h>
8#include <linux/fs.h>
9#include <linux/iomap.h>
f361bf4a 10
ae259a9c
CH
11/*
12 * Execute a iomap write on a segment of the mapping that spans a
13 * contiguous range of pages that have identical block mapping state.
14 *
15 * This avoids the need to map pages individually, do individual allocations
16 * for each page and most importantly avoid the need for filesystem specific
17 * locking per page. Instead, all the operations are amortised over the entire
18 * range of pages. It is assumed that the filesystems will lock whatever
19 * resources they require in the iomap_begin call, and release them in the
20 * iomap_end call.
21 */
befb503c 22loff_t
ae259a9c 23iomap_apply(struct inode *inode, loff_t pos, loff_t length, unsigned flags,
8ff6daa1 24 const struct iomap_ops *ops, void *data, iomap_actor_t actor)
ae259a9c
CH
25{
26 struct iomap iomap = { 0 };
27 loff_t written = 0, ret;
28
29 /*
30 * Need to map a range from start position for length bytes. This can
31 * span multiple pages - it is only guaranteed to return a range of a
32 * single type of pages (e.g. all into a hole, all mapped or all
33 * unwritten). Failure at this point has nothing to undo.
34 *
35 * If allocation is required for this range, reserve the space now so
36 * that the allocation is guaranteed to succeed later on. Once we copy
37 * the data into the page cache pages, then we cannot fail otherwise we
38 * expose transient stale data. If the reserve fails, we can safely
39 * back out at this point as there is nothing to undo.
40 */
41 ret = ops->iomap_begin(inode, pos, length, flags, &iomap);
42 if (ret)
43 return ret;
44 if (WARN_ON(iomap.offset > pos))
45 return -EIO;
0c6dda7a
DW
46 if (WARN_ON(iomap.length == 0))
47 return -EIO;
ae259a9c
CH
48
49 /*
50 * Cut down the length to the one actually provided by the filesystem,
51 * as it might not be able to give us the whole size that we requested.
52 */
53 if (iomap.offset + iomap.length < pos + length)
54 length = iomap.offset + iomap.length - pos;
55
56 /*
57 * Now that we have guaranteed that the space allocation will succeed.
58 * we can do the copy-in page by page without having to worry about
59 * failures exposing transient data.
60 */
61 written = actor(inode, pos, length, data, &iomap);
62
63 /*
64 * Now the data has been copied, commit the range we've copied. This
65 * should not fail unless the filesystem has had a fatal error.
66 */
f20ac7ab
CH
67 if (ops->iomap_end) {
68 ret = ops->iomap_end(inode, pos, length,
69 written > 0 ? written : 0,
70 flags, &iomap);
71 }
ae259a9c
CH
72
73 return written ? written : ret;
74}