Whamcloud - gitweb
991a03e0ea9b5a65e4efee0abe0230c31f5beec2
[fs/lustre-release.git] / lustre / mdt / mdt_coordinator.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  * Copyright (c) 2012, 2013, Intel Corporation.
24  * Use is subject to license terms.
25  * Copyright (c) 2011, 2012 Commissariat a l'energie atomique et aux energies
26  *                          alternatives
27  */
28 /*
29  * lustre/mdt/mdt_coordinator.c
30  *
31  * Lustre HSM Coordinator
32  *
33  * Author: Jacques-Charles Lafoucriere <jacques-charles.lafoucriere@cea.fr>
34  * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
35  * Author: Thomas Leibovici <thomas.leibovici@cea.fr>
36  */
37
38 #define DEBUG_SUBSYSTEM S_MDS
39
40 #include <obd_support.h>
41 #include <lustre_net.h>
42 #include <lustre_export.h>
43 #include <obd.h>
44 #include <obd_lov.h>
45 #include <lprocfs_status.h>
46 #include <lustre_log.h>
47 #include "mdt_internal.h"
48
49 static struct lprocfs_vars lprocfs_mdt_hsm_vars[];
50
51 /**
52  * get obj and HSM attributes on a fid
53  * \param mti [IN] context
54  * \param fid [IN] object fid
55  * \param hsm [OUT] HSM meta data
56  * \retval obj or error (-ENOENT if not found)
57  */
58 struct mdt_object *mdt_hsm_get_md_hsm(struct mdt_thread_info *mti,
59                                       const struct lu_fid *fid,
60                                       struct md_hsm *hsm)
61 {
62         struct md_attr          *ma;
63         struct mdt_object       *obj;
64         int                      rc;
65         ENTRY;
66
67         ma = &mti->mti_attr;
68         ma->ma_need = MA_HSM;
69         ma->ma_valid = 0;
70
71         /* find object by FID */
72         obj = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
73         if (IS_ERR(obj))
74                 RETURN(obj);
75
76         if (!mdt_object_exists(obj)) {
77                 /* no more object */
78                 mdt_object_put(mti->mti_env, obj);
79                 RETURN(ERR_PTR(-ENOENT));
80         }
81
82         rc = mdt_attr_get_complex(mti, obj, ma);
83         if (rc) {
84                 mdt_object_put(mti->mti_env, obj);
85                 RETURN(ERR_PTR(rc));
86         }
87
88         if (ma->ma_valid & MA_HSM)
89                 *hsm = ma->ma_hsm;
90         else
91                 memset(hsm, 0, sizeof(*hsm));
92         ma->ma_valid = 0;
93         RETURN(obj);
94 }
95
96 void mdt_hsm_dump_hal(int level, const char *prefix,
97                       struct hsm_action_list *hal)
98 {
99         int                      i, sz;
100         struct hsm_action_item  *hai;
101         char                     buf[12];
102
103         CDEBUG(level, "%s: HAL header: version %X count %d compound "LPX64
104                       " archive_id %d flags "LPX64"\n",
105                prefix, hal->hal_version, hal->hal_count,
106                hal->hal_compound_id, hal->hal_archive_id, hal->hal_flags);
107
108         hai = hai_first(hal);
109         for (i = 0; i < hal->hal_count; i++) {
110                 sz = hai->hai_len - sizeof(*hai);
111                 CDEBUG(level, "%s %d: fid="DFID" dfid="DFID
112                        " compound/cookie="LPX64"/"LPX64
113                        " action=%s extent="LPX64"-"LPX64" gid="LPX64
114                        " datalen=%d data=[%s]\n",
115                        prefix, i,
116                        PFID(&hai->hai_fid), PFID(&hai->hai_dfid),
117                        hal->hal_compound_id, hai->hai_cookie,
118                        hsm_copytool_action2name(hai->hai_action),
119                        hai->hai_extent.offset,
120                        hai->hai_extent.length,
121                        hai->hai_gid, sz,
122                        hai_dump_data_field(hai, buf, sizeof(buf)));
123                 hai = hai_next(hai);
124         }
125 }
126
127 /**
128  * data passed to llog_cat_process() callback
129  * to scan requests and take actions
130  */
131 struct hsm_scan_data {
132         struct mdt_thread_info          *mti;
133         char                             fs_name[MTI_NAME_MAXLEN+1];
134         /* request to be send to agents */
135         int                              request_sz;    /** allocated size */
136         int                              max_request;   /** vector size */
137         int                              request_cnt;   /** used count */
138         struct {
139                 int                      hal_sz;
140                 int                      hal_used_sz;
141                 struct hsm_action_list  *hal;
142         } *request;
143         /* records to be canceled */
144         int                              max_cookie;    /** vector size */
145         int                              cookie_cnt;    /** used count */
146         __u64                           *cookies;
147 };
148
149 /**
150  *  llog_cat_process() callback, used to:
151  *  - find waiting request and start action
152  *  - purge canceled and done requests
153  * \param env [IN] environment
154  * \param llh [IN] llog handle
155  * \param hdr [IN] llog record
156  * \param data [IN/OUT] cb data = struct hsm_scan_data
157  * \retval 0 success
158  * \retval -ve failure
159  */
160 static int mdt_coordinator_cb(const struct lu_env *env,
161                               struct llog_handle *llh,
162                               struct llog_rec_hdr *hdr,
163                               void *data)
164 {
165         const struct llog_agent_req_rec *larr;
166         struct hsm_scan_data            *hsd;
167         struct hsm_action_item          *hai;
168         struct mdt_device               *mdt;
169         struct coordinator              *cdt;
170         int                              rc;
171         ENTRY;
172
173         hsd = data;
174         mdt = hsd->mti->mti_mdt;
175         cdt = &mdt->mdt_coordinator;
176
177         larr = (struct llog_agent_req_rec *)hdr;
178         dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
179         switch (larr->arr_status) {
180         case ARS_WAITING: {
181                 int i, empty_slot, found;
182
183                 /* Are agents full? */
184                 if (atomic_read(&cdt->cdt_request_count) ==
185                     cdt->cdt_max_request)
186                         break;
187
188                 /* first search if the request if known in the list we have
189                  * build and if there is room in the request vector */
190                 empty_slot = -1;
191                 found = -1;
192                 for (i = 0; i < hsd->max_request &&
193                             (empty_slot == -1 || found == -1); i++) {
194                         if (hsd->request[i].hal == NULL) {
195                                 empty_slot = i;
196                                 continue;
197                         }
198                         if (hsd->request[i].hal->hal_compound_id ==
199                                 larr->arr_compound_id) {
200                                 found = i;
201                                 continue;
202                         }
203                 }
204                 if (found == -1 && empty_slot == -1)
205                         /* unknown request and no more room for new request,
206                          * continue scan for to find other entries for
207                          * already found request
208                          */
209                         RETURN(0);
210
211                 if (found == -1) {
212                         struct hsm_action_list *hal;
213
214                         /* request is not already known */
215                         /* allocates hai vector size just needs to be large
216                          * enough */
217                         hsd->request[empty_slot].hal_sz =
218                                      sizeof(*hsd->request[empty_slot].hal) +
219                                      cfs_size_round(MTI_NAME_MAXLEN+1) +
220                                      2 * cfs_size_round(larr->arr_hai.hai_len);
221                         OBD_ALLOC(hal, hsd->request[empty_slot].hal_sz);
222                         if (!hal) {
223                                 CERROR("%s: Cannot allocate memory (%d o)"
224                                        "for compound "LPX64"\n",
225                                        mdt_obd_name(mdt),
226                                        hsd->request[i].hal_sz,
227                                        larr->arr_compound_id);
228                                 RETURN(-ENOMEM);
229                         }
230                         hal->hal_version = HAL_VERSION;
231                         strncpy(hal->hal_fsname, hsd->fs_name,
232                                 MTI_NAME_MAXLEN);
233                         hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
234                         hal->hal_compound_id = larr->arr_compound_id;
235                         hal->hal_archive_id = larr->arr_archive_id;
236                         hal->hal_flags = larr->arr_flags;
237                         hal->hal_count = 0;
238                         hsd->request[empty_slot].hal_used_sz = hal_size(hal);
239                         hsd->request[empty_slot].hal = hal;
240                         hsd->request_cnt++;
241                         found = empty_slot;
242                         hai = hai_first(hal);
243                 } else {
244                         /* request is known */
245                         /* we check if record archive num is the same as the
246                          * known request, if not we will serve it in multiple
247                          * time because we do not know if the agent can serve
248                          * multiple backend
249                          * a use case is a compound made of multiple restore
250                          * where the files are not archived in the same backend
251                          */
252                         if (larr->arr_archive_id !=
253                             hsd->request[found].hal->hal_archive_id)
254                                 RETURN(0);
255
256                         if (hsd->request[found].hal_sz <
257                             hsd->request[found].hal_used_sz +
258                              cfs_size_round(larr->arr_hai.hai_len)) {
259                                 /* Not enough room, need an extension */
260                                 void *hal_buffer;
261                                 int sz;
262
263                                 sz = 2 * hsd->request[found].hal_sz;
264                                 OBD_ALLOC(hal_buffer, sz);
265                                 if (!hal_buffer) {
266                                         CERROR("%s: Cannot allocate memory "
267                                                "(%d o) for compound "LPX64"\n",
268                                                mdt_obd_name(mdt), sz,
269                                                larr->arr_compound_id);
270                                         RETURN(-ENOMEM);
271                                 }
272                                 memcpy(hal_buffer, hsd->request[found].hal,
273                                        hsd->request[found].hal_used_sz);
274                                 OBD_FREE(hsd->request[found].hal,
275                                          hsd->request[found].hal_sz);
276                                 hsd->request[found].hal = hal_buffer;
277                                 hsd->request[found].hal_sz = sz;
278                         }
279                         hai = hai_first(hsd->request[found].hal);
280                         for (i = 0; i < hsd->request[found].hal->hal_count;
281                              i++)
282                                 hai = hai_next(hai);
283                 }
284                 memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
285                 hai->hai_cookie = larr->arr_hai.hai_cookie;
286                 hai->hai_gid = larr->arr_hai.hai_gid;
287
288                 hsd->request[found].hal_used_sz +=
289                                                    cfs_size_round(hai->hai_len);
290                 hsd->request[found].hal->hal_count++;
291                 break;
292         }
293         case ARS_STARTED: {
294                 struct cdt_agent_req *car;
295                 cfs_time_t last;
296
297                 /* we search for a running request
298                  * error may happen if coordinator crashes or stopped
299                  * with running request
300                  */
301                 car = mdt_cdt_find_request(cdt, larr->arr_hai.hai_cookie, NULL);
302                 if (car == NULL) {
303                         last = larr->arr_req_create;
304                 } else {
305                         last = car->car_req_update;
306                         mdt_cdt_put_request(car);
307                 }
308
309                 /* test if request too long, if yes cancel it
310                  * the same way the copy tool acknowledge a cancel request */
311                 if ((last + cdt->cdt_timeout) < cfs_time_current_sec()) {
312                         struct hsm_progress_kernel pgs;
313
314                         dump_llog_agent_req_rec("mdt_coordinator_cb(): "
315                                                 "request timeouted, start "
316                                                 "cleaning", larr);
317                         /* a too old cancel request just needs to be removed
318                          * this can happen, if copy tool does not support cancel
319                          * for other requests, we have to remove the running
320                          * request and notify the copytool
321                          */
322                         pgs.hpk_fid = larr->arr_hai.hai_fid;
323                         pgs.hpk_cookie = larr->arr_hai.hai_cookie;
324                         pgs.hpk_extent = larr->arr_hai.hai_extent;
325                         pgs.hpk_flags = HP_FLAG_COMPLETED;
326                         pgs.hpk_errval = ENOSYS;
327                         pgs.hpk_data_version = 0;
328                         /* update request state, but do not record in llog, to
329                          * avoid deadlock on cdt_llog_lock
330                          */
331                         rc = mdt_hsm_update_request_state(hsd->mti, &pgs, 0);
332                         if (rc)
333                                 CERROR("%s: Cannot cleanup timeouted request: "
334                                        DFID" for cookie "LPX64" action=%s\n",
335                                        mdt_obd_name(mdt),
336                                        PFID(&pgs.hpk_fid), pgs.hpk_cookie,
337                                        hsm_copytool_action2name(
338                                                      larr->arr_hai.hai_action));
339
340                         /* add the cookie to the list of record to be
341                          * canceled by caller */
342                         if (hsd->max_cookie == (hsd->cookie_cnt - 1)) {
343                                 __u64 *ptr, *old_ptr;
344                                 int old_sz, new_sz, new_cnt;
345
346                                 /* need to increase vector size */
347                                 old_sz = sizeof(__u64) * hsd->max_cookie;
348                                 old_ptr = hsd->cookies;
349
350                                 new_cnt = 2 * hsd->max_cookie;
351                                 new_sz = sizeof(__u64) * new_cnt;
352
353                                 OBD_ALLOC(ptr, new_sz);
354                                 if (!ptr) {
355                                         CERROR("%s: Cannot allocate memory "
356                                                "(%d o) for cookie vector\n",
357                                                mdt_obd_name(mdt), new_sz);
358                                         RETURN(-ENOMEM);
359                                 }
360                                 memcpy(ptr, hsd->cookies, old_sz);
361                                 hsd->cookies = ptr;
362                                 hsd->max_cookie = new_cnt;
363                                 OBD_FREE(old_ptr, old_sz);
364                         }
365                         hsd->cookies[hsd->cookie_cnt] =
366                                                        larr->arr_hai.hai_cookie;
367                         hsd->cookie_cnt++;
368                 }
369                 break;
370         }
371         case ARS_FAILED:
372         case ARS_CANCELED:
373         case ARS_SUCCEED:
374                 if ((larr->arr_req_change + cdt->cdt_delay) <
375                     cfs_time_current_sec())
376                         RETURN(LLOG_DEL_RECORD);
377                 break;
378         }
379         RETURN(0);
380 }
381
382 /**
383  * create /proc entries for coordinator
384  * \param mdt [IN]
385  * \retval 0 success
386  * \retval -ve failure
387  */
388 int hsm_cdt_procfs_init(struct mdt_device *mdt)
389 {
390         struct coordinator      *cdt = &mdt->mdt_coordinator;
391         int                      rc = 0;
392         ENTRY;
393
394         /* init /proc entries, failure is not critical */
395         cdt->cdt_proc_dir = lprocfs_register("hsm",
396                                              mdt2obd_dev(mdt)->obd_proc_entry,
397                                              lprocfs_mdt_hsm_vars, mdt);
398         if (IS_ERR(cdt->cdt_proc_dir)) {
399                 rc = PTR_ERR(cdt->cdt_proc_dir);
400                 CERROR("%s: Cannot create 'hsm' directory in mdt proc dir,"
401                        " rc=%d\n", mdt_obd_name(mdt), rc);
402                 cdt->cdt_proc_dir = NULL;
403                 RETURN(rc);
404         }
405
406         RETURN(0);
407 }
408
409 /**
410  * remove /proc entries for coordinator
411  * \param mdt [IN]
412  */
413 void  hsm_cdt_procfs_fini(struct mdt_device *mdt)
414 {
415         struct coordinator      *cdt = &mdt->mdt_coordinator;
416
417         LASSERT(cdt->cdt_state == CDT_STOPPED);
418         if (cdt->cdt_proc_dir != NULL)
419                 lprocfs_remove(&cdt->cdt_proc_dir);
420 }
421
422 /**
423  * get vector of hsm cdt /proc vars
424  * \param none
425  * \retval var vector
426  */
427 struct lprocfs_vars *hsm_cdt_get_proc_vars(void)
428 {
429         return lprocfs_mdt_hsm_vars;
430 }
431
432 /**
433  * coordinator thread
434  * \param data [IN] obd device
435  * \retval 0 success
436  * \retval -ve failure
437  */
438 static int mdt_coordinator(void *data)
439 {
440         struct mdt_thread_info  *mti = data;
441         struct mdt_device       *mdt = mti->mti_mdt;
442         struct coordinator      *cdt = &mdt->mdt_coordinator;
443         struct hsm_scan_data     hsd = { 0 };
444         int                      rc = 0;
445         ENTRY;
446
447         cdt->cdt_thread.t_flags = SVC_RUNNING;
448         wake_up(&cdt->cdt_thread.t_ctl_waitq);
449
450         CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
451                mdt_obd_name(mdt), current_pid());
452
453         /* timeouted cookie vector initialization */
454         hsd.max_cookie = 0;
455         hsd.cookie_cnt = 0;
456         hsd.cookies = NULL;
457         /* we use a copy of cdt_max_request in the cb, so if cdt_max_request
458          * increases due to a change from /proc we do not overflow the
459          * hsd.request[] vector
460          */
461         hsd.max_request = cdt->cdt_max_request;
462         hsd.request_sz = hsd.max_request * sizeof(*hsd.request);
463         OBD_ALLOC(hsd.request, hsd.request_sz);
464         if (!hsd.request)
465                 GOTO(out, rc = -ENOMEM);
466
467         hsd.mti = mti;
468         obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
469
470         while (1) {
471                 struct l_wait_info lwi;
472                 int i;
473
474                 lwi = LWI_TIMEOUT(cfs_time_seconds(cdt->cdt_loop_period),
475                                   NULL, NULL);
476                 l_wait_event(cdt->cdt_thread.t_ctl_waitq,
477                              (cdt->cdt_thread.t_flags &
478                               (SVC_STOPPING|SVC_EVENT)),
479                              &lwi);
480
481                 CDEBUG(D_HSM, "coordinator resumes\n");
482
483                 if (cdt->cdt_thread.t_flags & SVC_STOPPING ||
484                     cdt->cdt_state == CDT_STOPPING) {
485                         cdt->cdt_thread.t_flags &= ~SVC_STOPPING;
486                         rc = 0;
487                         break;
488                 }
489
490                 /* wake up before timeout, new work arrives */
491                 if (cdt->cdt_thread.t_flags & SVC_EVENT)
492                         cdt->cdt_thread.t_flags &= ~SVC_EVENT;
493
494                 /* if coordinator is suspended continue to wait */
495                 if (cdt->cdt_state == CDT_DISABLE) {
496                         CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
497                         continue;
498                 }
499
500                 CDEBUG(D_HSM, "coordinator starts reading llog\n");
501
502                 if (hsd.max_request != cdt->cdt_max_request) {
503                         /* cdt_max_request has changed,
504                          * we need to allocate a new buffer
505                          */
506                         OBD_FREE(hsd.request, hsd.request_sz);
507                         hsd.max_request = cdt->cdt_max_request;
508                         hsd.request_sz =
509                                    hsd.max_request * sizeof(*hsd.request);
510                         OBD_ALLOC(hsd.request, hsd.request_sz);
511                         if (!hsd.request) {
512                                 rc = -ENOMEM;
513                                 break;
514                         }
515                 }
516
517                 /* create canceled cookie vector for an arbitrary size
518                  * if needed, vector will grow during llog scan
519                  */
520                 hsd.max_cookie = 10;
521                 hsd.cookie_cnt = 0;
522                 OBD_ALLOC(hsd.cookies, hsd.max_cookie * sizeof(__u64));
523                 if (!hsd.cookies) {
524                         rc = -ENOMEM;
525                         goto clean_cb_alloc;
526                 }
527                 hsd.request_cnt = 0;
528
529                 rc = cdt_llog_process(mti->mti_env, mdt,
530                                       mdt_coordinator_cb, &hsd);
531                 if (rc < 0)
532                         goto clean_cb_alloc;
533
534                 CDEBUG(D_HSM, "Found %d requests to send and %d"
535                               " requests to cancel\n",
536                        hsd.request_cnt, hsd.cookie_cnt);
537                 /* first we cancel llog records of the timeouted requests */
538                 if (hsd.cookie_cnt > 0) {
539                         rc = mdt_agent_record_update(mti->mti_env, mdt,
540                                                      hsd.cookies,
541                                                      hsd.cookie_cnt,
542                                                      ARS_CANCELED);
543                         if (rc)
544                                 CERROR("%s: mdt_agent_record_update() failed, "
545                                        "rc=%d, cannot update status to %s "
546                                        "for %d cookies\n",
547                                        mdt_obd_name(mdt), rc,
548                                        agent_req_status2name(ARS_CANCELED),
549                                        hsd.cookie_cnt);
550                 }
551
552                 if (list_empty(&cdt->cdt_agents)) {
553                         CDEBUG(D_HSM, "no agent available, "
554                                       "coordinator sleeps\n");
555                         goto clean_cb_alloc;
556                 }
557
558                 /* here hsd contains a list of requests to be started */
559                 for (i = 0; i < hsd.max_request; i++) {
560                         struct hsm_action_list  *hal;
561                         struct hsm_action_item  *hai;
562                         __u64                   *cookies;
563                         int                      sz, j;
564                         enum agent_req_status    status;
565
566                         /* still room for work ? */
567                         if (atomic_read(&cdt->cdt_request_count) ==
568                             cdt->cdt_max_request)
569                                 break;
570
571                         if (hsd.request[i].hal == NULL)
572                                 continue;
573
574                         /* found a request, we start it */
575                         /* kuc payload allocation so we avoid an additionnal
576                          * allocation in mdt_hsm_agent_send()
577                          */
578                         hal = kuc_alloc(hsd.request[i].hal_used_sz,
579                                         KUC_TRANSPORT_HSM, HMT_ACTION_LIST);
580                         if (IS_ERR(hal)) {
581                                 CERROR("%s: Cannot allocate memory (%d o) "
582                                        "for compound "LPX64"\n",
583                                        mdt_obd_name(mdt),
584                                        hsd.request[i].hal_used_sz,
585                                        hsd.request[i].hal->hal_compound_id);
586                                 continue;
587                         }
588                         memcpy(hal, hsd.request[i].hal,
589                                hsd.request[i].hal_used_sz);
590
591                         rc = mdt_hsm_agent_send(mti, hal, 0);
592                         /* if failure, we suppose it is temporary
593                          * if the copy tool failed to do the request
594                          * it has to use hsm_progress
595                          */
596                         status = (rc ? ARS_WAITING : ARS_STARTED);
597
598                         /* set up cookie vector to set records status
599                          * after copy tools start or failed
600                          */
601                         sz = hsd.request[i].hal->hal_count * sizeof(__u64);
602                         OBD_ALLOC(cookies, sz);
603                         if (cookies == NULL) {
604                                 CERROR("%s: Cannot allocate memory (%d o) "
605                                        "for cookies vector "LPX64"\n",
606                                        mdt_obd_name(mdt), sz,
607                                        hsd.request[i].hal->hal_compound_id);
608                                 kuc_free(hal, hsd.request[i].hal_used_sz);
609                                 continue;
610                         }
611                         hai = hai_first(hal);
612                         for (j = 0; j < hsd.request[i].hal->hal_count; j++) {
613                                 cookies[j] = hai->hai_cookie;
614                                 hai = hai_next(hai);
615                         }
616
617                         rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
618                                                 hsd.request[i].hal->hal_count,
619                                                 status);
620                         if (rc)
621                                 CERROR("%s: mdt_agent_record_update() failed, "
622                                        "rc=%d, cannot update status to %s "
623                                        "for %d cookies\n",
624                                        mdt_obd_name(mdt), rc,
625                                        agent_req_status2name(status),
626                                        hsd.request[i].hal->hal_count);
627
628                         OBD_FREE(cookies, sz);
629                         kuc_free(hal, hsd.request[i].hal_used_sz);
630                 }
631 clean_cb_alloc:
632                 /* free cookie vector allocated for/by callback */
633                 if (hsd.cookies) {
634                         OBD_FREE(hsd.cookies, hsd.max_cookie * sizeof(__u64));
635                         hsd.max_cookie = 0;
636                         hsd.cookie_cnt = 0;
637                         hsd.cookies = NULL;
638                 }
639
640                 /* free hal allocated by callback */
641                 for (i = 0; i < hsd.max_request; i++) {
642                         if (hsd.request[i].hal) {
643                                 OBD_FREE(hsd.request[i].hal,
644                                          hsd.request[i].hal_sz);
645                                 hsd.request[i].hal_sz = 0;
646                                 hsd.request[i].hal = NULL;
647                                 hsd.request_cnt--;
648                         }
649                 }
650                 LASSERT(hsd.request_cnt == 0);
651
652                 /* reset callback data */
653                 memset(hsd.request, 0, hsd.request_sz);
654         }
655         EXIT;
656 out:
657         if (hsd.request)
658                 OBD_FREE(hsd.request, hsd.request_sz);
659
660         if (hsd.cookies)
661                 OBD_FREE(hsd.cookies, hsd.max_cookie * sizeof(__u64));
662
663         if (cdt->cdt_state == CDT_STOPPING) {
664                 /* request comes from /proc path, so we need to clean cdt
665                  * struct */
666                  mdt_hsm_cdt_stop(mdt);
667                  mdt->mdt_opts.mo_coordinator = 0;
668         } else {
669                 /* request comes from a thread event, generated
670                  * by mdt_stop_coordinator(), we have to ack
671                  * and cdt cleaning will be done by event sender
672                  */
673                 cdt->cdt_thread.t_flags = SVC_STOPPED;
674                 wake_up(&cdt->cdt_thread.t_ctl_waitq);
675         }
676
677         if (rc != 0)
678                 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
679                        mdt_obd_name(mdt), current_pid(), rc);
680         else
681                 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
682                               " no error\n",
683                        mdt_obd_name(mdt), current_pid());
684
685         return rc;
686 }
687
688 /**
689  * lookup a restore handle by FID
690  * caller needs to hold cdt_restore_lock
691  * \param cdt [IN] coordinator
692  * \param fid [IN] FID
693  * \retval cdt_restore_handle found
694  * \retval NULL not found
695  */
696 static struct cdt_restore_handle *hsm_restore_hdl_find(struct coordinator *cdt,
697                                                        const struct lu_fid *fid)
698 {
699         struct cdt_restore_handle       *crh;
700         ENTRY;
701
702         list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
703                 if (lu_fid_eq(&crh->crh_fid, fid))
704                         RETURN(crh);
705         }
706         RETURN(NULL);
707 }
708
709 /**
710  * data passed to llog_cat_process() callback
711  * to scan requests and take actions
712  */
713 struct hsm_restore_data {
714         struct mdt_thread_info  *hrd_mti;
715 };
716
717 /**
718  *  llog_cat_process() callback, used to:
719  *  - find restore request and allocate the restore handle
720  * \param env [IN] environment
721  * \param llh [IN] llog handle
722  * \param hdr [IN] llog record
723  * \param data [IN/OUT] cb data = struct hsm_restore_data
724  * \retval 0 success
725  * \retval -ve failure
726  */
727 static int hsm_restore_cb(const struct lu_env *env,
728                           struct llog_handle *llh,
729                           struct llog_rec_hdr *hdr, void *data)
730 {
731         struct llog_agent_req_rec       *larr;
732         struct hsm_restore_data         *hrd;
733         struct cdt_restore_handle       *crh;
734         struct hsm_action_item          *hai;
735         struct mdt_thread_info          *mti;
736         struct coordinator              *cdt;
737         struct mdt_object               *child;
738         int rc;
739         ENTRY;
740
741         hrd = data;
742         mti = hrd->hrd_mti;
743         cdt = &mti->mti_mdt->mdt_coordinator;
744
745         larr = (struct llog_agent_req_rec *)hdr;
746         hai = &larr->arr_hai;
747         if (hai->hai_cookie > cdt->cdt_last_cookie)
748                 /* update the cookie to avoid collision */
749                 cdt->cdt_last_cookie = hai->hai_cookie + 1;
750
751         if (hai->hai_action != HSMA_RESTORE ||
752             agent_req_in_final_state(larr->arr_status))
753                 RETURN(0);
754
755         /* restore request not in a final state */
756
757         OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
758         if (crh == NULL)
759                 RETURN(-ENOMEM);
760
761         crh->crh_fid = hai->hai_fid;
762         /* in V1 all file is restored
763         crh->extent.start = hai->hai_extent.offset;
764         crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
765         */
766         crh->crh_extent.start = 0;
767         crh->crh_extent.end = hai->hai_extent.length;
768         /* get the layout lock */
769         mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
770         child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
771                                      MDS_INODELOCK_LAYOUT);
772         if (IS_ERR(child))
773                 GOTO(out, rc = PTR_ERR(child));
774
775         rc = 0;
776         /* we choose to not keep a reference
777          * on the object during the restore time which can be very long */
778         mdt_object_put(mti->mti_env, child);
779
780         mutex_lock(&cdt->cdt_restore_lock);
781         list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
782         mutex_unlock(&cdt->cdt_restore_lock);
783
784 out:
785         RETURN(rc);
786 }
787
788 /**
789  * restore coordinator state at startup
790  * the goal is to take a layout lock for each registered restore request
791  * \param mti [IN] context
792  */
793 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
794 {
795         struct hsm_restore_data  hrd;
796         int                      rc;
797         ENTRY;
798
799         hrd.hrd_mti = mti;
800
801         rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
802                               hsm_restore_cb, &hrd);
803
804         RETURN(rc);
805 }
806
807 static int hsm_init_ucred(struct lu_ucred *uc)
808 {
809         ENTRY;
810
811         uc->uc_valid = UCRED_OLD;
812         uc->uc_o_uid = 0;
813         uc->uc_o_gid = 0;
814         uc->uc_o_fsuid = 0;
815         uc->uc_o_fsgid = 0;
816         uc->uc_uid = 0;
817         uc->uc_gid = 0;
818         uc->uc_fsuid = 0;
819         uc->uc_fsgid = 0;
820         uc->uc_suppgids[0] = -1;
821         uc->uc_suppgids[1] = -1;
822         uc->uc_cap = CFS_CAP_FS_MASK;
823         uc->uc_umask = 0777;
824         uc->uc_ginfo = NULL;
825         uc->uc_identity = NULL;
826
827         RETURN(0);
828 }
829
830 /**
831  * wake up coordinator thread
832  * \param mdt [IN] device
833  * \retval 0 success
834  * \retval -ve failure
835  */
836 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
837 {
838         struct coordinator      *cdt = &mdt->mdt_coordinator;
839         ENTRY;
840
841         if (cdt->cdt_state == CDT_STOPPED)
842                 RETURN(-ESRCH);
843
844         /* wake up coordinator */
845         cdt->cdt_thread.t_flags = SVC_EVENT;
846         wake_up(&cdt->cdt_thread.t_ctl_waitq);
847
848         RETURN(0);
849 }
850
851 /**
852  * initialize coordinator struct
853  * \param mdt [IN] device
854  * \retval 0 success
855  * \retval -ve failure
856  */
857 int mdt_hsm_cdt_init(struct mdt_device *mdt)
858 {
859         struct coordinator      *cdt = &mdt->mdt_coordinator;
860         struct mdt_thread_info  *cdt_mti = NULL;
861         int                      rc;
862         ENTRY;
863
864         cdt->cdt_state = CDT_STOPPED;
865
866         init_waitqueue_head(&cdt->cdt_thread.t_ctl_waitq);
867         mutex_init(&cdt->cdt_llog_lock);
868         init_rwsem(&cdt->cdt_agent_lock);
869         init_rwsem(&cdt->cdt_request_lock);
870         mutex_init(&cdt->cdt_restore_lock);
871
872         CFS_INIT_LIST_HEAD(&cdt->cdt_requests);
873         CFS_INIT_LIST_HEAD(&cdt->cdt_agents);
874         CFS_INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
875
876         rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
877         if (rc < 0)
878                 RETURN(rc);
879
880         /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
881         rc = lu_context_init(&cdt->cdt_session, LCT_SESSION);
882         if (rc == 0) {
883                 lu_context_enter(&cdt->cdt_session);
884                 cdt->cdt_env.le_ses = &cdt->cdt_session;
885         } else {
886                 lu_env_fini(&cdt->cdt_env);
887                 RETURN(rc);
888         }
889
890         cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
891         LASSERT(cdt_mti != NULL);
892
893         cdt_mti->mti_env = &cdt->cdt_env;
894         cdt_mti->mti_mdt = mdt;
895
896         hsm_init_ucred(mdt_ucred(cdt_mti));
897
898         /* default values for /proc tunnables
899          * can be override by MGS conf */
900         cdt->cdt_default_archive_id = 1;
901         cdt->cdt_delay = 60;
902         cdt->cdt_loop_period = 10;
903         cdt->cdt_max_request = 3;
904         cdt->cdt_policy = CDT_DEFAULT_POLICY;
905         cdt->cdt_timeout = 3600;
906
907         RETURN(0);
908 }
909
910 /**
911  * free a coordinator thread
912  * \param mdt [IN] device
913  */
914 int  mdt_hsm_cdt_fini(struct mdt_device *mdt)
915 {
916         struct coordinator *cdt = &mdt->mdt_coordinator;
917         ENTRY;
918
919         lu_context_exit(cdt->cdt_env.le_ses);
920         lu_context_fini(cdt->cdt_env.le_ses);
921
922         lu_env_fini(&cdt->cdt_env);
923
924         RETURN(0);
925 }
926
927 /**
928  * start a coordinator thread
929  * \param mdt [IN] device
930  * \retval 0 success
931  * \retval -ve failure
932  */
933 int mdt_hsm_cdt_start(struct mdt_device *mdt)
934 {
935         struct coordinator      *cdt = &mdt->mdt_coordinator;
936         int                      rc;
937         void                    *ptr;
938         struct mdt_thread_info  *cdt_mti;
939         struct task_struct      *task;
940         ENTRY;
941
942         /* functions defined but not yet used
943          * this avoid compilation warning
944          */
945         ptr = dump_requests;
946
947         if (cdt->cdt_state != CDT_STOPPED) {
948                 CERROR("%s: Coordinator already started\n",
949                        mdt_obd_name(mdt));
950                 RETURN(-EALREADY);
951         }
952
953         CLASSERT(1 << (CDT_POLICY_SHIFT_COUNT - 1) == CDT_POLICY_LAST);
954         cdt->cdt_policy = CDT_DEFAULT_POLICY;
955
956         cdt->cdt_state = CDT_INIT;
957
958         atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
959         /* just need to be larger than previous one */
960         /* cdt_last_cookie is protected by cdt_llog_lock */
961         cdt->cdt_last_cookie = cfs_time_current_sec();
962
963         atomic_set(&cdt->cdt_request_count, 0);
964
965         /* to avoid deadlock when start is made through /proc
966          * /proc entries are created by the coordinator thread */
967
968         /* set up list of started restore requests */
969         cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
970         rc = mdt_hsm_pending_restore(cdt_mti);
971         if (rc)
972                 CERROR("%s: cannot take the layout locks needed"
973                        " for registered restore: %d",
974                        mdt_obd_name(mdt), rc);
975
976         task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
977         if (IS_ERR(task)) {
978                 rc = PTR_ERR(task);
979                 cdt->cdt_state = CDT_STOPPED;
980                 CERROR("%s: error starting coordinator thread: %d\n",
981                        mdt_obd_name(mdt), rc);
982                 RETURN(rc);
983         } else {
984                 CDEBUG(D_HSM, "%s: coordinator thread started\n",
985                        mdt_obd_name(mdt));
986                 rc = 0;
987         }
988
989         wait_event(cdt->cdt_thread.t_ctl_waitq,
990                        (cdt->cdt_thread.t_flags & SVC_RUNNING));
991
992         cdt->cdt_state = CDT_RUNNING;
993         mdt->mdt_opts.mo_coordinator = 1;
994         RETURN(0);
995 }
996
997 /**
998  * stop a coordinator thread
999  * \param mdt [IN] device
1000  */
1001 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
1002 {
1003         struct coordinator              *cdt = &mdt->mdt_coordinator;
1004         struct cdt_agent_req            *car, *tmp1;
1005         struct hsm_agent                *ha, *tmp2;
1006         struct cdt_restore_handle       *crh, *tmp3;
1007         struct mdt_thread_info          *cdt_mti;
1008         ENTRY;
1009
1010         if (cdt->cdt_state == CDT_STOPPED) {
1011                 CERROR("%s: Coordinator already stopped\n",
1012                        mdt_obd_name(mdt));
1013                 RETURN(-EALREADY);
1014         }
1015
1016         if (cdt->cdt_state != CDT_STOPPING) {
1017                 /* stop coordinator thread before cleaning */
1018                 cdt->cdt_thread.t_flags = SVC_STOPPING;
1019                 wake_up(&cdt->cdt_thread.t_ctl_waitq);
1020                 wait_event(cdt->cdt_thread.t_ctl_waitq,
1021                            cdt->cdt_thread.t_flags & SVC_STOPPED);
1022         }
1023         cdt->cdt_state = CDT_STOPPED;
1024
1025         /* start cleaning */
1026         down_write(&cdt->cdt_request_lock);
1027         list_for_each_entry_safe(car, tmp1, &cdt->cdt_requests,
1028                                  car_request_list) {
1029                 list_del(&car->car_request_list);
1030                 mdt_cdt_free_request(car);
1031         }
1032         up_write(&cdt->cdt_request_lock);
1033
1034         down_write(&cdt->cdt_agent_lock);
1035         list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
1036                 list_del(&ha->ha_list);
1037                 OBD_FREE_PTR(ha);
1038         }
1039         up_write(&cdt->cdt_agent_lock);
1040
1041         cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1042         mutex_lock(&cdt->cdt_restore_lock);
1043         list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
1044                 struct mdt_object       *child;
1045
1046                 /* give back layout lock */
1047                 child = mdt_object_find(&cdt->cdt_env, mdt, &crh->crh_fid);
1048                 if (!IS_ERR(child))
1049                         mdt_object_unlock_put(cdt_mti, child, &crh->crh_lh, 1);
1050
1051                 list_del(&crh->crh_list);
1052
1053                 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1054         }
1055         mutex_unlock(&cdt->cdt_restore_lock);
1056
1057         mdt->mdt_opts.mo_coordinator = 0;
1058
1059         RETURN(0);
1060 }
1061
1062 /**
1063  * register all requests from an hal in the memory list
1064  * \param mti [IN] context
1065  * \param hal [IN] request
1066  * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1067  *  which is running the CT
1068  * \retval 0 success
1069  * \retval -ve failure
1070  */
1071 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1072                     struct hsm_action_list *hal, struct obd_uuid *uuid)
1073 {
1074         struct mdt_device       *mdt = mti->mti_mdt;
1075         struct coordinator      *cdt = &mdt->mdt_coordinator;
1076         struct hsm_action_item  *hai;
1077         int                      rc = 0, i;
1078         ENTRY;
1079
1080         /* register request in memory list */
1081         hai = hai_first(hal);
1082         for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1083                 struct cdt_agent_req *car;
1084
1085                 /* in case of a cancel request, we first mark the ondisk
1086                  * record of the request we want to stop as canceled
1087                  * this does not change the cancel record
1088                  * it will be done when updating the request status
1089                  */
1090                 if (hai->hai_action == HSMA_CANCEL) {
1091                         rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1092                                                      &hai->hai_cookie,
1093                                                      1, ARS_CANCELED);
1094                         if (rc) {
1095                                 CERROR("%s: mdt_agent_record_update() failed, "
1096                                        "rc=%d, cannot update status to %s "
1097                                        "for cookie "LPX64"\n",
1098                                        mdt_obd_name(mdt), rc,
1099                                        agent_req_status2name(ARS_CANCELED),
1100                                        hai->hai_cookie);
1101                                 GOTO(out, rc);
1102                         }
1103
1104                         /* find the running request to set it canceled */
1105                         car = mdt_cdt_find_request(cdt, hai->hai_cookie, NULL);
1106                         if (car != NULL) {
1107                                 car->car_canceled = 1;
1108                                 /* uuid has to be changed to the one running the
1109                                 * request to cancel */
1110                                 *uuid = car->car_uuid;
1111                                 mdt_cdt_put_request(car);
1112                         }
1113                         /* no need to memorize cancel request
1114                          * this also avoid a deadlock when we receive
1115                          * a purge all requests command
1116                          */
1117                         continue;
1118                 }
1119
1120                 if (hai->hai_action == HSMA_ARCHIVE) {
1121                         struct mdt_object *obj;
1122                         struct md_hsm hsm;
1123
1124                         obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &hsm);
1125                         if (IS_ERR(obj) && (PTR_ERR(obj) == -ENOENT))
1126                                 continue;
1127                         if (IS_ERR(obj))
1128                                 GOTO(out, rc = PTR_ERR(obj));
1129
1130                         hsm.mh_flags |= HS_EXISTS;
1131                         hsm.mh_arch_id = hal->hal_archive_id;
1132                         rc = mdt_hsm_attr_set(mti, obj, &hsm);
1133                         mdt_object_put(mti->mti_env, obj);
1134                         if (rc)
1135                                 GOTO(out, rc);
1136                 }
1137
1138                 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1139                                             hal->hal_archive_id, hal->hal_flags,
1140                                             uuid, hai);
1141                 if (IS_ERR(car))
1142                         GOTO(out, rc = PTR_ERR(car));
1143
1144                 rc = mdt_cdt_add_request(cdt, car);
1145                 if (rc != 0)
1146                         mdt_cdt_free_request(car);
1147         }
1148 out:
1149         RETURN(rc);
1150 }
1151
1152 /**
1153  * swap layouts between 2 fids
1154  * \param mti [IN] context
1155  * \param fid1 [IN]
1156  * \param fid2 [IN]
1157  */
1158 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1159                             const lustre_fid *fid, const lustre_fid *dfid)
1160 {
1161         struct mdt_device       *mdt = mti->mti_mdt;
1162         struct mdt_object       *child1, *child2;
1163         struct mdt_lock_handle  *lh2;
1164         int                      rc;
1165         ENTRY;
1166
1167         child1 = mdt_object_find(mti->mti_env, mdt, fid);
1168         if (IS_ERR(child1))
1169                 GOTO(out, rc = PTR_ERR(child1));
1170
1171         /* we already have layout lock on FID so take only
1172          * on dfid */
1173         lh2 = &mti->mti_lh[MDT_LH_OLD];
1174         mdt_lock_reg_init(lh2, LCK_EX);
1175         child2 = mdt_object_find_lock(mti, dfid, lh2, MDS_INODELOCK_LAYOUT);
1176         if (IS_ERR(child2))
1177                 GOTO(out_child1, rc = PTR_ERR(child2));
1178
1179         /* if copy tool closes the volatile before sending the final
1180          * progress through llapi_hsm_copy_end(), all the objects
1181          * are removed and mdd_swap_layout LBUG */
1182         if (mdt_object_exists(child2)) {
1183                 rc = mo_swap_layouts(mti->mti_env, mdt_object_child(child1),
1184                                      mdt_object_child(child2), 0);
1185         } else {
1186                 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1187                        mdt_obd_name(mti->mti_mdt), PFID(dfid));
1188                 rc = -ENOENT;
1189         }
1190
1191         mdt_object_unlock_put(mti, child2, lh2, 1);
1192 out_child1:
1193         mdt_object_put(mti->mti_env, child1);
1194 out:
1195         RETURN(rc);
1196 }
1197
1198 /**
1199  * update status of a completed request
1200  * \param mti [IN] context
1201  * \param pgs [IN] progress of the copy tool
1202  * \param update_record [IN] update llog record
1203  * \retval 0 success
1204  * \retval -ve failure
1205  */
1206 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1207                                      struct hsm_progress_kernel *pgs,
1208                                      const struct cdt_agent_req *car,
1209                                      enum agent_req_status *status)
1210 {
1211         const struct lu_env     *env = mti->mti_env;
1212         struct mdt_device       *mdt = mti->mti_mdt;
1213         struct coordinator      *cdt = &mdt->mdt_coordinator;
1214         struct mdt_object       *obj = NULL;
1215         int                      cl_flags = 0, rc = 0;
1216         struct md_hsm            mh;
1217         bool                     is_mh_changed;
1218         ENTRY;
1219
1220         /* default is to retry */
1221         *status = ARS_WAITING;
1222
1223         /* find object by FID */
1224         obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1225         /* we will update MD HSM only if needed */
1226         is_mh_changed = false;
1227         if (IS_ERR(obj)) {
1228                 /* object removed */
1229                 *status = ARS_SUCCEED;
1230                 goto unlock;
1231         }
1232
1233         /* no need to change mh->mh_arch_id
1234          * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1235          */
1236         if (pgs->hpk_errval != 0) {
1237                 switch (pgs->hpk_errval) {
1238                 case ENOSYS:
1239                         /* the copy tool does not support cancel
1240                          * so the cancel request is failed
1241                          * As we cannot distinguish a cancel progress
1242                          * from another action progress (they have the
1243                          * same cookie), we suppose here the CT returns
1244                          * ENOSYS only if does not support cancel
1245                          */
1246                         /* this can also happen when cdt calls it to
1247                          * for a timeouted request */
1248                         *status = ARS_FAILED;
1249                         /* to have a cancel event in changelog */
1250                         pgs->hpk_errval = ECANCELED;
1251                         break;
1252                 case ECANCELED:
1253                         /* the request record has already been set to
1254                          * ARS_CANCELED, this set the cancel request
1255                          * to ARS_SUCCEED */
1256                         *status = ARS_SUCCEED;
1257                         break;
1258                 default:
1259                         *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1260                                    !(pgs->hpk_flags & HP_FLAG_RETRY) ?
1261                                    ARS_FAILED : ARS_WAITING);
1262                         break;
1263                 }
1264
1265                 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1266                         CERROR("%s: Request "LPX64" on "DFID
1267                                " failed, error code %d too large\n",
1268                                mdt_obd_name(mdt),
1269                                pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1270                                pgs->hpk_errval);
1271                         hsm_set_cl_error(&cl_flags,
1272                                          CLF_HSM_ERROVERFLOW);
1273                         rc = -EINVAL;
1274                 } else {
1275                         hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1276                 }
1277
1278                 switch (car->car_hai->hai_action) {
1279                 case HSMA_ARCHIVE:
1280                         hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1281                         break;
1282                 case HSMA_RESTORE:
1283                         hsm_set_cl_event(&cl_flags, HE_RESTORE);
1284                         break;
1285                 case HSMA_REMOVE:
1286                         hsm_set_cl_event(&cl_flags, HE_REMOVE);
1287                         break;
1288                 case HSMA_CANCEL:
1289                         hsm_set_cl_event(&cl_flags, HE_CANCEL);
1290                         CERROR("%s: Failed request "LPX64" on "DFID
1291                                " cannot be a CANCEL\n",
1292                                mdt_obd_name(mdt),
1293                                pgs->hpk_cookie,
1294                                PFID(&pgs->hpk_fid));
1295                         break;
1296                 default:
1297                         CERROR("%s: Failed request "LPX64" on "DFID
1298                                " %d is an unknown action\n",
1299                                mdt_obd_name(mdt),
1300                                pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1301                                car->car_hai->hai_action);
1302                         rc = -EINVAL;
1303                         break;
1304                 }
1305         } else {
1306                 *status = ARS_SUCCEED;
1307                 switch (car->car_hai->hai_action) {
1308                 case HSMA_ARCHIVE:
1309                         hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1310                         /* set ARCHIVE keep EXIST and clear LOST and
1311                          * DIRTY */
1312                         mh.mh_arch_ver = pgs->hpk_data_version;
1313                         mh.mh_flags |= HS_ARCHIVED;
1314                         mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1315                         is_mh_changed = true;
1316                         break;
1317                 case HSMA_RESTORE:
1318                         hsm_set_cl_event(&cl_flags, HE_RESTORE);
1319
1320                         /* clear RELEASED and DIRTY */
1321                         mh.mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1322                         /* Restoring has changed the file version on
1323                          * disk. */
1324                         mh.mh_arch_ver = pgs->hpk_data_version;
1325                         is_mh_changed = true;
1326                         break;
1327                 case HSMA_REMOVE:
1328                         hsm_set_cl_event(&cl_flags, HE_REMOVE);
1329                         /* clear ARCHIVED EXISTS and LOST */
1330                         mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1331                         is_mh_changed = true;
1332                         break;
1333                 case HSMA_CANCEL:
1334                         hsm_set_cl_event(&cl_flags, HE_CANCEL);
1335                         CERROR("%s: Successful request "LPX64
1336                                " on "DFID
1337                                " cannot be a CANCEL\n",
1338                                mdt_obd_name(mdt),
1339                                pgs->hpk_cookie,
1340                                PFID(&pgs->hpk_fid));
1341                         break;
1342                 default:
1343                         CERROR("%s: Successful request "LPX64
1344                                " on "DFID
1345                                " %d is an unknown action\n",
1346                                mdt_obd_name(mdt),
1347                                pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1348                                car->car_hai->hai_action);
1349                         rc = -EINVAL;
1350                         break;
1351                 }
1352         }
1353
1354         /* rc != 0 means error when analysing action, it may come from
1355          * a crasy CT no need to manage DIRTY
1356          */
1357         if (rc == 0)
1358                 hsm_set_cl_flags(&cl_flags,
1359                                  mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1360
1361         /* unlock is done later, after layout lock management */
1362         if (is_mh_changed)
1363                 rc = mdt_hsm_attr_set(mti, obj, &mh);
1364
1365 unlock:
1366         /* we give back layout lock only if restore was successful or
1367          * if restore was canceled or if policy is to not retry
1368          * in other cases we just unlock the object */
1369         if (car->car_hai->hai_action == HSMA_RESTORE &&
1370             (pgs->hpk_errval == 0 || pgs->hpk_errval == ECANCELED ||
1371              cdt->cdt_policy & CDT_NORETRY_ACTION)) {
1372                 struct cdt_restore_handle       *crh;
1373
1374                 /* restore in data FID done, we swap the layouts
1375                  * only if restore is successfull */
1376                 if (pgs->hpk_errval == 0) {
1377                         rc = hsm_swap_layouts(mti, &car->car_hai->hai_fid,
1378                                               &car->car_hai->hai_dfid);
1379                         if (rc) {
1380                                 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1381                                         *status = ARS_FAILED;
1382                                 pgs->hpk_errval = -rc;
1383                         }
1384                 }
1385                 /* we have to retry, so keep layout lock */
1386                 if (*status == ARS_WAITING)
1387                         GOTO(out, rc);
1388
1389                 /* give back layout lock */
1390                 mutex_lock(&cdt->cdt_restore_lock);
1391                 crh = hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1392                 if (crh != NULL)
1393                         list_del(&crh->crh_list);
1394                 mutex_unlock(&cdt->cdt_restore_lock);
1395                 /* just give back layout lock, we keep
1396                  * the reference which is given back
1397                  * later with the lock for HSM flags */
1398                 if (!IS_ERR(obj) && crh != NULL)
1399                         mdt_object_unlock(mti, obj, &crh->crh_lh, 1);
1400
1401                 if (crh != NULL)
1402                         OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1403         }
1404
1405         GOTO(out, rc);
1406
1407 out:
1408         if (obj != NULL && !IS_ERR(obj)) {
1409                 mo_changelog(env, CL_HSM, cl_flags,
1410                              mdt_object_child(obj));
1411                 mdt_object_put(mti->mti_env, obj);
1412         }
1413
1414         RETURN(rc);
1415 }
1416
1417 /**
1418  * update status of a request
1419  * \param mti [IN] context
1420  * \param pgs [IN] progress of the copy tool
1421  * \param update_record [IN] update llog record
1422  * \retval 0 success
1423  * \retval -ve failure
1424  */
1425 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1426                                  struct hsm_progress_kernel *pgs,
1427                                  const int update_record)
1428 {
1429         struct mdt_device       *mdt = mti->mti_mdt;
1430         struct coordinator      *cdt = &mdt->mdt_coordinator;
1431         struct cdt_agent_req    *car;
1432         int                      rc = 0;
1433         ENTRY;
1434
1435         /* no coordinator started, so we cannot serve requests */
1436         if (cdt->cdt_state == CDT_STOPPED)
1437                 RETURN(-EAGAIN);
1438
1439         /* first do sanity checks */
1440         car = mdt_cdt_update_request(cdt, pgs);
1441         if (IS_ERR(car)) {
1442                 CERROR("%s: Cannot find running request for cookie "LPX64
1443                        " on fid="DFID"\n",
1444                        mdt_obd_name(mdt),
1445                        pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1446                 if (car == NULL)
1447                         RETURN(-ENOENT);
1448                 RETURN(PTR_ERR(car));
1449         }
1450
1451         CDEBUG(D_HSM, "Progress received for fid="DFID" cookie="LPX64
1452                       " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1453                       PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1454                       hsm_copytool_action2name(car->car_hai->hai_action),
1455                       pgs->hpk_flags, pgs->hpk_errval,
1456                       PFID(&car->car_hai->hai_fid),
1457                       PFID(&car->car_hai->hai_dfid));
1458
1459         /* progress is done on FID or data FID depending of the action and
1460          * of the copy progress */
1461         /* for restore progress is used to send back the data FID to cdt */
1462         if (car->car_hai->hai_action == HSMA_RESTORE &&
1463             lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1464                 car->car_hai->hai_dfid = pgs->hpk_fid;
1465
1466         if ((car->car_hai->hai_action == HSMA_RESTORE ||
1467              car->car_hai->hai_action == HSMA_ARCHIVE) &&
1468             (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1469              !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1470                 CERROR("%s: Progress on "DFID" for cookie "LPX64
1471                        " does not match request FID "DFID" nor data FID "
1472                        DFID"\n",
1473                        mdt_obd_name(mdt),
1474                        PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1475                        PFID(&car->car_hai->hai_fid),
1476                        PFID(&car->car_hai->hai_dfid));
1477                 GOTO(out, rc = -EINVAL);
1478         }
1479
1480         if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1481                 CERROR("%s: Progress on "DFID" for cookie "LPX64" action=%s"
1482                        " is not coherent (err=%d and not completed"
1483                        " (flags=%d))\n",
1484                        mdt_obd_name(mdt),
1485                        PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1486                        hsm_copytool_action2name(car->car_hai->hai_action),
1487                        pgs->hpk_errval, pgs->hpk_flags);
1488                 GOTO(out, rc = -EINVAL);
1489         }
1490
1491         /* now progress is valid */
1492
1493         /* we use a root like ucred */
1494         hsm_init_ucred(mdt_ucred(mti));
1495
1496         if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1497                 enum agent_req_status    status;
1498
1499                 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1500
1501                 /* remove request from memory list */
1502                 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1503
1504                 CDEBUG(D_HSM, "Updating record: fid="DFID" cookie="LPX64
1505                               " action=%s status=%s\n", PFID(&pgs->hpk_fid),
1506                        pgs->hpk_cookie,
1507                        hsm_copytool_action2name(car->car_hai->hai_action),
1508                        agent_req_status2name(status));
1509
1510                 if (update_record) {
1511                         int rc1;
1512
1513                         rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1514                                                      &pgs->hpk_cookie, 1,
1515                                                      status);
1516                         if (rc1)
1517                                 CERROR("%s: mdt_agent_record_update() failed,"
1518                                        " rc=%d, cannot update status to %s"
1519                                        " for cookie "LPX64"\n",
1520                                        mdt_obd_name(mdt), rc1,
1521                                        agent_req_status2name(status),
1522                                        pgs->hpk_cookie);
1523                         rc = (rc != 0 ? rc : rc1);
1524                 }
1525                 /* ct has completed a request, so a slot is available, wakeup
1526                  * cdt to find new work */
1527                 mdt_hsm_cdt_wakeup(mdt);
1528         } else {
1529                 /* if copytool send a progress on a canceled request
1530                  * we inform copytool it should stop
1531                  */
1532                 if (car->car_canceled == 1)
1533                         rc = -ECANCELED;
1534         }
1535         GOTO(out, rc);
1536
1537 out:
1538         /* remove ref got from mdt_cdt_update_request() */
1539         mdt_cdt_put_request(car);
1540
1541         return rc;
1542 }
1543
1544
1545 /**
1546  * data passed to llog_cat_process() callback
1547  * to cancel requests
1548  */
1549 struct hsm_cancel_all_data {
1550         struct mdt_device       *mdt;
1551 };
1552
1553 /**
1554  *  llog_cat_process() callback, used to:
1555  *  - purge all requests
1556  * \param env [IN] environment
1557  * \param llh [IN] llog handle
1558  * \param hdr [IN] llog record
1559  * \param data [IN] cb data = struct hsm_cancel_all_data
1560  * \retval 0 success
1561  * \retval -ve failure
1562  */
1563 static int mdt_cancel_all_cb(const struct lu_env *env,
1564                              struct llog_handle *llh,
1565                              struct llog_rec_hdr *hdr, void *data)
1566 {
1567         struct llog_agent_req_rec       *larr;
1568         struct hsm_cancel_all_data      *hcad;
1569         int                              rc = 0;
1570         ENTRY;
1571
1572         larr = (struct llog_agent_req_rec *)hdr;
1573         hcad = data;
1574         if (larr->arr_status == ARS_WAITING ||
1575             larr->arr_status == ARS_STARTED) {
1576                 larr->arr_status = ARS_CANCELED;
1577                 larr->arr_req_change = cfs_time_current_sec();
1578                 rc = mdt_agent_llog_update_rec(env, hcad->mdt, llh, larr);
1579                 if (rc == 0)
1580                         RETURN(LLOG_DEL_RECORD);
1581         }
1582         RETURN(rc);
1583 }
1584
1585 /**
1586  * cancel all actions
1587  * \param obd [IN] MDT device
1588  */
1589 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1590 {
1591         struct mdt_thread_info          *mti;
1592         struct coordinator              *cdt = &mdt->mdt_coordinator;
1593         struct cdt_agent_req            *car;
1594         struct hsm_action_list          *hal = NULL;
1595         struct hsm_action_item          *hai;
1596         struct hsm_cancel_all_data       hcad;
1597         int                              hal_sz = 0, hal_len, rc;
1598         enum cdt_states                  save_state;
1599         ENTRY;
1600
1601         /* retrieve coordinator context */
1602         mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1603
1604         /* disable coordinator */
1605         save_state = cdt->cdt_state;
1606         cdt->cdt_state = CDT_DISABLE;
1607
1608         /* send cancel to all running requests */
1609         down_read(&cdt->cdt_request_lock);
1610         list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
1611                 mdt_cdt_get_request(car);
1612                 /* request is not yet removed from list, it will be done
1613                  * when copytool will return progress
1614                  */
1615
1616                 if (car->car_hai->hai_action == HSMA_CANCEL) {
1617                         mdt_cdt_put_request(car);
1618                         continue;
1619                 }
1620
1621                 /* needed size */
1622                 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1623                           cfs_size_round(car->car_hai->hai_len);
1624
1625                 if (hal_len > hal_sz && hal_sz > 0) {
1626                         /* not enough room, free old buffer */
1627                         OBD_FREE(hal, hal_sz);
1628                         hal = NULL;
1629                 }
1630
1631                 /* empty buffer, allocate one */
1632                 if (hal == NULL) {
1633                         hal_sz = hal_len;
1634                         OBD_ALLOC(hal, hal_sz);
1635                         if (hal == NULL) {
1636                                 mdt_cdt_put_request(car);
1637                                 up_read(&cdt->cdt_request_lock);
1638                                 GOTO(out, rc = -ENOMEM);
1639                         }
1640                 }
1641
1642                 hal->hal_version = HAL_VERSION;
1643                 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1644                                 MTI_NAME_MAXLEN);
1645                 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1646                 hal->hal_compound_id = car->car_compound_id;
1647                 hal->hal_archive_id = car->car_archive_id;
1648                 hal->hal_flags = car->car_flags;
1649                 hal->hal_count = 0;
1650
1651                 hai = hai_first(hal);
1652                 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1653                 hai->hai_action = HSMA_CANCEL;
1654                 hal->hal_count = 1;
1655
1656                 /* it is possible to safely call mdt_hsm_agent_send()
1657                  * (ie without a deadlock on cdt_request_lock), because the
1658                  * write lock is taken only if we are not in purge mode
1659                  * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1660                  *   nor mdt_cdt_remove_request())
1661                  */
1662                 /* no conflict with cdt thread because cdt is disable and we
1663                  * have the request lock */
1664                 mdt_hsm_agent_send(mti, hal, 1);
1665
1666                 mdt_cdt_put_request(car);
1667         }
1668         up_read(&cdt->cdt_request_lock);
1669
1670         if (hal != NULL)
1671                 OBD_FREE(hal, hal_sz);
1672
1673         /* cancel all on-disk records */
1674         hcad.mdt = mdt;
1675
1676         rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1677                               mdt_cancel_all_cb, &hcad);
1678 out:
1679         /* enable coordinator */
1680         cdt->cdt_state = save_state;
1681
1682         RETURN(rc);
1683 }
1684
1685 /**
1686  * check if a request is comptaible with file status
1687  * \param hai [IN] request description
1688  * \param hal_an [IN] request archive number (not used)
1689  * \param rq_flags [IN] request flags
1690  * \param hsm [IN] file HSM metadata
1691  * \retval boolean
1692  */
1693 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1694                               const int hal_an, const __u64 rq_flags,
1695                               const struct md_hsm *hsm)
1696 {
1697         int      is_compat = false;
1698         int      hsm_flags;
1699         ENTRY;
1700
1701         hsm_flags = hsm->mh_flags;
1702         switch (hai->hai_action) {
1703         case HSMA_ARCHIVE:
1704                 if (!(hsm_flags & HS_NOARCHIVE) &&
1705                     (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1706                         is_compat = true;
1707                 break;
1708         case HSMA_RESTORE:
1709                 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1710                     hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1711                         is_compat = true;
1712                 break;
1713         case HSMA_REMOVE:
1714                 if (!(hsm_flags & HS_RELEASED) &&
1715                     (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1716                         is_compat = true;
1717                 break;
1718         case HSMA_CANCEL:
1719                 is_compat = true;
1720                 break;
1721         }
1722         CDEBUG(D_HSM, "fid="DFID" action=%s flags="LPX64
1723                       " extent="LPX64"-"LPX64" hsm_flags=%.8X %s\n",
1724                       PFID(&hai->hai_fid),
1725                       hsm_copytool_action2name(hai->hai_action), rq_flags,
1726                       hai->hai_extent.offset, hai->hai_extent.length,
1727                       hsm->mh_flags,
1728                       (is_compat ? "compatible" : "uncompatible"));
1729
1730         RETURN(is_compat);
1731 }
1732
1733 /*
1734  * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1735  */
1736 static const struct {
1737         __u64            bit;
1738         char            *name;
1739         char            *nickname;
1740 } hsm_policy_names[] = {
1741         { CDT_NONBLOCKING_RESTORE,      "NonBlockingRestore",   "NBR"},
1742         { CDT_NORETRY_ACTION,           "NoRetryAction",        "NRA"},
1743         { 0 },
1744 };
1745
1746 /**
1747  * convert a policy name to a bit
1748  * \param name [IN] policy name
1749  * \retval 0 unknown
1750  * \retval   policy bit
1751  */
1752 static __u64 hsm_policy_str2bit(const char *name)
1753 {
1754         int      i;
1755
1756         for (i = 0; hsm_policy_names[i].bit != 0; i++)
1757                 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
1758                     strcmp(hsm_policy_names[i].name, name) == 0)
1759                         return hsm_policy_names[i].bit;
1760         return 0;
1761 }
1762
1763 /**
1764  * convert a policy bit field to a string
1765  * \param mask [IN] policy bit field
1766  * \param hexa [IN] print mask before bit names
1767  * \param buffer [OUT] string
1768  * \param count [IN] size of buffer
1769  * \retval size filled in buffer
1770  */
1771 static int hsm_policy_bit2str(const __u64 mask, const bool hexa, char *buffer,
1772                               int count)
1773 {
1774         int      i, j, sz;
1775         char    *ptr;
1776         __u64    bit;
1777         ENTRY;
1778
1779         ptr = buffer;
1780         if (hexa) {
1781                 sz = snprintf(buffer, count, "("LPX64") ", mask);
1782                 ptr += sz;
1783                 count -= sz;
1784         }
1785         for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
1786                 bit = (1ULL << i);
1787
1788                 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1789                         if (hsm_policy_names[j].bit == bit)
1790                                 break;
1791                 }
1792                 if (bit & mask)
1793                         sz = snprintf(ptr, count, "[%s] ",
1794                                       hsm_policy_names[j].name);
1795                 else
1796                         sz = snprintf(ptr, count, "%s ",
1797                                       hsm_policy_names[j].name);
1798
1799                 ptr += sz;
1800                 count -= sz;
1801         }
1802         /* remove last ' ' */
1803         *ptr = '\0';
1804         ptr--;
1805         RETURN(ptr - buffer);
1806 }
1807
1808 /* methods to read/write HSM policy flags */
1809 static int lprocfs_rd_hsm_policy(char *page, char **start, off_t off,
1810                                  int count, int *eof, void *data)
1811 {
1812         struct mdt_device       *mdt = data;
1813         struct coordinator      *cdt = &mdt->mdt_coordinator;
1814         int                      sz;
1815         ENTRY;
1816
1817         sz = hsm_policy_bit2str(cdt->cdt_policy, false, page, count);
1818         page[sz] = '\n';
1819         sz++;
1820         page[sz] = '\0';
1821         *eof = 1;
1822         RETURN(sz);
1823 }
1824
1825 static int lprocfs_wr_hsm_policy(struct file *file, const char *buffer,
1826                                  unsigned long count, void *data)
1827 {
1828         struct mdt_device       *mdt = data;
1829         struct coordinator      *cdt = &mdt->mdt_coordinator;
1830         char                    *start, *token, sign;
1831         char                    *buf;
1832         __u64                    policy;
1833         __u64                    add_mask, remove_mask, set_mask;
1834         int                      sz;
1835         int                      rc;
1836         ENTRY;
1837
1838         if (count + 1 > PAGE_SIZE)
1839                 RETURN(-EINVAL);
1840
1841         OBD_ALLOC(buf, count + 1);
1842         if (buf == NULL)
1843                 RETURN(-ENOMEM);
1844
1845         if (copy_from_user(buf, buffer, count))
1846                 RETURN(-EFAULT);
1847         buf[count] = '\0';
1848
1849         start = buf;
1850         CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
1851                start);
1852
1853         add_mask = remove_mask = set_mask = 0;
1854         do {
1855                 token = strsep(&start, "\n ");
1856                 sign = *token;
1857
1858                 if (sign == '\0')
1859                         continue;
1860
1861                 if (sign == '-' || sign == '+')
1862                         token++;
1863
1864                 policy = hsm_policy_str2bit(token);
1865                 if (policy == 0) {
1866                         char *msg;
1867
1868                         sz = PAGE_SIZE;
1869                         OBD_ALLOC(msg, sz);
1870                         if (!msg)
1871                                 RETURN(-ENOMEM);
1872
1873                         hsm_policy_bit2str(0, false, msg, sz);
1874                         CWARN("%s: '%s' is unknown, "
1875                               "supported policies are: %s\n", mdt_obd_name(mdt),
1876                               token, msg);
1877                         OBD_FREE(msg, sz);
1878                         GOTO(out, rc = -EINVAL);
1879                 }
1880                 switch (sign) {
1881                 case '-':
1882                         remove_mask |= policy;
1883                         break;
1884                 case '+':
1885                         add_mask |= policy;
1886                         break;
1887                 default:
1888                         set_mask |= policy;
1889                         break;
1890                 }
1891
1892         } while (start != NULL);
1893
1894         CDEBUG(D_HSM, "%s: new policy: rm="LPX64" add="LPX64" set="LPX64"\n",
1895                mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
1896
1897         /* if no sign in all string, it is a clear and set
1898          * if some sign found, all unsigned are converted
1899          * to add
1900          * P1 P2 = set to P1 and P2
1901          * P1 -P2 = add P1 clear P2 same as +P1 -P2
1902          */
1903         if (remove_mask == 0 && add_mask == 0) {
1904                 cdt->cdt_policy = set_mask;
1905         } else {
1906                 cdt->cdt_policy |= set_mask | add_mask;
1907                 cdt->cdt_policy &= ~remove_mask;
1908         }
1909
1910         GOTO(out, rc = count);
1911
1912 out:
1913         OBD_FREE(buf, count + 1);
1914         RETURN(rc);
1915 }
1916
1917 #define GENERATE_PROC_METHOD(VAR)                                       \
1918 static int lprocfs_rd_hsm_##VAR(char *page, char **start, off_t off,    \
1919                                 int count, int *eof, void *data)        \
1920 {                                                                       \
1921         struct mdt_device       *mdt = data;                            \
1922         struct coordinator      *cdt = &mdt->mdt_coordinator;           \
1923         int                      sz;                                    \
1924         ENTRY;                                                          \
1925                                                                         \
1926         sz = snprintf(page, count, LPU64"\n", (__u64)cdt->VAR);         \
1927         *eof = 1;                                                       \
1928         RETURN(sz);                                                     \
1929 }                                                                       \
1930 static int lprocfs_wr_hsm_##VAR(struct file *file, const char *buffer,  \
1931                                 unsigned long count, void *data)        \
1932                                                                         \
1933 {                                                                       \
1934         struct mdt_device       *mdt = data;                            \
1935         struct coordinator      *cdt = &mdt->mdt_coordinator;           \
1936         int                      val;                                   \
1937         int                      rc;                                    \
1938         ENTRY;                                                          \
1939                                                                         \
1940         rc = lprocfs_write_helper(buffer, count, &val);                 \
1941         if (rc)                                                         \
1942                 RETURN(rc);                                             \
1943         if (val > 0) {                                                  \
1944                 cdt->VAR = val;                                         \
1945                 RETURN(count);                                          \
1946         }                                                               \
1947         RETURN(-EINVAL);                                                \
1948 }
1949
1950 GENERATE_PROC_METHOD(cdt_loop_period)
1951 GENERATE_PROC_METHOD(cdt_delay)
1952 GENERATE_PROC_METHOD(cdt_timeout)
1953 GENERATE_PROC_METHOD(cdt_max_request)
1954 GENERATE_PROC_METHOD(cdt_default_archive_id)
1955
1956 /*
1957  * procfs write method for MDT/hsm_control
1958  * proc entry is in mdt directory so data is mdt obd_device pointer
1959  */
1960 #define CDT_ENABLE_CMD   "enabled"
1961 #define CDT_STOP_CMD     "shutdown"
1962 #define CDT_DISABLE_CMD  "disabled"
1963 #define CDT_PURGE_CMD    "purge"
1964 #define CDT_HELP_CMD     "help"
1965
1966 int lprocfs_wr_hsm_cdt_control(struct file *file, const char *buffer,
1967                                unsigned long count, void *data)
1968 {
1969         struct obd_device       *obd = data;
1970         struct mdt_device       *mdt = mdt_dev(obd->obd_lu_dev);
1971         struct coordinator      *cdt = &(mdt->mdt_coordinator);
1972         int                      rc, usage = 0;
1973         ENTRY;
1974
1975         rc = 0;
1976         if (strncmp(buffer, CDT_ENABLE_CMD, strlen(CDT_ENABLE_CMD)) == 0) {
1977                 if (cdt->cdt_state == CDT_DISABLE) {
1978                         cdt->cdt_state = CDT_RUNNING;
1979                         mdt_hsm_cdt_wakeup(mdt);
1980                 } else {
1981                         rc = mdt_hsm_cdt_start(mdt);
1982                 }
1983         } else if (strncmp(buffer, CDT_STOP_CMD, strlen(CDT_STOP_CMD)) == 0) {
1984                 cdt->cdt_state = CDT_STOPPING;
1985         } else if (strncmp(buffer, CDT_DISABLE_CMD,
1986                            strlen(CDT_DISABLE_CMD)) == 0) {
1987                 cdt->cdt_state = CDT_DISABLE;
1988         } else if (strncmp(buffer, CDT_PURGE_CMD, strlen(CDT_PURGE_CMD)) == 0) {
1989                 rc = hsm_cancel_all_actions(mdt);
1990         } else if (strncmp(buffer, CDT_HELP_CMD, strlen(CDT_HELP_CMD)) == 0) {
1991                 usage = 1;
1992         } else {
1993                 usage = 1;
1994                 rc = -EINVAL;
1995         }
1996
1997         if (usage == 1)
1998                 CERROR("%s: Valid coordinator control commands are: "
1999                        "%s %s %s %s %s\n", mdt_obd_name(mdt),
2000                        CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
2001                        CDT_PURGE_CMD, CDT_HELP_CMD);
2002
2003         if (rc)
2004                 RETURN(rc);
2005
2006         RETURN(count);
2007 }
2008
2009 int lprocfs_rd_hsm_cdt_control(char *page, char **start, off_t off,
2010                                int count, int *eof, void *data)
2011 {
2012         struct obd_device       *obd = data;
2013         struct coordinator      *cdt;
2014         int                      sz;
2015         ENTRY;
2016
2017         cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
2018         *eof = 1;
2019
2020         if (cdt->cdt_state == CDT_INIT)
2021                 sz = snprintf(page, count, "init\n");
2022         else if (cdt->cdt_state == CDT_RUNNING)
2023                 sz = snprintf(page, count, "enabled\n");
2024         else if (cdt->cdt_state == CDT_STOPPING)
2025                 sz = snprintf(page, count, "stopping\n");
2026         else if (cdt->cdt_state == CDT_STOPPED)
2027                 sz = snprintf(page, count, "stopped\n");
2028         else if (cdt->cdt_state == CDT_DISABLE)
2029                 sz = snprintf(page, count, "disabled\n");
2030         else
2031                 sz = snprintf(page, count, "unknown\n");
2032
2033         RETURN(sz);
2034 }
2035
2036 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
2037         { "agents",             NULL, NULL, NULL, &mdt_hsm_agent_fops, 0 },
2038         { "agent_actions",      NULL, NULL, NULL,
2039                                 &mdt_agent_actions_fops, 0444 },
2040         { "default_archive_id", lprocfs_rd_hsm_cdt_default_archive_id,
2041                                 lprocfs_wr_hsm_cdt_default_archive_id,
2042                                 NULL, NULL, 0 },
2043         { "grace_delay",        lprocfs_rd_hsm_cdt_delay,
2044                                 lprocfs_wr_hsm_cdt_delay,
2045                                 NULL, NULL, 0 },
2046         { "loop_period",        lprocfs_rd_hsm_cdt_loop_period,
2047                                 lprocfs_wr_hsm_cdt_loop_period,
2048                                 NULL, NULL, 0 },
2049         { "max_requests",       lprocfs_rd_hsm_cdt_max_request,
2050                                 lprocfs_wr_hsm_cdt_max_request,
2051                                 NULL, NULL, 0 },
2052         { "policy",             lprocfs_rd_hsm_policy, lprocfs_wr_hsm_policy,
2053                                 NULL, NULL, 0 },
2054         { "request_timeout",    lprocfs_rd_hsm_cdt_timeout,
2055                                 lprocfs_wr_hsm_cdt_timeout,
2056                                 NULL, NULL, 0 },
2057         { "requests",           NULL, NULL, NULL, &mdt_hsm_request_fops, 0 },
2058         { 0 }
2059 };