xref: /linux/fs/netfs/direct_write.c (revision ca9ca1a5d5a980550db1001ea825f9fdfa550b83)
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;
15153a9961SDavid Howells 	unsigned long long end = wreq->start + wreq->len;
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  */
300e4d464cSDavid Howells static 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;
37153a9961SDavid Howells 	bool async = !is_sync_kiocb(iocb);
38153a9961SDavid Howells 
39153a9961SDavid Howells 	_enter("");
40153a9961SDavid Howells 
41153a9961SDavid Howells 	/* We're going to need a bounce buffer if what we transmit is going to
42153a9961SDavid Howells 	 * be different in some way to the source buffer, e.g. because it gets
43153a9961SDavid Howells 	 * encrypted/compressed or because it needs expanding to a block size.
44153a9961SDavid Howells 	 */
45153a9961SDavid Howells 	// TODO
46153a9961SDavid Howells 
47153a9961SDavid Howells 	_debug("uw %llx-%llx", start, end);
48153a9961SDavid Howells 
49153a9961SDavid Howells 	wreq = netfs_alloc_request(iocb->ki_filp->f_mapping, iocb->ki_filp,
50153a9961SDavid Howells 				   start, end - 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 
56153a9961SDavid Howells 	{
57153a9961SDavid Howells 		/* If this is an async op and we're not using a bounce buffer,
58153a9961SDavid Howells 		 * we have to save the source buffer as the iterator is only
59153a9961SDavid Howells 		 * good until we return.  In such a case, extract an iterator
60153a9961SDavid Howells 		 * to represent as much of the the output buffer as we can
61153a9961SDavid Howells 		 * manage.  Note that the extraction might not be able to
62153a9961SDavid Howells 		 * allocate a sufficiently large bvec array and may shorten the
63153a9961SDavid Howells 		 * request.
64153a9961SDavid Howells 		 */
65153a9961SDavid Howells 		if (async || user_backed_iter(iter)) {
66153a9961SDavid Howells 			n = netfs_extract_user_iter(iter, wreq->len, &wreq->iter, 0);
67153a9961SDavid Howells 			if (n < 0) {
68153a9961SDavid Howells 				ret = n;
69153a9961SDavid Howells 				goto out;
70153a9961SDavid Howells 			}
71153a9961SDavid Howells 			wreq->direct_bv = (struct bio_vec *)wreq->iter.bvec;
72153a9961SDavid Howells 			wreq->direct_bv_count = n;
73153a9961SDavid Howells 			wreq->direct_bv_unpin = iov_iter_extract_will_pin(iter);
74153a9961SDavid Howells 			wreq->len = iov_iter_count(&wreq->iter);
75153a9961SDavid Howells 		} else {
76153a9961SDavid Howells 			wreq->iter = *iter;
77153a9961SDavid Howells 		}
78153a9961SDavid Howells 
79153a9961SDavid Howells 		wreq->io_iter = wreq->iter;
80153a9961SDavid Howells 	}
81153a9961SDavid Howells 
82153a9961SDavid Howells 	/* Copy the data into the bounce buffer and encrypt it. */
83153a9961SDavid Howells 	// TODO
84153a9961SDavid Howells 
85153a9961SDavid Howells 	/* Dispatch the write. */
86153a9961SDavid Howells 	__set_bit(NETFS_RREQ_UPLOAD_TO_SERVER, &wreq->flags);
87153a9961SDavid Howells 	if (async)
88153a9961SDavid Howells 		wreq->iocb = iocb;
89153a9961SDavid Howells 	wreq->cleanup = netfs_cleanup_dio_write;
90153a9961SDavid Howells 	ret = netfs_begin_write(wreq, is_sync_kiocb(iocb),
91153a9961SDavid Howells 				iocb->ki_flags & IOCB_DIRECT ?
92153a9961SDavid Howells 				netfs_write_trace_dio_write :
93153a9961SDavid Howells 				netfs_write_trace_unbuffered_write);
94153a9961SDavid Howells 	if (ret < 0) {
95153a9961SDavid Howells 		_debug("begin = %zd", ret);
96153a9961SDavid Howells 		goto out;
97153a9961SDavid Howells 	}
98153a9961SDavid Howells 
99153a9961SDavid Howells 	if (!async) {
100153a9961SDavid Howells 		trace_netfs_rreq(wreq, netfs_rreq_trace_wait_ip);
101153a9961SDavid Howells 		wait_on_bit(&wreq->flags, NETFS_RREQ_IN_PROGRESS,
102153a9961SDavid Howells 			    TASK_UNINTERRUPTIBLE);
103153a9961SDavid Howells 
104153a9961SDavid Howells 		ret = wreq->error;
105153a9961SDavid Howells 		_debug("waited = %zd", ret);
106153a9961SDavid Howells 		if (ret == 0) {
107153a9961SDavid Howells 			ret = wreq->transferred;
108153a9961SDavid Howells 			iocb->ki_pos += ret;
109153a9961SDavid Howells 		}
110153a9961SDavid Howells 	} else {
111153a9961SDavid Howells 		ret = -EIOCBQUEUED;
112153a9961SDavid Howells 	}
113153a9961SDavid Howells 
114153a9961SDavid Howells out:
115153a9961SDavid Howells 	netfs_put_request(wreq, false, netfs_rreq_trace_put_return);
116153a9961SDavid Howells 	return ret;
117153a9961SDavid Howells }
118153a9961SDavid Howells 
119153a9961SDavid Howells /**
120153a9961SDavid Howells  * netfs_unbuffered_write_iter - Unbuffered write to a file
121153a9961SDavid Howells  * @iocb: IO state structure
122153a9961SDavid Howells  * @from: iov_iter with data to write
123153a9961SDavid Howells  *
124153a9961SDavid Howells  * Do an unbuffered write to a file, writing the data directly to the server
125153a9961SDavid Howells  * and not lodging the data in the pagecache.
126153a9961SDavid Howells  *
127153a9961SDavid Howells  * Return:
128153a9961SDavid Howells  * * Negative error code if no data has been written at all of
129153a9961SDavid Howells  *   vfs_fsync_range() failed for a synchronous write
130153a9961SDavid Howells  * * Number of bytes written, even for truncated writes
131153a9961SDavid Howells  */
132153a9961SDavid Howells ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from)
133153a9961SDavid Howells {
134153a9961SDavid Howells 	struct file *file = iocb->ki_filp;
135153a9961SDavid Howells 	struct inode *inode = file->f_mapping->host;
136153a9961SDavid Howells 	struct netfs_inode *ictx = netfs_inode(inode);
137100ccd18SDavid Howells 	unsigned long long end;
138153a9961SDavid Howells 	ssize_t ret;
139153a9961SDavid Howells 
140153a9961SDavid Howells 	_enter("%llx,%zx,%llx", iocb->ki_pos, iov_iter_count(from), i_size_read(inode));
141153a9961SDavid Howells 
142*ca9ca1a5SDavid Howells 	if (!iov_iter_count(from))
143*ca9ca1a5SDavid Howells 		return 0;
144*ca9ca1a5SDavid Howells 
145153a9961SDavid Howells 	trace_netfs_write_iter(iocb, from);
1464088e389SDavid Howells 	netfs_stat(&netfs_n_rh_dio_write);
147153a9961SDavid Howells 
148153a9961SDavid Howells 	ret = netfs_start_io_direct(inode);
149153a9961SDavid Howells 	if (ret < 0)
150153a9961SDavid Howells 		return ret;
151153a9961SDavid Howells 	ret = generic_write_checks(iocb, from);
152*ca9ca1a5SDavid Howells 	if (ret <= 0)
153153a9961SDavid Howells 		goto out;
154153a9961SDavid Howells 	ret = file_remove_privs(file);
155153a9961SDavid Howells 	if (ret < 0)
156153a9961SDavid Howells 		goto out;
157153a9961SDavid Howells 	ret = file_update_time(file);
158153a9961SDavid Howells 	if (ret < 0)
159153a9961SDavid Howells 		goto out;
160153a9961SDavid Howells 	ret = kiocb_invalidate_pages(iocb, iov_iter_count(from));
161153a9961SDavid Howells 	if (ret < 0)
162153a9961SDavid Howells 		goto out;
163100ccd18SDavid Howells 	end = iocb->ki_pos + iov_iter_count(from);
164100ccd18SDavid Howells 	if (end > ictx->zero_point)
165100ccd18SDavid Howells 		ictx->zero_point = end;
166153a9961SDavid Howells 
167153a9961SDavid Howells 	fscache_invalidate(netfs_i_cookie(ictx), NULL, i_size_read(inode),
168153a9961SDavid Howells 			   FSCACHE_INVAL_DIO_WRITE);
169153a9961SDavid Howells 	ret = netfs_unbuffered_write_iter_locked(iocb, from, NULL);
170153a9961SDavid Howells out:
171153a9961SDavid Howells 	netfs_end_io_direct(inode);
172153a9961SDavid Howells 	return ret;
173153a9961SDavid Howells }
174153a9961SDavid Howells EXPORT_SYMBOL(netfs_unbuffered_write_iter);
175