1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Unbuffered and direct write support.
4 * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/export.h>
12 static void netfs_cleanup_dio_write(struct netfs_io_request
*wreq
)
14 struct inode
*inode
= wreq
->inode
;
15 unsigned long long end
= wreq
->start
+ wreq
->transferred
;
18 i_size_read(inode
) < end
) {
19 if (wreq
->netfs_ops
->update_i_size
)
20 wreq
->netfs_ops
->update_i_size(inode
, end
);
22 i_size_write(inode
, end
);
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.
30 ssize_t
netfs_unbuffered_write_iter_locked(struct kiocb
*iocb
, struct iov_iter
*iter
,
31 struct netfs_group
*netfs_group
)
33 struct netfs_io_request
*wreq
;
34 unsigned long long start
= iocb
->ki_pos
;
35 unsigned long long end
= start
+ iov_iter_count(iter
);
37 size_t len
= iov_iter_count(iter
);
38 bool async
= !is_sync_kiocb(iocb
);
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.
48 _debug("uw %llx-%llx", start
, end
);
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
);
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
));
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
70 if (async
|| user_backed_iter(iter
)) {
71 n
= netfs_extract_user_iter(iter
, len
, &wreq
->iter
, 0);
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
);
83 wreq
->io_iter
= wreq
->iter
;
86 __set_bit(NETFS_RREQ_USE_IO_ITER
, &wreq
->flags
);
88 /* Copy the data into the bounce buffer and encrypt it. */
91 /* Dispatch the write. */
92 __set_bit(NETFS_RREQ_UPLOAD_TO_SERVER
, &wreq
->flags
);
95 wreq
->len
= iov_iter_count(&wreq
->io_iter
);
96 wreq
->cleanup
= netfs_cleanup_dio_write
;
97 ret
= netfs_unbuffered_write(wreq
, is_sync_kiocb(iocb
), wreq
->len
);
99 _debug("begin = %zd", ret
);
104 trace_netfs_rreq(wreq
, netfs_rreq_trace_wait_ip
);
105 wait_on_bit(&wreq
->flags
, NETFS_RREQ_IN_PROGRESS
,
106 TASK_UNINTERRUPTIBLE
);
107 smp_rmb(); /* Read error/transferred after RIP flag */
110 ret
= wreq
->transferred
;
118 netfs_put_request(wreq
, false, netfs_rreq_trace_put_return
);
121 EXPORT_SYMBOL(netfs_unbuffered_write_iter_locked
);
124 * netfs_unbuffered_write_iter - Unbuffered write to a file
125 * @iocb: IO state structure
126 * @from: iov_iter with data to write
128 * Do an unbuffered write to a file, writing the data directly to the server
129 * and not lodging the data in the pagecache.
132 * * Negative error code if no data has been written at all of
133 * vfs_fsync_range() failed for a synchronous write
134 * * Number of bytes written, even for truncated writes
136 ssize_t
netfs_unbuffered_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
138 struct file
*file
= iocb
->ki_filp
;
139 struct address_space
*mapping
= file
->f_mapping
;
140 struct inode
*inode
= mapping
->host
;
141 struct netfs_inode
*ictx
= netfs_inode(inode
);
143 loff_t pos
= iocb
->ki_pos
;
144 unsigned long long end
= pos
+ iov_iter_count(from
) - 1;
146 _enter("%llx,%zx,%llx", pos
, iov_iter_count(from
), i_size_read(inode
));
148 if (!iov_iter_count(from
))
151 trace_netfs_write_iter(iocb
, from
);
152 netfs_stat(&netfs_n_wh_dio_write
);
154 ret
= netfs_start_io_direct(inode
);
157 ret
= generic_write_checks(iocb
, from
);
160 ret
= file_remove_privs(file
);
163 ret
= file_update_time(file
);
166 if (iocb
->ki_flags
& IOCB_NOWAIT
) {
167 /* We could block if there are any pages in the range. */
169 if (filemap_range_has_page(mapping
, pos
, end
))
170 if (filemap_invalidate_inode(inode
, true, pos
, end
))
173 ret
= filemap_write_and_wait_range(mapping
, pos
, end
);
179 * After a write we want buffered reads to be sure to go to disk to get
180 * the new data. We invalidate clean cached page from the region we're
181 * about to write. We do this *before* the write so that we can return
182 * without clobbering -EIOCBQUEUED from ->direct_IO().
184 ret
= filemap_invalidate_inode(inode
, true, pos
, end
);
187 end
= iocb
->ki_pos
+ iov_iter_count(from
);
188 if (end
> ictx
->zero_point
)
189 ictx
->zero_point
= end
;
191 fscache_invalidate(netfs_i_cookie(ictx
), NULL
, i_size_read(inode
),
192 FSCACHE_INVAL_DIO_WRITE
);
193 ret
= netfs_unbuffered_write_iter_locked(iocb
, from
, NULL
);
195 netfs_end_io_direct(inode
);
198 EXPORT_SYMBOL(netfs_unbuffered_write_iter
);