Skip to content

Commit 3a4a38e

Browse files
committed
netfs: Split netfs_io_* object handling out
Split netfs_io_* object handling out into a file that's going to contain object allocation, get and put routines. Signed-off-by: David Howells <dhowells@redhat.com> Reviewed-by: Jeff Layton <jlayton@redhat.com> cc: linux-cachefs@redhat.com Link: https://lore.kernel.org/r/164622995118.3564931.6089530629052064470.stgit@warthog.procyon.org.uk/ # v1 Link: https://lore.kernel.org/r/164678197044.1200972.11511937252083343775.stgit@warthog.procyon.org.uk/ # v2 Link: https://lore.kernel.org/r/164692894693.2099075.7831091294248735173.stgit@warthog.procyon.org.uk/ # v3
1 parent f18a378 commit 3a4a38e

4 files changed

Lines changed: 147 additions & 118 deletions

File tree

fs/netfs/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# SPDX-License-Identifier: GPL-2.0
22

3-
netfs-y := read_helper.o stats.o
3+
netfs-y := \
4+
objects.o \
5+
read_helper.o
6+
7+
netfs-$(CONFIG_NETFS_STATS) += stats.o
48

59
obj-$(CONFIG_NETFS_SUPPORT) := netfs.o

fs/netfs/internal.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,35 @@
55
* Written by David Howells (dhowells@redhat.com)
66
*/
77

8+
#include <linux/netfs.h>
9+
#include <trace/events/netfs.h>
10+
811
#ifdef pr_fmt
912
#undef pr_fmt
1013
#endif
1114

1215
#define pr_fmt(fmt) "netfs: " fmt
1316

17+
/*
18+
* objects.c
19+
*/
20+
struct netfs_io_request *netfs_alloc_request(const struct netfs_request_ops *ops,
21+
void *netfs_priv,
22+
struct file *file);
23+
void netfs_get_request(struct netfs_io_request *rreq);
24+
void netfs_clear_subrequests(struct netfs_io_request *rreq, bool was_async);
25+
void netfs_put_request(struct netfs_io_request *rreq, bool was_async);
26+
struct netfs_io_subrequest *netfs_alloc_subrequest(struct netfs_io_request *rreq);
27+
void netfs_put_subrequest(struct netfs_io_subrequest *subreq, bool was_async);
28+
void netfs_get_subrequest(struct netfs_io_subrequest *subreq);
29+
1430
/*
1531
* read_helper.c
1632
*/
1733
extern unsigned int netfs_debug;
1834

35+
void netfs_rreq_work(struct work_struct *work);
36+
1937
/*
2038
* stats.c
2139
*/

