2021-04-07 21:22:45

by David Howells

[permalink] [raw]
Subject: [PATCH 0/5] netfs: Fixes for the netfs lib


Hi Jeff,

Here's a bunch of fixes plus a tracepoint for the netfs library. I'm going
to roll them into other patches, but I'm posting them here for separate
review.

David
---
David Howells (5):
netfs: Fix a missing rreq put in netfs_write_begin()
netfs: Call trace_netfs_read() after ->begin_cache_operation()
netfs: Don't record the copy termination error
netfs: Fix copy-to-cache amalgamation
netfs: Add a tracepoint to log failures that would be otherwise unseen


fs/cachefiles/io.c | 17 ++++++++++
fs/netfs/read_helper.c | 58 +++++++++++++++++++---------------
include/linux/netfs.h | 6 ++++
include/trace/events/netfs.h | 60 ++++++++++++++++++++++++++++++++++++
4 files changed, 116 insertions(+), 25 deletions(-)



2021-04-07 21:22:57

by David Howells

[permalink] [raw]
Subject: [PATCH 1/5] netfs: Fix a missing rreq put in netfs_write_begin()

netfs_write_begin() needs to drop a ref on the read request if the network
filesystem gives an error when called to begin the caching op.

Signed-off-by: David Howells <[email protected]>
---

