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)
82 static int mdt_hsm_coordinator_get_actions(struct mdt_thread_info *mti,
83 struct hsm_action_list *hal)
88 static int mdt_hsm_coordinator_actions(struct mdt_thread_info *info,
89 struct hsm_action_list *hal,
91 int mti_attr_is_valid)
97 * Update on-disk HSM attributes.
99 int mdt_hsm_attr_set(struct mdt_thread_info *info, struct mdt_object *obj,
102 struct md_object *next = mdt_object_child(obj);
103 struct lu_buf *buf = &info->mti_buf;
104 struct hsm_attrs *attrs;
108 attrs = (struct hsm_attrs *)info->mti_xattr_buf;
109 CLASSERT(sizeof(info->mti_xattr_buf) >= sizeof(*attrs));
111 /* pack HSM attributes */
112 lustre_hsm2buf(info->mti_xattr_buf, mh);
114 /* update SOM attributes */
116 buf->lb_len = sizeof(*attrs);
117 rc = mo_xattr_set(info->mti_env, next, buf, XATTR_NAME_HSM, 0);
123 * Extract information coming from a copytool and asks coordinator to update
124 * a request status depending on the update content.
126 * Copytools could use this to report failure in their process.
128 * This is HSM_PROGRESS RPC handler.
130 int mdt_hsm_progress(struct mdt_thread_info *info)
132 struct mdt_body *body;
133 struct hsm_progress_kernel *hpk;
137 body = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
141 hpk = req_capsule_client_get(info->mti_pill, &RMF_MDS_HSM_PROGRESS);
145 hpk->hpk_errval = lustre_errno_ntoh(hpk->hpk_errval);
147 CDEBUG(D_HSM, "Progress on "DFID": len="LPU64" err=%d\n",
148 PFID(&hpk->hpk_fid), hpk->hpk_extent.length, hpk->hpk_errval);
151 CDEBUG(D_HSM, "Copytool progress on "DFID" failed (%d); %s.\n",
152 PFID(&hpk->hpk_fid), hpk->hpk_errval,
153 hpk->hpk_flags & HP_FLAG_RETRY ? "will retry" : "fatal");
155 if (hpk->hpk_flags & HP_FLAG_COMPLETED)
156 CDEBUG(D_HSM, "Finished "DFID" (%d) cancel cookie="LPX64"\n",
157 PFID(&hpk->hpk_fid), hpk->hpk_errval, hpk->hpk_cookie);
159 rc = mdt_hsm_coordinator_update(info, hpk);
164 int mdt_hsm_ct_register(struct mdt_thread_info *info)
166 struct mdt_body *body;
167 struct ptlrpc_request *req = mdt_info_req(info);
172 body = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
176 archives = req_capsule_client_get(info->mti_pill, &RMF_MDS_HSM_ARCHIVE);
177 if (archives == NULL)
180 /* XXX: directly include this function here? */
181 rc = mdt_hsm_agent_register_mask(info, &req->rq_export->exp_client_uuid,
187 int mdt_hsm_ct_unregister(struct mdt_thread_info *info)
189 struct mdt_body *body;
190 struct ptlrpc_request *req = mdt_info_req(info);
194 body = req_capsule_client_get(info->mti_pill, &RMF_MDT_BODY);
198 /* XXX: directly include this function here? */
199 rc = mdt_hsm_agent_unregister(info, &req->rq_export->exp_client_uuid);
205 * Retrieve the current HSM flags, archive id and undergoing HSM requests for
206 * the fid provided in RPC body.
208 * Current requests are read from coordinator states.
210 * This is MDS_HSM_STATE_GET RPC handler.
212 int mdt_hsm_state_get(struct mdt_thread_info *info)
214 struct mdt_object *obj = info->mti_object;
215 struct md_attr *ma = &info->mti_attr;
216 struct hsm_user_state *hus;
217 struct mdt_lock_handle *lh;
221 lh = &info->mti_lh[MDT_LH_CHILD];
222 mdt_lock_reg_init(lh, LCK_PR);
223 rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP,
228 /* Only valid if client is remote */
229 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
231 GOTO(out_unlock, rc = err_serious(rc));
234 ma->ma_need = MA_HSM;
235 rc = mdt_attr_get_complex(info, obj, ma);
239 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
240 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
241 req_capsule_client_get(info->mti_pill, &RMF_CAPA1));
243 hus = req_capsule_server_get(info->mti_pill, &RMF_HSM_USER_STATE);
245 GOTO(out_ucred, rc = -EPROTO);
247 /* Current HSM flags */
248 hus->hus_states = ma->ma_hsm.mh_flags;
249 hus->hus_archive_id = ma->ma_hsm.mh_arch_id;
253 mdt_exit_ucred(info);
255 mdt_object_unlock(info, obj, lh, 1);
260 * Change HSM state and archive number of a file.
262 * Archive number is changed iif the value is not 0.
263 * The new flagset that will be computed should result in a coherent state.
264 * This function checks that are flags are compatible.
266 * This is MDS_HSM_STATE_SET RPC handler.
268 int mdt_hsm_state_set(struct mdt_thread_info *info)
270 struct mdt_object *obj = info->mti_object;
271 struct md_attr *ma = &info->mti_attr;
272 struct hsm_state_set *hss;
273 struct mdt_lock_handle *lh;
278 lh = &info->mti_lh[MDT_LH_CHILD];
279 mdt_lock_reg_init(lh, LCK_PW);
280 rc = mdt_object_lock(info, obj, lh, MDS_INODELOCK_LOOKUP,
285 /* Only valid if client is remote */
286 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
288 GOTO(out_obj, rc = err_serious(rc));
290 /* Read current HSM info */
292 ma->ma_need = MA_HSM;
293 rc = mdt_attr_get_complex(info, obj, ma);
297 hss = req_capsule_client_get(info->mti_pill, &RMF_HSM_STATE_SET);
299 GOTO(out_ucred, rc = -EPROTO);
301 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
302 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
303 req_capsule_client_get(info->mti_pill, &RMF_CAPA1));
305 /* Change HSM flags depending on provided masks */
306 if (hss->hss_valid & HSS_SETMASK)
307 ma->ma_hsm.mh_flags |= hss->hss_setmask;
308 if (hss->hss_valid & HSS_CLEARMASK)
309 ma->ma_hsm.mh_flags &= ~hss->hss_clearmask;
311 /* Change archive_id if provided. */
312 if (hss->hss_valid & HSS_ARCHIVE_ID) {
313 if (!(ma->ma_hsm.mh_flags & HS_EXISTS)) {
314 CDEBUG(D_HSM, "Could not set an archive number for "
315 DFID "if HSM EXISTS flag is not set.\n",
316 PFID(&info->mti_body->fid1));
319 ma->ma_hsm.mh_arch_id = hss->hss_archive_id;
322 /* Check for inconsistant HSM flagset.
323 * DIRTY without EXISTS: no dirty if no archive was created.
324 * DIRTY and RELEASED: a dirty file could not be released.
325 * RELEASED without ARCHIVED: do not release a non-archived file.
326 * LOST without ARCHIVED: cannot lost a non-archived file.
328 flags = ma->ma_hsm.mh_flags;
329 if (((flags & HS_DIRTY) && !(flags & HS_EXISTS)) ||
330 ((flags & HS_RELEASED) && (flags & HS_DIRTY)) ||
331 ((flags & HS_RELEASED) && !(flags & HS_ARCHIVED)) ||
332 ((flags & HS_LOST) && !(flags & HS_ARCHIVED))) {
333 CDEBUG(D_HSM, "Incompatible flag change on "DFID
335 PFID(&info->mti_body->fid1), flags);
336 GOTO(out_ucred, rc = -EINVAL);
339 /* Save the modified flags */
340 rc = mdt_hsm_attr_set(info, obj, &ma->ma_hsm);
347 mdt_exit_ucred(info);
349 mdt_object_unlock(info, obj, lh, 1);
354 * Retrieve undergoing HSM requests for the fid provided in RPC body.
355 * Current requests are read from coordinator states.
357 * This is MDS_HSM_ACTION RPC handler.
359 int mdt_hsm_action(struct mdt_thread_info *info)
361 struct hsm_current_action *hca;
362 struct hsm_action_list *hal = NULL;
363 struct hsm_action_item *hai;
368 /* Only valid if client is remote */
369 rc = mdt_init_ucred(info, (struct mdt_body *)info->mti_body);
371 RETURN(rc = err_serious(rc));
373 if (req_capsule_get_size(info->mti_pill, &RMF_CAPA1, RCL_CLIENT))
374 mdt_set_capainfo(info, 0, &info->mti_body->fid1,
375 req_capsule_client_get(info->mti_pill,
378 hca = req_capsule_server_get(info->mti_pill,
379 &RMF_MDS_HSM_CURRENT_ACTION);
381 GOTO(out_ucred, rc = -EPROTO);
383 /* Coordinator information */
384 hal_size = sizeof(*hal) +
385 cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
386 cfs_size_round(sizeof(*hai));
388 MDT_HSM_ALLOC(hal, hal_size);
390 GOTO(out_ucred, rc = -ENOMEM);
392 hal->hal_version = HAL_VERSION;
393 hal->hal_archive_id = 0;
395 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
399 hai->hai_action = HSMA_NONE;
402 hai->hai_fid = info->mti_body->fid1;
403 hai->hai_len = sizeof(*hai);
405 rc = mdt_hsm_coordinator_get_actions(info, hal);
409 /* cookie is used to give back request status */
410 if (hai->hai_cookie == 0)
411 hca->hca_state = HPS_WAITING;
413 hca->hca_state = HPS_RUNNING;
415 switch (hai->hai_action) {
417 hca->hca_action = HUA_NONE;
420 hca->hca_action = HUA_ARCHIVE;
423 hca->hca_action = HUA_RESTORE;
426 hca->hca_action = HUA_REMOVE;
429 hca->hca_action = HUA_CANCEL;
432 hca->hca_action = HUA_NONE;
433 CERROR("%s: Unknown hsm action: %d on "DFID"\n",
434 mdt_obd_name(info->mti_mdt),
435 hai->hai_action, PFID(&hai->hai_fid));
439 hca->hca_location = hai->hai_extent;
443 MDT_HSM_FREE(hal, hal_size);
445 mdt_exit_ucred(info);
450 * Process the HSM actions described in a struct hsm_user_request.
452 * The action described in hur will be send to coordinator to be saved and
453 * processed later or either handled directly if hur.hur_action is HUA_RELEASE.
455 * This is MDS_HSM_REQUEST RPC handler.
457 int mdt_hsm_request(struct mdt_thread_info *info)
459 struct req_capsule *pill = info->mti_pill;
460 struct mdt_body *body;
461 struct hsm_request *hr;
462 struct hsm_user_item *hui;
463 struct hsm_action_list *hal;
464 struct hsm_action_item *hai;
468 enum hsm_copytool_action action = HSMA_NONE;
473 body = req_capsule_client_get(pill, &RMF_MDT_BODY);
474 hr = req_capsule_client_get(pill, &RMF_MDS_HSM_REQUEST);
475 hui = req_capsule_client_get(pill, &RMF_MDS_HSM_USER_ITEM);
476 data = req_capsule_client_get(pill, &RMF_GENERIC_DATA);
478 if (body == NULL || hr == NULL || hui == NULL || data == NULL)
481 /* Sanity check. Nothing to do with an empty list */
482 if (hr->hr_itemcount == 0)
485 hui_list_size = req_capsule_get_size(pill, &RMF_MDS_HSM_USER_ITEM,
487 if (hui_list_size < hr->hr_itemcount * sizeof(*hui))
490 data_size = req_capsule_get_size(pill, &RMF_GENERIC_DATA, RCL_CLIENT);
491 if (data_size != hr->hr_data_len)
494 /* Only valid if client is remote */
495 rc = mdt_init_ucred(info, body);
497 RETURN(err_serious(rc));
499 switch (hr->hr_action) {
500 /* code to be removed in hsm1_merge and final patch */
502 CERROR("Release action is not working in hsm1_coord\n");
503 GOTO(out_ucred, rc = -EINVAL);
505 /* end of code to be removed */
507 action = HSMA_ARCHIVE;
510 action = HSMA_RESTORE;
513 action = HSMA_REMOVE;
516 action = HSMA_CANCEL;
519 CERROR("Unknown hsm action: %d\n", hr->hr_action);
520 GOTO(out_ucred, rc = -EINVAL);
523 hal_size = sizeof(*hal) + cfs_size_round(MTI_NAME_MAXLEN) /* fsname */ +
524 (sizeof(*hai) + cfs_size_round(hr->hr_data_len)) *
527 MDT_HSM_ALLOC(hal, hal_size);
529 GOTO(out_ucred, rc = -ENOMEM);
531 hal->hal_version = HAL_VERSION;
532 hal->hal_archive_id = hr->hr_archive_id;
533 hal->hal_flags = hr->hr_flags;
534 obd_uuid2fsname(hal->hal_fsname, mdt_obd_name(info->mti_mdt),
537 hal->hal_count = hr->hr_itemcount;
539 for (i = 0; i < hr->hr_itemcount; i++) {
540 hai->hai_action = action;
543 hai->hai_fid = hui[i].hui_fid;
544 hai->hai_extent = hui[i].hui_extent;
545 memcpy(hai->hai_data, data, hr->hr_data_len);
546 hai->hai_len = sizeof(*hai) + hr->hr_data_len;
550 rc = mdt_hsm_coordinator_actions(info, hal, &compound_id, 0);
551 /* ENODATA error code is needed only for implicit requests */
555 MDT_HSM_FREE(hal, hal_size);
560 mdt_exit_ucred(info);