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"
45 * fake functions, will be replace by real one with HSM Coordinator patch
48 int mdt_hsm_copytool_send(struct obd_export *exp)
53 static int mdt_hsm_coordinator_update(struct mdt_thread_info *info,
54 struct hsm_progress_kernel *pgs)
59 static int mdt_hsm_agent_register_mask(struct mdt_thread_info *info,
60 struct obd_uuid *uuid,
66 static int mdt_hsm_agent_unregister(struct mdt_thread_info *info,
67 struct obd_uuid *uuid)
72 static int mdt_hsm_coordinator_get_actions(struct mdt_thread_info *mti,
73 struct hsm_action_list *hal)
78 static int mdt_hsm_coordinator_actions(struct mdt_thread_info *info,
79 struct hsm_action_list *hal,
81 int mti_attr_is_valid)
87 * Update on-disk HSM attributes.
89 int mdt_hsm_attr_set(struct mdt_thread_info *info, struct mdt_object *obj,
92 struct md_object *next = mdt_object_child(obj);
93 struct lu_buf *buf = &info->mti_buf;
94 struct hsm_attrs *attrs;
98 attrs = (struct hsm_attrs *)info->mti_xattr_buf;
99 CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*attrs));
101 /* pack HSM attributes */
102 lustre_hsm2buf(info->mti_xattr_buf, mh);
104 /* update SOM attributes */
106 buf->lb_len = sizeof(*attrs);
107 rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_HSM, 0);
113 * Extract information coming from a copytool and asks coordinator to update
114 * a request status depending on the update content.
116 * Copytools could use this to report failure in their process.
118 * This is HSM_PROGRESS RPC handler.
120 int mdt_hsm_progress(struct mdt_thread_info *info)
122 struct hsm_progress_kernel *hpk;
126 hpk = req_capsule_client_get(info->mti_pill, &RMF_MDS_HSM_PROGRESS);
129 CDEBUG(D_HSM, "Progress on "DFID": len="LPU64" err=%d\n",
130 PFID(&hpk->hpk_fid), hpk->hpk_extent.length, hpk->hpk_errval);
133 CDEBUG(D_HSM, "Copytool progress on "DFID" failed (%d); %s.\n",
134 PFID(&hpk->hpk_fid), hpk->hpk_errval,
135 hpk->hpk_flags & HP_FLAG_RETRY ? "will retry" : "fatal");
137 if (hpk->hpk_flags & HP_FLAG_COMPLETED)
138 CDEBUG(D_HSM, "Finished "DFID" (%d) cancel cookie="LPX64"\n",
139 PFID(&hpk->hpk_fid), hpk->hpk_errval, hpk->hpk_cookie);
141 rc = mdt_hsm_coordinator_update(info, hpk);
146 int mdt_hsm_ct_register(struct mdt_thread_info *info)
148 struct ptlrpc_request *req = mdt_info_req(info);
153 archives = req_capsule_client_get(info->mti_pill, &RMF_MDS_HSM_ARCHIVE);
156 /* XXX: directly include this function here? */
157 rc = mdt_hsm_agent_register_mask(info, &req->rq_export->exp_client_uuid,
163 int mdt_hsm_ct_unregister(struct mdt_thread_info *info)
165 struct ptlrpc_request *req = mdt_info_req(info);
169 /* XXX: directly include this function here? */
170 rc = mdt_hsm_agent_unregister(info, &req->rq_export->exp_client_uuid);
177 * Retrieve the current HSM flags, archive id and undergoing HSM requests for
178 * the fid provided in RPC body.
180 * Current requests are read from coordinator states.
182 * This is MDS_HSM_STATE_GET RPC handler.
184 int mdt_hsm_state_get(struct mdt_thread_info *info)
186 struct mdt_object *obj = info->mti_object;
187 struct md_attr *ma = &info->mti_attr;
188 struct hsm_user_state *hus;
189 struct mdt_lock_handle *lh;
193 lh = &info->mti_lh[MDT_LH_CHILD];
194 mdt_lock_reg_init(lh, LCK_PR);
195 rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP,
200 /* Only valid if client is remote */
201 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
203 GOTO(out_unlock, rc = err_serious(rc));
206 ma->ma_need = MA_HSM;
207 rc = mdt_attr_get_complex(info, obj, ma);
211 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
212 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
213 req_capsule_client_get(info->mti_pill, &RMF_CAPA1));
215 hus = req_capsule_server_get(info->mti_pill, &RMF_HSM_USER_STATE);
218 /* Current HSM flags */
219 hus->hus_states = ma->ma_hsm.mh_flags;
220 hus->hus_archive_id = ma->ma_hsm.mh_arch_id;
224 mdt_exit_ucred(info);
226 mdt_object_unlock(info, obj, lh, 1);
231 * Change HSM state and archive number of a file.
233 * Archive number is changed iif the value is not 0.
234 * The new flagset that will be computed should result in a coherent state.
235 * This function checks that are flags are compatible.
237 * This is MDS_HSM_STATE_SET RPC handler.
239 int mdt_hsm_state_set(struct mdt_thread_info *info)
241 struct mdt_object *obj = info->mti_object;
242 struct md_attr *ma = &info->mti_attr;
243 struct hsm_state_set *hss;
244 struct mdt_lock_handle *lh;
249 lh = &info->mti_lh[MDT_LH_CHILD];
250 mdt_lock_reg_init(lh, LCK_PW);
251 rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP,
256 /* Only valid if client is remote */
257 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
259 GOTO(out_obj, rc = err_serious(rc));
261 /* Read current HSM info */
263 ma->ma_need = MA_HSM;
264 rc = mdt_attr_get_complex(info, obj, ma);
268 hss = req_capsule_client_get(info->mti_pill, &RMF_HSM_STATE_SET);
271 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
272 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
273 req_capsule_client_get(info->mti_pill, &RMF_CAPA1));
275 /* Change HSM flags depending on provided masks */
276 if (hss->hss_valid & HSS_SETMASK)
277 ma->ma_hsm.mh_flags |= hss->hss_setmask;
278 if (hss->hss_valid & HSS_CLEARMASK)
279 ma->ma_hsm.mh_flags &= ~hss->hss_clearmask;
281 /* Change archive_id if provided. */
282 if (hss->hss_valid & HSS_ARCHIVE_ID) {
283 if (!(ma->ma_hsm.mh_flags & HS_EXISTS)) {
284 CDEBUG(D_HSM, "Could not set an archive number for "
285 DFID "if HSM EXISTS flag is not set.\n",
286 PFID(&info->mti_body->fid1));
289 ma->ma_hsm.mh_arch_id = hss->hss_archive_id;
292 /* Check for inconsistant HSM flagset.
293 * DIRTY without EXISTS: no dirty if no archive was created.
294 * DIRTY and RELEASED: a dirty file could not be released.
295 * RELEASED without ARCHIVED: do not release a non-archived file.
296 * LOST without ARCHIVED: cannot lost a non-archived file.
298 flags = ma->ma_hsm.mh_flags;
299 if (((flags & HS_DIRTY) && !(flags & HS_EXISTS)) ||
300 ((flags & HS_RELEASED) && (flags & HS_DIRTY)) ||
301 ((flags & HS_RELEASED) && !(flags & HS_ARCHIVED)) ||
302 ((flags & HS_LOST) && !(flags & HS_ARCHIVED))) {
303 CDEBUG(D_HSM, "Incompatible flag change on "DFID
305 PFID(&info->mti_body->fid1), flags);
306 GOTO(out_ucred, rc = -EINVAL);
309 /* Save the modified flags */
310 rc = mdt_hsm_attr_set(info, obj, &ma->ma_hsm);
317 mdt_exit_ucred(info);
319 mdt_object_unlock(info, obj, lh, 1);
324 * Retrieve undergoing HSM requests for the fid provided in RPC body.
325 * Current requests are read from coordinator states.
327 * This is MDS_HSM_ACTION RPC handler.
329 int mdt_hsm_action(struct mdt_thread_info *info)
331 struct hsm_current_action *hca;
332 struct hsm_action_list *hal = NULL;
333 struct hsm_action_item *hai;
337 /* Only valid if client is remote */
338 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
340 RETURN(rc = err_serious(rc));
342 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
343 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
344 req_capsule_client_get(info->mti_pill,
347 hca = req_capsule_server_get(info->mti_pill,
348 &RMF_MDS_HSM_CURRENT_ACTION);
351 /* Coordinator information */
352 len = sizeof(*hal) + MTI_NAME_MAXLEN /* fsname */ +
353 cfs_size_round(sizeof(*hai));
357 GOTO(out_ucred, -ENOMEM);
359 hal->hal_version = HAL_VERSION;
360 hal->hal_archive_id = 0;
362 obd_uuid2fsname(hal->hal_fsname, mdt2obd_dev(info->mti_mdt)->obd_name,
366 hai->hai_action = HSMA_NONE;
369 hai->hai_fid = info->mti_body->fid1;
370 hai->hai_len = sizeof(*hai);
372 rc = mdt_hsm_coordinator_get_actions(info, hal);
376 /* cookie is used to give back request status */
377 if (hai->hai_cookie == 0)
378 hca->hca_state = HPS_WAITING;
380 hca->hca_state = HPS_RUNNING;
382 switch (hai->hai_action) {
384 hca->hca_action = HUA_NONE;
387 hca->hca_action = HUA_ARCHIVE;
390 hca->hca_action = HUA_RESTORE;
393 hca->hca_action = HUA_REMOVE;
396 hca->hca_action = HUA_CANCEL;
399 hca->hca_action = HUA_NONE;
400 CERROR("%s: Unknown hsm action: %d on "DFID"\n",
401 mdt2obd_dev(info->mti_mdt)->obd_name,
402 hai->hai_action, PFID(&hai->hai_fid));
406 hca->hca_location = hai->hai_extent;
412 mdt_exit_ucred(info);
417 * Process the HSM actions described in a struct hsm_user_request.
419 * The action described in hur will be send to coordinator to be saved and
420 * processed later or either handled directly if hur.hur_action is HUA_RELEASE.
422 * This is MDS_HSM_REQUEST RPC handler.
424 int mdt_hsm_request(struct mdt_thread_info *info)
426 struct req_capsule *pill = info->mti_pill;
427 struct mdt_body *body;
428 struct hsm_request *hr;
429 struct hsm_user_item *hui;
430 struct hsm_action_list *hal;
431 struct hsm_action_item *hai;
433 enum hsm_copytool_action action = HSMA_NONE;
438 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
441 hr = req_capsule_client_get(pill, &RMF_MDS_HSM_REQUEST);
444 hui = req_capsule_client_get(pill, &RMF_MDS_HSM_USER_ITEM);
447 opaque = req_capsule_client_get(pill, &RMF_GENERIC_DATA);
450 /* Sanity check. Nothing to do with an empty list */
451 if (hr->hr_itemcount == 0)
454 /* Only valid if client is remote */
455 rc = mdt_init_ucred(info, body);
457 RETURN(err_serious(rc));
459 switch (hr->hr_action) {
460 /* code to be removed in hsm1_merge and final patch */
462 CERROR("Release action is not working in hsm1_coord\n");
463 GOTO(out_ucred, rc = -EINVAL);
465 /* end of code to be removed */
467 action = HSMA_ARCHIVE;
470 action = HSMA_RESTORE;
473 action = HSMA_REMOVE;
476 action = HSMA_CANCEL;
479 CERROR("Unknown hsm action: %d\n", hr->hr_action);
480 GOTO(out_ucred, rc = -EINVAL);
483 len = sizeof(*hal) + MTI_NAME_MAXLEN /* fsname */ +
484 cfs_size_round(sizeof(*hai) * hr->hr_itemcount) +
485 cfs_size_round(hr->hr_data_len * hr->hr_itemcount);
489 GOTO(out_ucred, rc = -ENOMEM);
491 hal->hal_version = HAL_VERSION;
492 hal->hal_archive_id = hr->hr_archive_id;
493 hal->hal_flags = hr->hr_flags;
494 obd_uuid2fsname(hal->hal_fsname, mdt2obd_dev(info->mti_mdt)->obd_name,
497 hal->hal_count = hr->hr_itemcount;
499 for (i = 0; i < hr->hr_itemcount; i++) {
500 hai->hai_action = action;
503 hai->hai_fid = hui[i].hui_fid;
504 hai->hai_extent = hui[i].hui_extent;
505 memcpy(hai->hai_data, opaque, hr->hr_data_len);
506 hai->hai_len = sizeof(*hai) + hr->hr_data_len;
510 rc = mdt_hsm_coordinator_actions(info, hal, &compound_id, 0);
511 /* ENODATA error code is needed only for implicit requests */
518 mdt_exit_ucred(info);