1153a9961SDavid Howells // SPDX-License-Identifier: GPL-2.0-or-later 2153a9961SDavid Howells /* Unbuffered and direct write support. 3153a9961SDavid Howells * 4153a9961SDavid Howells * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. 5153a9961SDavid Howells * Written by David Howells (dhowells@redhat.com) 6153a9961SDavid Howells */ 7153a9961SDavid Howells 8153a9961SDavid Howells #include <linux/export.h> 9153a9961SDavid Howells #include <linux/uio.h> 10153a9961SDavid Howells #include "internal.h" 11153a9961SDavid Howells 12153a9961SDavid Howells static void netfs_cleanup_dio_write(struct netfs_io_request *wreq) 13153a9961SDavid Howells { 14153a9961SDavid Howells struct inode *inode = wreq->inode; 159b038d00SDavid Howells unsigned long long end = wreq->start + wreq->transferred; 16153a9961SDavid Howells 17153a9961SDavid Howells if (!wreq->error && 18153a9961SDavid Howells i_size_read(inode) < end) { 19153a9961SDavid Howells if (wreq->netfs_ops->update_i_size) 20153a9961SDavid Howells wreq->netfs_ops->update_i_size(inode, end); 21153a9961SDavid Howells else 22153a9961SDavid Howells i_size_write(inode, end); 23153a9961SDavid Howells } 24153a9961SDavid Howells } 25153a9961SDavid Howells 26153a9961SDavid Howells /* 27153a9961SDavid Howells * Perform an unbuffered write where we may have to do an RMW operation on an 28153a9961SDavid Howells * encrypted file. This can also be used for direct I/O writes. 29153a9961SDavid Howells */ 3016e00683SSteve French ssize_t netfs_unbuffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *iter, 31153a9961SDavid Howells struct netfs_group *netfs_group) 32153a9961SDavid Howells { 33153a9961SDavid Howells struct netfs_io_request *wreq; 34153a9961SDavid Howells unsigned long long start = iocb->ki_pos; 35153a9961SDavid Howells unsigned long long end = start + iov_iter_count(iter); 36153a9961SDavid Howells ssize_t ret, n; 372df86547SDavid Howells size_t len = iov_iter_count(iter); 38153a9961SDavid Howells bool async = !is_sync_kiocb(iocb); 39153a9961SDavid Howells 40*a9d47a50SDavid Howells _enter(""); 41153a9961SDavid Howells 42153a9961SDavid Howells /* We're going to need a bounce buffer if what we transmit is going to 43153a9961SDavid Howells * be different in some way to the source buffer, e.g. because it gets 44153a9961SDavid Howells * encrypted/compressed or because it needs expanding to a block size. 45153a9961SDavid Howells */ 46153a9961SDavid Howells // TODO 47153a9961SDavid Howells 48*a9d47a50SDavid Howells _debug("uw %llx-%llx", start, end); 49153a9961SDavid Howells 502df86547SDavid Howells wreq = netfs_create_write_req(iocb->ki_filp->f_mapping, iocb->ki_filp, start, 51153a9961SDavid Howells iocb->ki_flags & IOCB_DIRECT ? 52153a9961SDavid Howells NETFS_DIO_WRITE : NETFS_UNBUFFERED_WRITE); 53153a9961SDavid Howells if (IS_ERR(wreq)) 54153a9961SDavid Howells return PTR_ERR(wreq); 55153a9961SDavid Howells 562df86547SDavid Howells wreq->io_streams[0].avail = true; 572df86547SDavid Howells trace_netfs_write(wreq, (iocb->ki_flags & IOCB_DIRECT ? 582df86547SDavid Howells netfs_write_trace_dio_write : 592df86547SDavid Howells netfs_write_trace_unbuffered_write)); 602df86547SDavid Howells 61153a9961SDavid Howells { 62153a9961SDavid Howells /* If this is an async op and we're not using a bounce buffer, 63153a9961SDavid Howells * we have to save the source buffer as the iterator is only 64153a9961SDavid Howells * good until we return. In such a case, extract an iterator 65153a9961SDavid Howells * to represent as much of the the output buffer as we can 66153a9961SDavid Howells * manage. Note that the extraction might not be able to 67153a9961SDavid Howells * allocate a sufficiently large bvec array and may shorten the 68153a9961SDavid Howells * request. 69153a9961SDavid Howells */ 70153a9961SDavid Howells if (async || user_backed_iter(iter)) { 712df86547SDavid Howells n = netfs_extract_user_iter(iter, len, &wreq->iter, 0); 72153a9961SDavid Howells if (n < 0) { 73153a9961SDavid Howells ret = n; 74153a9961SDavid Howells goto out; 75153a9961SDavid Howells } 76153a9961SDavid Howells wreq->direct_bv = (struct bio_vec *)wreq->iter.bvec; 77153a9961SDavid Howells wreq->direct_bv_count = n; 78153a9961SDavid Howells wreq->direct_bv_unpin = iov_iter_extract_will_pin(iter); 79153a9961SDavid Howells } else { 80153a9961SDavid Howells wreq->iter = *iter; 81153a9961SDavid Howells } 82153a9961SDavid Howells 83153a9961SDavid Howells wreq->io_iter = wreq->iter; 84153a9961SDavid Howells } 85153a9961SDavid Howells 862df86547SDavid Howells __set_bit(NETFS_RREQ_USE_IO_ITER, &wreq->flags); 872df86547SDavid Howells 88153a9961SDavid Howells /* Copy the data into the bounce buffer and encrypt it. */ 89153a9961SDavid Howells // TODO 90153a9961SDavid Howells 91153a9961SDavid Howells /* Dispatch the write. */ 92153a9961SDavid Howells __set_bit(NETFS_RREQ_UPLOAD_TO_SERVER, &wreq->flags); 93153a9961SDavid Howells if (async) 94153a9961SDavid Howells wreq->iocb = iocb; 95d98b7d7dSDavid Howells wreq->len = iov_iter_count(&wreq->io_iter); 96153a9961SDavid Howells wreq->cleanup = netfs_cleanup_dio_write; 97d98b7d7dSDavid Howells ret = netfs_unbuffered_write(wreq, is_sync_kiocb(iocb), wreq->len); 98153a9961SDavid Howells if (ret < 0) { 99*a9d47a50SDavid Howells _debug("begin = %zd", ret); 100153a9961SDavid Howells goto out; 101153a9961SDavid Howells } 102153a9961SDavid Howells 103153a9961SDavid Howells if (!async) { 104153a9961SDavid Howells trace_netfs_rreq(wreq, netfs_rreq_trace_wait_ip); 105153a9961SDavid Howells wait_on_bit(&wreq->flags, NETFS_RREQ_IN_PROGRESS, 106153a9961SDavid Howells TASK_UNINTERRUPTIBLE); 1072df86547SDavid Howells smp_rmb(); /* Read error/transferred after RIP flag */ 108153a9961SDavid Howells ret = wreq->error; 109153a9961SDavid Howells if (ret == 0) { 110153a9961SDavid Howells ret = wreq->transferred; 111153a9961SDavid Howells iocb->ki_pos += ret; 112153a9961SDavid Howells } 113153a9961SDavid Howells } else { 114153a9961SDavid Howells ret = -EIOCBQUEUED; 115153a9961SDavid Howells } 116153a9961SDavid Howells 117153a9961SDavid Howells out: 118153a9961SDavid Howells netfs_put_request(wreq, false, netfs_rreq_trace_put_return); 119153a9961SDavid Howells return ret; 120153a9961SDavid Howells } 12116e00683SSteve French EXPORT_SYMBOL(netfs_unbuffered_write_iter_locked); 122153a9961SDavid Howells 123153a9961SDavid Howells /** 124153a9961SDavid Howells * netfs_unbuffered_write_iter - Unbuffered write to a file 125153a9961SDavid Howells * @iocb: IO state structure 126153a9961SDavid Howells * @from: iov_iter with data to write 127153a9961SDavid Howells * 128153a9961SDavid Howells * Do an unbuffered write to a file, writing the data directly to the server 129153a9961SDavid Howells * and not lodging the data in the pagecache. 130153a9961SDavid Howells * 131153a9961SDavid Howells * Return: 132153a9961SDavid Howells * * Negative error code if no data has been written at all of 133153a9961SDavid Howells * vfs_fsync_range() failed for a synchronous write 134153a9961SDavid Howells * * Number of bytes written, even for truncated writes 135153a9961SDavid Howells */ 136153a9961SDavid Howells ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from) 137153a9961SDavid Howells { 138153a9961SDavid Howells struct file *file = iocb->ki_filp; 13974e797d7SDavid Howells struct address_space *mapping = file->f_mapping; 14074e797d7SDavid Howells struct inode *inode = mapping->host; 141153a9961SDavid Howells struct netfs_inode *ictx = netfs_inode(inode); 142153a9961SDavid Howells ssize_t ret; 14374e797d7SDavid Howells loff_t pos = iocb->ki_pos; 14474e797d7SDavid Howells unsigned long long end = pos + iov_iter_count(from) - 1; 145153a9961SDavid Howells 146*a9d47a50SDavid Howells _enter("%llx,%zx,%llx", pos, iov_iter_count(from), i_size_read(inode)); 147153a9961SDavid Howells 148ca9ca1a5SDavid Howells if (!iov_iter_count(from)) 149ca9ca1a5SDavid Howells return 0; 150ca9ca1a5SDavid Howells 151153a9961SDavid Howells trace_netfs_write_iter(iocb, from); 1524824e591SDavid Howells netfs_stat(&netfs_n_wh_dio_write); 153153a9961SDavid Howells 154153a9961SDavid Howells ret = netfs_start_io_direct(inode); 155153a9961SDavid Howells if (ret < 0) 156153a9961SDavid Howells return ret; 157153a9961SDavid Howells ret = generic_write_checks(iocb, from); 158ca9ca1a5SDavid Howells if (ret <= 0) 159153a9961SDavid Howells goto out; 160153a9961SDavid Howells ret = file_remove_privs(file); 161153a9961SDavid Howells if (ret < 0) 162153a9961SDavid Howells goto out; 163153a9961SDavid Howells ret = file_update_time(file); 164153a9961SDavid Howells if (ret < 0) 165153a9961SDavid Howells goto out; 16674e797d7SDavid Howells if (iocb->ki_flags & IOCB_NOWAIT) { 16774e797d7SDavid Howells /* We could block if there are any pages in the range. */ 16874e797d7SDavid Howells ret = -EAGAIN; 16974e797d7SDavid Howells if (filemap_range_has_page(mapping, pos, end)) 17074e797d7SDavid Howells if (filemap_invalidate_inode(inode, true, pos, end)) 17174e797d7SDavid Howells goto out; 17274e797d7SDavid Howells } else { 17374e797d7SDavid Howells ret = filemap_write_and_wait_range(mapping, pos, end); 17474e797d7SDavid Howells if (ret < 0) 17574e797d7SDavid Howells goto out; 17674e797d7SDavid Howells } 17774e797d7SDavid Howells 17874e797d7SDavid Howells /* 17974e797d7SDavid Howells * After a write we want buffered reads to be sure to go to disk to get 18074e797d7SDavid Howells * the new data. We invalidate clean cached page from the region we're 18174e797d7SDavid Howells * about to write. We do this *before* the write so that we can return 18274e797d7SDavid Howells * without clobbering -EIOCBQUEUED from ->direct_IO(). 18374e797d7SDavid Howells */ 18474e797d7SDavid Howells ret = filemap_invalidate_inode(inode, true, pos, end); 185153a9961SDavid Howells if (ret < 0) 186153a9961SDavid Howells goto out; 187100ccd18SDavid Howells end = iocb->ki_pos + iov_iter_count(from); 188100ccd18SDavid Howells if (end > ictx->zero_point) 189100ccd18SDavid Howells ictx->zero_point = end; 190153a9961SDavid Howells 191153a9961SDavid Howells fscache_invalidate(netfs_i_cookie(ictx), NULL, i_size_read(inode), 192153a9961SDavid Howells FSCACHE_INVAL_DIO_WRITE); 193153a9961SDavid Howells ret = netfs_unbuffered_write_iter_locked(iocb, from, NULL); 194153a9961SDavid Howells out: 195153a9961SDavid Howells netfs_end_io_direct(inode); 196153a9961SDavid Howells return ret; 197153a9961SDavid Howells } 198153a9961SDavid Howells EXPORT_SYMBOL(netfs_unbuffered_write_iter); 199