Skip to content

Commit 5e1e6ec

Browse files
dhowellsbrauner
authored andcommitted
netfs: Merge i_size update functions
Netfslib has two functions for updating the i_size after a write: one for buffered writes into the pagecache and one for direct/unbuffered writes. However, what needs to be done is much the same in both cases, so merge them together. This does raise one question, though: should updating the i_size after a direct write do the same estimated update of i_blocks as is done for buffered writes. Also get rid of the cleanup function pointer from netfs_io_request as it's only used for direct write to update i_size; instead do the i_size setting directly from write collection. Signed-off-by: David Howells <[email protected]> Link: https://lore.kernel.org/[email protected] cc: Steve French <[email protected]> cc: Paulo Alcantara <[email protected]> cc: [email protected] cc: [email protected] cc: [email protected] Signed-off-by: Christian Brauner <[email protected]>
1 parent 2e06589 commit 5e1e6ec

File tree

5 files changed

+31
-37
lines changed

5 files changed

+31
-37
lines changed

fs/netfs/buffered_write.c

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,38 @@ static struct folio *netfs_grab_folio_for_write(struct address_space *mapping,
5353
* data written into the pagecache until we can find out from the server what
5454
* the values actually are.
5555
*/
56-
static void netfs_update_i_size(struct netfs_inode *ctx, struct inode *inode,
57-
loff_t i_size, loff_t pos, size_t copied)
56+
void netfs_update_i_size(struct netfs_inode *ctx, struct inode *inode,
57+
loff_t pos, size_t copied)
5858
{
59+
loff_t i_size, end = pos + copied;
5960
blkcnt_t add;
6061
size_t gap;
6162

63+
if (end <= i_size_read(inode))
64+
return;
65+
6266
if (ctx->ops->update_i_size) {
63-
ctx->ops->update_i_size(inode, pos);
67+
ctx->ops->update_i_size(inode, end);
6468
return;
6569
}
6670

6771
spin_lock(&inode->i_lock);
68-
i_size_write(inode, pos);
72+
73+
i_size = i_size_read(inode);
74+
if (end > i_size) {
75+
i_size_write(inode, end);
6976
#if IS_ENABLED(CONFIG_FSCACHE)
70-
fscache_update_cookie(ctx->cache, NULL, &pos);
77+
fscache_update_cookie(ctx->cache, NULL, &end);
7178
#endif
7279

73-
gap = SECTOR_SIZE - (i_size & (SECTOR_SIZE - 1));
74-
if (copied > gap) {
75-
add = DIV_ROUND_UP(copied - gap, SECTOR_SIZE);
80+
gap = SECTOR_SIZE - (i_size & (SECTOR_SIZE - 1));
81+
if (copied > gap) {
82+
add = DIV_ROUND_UP(copied - gap, SECTOR_SIZE);
7683

77-
inode->i_blocks = min_t(blkcnt_t,
78-
DIV_ROUND_UP(pos, SECTOR_SIZE),
79-
inode->i_blocks + add);
84+
inode->i_blocks = min_t(blkcnt_t,
85+
DIV_ROUND_UP(end, SECTOR_SIZE),
86+
inode->i_blocks + add);
87+
}
8088
}
8189
spin_unlock(&inode->i_lock);
8290
}
@@ -113,7 +121,7 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,
113121
struct folio *folio = NULL, *writethrough = NULL;
114122
unsigned int bdp_flags = (iocb->ki_flags & IOCB_NOWAIT) ? BDP_ASYNC : 0;
115123
ssize_t written = 0, ret, ret2;
116-
loff_t i_size, pos = iocb->ki_pos;
124+
loff_t pos = iocb->ki_pos;
117125
size_t max_chunk = mapping_max_folio_size(mapping);
118126
bool maybe_trouble = false;
119127

@@ -346,10 +354,8 @@ ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,
346354
flush_dcache_folio(folio);
347355

348356
/* Update the inode size if we moved the EOF marker */
357+
netfs_update_i_size(ctx, inode, pos, copied);
349358
pos += copied;
350-
i_size = i_size_read(inode);
351-
if (pos > i_size)
352-
netfs_update_i_size(ctx, inode, i_size, pos, copied);
353359
written += copied;
354360

355361
if (likely(!wreq)) {

fs/netfs/direct_write.c

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,6 @@
99
#include <linux/uio.h>
1010
#include "internal.h"
1111

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 || end <= i_size_read(inode))
18-
return;
19-
20-
spin_lock(&inode->i_lock);
21-
if (end > i_size_read(inode)) {
22-
if (wreq->netfs_ops->update_i_size)
23-
wreq->netfs_ops->update_i_size(inode, end);
24-
else
25-
i_size_write(inode, end);
26-
}
27-
spin_unlock(&inode->i_lock);
28-
}
29-
3012
/*
3113
* Perform an unbuffered write where we may have to do an RMW operation on an
3214
* encrypted file. This can also be used for direct I/O writes.
@@ -102,7 +84,6 @@ ssize_t netfs_unbuffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *
10284
if (async)
10385
wreq->iocb = iocb;
10486
wreq->len = iov_iter_count(&wreq->buffer.iter);
105-
wreq->cleanup = netfs_cleanup_dio_write;
10687
ret = netfs_unbuffered_write(wreq, is_sync_kiocb(iocb), wreq->len);
10788
if (ret < 0) {
10889
_debug("begin = %zd", ret);

fs/netfs/internal.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ void netfs_cache_read_terminated(void *priv, ssize_t transferred_or_error);
2727
int netfs_prefetch_for_write(struct file *file, struct folio *folio,
2828
size_t offset, size_t len);
2929

30+
/*
31+
* buffered_write.c
32+
*/
33+
void netfs_update_i_size(struct netfs_inode *ctx, struct inode *inode,
34+
loff_t pos, size_t copied);
35+
3036
/*
3137
* main.c
3238
*/

fs/netfs/write_collect.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,10 @@ bool netfs_write_collection(struct netfs_io_request *wreq)
393393
ictx->ops->invalidate_cache(wreq);
394394
}
395395

396-
if (wreq->cleanup)
397-
wreq->cleanup(wreq);
396+
if ((wreq->origin == NETFS_UNBUFFERED_WRITE ||
397+
wreq->origin == NETFS_DIO_WRITE) &&
398+
!wreq->error)
399+
netfs_update_i_size(ictx, &ictx->inode, wreq->start, wreq->transferred);
398400

399401
if (wreq->origin == NETFS_DIO_WRITE &&
400402
wreq->mapping->nrpages) {

include/linux/netfs.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ struct netfs_io_request {
279279
#define NETFS_RREQ_USE_PGPRIV2 31 /* [DEPRECATED] Use PG_private_2 to mark
280280
* write to cache on read */
281281
const struct netfs_request_ops *netfs_ops;
282-
void (*cleanup)(struct netfs_io_request *req);
283282
};
284283

285284
/*

0 commit comments

Comments
 (0)