Whamcloud - gitweb
711747c0b2f3a1342b6a0bbb1b23cd9357f87a2e
[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, 2016, 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_export.h>
41 #include <obd.h>
42 #include <lprocfs_status.h>
43 #include <lustre_log.h>
44 #include "mdt_internal.h"
45
46 void dump_llog_agent_req_rec(const char *prefix,
47                              const struct llog_agent_req_rec *larr)
48 {
49         char    buf[12];
50         int     sz;
51
52         sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
53         CDEBUG(D_HSM, "%slrh=[type=%X len=%d idx=%d] fid="DFID
54                " dfid="DFID
55                " compound/cookie=%#llx/%#llx"
56                " status=%s action=%s archive#=%d flags=%#llx"
57                " create=%llu change=%llu"
58                " extent=%#llx-%#llx gid=%#llx datalen=%d"
59                " data=[%s]\n",
60                prefix,
61                larr->arr_hdr.lrh_type,
62                larr->arr_hdr.lrh_len, larr->arr_hdr.lrh_index,
63                PFID(&larr->arr_hai.hai_fid),
64                PFID(&larr->arr_hai.hai_dfid),
65                larr->arr_compound_id, larr->arr_hai.hai_cookie,
66                agent_req_status2name(larr->arr_status),
67                hsm_copytool_action2name(larr->arr_hai.hai_action),
68                larr->arr_archive_id,
69                larr->arr_flags,
70                larr->arr_req_create, larr->arr_req_change,
71                larr->arr_hai.hai_extent.offset,
72                larr->arr_hai.hai_extent.length,
73                larr->arr_hai.hai_gid, sz,
74                hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
75 }
76
77 /*
78  * process the actions llog
79  * \param env [IN] environment
80  * \param mdt [IN] MDT device
81  * \param cb [IN] llog callback funtion
82  * \param data [IN] llog callback  data
83  * \retval 0 success
84  * \retval -ve failure
85  */
86 int cdt_llog_process(const struct lu_env *env, struct mdt_device *mdt,
87                      llog_cb_t cb, void *data)
88 {
89         struct obd_device       *obd = mdt2obd_dev(mdt);
90         struct llog_ctxt        *lctxt = NULL;
91         struct coordinator      *cdt = &mdt->mdt_coordinator;
92         int                      rc;
93         ENTRY;
94
95         lctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
96         if (lctxt == NULL || lctxt->loc_handle == NULL)
97                 RETURN(-ENOENT);
98
99         mutex_lock(&cdt->cdt_llog_lock);
100
101         rc = llog_cat_process(env, lctxt->loc_handle, cb, data, 0, 0);
102         if (rc < 0)
103                 CERROR("%s: failed to process HSM_ACTIONS llog (rc=%d)\n",
104                         mdt_obd_name(mdt), rc);
105         else
106                 rc = 0;
107
108         llog_ctxt_put(lctxt);
109         mutex_unlock(&cdt->cdt_llog_lock);
110         RETURN(rc);
111 }
112
113 /**
114  * add an entry in agent llog
115  * \param env [IN] environment
116  * \param mdt [IN] PDT device
117  * \param compound_id [IN] global id associated with the record
118  * \param archive_id [IN] backend archive number
119  * \param hai [IN] record to register
120  * \retval 0 success
121  * \retval -ve failure
122  */
123 int mdt_agent_record_add(const struct lu_env *env,
124                          struct mdt_device *mdt,
125                          __u64 compound_id, __u32 archive_id,
126                          __u64 flags, struct hsm_action_item *hai)
127 {
128         struct obd_device               *obd = mdt2obd_dev(mdt);
129         struct coordinator              *cdt = &mdt->mdt_coordinator;
130         struct llog_ctxt                *lctxt = NULL;
131         struct llog_agent_req_rec       *larr;
132         int                              rc;
133         int                              sz;
134         ENTRY;
135
136         sz = llog_data_len(sizeof(*larr) + hai->hai_len - sizeof(*hai));
137         OBD_ALLOC(larr, sz);
138         if (!larr)
139                 RETURN(-ENOMEM);
140         larr->arr_hdr.lrh_len = sz;
141         larr->arr_hdr.lrh_type = HSM_AGENT_REC;
142         larr->arr_status = ARS_WAITING;
143         larr->arr_compound_id = compound_id;
144         larr->arr_archive_id = archive_id;
145         larr->arr_flags = flags;
146         larr->arr_req_create = cfs_time_current_sec();
147         larr->arr_req_change = larr->arr_req_create;
148         memcpy(&larr->arr_hai, hai, hai->hai_len);
149
150         lctxt = llog_get_context(obd, LLOG_AGENT_ORIG_CTXT);
151         if (lctxt == NULL || lctxt->loc_handle == NULL)
152                 GOTO(free, rc = -ENOENT);
153
154         mutex_lock(&cdt->cdt_llog_lock);
155
156         /* in case of cancel request, the cookie is already set to the
157          * value of the request cookie to be cancelled
158          * so we do not change it */
159         if (hai->hai_action == HSMA_CANCEL) {
160                 larr->arr_hai.hai_cookie = hai->hai_cookie;
161         } else {
162                 cdt->cdt_last_cookie++;
163                 larr->arr_hai.hai_cookie = cdt->cdt_last_cookie;
164         }
165
166         rc = llog_cat_add(env, lctxt->loc_handle, &larr->arr_hdr, NULL);
167         if (rc > 0)
168                 rc = 0;
169
170         mutex_unlock(&cdt->cdt_llog_lock);
171         llog_ctxt_put(lctxt);
172
173         EXIT;
174 free:
175         OBD_FREE(larr, sz);
176         return rc;
177 }
178
179 /**
180  * data passed to llog_cat_process() callback
181  * to find requests
182  */
183 struct data_update_cb {
184         struct mdt_device       *mdt;
185         __u64                   *cookies;
186         int                      cookies_count;
187         int                      cookies_done;
188         enum agent_req_status    status;
189         cfs_time_t               change_time;
190 };
191
192 /**
193  *  llog_cat_process() callback, used to update a record
194  * \param env [IN] environment
195  * \param llh [IN] llog handle
196  * \param hdr [IN] llog record
197  * \param data [IN] cb data = data_update_cb
198  * \retval 0 success
199  * \retval -ve failure
200  */
201 static int mdt_agent_record_update_cb(const struct lu_env *env,
202                                       struct llog_handle *llh,
203                                       struct llog_rec_hdr *hdr,
204                                       void *data)
205 {
206         struct llog_agent_req_rec       *larr;
207         struct data_update_cb           *ducb;
208         int                              rc, i;
209         ENTRY;
210
211         larr = (struct llog_agent_req_rec *)hdr;
212         ducb = data;
213
214         /* check if all done */
215         if (ducb->cookies_count == ducb->cookies_done)
216                 RETURN(LLOG_PROC_BREAK);
217
218         /* if record is in final state, never change */
219         /* if record is a cancel request, it cannot be canceled
220          * this is to manage the following case:
221          * when a request is canceled, we have 2 records with the
222          * the same cookie : the one to cancel and the cancel request
223          * the 1st has to be set to ARS_CANCELED and the 2nd to ARS_SUCCEED
224          */
225         if (agent_req_in_final_state(larr->arr_status) ||
226             (larr->arr_hai.hai_action == HSMA_CANCEL &&
227              ducb->status == ARS_CANCELED))
228                 RETURN(0);
229
230         rc = 0;
231         for (i = 0 ; i < ducb->cookies_count ; i++) {
232                 CDEBUG(D_HSM, "%s: search %#llx, found %#llx\n",
233                        mdt_obd_name(ducb->mdt), ducb->cookies[i],
234                        larr->arr_hai.hai_cookie);
235                 if (larr->arr_hai.hai_cookie == ducb->cookies[i]) {
236
237                         larr->arr_status = ducb->status;
238                         larr->arr_req_change = ducb->change_time;
239                         rc = llog_write(env, llh, hdr, hdr->lrh_index);
240                         ducb->cookies_done++;
241                         break;
242                 }
243         }
244
245         if (rc < 0)
246                 CERROR("%s: mdt_agent_llog_update_rec() failed, rc = %d\n",
247                        mdt_obd_name(ducb->mdt), rc);
248
249         RETURN(rc);
250 }
251
252 /**
253  * update an entry in agent llog
254  * \param env [IN] environment
255  * \param mdt [IN] MDT device
256  * \param cookie [IN] entries to update
257  *    log cookie are returned by register
258  * \param status [IN] new status of the request
259  * \retval 0 success
260  * \retval -ve failure
261  */
262 int mdt_agent_record_update(const struct lu_env *env, struct mdt_device *mdt,
263                             __u64 *cookies, int cookies_count,
264                             enum agent_req_status status)
265 {
266         struct data_update_cb    ducb;
267         int                      rc;
268         ENTRY;
269
270         ducb.mdt = mdt;
271         ducb.cookies = cookies;
272         ducb.cookies_count = cookies_count;
273         ducb.cookies_done = 0;
274         ducb.status = status;
275         ducb.change_time = cfs_time_current_sec();
276
277         rc = cdt_llog_process(env, mdt, mdt_agent_record_update_cb, &ducb);
278         if (rc < 0)
279                 CERROR("%s: cdt_llog_process() failed, rc=%d, cannot update "
280                        "status to %s for %d cookies, done %d\n",
281                        mdt_obd_name(mdt), rc,
282                        agent_req_status2name(status),
283                        cookies_count, ducb.cookies_done);
284         RETURN(rc);
285 }
286
287 /*
288  * Agent actions /proc seq_file methods
289  * As llog processing uses a callback for each entry, we cannot do a sequential
290  * read. To limit calls to llog_cat_process (it spawns a thread), we fill
291  * multiple record in seq_file buffer in one show call.
292  * op->start() sets the iterator up and returns the first element of sequence
293  * op->stop() shuts it down.
294  * op->show() iterate llog and print element into the buffer.
295  * In case of error ->start() and ->next() return ERR_PTR(error)
296  * In the end of sequence they return %NULL
297  * op->show() returns 0 in case of success and negative number in case of error.
298  *
299  */
300 /**
301  * seq_file iterator for agent_action entry
302  */
303 #define AGENT_ACTIONS_IT_MAGIC 0x19660426
304 struct agent_action_iterator {
305         int                      aai_magic;      /**< magic number */
306         bool                     aai_eof;        /**< all done */
307         struct lu_env            aai_env;        /**< lustre env for llog */
308         struct mdt_device       *aai_mdt;        /**< metadata device */
309         struct llog_ctxt        *aai_ctxt;       /**< llog context */
310         int                      aai_cat_index;  /**< cata idx already shown */
311         int                      aai_index;      /**< idx in cata shown */
312 };
313
314 /**
315  * seq_file method called to start access to /proc file
316  * get llog context + llog handle
317  */
318 static void *mdt_hsm_actions_proc_start(struct seq_file *s, loff_t *pos)
319 {
320         struct agent_action_iterator    *aai = s->private;
321         ENTRY;
322
323         LASSERTF(aai->aai_magic == AGENT_ACTIONS_IT_MAGIC, "%08X\n",
324                  aai->aai_magic);
325
326         aai->aai_ctxt = llog_get_context(mdt2obd_dev(aai->aai_mdt),
327                                          LLOG_AGENT_ORIG_CTXT);
328         if (aai->aai_ctxt == NULL || aai->aai_ctxt->loc_handle == NULL) {
329                 CERROR("llog_get_context() failed\n");
330                 RETURN(ERR_PTR(-ENOENT));
331         }
332
333         CDEBUG(D_HSM, "llog successfully initialized, start from %lld\n",
334                *pos);
335         /* first call = rewind */
336         if (*pos == 0) {
337                 aai->aai_cat_index = 0;
338                 aai->aai_index = 0;
339                 aai->aai_eof = false;
340                 *pos = 1;
341         }
342
343         if (aai->aai_eof)
344                 RETURN(NULL);
345
346         RETURN(aai);
347 }
348
349 static void *mdt_hsm_actions_proc_next(struct seq_file *s, void *v,
350                                          loff_t *pos)
351 {
352         RETURN(NULL);
353 }
354
355 /**
356  *  llog_cat_process() callback, used to fill a seq_file buffer
357  */
358 static int hsm_actions_show_cb(const struct lu_env *env,
359                                  struct llog_handle *llh,
360                                  struct llog_rec_hdr *hdr,
361                                  void *data)
362 {
363         struct llog_agent_req_rec    *larr = (struct llog_agent_req_rec *)hdr;
364         struct seq_file              *s = data;
365         struct agent_action_iterator *aai;
366         int                           sz;
367         char                          buf[12];
368         ENTRY;
369
370         aai = s->private;
371         LASSERTF(aai->aai_magic == AGENT_ACTIONS_IT_MAGIC, "%08X\n",
372                  aai->aai_magic);
373
374         /* if rec already printed => skip */
375         if (unlikely(llh->lgh_hdr->llh_cat_idx < aai->aai_cat_index))
376                 RETURN(0);
377
378         if (unlikely(llh->lgh_hdr->llh_cat_idx == aai->aai_cat_index &&
379                      hdr->lrh_index <= aai->aai_index))
380                 RETURN(0);
381
382         sz = larr->arr_hai.hai_len - sizeof(larr->arr_hai);
383         seq_printf(s, "lrh=[type=%X len=%d idx=%d/%d] fid="DFID
384                    " dfid="DFID" compound/cookie=%#llx/%#llx"
385                    " action=%s archive#=%d flags=%#llx"
386                    " extent=%#llx-%#llx"
387                    " gid=%#llx datalen=%d status=%s data=[%s]\n",
388                    hdr->lrh_type, hdr->lrh_len,
389                    llh->lgh_hdr->llh_cat_idx, hdr->lrh_index,
390                    PFID(&larr->arr_hai.hai_fid),
391                    PFID(&larr->arr_hai.hai_dfid),
392                    larr->arr_compound_id, larr->arr_hai.hai_cookie,
393                    hsm_copytool_action2name(larr->arr_hai.hai_action),
394                    larr->arr_archive_id,
395                    larr->arr_flags,
396                    larr->arr_hai.hai_extent.offset,
397                    larr->arr_hai.hai_extent.length,
398                    larr->arr_hai.hai_gid, sz,
399                    agent_req_status2name(larr->arr_status),
400                    hai_dump_data_field(&larr->arr_hai, buf, sizeof(buf)));
401
402         aai->aai_cat_index = llh->lgh_hdr->llh_cat_idx;
403         aai->aai_index = hdr->lrh_index;
404
405         RETURN(0);
406 }
407
408 /**
409  * mdt_hsm_actions_proc_show() is called at for each seq record
410  * process the llog, with a cb which fill the file_seq buffer
411  * to be faster, one show will fill multiple records
412  */
413 static int mdt_hsm_actions_proc_show(struct seq_file *s, void *v)
414 {
415         struct agent_action_iterator    *aai = s->private;
416         struct coordinator              *cdt = &aai->aai_mdt->mdt_coordinator;
417         int                              rc;
418         ENTRY;
419
420         LASSERTF(aai->aai_magic == AGENT_ACTIONS_IT_MAGIC, "%08X\n",
421                  aai->aai_magic);
422
423         CDEBUG(D_HSM, "show from cat %d index %d eof=%d\n",
424                aai->aai_cat_index, aai->aai_index, aai->aai_eof);
425         if (aai->aai_eof)
426                 RETURN(0);
427
428         mutex_lock(&cdt->cdt_llog_lock);
429         rc = llog_cat_process(&aai->aai_env, aai->aai_ctxt->loc_handle,
430                               hsm_actions_show_cb, s,
431                               aai->aai_cat_index, aai->aai_index);
432         mutex_unlock(&cdt->cdt_llog_lock);
433         if (rc == 0) /* all llog parsed */
434                 aai->aai_eof = true;
435         if (rc == LLOG_PROC_BREAK) /* buffer full */
436                 rc = 0;
437         RETURN(rc);
438 }
439
440 /**
441  * seq_file method called to stop access to /proc file
442  * clean + put llog context
443  */
444 static void mdt_hsm_actions_proc_stop(struct seq_file *s, void *v)
445 {
446         struct agent_action_iterator *aai = s->private;
447         ENTRY;
448
449         LASSERTF(aai->aai_magic == AGENT_ACTIONS_IT_MAGIC, "%08X\n",
450                  aai->aai_magic);
451
452         if (aai->aai_ctxt)
453                 llog_ctxt_put(aai->aai_ctxt);
454
455         EXIT;
456         return;
457 }
458
459 static const struct seq_operations mdt_hsm_actions_proc_ops = {
460         .start  = mdt_hsm_actions_proc_start,
461         .next   = mdt_hsm_actions_proc_next,
462         .show   = mdt_hsm_actions_proc_show,
463         .stop   = mdt_hsm_actions_proc_stop,
464 };
465
466 static int lprocfs_open_hsm_actions(struct inode *inode, struct file *file)
467 {
468         struct agent_action_iterator    *aai;
469         struct seq_file                 *s;
470         int                              rc;
471         struct mdt_device               *mdt;
472         ENTRY;
473
474         rc = seq_open(file, &mdt_hsm_actions_proc_ops);
475         if (rc)
476                 RETURN(rc);
477
478         OBD_ALLOC_PTR(aai);
479         if (aai == NULL)
480                 GOTO(err, rc = -ENOMEM);
481
482         aai->aai_magic = AGENT_ACTIONS_IT_MAGIC;
483         rc = lu_env_init(&aai->aai_env, LCT_LOCAL);
484         if (rc)
485                 GOTO(err, rc);
486
487         /* mdt is saved in proc_dir_entry->data by
488          * mdt_coordinator_procfs_init() calling lprocfs_register()
489          */
490         mdt = (struct mdt_device *)PDE_DATA(inode);
491         aai->aai_mdt = mdt;
492         s = file->private_data;
493         s->private = aai;
494
495         GOTO(out, rc = 0);
496
497 err:
498         lprocfs_seq_release(inode, file);
499         if (aai && aai->aai_env.le_ses)
500                 OBD_FREE_PTR(aai->aai_env.le_ses);
501         if (aai)
502                 OBD_FREE_PTR(aai);
503 out:
504         return rc;
505 }
506
507 /**
508  * lprocfs_release_hsm_actions() is called at end of /proc access.
509  * It frees allocated resources and calls cleanup lprocfs methods.
510  */
511 static int lprocfs_release_hsm_actions(struct inode *inode, struct file *file)
512 {
513         struct seq_file                 *seq = file->private_data;
514         struct agent_action_iterator    *aai = seq->private;
515
516         if (aai) {
517                 lu_env_fini(&aai->aai_env);
518                 OBD_FREE_PTR(aai);
519         }
520
521         return lprocfs_seq_release(inode, file);
522 }
523
524 /* Methods to access HSM action list LLOG through /proc */
525 const struct file_operations mdt_hsm_actions_fops = {
526         .owner          = THIS_MODULE,
527         .open           = lprocfs_open_hsm_actions,
528         .read           = seq_read,
529         .llseek         = seq_lseek,
530         .release        = lprocfs_release_hsm_actions,
531 };
532