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
26 * Copyright (c) 2012, 2014, Intel Corporation.
27 * Use is subject to license terms.
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 "mdt_internal.h"
42 /* Max allocation to satisfy single HSM RPC. */
43 #define MDT_HSM_ALLOC_MAX (1 << 20)
45 #define MDT_HSM_ALLOC(ptr, size) \
47 if ((size) <= MDT_HSM_ALLOC_MAX) \
48 OBD_ALLOC_LARGE((ptr), (size)); \
53 #define MDT_HSM_FREE(ptr, size) OBD_FREE_LARGE((ptr), (size))
56 * Update on-disk HSM attributes.
58 int mdt_hsm_attr_set(struct mdt_thread_info *info, struct mdt_object *obj,
59 const struct md_hsm *mh)
61 struct md_object *next = mdt_object_child(obj);
62 struct lu_buf *buf = &info->mti_buf;
63 struct hsm_attrs *attrs;
67 attrs = (struct hsm_attrs *)info->mti_xattr_buf;
68 CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*attrs));
70 /* pack HSM attributes */
71 lustre_hsm2buf(info->mti_xattr_buf, mh);
73 /* update SOM attributes */
75 buf->lb_len = sizeof(*attrs);
76 rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_HSM, 0);
81 static inline bool mdt_hsm_is_admin(struct mdt_thread_info *info)
86 if (info->mti_body == NULL)
89 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
93 is_admin = md_capable(mdt_ucred(info), CFS_CAP_SYS_ADMIN);
101 * Extract information coming from a copytool and asks coordinator to update
102 * a request status depending on the update content.
104 * Copytools could use this to report failure in their process.
106 * This is HSM_PROGRESS RPC handler.
108 int mdt_hsm_progress(struct tgt_session_info *tsi)
110 struct mdt_thread_info *info;
111 struct hsm_progress_kernel *hpk;
115 if (tsi->tsi_mdt_body == NULL)
118 hpk = req_capsule_client_get(tsi->tsi_pill, &RMF_MDS_HSM_PROGRESS);
120 RETURN(err_serious(-EPROTO));
122 hpk->hpk_errval = lustre_errno_ntoh(hpk->hpk_errval);
124 CDEBUG(D_HSM, "Progress on "DFID": len="LPU64" err=%d\n",
125 PFID(&hpk->hpk_fid), hpk->hpk_extent.length, 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");
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);
136 info = tsi2mdt_info(tsi);
137 if (!mdt_hsm_is_admin(info))
138 GOTO(out, rc = -EPERM);
140 rc = mdt_hsm_coordinator_update(info, hpk);
142 mdt_thread_info_fini(info);
146 int mdt_hsm_ct_register(struct tgt_session_info *tsi)
148 struct mdt_thread_info *info;
153 archives = req_capsule_client_get(tsi->tsi_pill, &RMF_MDS_HSM_ARCHIVE);
154 if (archives == NULL)
155 RETURN(err_serious(-EPROTO));
157 info = tsi2mdt_info(tsi);
158 if (!mdt_hsm_is_admin(info))
159 GOTO(out, rc = -EPERM);
161 /* XXX: directly include this function here? */
162 rc = mdt_hsm_agent_register_mask(info, &tsi->tsi_exp->exp_client_uuid,
165 mdt_thread_info_fini(info);
169 int mdt_hsm_ct_unregister(struct tgt_session_info *tsi)
171 struct mdt_thread_info *info;
175 if (tsi->tsi_mdt_body == NULL)
178 info = tsi2mdt_info(tsi);
179 if (!mdt_hsm_is_admin(info))
180 GOTO(out, rc = -EPERM);
182 /* XXX: directly include this function here? */
183 rc = mdt_hsm_agent_unregister(info, &tsi->tsi_exp->exp_client_uuid);
185 mdt_thread_info_fini(info);
190 * Retrieve the current HSM flags, archive id and undergoing HSM requests for
191 * the fid provided in RPC body.
193 * Current requests are read from coordinator states.
195 * This is MDS_HSM_STATE_GET RPC handler.
197 int mdt_hsm_state_get(struct tgt_session_info *tsi)
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;
207 if (info->mti_body == NULL || obj == NULL)
208 GOTO(out, rc = -EPROTO);
210 /* Only valid if client is remote */
211 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
213 GOTO(out, rc = err_serious(rc));
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,
223 ma->ma_need = MA_HSM;
224 rc = mdt_attr_get_complex(info, obj, ma);
226 GOTO(out_unlock, rc);
228 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
229 mdt_set_capainfo(info, 0, &info->mti_body->mbo_fid1,
230 req_capsule_client_get(info->mti_pill,
233 hus = req_capsule_server_get(tsi->tsi_pill, &RMF_HSM_USER_STATE);
235 GOTO(out_unlock, rc = -EPROTO);
237 /* Current HSM flags */
238 hus->hus_states = ma->ma_hsm.mh_flags;
239 hus->hus_archive_id = ma->ma_hsm.mh_arch_id;
243 mdt_object_unlock(info, obj, lh, 1);
245 mdt_exit_ucred(info);
247 mdt_thread_info_fini(info);
252 * Change HSM state and archive number of a file.
254 * Archive number is changed iif the value is not 0.
255 * The new flagset that will be computed should result in a coherent state.
256 * This function checks that are flags are compatible.
258 * This is MDS_HSM_STATE_SET RPC handler.
260 int mdt_hsm_state_set(struct tgt_session_info *tsi)
262 struct mdt_thread_info *info = tsi2mdt_info(tsi);
263 struct mdt_object *obj = info->mti_object;
264 struct md_attr *ma = &info->mti_attr;
265 struct hsm_state_set *hss;
266 struct mdt_lock_handle *lh;
271 hss = req_capsule_client_get(info->mti_pill, &RMF_HSM_STATE_SET);
273 if (info->mti_body == NULL || obj == NULL || hss == NULL)
274 GOTO(out, rc = -EPROTO);
276 /* Only valid if client is remote */
277 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
279 GOTO(out, rc = err_serious(rc));
281 lh = &info->mti_lh[MDT_LH_CHILD];
282 mdt_lock_reg_init(lh, LCK_PW);
283 rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP |
284 MDS_INODELOCK_XATTR, MDT_LOCAL_LOCK);
288 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
289 mdt_set_capainfo(info, 0, &info->mti_body->mbo_fid1,
290 req_capsule_client_get(info->mti_pill, &RMF_CAPA1));
292 /* Detect out-of range masks */
293 if ((hss->hss_setmask | hss->hss_clearmask) & ~HSM_FLAGS_MASK) {
294 CDEBUG(D_HSM, "Incompatible masks provided (set "LPX64
295 ", clear "LPX64") vs supported set (%#x).\n",
296 hss->hss_setmask, hss->hss_clearmask, HSM_FLAGS_MASK);
297 GOTO(out_unlock, rc = -EINVAL);
300 /* Non-root users are forbidden to set or clear flags which are
301 * NOT defined in HSM_USER_MASK. */
302 if (((hss->hss_setmask | hss->hss_clearmask) & ~HSM_USER_MASK) &&
303 !md_capable(mdt_ucred(info), CFS_CAP_SYS_ADMIN)) {
304 CDEBUG(D_HSM, "Incompatible masks provided (set "LPX64
305 ", clear "LPX64") vs unprivileged set (%#x).\n",
306 hss->hss_setmask, hss->hss_clearmask, HSM_USER_MASK);
307 GOTO(out_unlock, rc = -EPERM);
310 /* Read current HSM info */
312 ma->ma_need = MA_HSM;
313 rc = mdt_attr_get_complex(info, obj, ma);
315 GOTO(out_unlock, rc);
317 /* Change HSM flags depending on provided masks */
318 if (hss->hss_valid & HSS_SETMASK)
319 ma->ma_hsm.mh_flags |= hss->hss_setmask;
320 if (hss->hss_valid & HSS_CLEARMASK)
321 ma->ma_hsm.mh_flags &= ~hss->hss_clearmask;
323 /* Change archive_id if provided. */
324 if (hss->hss_valid & HSS_ARCHIVE_ID) {
325 if (!(ma->ma_hsm.mh_flags & HS_EXISTS)) {
326 CDEBUG(D_HSM, "Could not set an archive number for "
327 DFID "if HSM EXISTS flag is not set.\n",
328 PFID(&info->mti_body->mbo_fid1));
329 GOTO(out_unlock, rc);
332 /* Detect out-of range archive id */
333 if (hss->hss_archive_id > LL_HSM_MAX_ARCHIVE) {
334 CDEBUG(D_HSM, "archive id %u exceeds maximum %zu.\n",
335 hss->hss_archive_id, LL_HSM_MAX_ARCHIVE);
336 GOTO(out_unlock, rc = -EINVAL);
339 ma->ma_hsm.mh_arch_id = hss->hss_archive_id;
342 /* Check for inconsistant HSM flagset.
343 * DIRTY without EXISTS: no dirty if no archive was created.
344 * DIRTY and RELEASED: a dirty file could not be released.
345 * RELEASED without ARCHIVED: do not release a non-archived file.
346 * LOST without ARCHIVED: cannot lost a non-archived file.
348 flags = ma->ma_hsm.mh_flags;
349 if ((flags & HS_DIRTY && !(flags & HS_EXISTS)) ||
350 (flags & HS_RELEASED && flags & HS_DIRTY) ||
351 (flags & HS_RELEASED && !(flags & HS_ARCHIVED)) ||
352 (flags & HS_LOST && !(flags & HS_ARCHIVED))) {
353 CDEBUG(D_HSM, "Incompatible flag change on "DFID
355 PFID(&info->mti_body->mbo_fid1), flags);
356 GOTO(out_unlock, rc = -EINVAL);
359 /* Save the modified flags */
360 rc = mdt_hsm_attr_set(info, obj, &ma->ma_hsm);
362 GOTO(out_unlock, rc);
367 mdt_object_unlock(info, obj, lh, 1);
369 mdt_exit_ucred(info);
371 mdt_thread_info_fini(info);
376 * Retrieve undergoing HSM requests for the fid provided in RPC body.
377 * Current requests are read from coordinator states.
379 * This is MDS_HSM_ACTION RPC handler.
381 int mdt_hsm_action(struct tgt_session_info *tsi)
383 struct mdt_thread_info *info;
384 struct hsm_current_action *hca;
385 struct hsm_action_list *hal = NULL;
386 struct hsm_action_item *hai;
391 hca = req_capsule_server_get(tsi->tsi_pill,
392 &RMF_MDS_HSM_CURRENT_ACTION);
394 RETURN(err_serious(-EPROTO));
396 if (tsi->tsi_mdt_body == NULL)
399 info = tsi2mdt_info(tsi);
400 /* Only valid if client is remote */
401 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
403 GOTO(out, rc = err_serious(rc));
405 if (req_capsule_get_size(tsi->tsi_pill, &RMF_CAPA1, RCL_CLIENT))
406 mdt_set_capainfo(info, 0, &info->mti_body->mbo_fid1,
407 req_capsule_client_get(info->mti_pill,
410 /* Coordinator information */
411 hal_size = sizeof(*hal) +
412 cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
413 cfs_size_round(sizeof(*hai));
415 MDT_HSM_ALLOC(hal, hal_size);
417 GOTO(out_ucred, rc = -ENOMEM);
419 hal->hal_version = HAL_VERSION;
420 hal->hal_archive_id = 0;
422 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
425 hai = hai_first(hal);
426 hai->hai_action = HSMA_NONE;
429 hai->hai_fid = info->mti_body->mbo_fid1;
430 hai->hai_len = sizeof(*hai);
432 rc = mdt_hsm_get_actions(info, hal);
436 /* cookie is used to give back request status */
437 if (hai->hai_cookie == 0)
438 hca->hca_state = HPS_WAITING;
440 hca->hca_state = HPS_RUNNING;
442 switch (hai->hai_action) {
444 hca->hca_action = HUA_NONE;
447 hca->hca_action = HUA_ARCHIVE;
450 hca->hca_action = HUA_RESTORE;
453 hca->hca_action = HUA_REMOVE;
456 hca->hca_action = HUA_CANCEL;
459 hca->hca_action = HUA_NONE;
460 CERROR("%s: Unknown hsm action: %d on "DFID"\n",
461 mdt_obd_name(info->mti_mdt),
462 hai->hai_action, PFID(&hai->hai_fid));
466 hca->hca_location = hai->hai_extent;
470 MDT_HSM_FREE(hal, hal_size);
472 mdt_exit_ucred(info);
474 mdt_thread_info_fini(info);
479 * Process the HSM actions described in a struct hsm_user_request.
481 * The action described in hur will be send to coordinator to be saved and
482 * processed later or either handled directly if hur.hur_action is HUA_RELEASE.
484 * This is MDS_HSM_REQUEST RPC handler.
486 int mdt_hsm_request(struct tgt_session_info *tsi)
488 struct mdt_thread_info *info;
489 struct req_capsule *pill = tsi->tsi_pill;
490 struct hsm_request *hr;
491 struct hsm_user_item *hui;
492 struct hsm_action_list *hal;
493 struct hsm_action_item *hai;
497 enum hsm_copytool_action action = HSMA_NONE;
502 hr = req_capsule_client_get(pill, &RMF_MDS_HSM_REQUEST);
503 hui = req_capsule_client_get(pill, &RMF_MDS_HSM_USER_ITEM);
504 data = req_capsule_client_get(pill, &RMF_GENERIC_DATA);
506 if (tsi->tsi_mdt_body == NULL || hr == NULL || hui == NULL || data == NULL)
509 /* Sanity check. Nothing to do with an empty list */
510 if (hr->hr_itemcount == 0)
513 hui_list_size = req_capsule_get_size(pill, &RMF_MDS_HSM_USER_ITEM,
515 if (hui_list_size < hr->hr_itemcount * sizeof(*hui))
518 data_size = req_capsule_get_size(pill, &RMF_GENERIC_DATA, RCL_CLIENT);
519 if (data_size != hr->hr_data_len)
522 info = tsi2mdt_info(tsi);
523 /* Only valid if client is remote */
524 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
528 switch (hr->hr_action) {
529 /* code to be removed in hsm1_merge and final patch */
531 CERROR("Release action is not working in hsm1_coord\n");
532 GOTO(out_ucred, rc = -EINVAL);
534 /* end of code to be removed */
536 action = HSMA_ARCHIVE;
539 action = HSMA_RESTORE;
542 action = HSMA_REMOVE;
545 action = HSMA_CANCEL;
548 CERROR("Unknown hsm action: %d\n", hr->hr_action);
549 GOTO(out_ucred, rc = -EINVAL);
552 hal_size = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
553 (sizeof(*hai) + cfs_size_round(hr->hr_data_len)) *
556 MDT_HSM_ALLOC(hal, hal_size);
558 GOTO(out_ucred, rc = -ENOMEM);
560 hal->hal_version = HAL_VERSION;
561 hal->hal_archive_id = hr->hr_archive_id;
562 hal->hal_flags = hr->hr_flags;
563 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
566 hal->hal_count = hr->hr_itemcount;
567 hai = hai_first(hal);
568 for (i = 0; i < hr->hr_itemcount; i++, hai = hai_next(hai)) {
569 hai->hai_action = action;
572 hai->hai_fid = hui[i].hui_fid;
573 hai->hai_extent = hui[i].hui_extent;
574 memcpy(hai->hai_data, data, hr->hr_data_len);
575 hai->hai_len = sizeof(*hai) + hr->hr_data_len;
578 rc = mdt_hsm_add_actions(info, hal, &compound_id);
580 MDT_HSM_FREE(hal, hal_size);
585 mdt_exit_ucred(info);
587 mdt_thread_info_fini(info);