~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/netfs/direct_write.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 (async || 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                         wreq->iter = *iter;
 81                 }
 82 
 83                 wreq->io_iter = wreq->iter;
 84         }
 85 
 86         __set_bit(NETFS_RREQ_USE_IO_ITER, &wreq->flags);
 87 
 88         /* Copy the data into the bounce buffer and encrypt it. */
 89         // TODO
 90 
 91         /* Dispatch the write. */
 92         __set_bit(NETFS_RREQ_UPLOAD_TO_SERVER, &wreq->flags);
 93         if (async)
 94                 wreq->iocb = iocb;
 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);
 98         if (ret < 0) {
 99                 _debug("begin = %zd", ret);
100                 goto out;
101         }
102 
103         if (!async) {
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 */
108                 ret = wreq->error;
109                 if (ret == 0) {
110                         ret = wreq->transferred;
111                         iocb->ki_pos += ret;
112                 }
113         } else {
114                 ret = -EIOCBQUEUED;
115         }
116 
117 out:
118         netfs_put_request(wreq, false, netfs_rreq_trace_put_return);
119         return ret;
120 }
121 EXPORT_SYMBOL(netfs_unbuffered_write_iter_locked);
122 
123 /**
124  * netfs_unbuffered_write_iter - Unbuffered write to a file
125  * @iocb: IO state structure
126  * @from: iov_iter with data to write
127  *
128  * Do an unbuffered write to a file, writing the data directly to the server
129  * and not lodging the data in the pagecache.
130  *
131  * Return:
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
135  */
136 ssize_t netfs_unbuffered_write_iter(struct kiocb *iocb, struct iov_iter *from)
137 {
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);
142         ssize_t ret;
143         loff_t pos = iocb->ki_pos;
144         unsigned long long end = pos + iov_iter_count(from) - 1;
145 
146         _enter("%llx,%zx,%llx", pos, iov_iter_count(from), i_size_read(inode));
147 
148         if (!iov_iter_count(from))
149                 return 0;
150 
151         trace_netfs_write_iter(iocb, from);
152         netfs_stat(&netfs_n_wh_dio_write);
153 
154         ret = netfs_start_io_direct(inode);
155         if (ret < 0)
156                 return ret;
157         ret = generic_write_checks(iocb, from);
158         if (ret <= 0)
159                 goto out;
160         ret = file_remove_privs(file);
161         if (ret < 0)
162                 goto out;
163         ret = file_update_time(file);
164         if (ret < 0)
165                 goto out;
166         if (iocb->ki_flags & IOCB_NOWAIT) {
167                 /* We could block if there are any pages in the range. */
168                 ret = -EAGAIN;
169                 if (filemap_range_has_page(mapping, pos, end))
170                         if (filemap_invalidate_inode(inode, true, pos, end))
171                                 goto out;
172         } else {
173                 ret = filemap_write_and_wait_range(mapping, pos, end);
174                 if (ret < 0)
175                         goto out;
176         }
177 
178         /*
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().
183          */
184         ret = filemap_invalidate_inode(inode, true, pos, end);
185         if (ret < 0)
186                 goto out;
187         end = iocb->ki_pos + iov_iter_count(from);
188         if (end > ictx->zero_point)
189                 ictx->zero_point = end;
190 
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);
194 out:
195         netfs_end_io_direct(inode);
196         return ret;
197 }
198 EXPORT_SYMBOL(netfs_unbuffered_write_iter);
199 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php