| 1 | // SPDX-License-Identifier: GPL-2.0-or-later | 
|---|
| 2 | /* Unbuffered and direct write support. | 
|---|
| 3 | * | 
|---|
| 4 | * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. | 
|---|
| 5 | * Written by David Howells (dhowells@redhat.com) | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include <linux/export.h> | 
|---|
| 9 | #include <linux/uio.h> | 
|---|
| 10 | #include "internal.h" | 
|---|
| 11 |  | 
|---|
| 12 | /* | 
|---|
| 13 | * Perform an unbuffered write where we may have to do an RMW operation on an | 
|---|
| 14 | * encrypted file.  This can also be used for direct I/O writes. | 
|---|
| 15 | */ | 
|---|
| 16 | ssize_t netfs_unbuffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *iter, | 
|---|
| 17 | struct netfs_group *netfs_group) | 
|---|
| 18 | { | 
|---|
| 19 | struct netfs_io_request *wreq; | 
|---|
| 20 | unsigned long long start = iocb->ki_pos; | 
|---|
| 21 | unsigned long long end = start + iov_iter_count(i: iter); | 
|---|
| 22 | ssize_t ret, n; | 
|---|
| 23 | size_t len = iov_iter_count(i: iter); | 
|---|
| 24 | bool async = !is_sync_kiocb(kiocb: iocb); | 
|---|
| 25 |  | 
|---|
| 26 | _enter( ""); | 
|---|
| 27 |  | 
|---|
| 28 | /* We're going to need a bounce buffer if what we transmit is going to | 
|---|
| 29 | * be different in some way to the source buffer, e.g. because it gets | 
|---|
| 30 | * encrypted/compressed or because it needs expanding to a block size. | 
|---|
| 31 | */ | 
|---|
| 32 | // TODO | 
|---|
| 33 |  | 
|---|
| 34 | _debug( "uw %llx-%llx", start, end); | 
|---|
| 35 |  | 
|---|
| 36 | wreq = netfs_create_write_req(mapping: iocb->ki_filp->f_mapping, file: iocb->ki_filp, start, | 
|---|
| 37 | origin: iocb->ki_flags & IOCB_DIRECT ? | 
|---|
| 38 | NETFS_DIO_WRITE : NETFS_UNBUFFERED_WRITE); | 
|---|
| 39 | if (IS_ERR(ptr: wreq)) | 
|---|
| 40 | return PTR_ERR(ptr: wreq); | 
|---|
| 41 |  | 
|---|
| 42 | wreq->io_streams[0].avail = true; | 
|---|
| 43 | trace_netfs_write(wreq, what: (iocb->ki_flags & IOCB_DIRECT ? | 
|---|
| 44 | netfs_write_trace_dio_write : | 
|---|
| 45 | netfs_write_trace_unbuffered_write)); | 
|---|
| 46 |  | 
|---|
| 47 | { | 
|---|
| 48 | /* If this is an async op and we're not using a bounce buffer, | 
|---|
| 49 | * we have to save the source buffer as the iterator is only | 
|---|
| 50 | * good until we return.  In such a case, extract an iterator | 
|---|
| 51 | * to represent as much of the the output buffer as we can | 
|---|
| 52 | * manage.  Note that the extraction might not be able to | 
|---|
| 53 | * allocate a sufficiently large bvec array and may shorten the | 
|---|
| 54 | * request. | 
|---|
| 55 | */ | 
|---|
| 56 | if (user_backed_iter(i: iter)) { | 
|---|
| 57 | n = netfs_extract_user_iter(orig: iter, orig_len: len, new: &wreq->buffer.iter, extraction_flags: 0); | 
|---|
| 58 | if (n < 0) { | 
|---|
| 59 | ret = n; | 
|---|
| 60 | goto error_put; | 
|---|
| 61 | } | 
|---|
| 62 | wreq->direct_bv = (struct bio_vec *)wreq->buffer.iter.bvec; | 
|---|
| 63 | wreq->direct_bv_count = n; | 
|---|
| 64 | wreq->direct_bv_unpin = iov_iter_extract_will_pin(iter); | 
|---|
| 65 | } else { | 
|---|
| 66 | /* If this is a kernel-generated async DIO request, | 
|---|
| 67 | * assume that any resources the iterator points to | 
|---|
| 68 | * (eg. a bio_vec array) will persist till the end of | 
|---|
| 69 | * the op. | 
|---|
| 70 | */ | 
|---|
| 71 | wreq->buffer.iter = *iter; | 
|---|
| 72 | } | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 | __set_bit(NETFS_RREQ_USE_IO_ITER, &wreq->flags); | 
|---|
| 76 | if (async) | 
|---|
| 77 | __set_bit(NETFS_RREQ_OFFLOAD_COLLECTION, &wreq->flags); | 
|---|
| 78 |  | 
|---|
| 79 | /* Copy the data into the bounce buffer and encrypt it. */ | 
|---|
| 80 | // TODO | 
|---|
| 81 |  | 
|---|
| 82 | /* Dispatch the write. */ | 
|---|
| 83 | __set_bit(NETFS_RREQ_UPLOAD_TO_SERVER, &wreq->flags); | 
|---|
| 84 | if (async) | 
|---|
| 85 | wreq->iocb = iocb; | 
|---|
| 86 | wreq->len = iov_iter_count(i: &wreq->buffer.iter); | 
|---|
| 87 | ret = netfs_unbuffered_write(wreq, may_wait: is_sync_kiocb(kiocb: iocb), len: wreq->len); | 
|---|
| 88 | if (ret < 0) { | 
|---|
| 89 | _debug( "begin = %zd", ret); | 
|---|
| 90 | goto out; | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | if (!async) { | 
|---|
| 94 | ret = netfs_wait_for_write(rreq: wreq); | 
|---|
| 95 | if (ret > 0) | 
|---|
| 96 | iocb->ki_pos += ret; | 
|---|
| 97 | } else { | 
|---|
| 98 | ret = -EIOCBQUEUED; | 
|---|
| 99 | } | 
|---|
| 100 |  | 
|---|
| 101 | out: | 
|---|
| 102 | netfs_put_request(rreq: wreq, what: netfs_rreq_trace_put_return); | 
|---|
| 103 | return ret; | 
|---|
| 104 |  | 
|---|
| 105 | error_put: | 
|---|
| 106 | netfs_put_failed_request(rreq: wreq); | 
|---|
| 107 | return ret; | 
|---|
| 108 | } | 
|---|
| 109 | EXPORT_SYMBOL(netfs_unbuffered_write_iter_locked); | 
|---|
| 110 |  | 
|---|
| 111 | /** | 
|---|
| 112 | * netfs_unbuffered_write_iter - Unbuffered write to a file | 
|---|
| 113 | * @iocb: IO state structure | 
|---|
| 114 | * @from: iov_iter with data to write | 
|---|
| 115 | * | 
|---|
| 116 | * Do an unbuffered write to a file, writing the data directly to the server | 
|---|
| 117 | * and not lodging the data in the pagecache. | 
|---|
| 118 | * | 
|---|
| 119 | * Return: | 
|---|
| 120 | * * Negative error code if no data has been written at all of | 
|---|
| 121 | *   vfs_fsync_range() failed for a synchronous write | 
|---|
| 122 | * * Number of bytes written, even for truncated writes | 
|---|
| 123 | */ | 
|---|
| 124 | ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from) | 
|---|
| 125 | { | 
|---|
| 126 | struct file *file = iocb->ki_filp; | 
|---|
| 127 | struct address_space *mapping = file->f_mapping; | 
|---|
| 128 | struct inode *inode = mapping->host; | 
|---|
| 129 | struct netfs_inode *ictx = netfs_inode(inode); | 
|---|
| 130 | ssize_t ret; | 
|---|
| 131 | loff_t pos = iocb->ki_pos; | 
|---|
| 132 | unsigned long long end = pos + iov_iter_count(i: from) - 1; | 
|---|
| 133 |  | 
|---|
| 134 | _enter( "%llx,%zx,%llx", pos, iov_iter_count(from), i_size_read(inode)); | 
|---|
| 135 |  | 
|---|
| 136 | if (!iov_iter_count(i: from)) | 
|---|
| 137 | return 0; | 
|---|
| 138 |  | 
|---|
| 139 | trace_netfs_write_iter(iocb, from); | 
|---|
| 140 | netfs_stat(&netfs_n_wh_dio_write); | 
|---|
| 141 |  | 
|---|
| 142 | ret = netfs_start_io_direct(inode); | 
|---|
| 143 | if (ret < 0) | 
|---|
| 144 | return ret; | 
|---|
| 145 | ret = generic_write_checks(iocb, from); | 
|---|
| 146 | if (ret <= 0) | 
|---|
| 147 | goto out; | 
|---|
| 148 | ret = file_remove_privs(file); | 
|---|
| 149 | if (ret < 0) | 
|---|
| 150 | goto out; | 
|---|
| 151 | ret = file_update_time(file); | 
|---|
| 152 | if (ret < 0) | 
|---|
| 153 | goto out; | 
|---|
| 154 | if (iocb->ki_flags & IOCB_NOWAIT) { | 
|---|
| 155 | /* We could block if there are any pages in the range. */ | 
|---|
| 156 | ret = -EAGAIN; | 
|---|
| 157 | if (filemap_range_has_page(mapping, lstart: pos, lend: end)) | 
|---|
| 158 | if (filemap_invalidate_inode(inode, flush: true, start: pos, end)) | 
|---|
| 159 | goto out; | 
|---|
| 160 | } else { | 
|---|
| 161 | ret = filemap_write_and_wait_range(mapping, lstart: pos, lend: end); | 
|---|
| 162 | if (ret < 0) | 
|---|
| 163 | goto out; | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | /* | 
|---|
| 167 | * After a write we want buffered reads to be sure to go to disk to get | 
|---|
| 168 | * the new data.  We invalidate clean cached page from the region we're | 
|---|
| 169 | * about to write.  We do this *before* the write so that we can return | 
|---|
| 170 | * without clobbering -EIOCBQUEUED from ->direct_IO(). | 
|---|
| 171 | */ | 
|---|
| 172 | ret = filemap_invalidate_inode(inode, flush: true, start: pos, end); | 
|---|
| 173 | if (ret < 0) | 
|---|
| 174 | goto out; | 
|---|
| 175 | end = iocb->ki_pos + iov_iter_count(i: from); | 
|---|
| 176 | if (end > ictx->zero_point) | 
|---|
| 177 | ictx->zero_point = end; | 
|---|
| 178 |  | 
|---|
| 179 | fscache_invalidate(cookie: netfs_i_cookie(ctx: ictx), NULL, size: i_size_read(inode), | 
|---|
| 180 | FSCACHE_INVAL_DIO_WRITE); | 
|---|
| 181 | ret = netfs_unbuffered_write_iter_locked(iocb, from, NULL); | 
|---|
| 182 | out: | 
|---|
| 183 | netfs_end_io_direct(inode); | 
|---|
| 184 | return ret; | 
|---|
| 185 | } | 
|---|
| 186 | EXPORT_SYMBOL(netfs_unbuffered_write_iter); | 
|---|
| 187 |  | 
|---|