Whamcloud - gitweb
d4cf668b2bc9b2353be3893a19940a8b2df090ee
[fs/lustre-release.git] / lustre / mdt / mdt_hsm.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  * Use is subject to license terms.
26  *
27  * Copyright (c) 2012, 2015, Intel Corporation.
28  */
29 /*
30  * lustre/mdt/mdt_hsm.c
31  *
32  * Lustre Metadata Target (mdt) request handler
33  *
34  * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
35  * Author: JC Lafoucriere <jacques-charles.lafoucriere@cea.fr>
36  */
37
38 #define DEBUG_SUBSYSTEM S_MDS
39
40 #include "mdt_internal.h"
41
42 /* Max allocation to satisfy single HSM RPC. */
43 #define MDT_HSM_ALLOC_MAX (1 << 20)
44
45 #define MDT_HSM_ALLOC(ptr, size)                        \
46         do {                                            \
47                 if ((size) <= MDT_HSM_ALLOC_MAX)        \
48                         OBD_ALLOC_LARGE((ptr), (size)); \
49                 else                                    \
50                         (ptr) = NULL;                   \
51         } while (0)
52
53 #define MDT_HSM_FREE(ptr, size) OBD_FREE_LARGE((ptr), (size))
54
55 /**
56  * Update on-disk HSM attributes.
57  */
58 int mdt_hsm_attr_set(struct mdt_thread_info *info, struct mdt_object *obj,
59                      const struct md_hsm *mh)
60 {
61         struct md_object        *next = mdt_object_child(obj);
62         struct lu_buf           *buf = &info->mti_buf;
63         struct hsm_attrs        *attrs;
64         int                      rc;
65         ENTRY;
66
67         attrs = (struct hsm_attrs *)info->mti_xattr_buf;
68         CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*attrs));
69
70         /* pack HSM attributes */
71         lustre_hsm2buf(info->mti_xattr_buf, mh);
72
73         /* update HSM attributes */
74         buf->lb_buf = attrs;
75         buf->lb_len = sizeof(*attrs);
76         rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_HSM, 0);
77
78         RETURN(rc);
79 }
80
81 static inline bool mdt_hsm_is_admin(struct mdt_thread_info *info)
82 {
83         bool is_admin;
84         int rc;
85
86         if (info->mti_body == NULL)
87                 return false;
88
89         rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
90         if (rc < 0)
91                 return false;
92
93         is_admin = md_capable(mdt_ucred(info), CFS_CAP_SYS_ADMIN);
94
95         mdt_exit_ucred(info);
96
97         return is_admin;
98 }
99
100 /**
101  * Extract information coming from a copytool and asks coordinator to update
102  * a request status depending on the update content.
103  *
104  * Copytools could use this to report failure in their process.
105  *
106  * This is HSM_PROGRESS RPC handler.
107  */
108 int mdt_hsm_progress(struct tgt_session_info *tsi)
109 {
110         struct mdt_thread_info          *info;
111         struct hsm_progress_kernel      *hpk;
112         int                              rc;
113         ENTRY;
114
115         if (tsi->tsi_mdt_body == NULL)
116                 RETURN(-EPROTO);
117
118         hpk = req_capsule_client_get(tsi->tsi_pill, &RMF_MDS_HSM_PROGRESS);
119         if (hpk == NULL)
120                 RETURN(err_serious(-EPROTO));
121
122         hpk->hpk_errval = lustre_errno_ntoh(hpk->hpk_errval);
123
124         CDEBUG(D_HSM, "Progress on "DFID": len="LPU64" err=%d\n",
125                PFID(&hpk->hpk_fid), hpk->hpk_extent.length, hpk->hpk_errval);
126
127         if (hpk->hpk_errval)
128                 CDEBUG(D_HSM, "Copytool progress on "DFID" failed (%d); %s.\n",
129                        PFID(&hpk->hpk_fid), hpk->hpk_errval,
130                        hpk->hpk_flags & HP_FLAG_RETRY ? "will retry" : "fatal");
131
132         if (hpk->hpk_flags & HP_FLAG_COMPLETED)
133                 CDEBUG(D_HSM, "Finished "DFID" (%d) cancel cookie="LPX64"\n",
134                        PFID(&hpk->hpk_fid), hpk->hpk_errval, hpk->hpk_cookie);
135
136         info = tsi2mdt_info(tsi);
137         if (!mdt_hsm_is_admin(info))
138                 GOTO(out, rc = -EPERM);
139
140         rc = mdt_hsm_coordinator_update(info, hpk);
141 out:
142         mdt_thread_info_fini(info);
143         RETURN(rc);
144 }
145
146 int mdt_hsm_ct_register(struct tgt_session_info *tsi)
147 {
148         struct mdt_thread_info  *info;
149         __u32                   *archives;
150         int                      rc;
151         ENTRY;
152
153         archives = req_capsule_client_get(tsi->tsi_pill, &RMF_MDS_HSM_ARCHIVE);
154         if (archives == NULL)
155                 RETURN(err_serious(-EPROTO));
156
157         info = tsi2mdt_info(tsi);
158         if (!mdt_hsm_is_admin(info))
159                 GOTO(out, rc = -EPERM);
160
161         /* XXX: directly include this function here? */
162         rc = mdt_hsm_agent_register_mask(info, &tsi->tsi_exp->exp_client_uuid,
163                                          *archives);
164 out:
165         mdt_thread_info_fini(info);
166         RETURN(rc);
167 }
168
169 int mdt_hsm_ct_unregister(struct tgt_session_info *tsi)
170 {
171         struct mdt_thread_info  *info;
172         int                      rc;
173         ENTRY;
174
175         if (tsi->tsi_mdt_body == NULL)
176                 RETURN(-EPROTO);
177
178         info = tsi2mdt_info(tsi);
179         if (!mdt_hsm_is_admin(info))
180                 GOTO(out, rc = -EPERM);
181
182         /* XXX: directly include this function here? */
183         rc = mdt_hsm_agent_unregister(info, &tsi->tsi_exp->exp_client_uuid);
184 out:
185         mdt_thread_info_fini(info);
186         RETURN(rc);
187 }
188
189 /**
190  * Retrieve the current HSM flags, archive id and undergoing HSM requests for
191  * the fid provided in RPC body.
192  *
193  * Current requests are read from coordinator states.
194  *
195  * This is MDS_HSM_STATE_GET RPC handler.
196  */
197 int mdt_hsm_state_get(struct tgt_session_info *tsi)
198 {
199         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
200         struct mdt_object       *obj = info->mti_object;
201         struct md_attr          *ma  = &info->mti_attr;
202         struct hsm_user_state   *hus;
203         struct mdt_lock_handle  *lh;
204         int                      rc;
205         ENTRY;
206
207         if (info->mti_body == NULL || obj == NULL)
208                 GOTO(out, rc = -EPROTO);
209
210         /* Only valid if client is remote */
211         rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
212         if (rc < 0)
213                 GOTO(out, rc = err_serious(rc));
214
215         lh = &info->mti_lh[MDT_LH_CHILD];
216         mdt_lock_reg_init(lh, LCK_PR);
217         rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP);
218         if (rc < 0)
219                 GOTO(out_ucred, rc);
220
221         ma->ma_valid = 0;
222         ma->ma_need = MA_HSM;
223         rc = mdt_attr_get_complex(info, obj, ma);
224         if (rc)
225                 GOTO(out_unlock, rc);
226
227         hus = req_capsule_server_get(tsi->tsi_pill, &RMF_HSM_USER_STATE);
228         if (hus == NULL)
229                 GOTO(out_unlock, rc = -EPROTO);
230
231         /* Current HSM flags */
232         hus->hus_states = ma->ma_hsm.mh_flags;
233         hus->hus_archive_id = ma->ma_hsm.mh_arch_id;
234
235         EXIT;
236 out_unlock:
237         mdt_object_unlock(info, obj, lh, 1);
238 out_ucred:
239         mdt_exit_ucred(info);
240 out:
241         mdt_thread_info_fini(info);
242         return rc;
243 }
244
245 /**
246  * Change HSM state and archive number of a file.
247  *
248  * Archive number is changed iif the value is not 0.
249  * The new flagset that will be computed should result in a coherent state.
250  * This function checks that flags are compatible.
251  *
252  * This is MDS_HSM_STATE_SET RPC handler.
253  */
254 int mdt_hsm_state_set(struct tgt_session_info *tsi)
255 {
256         struct mdt_thread_info  *info = tsi2mdt_info(tsi);
257         struct mdt_object       *obj = info->mti_object;
258         struct md_attr          *ma = &info->mti_attr;
259         struct hsm_state_set    *hss;
260         struct mdt_lock_handle  *lh;
261         int                      rc;
262         __u64                    flags;
263         ENTRY;
264
265         hss = req_capsule_client_get(info->mti_pill, &RMF_HSM_STATE_SET);
266
267         if (info->mti_body == NULL || obj == NULL || hss == NULL)
268                 GOTO(out, rc = -EPROTO);
269
270         /* Only valid if client is remote */
271         rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
272         if (rc < 0)
273                 GOTO(out, rc = err_serious(rc));
274
275         lh = &info->mti_lh[MDT_LH_CHILD];
276         mdt_lock_reg_init(lh, LCK_PW);
277         rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP |
278                              MDS_INODELOCK_XATTR);
279         if (rc < 0)
280                 GOTO(out_ucred, rc);
281
282         /* Detect out-of range masks */
283         if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK) {
284                 CDEBUG(D_HSM, "Incompatible masks provided (set "LPX64
285                        ", clear "LPX64") vs supported set (%#x).\n",
286                        hss->hss_setmask, hss->hss_clearmask, HSM_FLAGS_MASK);
287                 GOTO(out_unlock, rc = -EINVAL);
288         }
289
290         /* Non-root users are forbidden to set or clear flags which are
291          * NOT defined in HSM_USER_MASK. */
292         if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
293             !md_capable(mdt_ucred(info), CFS_CAP_SYS_ADMIN)) {
294                 CDEBUG(D_HSM, "Incompatible masks provided (set "LPX64
295                        ", clear "LPX64") vs unprivileged set (%#x).\n",
296                        hss->hss_setmask, hss->hss_clearmask, HSM_USER_MASK);
297                 GOTO(out_unlock, rc = -EPERM);
298         }
299
300         /* Read current HSM info */
301         ma->ma_valid = 0;
302         ma->ma_need = MA_HSM;
303         rc = mdt_attr_get_complex(info, obj, ma);
304         if (rc)
305                 GOTO(out_unlock, rc);
306
307         /* Change HSM flags depending on provided masks */
308         if (hss->hss_valid & HSS_SETMASK)
309                 ma->ma_hsm.mh_flags |= hss->hss_setmask;
310         if (hss->hss_valid & HSS_CLEARMASK)
311                 ma->ma_hsm.mh_flags &= ~hss->hss_clearmask;
312
313         /* Change archive_id if provided. */
314         if (hss->hss_valid & HSS_ARCHIVE_ID) {
315                 if (!(ma->ma_hsm.mh_flags & HS_EXISTS)) {
316                         CDEBUG(D_HSM, "Could not set an archive number for "
317                                DFID "if HSM EXISTS flag is not set.\n",
318                                PFID(&info->mti_body->mbo_fid1));
319                         GOTO(out_unlock, rc);
320                 }
321
322                 /* Detect out-of range archive id */
323                 if (hss->hss_archive_id > LL_HSM_MAX_ARCHIVE) {
324                         CDEBUG(D_HSM, "archive id %u exceeds maximum %zu.\n",
325                                hss->hss_archive_id, LL_HSM_MAX_ARCHIVE);
326                         GOTO(out_unlock, rc = -EINVAL);
327                 }
328
329                 ma->ma_hsm.mh_arch_id = hss->hss_archive_id;
330         }
331
332         /* Check for inconsistant HSM flagset.
333          * DIRTY without EXISTS: no dirty if no archive was created.
334          * DIRTY and RELEASED: a dirty file could not be released.
335          * RELEASED without ARCHIVED: do not release a non-archived file.
336          * LOST without ARCHIVED: cannot lost a non-archived file.
337          */
338         flags = ma->ma_hsm.mh_flags;
339         if ((flags & HS_DIRTY    && !(flags & HS_EXISTS)) ||
340             (flags & HS_RELEASED && flags & HS_DIRTY) ||
341             (flags & HS_RELEASED && !(flags & HS_ARCHIVED)) ||
342             (flags & HS_LOST     && !(flags & HS_ARCHIVED))) {
343                 CDEBUG(D_HSM, "Incompatible flag change on "DFID
344                               "flags="LPX64"\n",
345                        PFID(&info->mti_body->mbo_fid1), flags);
346                 GOTO(out_unlock, rc = -EINVAL);
347         }
348
349         /* Save the modified flags */
350         rc = mdt_hsm_attr_set(info, obj, &ma->ma_hsm);
351         if (rc)
352                 GOTO(out_unlock, rc);
353
354         EXIT;
355
356 out_unlock:
357         mdt_object_unlock(info, obj, lh, 1);
358 out_ucred:
359         mdt_exit_ucred(info);
360 out:
361         mdt_thread_info_fini(info);
362         return rc;
363 }
364
365 /**
366  * Retrieve undergoing HSM requests for the fid provided in RPC body.
367  * Current requests are read from coordinator states.
368  *
369  * This is MDS_HSM_ACTION RPC handler.
370  */
371 int mdt_hsm_action(struct tgt_session_info *tsi)
372 {
373         struct mdt_thread_info          *info;
374         struct hsm_current_action       *hca;
375         struct hsm_action_list          *hal = NULL;
376         struct hsm_action_item          *hai;
377         int                              hal_size;
378         int                              rc;
379         ENTRY;
380
381         hca = req_capsule_server_get(tsi->tsi_pill,
382                                      &RMF_MDS_HSM_CURRENT_ACTION);
383         if (hca == NULL)
384                 RETURN(err_serious(-EPROTO));
385
386         if (tsi->tsi_mdt_body == NULL)
387                 RETURN(-EPROTO);
388
389         info = tsi2mdt_info(tsi);
390         /* Only valid if client is remote */
391         rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
392         if (rc)
393                 GOTO(out, rc = err_serious(rc));
394
395         /* Coordinator information */
396         hal_size = sizeof(*hal) +
397                    cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
398                    cfs_size_round(sizeof(*hai));
399
400         MDT_HSM_ALLOC(hal, hal_size);
401         if (hal == NULL)
402                 GOTO(out_ucred, rc = -ENOMEM);
403
404         hal->hal_version = HAL_VERSION;
405         hal->hal_archive_id = 0;
406         hal->hal_flags = 0;
407         obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
408                         MTI_NAME_MAXLEN);
409         hal->hal_count = 1;
410         hai = hai_first(hal);
411         hai->hai_action = HSMA_NONE;
412         hai->hai_cookie = 0;
413         hai->hai_gid = 0;
414         hai->hai_fid = info->mti_body->mbo_fid1;
415         hai->hai_len = sizeof(*hai);
416
417         rc = mdt_hsm_get_actions(info, hal);
418         if (rc)
419                 GOTO(out_free, rc);
420
421         /* cookie is used to give back request status */
422         if (hai->hai_cookie == 0)
423                 hca->hca_state = HPS_WAITING;
424         else
425                 hca->hca_state = HPS_RUNNING;
426
427         switch (hai->hai_action) {
428         case HSMA_NONE:
429                 hca->hca_action = HUA_NONE;
430                 break;
431         case HSMA_ARCHIVE:
432                 hca->hca_action = HUA_ARCHIVE;
433                 break;
434         case HSMA_RESTORE:
435                 hca->hca_action = HUA_RESTORE;
436                 break;
437         case HSMA_REMOVE:
438                 hca->hca_action = HUA_REMOVE;
439                 break;
440         case HSMA_CANCEL:
441                 hca->hca_action = HUA_CANCEL;
442                 break;
443         default:
444                 hca->hca_action = HUA_NONE;
445                 CERROR("%s: Unknown hsm action: %d on "DFID"\n",
446                        mdt_obd_name(info->mti_mdt),
447                        hai->hai_action, PFID(&hai->hai_fid));
448                 break;
449         }
450
451         hca->hca_location = hai->hai_extent;
452
453         EXIT;
454 out_free:
455         MDT_HSM_FREE(hal, hal_size);
456 out_ucred:
457         mdt_exit_ucred(info);
458 out:
459         mdt_thread_info_fini(info);
460         return rc;
461 }
462
463 /**
464  * Process the HSM actions described in a struct hsm_user_request.
465  *
466  * The action described in hur will be send to coordinator to be saved and
467  * processed later or either handled directly if hur.hur_action is HUA_RELEASE.
468  *
469  * This is MDS_HSM_REQUEST RPC handler.
470  */
471 int mdt_hsm_request(struct tgt_session_info *tsi)
472 {
473         struct mdt_thread_info          *info;
474         struct req_capsule              *pill = tsi->tsi_pill;
475         struct hsm_request              *hr;
476         struct hsm_user_item            *hui;
477         struct hsm_action_list          *hal;
478         struct hsm_action_item          *hai;
479         const void                      *data;
480         int                              hui_list_size;
481         int                              data_size;
482         enum hsm_copytool_action         action = HSMA_NONE;
483         __u64                            compound_id;
484         int                              hal_size, i, rc;
485         ENTRY;
486
487         hr = req_capsule_client_get(pill, &RMF_MDS_HSM_REQUEST);
488         hui = req_capsule_client_get(pill, &RMF_MDS_HSM_USER_ITEM);
489         data = req_capsule_client_get(pill, &RMF_GENERIC_DATA);
490
491         if (tsi->tsi_mdt_body == NULL || hr == NULL || hui == NULL || data == NULL)
492                 RETURN(-EPROTO);
493
494         /* Sanity check. Nothing to do with an empty list */
495         if (hr->hr_itemcount == 0)
496                 RETURN(0);
497
498         hui_list_size = req_capsule_get_size(pill, &RMF_MDS_HSM_USER_ITEM,
499                                              RCL_CLIENT);
500         if (hui_list_size < hr->hr_itemcount * sizeof(*hui))
501                 RETURN(-EPROTO);
502
503         data_size = req_capsule_get_size(pill, &RMF_GENERIC_DATA, RCL_CLIENT);
504         if (data_size != hr->hr_data_len)
505                 RETURN(-EPROTO);
506
507         info = tsi2mdt_info(tsi);
508         /* Only valid if client is remote */
509         rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
510         if (rc)
511                 GOTO(out, rc);
512
513         switch (hr->hr_action) {
514         /* code to be removed in hsm1_merge and final patch */
515         case HUA_RELEASE:
516                 CERROR("Release action is not working in hsm1_coord\n");
517                 GOTO(out_ucred, rc = -EINVAL);
518                 break;
519         /* end of code to be removed */
520         case HUA_ARCHIVE:
521                 action = HSMA_ARCHIVE;
522                 break;
523         case HUA_RESTORE:
524                 action = HSMA_RESTORE;
525                 break;
526         case HUA_REMOVE:
527                 action = HSMA_REMOVE;
528                 break;
529         case HUA_CANCEL:
530                 action = HSMA_CANCEL;
531                 break;
532         default:
533                 CERROR("Unknown hsm action: %d\n", hr->hr_action);
534                 GOTO(out_ucred, rc = -EINVAL);
535         }
536
537         hal_size = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
538                    (sizeof(*hai) + cfs_size_round(hr->hr_data_len)) *
539                    hr->hr_itemcount;
540
541         MDT_HSM_ALLOC(hal, hal_size);
542         if (hal == NULL)
543                 GOTO(out_ucred, rc = -ENOMEM);
544
545         hal->hal_version = HAL_VERSION;
546         hal->hal_archive_id = hr->hr_archive_id;
547         hal->hal_flags = hr->hr_flags;
548         obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
549                         MTI_NAME_MAXLEN);
550
551         hal->hal_count = hr->hr_itemcount;
552         hai = hai_first(hal);
553         for (i = 0; i < hr->hr_itemcount; i++, hai = hai_next(hai)) {
554                 hai->hai_action = action;
555                 hai->hai_cookie = 0;
556                 hai->hai_gid = 0;
557                 hai->hai_fid = hui[i].hui_fid;
558                 hai->hai_extent = hui[i].hui_extent;
559                 memcpy(hai->hai_data, data, hr->hr_data_len);
560                 hai->hai_len = sizeof(*hai) + hr->hr_data_len;
561         }
562
563         rc = mdt_hsm_add_actions(info, hal, &compound_id);
564
565         MDT_HSM_FREE(hal, hal_size);
566
567         GOTO(out_ucred, rc);
568
569 out_ucred:
570         mdt_exit_ucred(info);
571 out:
572         mdt_thread_info_fini(info);
573         return rc;
574 }