fs/netfs/objects.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
/* Object lifetime handling and tracing.
3+
*
4+
* Copyright (C) 2022 Red Hat, Inc. All Rights Reserved.
5+
* Written by David Howells (dhowells@redhat.com)
6+
*/
7+
8+
#include <linux/slab.h>
9+
#include "internal.h"
10+
11+
/*
12+
* Allocate an I/O request and initialise it.
13+
*/
14+
struct netfs_io_request *netfs_alloc_request(
15+
const struct netfs_request_ops *ops, void *netfs_priv,
16+
struct file *file)
17+
{
18+
static atomic_t debug_ids;
19+
struct netfs_io_request *rreq;
20+
21+
rreq = kzalloc(sizeof(struct netfs_io_request), GFP_KERNEL);
22+
if (rreq) {
23+
rreq->netfs_ops = ops;
24+
rreq->netfs_priv = netfs_priv;
25+
rreq->inode = file_inode(file);
26+
rreq->i_size = i_size_read(rreq->inode);
27+
rreq->debug_id = atomic_inc_return(&debug_ids);
28+
INIT_LIST_HEAD(&rreq->subrequests);
29+
INIT_WORK(&rreq->work, netfs_rreq_work);
30+
refcount_set(&rreq->usage, 1);
31+
__set_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags);
32+
if (ops->init_request)
33+
ops->init_request(rreq, file);
34+
netfs_stat(&netfs_n_rh_rreq);
35+
}
36+
37+
return rreq;
38+
}
39+
40+
void netfs_get_request(struct netfs_io_request *rreq)
41+
{
42+
refcount_inc(&rreq->usage);
43+
}
44+
45+
void netfs_clear_subrequests(struct netfs_io_request *rreq, bool was_async)
46+
{
47+
struct netfs_io_subrequest *subreq;
48+
49+
while (!list_empty(&rreq->subrequests)) {
50+
subreq = list_first_entry(&rreq->subrequests,
51+
struct netfs_io_subrequest, rreq_link);
52+
list_del(&subreq->rreq_link);
53+
netfs_put_subrequest(subreq, was_async);
54+
}
55+
}
56+
57+
static void netfs_free_request(struct work_struct *work)
58+
{
59+
struct netfs_io_request *rreq =
60+
container_of(work, struct netfs_io_request, work);
61+
netfs_clear_subrequests(rreq, false);
62+
if (rreq->netfs_priv)
63+
rreq->netfs_ops->cleanup(rreq->mapping, rreq->netfs_priv);
64+
trace_netfs_rreq(rreq, netfs_rreq_trace_free);
65+
if (rreq->cache_resources.ops)
66+
rreq->cache_resources.ops->end_operation(&rreq->cache_resources);
67+
kfree(rreq);
68+
netfs_stat_d(&netfs_n_rh_rreq);
69+
}
70+
71+
void netfs_put_request(struct netfs_io_request *rreq, bool was_async)
72+
{
73+
if (refcount_dec_and_test(&rreq->usage)) {
74+
if (was_async) {
75+
rreq->work.func = netfs_free_request;
76+
if (!queue_work(system_unbound_wq, &rreq->work))
77+
BUG();
78+
} else {
79+
netfs_free_request(&rreq->work);
80+
}
81+
}
82+
}
83+
84+
/*
85+
* Allocate and partially initialise an I/O request structure.
86+
*/
87+
struct netfs_io_subrequest *netfs_alloc_subrequest(struct netfs_io_request *rreq)
88+
{
89+
struct netfs_io_subrequest *subreq;
90+
91+
subreq = kzalloc(sizeof(struct netfs_io_subrequest), GFP_KERNEL);
92+
if (subreq) {
93+
INIT_LIST_HEAD(&subreq->rreq_link);
94+
refcount_set(&subreq->usage, 2);
95+
subreq->rreq = rreq;
96+
netfs_get_request(rreq);
97+
netfs_stat(&netfs_n_rh_sreq);
98+
}
99+
100+
return subreq;
101+
}
102+
103+
void netfs_get_subrequest(struct netfs_io_subrequest *subreq)
104+
{
105+
refcount_inc(&subreq->usage);
106+
}
107+
108+
static void __netfs_put_subrequest(struct netfs_io_subrequest *subreq,
109+
bool was_async)
110+
{
111+
struct netfs_io_request *rreq = subreq->rreq;
112+
113+
trace_netfs_sreq(subreq, netfs_sreq_trace_free);
114+
kfree(subreq);
115+
netfs_stat_d(&netfs_n_rh_sreq);
116+
netfs_put_request(rreq, was_async);
117+
}
118+
119+
void netfs_put_subrequest(struct netfs_io_subrequest *subreq, bool was_async)
120+
{
121+
if (refcount_dec_and_test(&subreq->usage))
122+
__netfs_put_subrequest(subreq, was_async);
123+
}

fs/netfs/read_helper.c

Lines changed: 1 addition & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -27,122 +27,6 @@ unsigned netfs_debug;
2727
module_param_named(debug, netfs_debug, uint, S_IWUSR | S_IRUGO);
2828
MODULE_PARM_DESC(netfs_debug, "Netfs support debugging mask");
2929

