afs: Provide a splice-read wrapper
[linux-block.git] / fs / netfs / objects.c
CommitLineData
3a4a38e6
DH
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 */
663dfb65
DH
14struct netfs_io_request *netfs_alloc_request(struct address_space *mapping,
15 struct file *file,
663dfb65
DH
16 loff_t start, size_t len,
17 enum netfs_io_origin origin)
3a4a38e6
DH
18{
19 static atomic_t debug_ids;
bc899ee1 20 struct inode *inode = file ? file_inode(file) : mapping->host;
874c8ca1 21 struct netfs_inode *ctx = netfs_inode(inode);
3a4a38e6 22 struct netfs_io_request *rreq;
2de16041 23 int ret;
3a4a38e6
DH
24
25 rreq = kzalloc(sizeof(struct netfs_io_request), GFP_KERNEL);
2de16041
DH
26 if (!rreq)
27 return ERR_PTR(-ENOMEM);
28
29 rreq->start = start;
30 rreq->len = len;
31 rreq->origin = origin;
bc899ee1 32 rreq->netfs_ops = ctx->ops;
2de16041 33 rreq->mapping = mapping;
bc899ee1
DH
34 rreq->inode = inode;
35 rreq->i_size = i_size_read(inode);
2de16041
DH
36 rreq->debug_id = atomic_inc_return(&debug_ids);
37 INIT_LIST_HEAD(&rreq->subrequests);
2de16041
DH
38 refcount_set(&rreq->ref, 1);
39 __set_bit(NETFS_RREQ_IN_PROGRESS, &rreq->flags);
40 if (rreq->netfs_ops->init_request) {
41 ret = rreq->netfs_ops->init_request(rreq, file);
42 if (ret < 0) {
43 kfree(rreq);
44 return ERR_PTR(ret);
45 }
3a4a38e6
DH
46 }
47
2de16041 48 netfs_stat(&netfs_n_rh_rreq);
3a4a38e6
DH
49 return rreq;
50}
51
de74023b 52void netfs_get_request(struct netfs_io_request *rreq, enum netfs_rreq_ref_trace what)
3a4a38e6 53{
de74023b
DH
54 int r;
55
56 __refcount_inc(&rreq->ref, &r);
57 trace_netfs_rreq_ref(rreq->debug_id, r + 1, what);
3a4a38e6
DH
58}
59
60void netfs_clear_subrequests(struct netfs_io_request *rreq, bool was_async)
61{
62 struct netfs_io_subrequest *subreq;
63
64 while (!list_empty(&rreq->subrequests)) {
65 subreq = list_first_entry(&rreq->subrequests,
66 struct netfs_io_subrequest, rreq_link);
67 list_del(&subreq->rreq_link);
6cd3d6fd
DH
68 netfs_put_subrequest(subreq, was_async,
69 netfs_sreq_trace_put_clear);
3a4a38e6
DH
70 }
71}
72
73static void netfs_free_request(struct work_struct *work)
74{
75 struct netfs_io_request *rreq =
76 container_of(work, struct netfs_io_request, work);
bc899ee1 77
3a4a38e6 78 trace_netfs_rreq(rreq, netfs_rreq_trace_free);
40a81101
DH
79 netfs_clear_subrequests(rreq, false);
80 if (rreq->netfs_ops->free_request)
81 rreq->netfs_ops->free_request(rreq);
3a4a38e6
DH
82 if (rreq->cache_resources.ops)
83 rreq->cache_resources.ops->end_operation(&rreq->cache_resources);
84 kfree(rreq);
85 netfs_stat_d(&netfs_n_rh_rreq);
86}
87
de74023b
DH
88void netfs_put_request(struct netfs_io_request *rreq, bool was_async,
89 enum netfs_rreq_ref_trace what)
3a4a38e6 90{
de74023b
DH
91 unsigned int debug_id = rreq->debug_id;
92 bool dead;
93 int r;
94
95 dead = __refcount_dec_and_test(&rreq->ref, &r);
96 trace_netfs_rreq_ref(debug_id, r - 1, what);
97 if (dead) {
3a4a38e6
DH
98 if (was_async) {
99 rreq->work.func = netfs_free_request;
100 if (!queue_work(system_unbound_wq, &rreq->work))
101 BUG();
102 } else {
103 netfs_free_request(&rreq->work);
104 }
105 }
106}
107
108/*
109 * Allocate and partially initialise an I/O request structure.
110 */
111struct netfs_io_subrequest *netfs_alloc_subrequest(struct netfs_io_request *rreq)
112{
113 struct netfs_io_subrequest *subreq;
114
115 subreq = kzalloc(sizeof(struct netfs_io_subrequest), GFP_KERNEL);
116 if (subreq) {
117 INIT_LIST_HEAD(&subreq->rreq_link);
6cd3d6fd 118 refcount_set(&subreq->ref, 2);
3a4a38e6 119 subreq->rreq = rreq;
de74023b 120 netfs_get_request(rreq, netfs_rreq_trace_get_subreq);
3a4a38e6
DH
121 netfs_stat(&netfs_n_rh_sreq);
122 }
123
124 return subreq;
125}
126
6cd3d6fd
DH
127void netfs_get_subrequest(struct netfs_io_subrequest *subreq,
128 enum netfs_sreq_ref_trace what)
3a4a38e6 129{
6cd3d6fd
DH
130 int r;
131
132 __refcount_inc(&subreq->ref, &r);
133 trace_netfs_sreq_ref(subreq->rreq->debug_id, subreq->debug_index, r + 1,
134 what);
3a4a38e6
DH
135}
136
6cd3d6fd
DH
137static void netfs_free_subrequest(struct netfs_io_subrequest *subreq,
138 bool was_async)
3a4a38e6
DH
139{
140 struct netfs_io_request *rreq = subreq->rreq;
141
142 trace_netfs_sreq(subreq, netfs_sreq_trace_free);
143 kfree(subreq);
144 netfs_stat_d(&netfs_n_rh_sreq);
de74023b 145 netfs_put_request(rreq, was_async, netfs_rreq_trace_put_subreq);
3a4a38e6
DH
146}
147
6cd3d6fd
DH
148void netfs_put_subrequest(struct netfs_io_subrequest *subreq, bool was_async,
149 enum netfs_sreq_ref_trace what)
3a4a38e6 150{
6cd3d6fd
DH
151 unsigned int debug_index = subreq->debug_index;
152 unsigned int debug_id = subreq->rreq->debug_id;
153 bool dead;
154 int r;
155
156 dead = __refcount_dec_and_test(&subreq->ref, &r);
157 trace_netfs_sreq_ref(debug_id, debug_index, r - 1, what);
158 if (dead)
159 netfs_free_subrequest(subreq, was_async);
3a4a38e6 160}