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
26 * Copyright (c) 2014, 2017, Intel Corporation.
29 * lustre/mdt/mdt_hsm_cdt_requests.c
31 * Lustre HSM Coordinator
33 * Author: Jacques-Charles Lafoucriere <jacques-charles.lafoucriere@cea.fr>
34 * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
37 #define DEBUG_SUBSYSTEM S_MDS
39 #include <libcfs/libcfs.h>
40 #include <libcfs/libcfs_hash.h>
41 #include <obd_support.h>
42 #include <lprocfs_status.h>
43 #include "mdt_internal.h"
46 cdt_request_cookie_hash(struct cfs_hash *hs, const void *key, unsigned int mask)
48 return cfs_hash_djb2_hash(key, sizeof(u64), mask);
51 static void *cdt_request_cookie_object(struct hlist_node *hnode)
53 return hlist_entry(hnode, struct cdt_agent_req, car_cookie_hash);
56 static void *cdt_request_cookie_key(struct hlist_node *hnode)
58 struct cdt_agent_req *car = cdt_request_cookie_object(hnode);
60 return &car->car_hai->hai_cookie;
63 static int cdt_request_cookie_keycmp(const void *key, struct hlist_node *hnode)
65 const u64 *cookie2 = cdt_request_cookie_key(hnode);
67 return *(u64 *)key == *cookie2;
71 cdt_request_cookie_get(struct cfs_hash *hs, struct hlist_node *hnode)
73 struct cdt_agent_req *car = cdt_request_cookie_object(hnode);
75 mdt_cdt_get_request(car);
79 cdt_request_cookie_put(struct cfs_hash *hs, struct hlist_node *hnode)
81 struct cdt_agent_req *car = cdt_request_cookie_object(hnode);
83 mdt_cdt_put_request(car);
86 struct cfs_hash_ops cdt_request_cookie_hash_ops = {
87 .hs_hash = cdt_request_cookie_hash,
88 .hs_key = cdt_request_cookie_key,
89 .hs_keycmp = cdt_request_cookie_keycmp,
90 .hs_object = cdt_request_cookie_object,
91 .hs_get = cdt_request_cookie_get,
92 .hs_put_locked = cdt_request_cookie_put,
97 * \param cdt [IN] coordinator
99 void dump_requests(char *prefix, struct coordinator *cdt)
101 struct cdt_agent_req *car;
103 down_read(&cdt->cdt_request_lock);
104 list_for_each_entry(car, &cdt->cdt_request_list, car_request_list) {
105 CDEBUG(D_HSM, "%s fid="DFID" dfid="DFID
107 " action=%s archive#=%d flags=%#llx"
108 " extent=%#llx-%#llx"
109 " gid=%#llx refcount=%d canceled=%d\n",
110 prefix, PFID(&car->car_hai->hai_fid),
111 PFID(&car->car_hai->hai_dfid),
112 car->car_hai->hai_cookie,
113 hsm_copytool_action2name(car->car_hai->hai_action),
114 car->car_archive_id, car->car_flags,
115 car->car_hai->hai_extent.offset,
116 car->car_hai->hai_extent.length,
117 car->car_hai->hai_gid,
118 atomic_read(&car->car_refcount),
121 up_read(&cdt->cdt_request_lock);
124 struct req_interval_data {
125 struct cdt_req_progress *crp;
130 * interval tree cb, used to go through all the tree of extent done
132 static enum interval_iter req_interval_cb(struct interval_node *node,
135 struct req_interval_data *data;
139 data->done_sz += node->in_extent.end - node->in_extent.start;
140 RETURN(INTERVAL_ITER_CONT);
144 * scan the interval tree associated to a request
145 * to compute the amount of work done
146 * \param car [IN] request
147 * \param done_sz [OUT] will be set to the size of work done
149 void mdt_cdt_get_work_done(struct cdt_agent_req *car, __u64 *done_sz)
151 struct req_interval_data rid;
152 struct cdt_req_progress *crp = &car->car_progress;
154 mutex_lock(&crp->crp_lock);
158 interval_iterate(crp->crp_root, req_interval_cb, &rid);
159 *done_sz = rid.done_sz;
161 mutex_unlock(&crp->crp_lock);
164 #define NODE_VECTOR_SZ 256
166 * free the interval tree associated to a request
168 static void mdt_cdt_free_request_tree(struct cdt_req_progress *crp)
170 struct interval_node *node, *vn;
174 mutex_lock(&crp->crp_lock);
176 if (crp->crp_max == 0)
179 /* remove all nodes from tree */
180 for (i = 0 ; i < crp->crp_cnt ; i++) {
181 vn = crp->crp_node[i / NODE_VECTOR_SZ];
182 node = &vn[i % NODE_VECTOR_SZ];
183 interval_erase(node, &crp->crp_root);
185 /* free all sub vectors */
186 for (i = 0 ; i <= crp->crp_max / NODE_VECTOR_SZ ; i++)
187 OBD_FREE(crp->crp_node[i],
188 NODE_VECTOR_SZ * sizeof(crp->crp_node[i][0]));
190 /* free main vector */
191 OBD_FREE(crp->crp_node,
192 sizeof(crp->crp_node[0]) *
193 (crp->crp_max / NODE_VECTOR_SZ + 1));
198 mutex_unlock(&crp->crp_lock);
203 * update data moved information during a request
205 static int hsm_update_work(struct cdt_req_progress *crp,
206 const struct hsm_extent *extent)
209 struct interval_node **new_vv;
210 struct interval_node *v, *node;
214 end = extent->offset + extent->length;
215 if (end <= extent->offset)
218 mutex_lock(&crp->crp_lock);
221 if (crp->crp_cnt >= crp->crp_max) {
223 /* allocate a new vector */
224 OBD_ALLOC(v, NODE_VECTOR_SZ * sizeof(v[0]));
226 GOTO(out, rc = -ENOMEM);
228 if (crp->crp_max == 0)
231 osz = sizeof(new_vv[0]) *
232 (crp->crp_max / NODE_VECTOR_SZ + 1);
234 nsz = osz + sizeof(new_vv[0]);
235 /* increase main vector size */
236 OBD_ALLOC(new_vv, nsz);
237 if (new_vv == NULL) {
238 OBD_FREE(v, NODE_VECTOR_SZ * sizeof(v[0]));
239 GOTO(out, rc = -ENOMEM);
243 crp->crp_max = NODE_VECTOR_SZ - 1;
245 memcpy(new_vv, crp->crp_node, osz);
246 OBD_FREE(crp->crp_node, osz);
247 crp->crp_max += NODE_VECTOR_SZ;
250 crp->crp_node = new_vv;
251 crp->crp_node[crp->crp_max / NODE_VECTOR_SZ] = v;
254 v = crp->crp_node[crp->crp_cnt / NODE_VECTOR_SZ];
255 node = &v[crp->crp_cnt % NODE_VECTOR_SZ];
256 rc = interval_set(node, extent->offset, end);
259 /* try to insert, if entry already exist ignore the new one
260 * it can happen if ct sends 2 times the same progress */
261 if (interval_insert(node, &crp->crp_root) == NULL)
266 mutex_unlock(&crp->crp_lock);
271 * init the interval tree associated to a request
273 static void mdt_cdt_init_request_tree(struct cdt_req_progress *crp)
275 mutex_init(&crp->crp_lock);
276 crp->crp_root = NULL;
281 /** Allocate/init an agent request and its sub-structures.
283 * \param archive_id [IN]
287 * \retval car [OUT] success valid structure
290 struct cdt_agent_req *mdt_cdt_alloc_request(__u32 archive_id, __u64 flags,
291 struct obd_uuid *uuid,
292 struct hsm_action_item *hai)
294 struct cdt_agent_req *car;
297 OBD_SLAB_ALLOC_PTR(car, mdt_hsm_car_kmem);
299 RETURN(ERR_PTR(-ENOMEM));
301 atomic_set(&car->car_refcount, 1);
302 car->car_archive_id = archive_id;
303 car->car_flags = flags;
304 car->car_canceled = 0;
305 car->car_req_start = ktime_get_real_seconds();
306 car->car_req_update = car->car_req_start;
307 car->car_uuid = *uuid;
308 OBD_ALLOC(car->car_hai, hai->hai_len);
309 if (car->car_hai == NULL) {
310 OBD_SLAB_FREE_PTR(car, mdt_hsm_car_kmem);
311 RETURN(ERR_PTR(-ENOMEM));
313 memcpy(car->car_hai, hai, hai->hai_len);
314 mdt_cdt_init_request_tree(&car->car_progress);
320 * Free an agent request and its sub-structures.
322 * \param car [IN] Request to be freed.
324 void mdt_cdt_free_request(struct cdt_agent_req *car)
326 mdt_cdt_free_request_tree(&car->car_progress);
327 OBD_FREE(car->car_hai, car->car_hai->hai_len);
328 OBD_SLAB_FREE_PTR(car, mdt_hsm_car_kmem);
332 * inc refcount of a request
333 * \param car [IN] request
335 void mdt_cdt_get_request(struct cdt_agent_req *car)
337 atomic_inc(&car->car_refcount);
341 * dec refcount of a request
342 * free if no more refcount
343 * \param car [IN] request
345 void mdt_cdt_put_request(struct cdt_agent_req *car)
347 LASSERT(atomic_read(&car->car_refcount) > 0);
348 if (atomic_dec_and_test(&car->car_refcount))
349 mdt_cdt_free_request(car);
353 * add a request to the list
354 * \param cdt [IN] coordinator
355 * \param car [IN] request
357 * \retval -ve failure
359 int mdt_cdt_add_request(struct coordinator *cdt, struct cdt_agent_req *car)
364 /* cancel requests are not kept in memory */
365 LASSERT(car->car_hai->hai_action != HSMA_CANCEL);
367 down_write(&cdt->cdt_request_lock);
369 rc = cfs_hash_add_unique(cdt->cdt_request_cookie_hash,
370 &car->car_hai->hai_cookie,
371 &car->car_cookie_hash);
373 up_write(&cdt->cdt_request_lock);
377 list_add_tail(&car->car_request_list, &cdt->cdt_request_list);
379 up_write(&cdt->cdt_request_lock);
381 mdt_hsm_agent_update_statistics(cdt, 0, 0, 1, &car->car_uuid);
383 switch (car->car_hai->hai_action) {
385 atomic_inc(&cdt->cdt_archive_count);
388 atomic_inc(&cdt->cdt_restore_count);
391 atomic_inc(&cdt->cdt_remove_count);
394 atomic_inc(&cdt->cdt_request_count);
400 * find request in the list by cookie or by fid
401 * \param cdt [IN] coordinator
402 * \param cookie [IN] request cookie
403 * \param fid [IN] fid
404 * \retval request pointer or NULL if not found
406 struct cdt_agent_req *mdt_cdt_find_request(struct coordinator *cdt, u64 cookie)
408 struct cdt_agent_req *car;
411 down_read(&cdt->cdt_request_lock);
412 car = cfs_hash_lookup(cdt->cdt_request_cookie_hash, &cookie);
413 up_read(&cdt->cdt_request_lock);
419 * remove request from the list
420 * \param cdt [IN] coordinator
421 * \param cookie [IN] request cookie
422 * \retval request pointer
424 int mdt_cdt_remove_request(struct coordinator *cdt, __u64 cookie)
426 struct cdt_agent_req *car;
429 down_write(&cdt->cdt_request_lock);
430 car = cfs_hash_del_key(cdt->cdt_request_cookie_hash, &cookie);
432 up_write(&cdt->cdt_request_lock);
436 list_del(&car->car_request_list);
437 up_write(&cdt->cdt_request_lock);
439 switch (car->car_hai->hai_action) {
441 atomic_dec(&cdt->cdt_archive_count);
444 atomic_dec(&cdt->cdt_restore_count);
447 atomic_dec(&cdt->cdt_remove_count);
451 /* Drop reference from cdt_request_list. */
452 mdt_cdt_put_request(car);
454 LASSERT(atomic_read(&cdt->cdt_request_count) >= 1);
455 if (atomic_dec_and_test(&cdt->cdt_request_count)) {
456 /* request count is empty, nudge coordinator for more work */
457 cdt->cdt_wakeup_coordinator = true;
458 wake_up_interruptible(&cdt->cdt_waitq);
465 * update a request in the list
466 * on success, add a ref to the request returned
467 * \param cdt [IN] coordinator
468 * \param pgs [IN] progression (cookie + extent + err)
469 * \retval request pointer
470 * \retval -ve failure
472 struct cdt_agent_req *mdt_cdt_update_request(struct coordinator *cdt,
473 const struct hsm_progress_kernel *pgs)
475 struct cdt_agent_req *car;
479 car = mdt_cdt_find_request(cdt, pgs->hpk_cookie);
481 RETURN(ERR_PTR(-ENOENT));
483 car->car_req_update = ktime_get_real_seconds();
485 /* update data move progress done by copy tool */
486 if (car->car_hai->hai_action != HSMA_REMOVE && pgs->hpk_errval == 0 &&
487 pgs->hpk_extent.length != 0) {
488 rc = hsm_update_work(&car->car_progress, &pgs->hpk_extent);
490 mdt_cdt_put_request(car);
495 if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
496 if (pgs->hpk_errval != 0)
497 mdt_hsm_agent_update_statistics(cdt, 0, 1, 0,
500 mdt_hsm_agent_update_statistics(cdt, 1, 0, 0,
507 * seq_file method called to start access to /proc file
509 static void *mdt_hsm_active_requests_proc_start(struct seq_file *s, loff_t *p)
511 struct mdt_device *mdt = s->private;
512 struct coordinator *cdt = &mdt->mdt_coordinator;
513 struct list_head *pos;
517 down_read(&cdt->cdt_request_lock);
519 if (list_empty(&cdt->cdt_request_list))
523 RETURN(SEQ_START_TOKEN);
526 list_for_each(pos, &cdt->cdt_request_list) {
535 * seq_file method called to get next item
536 * just returns NULL at eof
538 static void *mdt_hsm_active_requests_proc_next(struct seq_file *s, void *v,
541 struct mdt_device *mdt = s->private;
542 struct coordinator *cdt = &mdt->mdt_coordinator;
543 struct list_head *pos = v;
546 if (pos == SEQ_START_TOKEN)
547 pos = cdt->cdt_request_list.next;
552 if (pos != &cdt->cdt_request_list)
559 * display request data
561 static int mdt_hsm_active_requests_proc_show(struct seq_file *s, void *v)
563 struct list_head *pos = v;
564 struct cdt_agent_req *car;
569 if (pos == SEQ_START_TOKEN)
572 car = list_entry(pos, struct cdt_agent_req, car_request_list);
573 mdt_cdt_get_work_done(car, &data_moved);
575 seq_printf(s, "fid="DFID" dfid="DFID
576 " compound/cookie=%#llx/%#llx"
577 " action=%s archive#=%d flags=%#llx"
578 " extent=%#llx-%#llx gid=%#llx"
579 " data=[%s] canceled=%d uuid=%s done=%llu\n",
580 PFID(&car->car_hai->hai_fid),
581 PFID(&car->car_hai->hai_dfid),
582 0ULL /* compound_id */, car->car_hai->hai_cookie,
583 hsm_copytool_action2name(car->car_hai->hai_action),
584 car->car_archive_id, car->car_flags,
585 car->car_hai->hai_extent.offset,
586 car->car_hai->hai_extent.length,
587 car->car_hai->hai_gid,
588 hai_dump_data_field(car->car_hai, buf, sizeof(buf)),
589 car->car_canceled, obd_uuid2str(&car->car_uuid),
595 * seq_file method called to stop access to /proc file
597 static void mdt_hsm_active_requests_proc_stop(struct seq_file *s, void *v)
599 struct mdt_device *mdt = s->private;
600 struct coordinator *cdt = &mdt->mdt_coordinator;
603 up_read(&cdt->cdt_request_lock);
608 /* hsm agent list proc functions */
609 static const struct seq_operations mdt_hsm_active_requests_proc_ops = {
610 .start = mdt_hsm_active_requests_proc_start,
611 .next = mdt_hsm_active_requests_proc_next,
612 .show = mdt_hsm_active_requests_proc_show,
613 .stop = mdt_hsm_active_requests_proc_stop,
617 * public function called at open of /proc file to get
620 static int ldebugfs_open_hsm_active_requests(struct inode *inode,
627 rc = seq_open(file, &mdt_hsm_active_requests_proc_ops);
631 s = file->private_data;
632 s->private = inode->i_private;
637 /* methods to access hsm request list */
638 const struct file_operations mdt_hsm_active_requests_fops = {
639 .owner = THIS_MODULE,
640 .open = ldebugfs_open_hsm_active_requests,
643 .release = seq_release,