Whamcloud - gitweb
LU-6635 lfsck: block replacing the OST-object for test
[fs/lustre-release.git] / lustre / mdt / mdt_hsm_cdt_actions.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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.
15  *
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
19  *
20  * GPL HEADER END
21  */
22 /*
23  * (C) Copyright 2012 Commissariat a l'energie atomique et aux energies
24  *     alternatives
25  *
26  * Copyright (c) 2013, 2014, Intel Corporation.
27  */
28 /*
29  * lustre/mdt/mdt_hsm_cdt_actions.c
30  *
31  * Lustre HSM
32  *
33  * Author: Jacques-Charles Lafoucriere <jacques-charles.lafoucriere@cea.fr>
34  * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
35  */
36
37 #define DEBUG_SUBSYSTEM S_MDS
38
39 #include <obd_support.h>
40 #include <lustre_net.h>
41 #include <lustre_export.h>
42 #include <obd.h>
43 #include <lprocfs_status.h>
44 #include <lustre_log.h>
45 #include "mdt_internal.h"
46
47 void dump_llog_agent_req_rec(const char *prefix,
48                              const struct llog_agent_req_rec *larr)
49 {
50         char    buf[12];
51         int     sz;
52
53         sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
54         CDEBUG(D_HSM, "%slrh=[type=%X len=%d idx=%d] fid="DFID
55                " dfid="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"
60                " data=[%s]\n",
61                prefix,
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),
69                larr->arr_archive_id,
70                larr->arr_flags,
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)));
76 }
77
78 /*
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
84  * \retval 0 success
85  * \retval -ve failure
86  */
87 int cdt_llog_process(const struct lu_env *env, struct mdt_device *mdt,
88                      llog_cb_t cb, void *data)
89 {
90         struct obd_device       *obd = mdt2obd_dev(mdt);
91         struct llog_ctxt        *lctxt = NULL;
92         struct coordinator      *cdt = &mdt->mdt_coordinator;
93         int                      rc;
94         ENTRY;
95
96         lctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
97         if (lctxt == NULL || lctxt->loc_handle == NULL)
98                 RETURN(-ENOENT);
99
100         mutex_lock(&cdt->cdt_llog_lock);
101
102         rc = llog_cat_process(env, lctxt->loc_handle, cb, data, 0, 0);
103         if (rc < 0)
104                 CERROR("%s: failed to process HSM_ACTIONS llog (rc=%d)\n",
105                         mdt_obd_name(mdt), rc);
106         else
107                 rc = 0;
108
109         llog_ctxt_put(lctxt);
110         mutex_unlock(&cdt->cdt_llog_lock);
111         RETURN(rc);
112 }
113
114 /**
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
121  * \retval 0 success
122  * \retval -ve failure
123  */
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)
128 {
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;
133         int                              rc;
134         int                              sz;
135         ENTRY;
136
137         sz = llog_data_len(sizeof(*larr) + hai->hai_len - sizeof(*hai));
138         OBD_ALLOC(larr, sz);
139         if (!larr)
140                 RETURN(-ENOMEM);
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);
150
151         lctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
152         if (lctxt == NULL || lctxt->loc_handle == NULL)
153                 GOTO(free, rc = -ENOENT);
154
155         mutex_lock(&cdt->cdt_llog_lock);
156
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;
162         } else {
163                 cdt->cdt_last_cookie++;
164                 larr->arr_hai.hai_cookie = cdt->cdt_last_cookie;
165         }
166
167         rc = llog_cat_add(env, lctxt->loc_handle, &larr->arr_hdr, NULL);
168         if (rc > 0)
169                 rc = 0;
170
171         mutex_unlock(&cdt->cdt_llog_lock);
172         llog_ctxt_put(lctxt);
173
174         EXIT;
175 free:
176         OBD_FREE(larr, sz);
177         return rc;
178 }
179
180 /**
181  * data passed to llog_cat_process() callback
182  * to find requests
183  */
184 struct data_update_cb {
185         struct mdt_device       *mdt;
186         __u64                   *cookies;
187         int                      cookies_count;
188         int                      cookies_done;
189         enum agent_req_status    status;
190         cfs_time_t               change_time;
191 };
192
193 /**
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
199  * \retval 0 success
200  * \retval -ve failure
201  */
202 static int mdt_agent_record_update_cb(const struct lu_env *env,
203                                       struct llog_handle *llh,
204                                       struct llog_rec_hdr *hdr,
205                                       void *data)
206 {
207         struct llog_agent_req_rec       *larr;
208         struct data_update_cb           *ducb;
209         int                              rc, i;
210         ENTRY;
211
212         larr = (struct llog_agent_req_rec *)hdr;
213         ducb = data;
214
215         /* check if all done */
216         if (ducb->cookies_count == ducb->cookies_done)
217                 RETURN(LLOG_PROC_BREAK);
218
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
225          */
226         if (agent_req_in_final_state(larr->arr_status) ||
227             (larr->arr_hai.hai_action == HSMA_CANCEL &&
228              ducb->status == ARS_CANCELED))
229                 RETURN(0);
230
231         rc = 0;
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]) {
237
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++;
242                         break;
243                 }
244         }
245
246         if (rc < 0)
247                 CERROR("%s: mdt_agent_llog_update_rec() failed, rc = %d\n",
248                        mdt_obd_name(ducb->mdt), rc);
249
250         RETURN(rc);
251 }
252
253 /**
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
260  * \retval 0 success
261  * \retval -ve failure
262  */
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)
266 {
267         struct data_update_cb    ducb;
268         int                      rc;
269         ENTRY;
270
271         ducb.mdt = mdt;
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();
277
278         rc = cdt_llog_process(env, mdt, mdt_agent_record_update_cb, &ducb);
279         if (rc < 0)
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);
285         RETURN(rc);
286 }
287
288 /*
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.
299  *
300  */
301 /**
302  * seq_file iterator for agent_action entry
303  */
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 */
313 };
314
315 /**
316  * seq_file method called to start access to /proc file
317  * get llog context + llog handle
318  */
319 static void *mdt_hsm_actions_proc_start(struct seq_file *s, loff_t *pos)
320 {
321         struct agent_action_iterator    *aai = s->private;
322         ENTRY;
323
324         LASSERTF(aai->aai_magic == AGENT_ACTIONS_IT_MAGIC, "%08X\n",
325                  aai->aai_magic);
326
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));
332         }
333
334         CDEBUG(D_HSM, "llog successfully initialized, start from %lld\n",
335                *pos);
336         /* first call = rewind */
337         if (*pos == 0) {
338                 aai->aai_cat_index = 0;
339                 aai->aai_index = 0;
340                 aai->aai_eof = false;
341                 *pos = 1;
342         }
343
344         if (aai->aai_eof)
345                 RETURN(NULL);
346
347         RETURN(aai);
348 }
349
350 static void *mdt_hsm_actions_proc_next(struct seq_file *s, void *v,
351                                          loff_t *pos)
352 {
353         RETURN(NULL);
354 }
355
356 /**
357  *  llog_cat_process() callback, used to fill a seq_file buffer
358  */
359 static int hsm_actions_show_cb(const struct lu_env *env,
360                                  struct llog_handle *llh,
361                                  struct llog_rec_hdr *hdr,
362                                  void *data)
363 {
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;
367         int                           sz;
368         size_t                        count;
369         char                          buf[12];
370         ENTRY;
371
372         aai = s->private;
373         LASSERTF(aai->aai_magic == AGENT_ACTIONS_IT_MAGIC, "%08X\n",
374                  aai->aai_magic);
375
376         /* if rec already printed => skip */
377         if (unlikely(llh->lgh_hdr->llh_cat_idx < aai->aai_cat_index))
378                 RETURN(0);
379
380         if (unlikely(llh->lgh_hdr->llh_cat_idx == aai->aai_cat_index &&
381                      hdr->lrh_index <= aai->aai_index))
382                 RETURN(0);
383
384         count = s->count;
385         sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
386         seq_printf(s, "lrh=[type=%X len=%d idx=%d/%d] fid="DFID
387                    " dfid="DFID" compound/cookie=%#llx/%#llx"
388                    " action=%s archive#=%d flags=%#llx"
389                    " extent=%#llx-%#llx"
390                    " gid=%#llx datalen=%d status=%s data=[%s]\n",
391                    hdr->lrh_type, hdr->lrh_len,
392                    llh->lgh_hdr->llh_cat_idx, hdr->lrh_index,
393                    PFID(&larr->arr_hai.hai_fid),
394                    PFID(&larr->arr_hai.hai_dfid),
395                    larr->arr_compound_id, larr->arr_hai.hai_cookie,
396                    hsm_copytool_action2name(larr->arr_hai.hai_action),
397                    larr->arr_archive_id,
398                    larr->arr_flags,
399                    larr->arr_hai.hai_extent.offset,
400                    larr->arr_hai.hai_extent.length,
401                    larr->arr_hai.hai_gid, sz,
402                    agent_req_status2name(larr->arr_status),
403                    hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
404
405         aai->aai_cat_index = llh->lgh_hdr->llh_cat_idx;
406         aai->aai_index = hdr->lrh_index;
407
408         RETURN(0);
409 }
410
411 /**
412  * mdt_hsm_actions_proc_show() is called at for each seq record
413  * process the llog, with a cb which fill the file_seq buffer
414  * to be faster, one show will fill multiple records
415  */
416 static int mdt_hsm_actions_proc_show(struct seq_file *s, void *v)
417 {
418         struct agent_action_iterator    *aai = s->private;
419         struct coordinator              *cdt = &aai->aai_mdt->mdt_coordinator;
420         int                              rc;
421         ENTRY;
422
423         LASSERTF(aai->aai_magic == AGENT_ACTIONS_IT_MAGIC, "%08X\n",
424                  aai->aai_magic);
425
426         CDEBUG(D_HSM, "show from cat %d index %d eof=%d\n",
427                aai->aai_cat_index, aai->aai_index, aai->aai_eof);
428         if (aai->aai_eof)
429                 RETURN(0);
430
431         mutex_lock(&cdt->cdt_llog_lock);
432         rc = llog_cat_process(&aai->aai_env, aai->aai_ctxt->loc_handle,
433                               hsm_actions_show_cb, s,
434                               aai->aai_cat_index, aai->aai_index);
435         mutex_unlock(&cdt->cdt_llog_lock);
436         if (rc == 0) /* all llog parsed */
437                 aai->aai_eof = true;
438         if (rc == LLOG_PROC_BREAK) /* buffer full */
439                 rc = 0;
440         RETURN(rc);
441 }
442
443 /**
444  * seq_file method called to stop access to /proc file
445  * clean + put llog context
446  */
447 static void mdt_hsm_actions_proc_stop(struct seq_file *s, void *v)
448 {
449         struct agent_action_iterator *aai = s->private;
450         ENTRY;
451
452         LASSERTF(aai->aai_magic == AGENT_ACTIONS_IT_MAGIC, "%08X\n",
453                  aai->aai_magic);
454
455         if (aai->aai_ctxt)
456                 llog_ctxt_put(aai->aai_ctxt);
457
458         EXIT;
459         return;
460 }
461
462 static const struct seq_operations mdt_hsm_actions_proc_ops = {
463         .start  = mdt_hsm_actions_proc_start,
464         .next   = mdt_hsm_actions_proc_next,
465         .show   = mdt_hsm_actions_proc_show,
466         .stop   = mdt_hsm_actions_proc_stop,
467 };
468
469 static int lprocfs_open_hsm_actions(struct inode *inode, struct file *file)
470 {
471         struct agent_action_iterator    *aai;
472         struct seq_file                 *s;
473         int                              rc;
474         struct mdt_device               *mdt;
475         ENTRY;
476
477         rc = seq_open(file, &mdt_hsm_actions_proc_ops);
478         if (rc)
479                 RETURN(rc);
480
481         OBD_ALLOC_PTR(aai);
482         if (aai == NULL)
483                 GOTO(err, rc = -ENOMEM);
484
485         aai->aai_magic = AGENT_ACTIONS_IT_MAGIC;
486         rc = lu_env_init(&aai->aai_env, LCT_LOCAL);
487         if (rc)
488                 GOTO(err, rc);
489
490         /* mdt is saved in proc_dir_entry->data by
491          * mdt_coordinator_procfs_init() calling lprocfs_register()
492          */
493         mdt = (struct mdt_device *)PDE_DATA(inode);
494         aai->aai_mdt = mdt;
495         s = file->private_data;
496         s->private = aai;
497
498         GOTO(out, rc = 0);
499
500 err:
501         lprocfs_seq_release(inode, file);
502         if (aai && aai->aai_env.le_ses)
503                 OBD_FREE_PTR(aai->aai_env.le_ses);
504         if (aai)
505                 OBD_FREE_PTR(aai);
506 out:
507         return rc;
508 }
509
510 /**
511  * lprocfs_release_hsm_actions() is called at end of /proc access.
512  * It frees allocated resources and calls cleanup lprocfs methods.
513  */
514 static int lprocfs_release_hsm_actions(struct inode *inode, struct file *file)
515 {
516         struct seq_file                 *seq = file->private_data;
517         struct agent_action_iterator    *aai = seq->private;
518
519         if (aai) {
520                 lu_env_fini(&aai->aai_env);
521                 OBD_FREE_PTR(aai);
522         }
523
524         return lprocfs_seq_release(inode, file);
525 }
526
527 /* Methods to access HSM action list LLOG through /proc */
528 const struct file_operations mdt_hsm_actions_fops = {
529         .owner          = THIS_MODULE,
530         .open           = lprocfs_open_hsm_actions,
531         .read           = seq_read,
532         .llseek         = seq_lseek,
533         .release        = lprocfs_release_hsm_actions,
534 };
535