1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Object lifetime handling and tracing.
4 * Copyright (C) 2022 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/slab.h>
12 * Allocate an I/O request and initialise it.
14 struct netfs_io_request *netfs_alloc_request(struct address_space *mapping,
16 loff_t start, size_t len,
17 enum netfs_io_origin origin)
19 static atomic_t debug_ids;
20 struct inode *inode = file ? file_inode(file) : mapping->host;
21 struct netfs_inode *ctx = netfs_inode(inode);
22 struct netfs_io_request *rreq;
25 rreq = kzalloc(sizeof(struct netfs_io_request), GFP_KERNEL);
27 return ERR_PTR(-ENOMEM);
31 rreq->origin = origin;
32 rreq->netfs_ops = ctx->ops;
33 rreq->mapping = mapping;
35 rreq->i_size = i_size_read(inode);
36 rreq->debug_id = atomic_inc_return(&debug_ids);
37 INIT_LIST_HEAD(&rreq->subrequests);
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);
48 netfs_stat(&netfs_n_rh_rreq);
52 void netfs_get_request(struct netfs_io_request *rreq, enum netfs_rreq_ref_trace what)
56 __refcount_inc(&rreq->ref, &r);
57 trace_netfs_rreq_ref(rreq->debug_id, r + 1, what);
60 void netfs_clear_subrequests(struct netfs_io_request *rreq, bool was_async)
62 struct netfs_io_subrequest *subreq;
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);
68 netfs_put_subrequest(subreq, was_async,
69 netfs_sreq_trace_put_clear);
73 static void netfs_free_request(struct work_struct *work)
75 struct netfs_io_request *rreq =
76 container_of(work, struct netfs_io_request, work);
78 trace_netfs_rreq(rreq, netfs_rreq_trace_free);
79 netfs_clear_subrequests(rreq, false);
80 if (rreq->netfs_ops->free_request)
81 rreq->netfs_ops->free_request(rreq);
82 if (rreq->cache_resources.ops)
83 rreq->cache_resources.ops->end_operation(&rreq->cache_resources);
85 netfs_stat_d(&netfs_n_rh_rreq);
88 void netfs_put_request(struct netfs_io_request *rreq, bool was_async,
89 enum netfs_rreq_ref_trace what)
91 unsigned int debug_id = rreq->debug_id;
95 dead = __refcount_dec_and_test(&rreq->ref, &r);
96 trace_netfs_rreq_ref(debug_id, r - 1, what);
99 rreq->work.func = netfs_free_request;
100 if (!queue_work(system_unbound_wq, &rreq->work))
103 netfs_free_request(&rreq->work);
109 * Allocate and partially initialise an I/O request structure.
111 struct netfs_io_subrequest *netfs_alloc_subrequest(struct netfs_io_request *rreq)
113 struct netfs_io_subrequest *subreq;
115 subreq = kzalloc(sizeof(struct netfs_io_subrequest), GFP_KERNEL);
117 INIT_LIST_HEAD(&subreq->rreq_link);
118 refcount_set(&subreq->ref, 2);
120 netfs_get_request(rreq, netfs_rreq_trace_get_subreq);
121 netfs_stat(&netfs_n_rh_sreq);
127 void netfs_get_subrequest(struct netfs_io_subrequest *subreq,
128 enum netfs_sreq_ref_trace what)
132 __refcount_inc(&subreq->ref, &r);
133 trace_netfs_sreq_ref(subreq->rreq->debug_id, subreq->debug_index, r + 1,
137 static void netfs_free_subrequest(struct netfs_io_subrequest *subreq,
140 struct netfs_io_request *rreq = subreq->rreq;
142 trace_netfs_sreq(subreq, netfs_sreq_trace_free);
144 netfs_stat_d(&netfs_n_rh_sreq);
145 netfs_put_request(rreq, was_async, netfs_rreq_trace_put_subreq);
148 void netfs_put_subrequest(struct netfs_io_subrequest *subreq, bool was_async,
149 enum netfs_sreq_ref_trace what)
151 unsigned int debug_index = subreq->debug_index;
152 unsigned int debug_id = subreq->rreq->debug_id;
156 dead = __refcount_dec_and_test(&subreq->ref, &r);
157 trace_netfs_sreq_ref(debug_id, debug_index, r - 1, what);
159 netfs_free_subrequest(subreq, was_async);