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) 2013, 2016, Intel Corporation.
29 * lustre/mdt/mdt_hsm_cdt_actions.c
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 <obd_support.h>
40 #include <lustre_net.h>
41 #include <lustre_export.h>
43 #include <lprocfs_status.h>
44 #include <lustre_log.h>
45 #include "mdt_internal.h"
47 void dump_llog_agent_req_rec(const char *prefix,
48 const struct llog_agent_req_rec *larr)
53 sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
54 CDEBUG(D_HSM, "%slrh=[type=%X len=%d idx=%d] fid="DFID
56 " compound/cookie=%#llx/%#llx"
57 " status=%s action=%s archive#=%d flags=%#llx"
58 " create=%llu change=%llu"
59 " extent=%#llx-%#llx gid=%#llx datalen=%d"
62 larr->arr_hdr.lrh_type,
63 larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
64 PFID(&larr->arr_hai.hai_fid),
65 PFID(&larr->arr_hai.hai_dfid),
66 larr->arr_compound_id, larr->arr_hai.hai_cookie,
67 agent_req_status2name(larr->arr_status),
68 hsm_copytool_action2name(larr->arr_hai.hai_action),
71 larr->arr_req_create, larr->arr_req_change,
72 larr->arr_hai.hai_extent.offset,
73 larr->arr_hai.hai_extent.length,
74 larr->arr_hai.hai_gid, sz,
75 hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
79 * process the actions llog
80 * \param env [IN] environment
81 * \param mdt [IN] MDT device
82 * \param cb [IN] llog callback funtion
83 * \param data [IN] llog callback data
87 int cdt_llog_process(const struct lu_env *env, struct mdt_device *mdt,
88 llog_cb_t cb, void *data)
90 struct obd_device *obd = mdt2obd_dev(mdt);
91 struct llog_ctxt *lctxt = NULL;
92 struct coordinator *cdt = &mdt->mdt_coordinator;
96 lctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
97 if (lctxt == NULL || lctxt->loc_handle == NULL)
100 mutex_lock(&cdt->cdt_llog_lock);
102 rc = llog_cat_process(env, lctxt->loc_handle, cb, data, 0, 0);
104 CERROR("%s: failed to process HSM_ACTIONS llog (rc=%d)\n",
105 mdt_obd_name(mdt), rc);
109 llog_ctxt_put(lctxt);
110 mutex_unlock(&cdt->cdt_llog_lock);
115 * add an entry in agent llog
116 * \param env [IN] environment
117 * \param mdt [IN] PDT device
118 * \param compound_id [IN] global id associated with the record
119 * \param archive_id [IN] backend archive number
120 * \param hai [IN] record to register
122 * \retval -ve failure
124 int mdt_agent_record_add(const struct lu_env *env,
125 struct mdt_device *mdt,
126 __u64 compound_id, __u32 archive_id,
127 __u64 flags, struct hsm_action_item *hai)
129 struct obd_device *obd = mdt2obd_dev(mdt);
130 struct coordinator *cdt = &mdt->mdt_coordinator;
131 struct llog_ctxt *lctxt = NULL;
132 struct llog_agent_req_rec *larr;
137 sz = llog_data_len(sizeof(*larr) + hai->hai_len - sizeof(*hai));
141 larr->arr_hdr.lrh_len = sz;
142 larr->arr_hdr.lrh_type = HSM_AGENT_REC;
143 larr->arr_status = ARS_WAITING;
144 larr->arr_compound_id = compound_id;
145 larr->arr_archive_id = archive_id;
146 larr->arr_flags = flags;
147 larr->arr_req_create = cfs_time_current_sec();
148 larr->arr_req_change = larr->arr_req_create;
149 memcpy(&larr->arr_hai, hai, hai->hai_len);
151 lctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
152 if (lctxt == NULL || lctxt->loc_handle == NULL)
153 GOTO(free, rc = -ENOENT);
155 mutex_lock(&cdt->cdt_llog_lock);
157 /* in case of cancel request, the cookie is already set to the
158 * value of the request cookie to be cancelled
159 * so we do not change it */
160 if (hai->hai_action == HSMA_CANCEL) {
161 larr->arr_hai.hai_cookie = hai->hai_cookie;
163 cdt->cdt_last_cookie++;
164 larr->arr_hai.hai_cookie = cdt->cdt_last_cookie;
167 rc = llog_cat_add(env, lctxt->loc_handle, &larr->arr_hdr, NULL);
171 mutex_unlock(&cdt->cdt_llog_lock);
172 llog_ctxt_put(lctxt);
181 * data passed to llog_cat_process() callback
184 struct data_update_cb {
185 struct mdt_device *mdt;
189 enum agent_req_status status;
190 cfs_time_t change_time;
194 * llog_cat_process() callback, used to update a record
195 * \param env [IN] environment
196 * \param llh [IN] llog handle
197 * \param hdr [IN] llog record
198 * \param data [IN] cb data = data_update_cb
200 * \retval -ve failure
202 static int mdt_agent_record_update_cb(const struct lu_env *env,
203 struct llog_handle *llh,
204 struct llog_rec_hdr *hdr,
207 struct llog_agent_req_rec *larr;
208 struct data_update_cb *ducb;
212 larr = (struct llog_agent_req_rec *)hdr;
215 /* check if all done */
216 if (ducb->cookies_count == ducb->cookies_done)
217 RETURN(LLOG_PROC_BREAK);
219 /* if record is in final state, never change */
220 /* if record is a cancel request, it cannot be canceled
221 * this is to manage the following case:
222 * when a request is canceled, we have 2 records with the
223 * the same cookie : the one to cancel and the cancel request
224 * the 1st has to be set to ARS_CANCELED and the 2nd to ARS_SUCCEED
226 if (agent_req_in_final_state(larr->arr_status) ||
227 (larr->arr_hai.hai_action == HSMA_CANCEL &&
228 ducb->status == ARS_CANCELED))
232 for (i = 0 ; i < ducb->cookies_count ; i++) {
233 CDEBUG(D_HSM, "%s: search %#llx, found %#llx\n",
234 mdt_obd_name(ducb->mdt), ducb->cookies[i],
235 larr->arr_hai.hai_cookie);
236 if (larr->arr_hai.hai_cookie == ducb->cookies[i]) {
238 larr->arr_status = ducb->status;
239 larr->arr_req_change = ducb->change_time;
240 rc = llog_write(env, llh, hdr, hdr->lrh_index);
241 ducb->cookies_done++;
247 CERROR("%s: mdt_agent_llog_update_rec() failed, rc = %d\n",
248 mdt_obd_name(ducb->mdt), rc);
254 * update an entry in agent llog
255 * \param env [IN] environment
256 * \param mdt [IN] MDT device
257 * \param cookie [IN] entries to update
258 * log cookie are returned by register
259 * \param status [IN] new status of the request
261 * \retval -ve failure
263 int mdt_agent_record_update(const struct lu_env *env, struct mdt_device *mdt,
264 __u64 *cookies, int cookies_count,
265 enum agent_req_status status)
267 struct data_update_cb ducb;
272 ducb.cookies = cookies;
273 ducb.cookies_count = cookies_count;
274 ducb.cookies_done = 0;
275 ducb.status = status;
276 ducb.change_time = cfs_time_current_sec();
278 rc = cdt_llog_process(env, mdt, mdt_agent_record_update_cb, &ducb);
280 CERROR("%s: cdt_llog_process() failed, rc=%d, cannot update "
281 "status to %s for %d cookies, done %d\n",
282 mdt_obd_name(mdt), rc,
283 agent_req_status2name(status),
284 cookies_count, ducb.cookies_done);
289 * Agent actions /proc seq_file methods
290 * As llog processing uses a callback for each entry, we cannot do a sequential
291 * read. To limit calls to llog_cat_process (it spawns a thread), we fill
292 * multiple record in seq_file buffer in one show call.
293 * op->start() sets the iterator up and returns the first element of sequence
294 * op->stop() shuts it down.
295 * op->show() iterate llog and print element into the buffer.
296 * In case of error ->start() and ->next() return ERR_PTR(error)
297 * In the end of sequence they return %NULL
298 * op->show() returns 0 in case of success and negative number in case of error.
302 * seq_file iterator for agent_action entry
304 #define AGENT_ACTIONS_IT_MAGIC 0x19660426
305 struct agent_action_iterator {
306 int aai_magic; /**< magic number */
307 bool aai_eof; /**< all done */
308 struct lu_env aai_env; /**< lustre env for llog */
309 struct mdt_device *aai_mdt; /**< metadata device */
310 struct llog_ctxt *aai_ctxt; /**< llog context */
311 int aai_cat_index; /**< cata idx already shown */
312 int aai_index; /**< idx in cata shown */
316 * seq_file method called to start access to /proc file
317 * get llog context + llog handle
319 static void *mdt_hsm_actions_proc_start(struct seq_file *s, loff_t *pos)
321 struct agent_action_iterator *aai = s->private;
324 LASSERTF(aai->aai_magic == AGENT_ACTIONS_IT_MAGIC, "%08X\n",
327 aai->aai_ctxt = llog_get_context(mdt2obd_dev(aai->aai_mdt),
328 LLOG_AGENT_ORIG_CTXT);
329 if (aai->aai_ctxt == NULL || aai->aai_ctxt->loc_handle == NULL) {
330 CERROR("llog_get_context() failed\n");
331 RETURN(ERR_PTR(-ENOENT));
334 CDEBUG(D_HSM, "llog successfully initialized, start from %lld\n",
336 /* first call = rewind */
338 aai->aai_cat_index = 0;
340 aai->aai_eof = false;
350 static void *mdt_hsm_actions_proc_next(struct seq_file *s, void *v,
357 * llog_cat_process() callback, used to fill a seq_file buffer
359 static int hsm_actions_show_cb(const struct lu_env *env,
360 struct llog_handle *llh,
361 struct llog_rec_hdr *hdr,
364 struct llog_agent_req_rec *larr = (struct llog_agent_req_rec *)hdr;
365 struct seq_file *s = data;
366 struct agent_action_iterator *aai;
372 LASSERTF(aai->aai_magic == AGENT_ACTIONS_IT_MAGIC, "%08X\n",
375 /* if rec already printed => skip */
376 if (unlikely(llh->lgh_hdr->llh_cat_idx < aai->aai_cat_index))
379 if (unlikely(llh->lgh_hdr->llh_cat_idx == aai->aai_cat_index &&
380 hdr->lrh_index <= aai->aai_index))
383 sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
384 seq_printf(s, "lrh=[type=%X len=%d idx=%d/%d] fid="DFID
385 " dfid="DFID" compound/cookie=%#llx/%#llx"
386 " action=%s archive#=%d flags=%#llx"
387 " extent=%#llx-%#llx"
388 " gid=%#llx datalen=%d status=%s data=[%s]\n",
389 hdr->lrh_type, hdr->lrh_len,
390 llh->lgh_hdr->llh_cat_idx, hdr->lrh_index,
391 PFID(&larr->arr_hai.hai_fid),
392 PFID(&larr->arr_hai.hai_dfid),
393 larr->arr_compound_id, larr->arr_hai.hai_cookie,
394 hsm_copytool_action2name(larr->arr_hai.hai_action),
395 larr->arr_archive_id,
397 larr->arr_hai.hai_extent.offset,
398 larr->arr_hai.hai_extent.length,
399 larr->arr_hai.hai_gid, sz,
400 agent_req_status2name(larr->arr_status),
401 hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
403 aai->aai_cat_index = llh->lgh_hdr->llh_cat_idx;
404 aai->aai_index = hdr->lrh_index;
410 * mdt_hsm_actions_proc_show() is called at for each seq record
411 * process the llog, with a cb which fill the file_seq buffer
412 * to be faster, one show will fill multiple records
414 static int mdt_hsm_actions_proc_show(struct seq_file *s, void *v)
416 struct agent_action_iterator *aai = s->private;
417 struct coordinator *cdt = &aai->aai_mdt->mdt_coordinator;
421 LASSERTF(aai->aai_magic == AGENT_ACTIONS_IT_MAGIC, "%08X\n",
424 CDEBUG(D_HSM, "show from cat %d index %d eof=%d\n",
425 aai->aai_cat_index, aai->aai_index, aai->aai_eof);
429 mutex_lock(&cdt->cdt_llog_lock);
430 rc = llog_cat_process(&aai->aai_env, aai->aai_ctxt->loc_handle,
431 hsm_actions_show_cb, s,
432 aai->aai_cat_index, aai->aai_index);
433 mutex_unlock(&cdt->cdt_llog_lock);
434 if (rc == 0) /* all llog parsed */
436 if (rc == LLOG_PROC_BREAK) /* buffer full */
442 * seq_file method called to stop access to /proc file
443 * clean + put llog context
445 static void mdt_hsm_actions_proc_stop(struct seq_file *s, void *v)
447 struct agent_action_iterator *aai = s->private;
450 LASSERTF(aai->aai_magic == AGENT_ACTIONS_IT_MAGIC, "%08X\n",
454 llog_ctxt_put(aai->aai_ctxt);
460 static const struct seq_operations mdt_hsm_actions_proc_ops = {
461 .start = mdt_hsm_actions_proc_start,
462 .next = mdt_hsm_actions_proc_next,
463 .show = mdt_hsm_actions_proc_show,
464 .stop = mdt_hsm_actions_proc_stop,
467 static int lprocfs_open_hsm_actions(struct inode *inode, struct file *file)
469 struct agent_action_iterator *aai;
472 struct mdt_device *mdt;
475 rc = seq_open(file, &mdt_hsm_actions_proc_ops);
481 GOTO(err, rc = -ENOMEM);
483 aai->aai_magic = AGENT_ACTIONS_IT_MAGIC;
484 rc = lu_env_init(&aai->aai_env, LCT_LOCAL);
488 /* mdt is saved in proc_dir_entry->data by
489 * mdt_coordinator_procfs_init() calling lprocfs_register()
491 mdt = (struct mdt_device *)PDE_DATA(inode);
493 s = file->private_data;
499 lprocfs_seq_release(inode, file);
500 if (aai && aai->aai_env.le_ses)
501 OBD_FREE_PTR(aai->aai_env.le_ses);
509 * lprocfs_release_hsm_actions() is called at end of /proc access.
510 * It frees allocated resources and calls cleanup lprocfs methods.
512 static int lprocfs_release_hsm_actions(struct inode *inode, struct file *file)
514 struct seq_file *seq = file->private_data;
515 struct agent_action_iterator *aai = seq->private;
518 lu_env_fini(&aai->aai_env);
522 return lprocfs_seq_release(inode, file);
525 /* Methods to access HSM action list LLOG through /proc */
526 const struct file_operations mdt_hsm_actions_fops = {
527 .owner = THIS_MODULE,
528 .open = lprocfs_open_hsm_actions,
531 .release = lprocfs_release_hsm_actions,