1*153a9961SDavid Howells // SPDX-License-Identifier: GPL-2.0-or-later 2*153a9961SDavid Howells /* Unbuffered and direct write support. 3*153a9961SDavid Howells * 4*153a9961SDavid Howells * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. 5*153a9961SDavid Howells * Written by David Howells (dhowells@redhat.com) 6*153a9961SDavid Howells */ 7*153a9961SDavid Howells 8*153a9961SDavid Howells #include <linux/export.h> 9*153a9961SDavid Howells #include <linux/uio.h> 10*153a9961SDavid Howells #include "internal.h" 11*153a9961SDavid Howells 12*153a9961SDavid Howells static void netfs_cleanup_dio_write(struct netfs_io_request *wreq) 13*153a9961SDavid Howells { 14*153a9961SDavid Howells struct inode *inode = wreq->inode; 15*153a9961SDavid Howells unsigned long long end = wreq->start + wreq->len; 16*153a9961SDavid Howells 17*153a9961SDavid Howells if (!wreq->error && 18*153a9961SDavid Howells i_size_read(inode) < end) { 19*153a9961SDavid Howells if (wreq->netfs_ops->update_i_size) 20*153a9961SDavid Howells wreq->netfs_ops->update_i_size(inode, end); 21*153a9961SDavid Howells else 22*153a9961SDavid Howells i_size_write(inode, end); 23*153a9961SDavid Howells } 24*153a9961SDavid Howells } 25*153a9961SDavid Howells 26*153a9961SDavid Howells /* 27*153a9961SDavid Howells * Perform an unbuffered write where we may have to do an RMW operation on an 28*153a9961SDavid Howells * encrypted file. This can also be used for direct I/O writes. 29*153a9961SDavid Howells */ 30*153a9961SDavid Howells ssize_t netfs_unbuffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *iter, 31*153a9961SDavid Howells struct netfs_group *netfs_group) 32*153a9961SDavid Howells { 33*153a9961SDavid Howells struct netfs_io_request *wreq; 34*153a9961SDavid Howells unsigned long long start = iocb->ki_pos; 35*153a9961SDavid Howells unsigned long long end = start + iov_iter_count(iter); 36*153a9961SDavid Howells ssize_t ret, n; 37*153a9961SDavid Howells bool async = !is_sync_kiocb(iocb); 38*153a9961SDavid Howells 39*153a9961SDavid Howells _enter(""); 40*153a9961SDavid Howells 41*153a9961SDavid Howells /* We're going to need a bounce buffer if what we transmit is going to 42*153a9961SDavid Howells * be different in some way to the source buffer, e.g. because it gets 43*153a9961SDavid Howells * encrypted/compressed or because it needs expanding to a block size. 44*153a9961SDavid Howells */ 45*153a9961SDavid Howells // TODO 46*153a9961SDavid Howells 47*153a9961SDavid Howells _debug("uw %llx-%llx", start, end); 48*153a9961SDavid Howells 49*153a9961SDavid Howells wreq = netfs_alloc_request(iocb->ki_filp->f_mapping, iocb->ki_filp, 50*153a9961SDavid Howells start, end - start, 51*153a9961SDavid Howells iocb->ki_flags & IOCB_DIRECT ? 52*153a9961SDavid Howells NETFS_DIO_WRITE : NETFS_UNBUFFERED_WRITE); 53*153a9961SDavid Howells if (IS_ERR(wreq)) 54*153a9961SDavid Howells return PTR_ERR(wreq); 55*153a9961SDavid Howells 56*153a9961SDavid Howells { 57*153a9961SDavid Howells /* If this is an async op and we're not using a bounce buffer, 58*153a9961SDavid Howells * we have to save the source buffer as the iterator is only 59*153a9961SDavid Howells * good until we return. In such a case, extract an iterator 60*153a9961SDavid Howells * to represent as much of the the output buffer as we can 61*153a9961SDavid Howells * manage. Note that the extraction might not be able to 62*153a9961SDavid Howells * allocate a sufficiently large bvec array and may shorten the 63*153a9961SDavid Howells * request. 64*153a9961SDavid Howells */ 65*153a9961SDavid Howells if (async || user_backed_iter(iter)) { 66*153a9961SDavid Howells n = netfs_extract_user_iter(iter, wreq->len, &wreq->iter, 0); 67*153a9961SDavid Howells if (n < 0) { 68*153a9961SDavid Howells ret = n; 69*153a9961SDavid Howells goto out; 70*153a9961SDavid Howells } 71*153a9961SDavid Howells wreq->direct_bv = (struct bio_vec *)wreq->iter.bvec; 72*153a9961SDavid Howells wreq->direct_bv_count = n; 73*153a9961SDavid Howells wreq->direct_bv_unpin = iov_iter_extract_will_pin(iter); 74*153a9961SDavid Howells wreq->len = iov_iter_count(&wreq->iter); 75*153a9961SDavid Howells } else { 76*153a9961SDavid Howells wreq->iter = *iter; 77*153a9961SDavid Howells } 78*153a9961SDavid Howells 79*153a9961SDavid Howells wreq->io_iter = wreq->iter; 80*153a9961SDavid Howells } 81*153a9961SDavid Howells 82*153a9961SDavid Howells /* Copy the data into the bounce buffer and encrypt it. */ 83*153a9961SDavid Howells // TODO 84*153a9961SDavid Howells 85*153a9961SDavid Howells /* Dispatch the write. */ 86*153a9961SDavid Howells __set_bit(NETFS_RREQ_UPLOAD_TO_SERVER, &wreq->flags); 87*153a9961SDavid Howells if (async) 88*153a9961SDavid Howells wreq->iocb = iocb; 89*153a9961SDavid Howells wreq->cleanup = netfs_cleanup_dio_write; 90*153a9961SDavid Howells ret = netfs_begin_write(wreq, is_sync_kiocb(iocb), 91*153a9961SDavid Howells iocb->ki_flags & IOCB_DIRECT ? 92*153a9961SDavid Howells netfs_write_trace_dio_write : 93*153a9961SDavid Howells netfs_write_trace_unbuffered_write); 94*153a9961SDavid Howells if (ret < 0) { 95*153a9961SDavid Howells _debug("begin = %zd", ret); 96*153a9961SDavid Howells goto out; 97*153a9961SDavid Howells } 98*153a9961SDavid Howells 99*153a9961SDavid Howells if (!async) { 100*153a9961SDavid Howells trace_netfs_rreq(wreq, netfs_rreq_trace_wait_ip); 101*153a9961SDavid Howells wait_on_bit(&wreq->flags, NETFS_RREQ_IN_PROGRESS, 102*153a9961SDavid Howells TASK_UNINTERRUPTIBLE); 103*153a9961SDavid Howells 104*153a9961SDavid Howells ret = wreq->error; 105*153a9961SDavid Howells _debug("waited = %zd", ret); 106*153a9961SDavid Howells if (ret == 0) { 107*153a9961SDavid Howells ret = wreq->transferred; 108*153a9961SDavid Howells iocb->ki_pos += ret; 109*153a9961SDavid Howells } 110*153a9961SDavid Howells } else { 111*153a9961SDavid Howells ret = -EIOCBQUEUED; 112*153a9961SDavid Howells } 113*153a9961SDavid Howells 114*153a9961SDavid Howells out: 115*153a9961SDavid Howells netfs_put_request(wreq, false, netfs_rreq_trace_put_return); 116*153a9961SDavid Howells return ret; 117*153a9961SDavid Howells } 118*153a9961SDavid Howells 119*153a9961SDavid Howells /** 120*153a9961SDavid Howells * netfs_unbuffered_write_iter - Unbuffered write to a file 121*153a9961SDavid Howells * @iocb: IO state structure 122*153a9961SDavid Howells * @from: iov_iter with data to write 123*153a9961SDavid Howells * 124*153a9961SDavid Howells * Do an unbuffered write to a file, writing the data directly to the server 125*153a9961SDavid Howells * and not lodging the data in the pagecache. 126*153a9961SDavid Howells * 127*153a9961SDavid Howells * Return: 128*153a9961SDavid Howells * * Negative error code if no data has been written at all of 129*153a9961SDavid Howells * vfs_fsync_range() failed for a synchronous write 130*153a9961SDavid Howells * * Number of bytes written, even for truncated writes 131*153a9961SDavid Howells */ 132*153a9961SDavid Howells ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from) 133*153a9961SDavid Howells { 134*153a9961SDavid Howells struct file *file = iocb->ki_filp; 135*153a9961SDavid Howells struct inode *inode = file->f_mapping->host; 136*153a9961SDavid Howells struct netfs_inode *ictx = netfs_inode(inode); 137*153a9961SDavid Howells ssize_t ret; 138*153a9961SDavid Howells 139*153a9961SDavid Howells _enter("%llx,%zx,%llx", iocb->ki_pos, iov_iter_count(from), i_size_read(inode)); 140*153a9961SDavid Howells 141*153a9961SDavid Howells trace_netfs_write_iter(iocb, from); 142*153a9961SDavid Howells 143*153a9961SDavid Howells ret = netfs_start_io_direct(inode); 144*153a9961SDavid Howells if (ret < 0) 145*153a9961SDavid Howells return ret; 146*153a9961SDavid Howells ret = generic_write_checks(iocb, from); 147*153a9961SDavid Howells if (ret < 0) 148*153a9961SDavid Howells goto out; 149*153a9961SDavid Howells ret = file_remove_privs(file); 150*153a9961SDavid Howells if (ret < 0) 151*153a9961SDavid Howells goto out; 152*153a9961SDavid Howells ret = file_update_time(file); 153*153a9961SDavid Howells if (ret < 0) 154*153a9961SDavid Howells goto out; 155*153a9961SDavid Howells ret = kiocb_invalidate_pages(iocb, iov_iter_count(from)); 156*153a9961SDavid Howells if (ret < 0) 157*153a9961SDavid Howells goto out; 158*153a9961SDavid Howells 159*153a9961SDavid Howells fscache_invalidate(netfs_i_cookie(ictx), NULL, i_size_read(inode), 160*153a9961SDavid Howells FSCACHE_INVAL_DIO_WRITE); 161*153a9961SDavid Howells ret = netfs_unbuffered_write_iter_locked(iocb, from, NULL); 162*153a9961SDavid Howells out: 163*153a9961SDavid Howells netfs_end_io_direct(inode); 164*153a9961SDavid Howells return ret; 165*153a9961SDavid Howells } 166*153a9961SDavid Howells EXPORT_SYMBOL(netfs_unbuffered_write_iter); 167