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, 2014, 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="LPX64"/"LPX64
57 " status=%s action=%s archive#=%d flags="LPX64
58 " create="LPU64" change="LPU64
59 " extent="LPX64"-"LPX64" gid="LPX64" 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 cdt->cdt_last_cookie++;
162 hai->hai_cookie = cdt->cdt_last_cookie;
164 larr->arr_hai.hai_cookie = hai->hai_cookie;
165 rc = llog_cat_add(env, lctxt->loc_handle, &larr->arr_hdr, NULL);
169 mutex_unlock(&cdt->cdt_llog_lock);
170 llog_ctxt_put(lctxt);
179 * data passed to llog_cat_process() callback
182 struct data_update_cb {
183 struct mdt_device *mdt;
187 enum agent_req_status status;
188 cfs_time_t change_time;
192 * llog_cat_process() callback, used to update a record
193 * \param env [IN] environment
194 * \param llh [IN] llog handle
195 * \param hdr [IN] llog record
196 * \param data [IN] cb data = data_update_cb
198 * \retval -ve failure
200 static int mdt_agent_record_update_cb(const struct lu_env *env,
201 struct llog_handle *llh,
202 struct llog_rec_hdr *hdr,
205 struct llog_agent_req_rec *larr;
206 struct data_update_cb *ducb;
210 larr = (struct llog_agent_req_rec *)hdr;
213 /* check if all done */
214 if (ducb->cookies_count == ducb->cookies_done)
215 RETURN(LLOG_PROC_BREAK);
217 /* if record is in final state, never change */
218 /* if record is a cancel request, it cannot be canceled
219 * this is to manage the following case:
220 * when a request is canceled, we have 2 records with the
221 * the same cookie : the one to cancel and the cancel request
222 * the 1st has to be set to ARS_CANCELED and the 2nd to ARS_SUCCEED
224 if (agent_req_in_final_state(larr->arr_status) ||
225 (larr->arr_hai.hai_action == HSMA_CANCEL &&
226 ducb->status == ARS_CANCELED))
230 for (i = 0 ; i < ducb->cookies_count ; i++) {
231 CDEBUG(D_HSM, "%s: search "LPX64", found "LPX64"\n",
232 mdt_obd_name(ducb->mdt), ducb->cookies[i],
233 larr->arr_hai.hai_cookie);
234 if (larr->arr_hai.hai_cookie == ducb->cookies[i]) {
236 larr->arr_status = ducb->status;
237 larr->arr_req_change = ducb->change_time;
238 rc = llog_write(env, llh, hdr, hdr->lrh_index);
239 ducb->cookies_done++;
245 CERROR("%s: mdt_agent_llog_update_rec() failed, rc = %d\n",
246 mdt_obd_name(ducb->mdt), rc);
252 * update an entry in agent llog
253 * \param env [IN] environment
254 * \param mdt [IN] MDT device
255 * \param cookie [IN] entries to update
256 * log cookie are returned by register
257 * \param status [IN] new status of the request
259 * \retval -ve failure
261 int mdt_agent_record_update(const struct lu_env *env, struct mdt_device *mdt,
262 __u64 *cookies, int cookies_count,
263 enum agent_req_status status)
265 struct data_update_cb ducb;
270 ducb.cookies = cookies;
271 ducb.cookies_count = cookies_count;
272 ducb.cookies_done = 0;
273 ducb.status = status;
274 ducb.change_time = cfs_time_current_sec();
276 rc = cdt_llog_process(env, mdt, mdt_agent_record_update_cb, &ducb);
278 CERROR("%s: cdt_llog_process() failed, rc=%d, cannot update "
279 "status to %s for %d cookies, done %d\n",
280 mdt_obd_name(mdt), rc,
281 agent_req_status2name(status),
282 cookies_count, ducb.cookies_done);
287 * Agent actions /proc seq_file methods
288 * As llog processing uses a callback for each entry, we cannot do a sequential
289 * read. To limit calls to llog_cat_process (it spawns a thread), we fill
290 * multiple record in seq_file buffer in one show call.
291 * op->start() sets the iterator up and returns the first element of sequence
292 * op->stop() shuts it down.
293 * op->show() iterate llog and print element into the buffer.
294 * In case of error ->start() and ->next() return ERR_PTR(error)
295 * In the end of sequence they return %NULL
296 * op->show() returns 0 in case of success and negative number in case of error.
300 * seq_file iterator for agent_action entry
302 #define AGENT_ACTIONS_IT_MAGIC 0x19660426
303 struct agent_action_iterator {
304 int aai_magic; /**< magic number */
305 bool aai_eof; /**< all done */
306 struct lu_env aai_env; /**< lustre env for llog */
307 struct mdt_device *aai_mdt; /**< metadata device */
308 struct llog_ctxt *aai_ctxt; /**< llog context */
309 int aai_cat_index; /**< cata idx already shown */
310 int aai_index; /**< idx in cata shown */
314 * seq_file method called to start access to /proc file
315 * get llog context + llog handle
317 static void *mdt_hsm_actions_proc_start(struct seq_file *s, loff_t *pos)
319 struct agent_action_iterator *aai = s->private;
322 LASSERTF(aai->aai_magic == AGENT_ACTIONS_IT_MAGIC, "%08X\n",
325 aai->aai_ctxt = llog_get_context(mdt2obd_dev(aai->aai_mdt),
326 LLOG_AGENT_ORIG_CTXT);
327 if (aai->aai_ctxt == NULL || aai->aai_ctxt->loc_handle == NULL) {
328 CERROR("llog_get_context() failed\n");
329 RETURN(ERR_PTR(-ENOENT));
332 CDEBUG(D_HSM, "llog successfully initialized, start from "LPD64"\n",
334 /* first call = rewind */
336 aai->aai_cat_index = 0;
338 aai->aai_eof = false;
348 static void *mdt_hsm_actions_proc_next(struct seq_file *s, void *v,
355 * llog_cat_process() callback, used to fill a seq_file buffer
357 static int hsm_actions_show_cb(const struct lu_env *env,
358 struct llog_handle *llh,
359 struct llog_rec_hdr *hdr,
362 struct llog_agent_req_rec *larr = (struct llog_agent_req_rec *)hdr;
363 struct seq_file *s = data;
364 struct agent_action_iterator *aai;
371 LASSERTF(aai->aai_magic == AGENT_ACTIONS_IT_MAGIC, "%08X\n",
374 /* if rec already printed => skip */
375 if (unlikely(llh->lgh_hdr->llh_cat_idx < aai->aai_cat_index))
378 if (unlikely(llh->lgh_hdr->llh_cat_idx == aai->aai_cat_index &&
379 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="LPX64"/"LPX64
386 " action=%s archive#=%d flags="LPX64
387 " extent="LPX64"-"LPX64
388 " gid="LPX64" 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,