4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License version 2 for more details. A copy is
14 * included in the COPYING file that accompanied this code.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * (C) Copyright 2012 Commissariat a l'energie atomique et aux energies
28 * lustre/mdt/mdt_hsm_cdt_requests.c
30 * Lustre HSM Coordinator
32 * Author: Jacques-Charles Lafoucriere <jacques-charles.lafoucriere@cea.fr>
33 * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
36 #define DEBUG_SUBSYSTEM S_MDS
38 #include <obd_support.h>
39 #include <lustre/lustre_user.h>
40 #include <lprocfs_status.h>
41 #include "mdt_internal.h"
45 * \param cdt [IN] coordinator
47 void dump_requests(char *prefix, struct coordinator *cdt)
50 struct cdt_agent_req *car;
52 down_read(&cdt->cdt_request_lock);
53 cfs_list_for_each(pos, &cdt->cdt_requests) {
54 car = cfs_list_entry(pos, struct cdt_agent_req,
56 CDEBUG(D_HSM, "%s fid="DFID" dfid="DFID
57 " compound/cookie="LPX64"/"LPX64
58 " action=%s archive#=%d flags="LPX64
59 " extent="LPX64"-"LPX64
60 " gid="LPX64" refcount=%d canceled=%d\n",
61 prefix, PFID(&car->car_hai->hai_fid),
62 PFID(&car->car_hai->hai_dfid),
63 car->car_compound_id, car->car_hai->hai_cookie,
64 hsm_copytool_action2name(car->car_hai->hai_action),
65 car->car_archive_id, car->car_flags,
66 car->car_hai->hai_extent.offset,
67 car->car_hai->hai_extent.length,
68 car->car_hai->hai_gid,
69 cfs_atomic_read(&car->car_refcount),
72 up_read(&cdt->cdt_request_lock);
75 struct req_interval_data {
76 struct cdt_req_progress *crp;
81 * interval tree cb, used to go through all the tree of extent done
83 static enum interval_iter req_interval_cb(struct interval_node *node,
86 struct req_interval_data *data;
90 data->done_sz += node->in_extent.end - node->in_extent.start;
91 RETURN(INTERVAL_ITER_CONT);
95 * scan the interval tree associated to a request
96 * to compute the amount of work done
97 * \param car [IN] request
98 * \param done_sz [OUT] will be set to the size of work done
100 void mdt_cdt_get_work_done(struct cdt_agent_req *car, __u64 *done_sz)
102 struct req_interval_data rid;
103 struct cdt_req_progress *crp = &car->car_progress;
105 mutex_lock(&crp->crp_lock);
109 interval_iterate(crp->crp_root, req_interval_cb, &rid);
110 *done_sz = rid.done_sz;
112 mutex_unlock(&crp->crp_lock);
115 #define NODE_VECTOR_SZ 256
117 * free the interval tree associated to a request
119 static void mdt_cdt_free_request_tree(struct cdt_req_progress *crp)
121 struct interval_node *node, *vn;
125 mutex_lock(&crp->crp_lock);
127 if (crp->crp_max == 0)
130 /* remove all nodes from tree */
131 for (i = 0 ; i < crp->crp_cnt ; i++) {
132 vn = crp->crp_node[i / NODE_VECTOR_SZ];
133 node = &vn[i % NODE_VECTOR_SZ];
134 interval_erase(node, &crp->crp_root);
136 /* free all sub vectors */
137 for (i = 0 ; i <= crp->crp_max / NODE_VECTOR_SZ ; i++)
138 OBD_FREE(crp->crp_node[i],
139 NODE_VECTOR_SZ * sizeof(crp->crp_node[i][0]));
141 /* free main vector */
142 OBD_FREE(crp->crp_node,
143 sizeof(crp->crp_node[0]) *
144 (crp->crp_max / NODE_VECTOR_SZ + 1));
149 mutex_unlock(&crp->crp_lock);
154 * update data moved information during a request
156 static int hsm_update_work(struct cdt_req_progress *crp,
157 const struct hsm_extent *extent)
160 struct interval_node **new_vv;
161 struct interval_node *v, *node;
164 mutex_lock(&crp->crp_lock);
167 if (crp->crp_cnt >= crp->crp_max) {
169 /* allocate a new vector */
170 OBD_ALLOC(v, NODE_VECTOR_SZ * sizeof(v[0]));
172 GOTO(out, rc = -ENOMEM);
174 if (crp->crp_max == 0)
177 osz = sizeof(new_vv[0]) *
178 (crp->crp_max / NODE_VECTOR_SZ + 1);
180 nsz = osz + sizeof(new_vv[0]);
181 /* increase main vector size */
182 OBD_ALLOC(new_vv, nsz);
183 if (new_vv == NULL) {
184 OBD_FREE(v, NODE_VECTOR_SZ * sizeof(v[0]));
185 GOTO(out, rc = -ENOMEM);
189 crp->crp_max = NODE_VECTOR_SZ - 1;
191 memcpy(new_vv, crp->crp_node, osz);
192 OBD_FREE(crp->crp_node, osz);
193 crp->crp_max += NODE_VECTOR_SZ;
196 crp->crp_node = new_vv;
197 crp->crp_node[crp->crp_max / NODE_VECTOR_SZ] = v;
200 v = crp->crp_node[crp->crp_cnt / NODE_VECTOR_SZ];
201 node = &v[crp->crp_cnt % NODE_VECTOR_SZ];
202 interval_set(node, extent->offset, extent->offset + extent->length);
203 /* try to insert, if entry already exist ignore the new one
204 * it can happen if ct sends 2 times the same progress */
205 if (interval_insert(node, &crp->crp_root) == NULL)
210 mutex_unlock(&crp->crp_lock);
215 * init the interval tree associated to a request
217 static void mdt_cdt_init_request_tree(struct cdt_req_progress *crp)
219 mutex_init(&crp->crp_lock);
220 crp->crp_root = NULL;
225 /** Allocate/init a agent request and its sub-structures.
227 * \param compound_id [IN]
228 * \param archive_id [IN]
232 * \retval car [OUT] success valid structure
235 struct cdt_agent_req *mdt_cdt_alloc_request(__u64 compound_id, __u32 archive_id,
236 __u64 flags, struct obd_uuid *uuid,
237 struct hsm_action_item *hai)
239 struct cdt_agent_req *car;
242 OBD_SLAB_ALLOC_PTR(car, mdt_hsm_car_kmem);
244 RETURN(ERR_PTR(-ENOMEM));
246 cfs_atomic_set(&car->car_refcount, 1);
247 car->car_compound_id = compound_id;
248 car->car_archive_id = archive_id;
249 car->car_flags = flags;
250 car->car_canceled = 0;
251 car->car_req_start = cfs_time_current_sec();
252 car->car_req_update = car->car_req_start;
253 car->car_uuid = *uuid;
254 OBD_ALLOC(car->car_hai, hai->hai_len);
255 if (car->car_hai == NULL) {
256 OBD_SLAB_FREE_PTR(car, mdt_hsm_car_kmem);
257 RETURN(ERR_PTR(-ENOMEM));
259 memcpy(car->car_hai, hai, hai->hai_len);
260 mdt_cdt_init_request_tree(&car->car_progress);
266 * Free a agent request and its sub-structures.
268 * \param car [IN] Request to be freed.
270 void mdt_cdt_free_request(struct cdt_agent_req *car)
272 mdt_cdt_free_request_tree(&car->car_progress);
273 OBD_FREE(car->car_hai, car->car_hai->hai_len);
274 OBD_SLAB_FREE_PTR(car, mdt_hsm_car_kmem);
278 * inc refcount of a request
279 * \param car [IN] request
281 void mdt_cdt_get_request(struct cdt_agent_req *car)
283 cfs_atomic_inc(&car->car_refcount);
287 * dec refcount of a request
288 * free if no more refcount
289 * \param car [IN] request
291 void mdt_cdt_put_request(struct cdt_agent_req *car)
293 LASSERT(cfs_atomic_read(&car->car_refcount) > 0);
294 if (cfs_atomic_dec_and_test(&car->car_refcount))
295 mdt_cdt_free_request(car);
299 * find request in the list by cookie or by fid
300 * lock cdt_request_lock needs to be hold by caller
301 * \param cdt [IN] coordinator
302 * \param cookie [IN] request cookie
303 * \param fid [IN] fid
304 * \retval request pointer
306 static struct cdt_agent_req *cdt_find_request_nolock(struct coordinator *cdt,
308 const struct lu_fid *fid)
310 struct cdt_agent_req *car;
311 struct cdt_agent_req *found = NULL;
314 cfs_list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
315 if ((car->car_hai->hai_cookie == cookie) ||
316 ((fid != NULL) && lu_fid_eq(fid, &car->car_hai->hai_fid))) {
317 mdt_cdt_get_request(car);
327 * add a request to the list
328 * \param cdt [IN] coordinator
329 * \param car [IN] request
331 * \retval -ve failure
333 int mdt_cdt_add_request(struct coordinator *cdt, struct cdt_agent_req *new_car)
335 struct cdt_agent_req *car;
338 /* cancel requests are not kept in memory */
339 LASSERT(new_car->car_hai->hai_action != HSMA_CANCEL);
341 down_write(&cdt->cdt_request_lock);
342 car = cdt_find_request_nolock(cdt, new_car->car_hai->hai_cookie, NULL);
344 mdt_cdt_put_request(car);
345 up_write(&cdt->cdt_request_lock);
349 cfs_list_add_tail(&new_car->car_request_list, &cdt->cdt_requests);
350 up_write(&cdt->cdt_request_lock);
352 mdt_hsm_agent_update_statistics(cdt, 0, 0, 1, &new_car->car_uuid);
354 atomic_inc(&cdt->cdt_request_count);
360 * find request in the list by cookie or by fid
361 * \param cdt [IN] coordinator
362 * \param cookie [IN] request cookie
363 * \param fid [IN] fid
364 * \retval request pointer
366 struct cdt_agent_req *mdt_cdt_find_request(struct coordinator *cdt,
368 const struct lu_fid *fid)
370 struct cdt_agent_req *car;
373 down_read(&cdt->cdt_request_lock);
374 car = cdt_find_request_nolock(cdt, cookie, fid);
375 up_read(&cdt->cdt_request_lock);
381 * remove request from the list
382 * \param cdt [IN] coordinator
383 * \param cookie [IN] request cookie
384 * \retval request pointer
386 int mdt_cdt_remove_request(struct coordinator *cdt, __u64 cookie)
388 struct cdt_agent_req *car;
391 down_write(&cdt->cdt_request_lock);
392 car = cdt_find_request_nolock(cdt, cookie, NULL);
394 cfs_list_del(&car->car_request_list);
395 up_write(&cdt->cdt_request_lock);
397 /* reference from cdt_requests list */
398 mdt_cdt_put_request(car);
400 /* reference from cdt_find_request_nolock() */
401 mdt_cdt_put_request(car);
403 LASSERT(atomic_read(&cdt->cdt_request_count) > 0);
404 atomic_dec(&cdt->cdt_request_count);
408 up_write(&cdt->cdt_request_lock);
414 * update a request in the list
415 * on success, add a ref to the request returned
416 * \param cdt [IN] coordinator
417 * \param pgs [IN] progression (cookie + extent + err)
418 * \retval request pointer
419 * \retval -ve failure
421 struct cdt_agent_req *mdt_cdt_update_request(struct coordinator *cdt,
422 const struct hsm_progress_kernel *pgs)
424 struct cdt_agent_req *car;
428 car = mdt_cdt_find_request(cdt, pgs->hpk_cookie, NULL);
430 RETURN(ERR_PTR(-ENOENT));
432 car->car_req_update = cfs_time_current_sec();
434 /* update progress done by copy tool */
435 if (pgs->hpk_errval == 0 && pgs->hpk_extent.length != 0) {
436 rc = hsm_update_work(&car->car_progress, &pgs->hpk_extent);
438 mdt_cdt_put_request(car);
443 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
444 if (pgs->hpk_errval != 0)
445 mdt_hsm_agent_update_statistics(cdt, 0, 1, 0,
448 mdt_hsm_agent_update_statistics(cdt, 1, 0, 0,
455 * seq_file method called to start access to /proc file
457 static void *mdt_hsm_request_proc_start(struct seq_file *s, loff_t *p)
459 struct mdt_device *mdt = s->private;
460 struct coordinator *cdt = &mdt->mdt_coordinator;
465 down_read(&cdt->cdt_request_lock);
467 if (cfs_list_empty(&cdt->cdt_requests))
471 RETURN(SEQ_START_TOKEN);
474 cfs_list_for_each(pos, &cdt->cdt_requests) {
483 * seq_file method called to get next item
484 * just returns NULL at eof
486 static void *mdt_hsm_request_proc_next(struct seq_file *s, void *v, loff_t *p)
488 struct mdt_device *mdt = s->private;
489 struct coordinator *cdt = &mdt->mdt_coordinator;
493 if (pos == SEQ_START_TOKEN)
494 pos = cdt->cdt_requests.next;
499 if (pos != &cdt->cdt_requests)
506 * display request data
508 static int mdt_hsm_request_proc_show(struct seq_file *s, void *v)
511 struct cdt_agent_req *car;
516 if (pos == SEQ_START_TOKEN)
519 car = cfs_list_entry(pos, struct cdt_agent_req, car_request_list);
520 mdt_cdt_get_work_done(car, &data_moved);
522 seq_printf(s, "fid="DFID" dfid="DFID
523 " compound/cookie="LPX64"/"LPX64
524 " action=%s archive#=%d flags="LPX64
525 " extent="LPX64"-"LPX64" gid="LPX64
526 " data=[%s] canceled=%d uuid=%s done="LPU64"%%\n",
527 PFID(&car->car_hai->hai_fid),
528 PFID(&car->car_hai->hai_dfid),
529 car->car_compound_id, car->car_hai->hai_cookie,
530 hsm_copytool_action2name(car->car_hai->hai_action),
531 car->car_archive_id, car->car_flags,
532 car->car_hai->hai_extent.offset,
533 car->car_hai->hai_extent.length,
534 car->car_hai->hai_gid,
535 hai_dump_data_field(car->car_hai, buf, sizeof(buf)),
536 car->car_canceled, obd_uuid2str(&car->car_uuid),
542 * seq_file method called to stop access to /proc file
544 static void mdt_hsm_request_proc_stop(struct seq_file *s, void *v)
546 struct mdt_device *mdt = s->private;
547 struct coordinator *cdt = &mdt->mdt_coordinator;
550 up_read(&cdt->cdt_request_lock);
555 /* hsm agent list proc functions */
556 static const struct seq_operations mdt_hsm_request_proc_ops = {
557 .start = mdt_hsm_request_proc_start,
558 .next = mdt_hsm_request_proc_next,
559 .show = mdt_hsm_request_proc_show,
560 .stop = mdt_hsm_request_proc_stop,
564 * public function called at open of /proc file to get
567 static int lprocfs_open_hsm_request(struct inode *inode, struct file *file)
573 if (LPROCFS_ENTRY_AND_CHECK(PDE(inode)))
576 rc = seq_open(file, &mdt_hsm_request_proc_ops);
581 s = file->private_data;
582 s->private = PDE(inode)->data;
587 /* methods to access hsm request list */
588 const struct file_operations mdt_hsm_request_fops = {
589 .owner = THIS_MODULE,
590 .open = lprocfs_open_hsm_request,
593 .release = lprocfs_seq_release,