fs/netfs/read_helper.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/fs/netfs/read_helper.c b/fs/netfs/read_helper.c
index 3498bde035eb..0066db21aa11 100644
--- a/fs/netfs/read_helper.c
+++ b/fs/netfs/read_helper.c
@@ -1169,6 +1169,8 @@ int netfs_write_begin(struct file *file, struct address_space *mapping,
_leave(" = 0");
return 0;

+error_put:
+ netfs_put_read_request(rreq, false);
error:
unlock_page(page);
put_page(page);


2021-04-07 21:23:16

by David Howells

[permalink] [raw]
Subject: [PATCH 3/5] netfs: Don't record the copy termination error

Don't record the copy termination error in the subrequest. We shouldn't
return it through netfs_readpage() or netfs_write_begin() as we don't let
the netfs see cache errors.

Signed-off-by: David Howells <[email protected]>
---

fs/netfs/read_helper.c | 2 --
1 file changed, 2 deletions(-)

diff --git a/fs/netfs/read_helper.c b/fs/netfs/read_helper.c
index 8040b76da1b6..ad0dc01319ce 100644
--- a/fs/netfs/read_helper.c
+++ b/fs/netfs/read_helper.c
@@ -270,10 +270,8 @@ static void netfs_rreq_copy_terminated(void *priv, ssize_t transferred_or_error,
struct netfs_read_request *rreq = subreq->rreq;

if (IS_ERR_VALUE(transferred_or_error)) {
- subreq->error = transferred_or_error;
netfs_stat(&netfs_n_rh_write_failed);
} else {
- subreq->error = 0;
netfs_stat(&netfs_n_rh_write_done);
}



2021-04-07 21:23:17

by David Howells

[permalink] [raw]
Subject: [PATCH 4/5] netfs: Fix copy-to-cache amalgamation

Fix the amalgamation of subrequests when copying to the cache. We
shouldn't be rounding up the size to PAGE_SIZE as we go along as that ends
up with the composite subrequest length being too long - and this leads to
EIO from the cache write because the source iterator doesn't contain enough
data.

Instead, we only need to deal with contiguous subreqs and then ask the
cache to round off as it needs - which also means we don't have to make any
assumptions about the cache granularity.

Signed-off-by: David Howells <[email protected]>
---

fs/cachefiles/io.c | 17 +++++++++++++++++
fs/netfs/read_helper.c | 19 +++++++++----------
include/linux/netfs.h | 6 ++++++
include/trace/events/netfs.h | 2 ++
4 files changed, 34 insertions(+), 10 deletions(-)

diff --git a/fs/cachefiles/io.c b/fs/cachefiles/io.c
index 620959d1e95b..b13fb45fc3f3 100644
--- a/fs/cachefiles/io.c
+++ b/fs/cachefiles/io.c
@@ -330,6 +330,22 @@ static enum netfs_read_source cachefiles_prepare_read(struct netfs_read_subreque
return NETFS_DOWNLOAD_FROM_SERVER;
}

+/*
+ * Prepare for a write to occur.
+ */
+static int cachefiles_prepare_write(struct netfs_cache_resources *cres,
+ loff_t *_start, size_t *_len, loff_t i_size)
+{
+ loff_t start = *_start;
+ size_t len = *_len, down;
+
+ /* Round to DIO size */
+ down = start - round_down(start, PAGE_SIZE);
+ *_start = start - down;
+ *_len = round_up(down + len, PAGE_SIZE);
+ return 0;
+}
+
/*
* Clean up an operation.
*/
@@ -355,6 +371,7 @@ static const struct netfs_cache_ops cachefiles_netfs_cache_ops = {
.read = cachefiles_read,
.write = cachefiles_write,
.prepare_read = cachefiles_prepare_read,
+ .prepare_write = cachefiles_prepare_write,
};

/*
diff --git a/fs/netfs/read_helper.c b/fs/netfs/read_helper.c
index ad0dc01319ce..ce2f31d20250 100644
--- a/fs/netfs/read_helper.c
+++ b/fs/netfs/read_helper.c
@@ -293,7 +293,7 @@ static void netfs_rreq_do_write_to_cache(struct netfs_read_request *rreq)
struct netfs_cache_resources *cres = &rreq->cache_resources;
struct netfs_read_subrequest *subreq, *next, *p;
struct iov_iter iter;
- loff_t pos;
+ int ret;

trace_netfs_rreq(rreq, netfs_rreq_trace_write);

@@ -311,23 +311,22 @@ static void netfs_rreq_do_write_to_cache(struct netfs_read_request *rreq)

list_for_each_entry(subreq, &rreq->subrequests, rreq_link) {
/* Amalgamate adjacent writes */
- pos = round_down(subreq->start, PAGE_SIZE);
- if (pos != subreq->start) {
- subreq->len += subreq->start - pos;
- subreq->start = pos;
- }
- subreq->len = round_up(subreq->len, PAGE_SIZE);
-
while (!list_is_last(&subreq->rreq_link, &rreq->subrequests)) {
next = list_next_entry(subreq, rreq_link);
- if (next->start > subreq->start + subreq->len)
+ if (next->start != subreq->start + subreq->len)
break;
subreq->len += next->len;
- subreq->len = round_up(subreq->len, PAGE_SIZE);
list_del_init(&next->rreq_link);
netfs_put_subrequest(next, false);
}

+ ret = cres->ops->prepare_write(cres, &subreq->start, &subreq->len,
+ rreq->i_size);
+ if (ret < 0) {
+ trace_netfs_sreq(subreq, netfs_sreq_trace_write_skip);
+ continue;
+ }
+
iov_iter_xarray(&iter, WRITE, &rreq->mapping->i_pages,
subreq->start, subreq->len);

diff --git a/include/linux/netfs.h b/include/linux/netfs.h
index 2299e7662ff0..9062adfa2fb9 100644
--- a/include/linux/netfs.h
+++ b/include/linux/netfs.h
@@ -206,6 +206,12 @@ struct netfs_cache_ops {
*/
enum netfs_read_source (*prepare_read)(struct netfs_read_subrequest *subreq,
loff_t i_size);
+
+ /* Prepare a write operation, working out what part of the write we can
+ * actually do.
+ */
+ int (*prepare_write)(struct netfs_cache_resources *cres,
+ loff_t *_start, size_t *_len, loff_t i_size);
};

struct readahead_control;
diff --git a/include/trace/events/netfs.h b/include/trace/events/netfs.h
index a2bf6cd84bd4..e3ebeabd3852 100644
--- a/include/trace/events/netfs.h
+++ b/include/trace/events/netfs.h
@@ -43,6 +43,7 @@ enum netfs_sreq_trace {
netfs_sreq_trace_submit,
netfs_sreq_trace_terminated,
netfs_sreq_trace_write,
+ netfs_sreq_trace_write_skip,
netfs_sreq_trace_write_term,
};

@@ -77,6 +78,7 @@ enum netfs_sreq_trace {
EM(netfs_sreq_trace_submit, "SUBMT") \
EM(netfs_sreq_trace_terminated, "TERM ") \
EM(netfs_sreq_trace_write, "WRITE") \
+ EM(netfs_sreq_trace_write_skip, "SKIP ") \
E_(netfs_sreq_trace_write_term, "WTERM")




2021-04-07 21:23:46

by David Howells

[permalink] [raw]
Subject: [PATCH 5/5] netfs: Add a tracepoint to log failures that would be otherwise unseen

Add a tracepoint to log internal failures (such as cache errors) that we
don't otherwise want to pass back to the netfs.

Signed-off-by: David Howells <[email protected]>
---

fs/netfs/read_helper.c | 14 +++++++++-
include/trace/events/netfs.h | 58 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 70 insertions(+), 2 deletions(-)

diff --git a/fs/netfs/read_helper.c b/fs/netfs/read_helper.c
index ce2f31d20250..762a15350242 100644
--- a/fs/netfs/read_helper.c
+++ b/fs/netfs/read_helper.c
@@ -271,6 +271,8 @@ static void netfs_rreq_copy_terminated(void *priv, ssize_t transferred_or_error,

if (IS_ERR_VALUE(transferred_or_error)) {
netfs_stat(&netfs_n_rh_write_failed);
+ trace_netfs_failure(rreq, subreq, transferred_or_error,
+ netfs_fail_copy_to_cache);
} else {
netfs_stat(&netfs_n_rh_write_done);
}
@@ -323,6 +325,7 @@ static void netfs_rreq_do_write_to_cache(struct netfs_read_request *rreq)
ret = cres->ops->prepare_write(cres, &subreq->start, &subreq->len,
rreq->i_size);
if (ret < 0) {
+ trace_netfs_failure(rreq, subreq, ret, netfs_fail_prepare_write);
trace_netfs_sreq(subreq, netfs_sreq_trace_write_skip);
continue;
}
@@ -627,6 +630,8 @@ void netfs_subreq_terminated(struct netfs_read_subrequest *subreq,

if (IS_ERR_VALUE(transferred_or_error)) {
subreq->error = transferred_or_error;
+ trace_netfs_failure(rreq, subreq, transferred_or_error,
+ netfs_fail_read);
goto failed;
}

@@ -996,8 +1001,10 @@ int netfs_readpage(struct file *file,
} while (test_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags));

ret = rreq->error;
- if (ret == 0 && rreq->submitted < rreq->len)
+ if (ret == 0 && rreq->submitted < rreq->len) {
+ trace_netfs_failure(rreq, NULL, ret, netfs_fail_short_readpage);
ret = -EIO;
+ }
out:
netfs_put_read_request(rreq, false);
return ret;
@@ -1074,6 +1081,7 @@ int netfs_write_begin(struct file *file, struct address_space *mapping,
/* Allow the netfs (eg. ceph) to flush conflicts. */
ret = ops->check_write_begin(file, pos, len, page, _fsdata);
if (ret < 0) {
+ trace_netfs_failure(NULL, NULL, ret, netfs_fail_check_write_begin);
if (ret == -EAGAIN)
goto retry;
goto error;
@@ -1150,8 +1158,10 @@ int netfs_write_begin(struct file *file, struct address_space *mapping,
}

ret = rreq->error;
- if (ret == 0 && rreq->submitted < rreq->len)
+ if (ret == 0 && rreq->submitted < rreq->len) {
+ trace_netfs_failure(rreq, NULL, ret, netfs_fail_short_write_begin);
ret = -EIO;
+ }
netfs_put_read_request(rreq, false);
if (ret < 0)
goto error;
diff --git a/include/trace/events/netfs.h b/include/trace/events/netfs.h
index e3ebeabd3852..de1c64635e42 100644
--- a/include/trace/events/netfs.h
+++ b/include/trace/events/netfs.h
@@ -47,6 +47,15 @@ enum netfs_sreq_trace {
netfs_sreq_trace_write_term,
};

+enum netfs_failure {
+ netfs_fail_check_write_begin,
+ netfs_fail_copy_to_cache,
+ netfs_fail_read,
+ netfs_fail_short_readpage,
+ netfs_fail_short_write_begin,
+ netfs_fail_prepare_write,
+};
+
#endif

#define netfs_read_traces \
@@ -81,6 +90,14 @@ enum netfs_sreq_trace {
EM(netfs_sreq_trace_write_skip, "SKIP ") \
E_(netfs_sreq_trace_write_term, "WTERM")

+#define netfs_failures \
+ EM(netfs_fail_check_write_begin, "check-write-begin") \
+ EM(netfs_fail_copy_to_cache, "copy-to-cache") \
+ EM(netfs_fail_read, "read") \
+ EM(netfs_fail_short_readpage, "short-readpage") \
+ EM(netfs_fail_short_write_begin, "short-write-begin") \
+ E_(netfs_fail_prepare_write, "prep-write")
+

/*
* Export enum symbols via userspace.
@@ -94,6 +111,7 @@ netfs_read_traces;
netfs_rreq_traces;
netfs_sreq_sources;
netfs_sreq_traces;
+netfs_failures;

/*
* Now redefine the EM() and E_() macros to map the enums to the strings that
@@ -197,6 +215,46 @@ TRACE_EVENT(netfs_sreq,
__entry->error)
);

+TRACE_EVENT(netfs_failure,
+ TP_PROTO(struct netfs_read_request *rreq,
+ struct netfs_read_subrequest *sreq,
+ int error, enum netfs_failure what),
+
+ TP_ARGS(rreq, sreq, error, what),
+
+ TP_STRUCT__entry(
+ __field(unsigned int, rreq )
+ __field(unsigned short, index )
+ __field(short, error )
+ __field(unsigned short, flags )
+ __field(enum netfs_read_source, source )
+ __field(enum netfs_failure, what )
+ __field(size_t, len )
+ __field(size_t, transferred )
+ __field(loff_t, start )
+ ),
+
+ TP_fast_assign(
+ __entry->rreq = rreq->debug_id;
+ __entry->index = sreq ? sreq->debug_index : 0;
+ __entry->error = error;
+ __entry->flags = sreq ? sreq->flags : 0;
+ __entry->source = sreq ? sreq->source : NETFS_INVALID_READ;
+ __entry->what = what;
+ __entry->len = sreq ? sreq->len : 0;
+ __entry->transferred = sreq ? sreq->transferred : 0;
+ __entry->start = sreq ? sreq->start : 0;
+ ),
+
+ TP_printk("R=%08x[%u] %s f=%02x s=%llx %zx/%zx %s e=%d",
+ __entry->rreq, __entry->index,
+ __print_symbolic(__entry->source, netfs_sreq_sources),
+ __entry->flags,
+ __entry->start, __entry->transferred, __entry->len,
+ __print_symbolic(__entry->what, netfs_failures),
+ __entry->error)
+ );
+
#endif /* _TRACE_NETFS_H */

/* This part must be outside protection */


2021-04-07 21:25:05

by David Howells

[permalink] [raw]
Subject: [PATCH 2/5] netfs: Call trace_netfs_read() after ->begin_cache_operation()

Reorder the netfs library API functions slightly to log the netfs_read
tracepoint after calling out to the network filesystem to begin a caching
operation. This sets rreq->cookie_debug_id so that it can be logged in
tracepoints.

Signed-off-by: David Howells <[email protected]>
---

fs/netfs/read_helper.c | 23 ++++++++++++-----------
1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/fs/netfs/read_helper.c b/fs/netfs/read_helper.c
index 0066db21aa11..8040b76da1b6 100644
--- a/fs/netfs/read_helper.c
+++ b/fs/netfs/read_helper.c
@@ -890,15 +890,16 @@ void netfs_readahead(struct readahead_control *ractl,
rreq->start = readahead_pos(ractl);
rreq->len = readahead_length(ractl);

- netfs_stat(&netfs_n_rh_readahead);
- trace_netfs_read(rreq, readahead_pos(ractl), readahead_length(ractl),
- netfs_read_trace_readahead);
-
if (ops->begin_cache_operation) {
ret = ops->begin_cache_operation(rreq);
if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
goto cleanup_free;
}
+
+ netfs_stat(&netfs_n_rh_readahead);
+ trace_netfs_read(rreq, readahead_pos(ractl), readahead_length(ractl),
+ netfs_read_trace_readahead);
+
netfs_rreq_expand(rreq, ractl);

atomic_set(&rreq->nr_rd_ops, 1);
@@ -968,9 +969,6 @@ int netfs_readpage(struct file *file,
rreq->start = page_index(page) * PAGE_SIZE;
rreq->len = thp_size(page);

- netfs_stat(&netfs_n_rh_readpage);
- trace_netfs_read(rreq, rreq->start, rreq->len, netfs_read_trace_readpage);
-
if (ops->begin_cache_operation) {
ret = ops->begin_cache_operation(rreq);
if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS) {
@@ -979,6 +977,9 @@ int netfs_readpage(struct file *file,
}
}

+ netfs_stat(&netfs_n_rh_readpage);
+ trace_netfs_read(rreq, rreq->start, rreq->len, netfs_read_trace_readpage);
+
netfs_get_read_request(rreq);

atomic_set(&rreq->nr_rd_ops, 1);
@@ -1111,15 +1112,15 @@ int netfs_write_begin(struct file *file, struct address_space *mapping,
__set_bit(NETFS_RREQ_NO_UNLOCK_PAGE, &rreq->flags);
netfs_priv = NULL;

- netfs_stat(&netfs_n_rh_write_begin);
- trace_netfs_read(rreq, pos, len, netfs_read_trace_write_begin);
-
if (ops->begin_cache_operation) {
ret = ops->begin_cache_operation(rreq);
if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
- goto error;
+ goto error_put;
}

+ netfs_stat(&netfs_n_rh_write_begin);
+ trace_netfs_read(rreq, pos, len, netfs_read_trace_write_begin);
+
/* Expand the request to meet caching requirements and download
* preferences.
*/


2021-04-07 22:02:50

by Jeff Layton

[permalink] [raw]
Subject: Re: [PATCH 0/5] netfs: Fixes for the netfs lib

On Wed, 2021-04-07 at 16:46 +0100, David Howells wrote:
> Hi Jeff,
>
> Here's a bunch of fixes plus a tracepoint for the netfs library. I'm going
> to roll them into other patches, but I'm posting them here for separate
> review.
>
> David
> ---
> David Howells (5):
> netfs: Fix a missing rreq put in netfs_write_begin()
> netfs: Call trace_netfs_read() after ->begin_cache_operation()
> netfs: Don't record the copy termination error
> netfs: Fix copy-to-cache amalgamation
> netfs: Add a tracepoint to log failures that would be otherwise unseen
>
>
> fs/cachefiles/io.c | 17 ++++++++++
> fs/netfs/read_helper.c | 58 +++++++++++++++++++---------------
> include/linux/netfs.h | 6 ++++
> include/trace/events/netfs.h | 60 ++++++++++++++++++++++++++++++++++++
> 4 files changed, 116 insertions(+), 25 deletions(-)
>
>

Thanks David,

I rebased onto your branch and gave ceph a spin with fscache and it all
worked fine. Let me know when you get those rolled into your branch and
I'll rebase the ceph/testing branch on top of it.

Cheers,
--
Jeff Layton <[email protected]>