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, 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 CDEBUG(D_HSM, "Progress on "DFID": len="LPU64" err=%d\n",
149 PFID(&hpk->hpk_fid), hpk->hpk_extent.length, hpk->hpk_errval);
152 CDEBUG(D_HSM, "Copytool progress on "DFID" failed (%d); %s.\n",
153 PFID(&hpk->hpk_fid), hpk->hpk_errval,
154 hpk->hpk_flags & HP_FLAG_RETRY ? "will retry" : "fatal");
156 if (hpk->hpk_flags & HP_FLAG_COMPLETED)
157 CDEBUG(D_HSM, "Finished "DFID" (%d) cancel cookie="LPX64"\n",
158 PFID(&hpk->hpk_fid), hpk->hpk_errval, hpk->hpk_cookie);
160 rc = mdt_hsm_coordinator_update(info, hpk);
165 int mdt_hsm_ct_register(struct mdt_thread_info *info)
167 struct mdt_body *body;
168 struct ptlrpc_request *req = mdt_info_req(info);
173 body = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
177 archives = req_capsule_client_get(info->mti_pill, &RMF_MDS_HSM_ARCHIVE);
178 if (archives == NULL)
181 /* XXX: directly include this function here? */
182 rc = mdt_hsm_agent_register_mask(info, &req->rq_export->exp_client_uuid,
188 int mdt_hsm_ct_unregister(struct mdt_thread_info *info)
190 struct mdt_body *body;
191 struct ptlrpc_request *req = mdt_info_req(info);
195 body = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
199 /* XXX: directly include this function here? */
200 rc = mdt_hsm_agent_unregister(info, &req->rq_export->exp_client_uuid);
206 * Retrieve the current HSM flags, archive id and undergoing HSM requests for
207 * the fid provided in RPC body.
209 * Current requests are read from coordinator states.
211 * This is MDS_HSM_STATE_GET RPC handler.
213 int mdt_hsm_state_get(struct mdt_thread_info *info)
215 struct mdt_object *obj = info->mti_object;
216 struct md_attr *ma = &info->mti_attr;
217 struct hsm_user_state *hus;
218 struct mdt_lock_handle *lh;
222 lh = &info->mti_lh[MDT_LH_CHILD];
223 mdt_lock_reg_init(lh, LCK_PR);
224 rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP,
229 /* Only valid if client is remote */
230 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
232 GOTO(out_unlock, rc = err_serious(rc));
235 ma->ma_need = MA_HSM;
236 rc = mdt_attr_get_complex(info, obj, ma);
240 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
241 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
242 req_capsule_client_get(info->mti_pill, &RMF_CAPA1));
244 hus = req_capsule_server_get(info->mti_pill, &RMF_HSM_USER_STATE);
246 GOTO(out_ucred, rc = -EPROTO);
248 /* Current HSM flags */
249 hus->hus_states = ma->ma_hsm.mh_flags;
250 hus->hus_archive_id = ma->ma_hsm.mh_arch_id;
254 mdt_exit_ucred(info);
256 mdt_object_unlock(info, obj, lh, 1);
261 * Change HSM state and archive number of a file.
263 * Archive number is changed iif the value is not 0.
264 * The new flagset that will be computed should result in a coherent state.
265 * This function checks that are flags are compatible.
267 * This is MDS_HSM_STATE_SET RPC handler.
269 int mdt_hsm_state_set(struct mdt_thread_info *info)
271 struct mdt_object *obj = info->mti_object;
272 struct md_attr *ma = &info->mti_attr;
273 struct hsm_state_set *hss;
274 struct mdt_lock_handle *lh;
279 lh = &info->mti_lh[MDT_LH_CHILD];
280 mdt_lock_reg_init(lh, LCK_PW);
281 rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP,
286 /* Only valid if client is remote */
287 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
289 GOTO(out_obj, rc = err_serious(rc));
291 /* Read current HSM info */
293 ma->ma_need = MA_HSM;
294 rc = mdt_attr_get_complex(info, obj, ma);
298 hss = req_capsule_client_get(info->mti_pill, &RMF_HSM_STATE_SET);
300 GOTO(out_ucred, rc = -EPROTO);
302 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
303 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
304 req_capsule_client_get(info->mti_pill, &RMF_CAPA1));
306 /* Change HSM flags depending on provided masks */
307 if (hss->hss_valid & HSS_SETMASK)
308 ma->ma_hsm.mh_flags |= hss->hss_setmask;
309 if (hss->hss_valid & HSS_CLEARMASK)
310 ma->ma_hsm.mh_flags &= ~hss->hss_clearmask;
312 /* Change archive_id if provided. */
313 if (hss->hss_valid & HSS_ARCHIVE_ID) {
314 if (!(ma->ma_hsm.mh_flags & HS_EXISTS)) {
315 CDEBUG(D_HSM, "Could not set an archive number for "
316 DFID "if HSM EXISTS flag is not set.\n",
317 PFID(&info->mti_body->fid1));
320 ma->ma_hsm.mh_arch_id = hss->hss_archive_id;
323 /* Check for inconsistant HSM flagset.
324 * DIRTY without EXISTS: no dirty if no archive was created.
325 * DIRTY and RELEASED: a dirty file could not be released.
326 * RELEASED without ARCHIVED: do not release a non-archived file.
327 * LOST without ARCHIVED: cannot lost a non-archived file.
329 flags = ma->ma_hsm.mh_flags;
330 if (((flags & HS_DIRTY) && !(flags & HS_EXISTS)) ||
331 ((flags & HS_RELEASED) && (flags & HS_DIRTY)) ||
332 ((flags & HS_RELEASED) && !(flags & HS_ARCHIVED)) ||
333 ((flags & HS_LOST) && !(flags & HS_ARCHIVED))) {
334 CDEBUG(D_HSM, "Incompatible flag change on "DFID
336 PFID(&info->mti_body->fid1), flags);
337 GOTO(out_ucred, rc = -EINVAL);
340 /* Save the modified flags */
341 rc = mdt_hsm_attr_set(info, obj, &ma->ma_hsm);
348 mdt_exit_ucred(info);
350 mdt_object_unlock(info, obj, lh, 1);
355 * Retrieve undergoing HSM requests for the fid provided in RPC body.
356 * Current requests are read from coordinator states.
358 * This is MDS_HSM_ACTION RPC handler.
360 int mdt_hsm_action(struct mdt_thread_info *info)
362 struct hsm_current_action *hca;
363 struct hsm_action_list *hal = NULL;
364 struct hsm_action_item *hai;
369 /* Only valid if client is remote */
370 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
372 RETURN(rc = err_serious(rc));
374 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
375 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
376 req_capsule_client_get(info->mti_pill,
379 hca = req_capsule_server_get(info->mti_pill,
380 &RMF_MDS_HSM_CURRENT_ACTION);
382 GOTO(out_ucred, rc = -EPROTO);
384 /* Coordinator information */
385 hal_size = sizeof(*hal) +
386 cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
387 cfs_size_round(sizeof(*hai));
389 MDT_HSM_ALLOC(hal, hal_size);
391 GOTO(out_ucred, rc = -ENOMEM);
393 hal->hal_version = HAL_VERSION;
394 hal->hal_archive_id = 0;
396 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
400 hai->hai_action = HSMA_NONE;
403 hai->hai_fid = info->mti_body->fid1;
404 hai->hai_len = sizeof(*hai);
406 rc = mdt_hsm_coordinator_get_actions(info, hal);
410 /* cookie is used to give back request status */
411 if (hai->hai_cookie == 0)
412 hca->hca_state = HPS_WAITING;
414 hca->hca_state = HPS_RUNNING;
416 switch (hai->hai_action) {
418 hca->hca_action = HUA_NONE;
421 hca->hca_action = HUA_ARCHIVE;
424 hca->hca_action = HUA_RESTORE;
427 hca->hca_action = HUA_REMOVE;
430 hca->hca_action = HUA_CANCEL;
433 hca->hca_action = HUA_NONE;
434 CERROR("%s: Unknown hsm action: %d on "DFID"\n",
435 mdt_obd_name(info->mti_mdt),
436 hai->hai_action, PFID(&hai->hai_fid));
440 hca->hca_location = hai->hai_extent;
444 MDT_HSM_FREE(hal, hal_size);
446 mdt_exit_ucred(info);
451 * Process the HSM actions described in a struct hsm_user_request.
453 * The action described in hur will be send to coordinator to be saved and
454 * processed later or either handled directly if hur.hur_action is HUA_RELEASE.
456 * This is MDS_HSM_REQUEST RPC handler.
458 int mdt_hsm_request(struct mdt_thread_info *info)
460 struct req_capsule *pill = info->mti_pill;
461 struct mdt_body *body;
462 struct hsm_request *hr;
463 struct hsm_user_item *hui;
464 struct hsm_action_list *hal;
465 struct hsm_action_item *hai;
469 enum hsm_copytool_action action = HSMA_NONE;
474 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
475 hr = req_capsule_client_get(pill, &RMF_MDS_HSM_REQUEST);
476 hui = req_capsule_client_get(pill, &RMF_MDS_HSM_USER_ITEM);
477 data = req_capsule_client_get(pill, &RMF_GENERIC_DATA);
479 if (body == NULL || hr == NULL || hui == NULL || data == NULL)
482 /* Sanity check. Nothing to do with an empty list */
483 if (hr->hr_itemcount == 0)
486 hui_list_size = req_capsule_get_size(pill, &RMF_MDS_HSM_USER_ITEM,
488 if (hui_list_size < hr->hr_itemcount * sizeof(*hui))
491 data_size = req_capsule_get_size(pill, &RMF_GENERIC_DATA, RCL_CLIENT);
492 if (data_size != hr->hr_data_len)
495 /* Only valid if client is remote */
496 rc = mdt_init_ucred(info, body);
498 RETURN(err_serious(rc));
500 switch (hr->hr_action) {
501 /* code to be removed in hsm1_merge and final patch */
503 CERROR("Release action is not working in hsm1_coord\n");
504 GOTO(out_ucred, rc = -EINVAL);
506 /* end of code to be removed */
508 action = HSMA_ARCHIVE;
511 action = HSMA_RESTORE;
514 action = HSMA_REMOVE;
517 action = HSMA_CANCEL;
520 CERROR("Unknown hsm action: %d\n", hr->hr_action);
521 GOTO(out_ucred, rc = -EINVAL);
524 hal_size = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
525 (sizeof(*hai) + cfs_size_round(hr->hr_data_len)) *
528 MDT_HSM_ALLOC(hal, hal_size);
530 GOTO(out_ucred, rc = -ENOMEM);
532 hal->hal_version = HAL_VERSION;
533 hal->hal_archive_id = hr->hr_archive_id;
534 hal->hal_flags = hr->hr_flags;
535 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
538 hal->hal_count = hr->hr_itemcount;
540 for (i = 0; i < hr->hr_itemcount; i++) {
541 hai->hai_action = action;
544 hai->hai_fid = hui[i].hui_fid;
545 hai->hai_extent = hui[i].hui_extent;
546 memcpy(hai->hai_data, data, hr->hr_data_len);
547 hai->hai_len = sizeof(*hai) + hr->hr_data_len;
551 rc = mdt_hsm_coordinator_actions(info, hal, &compound_id, 0);
552 /* ENODATA error code is needed only for implicit requests */
556 MDT_HSM_FREE(hal, hal_size);
559 mdt_exit_ucred(info);