Whamcloud - gitweb
LU-15132 hsm: Protect against parallel HSM restore requests
[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) 2011, 2012 Commissariat a l'energie atomique et aux energies
24  *                          alternatives
25  *
26  * Copyright (c) 2013, 2017, Intel Corporation.
27  * Use is subject to license terms.
28  */
29 /*
30  * lustre/mdt/mdt_coordinator.c
31  *
32  * Lustre HSM Coordinator
33  *
34  * Author: Jacques-Charles Lafoucriere <jacques-charles.lafoucriere@cea.fr>
35  * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
36  * Author: Thomas Leibovici <thomas.leibovici@cea.fr>
37  */
38
39 #define DEBUG_SUBSYSTEM S_MDS
40
41 #include <linux/kthread.h>
42 #include <obd_support.h>
43 #include <lustre_export.h>
44 #include <obd.h>
45 #include <lprocfs_status.h>
46 #include <lustre_log.h>
47 #include <lustre_kernelcomm.h>
48 #include "mdt_internal.h"
49
50 /**
51  * get obj and HSM attributes on a fid
52  * \param mti [IN] context
53  * \param fid [IN] object fid
54  * \param hsm [OUT] HSM meta data
55  * \retval obj or error (-ENOENT if not found)
56  */
57 struct mdt_object *mdt_hsm_get_md_hsm(struct mdt_thread_info *mti,
58                                       const struct lu_fid *fid,
59                                       struct md_hsm *hsm)
60 {
61         struct md_attr          *ma;
62         struct mdt_object       *obj;
63         int                      rc;
64         ENTRY;
65
66         ma = &mti->mti_attr;
67         ma->ma_need = MA_HSM;
68         ma->ma_valid = 0;
69
70         /* find object by FID */
71         obj = mdt_object_find(mti->mti_env, mti->mti_mdt, fid);
72         if (IS_ERR(obj))
73                 RETURN(obj);
74
75         if (!mdt_object_exists(obj)) {
76                 /* no more object */
77                 mdt_object_put(mti->mti_env, obj);
78                 RETURN(ERR_PTR(-ENOENT));
79         }
80
81         rc = mdt_attr_get_complex(mti, obj, ma);
82         if (rc) {
83                 mdt_object_put(mti->mti_env, obj);
84                 RETURN(ERR_PTR(rc));
85         }
86
87         if (ma->ma_valid & MA_HSM)
88                 *hsm = ma->ma_hsm;
89         else
90                 memset(hsm, 0, sizeof(*hsm));
91         ma->ma_valid = 0;
92         RETURN(obj);
93 }
94
95 void mdt_hsm_dump_hal(int level, const char *prefix,
96                       struct hsm_action_list *hal)
97 {
98         int                      i, sz;
99         struct hsm_action_item  *hai;
100         char                     buf[12];
101
102         CDEBUG(level, "%s: HAL header: version %X count %d"
103                       " archive_id %d flags %#llx\n",
104                prefix, hal->hal_version, hal->hal_count,
105                hal->hal_archive_id, hal->hal_flags);
106
107         hai = hai_first(hal);
108         for (i = 0; i < hal->hal_count; i++) {
109                 sz = hai->hai_len - sizeof(*hai);
110                 CDEBUG(level, "%s %d: fid="DFID" dfid="DFID
111                        " cookie=%#llx"
112                        " action=%s extent=%#llx-%#llx gid=%#llx"
113                        " datalen=%d data=[%s]\n",
114                        prefix, i,
115                        PFID(&hai->hai_fid), PFID(&hai->hai_dfid),
116                        hai->hai_cookie,
117                        hsm_copytool_action2name(hai->hai_action),
118                        hai->hai_extent.offset,
119                        hai->hai_extent.length,
120                        hai->hai_gid, sz,
121                        hai_dump_data_field(hai, buf, sizeof(buf)));
122                 hai = hai_next(hai);
123         }
124 }
125
126 /**
127  * data passed to llog_cat_process() callback
128  * to scan requests and take actions
129  */
130 struct hsm_scan_request {
131         int                      hal_sz;
132         int                      hal_used_sz;
133         struct hsm_action_list  *hal;
134 };
135
136 struct hsm_scan_data {
137         struct mdt_thread_info  *hsd_mti;
138         char                     hsd_fsname[MTI_NAME_MAXLEN + 1];
139         /* are we scanning the logs for housekeeping, or just looking
140          * for new work?
141          */
142         bool                     hsd_housekeeping;
143         bool                     hsd_one_restore;
144         u32                      hsd_start_cat_idx;
145         u32                      hsd_start_rec_idx;
146         int                      hsd_action_count;
147         int                      hsd_request_len; /* array alloc len */
148         int                      hsd_request_count; /* array used count */
149         struct hsm_scan_request *hsd_request;
150 };
151
152 static int mdt_cdt_waiting_cb(const struct lu_env *env,
153                               struct mdt_device *mdt,
154                               struct llog_handle *llh,
155                               struct llog_agent_req_rec *larr,
156                               struct hsm_scan_data *hsd)
157 {
158         struct coordinator *cdt = &mdt->mdt_coordinator;
159         struct hsm_scan_request *request;
160         struct hsm_action_item *hai;
161         size_t hai_size;
162         u32 archive_id;
163         bool wrapped;
164         int i;
165
166         /* Are agents full? */
167         if (atomic_read(&cdt->cdt_request_count) >= cdt->cdt_max_requests)
168                 RETURN(hsd->hsd_housekeeping ? 0 : LLOG_PROC_BREAK);
169
170         if (hsd->hsd_action_count + atomic_read(&cdt->cdt_request_count) >=
171             cdt->cdt_max_requests) {
172                 /* We cannot send any more request
173                  *
174                  *                     *** SPECIAL CASE ***
175                  *
176                  * Restore requests are too important not to schedule at least
177                  * one, everytime we can.
178                  */
179                 if (larr->arr_hai.hai_action != HSMA_RESTORE ||
180                     hsd->hsd_one_restore)
181                         RETURN(hsd->hsd_housekeeping ? 0 : LLOG_PROC_BREAK);
182         }
183
184         hai_size = cfs_size_round(larr->arr_hai.hai_len);
185         archive_id = larr->arr_archive_id;
186
187         /* Can we add this action to one of the existing HALs in hsd. */
188         request = NULL;
189         for (i = 0; i < hsd->hsd_request_count; i++) {
190                 if (hsd->hsd_request[i].hal->hal_archive_id == archive_id &&
191                     hsd->hsd_request[i].hal_used_sz + hai_size <=
192                     LDLM_MAXREQSIZE) {
193                         request = &hsd->hsd_request[i];
194                         break;
195                 }
196         }
197
198         /* Are we trying to force-schedule a request? */
199         if (hsd->hsd_action_count + atomic_read(&cdt->cdt_request_count) >=
200             cdt->cdt_max_requests) {
201                 /* Is there really no compatible hsm_scan_request? */
202                 if (!request) {
203                         for (i -= 1; i >= 0; i--) {
204                                 if (hsd->hsd_request[i].hal->hal_archive_id ==
205                                     archive_id) {
206                                         request = &hsd->hsd_request[i];
207                                         break;
208                                 }
209                         }
210                 }
211
212                 /* Make room for the hai */
213                 if (request) {
214                         /* Discard the last hai until there is enough space */
215                         do {
216                                 request->hal->hal_count--;
217
218                                 hai = hai_first(request->hal);
219                                 for (i = 0; i < request->hal->hal_count; i++)
220                                         hai = hai_next(hai);
221                                 request->hal_used_sz -=
222                                         cfs_size_round(hai->hai_len);
223                                 hsd->hsd_action_count--;
224                         } while (request->hal_used_sz + hai_size >
225                                  LDLM_MAXREQSIZE);
226                 } else if (hsd->hsd_housekeeping) {
227                         struct hsm_scan_request *tmp;
228
229                         /* Discard the (whole) last hal */
230                         hsd->hsd_request_count--;
231                         LASSERT(hsd->hsd_request_count >= 0);
232                         tmp = &hsd->hsd_request[hsd->hsd_request_count];
233                         hsd->hsd_action_count -= tmp->hal->hal_count;
234                         LASSERT(hsd->hsd_action_count >= 0);
235                         OBD_FREE(tmp->hal, tmp->hal_sz);
236                 } else {
237                         /* Bailing out, this code path is too hot */
238                         RETURN(LLOG_PROC_BREAK);
239
240                 }
241         }
242
243         if (!request) {
244                 struct hsm_action_list *hal;
245
246                 LASSERT(hsd->hsd_request_count < hsd->hsd_request_len);
247                 request = &hsd->hsd_request[hsd->hsd_request_count];
248
249                 /* allocates hai vector size just needs to be large
250                  * enough */
251                 request->hal_sz = sizeof(*request->hal) +
252                         cfs_size_round(MTI_NAME_MAXLEN + 1) + 2 * hai_size;
253                 OBD_ALLOC_LARGE(hal, request->hal_sz);
254                 if (!hal)
255                         RETURN(-ENOMEM);
256
257                 hal->hal_version = HAL_VERSION;
258                 strlcpy(hal->hal_fsname, hsd->hsd_fsname, MTI_NAME_MAXLEN + 1);
259                 hal->hal_archive_id = larr->arr_archive_id;
260                 hal->hal_flags = larr->arr_flags;
261                 hal->hal_count = 0;
262                 request->hal_used_sz = hal_size(hal);
263                 request->hal = hal;
264                 hsd->hsd_request_count++;
265         } else if (request->hal_sz < request->hal_used_sz + hai_size) {
266                 /* Not enough room, need an extension */
267                 void *hal_buffer;
268                 int sz;
269
270                 sz = min_t(int, 2 * request->hal_sz, LDLM_MAXREQSIZE);
271                 LASSERT(request->hal_used_sz + hai_size < sz);
272
273                 OBD_ALLOC_LARGE(hal_buffer, sz);
274                 if (!hal_buffer)
275                         RETURN(-ENOMEM);
276
277                 memcpy(hal_buffer, request->hal, request->hal_used_sz);
278                 OBD_FREE_LARGE(request->hal, request->hal_sz);
279                 request->hal = hal_buffer;
280                 request->hal_sz = sz;
281         }
282
283         hai = hai_first(request->hal);
284         for (i = 0; i < request->hal->hal_count; i++)
285                 hai = hai_next(hai);
286
287         memcpy(hai, &larr->arr_hai, larr->arr_hai.hai_len);
288
289         request->hal_used_sz += hai_size;
290         request->hal->hal_count++;
291
292         hsd->hsd_action_count++;
293
294         switch (hai->hai_action) {
295         case HSMA_CANCEL:
296                 break;
297         case HSMA_RESTORE:
298                 hsd->hsd_one_restore = true;
299                 fallthrough;
300         default:
301                 cdt_agent_record_hash_add(cdt, hai->hai_cookie,
302                                           llh->lgh_hdr->llh_cat_idx,
303                                           larr->arr_hdr.lrh_index);
304         }
305
306         wrapped = llh->lgh_hdr->llh_cat_idx >= llh->lgh_last_idx &&
307                   llh->lgh_hdr->llh_count > 1;
308         if ((!wrapped && llh->lgh_hdr->llh_cat_idx > hsd->hsd_start_cat_idx) ||
309             (wrapped && llh->lgh_hdr->llh_cat_idx < hsd->hsd_start_cat_idx) ||
310             (llh->lgh_hdr->llh_cat_idx == hsd->hsd_start_cat_idx &&
311              larr->arr_hdr.lrh_index > hsd->hsd_start_rec_idx)) {
312                 hsd->hsd_start_cat_idx = llh->lgh_hdr->llh_cat_idx;
313                 hsd->hsd_start_rec_idx = larr->arr_hdr.lrh_index;
314         }
315
316         RETURN(0);
317 }
318
319 static int mdt_cdt_started_cb(const struct lu_env *env,
320                               struct mdt_device *mdt,
321                               struct llog_handle *llh,
322                               struct llog_agent_req_rec *larr,
323                               struct hsm_scan_data *hsd)
324 {
325         struct coordinator *cdt = &mdt->mdt_coordinator;
326         struct hsm_action_item *hai = &larr->arr_hai;
327         struct cdt_agent_req *car;
328         time64_t now = ktime_get_real_seconds();
329         time64_t last;
330         enum changelog_rec_flags clf_flags;
331         int rc;
332
333         if (!hsd->hsd_housekeeping)
334                 RETURN(0);
335
336         /* we search for a running request
337          * error may happen if coordinator crashes or stopped
338          * with running request
339          */
340         car = mdt_cdt_find_request(cdt, hai->hai_cookie);
341         if (car == NULL) {
342                 last = larr->arr_req_change;
343         } else {
344                 last = car->car_req_update;
345         }
346
347         /* test if request too long, if yes cancel it
348          * the same way the copy tool acknowledge a cancel request */
349         if (now <= last + cdt->cdt_active_req_timeout)
350                 GOTO(out_car, rc = 0);
351
352         dump_llog_agent_req_rec("request timed out, start cleaning", larr);
353
354         if (car != NULL) {
355                 car->car_req_update = now;
356                 mdt_hsm_agent_update_statistics(cdt, 0, 1, 0, &car->car_uuid);
357                 /* Remove car from memory list (LU-9075) */
358                 mdt_cdt_remove_request(cdt, hai->hai_cookie);
359         }
360
361         /* Emit a changelog record for the failed action.*/
362         clf_flags = 0;
363         hsm_set_cl_error(&clf_flags, ECANCELED);
364
365         switch (hai->hai_action) {
366         case HSMA_ARCHIVE:
367                 hsm_set_cl_event(&clf_flags, HE_ARCHIVE);
368                 break;
369         case HSMA_RESTORE:
370                 hsm_set_cl_event(&clf_flags, HE_RESTORE);
371                 break;
372         case HSMA_REMOVE:
373                 hsm_set_cl_event(&clf_flags, HE_REMOVE);
374                 break;
375         case HSMA_CANCEL:
376                 hsm_set_cl_event(&clf_flags, HE_CANCEL);
377                 break;
378         default:
379                 /* Unknown record type, skip changelog. */
380                 clf_flags = 0;
381                 break;
382         }
383
384         if (clf_flags != 0)
385                 mo_changelog(env, CL_HSM, clf_flags, mdt->mdt_child,
386                              &hai->hai_fid);
387
388         if (hai->hai_action == HSMA_RESTORE)
389                 cdt_restore_handle_del(hsd->hsd_mti, cdt, &hai->hai_fid);
390
391         larr->arr_status = ARS_CANCELED;
392         larr->arr_req_change = now;
393         rc = llog_write(hsd->hsd_mti->mti_env, llh, &larr->arr_hdr,
394                         larr->arr_hdr.lrh_index);
395         if (rc < 0) {
396                 CERROR("%s: cannot update agent log: rc = %d\n",
397                        mdt_obd_name(mdt), rc);
398                 rc = LLOG_DEL_RECORD;
399         }
400
401         /* ct has completed a request, so a slot is available,
402          * signal the coordinator to find new work */
403         mdt_hsm_cdt_event(cdt);
404 out_car:
405         if (car != NULL)
406                 mdt_cdt_put_request(car);
407
408         RETURN(rc);
409 }
410
411 /**
412  *  llog_cat_process() callback, used to:
413  *  - find waiting request and start action
414  *  - purge canceled and done requests
415  * \param env [IN] environment
416  * \param llh [IN] llog handle
417  * \param hdr [IN] llog record
418  * \param data [IN/OUT] cb data = struct hsm_scan_data
419  * \retval 0 success
420  * \retval -ve failure
421  */
422 static int mdt_coordinator_cb(const struct lu_env *env,
423                               struct llog_handle *llh,
424                               struct llog_rec_hdr *hdr,
425                               void *data)
426 {
427         struct llog_agent_req_rec *larr = (struct llog_agent_req_rec *)hdr;
428         struct hsm_scan_data *hsd = data;
429         struct mdt_device *mdt = hsd->hsd_mti->mti_mdt;
430         struct coordinator *cdt = &mdt->mdt_coordinator;
431         ENTRY;
432
433         larr = (struct llog_agent_req_rec *)hdr;
434         dump_llog_agent_req_rec("mdt_coordinator_cb(): ", larr);
435         switch (larr->arr_status) {
436         case ARS_WAITING:
437                 RETURN(mdt_cdt_waiting_cb(env, mdt, llh, larr, hsd));
438         case ARS_STARTED:
439                 RETURN(mdt_cdt_started_cb(env, mdt, llh, larr, hsd));
440         default:
441                 if (!hsd->hsd_housekeeping)
442                         RETURN(0);
443
444                 if ((larr->arr_req_change + cdt->cdt_grace_delay) <
445                     ktime_get_real_seconds()) {
446                         cdt_agent_record_hash_del(cdt,
447                                                   larr->arr_hai.hai_cookie);
448                         RETURN(LLOG_DEL_RECORD);
449                 }
450
451                 RETURN(0);
452         }
453 }
454
455 /* Release the ressource used by the coordinator. Called when the
456  * coordinator is stopping. */
457 static void mdt_hsm_cdt_cleanup(struct mdt_device *mdt)
458 {
459         struct coordinator              *cdt = &mdt->mdt_coordinator;
460         struct cdt_agent_req            *car, *tmp1;
461         struct hsm_agent                *ha, *tmp2;
462         struct cdt_restore_handle       *crh, *tmp3;
463         struct mdt_thread_info          *cdt_mti;
464
465         /* start cleaning */
466         down_write(&cdt->cdt_request_lock);
467         list_for_each_entry_safe(car, tmp1, &cdt->cdt_request_list,
468                                  car_request_list) {
469                 cfs_hash_del(cdt->cdt_request_cookie_hash,
470                              &car->car_hai->hai_cookie,
471                              &car->car_cookie_hash);
472                 list_del(&car->car_request_list);
473                 mdt_cdt_put_request(car);
474         }
475         up_write(&cdt->cdt_request_lock);
476
477         down_write(&cdt->cdt_agent_lock);
478         list_for_each_entry_safe(ha, tmp2, &cdt->cdt_agents, ha_list) {
479                 list_del(&ha->ha_list);
480                 if (ha->ha_archive_cnt != 0)
481                         OBD_FREE_PTR_ARRAY(ha->ha_archive_id,
482                                            ha->ha_archive_cnt);
483                 OBD_FREE_PTR(ha);
484         }
485         up_write(&cdt->cdt_agent_lock);
486
487         cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
488         mutex_lock(&cdt->cdt_restore_lock);
489         list_for_each_entry_safe(crh, tmp3, &cdt->cdt_restore_handle_list,
490                                  crh_list) {
491                 /* not locked yet, cleanup by cdt_restore_handle_add() */
492                 if (crh->crh_lh.mlh_type == MDT_NUL_LOCK)
493                         continue;
494                 list_del(&crh->crh_list);
495                 /* give back layout lock */
496                 mdt_object_unlock(cdt_mti, NULL, &crh->crh_lh, 1);
497                 OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
498         }
499         mutex_unlock(&cdt->cdt_restore_lock);
500 }
501
502 /*
503  * Coordinator state transition table, indexed on enum cdt_states, taking
504  * from and to states. For instance since CDT_INIT to CDT_RUNNING is a
505  * valid transition, cdt_transition[CDT_INIT][CDT_RUNNING] is true.
506  */
507 static bool cdt_transition[CDT_STATES_COUNT][CDT_STATES_COUNT] = {
508         /* from -> to:    stopped init   running disable stopping */
509         /* stopped */   { true,   true,  false,  false,  false },
510         /* init */      { true,   false, true,   false,  false },
511         /* running */   { false,  false, true,   true,   true },
512         /* disable */   { false,  false, true,   true,   true },
513         /* stopping */  { true,   false, false,  false,  false }
514 };
515
516 /**
517  * Change coordinator thread state
518  * Some combinations are not valid, so catch them here.
519  *
520  * Returns 0 on success, with old_state set if not NULL, or -EINVAL if
521  * the transition was not possible.
522  */
523 static int set_cdt_state_locked(struct coordinator *cdt,
524                                 enum cdt_states new_state)
525 {
526         int rc;
527         enum cdt_states state;
528
529         state = cdt->cdt_state;
530
531         if (cdt_transition[state][new_state]) {
532                 cdt->cdt_state = new_state;
533                 rc = 0;
534         } else {
535                 CDEBUG(D_HSM,
536                        "unexpected coordinator transition, from=%s, to=%s\n",
537                        cdt_mdt_state2str(state), cdt_mdt_state2str(new_state));
538                 rc = -EINVAL;
539         }
540
541         return rc;
542 }
543
544 static int set_cdt_state(struct coordinator *cdt, enum cdt_states new_state)
545 {
546         int rc;
547
548         mutex_lock(&cdt->cdt_state_lock);
549         rc = set_cdt_state_locked(cdt, new_state);
550         mutex_unlock(&cdt->cdt_state_lock);
551
552         return rc;
553 }
554
555 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti);
556
557 static void cdt_start_pending_restore(struct mdt_device *mdt,
558                                       struct coordinator *cdt)
559 {
560         struct mdt_thread_info *cdt_mti;
561         unsigned int i = 0;
562         int rc;
563
564         /* wait until MDD initialize hsm actions llog */
565         while (!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state) && i < obd_timeout) {
566                 schedule_timeout_interruptible(cfs_time_seconds(1));
567                 i++;
568         }
569         if (!test_bit(MDT_FL_CFGLOG, &mdt->mdt_state))
570                 CWARN("%s: trying to init HSM before MDD\n", mdt_obd_name(mdt));
571
572         /* set up list of started restore requests */
573         cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
574         rc = mdt_hsm_pending_restore(cdt_mti);
575         if (rc)
576                 CERROR("%s: cannot take the layout locks needed for registered restore: %d\n",
577                        mdt_obd_name(mdt), rc);
578
579 }
580
581 /**
582  * coordinator thread
583  * \param data [IN] obd device
584  * \retval 0 success
585  * \retval -ve failure
586  */
587 static int mdt_coordinator(void *data)
588 {
589         struct mdt_thread_info  *mti = data;
590         struct mdt_device       *mdt = mti->mti_mdt;
591         struct coordinator      *cdt = &mdt->mdt_coordinator;
592         struct hsm_scan_data     hsd = { NULL };
593         time64_t                 last_housekeeping = 0;
594         size_t request_sz = 0;
595         int rc;
596         ENTRY;
597
598         CDEBUG(D_HSM, "%s: coordinator thread starting, pid=%d\n",
599                mdt_obd_name(mdt), current->pid);
600
601         hsd.hsd_mti = mti;
602         obd_uuid2fsname(hsd.hsd_fsname, mdt_obd_name(mdt),
603                         sizeof(hsd.hsd_fsname));
604
605         set_cdt_state(cdt, CDT_RUNNING);
606
607         /* Inform mdt_hsm_cdt_start(). */
608         wake_up(&cdt->cdt_waitq);
609         cdt_start_pending_restore(mdt, cdt);
610
611         while (1) {
612                 int i;
613                 int update_idx = 0;
614                 int updates_sz;
615                 int updates_cnt;
616                 u32 start_cat_idx;
617                 u32 start_rec_idx;
618                 struct hsm_record_update *updates;
619
620                 /* Limit execution of the expensive requests traversal
621                  * to at most one second. This prevents repeatedly
622                  * locking/unlocking the catalog for each request
623                  * and preventing other HSM operations from happening
624                  */
625                 wait_event_interruptible_timeout(cdt->cdt_waitq,
626                                                  kthread_should_stop() ||
627                                                  cdt->cdt_wakeup_coordinator,
628                                                  cfs_time_seconds(1));
629
630                 cdt->cdt_wakeup_coordinator = false;
631                 CDEBUG(D_HSM, "coordinator resumes\n");
632
633                 if (kthread_should_stop()) {
634                         CDEBUG(D_HSM, "Coordinator stops\n");
635                         rc = 0;
636                         break;
637                 }
638
639                 /* if coordinator is suspended continue to wait */
640                 if (cdt->cdt_state == CDT_DISABLE) {
641                         CDEBUG(D_HSM, "disable state, coordinator sleeps\n");
642                         continue;
643                 }
644
645                 /* If no event, and no housekeeping to do, continue to
646                  * wait. */
647                 if (last_housekeeping + cdt->cdt_loop_period <=
648                     ktime_get_real_seconds()) {
649                         last_housekeeping = ktime_get_real_seconds();
650                         hsd.hsd_housekeeping = true;
651                         start_cat_idx = 0;
652                         start_rec_idx = 0;
653                 } else if (cdt->cdt_event) {
654                         hsd.hsd_housekeeping = false;
655                         start_cat_idx = hsd.hsd_start_cat_idx;
656                         start_rec_idx = hsd.hsd_start_rec_idx;
657                 } else {
658                         continue;
659                 }
660
661                 cdt->cdt_event = false;
662
663                 CDEBUG(D_HSM, "coordinator starts reading llog\n");
664
665                 if (hsd.hsd_request_len != cdt->cdt_max_requests) {
666                         /* cdt_max_requests has changed,
667                          * we need to allocate a new buffer
668                          */
669                         struct hsm_scan_request *tmp = NULL;
670                         int max_requests = cdt->cdt_max_requests;
671                         OBD_ALLOC_LARGE(tmp, max_requests *
672                                         sizeof(struct hsm_scan_request));
673                         if (!tmp) {
674                                 CERROR("Failed to resize request buffer, "
675                                        "keeping it at %d\n",
676                                        hsd.hsd_request_len);
677                         } else {
678                                 if (hsd.hsd_request != NULL)
679                                         OBD_FREE_LARGE(hsd.hsd_request,
680                                                        request_sz);
681
682                                 hsd.hsd_request_len = max_requests;
683                                 request_sz = hsd.hsd_request_len *
684                                         sizeof(struct hsm_scan_request);
685                                 hsd.hsd_request = tmp;
686                         }
687                 }
688
689                 hsd.hsd_action_count = 0;
690                 hsd.hsd_request_count = 0;
691                 hsd.hsd_one_restore = false;
692
693                 rc = cdt_llog_process(mti->mti_env, mdt, mdt_coordinator_cb,
694                                       &hsd, start_cat_idx, start_rec_idx,
695                                       WRITE);
696                 if (rc < 0)
697                         goto clean_cb_alloc;
698
699                 CDEBUG(D_HSM, "found %d requests to send\n",
700                        hsd.hsd_request_count);
701
702                 if (list_empty(&cdt->cdt_agents)) {
703                         CDEBUG(D_HSM, "no agent available, "
704                                       "coordinator sleeps\n");
705                         /* reset HSM scanning index range. */
706                         hsd.hsd_start_cat_idx = start_cat_idx;
707                         hsd.hsd_start_rec_idx = start_rec_idx;
708                         goto clean_cb_alloc;
709                 }
710
711                 /* Compute how many HAI we have in all the requests */
712                 updates_cnt = 0;
713                 for (i = 0; i < hsd.hsd_request_count; i++) {
714                         const struct hsm_scan_request *request =
715                                 &hsd.hsd_request[i];
716
717                         updates_cnt += request->hal->hal_count;
718                 }
719
720                 /* Allocate a temporary array to store the cookies to
721                  * update, and their status. */
722                 updates_sz = updates_cnt * sizeof(*updates);
723                 OBD_ALLOC_LARGE(updates, updates_sz);
724                 if (updates == NULL) {
725                         CERROR("%s: Cannot allocate memory (%d bytes) "
726                                "for %d updates. Too many HSM requests?\n",
727                                mdt_obd_name(mdt), updates_sz, updates_cnt);
728                         goto clean_cb_alloc;
729                 }
730
731                 /* here hsd contains a list of requests to be started */
732                 for (i = 0; i < hsd.hsd_request_count; i++) {
733                         struct hsm_scan_request *request = &hsd.hsd_request[i];
734                         struct hsm_action_list  *hal = request->hal;
735                         struct hsm_action_item  *hai;
736                         int                      j;
737
738                         /* still room for work ? */
739                         if (atomic_read(&cdt->cdt_request_count) >=
740                             cdt->cdt_max_requests)
741                                 break;
742
743                         rc = mdt_hsm_agent_send(mti, hal, 0);
744                         /* if failure, we suppose it is temporary
745                          * if the copy tool failed to do the request
746                          * it has to use hsm_progress
747                          */
748
749                         /* set up cookie vector to set records status
750                          * after copy tools start or failed
751                          */
752                         hai = hai_first(hal);
753                         for (j = 0; j < hal->hal_count; j++) {
754                                 updates[update_idx].cookie = hai->hai_cookie;
755                                 updates[update_idx].status =
756                                         (rc ? ARS_WAITING : ARS_STARTED);
757                                 hai = hai_next(hai);
758                                 update_idx++;
759                         }
760
761                         /* TODO: narrow down the HSM action range that already
762                          * scanned accroding to the cookies when a failure
763                          * occurs.
764                          */
765                         if (rc) {
766                                 hsd.hsd_start_cat_idx = start_cat_idx;
767                                 hsd.hsd_start_rec_idx = start_rec_idx;
768                         }
769                 }
770
771                 if (update_idx) {
772                         rc = mdt_agent_record_update(mti, updates, update_idx);
773                         if (rc)
774                                 CERROR("%s: mdt_agent_record_update() failed, "
775                                        "rc=%d, cannot update records "
776                                        "for %d cookies\n",
777                                        mdt_obd_name(mdt), rc, update_idx);
778                 }
779
780                 OBD_FREE_LARGE(updates, updates_sz);
781
782 clean_cb_alloc:
783                 /* free hal allocated by callback */
784                 for (i = 0; i < hsd.hsd_request_count; i++) {
785                         struct hsm_scan_request *request = &hsd.hsd_request[i];
786
787                         OBD_FREE_LARGE(request->hal, request->hal_sz);
788                 }
789         }
790
791         if (hsd.hsd_request != NULL)
792                 OBD_FREE_LARGE(hsd.hsd_request, request_sz);
793
794         mdt_hsm_cdt_cleanup(mdt);
795
796         if (rc != 0)
797                 CERROR("%s: coordinator thread exiting, process=%d, rc=%d\n",
798                        mdt_obd_name(mdt), current->pid, rc);
799         else
800                 CDEBUG(D_HSM, "%s: coordinator thread exiting, process=%d,"
801                               " no error\n",
802                        mdt_obd_name(mdt), current->pid);
803
804         RETURN(rc);
805 }
806
807 /**
808  * register a new HSM restore handle for a file and take EX lock on the layout
809  * \param mti [IN] thread info
810  * \param cdt [IN] coordinator
811  * \param fid [IN] fid of the file to restore
812  * \param he  [IN] HSM extent
813  * \retval 0 success
814  * \retval 1 restore handle already exists for the fid
815  * \retval -ve failure
816  */
817 int cdt_restore_handle_add(struct mdt_thread_info *mti, struct coordinator *cdt,
818                            const struct lu_fid *fid,
819                            const struct hsm_extent *he)
820 {
821         struct mdt_lock_handle lh = { 0 };
822         struct cdt_restore_handle *crh;
823         struct mdt_object *obj;
824         int rc;
825         ENTRY;
826
827         OBD_SLAB_ALLOC_PTR(crh, mdt_hsm_cdt_kmem);
828         if (crh == NULL)
829                 RETURN(-ENOMEM);
830
831         crh->crh_fid = *fid;
832         /* in V1 all file is restored
833          * crh->extent.start = he->offset;
834          * crh->extent.end = he->offset + he->length;
835          */
836         crh->crh_extent.start = 0;
837         crh->crh_extent.end = he->length;
838         crh->crh_lh.mlh_type = MDT_NUL_LOCK;
839
840         mutex_lock(&cdt->cdt_restore_lock);
841         if (cdt_restore_handle_find(cdt, fid) != NULL)
842                 GOTO(out_crl, rc = 1);
843
844         if (unlikely(cdt->cdt_state == CDT_STOPPED ||
845                      cdt->cdt_state == CDT_STOPPING))
846                 GOTO(out_crl, rc = -EAGAIN);
847
848         list_add_tail(&crh->crh_list, &cdt->cdt_restore_handle_list);
849         mutex_unlock(&cdt->cdt_restore_lock);
850
851         /* get the layout lock */
852         mdt_lock_reg_init(&lh, LCK_EX);
853         obj = mdt_object_find_lock(mti, &crh->crh_fid, &lh,
854                                    MDS_INODELOCK_LAYOUT);
855         if (IS_ERR(obj)) {
856                 mutex_lock(&cdt->cdt_restore_lock);
857                 GOTO(out_ldel, rc = PTR_ERR(obj));
858         }
859
860         /* We do not keep a reference on the object during the restore
861          * which can be very long.
862          */
863         mdt_object_put(mti->mti_env, obj);
864
865         mutex_lock(&cdt->cdt_restore_lock);
866         if (unlikely(cdt->cdt_state == CDT_STOPPED ||
867                      cdt->cdt_state == CDT_STOPPING))
868                 GOTO(out_lh, rc = -EAGAIN);
869
870         crh->crh_lh = lh;
871         mutex_unlock(&cdt->cdt_restore_lock);
872
873         RETURN(0);
874 out_lh:
875         mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
876 out_ldel:
877         list_del(&crh->crh_list);
878 out_crl:
879         mutex_unlock(&cdt->cdt_restore_lock);
880         OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
881
882         return rc;
883 }
884
885 /**
886  * lookup a restore handle by FID
887  * caller needs to hold cdt_restore_lock
888  * \param cdt [IN] coordinator
889  * \param fid [IN] FID
890  * \retval cdt_restore_handle found
891  * \retval NULL not found
892  */
893 struct cdt_restore_handle *cdt_restore_handle_find(struct coordinator *cdt,
894                                                    const struct lu_fid *fid)
895 {
896         struct cdt_restore_handle *crh;
897         ENTRY;
898
899         list_for_each_entry(crh, &cdt->cdt_restore_handle_list, crh_list) {
900                 if (lu_fid_eq(&crh->crh_fid, fid))
901                         RETURN(crh);
902         }
903
904         RETURN(NULL);
905 }
906
907 void cdt_restore_handle_del(struct mdt_thread_info *mti,
908                             struct coordinator *cdt, const struct lu_fid *fid)
909 {
910         struct cdt_restore_handle *crh;
911
912         /* give back layout lock */
913         mutex_lock(&cdt->cdt_restore_lock);
914         crh = cdt_restore_handle_find(cdt, fid);
915         if (crh != NULL)
916                 list_del(&crh->crh_list);
917         mutex_unlock(&cdt->cdt_restore_lock);
918
919         if (crh == NULL)
920                 return;
921
922         /* XXX We pass a NULL object since the restore handle does not
923          * keep a reference on the object being restored. */
924         mdt_object_unlock(mti, NULL, &crh->crh_lh, 1);
925         OBD_SLAB_FREE_PTR(crh, mdt_hsm_cdt_kmem);
926 }
927
928 /**
929  * data passed to llog_cat_process() callback
930  * to scan requests and take actions
931  */
932 struct hsm_restore_data {
933         struct mdt_thread_info  *hrd_mti;
934 };
935
936 /**
937  *  llog_cat_process() callback, used to:
938  *  - find restore request and allocate the restore handle
939  * \param env [IN] environment
940  * \param llh [IN] llog handle
941  * \param hdr [IN] llog record
942  * \param data [IN/OUT] cb data = struct hsm_restore_data
943  * \retval 0 success
944  * \retval -ve failure
945  */
946 static int hsm_restore_cb(const struct lu_env *env,
947                           struct llog_handle *llh,
948                           struct llog_rec_hdr *hdr, void *data)
949 {
950         struct llog_agent_req_rec       *larr;
951         struct hsm_restore_data         *hrd;
952         struct hsm_action_item          *hai;
953         struct mdt_thread_info          *mti;
954         struct coordinator              *cdt;
955         int rc;
956         ENTRY;
957
958         hrd = data;
959         mti = hrd->hrd_mti;
960         cdt = &mti->mti_mdt->mdt_coordinator;
961
962         larr = (struct llog_agent_req_rec *)hdr;
963         hai = &larr->arr_hai;
964         if (hai->hai_cookie >= cdt->cdt_last_cookie) {
965                 /* update the cookie to avoid collision */
966                 cdt->cdt_last_cookie = hai->hai_cookie + 1;
967         }
968
969         if (hai->hai_action != HSMA_RESTORE ||
970             agent_req_in_final_state(larr->arr_status))
971                 RETURN(0);
972
973         /* restore request not in a final state */
974
975         /* force replay of restore requests left in started state from previous
976          * CDT context, to be canceled later if finally found to be incompatible
977          * when being re-started */
978         if (larr->arr_status == ARS_STARTED) {
979                 larr->arr_status = ARS_WAITING;
980                 larr->arr_req_change = ktime_get_real_seconds();
981                 rc = llog_write(env, llh, hdr, hdr->lrh_index);
982                 if (rc != 0)
983                         GOTO(out, rc);
984         }
985
986         rc = cdt_restore_handle_add(mti, cdt, &hai->hai_fid, &hai->hai_extent);
987         if (rc == 1)
988                 rc = 0;
989 out:
990         RETURN(rc);
991 }
992
993 /**
994  * restore coordinator state at startup
995  * the goal is to take a layout lock for each registered restore request
996  * \param mti [IN] context
997  */
998 static int mdt_hsm_pending_restore(struct mdt_thread_info *mti)
999 {
1000         struct hsm_restore_data  hrd;
1001         int                      rc;
1002         ENTRY;
1003
1004         hrd.hrd_mti = mti;
1005
1006         rc = cdt_llog_process(mti->mti_env, mti->mti_mdt, hsm_restore_cb, &hrd,
1007                               0, 0, WRITE);
1008
1009         RETURN(rc);
1010 }
1011
1012 int hsm_init_ucred(struct lu_ucred *uc)
1013 {
1014         ENTRY;
1015         uc->uc_valid = UCRED_OLD;
1016         uc->uc_o_uid = 0;
1017         uc->uc_o_gid = 0;
1018         uc->uc_o_fsuid = 0;
1019         uc->uc_o_fsgid = 0;
1020         uc->uc_uid = 0;
1021         uc->uc_gid = 0;
1022         uc->uc_fsuid = 0;
1023         uc->uc_fsgid = 0;
1024         uc->uc_suppgids[0] = -1;
1025         uc->uc_suppgids[1] = -1;
1026         uc->uc_cap = cap_combine(CAP_FS_SET, CAP_NFSD_SET);
1027         uc->uc_umask = 0777;
1028         uc->uc_ginfo = NULL;
1029         uc->uc_identity = NULL;
1030         /* always record internal HSM activity if also enabled globally */
1031         uc->uc_enable_audit = 1;
1032
1033         RETURN(0);
1034 }
1035
1036 /**
1037  * initialize coordinator struct
1038  * \param mdt [IN] device
1039  * \retval 0 success
1040  * \retval -ve failure
1041  */
1042 int mdt_hsm_cdt_init(struct mdt_device *mdt)
1043 {
1044         struct coordinator      *cdt = &mdt->mdt_coordinator;
1045         struct mdt_thread_info  *cdt_mti = NULL;
1046         int                      rc;
1047         ENTRY;
1048
1049         init_waitqueue_head(&cdt->cdt_waitq);
1050         init_rwsem(&cdt->cdt_llog_lock);
1051         init_rwsem(&cdt->cdt_agent_lock);
1052         init_rwsem(&cdt->cdt_request_lock);
1053         mutex_init(&cdt->cdt_restore_lock);
1054         mutex_init(&cdt->cdt_state_lock);
1055         set_cdt_state(cdt, CDT_STOPPED);
1056
1057         INIT_LIST_HEAD(&cdt->cdt_request_list);
1058         INIT_LIST_HEAD(&cdt->cdt_agents);
1059         INIT_LIST_HEAD(&cdt->cdt_restore_handle_list);
1060
1061         cdt->cdt_request_cookie_hash = cfs_hash_create("REQUEST_COOKIE_HASH",
1062                                                        CFS_HASH_BITS_MIN,
1063                                                        CFS_HASH_BITS_MAX,
1064                                                        CFS_HASH_BKT_BITS,
1065                                                        0 /* extra bytes */,
1066                                                        CFS_HASH_MIN_THETA,
1067                                                        CFS_HASH_MAX_THETA,
1068                                                 &cdt_request_cookie_hash_ops,
1069                                                        CFS_HASH_DEFAULT);
1070         if (cdt->cdt_request_cookie_hash == NULL)
1071                 RETURN(-ENOMEM);
1072
1073         cdt->cdt_agent_record_hash = cfs_hash_create("AGENT_RECORD_HASH",
1074                                                      CFS_HASH_BITS_MIN,
1075                                                      CFS_HASH_BITS_MAX,
1076                                                      CFS_HASH_BKT_BITS,
1077                                                      0 /* extra bytes */,
1078                                                      CFS_HASH_MIN_THETA,
1079                                                      CFS_HASH_MAX_THETA,
1080                                                      &cdt_agent_record_hash_ops,
1081                                                      CFS_HASH_DEFAULT);
1082         if (cdt->cdt_agent_record_hash == NULL)
1083                 GOTO(out_request_cookie_hash, rc = -ENOMEM);
1084
1085         rc = lu_env_init(&cdt->cdt_env, LCT_MD_THREAD);
1086         if (rc < 0)
1087                 GOTO(out_agent_record_hash, rc);
1088
1089         /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1090         rc = lu_context_init(&cdt->cdt_session, LCT_SERVER_SESSION);
1091         if (rc < 0)
1092                 GOTO(out_env, rc);
1093
1094         lu_context_enter(&cdt->cdt_session);
1095         cdt->cdt_env.le_ses = &cdt->cdt_session;
1096
1097         cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1098         LASSERT(cdt_mti != NULL);
1099
1100         cdt_mti->mti_env = &cdt->cdt_env;
1101         cdt_mti->mti_mdt = mdt;
1102
1103         hsm_init_ucred(mdt_ucred(cdt_mti));
1104
1105         /* default values for sysfs tunnables
1106          * can be override by MGS conf */
1107         cdt->cdt_default_archive_id = 1;
1108         cdt->cdt_grace_delay = 60;
1109         cdt->cdt_loop_period = 10;
1110         cdt->cdt_max_requests = 3;
1111         cdt->cdt_policy = CDT_DEFAULT_POLICY;
1112         cdt->cdt_active_req_timeout = 3600;
1113
1114         /* by default do not remove archives on last unlink */
1115         cdt->cdt_remove_archive_on_last_unlink = false;
1116
1117         RETURN(0);
1118
1119 out_env:
1120         lu_env_fini(&cdt->cdt_env);
1121 out_agent_record_hash:
1122         cfs_hash_putref(cdt->cdt_agent_record_hash);
1123         cdt->cdt_agent_record_hash = NULL;
1124 out_request_cookie_hash:
1125         cfs_hash_putref(cdt->cdt_request_cookie_hash);
1126         cdt->cdt_request_cookie_hash = NULL;
1127
1128         return rc;
1129 }
1130
1131 /**
1132  * free a coordinator thread
1133  * \param mdt [IN] device
1134  */
1135 int  mdt_hsm_cdt_fini(struct mdt_device *mdt)
1136 {
1137         struct coordinator *cdt = &mdt->mdt_coordinator;
1138         ENTRY;
1139
1140         lu_context_exit(cdt->cdt_env.le_ses);
1141         lu_context_fini(cdt->cdt_env.le_ses);
1142
1143         lu_env_fini(&cdt->cdt_env);
1144
1145         cfs_hash_putref(cdt->cdt_agent_record_hash);
1146         cdt->cdt_agent_record_hash = NULL;
1147
1148         cfs_hash_putref(cdt->cdt_request_cookie_hash);
1149         cdt->cdt_request_cookie_hash = NULL;
1150
1151         RETURN(0);
1152 }
1153
1154 /**
1155  * start a coordinator thread
1156  * \param mdt [IN] device
1157  * \retval 0 success
1158  * \retval -ve failure
1159  */
1160 static int mdt_hsm_cdt_start(struct mdt_device *mdt)
1161 {
1162         struct coordinator *cdt = &mdt->mdt_coordinator;
1163         struct mdt_thread_info *cdt_mti;
1164         int rc;
1165         void *ptr;
1166         struct task_struct *task;
1167         ENTRY;
1168
1169         /* functions defined but not yet used
1170          * this avoid compilation warning
1171          */
1172         ptr = dump_requests;
1173
1174         rc = set_cdt_state(cdt, CDT_INIT);
1175         if (rc) {
1176                 CERROR("%s: Coordinator already started or stopping\n",
1177                        mdt_obd_name(mdt));
1178                 RETURN(-EALREADY);
1179         }
1180
1181         BUILD_BUG_ON(BIT(CDT_POLICY_SHIFT_COUNT - 1) != CDT_POLICY_LAST);
1182         cdt->cdt_policy = CDT_DEFAULT_POLICY;
1183
1184         /* just need to be larger than previous one */
1185         /* cdt_last_cookie is protected by cdt_llog_lock */
1186         cdt->cdt_last_cookie = ktime_get_real_seconds();
1187         atomic_set(&cdt->cdt_request_count, 0);
1188         atomic_set(&cdt->cdt_archive_count, 0);
1189         atomic_set(&cdt->cdt_restore_count, 0);
1190         atomic_set(&cdt->cdt_remove_count, 0);
1191         cdt->cdt_user_request_mask = (1UL << HSMA_RESTORE);
1192         cdt->cdt_group_request_mask = (1UL << HSMA_RESTORE);
1193         cdt->cdt_other_request_mask = (1UL << HSMA_RESTORE);
1194
1195         /* to avoid deadlock when start is made through sysfs
1196          * sysfs entries are created by the coordinator thread
1197          */
1198         if (mdt->mdt_bottom->dd_rdonly)
1199                 RETURN(0);
1200
1201         cdt_mti = lu_context_key_get(&cdt->cdt_env.le_ctx, &mdt_thread_key);
1202         task = kthread_run(mdt_coordinator, cdt_mti, "hsm_cdtr");
1203         if (IS_ERR(task)) {
1204                 rc = PTR_ERR(task);
1205                 set_cdt_state(cdt, CDT_STOPPED);
1206                 CERROR("%s: error starting coordinator thread: %d\n",
1207                        mdt_obd_name(mdt), rc);
1208         } else {
1209                 cdt->cdt_task = task;
1210                 wait_event(cdt->cdt_waitq,
1211                            cdt->cdt_state != CDT_INIT);
1212                 CDEBUG(D_HSM, "%s: coordinator thread started\n",
1213                        mdt_obd_name(mdt));
1214                 rc = 0;
1215         }
1216
1217         RETURN(rc);
1218 }
1219
1220 /**
1221  * stop a coordinator thread
1222  * \param mdt [IN] device
1223  */
1224 int mdt_hsm_cdt_stop(struct mdt_device *mdt)
1225 {
1226         struct coordinator *cdt = &mdt->mdt_coordinator;
1227         int rc;
1228
1229         ENTRY;
1230         /* stop coordinator thread */
1231         rc = set_cdt_state(cdt, CDT_STOPPING);
1232         if (rc == 0) {
1233                 kthread_stop(cdt->cdt_task);
1234                 cdt->cdt_task = NULL;
1235                 set_cdt_state(cdt, CDT_STOPPED);
1236         }
1237
1238         RETURN(rc);
1239 }
1240
1241 static int mdt_hsm_set_exists(struct mdt_thread_info *mti,
1242                               const struct lu_fid *fid,
1243                               u32 archive_id)
1244 {
1245         struct mdt_object *obj;
1246         struct md_hsm mh;
1247         int rc;
1248
1249         obj = mdt_hsm_get_md_hsm(mti, fid, &mh);
1250         if (IS_ERR(obj))
1251                 GOTO(out, rc = PTR_ERR(obj));
1252
1253         if (mh.mh_flags & HS_EXISTS &&
1254             mh.mh_arch_id == archive_id)
1255                 GOTO(out_obj, rc = 0);
1256
1257         mh.mh_flags |= HS_EXISTS;
1258         mh.mh_arch_id = archive_id;
1259         rc = mdt_hsm_attr_set(mti, obj, &mh);
1260
1261 out_obj:
1262         mdt_object_put(mti->mti_env, obj);
1263 out:
1264         return rc;
1265 }
1266
1267 /**
1268  * register all requests from an hal in the memory list
1269  * \param mti [IN] context
1270  * \param hal [IN] request
1271  * \param uuid [OUT] in case of CANCEL, the uuid of the agent
1272  *  which is running the CT
1273  * \retval 0 success
1274  * \retval -ve failure
1275  */
1276 int mdt_hsm_add_hal(struct mdt_thread_info *mti,
1277                     struct hsm_action_list *hal, struct obd_uuid *uuid)
1278 {
1279         struct mdt_device       *mdt = mti->mti_mdt;
1280         struct coordinator      *cdt = &mdt->mdt_coordinator;
1281         struct hsm_action_item  *hai;
1282         int                      rc = 0, i;
1283         ENTRY;
1284
1285         /* register request in memory list */
1286         hai = hai_first(hal);
1287         for (i = 0; i < hal->hal_count; i++, hai = hai_next(hai)) {
1288                 struct cdt_agent_req *car;
1289
1290                 /* in case of a cancel request, we first mark the ondisk
1291                  * record of the request we want to stop as canceled
1292                  * this does not change the cancel record
1293                  * it will be done when updating the request status
1294                  */
1295                 if (hai->hai_action == HSMA_CANCEL) {
1296                         struct hsm_record_update update = {
1297                                 .cookie = hai->hai_cookie,
1298                                 .status = ARS_CANCELED,
1299                         };
1300
1301                         rc = mdt_agent_record_update(mti, &update, 1);
1302                         if (rc) {
1303                                 CERROR("%s: mdt_agent_record_update() failed, "
1304                                        "rc=%d, cannot update status to %s "
1305                                        "for cookie %#llx\n",
1306                                        mdt_obd_name(mdt), rc,
1307                                        agent_req_status2name(ARS_CANCELED),
1308                                        hai->hai_cookie);
1309                                 GOTO(out, rc);
1310                         }
1311
1312                         /* find the running request to set it canceled */
1313                         car = mdt_cdt_find_request(cdt, hai->hai_cookie);
1314                         if (car != NULL) {
1315                                 car->car_canceled = 1;
1316                                 /* uuid has to be changed to the one running the
1317                                 * request to cancel */
1318                                 *uuid = car->car_uuid;
1319                                 mdt_cdt_put_request(car);
1320                         }
1321                         /* no need to memorize cancel request
1322                          * this also avoid a deadlock when we receive
1323                          * a purge all requests command
1324                          */
1325                         continue;
1326                 }
1327
1328                 if (hai->hai_action == HSMA_ARCHIVE) {
1329                         rc = mdt_hsm_set_exists(mti, &hai->hai_fid,
1330                                                 hal->hal_archive_id);
1331                         if (rc == -ENOENT)
1332                                 continue;
1333                         else if (rc < 0)
1334                                 GOTO(out, rc);
1335                 }
1336
1337                 car = mdt_cdt_alloc_request(hal->hal_archive_id, hal->hal_flags,
1338                                             uuid, hai);
1339                 if (IS_ERR(car))
1340                         GOTO(out, rc = PTR_ERR(car));
1341
1342                 rc = mdt_cdt_add_request(cdt, car);
1343                 if (rc != 0)
1344                         mdt_cdt_free_request(car);
1345         }
1346 out:
1347         RETURN(rc);
1348 }
1349
1350 /**
1351  * swap layouts between 2 fids
1352  * \param mti [IN] context
1353  * \param obj [IN]
1354  * \param dfid [IN]
1355  * \param mh_common [IN] MD HSM
1356  */
1357 static int hsm_swap_layouts(struct mdt_thread_info *mti,
1358                             struct mdt_object *obj, const struct lu_fid *dfid,
1359                             struct md_hsm *mh_common)
1360 {
1361         struct mdt_object       *dobj;
1362         struct mdt_lock_handle  *dlh;
1363         int                      rc;
1364         ENTRY;
1365
1366         if (!mdt_object_exists(obj))
1367                 GOTO(out, rc = -ENOENT);
1368
1369         /* we already have layout lock on obj so take only
1370          * on dfid */
1371         dlh = &mti->mti_lh[MDT_LH_OLD];
1372         mdt_lock_reg_init(dlh, LCK_EX);
1373         dobj = mdt_object_find_lock(mti, dfid, dlh, MDS_INODELOCK_LAYOUT);
1374         if (IS_ERR(dobj))
1375                 GOTO(out, rc = PTR_ERR(dobj));
1376
1377         /* if copy tool closes the volatile before sending the final
1378          * progress through llapi_hsm_copy_end(), all the objects
1379          * are removed and mdd_swap_layout LBUG */
1380         if (!mdt_object_exists(dobj)) {
1381                 CERROR("%s: Copytool has closed volatile file "DFID"\n",
1382                        mdt_obd_name(mti->mti_mdt), PFID(dfid));
1383                 GOTO(out_dobj, rc = -ENOENT);
1384         }
1385         /* Since we only handle restores here, unconditionally use
1386          * SWAP_LAYOUTS_MDS_HSM flag to ensure original layout will
1387          * be preserved in case of failure during swap_layout and not
1388          * leave a file in an intermediate but incoherent state.
1389          * But need to setup HSM xattr of data FID before, reuse
1390          * mti and mh presets for FID in hsm_cdt_request_completed(),
1391          * only need to clear RELEASED and DIRTY.
1392          */
1393         mh_common->mh_flags &= ~(HS_RELEASED | HS_DIRTY);
1394         rc = mdt_hsm_attr_set(mti, dobj, mh_common);
1395         if (rc == 0)
1396                 rc = mo_swap_layouts(mti->mti_env,
1397                                      mdt_object_child(obj),
1398                                      mdt_object_child(dobj),
1399                                      SWAP_LAYOUTS_MDS_HSM);
1400         if (rc == 0) {
1401                 rc = mdt_lsom_downgrade(mti, obj);
1402                 if (rc)
1403                         CDEBUG(D_INODE,
1404                                "%s: File fid="DFID" SOM "
1405                                "downgrade failed, rc = %d\n",
1406                                mdt_obd_name(mti->mti_mdt),
1407                                PFID(mdt_object_fid(obj)), rc);
1408         }
1409 out_dobj:
1410         mdt_object_unlock_put(mti, dobj, dlh, 1);
1411 out:
1412         RETURN(rc);
1413 }
1414
1415 /**
1416  * update status of a completed request
1417  * \param mti [IN] context
1418  * \param pgs [IN] progress of the copy tool
1419  * \retval 0 success
1420  * \retval -ve failure
1421  */
1422 static int hsm_cdt_request_completed(struct mdt_thread_info *mti,
1423                                      struct hsm_progress_kernel *pgs,
1424                                      const struct cdt_agent_req *car,
1425                                      enum agent_req_status *status)
1426 {
1427         const struct lu_env *env = mti->mti_env;
1428         struct mdt_device *mdt = mti->mti_mdt;
1429         struct coordinator *cdt = &mdt->mdt_coordinator;
1430         struct mdt_object *obj = NULL;
1431         enum changelog_rec_flags clf_flags = 0;
1432         struct md_hsm mh;
1433         bool is_mh_changed;
1434         bool need_changelog = true;
1435         int rc = 0;
1436
1437         ENTRY;
1438         /* default is to retry */
1439         *status = ARS_WAITING;
1440
1441         /* find object by FID, mdt_hsm_get_md_hsm() returns obj or err
1442          * if error/removed continue anyway to get correct reporting done */
1443         obj = mdt_hsm_get_md_hsm(mti, &car->car_hai->hai_fid, &mh);
1444         /* we will update MD HSM only if needed */
1445         is_mh_changed = false;
1446
1447         /* no need to change mh->mh_arch_id
1448          * mdt_hsm_get_md_hsm() got it from disk and it is still valid
1449          */
1450         if (pgs->hpk_errval != 0) {
1451                 switch (pgs->hpk_errval) {
1452                 case ENOSYS:
1453                         /* the copy tool does not support cancel
1454                          * so the cancel request is failed
1455                          * As we cannot distinguish a cancel progress
1456                          * from another action progress (they have the
1457                          * same cookie), we suppose here the CT returns
1458                          * ENOSYS only if does not support cancel
1459                          */
1460                         /* this can also happen when cdt calls it to
1461                          * for a timed out request */
1462                         *status = ARS_FAILED;
1463                         /* to have a cancel event in changelog */
1464                         pgs->hpk_errval = ECANCELED;
1465                         break;
1466                 case ECANCELED:
1467                         /* the request record has already been set to
1468                          * ARS_CANCELED, this set the cancel request
1469                          * to ARS_SUCCEED */
1470                         *status = ARS_SUCCEED;
1471                         break;
1472                 default:
1473                         /* retry only if current policy or requested, and
1474                          * object is not on error/removed */
1475                         *status = (cdt->cdt_policy & CDT_NORETRY_ACTION ||
1476                                    !(pgs->hpk_flags & HP_FLAG_RETRY) ||
1477                                    IS_ERR(obj)) ? ARS_FAILED : ARS_WAITING;
1478                         break;
1479                 }
1480
1481                 if (pgs->hpk_errval > CLF_HSM_MAXERROR) {
1482                         CERROR("%s: Request %#llx on "DFID
1483                                " failed, error code %d too large\n",
1484                                mdt_obd_name(mdt),
1485                                pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1486                                pgs->hpk_errval);
1487                         hsm_set_cl_error(&clf_flags, CLF_HSM_ERROVERFLOW);
1488                         rc = -EINVAL;
1489                 } else {
1490                         hsm_set_cl_error(&clf_flags, pgs->hpk_errval);
1491                 }
1492
1493                 switch (car->car_hai->hai_action) {
1494                 case HSMA_ARCHIVE:
1495                         hsm_set_cl_event(&clf_flags, HE_ARCHIVE);
1496                         break;
1497                 case HSMA_RESTORE:
1498                         hsm_set_cl_event(&clf_flags, HE_RESTORE);
1499                         break;
1500                 case HSMA_REMOVE:
1501                         hsm_set_cl_event(&clf_flags, HE_REMOVE);
1502                         break;
1503                 case HSMA_CANCEL:
1504                         hsm_set_cl_event(&clf_flags, HE_CANCEL);
1505                         CERROR("%s: Failed request %#llx on "DFID
1506                                " cannot be a CANCEL\n",
1507                                mdt_obd_name(mdt),
1508                                pgs->hpk_cookie,
1509                                PFID(&pgs->hpk_fid));
1510                         break;
1511                 default:
1512                         CERROR("%s: Failed request %#llx on "DFID
1513                                " %d is an unknown action\n",
1514                                mdt_obd_name(mdt),
1515                                pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1516                                car->car_hai->hai_action);
1517                         rc = -EINVAL;
1518                         break;
1519                 }
1520         } else {
1521                 *status = ARS_SUCCEED;
1522                 switch (car->car_hai->hai_action) {
1523                 case HSMA_ARCHIVE:
1524                         hsm_set_cl_event(&clf_flags, HE_ARCHIVE);
1525                         /* set ARCHIVE keep EXIST and clear LOST and
1526                          * DIRTY */
1527                         mh.mh_arch_ver = pgs->hpk_data_version;
1528                         mh.mh_flags |= HS_ARCHIVED;
1529                         mh.mh_flags &= ~(HS_LOST|HS_DIRTY);
1530                         is_mh_changed = true;
1531                         break;
1532                 case HSMA_RESTORE:
1533                         hsm_set_cl_event(&clf_flags, HE_RESTORE);
1534
1535                         /* do not clear RELEASED and DIRTY here
1536                          * this will occur in hsm_swap_layouts()
1537                          */
1538
1539                         /* Restoring has changed the file version on
1540                          * disk. */
1541                         mh.mh_arch_ver = pgs->hpk_data_version;
1542                         is_mh_changed = true;
1543                         break;
1544                 case HSMA_REMOVE:
1545                         hsm_set_cl_event(&clf_flags, HE_REMOVE);
1546                         /* clear ARCHIVED EXISTS and LOST */
1547                         mh.mh_flags &= ~(HS_ARCHIVED | HS_EXISTS | HS_LOST);
1548                         is_mh_changed = true;
1549                         break;
1550                 case HSMA_CANCEL:
1551                         hsm_set_cl_event(&clf_flags, HE_CANCEL);
1552                         CERROR("%s: Successful request %#llx on "DFID" cannot be a CANCEL\n",
1553                                mdt_obd_name(mdt),
1554                                pgs->hpk_cookie,
1555                                PFID(&pgs->hpk_fid));
1556                         break;
1557                 default:
1558                         CERROR("%s: Successful request %#llx on "DFID" %d is an unknown action\n",
1559                                mdt_obd_name(mdt),
1560                                pgs->hpk_cookie, PFID(&pgs->hpk_fid),
1561                                car->car_hai->hai_action);
1562                         rc = -EINVAL;
1563                         break;
1564                 }
1565         }
1566
1567         /* rc != 0 means error when analysing action, it may come from
1568          * a crasy CT no need to manage DIRTY
1569          * and if mdt_hsm_get_md_hsm() has returned an error, mh has not been
1570          * filled
1571          */
1572         if (rc == 0 && !IS_ERR(obj))
1573                 hsm_set_cl_flags(&clf_flags,
1574                                  mh.mh_flags & HS_DIRTY ? CLF_HSM_DIRTY : 0);
1575
1576         /* unlock is done later, after layout lock management */
1577         if (is_mh_changed && !IS_ERR(obj))
1578                 rc = mdt_hsm_attr_set(mti, obj, &mh);
1579
1580         /* we give back layout lock only if restore was successful or
1581          * if no retry will be attempted and if object is still alive,
1582          * in other cases we just unlock the object */
1583         if (car->car_hai->hai_action == HSMA_RESTORE) {
1584                 struct mdt_lock_handle *lh;
1585
1586                 /* restore in data FID done, we swap the layouts
1587                  * only if restore is successful */
1588                 if (pgs->hpk_errval == 0 && !IS_ERR(obj)) {
1589                         rc = hsm_swap_layouts(mti, obj, &car->car_hai->hai_dfid,
1590                                               &mh);
1591                         if (rc) {
1592                                 if (cdt->cdt_policy & CDT_NORETRY_ACTION)
1593                                         *status = ARS_FAILED;
1594                                 pgs->hpk_errval = -rc;
1595                         }
1596                 }
1597                 /* we have to retry, so keep layout lock */
1598                 if (*status == ARS_WAITING)
1599                         GOTO(out, rc);
1600
1601                 /* restore special case, need to create ChangeLog record
1602                  * before to give back layout lock to avoid concurrent
1603                  * file updater to post out of order ChangeLog */
1604                 mo_changelog(env, CL_HSM, clf_flags, mdt->mdt_child,
1605                              &car->car_hai->hai_fid);
1606                 need_changelog = false;
1607
1608                 cdt_restore_handle_del(mti, cdt, &car->car_hai->hai_fid);
1609                 if (!IS_ERR_OR_NULL(obj)) {
1610                         /* flush UPDATE lock so attributes are upadated */
1611                         lh = &mti->mti_lh[MDT_LH_OLD];
1612                         mdt_lock_reg_init(lh, LCK_EX);
1613                         mdt_object_lock(mti, obj, lh, MDS_INODELOCK_UPDATE);
1614                         mdt_object_unlock(mti, obj, lh, 1);
1615                 }
1616         }
1617
1618         GOTO(out, rc);
1619
1620 out:
1621         /* always add a ChangeLog record */
1622         if (need_changelog)
1623                 mo_changelog(env, CL_HSM, clf_flags, mdt->mdt_child,
1624                              &car->car_hai->hai_fid);
1625
1626         if (!IS_ERR(obj))
1627                 mdt_object_put(mti->mti_env, obj);
1628
1629         RETURN(rc);
1630 }
1631
1632 /**
1633  * update status of a request
1634  * \param mti [IN] context
1635  * \param pgs [IN] progress of the copy tool
1636  * \retval 0 success
1637  * \retval -ve failure
1638  */
1639 int mdt_hsm_update_request_state(struct mdt_thread_info *mti,
1640                                  struct hsm_progress_kernel *pgs)
1641 {
1642         struct mdt_device       *mdt = mti->mti_mdt;
1643         struct coordinator      *cdt = &mdt->mdt_coordinator;
1644         struct cdt_agent_req    *car;
1645         int                      rc = 0;
1646         ENTRY;
1647
1648         /* no coordinator started, so we cannot serve requests */
1649         if (cdt->cdt_state == CDT_STOPPED)
1650                 RETURN(-EAGAIN);
1651
1652         /* first do sanity checks */
1653         car = mdt_cdt_update_request(cdt, pgs);
1654         if (IS_ERR(car)) {
1655                 CERROR("%s: Cannot find running request for cookie %#llx"
1656                        " on fid="DFID"\n",
1657                        mdt_obd_name(mdt),
1658                        pgs->hpk_cookie, PFID(&pgs->hpk_fid));
1659
1660                 RETURN(PTR_ERR(car));
1661         }
1662
1663         CDEBUG(D_HSM, "Progress received for fid="DFID" cookie=%#llx"
1664                       " action=%s flags=%d err=%d fid="DFID" dfid="DFID"\n",
1665                       PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1666                       hsm_copytool_action2name(car->car_hai->hai_action),
1667                       pgs->hpk_flags, pgs->hpk_errval,
1668                       PFID(&car->car_hai->hai_fid),
1669                       PFID(&car->car_hai->hai_dfid));
1670
1671         /* progress is done on FID or data FID depending of the action and
1672          * of the copy progress */
1673         /* for restore progress is used to send back the data FID to cdt */
1674         if (car->car_hai->hai_action == HSMA_RESTORE &&
1675             lu_fid_eq(&car->car_hai->hai_fid, &car->car_hai->hai_dfid))
1676                 car->car_hai->hai_dfid = pgs->hpk_fid;
1677
1678         if ((car->car_hai->hai_action == HSMA_RESTORE ||
1679              car->car_hai->hai_action == HSMA_ARCHIVE) &&
1680             (!lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_dfid) &&
1681              !lu_fid_eq(&pgs->hpk_fid, &car->car_hai->hai_fid))) {
1682                 CERROR("%s: Progress on "DFID" for cookie %#llx"
1683                        " does not match request FID "DFID" nor data FID "
1684                        DFID"\n",
1685                        mdt_obd_name(mdt),
1686                        PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1687                        PFID(&car->car_hai->hai_fid),
1688                        PFID(&car->car_hai->hai_dfid));
1689                 GOTO(out, rc = -EINVAL);
1690         }
1691
1692         if (pgs->hpk_errval != 0 && !(pgs->hpk_flags & HP_FLAG_COMPLETED)) {
1693                 CERROR("%s: Progress on "DFID" for cookie %#llx action=%s"
1694                        " is not coherent (err=%d and not completed"
1695                        " (flags=%d))\n",
1696                        mdt_obd_name(mdt),
1697                        PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1698                        hsm_copytool_action2name(car->car_hai->hai_action),
1699                        pgs->hpk_errval, pgs->hpk_flags);
1700                 GOTO(out, rc = -EINVAL);
1701         }
1702
1703         /* now progress is valid */
1704
1705         /* we use a root like ucred */
1706         hsm_init_ucred(mdt_ucred(mti));
1707
1708         if (pgs->hpk_flags & HP_FLAG_COMPLETED) {
1709                 enum agent_req_status status;
1710                 struct hsm_record_update update;
1711                 int rc1;
1712
1713                 rc = hsm_cdt_request_completed(mti, pgs, car, &status);
1714
1715                 CDEBUG(D_HSM, "updating record: fid="DFID" cookie=%#llx action=%s "
1716                               "status=%s\n",
1717                        PFID(&pgs->hpk_fid), pgs->hpk_cookie,
1718                        hsm_copytool_action2name(car->car_hai->hai_action),
1719                        agent_req_status2name(status));
1720
1721                 /* update record first (LU-9075) */
1722                 update.cookie = pgs->hpk_cookie;
1723                 update.status = status;
1724
1725                 rc1 = mdt_agent_record_update(mti, &update, 1);
1726                 if (rc1)
1727                         CERROR("%s: mdt_agent_record_update() failed,"
1728                                " rc=%d, cannot update status to %s"
1729                                " for cookie %#llx\n",
1730                                mdt_obd_name(mdt), rc1,
1731                                agent_req_status2name(status),
1732                                pgs->hpk_cookie);
1733                 rc = (rc != 0 ? rc : rc1);
1734
1735                 /* then remove request from memory list (LU-9075) */
1736                 mdt_cdt_remove_request(cdt, pgs->hpk_cookie);
1737
1738                 /* ct has completed a request, so a slot is available,
1739                  * signal the coordinator to find new work */
1740                 mdt_hsm_cdt_event(cdt);
1741         } else {
1742                 /* if copytool send a progress on a canceled request
1743                  * we inform copytool it should stop
1744                  */
1745                 if (car->car_canceled == 1)
1746                         rc = -ECANCELED;
1747         }
1748         GOTO(out, rc);
1749
1750 out:
1751         /* remove ref got from mdt_cdt_update_request() */
1752         mdt_cdt_put_request(car);
1753
1754         return rc;
1755 }
1756
1757
1758 /**
1759  *  llog_cat_process() callback, used to:
1760  *  - purge all requests
1761  * \param env [IN] environment
1762  * \param llh [IN] llog handle
1763  * \param hdr [IN] llog record
1764  * \param data [IN] cb data = struct mdt_thread_info
1765  * \retval 0 success
1766  * \retval -ve failure
1767  */
1768 static int mdt_cancel_all_cb(const struct lu_env *env,
1769                              struct llog_handle *llh,
1770                              struct llog_rec_hdr *hdr, void *data)
1771 {
1772         struct llog_agent_req_rec *larr = (struct llog_agent_req_rec *)hdr;
1773         struct hsm_action_item *hai = &larr->arr_hai;
1774         struct mdt_thread_info  *mti = data;
1775         struct coordinator *cdt = &mti->mti_mdt->mdt_coordinator;
1776         int rc;
1777         ENTRY;
1778
1779         if (larr->arr_status != ARS_WAITING &&
1780             larr->arr_status != ARS_STARTED)
1781                 RETURN(0);
1782
1783         /* Unlock the EX layout lock */
1784         if (hai->hai_action == HSMA_RESTORE)
1785                 cdt_restore_handle_del(mti, cdt, &hai->hai_fid);
1786
1787         larr->arr_status = ARS_CANCELED;
1788         larr->arr_req_change = ktime_get_real_seconds();
1789         rc = llog_write(env, llh, hdr, hdr->lrh_index);
1790         if (rc < 0) {
1791                 CERROR("%s: cannot update agent log: rc = %d\n",
1792                        mdt_obd_name(mti->mti_mdt), rc);
1793                 rc = LLOG_DEL_RECORD;
1794         }
1795
1796         RETURN(rc);
1797 }
1798
1799 /**
1800  * cancel all actions
1801  * \param obd [IN] MDT device
1802  */
1803 static int hsm_cancel_all_actions(struct mdt_device *mdt)
1804 {
1805         struct lu_env                    env;
1806         struct lu_context                session;
1807         struct mdt_thread_info          *mti;
1808         struct coordinator              *cdt = &mdt->mdt_coordinator;
1809         struct cdt_agent_req            *car;
1810         struct hsm_action_list          *hal = NULL;
1811         struct hsm_action_item          *hai;
1812         int                              hal_sz = 0, hal_len, rc;
1813         enum cdt_states                  old_state;
1814         ENTRY;
1815
1816         rc = lu_env_init(&env, LCT_MD_THREAD);
1817         if (rc < 0)
1818                 RETURN(rc);
1819
1820         /* for mdt_ucred(), lu_ucred stored in lu_ucred_key */
1821         rc = lu_context_init(&session, LCT_SERVER_SESSION);
1822         if (rc < 0)
1823                 GOTO(out_env, rc);
1824
1825         lu_context_enter(&session);
1826         env.le_ses = &session;
1827
1828         mti = lu_context_key_get(&env.le_ctx, &mdt_thread_key);
1829         LASSERT(mti != NULL);
1830
1831         mti->mti_env = &env;
1832         mti->mti_mdt = mdt;
1833
1834         hsm_init_ucred(mdt_ucred(mti));
1835
1836         mutex_lock(&cdt->cdt_state_lock);
1837         old_state = cdt->cdt_state;
1838
1839         /* disable coordinator */
1840         rc = set_cdt_state_locked(cdt, CDT_DISABLE);
1841         if (rc)
1842                 GOTO(out_cdt_state_unlock, rc);
1843
1844         /* send cancel to all running requests */
1845         down_read(&cdt->cdt_request_lock);
1846         list_for_each_entry(car, &cdt->cdt_request_list, car_request_list) {
1847                 mdt_cdt_get_request(car);
1848                 /* request is not yet removed from list, it will be done
1849                  * when copytool will return progress
1850                  */
1851
1852                 if (car->car_hai->hai_action == HSMA_CANCEL) {
1853                         mdt_cdt_put_request(car);
1854                         continue;
1855                 }
1856
1857                 /* needed size */
1858                 hal_len = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN + 1) +
1859                           cfs_size_round(car->car_hai->hai_len);
1860
1861                 if (hal_len > hal_sz && hal_sz > 0) {
1862                         /* not enough room, free old buffer */
1863                         OBD_FREE(hal, hal_sz);
1864                         hal = NULL;
1865                 }
1866
1867                 /* empty buffer, allocate one */
1868                 if (hal == NULL) {
1869                         hal_sz = hal_len;
1870                         OBD_ALLOC(hal, hal_sz);
1871                         if (hal == NULL) {
1872                                 mdt_cdt_put_request(car);
1873                                 up_read(&cdt->cdt_request_lock);
1874                                 GOTO(out_cdt_state, rc = -ENOMEM);
1875                         }
1876                 }
1877
1878                 hal->hal_version = HAL_VERSION;
1879                 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(mdt),
1880                                 MTI_NAME_MAXLEN);
1881                 hal->hal_fsname[MTI_NAME_MAXLEN] = '\0';
1882                 hal->hal_archive_id = car->car_archive_id;
1883                 hal->hal_flags = car->car_flags;
1884                 hal->hal_count = 0;
1885
1886                 hai = hai_first(hal);
1887                 memcpy(hai, car->car_hai, car->car_hai->hai_len);
1888                 hai->hai_action = HSMA_CANCEL;
1889                 hal->hal_count = 1;
1890
1891                 /* it is possible to safely call mdt_hsm_agent_send()
1892                  * (ie without a deadlock on cdt_request_lock), because the
1893                  * write lock is taken only if we are not in purge mode
1894                  * (mdt_hsm_agent_send() does not call mdt_cdt_add_request()
1895                  *   nor mdt_cdt_remove_request())
1896                  */
1897                 /* no conflict with cdt thread because cdt is disable and we
1898                  * have the request lock */
1899                 mdt_hsm_agent_send(mti, hal, 1);
1900
1901                 mdt_cdt_put_request(car);
1902         }
1903         up_read(&cdt->cdt_request_lock);
1904
1905         if (hal != NULL)
1906                 OBD_FREE(hal, hal_sz);
1907
1908         /* cancel all on-disk records */
1909         rc = cdt_llog_process(mti->mti_env, mti->mti_mdt, mdt_cancel_all_cb,
1910                               (void *)mti, 0, 0, WRITE);
1911 out_cdt_state:
1912         /* Enable coordinator, unless the coordinator was stopping. */
1913         set_cdt_state_locked(cdt, old_state);
1914 out_cdt_state_unlock:
1915         mutex_unlock(&cdt->cdt_state_lock);
1916
1917         lu_context_exit(&session);
1918         lu_context_fini(&session);
1919 out_env:
1920         lu_env_fini(&env);
1921
1922         RETURN(rc);
1923 }
1924
1925 /**
1926  * check if a request is compatible with file status
1927  * \param hai [IN] request description
1928  * \param archive_id [IN] request archive id
1929  * \param rq_flags [IN] request flags
1930  * \param hsm [IN] file HSM metadata
1931  * \retval boolean
1932  */
1933 bool mdt_hsm_is_action_compat(const struct hsm_action_item *hai,
1934                               u32 archive_id, u64 rq_flags,
1935                               const struct md_hsm *hsm)
1936 {
1937         int      is_compat = false;
1938         int      hsm_flags;
1939         ENTRY;
1940
1941         hsm_flags = hsm->mh_flags;
1942         switch (hai->hai_action) {
1943         case HSMA_ARCHIVE:
1944                 if (!(hsm_flags & HS_NOARCHIVE) &&
1945                     (hsm_flags & HS_DIRTY || !(hsm_flags & HS_ARCHIVED)))
1946                         is_compat = true;
1947
1948                 if (hsm_flags & HS_EXISTS &&
1949                     archive_id != 0 &&
1950                     archive_id != hsm->mh_arch_id)
1951                         is_compat = false;
1952
1953                 break;
1954         case HSMA_RESTORE:
1955                 if (!(hsm_flags & HS_DIRTY) && (hsm_flags & HS_RELEASED) &&
1956                     hsm_flags & HS_ARCHIVED && !(hsm_flags & HS_LOST))
1957                         is_compat = true;
1958                 break;
1959         case HSMA_REMOVE:
1960                 if (!(hsm_flags & HS_RELEASED) &&
1961                     (hsm_flags & (HS_ARCHIVED | HS_EXISTS)))
1962                         is_compat = true;
1963                 break;
1964         case HSMA_CANCEL:
1965                 is_compat = true;
1966                 break;
1967         }
1968         CDEBUG(D_HSM, "fid="DFID" action=%s flags=%#llx"
1969                       " extent=%#llx-%#llx hsm_flags=%.8X %s\n",
1970                       PFID(&hai->hai_fid),
1971                       hsm_copytool_action2name(hai->hai_action), rq_flags,
1972                       hai->hai_extent.offset, hai->hai_extent.length,
1973                       hsm->mh_flags,
1974                       (is_compat ? "compatible" : "uncompatible"));
1975
1976         RETURN(is_compat);
1977 }
1978
1979 /*
1980  * sysfs interface used to get/set HSM behaviour (cdt->cdt_policy)
1981  */
1982 static const struct {
1983         __u64            bit;
1984         char            *name;
1985         char            *nickname;
1986 } hsm_policy_names[] = {
1987         { CDT_NONBLOCKING_RESTORE,      "NonBlockingRestore",   "NBR"},
1988         { CDT_NORETRY_ACTION,           "NoRetryAction",        "NRA"},
1989         { 0 },
1990 };
1991
1992 /**
1993  * convert a policy name to a bit
1994  * \param name [IN] policy name
1995  * \retval 0 unknown
1996  * \retval   policy bit
1997  */
1998 static __u64 hsm_policy_str2bit(const char *name)
1999 {
2000         int      i;
2001
2002         for (i = 0; hsm_policy_names[i].bit != 0; i++)
2003                 if (strcmp(hsm_policy_names[i].nickname, name) == 0 ||
2004                     strcmp(hsm_policy_names[i].name, name) == 0)
2005                         return hsm_policy_names[i].bit;
2006         return 0;
2007 }
2008
2009 /**
2010  * convert a policy bit field to a string
2011  * \param mask [IN] policy bit field
2012  * \param hexa [IN] print mask before bit names
2013  * \param buffer [OUT] string
2014  * \param count [IN] size of buffer
2015  */
2016 static void hsm_policy_bit2str(struct seq_file *m, const __u64 mask,
2017                                 const bool hexa)
2018 {
2019         int      i, j;
2020         __u64    bit;
2021         ENTRY;
2022
2023         if (hexa)
2024                 seq_printf(m, "(%#llx) ", mask);
2025
2026         for (i = 0; i < CDT_POLICY_SHIFT_COUNT; i++) {
2027                 bit = (1ULL << i);
2028
2029                 for (j = 0; hsm_policy_names[j].bit != 0; j++) {
2030                         if (hsm_policy_names[j].bit == bit)
2031                                 break;
2032                 }
2033                 if (bit & mask)
2034                         seq_printf(m, "[%s] ", hsm_policy_names[j].name);
2035                 else
2036                         seq_printf(m, "%s ", hsm_policy_names[j].name);
2037         }
2038         /* remove last ' ' */
2039         m->count--;
2040         seq_putc(m, '\n');
2041 }
2042
2043 /* methods to read/write HSM policy flags */
2044 static int mdt_hsm_policy_seq_show(struct seq_file *m, void *data)
2045 {
2046         struct mdt_device       *mdt = m->private;
2047         struct coordinator      *cdt = &mdt->mdt_coordinator;
2048         ENTRY;
2049
2050         hsm_policy_bit2str(m, cdt->cdt_policy, false);
2051         RETURN(0);
2052 }
2053
2054 static ssize_t
2055 mdt_hsm_policy_seq_write(struct file *file, const char __user *buffer,
2056                          size_t count, loff_t *off)
2057 {
2058         struct seq_file         *m = file->private_data;
2059         struct mdt_device       *mdt = m->private;
2060         struct coordinator      *cdt = &mdt->mdt_coordinator;
2061         char                    *start, *token, sign;
2062         char                    *buf;
2063         __u64                    policy;
2064         __u64                    add_mask, remove_mask, set_mask;
2065         int                      rc;
2066         ENTRY;
2067
2068         if (count + 1 > PAGE_SIZE)
2069                 RETURN(-EINVAL);
2070
2071         OBD_ALLOC(buf, count + 1);
2072         if (buf == NULL)
2073                 RETURN(-ENOMEM);
2074
2075         if (copy_from_user(buf, buffer, count))
2076                 GOTO(out, rc = -EFAULT);
2077
2078         buf[count] = '\0';
2079
2080         start = buf;
2081         CDEBUG(D_HSM, "%s: receive new policy: '%s'\n", mdt_obd_name(mdt),
2082                start);
2083
2084         add_mask = remove_mask = set_mask = 0;
2085         do {
2086                 token = strsep(&start, "\n ");
2087                 sign = *token;
2088
2089                 if (sign == '\0')
2090                         continue;
2091
2092                 if (sign == '-' || sign == '+')
2093                         token++;
2094
2095                 policy = hsm_policy_str2bit(token);
2096                 if (policy == 0) {
2097                         CWARN("%s: '%s' is unknown, "
2098                               "supported policies are:\n", mdt_obd_name(mdt),
2099                               token);
2100                         hsm_policy_bit2str(m, 0, false);
2101                         GOTO(out, rc = -EINVAL);
2102                 }
2103                 switch (sign) {
2104                 case '-':
2105                         remove_mask |= policy;
2106                         break;
2107                 case '+':
2108                         add_mask |= policy;
2109                         break;
2110                 default:
2111                         set_mask |= policy;
2112                         break;
2113                 }
2114
2115         } while (start != NULL);
2116
2117         CDEBUG(D_HSM, "%s: new policy: rm=%#llx add=%#llx set=%#llx\n",
2118                mdt_obd_name(mdt), remove_mask, add_mask, set_mask);
2119
2120         /* if no sign in all string, it is a clear and set
2121          * if some sign found, all unsigned are converted
2122          * to add
2123          * P1 P2 = set to P1 and P2
2124          * P1 -P2 = add P1 clear P2 same as +P1 -P2
2125          */
2126         if (remove_mask == 0 && add_mask == 0) {
2127                 cdt->cdt_policy = set_mask;
2128         } else {
2129                 cdt->cdt_policy |= set_mask | add_mask;
2130                 cdt->cdt_policy &= ~remove_mask;
2131         }
2132
2133         GOTO(out, rc = count);
2134
2135 out:
2136         OBD_FREE(buf, count + 1);
2137         RETURN(rc);
2138 }
2139 LDEBUGFS_SEQ_FOPS(mdt_hsm_policy);
2140
2141 ssize_t loop_period_show(struct kobject *kobj, struct attribute *attr,
2142                          char *buf)
2143 {
2144         struct coordinator *cdt = container_of(kobj, struct coordinator,
2145                                                cdt_hsm_kobj);
2146
2147         return scnprintf(buf, PAGE_SIZE, "%u\n", cdt->cdt_loop_period);
2148 }
2149
2150 ssize_t loop_period_store(struct kobject *kobj, struct attribute *attr,
2151                           const char *buffer, size_t count)
2152 {
2153         struct coordinator *cdt = container_of(kobj, struct coordinator,
2154                                                cdt_hsm_kobj);
2155         unsigned int val;
2156         int rc;
2157
2158         rc = kstrtouint(buffer, 0, &val);
2159         if (rc)
2160                 return rc;
2161
2162         if (val != 0)
2163                 cdt->cdt_loop_period = val;
2164
2165         return val ? count : -EINVAL;
2166 }
2167 LUSTRE_RW_ATTR(loop_period);
2168
2169 ssize_t grace_delay_show(struct kobject *kobj, struct attribute *attr,
2170                          char *buf)
2171 {
2172         struct coordinator *cdt = container_of(kobj, struct coordinator,
2173                                                cdt_hsm_kobj);
2174
2175         return scnprintf(buf, PAGE_SIZE, "%u\n", cdt->cdt_grace_delay);
2176 }
2177
2178 ssize_t grace_delay_store(struct kobject *kobj, struct attribute *attr,
2179                           const char *buffer, size_t count)
2180 {
2181         struct coordinator *cdt = container_of(kobj, struct coordinator,
2182                                                cdt_hsm_kobj);
2183         unsigned int val;
2184         int rc;
2185
2186         rc = kstrtouint(buffer, 0, &val);
2187         if (rc)
2188                 return rc;
2189
2190         if (val != 0)
2191                 cdt->cdt_grace_delay = val;
2192
2193         return val ? count : -EINVAL;
2194 }
2195 LUSTRE_RW_ATTR(grace_delay);
2196
2197 ssize_t active_request_timeout_show(struct kobject *kobj,
2198                                     struct attribute *attr,
2199                                     char *buf)
2200 {
2201         struct coordinator *cdt = container_of(kobj, struct coordinator,
2202                                                cdt_hsm_kobj);
2203
2204         return scnprintf(buf, PAGE_SIZE, "%d\n", cdt->cdt_active_req_timeout);
2205 }
2206
2207 ssize_t active_request_timeout_store(struct kobject *kobj,
2208                                      struct attribute *attr,
2209                                      const char *buffer, size_t count)
2210 {
2211         struct coordinator *cdt = container_of(kobj, struct coordinator,
2212                                                cdt_hsm_kobj);
2213         unsigned int val;
2214         int rc;
2215
2216         rc = kstrtouint(buffer, 0, &val);
2217         if (rc)
2218                 return rc;
2219
2220         if (val != 0)
2221                 cdt->cdt_active_req_timeout = val;
2222
2223         return val ? count : -EINVAL;
2224 }
2225 LUSTRE_RW_ATTR(active_request_timeout);
2226
2227 ssize_t max_requests_show(struct kobject *kobj, struct attribute *attr,
2228                           char *buf)
2229 {
2230         struct coordinator *cdt = container_of(kobj, struct coordinator,
2231                                                cdt_hsm_kobj);
2232
2233         return scnprintf(buf, PAGE_SIZE, "%llu\n", cdt->cdt_max_requests);
2234 }
2235
2236 ssize_t max_requests_store(struct kobject *kobj, struct attribute *attr,
2237                            const char *buffer, size_t count)
2238 {
2239         struct coordinator *cdt = container_of(kobj, struct coordinator,
2240                                                cdt_hsm_kobj);
2241         unsigned long long val;
2242         int rc;
2243
2244         rc = kstrtoull(buffer, 0, &val);
2245         if (rc)
2246                 return rc;
2247
2248         if (val != 0)
2249                 cdt->cdt_max_requests = val;
2250
2251         return val ? count : -EINVAL;
2252 }
2253 LUSTRE_RW_ATTR(max_requests);
2254
2255 ssize_t default_archive_id_show(struct kobject *kobj, struct attribute *attr,
2256                                 char *buf)
2257 {
2258         struct coordinator *cdt = container_of(kobj, struct coordinator,
2259                                                cdt_hsm_kobj);
2260
2261         return scnprintf(buf, PAGE_SIZE, "%u\n", cdt->cdt_default_archive_id);
2262 }
2263
2264 ssize_t default_archive_id_store(struct kobject *kobj, struct attribute *attr,
2265                                  const char *buffer, size_t count)
2266 {
2267         struct coordinator *cdt = container_of(kobj, struct coordinator,
2268                                                cdt_hsm_kobj);
2269         unsigned int val;
2270         int rc;
2271
2272         rc = kstrtouint(buffer, 0, &val);
2273         if (rc)
2274                 return rc;
2275
2276         if (val != 0)
2277                 cdt->cdt_default_archive_id = val;
2278
2279         return val ? count : -EINVAL;
2280 }
2281 LUSTRE_RW_ATTR(default_archive_id);
2282
2283 /*
2284  * procfs write method for MDT/hsm_control
2285  * proc entry is in mdt directory so data is mdt obd_device pointer
2286  */
2287 #define CDT_ENABLE_CMD   "enabled"
2288 #define CDT_STOP_CMD     "shutdown"
2289 #define CDT_DISABLE_CMD  "disabled"
2290 #define CDT_PURGE_CMD    "purge"
2291 #define CDT_HELP_CMD     "help"
2292 #define CDT_MAX_CMD_LEN  10
2293
2294 ssize_t hsm_control_store(struct kobject *kobj, struct attribute *attr,
2295                           const char *buffer, size_t count)
2296 {
2297         struct obd_device *obd = container_of(kobj, struct obd_device,
2298                                               obd_kset.kobj);
2299         struct mdt_device *mdt = mdt_dev(obd->obd_lu_dev);
2300         struct coordinator *cdt = &(mdt->mdt_coordinator);
2301         int usage = 0;
2302         int rc = 0;
2303
2304         if (count == 0 || count >= CDT_MAX_CMD_LEN)
2305                 return -EINVAL;
2306
2307         if (strncmp(buffer, CDT_ENABLE_CMD, strlen(CDT_ENABLE_CMD)) == 0) {
2308                 if (cdt->cdt_state == CDT_DISABLE) {
2309                         rc = set_cdt_state(cdt, CDT_RUNNING);
2310                         mdt_hsm_cdt_event(cdt);
2311                         wake_up(&cdt->cdt_waitq);
2312                 } else if (cdt->cdt_state == CDT_RUNNING) {
2313                         rc = 0;
2314                 } else {
2315                         rc = mdt_hsm_cdt_start(mdt);
2316                 }
2317         } else if (strncmp(buffer, CDT_STOP_CMD, strlen(CDT_STOP_CMD)) == 0) {
2318                 if (cdt->cdt_state == CDT_STOPPING) {
2319                         CERROR("%s: Coordinator is already stopping\n",
2320                                mdt_obd_name(mdt));
2321                         rc = -EALREADY;
2322                 } else if (cdt->cdt_state == CDT_STOPPED) {
2323                         rc = 0;
2324                 } else {
2325                         rc = mdt_hsm_cdt_stop(mdt);
2326                 }
2327         } else if (strncmp(buffer, CDT_DISABLE_CMD,
2328                            strlen(CDT_DISABLE_CMD)) == 0) {
2329                 if ((cdt->cdt_state == CDT_STOPPING) ||
2330                     (cdt->cdt_state == CDT_STOPPED)) {
2331                         /* exit gracefully if coordinator is being stopped
2332                          * or stopped already.
2333                          */
2334                         rc = 0;
2335                 } else {
2336                         rc = set_cdt_state(cdt, CDT_DISABLE);
2337                 }
2338         } else if (strncmp(buffer, CDT_PURGE_CMD,
2339                            strlen(CDT_PURGE_CMD)) == 0) {
2340                 rc = hsm_cancel_all_actions(mdt);
2341         } else if (strncmp(buffer, CDT_HELP_CMD,
2342                            strlen(CDT_HELP_CMD)) == 0) {
2343                 usage = 1;
2344         } else {
2345                 usage = 1;
2346                 rc = -EINVAL;
2347         }
2348
2349         if (usage == 1)
2350                 CERROR("%s: Valid coordinator control commands are: "
2351                        "%s %s %s %s %s\n", mdt_obd_name(mdt),
2352                        CDT_ENABLE_CMD, CDT_STOP_CMD, CDT_DISABLE_CMD,
2353                        CDT_PURGE_CMD, CDT_HELP_CMD);
2354
2355         if (rc)
2356                 RETURN(rc);
2357
2358         RETURN(count);
2359 }
2360
2361 ssize_t hsm_control_show(struct kobject *kobj, struct attribute *attr,
2362                          char *buf)
2363 {
2364         struct obd_device *obd = container_of(kobj, struct obd_device,
2365                                               obd_kset.kobj);
2366         struct coordinator *cdt;
2367
2368         cdt = &(mdt_dev(obd->obd_lu_dev)->mdt_coordinator);
2369
2370         return scnprintf(buf, PAGE_SIZE, "%s\n",
2371                          cdt_mdt_state2str(cdt->cdt_state));
2372 }
2373
2374 static int
2375 mdt_hsm_request_mask_show(struct seq_file *m, __u64 mask)
2376 {
2377         bool first = true;
2378         int i;
2379         ENTRY;
2380
2381         for (i = 0; i < 8 * sizeof(mask); i++) {
2382                 if (mask & (1UL << i)) {
2383                         seq_printf(m, "%s%s", first ? "" : " ",
2384                                    hsm_copytool_action2name(i));
2385                         first = false;
2386                 }
2387         }
2388         seq_putc(m, '\n');
2389
2390         RETURN(0);
2391 }
2392
2393 static int
2394 mdt_hsm_user_request_mask_seq_show(struct seq_file *m, void *data)
2395 {
2396         struct mdt_device *mdt = m->private;
2397         struct coordinator *cdt = &mdt->mdt_coordinator;
2398
2399         return mdt_hsm_request_mask_show(m, cdt->cdt_user_request_mask);
2400 }
2401
2402 static int
2403 mdt_hsm_group_request_mask_seq_show(struct seq_file *m, void *data)
2404 {
2405         struct mdt_device *mdt = m->private;
2406         struct coordinator *cdt = &mdt->mdt_coordinator;
2407
2408         return mdt_hsm_request_mask_show(m, cdt->cdt_group_request_mask);
2409 }
2410
2411 static int
2412 mdt_hsm_other_request_mask_seq_show(struct seq_file *m, void *data)
2413 {
2414         struct mdt_device *mdt = m->private;
2415         struct coordinator *cdt = &mdt->mdt_coordinator;
2416
2417         return mdt_hsm_request_mask_show(m, cdt->cdt_other_request_mask);
2418 }
2419
2420 static inline enum hsm_copytool_action
2421 hsm_copytool_name2action(const char *name)
2422 {
2423         if (strcasecmp(name, "NOOP") == 0)
2424                 return HSMA_NONE;
2425         else if (strcasecmp(name, "ARCHIVE") == 0)
2426                 return HSMA_ARCHIVE;
2427         else if (strcasecmp(name, "RESTORE") == 0)
2428                 return HSMA_RESTORE;
2429         else if (strcasecmp(name, "REMOVE") == 0)
2430                 return HSMA_REMOVE;
2431         else if (strcasecmp(name, "CANCEL") == 0)
2432                 return HSMA_CANCEL;
2433         else
2434                 return -1;
2435 }
2436
2437 static ssize_t
2438 mdt_write_hsm_request_mask(struct file *file, const char __user *user_buf,
2439                             size_t user_count, __u64 *mask)
2440 {
2441         char *buf, *pos, *name;
2442         size_t buf_size;
2443         __u64 new_mask = 0;
2444         int rc;
2445         ENTRY;
2446
2447         if (!(user_count < 4096))
2448                 RETURN(-ENOMEM);
2449
2450         buf_size = user_count + 1;
2451
2452         OBD_ALLOC(buf, buf_size);
2453         if (buf == NULL)
2454                 RETURN(-ENOMEM);
2455
2456         if (copy_from_user(buf, user_buf, buf_size - 1))
2457                 GOTO(out, rc = -EFAULT);
2458
2459         buf[buf_size - 1] = '\0';
2460
2461         pos = buf;
2462         while ((name = strsep(&pos, " \t\v\n")) != NULL) {
2463                 int action;
2464
2465                 if (*name == '\0')
2466                         continue;
2467
2468                 action = hsm_copytool_name2action(name);
2469                 if (action < 0)
2470                         GOTO(out, rc = -EINVAL);
2471
2472                 new_mask |= (1UL << action);
2473         }
2474
2475         *mask = new_mask;
2476         rc = user_count;
2477 out:
2478         OBD_FREE(buf, buf_size);
2479
2480         RETURN(rc);
2481 }
2482
2483 static ssize_t
2484 mdt_hsm_user_request_mask_seq_write(struct file *file, const char __user *buf,
2485                                         size_t count, loff_t *off)
2486 {
2487         struct seq_file         *m = file->private_data;
2488         struct mdt_device       *mdt = m->private;
2489         struct coordinator *cdt = &mdt->mdt_coordinator;
2490
2491         return mdt_write_hsm_request_mask(file, buf, count,
2492                                            &cdt->cdt_user_request_mask);
2493 }
2494
2495 static ssize_t
2496 mdt_hsm_group_request_mask_seq_write(struct file *file, const char __user *buf,
2497                                         size_t count, loff_t *off)
2498 {
2499         struct seq_file         *m = file->private_data;
2500         struct mdt_device       *mdt = m->private;
2501         struct coordinator      *cdt = &mdt->mdt_coordinator;
2502
2503         return mdt_write_hsm_request_mask(file, buf, count,
2504                                            &cdt->cdt_group_request_mask);
2505 }
2506
2507 static ssize_t
2508 mdt_hsm_other_request_mask_seq_write(struct file *file, const char __user *buf,
2509                                         size_t count, loff_t *off)
2510 {
2511         struct seq_file         *m = file->private_data;
2512         struct mdt_device       *mdt = m->private;
2513         struct coordinator      *cdt = &mdt->mdt_coordinator;
2514
2515         return mdt_write_hsm_request_mask(file, buf, count,
2516                                            &cdt->cdt_other_request_mask);
2517 }
2518
2519 static ssize_t remove_archive_on_last_unlink_show(struct kobject *kobj,
2520                                                   struct attribute *attr,
2521                                                   char *buf)
2522 {
2523         struct coordinator *cdt = container_of(kobj, struct coordinator,
2524                                                cdt_hsm_kobj);
2525
2526         return scnprintf(buf, PAGE_SIZE, "%u\n",
2527                          cdt->cdt_remove_archive_on_last_unlink);
2528 }
2529
2530 static ssize_t remove_archive_on_last_unlink_store(struct kobject *kobj,
2531                                                    struct attribute *attr,
2532                                                    const char *buffer,
2533                                                    size_t count)
2534 {
2535         struct coordinator *cdt = container_of(kobj, struct coordinator,
2536                                                cdt_hsm_kobj);
2537         bool val;
2538         int rc;
2539
2540         rc = kstrtobool(buffer, &val);
2541         if (rc < 0)
2542                 return rc;
2543
2544         cdt->cdt_remove_archive_on_last_unlink = val;
2545         return count;
2546 }
2547 LUSTRE_RW_ATTR(remove_archive_on_last_unlink);
2548
2549 LDEBUGFS_SEQ_FOPS(mdt_hsm_user_request_mask);
2550 LDEBUGFS_SEQ_FOPS(mdt_hsm_group_request_mask);
2551 LDEBUGFS_SEQ_FOPS(mdt_hsm_other_request_mask);
2552
2553 /* Read-only sysfs files for request counters */
2554 static ssize_t archive_count_show(struct kobject *kobj, struct attribute *attr,
2555                                   char *buf)
2556 {
2557         struct coordinator *cdt = container_of(kobj, struct coordinator,
2558                                                cdt_hsm_kobj);
2559
2560         return scnprintf(buf, PAGE_SIZE, "%d\n",
2561                          atomic_read(&cdt->cdt_archive_count));
2562 }
2563 LUSTRE_RO_ATTR(archive_count);
2564
2565 static ssize_t restore_count_show(struct kobject *kobj, struct attribute *attr,
2566                                   char *buf)
2567 {
2568         struct coordinator *cdt = container_of(kobj, struct coordinator,
2569                                                cdt_hsm_kobj);
2570
2571         return scnprintf(buf, PAGE_SIZE, "%d\n",
2572                          atomic_read(&cdt->cdt_restore_count));
2573 }
2574 LUSTRE_RO_ATTR(restore_count);
2575
2576 static ssize_t remove_count_show(struct kobject *kobj, struct attribute *attr,
2577                                  char *buf)
2578 {
2579         struct coordinator *cdt = container_of(kobj, struct coordinator,
2580                                                cdt_hsm_kobj);
2581
2582         return scnprintf(buf, PAGE_SIZE, "%d\n",
2583                          atomic_read(&cdt->cdt_remove_count));
2584 }
2585 LUSTRE_RO_ATTR(remove_count);
2586
2587 static struct ldebugfs_vars ldebugfs_mdt_hsm_vars[] = {
2588         { .name =       "agents",
2589           .fops =       &mdt_hsm_agent_fops                     },
2590         { .name =       "actions",
2591           .fops =       &mdt_hsm_actions_fops,
2592           .proc_mode =  0444                                    },
2593         { .name =       "policy",
2594           .fops =       &mdt_hsm_policy_fops                    },
2595         { .name =       "active_requests",
2596           .fops =       &mdt_hsm_active_requests_fops           },
2597         { .name =       "user_request_mask",
2598           .fops =       &mdt_hsm_user_request_mask_fops,        },
2599         { .name =       "group_request_mask",
2600           .fops =       &mdt_hsm_group_request_mask_fops,       },
2601         { .name =       "other_request_mask",
2602           .fops =       &mdt_hsm_other_request_mask_fops,       },
2603         { 0 }
2604 };
2605
2606 static struct attribute *hsm_attrs[] = {
2607         &lustre_attr_loop_period.attr,
2608         &lustre_attr_grace_delay.attr,
2609         &lustre_attr_active_request_timeout.attr,
2610         &lustre_attr_max_requests.attr,
2611         &lustre_attr_default_archive_id.attr,
2612         &lustre_attr_remove_archive_on_last_unlink.attr,
2613         &lustre_attr_archive_count.attr,
2614         &lustre_attr_restore_count.attr,
2615         &lustre_attr_remove_count.attr,
2616         NULL,
2617 };
2618
2619 static void hsm_kobj_release(struct kobject *kobj)
2620 {
2621         struct coordinator *cdt = container_of(kobj, struct coordinator,
2622                                                cdt_hsm_kobj);
2623
2624         debugfs_remove_recursive(cdt->cdt_debugfs_dir);
2625         cdt->cdt_debugfs_dir = NULL;
2626
2627         complete(&cdt->cdt_kobj_unregister);
2628 }
2629
2630 static struct kobj_type hsm_ktype = {
2631         .default_attrs  = hsm_attrs,
2632         .sysfs_ops      = &lustre_sysfs_ops,
2633         .release        = hsm_kobj_release,
2634 };
2635
2636 /**
2637  * create sysfs entries for coordinator
2638  * \param mdt [IN]
2639  * \retval 0 success
2640  * \retval -ve failure
2641  */
2642 int hsm_cdt_tunables_init(struct mdt_device *mdt)
2643 {
2644         struct coordinator *cdt = &mdt->mdt_coordinator;
2645         struct obd_device *obd = mdt2obd_dev(mdt);
2646         int rc;
2647
2648         init_completion(&cdt->cdt_kobj_unregister);
2649         rc = kobject_init_and_add(&cdt->cdt_hsm_kobj, &hsm_ktype,
2650                                   &obd->obd_kset.kobj, "%s", "hsm");
2651         if (rc) {
2652                 kobject_put(&cdt->cdt_hsm_kobj);
2653                 return rc;
2654         }
2655
2656         /* init debugfs entries, failure is not critical */
2657         cdt->cdt_debugfs_dir = debugfs_create_dir("hsm",
2658                                                   obd->obd_debugfs_entry);
2659         ldebugfs_add_vars(cdt->cdt_debugfs_dir, ldebugfs_mdt_hsm_vars, mdt);
2660
2661         return 0;
2662 }
2663
2664 /**
2665  * remove sysfs entries for coordinator
2666  *
2667  * @mdt
2668  */
2669 void hsm_cdt_tunables_fini(struct mdt_device *mdt)
2670 {
2671         struct coordinator *cdt = &mdt->mdt_coordinator;
2672
2673         kobject_put(&cdt->cdt_hsm_kobj);
2674         wait_for_completion(&cdt->cdt_kobj_unregister);
2675 }