Whamcloud - gitweb
f98ec0ef79c47886a88c2bc592d3790b133a8e7e
[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 static 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  * coordinator thread
411  * \param data [IN] obd device
412  * \retval 0 success
413  * \retval -ve failure
414  */
415 static int mdt_coordinator(void *data)
416 {
417         struct mdt_thread_info  *mti = data;
418         struct mdt_device       *mdt = mti->mti_mdt;
419         struct coordinator      *cdt = &mdt->mdt_coordinator;
420         struct hsm_scan_data     hsd = { 0 };
421         int                      rc = 0;
422         ENTRY;
423
424         cdt->cdt_thread.t_flags = SVC_RUNNING;
425         wake_up(&cdt->cdt_thread.t_ctl_waitq);
426
427         CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
428                mdt_obd_name(mdt), current_pid());
429
430         /*
431          * create /proc entries for coordinator
432          */
433         hsm_cdt_procfs_init(mdt);
434         /* timeouted cookie vector initialization */
435         hsd.max_cookie = 0;
436         hsd.cookie_cnt = 0;
437         hsd.cookies = NULL;
438         /* we use a copy of cdt_max_request in the cb, so if cdt_max_request
439          * increases due to a change from /proc we do not overflow the
440          * hsd.request[] vector
441          */
442         hsd.max_request = cdt->cdt_max_request;
443         hsd.request_sz = hsd.max_request * sizeof(*hsd.request);
444         OBD_ALLOC(hsd.request, hsd.request_sz);
445         if (!hsd.request)
446                 GOTO(out, rc = -ENOMEM);
447
448         hsd.mti = mti;
449         obd_uuid2fsname(hsd.fs_name, mdt_obd_name(mdt), MTI_NAME_MAXLEN);
450
451         while (1) {
452                 struct l_wait_info lwi;
453                 int i;
454
455                 lwi = LWI_TIMEOUT(cfs_time_seconds(cdt->cdt_loop_period),
456                                   NULL, NULL);
457                 l_wait_event(cdt->cdt_thread.t_ctl_waitq,
458                              (cdt->cdt_thread.t_flags &
459                               (SVC_STOPPING|SVC_EVENT)),
460                              &lwi);
461
462                 CDEBUG(D_HSM, "coordinator resumes\n");
463
464                 if (cdt->cdt_thread.t_flags & SVC_STOPPING ||
465                     cdt->cdt_state == CDT_STOPPING) {
466                         cdt->cdt_thread.t_flags &= ~SVC_STOPPING;
467                         rc = 0;
468                         break;
469                 }
470
471                 /* wake up before timeout, new work arrives */
472                 if (cdt->cdt_thread.t_flags & SVC_EVENT)
473                         cdt->cdt_thread.t_flags &= ~SVC_EVENT;
474
475                 /* if coordinator is suspended continue to wait */
476                 if (cdt->cdt_state == CDT_DISABLE) {
477                         CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
478                         continue;
479                 }
480
481                 CDEBUG(D_HSM, "coordinator starts reading llog\n");
482
483                 if (hsd.max_request != cdt->cdt_max_request) {
484                         /* cdt_max_request has changed,
485                          * we need to allocate a new buffer
486                          */
487                         OBD_FREE(hsd.request, hsd.request_sz);
488                         hsd.max_request = cdt->cdt_max_request;
489                         hsd.request_sz =
490                                    hsd.max_request * sizeof(*hsd.request);
491                         OBD_ALLOC(hsd.request, hsd.request_sz);
492                         if (!hsd.request) {
493                                 rc = -ENOMEM;
494                                 break;
495                         }
496                 }
497
498                 /* create canceled cookie vector for an arbitrary size
499                  * if needed, vector will grow during llog scan
500                  */
501                 hsd.max_cookie = 10;
502                 hsd.cookie_cnt = 0;
503                 OBD_ALLOC(hsd.cookies, hsd.max_cookie * sizeof(__u64));
504                 if (!hsd.cookies) {
505                         rc = -ENOMEM;
506                         goto clean_cb_alloc;
507                 }
508                 hsd.request_cnt = 0;
509
510                 rc = cdt_llog_process(mti->mti_env, mdt,
511                                       mdt_coordinator_cb, &hsd);
512                 if (rc < 0)
513                         goto clean_cb_alloc;
514
515                 CDEBUG(D_HSM, "Found %d requests to send and %d"
516                               " requests to cancel\n",
517                        hsd.request_cnt, hsd.cookie_cnt);
518                 /* first we cancel llog records of the timeouted requests */
519                 if (hsd.cookie_cnt > 0) {
520                         rc = mdt_agent_record_update(mti->mti_env, mdt,
521                                                      hsd.cookies,
522                                                      hsd.cookie_cnt,
523                                                      ARS_CANCELED);
524                         if (rc)
525                                 CERROR("%s: mdt_agent_record_update() failed, "
526                                        "rc=%d, cannot update status to %s "
527                                        "for %d cookies\n",
528                                        mdt_obd_name(mdt), rc,
529                                        agent_req_status2name(ARS_CANCELED),
530                                        hsd.cookie_cnt);
531                 }
532
533                 if (list_empty(&cdt->cdt_agents)) {
534                         CDEBUG(D_HSM, "no agent available, "
535                                       "coordinator sleeps\n");
536                         goto clean_cb_alloc;
537                 }
538
539                 /* here hsd contains a list of requests to be started */
540                 for (i = 0; i < hsd.max_request; i++) {
541                         struct hsm_action_list  *hal;
542                         struct hsm_action_item  *hai;
543                         __u64                   *cookies;
544                         int                      sz, j;
545                         enum agent_req_status    status;
546
547                         /* still room for work ? */
548                         if (atomic_read(&cdt->cdt_request_count) ==
549                             cdt->cdt_max_request)
550                                 break;
551
552                         if (hsd.request[i].hal == NULL)
553                                 continue;
554
555                         /* found a request, we start it */
556                         /* kuc payload allocation so we avoid an additionnal
557                          * allocation in mdt_hsm_agent_send()
558                          */
559                         hal = kuc_alloc(hsd.request[i].hal_used_sz,
560                                         KUC_TRANSPORT_HSM, HMT_ACTION_LIST);
561                         if (IS_ERR(hal)) {
562                                 CERROR("%s: Cannot allocate memory (%d o) "
563                                        "for compound "LPX64"\n",
564                                        mdt_obd_name(mdt),
565                                        hsd.request[i].hal_used_sz,
566                                        hsd.request[i].hal->hal_compound_id);
567                                 continue;
568                         }
569                         memcpy(hal, hsd.request[i].hal,
570                                hsd.request[i].hal_used_sz);
571
572                         rc = mdt_hsm_agent_send(mti, hal, 0);
573                         /* if failure, we suppose it is temporary
574                          * if the copy tool failed to do the request
575                          * it has to use hsm_progress
576                          */
577                         status = (rc ? ARS_WAITING : ARS_STARTED);
578
579                         /* set up cookie vector to set records status
580                          * after copy tools start or failed
581                          */
582                         sz = hsd.request[i].hal->hal_count * sizeof(__u64);
583                         OBD_ALLOC(cookies, sz);
584                         if (cookies == NULL) {
585                                 CERROR("%s: Cannot allocate memory (%d o) "
586                                        "for cookies vector "LPX64"\n",
587                                        mdt_obd_name(mdt), sz,
588                                        hsd.request[i].hal->hal_compound_id);
589                                 kuc_free(hal, hsd.request[i].hal_used_sz);
590                                 continue;
591                         }
592                         hai = hai_first(hal);
593                         for (j = 0; j < hsd.request[i].hal->hal_count; j++) {
594                                 cookies[j] = hai->hai_cookie;
595                                 hai = hai_next(hai);
596                         }
597
598                         rc = mdt_agent_record_update(mti->mti_env, mdt, cookies,
599                                                 hsd.request[i].hal->hal_count,
600                                                 status);
601                         if (rc)
602                                 CERROR("%s: mdt_agent_record_update() failed, "
603                                        "rc=%d, cannot update status to %s "
604                                        "for %d cookies\n",
605                                        mdt_obd_name(mdt), rc,
606                                        agent_req_status2name(status),
607                                        hsd.request[i].hal->hal_count);
608
609                         OBD_FREE(cookies, sz);
610                         kuc_free(hal, hsd.request[i].hal_used_sz);
611                 }
612 clean_cb_alloc:
613                 /* free cookie vector allocated for/by callback */
614                 if (hsd.cookies) {
615                         OBD_FREE(hsd.cookies, hsd.max_cookie * sizeof(__u64));
616                         hsd.max_cookie = 0;
617                         hsd.cookie_cnt = 0;
618                         hsd.cookies = NULL;
619                 }
620
621                 /* free hal allocated by callback */
622                 for (i = 0; i < hsd.max_request; i++) {
623                         if (hsd.request[i].hal) {
624                                 OBD_FREE(hsd.request[i].hal,
625                                          hsd.request[i].hal_sz);
626                                 hsd.request[i].hal_sz = 0;
627                                 hsd.request[i].hal = NULL;
628                                 hsd.request_cnt--;
629                         }
630                 }
631                 LASSERT(hsd.request_cnt == 0);
632
633                 /* reset callback data */
634                 memset(hsd.request, 0, hsd.request_sz);
635         }
636         EXIT;
637 out:
638         if (hsd.request)
639                 OBD_FREE(hsd.request, hsd.request_sz);
640
641         if (hsd.cookies)
642                 OBD_FREE(hsd.cookies, hsd.max_cookie * sizeof(__u64));
643
644         if (cdt->cdt_state == CDT_STOPPING) {
645                 /* request comes from /proc path, so we need to clean cdt
646                  * struct */
647                  mdt_hsm_cdt_stop(mdt);
648                  mdt->mdt_opts.mo_coordinator = 0;
649         } else {
650                 /* request comes from a thread event, generated
651                  * by mdt_stop_coordinator(), we have to ack
652                  * and cdt cleaning will be done by event sender
653                  */
654                 cdt->cdt_thread.t_flags = SVC_STOPPED;
655                 wake_up(&cdt->cdt_thread.t_ctl_waitq);
656         }
657
658         if (rc != 0)
659                 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
660                        mdt_obd_name(mdt), current_pid(), rc);
661         else
662                 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
663                               " no error\n",
664                        mdt_obd_name(mdt), current_pid());
665
666         return rc;
667 }
668
669 /**
670  * lookup a restore handle by FID
671  * caller needs to hold cdt_restore_lock
672  * \param cdt [IN] coordinator
673  * \param fid [IN] FID
674  * \retval cdt_restore_handle found
675  * \retval NULL not found
676  */
677 static struct cdt_restore_handle *hsm_restore_hdl_find(struct coordinator *cdt,
678                                                        const struct lu_fid *fid)
679 {
680         struct cdt_restore_handle       *crh;
681         ENTRY;
682
683         list_for_each_entry(crh, &cdt->cdt_restore_hdl, crh_list) {
684                 if (lu_fid_eq(&crh->crh_fid, fid))
685                         RETURN(crh);
686         }
687         RETURN(NULL);
688 }
689
690 /**
691  * data passed to llog_cat_process() callback
692  * to scan requests and take actions
693  */
694 struct hsm_restore_data {
695         struct mdt_thread_info  *hrd_mti;
696 };
697
698 /**
699  *  llog_cat_process() callback, used to:
700  *  - find restore request and allocate the restore handle
701  * \param env [IN] environment
702  * \param llh [IN] llog handle
703  * \param hdr [IN] llog record
704  * \param data [IN/OUT] cb data = struct hsm_restore_data
705  * \retval 0 success
706  * \retval -ve failure
707  */
708 static int hsm_restore_cb(const struct lu_env *env,
709                           struct llog_handle *llh,
710                           struct llog_rec_hdr *hdr, void *data)
711 {
712         struct llog_agent_req_rec       *larr;
713         struct hsm_restore_data         *hrd;
714         struct cdt_restore_handle       *crh;
715         struct hsm_action_item          *hai;
716         struct mdt_thread_info          *mti;
717         struct coordinator              *cdt;
718         struct mdt_object               *child;
719         int rc;
720         ENTRY;
721
722         hrd = data;
723         mti = hrd->hrd_mti;
724         cdt = &mti->mti_mdt->mdt_coordinator;
725
726         larr = (struct llog_agent_req_rec *)hdr;
727         hai = &larr->arr_hai;
728         if (hai->hai_action != HSMA_RESTORE ||
729             agent_req_in_final_state(larr->arr_status))
730                 RETURN(0);
731
732         /* restore request not in a final state */
733
734         OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
735         if (crh == NULL)
736                 RETURN(-ENOMEM);
737
738         crh->crh_fid = hai->hai_fid;
739         /* in V1 all file is restored
740         crh->extent.start = hai->hai_extent.offset;
741         crh->extent.end = hai->hai_extent.offset + hai->hai_extent.length;
742         */
743         crh->crh_extent.start = 0;
744         crh->crh_extent.end = OBD_OBJECT_EOF;
745         /* get the layout lock */
746         mdt_lock_reg_init(&crh->crh_lh, LCK_EX);
747         child = mdt_object_find_lock(mti, &crh->crh_fid, &crh->crh_lh,
748                                      MDS_INODELOCK_LAYOUT);
749         if (IS_ERR(child))
750                 GOTO(out, rc = PTR_ERR(child));
751
752         rc = 0;
753         /* we choose to not keep a reference
754          * on the object during the restore time which can be very long */
755         mdt_object_put(mti->mti_env, child);
756
757         mutex_lock(&cdt->cdt_restore_lock);
758         list_add_tail(&crh->crh_list, &cdt->cdt_restore_hdl);
759         mutex_unlock(&cdt->cdt_restore_lock);
760
761 out:
762         RETURN(rc);
763 }
764
765 /**
766  * restore coordinator state at startup
767  * the goal is to take a layout lock for each registered restore request
768  * \param mti [IN] context
769  */
770 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
771 {
772         struct hsm_restore_data  hrd;
773         int                      rc;
774         ENTRY;
775
776         hrd.hrd_mti = mti;
777
778         rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
779                               hsm_restore_cb, &hrd);
780
781         RETURN(rc);
782 }
783
784 static int hsm_init_ucred(struct lu_ucred *uc)
785 {
786         ENTRY;
787
788         uc->uc_valid = UCRED_OLD;
789         uc->uc_o_uid = 0;
790         uc->uc_o_gid = 0;
791         uc->uc_o_fsuid = 0;
792         uc->uc_o_fsgid = 0;
793         uc->uc_uid = 0;
794         uc->uc_gid = 0;
795         uc->uc_fsuid = 0;
796         uc->uc_fsgid = 0;
797         uc->uc_suppgids[0] = -1;
798         uc->uc_suppgids[1] = -1;
799         uc->uc_cap = CFS_CAP_FS_MASK;
800         uc->uc_umask = 0777;
801         uc->uc_ginfo = NULL;
802         uc->uc_identity = NULL;
803
804         RETURN(0);
805 }
806
807 /**
808  * wake up coordinator thread
809  * \param mdt [IN] device
810  * \retval 0 success
811  * \retval -ve failure
812  */
813 int mdt_hsm_cdt_wakeup(struct mdt_device *mdt)
814 {
815         struct coordinator      *cdt = &mdt->mdt_coordinator;
816         ENTRY;
817
818         if (cdt->cdt_state == CDT_STOPPED)
819                 RETURN(-ESRCH);
820
821         /* wake up coordinator */
822         cdt->cdt_thread.t_flags = SVC_EVENT;
823         wake_up(&cdt->cdt_thread.t_ctl_waitq);
824
825         RETURN(0);
826 }
827
828 /**
829  * initialize coordinator struct
830  * \param mdt [IN] device
831  * \retval 0 success
832  * \retval -ve failure
833  */
834 int mdt_hsm_cdt_init(struct mdt_device *mdt)
835 {
836         struct coordinator      *cdt = &mdt->mdt_coordinator;
837         struct mdt_thread_info  *cdt_mti = NULL;
838         int                      rc;
839         ENTRY;
840
841         cdt->cdt_state = CDT_STOPPED;
842
843         init_waitqueue_head(&cdt->cdt_thread.t_ctl_waitq);
844         mutex_init(&cdt->cdt_llog_lock);
845         init_rwsem(&cdt->cdt_agent_lock);
846         init_rwsem(&cdt->cdt_request_lock);
847         mutex_init(&cdt->cdt_restore_lock);
848
849         CFS_INIT_LIST_HEAD(&cdt->cdt_requests);
850         CFS_INIT_LIST_HEAD(&cdt->cdt_agents);
851         CFS_INIT_LIST_HEAD(&cdt->cdt_restore_hdl);
852
853         rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
854         if (rc < 0)
855                 RETURN(rc);
856
857         /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
858         rc = lu_context_init(&cdt->cdt_session, LCT_SESSION);
859         if (rc == 0) {
860                 lu_context_enter(&cdt->cdt_session);
861                 cdt->cdt_env.le_ses = &cdt->cdt_session;
862         } else {
863                 lu_env_fini(&cdt->cdt_env);
864                 RETURN(rc);
865         }
866
867         cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
868         LASSERT(cdt_mti != NULL);
869
870         cdt_mti->mti_env = &cdt->cdt_env;
871         cdt_mti->mti_mdt = mdt;
872
873         hsm_init_ucred(mdt_ucred(cdt_mti));
874
875         RETURN(0);
876 }
877
878 /**
879  * free a coordinator thread
880  * \param mdt [IN] device
881  */
882 int  mdt_hsm_cdt_fini(struct mdt_device *mdt)
883 {
884         struct coordinator *cdt = &mdt->mdt_coordinator;
885         ENTRY;
886
887         lu_context_exit(cdt->cdt_env.le_ses);
888         lu_context_fini(cdt->cdt_env.le_ses);
889
890         lu_env_fini(&cdt->cdt_env);
891
892         RETURN(0);
893 }
894
895 /**
896  * start a coordinator thread
897  * \param mdt [IN] device
898  * \retval 0 success
899  * \retval -ve failure
900  */
901 int mdt_hsm_cdt_start(struct mdt_device *mdt)
902 {
903         struct coordinator      *cdt = &mdt->mdt_coordinator;
904         int                      rc;
905         void                    *ptr;
906         struct mdt_thread_info  *cdt_mti;
907         struct task_struct      *task;
908         ENTRY;
909
910         /* functions defined but not yet used
911          * this avoid compilation warning
912          */
913         ptr = dump_requests;
914
915         if (cdt->cdt_state != CDT_STOPPED) {
916                 CERROR("%s: Coordinator already started\n",
917                        mdt_obd_name(mdt));
918                 RETURN(-EALREADY);
919         }
920
921         cdt->cdt_policy = CDT_DEFAULT_POLICY;
922         cdt->cdt_state = CDT_INIT;
923
924         atomic_set(&cdt->cdt_compound_id, cfs_time_current_sec());
925         /* just need to be larger than previous one */
926         /* cdt_last_cookie is protected by cdt_llog_lock */
927         cdt->cdt_last_cookie = cfs_time_current_sec();
928         cdt->cdt_loop_period = 10;
929         cdt->cdt_delay = 60;
930         cdt->cdt_timeout = 3600;
931         cdt->cdt_max_request = 3;
932         cdt->cdt_archive_id = 1;
933         atomic_set(&cdt->cdt_request_count, 0);
934
935         /* to avoid deadlock when start is made through /proc
936          * /proc entries are created by the coordinator thread */
937
938         /* set up list of started restore requests */
939         cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
940         rc = mdt_hsm_pending_restore(cdt_mti);
941         if (rc)
942                 CERROR("%s: cannot take the layout locks needed"
943                        " for registered restore: %d",
944                        mdt_obd_name(mdt), rc);
945
946         task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
947         if (IS_ERR(task)) {
948                 rc = PTR_ERR(task);
949                 cdt->cdt_state = CDT_STOPPED;
950                 CERROR("%s: error starting coordinator thread: %d\n",
951                        mdt_obd_name(mdt), rc);
952                 RETURN(rc);
953         } else {
954                 CDEBUG(D_HSM, "%s: coordinator thread started\n",
955                        mdt_obd_name(mdt));
956                 rc = 0;
957         }
958
959         wait_event(cdt->cdt_thread.t_ctl_waitq,
960                        (cdt->cdt_thread.t_flags & SVC_RUNNING));
961
962         cdt->cdt_state = CDT_RUNNING;
963         mdt->mdt_opts.mo_coordinator = 1;
964         RETURN(0);
965 }
966
967 /**
968  * stop a coordinator thread
969  * \param mdt [IN] device
970  */
971 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
972 {
973         struct coordinator              *cdt = &mdt->mdt_coordinator;
974         struct cdt_agent_req            *car, *tmp1;
975         struct hsm_agent                *ha, *tmp2;
976         struct cdt_restore_handle       *crh, *tmp3;
977         struct mdt_thread_info          *cdt_mti;
978         ENTRY;
979
980         if (cdt->cdt_state == CDT_STOPPED) {
981                 CERROR("%s: Coordinator already stopped\n",
982                        mdt_obd_name(mdt));
983                 RETURN(-EALREADY);
984         }
985
986         /* remove proc entries */
987         if (cdt->cdt_proc_dir != NULL)
988                 lprocfs_remove(&cdt->cdt_proc_dir);
989
990         if (cdt->cdt_state != CDT_STOPPING) {
991                 /* stop coordinator thread before cleaning */
992                 cdt->cdt_thread.t_flags = SVC_STOPPING;
993                 wake_up(&cdt->cdt_thread.t_ctl_waitq);
994                 wait_event(cdt->cdt_thread.t_ctl_waitq,
995                            cdt->cdt_thread.t_flags & SVC_STOPPED);
996         }
997         cdt->cdt_state = CDT_STOPPED;
998
999         /* start cleaning */
1000         down_write(&cdt->cdt_request_lock);
1001         list_for_each_entry_safe(car, tmp1, &cdt->cdt_requests,
1002                                  car_request_list) {
1003                 list_del(&car->car_request_list);
1004                 mdt_cdt_free_request(car);
1005         }
1006         up_write(&cdt->cdt_request_lock);
1007
1008         down_write(&cdt->cdt_agent_lock);
1009         list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
1010                 list_del(&ha->ha_list);
1011                 OBD_FREE_PTR(ha);
1012         }
1013         up_write(&cdt->cdt_agent_lock);
1014
1015         cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1016         mutex_lock(&cdt->cdt_restore_lock);
1017         list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_hdl, crh_list) {
1018                 struct mdt_object       *child;
1019
1020                 /* give back layout lock */
1021                 child = mdt_object_find(&cdt->cdt_env, mdt, &crh->crh_fid);
1022                 if (!IS_ERR(child))
1023                         mdt_object_unlock_put(cdt_mti, child, &crh->crh_lh, 1);
1024
1025                 list_del(&crh->crh_list);
1026
1027                 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1028         }
1029         mutex_unlock(&cdt->cdt_restore_lock);
1030
1031         mdt->mdt_opts.mo_coordinator = 0;
1032
1033         RETURN(0);
1034 }
1035
1036 /**
1037  * register all requests from an hal in the memory list
1038  * \param mti [IN] context
1039  * \param hal [IN] request
1040  * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1041  *  which is running the CT
1042  * \retval 0 success
1043  * \retval -ve failure
1044  */
1045 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1046                     struct hsm_action_list *hal, struct obd_uuid *uuid)
1047 {
1048         struct mdt_device       *mdt = mti->mti_mdt;
1049         struct coordinator      *cdt = &mdt->mdt_coordinator;
1050         struct hsm_action_item  *hai;
1051         int                      rc = 0, i;
1052         ENTRY;
1053
1054         /* register request in memory list */
1055         hai = hai_first(hal);
1056         for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1057                 struct cdt_agent_req *car;
1058
1059                 /* in case of a cancel request, we first mark the ondisk
1060                  * record of the request we want to stop as canceled
1061                  * this does not change the cancel record
1062                  * it will be done when updating the request status
1063                  */
1064                 if (hai->hai_action == HSMA_CANCEL) {
1065                         rc = mdt_agent_record_update(mti->mti_env, mti->mti_mdt,
1066                                                      &hai->hai_cookie,
1067                                                      1, ARS_CANCELED);
1068                         if (rc) {
1069                                 CERROR("%s: mdt_agent_record_update() failed, "
1070                                        "rc=%d, cannot update status to %s "
1071                                        "for cookie "LPX64"\n",
1072                                        mdt_obd_name(mdt), rc,
1073                                        agent_req_status2name(ARS_CANCELED),
1074                                        hai->hai_cookie);
1075                                 GOTO(out, rc);
1076                         }
1077
1078                         /* find the running request to set it canceled */
1079                         car = mdt_cdt_find_request(cdt, hai->hai_cookie, NULL);
1080                         if (car != NULL) {
1081                                 car->car_canceled = 1;
1082                                 /* uuid has to be changed to the one running the
1083                                 * request to cancel */
1084                                 *uuid = car->car_uuid;
1085                                 mdt_cdt_put_request(car);
1086                         }
1087                         /* no need to memorize cancel request
1088                          * this also avoid a deadlock when we receive
1089                          * a purge all requests command
1090                          */
1091                         continue;
1092                 }
1093
1094                 if (hai->hai_action == HSMA_ARCHIVE) {
1095                         struct mdt_object *obj;
1096                         struct md_hsm hsm;
1097
1098                         obj = mdt_hsm_get_md_hsm(mti, &hai->hai_fid, &hsm);
1099                         if (IS_ERR(obj) && (PTR_ERR(obj) == -ENOENT))
1100                                 continue;
1101                         if (IS_ERR(obj))
1102                                 GOTO(out, rc = PTR_ERR(obj));
1103
1104                         hsm.mh_flags |= HS_EXISTS;
1105                         hsm.mh_arch_id = hal->hal_archive_id;
1106                         rc = mdt_hsm_attr_set(mti, obj, &hsm);
1107                         mdt_object_put(mti->mti_env, obj);
1108                         if (rc)
1109                                 GOTO(out, rc);
1110                 }
1111
1112                 car = mdt_cdt_alloc_request(hal->hal_compound_id,
1113                                             hal->hal_archive_id, hal->hal_flags,
1114                                             uuid, hai);
1115                 if (IS_ERR(car))
1116                         GOTO(out, rc = PTR_ERR(car));
1117
1118                 rc = mdt_cdt_add_request(cdt, car);
1119                 if (rc != 0)
1120                         mdt_cdt_free_request(car);
1121         }
1122 out:
1123         RETURN(rc);
1124 }
1125
1126 /**
1127  * swap layouts between 2 fids
1128  * \param mti [IN] context
1129  * \param fid1 [IN]
1130  * \param fid2 [IN]
1131  */
1132 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1133                             const lustre_fid *fid, const lustre_fid *dfid)
1134 {
1135         struct mdt_device       *mdt = mti->mti_mdt;
1136         struct mdt_object       *child1, *child2;
1137         struct mdt_lock_handle  *lh2;
1138         int                      rc;
1139         ENTRY;
1140
1141         child1 = mdt_object_find(mti->mti_env, mdt, fid);
1142         if (IS_ERR(child1))
1143                 GOTO(out, rc = PTR_ERR(child1));
1144
1145         /* we already have layout lock on FID so take only
1146          * on dfid */
1147         lh2 = &mti->mti_lh[MDT_LH_OLD];
1148         mdt_lock_reg_init(lh2, LCK_EX);
1149         child2 = mdt_object_find_lock(mti, dfid, lh2, MDS_INODELOCK_LAYOUT);
1150         if (IS_ERR(child2))
1151                 GOTO(out_child1, rc = PTR_ERR(child2));
1152
1153         /* if copy tool closes the volatile before sending the final
1154          * progress through llapi_hsm_copy_end(), all the objects
1155          * are removed and mdd_swap_layout LBUG */
1156         if (mdt_object_exists(child2)) {
1157                 rc = mo_swap_layouts(mti->mti_env, mdt_object_child(child1),
1158                                      mdt_object_child(child2), 0);
1159         } else {
1160                 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1161                        mdt_obd_name(mti->mti_mdt), PFID(dfid));
1162                 rc = -ENOENT;
1163         }
1164
1165         mdt_object_unlock_put(mti, child2, lh2, 1);
1166 out_child1:
1167         mdt_object_put(mti->mti_env, child1);
1168 out:
1169         RETURN(rc);
1170 }
1171
1172 /**
1173  * update status of a completed request
1174  * \param mti [IN] context
1175  * \param pgs [IN] progress of the copy tool
1176  * \param update_record [IN] update llog record
1177  * \retval 0 success
1178  * \retval -ve failure
1179  */
1180 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1181                                      struct hsm_progress_kernel *pgs,
1182                                      const struct cdt_agent_req *car,
1183                                      enum agent_req_status *status)
1184 {
1185         const struct lu_env     *env = mti->mti_env;
1186         struct mdt_device       *mdt = mti->mti_mdt;
1187         struct coordinator      *cdt = &mdt->mdt_coordinator;
1188         struct mdt_object       *obj = NULL;
1189         int                      cl_flags = 0, rc = 0;
1190         struct md_hsm            mh;
1191         bool                     is_mh_changed;
1192         ENTRY;
1193
1194         /* default is to retry */
1195         *status = ARS_WAITING;
1196
1197         /* find object by FID */
1198         obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1199         /* we will update MD HSM only if needed */
1200         is_mh_changed = false;
1201         if (IS_ERR(obj)) {
1202                 /* object removed */
1203                 *status = ARS_SUCCEED;
1204                 goto unlock;
1205         }
1206
1207         /* no need to change mh->mh_arch_id
1208          * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1209          */
1210         if (pgs->hpk_errval != 0) {
1211                 switch (pgs->hpk_errval) {
1212                 case ENOSYS:
1213                         /* the copy tool does not support cancel
1214                          * so the cancel request is failed
1215                          * As we cannot distinguish a cancel progress
1216                          * from another action progress (they have the
1217                          * same cookie), we suppose here the CT returns
1218                          * ENOSYS only if does not support cancel
1219                          */
1220                         /* this can also happen when cdt calls it to
1221                          * for a timeouted request */
1222                         *status = ARS_FAILED;
1223                         /* to have a cancel event in changelog */
1224                         pgs->hpk_errval = ECANCELED;
1225                         break;
1226                 case ECANCELED:
1227                         /* the request record has already been set to
1228                          * ARS_CANCELED, this set the cancel request
1229                          * to ARS_SUCCEED */
1230                         *status = ARS_SUCCEED;
1231                         break;
1232                 default:
1233                         *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1234                                    !(pgs->hpk_flags & HP_FLAG_RETRY) ?
1235                                    ARS_FAILED : ARS_WAITING);
1236                         break;
1237                 }
1238
1239                 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1240                         CERROR("%s: Request "LPX64" on "DFID
1241                                " failed, error code %d too large\n",
1242                                mdt_obd_name(mdt),
1243                                pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1244                                pgs->hpk_errval);
1245                         hsm_set_cl_error(&cl_flags,
1246                                          CLF_HSM_ERROVERFLOW);
1247                         rc = -EINVAL;
1248                 } else {
1249                         hsm_set_cl_error(&cl_flags, pgs->hpk_errval);
1250                 }
1251
1252                 switch (car->car_hai->hai_action) {
1253                 case HSMA_ARCHIVE:
1254                         hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1255                         break;
1256                 case HSMA_RESTORE:
1257                         hsm_set_cl_event(&cl_flags, HE_RESTORE);
1258                         break;
1259                 case HSMA_REMOVE:
1260                         hsm_set_cl_event(&cl_flags, HE_REMOVE);
1261                         break;
1262                 case HSMA_CANCEL:
1263                         hsm_set_cl_event(&cl_flags, HE_CANCEL);
1264                         CERROR("%s: Failed request "LPX64" on "DFID
1265                                " cannot be a CANCEL\n",
1266                                mdt_obd_name(mdt),
1267                                pgs->hpk_cookie,
1268                                PFID(&pgs->hpk_fid));
1269                         break;
1270                 default:
1271                         CERROR("%s: Failed request "LPX64" on "DFID
1272                                " %d is an unknown action\n",
1273                                mdt_obd_name(mdt),
1274                                pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1275                                car->car_hai->hai_action);
1276                         rc = -EINVAL;
1277                         break;
1278                 }
1279         } else {
1280                 *status = ARS_SUCCEED;
1281                 switch (car->car_hai->hai_action) {
1282                 case HSMA_ARCHIVE:
1283                         hsm_set_cl_event(&cl_flags, HE_ARCHIVE);
1284                         /* set ARCHIVE keep EXIST and clear LOST and
1285                          * DIRTY */
1286                         mh.mh_arch_ver = pgs->hpk_data_version;
1287                         mh.mh_flags |= HS_ARCHIVED;
1288                         mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1289                         is_mh_changed = true;
1290                         break;
1291                 case HSMA_RESTORE:
1292                         hsm_set_cl_event(&cl_flags, HE_RESTORE);
1293
1294                         /* clear RELEASED and DIRTY */
1295                         mh.mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1296                         /* Restoring has changed the file version on
1297                          * disk. */
1298                         mh.mh_arch_ver = pgs->hpk_data_version;
1299                         is_mh_changed = true;
1300                         break;
1301                 case HSMA_REMOVE:
1302                         hsm_set_cl_event(&cl_flags, HE_REMOVE);
1303                         /* clear ARCHIVED EXISTS and LOST */
1304                         mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1305                         is_mh_changed = true;
1306                         break;
1307                 case HSMA_CANCEL:
1308                         hsm_set_cl_event(&cl_flags, HE_CANCEL);
1309                         CERROR("%s: Successful request "LPX64
1310                                " on "DFID
1311                                " cannot be a CANCEL\n",
1312                                mdt_obd_name(mdt),
1313                                pgs->hpk_cookie,
1314                                PFID(&pgs->hpk_fid));
1315                         break;
1316                 default:
1317                         CERROR("%s: Successful request "LPX64
1318                                " on "DFID
1319                                " %d is an unknown action\n",
1320                                mdt_obd_name(mdt),
1321                                pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1322                                car->car_hai->hai_action);
1323                         rc = -EINVAL;
1324                         break;
1325                 }
1326         }
1327
1328         /* rc != 0 means error when analysing action, it may come from
1329          * a crasy CT no need to manage DIRTY
1330          */
1331         if (rc == 0)
1332                 hsm_set_cl_flags(&cl_flags,
1333                                  mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1334
1335         /* unlock is done later, after layout lock management */
1336         if (is_mh_changed)
1337                 rc = mdt_hsm_attr_set(mti, obj, &mh);
1338
1339 unlock:
1340         /* we give back layout lock only if restore was successful or
1341          * if restore was canceled or if policy is to not retry
1342          * in other cases we just unlock the object */
1343         if (car->car_hai->hai_action == HSMA_RESTORE &&
1344             (pgs->hpk_errval == 0 || pgs->hpk_errval == ECANCELED ||
1345              cdt->cdt_policy & CDT_NORETRY_ACTION)) {
1346                 struct cdt_restore_handle       *crh;
1347
1348                 /* restore in data FID done, we swap the layouts
1349                  * only if restore is successfull */
1350                 if (pgs->hpk_errval == 0) {
1351                         rc = hsm_swap_layouts(mti, &car->car_hai->hai_fid,
1352                                               &car->car_hai->hai_dfid);
1353                         if (rc) {
1354                                 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1355                                         *status = ARS_FAILED;
1356                                 pgs->hpk_errval = -rc;
1357                         }
1358                 }
1359                 /* we have to retry, so keep layout lock */
1360                 if (*status == ARS_WAITING)
1361                         GOTO(out, rc);
1362
1363                 /* give back layout lock */
1364                 mutex_lock(&cdt->cdt_restore_lock);
1365                 crh = hsm_restore_hdl_find(cdt, &car->car_hai->hai_fid);
1366                 if (crh != NULL)
1367                         list_del(&crh->crh_list);
1368                 mutex_unlock(&cdt->cdt_restore_lock);
1369                 /* just give back layout lock, we keep
1370                  * the reference which is given back
1371                  * later with the lock for HSM flags */
1372                 if (!IS_ERR(obj) && crh != NULL)
1373                         mdt_object_unlock(mti, obj, &crh->crh_lh, 1);
1374
1375                 if (crh != NULL)
1376                         OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
1377         }
1378
1379         GOTO(out, rc);
1380
1381 out:
1382         if (obj != NULL && !IS_ERR(obj)) {
1383                 mo_changelog(env, CL_HSM, cl_flags,
1384                              mdt_object_child(obj));
1385                 mdt_object_put(mti->mti_env, obj);
1386         }
1387
1388         RETURN(rc);
1389 }
1390
1391 /**
1392  * update status of a request
1393  * \param mti [IN] context
1394  * \param pgs [IN] progress of the copy tool
1395  * \param update_record [IN] update llog record
1396  * \retval 0 success
1397  * \retval -ve failure
1398  */
1399 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1400                                  struct hsm_progress_kernel *pgs,
1401                                  const int update_record)
1402 {
1403         struct mdt_device       *mdt = mti->mti_mdt;
1404         struct coordinator      *cdt = &mdt->mdt_coordinator;
1405         struct cdt_agent_req    *car;
1406         int                      rc = 0;
1407         ENTRY;
1408
1409         /* no coordinator started, so we cannot serve requests */
1410         if (cdt->cdt_state == CDT_STOPPED)
1411                 RETURN(-EAGAIN);
1412
1413         /* first do sanity checks */
1414         car = mdt_cdt_update_request(cdt, pgs);
1415         if (IS_ERR(car)) {
1416                 CERROR("%s: Cannot find running request for cookie "LPX64
1417                        " on fid="DFID"\n",
1418                        mdt_obd_name(mdt),
1419                        pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1420                 if (car == NULL)
1421                         RETURN(-ENOENT);
1422                 RETURN(PTR_ERR(car));
1423         }
1424
1425         CDEBUG(D_HSM, "Progress received for fid="DFID" cookie="LPX64
1426                       " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1427                       PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1428                       hsm_copytool_action2name(car->car_hai->hai_action),
1429                       pgs->hpk_flags, pgs->hpk_errval,
1430                       PFID(&car->car_hai->hai_fid),
1431                       PFID(&car->car_hai->hai_dfid));
1432
1433         /* progress is done on FID or data FID depending of the action and
1434          * of the copy progress */
1435         /* for restore progress is used to send back the data FID to cdt */
1436         if (car->car_hai->hai_action == HSMA_RESTORE &&
1437             lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1438                 car->car_hai->hai_dfid = pgs->hpk_fid;
1439
1440         if ((car->car_hai->hai_action == HSMA_RESTORE ||
1441              car->car_hai->hai_action == HSMA_ARCHIVE) &&
1442             (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1443              !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1444                 CERROR("%s: Progress on "DFID" for cookie "LPX64
1445                        " does not match request FID "DFID" nor data FID "
1446                        DFID"\n",
1447                        mdt_obd_name(mdt),
1448                        PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1449                        PFID(&car->car_hai->hai_fid),
1450                        PFID(&car->car_hai->hai_dfid));
1451                 GOTO(out, rc = -EINVAL);
1452         }
1453
1454         if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1455                 CERROR("%s: Progress on "DFID" for cookie "LPX64" action=%s"
1456                        " is not coherent (err=%d and not completed"
1457                        " (flags=%d))\n",
1458                        mdt_obd_name(mdt),
1459                        PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1460                        hsm_copytool_action2name(car->car_hai->hai_action),
1461                        pgs->hpk_errval, pgs->hpk_flags);
1462                 GOTO(out, rc = -EINVAL);
1463         }
1464
1465         /* now progress is valid */
1466
1467         /* we use a root like ucred */
1468         hsm_init_ucred(mdt_ucred(mti));
1469
1470         if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1471                 enum agent_req_status    status;
1472
1473                 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1474
1475                 /* remove request from memory list */
1476                 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1477
1478                 CDEBUG(D_HSM, "Updating record: fid="DFID" cookie="LPX64
1479                               " action=%s status=%s\n", PFID(&pgs->hpk_fid),
1480                        pgs->hpk_cookie,
1481                        hsm_copytool_action2name(car->car_hai->hai_action),
1482                        agent_req_status2name(status));
1483
1484                 if (update_record) {
1485                         int rc1;
1486
1487                         rc1 = mdt_agent_record_update(mti->mti_env, mdt,
1488                                                      &pgs->hpk_cookie, 1,
1489                                                      status);
1490                         if (rc1)
1491                                 CERROR("%s: mdt_agent_record_update() failed,"
1492                                        " rc=%d, cannot update status to %s"
1493                                        " for cookie "LPX64"\n",
1494                                        mdt_obd_name(mdt), rc1,
1495                                        agent_req_status2name(status),
1496                                        pgs->hpk_cookie);
1497                         rc = (rc != 0 ? rc : rc1);
1498                 }
1499                 /* ct has completed a request, so a slot is available, wakeup
1500                  * cdt to find new work */
1501                 mdt_hsm_cdt_wakeup(mdt);
1502         } else {
1503                 /* if copytool send a progress on a canceled request
1504                  * we inform copytool it should stop
1505                  */
1506                 if (car->car_canceled == 1)
1507                         rc = -ECANCELED;
1508         }
1509         GOTO(out, rc);
1510
1511 out:
1512         /* remove ref got from mdt_cdt_update_request() */
1513         mdt_cdt_put_request(car);
1514
1515         return rc;
1516 }
1517
1518
1519 /**
1520  * data passed to llog_cat_process() callback
1521  * to cancel requests
1522  */
1523 struct hsm_cancel_all_data {
1524         struct mdt_device       *mdt;
1525 };
1526
1527 /**
1528  *  llog_cat_process() callback, used to:
1529  *  - purge all requests
1530  * \param env [IN] environment
1531  * \param llh [IN] llog handle
1532  * \param hdr [IN] llog record
1533  * \param data [IN] cb data = struct hsm_cancel_all_data
1534  * \retval 0 success
1535  * \retval -ve failure
1536  */
1537 static int mdt_cancel_all_cb(const struct lu_env *env,
1538                              struct llog_handle *llh,
1539                              struct llog_rec_hdr *hdr, void *data)
1540 {
1541         struct llog_agent_req_rec       *larr;
1542         struct hsm_cancel_all_data      *hcad;
1543         int                              rc = 0;
1544         ENTRY;
1545
1546         larr = (struct llog_agent_req_rec *)hdr;
1547         hcad = data;
1548         if (larr->arr_status == ARS_WAITING ||
1549             larr->arr_status == ARS_STARTED) {
1550                 larr->arr_status = ARS_CANCELED;
1551                 larr->arr_req_change = cfs_time_current_sec();
1552                 rc = mdt_agent_llog_update_rec(env, hcad->mdt, llh, larr);
1553                 if (rc == 0)
1554                         RETURN(LLOG_DEL_RECORD);
1555         }
1556         RETURN(rc);
1557 }
1558
1559 /**
1560  * cancel all actions
1561  * \param obd [IN] MDT device
1562  */
1563 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1564 {
1565         struct mdt_thread_info          *mti;
1566         struct coordinator              *cdt = &mdt->mdt_coordinator;
1567         struct cdt_agent_req            *car;
1568         struct hsm_action_list          *hal = NULL;
1569         struct hsm_action_item          *hai;
1570         struct hsm_cancel_all_data       hcad;
1571         int                              hal_sz = 0, hal_len, rc;
1572         enum cdt_states                  save_state;
1573         ENTRY;
1574
1575         /* retrieve coordinator context */
1576         mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1577
1578         /* disable coordinator */
1579         save_state = cdt->cdt_state;
1580         cdt->cdt_state = CDT_DISABLE;
1581
1582         /* send cancel to all running requests */
1583         down_read(&cdt->cdt_request_lock);
1584         list_for_each_entry(car, &cdt->cdt_requests, car_request_list) {
1585                 mdt_cdt_get_request(car);
1586                 /* request is not yet removed from list, it will be done
1587                  * when copytool will return progress
1588                  */
1589
1590                 if (car->car_hai->hai_action == HSMA_CANCEL) {
1591                         mdt_cdt_put_request(car);
1592                         continue;
1593                 }
1594
1595                 /* needed size */
1596                 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1597                           cfs_size_round(car->car_hai->hai_len);
1598
1599                 if (hal_len > hal_sz && hal_sz > 0) {
1600                         /* not enough room, free old buffer */
1601                         OBD_FREE(hal, hal_sz);
1602                         hal = NULL;
1603                 }
1604
1605                 /* empty buffer, allocate one */
1606                 if (hal == NULL) {
1607                         hal_sz = hal_len;
1608                         OBD_ALLOC(hal, hal_sz);
1609                         if (hal == NULL) {
1610                                 mdt_cdt_put_request(car);
1611                                 up_read(&cdt->cdt_request_lock);
1612                                 GOTO(out, rc = -ENOMEM);
1613                         }
1614                 }
1615
1616                 hal->hal_version = HAL_VERSION;
1617                 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1618                                 MTI_NAME_MAXLEN);
1619                 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1620                 hal->hal_compound_id = car->car_compound_id;
1621                 hal->hal_archive_id = car->car_archive_id;
1622                 hal->hal_flags = car->car_flags;
1623                 hal->hal_count = 0;
1624
1625                 hai = hai_first(hal);
1626                 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1627                 hai->hai_action = HSMA_CANCEL;
1628                 hal->hal_count = 1;
1629
1630                 /* it is possible to safely call mdt_hsm_agent_send()
1631                  * (ie without a deadlock on cdt_request_lock), because the
1632                  * write lock is taken only if we are not in purge mode
1633                  * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1634                  *   nor mdt_cdt_remove_request())
1635                  */
1636                 /* no conflict with cdt thread because cdt is disable and we
1637                  * have the request lock */
1638                 mdt_hsm_agent_send(mti, hal, 1);
1639
1640                 mdt_cdt_put_request(car);
1641         }
1642         up_read(&cdt->cdt_request_lock);
1643
1644         if (hal != NULL)
1645                 OBD_FREE(hal, hal_sz);
1646
1647         /* cancel all on-disk records */
1648         hcad.mdt = mdt;
1649
1650         rc = cdt_llog_process(mti->mti_env, mti->mti_mdt,
1651                               mdt_cancel_all_cb, &hcad);
1652 out:
1653         /* enable coordinator */
1654         cdt->cdt_state = save_state;
1655
1656         RETURN(rc);
1657 }
1658
1659 /**
1660  * check if a request is comptaible with file status
1661  * \param hai [IN] request description
1662  * \param hal_an [IN] request archive number (not used)
1663  * \param rq_flags [IN] request flags
1664  * \param hsm [IN] file HSM metadata
1665  * \retval boolean
1666  */
1667 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1668                               const int hal_an, const __u64 rq_flags,
1669                               const struct md_hsm *hsm)
1670 {
1671         int      is_compat = false;
1672         int      hsm_flags;
1673         ENTRY;
1674
1675         hsm_flags = hsm->mh_flags;
1676         switch (hai->hai_action) {
1677         case HSMA_ARCHIVE:
1678                 if (!(hsm_flags & HS_NOARCHIVE) &&
1679                     (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1680                         is_compat = true;
1681                 break;
1682         case HSMA_RESTORE:
1683                 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1684                     hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1685                         is_compat = true;
1686                 break;
1687         case HSMA_REMOVE:
1688                 if (!(hsm_flags & HS_RELEASED) &&
1689                     (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1690                         is_compat = true;
1691                 break;
1692         case HSMA_CANCEL:
1693                 is_compat = true;
1694                 break;
1695         }
1696         CDEBUG(D_HSM, "fid="DFID" action=%s flags="LPX64
1697                       " extent="LPX64"-"LPX64" hsm_flags=%.8X %s\n",
1698                       PFID(&hai->hai_fid),
1699                       hsm_copytool_action2name(hai->hai_action), rq_flags,
1700                       hai->hai_extent.offset, hai->hai_extent.length,
1701                       hsm->mh_flags,
1702                       (is_compat ? "compatible" : "uncompatible"));
1703
1704         RETURN(is_compat);
1705 }
1706
1707 /*
1708  * /proc interface used to get/set HSM behaviour (cdt->cdt_policy)
1709  */
1710 static const struct {
1711         __u64            bit;
1712         char            *name;
1713         char            *nickname;
1714 } hsm_policy_names[] = {
1715         { CDT_NONBLOCKING_RESTORE,      "non_blocking_restore", "nbr"},
1716         { CDT_NORETRY_ACTION,           "no_retry_action",      "nra"},
1717         { 0 },
1718 };
1719
1720 /**
1721  * convert a policy name to a bit
1722  * \param name [IN] policy name
1723  * \retval 0 unknown
1724  * \retval   policy bit
1725  */
1726 static __u64 hsm_policy_str2bit(const char *name)
1727 {
1728         int      i;
1729
1730         for (i = 0; hsm_policy_names[i].bit != 0; i++)
1731                 if (strcmp(hsm_policy_names[i].nickname, name) == 0)
1732                         return hsm_policy_names[i].bit;
1733         return 0;
1734 }
1735
1736 /**
1737  * convert a policy bit field to a string
1738  * \param mask [IN] policy bit field
1739  * \param buffer [OUT] string
1740  * \param count [IN] size of buffer
1741  * \retval size filled in buffer
1742  */
1743 static int hsm_policy_bit2str(const __u64 mask, char *buffer, int count)
1744 {
1745         int      i, j, sz;
1746         char    *ptr;
1747         __u64    bit;
1748         ENTRY;
1749
1750         ptr = buffer;
1751         sz = snprintf(buffer, count, "("LPX64") ", mask);
1752         ptr += sz;
1753         count -= sz;
1754         for (i = 0; i < (sizeof(mask) * 8); i++) {
1755                 bit = (1ULL << i);
1756                 if (!(bit  & mask))
1757                         continue;
1758
1759                 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
1760                         if (hsm_policy_names[j].bit == bit) {
1761                                 sz = snprintf(ptr, count, "%s(%s) ",
1762                                               hsm_policy_names[j].name,
1763                                               hsm_policy_names[j].nickname);
1764                                 ptr += sz;
1765                                 count -= sz;
1766                                 break;
1767                         }
1768                 }
1769         }
1770         RETURN(ptr - buffer);
1771 }
1772
1773 /* methods to read/write HSM policy flags */
1774 static int lprocfs_rd_hsm_policy(char *page, char **start, off_t off,
1775                                  int count, int *eof, void *data)
1776 {
1777         struct mdt_device       *mdt = data;
1778         struct coordinator      *cdt = &mdt->mdt_coordinator;
1779         int                      sz;
1780         ENTRY;
1781
1782         sz = hsm_policy_bit2str(cdt->cdt_policy, page, count);
1783         page[sz] = '\n';
1784         sz++;
1785         page[sz] = '\0';
1786         *eof = 1;
1787         RETURN(sz);
1788 }
1789
1790 static int lprocfs_wr_hsm_policy(struct file *file, const char *buffer,
1791                                  unsigned long count, void *data)
1792 {
1793         struct mdt_device       *mdt = data;
1794         struct coordinator      *cdt = &mdt->mdt_coordinator;
1795         int                      sz;
1796         char                    *start, *end;
1797         __u64                    policy;
1798         int                      set;
1799         char                    *buf;
1800         ENTRY;
1801
1802         if (strncmp(buffer, "help", 4) == 0) {
1803                 sz = PAGE_SIZE;
1804                 OBD_ALLOC(buf, sz);
1805                 if (!buf)
1806                         RETURN(-ENOMEM);
1807
1808                 hsm_policy_bit2str(CDT_POLICY_MASK, buf, sz);
1809                 CWARN("Supported policies are: %s\n", buf);
1810                 OBD_FREE(buf, sz);
1811                 RETURN(count);
1812         }
1813
1814         OBD_ALLOC(buf, count + 1);
1815         if (buf == NULL)
1816                 RETURN(-ENOMEM);
1817
1818         if (copy_from_user(buf, buffer, count))
1819                 RETURN(-EFAULT);
1820
1821         buf[count] = '\0';
1822         start = buf;
1823
1824         policy = 0;
1825         do {
1826                 end = strchr(start, ' ');
1827                 if (end != NULL)
1828                         *end = '\0';
1829                 switch (*start) {
1830                 case '-':
1831                         start++;
1832                         set = 0;
1833                         break;
1834                 case '+':
1835                         start++;
1836                         set = 1;
1837                         break;
1838                 default:
1839                         set = 2;
1840                         break;
1841                 }
1842                 policy = hsm_policy_str2bit(start);
1843                 if (!policy)
1844                         break;
1845
1846                 switch (set) {
1847                 case 0:
1848                         cdt->cdt_policy &= ~policy;
1849                         break;
1850                 case 1:
1851                         cdt->cdt_policy |= policy;
1852                         break;
1853                 case 2:
1854                         cdt->cdt_policy = policy;
1855                         break;
1856                 }
1857
1858                 start = end + 1;
1859         } while (end != NULL);
1860         OBD_FREE(buf, count + 1);
1861         RETURN(count);
1862 }
1863
1864 #define GENERATE_PROC_METHOD(VAR)                                       \
1865 static int lprocfs_rd_hsm_##VAR(char *page, char **start, off_t off,    \
1866                                 int count, int *eof, void *data)        \
1867 {                                                                       \
1868         struct mdt_device       *mdt = data;                            \
1869         struct coordinator      *cdt = &mdt->mdt_coordinator;           \
1870         int                      sz;                                    \
1871         ENTRY;                                                          \
1872                                                                         \
1873         sz = snprintf(page, count, LPU64"\n", (__u64)cdt->VAR);         \
1874         *eof = 1;                                                       \
1875         RETURN(sz);                                                     \
1876 }                                                                       \
1877 static int lprocfs_wr_hsm_##VAR(struct file *file, const char *buffer,  \
1878                                 unsigned long count, void *data)        \
1879                                                                         \
1880 {                                                                       \
1881         struct mdt_device       *mdt = data;                            \
1882         struct coordinator      *cdt = &mdt->mdt_coordinator;           \
1883         int                      val;                                   \
1884         int                      rc;                                    \
1885         ENTRY;                                                          \
1886                                                                         \
1887         rc = lprocfs_write_helper(buffer, count, &val);                 \
1888         if (rc)                                                         \
1889                 RETURN(rc);                                             \
1890         if (val > 0) {                                                  \
1891                 cdt->VAR = val;                                         \
1892                 RETURN(count);                                          \
1893         }                                                               \
1894         RETURN(-EINVAL);                                                \
1895 }
1896
1897 GENERATE_PROC_METHOD(cdt_loop_period)
1898 GENERATE_PROC_METHOD(cdt_delay)
1899 GENERATE_PROC_METHOD(cdt_timeout)
1900 GENERATE_PROC_METHOD(cdt_max_request)
1901 GENERATE_PROC_METHOD(cdt_archive_id)
1902
1903 /*
1904  * procfs write method for MDT/hsm_control
1905  * proc entry is in mdt directory so data is mdt obd_device pointer
1906  */
1907 #define CDT_ENABLE_CMD   "enabled"
1908 #define CDT_STOP_CMD     "shutdown"
1909 #define CDT_DISABLE_CMD  "disabled"
1910 #define CDT_PURGE_CMD    "purge"
1911 #define CDT_HELP_CMD     "help"
1912
1913 int lprocfs_wr_hsm_cdt_control(struct file *file, const char *buffer,
1914                                unsigned long count, void *data)
1915 {
1916         struct obd_device       *obd = data;
1917         struct mdt_device       *mdt = mdt_dev(obd->obd_lu_dev);
1918         struct coordinator      *cdt = &(mdt->mdt_coordinator);
1919         int                      rc, usage = 0;
1920         ENTRY;
1921
1922         rc = 0;
1923         if (strncmp(buffer, CDT_ENABLE_CMD, strlen(CDT_ENABLE_CMD)) == 0) {
1924                 if (cdt->cdt_state == CDT_DISABLE) {
1925                         cdt->cdt_state = CDT_RUNNING;
1926                         mdt_hsm_cdt_wakeup(mdt);
1927                 } else {
1928                         rc = mdt_hsm_cdt_start(mdt);
1929                 }
1930         } else if (strncmp(buffer, CDT_STOP_CMD, strlen(CDT_STOP_CMD)) == 0) {
1931                 cdt->cdt_state = CDT_STOPPING;
1932         } else if (strncmp(buffer, CDT_DISABLE_CMD,
1933                            strlen(CDT_DISABLE_CMD)) == 0) {
1934                 cdt->cdt_state = CDT_DISABLE;
1935         } else if (strncmp(buffer, CDT_PURGE_CMD, strlen(CDT_PURGE_CMD)) == 0) {
1936                 rc = hsm_cancel_all_actions(mdt);
1937         } else if (strncmp(buffer, CDT_HELP_CMD, strlen(CDT_HELP_CMD)) == 0) {
1938                 usage = 1;
1939         } else {
1940                 usage = 1;
1941                 rc = -EINVAL;
1942         }
1943
1944         if (usage == 1)
1945                 CERROR("%s: Valid coordinator control commands are: "
1946                        "%s %s %s %s %s\n", mdt_obd_name(mdt),
1947                        CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
1948                        CDT_PURGE_CMD, CDT_HELP_CMD);
1949
1950         if (rc)
1951                 RETURN(rc);
1952
1953         RETURN(count);
1954 }
1955
1956 int lprocfs_rd_hsm_cdt_control(char *page, char **start, off_t off,
1957                                int count, int *eof, void *data)
1958 {
1959         struct obd_device       *obd = data;
1960         struct coordinator      *cdt;
1961         int                      sz;
1962         ENTRY;
1963
1964         cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
1965         *eof = 1;
1966
1967         if (cdt->cdt_state == CDT_INIT)
1968                 sz = snprintf(page, count, "init\n");
1969         else if (cdt->cdt_state == CDT_RUNNING)
1970                 sz = snprintf(page, count, "enabled\n");
1971         else if (cdt->cdt_state == CDT_STOPPING)
1972                 sz = snprintf(page, count, "stopping\n");
1973         else if (cdt->cdt_state == CDT_STOPPED)
1974                 sz = snprintf(page, count, "stopped\n");
1975         else if (cdt->cdt_state == CDT_DISABLE)
1976                 sz = snprintf(page, count, "disabled\n");
1977         else
1978                 sz = snprintf(page, count, "unknown\n");
1979
1980         RETURN(sz);
1981 }
1982
1983 static struct lprocfs_vars lprocfs_mdt_hsm_vars[] = {
1984         { "agents",             NULL, NULL, NULL, &mdt_hsm_agent_fops, 0 },
1985         { "agent_actions",      NULL, NULL, NULL,
1986                                 &mdt_agent_actions_fops, 0444 },
1987         { "archive_id",         lprocfs_rd_hsm_cdt_archive_id,
1988                                 lprocfs_wr_hsm_cdt_archive_id,
1989                                 NULL, NULL, 0 },
1990         { "grace_delay",        lprocfs_rd_hsm_cdt_delay,
1991                                 lprocfs_wr_hsm_cdt_delay,
1992                                 NULL, NULL, 0 },
1993         { "loop_period",        lprocfs_rd_hsm_cdt_loop_period,
1994                                 lprocfs_wr_hsm_cdt_loop_period,
1995                                 NULL, NULL, 0 },
1996         { "max_requests",       lprocfs_rd_hsm_cdt_max_request,
1997                                 lprocfs_wr_hsm_cdt_max_request,
1998                                 NULL, NULL, 0 },
1999         { "policy",             lprocfs_rd_hsm_policy, lprocfs_wr_hsm_policy,
2000                                 NULL, NULL, 0 },
2001         { "request_timeout",    lprocfs_rd_hsm_cdt_timeout,
2002                                 lprocfs_wr_hsm_cdt_timeout,
2003                                 NULL, NULL, 0 },
2004         { "requests",           NULL, NULL, NULL, &mdt_hsm_request_fops, 0 },
2005         { 0 }
2006 };