30-
static void netfs_rreq_work(struct work_struct *);
31-
static void __netfs_put_subrequest(struct netfs_io_subrequest *, bool);
32-
33-
static void netfs_put_subrequest(struct netfs_io_subrequest *subreq,
34-
bool was_async)
35-
{
36-
if (refcount_dec_and_test(&subreq->usage))
37-
__netfs_put_subrequest(subreq, was_async);
38-
}
39-
40-
static struct netfs_io_request *netfs_alloc_request(
41-
const struct netfs_request_ops *ops, void *netfs_priv,
42-
struct file *file)
43-
{
44-
static atomic_t debug_ids;
45-
struct netfs_io_request *rreq;
46-
47-
rreq = kzalloc(sizeof(struct netfs_io_request), GFP_KERNEL);
48-
if (rreq) {
49-
rreq->netfs_ops = ops;
50-
rreq->netfs_priv = netfs_priv;
51-
rreq->inode = file_inode(file);
52-
rreq->i_size = i_size_read(rreq->inode);
53-
rreq->debug_id = atomic_inc_return(&debug_ids);
54-
INIT_LIST_HEAD(&rreq->subrequests);
55-
INIT_WORK(&rreq->work, netfs_rreq_work);
56-
refcount_set(&rreq->usage, 1);
57-
__set_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags);
58-
if (ops->init_request)
59-
ops->init_request(rreq, file);
60-
netfs_stat(&netfs_n_rh_rreq);
61-
}
62-
63-
return rreq;
64-
}
65-
66-
static void netfs_get_request(struct netfs_io_request *rreq)
67-
{
68-
refcount_inc(&rreq->usage);
69-
}
70-
71-
static void netfs_clear_subrequests(struct netfs_io_request *rreq, bool was_async)
72-
{
73-
struct netfs_io_subrequest *subreq;
74-
75-
while (!list_empty(&rreq->subrequests)) {
76-
subreq = list_first_entry(&rreq->subrequests,
77-
struct netfs_io_subrequest, rreq_link);
78-
list_del(&subreq->rreq_link);
79-
netfs_put_subrequest(subreq, was_async);
80-
}
81-
}
82-
83-
static void netfs_free_request(struct work_struct *work)
84-
{
85-
struct netfs_io_request *rreq =
86-
container_of(work, struct netfs_io_request, work);
87-
netfs_clear_subrequests(rreq, false);
88-
if (rreq->netfs_priv)
89-
rreq->netfs_ops->cleanup(rreq->mapping, rreq->netfs_priv);
90-
trace_netfs_rreq(rreq, netfs_rreq_trace_free);
91-
if (rreq->cache_resources.ops)
92-
rreq->cache_resources.ops->end_operation(&rreq->cache_resources);
93-
kfree(rreq);
94-
netfs_stat_d(&netfs_n_rh_rreq);
95-
}
96-
97-
static void netfs_put_request(struct netfs_io_request *rreq, bool was_async)
98-
{
99-
if (refcount_dec_and_test(&rreq->usage)) {
100-
if (was_async) {
101-
rreq->work.func = netfs_free_request;
102-
if (!queue_work(system_unbound_wq, &rreq->work))
103-
BUG();
104-
} else {
105-
netfs_free_request(&rreq->work);
106-
}
107-
}
108-
}
109-
110-
/*
111-
* Allocate and partially initialise an I/O request structure.
112-
*/
113-
static struct netfs_io_subrequest *netfs_alloc_subrequest(
114-
struct netfs_io_request *rreq)
115-
{
116-
struct netfs_io_subrequest *subreq;
117-
118-
subreq = kzalloc(sizeof(struct netfs_io_subrequest), GFP_KERNEL);
119-
if (subreq) {
120-
INIT_LIST_HEAD(&subreq->rreq_link);
121-
refcount_set(&subreq->usage, 2);
122-
subreq->rreq = rreq;
123-
netfs_get_request(rreq);
124-
netfs_stat(&netfs_n_rh_sreq);
125-
}
126-
127-
return subreq;
128-
}
129-
130-
static void netfs_get_subrequest(struct netfs_io_subrequest *subreq)
131-
{
132-
refcount_inc(&subreq->usage);
133-
}
134-
135-
static void __netfs_put_subrequest(struct netfs_io_subrequest *subreq,
136-
bool was_async)
137-
{
138-
struct netfs_io_request *rreq = subreq->rreq;
139-
140-
trace_netfs_sreq(subreq, netfs_sreq_trace_free);
141-
kfree(subreq);
142-
netfs_stat_d(&netfs_n_rh_sreq);
143-
netfs_put_request(rreq, was_async);
144-
}
145-
14630
/*
14731
* Clear the unread part of an I/O request.
14832
*/
@@ -558,7 +442,7 @@ static void netfs_rreq_assess(struct netfs_io_request *rreq, bool was_async)
558442
netfs_rreq_completed(rreq, was_async);
559443
}
560444

561-
static void netfs_rreq_work(struct work_struct *work)
445+
void netfs_rreq_work(struct work_struct *work)
562446
{
563447
struct netfs_io_request *rreq =
564448
container_of(work, struct netfs_io_request, work);

0 commit comments

Comments
 (0)