printf: Remove unused 'bprintf'
[drm/drm-misc.git] / fs / netfs / direct_write.c
blob88f2adfab75e926969a8fae3c4bf65051644f0ed
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Unbuffered and direct write support.
4 * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
6 */
8 #include <linux/export.h>
9 #include <linux/uio.h>
10 #include "internal.h"
12 static void netfs_cleanup_dio_write(struct netfs_io_request *wreq)
14 struct inode *inode = wreq->inode;
15 unsigned long long end = wreq->start + wreq->transferred;
17 if (!wreq->error &&
18 i_size_read(inode) < end) {
19 if (wreq->netfs_ops->update_i_size)
20 wreq->netfs_ops->update_i_size(inode, end);
21 else
22 i_size_write(inode, end);
27 * Perform an unbuffered write where we may have to do an RMW operation on an
28 * encrypted file. This can also be used for direct I/O writes.
30 ssize_t netfs_unbuffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *iter,
31 struct netfs_group *netfs_group)
33 struct netfs_io_request *wreq;
34 unsigned long long start = iocb->ki_pos;
35 unsigned long long end = start + iov_iter_count(iter);
36 ssize_t ret, n;
37 size_t len = iov_iter_count(iter);
38 bool async = !is_sync_kiocb(iocb);
40 _enter("");
42 /* We're going to need a bounce buffer if what we transmit is going to
43 * be different in some way to the source buffer, e.g. because it gets
44 * encrypted/compressed or because it needs expanding to a block size.
46 // TODO
48 _debug("uw %llx-%llx", start, end);
50 wreq = netfs_create_write_req(iocb->ki_filp->f_mapping, iocb->ki_filp, start,
51 iocb->ki_flags & IOCB_DIRECT ?
52 NETFS_DIO_WRITE : NETFS_UNBUFFERED_WRITE);
53 if (IS_ERR(wreq))
54 return PTR_ERR(wreq);
56 wreq->io_streams[0].avail = true;
57 trace_netfs_write(wreq, (iocb->ki_flags & IOCB_DIRECT ?
58 netfs_write_trace_dio_write :
59 netfs_write_trace_unbuffered_write));
62 /* If this is an async op and we're not using a bounce buffer,
63 * we have to save the source buffer as the iterator is only
64 * good until we return. In such a case, extract an iterator
65 * to represent as much of the the output buffer as we can
66 * manage. Note that the extraction might not be able to
67 * allocate a sufficiently large bvec array and may shorten the
68 * request.
70 if (async || user_backed_iter(iter)) {
71 n = netfs_extract_user_iter(iter, len, &wreq->iter, 0);
72 if (n < 0) {
73 ret = n;
74 goto out;
76 wreq->direct_bv = (struct bio_vec *)wreq->iter.bvec;
77 wreq->direct_bv_count = n;
78 wreq->direct_bv_unpin = iov_iter_extract_will_pin(iter);
79 } else {
80 wreq->iter = *iter;
83 wreq->io_iter = wreq->iter;
86 __set_bit(NETFS_RREQ_USE_IO_ITER, &wreq->flags);
88 /* Copy the data into the bounce buffer and encrypt it. */
89 // TODO
91 /* Dispatch the write. */
92 __set_bit(NETFS_RREQ_UPLOAD_TO_SERVER, &wreq->flags);
93 if (async)
94 wreq->iocb = iocb;
95 wreq->len = iov_iter_count(&wreq->io_iter);
96 wreq->cleanup = netfs_cleanup_dio_write;
97 ret = netfs_unbuffered_write(wreq, is_sync_kiocb(iocb), wreq->len);
98 if (ret < 0) {
99 _debug("begin = %zd", ret);
100 goto out;
103 if (!async) {
104 trace_netfs_rreq(wreq, netfs_rreq_trace_wait_ip);
105 wait_on_bit(&wreq->flags, NETFS_RREQ_IN_PROGRESS,
106 TASK_UNINTERRUPTIBLE);
107 smp_rmb(); /* Read error/transferred after RIP flag */
108 ret = wreq->error;
109 if (ret == 0) {
110 ret = wreq->transferred;
111 iocb->ki_pos += ret;
113 } else {
114 ret = -EIOCBQUEUED;
117 out:
118 netfs_put_request(wreq, false, netfs_rreq_trace_put_return);
119 return ret;
121 EXPORT_SYMBOL(netfs_unbuffered_write_iter_locked);
124 * netfs_unbuffered_write_iter - Unbuffered write to a file
125 * @iocb: IO state structure
126 * @from: iov_iter with data to write
128 * Do an unbuffered write to a file, writing the data directly to the server
129 * and not lodging the data in the pagecache.
131 * Return:
132 * * Negative error code if no data has been written at all of
133 * vfs_fsync_range() failed for a synchronous write
134 * * Number of bytes written, even for truncated writes
136 ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from)
138 struct file *file = iocb->ki_filp;
139 struct address_space *mapping = file->f_mapping;
140 struct inode *inode = mapping->host;
141 struct netfs_inode *ictx = netfs_inode(inode);
142 ssize_t ret;
143 loff_t pos = iocb->ki_pos;
144 unsigned long long end = pos + iov_iter_count(from) - 1;
146 _enter("%llx,%zx,%llx", pos, iov_iter_count(from), i_size_read(inode));
148 if (!iov_iter_count(from))
149 return 0;
151 trace_netfs_write_iter(iocb, from);
152 netfs_stat(&netfs_n_wh_dio_write);
154 ret = netfs_start_io_direct(inode);
155 if (ret < 0)
156 return ret;
157 ret = generic_write_checks(iocb, from);
158 if (ret <= 0)
159 goto out;
160 ret = file_remove_privs(file);
161 if (ret < 0)
162 goto out;
163 ret = file_update_time(file);
164 if (ret < 0)
165 goto out;
166 if (iocb->ki_flags & IOCB_NOWAIT) {
167 /* We could block if there are any pages in the range. */
168 ret = -EAGAIN;
169 if (filemap_range_has_page(mapping, pos, end))
170 if (filemap_invalidate_inode(inode, true, pos, end))
171 goto out;
172 } else {
173 ret = filemap_write_and_wait_range(mapping, pos, end);
174 if (ret < 0)
175 goto out;
179 * After a write we want buffered reads to be sure to go to disk to get
180 * the new data. We invalidate clean cached page from the region we're
181 * about to write. We do this *before* the write so that we can return
182 * without clobbering -EIOCBQUEUED from ->direct_IO().
184 ret = filemap_invalidate_inode(inode, true, pos, end);
185 if (ret < 0)
186 goto out;
187 end = iocb->ki_pos + iov_iter_count(from);
188 if (end > ictx->zero_point)
189 ictx->zero_point = end;
191 fscache_invalidate(netfs_i_cookie(ictx), NULL, i_size_read(inode),
192 FSCACHE_INVAL_DIO_WRITE);
193 ret = netfs_unbuffered_write_iter_locked(iocb, from, NULL);
194 out:
195 netfs_end_io_direct(inode);
196 return ret;
198 EXPORT_SYMBOL(netfs_unbuffered_write_iter);