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 static void netfs_cleanup_dio_write(struct netfs_io_request *wreq) 13 { 14 struct inode *inode = wreq->inode; 15 unsigned long long end = wreq->start + wreq->transferred; 16 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); 23 } 24 } 25 26 /* 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. 29 */ 30 ssize_t netfs_unbuffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *iter, 31 struct netfs_group *netfs_group) 32 { 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); 39 40 _enter(""); 41 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. 45 */ 46 // TODO 47 48 _debug("uw %llx-%llx", start, end); 49 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); 55 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)); 60 61 { 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. 69 */ 70 if (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; 75 } 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 /* If this is a kernel-generated async DIO request, 81 * assume that any resources the iterator points to 82 * (eg. a bio_vec array) will persist till the end of 83 * the op. 84 */ 85 wreq->iter = *iter; 86 } 87 88 wreq->io_iter = wreq->iter; 89 } 90 91 __set_bit(NETFS_RREQ_USE_IO_ITER, &wreq->flags); 92 93 /* Copy the data into the bounce buffer and encrypt it. */ 94 // TODO 95 96 /* Dispatch the write. */ 97 __set_bit(NETFS_RREQ_UPLOAD_TO_SERVER, &wreq->flags); 98 if (async) 99 wreq->iocb = iocb; 100 wreq->len = iov_iter_count(&wreq->io_iter); 101 wreq->cleanup = netfs_cleanup_dio_write; 102 ret = netfs_unbuffered_write(wreq, is_sync_kiocb(iocb), wreq->len); 103 if (ret < 0) { 104 _debug("begin = %zd", ret); 105 goto out; 106 } 107 108 if (!async) { 109 trace_netfs_rreq(wreq, netfs_rreq_trace_wait_ip); 110 wait_on_bit(&wreq->flags, NETFS_RREQ_IN_PROGRESS, 111 TASK_UNINTERRUPTIBLE); 112 ret = wreq->error; 113 if (ret == 0) { 114 ret = wreq->transferred; 115 iocb->ki_pos += ret; 116 } 117 } else { 118 ret = -EIOCBQUEUED; 119 } 120 121 out: 122 netfs_put_request(wreq, false, netfs_rreq_trace_put_return); 123 return ret; 124 } 125 EXPORT_SYMBOL(netfs_unbuffered_write_iter_locked); 126 127 /** 128 * netfs_unbuffered_write_iter - Unbuffered write to a file 129 * @iocb: IO state structure 130 * @from: iov_iter with data to write 131 * 132 * Do an unbuffered write to a file, writing the data directly to the server 133 * and not lodging the data in the pagecache. 134 * 135 * Return: 136 * * Negative error code if no data has been written at all of 137 * vfs_fsync_range() failed for a synchronous write 138 * * Number of bytes written, even for truncated writes 139 */ 140 ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from) 141 { 142 struct file *file = iocb->ki_filp; 143 struct address_space *mapping = file->f_mapping; 144 struct inode *inode = mapping->host; 145 struct netfs_inode *ictx = netfs_inode(inode); 146 ssize_t ret; 147 loff_t pos = iocb->ki_pos; 148 unsigned long long end = pos + iov_iter_count(from) - 1; 149 150 _enter("%llx,%zx,%llx", pos, iov_iter_count(from), i_size_read(inode)); 151 152 if (!iov_iter_count(from)) 153 return 0; 154 155 trace_netfs_write_iter(iocb, from); 156 netfs_stat(&netfs_n_wh_dio_write); 157 158 ret = netfs_start_io_direct(inode); 159 if (ret < 0) 160 return ret; 161 ret = generic_write_checks(iocb, from); 162 if (ret <= 0) 163 goto out; 164 ret = file_remove_privs(file); 165 if (ret < 0) 166 goto out; 167 ret = file_update_time(file); 168 if (ret < 0) 169 goto out; 170 if (iocb->ki_flags & IOCB_NOWAIT) { 171 /* We could block if there are any pages in the range. */ 172 ret = -EAGAIN; 173 if (filemap_range_has_page(mapping, pos, end)) 174 if (filemap_invalidate_inode(inode, true, pos, end)) 175 goto out; 176 } else { 177 ret = filemap_write_and_wait_range(mapping, pos, end); 178 if (ret < 0) 179 goto out; 180 } 181 182 /* 183 * After a write we want buffered reads to be sure to go to disk to get 184 * the new data. We invalidate clean cached page from the region we're 185 * about to write. We do this *before* the write so that we can return 186 * without clobbering -EIOCBQUEUED from ->direct_IO(). 187 */ 188 ret = filemap_invalidate_inode(inode, true, pos, end); 189 if (ret < 0) 190 goto out; 191 end = iocb->ki_pos + iov_iter_count(from); 192 if (end > ictx->zero_point) 193 ictx->zero_point = end; 194 195 fscache_invalidate(netfs_i_cookie(ictx), NULL, i_size_read(inode), 196 FSCACHE_INVAL_DIO_WRITE); 197 ret = netfs_unbuffered_write_iter_locked(iocb, from, NULL); 198 out: 199 netfs_end_io_direct(inode); 200 return ret; 201 } 202 EXPORT_SYMBOL(netfs_unbuffered_write_iter); 203