4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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.
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
23 * Copyright (c) 2011, 2012 Commissariat a l'energie atomique et aux energies
25 * Use is subject to license terms.
27 * Copyright (c) 2012, 2015, Intel Corporation.
30 * lustre/mdt/mdt_hsm.c
32 * Lustre Metadata Target (mdt) request handler
34 * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
35 * Author: JC Lafoucriere <jacques-charles.lafoucriere@cea.fr>
38 #define DEBUG_SUBSYSTEM S_MDS
40 #include <lustre_errno.h>
41 #include "mdt_internal.h"
43 /* Max allocation to satisfy single HSM RPC. */
44 #define MDT_HSM_ALLOC_MAX (1 << 20)
46 #define MDT_HSM_ALLOC(ptr, size) \
48 if ((size) <= MDT_HSM_ALLOC_MAX) \
49 OBD_ALLOC_LARGE((ptr), (size)); \
54 #define MDT_HSM_FREE(ptr, size) OBD_FREE_LARGE((ptr), (size))
57 * Update on-disk HSM attributes.
59 int mdt_hsm_attr_set(struct mdt_thread_info *info, struct mdt_object *obj,
60 const struct md_hsm *mh)
62 struct md_object *next = mdt_object_child(obj);
63 struct lu_buf *buf = &info->mti_buf;
64 struct hsm_attrs *attrs;
68 attrs = (struct hsm_attrs *)info->mti_xattr_buf;
69 CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*attrs));
71 /* pack HSM attributes */
72 lustre_hsm2buf(info->mti_xattr_buf, mh);
74 /* update HSM attributes */
76 buf->lb_len = sizeof(*attrs);
77 rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_HSM, 0);
82 static inline bool mdt_hsm_is_admin(struct mdt_thread_info *info)
87 if (info->mti_body == NULL)
90 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
94 is_admin = md_capable(mdt_ucred(info), CFS_CAP_SYS_ADMIN);
102 * Extract information coming from a copytool and asks coordinator to update
103 * a request status depending on the update content.
105 * Copytools could use this to report failure in their process.
107 * This is HSM_PROGRESS RPC handler.
109 int mdt_hsm_progress(struct tgt_session_info *tsi)
111 struct mdt_thread_info *info;
112 struct hsm_progress_kernel *hpk;
116 if (tsi->tsi_mdt_body == NULL)
119 hpk = req_capsule_client_get(tsi->tsi_pill, &RMF_MDS_HSM_PROGRESS);
121 RETURN(err_serious(-EPROTO));
123 hpk->hpk_errval = lustre_errno_ntoh(hpk->hpk_errval);
125 CDEBUG(D_HSM, "Progress on "DFID": len=%llu : rc = %d\n",
126 PFID(&hpk->hpk_fid), hpk->hpk_extent.length, hpk->hpk_errval);
129 CDEBUG(D_HSM, "Copytool progress on "DFID" failed : rc = %d; %s.\n",
130 PFID(&hpk->hpk_fid), hpk->hpk_errval,
131 hpk->hpk_flags & HP_FLAG_RETRY ? "will retry" : "fatal");
133 if (hpk->hpk_flags & HP_FLAG_COMPLETED)
134 CDEBUG(D_HSM, "Finished "DFID" : rc = %d; cancel cookie=%#llx\n",
135 PFID(&hpk->hpk_fid), hpk->hpk_errval, hpk->hpk_cookie);
137 info = tsi2mdt_info(tsi);
138 if (!mdt_hsm_is_admin(info))
139 GOTO(out, rc = -EPERM);
141 rc = mdt_hsm_update_request_state(info, hpk);
143 mdt_thread_info_fini(info);
147 int mdt_hsm_ct_register(struct tgt_session_info *tsi)
149 struct mdt_thread_info *info;
154 archives = req_capsule_client_get(tsi->tsi_pill, &RMF_MDS_HSM_ARCHIVE);
155 if (archives == NULL)
156 RETURN(err_serious(-EPROTO));
158 info = tsi2mdt_info(tsi);
159 if (!mdt_hsm_is_admin(info))
160 GOTO(out, rc = -EPERM);
162 /* XXX: directly include this function here? */
163 rc = mdt_hsm_agent_register_mask(info, &tsi->tsi_exp->exp_client_uuid,
166 mdt_thread_info_fini(info);
170 int mdt_hsm_ct_unregister(struct tgt_session_info *tsi)
172 struct mdt_thread_info *info;
176 if (tsi->tsi_mdt_body == NULL)
179 info = tsi2mdt_info(tsi);
180 if (!mdt_hsm_is_admin(info))
181 GOTO(out, rc = -EPERM);
183 /* XXX: directly include this function here? */
184 rc = mdt_hsm_agent_unregister(info, &tsi->tsi_exp->exp_client_uuid);
186 mdt_thread_info_fini(info);
191 * Retrieve the current HSM flags, archive id and undergoing HSM requests for
192 * the fid provided in RPC body.
194 * Current requests are read from coordinator states.
196 * This is MDS_HSM_STATE_GET RPC handler.
198 int mdt_hsm_state_get(struct tgt_session_info *tsi)
200 struct mdt_thread_info *info = tsi2mdt_info(tsi);
201 struct mdt_object *obj = info->mti_object;
202 struct md_attr *ma = &info->mti_attr;
203 struct hsm_user_state *hus;
204 struct mdt_lock_handle *lh;
208 if (info->mti_body == NULL || obj == NULL)
209 GOTO(out, rc = -EPROTO);
211 /* Only valid if client is remote */
212 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
214 GOTO(out, rc = err_serious(rc));
216 lh = &info->mti_lh[MDT_LH_CHILD];
217 mdt_lock_reg_init(lh, LCK_PR);
218 rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP);
223 ma->ma_need = MA_HSM;
224 rc = mdt_attr_get_complex(info, obj, ma);
226 GOTO(out_unlock, rc);
228 hus = req_capsule_server_get(tsi->tsi_pill, &RMF_HSM_USER_STATE);
230 GOTO(out_unlock, rc = -EPROTO);
232 /* Current HSM flags */
233 hus->hus_states = ma->ma_hsm.mh_flags;
234 hus->hus_archive_id = ma->ma_hsm.mh_arch_id;
238 mdt_object_unlock(info, obj, lh, 1);
240 mdt_exit_ucred(info);
242 mdt_thread_info_fini(info);
247 * Change HSM state and archive number of a file.
249 * Archive number is changed iif the value is not 0.
250 * The new flagset that will be computed should result in a coherent state.
251 * This function checks that flags are compatible.
253 * This is MDS_HSM_STATE_SET RPC handler.
255 int mdt_hsm_state_set(struct tgt_session_info *tsi)
257 struct mdt_thread_info *info = tsi2mdt_info(tsi);
258 struct mdt_object *obj = info->mti_object;
259 struct md_attr *ma = &info->mti_attr;
260 struct hsm_state_set *hss;
261 struct mdt_lock_handle *lh;
266 hss = req_capsule_client_get(info->mti_pill, &RMF_HSM_STATE_SET);
268 if (info->mti_body == NULL || obj == NULL || hss == NULL)
269 GOTO(out, rc = -EPROTO);
271 /* Only valid if client is remote */
272 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
274 GOTO(out, rc = err_serious(rc));
276 lh = &info->mti_lh[MDT_LH_CHILD];
277 mdt_lock_reg_init(lh, LCK_PW);
278 rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP |
279 MDS_INODELOCK_XATTR);
283 /* Detect out-of range masks */
284 if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK) {
285 CDEBUG(D_HSM, "Incompatible masks provided (set %#llx"
286 ", clear %#llx) vs supported set (%#x).\n",
287 hss->hss_setmask, hss->hss_clearmask, HSM_FLAGS_MASK);
288 GOTO(out_unlock, rc = -EINVAL);
291 /* Non-root users are forbidden to set or clear flags which are
292 * NOT defined in HSM_USER_MASK. */
293 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
294 !md_capable(mdt_ucred(info), CFS_CAP_SYS_ADMIN)) {
295 CDEBUG(D_HSM, "Incompatible masks provided (set %#llx"
296 ", clear %#llx) vs unprivileged set (%#x).\n",
297 hss->hss_setmask, hss->hss_clearmask, HSM_USER_MASK);
298 GOTO(out_unlock, rc = -EPERM);
301 /* Read current HSM info */
303 ma->ma_need = MA_HSM;
304 rc = mdt_attr_get_complex(info, obj, ma);
306 GOTO(out_unlock, rc);
308 /* Change HSM flags depending on provided masks */
309 if (hss->hss_valid & HSS_SETMASK)
310 ma->ma_hsm.mh_flags |= hss->hss_setmask;
311 if (hss->hss_valid & HSS_CLEARMASK)
312 ma->ma_hsm.mh_flags &= ~hss->hss_clearmask;
314 /* Change archive_id if provided. */
315 if (hss->hss_valid & HSS_ARCHIVE_ID) {
316 if (!(ma->ma_hsm.mh_flags & HS_EXISTS)) {
317 CDEBUG(D_HSM, "Could not set an archive number for "
318 DFID "if HSM EXISTS flag is not set.\n",
319 PFID(&info->mti_body->mbo_fid1));
320 GOTO(out_unlock, rc);
323 /* Detect out-of range archive id */
324 if (hss->hss_archive_id > LL_HSM_MAX_ARCHIVE) {
325 CDEBUG(D_HSM, "archive id %u exceeds maximum %zu.\n",
326 hss->hss_archive_id, LL_HSM_MAX_ARCHIVE);
327 GOTO(out_unlock, rc = -EINVAL);
330 ma->ma_hsm.mh_arch_id = hss->hss_archive_id;
333 /* Check for inconsistant HSM flagset.
334 * DIRTY without EXISTS: no dirty if no archive was created.
335 * DIRTY and RELEASED: a dirty file could not be released.
336 * RELEASED without ARCHIVED: do not release a non-archived file.
337 * LOST without ARCHIVED: cannot lost a non-archived file.
339 flags = ma->ma_hsm.mh_flags;
340 if ((flags & HS_DIRTY && !(flags & HS_EXISTS)) ||
341 (flags & HS_RELEASED && flags & HS_DIRTY) ||
342 (flags & HS_RELEASED && !(flags & HS_ARCHIVED)) ||
343 (flags & HS_LOST && !(flags & HS_ARCHIVED))) {
344 CDEBUG(D_HSM, "Incompatible flag change on "DFID
346 PFID(&info->mti_body->mbo_fid1), flags);
347 GOTO(out_unlock, rc = -EINVAL);
350 /* Save the modified flags */
351 rc = mdt_hsm_attr_set(info, obj, &ma->ma_hsm);
353 GOTO(out_unlock, rc);
358 mdt_object_unlock(info, obj, lh, 1);
360 mdt_exit_ucred(info);
362 mdt_thread_info_fini(info);
367 * Retrieve undergoing HSM requests for the fid provided in RPC body.
368 * Current requests are read from coordinator states.
370 * This is MDS_HSM_ACTION RPC handler.
372 int mdt_hsm_action(struct tgt_session_info *tsi)
374 struct mdt_thread_info *info;
375 struct hsm_current_action *hca;
376 struct hsm_action_list *hal = NULL;
377 struct hsm_action_item *hai;
382 hca = req_capsule_server_get(tsi->tsi_pill,
383 &RMF_MDS_HSM_CURRENT_ACTION);
385 RETURN(err_serious(-EPROTO));
387 if (tsi->tsi_mdt_body == NULL)
390 info = tsi2mdt_info(tsi);
391 /* Only valid if client is remote */
392 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
394 GOTO(out, rc = err_serious(rc));
396 /* Coordinator information */
397 hal_size = sizeof(*hal) +
398 cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
399 cfs_size_round(sizeof(*hai));
401 MDT_HSM_ALLOC(hal, hal_size);
403 GOTO(out_ucred, rc = -ENOMEM);
405 hal->hal_version = HAL_VERSION;
406 hal->hal_archive_id = 0;
408 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
411 hai = hai_first(hal);
412 hai->hai_action = HSMA_NONE;
415 hai->hai_fid = info->mti_body->mbo_fid1;
416 hai->hai_len = sizeof(*hai);
418 rc = mdt_hsm_get_actions(info, hal);
422 /* cookie is used to give back request status */
423 if (hai->hai_cookie == 0)
424 hca->hca_state = HPS_WAITING;
426 hca->hca_state = HPS_RUNNING;
428 switch (hai->hai_action) {
430 hca->hca_action = HUA_NONE;
433 hca->hca_action = HUA_ARCHIVE;
436 hca->hca_action = HUA_RESTORE;
439 hca->hca_action = HUA_REMOVE;
442 hca->hca_action = HUA_CANCEL;
445 hca->hca_action = HUA_NONE;
446 CERROR("%s: Unknown hsm action: %d on "DFID"\n",
447 mdt_obd_name(info->mti_mdt),
448 hai->hai_action, PFID(&hai->hai_fid));
452 hca->hca_location = hai->hai_extent;
456 MDT_HSM_FREE(hal, hal_size);
458 mdt_exit_ucred(info);
460 mdt_thread_info_fini(info);
464 /* Return true if a FID is present in an action list. */
465 static bool is_fid_in_hal(struct hsm_action_list *hal, const struct lu_fid *fid)
467 struct hsm_action_item *hai;
470 for (hai = hai_first(hal), i = 0;
472 i++, hai = hai_next(hai)) {
473 if (lu_fid_eq(&hai->hai_fid, fid))
481 * Process the HSM actions described in a struct hsm_user_request.
483 * The action described in hur will be send to coordinator to be saved and
484 * processed later or either handled directly if hur.hur_action is HUA_RELEASE.
486 * This is MDS_HSM_REQUEST RPC handler.
488 int mdt_hsm_request(struct tgt_session_info *tsi)
490 struct mdt_thread_info *info;
491 struct req_capsule *pill = tsi->tsi_pill;
492 struct hsm_request *hr;
493 struct hsm_user_item *hui;
494 struct hsm_action_list *hal;
495 struct hsm_action_item *hai;
499 enum hsm_copytool_action action = HSMA_NONE;
503 hr = req_capsule_client_get(pill, &RMF_MDS_HSM_REQUEST);
504 hui = req_capsule_client_get(pill, &RMF_MDS_HSM_USER_ITEM);
505 data = req_capsule_client_get(pill, &RMF_GENERIC_DATA);
507 if (tsi->tsi_mdt_body == NULL || hr == NULL || hui == NULL || data == NULL)
510 /* Sanity check. Nothing to do with an empty list */
511 if (hr->hr_itemcount == 0)
514 hui_list_size = req_capsule_get_size(pill, &RMF_MDS_HSM_USER_ITEM,
516 if (hui_list_size < hr->hr_itemcount * sizeof(*hui))
519 data_size = req_capsule_get_size(pill, &RMF_GENERIC_DATA, RCL_CLIENT);
520 if (data_size != hr->hr_data_len)
523 info = tsi2mdt_info(tsi);
524 /* Only valid if client is remote */
525 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
529 switch (hr->hr_action) {
530 /* code to be removed in hsm1_merge and final patch */
532 CERROR("Release action is not working in hsm1_coord\n");
533 GOTO(out_ucred, rc = -EINVAL);
535 /* end of code to be removed */
537 action = HSMA_ARCHIVE;
540 action = HSMA_RESTORE;
543 action = HSMA_REMOVE;
546 action = HSMA_CANCEL;
549 CERROR("Unknown hsm action: %d\n", hr->hr_action);
550 GOTO(out_ucred, rc = -EINVAL);
553 hal_size = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
554 (sizeof(*hai) + cfs_size_round(hr->hr_data_len)) *
557 MDT_HSM_ALLOC(hal, hal_size);
559 GOTO(out_ucred, rc = -ENOMEM);
561 hal->hal_version = HAL_VERSION;
562 hal->hal_archive_id = hr->hr_archive_id;
563 hal->hal_flags = hr->hr_flags;
564 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
568 hai = hai_first(hal);
569 for (i = 0; i < hr->hr_itemcount; i++, hai = hai_next(hai)) {
570 /* Get rid of duplicate entries. Otherwise we get
571 * duplicated work in the llog. */
572 if (is_fid_in_hal(hal, &hui[i].hui_fid))
575 hai->hai_action = action;
578 hai->hai_fid = hui[i].hui_fid;
579 hai->hai_extent = hui[i].hui_extent;
580 memcpy(hai->hai_data, data, hr->hr_data_len);
581 hai->hai_len = sizeof(*hai) + hr->hr_data_len;
586 rc = mdt_hsm_add_actions(info, hal);
588 MDT_HSM_FREE(hal, hal_size);
593 mdt_exit_ucred(info);
595 mdt_thread_info_fini(info);