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>
37 #define DEBUG_SUBSYSTEM S_MDS
39 #include "mdt_internal.h"
41 /* Max allocation to satisfy single HSM RPC. */
42 #define MDT_HSM_ALLOC_MAX (1 << 20)
44 #define MDT_HSM_ALLOC(ptr, size) \
46 if ((size) <= MDT_HSM_ALLOC_MAX) \
47 OBD_ALLOC_LARGE((ptr), (size)); \
52 #define MDT_HSM_FREE(ptr, size) OBD_FREE_LARGE((ptr), (size))
55 * fake functions, will be replaced by real one with HSM Coordinator patch
58 int mdt_hsm_copytool_send(struct obd_export *exp)
63 static int mdt_hsm_coordinator_update(struct mdt_thread_info *info,
64 struct hsm_progress_kernel *pgs)
69 static int mdt_hsm_agent_register_mask(struct mdt_thread_info *info,
70 struct obd_uuid *uuid,
76 static int mdt_hsm_agent_unregister(struct mdt_thread_info *info,
77 struct obd_uuid *uuid)
83 * Update on-disk HSM attributes.
85 int mdt_hsm_attr_set(struct mdt_thread_info *info, struct mdt_object *obj,
88 struct md_object *next = mdt_object_child(obj);
89 struct lu_buf *buf = &info->mti_buf;
90 struct hsm_attrs *attrs;
94 attrs = (struct hsm_attrs *)info->mti_xattr_buf;
95 CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*attrs));
97 /* pack HSM attributes */
98 lustre_hsm2buf(info->mti_xattr_buf, mh);
100 /* update SOM attributes */
102 buf->lb_len = sizeof(*attrs);
103 rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_HSM, 0);
109 * Extract information coming from a copytool and asks coordinator to update
110 * a request status depending on the update content.
112 * Copytools could use this to report failure in their process.
114 * This is HSM_PROGRESS RPC handler.
116 int mdt_hsm_progress(struct mdt_thread_info *info)
118 struct mdt_body *body;
119 struct hsm_progress_kernel *hpk;
123 body = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
127 hpk = req_capsule_client_get(info->mti_pill, &RMF_MDS_HSM_PROGRESS);
131 hpk->hpk_errval = lustre_errno_ntoh(hpk->hpk_errval);
133 CDEBUG(D_HSM, "Progress on "DFID": len="LPU64" err=%d\n",
134 PFID(&hpk->hpk_fid), hpk->hpk_extent.length, hpk->hpk_errval);
137 CDEBUG(D_HSM, "Copytool progress on "DFID" failed (%d); %s.\n",
138 PFID(&hpk->hpk_fid), hpk->hpk_errval,
139 hpk->hpk_flags & HP_FLAG_RETRY ? "will retry" : "fatal");
141 if (hpk->hpk_flags & HP_FLAG_COMPLETED)
142 CDEBUG(D_HSM, "Finished "DFID" (%d) cancel cookie="LPX64"\n",
143 PFID(&hpk->hpk_fid), hpk->hpk_errval, hpk->hpk_cookie);
145 rc = mdt_hsm_coordinator_update(info, hpk);
150 int mdt_hsm_ct_register(struct mdt_thread_info *info)
152 struct mdt_body *body;
153 struct ptlrpc_request *req = mdt_info_req(info);
158 body = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
162 archives = req_capsule_client_get(info->mti_pill, &RMF_MDS_HSM_ARCHIVE);
163 if (archives == NULL)
166 /* XXX: directly include this function here? */
167 rc = mdt_hsm_agent_register_mask(info, &req->rq_export->exp_client_uuid,
173 int mdt_hsm_ct_unregister(struct mdt_thread_info *info)
175 struct mdt_body *body;
176 struct ptlrpc_request *req = mdt_info_req(info);
180 body = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
184 /* XXX: directly include this function here? */
185 rc = mdt_hsm_agent_unregister(info, &req->rq_export->exp_client_uuid);
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 mdt_thread_info *info)
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 lh = &info->mti_lh[MDT_LH_CHILD];
208 mdt_lock_reg_init(lh, LCK_PR);
209 rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP,
214 /* Only valid if client is remote */
215 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
217 GOTO(out_unlock, rc = err_serious(rc));
220 ma->ma_need = MA_HSM;
221 rc = mdt_attr_get_complex(info, obj, ma);
225 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
226 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
227 req_capsule_client_get(info->mti_pill, &RMF_CAPA1));
229 hus = req_capsule_server_get(info->mti_pill, &RMF_HSM_USER_STATE);
231 GOTO(out_ucred, rc = -EPROTO);
233 /* Current HSM flags */
234 hus->hus_states = ma->ma_hsm.mh_flags;
235 hus->hus_archive_id = ma->ma_hsm.mh_arch_id;
239 mdt_exit_ucred(info);
241 mdt_object_unlock(info, obj, lh, 1);
246 * Change HSM state and archive number of a file.
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 are flags are compatible.
252 * This is MDS_HSM_STATE_SET RPC handler.
254 int mdt_hsm_state_set(struct mdt_thread_info *info)
256 struct mdt_object *obj = info->mti_object;
257 struct md_attr *ma = &info->mti_attr;
258 struct hsm_state_set *hss;
259 struct mdt_lock_handle *lh;
264 lh = &info->mti_lh[MDT_LH_CHILD];
265 mdt_lock_reg_init(lh, LCK_PW);
266 rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP,
271 /* Only valid if client is remote */
272 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
274 GOTO(out_obj, rc = err_serious(rc));
276 /* Read current HSM info */
278 ma->ma_need = MA_HSM;
279 rc = mdt_attr_get_complex(info, obj, ma);
283 hss = req_capsule_client_get(info->mti_pill, &RMF_HSM_STATE_SET);
285 GOTO(out_ucred, rc = -EPROTO);
287 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
288 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
289 req_capsule_client_get(info->mti_pill, &RMF_CAPA1));
291 /* Change HSM flags depending on provided masks */
292 if (hss->hss_valid & HSS_SETMASK)
293 ma->ma_hsm.mh_flags |= hss->hss_setmask;
294 if (hss->hss_valid & HSS_CLEARMASK)
295 ma->ma_hsm.mh_flags &= ~hss->hss_clearmask;
297 /* Change archive_id if provided. */
298 if (hss->hss_valid & HSS_ARCHIVE_ID) {
299 if (!(ma->ma_hsm.mh_flags & HS_EXISTS)) {
300 CDEBUG(D_HSM, "Could not set an archive number for "
301 DFID "if HSM EXISTS flag is not set.\n",
302 PFID(&info->mti_body->fid1));
305 ma->ma_hsm.mh_arch_id = hss->hss_archive_id;
308 /* Check for inconsistant HSM flagset.
309 * DIRTY without EXISTS: no dirty if no archive was created.
310 * DIRTY and RELEASED: a dirty file could not be released.
311 * RELEASED without ARCHIVED: do not release a non-archived file.
312 * LOST without ARCHIVED: cannot lost a non-archived file.
314 flags = ma->ma_hsm.mh_flags;
315 if (((flags & HS_DIRTY) && !(flags & HS_EXISTS)) ||
316 ((flags & HS_RELEASED) && (flags & HS_DIRTY)) ||
317 ((flags & HS_RELEASED) && !(flags & HS_ARCHIVED)) ||
318 ((flags & HS_LOST) && !(flags & HS_ARCHIVED))) {
319 CDEBUG(D_HSM, "Incompatible flag change on "DFID
321 PFID(&info->mti_body->fid1), flags);
322 GOTO(out_ucred, rc = -EINVAL);
325 /* Save the modified flags */
326 rc = mdt_hsm_attr_set(info, obj, &ma->ma_hsm);
333 mdt_exit_ucred(info);
335 mdt_object_unlock(info, obj, lh, 1);
340 * Retrieve undergoing HSM requests for the fid provided in RPC body.
341 * Current requests are read from coordinator states.
343 * This is MDS_HSM_ACTION RPC handler.
345 int mdt_hsm_action(struct mdt_thread_info *info)
347 struct hsm_current_action *hca;
348 struct hsm_action_list *hal = NULL;
349 struct hsm_action_item *hai;
354 /* Only valid if client is remote */
355 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
357 RETURN(rc = err_serious(rc));
359 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
360 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
361 req_capsule_client_get(info->mti_pill,
364 hca = req_capsule_server_get(info->mti_pill,
365 &RMF_MDS_HSM_CURRENT_ACTION);
367 GOTO(out_ucred, rc = -EPROTO);
369 /* Coordinator information */
370 hal_size = sizeof(*hal) +
371 cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
372 cfs_size_round(sizeof(*hai));
374 MDT_HSM_ALLOC(hal, hal_size);
376 GOTO(out_ucred, rc = -ENOMEM);
378 hal->hal_version = HAL_VERSION;
379 hal->hal_archive_id = 0;
381 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
385 hai->hai_action = HSMA_NONE;
388 hai->hai_fid = info->mti_body->fid1;
389 hai->hai_len = sizeof(*hai);
391 rc = mdt_hsm_get_actions(info, hal);
395 /* cookie is used to give back request status */
396 if (hai->hai_cookie == 0)
397 hca->hca_state = HPS_WAITING;
399 hca->hca_state = HPS_RUNNING;
401 switch (hai->hai_action) {
403 hca->hca_action = HUA_NONE;
406 hca->hca_action = HUA_ARCHIVE;
409 hca->hca_action = HUA_RESTORE;
412 hca->hca_action = HUA_REMOVE;
415 hca->hca_action = HUA_CANCEL;
418 hca->hca_action = HUA_NONE;
419 CERROR("%s: Unknown hsm action: %d on "DFID"\n",
420 mdt_obd_name(info->mti_mdt),
421 hai->hai_action, PFID(&hai->hai_fid));
425 hca->hca_location = hai->hai_extent;
429 MDT_HSM_FREE(hal, hal_size);
431 mdt_exit_ucred(info);
436 * Process the HSM actions described in a struct hsm_user_request.
438 * The action described in hur will be send to coordinator to be saved and
439 * processed later or either handled directly if hur.hur_action is HUA_RELEASE.
441 * This is MDS_HSM_REQUEST RPC handler.
443 int mdt_hsm_request(struct mdt_thread_info *info)
445 struct req_capsule *pill = info->mti_pill;
446 struct mdt_body *body;
447 struct hsm_request *hr;
448 struct hsm_user_item *hui;
449 struct hsm_action_list *hal;
450 struct hsm_action_item *hai;
454 enum hsm_copytool_action action = HSMA_NONE;
459 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
460 hr = req_capsule_client_get(pill, &RMF_MDS_HSM_REQUEST);
461 hui = req_capsule_client_get(pill, &RMF_MDS_HSM_USER_ITEM);
462 data = req_capsule_client_get(pill, &RMF_GENERIC_DATA);
464 if (body == NULL || hr == NULL || hui == NULL || data == NULL)
467 /* Sanity check. Nothing to do with an empty list */
468 if (hr->hr_itemcount == 0)
471 hui_list_size = req_capsule_get_size(pill, &RMF_MDS_HSM_USER_ITEM,
473 if (hui_list_size < hr->hr_itemcount * sizeof(*hui))
476 data_size = req_capsule_get_size(pill, &RMF_GENERIC_DATA, RCL_CLIENT);
477 if (data_size != hr->hr_data_len)
480 /* Only valid if client is remote */
481 rc = mdt_init_ucred(info, body);
483 RETURN(err_serious(rc));
485 switch (hr->hr_action) {
486 /* code to be removed in hsm1_merge and final patch */
488 CERROR("Release action is not working in hsm1_coord\n");
489 GOTO(out_ucred, rc = -EINVAL);
491 /* end of code to be removed */
493 action = HSMA_ARCHIVE;
496 action = HSMA_RESTORE;
499 action = HSMA_REMOVE;
502 action = HSMA_CANCEL;
505 CERROR("Unknown hsm action: %d\n", hr->hr_action);
506 GOTO(out_ucred, rc = -EINVAL);
509 hal_size = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
510 (sizeof(*hai) + cfs_size_round(hr->hr_data_len)) *
513 MDT_HSM_ALLOC(hal, hal_size);
515 GOTO(out_ucred, rc = -ENOMEM);
517 hal->hal_version = HAL_VERSION;
518 hal->hal_archive_id = hr->hr_archive_id;
519 hal->hal_flags = hr->hr_flags;
520 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
523 hal->hal_count = hr->hr_itemcount;
525 for (i = 0; i < hr->hr_itemcount; i++, hai = hai_next(hai)) {
526 hai->hai_action = action;
529 hai->hai_fid = hui[i].hui_fid;
530 hai->hai_extent = hui[i].hui_extent;
531 memcpy(hai->hai_data, data, hr->hr_data_len);
532 hai->hai_len = sizeof(*hai) + hr->hr_data_len;
535 rc = mdt_hsm_add_actions(info, hal, &compound_id);
536 /* ENODATA error code is needed only for implicit requests */
540 MDT_HSM_FREE(hal, hal_size);
545 mdt_exit_ucred(info);