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