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) 2012, 2013, Intel Corporation.
24 * Use is subject to license terms.
25 * Copyright (c) 2011, 2012 Commissariat a l'energie atomique et aux energies
29 * lustre/mdt/mdt_hsm.c
31 * Lustre Metadata Target (mdt) request handler
33 * Author: Aurelien Degremont <aurelien.degremont@cea.fr>
34 * Author: JC Lafoucriere <jacques-charles.lafoucriere@cea.fr>
38 # define EXPORT_SYMTAB
40 #define DEBUG_SUBSYSTEM S_MDS
42 #include "mdt_internal.h"
44 /* Max allocation to satisfy single HSM RPC. */
45 #define MDT_HSM_ALLOC_MAX (1 << 20)
47 #define MDT_HSM_ALLOC(ptr, size) \
49 if ((size) <= MDT_HSM_ALLOC_MAX) \
50 OBD_ALLOC_LARGE((ptr), (size)); \
55 #define MDT_HSM_FREE(ptr, size) OBD_FREE_LARGE((ptr), (size))
58 * fake functions, will be replace by real one with HSM Coordinator patch
61 int mdt_hsm_copytool_send(struct obd_export *exp)
66 static int mdt_hsm_coordinator_update(struct mdt_thread_info *info,
67 struct hsm_progress_kernel *pgs)
72 static int mdt_hsm_agent_register_mask(struct mdt_thread_info *info,
73 struct obd_uuid *uuid,
79 static int mdt_hsm_agent_unregister(struct mdt_thread_info *info,
80 struct obd_uuid *uuid)
85 static int mdt_hsm_coordinator_get_actions(struct mdt_thread_info *mti,
86 struct hsm_action_list *hal)
91 static int mdt_hsm_coordinator_actions(struct mdt_thread_info *info,
92 struct hsm_action_list *hal,
94 int mti_attr_is_valid)
100 * Update on-disk HSM attributes.
102 int mdt_hsm_attr_set(struct mdt_thread_info *info, struct mdt_object *obj,
105 struct md_object *next = mdt_object_child(obj);
106 struct lu_buf *buf = &info->mti_buf;
107 struct hsm_attrs *attrs;
111 attrs = (struct hsm_attrs *)info->mti_xattr_buf;
112 CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*attrs));
114 /* pack HSM attributes */
115 lustre_hsm2buf(info->mti_xattr_buf, mh);
117 /* update SOM attributes */
119 buf->lb_len = sizeof(*attrs);
120 rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_HSM, 0);
126 * Extract information coming from a copytool and asks coordinator to update
127 * a request status depending on the update content.
129 * Copytools could use this to report failure in their process.
131 * This is HSM_PROGRESS RPC handler.
133 int mdt_hsm_progress(struct mdt_thread_info *info)
135 struct mdt_body *body;
136 struct hsm_progress_kernel *hpk;
140 body = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
144 hpk = req_capsule_client_get(info->mti_pill, &RMF_MDS_HSM_PROGRESS);
148 hpk->hpk_errval = lustre_errno_ntoh(hpk->hpk_errval);
150 CDEBUG(D_HSM, "Progress on "DFID": len="LPU64" err=%d\n",
151 PFID(&hpk->hpk_fid), hpk->hpk_extent.length, hpk->hpk_errval);
154 CDEBUG(D_HSM, "Copytool progress on "DFID" failed (%d); %s.\n",
155 PFID(&hpk->hpk_fid), hpk->hpk_errval,
156 hpk->hpk_flags & HP_FLAG_RETRY ? "will retry" : "fatal");
158 if (hpk->hpk_flags & HP_FLAG_COMPLETED)
159 CDEBUG(D_HSM, "Finished "DFID" (%d) cancel cookie="LPX64"\n",
160 PFID(&hpk->hpk_fid), hpk->hpk_errval, hpk->hpk_cookie);
162 rc = mdt_hsm_coordinator_update(info, hpk);
167 int mdt_hsm_ct_register(struct mdt_thread_info *info)
169 struct mdt_body *body;
170 struct ptlrpc_request *req = mdt_info_req(info);
175 body = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
179 archives = req_capsule_client_get(info->mti_pill, &RMF_MDS_HSM_ARCHIVE);
180 if (archives == NULL)
183 /* XXX: directly include this function here? */
184 rc = mdt_hsm_agent_register_mask(info, &req->rq_export->exp_client_uuid,
190 int mdt_hsm_ct_unregister(struct mdt_thread_info *info)
192 struct mdt_body *body;
193 struct ptlrpc_request *req = mdt_info_req(info);
197 body = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
201 /* XXX: directly include this function here? */
202 rc = mdt_hsm_agent_unregister(info, &req->rq_export->exp_client_uuid);
208 * Retrieve the current HSM flags, archive id and undergoing HSM requests for
209 * the fid provided in RPC body.
211 * Current requests are read from coordinator states.
213 * This is MDS_HSM_STATE_GET RPC handler.
215 int mdt_hsm_state_get(struct mdt_thread_info *info)
217 struct mdt_object *obj = info->mti_object;
218 struct md_attr *ma = &info->mti_attr;
219 struct hsm_user_state *hus;
220 struct mdt_lock_handle *lh;
224 lh = &info->mti_lh[MDT_LH_CHILD];
225 mdt_lock_reg_init(lh, LCK_PR);
226 rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP,
231 /* Only valid if client is remote */
232 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
234 GOTO(out_unlock, rc = err_serious(rc));
237 ma->ma_need = MA_HSM;
238 rc = mdt_attr_get_complex(info, obj, ma);
242 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
243 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
244 req_capsule_client_get(info->mti_pill, &RMF_CAPA1));
246 hus = req_capsule_server_get(info->mti_pill, &RMF_HSM_USER_STATE);
248 GOTO(out_ucred, rc = -EPROTO);
250 /* Current HSM flags */
251 hus->hus_states = ma->ma_hsm.mh_flags;
252 hus->hus_archive_id = ma->ma_hsm.mh_arch_id;
256 mdt_exit_ucred(info);
258 mdt_object_unlock(info, obj, lh, 1);
263 * Change HSM state and archive number of a file.
265 * Archive number is changed iif the value is not 0.
266 * The new flagset that will be computed should result in a coherent state.
267 * This function checks that are flags are compatible.
269 * This is MDS_HSM_STATE_SET RPC handler.
271 int mdt_hsm_state_set(struct mdt_thread_info *info)
273 struct mdt_object *obj = info->mti_object;
274 struct md_attr *ma = &info->mti_attr;
275 struct hsm_state_set *hss;
276 struct mdt_lock_handle *lh;
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,
288 /* Only valid if client is remote */
289 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
291 GOTO(out_obj, rc = err_serious(rc));
293 /* Read current HSM info */
295 ma->ma_need = MA_HSM;
296 rc = mdt_attr_get_complex(info, obj, ma);
300 hss = req_capsule_client_get(info->mti_pill, &RMF_HSM_STATE_SET);
302 GOTO(out_ucred, rc = -EPROTO);
304 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
305 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
306 req_capsule_client_get(info->mti_pill, &RMF_CAPA1));
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->fid1));
322 ma->ma_hsm.mh_arch_id = hss->hss_archive_id;
325 /* Check for inconsistant HSM flagset.
326 * DIRTY without EXISTS: no dirty if no archive was created.
327 * DIRTY and RELEASED: a dirty file could not be released.
328 * RELEASED without ARCHIVED: do not release a non-archived file.
329 * LOST without ARCHIVED: cannot lost a non-archived file.
331 flags = ma->ma_hsm.mh_flags;
332 if (((flags & HS_DIRTY) && !(flags & HS_EXISTS)) ||
333 ((flags & HS_RELEASED) && (flags & HS_DIRTY)) ||
334 ((flags & HS_RELEASED) && !(flags & HS_ARCHIVED)) ||
335 ((flags & HS_LOST) && !(flags & HS_ARCHIVED))) {
336 CDEBUG(D_HSM, "Incompatible flag change on "DFID
338 PFID(&info->mti_body->fid1), flags);
339 GOTO(out_ucred, rc = -EINVAL);
342 /* Save the modified flags */
343 rc = mdt_hsm_attr_set(info, obj, &ma->ma_hsm);
350 mdt_exit_ucred(info);
352 mdt_object_unlock(info, obj, lh, 1);
357 * Retrieve undergoing HSM requests for the fid provided in RPC body.
358 * Current requests are read from coordinator states.
360 * This is MDS_HSM_ACTION RPC handler.
362 int mdt_hsm_action(struct mdt_thread_info *info)
364 struct hsm_current_action *hca;
365 struct hsm_action_list *hal = NULL;
366 struct hsm_action_item *hai;
371 /* Only valid if client is remote */
372 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
374 RETURN(rc = err_serious(rc));
376 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
377 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
378 req_capsule_client_get(info->mti_pill,
381 hca = req_capsule_server_get(info->mti_pill,
382 &RMF_MDS_HSM_CURRENT_ACTION);
384 GOTO(out_ucred, rc = -EPROTO);
386 /* Coordinator information */
387 hal_size = sizeof(*hal) +
388 cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
389 cfs_size_round(sizeof(*hai));
391 MDT_HSM_ALLOC(hal, hal_size);
393 GOTO(out_ucred, rc = -ENOMEM);
395 hal->hal_version = HAL_VERSION;
396 hal->hal_archive_id = 0;
398 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
402 hai->hai_action = HSMA_NONE;
405 hai->hai_fid = info->mti_body->fid1;
406 hai->hai_len = sizeof(*hai);
408 rc = mdt_hsm_coordinator_get_actions(info, hal);
412 /* cookie is used to give back request status */
413 if (hai->hai_cookie == 0)
414 hca->hca_state = HPS_WAITING;
416 hca->hca_state = HPS_RUNNING;
418 switch (hai->hai_action) {
420 hca->hca_action = HUA_NONE;
423 hca->hca_action = HUA_ARCHIVE;
426 hca->hca_action = HUA_RESTORE;
429 hca->hca_action = HUA_REMOVE;
432 hca->hca_action = HUA_CANCEL;
435 hca->hca_action = HUA_NONE;
436 CERROR("%s: Unknown hsm action: %d on "DFID"\n",
437 mdt_obd_name(info->mti_mdt),
438 hai->hai_action, PFID(&hai->hai_fid));
442 hca->hca_location = hai->hai_extent;
446 MDT_HSM_FREE(hal, hal_size);
448 mdt_exit_ucred(info);
453 * Process the HSM actions described in a struct hsm_user_request.
455 * The action described in hur will be send to coordinator to be saved and
456 * processed later or either handled directly if hur.hur_action is HUA_RELEASE.
458 * This is MDS_HSM_REQUEST RPC handler.
460 int mdt_hsm_request(struct mdt_thread_info *info)
462 struct req_capsule *pill = info->mti_pill;
463 struct mdt_body *body;
464 struct hsm_request *hr;
465 struct hsm_user_item *hui;
466 struct hsm_action_list *hal;
467 struct hsm_action_item *hai;
471 enum hsm_copytool_action action = HSMA_NONE;
476 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
477 hr = req_capsule_client_get(pill, &RMF_MDS_HSM_REQUEST);
478 hui = req_capsule_client_get(pill, &RMF_MDS_HSM_USER_ITEM);
479 data = req_capsule_client_get(pill, &RMF_GENERIC_DATA);
481 if (body == NULL || hr == NULL || hui == NULL || data == NULL)
484 /* Sanity check. Nothing to do with an empty list */
485 if (hr->hr_itemcount == 0)
488 hui_list_size = req_capsule_get_size(pill, &RMF_MDS_HSM_USER_ITEM,
490 if (hui_list_size < hr->hr_itemcount * sizeof(*hui))
493 data_size = req_capsule_get_size(pill, &RMF_GENERIC_DATA, RCL_CLIENT);
494 if (data_size != hr->hr_data_len)
497 /* Only valid if client is remote */
498 rc = mdt_init_ucred(info, body);
500 RETURN(err_serious(rc));
502 switch (hr->hr_action) {
503 /* code to be removed in hsm1_merge and final patch */
505 CERROR("Release action is not working in hsm1_coord\n");
506 GOTO(out_ucred, rc = -EINVAL);
508 /* end of code to be removed */
510 action = HSMA_ARCHIVE;
513 action = HSMA_RESTORE;
516 action = HSMA_REMOVE;
519 action = HSMA_CANCEL;
522 CERROR("Unknown hsm action: %d\n", hr->hr_action);
523 GOTO(out_ucred, rc = -EINVAL);
526 hal_size = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
527 (sizeof(*hai) + cfs_size_round(hr->hr_data_len)) *
530 MDT_HSM_ALLOC(hal, hal_size);
532 GOTO(out_ucred, rc = -ENOMEM);
534 hal->hal_version = HAL_VERSION;
535 hal->hal_archive_id = hr->hr_archive_id;
536 hal->hal_flags = hr->hr_flags;
537 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
540 hal->hal_count = hr->hr_itemcount;
542 for (i = 0; i < hr->hr_itemcount; i++) {
543 hai->hai_action = action;
546 hai->hai_fid = hui[i].hui_fid;
547 hai->hai_extent = hui[i].hui_extent;
548 memcpy(hai->hai_data, data, hr->hr_data_len);
549 hai->hai_len = sizeof(*hai) + hr->hr_data_len;
553 rc = mdt_hsm_coordinator_actions(info, hal, &compound_id, 0);
554 /* ENODATA error code is needed only for implicit requests */
558 MDT_HSM_FREE(hal, hal_size);
561 mdt_exit_ucred(info);